From a23faebde034a2fd6e20be3d31ee101e4d4cad1b Mon Sep 17 00:00:00 2001 From: Elliot Waddington Date: Thu, 12 Dec 2024 14:04:30 +0100 Subject: [PATCH] chore: remove deprecated discovery code (#226) --- src/__tests__/discovery.test.ts | 166 -------------------------------- src/index.ts | 1 - src/source-discovery.ts | 162 ------------------------------- 3 files changed, 329 deletions(-) delete mode 100644 src/__tests__/discovery.test.ts delete mode 100644 src/source-discovery.ts diff --git a/src/__tests__/discovery.test.ts b/src/__tests__/discovery.test.ts deleted file mode 100644 index 84ac62f..0000000 --- a/src/__tests__/discovery.test.ts +++ /dev/null @@ -1,166 +0,0 @@ -import { WS } from 'jest-websocket-mock' -import { GatewaySession } from '../gateway-session' -import { - GatewayRequest, - GatewayResponse, - Item, - Metadata, - Query, - QueryMethod, -} from '../protobuf' -import { SourceDiscovery } from '../source-discovery' -import { newItemAttributes, newTimestamp } from '../util' - -const TestServerAddress = 'ws://localhost:31035' - -describe('SourceDiscovery', () => { - describe('with a connection', () => { - let server: WS - let session: GatewaySession - let disco: SourceDiscovery - - beforeEach(async () => { - server = new WS(TestServerAddress, { - jsonProtocol: false, - }) - session = new GatewaySession(TestServerAddress) - - await session.ready - - disco = new SourceDiscovery(session) - }) - - afterEach(() => { - session.close() - server.close() - }) - - describe('#discoverTypes()', () => { - it('works like a hot damn', async () => { - disco.discoverTypes() - - // Send just the new request - const msg = await server.nextMessage - - expect(msg).not.toBeUndefined() - expect(msg).toBeInstanceOf(Uint8Array) - if (msg instanceof Uint8Array) { - const req = GatewayRequest.fromBinary(msg) - - expect(req.requestType.case).toBe('query') - if (req.requestType.case === 'query') { - expect(req.requestType.value.type).toEqual('overmind-type') - expect(req.requestType.value.scope).toEqual('global') - expect(req.requestType.value.method).toEqual(QueryMethod.LIST) - - // Send a response - const resp: GatewayResponse = new GatewayResponse({ - responseType: { - case: 'newItem', - value: new Item({ - type: 'overmind-type', - uniqueAttribute: 'name', - scope: 'global', - attributes: newItemAttributes({ - name: 'person', - }), - metadata: new Metadata({ - sourceName: 'overmind-type-metasource', - timestamp: newTimestamp(new Date()), - sourceQuery: new Query({ - scope: 'global', - recursionBehaviour: { linkDepth: 0 }, - method: QueryMethod.GET, - query: 'per', - type: 'overmind-type', - UUID: req.requestType.value.UUID, - }), - }), - }), - }, - }) - - const respBin = resp.toBinary() - - // Create a promise to wait for the new suggestions - const newSuggestions = new Promise((resolve) => { - disco.addEventListener('new-type', (event) => { - resolve(event.detail) - }) - }) - - server.send(respBin.buffer) - - const suggestions = await newSuggestions - - expect(suggestions).toEqual(['person']) - } - } - }) - }) - - describe('#discoverScopes()', () => { - it('works like a hot damn', async () => { - disco.discoverScopes() - - // Send just the new request - const msg = await server.nextMessage - - expect(msg).not.toBeUndefined() - expect(msg).toBeInstanceOf(Uint8Array) - if (msg instanceof Uint8Array) { - const req = GatewayRequest.fromBinary(msg) - - expect(req.requestType.case).toBe('query') - if (req.requestType.case === 'query') { - expect(req.requestType.value.type).toEqual('overmind-scope') - expect(req.requestType.value.scope).toEqual('global') - expect(req.requestType.value.method).toEqual(QueryMethod.LIST) - - // Send a response - const resp: GatewayResponse = new GatewayResponse({ - responseType: { - case: 'newItem', - value: new Item({ - type: 'overmind-scope', - uniqueAttribute: 'name', - scope: 'global', - attributes: newItemAttributes({ - name: 'person', - }), - metadata: new Metadata({ - sourceName: 'overmind-scope-metasource', - timestamp: newTimestamp(new Date()), - sourceQuery: new Query({ - scope: 'global', - recursionBehaviour: { linkDepth: 0 }, - method: QueryMethod.GET, - query: 'per', - type: 'overmind-type', - UUID: req.requestType.value.UUID, - }), - }), - }), - }, - }) - - const respBin = resp.toBinary() - - // Create a promise to wait for the new suggestions - const newSuggestions = new Promise((resolve) => { - disco.addEventListener('new-scope', (event) => { - resolve(event.detail) - }) - }) - - server.send(respBin.buffer) - - const suggestions = await newSuggestions - - expect(suggestions).toEqual(['person']) - } - } - }) - }) - }) -}) diff --git a/src/index.ts b/src/index.ts index ad56762..0fb49ea 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,3 @@ -export * from './source-discovery' export * from './gateway-session' export * from './request-progress' export * from './responder' diff --git a/src/source-discovery.ts b/src/source-discovery.ts deleted file mode 100644 index 6effc05..0000000 --- a/src/source-discovery.ts +++ /dev/null @@ -1,162 +0,0 @@ -import { v4, parse } from 'uuid' -import { - CustomEventListenerOrEventListenerObject, - GatewaySession, -} from './gateway-session' -import { GatewayRequest, Item, Query, QueryMethod } from './protobuf' -import { getUniqueAttributeValue, newDeadline } from './util' - -export enum DiscoveryField { - TYPE = 0, - SCOPE = 1, -} - -function toString(field: DiscoveryField): string { - switch (field) { - case DiscoveryField.TYPE: { - return 'overmind-type' - } - case DiscoveryField.SCOPE: { - return 'overmind-scope' - } - default: { - return '' - } - } -} - -export const NewTypeEvent = 'new-type' -export const NewScopeEvent = 'new-scope' - -export class SourceDiscovery extends EventTarget { - private session: GatewaySession - private types: string[] = [] - private scopes: string[] = [] - - constructor(session: GatewaySession) { - super() - - this.session = session - - // Listen for results - this.session.addEventListener('new-item', (item) => - this.processItem(item.detail), - ) - } - - addEventListener( - type: typeof NewTypeEvent, - callback: CustomEventListenerOrEventListenerObject | null, - options?: boolean | AddEventListenerOptions | undefined, - ): void - - addEventListener( - type: typeof NewScopeEvent, - callback: CustomEventListenerOrEventListenerObject | null, - options?: boolean | AddEventListenerOptions | undefined, - ): void - - addEventListener( - type: string, - callback: EventListenerOrEventListenerObject | null, - options?: AddEventListenerOptions | boolean, - ): void { - super.addEventListener(type, callback, options) - } - - removeEventListener( - type: typeof NewTypeEvent, - callback: CustomEventListenerOrEventListenerObject | null, - options?: boolean | AddEventListenerOptions | undefined, - ): void - - removeEventListener( - type: typeof NewScopeEvent, - callback: CustomEventListenerOrEventListenerObject | null, - options?: boolean | AddEventListenerOptions | undefined, - ): void - - removeEventListener( - type: string, - callback: EventListenerOrEventListenerObject | null, - options?: boolean | EventListenerOptions | undefined, - ): void { - super.removeEventListener(type, callback, options) - } - - /** - * Starts discovering available types. Results will be dispatched as a - * NewTypeEvent - */ - discoverTypes() { - this.discover('overmind-type') - } - - /** - * Starts discovering available scopes. Results will be dispatched as a - * NewTypeEvent - */ - discoverScopes() { - this.discover('overmind-scope') - } - - discoverField(field: DiscoveryField) { - this.discover(toString(field)) - } - - private discover(type: string) { - const request = new GatewayRequest({ - requestType: { - case: 'query', - value: new Query({ - scope: 'global', - method: QueryMethod.LIST, - deadline: newDeadline(5000), - UUID: parse(v4()), - type, - }), - }, - }) - - this.session.sendRequest(request) - } - - /** - * Processes incoming items and extracts autocomplete responses - * - * @param item The item to process - */ - processItem(item: Item): void { - switch (item.type) { - case 'overmind-scope': { - // Add the suggestion to the list - this.scopes.push(getUniqueAttributeValue(item)) - this.scopes.sort() - - // Dispatch an event - this.dispatchEvent( - new CustomEvent(NewScopeEvent, { - detail: this.scopes, - }), - ) - break - } - case 'overmind-type': { - // Add the suggestion to the list - this.types.push(getUniqueAttributeValue(item)) - this.types.sort() - - // Dispatch an event - this.dispatchEvent( - new CustomEvent(NewTypeEvent, { - detail: this.types, - }), - ) - break - } - default: { - break - } - } - } -}