const lockId = env.LOCK.idFromString(`${storeId}-${data.userEmail}`);
const lock = env.LOCK.get(lockId);
const lockUrl = new URL("http://0.0.0.0/lock");
lockUrl.searchParams.set("durationSecs", "30");
const lockResponse = await lock.fetch(lockUrl.toString());
if (lockResponse.status !== 204)
return new Error("Request being processed.");idFromString only accepts the result of an id.toString()
➜ wrangler pages deploy apps/page/.vercel/output/static --branch=main --project-name=<project-name> gives that exact resultzeroline.dev A @1.1.1.1 +noall +answerdiggy diggy hole
•4/17/24, 11:09 PM
zeroline.dev A @1.1.1.1 +noall +answerdiggy diggy hole
•4/17/24, 11:10 PM
09:48:54.023 ➤ YN0070: Migrating from Yarn 1; automatically enabling the compatibility node-modules linker
09:49:04.903 ➤ YN0028: │ The lockfile would have been modified by this install, which is explicitly forbidden.
idFromStringid.toString()➜ wrangler pages deploy apps/page/.vercel/output/static --branch=main --project-name=<project-name>zeroline.dev A @1.1.1.1 +noall +answerzeroline.dev A @1.1.1.1 +noall +answerNAME | TTL | DATA
-------------+------+--------------
zeroline.dev | 300s | 104.21.89.219
zeroline.dev | 300s | 172.67.165.53NAME | TTL | DATA
-------------+------+--------------
zeroline.dev | 300s | 172.67.165.53
zeroline.dev | 300s | 104.21.89.219...
09:48:47.149 Detected the following tools from environment: nodejs@19.9.0, yarn@3.6.3
09:48:47.150 Installing nodejs 19.9.0
09:48:49.166 Trying to update node-build... ok
09:48:49.385 Downloading node-v19.9.0-linux-x64.tar.gz...
09:48:49.385 -> https://nodejs.org/dist/v19.9.0/node-v19.9.0-linux-x64.tar.gz
09:48:50.984
09:48:50.985 WARNING: node-v19.9.0-linux-x64 is in LTS Maintenance mode and nearing its end of life.
09:48:50.985 It only receives *critical* security updates, *critical* bug fixes and documentation updates.
09:48:50.985
09:48:50.985 Installing node-v19.9.0-linux-x64...
09:48:51.407 Installed node-v19.9.0-linux-x64 to /opt/buildhome/.asdf/installs/nodejs/19.9.0
09:48:51.407
09:48:52.939 Preparing yarn@3.6.3 for immediate activation...
09:48:53.423 Installing project dependencies: yarn
09:48:54.023 ➤ YN0070: Migrating from Yarn 1; automatically enabling the compatibility node-modules linker 👍
09:48:54.023
09:48:54.099 ➤ YN0000: ┌ Resolution step
09:48:54.621 ➤ YN0061: │ vuex-persistedstate@npm:4.1.0 is deprecated: Package no longer supported. Contact Support at https://www.npmjs.com/support for more info.
09:48:54.725 ➤ YN0061: │ shvl@npm:2.0.3 is deprecated: older versions vulnerable to prototype pollution
09:48:55.033 ➤ YN0032: │ fsevents@npm:2.3.2: Implicit dependencies on node-gyp are discouraged
09:48:55.833 ➤ YN0061: │ @volar/vue-typescript@npm:1.2.0 is deprecated: WARNING: This project has been renamed to @vue/typescript. Install using @vue/typescript instead.
09:48:56.065 ➤ YN0061: │ sourcemap-codec@npm:1.4.8 is deprecated: Please use @jridgewell/sourcemap-codec instead
09:48:59.515 ➤ YN0061: │ rollup-plugin-inject@npm:3.0.2 is deprecated: This package has been deprecated and is no longer maintained. Please use @rollup/plugin-inject.
09:49:04.850 ➤ YN0002: │ aimoda@workspace:. doesn't provide @vue/cli-service (pa32e4), requested by @vue/cli-plugin-eslint
09:49:04.850 ➤ YN0002: │ aimoda@workspace:. doesn't provide webpack (pd52b7), requested by vue-loader
09:49:04.850 ➤ YN0002: │ aimoda@workspace:. doesn't provide worker-loader (pffbf6), requested by pdfjs-dist
09:49:04.850 ➤ YN0000: │ Some peer dependencies are incorrectly met; run yarn explain peer-requirements <hash> for details, where <hash> is the six-letter p-prefixed code
09:49:04.855 ➤ YN0000: └ Completed in 10s 755ms
09:49:04.902 ➤ YN0000: ┌ Post-resolution validation
09:49:04.903 ➤ YN0028: │ The lockfile would have been modified by this install, which is explicitly forbidden.
09:49:04.903 ➤ YN0000: └ Completed
09:49:04.903 ➤ YN0000: Failed with errors in 10s 808ms
09:49:04.981 Error: Exit with error code: 1
09:49:04.981 at ChildProcess.<anonymous> (/snapshot/dist/run-build.js)
09:49:04.982 at Object.onceWrapper (node:events:652:26)
09:49:04.982 at ChildProcess.emit (node:events:537:28)
09:49:04.982 at ChildProcess._handle.onexit (node:internal/child_process:291:12)
09:49:04.992 Failed: build command exited with code: 1
09:49:05.858 Failed: error occurred while running build commandasync fetch(request: Request) {
const url = new URL(request.url);
switch (url.pathname) {
case "/unlock": {
await this.state.storage.delete("lock");
return new Response(null, {
status: 204,
});
}
case "/lock": {
const lockedUntil = await this.state.storage.get<number>("lock");
if (lockedUntil && lockedUntil > Date.now())
return new Response(null, {
status: 409,
});
const durationSecsString = url.searchParams.get("durationSecs");
if (!durationSecsString)
return new Response(null, {
status: 400,
});
const durationSecsNumber = parseInt(durationSecsString);
if (
!Number.isSafeInteger(durationSecsNumber) ||
durationSecsNumber <= 0
)
return new Response(null, {
status: 400,
});
await this.state.storage.put("lock", Date.now());
return new Response(null, {
status: 204,
});
}
}
}export enum LockResult {
Success,
AlreadyLocked,
}
export class Lock {
private lockId: DurableObjectId;
private stub: DurableObjectStub;
constructor(name: string, cloudflareEnv: CloudflareEnv) {
this.lockId = cloudflareEnv.LOCK.idFromName(name);
this.stub = cloudflareEnv.LOCK.get(this.lockId);
}
async unlock() {
const unlockUrl = new URL("http://0.0.0.0/unlock");
const response = await this.stub.fetch(unlockUrl.toString());
if (response.status !== 204) throw new Error(await response.text());
}
async lock(durationSecs: number) {
const lockUrl = new URL("http://0.0.0.0/lock");
lockUrl.searchParams.set("durationSecs", durationSecs.toFixed(0));
const response = await this.stub.fetch(lockUrl.toString());
if (response.status === 204) return LockResult.Success;
if (response.status === 409) return LockResult.AlreadyLocked;
throw new Error(await response.text());
}
}
export class LockError extends Error {
constructor(msg: string) {
super(msg);
// Set the prototype explicitly.
Object.setPrototypeOf(this, LockError.prototype);
}
}
export async function withLock<T>(
name: string,
cloudflareEnv: CloudflareEnv,
lockExpirySecs: number,
fn: () => T,
unlockOnEnd = true,
): Promise<T> {
const lock = new Lock(name, cloudflareEnv);
const lockResult = await lock.lock(lockExpirySecs);
if (lockResult === LockResult.AlreadyLocked)
throw new LockError("Lock is already locked.");
try {
return fn();
} finally {
if (unlockOnEnd) await lock.unlock();
}
}