this repo has no description
1// This loads all middlewares exposed on the middleware object and then starts 2// the invocation chain. The big idea is that we can add these to the middleware 3// export dynamically through wrangler, or we can potentially let users directly 4// add them as a sort of "plugin" system. 5 6import ENTRY, { __INTERNAL_WRANGLER_MIDDLEWARE__ } from "/Users/chadmiller/code/pds-experiment/.wrangler/tmp/bundle-MaCAbF/middleware-insertion-facade.js"; 7import { __facade_invoke__, __facade_register__, Dispatcher } from "/Users/chadmiller/.npm/_npx/32026684e21afda6/node_modules/wrangler/templates/middleware/common.ts"; 8import type { WorkerEntrypointConstructor } from "/Users/chadmiller/code/pds-experiment/.wrangler/tmp/bundle-MaCAbF/middleware-insertion-facade.js"; 9 10// Preserve all the exports from the worker 11export * from "/Users/chadmiller/code/pds-experiment/.wrangler/tmp/bundle-MaCAbF/middleware-insertion-facade.js"; 12 13class __Facade_ScheduledController__ implements ScheduledController { 14 readonly #noRetry: ScheduledController["noRetry"]; 15 16 constructor( 17 readonly scheduledTime: number, 18 readonly cron: string, 19 noRetry: ScheduledController["noRetry"] 20 ) { 21 this.#noRetry = noRetry; 22 } 23 24 noRetry() { 25 if (!(this instanceof __Facade_ScheduledController__)) { 26 throw new TypeError("Illegal invocation"); 27 } 28 // Need to call native method immediately in case uncaught error thrown 29 this.#noRetry(); 30 } 31} 32 33function wrapExportedHandler(worker: ExportedHandler): ExportedHandler { 34 // If we don't have any middleware defined, just return the handler as is 35 if ( 36 __INTERNAL_WRANGLER_MIDDLEWARE__ === undefined || 37 __INTERNAL_WRANGLER_MIDDLEWARE__.length === 0 38 ) { 39 return worker; 40 } 41 // Otherwise, register all middleware once 42 for (const middleware of __INTERNAL_WRANGLER_MIDDLEWARE__) { 43 __facade_register__(middleware); 44 } 45 46 const fetchDispatcher: ExportedHandlerFetchHandler = function ( 47 request, 48 env, 49 ctx 50 ) { 51 if (worker.fetch === undefined) { 52 throw new Error("Handler does not export a fetch() function."); 53 } 54 return worker.fetch(request, env, ctx); 55 }; 56 57 return { 58 ...worker, 59 fetch(request, env, ctx) { 60 const dispatcher: Dispatcher = function (type, init) { 61 if (type === "scheduled" && worker.scheduled !== undefined) { 62 const controller = new __Facade_ScheduledController__( 63 Date.now(), 64 init.cron ?? "", 65 () => {} 66 ); 67 return worker.scheduled(controller, env, ctx); 68 } 69 }; 70 return __facade_invoke__(request, env, ctx, dispatcher, fetchDispatcher); 71 }, 72 }; 73} 74 75function wrapWorkerEntrypoint( 76 klass: WorkerEntrypointConstructor 77): WorkerEntrypointConstructor { 78 // If we don't have any middleware defined, just return the handler as is 79 if ( 80 __INTERNAL_WRANGLER_MIDDLEWARE__ === undefined || 81 __INTERNAL_WRANGLER_MIDDLEWARE__.length === 0 82 ) { 83 return klass; 84 } 85 // Otherwise, register all middleware once 86 for (const middleware of __INTERNAL_WRANGLER_MIDDLEWARE__) { 87 __facade_register__(middleware); 88 } 89 90 // `extend`ing `klass` here so other RPC methods remain callable 91 return class extends klass { 92 #fetchDispatcher: ExportedHandlerFetchHandler<Record<string, unknown>> = ( 93 request, 94 env, 95 ctx 96 ) => { 97 this.env = env; 98 this.ctx = ctx; 99 if (super.fetch === undefined) { 100 throw new Error("Entrypoint class does not define a fetch() function."); 101 } 102 return super.fetch(request); 103 }; 104 105 #dispatcher: Dispatcher = (type, init) => { 106 if (type === "scheduled" && super.scheduled !== undefined) { 107 const controller = new __Facade_ScheduledController__( 108 Date.now(), 109 init.cron ?? "", 110 () => {} 111 ); 112 return super.scheduled(controller); 113 } 114 }; 115 116 fetch(request: Request<unknown, IncomingRequestCfProperties>) { 117 return __facade_invoke__( 118 request, 119 this.env, 120 this.ctx, 121 this.#dispatcher, 122 this.#fetchDispatcher 123 ); 124 } 125 }; 126} 127 128let WRAPPED_ENTRY: ExportedHandler | WorkerEntrypointConstructor | undefined; 129if (typeof ENTRY === "object") { 130 WRAPPED_ENTRY = wrapExportedHandler(ENTRY); 131} else if (typeof ENTRY === "function") { 132 WRAPPED_ENTRY = wrapWorkerEntrypoint(ENTRY); 133} 134export default WRAPPED_ENTRY;