Skip to content

Commit

Permalink
wrangler: Improvements (#3999)
Browse files Browse the repository at this point in the history
* Wrangler support for Hyperdrive

* Update connection string parsing logic and API endpoints

* Update some copy and general cleanup

* Fix formatting

* Send scheme when updating or creating database config

* Update copy and route names to reflect changes on backend

* Add changeset and tests

* Fix trailing colon on scheme when sending create request

* Add wrangler.toml file support for hyperdrive

* Fix hyperdrive update and add option to disable caching

* Update binding variables and add configuration and deploy tests

* add `hyperdrive: []` to draftworker upload form

---------

Co-authored-by: Rahul Sethi <5822355+RamIdeas@users.noreply.github.com>
  • Loading branch information
OilyLime and RamIdeas authored Sep 26, 2023
1 parent d538973 commit ee6f345
Show file tree
Hide file tree
Showing 23 changed files with 898 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/happy-keys-rhyme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wrangler": minor
---

Adds support for Hyperdrive, via `wrangler hyperdrive`.
96 changes: 96 additions & 0 deletions packages/wrangler/src/__tests__/configuration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ describe("normalizeAndValidateConfig()", () => {
d1_databases: [],
vectorize: [],
constellation: [],
hyperdrive: [],
dev: {
ip: "0.0.0.0",
local_protocol: "http",
Expand Down Expand Up @@ -2076,6 +2077,101 @@ describe("normalizeAndValidateConfig()", () => {
});
});

describe("[hyperdrive]", () => {
it("should error if hyperdrive is an object", () => {
const { diagnostics } = normalizeAndValidateConfig(
{ hyperdrive: {} } as unknown as RawConfig,
undefined,
{ env: undefined }
);

expect(diagnostics.hasWarnings()).toBe(false);
expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
"Processing wrangler configuration:
- The field \\"hyperdrive\\" should be an array but got {}."
`);
});

it("should error if hyperdrive is a string", () => {
const { diagnostics } = normalizeAndValidateConfig(
{ hyperdrive: "BAD" } as unknown as RawConfig,
undefined,
{ env: undefined }
);

expect(diagnostics.hasWarnings()).toBe(false);
expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
"Processing wrangler configuration:
- The field \\"hyperdrive\\" should be an array but got \\"BAD\\"."
`);
});

it("should error if hyperdrive is a number", () => {
const { diagnostics } = normalizeAndValidateConfig(
{ hyperdrive: 999 } as unknown as RawConfig,
undefined,
{ env: undefined }
);

expect(diagnostics.hasWarnings()).toBe(false);
expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
"Processing wrangler configuration:
- The field \\"hyperdrive\\" should be an array but got 999."
`);
});

it("should error if hyperdrive is null", () => {
const { diagnostics } = normalizeAndValidateConfig(
{ hyperdrive: null } as unknown as RawConfig,
undefined,
{ env: undefined }
);

expect(diagnostics.hasWarnings()).toBe(false);
expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
"Processing wrangler configuration:
- The field \\"hyperdrive\\" should be an array but got null."
`);
});

it("should accept valid bindings", () => {
const { diagnostics } = normalizeAndValidateConfig(
{
hyperdrive: [
{ binding: "VALID", id: "343cd4f1d58c42fbb5bd082592fd7143" },
],
} as unknown as RawConfig,
undefined,
{ env: undefined }
);

expect(diagnostics.hasErrors()).toBe(false);
});

it("should error if hyperdrive.bindings are not valid", () => {
const { diagnostics } = normalizeAndValidateConfig(
{
hyperdrive: [
{},
{ binding: "VALID", id: "343cd4f1d58c42fbb5bd082592fd7143" },
{ binding: 2000, project: 2111 },
],
} as unknown as RawConfig,
undefined,
{ env: undefined }
);

expect(diagnostics.hasWarnings()).toBe(true);
expect(diagnostics.renderErrors()).toMatchInlineSnapshot(`
"Processing wrangler configuration:
- \\"hyperdrive[0]\\" bindings should have a string \\"binding\\" field but got {}.
- \\"hyperdrive[0]\\" bindings must have a \\"id\\" field but got {}.
- \\"hyperdrive[2]\\" bindings should have a string \\"binding\\" field but got {\\"binding\\":2000,\\"project\\":2111}.
- \\"hyperdrive[2]\\" bindings must have a \\"id\\" field but got {\\"binding\\":2000,\\"project\\":2111}."
`);
});
});

describe("[queues]", () => {
it("should error if queues is not an object", () => {
const { config, diagnostics } = normalizeAndValidateConfig(
Expand Down
36 changes: 36 additions & 0 deletions packages/wrangler/src/__tests__/deploy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8394,6 +8394,42 @@ export default{
});
});

describe("hyperdrive", () => {
it("should upload hyperdrive bindings", async () => {
writeWranglerToml({
hyperdrive: [
{
binding: "HYPERDRIVE",
id: "343cd4f1d58c42fbb5bd082592fd7143",
},
],
});
await fs.promises.writeFile("index.js", `export default {};`);
mockSubDomainRequest();
mockUploadWorkerRequest({
expectedBindings: [
{
type: "hyperdrive",
name: "HYPERDRIVE",
id: "343cd4f1d58c42fbb5bd082592fd7143",
},
],
});

await runWrangler("deploy index.js");
expect(std.out).toMatchInlineSnapshot(`
"Total Upload: xx KiB / gzip: xx KiB
Your worker has access to the following bindings:
- Hyperdrive Configs:
- HYPERDRIVE: 343cd4f1d58c42fbb5bd082592fd7143
Uploaded test-name (TIMINGS)
Published test-name (TIMINGS)
https://test-name.test-sub-domain.workers.dev
Current Deployment ID: Galaxy-Class"
`);
});
});

describe("mtls_certificates", () => {
it("should upload mtls_certificate bindings", async () => {
writeWranglerToml({
Expand Down
Loading

0 comments on commit ee6f345

Please sign in to comment.