deno.land / x / deno@v1.28.2 / ext / cache / 01_cache.js

نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license."use strict";
((window) => { const core = window.__bootstrap.core; const webidl = window.__bootstrap.webidl; const { Symbol, TypeError, ObjectPrototypeIsPrototypeOf, } = window.__bootstrap.primordials; const { Request, toInnerResponse, toInnerRequest, } = window.__bootstrap.fetch; const { URLPrototype } = window.__bootstrap.url; const RequestPrototype = Request.prototype; const { getHeader } = window.__bootstrap.headers; const { readableStreamForRid } = window.__bootstrap.streams;
class CacheStorage { constructor() { webidl.illegalConstructor(); }
async open(cacheName) { webidl.assertBranded(this, CacheStoragePrototype); const prefix = "Failed to execute 'open' on 'CacheStorage'"; webidl.requiredArguments(arguments.length, 1, { prefix }); cacheName = webidl.converters["DOMString"](cacheName, { prefix, context: "Argument 1", }); const cacheId = await core.opAsync("op_cache_storage_open", cacheName); const cache = webidl.createBranded(Cache); cache[_id] = cacheId; return cache; }
async has(cacheName) { webidl.assertBranded(this, CacheStoragePrototype); const prefix = "Failed to execute 'has' on 'CacheStorage'"; webidl.requiredArguments(arguments.length, 1, { prefix }); cacheName = webidl.converters["DOMString"](cacheName, { prefix, context: "Argument 1", }); return await core.opAsync("op_cache_storage_has", cacheName); }
async delete(cacheName) { webidl.assertBranded(this, CacheStoragePrototype); const prefix = "Failed to execute 'delete' on 'CacheStorage'"; webidl.requiredArguments(arguments.length, 1, { prefix }); cacheName = webidl.converters["DOMString"](cacheName, { prefix, context: "Argument 1", }); return await core.opAsync("op_cache_storage_delete", cacheName); } }
const _matchAll = Symbol("[[matchAll]]"); const _id = Symbol("id");
class Cache { /** @type {number} */ [_id];
constructor() { webidl.illegalConstructor(); }
/** See https://w3c.github.io/ServiceWorker/#dom-cache-put */ async put(request, response) { webidl.assertBranded(this, CachePrototype); const prefix = "Failed to execute 'put' on 'Cache'"; webidl.requiredArguments(arguments.length, 2, { prefix }); request = webidl.converters["RequestInfo_DOMString"](request, { prefix, context: "Argument 1", }); response = webidl.converters["Response"](response, { prefix, context: "Argument 2", }); // Step 1. let innerRequest = null; // Step 2. if (ObjectPrototypeIsPrototypeOf(RequestPrototype, request)) { innerRequest = toInnerRequest(request); } else { // Step 3. innerRequest = toInnerRequest(new Request(request)); } // Step 4. const reqUrl = new URL(innerRequest.url()); if (reqUrl.protocol !== "http:" && reqUrl.protocol !== "https:") { throw new TypeError( "Request url protocol must be 'http:' or 'https:'", ); } if (innerRequest.method !== "GET") { throw new TypeError("Request method must be GET"); } // Step 5. const innerResponse = toInnerResponse(response); // Step 6. if (innerResponse.status === 206) { throw new TypeError("Response status must not be 206"); } // Step 7. const varyHeader = getHeader(innerResponse.headerList, "vary"); if (varyHeader) { const fieldValues = varyHeader.split(","); for (const field of fieldValues) { if (field.trim() === "*") { throw new TypeError("Vary header must not contain '*'"); } } }
// Step 8. if (innerResponse.body !== null && innerResponse.body.unusable()) { throw new TypeError("Response body is already used"); } // acquire lock before async op const reader = innerResponse.body?.stream.getReader();
// Remove fragment from request URL before put. reqUrl.hash = "";
// Step 9-11. const rid = await core.opAsync( "op_cache_put", { cacheId: this[_id], requestUrl: reqUrl.toString(), responseHeaders: innerResponse.headerList, requestHeaders: innerRequest.headerList, responseHasBody: innerResponse.body !== null, responseStatus: innerResponse.status, responseStatusText: innerResponse.statusMessage, }, ); if (reader) { try { while (true) { const { value, done } = await reader.read(); if (done) { await core.shutdown(rid); break; } await core.writeAll(rid, value); } } finally { core.close(rid); } } // Step 12-19: TODO(@satyarohith): do the insertion in background. }
/** See https://w3c.github.io/ServiceWorker/#cache-match */ async match(request, options) { webidl.assertBranded(this, CachePrototype); const prefix = "Failed to execute 'match' on 'Cache'"; webidl.requiredArguments(arguments.length, 1, { prefix }); request = webidl.converters["RequestInfo_DOMString"](request, { prefix, context: "Argument 1", }); const p = await this[_matchAll](request, options); if (p.length > 0) { return p[0]; } else { return undefined; } }
/** See https://w3c.github.io/ServiceWorker/#cache-delete */ async delete(request, _options) { webidl.assertBranded(this, CachePrototype); const prefix = "Failed to execute 'delete' on 'Cache'"; webidl.requiredArguments(arguments.length, 1, { prefix }); request = webidl.converters["RequestInfo_DOMString"](request, { prefix, context: "Argument 1", }); // Step 1. let r = null; // Step 2. if (ObjectPrototypeIsPrototypeOf(RequestPrototype, request)) { r = request; if (request.method !== "GET") { return false; } } else if ( typeof request === "string" || ObjectPrototypeIsPrototypeOf(URLPrototype, request) ) { r = new Request(request); } return await core.opAsync("op_cache_delete", { cacheId: this[_id], requestUrl: r.url, }); }
/** See https://w3c.github.io/ServiceWorker/#cache-matchall * * Note: the function is private as we don't want to expose * this API to the public yet. * * The function will return an array of responses. */ async [_matchAll](request, _options) { // Step 1. let r = null; // Step 2. if (ObjectPrototypeIsPrototypeOf(RequestPrototype, request)) { r = request; if (request.method !== "GET") { return []; } } else if ( typeof request === "string" || ObjectPrototypeIsPrototypeOf(URLPrototype, request) ) { r = new Request(request); }
// Step 5. const responses = []; // Step 5.2 if (r === null) { // Step 5.3 // Note: we have to return all responses in the cache when // the request is null. // We deviate from the spec here and return an empty array // as we don't expose matchAll() API. return responses; } else { // Remove the fragment from the request URL. const url = new URL(r.url); url.hash = ""; const innerRequest = toInnerRequest(r); const matchResult = await core.opAsync( "op_cache_match", { cacheId: this[_id], requestUrl: url.toString(), requestHeaders: innerRequest.headerList, }, ); if (matchResult) { const [meta, responseBodyRid] = matchResult; let body = null; if (responseBodyRid !== null) { body = readableStreamForRid(responseBodyRid); } const response = new Response( body, { headers: meta.responseHeaders, status: meta.responseStatus, statusText: meta.responseStatusText, }, ); responses.push(response); } } // Step 5.4-5.5: don't apply in this context.
return responses; } }
webidl.configurePrototype(CacheStorage); webidl.configurePrototype(Cache); const CacheStoragePrototype = CacheStorage.prototype; const CachePrototype = Cache.prototype;
let cacheStorage; window.__bootstrap.caches = { CacheStorage, Cache, cacheStorage() { if (!cacheStorage) { cacheStorage = webidl.createBranded(CacheStorage); } return cacheStorage; }, };})(this);
deno

Version Info

Tagged at
a year ago