diff --git a/package.json b/package.json index 2550b4c..96f3390 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@overmindtech/sdp-js", - "version": "11.11.0", + "version": "12.0.0", "description": "Javascript and Typescript libraries for the State Description Protocol", "license": "MIT", "sideEffects": false, @@ -31,7 +31,7 @@ "qa": "pnpm typecheck && pnpm lint:fix && pnpm prettier:fix && npm test" }, "dependencies": { - "@bufbuild/protobuf": "^1.10.0", + "@bufbuild/protobuf": "^2.2.2", "uuid": "^11.0.3" }, "devDependencies": { diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 5f65139..5f65eba 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -9,8 +9,8 @@ importers: .: dependencies: '@bufbuild/protobuf': - specifier: ^1.10.0 - version: 1.10.0 + specifier: ^2.2.2 + version: 2.2.3 uuid: specifier: ^11.0.3 version: 11.0.3 @@ -139,8 +139,8 @@ packages: '@bcoe/v8-coverage@0.2.3': resolution: {integrity: sha512-0hYQ8SB4Db5zvZB4axdMHGwEaQjkZzFjQiN9LVYvIFB2nSUHW9tYpxWriPrWDASIxiaXax83REcLxuSdnGPZtw==} - '@bufbuild/protobuf@1.10.0': - resolution: {integrity: sha512-QDdVFLoN93Zjg36NoQPZfsVH9tZew7wKDKyV5qRdj8ntT4wQCOradQjRaTdwMhWUYsgKsvCINKKm87FdEk96Ag==} + '@bufbuild/protobuf@2.2.3': + resolution: {integrity: sha512-tFQoXHJdkEOSwj5tRIZSPNUuXK3RaR7T1nUrPgbYX1pUbvqqaaZAsfo+NXBPsz5rZMSKVFrgK1WL8Q/MSLvprg==} '@esbuild/aix-ppc64@0.19.12': resolution: {integrity: sha512-bmoCYyWdEL3wDQIVbcyzRyeKLgk2WtWLTWz1ZIAZF/EGbNOwSA6ew3PftJ1PqMiOOGu0OyFMzG53L0zqIpPeNA==} @@ -2706,7 +2706,7 @@ snapshots: '@bcoe/v8-coverage@0.2.3': {} - '@bufbuild/protobuf@1.10.0': {} + '@bufbuild/protobuf@2.2.3': {} '@esbuild/aix-ppc64@0.19.12': optional: true diff --git a/src/__tests__/gateway.test.ts b/src/__tests__/gateway.test.ts index 36d27bc..9d64dd2 100644 --- a/src/__tests__/gateway.test.ts +++ b/src/__tests__/gateway.test.ts @@ -21,15 +21,18 @@ import { } from '../events' import { GatewaySession } from '../gateway-session' import { - ChangeByReferenceSummary, + ChangeByReferenceSummarySchema, ChangesByReferenceToolFinish, - GatewayResponse, + GatewayRequestSchema, + GatewayRequestStatus_SummarySchema, + GatewayResponseSchema, QueryMethod, - StoreBookmark, - StoreSnapshot, + StoreBookmarkSchema, + StoreSnapshotSchema, } from '../protobuf' import * as data from './sampledata.helper' -import { Timestamp } from '@bufbuild/protobuf' +import { create, toBinary, toJsonString } from '@bufbuild/protobuf' +import { TimestampSchema } from '@bufbuild/protobuf/wkt' // create a WS instance const TestServerAddress = 'ws://localhost:31274' @@ -63,7 +66,7 @@ describe('GatewaySession', () => { gs.sendRequest(data.gatewayRequest.itemRequest) expect(await server.nextMessage).toEqual( - data.gatewayRequest.itemRequest.toBinary(), + toBinary(GatewayRequestSchema, data.gatewayRequest.itemRequest), ) }) }) @@ -82,7 +85,7 @@ describe('GatewaySession', () => { describe('Error', () => { it('should call the callback', () => new Promise((resolve) => { - const response = new GatewayResponse({ + const response = create(GatewayResponseSchema, { responseType: { case: 'error', value: 'some error', @@ -99,13 +102,13 @@ describe('GatewaySession', () => { { once: true }, ) - server.send(response.toBinary().buffer) + server.send(toBinary(GatewayResponseSchema, response).buffer) })) }) describe('NewItem', () => { it('should call the callback', () => new Promise((resolve) => { - const response = new GatewayResponse({ + const response = create(GatewayResponseSchema, { responseType: { case: 'newItem', value: data.item.dylan, @@ -122,13 +125,13 @@ describe('GatewaySession', () => { { once: true }, ) - server.send(response.toBinary().buffer) + server.send(toBinary(GatewayResponseSchema, response).buffer) })) }) describe('ChatResponse', () => { it('should call the callback', () => new Promise((resolve) => { - const response = new GatewayResponse({ + const response = create(GatewayResponseSchema, { responseType: { case: 'chatResponse', value: { @@ -147,14 +150,14 @@ describe('GatewaySession', () => { { once: true }, ) - server.send(response.toBinary().buffer) + server.send(toBinary(GatewayResponseSchema, response).buffer) })) }) describe('ToolStart', () => { describe('Query', () => { it('should call the callback', () => new Promise((resolve) => { - const response = new GatewayResponse({ + const response = create(GatewayResponseSchema, { responseType: { case: 'toolStart', value: { @@ -187,13 +190,13 @@ describe('GatewaySession', () => { { once: true }, ) - server.send(response.toBinary().buffer) + server.send(toBinary(GatewayResponseSchema, response).buffer) })) }) describe('Relationship', () => { it('should call the callback', () => new Promise((resolve) => { - const response = new GatewayResponse({ + const response = create(GatewayResponseSchema, { responseType: { case: 'toolStart', value: { @@ -224,13 +227,13 @@ describe('GatewaySession', () => { { once: true }, ) - server.send(response.toBinary().buffer) + server.send(toBinary(GatewayResponseSchema, response).buffer) })) }) describe('ChangesByReference', () => { it('should call the callback', () => new Promise((resolve) => { - const response = new GatewayResponse({ + const response = create(GatewayResponseSchema, { responseType: { case: 'toolStart', value: { @@ -263,7 +266,7 @@ describe('GatewaySession', () => { { once: true }, ) - server.send(response.toBinary().buffer) + server.send(toBinary(GatewayResponseSchema, response).buffer) })) }) }) @@ -271,7 +274,7 @@ describe('GatewaySession', () => { describe('Query', () => { it('should call the callback', () => new Promise((resolve) => { - const response = new GatewayResponse({ + const response = create(GatewayResponseSchema, { responseType: { case: 'toolFinish', value: { @@ -298,13 +301,13 @@ describe('GatewaySession', () => { { once: true }, ) - server.send(response.toBinary().buffer) + server.send(toBinary(GatewayResponseSchema, response).buffer) })) }) describe('Relationship', () => { it('should call the callback', () => new Promise((resolve) => { - const response = new GatewayResponse({ + const response = create(GatewayResponseSchema, { responseType: { case: 'toolFinish', value: { @@ -331,13 +334,13 @@ describe('GatewaySession', () => { { once: true }, ) - server.send(response.toBinary().buffer) + server.send(toBinary(GatewayResponseSchema, response).buffer) })) }) describe('ChangesByReference', () => { it('should call the callback', () => new Promise((resolve) => { - const response = new GatewayResponse({ + const response = create(GatewayResponseSchema, { responseType: { case: 'toolFinish', value: { @@ -345,11 +348,11 @@ describe('GatewaySession', () => { case: 'changesByReference', value: { changeSummaries: [ - new ChangeByReferenceSummary({ + create(ChangeByReferenceSummarySchema, { title: 'Some title', numAffectedItems: 123, owner: 'Some owner', - createdAt: new Timestamp({ + createdAt: create(TimestampSchema, { nanos: 100, seconds: BigInt(100_000_000), }), @@ -376,14 +379,14 @@ describe('GatewaySession', () => { { once: true }, ) - server.send(response.toBinary().buffer) + server.send(toBinary(GatewayResponseSchema, response).buffer) })) }) }) describe('NewEdge', () => { it('should call the callback', () => new Promise((resolve) => { - const response = new GatewayResponse({ + const response = create(GatewayResponseSchema, { responseType: { case: 'newEdge', value: data.edge.basic, @@ -401,13 +404,13 @@ describe('GatewaySession', () => { { once: true }, ) - server.send(response.toBinary().buffer) + server.send(toBinary(GatewayResponseSchema, response).buffer) })) }) describe('QueryError', () => { it('should call the callback', () => new Promise((resolve) => { - const response = new GatewayResponse({ + const response = create(GatewayResponseSchema, { responseType: { case: 'queryError', value: data.error.NOSCOPE, @@ -427,13 +430,13 @@ describe('GatewaySession', () => { { once: true }, ) - server.send(response.toBinary().buffer) + server.send(toBinary(GatewayResponseSchema, response).buffer) })) }) describe('DeleteItem', () => { it('should call the callback', () => new Promise((resolve) => { - const response = new GatewayResponse({ + const response = create(GatewayResponseSchema, { responseType: { case: 'deleteItem', value: data.reference, @@ -453,13 +456,13 @@ describe('GatewaySession', () => { { once: true }, ) - server.send(response.toBinary().buffer) + server.send(toBinary(GatewayResponseSchema, response).buffer) })) }) describe('DeleteEdge', () => { it('should call the callback', () => new Promise((resolve) => { - const response = new GatewayResponse({ + const response = create(GatewayResponseSchema, { responseType: { case: 'deleteEdge', value: data.edge.basic, @@ -481,13 +484,13 @@ describe('GatewaySession', () => { { once: true }, ) - server.send(response.toBinary().buffer) + server.send(toBinary(GatewayResponseSchema, response).buffer) })) }) describe('UpdateItem', () => { it('should call the callback', () => new Promise((resolve) => { - const response = new GatewayResponse({ + const response = create(GatewayResponseSchema, { responseType: { case: 'updateItem', value: data.item.dylan, @@ -499,21 +502,19 @@ describe('GatewaySession', () => { UpdateItemEvent, (event) => { expect(event.detail.scope).toEqual(data.item.dylan.scope) - expect(event.detail.getType()).toEqual( - data.item.dylan.getType(), - ) + expect(event.detail.type).toEqual(data.item.dylan.type) resolve(undefined) }, { once: true }, ) - server.send(response.toBinary().buffer) + server.send(toBinary(GatewayResponseSchema, response).buffer) })) }) describe('Status', () => { it('should call the callback', () => new Promise((resolve) => { - const response = new GatewayResponse({ + const response = create(GatewayResponseSchema, { responseType: { case: 'status', value: data.gatewayStatus.working, @@ -527,25 +528,34 @@ describe('GatewaySession', () => { expect(event.detail.postProcessingComplete).toEqual( data.gatewayStatus.working.postProcessingComplete, ) - expect(event.detail.summary?.toJsonString()).toEqual( - data.gatewayStatus.working.summary?.toJsonString(), - ) + if (event.detail.summary && data.gatewayStatus.working.summary) + expect( + toJsonString( + GatewayRequestStatus_SummarySchema, + event.detail.summary, + ), + ).toEqual( + toJsonString( + GatewayRequestStatus_SummarySchema, + data.gatewayStatus.working.summary, + ), + ) resolve(undefined) }, { once: true }, ) - server.send(response.toBinary().buffer) + server.send(toBinary(GatewayResponseSchema, response).buffer) })) it('should update the status', () => new Promise((resolve) => { - const working = new GatewayResponse({ + const working = create(GatewayResponseSchema, { responseType: { case: 'status', value: data.gatewayStatus.working, }, }) - const doneResponse = new GatewayResponse({ + const doneResponse = create(GatewayResponseSchema, { responseType: { case: 'status', value: data.gatewayStatus.done, @@ -559,9 +569,19 @@ describe('GatewaySession', () => { expect(gs.status?.postProcessingComplete).toEqual( data.gatewayStatus.working.postProcessingComplete, ) - expect(gs.status?.summary?.toJsonString()).toEqual( - data.gatewayStatus.working.summary?.toJsonString(), - ) + if (gs.status?.summary && data.gatewayStatus.working.summary) { + expect( + toJsonString( + GatewayRequestStatus_SummarySchema, + gs.status?.summary, + ), + ).toEqual( + toJsonString( + GatewayRequestStatus_SummarySchema, + data.gatewayStatus.working.summary, + ), + ) + } // Add the second test gs.addEventListener( @@ -570,20 +590,32 @@ describe('GatewaySession', () => { expect(gs.status?.postProcessingComplete).toEqual( data.gatewayStatus.done.postProcessingComplete, ) - expect(gs.status?.summary?.toJsonString()).toEqual( - data.gatewayStatus.done.summary?.toJsonString(), - ) + if (gs.status?.summary && data.gatewayStatus.done.summary) { + expect( + toJsonString( + GatewayRequestStatus_SummarySchema, + gs.status?.summary, + ), + ).toEqual( + toJsonString( + GatewayRequestStatus_SummarySchema, + data.gatewayStatus.done.summary, + ), + ) + } resolve(undefined) }, { once: true }, ) - server.send(doneResponse.toBinary().buffer) + server.send( + toBinary(GatewayResponseSchema, doneResponse).buffer, + ) }, { once: true }, ) - server.send(working.toBinary().buffer) + server.send(toBinary(GatewayResponseSchema, working).buffer) })) }) }) @@ -606,8 +638,8 @@ describe('GatewaySession', () => { description: 'new description', msgID: parse(v4()), } - const bookmark = new StoreBookmark(data) - const response = new GatewayResponse({ + const bookmark = create(StoreBookmarkSchema, data) + const response = create(GatewayResponseSchema, { responseType: { case: 'bookmarkStoreResult', value: { @@ -624,7 +656,7 @@ describe('GatewaySession', () => { resolve(undefined) }) - server.send(response.toBinary().buffer) + server.send(toBinary(GatewayResponseSchema, response).buffer) })) }) describe('storeSnapshot', () => { @@ -635,8 +667,8 @@ describe('GatewaySession', () => { description: 'new description', msgID: parse(v4()), } - const snapshot = new StoreSnapshot(data) - const response = new GatewayResponse({ + const snapshot = create(StoreSnapshotSchema, data) + const response = create(GatewayResponseSchema, { responseType: { case: 'snapshotStoreResult', value: { @@ -653,7 +685,7 @@ describe('GatewaySession', () => { resolve(undefined) }) - server.send(response.toBinary().buffer) + server.send(toBinary(GatewayResponseSchema, response).buffer) })) }) }) diff --git a/src/__tests__/sampledata.helper.ts b/src/__tests__/sampledata.helper.ts index 1517666..d44c7f4 100644 --- a/src/__tests__/sampledata.helper.ts +++ b/src/__tests__/sampledata.helper.ts @@ -1,38 +1,37 @@ +import { create } from '@bufbuild/protobuf' import { parse } from 'uuid' -import { newDuration, newItemAttributes } from '../util' -import { - CancelQuery, - Edge, - GatewayRequest, - Item, - Query, - QueryError, - Reference, - QueryMethod, - ResponderState, - Expand, - UndoQuery, - QueryError_ErrorType, -} from '../protobuf' -import { Response } from '../__generated__/responses_pb' import { - GatewayRequestStatus, - GatewayRequestStatus_Summary, + GatewayRequestSchema, + GatewayRequestStatus_SummarySchema, + GatewayRequestStatusSchema, } from '../__generated__/gateway_pb' -import { LinkedItem } from '../__generated__/items_pb' +import { + CancelQuerySchema, + EdgeSchema, + ExpandSchema, + ItemSchema, + LinkedItemSchema, + QueryErrorSchema, + QuerySchema, + ReferenceSchema, + UndoQuerySchema, +} from '../__generated__/items_pb' +import { ResponseSchema } from '../__generated__/responses_pb' +import { QueryError_ErrorType, QueryMethod, ResponderState } from '../protobuf' +import { newDuration, newItemAttributes } from '../util' export const error = { - NOTFOUND: new QueryError({ + NOTFOUND: create(QueryErrorSchema, { errorString: 'Could not be found', errorType: QueryError_ErrorType.NOTFOUND, scope: 'test.scope', }), - NOSCOPE: new QueryError({ + NOSCOPE: create(QueryErrorSchema, { errorString: 'Scope does not exist', errorType: QueryError_ErrorType.NOSCOPE, scope: 'test.scope', }), - OTHER: new QueryError({ + OTHER: create(QueryErrorSchema, { errorString: 'Unknown error', errorType: QueryError_ErrorType.OTHER, scope: 'test.scope', @@ -40,28 +39,28 @@ export const error = { } export const response = { - WORKING: new Response({ + WORKING: create(ResponseSchema, { responder: 'test.scope', state: ResponderState.WORKING, nextUpdateIn: newDuration(100), }), - COMPLETE: new Response({ + COMPLETE: create(ResponseSchema, { responder: 'test.scope', state: ResponderState.COMPLETE, nextUpdateIn: newDuration(100), }), - CANCELLED: new Response({ + CANCELLED: create(ResponseSchema, { responder: 'test.scope', state: ResponderState.CANCELLED, }), - ERROR: new Response({ + ERROR: create(ResponseSchema, { responder: 'test.scope', state: ResponderState.ERROR, }), } export const request = { - LIST: new Query({ + LIST: create(QuerySchema, { type: 'package', method: QueryMethod.LIST, recursionBehaviour: { linkDepth: 90 }, @@ -72,7 +71,7 @@ export const request = { } export const item = { - process: new Item({ + process: create(ItemSchema, { type: 'process', scope: 'myPod', uniqueAttribute: 'pid', @@ -82,7 +81,7 @@ export const item = { cpuPercent: 99.99, }), }), - dylan: new Item({ + dylan: create(ItemSchema, { scope: 'global', uniqueAttribute: 'name', type: 'person', @@ -91,8 +90,8 @@ export const item = { age: 27, }), linkedItems: [ - new LinkedItem({ - item: new Reference({ + create(LinkedItemSchema, { + item: create(ReferenceSchema, { scope: 'global', type: 'person', uniqueAttributeValue: 'katie', @@ -100,7 +99,7 @@ export const item = { }), ], }), - katie: new Item({ + katie: create(ItemSchema, { scope: 'global', uniqueAttribute: 'name', type: 'person', @@ -113,30 +112,30 @@ export const item = { export const items = [item.process, item.dylan, item.katie] -export const cancelQuery = new CancelQuery({ +export const cancelQuery = create(CancelQuerySchema, { UUID: parse('a520d67f-0b2a-4852-87d2-d02bbc74ad89'), }) -export const undoQuery = new UndoQuery({ +export const undoQuery = create(UndoQuerySchema, { UUID: parse('a520d67f-0b2a-4852-87d2-d02bbc74ad89'), }) -export const reference = new Reference({ +export const reference = create(ReferenceSchema, { scope: 'global', type: 'ip', uniqueAttributeValue: '1.1.1.1', }) -export const expand = new Expand({ +export const expand = create(ExpandSchema, { item: reference, linkDepth: 1, }) export const gatewayRequest = { - itemRequest: new GatewayRequest({ + itemRequest: create(GatewayRequestSchema, { requestType: { case: 'query', - value: new Query({ + value: create(QuerySchema, { scope: 'test', recursionBehaviour: { linkDepth: 10 }, method: QueryMethod.GET, @@ -146,10 +145,10 @@ export const gatewayRequest = { }), }, }), - cancel: new GatewayRequest({ + cancel: create(GatewayRequestSchema, { requestType: { case: 'cancelQuery', - value: new CancelQuery({ + value: create(CancelQuerySchema, { UUID: parse('a520d67f-0b2a-4852-87d2-d02bbc74ad89'), }), }, @@ -157,14 +156,14 @@ export const gatewayRequest = { } export const gatewayStatus = { - working: new GatewayRequestStatus({ + working: create(GatewayRequestStatusSchema, { responderStates: { 'responder.cancel': ResponderState.CANCELLED, 'responder.complete': ResponderState.COMPLETE, 'responder.error': ResponderState.ERROR, 'responder.working': ResponderState.WORKING, }, - summary: new GatewayRequestStatus_Summary({ + summary: create(GatewayRequestStatus_SummarySchema, { cancelled: 1, complete: 1, error: 1, @@ -174,14 +173,14 @@ export const gatewayStatus = { }), postProcessingComplete: false, }), - done: new GatewayRequestStatus({ + done: create(GatewayRequestStatusSchema, { responderStates: { 'responder.cancel': ResponderState.CANCELLED, 'responder.complete': ResponderState.COMPLETE, 'responder.error': ResponderState.ERROR, 'responder.working': ResponderState.COMPLETE, }, - summary: new GatewayRequestStatus_Summary({ + summary: create(GatewayRequestStatus_SummarySchema, { cancelled: 1, complete: 2, error: 1, @@ -194,13 +193,13 @@ export const gatewayStatus = { } export const edge = { - basic: new Edge({ - from: new Reference({ + basic: create(EdgeSchema, { + from: create(ReferenceSchema, { scope: 'test', type: 'user', uniqueAttributeValue: 'Dylan', }), - to: new Reference({ + to: create(ReferenceSchema, { scope: 'test', type: 'dog', uniqueAttributeValue: 'Manny', diff --git a/src/__tests__/util.test.ts b/src/__tests__/util.test.ts index ab59e80..086ba22 100644 --- a/src/__tests__/util.test.ts +++ b/src/__tests__/util.test.ts @@ -7,7 +7,9 @@ import { getGloballyUniqueName, toReference, } from '../util' -import { Item } from '../protobuf' +import { ItemSchema } from '../protobuf' +import { create } from '@bufbuild/protobuf' +import { timestampDate } from '@bufbuild/protobuf/wkt' describe('Util namespace', () => { describe('#newTimestamp', () => { @@ -15,7 +17,7 @@ describe('Util namespace', () => { const now = new Date() const ts = newTimestamp(now) - expect(now.toISOString()).toEqual(ts.toDate().toISOString()) + expect(now.toISOString()).toEqual(timestampDate(ts).toISOString()) }) }) @@ -30,7 +32,7 @@ describe('Util namespace', () => { describe('#newReference', () => { it('returns a fully populated reference', () => { - const item = new Item({ + const item = create(ItemSchema, { type: 'person', uniqueAttribute: 'name', attributes: newItemAttributes({ @@ -61,7 +63,7 @@ describe('Util namespace', () => { describe('#getUniqueAttributeValue', () => { it('returns the correct value', () => { - const item = new Item({ + const item = create(ItemSchema, { type: 'person', uniqueAttribute: 'name', attributes: newItemAttributes({ @@ -77,7 +79,7 @@ describe('Util namespace', () => { describe('#getGloballyUniqueName', () => { it('returns the correct value', () => { - const item = new Item({ + const item = create(ItemSchema, { type: 'person', uniqueAttribute: 'name', scope: 'scope', diff --git a/src/gateway-session.ts b/src/gateway-session.ts index 4d35ec3..68003c7 100644 --- a/src/gateway-session.ts +++ b/src/gateway-session.ts @@ -23,13 +23,10 @@ import { Edge, GatewayRequest, GatewayRequestStatus, - GatewayResponse, Item, QueryStatus, QueryError, Reference, - UndoQuery, - UndoExpand, BookmarkStoreResult, BookmarkLoadResult, SnapshotStoreResult, @@ -39,7 +36,12 @@ import { ChatResponse, ToolStart, ToolFinish, + GatewayRequestSchema, + UndoQuerySchema, + UndoExpandSchema, + GatewayResponseSchema, } from './protobuf' +import { create, fromBinary, toBinary } from '@bufbuild/protobuf' export interface CustomEventListener { (evt: CustomEvent): void @@ -181,7 +183,8 @@ export class GatewaySession extends EventTarget { // Loop over the data using the index for (let i = 0; i < data.length; i++) { // Process the message - this._processMessage(data[i].msg) + // eslint-disable-next-line unicorn/prefer-spread + this._processMessage(data[i].msg.slice().buffer) // If we are not at the end of the array, calculate the time to wait if (i < data.length - 1) { @@ -204,7 +207,7 @@ export class GatewaySession extends EventTarget { */ _processMessage(buffer: ArrayBuffer) { const binary = new Uint8Array(buffer) - const response = GatewayResponse.fromBinary(binary) + const response = fromBinary(GatewayResponseSchema, binary) switch (response.responseType.case) { case 'error': { @@ -582,7 +585,7 @@ export class GatewaySession extends EventTarget { * @param request The request to send */ sendRequest(request: GatewayRequest) { - const binary = request.toBinary() + const binary = toBinary(GatewayRequestSchema, request) this.socket.send(binary) } @@ -594,32 +597,32 @@ export class GatewaySession extends EventTarget { switch (request.requestType.case) { case 'query': { { - const undoReq = new GatewayRequest({ + const undoReq = create(GatewayRequestSchema, { requestType: { case: 'undoQuery', - value: new UndoQuery({ + value: create(UndoQuerySchema, { UUID: request.requestType.value.UUID, }), }, }) - const binary = undoReq.toBinary() + const binary = toBinary(GatewayRequestSchema, undoReq) this.socket.send(binary) } break } case 'expand': { { - const undoReq = new GatewayRequest({ + const undoReq = create(GatewayRequestSchema, { requestType: { case: 'undoExpand', - value: new UndoExpand({ + value: create(UndoExpandSchema, { UUID: request.requestType.value.UUID, }), }, }) - const binary = undoReq.toBinary() + const binary = toBinary(GatewayRequestSchema, undoReq) this.socket.send(binary) } break @@ -637,7 +640,7 @@ export class GatewaySession extends EventTarget { bookmark.msgID = parse(v4()) } - const req = new GatewayRequest({ + const req = create(GatewayRequestSchema, { requestType: { case: 'storeBookmark', value: bookmark, @@ -673,7 +676,7 @@ export class GatewaySession extends EventTarget { snapshot.msgID = parse(v4()) } - const req = new GatewayRequest({ + const req = create(GatewayRequestSchema, { requestType: { case: 'storeSnapshot', value: snapshot, diff --git a/src/index.ts b/src/index.ts index 0fb49ea..a73b034 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,5 +3,4 @@ export * from './request-progress' export * from './responder' export * from './util' export * from './protobuf' -export * from './services' export * from './connect' diff --git a/src/protobuf.ts b/src/protobuf.ts index 76e0d05..b877497 100644 --- a/src/protobuf.ts +++ b/src/protobuf.ts @@ -12,4 +12,3 @@ export * from './__generated__/revlink_pb' export * from './__generated__/snapshots_pb' export * from './__generated__/config_pb' export * from './__generated__/util_pb' -export type { PartialMessage } from '@bufbuild/protobuf' diff --git a/src/services.ts b/src/services.ts deleted file mode 100644 index d549a5d..0000000 --- a/src/services.ts +++ /dev/null @@ -1,9 +0,0 @@ -// Services -export * from './__generated__/account_connect' -export * from './__generated__/apikeys_connect' -export * from './__generated__/auth0support_connect' -export * from './__generated__/bookmarks_connect' -export * from './__generated__/changes_connect' -export * from './__generated__/invites_connect' -export * from './__generated__/snapshots_connect' -export * from './__generated__/config_connect' diff --git a/src/util.ts b/src/util.ts index bb21c97..56b0aa5 100644 --- a/src/util.ts +++ b/src/util.ts @@ -1,5 +1,17 @@ -import { Duration, JsonValue, Struct, Timestamp } from '@bufbuild/protobuf' -import { Item, ItemAttributes, Reference } from './protobuf' +import { create, JsonValue } from '@bufbuild/protobuf' +import { + Item, + ItemAttributes, + ItemAttributesSchema, + Reference, + ReferenceSchema, +} from './protobuf' +import { + Duration, + DurationSchema, + Timestamp, + TimestampSchema, +} from '@bufbuild/protobuf/wkt' /** * Creates a new ItemAttributes object from any javascript object that has @@ -10,9 +22,8 @@ import { Item, ItemAttributes, Reference } from './protobuf' export function newItemAttributes(value: { [key: string]: JsonValue }): ItemAttributes { - const attributes = new ItemAttributes() - attributes.attrStruct = new Struct() - attributes.attrStruct.fromJson(value) + const attributes = create(ItemAttributesSchema) + attributes.attrStruct = value return attributes } @@ -23,7 +34,7 @@ export function newItemAttributes(value: { * @returns A reference to that item */ export function toReference(item: Item): Reference { - const reference = new Reference() + const reference = create(ReferenceSchema) reference.scope = item.scope reference.type = item.type reference.uniqueAttributeValue = getUniqueAttributeValue(item) @@ -37,7 +48,7 @@ export function toReference(item: Item): Reference { * @returns A timestamp in protobuf format */ export function newTimestamp(date: Date): Timestamp { - const t = new Timestamp() + const t = create(TimestampSchema) t.seconds = BigInt(Math.floor(date.getTime() / 1000)) t.nanos = date.getMilliseconds() * 1_000_000 return t @@ -49,10 +60,9 @@ export function newTimestamp(date: Date): Timestamp { * @returns A duration in protobuf format */ export function newDuration(ms: number): Duration { - const d = new Duration({ - nanos: (ms % 1000) * 1e6, - seconds: BigInt(Math.floor(ms / 1000)), - }) + const d = create(DurationSchema) + d.nanos = (ms % 1000) * 1e6 + d.seconds = BigInt(Math.floor(ms / 1000)) return d } @@ -65,7 +75,10 @@ export function newDuration(ms: number): Duration { */ export function newDeadline(ms: number): Timestamp { const deadline = new Date(Date.now() + ms) - return Timestamp.fromDate(deadline) + const t = create(TimestampSchema) + t.seconds = BigInt(Math.floor(deadline.getTime() / 1000)) + t.nanos = deadline.getMilliseconds() * 1_000_000 + return t } /** @@ -78,13 +91,12 @@ export function newDeadline(ms: number): Timestamp { export function getAttributeValue( attributes: ItemAttributes, name: string, -): T | undefined { - const j = attributes.attrStruct?.toJson() +): JsonValue | undefined { + const j = attributes.attrStruct if (!j) { return undefined } - // @ts-expect-error // come back and type this properly. return j[name] }