You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Currently, it looks like edgy verifies the result value of a ViewerRequest handler execution by delegating to lib.payloadVerifyRequest() via its ViewerRequest._payloadVerify method implementation. But this does not support response objects coming from viewer request functions (i.e. a short-circuit response, without connecting to the origin).
When a viewer request handler yields a response object (one with a status field), then lib.payloadVerifyRequest() will fail, and it will cause vReq.execute() to fail.
Example:
const edgy = require('@magnetikonline/edgy');
async function myTest() {
const vReq = new edgy.ViewerRequest()
vReq
.setClientIp('1.2.3.4')
.setHttpMethod('PUT')
.setUri('/path/to/api/route')
.addRequestHttpHeader('X-Fancy-Header','apples')
const resp = await vReq.execute(
// Fails request verification because the yielded payload is a response object:
async function(event) {
return { status: '302', statusDescription: 'Redirect', headers: {} }
}
)
}
Result:
Error: payload property [clientIp] not found
❯ payloadPropertyExists node_modules/@magnetikonline/edgy/lib.js:636:8
❯ payloadPropertyExistsString node_modules/@magnetikonline/edgy/lib.js:649:2
❯ Object.payloadVerifyRequest node_modules/@magnetikonline/edgy/lib.js:420:2
❯ ViewerRequest._payloadVerify node_modules/@magnetikonline/edgy/main.js:12:7
❯ ViewerRequest.execute node_modules/@magnetikonline/edgy/lib.js:140:8
❯ tests/index.test.js:291:20
289| .addRequestHttpHeader('X-Fancy-Header','apples')
290|
291| const response = await vReq.execute(
| ^
292| // Fails request verification because the yielded payload is a response object:
293| async function(event) {
A workaround:
class ViewerRequest extends edgy.ViewerRequest {
_payloadVerify(payload) {
if ('status' in payload) { // Verify a response object result from the viewer request handler:
edgyLib.payloadVerifyResponse(payload)
} else { // Verify a request object result from the viewer request handler:
edgyLib.payloadVerifyRequest(payload)
}
}
}
Aside: response object verification requires a headers field, but only a status field is strictly required.
The same problem may be true of OriginRequest too.
Could ViewerRequest and OriginRequest classes support direct response object results?
The text was updated successfully, but these errors were encountered:
@magnetikonline Thanks, if it helps, I've pushed a PR at #5 with a possible solution, please check it out whenever you can.
I'd like to use edgy in a project, and while the subclass workaround is adequate for as long as needed, it would be nicer to add upstream support for response objects from request functions. 🙂
For sure @radhikalism - let me see if I can grab some time to put some love into this project - as you can see, got another long standing issue still in the backlog too 😄
Currently, it looks like
edgy
verifies the result value of aViewerRequest
handler execution by delegating tolib.payloadVerifyRequest()
via itsViewerRequest._payloadVerify
method implementation. But this does not support response objects coming from viewer request functions (i.e. a short-circuit response, without connecting to the origin).A viewer request function could yield a response object result, not always a request object result.
When a viewer request handler yields a response object (one with a
status
field), thenlib.payloadVerifyRequest()
will fail, and it will causevReq.execute()
to fail.Example:
Result:
A workaround:
Aside: response object verification requires a
headers
field, but only astatus
field is strictly required.The same problem may be true of
OriginRequest
too.Could
ViewerRequest
andOriginRequest
classes support direct response object results?The text was updated successfully, but these errors were encountered: