From 7b71b6d60e0fae080aef38f594cb8d27ebd3ee3a Mon Sep 17 00:00:00 2001 From: Amy Yan Date: Wed, 1 Nov 2023 13:39:05 +1100 Subject: [PATCH 1/4] feat: bump `@matrixai/rpc` to `0.2.6` and moved `timeoutMiddleware` from `src/client` to `@matrixai/rpc` chore: lintfix [ci-skip] --- package-lock.json | 8 +- package.json | 2 +- src/PolykeyClient.ts | 2 +- src/client/ClientService.ts | 2 +- src/client/index.ts | 1 - src/client/middleware.ts | 31 +-- src/client/timeoutMiddleware.ts | 104 --------- src/client/types.ts | 30 +-- src/nodes/NodeConnectionManager.ts | 2 +- src/nodes/agent/handlers/VaultsGitInfoGet.ts | 4 +- src/nodes/agent/handlers/VaultsGitPackGet.ts | 7 +- src/nodes/agent/types.ts | 37 ++-- tests/client/timeoutMiddleware.test.ts | 213 ------------------- 13 files changed, 50 insertions(+), 393 deletions(-) delete mode 100644 src/client/timeoutMiddleware.ts delete mode 100644 tests/client/timeoutMiddleware.test.ts diff --git a/package-lock.json b/package-lock.json index 5b52275e8..2fae4588a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -21,7 +21,7 @@ "@matrixai/mdns": "^1.2.3", "@matrixai/quic": "^1.1.1", "@matrixai/resources": "^1.1.5", - "@matrixai/rpc": "^0.2.4", + "@matrixai/rpc": "^0.2.6", "@matrixai/timer": "^1.1.2", "@matrixai/workers": "^1.3.7", "@matrixai/ws": "^1.1.7", @@ -1637,9 +1637,9 @@ "integrity": "sha512-m/DEZEe3wHqWEPTyoBtzFF6U9vWYhEnQtGgwvqiAlTxTM0rk96UBpWjDZCTF/vYG11ZlmlQFtg5H+zGgbjaB3Q==" }, "node_modules/@matrixai/rpc": { - "version": "0.2.4", - "resolved": "https://registry.npmjs.org/@matrixai/rpc/-/rpc-0.2.4.tgz", - "integrity": "sha512-OvIjAE00aYuufyeMbpMD+QXMAqrLVHNHMOU8kEGTiOg4fKoPi95/taD4jNvRsPFtvxY3SsHaRfkI9zN6Lf6iUA==", + "version": "0.2.6", + "resolved": "https://registry.npmjs.org/@matrixai/rpc/-/rpc-0.2.6.tgz", + "integrity": "sha512-FLd/w1vLSYYFBMpaQ2VG4haUyWugD9/CkSLPusMz/mfY/Q2gGmtXb7LfuQhsAqasU6n8iA+7fEF3u3T3fLeLBA==", "dependencies": { "@matrixai/async-init": "^1.9.4", "@matrixai/contexts": "^1.2.0", diff --git a/package.json b/package.json index 09bf020f9..29ec8ba10 100644 --- a/package.json +++ b/package.json @@ -77,7 +77,7 @@ "@matrixai/mdns": "^1.2.3", "@matrixai/quic": "^1.1.1", "@matrixai/resources": "^1.1.5", - "@matrixai/rpc": "^0.2.4", + "@matrixai/rpc": "^0.2.6", "@matrixai/timer": "^1.1.2", "@matrixai/workers": "^1.3.7", "@matrixai/ws": "^1.1.7", diff --git a/src/PolykeyClient.ts b/src/PolykeyClient.ts index ccd7531bf..95c3fe4d9 100644 --- a/src/PolykeyClient.ts +++ b/src/PolykeyClient.ts @@ -297,7 +297,7 @@ class PolykeyClient { optionsDefaulted.rpcParserBufferSize, ), toError: networkUtils.toError, - streamKeepAliveTimeoutTime: optionsDefaulted.rpcCallTimeoutTime, + timeoutTime: optionsDefaulted.rpcCallTimeoutTime, logger: this.logger.getChild(RPCClient.name), }); this._nodeId = nodeId_; diff --git a/src/client/ClientService.ts b/src/client/ClientService.ts index a193ac2c8..9d48514e7 100644 --- a/src/client/ClientService.ts +++ b/src/client/ClientService.ts @@ -83,7 +83,7 @@ class ClientService { this.logger = logger ?? new Logger(this.constructor.name); this.rpcServer = new RPCServer({ idGen, - handlerTimeoutTime: rpcCallTimeoutTime, + timeoutTime: rpcCallTimeoutTime, middlewareFactory: rpcMiddleware.defaultServerMiddlewareWrapper( middlewareFactory, rpcParserBufferSize, diff --git a/src/client/index.ts b/src/client/index.ts index 5bd24a8c3..c339d6e97 100644 --- a/src/client/index.ts +++ b/src/client/index.ts @@ -5,6 +5,5 @@ export * as handlers from './handlers'; export * as utils from './utils'; export * as middleware from './middleware'; export * as authenticationMiddleware from './authenticationMiddleware'; -export * as timeoutMiddleWare from './timeoutMiddleware'; export * as errors from './errors'; export * as types from './types'; diff --git a/src/client/middleware.ts b/src/client/middleware.ts index 06b179564..e556e840d 100644 --- a/src/client/middleware.ts +++ b/src/client/middleware.ts @@ -8,7 +8,6 @@ import type { import type SessionManager from '../sessions/SessionManager'; import type KeyRing from '../keys/KeyRing'; import * as authenticationMiddlewareUtils from './authenticationMiddleware'; -import * as timeoutMiddlewareUtils from './timeoutMiddleware'; function middlewareServer( sessionManager: SessionManager, @@ -26,24 +25,15 @@ function middlewareServer( ); return (ctx, cancel, meta) => { const authMiddleware = authMiddlewareFactory(ctx, cancel, meta); - const timeoutMiddleware = timeoutMiddlewareUtils.timeoutMiddlewareServer( - ctx, - cancel, - meta, - ); // Order is auth -> timeout return { forward: { writable: authMiddleware.forward.writable, - readable: authMiddleware.forward.readable.pipeThrough( - timeoutMiddleware.forward, - ), + readable: authMiddleware.forward.readable, }, reverse: { - writable: timeoutMiddleware.reverse.writable, - readable: timeoutMiddleware.reverse.readable.pipeThrough( - authMiddleware.reverse, - ), + writable: authMiddleware.reverse.writable, + readable: authMiddleware.reverse.readable, }, }; }; @@ -61,24 +51,15 @@ function middlewareClient( authenticationMiddlewareUtils.authenticationMiddlewareClient(session); return (ctx, cancel, meta) => { const authMiddleware = authMiddlewareFactory(ctx, cancel, meta); - const timeoutMiddleware = timeoutMiddlewareUtils.timeoutMiddlewareClient( - ctx, - cancel, - meta, - ); // Order is timeout -> auth return { forward: { - writable: timeoutMiddleware.forward.writable, - readable: timeoutMiddleware.forward.readable.pipeThrough( - authMiddleware.forward, - ), + writable: authMiddleware.forward.writable, + readable: authMiddleware.forward.readable, }, reverse: { writable: authMiddleware.reverse.writable, - readable: authMiddleware.reverse.readable.pipeThrough( - timeoutMiddleware.reverse, - ), + readable: authMiddleware.reverse.readable, }, }; }; diff --git a/src/client/timeoutMiddleware.ts b/src/client/timeoutMiddleware.ts deleted file mode 100644 index a66198962..000000000 --- a/src/client/timeoutMiddleware.ts +++ /dev/null @@ -1,104 +0,0 @@ -import type { ContextTimed } from '@matrixai/contexts'; -import type { JSONRPCRequest, JSONRPCResponse } from '@matrixai/rpc'; -import type { ClientRPCRequestParams, ClientRPCResponseResult } from './types'; -import type { JSONValue } from '@matrixai/rpc'; -import { TransformStream } from 'stream/web'; - -/** - * This adds its timeout to the reverse metadata and updates it's timeout based - * on the forward metadata. - */ -function timeoutMiddlewareServer( - ctx: ContextTimed, - _cancel: (reason?: any) => void, - _meta: Record | undefined, -) { - const currentTimeout = ctx.timer.delay; - // Flags for tracking if the first message has been processed - let forwardFirst = true; - let reverseFirst = true; - return { - forward: new TransformStream< - JSONRPCRequest, - JSONRPCRequest - >({ - transform: (chunk, controller) => { - controller.enqueue(chunk); - if (forwardFirst) { - forwardFirst = false; - const clientTimeout = chunk.params?.metadata?.timeout; - - if (clientTimeout == null) return; - if (clientTimeout < currentTimeout) ctx.timer.reset(clientTimeout); - } - }, - }), - reverse: new TransformStream< - JSONRPCResponse, - JSONRPCResponse - >({ - transform: (chunk, controller) => { - if (reverseFirst) { - reverseFirst = false; - if ('result' in chunk) { - if (chunk.result.metadata == null) chunk.result.metadata = {}; - chunk.result.metadata.timeout = currentTimeout; - } - } - controller.enqueue(chunk); - }, - }), - }; -} - -/** - * This adds its own timeout to the forward metadata and updates it's timeout - * based on the reverse metadata. - * @param ctx - * @param _cancel - * @param _meta - */ -function timeoutMiddlewareClient( - ctx: ContextTimed, - _cancel: (reason?: any) => void, - _meta: Record | undefined, -) { - const currentTimeout = ctx.timer.delay; - // Flags for tracking if the first message has been processed - let forwardFirst = true; - let reverseFirst = true; - return { - forward: new TransformStream< - JSONRPCRequest, - JSONRPCRequest - >({ - transform: (chunk, controller) => { - if (forwardFirst) { - forwardFirst = false; - if (chunk.params == null) chunk.params = {}; - if (chunk.params.metadata == null) chunk.params.metadata = {}; - chunk.params.metadata.timeout = currentTimeout; - } - controller.enqueue(chunk); - }, - }), - reverse: new TransformStream< - JSONRPCResponse, - JSONRPCResponse - >({ - transform: (chunk, controller) => { - controller.enqueue(chunk); - if (reverseFirst) { - reverseFirst = false; - if ('result' in chunk) { - const clientTimeout = chunk.result?.metadata?.timeout; - if (clientTimeout == null) return; - if (clientTimeout < currentTimeout) ctx.timer.reset(clientTimeout); - } - } - }, - }), - }; -} - -export { timeoutMiddlewareServer, timeoutMiddlewareClient }; diff --git a/src/client/types.ts b/src/client/types.ts index d154d36d8..56e4bf08b 100644 --- a/src/client/types.ts +++ b/src/client/types.ts @@ -1,4 +1,9 @@ -import type { JSONValue, ObjectEmpty } from '../types'; +import type { + JSONObject, + JSONValue, + JSONRPCParams, + JSONRPCResult, +} from '@matrixai/rpc'; import type { GestaltIdEncoded, IdentityId, @@ -18,27 +23,22 @@ import type { Notification } from '../notifications/types'; import type { ProviderToken } from '../identities/types'; // Prevent overwriting the metadata type with `Omit<>` -type ClientRPCRequestParams = ObjectEmpty> = - { - metadata?: { - [Key: string]: JSONValue; - } & Partial<{ - authorization: string; - timeout: number; - }>; - } & Omit; +type ClientRPCRequestParams = { + metadata?: { + [Key: string]: JSONValue; + } & Partial<{ + authorization: string; + }>; +} & JSONRPCParams; // Prevent overwriting the metadata type with `Omit<>` -type ClientRPCResponseResult< - T extends Record = ObjectEmpty, -> = { +type ClientRPCResponseResult = { metadata?: { [Key: string]: JSONValue; } & Partial<{ authorization: string; - timeout: number; }>; -} & Omit; +} & JSONRPCResult; type StatusResultMessage = { pid: number; diff --git a/src/nodes/NodeConnectionManager.ts b/src/nodes/NodeConnectionManager.ts index 7e75a9615..2299957a0 100644 --- a/src/nodes/NodeConnectionManager.ts +++ b/src/nodes/NodeConnectionManager.ts @@ -454,7 +454,7 @@ class NodeConnectionManager { this.rpcParserBufferSize, ), fromError: networkUtils.fromError, - handlerTimeoutTime: this.rpcCallTimeoutTime, + timeoutTime: this.rpcCallTimeoutTime, logger: this.logger.getChild(RPCServer.name), }); diff --git a/src/nodes/agent/handlers/VaultsGitInfoGet.ts b/src/nodes/agent/handlers/VaultsGitInfoGet.ts index ed9e32f87..0c820129b 100644 --- a/src/nodes/agent/handlers/VaultsGitInfoGet.ts +++ b/src/nodes/agent/handlers/VaultsGitInfoGet.ts @@ -1,10 +1,10 @@ import type { DB } from '@matrixai/db'; import type Logger from '@matrixai/logger'; +import type { JSONObject, JSONRPCRequest } from '@matrixai/rpc'; import type { ContextTimed } from '@matrixai/contexts'; import type ACL from '../../../acl/ACL'; import type VaultManager from '../../../vaults/VaultManager'; import type { JSONValue } from '../../../types'; -import type { JSONRPCRequest } from '@matrixai/rpc'; import { ReadableStream } from 'stream/web'; import { RawHandler } from '@matrixai/rpc'; import * as agentErrors from '../errors'; @@ -28,7 +28,7 @@ class VaultsGitInfoGet extends RawHandler<{ _cancel, meta: Record | undefined, _ctx: ContextTimed, // TODO: use - ): Promise<[JSONValue, ReadableStream]> => { + ): Promise<[JSONObject, ReadableStream]> => { const { db, vaultManager, acl } = this.container; const [headerMessage, inputStream] = input; await inputStream.cancel(); diff --git a/src/nodes/agent/handlers/VaultsGitPackGet.ts b/src/nodes/agent/handlers/VaultsGitPackGet.ts index b6a0472ed..9fdec6f82 100644 --- a/src/nodes/agent/handlers/VaultsGitPackGet.ts +++ b/src/nodes/agent/handlers/VaultsGitPackGet.ts @@ -1,10 +1,9 @@ import type { DB } from '@matrixai/db'; +import type { JSONObject, JSONRPCRequest } from '@matrixai/rpc'; import type { PassThrough } from 'readable-stream'; import type { VaultName } from '../../../vaults/types'; import type ACL from '../../../acl/ACL'; import type VaultManager from '../../../vaults/VaultManager'; -import type { JSONValue } from '../../../types'; -import type { JSONRPCRequest } from '@matrixai/rpc'; import { ReadableStream } from 'stream/web'; import { RawHandler } from '@matrixai/rpc'; import * as agentErrors from '../errors'; @@ -26,7 +25,7 @@ class VaultsGitPackGet extends RawHandler<{ input: [JSONRPCRequest, ReadableStream], _cancel, meta, - ): Promise<[JSONValue, ReadableStream]> => { + ): Promise<[JSONObject, ReadableStream]> => { const { vaultManager, acl, db } = this.container; const [headerMessage, inputStream] = input; const requestingNodeId = agentUtils.nodeIdFromMeta(meta); @@ -103,7 +102,7 @@ class VaultsGitPackGet extends RawHandler<{ sideBand.destroy(e); }, }); - return [null, outputStream]; + return [{}, outputStream]; }; } diff --git a/src/nodes/agent/types.ts b/src/nodes/agent/types.ts index a96242396..2aa91905a 100644 --- a/src/nodes/agent/types.ts +++ b/src/nodes/agent/types.ts @@ -1,30 +1,25 @@ +import type { JSONObject, JSONRPCParams, JSONRPCResult } from '@matrixai/rpc'; import type { SignedTokenEncoded } from '../../tokens/types'; import type { ClaimIdEncoded, NodeIdEncoded, VaultIdEncoded } from '../../ids'; import type { VaultAction, VaultName } from '../../vaults/types'; import type { SignedNotification } from '../../notifications/types'; -import type { JSONValue, ObjectEmpty } from '../../types'; +import type { JSONValue } from '../../types'; -// Prevent overwriting the metadata type with `Omit<>` -type AgentRPCRequestParams = ObjectEmpty> = - { - metadata?: { - [Key: string]: JSONValue; - } & Partial<{ - authorization: string; - timeout: number; - }>; - } & Omit; +type AgentRPCRequestParams = { + metadata?: { + [Key: string]: JSONValue; + } & Partial<{ + authorization: string; + }>; +} & JSONRPCParams; -// Prevent overwriting the metadata type with `Omit<>` -type AgentRPCResponseResult = ObjectEmpty> = - { - metadata?: { - [Key: string]: JSONValue; - } & Partial<{ - authorization: string; - timeout: number; - }>; - } & Omit; +type AgentRPCResponseResult = { + metadata?: { + [Key: string]: JSONValue; + } & Partial<{ + authorization: string; + }>; +} & JSONRPCResult; type ClaimIdMessage = { claimIdEncoded: ClaimIdEncoded; diff --git a/tests/client/timeoutMiddleware.test.ts b/tests/client/timeoutMiddleware.test.ts deleted file mode 100644 index c4b2f2ef0..000000000 --- a/tests/client/timeoutMiddleware.test.ts +++ /dev/null @@ -1,213 +0,0 @@ -import type { ContextTimed } from '@matrixai/contexts'; -import type { - ClientRPCRequestParams, - ClientRPCResponseResult, -} from '@/client/types'; -import type { TLSConfig } from '@/network/types'; -import fs from 'fs'; -import path from 'path'; -import os from 'os'; -import Logger, { LogLevel, StreamHandler } from '@matrixai/logger'; -import { DB } from '@matrixai/db'; -import { Timer } from '@matrixai/timer'; -import { - RPCClient, - UnaryCaller, - UnaryHandler, - middleware as rpcUtilsMiddleware, -} from '@matrixai/rpc'; -import { WebSocketClient } from '@matrixai/ws'; -import KeyRing from '@/keys/KeyRing'; -import TaskManager from '@/tasks/TaskManager'; -import CertManager from '@/keys/CertManager'; -import ClientService from '@/client/ClientService'; -import * as timeoutMiddleware from '@/client/timeoutMiddleware'; -import * as keysUtils from '@/keys/utils'; -import * as networkUtils from '@/network/utils'; -import * as utils from '@/utils'; -import * as testsUtils from '../utils'; - -describe('timeoutMiddleware', () => { - const logger = new Logger('agentUnlock test', LogLevel.WARN, [ - new StreamHandler(), - ]); - const password = 'helloWorld'; - const localhost = '127.0.0.1'; - let dataDir: string; - let db: DB; - let keyRing: KeyRing; - let taskManager: TaskManager; - let certManager: CertManager; - let clientService: ClientService; - let clientClient: WebSocketClient; - let tlsConfig: TLSConfig; - - beforeEach(async () => { - dataDir = await fs.promises.mkdtemp( - path.join(os.tmpdir(), 'polykey-test-'), - ); - const keysPath = path.join(dataDir, 'keys'); - const dbPath = path.join(dataDir, 'db'); - db = await DB.createDB({ - dbPath, - logger, - }); - keyRing = await KeyRing.createKeyRing({ - password, - keysPath, - logger, - passwordOpsLimit: keysUtils.passwordOpsLimits.min, - passwordMemLimit: keysUtils.passwordMemLimits.min, - strictMemoryLock: false, - }); - taskManager = await TaskManager.createTaskManager({ db, logger }); - certManager = await CertManager.createCertManager({ - db, - keyRing, - taskManager, - logger, - }); - tlsConfig = await testsUtils.createTLSConfig(keyRing.keyPair); - }); - afterEach(async () => { - await taskManager.stopProcessing(); - await taskManager.stopTasks(); - await clientService?.stop({ force: true }); - await clientClient?.destroy({ force: true }); - await certManager.stop(); - await taskManager.stop(); - await keyRing.stop(); - await db.stop(); - await fs.promises.rm(dataDir, { - force: true, - recursive: true, - }); - }); - test('server side timeout updates', async () => { - // Setup - const ctxProm = utils.promise(); - class EchoHandler extends UnaryHandler< - { logger: Logger }, - ClientRPCRequestParams, - ClientRPCResponseResult - > { - public handle = async ( - input: ClientRPCRequestParams, - _cancel, - _meta, - ctx, - ) => { - ctxProm.resolveP(ctx); - return input; - }; - } - clientService = new ClientService({ - tlsConfig, - middlewareFactory: timeoutMiddleware.timeoutMiddlewareServer, - logger: logger.getChild(ClientService.name), - }); - await clientService.start({ - manifest: { - testHandler: new EchoHandler({ logger }), - }, - host: localhost, - }); - clientClient = await WebSocketClient.createWebSocketClient({ - config: { - verifyPeer: false, - }, - host: localhost, - port: clientService.port, - logger, - }); - const rpcClient = new RPCClient({ - manifest: { - testHandler: new UnaryCaller< - ClientRPCRequestParams, - ClientRPCResponseResult - >(), - }, - streamFactory: () => clientClient.connection.newStream(), - toError: networkUtils.toError, - middlewareFactory: rpcUtilsMiddleware.defaultClientMiddlewareWrapper( - timeoutMiddleware.timeoutMiddlewareClient, - ), - logger, - }); - - // Doing the test - const timer = new Timer({ - delay: 100, - }); - await rpcClient.methods.testHandler({}, { timer }); - - const ctx = await ctxProm.p; - expect(ctx.timer.delay).toBe(100); - timer.cancel(); - await timer.then( - () => {}, - () => {}, - ); - await clientService.stop({ force: true }); - }); - test('client side timeout updates', async () => { - // Setup - class EchoHandler extends UnaryHandler< - { logger: Logger }, - ClientRPCRequestParams, - ClientRPCResponseResult - > { - public handle = async ( - input: ClientRPCRequestParams, - _cancel, - _meta, - _ctx, - ) => { - return input; - }; - } - clientService = new ClientService({ - tlsConfig, - middlewareFactory: timeoutMiddleware.timeoutMiddlewareServer, - rpcCallTimeoutTime: 100, - logger: logger.getChild(ClientService.name), - }); - await clientService.start({ - manifest: { - testHandler: new EchoHandler({ logger }), - }, - host: localhost, - }); - clientClient = await WebSocketClient.createWebSocketClient({ - config: { - verifyPeer: false, - }, - host: localhost, - port: clientService.port, - logger, - }); - const rpcClient = new RPCClient({ - idGen: async () => null, - manifest: { - testHandler: new UnaryCaller< - ClientRPCRequestParams, - ClientRPCResponseResult - >(), - }, - streamFactory: async () => clientClient.connection.newStream(), - toError: networkUtils.toError, - middlewareFactory: rpcUtilsMiddleware.defaultClientMiddlewareWrapper( - timeoutMiddleware.timeoutMiddlewareClient, - ), - logger, - }); - - // Doing the test - const timer = new Timer({ - delay: 1000, - }); - await rpcClient.methods.testHandler({}, { timer }); - expect(timer.delay).toBe(100); - timer.cancel(); - }); -}); From b2ca8c5d2596d8e232b2bea7101b8efa1c6a1d3f Mon Sep 17 00:00:00 2001 From: Amy Yan Date: Wed, 1 Nov 2023 13:57:15 +1100 Subject: [PATCH 2/4] fix: `timeout` for `IdentitiesAuthenticate` is now 2 minutes [ci-skip] --- src/client/handlers/IdentitiesAuthenticate.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/client/handlers/IdentitiesAuthenticate.ts b/src/client/handlers/IdentitiesAuthenticate.ts index 0d2567717..7dad24045 100644 --- a/src/client/handlers/IdentitiesAuthenticate.ts +++ b/src/client/handlers/IdentitiesAuthenticate.ts @@ -20,6 +20,7 @@ class IdentitiesAuthenticate extends ServerHandler< }>, ClientRPCResponseResult > { + public timeout = 120000; // 2 Minutes public async *handle( input: ClientRPCRequestParams<{ providerId: string; From af8cd4fed7e826189969675fd9f50b3af9cc4b31 Mon Sep 17 00:00:00 2001 From: Amy Yan Date: Wed, 1 Nov 2023 14:19:09 +1100 Subject: [PATCH 3/4] fix: `metadata.authorization` has been removed from `AgentRPCRequestParams` and `AgentRPCResponseResult` [ci-skip] --- src/nodes/agent/types.ts | 19 ++++--------------- 1 file changed, 4 insertions(+), 15 deletions(-) diff --git a/src/nodes/agent/types.ts b/src/nodes/agent/types.ts index 2aa91905a..33da0acb0 100644 --- a/src/nodes/agent/types.ts +++ b/src/nodes/agent/types.ts @@ -3,23 +3,12 @@ import type { SignedTokenEncoded } from '../../tokens/types'; import type { ClaimIdEncoded, NodeIdEncoded, VaultIdEncoded } from '../../ids'; import type { VaultAction, VaultName } from '../../vaults/types'; import type { SignedNotification } from '../../notifications/types'; -import type { JSONValue } from '../../types'; -type AgentRPCRequestParams = { - metadata?: { - [Key: string]: JSONValue; - } & Partial<{ - authorization: string; - }>; -} & JSONRPCParams; +type AgentRPCRequestParams = + JSONRPCParams; -type AgentRPCResponseResult = { - metadata?: { - [Key: string]: JSONValue; - } & Partial<{ - authorization: string; - }>; -} & JSONRPCResult; +type AgentRPCResponseResult = + JSONRPCResult; type ClaimIdMessage = { claimIdEncoded: ClaimIdEncoded; From 77d7fd71e22c0a90262610edd6b7ae4edd240fe5 Mon Sep 17 00:00:00 2001 From: Amy Yan Date: Thu, 2 Nov 2023 15:19:23 +1100 Subject: [PATCH 4/4] chore: bumped `@matrixai/rpc` to `0.4.1` and updated `JSONRPC` types to be less confusing chore: bump `@matrixai/rpc` to `0.3.1` [ci-skip] --- package-lock.json | 8 +++--- package.json | 2 +- src/client/authenticationMiddleware.ts | 9 ++++-- src/client/types.ts | 39 +++++++++++--------------- src/nodes/agent/types.ts | 10 +++++-- 5 files changed, 34 insertions(+), 34 deletions(-) diff --git a/package-lock.json b/package-lock.json index 2fae4588a..28c9b494f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -21,7 +21,7 @@ "@matrixai/mdns": "^1.2.3", "@matrixai/quic": "^1.1.1", "@matrixai/resources": "^1.1.5", - "@matrixai/rpc": "^0.2.6", + "@matrixai/rpc": "^0.4.1", "@matrixai/timer": "^1.1.2", "@matrixai/workers": "^1.3.7", "@matrixai/ws": "^1.1.7", @@ -1637,9 +1637,9 @@ "integrity": "sha512-m/DEZEe3wHqWEPTyoBtzFF6U9vWYhEnQtGgwvqiAlTxTM0rk96UBpWjDZCTF/vYG11ZlmlQFtg5H+zGgbjaB3Q==" }, "node_modules/@matrixai/rpc": { - "version": "0.2.6", - "resolved": "https://registry.npmjs.org/@matrixai/rpc/-/rpc-0.2.6.tgz", - "integrity": "sha512-FLd/w1vLSYYFBMpaQ2VG4haUyWugD9/CkSLPusMz/mfY/Q2gGmtXb7LfuQhsAqasU6n8iA+7fEF3u3T3fLeLBA==", + "version": "0.4.1", + "resolved": "https://registry.npmjs.org/@matrixai/rpc/-/rpc-0.4.1.tgz", + "integrity": "sha512-DF5hxdE8riMdlBgV2o+a7lEPA3uPwQTrKf9B9yOPJBEr6tfkNALTBzc/kvsedRPQeOcPYPZpmEOtyNhRtRAGcg==", "dependencies": { "@matrixai/async-init": "^1.9.4", "@matrixai/contexts": "^1.2.0", diff --git a/package.json b/package.json index 29ec8ba10..5202feccd 100644 --- a/package.json +++ b/package.json @@ -77,7 +77,7 @@ "@matrixai/mdns": "^1.2.3", "@matrixai/quic": "^1.1.1", "@matrixai/resources": "^1.1.5", - "@matrixai/rpc": "^0.2.6", + "@matrixai/rpc": "^0.4.1", "@matrixai/timer": "^1.1.2", "@matrixai/workers": "^1.3.7", "@matrixai/ws": "^1.1.7", diff --git a/src/client/authenticationMiddleware.ts b/src/client/authenticationMiddleware.ts index 5214144b5..e2fe30f26 100644 --- a/src/client/authenticationMiddleware.ts +++ b/src/client/authenticationMiddleware.ts @@ -7,7 +7,10 @@ import type { ClientRPCRequestParams, ClientRPCResponseResult } from './types'; import type { Session } from '../sessions'; import type SessionManager from '../sessions/SessionManager'; import type KeyRing from '../keys/KeyRing'; -import type { JSONRPCError, JSONRPCResponseError } from '@matrixai/rpc'; +import type { + JSONRPCResponseError, + JSONRPCResponseFailed, +} from '@matrixai/rpc'; import { TransformStream } from 'stream/web'; import { authenticate, decodeAuth } from './utils'; import { sysexits } from '../errors'; @@ -43,12 +46,12 @@ function authenticationMiddlewareServer( ); } catch (e) { controller.error(e); - const rpcError: JSONRPCError = { + const rpcError: JSONRPCResponseError = { code: e.exitCode ?? sysexits.UNKNOWN, message: e.description ?? '', data: networkUtils.fromError(e), }; - const rpcErrorMessage: JSONRPCResponseError = { + const rpcErrorMessage: JSONRPCResponseFailed = { jsonrpc: '2.0', error: rpcError, id: null, diff --git a/src/client/types.ts b/src/client/types.ts index 56e4bf08b..32d23361f 100644 --- a/src/client/types.ts +++ b/src/client/types.ts @@ -1,9 +1,4 @@ -import type { - JSONObject, - JSONValue, - JSONRPCParams, - JSONRPCResult, -} from '@matrixai/rpc'; +import type { JSONObject, JSONRPCResponseResult } from '@matrixai/rpc'; import type { GestaltIdEncoded, IdentityId, @@ -22,23 +17,21 @@ import type { import type { Notification } from '../notifications/types'; import type { ProviderToken } from '../identities/types'; -// Prevent overwriting the metadata type with `Omit<>` -type ClientRPCRequestParams = { - metadata?: { - [Key: string]: JSONValue; - } & Partial<{ - authorization: string; - }>; -} & JSONRPCParams; - -// Prevent overwriting the metadata type with `Omit<>` -type ClientRPCResponseResult = { - metadata?: { - [Key: string]: JSONValue; - } & Partial<{ - authorization: string; - }>; -} & JSONRPCResult; +type ClientRPCRequestParams = + JSONRPCResponseResult< + T, + Partial<{ + authorization: string; + }> + >; + +type ClientRPCResponseResult = + JSONRPCResponseResult< + T, + Partial<{ + authorization: string; + }> + >; type StatusResultMessage = { pid: number; diff --git a/src/nodes/agent/types.ts b/src/nodes/agent/types.ts index 33da0acb0..7c4252501 100644 --- a/src/nodes/agent/types.ts +++ b/src/nodes/agent/types.ts @@ -1,14 +1,18 @@ -import type { JSONObject, JSONRPCParams, JSONRPCResult } from '@matrixai/rpc'; +import type { + JSONObject, + JSONRPCRequestParams, + JSONRPCResponseResult, +} from '@matrixai/rpc'; import type { SignedTokenEncoded } from '../../tokens/types'; import type { ClaimIdEncoded, NodeIdEncoded, VaultIdEncoded } from '../../ids'; import type { VaultAction, VaultName } from '../../vaults/types'; import type { SignedNotification } from '../../notifications/types'; type AgentRPCRequestParams = - JSONRPCParams; + JSONRPCRequestParams; type AgentRPCResponseResult = - JSONRPCResult; + JSONRPCResponseResult; type ClaimIdMessage = { claimIdEncoded: ClaimIdEncoded;