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

feat: add support for --local-protocol=https to wrangler dev #580

Merged
merged 5 commits into from
Mar 13, 2022

Conversation

petebacondarwin
Copy link
Contributor

@petebacondarwin petebacondarwin commented Mar 12, 2022

This change adds full support for the setting the protocol that the localhost proxy server listens to.
Previously, it was only possible to use HTTP. But now you can set it to HTTPS as well.

To support HTTPS, Wrangler needs an SSL certificate.
Wrangler now generates a self-signed certificate, as needed, and caches it in the ~/.wrangler/local-cert directory.
These certificates expire after 30 days and are regenerated by Wrangler as needed.

Note that if you use HTTPS then your browser will complain about the self-signed and you must tell it to accept the certificate before it will let you access the page.

Closes #582

@changeset-bot
Copy link

changeset-bot bot commented Mar 12, 2022

🦋 Changeset detected

Latest commit: 0a36d4c

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
wrangler Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions
Copy link
Contributor

github-actions bot commented Mar 12, 2022

A wrangler prerelease is available for testing. You can install this latest build in your project with:

npm install --save-dev https://prerelease-registry.developers.workers.dev/runs/1976585367/npm-package-wrangler-580

You can reference the automatically updated head of this PR with:

npm install --save-dev https://prerelease-registry.developers.workers.dev/prs/580/npm-package-wrangler-580

Or you can use npx with this latest build directly:

npx https://prerelease-registry.developers.workers.dev/runs/1976585367/npm-package-wrangler-580 dev path/to/script.js

@petebacondarwin petebacondarwin force-pushed the dev-protocols branch 2 times, most recently from e78d0b8 to 0c46580 Compare March 12, 2022 12:22
@petebacondarwin petebacondarwin changed the title feat: add support for --local-protocol=https to wrangler dev feat: add support for --local-protocol=https to wrangler dev Mar 12, 2022
@petebacondarwin petebacondarwin marked this pull request as ready for review March 12, 2022 13:04
Copy link
Contributor

@threepointone threepointone left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I went through all the commits, this looks totally great. I was surprised you did this so quickly! (I'm a noob at all the network bits, I learnt a fair bit from this PR). Chrome popping up the warning is unfortunate but unavoidable. Let's land this and give it a spin! Thank thank thank you!

Comment on lines 95 to 97
fs.mkdirSync(CERT_CACHE_DIR, { recursive: true });
fs.writeFileSync(keyPath, key, "utf8");
fs.writeFileSync(certPath, cert, "utf8");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should have a guard around it so we can give a nicer error message if and when this fails because we don't have write perms to the directory

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well, it'll be dev and it would be weird if we don't have write perms, but still...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough. I'll add that.

// The typings are not quite clever enough to handle element accesses, only property accesses,
// so we need to cast here.
(args["local-protocol"] as "http" | "https") ||
config.dev.upstream_protocol
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wait, shouldn't this be

Suggested change
config.dev.upstream_protocol
config.dev.local_protocol

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops!

@@ -1263,6 +1269,7 @@ export async function main(argv: string[]): Promise<void> {
initialMode={args.local ? "local" : "remote"}
jsxFactory={envRootObj?.jsx_factory}
jsxFragment={envRootObj?.jsx_fragment}
localProtocol={config.dev.upstream_protocol}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
localProtocol={config.dev.upstream_protocol}
localProtocol={config.dev.local_protocol}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

This internal change just makes the final `Config["dev"]` type expect to have non-optional properties, making it cleaner to use in code.
This is done, similarly to the `Environment` type by introducing `DevConfig` and partial `RawDevConfig` types,
which represent the runtime config and the `wrangler.toml` types respectively.
The `normalizeAndValidateDev()` helper now converts from the `RawDevConfig` to the `DevConfig`.
This change adds full support for the setting the protocol that the localhost proxy server listens to.
Previously, it was only possible to use `HTTP`. But now you can set it to `HTTPS` as well.

To support `HTTPS`, Wrangler needs an SSL certificate.
Wrangler now generates a self-signed certificate, as needed, and caches it in the `~/.wrangler/local-cert` directory.
These certificates expire after 30 days and are regenerated by Wrangler as needed.

Note that if you use HTTPS then your browser will complain about the self-signed and you must tell it to accept the certificate before it will let you access the page.
@petebacondarwin
Copy link
Contributor Author

I fixed the issues, and added some tests, which actually found further issues!

@petebacondarwin petebacondarwin merged commit 1cb7ae2 into cloudflare:main Mar 13, 2022
@petebacondarwin petebacondarwin deleted the dev-protocols branch March 13, 2022 14:38
@github-actions github-actions bot mentioned this pull request Mar 13, 2022
mrbbot added a commit that referenced this pull request Oct 31, 2023
Miniflare extracts a relative module "name" from module paths. On
Windows, this process lead to names with `\`s in them. This meant
imports had to look like `import { ... } from "dir\\thing.js"` which
isn't right. This change normalises all import names to contain
forward slashes instead. Source mapping code is unaffected, as Node
will automatically normalise `/` to `\`s on Windows.
mrbbot added a commit that referenced this pull request Nov 1, 2023
Miniflare extracts a relative module "name" from module paths. On
Windows, this process lead to names with `\`s in them. This meant
imports had to look like `import { ... } from "dir\\thing.js"` which
isn't right. This change normalises all import names to contain
forward slashes instead. Source mapping code is unaffected, as Node
will automatically normalise `/` to `\`s on Windows.
mrbbot added a commit that referenced this pull request Nov 1, 2023
Miniflare extracts a relative module "name" from module paths. On
Windows, this process lead to names with `\`s in them. This meant
imports had to look like `import { ... } from "dir\\thing.js"` which
isn't right. This change normalises all import names to contain
forward slashes instead. Source mapping code is unaffected, as Node
will automatically normalise `/` to `\`s on Windows.
mrbbot added a commit that referenced this pull request Nov 1, 2023
Miniflare extracts a relative module "name" from module paths. On
Windows, this process lead to names with `\`s in them. This meant
imports had to look like `import { ... } from "dir\\thing.js"` which
isn't right. This change normalises all import names to contain
forward slashes instead. Source mapping code is unaffected, as Node
will automatically normalise `/` to `\`s on Windows.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement config.dev.local_protocol/--local-protocol
2 participants