wrangler 0.0.0-e88a54e → 0.0.0-e89ee2a5
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 -16
- package/bin/wrangler.js +123 -32
- package/kv-asset-handler.js +1 -0
- package/package.json +168 -86
- 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/checked-fetch.js +30 -0
- package/templates/facade.d.ts +17 -0
- package/templates/gitignore +167 -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 +67 -0
- package/templates/middleware/loader-modules.ts +136 -0
- package/templates/middleware/loader-sw.ts +229 -0
- package/templates/middleware/middleware-d1-beta.d.ts +3 -0
- package/templates/middleware/middleware-ensure-req-body-drained.ts +19 -0
- package/templates/middleware/middleware-miniflare3-json-error.ts +33 -0
- package/templates/middleware/middleware-multiworker-dev.d.ts +4 -0
- package/templates/middleware/middleware-multiworker-dev.ts +68 -0
- package/templates/middleware/middleware-pretty-error.ts +40 -0
- package/templates/middleware/middleware-scheduled.ts +15 -0
- package/templates/middleware/middleware-serve-static-assets.d.ts +6 -0
- package/templates/middleware/middleware-serve-static-assets.ts +56 -0
- package/templates/modules-watch-stub.js +4 -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 +22 -4
- package/templates/no-op-worker.js +10 -0
- package/templates/pages-dev-pipeline.ts +32 -0
- package/templates/pages-dev-util.ts +55 -0
- package/templates/pages-shim.ts +9 -0
- package/templates/pages-template-plugin.ts +190 -0
- package/templates/pages-template-worker.ts +198 -0
- package/templates/startDevWorker/InspectorProxyWorker.ts +661 -0
- package/templates/startDevWorker/ProxyWorker.ts +326 -0
- package/templates/tsconfig-sanity.ts +11 -0
- package/templates/tsconfig.init.json +105 -0
- package/templates/tsconfig.json +6 -103
- package/wrangler-dist/InspectorProxyWorker.js +464 -0
- package/wrangler-dist/InspectorProxyWorker.js.map +6 -0
- package/wrangler-dist/ProxyWorker.js +237 -0
- package/wrangler-dist/ProxyWorker.js.map +6 -0
- package/wrangler-dist/cli.d.ts +23948 -0
- package/wrangler-dist/cli.js +164439 -134006
- package/wrangler-dist/wasm-sync.wasm +0 -0
- package/import_meta_url.js +0 -3
- package/miniflare-config-stubs/.env.empty +0 -0
- package/miniflare-config-stubs/package.empty.json +0 -1
- package/miniflare-config-stubs/wrangler.empty.toml +0 -0
- package/miniflare-dist/index.mjs +0 -79
- package/miniflare-dist/index.mjs.map +0 -7
- 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__/configuration.test.ts +0 -2796
- package/src/__tests__/dev.test.tsx +0 -496
- package/src/__tests__/dev2.test.tsx +0 -154
- package/src/__tests__/guess-worker-format.test.ts +0 -58
- package/src/__tests__/helpers/clipboardy-mock.js +0 -4
- package/src/__tests__/helpers/cmd-shim.d.ts +0 -11
- package/src/__tests__/helpers/faye-websocket.d.ts +0 -6
- package/src/__tests__/helpers/mock-account-id.ts +0 -48
- package/src/__tests__/helpers/mock-bin.ts +0 -36
- package/src/__tests__/helpers/mock-cfetch.ts +0 -207
- package/src/__tests__/helpers/mock-console.ts +0 -70
- package/src/__tests__/helpers/mock-dialogs.ts +0 -65
- package/src/__tests__/helpers/mock-kv.ts +0 -33
- package/src/__tests__/helpers/mock-stdin.ts +0 -103
- package/src/__tests__/helpers/mock-web-socket.ts +0 -29
- package/src/__tests__/helpers/run-in-tmp.ts +0 -32
- package/src/__tests__/helpers/run-wrangler.ts +0 -16
- package/src/__tests__/helpers/write-wrangler-toml.ts +0 -17
- package/src/__tests__/https-options.test.ts +0 -127
- package/src/__tests__/index.test.ts +0 -991
- package/src/__tests__/jest.setup.ts +0 -69
- package/src/__tests__/kv.test.ts +0 -1382
- package/src/__tests__/logout.test.ts +0 -52
- package/src/__tests__/package-manager.test.ts +0 -206
- package/src/__tests__/pages.test.ts +0 -44
- package/src/__tests__/parse.test.ts +0 -322
- package/src/__tests__/publish.test.ts +0 -2704
- package/src/__tests__/r2.test.ts +0 -198
- package/src/__tests__/route.test.ts +0 -45
- package/src/__tests__/secret.test.ts +0 -425
- package/src/__tests__/sentry.test.ts +0 -146
- package/src/__tests__/tail.test.ts +0 -564
- package/src/__tests__/whoami.test.tsx +0 -100
- package/src/bundle.ts +0 -142
- package/src/cfetch/index.ts +0 -104
- package/src/cfetch/internal.ts +0 -139
- package/src/cli.ts +0 -19
- package/src/config/README.md +0 -107
- package/src/config/config.ts +0 -218
- package/src/config/diagnostics.ts +0 -80
- package/src/config/environment.ts +0 -366
- package/src/config/index.ts +0 -62
- package/src/config/validation-helpers.ts +0 -486
- package/src/config/validation.ts +0 -1090
- package/src/create-worker-preview.ts +0 -166
- package/src/create-worker-upload-form.ts +0 -217
- package/src/dev/dev.tsx +0 -364
- package/src/dev/local.tsx +0 -232
- package/src/dev/remote.tsx +0 -203
- package/src/dev/use-esbuild.ts +0 -100
- package/src/dialogs.tsx +0 -77
- package/src/entry.ts +0 -208
- package/src/environment-variables.ts +0 -33
- package/src/https-options.ts +0 -122
- package/src/index.tsx +0 -2373
- package/src/inspect.ts +0 -617
- package/src/kv.ts +0 -319
- package/src/miniflare-cli/README.md +0 -30
- package/src/miniflare-cli/enum-keys.ts +0 -17
- package/src/miniflare-cli/index.ts +0 -37
- package/src/module-collection.ts +0 -258
- package/src/open-in-browser.ts +0 -25
- package/src/package-manager.ts +0 -126
- package/src/pages.tsx +0 -1047
- package/src/parse.ts +0 -183
- package/src/paths.ts +0 -26
- package/src/proxy.ts +0 -440
- package/src/publish.ts +0 -347
- package/src/r2.ts +0 -50
- package/src/reporting.ts +0 -131
- package/src/selfsigned.d.ts +0 -29
- package/src/sites.tsx +0 -280
- package/src/tail/filters.ts +0 -279
- package/src/tail/index.ts +0 -299
- package/src/tail/printing.ts +0 -62
- package/src/user.tsx +0 -1118
- package/src/whoami.tsx +0 -69
- package/src/worker.ts +0 -163
- 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
|
@@ -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
|
-
}
|