Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error if the user is trying to implement DO's in a service worker #640

Merged
merged 4 commits into from
Mar 22, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions packages/wrangler/src/entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,22 @@ export async function getEntry(
directory,
args.format ?? config.build?.upload?.format
);

if (format === "service-worker" && hasDurableObjectImplementations(config)) {
const errorMessage =
"You seem to be trying to use Durable Objects with Service Worker syntax.";
const addScriptName =
"You can use Durable Objects defined in other (Module) Workers from a Service Worker by specifying a `script_name` in your wrangler.toml,where `script_name` is the name of the worker that implements that Durable Object. For example:";
caass marked this conversation as resolved.
Show resolved Hide resolved
const addScriptNameExamples = generateAddScriptNameExamples(config);
const migrateText =
"Alternatively, migrate your worker to Module format to implement a new Durable Object:";
petebacondarwin marked this conversation as resolved.
Show resolved Hide resolved
const migrateUrl =
"https://developers.cloudflare.com/workers/learning/migrating-to-module-workers/";
throw new Error(
`${errorMessage}\n${addScriptName}\n${addScriptNameExamples}\n${migrateText}\n${migrateUrl}`
);
}

return { file, directory, format };
}

Expand Down Expand Up @@ -157,3 +173,48 @@ export function fileExists(filePath: string): boolean {
}
return false;
}

/**
* Returns true if the given config contains Durable Object bindings that are implemented
* in this worker instead of being implemented elsewhere, and bound via a `script_name`
* property in wrangler.toml
*/
function hasDurableObjectImplementations(config: Config): boolean {
const allBindings = [
...config.durable_objects.bindings,
...Object.values(config.env)
.map((env) => env.durable_objects.bindings)
.flat(),
];

return allBindings.some((binding) => binding.script_name === undefined);
}

/**
* Generates some help text based on the Durable Object bindings in a given
* config indicating how the user can add a `script_name` field to bind an
* externally defined Durable Object.
*/
function generateAddScriptNameExamples(config: Config): string {
const allBindings = [
...config.durable_objects.bindings,
...Object.values(config.env)
.map((env) => env.durable_objects.bindings)
.flat(),
];
petebacondarwin marked this conversation as resolved.
Show resolved Hide resolved

function exampleScriptName(binding_name: string): string {
return `${binding_name.toLowerCase().replaceAll("_", "-")}-worker`;
}

return allBindings
.filter((binding) => binding.script_name === undefined)
.map(({ name, class_name }) => {
const script_name = exampleScriptName(name);
const currentBinding = `{ name = ${name}, class_name = ${class_name} }`;
const fixedBinding = `{ name = ${name}, class_name = ${class_name}, script_name = ${script_name} }`;

return `${currentBinding} ==> ${fixedBinding}`;
})
.join("\n");
}