@specprotected/spec-proxy-cloudflare-worker
Version:
Spec Proxy implementation for Cloudflare Edge Workers
42 lines (36 loc) • 1.36 kB
text/typescript
// This is a wrapper module to support cloudflare edge workers. We isolate
// this code to help shield implementation details from the inner workings of
// the library and help keep a standard interface while we change how our
// API operates.
//
import * as spec from "@specprotected/spec-proxy-service-worker";
export { SpecConfiguration } from "@specprotected/spec-proxy-service-worker";
class CloudflareRequest extends Request {
constructor(info: RequestInfo, init?: RequestInit, original?: Request) {
if (original) {
// assign the custom cloudflare properties of the RequestInit object
// i guess cloudflare didn't include this property on the Request type? so hack with any
init = { ...init, ...{ cf: (original as any).cf } };
}
super(info, init);
}
}
export function specProxyProcessRequest(
event: FetchEvent,
config: spec.SpecConfiguration = {},
): Request {
return spec.specProxyProcessRequest(event, config, CloudflareRequest);
}
export function specProxyProcessResponse(
request: Request,
response: Response,
config: spec.SpecConfiguration = {},
): Response {
return spec.specProxyProcessResponse(request, response, config);
}
export async function specProxyProcess(
event: FetchEvent,
config: spec.SpecConfiguration,
) {
return spec.specProxyProcess(event, config, CloudflareRequest);
}