import { serve, serveTLS, ServerRequest, Server } from "https://deno.land/std/http/server.ts";import { readerFromStreamReader, readableStreamFromIterable } from 'https://deno.land/std/io/streams.ts';import * as flags from "https://deno.land/std/flags/mod.ts";
const respond = (denoReq: ServerRequest) => async ({ body, headers, status }: Response) => { const reader = body?.getReader(); try { const denoReader = reader && readerFromStreamReader(reader); if (!headers.has('content-type')) headers.set('content-type', 'text/plain'); await denoReq.respond({ status, headers, body: denoReader, }); } catch (err) { reader?.cancel(); if (err instanceof Deno.errors.BrokenPipe) {} else console.error(err); }}
class DenoFetchEvent extends Event implements FetchEvent { #request: Request; #respond: (r: Response) => Promise<void>;
constructor(type: string, eventInitDict: FetchEventInit); constructor(denoReq: ServerRequest); constructor(denoReq: string | ServerRequest, _eventInitDict?: FetchEventInit) { super('fetch'); if (!(denoReq instanceof ServerRequest)) throw Error('Overload not implemented');
this.#respond = respond(denoReq);
const info = new URL(denoReq.url, self.location.origin).href; denoReq.headers.set('deno-connecting-ip', (denoReq.conn.remoteAddr as Deno.NetAddr).hostname); this.#request = new Request(info, { method: denoReq.method, headers: denoReq.headers, body: readableStreamFromIterable(Deno.iter(denoReq.body)), }); } get request(): Request { return this.#request }; respondWith(r: Response | Promise<Response>): void { if (r instanceof Promise) r.then(this.#respond); else this.#respond(r); } waitUntil(_f: any): void { }
get clientId(): string { return '' }; get preloadResponse(): Promise<any> { return Promise.resolve() }; get replacesClientId(): string { return '' }; get resultingClientId(): string { return '' };}
self.FetchEvent = DenoFetchEvent;
;(async () => { let server: Server;
if (self.location.protocol === 'https:' || self.location.port === '433') { const { c, cert, k, key } = flags.parse(Deno.args); const certFile = cert || c; const keyFile = key || k;
if (!certFile || !keyFile) { console.warn('When using HTTPS or port 443, a --cert and --key are required.'); }
server = serveTLS({ hostname: self.location.hostname, port: Number(self.location.port || 443), certFile, keyFile, }); } else { server = serve({ hostname: self.location.hostname, port: Number(self.location.port || 80), }); }
for await (const req of server) { self.dispatchEvent(new DenoFetchEvent(req)); }})();
declare global { interface ExtendableEvent extends Event { waitUntil(f: any): void; }
interface ExtendableEventInit extends EventInit { }
var ExtendableEvent: { prototype: ExtendableEvent; new(type: string, eventInitDict?: ExtendableEventInit): ExtendableEvent; };
interface FetchEventInit extends ExtendableEventInit { clientId?: string; preloadResponse?: Promise<any>; replacesClientId?: string; request: Request; resultingClientId?: string; }
var FetchEvent: { prototype: FetchEvent; new(type: string, eventInitDict: FetchEventInit): FetchEvent; };
interface FetchEvent extends ExtendableEvent { readonly clientId: string; readonly preloadResponse: Promise<any>; readonly replacesClientId: string; readonly request: Request; readonly resultingClientId: string; respondWith(r: Response | Promise<Response>): void; }
interface Window { FetchEvent: new (type: string, eventInitDict: FetchEventInit) => FetchEvent; }
function addEventListener(type: 'fetch', handler: (event: FetchEvent) => void): void;}