|
| 1 | +import shim, { |
| 2 | + Sequencer, Batcher, Cleaner, __wbg_reset_state, |
| 3 | +} from "./build/worker/shim.mjs"; |
| 4 | + |
| 5 | +// no-op worker entrypoint for now |
| 6 | +const WorkerEntrypointProxy = new Proxy(shim, {}); |
| 7 | + |
| 8 | +const DOProxy = new Proxy(Sequencer, { |
| 9 | + construct(target, args) { |
| 10 | + const state = args[0] // get state |
| 11 | + // const env = args[1] // get env |
| 12 | + try { |
| 13 | + const instance = new target(...args); |
| 14 | + |
| 15 | + return new Proxy(instance, { |
| 16 | + get(target, prop, receiver) { |
| 17 | + const original = Reflect.get(target, prop, receiver); |
| 18 | + // Check if the property is a function |
| 19 | + if (typeof original === 'function') { |
| 20 | + return new Proxy(original, { |
| 21 | + async apply(target, thisArg, argArray) { |
| 22 | + try { |
| 23 | + const resultPromise = await target.bind(thisArg, ...argArray)(); |
| 24 | + // const resultPromise = await target.bind(thisArg, ...argArray)() |
| 25 | + return resultPromise; |
| 26 | + } catch (e) { |
| 27 | + if (e.message == "memory access out of bounds") { |
| 28 | + console.error(e) |
| 29 | + __wbg_reset_state(); |
| 30 | + state.abort("Call to DO panicked, force-restarting DO") |
| 31 | + return new Response("Worker panicked... restarting DO") |
| 32 | + } |
| 33 | + throw e; |
| 34 | + } |
| 35 | + |
| 36 | + |
| 37 | + } |
| 38 | + }); |
| 39 | + } |
| 40 | + return original; // Return the property directly if it's not a function |
| 41 | + } |
| 42 | + }); |
| 43 | + |
| 44 | + } catch (err) { |
| 45 | + __wbg_reset_state(); |
| 46 | + state.abort("Constructor for DO panicked, force-restarting DO") |
| 47 | + } |
| 48 | + }, |
| 49 | +}); |
| 50 | + |
| 51 | +export { DOProxy as Sequencer, Batcher, Cleaner, WorkerEntrypointProxy as default }; |
0 commit comments