Skip to content

Commit

Permalink
fix: respect variable binding type when printing (#1939)
Browse files Browse the repository at this point in the history
  • Loading branch information
rozenmd committed Sep 28, 2022
1 parent 5679815 commit 5854cb6
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 20 deletions.
7 changes: 7 additions & 0 deletions .changeset/proud-cheetahs-kick.md
@@ -0,0 +1,7 @@
---
"wrangler": patch
---

fix: respect variable binding type when printing

After this change, when printing the bindings it has access to, wrangler will correctly only add quotes around string variables, and serialize objects via JSON.stringify (rather than printing `"[object Object]"`).
24 changes: 12 additions & 12 deletions packages/wrangler/src/__tests__/dev.test.tsx
Expand Up @@ -1326,18 +1326,18 @@ describe("wrangler dev", () => {
fs.writeFileSync("index.js", `export default {};`);
await runWrangler("dev index.js");
expect(std).toMatchInlineSnapshot(`
Object {
"debug": "",
"err": "",
"out": "Using vars defined in .dev.vars
Your worker has access to the following bindings:
- Vars:
- variable: \\"123\\"
- overriden: \\"(hidden)\\"
- SECRET: \\"(hidden)\\"",
"warn": "",
}
`);
Object {
"debug": "",
"err": "",
"out": "Using vars defined in .dev.vars
Your worker has access to the following bindings:
- Vars:
- variable: 123
- overriden: \\"(hidden)\\"
- SECRET: \\"(hidden)\\"",
"warn": "",
}
`);
});
});
});
Expand Down
11 changes: 7 additions & 4 deletions packages/wrangler/src/__tests__/publish.test.ts
Expand Up @@ -512,7 +512,7 @@ describe("publish", () => {
"Total Upload: xx KiB / gzip: xx KiB
Your worker has access to the following bindings:
- Vars:
- xyz: \\"123\\"
- xyz: 123
Uploaded test-name (TIMINGS)
Published test-name (TIMINGS)
https://test-name.test-sub-domain.workers.dev"
Expand Down Expand Up @@ -4527,7 +4527,7 @@ addEventListener('fetch', event => {});`
- some unsafe thing: UNSAFE_BINDING_ONE
- another unsafe thing: UNSAFE_BINDING_TWO
- Vars:
- ENV_VAR_ONE: \\"123\\"
- ENV_VAR_ONE: 123
- ENV_VAR_TWO: \\"Hello, I'm an environment variable\\"
- Wasm Modules:
- WASM_MODULE_ONE: some_wasm.wasm
Expand Down Expand Up @@ -5275,8 +5275,11 @@ addEventListener('fetch', event => {});`
Your worker has access to the following bindings:
- Vars:
- text: \\"plain ol' string\\"
- count: \\"1\\"
- complex: \\"[object Object]\\"
- count: 1
- complex: {
\\"enabled\\": true,
\\"id\\": 123
}
Uploaded test-name (TIMINGS)
Published test-name (TIMINGS)
https://test-name.test-sub-domain.workers.dev"
Expand Down
18 changes: 14 additions & 4 deletions packages/wrangler/src/config/index.ts
Expand Up @@ -219,10 +219,20 @@ export function printBindings(bindings: CfWorkerInit["bindings"]) {
if (vars !== undefined && Object.keys(vars).length > 0) {
output.push({
type: "Vars",
entries: Object.entries(vars).map(([key, value]) => ({
key,
value: `"${truncate(`${value}`)}"`,
})),
entries: Object.entries(vars).map(([key, value]) => {
let parsedValue;
if (typeof value === "string") {
parsedValue = `"${truncate(value)}"`;
} else if (typeof value === "object") {
parsedValue = JSON.stringify(value, null, 1);
} else {
parsedValue = `${truncate(`${value}`)}`;
}
return {
key,
value: parsedValue,
};
}),
});
}

Expand Down

0 comments on commit 5854cb6

Please sign in to comment.