Skip to content

Commit

Permalink
default persist delay of 2000, calling $persist() writes immediately
Browse files Browse the repository at this point in the history
  • Loading branch information
kwhitley committed Jun 12, 2024
1 parent bb00fe3 commit e45d9bc
Show file tree
Hide file tree
Showing 17 changed files with 607 additions and 46 deletions.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
30 changes: 30 additions & 0 deletions examples/counter/.wrangler/tmp/bundle-7e8VMq/checked-fetch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const urls = new Set();

function checkURL(request, init) {
const url =
request instanceof URL
? request
: new URL(
(typeof request === "string"
? new Request(request, init)
: request
).url
);
if (url.port && url.port !== "443" && url.protocol === "https:") {
if (!urls.has(url.toString())) {
urls.add(url.toString());
console.warn(
`WARNING: known issue with \`fetch()\` requests to custom HTTPS ports in published Workers:\n` +
` - ${url.toString()} - the custom port will be ignored when the Worker is published using the \`wrangler deploy\` command.\n`
);
}
}
}

globalThis.fetch = new Proxy(globalThis.fetch, {
apply(target, thisArg, argArray) {
const [request, init] = argArray;
checkURL(request, init);
return Reflect.apply(target, thisArg, argArray);
},
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import worker, * as OTHER_EXPORTS from "/Users/kevinwhitley/dev/kwhitley/itty-durable/examples/counter/index.ts";
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";
import * as __MIDDLEWARE_1__ from "/Users/kevinwhitley/.nvm/versions/node/v20.10.0/lib/node_modules/wrangler/templates/middleware/middleware-miniflare3-json-error.ts";

worker.middleware = [
__MIDDLEWARE_0__.default,__MIDDLEWARE_1__.default,
...(worker.middleware ?? []),
].filter(Boolean);

export * from "/Users/kevinwhitley/dev/kwhitley/itty-durable/examples/counter/index.ts";
export default worker;
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
// This loads all middlewares exposed on the middleware object and then starts
// the invocation chain. The big idea is that we can add these to the middleware
// export dynamically through wrangler, or we can potentially let users directly
// add them as a sort of "plugin" system.

import ENTRY from "/Users/kevinwhitley/dev/kwhitley/itty-durable/examples/counter/.wrangler/tmp/bundle-7e8VMq/middleware-insertion-facade.js";
import { __facade_invoke__, __facade_register__, Dispatcher } from "/Users/kevinwhitley/.nvm/versions/node/v20.10.0/lib/node_modules/wrangler/templates/middleware/common.ts";
import type {
WithMiddleware,
WorkerEntrypointConstructor,
} from "/Users/kevinwhitley/dev/kwhitley/itty-durable/examples/counter/.wrangler/tmp/bundle-7e8VMq/middleware-insertion-facade.js";

// Preserve all the exports from the worker
export * from "/Users/kevinwhitley/dev/kwhitley/itty-durable/examples/counter/.wrangler/tmp/bundle-7e8VMq/middleware-insertion-facade.js";

class __Facade_ScheduledController__ implements ScheduledController {
readonly #noRetry: ScheduledController["noRetry"];

constructor(
readonly scheduledTime: number,
readonly cron: string,
noRetry: ScheduledController["noRetry"]
) {
this.#noRetry = noRetry;
}

noRetry() {
if (!(this instanceof __Facade_ScheduledController__)) {
throw new TypeError("Illegal invocation");
}
// Need to call native method immediately in case uncaught error thrown
this.#noRetry();
}
}

function wrapExportedHandler(
worker: WithMiddleware<ExportedHandler>
): ExportedHandler {
// If we don't have any middleware defined, just return the handler as is
if (worker.middleware === undefined || worker.middleware.length === 0) {
return worker;
}
// Otherwise, register all middleware once
for (const middleware of worker.middleware) {
__facade_register__(middleware);
}

const fetchDispatcher: ExportedHandlerFetchHandler = function (
request,
env,
ctx
) {
if (worker.fetch === undefined) {
throw new Error("Handler does not export a fetch() function.");
}
return worker.fetch(request, env, ctx);
};

return {
...worker,
fetch(request, env, ctx) {
const dispatcher: Dispatcher = function (type, init) {
if (type === "scheduled" && worker.scheduled !== undefined) {
const controller = new __Facade_ScheduledController__(
Date.now(),
init.cron ?? "",
() => {}
);
return worker.scheduled(controller, env, ctx);
}
};
return __facade_invoke__(request, env, ctx, dispatcher, fetchDispatcher);
},
};
}

function wrapWorkerEntrypoint(
klass: WithMiddleware<WorkerEntrypointConstructor>
): WorkerEntrypointConstructor {
// If we don't have any middleware defined, just return the handler as is
if (klass.middleware === undefined || klass.middleware.length === 0) {
return klass;
}
// Otherwise, register all middleware once
for (const middleware of klass.middleware) {
__facade_register__(middleware);
}

// `extend`ing `klass` here so other RPC methods remain callable
return class extends klass {
#fetchDispatcher: ExportedHandlerFetchHandler<Record<string, unknown>> = (
request,
env,
ctx
) => {
this.env = env;
this.ctx = ctx;
if (super.fetch === undefined) {
throw new Error("Entrypoint class does not define a fetch() function.");
}
return super.fetch(request);
};

#dispatcher: Dispatcher = (type, init) => {
if (type === "scheduled" && super.scheduled !== undefined) {
const controller = new __Facade_ScheduledController__(
Date.now(),
init.cron ?? "",
() => {}
);
return super.scheduled(controller);
}
};

fetch(request: Request<unknown, IncomingRequestCfProperties>) {
return __facade_invoke__(
request,
this.env,
this.ctx,
this.#dispatcher,
this.#fetchDispatcher
);
}
};
}

let WRAPPED_ENTRY: ExportedHandler | WorkerEntrypointConstructor | undefined;
if (typeof ENTRY === "object") {
WRAPPED_ENTRY = wrapExportedHandler(ENTRY);
} else if (typeof ENTRY === "function") {
WRAPPED_ENTRY = wrapWorkerEntrypoint(ENTRY);
}
export default WRAPPED_ENTRY;
Loading

0 comments on commit e45d9bc

Please sign in to comment.