wrangler 0.0.0-e2a26a0 → 0.0.0-e3a458e

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (84) hide show
  1. package/package.json +24 -11
  2. package/pages/functions/filepath-routing.test.ts +212 -36
  3. package/pages/functions/filepath-routing.ts +69 -119
  4. package/pages/functions/routes.ts +8 -17
  5. package/pages/functions/template-worker.ts +3 -9
  6. package/src/__tests__/configuration.test.ts +2598 -0
  7. package/src/__tests__/dev.test.tsx +437 -63
  8. package/src/__tests__/dev2.test.tsx +154 -0
  9. package/src/__tests__/guess-worker-format.test.ts +58 -0
  10. package/src/__tests__/helpers/faye-websocket.d.ts +6 -0
  11. package/src/__tests__/helpers/mock-account-id.ts +26 -8
  12. package/src/__tests__/helpers/mock-bin.ts +4 -1
  13. package/src/__tests__/helpers/mock-cfetch.ts +5 -5
  14. package/src/__tests__/helpers/mock-console.ts +21 -7
  15. package/src/__tests__/helpers/mock-kv.ts +9 -16
  16. package/src/__tests__/helpers/mock-stdin.ts +103 -0
  17. package/src/__tests__/helpers/mock-web-socket.ts +29 -0
  18. package/src/__tests__/helpers/run-in-tmp.ts +3 -0
  19. package/src/__tests__/helpers/run-wrangler.ts +5 -3
  20. package/src/__tests__/helpers/write-wrangler-toml.ts +17 -0
  21. package/src/__tests__/https-options.test.ts +127 -0
  22. package/src/__tests__/index.test.ts +468 -20
  23. package/src/__tests__/jest.setup.ts +37 -5
  24. package/src/__tests__/kv.test.ts +426 -183
  25. package/src/__tests__/logout.test.ts +15 -13
  26. package/src/__tests__/package-manager.test.ts +2 -2
  27. package/src/__tests__/pages.test.ts +44 -0
  28. package/src/__tests__/parse.test.ts +322 -0
  29. package/src/__tests__/publish.test.ts +1733 -344
  30. package/src/__tests__/r2.test.ts +198 -0
  31. package/src/__tests__/route.test.ts +45 -0
  32. package/src/__tests__/secret.test.ts +301 -86
  33. package/src/__tests__/sentry.test.ts +146 -0
  34. package/src/__tests__/tail.test.ts +564 -0
  35. package/src/__tests__/whoami.test.tsx +21 -7
  36. package/src/bundle.ts +142 -0
  37. package/src/cfetch/index.ts +8 -6
  38. package/src/cfetch/internal.ts +60 -22
  39. package/src/cli.ts +10 -0
  40. package/src/config/README.md +107 -0
  41. package/src/config/config.ts +279 -0
  42. package/src/config/diagnostics.ts +80 -0
  43. package/src/config/environment.ts +305 -0
  44. package/src/config/index.ts +52 -0
  45. package/src/config/validation-helpers.ts +468 -0
  46. package/src/config/validation.ts +1057 -0
  47. package/src/{api/preview.ts → create-worker-preview.ts} +59 -21
  48. package/src/create-worker-upload-form.ts +217 -0
  49. package/src/dev/dev.tsx +362 -0
  50. package/src/dev/local.tsx +247 -0
  51. package/src/dev/remote.tsx +203 -0
  52. package/src/dev/use-esbuild.ts +100 -0
  53. package/src/entry.ts +159 -0
  54. package/src/environment-variables.ts +33 -0
  55. package/src/https-options.ts +122 -0
  56. package/src/index.tsx +1030 -766
  57. package/src/inspect.ts +25 -4
  58. package/src/{kv.tsx → kv.ts} +110 -55
  59. package/src/module-collection.ts +212 -22
  60. package/src/open-in-browser.ts +25 -0
  61. package/src/package-manager.ts +21 -15
  62. package/src/pages.tsx +10 -11
  63. package/src/parse.ts +183 -0
  64. package/src/proxy.ts +115 -45
  65. package/src/publish.ts +273 -329
  66. package/src/r2.ts +50 -0
  67. package/src/reporting.ts +131 -0
  68. package/src/selfsigned.d.ts +29 -0
  69. package/src/sites.tsx +66 -33
  70. package/src/tail/filters.ts +279 -0
  71. package/src/tail/index.ts +299 -0
  72. package/src/tail/printing.ts +62 -0
  73. package/src/user.tsx +223 -124
  74. package/src/{api/worker.ts → worker.ts} +44 -36
  75. package/templates/new-worker.js +1 -1
  76. package/templates/new-worker.ts +1 -1
  77. package/templates/tsconfig.json +105 -0
  78. package/wrangler-dist/cli.js +129900 -113128
  79. package/wrangler-dist/cli.js.map +3 -3
  80. package/src/__tests__/helpers/mock-user.ts +0 -27
  81. package/src/api/form_data.ts +0 -134
  82. package/src/config.ts +0 -641
  83. package/src/dev.tsx +0 -834
  84. package/src/tail.tsx +0 -73
@@ -0,0 +1,33 @@
1
+ /**
2
+ * Create a function used to access an environment variable.
3
+ *
4
+ * This is not memoized to allow us to change the value at runtime, such as in testing.
5
+ * A warning is shown if the client is using a deprecated version - but only once.
6
+ */
7
+ export function getEnvironmentVariableFactory({
8
+ variableName,
9
+ deprecatedName,
10
+ defaultValue,
11
+ }: {
12
+ variableName: string;
13
+ deprecatedName?: string;
14
+ defaultValue?: string;
15
+ }) {
16
+ let hasWarned = false;
17
+ return () => {
18
+ if (process.env[variableName]) {
19
+ return process.env[variableName];
20
+ } else if (deprecatedName && process.env[deprecatedName]) {
21
+ if (!hasWarned) {
22
+ // Only show the warning once.
23
+ hasWarned = true;
24
+ console.warn(
25
+ `Using "${deprecatedName}" environment variable. This is deprecated. Please use "${variableName}", instead.`
26
+ );
27
+ }
28
+ return process.env[deprecatedName];
29
+ } else {
30
+ return defaultValue;
31
+ }
32
+ };
33
+ }
@@ -0,0 +1,122 @@
1
+ import * as fs from "node:fs";
2
+ import { homedir, networkInterfaces } from "node:os";
3
+ import * as path from "node:path";
4
+ import { promisify } from "node:util";
5
+ import type { Attributes, Options } from "selfsigned";
6
+
7
+ // Most of this file has been borrowed from the implementation in Miniflare.
8
+ // See https://github.com/cloudflare/miniflare/blob/870b401ef5/packages/http-server/src/plugin.ts#L313-L397
9
+ // Thanks @mrbbot.
10
+ const CERT_EXPIRY_DAYS = 30;
11
+ const ONE_DAY_IN_MS = 86400000;
12
+
13
+ /**
14
+ * Get the options (i.e. SSL certificates) for running an HTTPS server.
15
+ *
16
+ * The certificates are self-signed and generated locally, and cached in the `CERT_ROOT` directory.
17
+ */
18
+ export async function getHttpsOptions() {
19
+ const certDirectory = path.join(homedir(), ".wrangler/local-cert");
20
+ const keyPath = path.join(certDirectory, "key.pem");
21
+ const certPath = path.join(certDirectory, "cert.pem");
22
+
23
+ const regenerate =
24
+ !fs.existsSync(keyPath) ||
25
+ !fs.existsSync(certPath) ||
26
+ hasCertificateExpired(keyPath, certPath);
27
+
28
+ if (regenerate) {
29
+ console.log("Generating new self-signed certificate...");
30
+ const { key, cert } = await generateCertificate();
31
+ try {
32
+ // Write certificate files so we can reuse them later.
33
+ fs.mkdirSync(certDirectory, { recursive: true });
34
+ fs.writeFileSync(keyPath, key, "utf8");
35
+ fs.writeFileSync(certPath, cert, "utf8");
36
+ } catch (e) {
37
+ const message = e instanceof Error ? e.message : `${e}`;
38
+ console.warn(
39
+ `Unable to cache generated self-signed certificate in ${certDirectory}.\n${message}`
40
+ );
41
+ }
42
+ return { key, cert };
43
+ } else {
44
+ return {
45
+ key: fs.readFileSync(keyPath, "utf8"),
46
+ cert: fs.readFileSync(certPath, "utf8"),
47
+ };
48
+ }
49
+ }
50
+
51
+ /**
52
+ * Determine if the certificate defined in `keyPath` and `certPath` files
53
+ * have expired based on the date when they were last modified.
54
+ */
55
+ function hasCertificateExpired(keyPath: string, certPath: string): boolean {
56
+ const keyStat = fs.statSync(keyPath);
57
+ const certStat = fs.statSync(certPath);
58
+ const created = Math.max(keyStat.mtimeMs, certStat.mtimeMs);
59
+ return Date.now() - created > (CERT_EXPIRY_DAYS - 2) * ONE_DAY_IN_MS;
60
+ }
61
+
62
+ /**
63
+ * Generate a new self-signed certificate and cache it in `CERT_ROOT` directory.
64
+ */
65
+ async function generateCertificate() {
66
+ // `selfsigned` imports `node-forge`, which is a pretty big library.
67
+ // To reduce startup time, only load this dynamically when needed.
68
+ // eslint-disable-next-line @typescript-eslint/consistent-type-imports
69
+ const generate: typeof import("selfsigned").generate = promisify(
70
+ // eslint-disable-next-line @typescript-eslint/no-var-requires
71
+ require("selfsigned").generate
72
+ );
73
+
74
+ const certAttrs: Attributes = [{ name: "commonName", value: "localhost" }];
75
+
76
+ const certOptions: Options = {
77
+ algorithm: "sha256",
78
+ days: CERT_EXPIRY_DAYS,
79
+ keySize: 2048,
80
+ extensions: [
81
+ { name: "basicConstraints", cA: true },
82
+ {
83
+ name: "keyUsage",
84
+ keyCertSign: true,
85
+ digitalSignature: true,
86
+ nonRepudiation: true,
87
+ keyEncipherment: true,
88
+ dataEncipherment: true,
89
+ },
90
+ {
91
+ name: "extKeyUsage",
92
+ serverAuth: true,
93
+ clientAuth: true,
94
+ codeSigning: true,
95
+ timeStamping: true,
96
+ },
97
+ {
98
+ name: "subjectAltName",
99
+ altNames: [
100
+ { type: 2, value: "localhost" },
101
+ ...getAccessibleHosts().map((ip) => ({ type: 7, ip })),
102
+ ],
103
+ },
104
+ ],
105
+ };
106
+
107
+ const { private: key, cert } = await generate(certAttrs, certOptions);
108
+ return { key, cert };
109
+ }
110
+
111
+ /**
112
+ * Ask the OS for the addresses of locally accessible hosts.
113
+ */
114
+ function getAccessibleHosts(ipv4 = false): string[] {
115
+ const hosts: string[] = [];
116
+ Object.values(networkInterfaces()).forEach((net) =>
117
+ net?.forEach(({ family, address }) => {
118
+ if (!ipv4 || family === "IPv4") hosts.push(address);
119
+ })
120
+ );
121
+ return hosts;
122
+ }