Skip to content

Commit

Permalink
Add (local) indicator to bindings using local data (#6972)
Browse files Browse the repository at this point in the history
  • Loading branch information
penalosa authored Oct 21, 2024
1 parent 381d04f commit c794935
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 58 deletions.
5 changes: 5 additions & 0 deletions .changeset/wet-dryers-pretend.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wrangler": patch
---

Add ` (local)` indicator to bindings using local data
36 changes: 18 additions & 18 deletions packages/wrangler/e2e/__snapshots__/pages-dev.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@ Your worker has access to the following bindings:
- DO_BINDING_2_TOML: DO_2_TOML (defined in 🔴 DO_SCRIPT_2_TOML)
- DO_BINDING_3_ARGS: DO_3_ARGS (defined in 🔴 DO_SCRIPT_3_ARGS)
- KV Namespaces:
- KV_BINDING_1_TOML: NEW_KV_ID_1
- KV_BINDING_2_TOML: KV_ID_2_TOML
- KV_BINDING_3_ARGS: KV_ID_3_ARGS
- KV_BINDING_1_TOML: NEW_KV_ID_1 (local)
- KV_BINDING_2_TOML: KV_ID_2_TOML (local)
- KV_BINDING_3_ARGS: KV_ID_3_ARGS (local)
- D1 Databases:
- D1_BINDING_1_TOML: local-D1_BINDING_1_TOML=NEW_D1_NAME_1 (NEW_D1_NAME_1)
- D1_BINDING_2_TOML: D1_NAME_2_TOML (D1_ID_2_TOML)
- D1_BINDING_3_ARGS: local-D1_BINDING_3_ARGS=D1_NAME_3_ARGS (D1_NAME_3_ARGS)
- D1_BINDING_1_TOML: local-D1_BINDING_1_TOML=NEW_D1_NAME_1 (NEW_D1_NAME_1) (local)
- D1_BINDING_2_TOML: D1_NAME_2_TOML (D1_ID_2_TOML) (local)
- D1_BINDING_3_ARGS: local-D1_BINDING_3_ARGS=D1_NAME_3_ARGS (D1_NAME_3_ARGS) (local)
- R2 Buckets:
- R2_BINDING_1_TOML: NEW_R2_BUCKET_1
- R2_BINDING_2_TOML: R2_BUCKET_2_TOML
- R2_BINDING_3_TOML: R2_BUCKET_3_ARGS
- R2_BINDING_1_TOML: NEW_R2_BUCKET_1 (local)
- R2_BINDING_2_TOML: R2_BUCKET_2_TOML (local)
- R2_BINDING_3_TOML: R2_BUCKET_3_ARGS (local)
- Services:
- SERVICE_BINDING_1_TOML: 🔴 NEW_SERVICE_NAME_1
- SERVICE_BINDING_2_TOML: 🔴 SERVICE_NAME_2_TOML
Expand Down Expand Up @@ -53,17 +53,17 @@ Your worker has access to the following bindings:
- DO_BINDING_2_TOML: DO_2_TOML (defined in 🔴 DO_SCRIPT_2_TOML)
- DO_BINDING_3_ARGS: DO_3_ARGS (defined in 🔴 DO_SCRIPT_3_ARGS)
- KV Namespaces:
- KV_BINDING_1_TOML: NEW_KV_ID_1
- KV_BINDING_2_TOML: KV_ID_2_TOML
- KV_BINDING_3_ARGS: KV_ID_3_ARGS
- KV_BINDING_1_TOML: NEW_KV_ID_1 (local)
- KV_BINDING_2_TOML: KV_ID_2_TOML (local)
- KV_BINDING_3_ARGS: KV_ID_3_ARGS (local)
- D1 Databases:
- D1_BINDING_1_TOML: local-D1_BINDING_1_TOML=NEW_D1_NAME_1 (NEW_D1_NAME_1)
- D1_BINDING_2_TOML: D1_NAME_2_TOML (D1_ID_2_TOML)
- D1_BINDING_3_ARGS: local-D1_BINDING_3_ARGS=D1_NAME_3_ARGS (D1_NAME_3_ARGS)
- D1_BINDING_1_TOML: local-D1_BINDING_1_TOML=NEW_D1_NAME_1 (NEW_D1_NAME_1) (local)
- D1_BINDING_2_TOML: D1_NAME_2_TOML (D1_ID_2_TOML) (local)
- D1_BINDING_3_ARGS: local-D1_BINDING_3_ARGS=D1_NAME_3_ARGS (D1_NAME_3_ARGS) (local)
- R2 Buckets:
- R2_BINDING_1_TOML: NEW_R2_BUCKET_1
- R2_BINDING_2_TOML: R2_BUCKET_2_TOML
- R2_BINDING_3_TOML: R2_BUCKET_3_ARGS
- R2_BINDING_1_TOML: NEW_R2_BUCKET_1 (local)
- R2_BINDING_2_TOML: R2_BUCKET_2_TOML (local)
- R2_BINDING_3_TOML: R2_BUCKET_3_ARGS (local)
- Services:
- SERVICE_BINDING_1_TOML: 🔴 NEW_SERVICE_NAME_1
- SERVICE_BINDING_2_TOML: 🔴 SERVICE_NAME_2_TOML
Expand Down
38 changes: 18 additions & 20 deletions packages/wrangler/e2e/pages-dev.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,20 +117,19 @@ describe.each([
`${cmd} . --port ${port} --service TEST_SERVICE=test-worker --kv TEST_KV --do TEST_DO=TestDurableObject@a --d1 TEST_D1 --r2 TEST_R2`
);
await worker.waitForReady();
expect(normalizeOutput(worker.currentOutput).replace(/\s/g, "")).toContain(
`
Your worker has access to the following bindings:
- Durable Objects:
- TEST_DO: TestDurableObject (defined in 🔴 a)
- KV Namespaces:
- TEST_KV: TEST_KV
- D1 Databases:
- TEST_D1: local-TEST_D1 (TEST_D1)
- R2 Buckets:
- TEST_R2: TEST_R2
- Services:
- TEST_SERVICE: 🔴 test-worker
`.replace(/\s/g, "")
expect(normalizeOutput(worker.currentOutput)).toContain(
dedent`Your worker has access to the following bindings:
- Durable Objects:
- TEST_DO: TestDurableObject (defined in 🔴 a)
- KV Namespaces:
- TEST_KV: TEST_KV (local)
- D1 Databases:
- TEST_D1: local-TEST_D1 (TEST_D1) (local)
- R2 Buckets:
- TEST_R2: TEST_R2 (local)
- Services:
- TEST_SERVICE: 🔴 test-worker
`
);
});

Expand Down Expand Up @@ -320,14 +319,13 @@ describe.each([
const text = await fetchText(url);

expect(text).toBe("⚡️ Pages ⚡️ supports wrangler.toml");
expect(normalizeOutput(worker.currentOutput).replace(/\s/g, "")).toContain(
`
Your worker has access to the following bindings:
expect(normalizeOutput(worker.currentOutput)).toContain(
dedent`Your worker has access to the following bindings:
- KV Namespaces:
- KV_BINDING_TOML: KV_ID_TOML
- KV_BINDING_TOML: KV_ID_TOML (local)
- Vars:
- PAGES: "⚡️ Pages ⚡️"
`.replace(/\s/g, "")
- PAGES: "⚡️ Pages ⚡️"
`
);
});

Expand Down
6 changes: 3 additions & 3 deletions packages/wrangler/src/__tests__/deploy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8189,7 +8189,7 @@ addEventListener('fetch', event => {});`
"Total Upload: xx KiB / gzip: xx KiB
Worker Startup Time: 100 ms
Your worker has access to the following bindings:
- dispatch namespaces:
- Dispatch Namespaces:
- foo: Foo
Uploaded test-name (TIMINGS)
Deployed test-name triggers (TIMINGS)
Expand Down Expand Up @@ -8247,7 +8247,7 @@ addEventListener('fetch', event => {});`
"Total Upload: xx KiB / gzip: xx KiB
Worker Startup Time: 100 ms
Your worker has access to the following bindings:
- dispatch namespaces:
- Dispatch Namespaces:
- foo: Foo (outbound -> foo_outbound)
- bar: Bar (outbound -> bar_outbound)
Uploaded test-name (TIMINGS)
Expand Down Expand Up @@ -8304,7 +8304,7 @@ addEventListener('fetch', event => {});`
"Total Upload: xx KiB / gzip: xx KiB
Worker Startup Time: 100 ms
Your worker has access to the following bindings:
- dispatch namespaces:
- Dispatch Namespaces:
- foo: Foo (outbound -> foo_outbound)
Uploaded test-name (TIMINGS)
Deployed test-name triggers (TIMINGS)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ async function resolveBindings(
...bindings,
vars: maskedVars,
},
input.dev?.registry
{ registry: input.dev?.registry, local: !input.dev?.remote }
);

return {
Expand Down
29 changes: 18 additions & 11 deletions packages/wrangler/src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,12 +192,19 @@ export function findWranglerToml(
return findUpSync(`wrangler.toml`, { cwd: referencePath });
}

function addLocalSuffix(id: string, local: boolean = false) {
return `${id}${local ? " (local)" : ""}`;
}

/**
* Print all the bindings a worker using a given config would have access to
*/
export function printBindings(
bindings: CfWorkerInit["bindings"],
registry?: WorkerRegistry
context: {
registry?: WorkerRegistry;
local?: boolean;
} = {}
) {
const truncate = (item: string | Record<string, unknown>) => {
const s = typeof item === "string" ? item : JSON.stringify(item);
Expand Down Expand Up @@ -257,7 +264,7 @@ export function printBindings(
({ name, class_name, script_name }) => {
let value = class_name;
if (script_name) {
const registryDefinition = registry?.[script_name];
const registryDefinition = context.registry?.[script_name];
if (
registryDefinition &&
registryDefinition.durableObjects.some(
Expand Down Expand Up @@ -302,7 +309,7 @@ export function printBindings(
entries: kv_namespaces.map(({ binding, id }) => {
return {
key: binding,
value: id,
value: addLocalSuffix(id, context.local),
};
}),
});
Expand Down Expand Up @@ -331,7 +338,7 @@ export function printBindings(
entries: queues.map(({ binding, queue_name }) => {
return {
key: binding,
value: queue_name,
value: addLocalSuffix(queue_name, context.local),
};
}),
});
Expand All @@ -352,7 +359,7 @@ export function printBindings(
}
return {
key: binding,
value: databaseValue,
value: addLocalSuffix(databaseValue, context.local),
};
}
),
Expand All @@ -365,7 +372,7 @@ export function printBindings(
entries: vectorize.map(({ binding, index_name }) => {
return {
key: binding,
value: index_name,
value: addLocalSuffix(index_name, context.local),
};
}),
});
Expand All @@ -377,7 +384,7 @@ export function printBindings(
entries: hyperdrive.map(({ binding, id }) => {
return {
key: binding,
value: id,
value: addLocalSuffix(id, context.local),
};
}),
});
Expand All @@ -392,7 +399,7 @@ export function printBindings(
}
return {
key: binding,
value: bucket_name,
value: addLocalSuffix(bucket_name, context.local),
};
}),
});
Expand All @@ -419,7 +426,7 @@ export function printBindings(
value += `#${entrypoint}`;
}

const registryDefinition = registry?.[service];
const registryDefinition = context.registry?.[service];
if (
registryDefinition &&
(!entrypoint || registryDefinition.entrypointAddresses?.[entrypoint])
Expand Down Expand Up @@ -478,7 +485,7 @@ export function printBindings(

output.push({
type: "AI",
entries: entries,
entries,
});
}

Expand Down Expand Up @@ -541,7 +548,7 @@ export function printBindings(

if (dispatch_namespaces !== undefined && dispatch_namespaces.length > 0) {
output.push({
type: "dispatch namespaces",
type: "Dispatch Namespaces",
entries: dispatch_namespaces.map(({ binding, namespace, outbound }) => {
return {
key: binding,
Expand Down
11 changes: 7 additions & 4 deletions packages/wrangler/src/dev.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1414,10 +1414,13 @@ export function getResolvedBindings(

const maskedVars = maskVars(bindings, configParam);

printBindings({
...bindings,
vars: maskedVars,
});
printBindings(
{
...bindings,
vars: maskedVars,
},
{ local: !args.remote }
);

return bindings;
}
Expand Down
8 changes: 7 additions & 1 deletion packages/wrangler/src/paths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,13 @@ export function getWranglerTmpDir(
const tmpPrefix = path.join(tmpRoot, `${prefix}-`);
const tmpDir = fs.realpathSync(fs.mkdtempSync(tmpPrefix));

const removeDir = () => fs.rmSync(tmpDir, { recursive: true, force: true });
const removeDir = () => {
try {
return fs.rmSync(tmpDir, { recursive: true, force: true });
} catch (e) {
// This sometimes fails on Windows with EBUSY
}
};
const removeExitListener = onExit(removeDir);

return {
Expand Down

0 comments on commit c794935

Please sign in to comment.