wrangler 0.0.0-e978986 → 0.0.0-e9e9872

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 (186) hide show
  1. package/README.md +7 -1
  2. package/bin/wrangler.js +114 -57
  3. package/kv-asset-handler.js +1 -0
  4. package/miniflare-dist/index.mjs +10 -2
  5. package/package.json +159 -152
  6. package/src/__tests__/config-cache-without-cache-dir.test.ts +38 -0
  7. package/src/__tests__/config-cache.test.ts +30 -24
  8. package/src/__tests__/configuration.test.ts +3935 -3363
  9. package/src/__tests__/dev.test.tsx +1128 -949
  10. package/src/__tests__/guess-worker-format.test.ts +68 -68
  11. package/src/__tests__/helpers/cmd-shim.d.ts +6 -6
  12. package/src/__tests__/helpers/mock-account-id.ts +24 -24
  13. package/src/__tests__/helpers/mock-bin.ts +20 -20
  14. package/src/__tests__/helpers/mock-cfetch.ts +92 -92
  15. package/src/__tests__/helpers/mock-console.ts +49 -39
  16. package/src/__tests__/helpers/mock-dialogs.ts +94 -71
  17. package/src/__tests__/helpers/mock-http-server.ts +30 -30
  18. package/src/__tests__/helpers/mock-istty.ts +65 -18
  19. package/src/__tests__/helpers/mock-kv.ts +26 -26
  20. package/src/__tests__/helpers/mock-oauth-flow.ts +223 -228
  21. package/src/__tests__/helpers/mock-process.ts +39 -0
  22. package/src/__tests__/helpers/mock-stdin.ts +82 -77
  23. package/src/__tests__/helpers/mock-web-socket.ts +21 -21
  24. package/src/__tests__/helpers/run-in-tmp.ts +27 -28
  25. package/src/__tests__/helpers/run-wrangler.ts +8 -8
  26. package/src/__tests__/helpers/write-worker-source.ts +16 -16
  27. package/src/__tests__/helpers/write-wrangler-toml.ts +9 -9
  28. package/src/__tests__/https-options.test.ts +142 -109
  29. package/src/__tests__/index.test.ts +239 -233
  30. package/src/__tests__/init.test.ts +1605 -1180
  31. package/src/__tests__/jest.setup.ts +76 -33
  32. package/src/__tests__/kv.test.ts +1160 -991
  33. package/src/__tests__/logger.test.ts +100 -74
  34. package/src/__tests__/package-manager.test.ts +303 -303
  35. package/src/__tests__/pages.test.ts +1160 -563
  36. package/src/__tests__/parse.test.ts +252 -252
  37. package/src/__tests__/publish.test.ts +6426 -5299
  38. package/src/__tests__/pubsub.test.ts +367 -0
  39. package/src/__tests__/r2.test.ts +133 -133
  40. package/src/__tests__/route.test.ts +18 -18
  41. package/src/__tests__/secret.test.ts +470 -459
  42. package/src/__tests__/tail.test.ts +538 -530
  43. package/src/__tests__/user.test.ts +124 -112
  44. package/src/__tests__/whoami.test.tsx +208 -83
  45. package/src/__tests__/worker-namespace.test.ts +327 -0
  46. package/src/abort.d.ts +1 -1
  47. package/src/api/dev.ts +52 -0
  48. package/src/api/index.ts +1 -0
  49. package/src/bundle-reporter.tsx +29 -0
  50. package/src/bundle.ts +158 -146
  51. package/src/cfetch/index.ts +80 -80
  52. package/src/cfetch/internal.ts +92 -88
  53. package/src/cli.ts +21 -7
  54. package/src/config/config.ts +204 -195
  55. package/src/config/diagnostics.ts +61 -61
  56. package/src/config/environment.ts +390 -357
  57. package/src/config/index.ts +206 -193
  58. package/src/config/validation-helpers.ts +366 -357
  59. package/src/config/validation.ts +1573 -1367
  60. package/src/config-cache.ts +79 -41
  61. package/src/create-worker-preview.ts +206 -136
  62. package/src/create-worker-upload-form.ts +247 -238
  63. package/src/dev/dev-vars.ts +13 -13
  64. package/src/dev/dev.tsx +334 -336
  65. package/src/dev/local.tsx +310 -274
  66. package/src/dev/remote.tsx +366 -211
  67. package/src/dev/use-esbuild.ts +126 -94
  68. package/src/dev.tsx +538 -0
  69. package/src/dialogs.tsx +97 -97
  70. package/src/durable.ts +87 -87
  71. package/src/entry.ts +234 -228
  72. package/src/environment-variables.ts +23 -23
  73. package/src/errors.ts +6 -6
  74. package/src/faye-websocket.d.ts +6 -0
  75. package/src/generate.ts +33 -0
  76. package/src/git-client.ts +42 -0
  77. package/src/global-wrangler-config-path.ts +26 -0
  78. package/src/https-options.ts +84 -80
  79. package/src/index.tsx +1800 -2752
  80. package/src/init.ts +549 -0
  81. package/src/inspect.ts +593 -593
  82. package/src/intl-polyfill.d.ts +123 -123
  83. package/src/is-interactive.ts +12 -0
  84. package/src/kv.ts +299 -277
  85. package/src/logger.ts +46 -39
  86. package/src/miniflare-cli/enum-keys.ts +8 -8
  87. package/src/miniflare-cli/index.ts +43 -31
  88. package/src/miniflare-cli/request-context.ts +18 -18
  89. package/src/module-collection.ts +212 -212
  90. package/src/open-in-browser.ts +4 -6
  91. package/src/package-manager.ts +173 -128
  92. package/src/pages/build.tsx +202 -0
  93. package/src/pages/constants.ts +7 -0
  94. package/src/pages/deployments.tsx +101 -0
  95. package/src/pages/dev.tsx +964 -0
  96. package/src/pages/functions/buildPlugin.ts +105 -0
  97. package/src/pages/functions/buildWorker.ts +151 -0
  98. package/{pages → src/pages}/functions/filepath-routing.test.ts +113 -113
  99. package/src/pages/functions/filepath-routing.ts +189 -0
  100. package/src/pages/functions/identifiers.ts +78 -0
  101. package/src/pages/functions/routes.ts +151 -0
  102. package/src/pages/index.tsx +84 -0
  103. package/src/pages/projects.tsx +157 -0
  104. package/src/pages/publish.tsx +335 -0
  105. package/src/pages/types.ts +40 -0
  106. package/src/pages/upload.tsx +396 -0
  107. package/src/pages/utils.ts +12 -0
  108. package/src/parse.ts +209 -125
  109. package/src/paths.ts +6 -6
  110. package/src/preview.ts +31 -0
  111. package/src/proxy.ts +400 -389
  112. package/src/publish.ts +702 -505
  113. package/src/pubsub/index.ts +286 -0
  114. package/src/pubsub/pubsub-commands.tsx +577 -0
  115. package/src/r2.ts +19 -19
  116. package/src/selfsigned.d.ts +23 -23
  117. package/src/sites.tsx +278 -201
  118. package/src/tail/filters.ts +108 -108
  119. package/src/tail/index.ts +217 -217
  120. package/src/tail/printing.ts +45 -45
  121. package/src/update-check.ts +11 -11
  122. package/src/user/choose-account.tsx +60 -0
  123. package/src/user/env-vars.ts +46 -0
  124. package/src/user/generate-auth-url.ts +33 -0
  125. package/src/user/generate-random-state.ts +16 -0
  126. package/src/user/index.ts +3 -0
  127. package/src/user/user.tsx +1170 -0
  128. package/src/whoami.tsx +61 -41
  129. package/src/worker-namespace.ts +190 -0
  130. package/src/worker.ts +110 -99
  131. package/src/zones.ts +76 -0
  132. package/templates/checked-fetch.js +17 -0
  133. package/templates/new-worker-scheduled.js +4 -4
  134. package/templates/new-worker-scheduled.ts +16 -16
  135. package/templates/new-worker.js +3 -3
  136. package/templates/new-worker.ts +15 -15
  137. package/templates/no-op-worker.js +10 -0
  138. package/templates/pages-template-plugin.ts +155 -0
  139. package/templates/pages-template-worker.ts +161 -0
  140. package/templates/static-asset-facade.js +38 -36
  141. package/templates/tsconfig.json +95 -95
  142. package/wrangler-dist/cli.d.ts +298 -0
  143. package/wrangler-dist/cli.js +33496 -31370
  144. package/pages/functions/buildPlugin.ts +0 -105
  145. package/pages/functions/buildWorker.ts +0 -151
  146. package/pages/functions/filepath-routing.ts +0 -189
  147. package/pages/functions/identifiers.ts +0 -78
  148. package/pages/functions/routes.ts +0 -156
  149. package/pages/functions/template-plugin.ts +0 -147
  150. package/pages/functions/template-worker.ts +0 -143
  151. package/src/__tests__/helpers/faye-websocket.d.ts +0 -6
  152. package/src/pages.tsx +0 -2027
  153. package/src/user.tsx +0 -1203
  154. package/vendor/@cloudflare/kv-asset-handler/CHANGELOG.md +0 -332
  155. package/vendor/@cloudflare/kv-asset-handler/LICENSE_APACHE +0 -176
  156. package/vendor/@cloudflare/kv-asset-handler/LICENSE_MIT +0 -25
  157. package/vendor/@cloudflare/kv-asset-handler/README.md +0 -245
  158. package/vendor/@cloudflare/kv-asset-handler/dist/index.d.ts +0 -32
  159. package/vendor/@cloudflare/kv-asset-handler/dist/index.js +0 -354
  160. package/vendor/@cloudflare/kv-asset-handler/dist/mocks.d.ts +0 -13
  161. package/vendor/@cloudflare/kv-asset-handler/dist/mocks.js +0 -148
  162. package/vendor/@cloudflare/kv-asset-handler/dist/test/getAssetFromKV.d.ts +0 -1
  163. package/vendor/@cloudflare/kv-asset-handler/dist/test/getAssetFromKV.js +0 -436
  164. package/vendor/@cloudflare/kv-asset-handler/dist/test/mapRequestToAsset.d.ts +0 -1
  165. package/vendor/@cloudflare/kv-asset-handler/dist/test/mapRequestToAsset.js +0 -40
  166. package/vendor/@cloudflare/kv-asset-handler/dist/test/serveSinglePageApp.d.ts +0 -1
  167. package/vendor/@cloudflare/kv-asset-handler/dist/test/serveSinglePageApp.js +0 -42
  168. package/vendor/@cloudflare/kv-asset-handler/dist/types.d.ts +0 -26
  169. package/vendor/@cloudflare/kv-asset-handler/dist/types.js +0 -31
  170. package/vendor/@cloudflare/kv-asset-handler/package.json +0 -52
  171. package/vendor/@cloudflare/kv-asset-handler/src/index.ts +0 -296
  172. package/vendor/@cloudflare/kv-asset-handler/src/mocks.ts +0 -136
  173. package/vendor/@cloudflare/kv-asset-handler/src/test/getAssetFromKV.ts +0 -464
  174. package/vendor/@cloudflare/kv-asset-handler/src/test/mapRequestToAsset.ts +0 -33
  175. package/vendor/@cloudflare/kv-asset-handler/src/test/serveSinglePageApp.ts +0 -42
  176. package/vendor/@cloudflare/kv-asset-handler/src/types.ts +0 -39
  177. package/vendor/wrangler-mime/CHANGELOG.md +0 -289
  178. package/vendor/wrangler-mime/LICENSE +0 -21
  179. package/vendor/wrangler-mime/Mime.js +0 -97
  180. package/vendor/wrangler-mime/README.md +0 -187
  181. package/vendor/wrangler-mime/cli.js +0 -46
  182. package/vendor/wrangler-mime/index.js +0 -4
  183. package/vendor/wrangler-mime/lite.js +0 -4
  184. package/vendor/wrangler-mime/package.json +0 -52
  185. package/vendor/wrangler-mime/types/other.js +0 -1
  186. package/vendor/wrangler-mime/types/standard.js +0 -1
@@ -1,148 +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
- exports.sleep = exports.mockGlobal = exports.mockCaches = exports.mockManifest = exports.mockKV = exports.getEvent = void 0;
13
- const makeServiceWorkerEnv = require('service-worker-mock');
14
- const HASH = '123HASHBROWN';
15
- const getEvent = (request) => {
16
- const waitUntil = (callback) => __awaiter(void 0, void 0, void 0, function* () {
17
- yield callback;
18
- });
19
- return {
20
- request,
21
- waitUntil,
22
- };
23
- };
24
- exports.getEvent = getEvent;
25
- const store = {
26
- 'key1.123HASHBROWN.txt': 'val1',
27
- 'key1.123HASHBROWN.png': 'val1',
28
- 'index.123HASHBROWN.html': 'index.html',
29
- 'cache.123HASHBROWN.html': 'cache me if you can',
30
- '测试.123HASHBROWN.html': 'My filename is non-ascii',
31
- '%not-really-percent-encoded.123HASHBROWN.html': 'browser percent encoded',
32
- '%2F.123HASHBROWN.html': 'user percent encoded',
33
- '你好.123HASHBROWN.html': 'I shouldnt be served',
34
- '%E4%BD%A0%E5%A5%BD.123HASHBROWN.html': 'Im important',
35
- 'nohash.txt': 'no hash but still got some result',
36
- 'sub/blah.123HASHBROWN.png': 'picturedis',
37
- 'sub/index.123HASHBROWN.html': 'picturedis',
38
- 'client.123HASHBROWN': 'important file',
39
- 'client.123HASHBROWN/index.html': 'Im here but serve my big bro above',
40
- '你好/index.123HASHBROWN.html': 'My path is non-ascii',
41
- };
42
- const mockKV = (store) => {
43
- return {
44
- get: (path) => store[path] || null,
45
- };
46
- };
47
- exports.mockKV = mockKV;
48
- const mockManifest = () => {
49
- return JSON.stringify({
50
- 'key1.txt': `key1.${HASH}.txt`,
51
- 'key1.png': `key1.${HASH}.png`,
52
- 'cache.html': `cache.${HASH}.html`,
53
- '测试.html': `测试.${HASH}.html`,
54
- '你好.html': `你好.${HASH}.html`,
55
- '%not-really-percent-encoded.html': `%not-really-percent-encoded.${HASH}.html`,
56
- '%2F.html': `%2F.${HASH}.html`,
57
- '%E4%BD%A0%E5%A5%BD.html': `%E4%BD%A0%E5%A5%BD.${HASH}.html`,
58
- 'index.html': `index.${HASH}.html`,
59
- 'sub/blah.png': `sub/blah.${HASH}.png`,
60
- 'sub/index.html': `sub/index.${HASH}.html`,
61
- client: `client.${HASH}`,
62
- 'client/index.html': `client.${HASH}`,
63
- '你好/index.html': `你好/index.${HASH}.html`,
64
- });
65
- };
66
- exports.mockManifest = mockManifest;
67
- let cacheStore = new Map();
68
- const mockCaches = () => {
69
- return {
70
- default: {
71
- match(key) {
72
- return __awaiter(this, void 0, void 0, function* () {
73
- let cacheKey = {
74
- url: key.url,
75
- headers: {},
76
- };
77
- let response;
78
- if (key.headers.has('if-none-match')) {
79
- let makeStrongEtag = key.headers.get('if-none-match').replace('W/', '');
80
- Reflect.set(cacheKey.headers, 'etag', makeStrongEtag);
81
- response = cacheStore.get(JSON.stringify(cacheKey));
82
- }
83
- else {
84
- // if client doesn't send if-none-match, we need to iterate through these keys
85
- // and just test the URL
86
- const activeCacheKeys = Array.from(cacheStore.keys());
87
- for (const cacheStoreKey of activeCacheKeys) {
88
- if (JSON.parse(cacheStoreKey).url === key.url) {
89
- response = cacheStore.get(cacheStoreKey);
90
- }
91
- }
92
- }
93
- // TODO: write test to accomodate for rare scenarios with where range requests accomodate etags
94
- if (response && !key.headers.has('if-none-match')) {
95
- // this appears overly verbose, but is necessary to document edge cache behavior
96
- // The Range request header triggers the response header Content-Range ...
97
- const range = key.headers.get('range');
98
- if (range) {
99
- response.headers.set('content-range', `bytes ${range.split('=').pop()}/${response.headers.get('content-length')}`);
100
- }
101
- // ... which we are using in this repository to set status 206
102
- if (response.headers.has('content-range')) {
103
- response.status = 206;
104
- }
105
- else {
106
- response.status = 200;
107
- }
108
- let etag = response.headers.get('etag');
109
- if (etag && !etag.includes('W/')) {
110
- response.headers.set('etag', `W/${etag}`);
111
- }
112
- }
113
- return response;
114
- });
115
- },
116
- put(key, val) {
117
- return __awaiter(this, void 0, void 0, function* () {
118
- let headers = new Headers(val.headers);
119
- let url = new URL(key.url);
120
- let resWithBody = new Response(val.body, { headers, status: 200 });
121
- let resNoBody = new Response(null, { headers, status: 304 });
122
- let cacheKey = {
123
- url: key.url,
124
- headers: {
125
- etag: `"${url.pathname.replace('/', '')}"`,
126
- },
127
- };
128
- cacheStore.set(JSON.stringify(cacheKey), resNoBody);
129
- cacheKey.headers = {};
130
- cacheStore.set(JSON.stringify(cacheKey), resWithBody);
131
- return;
132
- });
133
- },
134
- },
135
- };
136
- };
137
- exports.mockCaches = mockCaches;
138
- function mockGlobal() {
139
- Object.assign(global, makeServiceWorkerEnv());
140
- Object.assign(global, { __STATIC_CONTENT_MANIFEST: exports.mockManifest() });
141
- Object.assign(global, { __STATIC_CONTENT: exports.mockKV(store) });
142
- Object.assign(global, { caches: exports.mockCaches() });
143
- }
144
- exports.mockGlobal = mockGlobal;
145
- const sleep = (milliseconds) => {
146
- return new Promise((resolve) => setTimeout(resolve, milliseconds));
147
- };
148
- exports.sleep = sleep;
@@ -1,436 +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
- ava_1.default('getAssetFromKV return correct val from KV and default caching', (t) => __awaiter(void 0, void 0, void 0, function* () {
16
- mocks_1.mockGlobal();
17
- const event = mocks_1.getEvent(new Request('https://blah.com/key1.txt'));
18
- const res = yield index_1.getAssetFromKV(event);
19
- if (res) {
20
- t.is(res.headers.get('cache-control'), null);
21
- t.is(res.headers.get('cf-cache-status'), 'MISS');
22
- t.is(yield res.text(), 'val1');
23
- t.true(res.headers.get('content-type').includes('text'));
24
- }
25
- else {
26
- t.fail('Response was undefined');
27
- }
28
- }));
29
- ava_1.default('getAssetFromKV evaluated the file matching the extensionless path first /client/ -> client', (t) => __awaiter(void 0, void 0, void 0, function* () {
30
- mocks_1.mockGlobal();
31
- const event = mocks_1.getEvent(new Request(`https://foo.com/client/`));
32
- const res = yield index_1.getAssetFromKV(event);
33
- t.is(yield res.text(), 'important file');
34
- t.true(res.headers.get('content-type').includes('text'));
35
- }));
36
- ava_1.default('getAssetFromKV evaluated the file matching the extensionless path first /client -> client', (t) => __awaiter(void 0, void 0, void 0, function* () {
37
- mocks_1.mockGlobal();
38
- const event = mocks_1.getEvent(new Request(`https://foo.com/client`));
39
- const res = yield index_1.getAssetFromKV(event);
40
- t.is(yield res.text(), 'important file');
41
- t.true(res.headers.get('content-type').includes('text'));
42
- }));
43
- ava_1.default('getAssetFromKV if not in asset manifest still returns nohash.txt', (t) => __awaiter(void 0, void 0, void 0, function* () {
44
- mocks_1.mockGlobal();
45
- const event = mocks_1.getEvent(new Request('https://blah.com/nohash.txt'));
46
- const res = yield index_1.getAssetFromKV(event);
47
- if (res) {
48
- t.is(yield res.text(), 'no hash but still got some result');
49
- t.true(res.headers.get('content-type').includes('text'));
50
- }
51
- else {
52
- t.fail('Response was undefined');
53
- }
54
- }));
55
- ava_1.default('getAssetFromKV if no asset manifest /client -> client fails', (t) => __awaiter(void 0, void 0, void 0, function* () {
56
- mocks_1.mockGlobal();
57
- const event = mocks_1.getEvent(new Request(`https://foo.com/client`));
58
- const error = yield t.throwsAsync(index_1.getAssetFromKV(event, { ASSET_MANIFEST: {} }));
59
- t.is(error.status, 404);
60
- }));
61
- ava_1.default('getAssetFromKV if sub/ -> sub/index.html served', (t) => __awaiter(void 0, void 0, void 0, function* () {
62
- mocks_1.mockGlobal();
63
- const event = mocks_1.getEvent(new Request(`https://foo.com/sub`));
64
- const res = yield index_1.getAssetFromKV(event);
65
- if (res) {
66
- t.is(yield res.text(), 'picturedis');
67
- }
68
- else {
69
- t.fail('Response was undefined');
70
- }
71
- }));
72
- ava_1.default('getAssetFromKV gets index.html by default for / requests', (t) => __awaiter(void 0, void 0, void 0, function* () {
73
- mocks_1.mockGlobal();
74
- const event = mocks_1.getEvent(new Request('https://blah.com/'));
75
- const res = yield index_1.getAssetFromKV(event);
76
- if (res) {
77
- t.is(yield res.text(), 'index.html');
78
- t.true(res.headers.get('content-type').includes('html'));
79
- }
80
- else {
81
- t.fail('Response was undefined');
82
- }
83
- }));
84
- ava_1.default('getAssetFromKV non ASCII path support', (t) => __awaiter(void 0, void 0, void 0, function* () {
85
- mocks_1.mockGlobal();
86
- const event = mocks_1.getEvent(new Request('https://blah.com/测试.html'));
87
- const res = yield index_1.getAssetFromKV(event);
88
- if (res) {
89
- t.is(yield res.text(), 'My filename is non-ascii');
90
- }
91
- else {
92
- t.fail('Response was undefined');
93
- }
94
- }));
95
- ava_1.default('getAssetFromKV supports browser percent encoded URLs', (t) => __awaiter(void 0, void 0, void 0, function* () {
96
- mocks_1.mockGlobal();
97
- const event = mocks_1.getEvent(new Request('https://example.com/%not-really-percent-encoded.html'));
98
- const res = yield index_1.getAssetFromKV(event);
99
- if (res) {
100
- t.is(yield res.text(), 'browser percent encoded');
101
- }
102
- else {
103
- t.fail('Response was undefined');
104
- }
105
- }));
106
- ava_1.default('getAssetFromKV supports user percent encoded URLs', (t) => __awaiter(void 0, void 0, void 0, function* () {
107
- mocks_1.mockGlobal();
108
- const event = mocks_1.getEvent(new Request('https://blah.com/%2F.html'));
109
- const res = yield index_1.getAssetFromKV(event);
110
- if (res) {
111
- t.is(yield res.text(), 'user percent encoded');
112
- }
113
- else {
114
- t.fail('Response was undefined');
115
- }
116
- }));
117
- ava_1.default('getAssetFromKV only decode URL when necessary', (t) => __awaiter(void 0, void 0, void 0, function* () {
118
- mocks_1.mockGlobal();
119
- const event1 = mocks_1.getEvent(new Request('https://blah.com/%E4%BD%A0%E5%A5%BD.html'));
120
- const event2 = mocks_1.getEvent(new Request('https://blah.com/你好.html'));
121
- const res1 = yield index_1.getAssetFromKV(event1);
122
- const res2 = yield index_1.getAssetFromKV(event2);
123
- if (res1 && res2) {
124
- t.is(yield res1.text(), 'Im important');
125
- t.is(yield res2.text(), 'Im important');
126
- }
127
- else {
128
- t.fail('Response was undefined');
129
- }
130
- }));
131
- ava_1.default('getAssetFromKV Support for user decode url path', (t) => __awaiter(void 0, void 0, void 0, function* () {
132
- mocks_1.mockGlobal();
133
- const event1 = mocks_1.getEvent(new Request('https://blah.com/%E4%BD%A0%E5%A5%BD/'));
134
- const event2 = mocks_1.getEvent(new Request('https://blah.com/你好/'));
135
- const res1 = yield index_1.getAssetFromKV(event1);
136
- const res2 = yield index_1.getAssetFromKV(event2);
137
- if (res1 && res2) {
138
- t.is(yield res1.text(), 'My path is non-ascii');
139
- t.is(yield res2.text(), 'My path is non-ascii');
140
- }
141
- else {
142
- t.fail('Response was undefined');
143
- }
144
- }));
145
- ava_1.default('getAssetFromKV custom key modifier', (t) => __awaiter(void 0, void 0, void 0, function* () {
146
- mocks_1.mockGlobal();
147
- const event = mocks_1.getEvent(new Request('https://blah.com/docs/sub/blah.png'));
148
- const customRequestMapper = (request) => {
149
- let defaultModifiedRequest = index_1.mapRequestToAsset(request);
150
- let url = new URL(defaultModifiedRequest.url);
151
- url.pathname = url.pathname.replace('/docs', '');
152
- return new Request(url.toString(), request);
153
- };
154
- const res = yield index_1.getAssetFromKV(event, { mapRequestToAsset: customRequestMapper });
155
- if (res) {
156
- t.is(yield res.text(), 'picturedis');
157
- }
158
- else {
159
- t.fail('Response was undefined');
160
- }
161
- }));
162
- ava_1.default('getAssetFromKV when setting browser caching', (t) => __awaiter(void 0, void 0, void 0, function* () {
163
- mocks_1.mockGlobal();
164
- const event = mocks_1.getEvent(new Request('https://blah.com/'));
165
- const res = yield index_1.getAssetFromKV(event, { cacheControl: { browserTTL: 22 } });
166
- if (res) {
167
- t.is(res.headers.get('cache-control'), 'max-age=22');
168
- }
169
- else {
170
- t.fail('Response was undefined');
171
- }
172
- }));
173
- ava_1.default('getAssetFromKV when setting custom cache setting', (t) => __awaiter(void 0, void 0, void 0, function* () {
174
- mocks_1.mockGlobal();
175
- const event1 = mocks_1.getEvent(new Request('https://blah.com/'));
176
- const event2 = mocks_1.getEvent(new Request('https://blah.com/key1.png?blah=34'));
177
- const cacheOnlyPngs = (req) => {
178
- if (new URL(req.url).pathname.endsWith('.png'))
179
- return {
180
- browserTTL: 720,
181
- edgeTTL: 720,
182
- };
183
- else
184
- return {
185
- bypassCache: true,
186
- };
187
- };
188
- const res1 = yield index_1.getAssetFromKV(event1, { cacheControl: cacheOnlyPngs });
189
- const res2 = yield index_1.getAssetFromKV(event2, { cacheControl: cacheOnlyPngs });
190
- if (res1 && res2) {
191
- t.is(res1.headers.get('cache-control'), null);
192
- t.true(res2.headers.get('content-type').includes('png'));
193
- t.is(res2.headers.get('cache-control'), 'max-age=720');
194
- t.is(res2.headers.get('cf-cache-status'), 'MISS');
195
- }
196
- else {
197
- t.fail('Response was undefined');
198
- }
199
- }));
200
- ava_1.default('getAssetFromKV caches on two sequential requests', (t) => __awaiter(void 0, void 0, void 0, function* () {
201
- mocks_1.mockGlobal();
202
- const resourceKey = 'cache.html';
203
- const resourceVersion = JSON.parse(mocks_1.mockManifest())[resourceKey];
204
- const event1 = mocks_1.getEvent(new Request(`https://blah.com/${resourceKey}`));
205
- const event2 = mocks_1.getEvent(new Request(`https://blah.com/${resourceKey}`, {
206
- headers: {
207
- 'if-none-match': `"${resourceVersion}"`,
208
- },
209
- }));
210
- const res1 = yield index_1.getAssetFromKV(event1, { cacheControl: { edgeTTL: 720, browserTTL: 720 } });
211
- yield mocks_1.sleep(1);
212
- const res2 = yield index_1.getAssetFromKV(event2);
213
- if (res1 && res2) {
214
- t.is(res1.headers.get('cf-cache-status'), 'MISS');
215
- t.is(res1.headers.get('cache-control'), 'max-age=720');
216
- t.is(res2.headers.get('cf-cache-status'), 'REVALIDATED');
217
- }
218
- else {
219
- t.fail('Response was undefined');
220
- }
221
- }));
222
- ava_1.default('getAssetFromKV does not store max-age on two sequential requests', (t) => __awaiter(void 0, void 0, void 0, function* () {
223
- mocks_1.mockGlobal();
224
- const resourceKey = 'cache.html';
225
- const resourceVersion = JSON.parse(mocks_1.mockManifest())[resourceKey];
226
- const event1 = mocks_1.getEvent(new Request(`https://blah.com/${resourceKey}`));
227
- const event2 = mocks_1.getEvent(new Request(`https://blah.com/${resourceKey}`, {
228
- headers: {
229
- 'if-none-match': `"${resourceVersion}"`,
230
- },
231
- }));
232
- const res1 = yield index_1.getAssetFromKV(event1, { cacheControl: { edgeTTL: 720 } });
233
- yield mocks_1.sleep(100);
234
- const res2 = yield index_1.getAssetFromKV(event2);
235
- if (res1 && res2) {
236
- t.is(res1.headers.get('cf-cache-status'), 'MISS');
237
- t.is(res1.headers.get('cache-control'), null);
238
- t.is(res2.headers.get('cf-cache-status'), 'REVALIDATED');
239
- t.is(res2.headers.get('cache-control'), null);
240
- }
241
- else {
242
- t.fail('Response was undefined');
243
- }
244
- }));
245
- ava_1.default('getAssetFromKV does not cache on Cloudflare when bypass cache set', (t) => __awaiter(void 0, void 0, void 0, function* () {
246
- mocks_1.mockGlobal();
247
- const event = mocks_1.getEvent(new Request('https://blah.com/'));
248
- const res = yield index_1.getAssetFromKV(event, { cacheControl: { bypassCache: true } });
249
- if (res) {
250
- t.is(res.headers.get('cache-control'), null);
251
- t.is(res.headers.get('cf-cache-status'), null);
252
- }
253
- else {
254
- t.fail('Response was undefined');
255
- }
256
- }));
257
- ava_1.default('getAssetFromKV with no trailing slash on root', (t) => __awaiter(void 0, void 0, void 0, function* () {
258
- mocks_1.mockGlobal();
259
- const event = mocks_1.getEvent(new Request('https://blah.com'));
260
- const res = yield index_1.getAssetFromKV(event);
261
- if (res) {
262
- t.is(yield res.text(), 'index.html');
263
- }
264
- else {
265
- t.fail('Response was undefined');
266
- }
267
- }));
268
- ava_1.default('getAssetFromKV with no trailing slash on a subdirectory', (t) => __awaiter(void 0, void 0, void 0, function* () {
269
- mocks_1.mockGlobal();
270
- const event = mocks_1.getEvent(new Request('https://blah.com/sub/blah.png'));
271
- const res = yield index_1.getAssetFromKV(event);
272
- if (res) {
273
- t.is(yield res.text(), 'picturedis');
274
- }
275
- else {
276
- t.fail('Response was undefined');
277
- }
278
- }));
279
- ava_1.default('getAssetFromKV no result throws an error', (t) => __awaiter(void 0, void 0, void 0, function* () {
280
- mocks_1.mockGlobal();
281
- const event = mocks_1.getEvent(new Request('https://blah.com/random'));
282
- const error = yield t.throwsAsync(index_1.getAssetFromKV(event));
283
- t.is(error.status, 404);
284
- }));
285
- ava_1.default('getAssetFromKV TTls set to null should not cache on browser or edge', (t) => __awaiter(void 0, void 0, void 0, function* () {
286
- mocks_1.mockGlobal();
287
- const event = mocks_1.getEvent(new Request('https://blah.com/'));
288
- const res1 = yield index_1.getAssetFromKV(event, { cacheControl: { browserTTL: null, edgeTTL: null } });
289
- yield mocks_1.sleep(100);
290
- const res2 = yield index_1.getAssetFromKV(event, { cacheControl: { browserTTL: null, edgeTTL: null } });
291
- if (res1 && res2) {
292
- t.is(res1.headers.get('cf-cache-status'), null);
293
- t.is(res1.headers.get('cache-control'), null);
294
- t.is(res2.headers.get('cf-cache-status'), null);
295
- t.is(res2.headers.get('cache-control'), null);
296
- }
297
- else {
298
- t.fail('Response was undefined');
299
- }
300
- }));
301
- ava_1.default('getAssetFromKV passing in a custom NAMESPACE serves correct asset', (t) => __awaiter(void 0, void 0, void 0, function* () {
302
- mocks_1.mockGlobal();
303
- let CUSTOM_NAMESPACE = mocks_1.mockKV({
304
- 'key1.123HASHBROWN.txt': 'val1',
305
- });
306
- Object.assign(global, { CUSTOM_NAMESPACE });
307
- const event = mocks_1.getEvent(new Request('https://blah.com/'));
308
- const res = yield index_1.getAssetFromKV(event);
309
- if (res) {
310
- t.is(yield res.text(), 'index.html');
311
- t.true(res.headers.get('content-type').includes('html'));
312
- }
313
- else {
314
- t.fail('Response was undefined');
315
- }
316
- }));
317
- ava_1.default('getAssetFromKV when custom namespace without the asset should fail', (t) => __awaiter(void 0, void 0, void 0, function* () {
318
- mocks_1.mockGlobal();
319
- let CUSTOM_NAMESPACE = mocks_1.mockKV({
320
- 'key5.123HASHBROWN.txt': 'customvalu',
321
- });
322
- const event = mocks_1.getEvent(new Request('https://blah.com'));
323
- const error = yield t.throwsAsync(index_1.getAssetFromKV(event, { ASSET_NAMESPACE: CUSTOM_NAMESPACE }));
324
- t.is(error.status, 404);
325
- }));
326
- ava_1.default('getAssetFromKV when namespace not bound fails', (t) => __awaiter(void 0, void 0, void 0, function* () {
327
- mocks_1.mockGlobal();
328
- var MY_CUSTOM_NAMESPACE = undefined;
329
- Object.assign(global, { MY_CUSTOM_NAMESPACE });
330
- const event = mocks_1.getEvent(new Request('https://blah.com/'));
331
- const error = yield t.throwsAsync(index_1.getAssetFromKV(event, { ASSET_NAMESPACE: MY_CUSTOM_NAMESPACE }));
332
- t.is(error.status, 500);
333
- }));
334
- ava_1.default('getAssetFromKV when if-none-match === active resource version, should revalidate', (t) => __awaiter(void 0, void 0, void 0, function* () {
335
- mocks_1.mockGlobal();
336
- const resourceKey = 'key1.png';
337
- const resourceVersion = JSON.parse(mocks_1.mockManifest())[resourceKey];
338
- const event1 = mocks_1.getEvent(new Request(`https://blah.com/${resourceKey}`));
339
- const event2 = mocks_1.getEvent(new Request(`https://blah.com/${resourceKey}`, {
340
- headers: {
341
- 'if-none-match': `W/"${resourceVersion}"`,
342
- },
343
- }));
344
- const res1 = yield index_1.getAssetFromKV(event1, { cacheControl: { edgeTTL: 720 } });
345
- yield mocks_1.sleep(100);
346
- const res2 = yield index_1.getAssetFromKV(event2);
347
- if (res1 && res2) {
348
- t.is(res1.headers.get('cf-cache-status'), 'MISS');
349
- t.is(res2.headers.get('cf-cache-status'), 'REVALIDATED');
350
- }
351
- else {
352
- t.fail('Response was undefined');
353
- }
354
- }));
355
- ava_1.default('getAssetFromKV when if-none-match equals etag of stale resource then should bypass cache', (t) => __awaiter(void 0, void 0, void 0, function* () {
356
- mocks_1.mockGlobal();
357
- const resourceKey = 'key1.png';
358
- const resourceVersion = JSON.parse(mocks_1.mockManifest())[resourceKey];
359
- const req1 = new Request(`https://blah.com/${resourceKey}`, {
360
- headers: {
361
- 'if-none-match': `"${resourceVersion}"`,
362
- },
363
- });
364
- const req2 = new Request(`https://blah.com/${resourceKey}`, {
365
- headers: {
366
- 'if-none-match': `"${resourceVersion}-another-version"`,
367
- },
368
- });
369
- const event = mocks_1.getEvent(req1);
370
- const event2 = mocks_1.getEvent(req2);
371
- const res1 = yield index_1.getAssetFromKV(event, { cacheControl: { edgeTTL: 720 } });
372
- const res2 = yield index_1.getAssetFromKV(event);
373
- const res3 = yield index_1.getAssetFromKV(event2);
374
- if (res1 && res2 && res3) {
375
- t.is(res1.headers.get('cf-cache-status'), 'MISS');
376
- t.is(res2.headers.get('etag'), `W/${req1.headers.get('if-none-match')}`);
377
- t.is(res2.headers.get('cf-cache-status'), 'REVALIDATED');
378
- t.not(res3.headers.get('etag'), req2.headers.get('if-none-match'));
379
- t.is(res3.headers.get('cf-cache-status'), 'MISS');
380
- }
381
- else {
382
- t.fail('Response was undefined');
383
- }
384
- }));
385
- ava_1.default('getAssetFromKV when resource in cache, etag should be weakened before returned to eyeball', (t) => __awaiter(void 0, void 0, void 0, function* () {
386
- mocks_1.mockGlobal();
387
- const resourceKey = 'key1.png';
388
- const resourceVersion = JSON.parse(mocks_1.mockManifest())[resourceKey];
389
- const req1 = new Request(`https://blah.com/${resourceKey}`, {
390
- headers: {
391
- 'if-none-match': `"${resourceVersion}"`,
392
- },
393
- });
394
- const event = mocks_1.getEvent(req1);
395
- const res1 = yield index_1.getAssetFromKV(event, { cacheControl: { edgeTTL: 720 } });
396
- const res2 = yield index_1.getAssetFromKV(event);
397
- if (res1 && res2) {
398
- t.is(res1.headers.get('cf-cache-status'), 'MISS');
399
- t.is(res2.headers.get('etag'), `W/${req1.headers.get('if-none-match')}`);
400
- }
401
- else {
402
- t.fail('Response was undefined');
403
- }
404
- }));
405
- ava_1.default('getAssetFromKV if-none-match not sent but resource in cache, should return cache hit 200 OK', (t) => __awaiter(void 0, void 0, void 0, function* () {
406
- const resourceKey = 'cache.html';
407
- const event = mocks_1.getEvent(new Request(`https://blah.com/${resourceKey}`));
408
- const res1 = yield index_1.getAssetFromKV(event, { cacheControl: { edgeTTL: 720 } });
409
- yield mocks_1.sleep(1);
410
- const res2 = yield index_1.getAssetFromKV(event);
411
- if (res1 && res2) {
412
- t.is(res1.headers.get('cf-cache-status'), 'MISS');
413
- t.is(res1.headers.get('cache-control'), null);
414
- t.is(res2.status, 200);
415
- t.is(res2.headers.get('cf-cache-status'), 'HIT');
416
- }
417
- else {
418
- t.fail('Response was undefined');
419
- }
420
- }));
421
- ava_1.default('getAssetFromKV if range request submitted and resource in cache, request fulfilled', (t) => __awaiter(void 0, void 0, void 0, function* () {
422
- const resourceKey = 'cache.html';
423
- const event1 = mocks_1.getEvent(new Request(`https://blah.com/${resourceKey}`));
424
- const event2 = mocks_1.getEvent(new Request(`https://blah.com/${resourceKey}`, { headers: { range: 'bytes=0-10' } }));
425
- const res1 = index_1.getAssetFromKV(event1, { cacheControl: { edgeTTL: 720 } });
426
- yield res1;
427
- yield mocks_1.sleep(2);
428
- const res2 = yield index_1.getAssetFromKV(event2);
429
- if (res2.headers.has('content-range')) {
430
- t.is(res2.status, 206);
431
- }
432
- else {
433
- t.fail('Response was undefined');
434
- }
435
- }));
436
- ava_1.default.todo('getAssetFromKV when body not empty, should invoke .cancel()');
@@ -1,40 +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
- ava_1.default('mapRequestToAsset() correctly changes /about -> /about/index.html', (t) => __awaiter(void 0, void 0, void 0, function* () {
16
- mocks_1.mockGlobal();
17
- let path = '/about';
18
- let request = new Request(`https://foo.com${path}`);
19
- let newRequest = index_1.mapRequestToAsset(request);
20
- t.is(newRequest.url, request.url + '/index.html');
21
- }));
22
- ava_1.default('mapRequestToAsset() correctly changes /about/ -> /about/index.html', (t) => __awaiter(void 0, void 0, void 0, function* () {
23
- let path = '/about/';
24
- let request = new Request(`https://foo.com${path}`);
25
- let newRequest = index_1.mapRequestToAsset(request);
26
- t.is(newRequest.url, request.url + 'index.html');
27
- }));
28
- ava_1.default('mapRequestToAsset() correctly changes /about.me/ -> /about.me/index.html', (t) => __awaiter(void 0, void 0, void 0, function* () {
29
- let path = '/about.me/';
30
- let request = new Request(`https://foo.com${path}`);
31
- let newRequest = index_1.mapRequestToAsset(request);
32
- t.is(newRequest.url, request.url + 'index.html');
33
- }));
34
- ava_1.default('mapRequestToAsset() correctly changes /about -> /about/default.html', (t) => __awaiter(void 0, void 0, void 0, function* () {
35
- mocks_1.mockGlobal();
36
- let path = '/about';
37
- let request = new Request(`https://foo.com${path}`);
38
- let newRequest = index_1.mapRequestToAsset(request, { defaultDocument: 'default.html' });
39
- t.is(newRequest.url, request.url + '/default.html');
40
- }));