@metamask-previews/json-rpc-middleware-stream 8.0.8-preview-bfee350 → 8.0.8-preview-5c1b2b1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- {"version":3,"file":"createStreamMiddleware.cjs","sourceRoot":"","sources":["../src/createStreamMiddleware.ts"],"names":[],"mappings":";;;;;AAKA,sFAA4D;AAC5D,2CAMyB;AACzB,qDAAyC;AAkBzC;;;;;;;;GAQG;AACH,SAAwB,sBAAsB,CAAC,UAAmB,EAAE;IAClE,MAAM,KAAK,GAAU,EAAE,CAAC,CAAC,gCAAgC;IACzD,MAAM,MAAM,GAAG,IAAI,wBAAM,CAAC;QACxB,UAAU,EAAE,IAAI;QAChB,IAAI,EAAE,GAAG,EAAE,CAAC,SAAS;QACrB,KAAK,EAAE,cAAc;KACtB,CAAC,CAAC;IAEH,MAAM,MAAM,GAAG,IAAI,4BAAgB,EAAE,CAAC;IAEtC,MAAM,UAAU,GAAoD,CAClE,GAAG,EACH,GAAG,EACH,IAAI,EACJ,GAAG,EACH,EAAE;QACF,qFAAqF;QACrF,KAAK,CAAC,GAAG,CAAC,EAAuB,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,IAAI,EAAE,GAAG,EAAE,CAAC;QAC7D,sBAAsB;QACtB,YAAY,CAAC,GAAG,CAAC,CAAC;IACpB,CAAC,CAAC;IAEF,OAAO,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,EAAE,CAAC;IAEtC;;;;OAIG;IACH,SAAS,YAAY,CAAC,GAAmB;QACvC,mDAAmD;QACnD,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;IACnB,CAAC;IAED;;;;;;OAMG;IACH,SAAS,cAAc,CACrB,GAA0C,EAC1C,SAAkB,EAClB,mBAAmD;QAEnD,IAAI,QAAQ,GAAiB,IAAI,CAAC;QAClC,IAAI,CAAC;YACH,MAAM,cAAc,GAAG,CAAC,IAA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OAAO,CAAC,CAAC,EAAE,GAAG,EAAE,UAAU,GAAG,CAAC,EAAE,EAAE,EAAE;YACvD,2GAA2G;YAC3G,qFAAqF;YACrF,IAAI,CAAC,GAAG,CAAC,EAAE,EAAE,CAAC;gBACZ,OAAO;YACT,CAAC;YAED,IAAI,UAAU,IAAI,CAAC,EAAE,CAAC;gBACpB,MAAM,IAAI,KAAK,CACb,2DAA2D,GAAG,CAAC,EAAE,GAAG,CACrE,CAAC;YACJ,CAAC;YACD,MAAM,WAAW,GAAG,KAAK,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;YAClC,IAAI,WAAW,EAAE,CAAC;gBAChB,WAAW,CAAC,UAAU,GAAG,UAAU,GAAG,CAAC,CAAC;YAC1C,CAAC;YACD,YAAY,CAAC,GAAG,CAAC,CAAC;QACpB,CAAC,CAAC,CAAC;IACL,CAAC;AACH,CAAC;AAzHD,yCAyHC","sourcesContent":["import type {\n JsonRpcEngineNextCallback,\n JsonRpcEngineEndCallback,\n JsonRpcMiddleware,\n} from '@metamask/json-rpc-engine';\nimport SafeEventEmitter from '@metamask/safe-event-emitter';\nimport {\n hasProperty,\n type JsonRpcNotification,\n type JsonRpcParams,\n type JsonRpcRequest,\n type PendingJsonRpcResponse,\n} from '@metamask/utils';\nimport { Duplex } from 'readable-stream';\n\ntype IdMapValue = {\n req: JsonRpcRequest;\n res: PendingJsonRpcResponse<JsonRpcParams>;\n next: JsonRpcEngineNextCallback;\n end: JsonRpcEngineEndCallback;\n retryCount?: number;\n};\n\ntype IdMap = {\n [requestId: string]: IdMapValue;\n};\n\ntype Options = {\n retryOnMessage?: string;\n};\n\n/**\n * Creates a JsonRpcEngine middleware with an associated Duplex stream and\n * EventEmitter. The middleware, and by extension stream, assume that middleware\n * parameters are properly formatted. No runtime type checking or validation is\n * performed.\n *\n * @param options - Configuration options for middleware.\n * @returns The event emitter, middleware, and stream.\n */\nexport default function createStreamMiddleware(options: Options = {}) {\n const idMap: IdMap = {}; // TODO: replace with actual Map\n const stream = new Duplex({\n objectMode: true,\n read: () => undefined,\n write: processMessage,\n });\n\n const events = new SafeEventEmitter();\n\n const middleware: JsonRpcMiddleware<JsonRpcParams, JsonRpcParams> = (\n req,\n res,\n next,\n end,\n ) => {\n // register request on id map *before* sending it to the stream, to avoid race issues\n idMap[req.id as unknown as string] = { req, res, next, end };\n // write req to stream\n sendToStream(req);\n };\n\n return { events, middleware, stream };\n\n /**\n * Forwards JSON-RPC request to the stream.\n *\n * @param req - The JSON-RPC request object.\n */\n function sendToStream(req: JsonRpcRequest) {\n // TODO: limiting retries could be implemented here\n stream.push(req);\n }\n\n /**\n * Writes a JSON-RPC object to the stream.\n *\n * @param res - The JSON-RPC response object.\n * @param _encoding - The stream encoding, not used.\n * @param streamWriteCallback - The stream write callback.\n */\n function processMessage(\n res: PendingJsonRpcResponse<JsonRpcParams>,\n _encoding: unknown,\n streamWriteCallback: (error?: Error | null) => void,\n ) {\n let errorObj: Error | null = null;\n try {\n const isNotification = !hasProperty(res, 'id');\n if (isNotification) {\n processNotification(res as unknown as JsonRpcNotification);\n } else {\n processResponse(res);\n }\n } catch (_err) {\n errorObj = _err as Error;\n }\n // continue processing stream\n streamWriteCallback(errorObj);\n }\n\n /**\n * Processes a JSON-RPC response.\n *\n * @param res - The response to process.\n */\n function processResponse(res: PendingJsonRpcResponse<JsonRpcParams>) {\n const { id: responseId } = res;\n if (responseId === null) {\n return;\n }\n\n const context = idMap[responseId];\n if (!context) {\n console.warn(`StreamMiddleware - Unknown response id \"${responseId}\"`);\n return;\n }\n\n delete idMap[responseId];\n // copy whole res onto original res\n Object.assign(context.res, res);\n // run callback on empty stack,\n // prevent internal stream-handler from catching errors\n setTimeout(context.end);\n }\n\n /**\n * Processes a JSON-RPC notification.\n *\n * @param notif - The notification to process.\n */\n function processNotification(notif: JsonRpcNotification) {\n if (options?.retryOnMessage && notif.method === options.retryOnMessage) {\n retryStuckRequests();\n }\n events.emit('notification', notif);\n }\n\n /**\n * Retry pending requests.\n */\n function retryStuckRequests() {\n Object.values(idMap).forEach(({ req, retryCount = 0 }) => {\n // Avoid retrying requests without an id - they cannot have matching responses so retry logic doesn't apply\n // Check for retry count below ensure that a request is not retried more than 3 times\n if (!req.id) {\n return;\n }\n\n if (retryCount >= 3) {\n throw new Error(\n `StreamMiddleware - Retry limit exceeded for request id \"${req.id}\"`,\n );\n }\n const idMapObject = idMap[req.id];\n if (idMapObject) {\n idMapObject.retryCount = retryCount + 1;\n }\n sendToStream(req);\n });\n }\n}\n"]}
1
+ {"version":3,"file":"createStreamMiddleware.cjs","sourceRoot":"","sources":["../src/createStreamMiddleware.ts"],"names":[],"mappings":";;;;;AAKA,sFAA4D;AAC5D,2CAA8C;AAO9C,qDAAyC;AAkBzC;;;;;;;;GAQG;AACH,SAAwB,sBAAsB,CAAC,UAAmB,EAAE;IAClE,MAAM,KAAK,GAAU,EAAE,CAAC,CAAC,gCAAgC;IACzD,MAAM,MAAM,GAAG,IAAI,wBAAM,CAAC;QACxB,UAAU,EAAE,IAAI;QAChB,IAAI,EAAE,GAAG,EAAE,CAAC,SAAS;QACrB,KAAK,EAAE,cAAc;KACtB,CAAC,CAAC;IAEH,MAAM,MAAM,GAAG,IAAI,4BAAgB,EAAE,CAAC;IAEtC,MAAM,UAAU,GAAoD,CAClE,GAAG,EACH,GAAG,EACH,IAAI,EACJ,GAAG,EACH,EAAE;QACF,qFAAqF;QACrF,KAAK,CAAC,GAAG,CAAC,EAAuB,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,IAAI,EAAE,GAAG,EAAE,CAAC;QAC7D,sBAAsB;QACtB,YAAY,CAAC,GAAG,CAAC,CAAC;IACpB,CAAC,CAAC;IAEF,OAAO,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,EAAE,CAAC;IAEtC;;;;OAIG;IACH,SAAS,YAAY,CAAC,GAAmB;QACvC,mDAAmD;QACnD,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;IACnB,CAAC;IAED;;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vB,CAAC;QACD,MAAM,CAAC,IAAI,CAAC,cAAc,EAAE,KAAK,CAAC,CAAC;IACrC,CAAC;IAED;;OAEG;IACH,SAAS,kBAAkB;QACzB,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,OAAO,CAAC,CAAC,EAAE,GAAG,EAAE,UAAU,GAAG,CAAC,EAAE,EAAE,EAAE;YACvD,2GAA2G;YAC3G,qFAAqF;YACrF,IAAI,CAAC,GAAG,CAAC,EAAE,EAAE,CAAC;gBACZ,OAAO;YACT,CAAC;YAED,IAAI,UAAU,IAAI,CAAC,EAAE,CAAC;gBACpB,MAAM,IAAI,KAAK,CACb,2DAA2D,GAAG,CAAC,EAAE,GAAG,CACrE,CAAC;YACJ,CAAC;YACD,MAAM,WAAW,GAAG,KAAK,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;YAClC,IAAI,WAAW,EAAE,CAAC;gBAChB,WAAW,CAAC,UAAU,GAAG,UAAU,GAAG,CAAC,CAAC;YAC1C,CAAC;YACD,YAAY,CAAC,GAAG,CAAC,CAAC;QACpB,CAAC,CAAC,CAAC;IACL,CAAC;AACH,CAAC;AAzHD,yCAyHC","sourcesContent":["import type {\n JsonRpcEngineNextCallback,\n JsonRpcEngineEndCallback,\n JsonRpcMiddleware,\n} from '@metamask/json-rpc-engine';\nimport SafeEventEmitter from '@metamask/safe-event-emitter';\nimport { hasProperty } from '@metamask/utils';\nimport type {\n JsonRpcNotification,\n JsonRpcParams,\n JsonRpcRequest,\n PendingJsonRpcResponse,\n} from '@metamask/utils';\nimport { Duplex } from 'readable-stream';\n\ntype IdMapValue = {\n req: JsonRpcRequest;\n res: PendingJsonRpcResponse<JsonRpcParams>;\n next: JsonRpcEngineNextCallback;\n end: JsonRpcEngineEndCallback;\n retryCount?: number;\n};\n\ntype IdMap = {\n [requestId: string]: IdMapValue;\n};\n\ntype Options = {\n retryOnMessage?: string;\n};\n\n/**\n * Creates a JsonRpcEngine middleware with an associated Duplex stream and\n * EventEmitter. The middleware, and by extension stream, assume that middleware\n * parameters are properly formatted. No runtime type checking or validation is\n * performed.\n *\n * @param options - Configuration options for middleware.\n * @returns The event emitter, middleware, and stream.\n */\nexport default function createStreamMiddleware(options: Options = {}) {\n const idMap: IdMap = {}; // TODO: replace with actual Map\n const stream = new Duplex({\n objectMode: true,\n read: () => undefined,\n write: processMessage,\n });\n\n const events = new SafeEventEmitter();\n\n const middleware: JsonRpcMiddleware<JsonRpcParams, JsonRpcParams> = (\n req,\n res,\n next,\n end,\n ) => {\n // register request on id map *before* sending it to the stream, to avoid race issues\n idMap[req.id as unknown as string] = { req, res, next, end };\n // write req to stream\n sendToStream(req);\n };\n\n return { events, middleware, stream };\n\n /**\n * Forwards JSON-RPC request to the stream.\n *\n * @param req - The JSON-RPC request object.\n */\n function sendToStream(req: JsonRpcRequest) {\n // TODO: limiting retries could be implemented here\n stream.push(req);\n }\n\n /**\n * Writes a JSON-RPC object to the stream.\n *\n * @param res - The JSON-RPC response object.\n * @param _encoding - The stream encoding, not used.\n * @param streamWriteCallback - The stream write callback.\n */\n function processMessage(\n res: PendingJsonRpcResponse<JsonRpcParams>,\n _encoding: unknown,\n streamWriteCallback: (error?: Error | null) => void,\n ) {\n let errorObj: Error | null = null;\n try {\n const isNotification = !hasProperty(res, 'id');\n if (isNotification) {\n processNotification(res as unknown as JsonRpcNotification);\n } else {\n processResponse(res);\n }\n } catch (_err) {\n errorObj = _err as Error;\n }\n // continue processing stream\n streamWriteCallback(errorObj);\n }\n\n /**\n * Processes a JSON-RPC response.\n *\n * @param res - The response to process.\n */\n function processResponse(res: PendingJsonRpcResponse<JsonRpcParams>) {\n const { id: responseId } = res;\n if (responseId === null) {\n return;\n }\n\n const context = idMap[responseId];\n if (!context) {\n console.warn(`StreamMiddleware - Unknown response id \"${responseId}\"`);\n return;\n }\n\n delete idMap[responseId];\n // copy whole res onto original res\n Object.assign(context.res, res);\n // run callback on empty stack,\n // prevent internal stream-handler from catching errors\n setTimeout(context.end);\n }\n\n /**\n * Processes a JSON-RPC notification.\n *\n * @param notif - The notification to process.\n */\n function processNotification(notif: JsonRpcNotification) {\n if (options?.retryOnMessage && notif.method === options.retryOnMessage) {\n retryStuckRequests();\n }\n events.emit('notification', notif);\n }\n\n /**\n * Retry pending requests.\n */\n function retryStuckRequests() {\n Object.values(idMap).forEach(({ req, retryCount = 0 }) => {\n // Avoid retrying requests without an id - they cannot have matching responses so retry logic doesn't apply\n // Check for retry count below ensure that a request is not retried more than 3 times\n if (!req.id) {\n return;\n }\n\n if (retryCount >= 3) {\n throw new Error(\n `StreamMiddleware - Retry limit exceeded for request id \"${req.id}\"`,\n );\n }\n const idMapObject = idMap[req.id];\n if (idMapObject) {\n idMapObject.retryCount = retryCount + 1;\n }\n sendToStream(req);\n });\n }\n}\n"]}
@@ -1,6 +1,6 @@
1
1
  import type { JsonRpcMiddleware } from "@metamask/json-rpc-engine";
2
2
  import SafeEventEmitter from "@metamask/safe-event-emitter";
3
- import { type JsonRpcParams } from "@metamask/utils";
3
+ import type { JsonRpcParams } from "@metamask/utils";
4
4
  import { Duplex } from "readable-stream";
5
5
  type Options = {
6
6
  retryOnMessage?: string;
@@ -1 +1 @@
1
- {"version":3,"file":"createStreamMiddleware.d.cts","sourceRoot":"","sources":["../src/createStreamMiddleware.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAGV,iBAAiB,EAClB,kCAAkC;AACnC,OAAO,gBAAgB,qCAAqC;AAC5D,OAAO,EAGL,KAAK,aAAa,EAGnB,wBAAwB;AACzB,OAAO,EAAE,MAAM,EAAE,wBAAwB;AAczC,KAAK,OAAO,GAAG;IACb,cAAc,CAAC,EAAE,MAAM,CAAC;CACzB,CAAC;AAEF;;;;;;;;GAQG;AACH,MAAM,CAAC,OAAO,UAAU,sBAAsB,CAAC,OAAO,GAAE,OAAY;;;;EAyHnE"}
1
+ {"version":3,"file":"createStreamMiddleware.d.cts","sourceRoot":"","sources":["../src/createStreamMiddleware.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAGV,iBAAiB,EAClB,kCAAkC;AACnC,OAAO,gBAAgB,qCAAqC;AAE5D,OAAO,KAAK,EAEV,aAAa,EAGd,wBAAwB;AACzB,OAAO,EAAE,MAAM,EAAE,wBAAwB;AAczC,KAAK,OAAO,GAAG;IACb,cAAc,CAAC,EAAE,MAAM,CAAC;CACzB,CAAC;AAEF;;;;;;;;GAQG;AACH,MAAM,CAAC,OAAO,UAAU,sBAAsB,CAAC,OAAO,GAAE,OAAY;;;;EAyHnE"}
@@ -1,6 +1,6 @@
1
1
  import type { JsonRpcMiddleware } from "@metamask/json-rpc-engine";
2
2
  import SafeEventEmitter from "@metamask/safe-event-emitter";
3
- import { type JsonRpcParams } from "@metamask/utils";
3
+ import type { JsonRpcParams } from "@metamask/utils";
4
4
  import { Duplex } from "readable-stream";
5
5
  type Options = {
6
6
  retryOnMessage?: string;
@@ -1 +1 @@
1
- {"version":3,"file":"createStreamMiddleware.d.mts","sourceRoot":"","sources":["../src/createStreamMiddleware.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAGV,iBAAiB,EAClB,kCAAkC;AACnC,OAAO,gBAAgB,qCAAqC;AAC5D,OAAO,EAGL,KAAK,aAAa,EAGnB,wBAAwB;AACzB,OAAO,EAAE,MAAM,EAAE,wBAAwB;AAczC,KAAK,OAAO,GAAG;IACb,cAAc,CAAC,EAAE,MAAM,CAAC;CACzB,CAAC;AAEF;;;;;;;;GAQG;AACH,MAAM,CAAC,OAAO,UAAU,sBAAsB,CAAC,OAAO,GAAE,OAAY;;;;EAyHnE"}
1
+ {"version":3,"file":"createStreamMiddleware.d.mts","sourceRoot":"","sources":["../src/createStreamMiddleware.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAGV,iBAAiB,EAClB,kCAAkC;AACnC,OAAO,gBAAgB,qCAAqC;AAE5D,OAAO,KAAK,EAEV,aAAa,EAGd,wBAAwB;AACzB,OAAO,EAAE,MAAM,EAAE,wBAAwB;AAczC,KAAK,OA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EG;IACH,SAAS,kBAAkB;QACzB,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,OAAO,CAAC,CAAC,EAAE,GAAG,EAAE,UAAU,GAAG,CAAC,EAAE,EAAE,EAAE;YACvD,2GAA2G;YAC3G,qFAAqF;YACrF,IAAI,CAAC,GAAG,CAAC,EAAE,EAAE,CAAC;gBACZ,OAAO;YACT,CAAC;YAED,IAAI,UAAU,IAAI,CAAC,EAAE,CAAC;gBACpB,MAAM,IAAI,KAAK,CACb,2DAA2D,GAAG,CAAC,EAAE,GAAG,CACrE,CAAC;YACJ,CAAC;YACD,MAAM,WAAW,GAAG,KAAK,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;YAClC,IAAI,WAAW,EAAE,CAAC;gBAChB,WAAW,CAAC,UAAU,GAAG,UAAU,GAAG,CAAC,CAAC;YAC1C,CAAC;YACD,YAAY,CAAC,GAAG,CAAC,CAAC;QACpB,CAAC,CAAC,CAAC;IACL,CAAC;AACH,CAAC","sourcesContent":["import type {\n JsonRpcEngineNextCallback,\n JsonRpcEngineEndCallback,\n JsonRpcMiddleware,\n} from '@metamask/json-rpc-engine';\nimport SafeEventEmitter from '@metamask/safe-event-emitter';\nimport {\n hasProperty,\n type JsonRpcNotification,\n type JsonRpcParams,\n type JsonRpcRequest,\n type PendingJsonRpcResponse,\n} from '@metamask/utils';\nimport { Duplex } from 'readable-stream';\n\ntype IdMapValue = {\n req: JsonRpcRequest;\n res: PendingJsonRpcResponse<JsonRpcParams>;\n next: JsonRpcEngineNextCallback;\n end: JsonRpcEngineEndCallback;\n retryCount?: number;\n};\n\ntype IdMap = {\n [requestId: string]: IdMapValue;\n};\n\ntype Options = {\n retryOnMessage?: string;\n};\n\n/**\n * Creates a JsonRpcEngine middleware with an associated Duplex stream and\n * EventEmitter. The middleware, and by extension stream, assume that middleware\n * parameters are properly formatted. No runtime type checking or validation is\n * performed.\n *\n * @param options - Configuration options for middleware.\n * @returns The event emitter, middleware, and stream.\n */\nexport default function createStreamMiddleware(options: Options = {}) {\n const idMap: IdMap = {}; // TODO: replace with actual Map\n const stream = new Duplex({\n objectMode: true,\n read: () => undefined,\n write: processMessage,\n });\n\n const events = new SafeEventEmitter();\n\n const middleware: JsonRpcMiddleware<JsonRpcParams, JsonRpcParams> = (\n req,\n res,\n next,\n end,\n ) => {\n // register request on id map *before* sending it to the stream, to avoid race issues\n idMap[req.id as unknown as string] = { req, res, next, end };\n // write req to stream\n sendToStream(req);\n };\n\n return { events, middleware, stream };\n\n /**\n * Forwards JSON-RPC request to the stream.\n *\n * @param req - The JSON-RPC request object.\n */\n function sendToStream(req: JsonRpcRequest) {\n // TODO: limiting retries could be implemented here\n stream.push(req);\n }\n\n /**\n * Writes a JSON-RPC object to the stream.\n *\n * @param res - The JSON-RPC response object.\n * @param _encoding - The stream encoding, not used.\n * @param streamWriteCallback - The stream write callback.\n */\n function processMessage(\n res: PendingJsonRpcResponse<JsonRpcParams>,\n _encoding: unknown,\n streamWriteCallback: (error?: Error | null) => void,\n ) {\n let errorObj: Error | null = null;\n try {\n const isNotification = !hasProperty(res, 'id');\n if (isNotification) {\n processNotification(res as unknown as JsonRpcNotification);\n } else {\n processResponse(res);\n }\n } catch (_err) {\n errorObj = _err as Error;\n }\n // continue processing stream\n streamWriteCallback(errorObj);\n }\n\n /**\n * Processes a JSON-RPC response.\n *\n * @param res - The response to process.\n */\n function processResponse(res: PendingJsonRpcResponse<JsonRpcParams>) {\n const { id: responseId } = res;\n if (responseId === null) {\n return;\n }\n\n const context = idMap[responseId];\n if (!context) {\n console.warn(`StreamMiddleware - Unknown response id \"${responseId}\"`);\n return;\n }\n\n delete idMap[responseId];\n // copy whole res onto original res\n Object.assign(context.res, res);\n // run callback on empty stack,\n // prevent internal stream-handler from catching errors\n setTimeout(context.end);\n }\n\n /**\n * Processes a JSON-RPC notification.\n *\n * @param notif - The notification to process.\n */\n function processNotification(notif: JsonRpcNotification) {\n if (options?.retryOnMessage && notif.method === options.retryOnMessage) {\n retryStuckRequests();\n }\n events.emit('notification', notif);\n }\n\n /**\n * Retry pending requests.\n */\n function retryStuckRequests() {\n Object.values(idMap).forEach(({ req, retryCount = 0 }) => {\n // Avoid retrying requests without an id - they cannot have matching responses so retry logic doesn't apply\n // Check for retry count below ensure that a request is not retried more than 3 times\n if (!req.id) {\n return;\n }\n\n if (retryCount >= 3) {\n throw new Error(\n `StreamMiddleware - Retry limit exceeded for request id \"${req.id}\"`,\n );\n }\n const idMapObject = idMap[req.id];\n if (idMapObject) {\n idMapObject.retryCount = retryCount + 1;\n }\n sendToStream(req);\n });\n }\n}\n"]}
1
+ {"version":3,"file":"createStreamMiddleware.mjs","sourceRoot":"","sources":["../src/createStreamMiddleware.ts"],"names":[],"mappings":"AAKA,OAAO,gBAAgB,qCAAqC;AAC5D,OAAO,EAAE,WAAW,EAAE,wBAAwB;AAO9C,OAAO,EAAE,MAAM,EAAE,wBAAwB;AAkBzC;;;;;;;;GAQG;AACH,MAAM,CAAC,OAAO,UAAU,sBAAsB,CAAC,UAAmB,EAAE;IAClE,MAAM,KAAK,GAAU,EAAE,CAAC,CAAC,gCAAgC;IACzD,MAAM,MAAM,GAAG,IAAI,MAAM,CAAC;QACxB,UAAU,EAAE,IAAI;QAChB,IAAI,EAAE,GAAG,EAAE,CAAC,SAAS;QACrB,KAAK,EAAE,cAAc;KACtB,CAAC,CAAC;IAEH,MAAM,MAAM,GAAG,IAAI,gBAAgB,EAAE,CAAC;IAEtC,MAAM,UAAU,GAAoD,CAClE,GAAG,EACH,GAAG,EACH,IAAI,EACJ,GAAG,EACH,EAAE;QACF,qFAAqF;QACrF,KAAK,CAAC,GAAG,CAAC,EAAuB,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,IAAI,EAAE,GAAG,EAAE,CAAC;QAC7D,sBAAsB;QACtB,YAAY,CAAC,GAAG,CAAC,CAAC;IACpB,CAAC,CAAC;IAEF,OAAO,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,EAAE,CAAC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B;QACrD,IAAI,OAAO,EAAE,cAAc,IAAI,KAAK,CAAC,MAAM,KAAK,OAAO,CAAC,cAAc,EAAE,CAAC;YACvE,kBAAkB,EAAE,CAAC;QACvB,CAAC;QACD,MAAM,CAAC,IAAI,CAAC,cAAc,EAAE,KAAK,CAAC,CAAC;IACrC,CAAC;IAED;;OAEG;IACH,SAAS,kBAAkB;QACzB,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,OAAO,CAAC,CAAC,EAAE,GAAG,EAAE,UAAU,GAAG,CAAC,EAAE,EAAE,EAAE;YACvD,2GAA2G;YAC3G,qFAAqF;YACrF,IAAI,CAAC,GAAG,CAAC,EAAE,EAAE,CAAC;gBACZ,OAAO;YACT,CAAC;YAED,IAAI,UAAU,IAAI,CAAC,EAAE,CAAC;gBACpB,MAAM,IAAI,KAAK,CACb,2DAA2D,GAAG,CAAC,EAAE,GAAG,CACrE,CAAC;YACJ,CAAC;YACD,MAAM,WAAW,GAAG,KAAK,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;YAClC,IAAI,WAAW,EAAE,CAAC;gBAChB,WAAW,CAAC,UAAU,GAAG,UAAU,GAAG,CAAC,CAAC;YAC1C,CAAC;YACD,YAAY,CAAC,GAAG,CAAC,CAAC;QACpB,CAAC,CAAC,CAAC;IACL,CAAC;AACH,CAAC","sourcesContent":["import type {\n JsonRpcEngineNextCallback,\n JsonRpcEngineEndCallback,\n JsonRpcMiddleware,\n} from '@metamask/json-rpc-engine';\nimport SafeEventEmitter from '@metamask/safe-event-emitter';\nimport { hasProperty } from '@metamask/utils';\nimport type {\n JsonRpcNotification,\n JsonRpcParams,\n JsonRpcRequest,\n PendingJsonRpcResponse,\n} from '@metamask/utils';\nimport { Duplex } from 'readable-stream';\n\ntype IdMapValue = {\n req: JsonRpcRequest;\n res: PendingJsonRpcResponse<JsonRpcParams>;\n next: JsonRpcEngineNextCallback;\n end: JsonRpcEngineEndCallback;\n retryCount?: number;\n};\n\ntype IdMap = {\n [requestId: string]: IdMapValue;\n};\n\ntype Options = {\n retryOnMessage?: string;\n};\n\n/**\n * Creates a JsonRpcEngine middleware with an associated Duplex stream and\n * EventEmitter. The middleware, and by extension stream, assume that middleware\n * parameters are properly formatted. No runtime type checking or validation is\n * performed.\n *\n * @param options - Configuration options for middleware.\n * @returns The event emitter, middleware, and stream.\n */\nexport default function createStreamMiddleware(options: Options = {}) {\n const idMap: IdMap = {}; // TODO: replace with actual Map\n const stream = new Duplex({\n objectMode: true,\n read: () => undefined,\n write: processMessage,\n });\n\n const events = new SafeEventEmitter();\n\n const middleware: JsonRpcMiddleware<JsonRpcParams, JsonRpcParams> = (\n req,\n res,\n next,\n end,\n ) => {\n // register request on id map *before* sending it to the stream, to avoid race issues\n idMap[req.id as unknown as string] = { req, res, next, end };\n // write req to stream\n sendToStream(req);\n };\n\n return { events, middleware, stream };\n\n /**\n * Forwards JSON-RPC request to the stream.\n *\n * @param req - The JSON-RPC request object.\n */\n function sendToStream(req: JsonRpcRequest) {\n // TODO: limiting retries could be implemented here\n stream.push(req);\n }\n\n /**\n * Writes a JSON-RPC object to the stream.\n *\n * @param res - The JSON-RPC response object.\n * @param _encoding - The stream encoding, not used.\n * @param streamWriteCallback - The stream write callback.\n */\n function processMessage(\n res: PendingJsonRpcResponse<JsonRpcParams>,\n _encoding: unknown,\n streamWriteCallback: (error?: Error | null) => void,\n ) {\n let errorObj: Error | null = null;\n try {\n const isNotification = !hasProperty(res, 'id');\n if (isNotification) {\n processNotification(res as unknown as JsonRpcNotification);\n } else {\n processResponse(res);\n }\n } catch (_err) {\n errorObj = _err as Error;\n }\n // continue processing stream\n streamWriteCallback(errorObj);\n }\n\n /**\n * Processes a JSON-RPC response.\n *\n * @param res - The response to process.\n */\n function processResponse(res: PendingJsonRpcResponse<JsonRpcParams>) {\n const { id: responseId } = res;\n if (responseId === null) {\n return;\n }\n\n const context = idMap[responseId];\n if (!context) {\n console.warn(`StreamMiddleware - Unknown response id \"${responseId}\"`);\n return;\n }\n\n delete idMap[responseId];\n // copy whole res onto original res\n Object.assign(context.res, res);\n // run callback on empty stack,\n // prevent internal stream-handler from catching errors\n setTimeout(context.end);\n }\n\n /**\n * Processes a JSON-RPC notification.\n *\n * @param notif - The notification to process.\n */\n function processNotification(notif: JsonRpcNotification) {\n if (options?.retryOnMessage && notif.method === options.retryOnMessage) {\n retryStuckRequests();\n }\n events.emit('notification', notif);\n }\n\n /**\n * Retry pending requests.\n */\n function retryStuckRequests() {\n Object.values(idMap).forEach(({ req, retryCount = 0 }) => {\n // Avoid retrying requests without an id - they cannot have matching responses so retry logic doesn't apply\n // Check for retry count below ensure that a request is not retried more than 3 times\n if (!req.id) {\n return;\n }\n\n if (retryCount >= 3) {\n throw new Error(\n `StreamMiddleware - Retry limit exceeded for request id \"${req.id}\"`,\n );\n }\n const idMapObject = idMap[req.id];\n if (idMapObject) {\n idMapObject.retryCount = retryCount + 1;\n }\n sendToStream(req);\n });\n }\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@metamask-previews/json-rpc-middleware-stream",
3
- "version": "8.0.8-preview-bfee350",
3
+ "version": "8.0.8-preview-5c1b2b1",
4
4
  "description": "A small toolset for streaming JSON-RPC data and matching requests and responses",
5
5
  "keywords": [
6
6
  "MetaMask",