Skip to content

Commit e45d9bc

Browse files
committed
default persist delay of 2000, calling $persist() writes immediately
1 parent bb00fe3 commit e45d9bc

17 files changed

+607
-46
lines changed

examples/counter/.wrangler/state/v3/do/durable-rpc-Counter/36515cee978e9313732757a355a6ecb16683602779f21c4d90f93bcc07c41819.sqlite-wal

Whitespace-only changes.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
const urls = new Set();
2+
3+
function checkURL(request, init) {
4+
const url =
5+
request instanceof URL
6+
? request
7+
: new URL(
8+
(typeof request === "string"
9+
? new Request(request, init)
10+
: request
11+
).url
12+
);
13+
if (url.port && url.port !== "443" && url.protocol === "https:") {
14+
if (!urls.has(url.toString())) {
15+
urls.add(url.toString());
16+
console.warn(
17+
`WARNING: known issue with \`fetch()\` requests to custom HTTPS ports in published Workers:\n` +
18+
` - ${url.toString()} - the custom port will be ignored when the Worker is published using the \`wrangler deploy\` command.\n`
19+
);
20+
}
21+
}
22+
}
23+
24+
globalThis.fetch = new Proxy(globalThis.fetch, {
25+
apply(target, thisArg, argArray) {
26+
const [request, init] = argArray;
27+
checkURL(request, init);
28+
return Reflect.apply(target, thisArg, argArray);
29+
},
30+
});
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import worker, * as OTHER_EXPORTS from "/Users/kevinwhitley/dev/kwhitley/itty-durable/examples/counter/index.ts";
2+
import * as __MIDDLEWARE_0__ from "/Users/kevinwhitley/.nvm/versions/node/v20.10.0/lib/node_modules/wrangler/templates/middleware/middleware-ensure-req-body-drained.ts";
3+
import * as __MIDDLEWARE_1__ from "/Users/kevinwhitley/.nvm/versions/node/v20.10.0/lib/node_modules/wrangler/templates/middleware/middleware-miniflare3-json-error.ts";
4+
5+
worker.middleware = [
6+
__MIDDLEWARE_0__.default,__MIDDLEWARE_1__.default,
7+
...(worker.middleware ?? []),
8+
].filter(Boolean);
9+
10+
export * from "/Users/kevinwhitley/dev/kwhitley/itty-durable/examples/counter/index.ts";
11+
export default worker;
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
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

Comments
 (0)