wrangler 0.0.0-e3a458e → 0.0.0-e3c036d7
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.
- package/Cloudflare_CA.pem +18 -0
- package/README.md +47 -14
- package/bin/wrangler.js +181 -29
- package/kv-asset-handler.js +1 -0
- package/miniflare-dist/index.mjs +6368 -0
- package/package.json +193 -142
- package/src/__tests__/access.test.ts +25 -0
- package/src/__tests__/api-dev.test.ts +238 -0
- package/src/__tests__/api-devregistry.test.js +64 -0
- package/src/__tests__/api.test.ts +80 -0
- package/src/__tests__/config-cache-without-cache-dir.test.ts +38 -0
- package/src/__tests__/config-cache.test.ts +42 -0
- package/src/__tests__/configuration.test.ts +4454 -2590
- package/src/__tests__/d1/d1.test.ts +82 -0
- package/src/__tests__/d1/execute.test.ts +64 -0
- package/src/__tests__/d1/migrate.test.ts +155 -0
- package/src/__tests__/d1/splitter.test.ts +255 -0
- package/src/__tests__/delete.test.ts +266 -0
- package/src/__tests__/deployments.test.ts +129 -0
- package/src/__tests__/dev.test.tsx +1589 -432
- package/src/__tests__/generate.test.ts +238 -0
- package/src/__tests__/get-host-from-url.test.ts +16 -0
- package/src/__tests__/guess-worker-format.test.ts +113 -51
- package/src/__tests__/helpers/cmd-shim.d.ts +6 -6
- package/src/__tests__/helpers/end-event-loop.ts +6 -0
- package/src/__tests__/helpers/mock-account-id.ts +24 -24
- package/src/__tests__/helpers/mock-auth-domain.ts +20 -0
- package/src/__tests__/helpers/mock-bin.ts +20 -20
- package/src/__tests__/helpers/mock-console.ts +68 -32
- package/src/__tests__/helpers/mock-dialogs.ts +107 -33
- package/src/__tests__/helpers/mock-get-pages-upload-token.ts +25 -0
- package/src/__tests__/helpers/mock-get-zone-from-host.ts +11 -0
- package/src/__tests__/helpers/mock-http-server.ts +46 -0
- package/src/__tests__/helpers/mock-istty.ts +74 -0
- package/src/__tests__/helpers/mock-known-routes.ts +12 -0
- package/src/__tests__/helpers/mock-kv.ts +41 -28
- package/src/__tests__/helpers/mock-oauth-flow.ts +263 -0
- package/src/__tests__/helpers/mock-process.ts +34 -0
- package/src/__tests__/helpers/mock-set-timeout.ts +16 -0
- package/src/__tests__/helpers/mock-stdin.ts +82 -77
- package/src/__tests__/helpers/mock-web-socket.ts +21 -21
- package/src/__tests__/helpers/msw/blob-worker.cjs +19 -0
- package/src/__tests__/helpers/msw/handlers/access.ts +13 -0
- package/src/__tests__/helpers/msw/handlers/deployments.ts +93 -0
- package/src/__tests__/helpers/msw/handlers/namespaces.ts +81 -0
- package/src/__tests__/helpers/msw/handlers/oauth.ts +31 -0
- package/src/__tests__/helpers/msw/handlers/r2.ts +60 -0
- package/src/__tests__/helpers/msw/handlers/script.ts +56 -0
- package/src/__tests__/helpers/msw/handlers/user.ts +52 -0
- package/src/__tests__/helpers/msw/handlers/zones.ts +20 -0
- package/src/__tests__/helpers/msw/index.ts +50 -0
- package/src/__tests__/helpers/msw/read-file-sync.js +61 -0
- package/src/__tests__/helpers/run-in-tmp.ts +28 -22
- package/src/__tests__/helpers/run-wrangler.ts +8 -8
- package/src/__tests__/helpers/string-dynamic-values-matcher.ts +28 -0
- package/src/__tests__/helpers/worker-scripts/child-wrangler.toml +1 -0
- package/src/__tests__/helpers/worker-scripts/hello-world-worker.js +5 -0
- package/src/__tests__/helpers/worker-scripts/hello-world-wrangler.toml +1 -0
- package/src/__tests__/helpers/worker-scripts/parent-worker.js +8 -0
- package/src/__tests__/helpers/worker-scripts/parent-wrangler.toml +5 -0
- package/src/__tests__/helpers/write-worker-source.ts +31 -0
- package/src/__tests__/helpers/write-wrangler-toml.ts +9 -9
- package/src/__tests__/https-options.test.ts +144 -108
- package/src/__tests__/index.test.ts +258 -975
- package/src/__tests__/init.test.ts +3165 -0
- package/src/__tests__/jest.setup.ts +154 -48
- package/src/__tests__/kv.test.ts +1789 -1372
- package/src/__tests__/logger.test.ts +141 -0
- package/src/__tests__/logout.test.ts +36 -41
- package/src/__tests__/metrics.test.ts +493 -0
- package/src/__tests__/middleware.scheduled.test.ts +145 -0
- package/src/__tests__/middleware.test.ts +816 -0
- package/src/__tests__/mtls-certificates.test.ts +585 -0
- package/src/__tests__/package-manager.test.ts +312 -165
- package/src/__tests__/pages/deployment-list.test.ts +78 -0
- package/src/__tests__/pages/functions-build.test.ts +453 -0
- package/src/__tests__/pages/pages.test.ts +81 -0
- package/src/__tests__/pages/project-create.test.ts +63 -0
- package/src/__tests__/pages/project-list.test.ts +108 -0
- package/src/__tests__/pages/project-upload.test.ts +481 -0
- package/src/__tests__/pages/publish.test.ts +2745 -0
- package/src/__tests__/pages-deployment-tail.test.ts +955 -0
- package/src/__tests__/parse.test.ts +359 -245
- package/src/__tests__/paths.test.ts +39 -0
- package/src/__tests__/publish.test.ts +8209 -2558
- package/src/__tests__/pubsub.test.ts +493 -0
- package/src/__tests__/queues.test.ts +529 -0
- package/src/__tests__/r2.test.ts +369 -193
- package/src/__tests__/route.test.ts +22 -22
- package/src/__tests__/secret.test.ts +683 -415
- package/src/__tests__/tail.test.ts +838 -444
- package/src/__tests__/test-old-node-version.js +31 -0
- package/src/__tests__/tsconfig-sanity.ts +12 -0
- package/src/__tests__/tsconfig.json +8 -0
- package/src/__tests__/tsconfig.tsbuildinfo +1 -0
- package/src/__tests__/type-generation.test.ts +234 -0
- package/src/__tests__/user.test.ts +118 -0
- package/src/__tests__/utils-collectKeyValues.test.ts +47 -0
- package/src/__tests__/validate-dev-props.test.ts +56 -0
- package/src/__tests__/version.test.ts +35 -0
- package/src/__tests__/whoami.test.tsx +166 -94
- package/src/__tests__/worker-namespace.test.ts +339 -0
- package/src/abort.d.ts +3 -0
- package/src/api/dev.ts +327 -0
- package/src/api/index.ts +11 -0
- package/src/api/mtls-certificate.ts +148 -0
- package/src/api/pages/create-worker-bundle-contents.ts +75 -0
- package/src/api/pages/index.ts +5 -0
- package/src/api/pages/publish.tsx +375 -0
- package/src/bundle-reporter.ts +68 -0
- package/src/bundle.ts +858 -110
- package/src/cfetch/index.ts +123 -69
- package/src/cfetch/internal.ts +217 -98
- package/src/cli.ts +23 -14
- package/src/config/README.md +7 -7
- package/src/config/config.ts +232 -229
- package/src/config/diagnostics.ts +61 -61
- package/src/config/environment.ts +542 -256
- package/src/config/index.ts +367 -34
- package/src/config/validation-helpers.ts +415 -286
- package/src/config/validation.ts +2137 -942
- package/src/config-cache.ts +85 -0
- package/src/create-worker-preview.ts +251 -124
- package/src/create-worker-upload-form.ts +326 -201
- package/src/d1/backups.tsx +215 -0
- package/src/d1/constants.ts +2 -0
- package/src/d1/create.tsx +64 -0
- package/src/d1/delete.ts +53 -0
- package/src/d1/execute.tsx +354 -0
- package/src/d1/formatTimeAgo.ts +14 -0
- package/src/d1/index.ts +100 -0
- package/src/d1/list.tsx +60 -0
- package/src/d1/migrations/apply.tsx +206 -0
- package/src/d1/migrations/create.tsx +76 -0
- package/src/d1/migrations/helpers.ts +172 -0
- package/src/d1/migrations/index.ts +3 -0
- package/src/d1/migrations/list.tsx +92 -0
- package/src/d1/migrations/options.ts +23 -0
- package/src/d1/options.ts +22 -0
- package/src/d1/splitter.ts +161 -0
- package/src/d1/types.ts +25 -0
- package/src/d1/utils.ts +49 -0
- package/src/delete.ts +100 -0
- package/src/deployments.ts +116 -0
- package/src/deprecated/index.ts +144 -0
- package/src/dev/dev-vars.ts +39 -0
- package/src/dev/dev.tsx +544 -306
- package/src/dev/get-local-persistence-path.ts +31 -0
- package/src/dev/local.tsx +906 -233
- package/src/dev/remote.tsx +621 -192
- package/src/dev/start-server.ts +540 -0
- package/src/dev/use-esbuild.ts +189 -80
- package/src/dev/validate-dev-props.ts +40 -0
- package/src/dev-registry.ts +201 -0
- package/src/dev.tsx +928 -0
- package/src/dialogs.ts +136 -0
- package/src/dispatch-namespace.ts +211 -0
- package/src/docs/index.ts +97 -0
- package/src/durable.ts +102 -0
- package/src/entry.ts +277 -109
- package/src/environment-variables/factory.ts +88 -0
- package/src/environment-variables/misc-variables.ts +30 -0
- package/src/errors.ts +11 -0
- package/src/generate/index.ts +298 -0
- package/src/git-client.ts +135 -0
- package/src/global-wrangler-config-path.ts +26 -0
- package/src/https-options.ts +85 -80
- package/src/index.ts +705 -0
- package/src/init.ts +1023 -0
- package/src/inspect.ts +722 -557
- package/src/intl-polyfill.d.ts +139 -0
- package/src/is-ci.ts +14 -0
- package/src/is-interactive.ts +16 -0
- package/src/jest.d.ts +4 -0
- package/src/kv/helpers.ts +430 -0
- package/src/kv/index.ts +594 -0
- package/src/logger.ts +104 -0
- package/src/metrics/index.ts +5 -0
- package/src/metrics/metrics-config.ts +239 -0
- package/src/metrics/metrics-dispatcher.ts +96 -0
- package/src/metrics/metrics-usage-headers.ts +24 -0
- package/src/metrics/send-event.ts +98 -0
- package/src/miniflare-cli/README.md +30 -0
- package/src/miniflare-cli/assets.ts +251 -0
- package/src/miniflare-cli/index.ts +210 -0
- package/src/miniflare-cli/request-context.ts +40 -0
- package/src/miniflare-cli/tsconfig.json +9 -0
- package/src/miniflare-cli/tsconfig.tsbuildinfo +1 -0
- package/src/miniflare-cli/types.ts +11 -0
- package/src/module-collection.ts +241 -227
- package/src/mtls-certificate/cli.ts +155 -0
- package/src/open-in-browser.ts +8 -16
- package/src/package-manager.ts +183 -90
- package/src/pages/build.ts +252 -0
- package/src/pages/buildFunctions.ts +133 -0
- package/src/pages/constants.ts +18 -0
- package/src/pages/deployment-tails.ts +281 -0
- package/src/pages/deployments.tsx +86 -0
- package/src/pages/dev.ts +726 -0
- package/src/pages/errors.ts +66 -0
- package/src/pages/functions/buildPlugin.ts +97 -0
- package/src/pages/functions/buildWorker.ts +294 -0
- package/src/pages/functions/filepath-routing.test.ts +234 -0
- package/src/pages/functions/filepath-routing.ts +189 -0
- package/src/pages/functions/identifiers.ts +78 -0
- package/src/pages/functions/routes-consolidation.test.ts +250 -0
- package/src/pages/functions/routes-consolidation.ts +73 -0
- package/src/pages/functions/routes-transformation.test.ts +282 -0
- package/src/pages/functions/routes-transformation.ts +115 -0
- package/src/pages/functions/routes-validation.test.ts +403 -0
- package/src/pages/functions/routes-validation.ts +202 -0
- package/src/pages/functions/routes.ts +151 -0
- package/src/pages/functions/tsconfig.json +8 -0
- package/src/pages/functions/tsconfig.tsbuildinfo +1 -0
- package/src/pages/functions.ts +86 -0
- package/src/pages/hash.ts +13 -0
- package/src/pages/index.ts +102 -0
- package/src/pages/projects.tsx +162 -0
- package/src/pages/prompt-select-project.tsx +31 -0
- package/src/pages/publish.tsx +276 -0
- package/src/pages/types.ts +46 -0
- package/src/pages/upload.tsx +449 -0
- package/src/pages/utils.ts +23 -0
- package/src/parse.ts +247 -122
- package/src/paths.ts +53 -8
- package/src/proxy.ts +629 -375
- package/src/publish/index.ts +274 -0
- package/src/publish/publish.ts +1076 -0
- package/src/pubsub/index.ts +286 -0
- package/src/pubsub/pubsub-commands.ts +623 -0
- package/src/queues/cli/commands/consumer/add.ts +65 -0
- package/src/queues/cli/commands/consumer/index.ts +19 -0
- package/src/queues/cli/commands/consumer/remove.ts +31 -0
- package/src/queues/cli/commands/create.ts +25 -0
- package/src/queues/cli/commands/delete.ts +26 -0
- package/src/queues/cli/commands/index.ts +35 -0
- package/src/queues/cli/commands/list.ts +25 -0
- package/src/queues/client.ts +135 -0
- package/src/queues/utils.ts +18 -0
- package/src/r2/constants.ts +4 -0
- package/src/r2/helpers.ts +132 -0
- package/src/r2/index.ts +289 -0
- package/src/routes.ts +140 -0
- package/src/secret/index.ts +375 -0
- package/src/selfsigned.d.ts +23 -23
- package/src/sites.ts +372 -0
- package/src/tail/createTail.ts +406 -0
- package/src/tail/filters.ts +109 -111
- package/src/tail/index.ts +208 -296
- package/src/tail/printing.ts +107 -47
- package/src/tsconfig-sanity.ts +16 -0
- package/src/type-generation.ts +181 -0
- package/src/update-check.ts +19 -0
- package/src/user/access.ts +68 -0
- package/src/user/auth-variables.ts +113 -0
- package/src/user/choose-account.tsx +39 -0
- package/src/user/generate-auth-url.ts +33 -0
- package/src/user/generate-random-state.ts +16 -0
- package/src/user/index.ts +2 -0
- package/src/user/user.ts +1233 -0
- package/src/utils/collectKeyValues.ts +14 -0
- package/src/whoami.ts +135 -0
- package/src/worker.ts +201 -100
- package/src/yargs-types.ts +37 -0
- package/src/zones.ts +191 -0
- package/templates/__tests__/pages-dev-util.test.ts +128 -0
- package/templates/__tests__/tsconfig-sanity.ts +12 -0
- package/templates/__tests__/tsconfig.json +8 -0
- package/templates/__tests__/tsconfig.tsbuildinfo +1 -0
- package/templates/checked-fetch.js +23 -0
- package/templates/d1-beta-facade.js +241 -0
- package/templates/facade.d.ts +14 -0
- package/templates/first-party-worker-module-facade.ts +19 -0
- package/templates/format-dev-errors.ts +34 -0
- package/templates/gitignore +172 -0
- package/templates/init-tests/test-jest-new-worker.js +23 -0
- package/templates/init-tests/test-vitest-new-worker.js +24 -0
- package/templates/init-tests/test-vitest-new-worker.ts +25 -0
- package/templates/middleware/common.ts +62 -0
- package/templates/middleware/loader-modules.ts +84 -0
- package/templates/middleware/loader-sw.ts +233 -0
- package/templates/middleware/middleware-miniflare3-json-error.ts +32 -0
- package/templates/middleware/middleware-pretty-error.ts +40 -0
- package/templates/middleware/middleware-scheduled.ts +15 -0
- package/templates/new-worker-scheduled.js +17 -0
- package/templates/new-worker-scheduled.ts +32 -0
- package/templates/new-worker.js +3 -3
- package/templates/new-worker.ts +21 -3
- package/templates/no-op-worker.js +10 -0
- package/templates/pages-dev-pipeline.ts +31 -0
- package/templates/pages-dev-util.ts +52 -0
- package/templates/pages-shim.ts +12 -0
- package/templates/pages-template-plugin.ts +180 -0
- package/templates/pages-template-worker.ts +189 -0
- package/templates/serve-static-assets.ts +66 -0
- package/templates/service-bindings-module-facade.js +55 -0
- package/templates/service-bindings-sw-facade.js +42 -0
- package/templates/tsconfig-sanity.ts +11 -0
- package/templates/tsconfig.init.json +106 -0
- package/templates/tsconfig.json +6 -103
- package/templates/tsconfig.tsbuildinfo +1 -0
- package/wrangler-dist/cli.d.ts +1624 -0
- package/wrangler-dist/cli.js +139300 -128984
- package/wrangler-dist/wasm-sync.wasm +0 -0
- package/pages/functions/buildWorker.ts +0 -62
- package/pages/functions/filepath-routing.test.ts +0 -223
- package/pages/functions/filepath-routing.ts +0 -181
- package/pages/functions/identifiers.ts +0 -78
- package/pages/functions/routes.ts +0 -151
- package/pages/functions/template-worker.ts +0 -130
- package/src/__tests__/dev2.test.tsx +0 -154
- package/src/__tests__/helpers/faye-websocket.d.ts +0 -6
- package/src/__tests__/helpers/mock-cfetch.ts +0 -207
- package/src/__tests__/pages.test.ts +0 -44
- package/src/__tests__/sentry.test.ts +0 -146
- package/src/dialogs.tsx +0 -77
- package/src/environment-variables.ts +0 -33
- package/src/index.tsx +0 -2365
- package/src/kv.ts +0 -339
- package/src/pages.tsx +0 -1045
- package/src/publish.ts +0 -354
- package/src/r2.ts +0 -50
- package/src/reporting.ts +0 -131
- package/src/sites.tsx +0 -273
- package/src/user.tsx +0 -1118
- package/src/whoami.tsx +0 -69
- package/templates/static-asset-facade.js +0 -47
- package/vendor/@cloudflare/kv-asset-handler/CHANGELOG.md +0 -332
- package/vendor/@cloudflare/kv-asset-handler/LICENSE_APACHE +0 -176
- package/vendor/@cloudflare/kv-asset-handler/LICENSE_MIT +0 -25
- package/vendor/@cloudflare/kv-asset-handler/README.md +0 -245
- package/vendor/@cloudflare/kv-asset-handler/dist/index.d.ts +0 -32
- package/vendor/@cloudflare/kv-asset-handler/dist/index.js +0 -354
- package/vendor/@cloudflare/kv-asset-handler/dist/mocks.d.ts +0 -13
- package/vendor/@cloudflare/kv-asset-handler/dist/mocks.js +0 -148
- package/vendor/@cloudflare/kv-asset-handler/dist/test/getAssetFromKV.d.ts +0 -1
- package/vendor/@cloudflare/kv-asset-handler/dist/test/getAssetFromKV.js +0 -436
- package/vendor/@cloudflare/kv-asset-handler/dist/test/mapRequestToAsset.d.ts +0 -1
- package/vendor/@cloudflare/kv-asset-handler/dist/test/mapRequestToAsset.js +0 -40
- package/vendor/@cloudflare/kv-asset-handler/dist/test/serveSinglePageApp.d.ts +0 -1
- package/vendor/@cloudflare/kv-asset-handler/dist/test/serveSinglePageApp.js +0 -42
- package/vendor/@cloudflare/kv-asset-handler/dist/types.d.ts +0 -26
- package/vendor/@cloudflare/kv-asset-handler/dist/types.js +0 -31
- package/vendor/@cloudflare/kv-asset-handler/package.json +0 -52
- package/vendor/@cloudflare/kv-asset-handler/src/index.ts +0 -296
- package/vendor/@cloudflare/kv-asset-handler/src/mocks.ts +0 -136
- package/vendor/@cloudflare/kv-asset-handler/src/test/getAssetFromKV.ts +0 -464
- package/vendor/@cloudflare/kv-asset-handler/src/test/mapRequestToAsset.ts +0 -33
- package/vendor/@cloudflare/kv-asset-handler/src/test/serveSinglePageApp.ts +0 -42
- package/vendor/@cloudflare/kv-asset-handler/src/types.ts +0 -39
- package/vendor/wrangler-mime/CHANGELOG.md +0 -289
- package/vendor/wrangler-mime/LICENSE +0 -21
- package/vendor/wrangler-mime/Mime.js +0 -97
- package/vendor/wrangler-mime/README.md +0 -187
- package/vendor/wrangler-mime/cli.js +0 -46
- package/vendor/wrangler-mime/index.js +0 -4
- package/vendor/wrangler-mime/lite.js +0 -4
- package/vendor/wrangler-mime/package.json +0 -52
- package/vendor/wrangler-mime/types/other.js +0 -1
- package/vendor/wrangler-mime/types/standard.js +0 -1
- package/wrangler-dist/cli.js.map +0 -7
package/src/sites.tsx
DELETED
|
@@ -1,273 +0,0 @@
|
|
|
1
|
-
import { readdir, readFile, stat } from "node:fs/promises";
|
|
2
|
-
import * as path from "node:path";
|
|
3
|
-
import ignore from "ignore";
|
|
4
|
-
import xxhash from "xxhash-wasm";
|
|
5
|
-
import {
|
|
6
|
-
createNamespace,
|
|
7
|
-
getKeyValue,
|
|
8
|
-
listNamespaceKeys,
|
|
9
|
-
listNamespaces,
|
|
10
|
-
putBulkKeyValue,
|
|
11
|
-
} from "./kv";
|
|
12
|
-
import type { Config } from "./config";
|
|
13
|
-
import type { KeyValue, NamespaceKeyInfo } from "./kv";
|
|
14
|
-
import type { XXHashAPI } from "xxhash-wasm";
|
|
15
|
-
|
|
16
|
-
/** Paths to always ignore. */
|
|
17
|
-
const ALWAYS_IGNORE = new Set(["node_modules"]);
|
|
18
|
-
const HIDDEN_FILES_TO_INCLUDE = new Set([
|
|
19
|
-
".well-known", // See https://datatracker.ietf.org/doc/html/rfc8615
|
|
20
|
-
]);
|
|
21
|
-
const FIVE_MINUTES = 60 * 5; // expressed in seconds, since that's what the api expects
|
|
22
|
-
|
|
23
|
-
async function* getFilesInFolder(dirPath: string): AsyncIterable<string> {
|
|
24
|
-
const files = await readdir(dirPath, { withFileTypes: true });
|
|
25
|
-
for (const file of files) {
|
|
26
|
-
// Skip files that we never want to process.
|
|
27
|
-
if (ALWAYS_IGNORE.has(file.name)) {
|
|
28
|
-
continue;
|
|
29
|
-
}
|
|
30
|
-
// Skip hidden files (starting with .) except for some special ones
|
|
31
|
-
if (file.name.startsWith(".") && !HIDDEN_FILES_TO_INCLUDE.has(file.name)) {
|
|
32
|
-
continue;
|
|
33
|
-
}
|
|
34
|
-
// TODO: follow symlinks??
|
|
35
|
-
if (file.isDirectory()) {
|
|
36
|
-
yield* await getFilesInFolder(path.join(dirPath, file.name));
|
|
37
|
-
} else {
|
|
38
|
-
yield path.join(dirPath, file.name);
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* Create a hash key for the given content using the xxhash algorithm.
|
|
45
|
-
*
|
|
46
|
-
* Note we only return the first 10 characters, since we will also include the file name in the asset manifest key
|
|
47
|
-
* the most important thing here is to detect changes of a single file to invalidate the cache and
|
|
48
|
-
* it's impossible to serve two different files with the same name
|
|
49
|
-
*/
|
|
50
|
-
function hashFileContent(hasher: XXHashAPI, content: string): string {
|
|
51
|
-
return hasher.h64ToString(content).substring(0, 10);
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* Create a hashed asset key for the given asset.
|
|
56
|
-
*
|
|
57
|
-
* The key will change if the file path or content of the asset changes.
|
|
58
|
-
* The algorithm used here matches that of Wrangler 1.
|
|
59
|
-
*/
|
|
60
|
-
function hashAsset(
|
|
61
|
-
hasher: XXHashAPI,
|
|
62
|
-
filePath: string,
|
|
63
|
-
content: string
|
|
64
|
-
): string {
|
|
65
|
-
const extName = path.extname(filePath) || "";
|
|
66
|
-
const baseName = path.basename(filePath, extName);
|
|
67
|
-
const directory = path.dirname(filePath);
|
|
68
|
-
const hash = hashFileContent(hasher, content);
|
|
69
|
-
return urlSafe(path.join(directory, `${baseName}.${hash}${extName}`));
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
async function createKVNamespaceIfNotAlreadyExisting(
|
|
73
|
-
title: string,
|
|
74
|
-
accountId: string
|
|
75
|
-
) {
|
|
76
|
-
// check if it already exists
|
|
77
|
-
// TODO: this is super inefficient, should be made better
|
|
78
|
-
const namespaces = await listNamespaces(accountId);
|
|
79
|
-
const found = namespaces.find((x) => x.title === title);
|
|
80
|
-
if (found) {
|
|
81
|
-
return { created: false, id: found.id };
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
// else we make the namespace
|
|
85
|
-
const id = await createNamespace(accountId, title);
|
|
86
|
-
console.log(`🌀 Created namespace for Workers Site "${title}"`);
|
|
87
|
-
|
|
88
|
-
return {
|
|
89
|
-
created: true,
|
|
90
|
-
id,
|
|
91
|
-
};
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
/**
|
|
95
|
-
* Upload the assets found within the `dirPath` directory to the sites assets KV namespace for
|
|
96
|
-
* the worker given by `scriptName`.
|
|
97
|
-
*
|
|
98
|
-
* @param accountId the account to upload to.
|
|
99
|
-
* @param scriptName the name of the worker whose assets we are uploading.
|
|
100
|
-
* @param siteAssets an objects describing what assets to upload, or undefined if there are no assets to upload.
|
|
101
|
-
* @param preview if true then upload to a "preview" KV namespace.
|
|
102
|
-
* @returns a promise for an object mapping the relative paths of the assets to the key of that
|
|
103
|
-
* asset in the KV namespace.
|
|
104
|
-
*/
|
|
105
|
-
export async function syncAssets(
|
|
106
|
-
accountId: string,
|
|
107
|
-
scriptName: string,
|
|
108
|
-
siteAssets: AssetPaths | undefined,
|
|
109
|
-
preview: boolean
|
|
110
|
-
): Promise<{
|
|
111
|
-
manifest: { [filePath: string]: string } | undefined;
|
|
112
|
-
namespace: string | undefined;
|
|
113
|
-
}> {
|
|
114
|
-
if (siteAssets === undefined) {
|
|
115
|
-
return { manifest: undefined, namespace: undefined };
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
const title = `__${scriptName}-workers_sites_assets${
|
|
119
|
-
preview ? "_preview" : ""
|
|
120
|
-
}`;
|
|
121
|
-
const { id: namespace } = await createKVNamespaceIfNotAlreadyExisting(
|
|
122
|
-
title,
|
|
123
|
-
accountId
|
|
124
|
-
);
|
|
125
|
-
|
|
126
|
-
// let's get all the keys in this namespace
|
|
127
|
-
const result = await listNamespaceKeys(accountId, namespace);
|
|
128
|
-
const keyMap = result.reduce<Record<string, NamespaceKeyInfo>>(
|
|
129
|
-
(km, key) => Object.assign(km, { [key.name]: key }),
|
|
130
|
-
{}
|
|
131
|
-
);
|
|
132
|
-
|
|
133
|
-
const manifest: Record<string, string> = {};
|
|
134
|
-
const toUpload: KeyValue[] = [];
|
|
135
|
-
|
|
136
|
-
const include = createPatternMatcher(siteAssets.includePatterns, false);
|
|
137
|
-
const exclude = createPatternMatcher(siteAssets.excludePatterns, true);
|
|
138
|
-
const hasher = await xxhash();
|
|
139
|
-
|
|
140
|
-
for await (const file of getFilesInFolder(siteAssets.baseDirectory)) {
|
|
141
|
-
if (!include(file)) {
|
|
142
|
-
continue;
|
|
143
|
-
}
|
|
144
|
-
if (exclude(file)) {
|
|
145
|
-
continue;
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
await validateAssetSize(file);
|
|
149
|
-
console.log(`reading ${file}...`);
|
|
150
|
-
const content = await readFile(file, "base64");
|
|
151
|
-
|
|
152
|
-
const assetKey = hashAsset(hasher, file, content);
|
|
153
|
-
validateAssetKey(assetKey);
|
|
154
|
-
|
|
155
|
-
// now put each of the files into kv
|
|
156
|
-
if (!(assetKey in keyMap) || keyMap[assetKey].expiration) {
|
|
157
|
-
console.log(`uploading as ${assetKey}...`);
|
|
158
|
-
toUpload.push({
|
|
159
|
-
key: assetKey,
|
|
160
|
-
value: content,
|
|
161
|
-
base64: true,
|
|
162
|
-
});
|
|
163
|
-
} else {
|
|
164
|
-
console.log(`skipping - already uploaded`);
|
|
165
|
-
}
|
|
166
|
-
// remove the key from the set so we know we've seen it
|
|
167
|
-
delete keyMap[assetKey];
|
|
168
|
-
manifest[path.relative(siteAssets.baseDirectory, file)] = assetKey;
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
// `keyMap` now contains the assets that we need to expire
|
|
172
|
-
|
|
173
|
-
let toExpire: KeyValue[] = [];
|
|
174
|
-
for (const asset of Object.values(keyMap)) {
|
|
175
|
-
if (!asset.expiration) {
|
|
176
|
-
console.log(`expiring unused ${asset.name}...`);
|
|
177
|
-
toExpire.push({
|
|
178
|
-
key: asset.name,
|
|
179
|
-
value: "", // we'll fill all the values in one go
|
|
180
|
-
// we use expiration_ttl, since we can't trust the time
|
|
181
|
-
// that a deploy source might provide (eg - https://github.com/cloudflare/wrangler/issues/2224)
|
|
182
|
-
expiration_ttl: FIVE_MINUTES,
|
|
183
|
-
base64: true,
|
|
184
|
-
});
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
// TODO: batch these in groups if it causes problems
|
|
189
|
-
toExpire = await Promise.all(
|
|
190
|
-
toExpire.map(async (asset) => ({
|
|
191
|
-
...asset,
|
|
192
|
-
// it would be great if we didn't have to do this fetch at all
|
|
193
|
-
value: await getKeyValue(accountId, namespace, asset.key),
|
|
194
|
-
}))
|
|
195
|
-
);
|
|
196
|
-
|
|
197
|
-
await putBulkKeyValue(
|
|
198
|
-
accountId,
|
|
199
|
-
namespace,
|
|
200
|
-
toUpload.concat(toExpire),
|
|
201
|
-
() => {}
|
|
202
|
-
);
|
|
203
|
-
return { manifest, namespace };
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
function createPatternMatcher(
|
|
207
|
-
patterns: string[],
|
|
208
|
-
exclude: boolean
|
|
209
|
-
): (filePath: string) => boolean {
|
|
210
|
-
if (patterns.length === 0) {
|
|
211
|
-
return (_filePath) => !exclude;
|
|
212
|
-
} else {
|
|
213
|
-
const ignorer = ignore().add(patterns);
|
|
214
|
-
return (filePath) => ignorer.test(filePath).ignored;
|
|
215
|
-
}
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
async function validateAssetSize(filePath: string) {
|
|
219
|
-
const { size } = await stat(filePath);
|
|
220
|
-
if (size > 25 * 1024 * 1024) {
|
|
221
|
-
throw new Error(
|
|
222
|
-
`File ${filePath} is too big, it should be under 25 MiB. See https://developers.cloudflare.com/workers/platform/limits#kv-limits`
|
|
223
|
-
);
|
|
224
|
-
}
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
function validateAssetKey(assetKey: string) {
|
|
228
|
-
if (assetKey.length > 512) {
|
|
229
|
-
throw new Error(
|
|
230
|
-
`The asset path key "${assetKey}" exceeds the maximum key size limit of 512. See https://developers.cloudflare.com/workers/platform/limits#kv-limits",`
|
|
231
|
-
);
|
|
232
|
-
}
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
/**
|
|
236
|
-
* Convert a filePath to be safe to use as a relative URL.
|
|
237
|
-
*
|
|
238
|
-
* Primarily this involves converting Windows backslashes to forward slashes.
|
|
239
|
-
*/
|
|
240
|
-
function urlSafe(filePath: string): string {
|
|
241
|
-
return filePath.replace(/\\/g, "/");
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
/**
|
|
245
|
-
* Information about the assets that should be uploaded
|
|
246
|
-
*/
|
|
247
|
-
export interface AssetPaths {
|
|
248
|
-
baseDirectory: string;
|
|
249
|
-
includePatterns: string[];
|
|
250
|
-
excludePatterns: string[];
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
/**
|
|
254
|
-
* Get an object that describes what assets to upload, if any.
|
|
255
|
-
*
|
|
256
|
-
* Uses the args (passed from the command line) if available,
|
|
257
|
-
* falling back to those defined in the config.
|
|
258
|
-
*
|
|
259
|
-
*/
|
|
260
|
-
export function getAssetPaths(
|
|
261
|
-
config: Config,
|
|
262
|
-
baseDirectory = config.site?.bucket,
|
|
263
|
-
includePatterns = config.site?.include ?? [],
|
|
264
|
-
excludePatterns = config.site?.exclude ?? []
|
|
265
|
-
): undefined | AssetPaths {
|
|
266
|
-
return baseDirectory
|
|
267
|
-
? {
|
|
268
|
-
baseDirectory,
|
|
269
|
-
includePatterns,
|
|
270
|
-
excludePatterns,
|
|
271
|
-
}
|
|
272
|
-
: undefined;
|
|
273
|
-
}
|