Skip to content

Commit 9d8028f

Browse files
snejborrrden
authored andcommitted
JS Server: Normalize collection names everywhere
i.e. convert `_default._default` to `_default`
1 parent 4466ba3 commit 9d8028f

File tree

6 files changed

+32
-27
lines changed

6 files changed

+32
-27
lines changed

servers/javascript/src/keyPath.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
//
1212

1313
import type { CBLDictionary, CBLValue } from "@couchbase/lite-js";
14-
import { HTTPError } from "./httpError";
14+
import { HTTPError } from "./utils";
1515

1616
type KeyPathComponents = Array<string | number>;
1717

servers/javascript/src/snapshot.ts

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
// the file licenses/APL2.txt.
1111
//
1212

13-
import { HTTPError } from "./httpError";
13+
import { HTTPError, normalizeCollectionID } from "./utils";
1414
import { KeyPath, KeyPathCache } from "./keyPath";
1515
import type * as tdk from "./tdkSchema";
1616
import type * as cbl from "@couchbase/lite-js";
@@ -22,7 +22,7 @@ export class Snapshot {
2222

2323
/** Adds a (possibly nonexistent) document to the snapshot. */
2424
async record(collection: string, id: cbl.DocID) {
25-
const doc = await this.db.getCollection(collection).getDocument(id);
25+
const doc = await this.#getCollection(collection).getDocument(id);
2626
this.#documents.set(collection, id, doc ?? null);
2727
}
2828

@@ -42,14 +42,14 @@ export class Snapshot {
4242
let expected: cbl.CBLDocument | undefined;
4343
// Update the old document with the changes listed in the DatabaseUpdateItem:
4444
if (oldDoc) {
45-
expected = update ? this.applyUpdate(oldDoc, update) : oldDoc;
45+
expected = update ? this.#applyUpdate(oldDoc, update) : oldDoc;
4646
} else {
47-
oldDoc = this.db.getCollection(collection).createDocument(id);
48-
expected = this.applyUpdate(oldDoc, update!!);
47+
oldDoc = this.#getCollection(collection).createDocument(id);
48+
expected = this.#applyUpdate(oldDoc, update!!);
4949
}
5050
// Compare the updated oldDoc against the database's current document:
51-
const newDoc = await this.db.getCollection(collection).getDocument(id);
52-
const result = this.compareDocs(expected, newDoc, update?.type);
51+
const newDoc = await this.#getCollection(collection).getDocument(id);
52+
const result = this.#compareDocs(expected, newDoc, update?.type);
5353
if (result !== undefined) {
5454
// On failure, return the result:
5555
result.result = false;
@@ -63,9 +63,8 @@ export class Snapshot {
6363
}
6464

6565

66-
/** Applies the changes described in a `DatabaseUpdateItem` to a `CBLDocument`.
67-
* @internal (exposed for testing) */
68-
applyUpdate(doc: cbl.CBLDocument, update: tdk.DatabaseUpdateItem)
66+
/** Applies the changes described in a `DatabaseUpdateItem` to a `CBLDocument`. */
67+
#applyUpdate(doc: cbl.CBLDocument, update: tdk.DatabaseUpdateItem)
6968
: cbl.CBLDocument | undefined
7069
{
7170
if (update.type !== 'UPDATE')
@@ -96,11 +95,10 @@ export class Snapshot {
9695

9796

9897
/** Compares the expected and actual bodies of a document.
99-
* @returns `undefined` if they're equal, or a response describing the mismatch.
100-
* @internal (exposed for testing) */
101-
compareDocs(expected: cbl.CBLDocument | undefined,
102-
actual: cbl.CBLDocument | undefined,
103-
updateType: string | undefined) : tdk.VerifyDocumentsResponse | undefined {
98+
* @returns `undefined` if they're equal, or a response describing the mismatch. */
99+
#compareDocs(expected: cbl.CBLDocument | undefined,
100+
actual: cbl.CBLDocument | undefined,
101+
updateType: string | undefined) : tdk.VerifyDocumentsResponse | undefined {
104102
let response: tdk.VerifyDocumentsResponse | undefined = undefined;
105103
let path = new Array<string|number>();
106104

@@ -185,6 +183,11 @@ export class Snapshot {
185183
}
186184

187185

186+
#getCollection(name: string): cbl.Collection {
187+
return this.db.getCollection(normalizeCollectionID(name));
188+
}
189+
190+
188191
readonly #documents = new DocumentMap<cbl.CBLDocument | null>();
189192
}
190193

servers/javascript/src/tdk.ts

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
import { KeyPathCache } from "./keyPath";
1616
import { LogSlurpSender } from "./logSlurpSender";
17-
import { check, HTTPError } from "./httpError";
17+
import { check, HTTPError, normalizeCollectionID } from "./utils";
1818
import { Snapshot } from "./snapshot";
1919
import type { TestRequest } from "./testServer";
2020
import * as tdk from "./tdkSchema";
@@ -109,7 +109,7 @@ export class TDKImpl implements tdk.TDK, AsyncDisposable {
109109
const response: tdk.GetAllDocumentsResponse = {};
110110
for (const collName of rq.collections) {
111111
if (collName in db.collections) {
112-
const coll = db.getCollection(collName);
112+
const coll = db.getCollection(normalizeCollectionID(collName));
113113
const docs = new Array<{id:cbl.DocID, rev:cbl.RevID}>();
114114
response[collName] = docs;
115115
await coll.eachDocument( doc => {
@@ -374,7 +374,7 @@ export class TDKImpl implements tdk.TDK, AsyncDisposable {
374374
let totalDocs = 0, totalBlobs = 0;
375375
for (const collID of config.collections) {
376376
this.#logger.debug `- Loading docs in collection ${collID}...`;
377-
const collection = db.getCollection(collID);
377+
const collection = db.getCollection(normalizeCollectionID(collID));
378378
const docs: cbl.CBLDocument[] = [];
379379
const jsonl = await fetchRelative(`${collID}.jsonl`);
380380
for (const line of jsonl.trim().split('\n')) {
@@ -466,12 +466,6 @@ export class TDKImpl implements tdk.TDK, AsyncDisposable {
466466
#logSender? : LogSlurpSender;
467467
}
468468

469-
470-
/** Strips the default scope name from an incoming collection ID. */
471-
function normalizeCollectionID(id: string): string {
472-
return id.startsWith("_default.") ? id.substring(9) : id;
473-
}
474-
475469
/** Adds the default scope name, if necessary, to an outgoing collection ID. */
476470
function collectionIDWithScope(id: string): string {
477471
return id.includes('.') ? id : `_default.${id}`;

servers/javascript/src/testServer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
/* eslint-disable camelcase */
1414

1515
import { WebSocketClient } from "./webSocketClient";
16-
import { check, HTTPError } from "./httpError";
16+
import { check, HTTPError } from "./utils";
1717
import * as logtape from "@logtape/logtape";
1818
import * as cbl from "@couchbase/lite-js";
1919

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,15 @@ export class HTTPError extends Error {
1919
}
2020
}
2121

22+
23+
/** Simple assertion that throws an HTTPError with status 400 on failure. */
2224
export function check(cond: boolean, message: string): asserts cond {
2325
if (!cond)
2426
throw new HTTPError(400, message);
2527
}
28+
29+
30+
/** Strips the default scope name from an incoming collection ID. */
31+
export function normalizeCollectionID(id: string): string {
32+
return id.startsWith("_default.") ? id.substring(9) : id;
33+
}

servers/javascript/src/webSocketClient.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
// the file licenses/APL2.txt.
1111
//
1212

13-
import { check } from "./httpError";
13+
import { check } from "./utils";
1414

1515

1616
/** States of WebSocketClient. (Same as the all-caps constants in `WebSocket`.) */

0 commit comments

Comments
 (0)