This repository has been archived by the owner on Nov 9, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* @metamask/eslint-config@latest * Convert to TypeScript * Update internal imports * Format, add docstrings
- Loading branch information
Showing
12 changed files
with
696 additions
and
394 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
|
||
dist | ||
package-lock.json | ||
|
||
# Created by https://www.gitignore.io/api/osx,node | ||
|
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { Duplex } from 'readable-stream'; | ||
import { JsonRpcEngine, JsonRpcRequest } from 'json-rpc-engine'; | ||
|
||
interface EngineStreamOptions { | ||
engine: JsonRpcEngine; | ||
} | ||
|
||
/** | ||
* Takes a JsonRpcEngine and returns a Duplex stream wrapping it. | ||
* | ||
* @param opts - Options bag. | ||
* @param opts.engine - The JsonRpcEngine to wrap in a stream. | ||
* @returns The stream wrapping the engine. | ||
*/ | ||
export default function createEngineStream(opts: EngineStreamOptions): Duplex { | ||
if (!opts || !opts.engine) { | ||
throw new Error('Missing engine parameter!'); | ||
} | ||
|
||
const { engine } = opts; | ||
const stream = new Duplex({ objectMode: true, read, write }); | ||
// forward notifications | ||
if (engine.on) { | ||
engine.on('notification', (message) => { | ||
stream.push(message); | ||
}); | ||
} | ||
return stream; | ||
|
||
function read() { | ||
return undefined; | ||
} | ||
|
||
function write( | ||
req: JsonRpcRequest<unknown>, | ||
_encoding: unknown, | ||
cb: (error?: Error | null) => void, | ||
) { | ||
engine.handle(req, (_err, res) => { | ||
stream.push(res); | ||
}); | ||
cb(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import SafeEventEmitter from '@metamask/safe-event-emitter'; | ||
import { Duplex } from 'readable-stream'; | ||
import { | ||
JsonRpcEngineNextCallback, | ||
JsonRpcEngineEndCallback, | ||
JsonRpcNotification, | ||
JsonRpcMiddleware, | ||
JsonRpcRequest, | ||
PendingJsonRpcResponse, | ||
} from 'json-rpc-engine'; | ||
|
||
interface IdMapValue { | ||
req: JsonRpcRequest<unknown>; | ||
res: PendingJsonRpcResponse<unknown>; | ||
next: JsonRpcEngineNextCallback; | ||
end: JsonRpcEngineEndCallback; | ||
} | ||
|
||
interface IdMap { | ||
[requestId: string]: IdMapValue; | ||
} | ||
|
||
/** | ||
* Creates a JsonRpcEngine middleware with an associated Duplex stream and | ||
* EventEmitter. The middleware, and by extension stream, assume that middleware | ||
* parameters are properly formatted. No runtime type checking or validation is | ||
* performed. | ||
* | ||
* @returns The event emitter, middleware, and stream. | ||
*/ | ||
export default function createStreamMiddleware() { | ||
const idMap: IdMap = {}; | ||
const stream = new Duplex({ | ||
objectMode: true, | ||
read: readNoop, | ||
write: processMessage, | ||
}); | ||
|
||
const events = new SafeEventEmitter(); | ||
|
||
const middleware: JsonRpcMiddleware<unknown, unknown> = ( | ||
req, | ||
res, | ||
next, | ||
end, | ||
) => { | ||
// write req to stream | ||
stream.push(req); | ||
// register request on id map | ||
idMap[(req.id as unknown) as string] = { req, res, next, end }; | ||
}; | ||
|
||
return { events, middleware, stream }; | ||
|
||
function readNoop() { | ||
return false; | ||
} | ||
|
||
function processMessage( | ||
res: PendingJsonRpcResponse<unknown>, | ||
_encoding: unknown, | ||
cb: (error?: Error | null) => void, | ||
) { | ||
let err; | ||
try { | ||
const isNotification = !res.id; | ||
if (isNotification) { | ||
processNotification((res as unknown) as JsonRpcNotification<unknown>); | ||
} else { | ||
processResponse(res); | ||
} | ||
} catch (_err) { | ||
err = _err; | ||
} | ||
// continue processing stream | ||
cb(err); | ||
} | ||
|
||
function processResponse(res: PendingJsonRpcResponse<unknown>) { | ||
const context = idMap[(res.id as unknown) as string]; | ||
if (!context) { | ||
throw new Error(`StreamMiddleware - Unknown response id "${res.id}"`); | ||
} | ||
|
||
delete idMap[(res.id as unknown) as string]; | ||
// copy whole res onto original res | ||
Object.assign(context.res, res); | ||
// run callback on empty stack, | ||
// prevent internal stream-handler from catching errors | ||
setTimeout(context.end); | ||
} | ||
|
||
function processNotification(res: JsonRpcNotification<unknown>) { | ||
events.emit('notification', res); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import createEngineStream from './createEngineStream'; | ||
import createStreamMiddleware from './createStreamMiddleware'; | ||
|
||
export { createEngineStream, createStreamMiddleware }; |
Oops, something went wrong.