From 596428572b69d612fd1b8261853e451bc28b3794 Mon Sep 17 00:00:00 2001 From: Justin Fagnani Date: Fri, 21 Oct 2022 15:40:57 -0700 Subject: [PATCH] Partition custom element documents with namespace --- .../src/lib/firestore/firestore-repository.ts | 2 + .../firestore/firestore-repository_test.ts | 86 +++++++++++++++++++ 2 files changed, 88 insertions(+) create mode 100644 packages/catalog-server/src/test/lib/firestore/firestore-repository_test.ts diff --git a/packages/catalog-server/src/lib/firestore/firestore-repository.ts b/packages/catalog-server/src/lib/firestore/firestore-repository.ts index 6a339e2f..9079a9da 100644 --- a/packages/catalog-server/src/lib/firestore/firestore-repository.ts +++ b/packages/catalog-server/src/lib/firestore/firestore-repository.ts @@ -333,6 +333,7 @@ export class FirestoreRepository implements Repository { ]; batch.create(customElementsRef.doc(), { + namespace: this.namespace, package: packageName, version, distTags, @@ -506,6 +507,7 @@ export class FirestoreRepository implements Repository { .collectionGroup('customElements') .withConverter(customElementConverter) .where('isLatest', '==', true) + .where('namespace', '==', this.namespace) .limit(limit ?? 25); if (query !== undefined) { diff --git a/packages/catalog-server/src/test/lib/firestore/firestore-repository_test.ts b/packages/catalog-server/src/test/lib/firestore/firestore-repository_test.ts new file mode 100644 index 00000000..1e82db75 --- /dev/null +++ b/packages/catalog-server/src/test/lib/firestore/firestore-repository_test.ts @@ -0,0 +1,86 @@ +/** + * @license + * Copyright 2022 Google LLC + * SPDX-License-Identifier: Apache-2.0 + */ + +import { + getModule, + Version, +} from '@webcomponents/custom-elements-manifest-tools'; +import { + CustomElementDeclaration, + CustomElementExport, + Package, +} from 'custom-elements-manifest'; +import {suite} from 'uvu'; +import * as assert from 'uvu/assert'; + +import {FirestoreRepository} from '../../../lib/firestore/firestore-repository.js'; + +const test = suite('FirestoreRepository tests'); + +test('Limits element searches to a namespace', async () => { + const manifest: Package = { + schemaVersion: '1.0.0', + readme: 'README.md', + modules: [ + { + kind: 'javascript-module', + path: 'foo.js', + exports: [ + { + kind: 'custom-element-definition', + name: 'x-foo', + declaration: { + name: 'FooElement', + }, + }, + ], + declarations: [ + { + kind: 'class', + customElement: true, + tagName: 'x-foo', + name: 'FooElement', + members: [], + }, + ], + }, + ], + }; + + const module = getModule(manifest, 'foo.js')!; + const customElementExport = module.exports![0] as CustomElementExport; + const customElementDeclaration = + module.declarations![0] as CustomElementDeclaration; + + const repo1 = new FirestoreRepository('firestore-repository-test-1'); + const repo2 = new FirestoreRepository('firestore-repository-test-2'); + + const writeElement = async (repo: FirestoreRepository) => + repo.writeCustomElements( + {name: 'foo', version: '1.0.0', description: 'test package'} as Version, + [ + { + package: manifest, + module, + export: customElementExport, + declaration: customElementDeclaration, + declarationReference: customElementExport.declaration, + }, + ], + ['latest'], + 'joe' + ); + + await Promise.all([writeElement(repo1), writeElement(repo2)]); + const [result1, result2] = await Promise.all([ + repo1.queryElements({query: 'foo'}), + repo2.queryElements({query: 'foo'}), + ]); + assert.equal(result1.length, 1); + assert.equal(result2.length, 1); +}); + +test.run();