wrangler 0.0.0-e928f94 → 0.0.0-e978986

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 (124) hide show
  1. package/Cloudflare_CA.pem +18 -0
  2. package/README.md +27 -14
  3. package/bin/wrangler.js +48 -4
  4. package/miniflare-dist/index.mjs +4886 -0
  5. package/package.json +74 -34
  6. package/pages/functions/buildPlugin.ts +105 -0
  7. package/pages/functions/buildWorker.ts +151 -0
  8. package/pages/functions/filepath-routing.test.ts +234 -0
  9. package/pages/functions/filepath-routing.ts +189 -0
  10. package/pages/functions/identifiers.ts +78 -0
  11. package/pages/functions/routes.ts +156 -0
  12. package/pages/functions/template-plugin.ts +147 -0
  13. package/pages/functions/template-worker.ts +143 -0
  14. package/src/__tests__/config-cache.test.ts +36 -0
  15. package/src/__tests__/configuration.test.ts +3374 -0
  16. package/src/__tests__/dev.test.tsx +980 -0
  17. package/src/__tests__/guess-worker-format.test.ts +85 -0
  18. package/src/__tests__/{clipboardy-mock.js → helpers/clipboardy-mock.js} +0 -0
  19. package/src/__tests__/helpers/cmd-shim.d.ts +11 -0
  20. package/src/__tests__/helpers/faye-websocket.d.ts +6 -0
  21. package/src/__tests__/helpers/mock-account-id.ts +48 -0
  22. package/src/__tests__/helpers/mock-bin.ts +36 -0
  23. package/src/__tests__/helpers/mock-cfetch.ts +211 -0
  24. package/src/__tests__/helpers/mock-console.ts +87 -0
  25. package/src/__tests__/helpers/mock-dialogs.ts +130 -0
  26. package/src/__tests__/helpers/mock-http-server.ts +46 -0
  27. package/src/__tests__/helpers/mock-istty.ts +27 -0
  28. package/src/__tests__/helpers/mock-kv.ts +33 -0
  29. package/src/__tests__/helpers/mock-oauth-flow.ts +255 -0
  30. package/src/__tests__/helpers/mock-stdin.ts +103 -0
  31. package/src/__tests__/helpers/mock-web-socket.ts +29 -0
  32. package/src/__tests__/helpers/run-in-tmp.ts +39 -0
  33. package/src/__tests__/helpers/run-wrangler.ts +16 -0
  34. package/src/__tests__/helpers/write-worker-source.ts +31 -0
  35. package/src/__tests__/helpers/write-wrangler-toml.ts +17 -0
  36. package/src/__tests__/https-options.test.ts +130 -0
  37. package/src/__tests__/index.test.ts +233 -130
  38. package/src/__tests__/init.test.ts +1567 -0
  39. package/src/__tests__/jest.setup.ts +77 -0
  40. package/src/__tests__/kv.test.ts +1481 -0
  41. package/src/__tests__/logger.test.ts +115 -0
  42. package/src/__tests__/package-manager.test.ts +353 -0
  43. package/src/__tests__/pages.test.ts +657 -0
  44. package/src/__tests__/parse.test.ts +330 -0
  45. package/src/__tests__/publish.test.ts +5468 -0
  46. package/src/__tests__/r2.test.ts +233 -0
  47. package/src/__tests__/route.test.ts +45 -0
  48. package/src/__tests__/secret.test.ts +481 -0
  49. package/src/__tests__/tail.test.ts +690 -0
  50. package/src/__tests__/user.test.ts +131 -0
  51. package/src/__tests__/whoami.test.tsx +105 -0
  52. package/src/abort.d.ts +3 -0
  53. package/src/bundle.ts +200 -0
  54. package/src/cfetch/index.ts +121 -0
  55. package/src/cfetch/internal.ts +145 -0
  56. package/src/cli.ts +10 -7
  57. package/src/config/README.md +107 -0
  58. package/src/config/config.ts +234 -0
  59. package/src/config/diagnostics.ts +80 -0
  60. package/src/config/environment.ts +420 -0
  61. package/src/config/index.ts +225 -0
  62. package/src/config/validation-helpers.ts +575 -0
  63. package/src/config/validation.ts +1539 -0
  64. package/src/config-cache.ts +45 -0
  65. package/src/create-worker-preview.ts +185 -0
  66. package/src/create-worker-upload-form.ts +254 -0
  67. package/src/dev/dev-vars.ts +36 -0
  68. package/src/dev/dev.tsx +419 -0
  69. package/src/dev/local.tsx +318 -0
  70. package/src/dev/remote.tsx +227 -0
  71. package/src/dev/use-esbuild.ts +118 -0
  72. package/src/dialogs.tsx +67 -17
  73. package/src/durable.ts +102 -0
  74. package/src/entry.ts +322 -0
  75. package/src/environment-variables.ts +35 -0
  76. package/src/errors.ts +11 -0
  77. package/src/https-options.ts +123 -0
  78. package/src/index.tsx +2031 -1041
  79. package/src/inspect.ts +662 -0
  80. package/src/intl-polyfill.d.ts +139 -0
  81. package/src/kv.ts +408 -0
  82. package/src/logger.ts +66 -0
  83. package/src/miniflare-cli/README.md +30 -0
  84. package/src/miniflare-cli/enum-keys.ts +17 -0
  85. package/src/miniflare-cli/index.ts +41 -0
  86. package/src/miniflare-cli/request-context.ts +40 -0
  87. package/src/module-collection.ts +229 -35
  88. package/src/open-in-browser.ts +19 -0
  89. package/src/package-manager.ts +174 -0
  90. package/src/pages.tsx +1444 -260
  91. package/src/parse.ts +202 -0
  92. package/src/paths.ts +26 -0
  93. package/src/proxy.ts +465 -0
  94. package/src/publish.ts +498 -261
  95. package/src/r2.ts +50 -0
  96. package/src/selfsigned.d.ts +29 -0
  97. package/src/sites.tsx +240 -60
  98. package/src/tail/filters.ts +279 -0
  99. package/src/tail/index.ts +299 -0
  100. package/src/tail/printing.ts +67 -0
  101. package/src/update-check.ts +19 -0
  102. package/src/user.tsx +395 -221
  103. package/src/whoami.tsx +70 -0
  104. package/src/worker.ts +180 -0
  105. package/templates/gitignore +171 -0
  106. package/templates/new-worker-scheduled.js +17 -0
  107. package/templates/new-worker-scheduled.ts +32 -0
  108. package/templates/new-worker.js +15 -0
  109. package/templates/new-worker.ts +30 -0
  110. package/{static-asset-facade.js → templates/static-asset-facade.js} +1 -1
  111. package/templates/tsconfig.json +105 -0
  112. package/wrangler-dist/cli.js +118298 -116056
  113. package/src/__tests__/fixtures/init/.gitkeep +0 -0
  114. package/src/__tests__/mock-cfetch.js +0 -46
  115. package/src/api/form_data.ts +0 -141
  116. package/src/api/inspect.ts +0 -441
  117. package/src/api/preview.ts +0 -128
  118. package/src/api/worker.ts +0 -161
  119. package/src/cfetch.ts +0 -72
  120. package/src/config.ts +0 -122
  121. package/src/dev.tsx +0 -724
  122. package/src/kv.tsx +0 -211
  123. package/src/tail.tsx +0 -71
  124. package/wrangler-dist/cli.js.map +0 -7
@@ -0,0 +1,123 @@
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 { logger } from "./logger";
6
+ import type { Attributes, Options } from "selfsigned";
7
+
8
+ // Most of this file has been borrowed from the implementation in Miniflare.
9
+ // See https://github.com/cloudflare/miniflare/blob/870b401ef5/packages/http-server/src/plugin.ts#L313-L397
10
+ // Thanks @mrbbot.
11
+ const CERT_EXPIRY_DAYS = 30;
12
+ const ONE_DAY_IN_MS = 86400000;
13
+
14
+ /**
15
+ * Get the options (i.e. SSL certificates) for running an HTTPS server.
16
+ *
17
+ * The certificates are self-signed and generated locally, and cached in the `CERT_ROOT` directory.
18
+ */
19
+ export async function getHttpsOptions() {
20
+ const certDirectory = path.join(homedir(), ".wrangler/local-cert");
21
+ const keyPath = path.join(certDirectory, "key.pem");
22
+ const certPath = path.join(certDirectory, "cert.pem");
23
+
24
+ const regenerate =
25
+ !fs.existsSync(keyPath) ||
26
+ !fs.existsSync(certPath) ||
27
+ hasCertificateExpired(keyPath, certPath);
28
+
29
+ if (regenerate) {
30
+ logger.log("Generating new self-signed certificate...");
31
+ const { key, cert } = await generateCertificate();
32
+ try {
33
+ // Write certificate files so we can reuse them later.
34
+ fs.mkdirSync(certDirectory, { recursive: true });
35
+ fs.writeFileSync(keyPath, key, "utf8");
36
+ fs.writeFileSync(certPath, cert, "utf8");
37
+ } catch (e) {
38
+ const message = e instanceof Error ? e.message : `${e}`;
39
+ logger.warn(
40
+ `Unable to cache generated self-signed certificate in ${certDirectory}.\n${message}`
41
+ );
42
+ }
43
+ return { key, cert };
44
+ } else {
45
+ return {
46
+ key: fs.readFileSync(keyPath, "utf8"),
47
+ cert: fs.readFileSync(certPath, "utf8"),
48
+ };
49
+ }
50
+ }
51
+
52
+ /**
53
+ * Determine if the certificate defined in `keyPath` and `certPath` files
54
+ * have expired based on the date when they were last modified.
55
+ */
56
+ function hasCertificateExpired(keyPath: string, certPath: string): boolean {
57
+ const keyStat = fs.statSync(keyPath);
58
+ const certStat = fs.statSync(certPath);
59
+ const created = Math.max(keyStat.mtimeMs, certStat.mtimeMs);
60
+ return Date.now() - created > (CERT_EXPIRY_DAYS - 2) * ONE_DAY_IN_MS;
61
+ }
62
+
63
+ /**
64
+ * Generate a new self-signed certificate and cache it in `CERT_ROOT` directory.
65
+ */
66
+ async function generateCertificate() {
67
+ // `selfsigned` imports `node-forge`, which is a pretty big library.
68
+ // To reduce startup time, only load this dynamically when needed.
69
+ // eslint-disable-next-line @typescript-eslint/consistent-type-imports
70
+ const generate: typeof import("selfsigned").generate = promisify(
71
+ // eslint-disable-next-line @typescript-eslint/no-var-requires
72
+ require("selfsigned").generate
73
+ );
74
+
75
+ const certAttrs: Attributes = [{ name: "commonName", value: "localhost" }];
76
+
77
+ const certOptions: Options = {
78
+ algorithm: "sha256",
79
+ days: CERT_EXPIRY_DAYS,
80
+ keySize: 2048,
81
+ extensions: [
82
+ { name: "basicConstraints", cA: true },
83
+ {
84
+ name: "keyUsage",
85
+ keyCertSign: true,
86
+ digitalSignature: true,
87
+ nonRepudiation: true,
88
+ keyEncipherment: true,
89
+ dataEncipherment: true,
90
+ },
91
+ {
92
+ name: "extKeyUsage",
93
+ serverAuth: true,
94
+ clientAuth: true,
95
+ codeSigning: true,
96
+ timeStamping: true,
97
+ },
98
+ {
99
+ name: "subjectAltName",
100
+ altNames: [
101
+ { type: 2, value: "localhost" },
102
+ ...getAccessibleHosts().map((ip) => ({ type: 7, ip })),
103
+ ],
104
+ },
105
+ ],
106
+ };
107
+
108
+ const { private: key, cert } = await generate(certAttrs, certOptions);
109
+ return { key, cert };
110
+ }
111
+
112
+ /**
113
+ * Ask the OS for the addresses of locally accessible hosts.
114
+ */
115
+ function getAccessibleHosts(ipv4 = false): string[] {
116
+ const hosts: string[] = [];
117
+ Object.values(networkInterfaces()).forEach((net) =>
118
+ net?.forEach(({ family, address }) => {
119
+ if (!ipv4 || family === "IPv4") hosts.push(address);
120
+ })
121
+ );
122
+ return hosts;
123
+ }