UNPKG

@specprotected/spec-proxy-akamai-worker

Version:

Akamai EdgeWorker library to facilitate communication with the Spec Proxy product.

120 lines (119 loc) 5.02 kB
/// <reference types="akamai-edgeworkers" /> import { HttpResponse, RequestBody } from "http-request"; export declare type AkamaiRequest = { host: string; url: string; method: string; headers: EW.Headers; body: RequestBody | undefined; [others: string]: any; }; export interface SpecConfiguration { /** * When true, disable Spec Proxy, this library and all functionality is disabled */ disableSpecProxy?: boolean; /** * When true, the request returned by this function is modified * to make a request to Spec Proxy, which will result in Spec Proxy making * the request to the customer origin itself */ inlineMode?: boolean; /** * A number between 0 and 100 that identifies the percentage of IP traffic * the SpecTrust platform should process. */ percentageOfIPs?: number; } /** * This function is responsible for processing the `ResponseProviderRequest` * that the Akamai API provides clients during the * [Response Provider Event](https://techdocs.akamai.com/edgeworkers/docs/response-orchestration). * * Clients of this library need only create a `SpecConfiguration` object and pass * the arguments of the `responseProvider` function into this method along with * your configuration object. * * ```javascript * import { * specProxyProcess, * } from '@specprotected/spec-proxy-akamai-worker'; * * const specConfig = { inlineMode: false }; * * export async function responseProvider(request: EW.ResponseProviderRequest) { * return await specProxyProcess(request, specConfig); * } * ``` * * @param request - the incoming ResponseProviderRequest object * @param config - configuration object to adjust Spec Proxy behavior * @returns - response created from calling createResponse, akamai doesn't provide a type for this value */ export declare function specProxyProcess(request: EW.ResponseProviderRequest, config: SpecConfiguration): Promise<HttpResponse>; /** * This function is responsible for processing the `ResponseProviderRequest` * that the Akamai API provides clients during the * [Response Provider Event](https://techdocs.akamai.com/edgeworkers/docs/response-orchestration). * * Clients of this library need only create a `SpecConfiguration` object and pass * the arguments of the `responseProvider` function into this method along with * your configuration object. * * Due to the nature of how some of our features work, you should use our * returned `AkamaiRequest` type instead of the `ResponseProviderRequest` * you pass in as an argument. For bodies that are streamed, we attach a * consumer to the stream to split it so we don't incur any performance * penalties when redirecting requests. This is a custom type from this library, * but should be able to pass any Akamai properties through to `httpRequest` * calls. * * * ```javascript * import { * specProxyProcessRequest, * } from '@specprotected/spec-proxy-akamai-worker'; * * const specConfig = { inlineMode: false }; * * export async function responseProvider(request: EW.ResponseProviderRequest) { * let request: AkamaiRequest = await spec.specProxyProcessRequest(request, specConfig); * let response = await httpRequest(request); * return createResponse(response.body, response); * } * ``` * * @param ogRequest - the incoming ResponseProviderRequest object * @param config - configuration object to adjust Spec Proxy behavior * @returns - the modified Request object, in this library's AkamaiRequest type */ export declare function specProxyProcessRequest(ogRequest: EW.ResponseProviderRequest, config?: SpecConfiguration): Promise<AkamaiRequest>; /** * Process the Response as it returns to the originator of the Request. This * function generally adds any details the SpecTrust platform requires to identify * site visitors, such as the Spec Cookie. * * Note: this function does not process the Body of a Response, so won't require * awaiting while reading the body stream, which enables efficient processing. * * ```javascript * import { * specProxyProcessRequest, * specProxyProcessResponse, * } from '@specprotected/spec-proxy-akamai-worker'; * * const specConfig = { inlineMode: false }; * * export async function responseProvider(request: EW.ResponseProviderRequest) { * let request: AkamaiRequest = await spec.specProxyProcessRequest(request, specConfig); * let response = spec.specProxyProcessResponse(request, await httpRequest(request)); * return createResponse(response.body, response); * } * ``` * * @param request - the request object that was sent to customer servers * @param response - the response object that was returned from customer servers * @param config - the configuration object that defines how this library should behave * @returns - the modified response object */ export declare function specProxyProcessResponse(request: EW.ResponseProviderRequest, response: HttpResponse, config?: SpecConfiguration): HttpResponse;