Skip to content
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

Support response object results from request handlers #4

Open
radhikalism opened this issue Mar 17, 2024 · 3 comments
Open

Support response object results from request handlers #4

radhikalism opened this issue Mar 17, 2024 · 3 comments
Assignees
Labels
enhancement New feature or request

Comments

@radhikalism
Copy link

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).

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), 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?

@magnetikonline
Copy link
Owner

magnetikonline commented Mar 17, 2024

This is fantastic @radhikalism - thanks for the very detailed writeup and set of AWS documentation links.

Agreed - this should be supported. I'll add it to to backlog of work I'll put into this package. 👍

As always, it's finding the time to get these things done 😄.

@radhikalism
Copy link
Author

@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. 🙂

@magnetikonline
Copy link
Owner

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 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants