Comparing version 0.0.757 to 0.0.758
@@ -10,3 +10,3 @@ import { Coroutine } from './coroutine'; | ||
method?: string; | ||
headers?: Headers; | ||
headers?: Record<string, string> | Headers; | ||
body?: Document | XMLHttpRequestBodyInit | null; | ||
@@ -37,4 +37,5 @@ responseType?: XMLHttpRequestResponseType; | ||
opts.headers = new Headers(opts.headers); | ||
const { method, headers, cache } = opts; | ||
let state: 'load' | 'error' | 'abort' | 'timeout'; | ||
const key = `${opts.method}:${url}`; | ||
const key = `${method}:${url}`; | ||
const xhr = new XMLHttpRequest(); | ||
@@ -48,6 +49,6 @@ const listener = new Colistener<ProgressEvent>(listener => { | ||
} | ||
if (['GET', 'PUT'].includes(opts.method!) && | ||
opts.cache && opts.cache.has(key) && memory.has(opts.cache.get(key)!) && | ||
Date.now() > memory.get(opts.cache.get(key)!)!.expiration) { | ||
opts.headers!.set('If-None-Match', opts.cache.get(key)!.getResponseHeader('ETag')!); | ||
if (['GET', 'PUT'].includes(method) && | ||
cache && cache.has(key) && memory.has(cache.get(key)!) && | ||
Date.now() > memory.get(cache.get(key)!)!.expiration) { | ||
headers.set('If-None-Match', cache.get(key)!.getResponseHeader('ETag')!); | ||
} | ||
@@ -67,32 +68,30 @@ fetch(xhr, url, opts); | ||
case 'load': | ||
if (opts.cache) { | ||
switch (opts.method) { | ||
case 'GET': | ||
case 'PUT': | ||
if (`${xhr.status}`.match(/^2..$/)) { | ||
const cc = new Map<string, string>( | ||
xhr.getResponseHeader('Cache-Control') | ||
// eslint-disable-next-line redos/no-vulnerable | ||
? xhr.getResponseHeader('Cache-Control')!.trim().split(/\s*,\s*/) | ||
.filter(v => v.length > 0) | ||
.map(v => [...v.split('='), ''] as [string, string]) | ||
: []); | ||
if (xhr.getResponseHeader('ETag') && !cc.has('no-store')) { | ||
memory.set(xhr, { | ||
expiration: cc.has('max-age') && !cc.has('no-cache') | ||
? Date.now() + +cc.get('max-age')! * 1000 || 0 | ||
: 0, | ||
}); | ||
opts.cache.set(key, xhr); | ||
} | ||
else { | ||
memory.delete(xhr); | ||
opts.cache.delete(key); | ||
} | ||
if (cache) switch (method) { | ||
case 'GET': | ||
case 'PUT': | ||
if (`${xhr.status}`.match(/^2..$/)) { | ||
const cc = new Map<string, string>( | ||
xhr.getResponseHeader('Cache-Control') | ||
// eslint-disable-next-line redos/no-vulnerable | ||
? xhr.getResponseHeader('Cache-Control')!.trim().split(/\s*,\s*/) | ||
.filter(v => v.length > 0) | ||
.map(v => [...v.split('='), ''] as [string, string]) | ||
: []); | ||
if (xhr.getResponseHeader('ETag') && !cc.has('no-store')) { | ||
memory.set(xhr, { | ||
expiration: cc.has('max-age') && !cc.has('no-cache') | ||
? Date.now() + +cc.get('max-age')! * 1000 || 0 | ||
: 0, | ||
}); | ||
cache.set(key, xhr); | ||
} | ||
if (xhr.status === 304 && opts.cache.has(key)) { | ||
return opts.cache.get(key)!; | ||
else { | ||
memory.delete(xhr); | ||
cache.delete(key); | ||
} | ||
break; | ||
} | ||
} | ||
if (xhr.status === 304 && cache.has(key)) { | ||
return cache.get(key)!; | ||
} | ||
break; | ||
} | ||
@@ -123,3 +122,3 @@ return xhr; | ||
case 'headers': | ||
opts.headers?.forEach(([name, value]) => | ||
(opts.headers as Headers).forEach(([name, value]) => | ||
void xhr.setRequestHeader(name, value)); | ||
@@ -126,0 +125,0 @@ continue; |
{ | ||
"name": "spica", | ||
"version": "0.0.757", | ||
"version": "0.0.758", | ||
"description": "Supervisor, Coroutine, Channel, select, AtomicPromise, Cancellation, Cache, List, Queue, Stack, and some utils.", | ||
@@ -5,0 +5,0 @@ "private": false, |
619529
16911