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