wrangler 0.0.0-e6733a3 → 0.0.0-e6ada079

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.

Potentially problematic release.


This version of wrangler might be problematic. Click here for more details.

Files changed (119) hide show
  1. package/README.md +47 -16
  2. package/bin/wrangler.js +94 -31
  3. package/config-schema.json +3100 -0
  4. package/kv-asset-handler.js +1 -0
  5. package/package.json +154 -82
  6. package/templates/__tests__/pages-dev-util.test.ts +128 -0
  7. package/templates/__tests__/tsconfig-sanity.ts +12 -0
  8. package/templates/__tests__/tsconfig.json +8 -0
  9. package/templates/checked-fetch.js +30 -0
  10. package/templates/facade.d.ts +19 -0
  11. package/templates/gitignore +170 -0
  12. package/templates/init-tests/test-jest-new-worker.js +23 -0
  13. package/templates/init-tests/test-vitest-new-worker.js +24 -0
  14. package/templates/init-tests/test-vitest-new-worker.ts +25 -0
  15. package/templates/middleware/common.ts +67 -0
  16. package/templates/middleware/loader-modules.ts +134 -0
  17. package/templates/middleware/loader-sw.ts +229 -0
  18. package/templates/middleware/middleware-ensure-req-body-drained.ts +18 -0
  19. package/templates/middleware/middleware-miniflare3-json-error.ts +32 -0
  20. package/templates/middleware/middleware-pretty-error.ts +40 -0
  21. package/templates/middleware/middleware-scheduled.ts +15 -0
  22. package/templates/middleware/middleware-serve-static-assets.d.ts +6 -0
  23. package/templates/middleware/middleware-serve-static-assets.ts +56 -0
  24. package/templates/modules-watch-stub.js +4 -0
  25. package/templates/new-worker-scheduled.js +17 -0
  26. package/templates/new-worker-scheduled.ts +32 -0
  27. package/templates/new-worker.js +15 -0
  28. package/templates/new-worker.ts +33 -0
  29. package/templates/no-op-worker.js +10 -0
  30. package/templates/pages-dev-pipeline.ts +32 -0
  31. package/templates/pages-dev-util.ts +55 -0
  32. package/templates/pages-shim.ts +9 -0
  33. package/templates/pages-template-plugin.ts +190 -0
  34. package/templates/pages-template-worker.ts +198 -0
  35. package/templates/startDevWorker/InspectorProxyWorker.ts +664 -0
  36. package/templates/startDevWorker/ProxyWorker.ts +334 -0
  37. package/templates/tsconfig-sanity.ts +11 -0
  38. package/templates/tsconfig.init.json +22 -0
  39. package/templates/tsconfig.json +8 -0
  40. package/wrangler-dist/InspectorProxyWorker.js +464 -0
  41. package/wrangler-dist/InspectorProxyWorker.js.map +6 -0
  42. package/wrangler-dist/ProxyWorker.js +240 -0
  43. package/wrangler-dist/ProxyWorker.js.map +6 -0
  44. package/wrangler-dist/cli.d.ts +26391 -0
  45. package/wrangler-dist/cli.js +204293 -116652
  46. package/wrangler-dist/wasm-sync.wasm +0 -0
  47. package/import_meta_url.js +0 -3
  48. package/miniflare-config-stubs/.env.empty +0 -0
  49. package/miniflare-config-stubs/package.empty.json +0 -1
  50. package/miniflare-config-stubs/wrangler.empty.toml +0 -0
  51. package/pages/functions/buildWorker.ts +0 -62
  52. package/pages/functions/filepath-routing.test.ts +0 -39
  53. package/pages/functions/filepath-routing.ts +0 -221
  54. package/pages/functions/identifiers.ts +0 -78
  55. package/pages/functions/routes.ts +0 -158
  56. package/pages/functions/template-worker.ts +0 -144
  57. package/src/__tests__/clipboardy-mock.js +0 -4
  58. package/src/__tests__/dev.test.tsx +0 -66
  59. package/src/__tests__/index.test.ts +0 -287
  60. package/src/__tests__/jest.setup.ts +0 -22
  61. package/src/__tests__/kv.test.ts +0 -1098
  62. package/src/__tests__/mock-cfetch.ts +0 -171
  63. package/src/__tests__/mock-dialogs.ts +0 -65
  64. package/src/__tests__/run-in-tmp.ts +0 -19
  65. package/src/__tests__/run-wrangler.ts +0 -32
  66. package/src/api/form_data.ts +0 -131
  67. package/src/api/preview.ts +0 -128
  68. package/src/api/worker.ts +0 -155
  69. package/src/cfetch/index.ts +0 -102
  70. package/src/cfetch/internal.ts +0 -69
  71. package/src/cli.ts +0 -9
  72. package/src/config.ts +0 -487
  73. package/src/dev.tsx +0 -771
  74. package/src/dialogs.tsx +0 -77
  75. package/src/index.tsx +0 -1974
  76. package/src/inspect.ts +0 -524
  77. package/src/kv.tsx +0 -267
  78. package/src/module-collection.ts +0 -64
  79. package/src/pages.tsx +0 -1031
  80. package/src/proxy.ts +0 -294
  81. package/src/publish.ts +0 -358
  82. package/src/sites.tsx +0 -114
  83. package/src/tail.tsx +0 -73
  84. package/src/user.tsx +0 -1025
  85. package/static-asset-facade.js +0 -47
  86. package/vendor/@cloudflare/kv-asset-handler/CHANGELOG.md +0 -332
  87. package/vendor/@cloudflare/kv-asset-handler/LICENSE_APACHE +0 -176
  88. package/vendor/@cloudflare/kv-asset-handler/LICENSE_MIT +0 -25
  89. package/vendor/@cloudflare/kv-asset-handler/README.md +0 -245
  90. package/vendor/@cloudflare/kv-asset-handler/dist/index.d.ts +0 -32
  91. package/vendor/@cloudflare/kv-asset-handler/dist/index.js +0 -354
  92. package/vendor/@cloudflare/kv-asset-handler/dist/mocks.d.ts +0 -13
  93. package/vendor/@cloudflare/kv-asset-handler/dist/mocks.js +0 -148
  94. package/vendor/@cloudflare/kv-asset-handler/dist/test/getAssetFromKV.d.ts +0 -1
  95. package/vendor/@cloudflare/kv-asset-handler/dist/test/getAssetFromKV.js +0 -436
  96. package/vendor/@cloudflare/kv-asset-handler/dist/test/mapRequestToAsset.d.ts +0 -1
  97. package/vendor/@cloudflare/kv-asset-handler/dist/test/mapRequestToAsset.js +0 -40
  98. package/vendor/@cloudflare/kv-asset-handler/dist/test/serveSinglePageApp.d.ts +0 -1
  99. package/vendor/@cloudflare/kv-asset-handler/dist/test/serveSinglePageApp.js +0 -42
  100. package/vendor/@cloudflare/kv-asset-handler/dist/types.d.ts +0 -26
  101. package/vendor/@cloudflare/kv-asset-handler/dist/types.js +0 -31
  102. package/vendor/@cloudflare/kv-asset-handler/package.json +0 -52
  103. package/vendor/@cloudflare/kv-asset-handler/src/index.ts +0 -296
  104. package/vendor/@cloudflare/kv-asset-handler/src/mocks.ts +0 -136
  105. package/vendor/@cloudflare/kv-asset-handler/src/test/getAssetFromKV.ts +0 -464
  106. package/vendor/@cloudflare/kv-asset-handler/src/test/mapRequestToAsset.ts +0 -33
  107. package/vendor/@cloudflare/kv-asset-handler/src/test/serveSinglePageApp.ts +0 -42
  108. package/vendor/@cloudflare/kv-asset-handler/src/types.ts +0 -39
  109. package/vendor/wrangler-mime/CHANGELOG.md +0 -289
  110. package/vendor/wrangler-mime/LICENSE +0 -21
  111. package/vendor/wrangler-mime/Mime.js +0 -97
  112. package/vendor/wrangler-mime/README.md +0 -187
  113. package/vendor/wrangler-mime/cli.js +0 -46
  114. package/vendor/wrangler-mime/index.js +0 -4
  115. package/vendor/wrangler-mime/lite.js +0 -4
  116. package/vendor/wrangler-mime/package.json +0 -52
  117. package/vendor/wrangler-mime/types/other.js +0 -1
  118. package/vendor/wrangler-mime/types/standard.js +0 -1
  119. package/wrangler-dist/cli.js.map +0 -7
@@ -1,42 +0,0 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- const ava_1 = require("ava");
13
- const mocks_1 = require("../mocks");
14
- const index_1 = require("../index");
15
- function testRequest(path) {
16
- mocks_1.mockGlobal();
17
- let url = new URL('https://example.com');
18
- url.pathname = path;
19
- let request = new Request(url.toString());
20
- return request;
21
- }
22
- ava_1.default('serveSinglePageApp returns root asset path when request path ends in .html', (t) => __awaiter(void 0, void 0, void 0, function* () {
23
- let path = '/foo/thing.html';
24
- let request = testRequest(path);
25
- let expected_request = testRequest('/index.html');
26
- let actual_request = index_1.serveSinglePageApp(request);
27
- t.deepEqual(expected_request, actual_request);
28
- }));
29
- ava_1.default('serveSinglePageApp returns root asset path when request path does not have extension', (t) => __awaiter(void 0, void 0, void 0, function* () {
30
- let path = '/foo/thing';
31
- let request = testRequest(path);
32
- let expected_request = testRequest('/index.html');
33
- let actual_request = index_1.serveSinglePageApp(request);
34
- t.deepEqual(expected_request, actual_request);
35
- }));
36
- ava_1.default('serveSinglePageApp returns requested asset when request path has non-html extension', (t) => __awaiter(void 0, void 0, void 0, function* () {
37
- let path = '/foo/thing.js';
38
- let request = testRequest(path);
39
- let expected_request = request;
40
- let actual_request = index_1.serveSinglePageApp(request);
41
- t.deepEqual(expected_request, actual_request);
42
- }));
@@ -1,26 +0,0 @@
1
- export declare type CacheControl = {
2
- browserTTL: number;
3
- edgeTTL: number;
4
- bypassCache: boolean;
5
- };
6
- export declare type Options = {
7
- cacheControl: ((req: Request) => Partial<CacheControl>) | Partial<CacheControl>;
8
- ASSET_NAMESPACE: any;
9
- ASSET_MANIFEST: Object | string;
10
- mapRequestToAsset?: (req: Request, options?: Partial<Options>) => Request;
11
- defaultMimeType: string;
12
- defaultDocument: string;
13
- };
14
- export declare class KVError extends Error {
15
- constructor(message?: string, status?: number);
16
- status: number;
17
- }
18
- export declare class MethodNotAllowedError extends KVError {
19
- constructor(message?: string, status?: number);
20
- }
21
- export declare class NotFoundError extends KVError {
22
- constructor(message?: string, status?: number);
23
- }
24
- export declare class InternalError extends KVError {
25
- constructor(message?: string, status?: number);
26
- }
@@ -1,31 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.InternalError = exports.NotFoundError = exports.MethodNotAllowedError = exports.KVError = void 0;
4
- class KVError extends Error {
5
- constructor(message, status = 500) {
6
- super(message);
7
- // see: typescriptlang.org/docs/handbook/release-notes/typescript-2-2.html
8
- Object.setPrototypeOf(this, new.target.prototype); // restore prototype chain
9
- this.name = KVError.name; // stack traces display correctly now
10
- this.status = status;
11
- }
12
- }
13
- exports.KVError = KVError;
14
- class MethodNotAllowedError extends KVError {
15
- constructor(message = `Not a valid request method`, status = 405) {
16
- super(message, status);
17
- }
18
- }
19
- exports.MethodNotAllowedError = MethodNotAllowedError;
20
- class NotFoundError extends KVError {
21
- constructor(message = `Not Found`, status = 404) {
22
- super(message, status);
23
- }
24
- }
25
- exports.NotFoundError = NotFoundError;
26
- class InternalError extends KVError {
27
- constructor(message = `Internal Error in KV Asset Handler`, status = 500) {
28
- super(message, status);
29
- }
30
- }
31
- exports.InternalError = InternalError;
@@ -1,52 +0,0 @@
1
- {
2
- "name": "@cloudflare/kv-asset-handler",
3
- "version": "0.1.2",
4
- "description": "Routes requests to KV assets",
5
- "main": "dist/index.js",
6
- "types": "dist/index.d.ts",
7
- "scripts": {
8
- "prepack": "npm run build",
9
- "build": "tsc -d",
10
- "format": "prettier --write \"**/*.{js,ts,json,md}\"",
11
- "pretest": "npm run build",
12
- "lint:code": "prettier --check \"**/*.{js,ts,json,md}\"",
13
- "lint:markdown": "markdownlint \"**/*.md\" --ignore node_modules",
14
- "test": "ava dist/test/*.js --verbose"
15
- },
16
- "repository": {
17
- "type": "git",
18
- "url": "git+https://github.com/cloudflare/kv-asset-handler.git"
19
- },
20
- "keywords": [
21
- "kv",
22
- "cloudflare",
23
- "workers",
24
- "wrangler",
25
- "assets"
26
- ],
27
- "files": [
28
- "src",
29
- "dist",
30
- "LICENSE_APACHE",
31
- "LICENSE_MIT"
32
- ],
33
- "author": "wrangler@cloudflare.com",
34
- "license": "MIT OR Apache-2.0",
35
- "bugs": {
36
- "url": "https://github.com/cloudflare/kv-asset-handler/issues"
37
- },
38
- "homepage": "https://github.com/cloudflare/kv-asset-handler#readme",
39
- "dependencies": {
40
- "wrangler-mime": "^2.5.2"
41
- },
42
- "devDependencies": {
43
- "@ava/typescript": "^1.1.1",
44
- "@cloudflare/workers-types": "^2.2.2",
45
- "@types/mime": "^2.0.3",
46
- "@types/node": "^15.3.0",
47
- "ava": "^3.15.0",
48
- "prettier": "^2.3.0",
49
- "service-worker-mock": "^2.0.5",
50
- "typescript": "^4.2.4"
51
- }
52
- }
@@ -1,296 +0,0 @@
1
- import * as mime from 'mime'
2
- import { Options, CacheControl, MethodNotAllowedError, NotFoundError, InternalError } from './types'
3
-
4
- declare global {
5
- var __STATIC_CONTENT: any, __STATIC_CONTENT_MANIFEST: string
6
- }
7
-
8
- const defaultCacheControl: CacheControl = {
9
- browserTTL: null,
10
- edgeTTL: 2 * 60 * 60 * 24, // 2 days
11
- bypassCache: false, // do not bypass Cloudflare's cache
12
- }
13
-
14
- function assignOptions(options?: Partial<Options>): Options {
15
- // Assign any missing options passed in to the default
16
- // options.mapRequestToAsset is handled manually later
17
- return Object.assign(
18
- {
19
- ASSET_NAMESPACE: __STATIC_CONTENT,
20
- ASSET_MANIFEST: __STATIC_CONTENT_MANIFEST,
21
- cacheControl: defaultCacheControl,
22
- defaultMimeType: 'text/plain',
23
- defaultDocument: 'index.html',
24
- },
25
- options,
26
- )
27
- }
28
-
29
- /**
30
- * maps the path of incoming request to the request pathKey to look up
31
- * in bucket and in cache
32
- * e.g. for a path '/' returns '/index.html' which serves
33
- * the content of bucket/index.html
34
- * @param {Request} request incoming request
35
- */
36
- const mapRequestToAsset = (request: Request, options?: Partial<Options>) => {
37
- options = assignOptions(options)
38
-
39
- const parsedUrl = new URL(request.url)
40
- let pathname = parsedUrl.pathname
41
-
42
- if (pathname.endsWith('/')) {
43
- // If path looks like a directory append options.defaultDocument
44
- // e.g. If path is /about/ -> /about/index.html
45
- pathname = pathname.concat(options.defaultDocument)
46
- } else if (!mime.getType(pathname)) {
47
- // If path doesn't look like valid content
48
- // e.g. /about.me -> /about.me/index.html
49
- pathname = pathname.concat('/' + options.defaultDocument)
50
- }
51
-
52
- parsedUrl.pathname = pathname
53
- return new Request(parsedUrl.toString(), request)
54
- }
55
-
56
- /**
57
- * maps the path of incoming request to /index.html if it evaluates to
58
- * any HTML file.
59
- * @param {Request} request incoming request
60
- */
61
- function serveSinglePageApp(request: Request, options?: Partial<Options>): Request {
62
- options = assignOptions(options)
63
-
64
- // First apply the default handler, which already has logic to detect
65
- // paths that should map to HTML files.
66
- request = mapRequestToAsset(request, options)
67
-
68
- const parsedUrl = new URL(request.url)
69
-
70
- // Detect if the default handler decided to map to
71
- // a HTML file in some specific directory.
72
- if (parsedUrl.pathname.endsWith('.html')) {
73
- // If expected HTML file was missing, just return the root index.html (or options.defaultDocument)
74
- return new Request(`${parsedUrl.origin}/${options.defaultDocument}`, request)
75
- } else {
76
- // The default handler decided this is not an HTML page. It's probably
77
- // an image, CSS, or JS file. Leave it as-is.
78
- return request
79
- }
80
- }
81
-
82
- /**
83
- * takes the path of the incoming request, gathers the appropriate content from KV, and returns
84
- * the response
85
- *
86
- * @param {FetchEvent} event the fetch event of the triggered request
87
- * @param {{mapRequestToAsset: (string: Request) => Request, cacheControl: {bypassCache:boolean, edgeTTL: number, browserTTL:number}, ASSET_NAMESPACE: any, ASSET_MANIFEST:any}} [options] configurable options
88
- * @param {CacheControl} [options.cacheControl] determine how to cache on Cloudflare and the browser
89
- * @param {typeof(options.mapRequestToAsset)} [options.mapRequestToAsset] maps the path of incoming request to the request pathKey to look up
90
- * @param {Object | string} [options.ASSET_NAMESPACE] the binding to the namespace that script references
91
- * @param {any} [options.ASSET_MANIFEST] the map of the key to cache and store in KV
92
- * */
93
- const getAssetFromKV = async (event: FetchEvent, options?: Partial<Options>): Promise<Response> => {
94
- options = assignOptions(options)
95
-
96
- const request = event.request
97
- const ASSET_NAMESPACE = options.ASSET_NAMESPACE
98
- const ASSET_MANIFEST =
99
- typeof options.ASSET_MANIFEST === 'string'
100
- ? JSON.parse(options.ASSET_MANIFEST)
101
- : options.ASSET_MANIFEST
102
-
103
- if (typeof ASSET_NAMESPACE === 'undefined') {
104
- throw new InternalError(`there is no KV namespace bound to the script`)
105
- }
106
-
107
- const rawPathKey = new URL(request.url).pathname.replace(/^\/+/, '') // strip any preceding /'s
108
- let pathIsEncoded = false
109
- let requestKey
110
- // if options.mapRequestToAsset is explicitly passed in, always use it and assume user has own intentions
111
- // otherwise handle request as normal, with default mapRequestToAsset below
112
- if (options.mapRequestToAsset) {
113
- requestKey = options.mapRequestToAsset(request)
114
- } else if (ASSET_MANIFEST[rawPathKey]) {
115
- requestKey = request
116
- } else if (ASSET_MANIFEST[decodeURIComponent(rawPathKey)]) {
117
- pathIsEncoded = true
118
- requestKey = request
119
- } else {
120
- const mappedRequest = mapRequestToAsset(request)
121
- const mappedRawPathKey = new URL(mappedRequest.url).pathname.replace(/^\/+/, '')
122
- if (ASSET_MANIFEST[decodeURIComponent(mappedRawPathKey)]) {
123
- pathIsEncoded = true
124
- requestKey = mappedRequest
125
- } else {
126
- // use default mapRequestToAsset
127
- requestKey = mapRequestToAsset(request, options)
128
- }
129
- }
130
-
131
- const SUPPORTED_METHODS = ['GET', 'HEAD']
132
- if (!SUPPORTED_METHODS.includes(requestKey.method)) {
133
- throw new MethodNotAllowedError(`${requestKey.method} is not a valid request method`)
134
- }
135
-
136
- const parsedUrl = new URL(requestKey.url)
137
- const pathname = pathIsEncoded ? decodeURIComponent(parsedUrl.pathname) : parsedUrl.pathname // decode percentage encoded path only when necessary
138
-
139
- // pathKey is the file path to look up in the manifest
140
- let pathKey = pathname.replace(/^\/+/, '') // remove prepended /
141
-
142
- // @ts-ignore
143
- const cache = caches.default
144
- let mimeType = mime.getType(pathKey) || options.defaultMimeType
145
- if (mimeType.startsWith('text') || mimeType === 'application/javascript') {
146
- mimeType += '; charset=utf-8'
147
- }
148
-
149
- let shouldEdgeCache = false // false if storing in KV by raw file path i.e. no hash
150
- // check manifest for map from file path to hash
151
- if (typeof ASSET_MANIFEST !== 'undefined') {
152
- if (ASSET_MANIFEST[pathKey]) {
153
- pathKey = ASSET_MANIFEST[pathKey]
154
- // if path key is in asset manifest, we can assume it contains a content hash and can be cached
155
- shouldEdgeCache = true
156
- }
157
- }
158
-
159
- // TODO this excludes search params from cache, investigate ideal behavior
160
- let cacheKey = new Request(`${parsedUrl.origin}/${pathKey}`, request)
161
-
162
- // if argument passed in for cacheControl is a function then
163
- // evaluate that function. otherwise return the Object passed in
164
- // or default Object
165
- const evalCacheOpts = (() => {
166
- switch (typeof options.cacheControl) {
167
- case 'function':
168
- return options.cacheControl(request)
169
- case 'object':
170
- return options.cacheControl
171
- default:
172
- return defaultCacheControl
173
- }
174
- })()
175
-
176
- // formats the etag depending on the response context. if the entityId
177
- // is invalid, returns an empty string (instead of null) to prevent the
178
- // the potentially disastrous scenario where the value of the Etag resp
179
- // header is "null". Could be modified in future to base64 encode etc
180
- const formatETag = (entityId: any = pathKey, validatorType: string = 'strong') => {
181
- if (!entityId) {
182
- return ''
183
- }
184
- switch (validatorType) {
185
- case 'weak':
186
- if (!entityId.startsWith('W/')) {
187
- return `W/${entityId}`
188
- }
189
- return entityId
190
- case 'strong':
191
- if (entityId.startsWith(`W/"`)) {
192
- entityId = entityId.replace('W/', '')
193
- }
194
- if (!entityId.endsWith(`"`)) {
195
- entityId = `"${entityId}"`
196
- }
197
- return entityId
198
- default:
199
- return ''
200
- }
201
- }
202
-
203
- options.cacheControl = Object.assign({}, defaultCacheControl, evalCacheOpts)
204
-
205
- // override shouldEdgeCache if options say to bypassCache
206
- if (
207
- options.cacheControl.bypassCache ||
208
- options.cacheControl.edgeTTL === null ||
209
- request.method == 'HEAD'
210
- ) {
211
- shouldEdgeCache = false
212
- }
213
- // only set max-age if explicitly passed in a number as an arg
214
- const shouldSetBrowserCache = typeof options.cacheControl.browserTTL === 'number'
215
-
216
- let response = null
217
- if (shouldEdgeCache) {
218
- response = await cache.match(cacheKey)
219
- }
220
-
221
- if (response) {
222
- if (response.status > 300 && response.status < 400) {
223
- if (response.body && 'cancel' in Object.getPrototypeOf(response.body)) {
224
- response.body.cancel()
225
- console.log('Body exists and environment supports readable streams. Body cancelled')
226
- } else {
227
- console.log('Environment doesnt support readable streams')
228
- }
229
- response = new Response(null, response)
230
- } else {
231
- // fixes #165
232
- let opts = {
233
- headers: new Headers(response.headers),
234
- status: 0,
235
- statusText: '',
236
- }
237
-
238
- opts.headers.set('cf-cache-status', 'HIT')
239
-
240
- if (response.status) {
241
- opts.status = response.status
242
- opts.statusText = response.statusText
243
- } else if (opts.headers.has('Content-Range')) {
244
- opts.status = 206
245
- opts.statusText = 'Partial Content'
246
- } else {
247
- opts.status = 200
248
- opts.statusText = 'OK'
249
- }
250
- response = new Response(response.body, opts)
251
- }
252
- } else {
253
- const body = await ASSET_NAMESPACE.get(pathKey, 'arrayBuffer')
254
- if (body === null) {
255
- throw new NotFoundError(`could not find ${pathKey} in your content namespace`)
256
- }
257
- response = new Response(body)
258
-
259
- if (shouldEdgeCache) {
260
- response.headers.set('Accept-Ranges', 'bytes')
261
- response.headers.set('Content-Length', body.length)
262
- // set etag before cache insertion
263
- if (!response.headers.has('etag')) {
264
- response.headers.set('etag', formatETag(pathKey, 'strong'))
265
- }
266
- // determine Cloudflare cache behavior
267
- response.headers.set('Cache-Control', `max-age=${options.cacheControl.edgeTTL}`)
268
- event.waitUntil(cache.put(cacheKey, response.clone()))
269
- response.headers.set('CF-Cache-Status', 'MISS')
270
- }
271
- }
272
- response.headers.set('Content-Type', mimeType)
273
-
274
- if (response.status === 304) {
275
- let etag = formatETag(response.headers.get('etag'), 'strong')
276
- let ifNoneMatch = cacheKey.headers.get('if-none-match')
277
- let proxyCacheStatus = response.headers.get('CF-Cache-Status')
278
- if (etag) {
279
- if (ifNoneMatch && ifNoneMatch === etag && proxyCacheStatus === 'MISS') {
280
- response.headers.set('CF-Cache-Status', 'EXPIRED')
281
- } else {
282
- response.headers.set('CF-Cache-Status', 'REVALIDATED')
283
- }
284
- response.headers.set('etag', formatETag(etag, 'weak'))
285
- }
286
- }
287
- if (shouldSetBrowserCache) {
288
- response.headers.set('Cache-Control', `max-age=${options.cacheControl.browserTTL}`)
289
- } else {
290
- response.headers.delete('Cache-Control')
291
- }
292
- return response
293
- }
294
-
295
- export { getAssetFromKV, mapRequestToAsset, serveSinglePageApp }
296
- export { Options, CacheControl, MethodNotAllowedError, NotFoundError, InternalError }
@@ -1,136 +0,0 @@
1
- const makeServiceWorkerEnv = require('service-worker-mock')
2
-
3
- const HASH = '123HASHBROWN'
4
-
5
- export const getEvent = (request: Request): any => {
6
- const waitUntil = async (callback: any) => {
7
- await callback
8
- }
9
- return {
10
- request,
11
- waitUntil,
12
- }
13
- }
14
- const store: any = {
15
- 'key1.123HASHBROWN.txt': 'val1',
16
- 'key1.123HASHBROWN.png': 'val1',
17
- 'index.123HASHBROWN.html': 'index.html',
18
- 'cache.123HASHBROWN.html': 'cache me if you can',
19
- '测试.123HASHBROWN.html': 'My filename is non-ascii',
20
- '%not-really-percent-encoded.123HASHBROWN.html': 'browser percent encoded',
21
- '%2F.123HASHBROWN.html': 'user percent encoded',
22
- '你好.123HASHBROWN.html': 'I shouldnt be served',
23
- '%E4%BD%A0%E5%A5%BD.123HASHBROWN.html': 'Im important',
24
- 'nohash.txt': 'no hash but still got some result',
25
- 'sub/blah.123HASHBROWN.png': 'picturedis',
26
- 'sub/index.123HASHBROWN.html': 'picturedis',
27
- 'client.123HASHBROWN': 'important file',
28
- 'client.123HASHBROWN/index.html': 'Im here but serve my big bro above',
29
- '你好/index.123HASHBROWN.html': 'My path is non-ascii',
30
- }
31
- export const mockKV = (store: any) => {
32
- return {
33
- get: (path: string) => store[path] || null,
34
- }
35
- }
36
-
37
- export const mockManifest = () => {
38
- return JSON.stringify({
39
- 'key1.txt': `key1.${HASH}.txt`,
40
- 'key1.png': `key1.${HASH}.png`,
41
- 'cache.html': `cache.${HASH}.html`,
42
- '测试.html': `测试.${HASH}.html`,
43
- '你好.html': `你好.${HASH}.html`,
44
- '%not-really-percent-encoded.html': `%not-really-percent-encoded.${HASH}.html`,
45
- '%2F.html': `%2F.${HASH}.html`,
46
- '%E4%BD%A0%E5%A5%BD.html': `%E4%BD%A0%E5%A5%BD.${HASH}.html`,
47
- 'index.html': `index.${HASH}.html`,
48
- 'sub/blah.png': `sub/blah.${HASH}.png`,
49
- 'sub/index.html': `sub/index.${HASH}.html`,
50
- client: `client.${HASH}`,
51
- 'client/index.html': `client.${HASH}`,
52
- '你好/index.html': `你好/index.${HASH}.html`,
53
- })
54
- }
55
-
56
- let cacheStore: any = new Map()
57
- interface CacheKey {
58
- url: object
59
- headers: object
60
- }
61
- export const mockCaches = () => {
62
- return {
63
- default: {
64
- async match(key: any) {
65
- let cacheKey: CacheKey = {
66
- url: key.url,
67
- headers: {},
68
- }
69
- let response
70
- if (key.headers.has('if-none-match')) {
71
- let makeStrongEtag = key.headers.get('if-none-match').replace('W/', '')
72
- Reflect.set(cacheKey.headers, 'etag', makeStrongEtag)
73
- response = cacheStore.get(JSON.stringify(cacheKey))
74
- } else {
75
- // if client doesn't send if-none-match, we need to iterate through these keys
76
- // and just test the URL
77
- const activeCacheKeys: Array<string> = Array.from(cacheStore.keys())
78
- for (const cacheStoreKey of activeCacheKeys) {
79
- if (JSON.parse(cacheStoreKey).url === key.url) {
80
- response = cacheStore.get(cacheStoreKey)
81
- }
82
- }
83
- }
84
- // TODO: write test to accomodate for rare scenarios with where range requests accomodate etags
85
- if (response && !key.headers.has('if-none-match')) {
86
- // this appears overly verbose, but is necessary to document edge cache behavior
87
- // The Range request header triggers the response header Content-Range ...
88
- const range = key.headers.get('range')
89
- if (range) {
90
- response.headers.set(
91
- 'content-range',
92
- `bytes ${range.split('=').pop()}/${response.headers.get('content-length')}`,
93
- )
94
- }
95
- // ... which we are using in this repository to set status 206
96
- if (response.headers.has('content-range')) {
97
- response.status = 206
98
- } else {
99
- response.status = 200
100
- }
101
- let etag = response.headers.get('etag')
102
- if (etag && !etag.includes('W/')) {
103
- response.headers.set('etag', `W/${etag}`)
104
- }
105
- }
106
- return response
107
- },
108
- async put(key: any, val: Response) {
109
- let headers = new Headers(val.headers)
110
- let url = new URL(key.url)
111
- let resWithBody = new Response(val.body, { headers, status: 200 })
112
- let resNoBody = new Response(null, { headers, status: 304 })
113
- let cacheKey: CacheKey = {
114
- url: key.url,
115
- headers: {
116
- etag: `"${url.pathname.replace('/', '')}"`,
117
- },
118
- }
119
- cacheStore.set(JSON.stringify(cacheKey), resNoBody)
120
- cacheKey.headers = {}
121
- cacheStore.set(JSON.stringify(cacheKey), resWithBody)
122
- return
123
- },
124
- },
125
- }
126
- }
127
-
128
- export function mockGlobal() {
129
- Object.assign(global, makeServiceWorkerEnv())
130
- Object.assign(global, { __STATIC_CONTENT_MANIFEST: mockManifest() })
131
- Object.assign(global, { __STATIC_CONTENT: mockKV(store) })
132
- Object.assign(global, { caches: mockCaches() })
133
- }
134
- export const sleep = (milliseconds: number) => {
135
- return new Promise((resolve) => setTimeout(resolve, milliseconds))
136
- }