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-Encoding
hop-by-hop headers should not be set when creating a request.The
Host
,Content-Length
, andVary
headers 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
Host
header can break your request.- An incorrect value in the
Content-Length
header will break the response. Make sure that the value reflects the actual length of the payload you're passing.- An incorrect value in the
Vary
header 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 OK
response 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
responseProvider
when 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
createResponse(status, headers, body, [deny_reason])
Review the table for information about the available parameters.
Parameter | Type | Description |
---|---|---|
status | Integer | HTTP status code of the outgoing response Note: The status supports 2xx Success, 3xx Redirection 4xx Client Error, and 5xx Server Error status codes. |
headers | Object Note: A string cannot contain illegal characters. Refer to section three of the RFC 7230 document for information about the allowed characters. | Properties used as key:value pairs for the response headers. Keys are strings that contain header names, values are either strings or arrays of strings. Header names and values must conform to the RFC 7230 standards. |
body | String or ReadableStream | Content of the response body When specified as a string, the body is limited to 16 KB. When specified as a ReadableStream, there is no size limit. The ReadableStream should consist of bytes of data. You can use the TextEncoder class to transform a text stream into a byte stream. |
deny_reason | String | Optional Deny reason for the 403 status code. |
import { createResponse } from 'create-response';
export function responseProvider (request) {
return Promise.resolve(createResponse(200, { 'Powered-By': ['Akamai EdgeWorkers'] }
, '<html><body><p>Hello World</p></body></html>'));
}
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.
createResponse([body, [options]])
Review the table for information about the available parameters when using the options object.
Parameter | Type | Description |
---|---|---|
body | String or ReadableStream | Defaults to an empty body if not provided. |
status | Number | Defaults to 200 if not provided. |
headers | Object | Defaults to {}. |
deny_reason | String | Defaults to "". |
import { createResponse } from 'create-response';
export function responseProvider (request) {
const options = {};
options.status = 200;
options.headers = { header1: 'value1', header2: 'value2' };
return Promise.resolve(createResponse('<html><body><p>Hello World</p></body></html>', options));
}
Updated 12 months ago