-
Notifications
You must be signed in to change notification settings - Fork 26
feat: jrpc v2 readiness #383
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
3d932e0
feat: V2 Engine/Middleware to provider utils
lwin-kyaw b34ab4d
feat: duplex message stream with JRPCV2 engine
lwin-kyaw 12117b9
fix: updated tests
lwin-kyaw 74a9c0f
fix: added missing callback after stream push
lwin-kyaw 7c6cc6a
chore: resolved conflicts
lwin-kyaw 964e3f2
feat: added util func to propagate the forzen request to mutable one
lwin-kyaw b01101b
fix: fixed stream write callback
lwin-kyaw File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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,70 @@ | ||
| import log from "loglevel"; | ||
| import { Duplex } from "readable-stream"; | ||
|
|
||
| import { isRequest } from "../../utils/jrpc"; | ||
| import { rpcErrors } from "../errors"; | ||
| import { JRPCRequest } from "../interfaces"; | ||
| import { SafeEventEmitter } from "../safeEventEmitter"; | ||
| import { JRPCEngineV2 } from "./jrpcEngineV2"; | ||
|
|
||
| /** | ||
| * Creates a Duplex object stream for an engine (JRPCEngineV2) + a separate notification emitter. | ||
| * | ||
| * Replaces V1's createEngineStream by decoupling notification forwarding from | ||
| * the engine itself. Notifications are routed through a SafeEventEmitter that | ||
| * pushes onto the same stream, so the engine no longer needs to be an EventEmitter. | ||
| */ | ||
| export function createEngineStreamV2({ engine, notificationEmitter }: { engine: JRPCEngineV2; notificationEmitter?: SafeEventEmitter }): Duplex { | ||
| let stream: Duplex | undefined = undefined; | ||
|
|
||
| function noop() { | ||
| // noop | ||
| } | ||
|
|
||
| function handleRequest(req: JRPCRequest<unknown>) { | ||
| return engine | ||
| .handle(req) | ||
| .then((res): undefined => { | ||
| if (res !== undefined && isRequest(req)) { | ||
| stream?.push({ | ||
| id: req.id, | ||
| jsonrpc: "2.0", | ||
| result: res, | ||
| }); | ||
| } | ||
| return undefined; | ||
| }) | ||
| .catch((err: unknown) => { | ||
| if (isRequest(req)) { | ||
| const message = err instanceof Error ? err.message : "Internal JSON-RPC error"; | ||
| stream?.push({ | ||
| id: req.id, | ||
| jsonrpc: "2.0", | ||
| error: rpcErrors.internal({ message }), | ||
| }); | ||
| } | ||
cursor[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| log.error(err); | ||
| }); | ||
| } | ||
cursor[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| function write(req: JRPCRequest<unknown>, _encoding: BufferEncoding, cb: (error?: Error | null) => void) { | ||
| return handleRequest(req).finally(() => { | ||
| cb(); | ||
| }); | ||
| } | ||
|
|
||
| stream = new Duplex({ objectMode: true, read: noop, write }); | ||
|
|
||
| if (notificationEmitter) { | ||
| const onNotification = (message: unknown) => { | ||
| stream?.push(message); | ||
| }; | ||
|
|
||
| notificationEmitter.on("notification", onNotification); | ||
| stream?.once("close", () => { | ||
| notificationEmitter.removeListener("notification", onNotification); | ||
| }); | ||
| } | ||
|
|
||
| return stream; | ||
| } | ||
This file contains hidden or 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,85 @@ | ||
| import { getUniqueId } from "../../utils"; | ||
| import { serializeJrpcError } from "../errors"; | ||
| import { JRPCParams, JRPCRequest, JRPCResponse, Json, RequestArguments } from "../interfaces"; | ||
| import { ProviderEvents, SafeEventEmitterProvider } from "../jrpcEngine"; | ||
| import { SafeEventEmitter } from "../safeEventEmitter"; | ||
| import { deepClone, propagateToRequest } from "./compatibility-utils"; | ||
| import { JRPCEngineV2 } from "./jrpcEngineV2"; | ||
| import type { JRPCMiddlewareV2 } from "./v2interfaces"; | ||
|
|
||
| /** | ||
| * Create a {@link SafeEventEmitterProvider} from a {@link JRPCEngineV2}. | ||
| * | ||
| * Unlike the V1 counterpart, the V2 engine throws errors directly rather than | ||
| * wrapping them in response objects, so `sendAsync` simply propagates thrown errors. | ||
| * Notification forwarding is not supported since {@link JRPCEngineV2} is not an event emitter. | ||
| * | ||
| * @param engine - The V2 JSON-RPC engine. | ||
| * @returns A provider backed by the engine. | ||
| */ | ||
| export function providerFromEngine(engine: JRPCEngineV2): SafeEventEmitterProvider { | ||
| const provider: SafeEventEmitterProvider = new SafeEventEmitter<ProviderEvents>() as SafeEventEmitterProvider; | ||
|
|
||
| provider.sendAsync = async <T extends JRPCParams, U>(req: JRPCRequest<T>) => { | ||
| const result = await engine.handle(req as JRPCRequest); | ||
| return result as U; | ||
| }; | ||
|
|
||
| async function handleWithCallback<T extends JRPCParams, U>(req: JRPCRequest<T>, callback: (error: unknown, providerRes: JRPCResponse<U>) => void) { | ||
| try { | ||
| const result = await engine.handle(req as JRPCRequest); | ||
| callback(null, { id: req.id, jsonrpc: "2.0", result: result as U }); | ||
| } catch (error) { | ||
| const serializedError = serializeJrpcError(error, { | ||
| shouldIncludeStack: false, | ||
| shouldPreserveMessage: true, | ||
| }); | ||
| callback(serializedError, { id: req.id, jsonrpc: "2.0", error: serializedError }); | ||
| } | ||
| } | ||
chaitanyapotti marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| provider.send = <T extends JRPCParams, U>(req: JRPCRequest<T>, callback: (error: unknown, providerRes: JRPCResponse<U>) => void) => { | ||
| if (typeof callback !== "function") { | ||
| throw new Error('Must provide callback to "send" method.'); | ||
| } | ||
| handleWithCallback(req, callback); | ||
| }; | ||
|
|
||
| provider.request = async <T extends JRPCParams, U>(args: RequestArguments<T>) => { | ||
| const req: JRPCRequest<JRPCParams> = { | ||
| ...args, | ||
| id: getUniqueId(), | ||
| jsonrpc: "2.0", | ||
| }; | ||
| const res = await provider.sendAsync(req); | ||
| return res as U; | ||
| }; | ||
|
|
||
| return provider; | ||
| } | ||
|
|
||
| /** | ||
| * Create a {@link SafeEventEmitterProvider} from one or more V2 middleware. | ||
| * | ||
| * @param middleware - The V2 middleware to back the provider. | ||
| * @returns A provider backed by an engine composed of the given middleware. | ||
| */ | ||
| export function providerFromMiddleware(middleware: JRPCMiddlewareV2): SafeEventEmitterProvider { | ||
| const engine = JRPCEngineV2.create({ middleware: [middleware] }); | ||
| return providerFromEngine(engine as JRPCEngineV2); | ||
| } | ||
|
|
||
| /** | ||
| * Convert a {@link SafeEventEmitterProvider} into a V2 middleware. | ||
| * The middleware delegates all requests to the provider's `sendAsync` method. | ||
| * | ||
| * @param provider - The provider to wrap as middleware. | ||
| * @returns A V2 middleware that forwards requests to the provider. | ||
| */ | ||
| export function providerAsMiddleware(provider: SafeEventEmitterProvider): JRPCMiddlewareV2<JRPCRequest, Json> { | ||
| return async ({ request, context }) => { | ||
| const providerRequest = deepClone(request); | ||
| propagateToRequest(providerRequest, context); | ||
| return (await provider.sendAsync(providerRequest)) as Json; | ||
| }; | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.