This repository was archived by the owner on Nov 9, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 63
Allow middleware to be async, deprecate createAsyncMiddleware #81
Open
rekmarks
wants to merge
8
commits into
main
Choose a base branch
from
remove-createAsyncMiddleware
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
378a820
Remove createAsyncMiddleware
rekmarks 3fb1a7c
Add back and deprecate createAsyncMiddleware
rekmarks a9028af
Update createAsyncMiddleware docstring
rekmarks afd85f0
Reorder some stuff
rekmarks 36d91ad
Undo unintended change to scaffold middleware
rekmarks 6d5ddc6
Revert "Undo unintended change to scaffold middleware"
rekmarks 49c1d95
Merge branch 'main' into remove-createAsyncMiddleware
legobeat 252827c
Merge branch 'main' into remove-createAsyncMiddleware
legobeat 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
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 |
---|---|---|
|
@@ -34,7 +34,7 @@ export type JsonRpcMiddleware< | |
res: PendingJsonRpcResponse<Result>, | ||
next: JsonRpcEngineNextCallback, | ||
end: JsonRpcEngineEndCallback, | ||
): void; | ||
): void | Promise<void>; | ||
destroy?: () => void | Promise<void>; | ||
}; | ||
|
||
|
@@ -534,49 +534,55 @@ export class JsonRpcEngine extends SafeEventEmitter { | |
middleware: JsonRpcMiddleware<JsonRpcParams, Json>, | ||
returnHandlers: JsonRpcEngineReturnHandler[], | ||
): Promise<[unknown, boolean]> { | ||
return new Promise((resolve) => { | ||
const end: JsonRpcEngineEndCallback = (error?: unknown) => { | ||
const parsedError = error || response.error; | ||
if (parsedError) { | ||
response.error = serializeError(parsedError); | ||
} | ||
// True indicates that the request should end | ||
resolve([parsedError, true]); | ||
}; | ||
|
||
const next: JsonRpcEngineNextCallback = ( | ||
returnHandler?: JsonRpcEngineReturnHandler, | ||
) => { | ||
if (response.error) { | ||
end(response.error); | ||
} else { | ||
if (returnHandler) { | ||
if (typeof returnHandler !== 'function') { | ||
end( | ||
new JsonRpcError( | ||
errorCodes.rpc.internal, | ||
`JsonRpcEngine: "next" return handlers must be functions. ` + | ||
`Received "${typeof returnHandler}" for request:\n${jsonify( | ||
request, | ||
)}`, | ||
{ request: request as Json }, | ||
), | ||
); | ||
} | ||
returnHandlers.push(returnHandler); | ||
} | ||
let resolve: (value: [unknown, boolean]) => void; | ||
const middlewareCallbackPromise = new Promise<[unknown, boolean]>( | ||
(_resolve) => { | ||
resolve = _resolve; | ||
}, | ||
); | ||
|
||
// False indicates that the request should not end | ||
resolve([null, false]); | ||
} | ||
}; | ||
const end: JsonRpcEngineEndCallback = (error?: unknown) => { | ||
const parsedError = error || response.error; | ||
if (parsedError) { | ||
response.error = serializeError(parsedError); | ||
} | ||
// True indicates that the request should end | ||
resolve([parsedError, true]); | ||
}; | ||
|
||
try { | ||
middleware(request, response, next, end); | ||
} catch (error: any) { | ||
end(error); | ||
const next: JsonRpcEngineNextCallback = ( | ||
returnHandler?: JsonRpcEngineReturnHandler, | ||
) => { | ||
if (response.error) { | ||
return end(response.error); | ||
} | ||
}); | ||
|
||
if (returnHandler) { | ||
if (typeof returnHandler !== 'function') { | ||
return end( | ||
new JsonRpcError( | ||
errorCodes.rpc.internal, | ||
`JsonRpcEngine: "next" return handlers must be functions. ` + | ||
`Received "${typeof returnHandler}" for request:\n${jsonify( | ||
request, | ||
)}`, | ||
{ request: request as Json }, | ||
), | ||
); | ||
} | ||
returnHandlers.push(returnHandler); | ||
} | ||
|
||
// False indicates that the request should not end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thought for another PR: I'm curious whether it would make more sense to make this an object rather than an array so we can make this self-documenting: return resolve({ error: null, isComplete: false }); |
||
return resolve([null, false]); | ||
}; | ||
|
||
try { | ||
await middleware(request, response, next, end); | ||
} catch (error: any) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: Does it make more sense to use |
||
end(error); | ||
} | ||
return middlewareCallbackPromise; | ||
} | ||
|
||
/** | ||
|
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, I see, the reason why we have to do this now is because the function that the Promise constructor takes can't itself be
async
? Good solution.