create-response
This module is available to use in your EdgeWorker code bundles to return an object from a Promise and use it as a response. It is only available when using the responseProvider event handler. It exports the createResponse() function.
👍 The
Connection,Keep-Alive,Proxy-Authenticate,Proxy-Authorization,TE,Trailers, andTransfer-Encodinghop-by-hop headers should not be set when creating a request.The
Host,Content-Length, andVaryheaders should not be set or copied from another request. If you opt to set them anyway, you need to make sure that the values are correct.
- An incorrect value in the
Hostheader can break your request.- An incorrect value in the
Content-Lengthheader will break the response. Make sure that the value reflects the actual length of the payload you’re passing.- An incorrect value in the
Varyheader can break cacheability.
Error handling when streaming with createResponse()
As soon as the EdgeWorker returns the opaque object from responseProvider() the status code and headers registered with createResponse() are sent to the client. If you pass a ReadableStream into createResponse() it allows your EdgeWorker to continue executing so it can stream the response body to the client.
If there is an uncaught error during streaming, the following can occur:
- The EdgeWorker sends a
200 OKresponse back to the client. - The EdgeWorker writes part of the response to the client.
- The EdgeWorker encounters a fatal error such as an exception or exceeding a limit. When a fatal error occurs, the response body is truncated.
Since your EdgeWorker has already sent the 200 OK, the client probably expects a valid response and may encounter problems.
For more information, go to the Response body processing tutorial.
createResponse()
Generates the return value for responseProvider().
Use this function to validate the passed values and return an opaque object. The opaque object can be used to resolve the Promise returned from responseProvider(). The function accepts either a list of parameters or an options object.
👍 You need to enable the multi-part response header for
responseProviderwhen adding the built-in log module to your EdgeWorkers functions. For more information see Enable and JavaScript logging for responseProvider.
📘 An exception is thrown if callers specify invalid parameters.
Parameters
Review the table for information about the available parameters.
Options object parameters
Optional values that the user can choose to specify, where options is an object: { status, headers, deny_reason }. The definition of each property is the same as above.
Review the table for information about the available parameters when using the options object.