quickwin 2026.5.2-3.145209
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/README.md +6 -0
- package/examples/pdf_preview.js +440 -0
- package/examples/pdf_preview.ts +470 -0
- package/examples/preact_demo.js +35 -0
- package/examples/preact_demo.tsx +49 -0
- package/examples/tray_demo.js +75 -0
- package/examples/tray_demo.tsx +79 -0
- package/lib/fetch.js +746 -0
- package/lib/fetch.ts +811 -0
- package/lib/polyfill.js +500 -0
- package/lib/polyfill.ts +454 -0
- package/lib/preact/hooks.js +287 -0
- package/lib/preact/hooks.ts +330 -0
- package/lib/preact/jsx-runtime.js +1 -0
- package/lib/preact/jsx-runtime.ts +2 -0
- package/lib/preact/jsx.d.ts +36 -0
- package/lib/preact/layout.js +153 -0
- package/lib/preact/layout.ts +183 -0
- package/lib/preact/preact.js +54 -0
- package/lib/preact/preact.ts +133 -0
- package/lib/preact/props.js +99 -0
- package/lib/preact/props.ts +119 -0
- package/lib/preact/render.js +320 -0
- package/lib/preact/render.ts +353 -0
- package/lib/websocket.js +540 -0
- package/lib/websocket.ts +574 -0
- package/package.json +32 -0
- package/quickwin.d.ts +657 -0
- package/test/add.wasm +0 -0
- package/test/complex.wasm +0 -0
- package/test/complex_imports.wasm +0 -0
- package/test/global_imports.wasm +0 -0
- package/test/import_func.wasm +0 -0
- package/test/imports.wasm +0 -0
- package/test/run.js +86 -0
- package/test/run.ts +90 -0
- package/test/sjlj.wasm +0 -0
- package/test/test_basic.js +7 -0
- package/test/test_basic.ts +9 -0
- package/test/test_brotli.js +48 -0
- package/test/test_brotli.ts +52 -0
- package/test/test_fetch_cache.js +131 -0
- package/test/test_fetch_cache.ts +141 -0
- package/test/test_ffi.js +157 -0
- package/test/test_ffi.ts +174 -0
- package/test/test_frame_encoding.js +128 -0
- package/test/test_frame_encoding.ts +132 -0
- package/test/test_helper.js +84 -0
- package/test/test_helper.ts +80 -0
- package/test/test_http_import.js +78 -0
- package/test/test_http_import.ts +74 -0
- package/test/test_mupdf_render.js +69 -0
- package/test/test_mupdf_render.ts +74 -0
- package/test/test_mupdf_twice.js +77 -0
- package/test/test_mupdf_twice.ts +81 -0
- package/test/test_mupdf_wasm.js +33 -0
- package/test/test_mupdf_wasm.ts +30 -0
- package/test/test_net_event.js +63 -0
- package/test/test_net_event.ts +59 -0
- package/test/test_net_fetch.js +153 -0
- package/test/test_net_fetch.ts +131 -0
- package/test/test_net_websocket.js +158 -0
- package/test/test_net_websocket.ts +144 -0
- package/test/test_polyfill.js +58 -0
- package/test/test_polyfill.ts +60 -0
- package/test/test_url.js +173 -0
- package/test/test_url.ts +183 -0
- package/test/test_wasm_basic.js +82 -0
- package/test/test_wasm_basic.ts +70 -0
- package/test/test_wasm_import_global.js +41 -0
- package/test/test_wasm_import_global.ts +39 -0
- package/test/test_wasm_sjlj.js +153 -0
- package/test/test_wasm_sjlj.ts +134 -0
- package/test/test_wasm_types.js +96 -0
- package/test/test_wasm_types.ts +108 -0
- package/test/types.wasm +0 -0
- package/tsconfig.json +18 -0
- package/vendor/mupdf-wasm/mupdf-wasm.d.ts +571 -0
- package/vendor/mupdf-wasm/mupdf-wasm.js +2749 -0
- package/vendor/mupdf-wasm/mupdf-wasm.wasm +0 -0
- package/vendor/mupdf-wasm/mupdf.d.ts +939 -0
- package/vendor/mupdf-wasm/mupdf.js +3317 -0
- package/win-mingw64.exe +0 -0
package/test/run.js
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import * as std from 'std';
|
|
2
|
+
import { Tester } from './test_helper.js';
|
|
3
|
+
function formatDuration(ms) {
|
|
4
|
+
if (ms >= 1000)
|
|
5
|
+
return (ms / 1000).toFixed(2) + 's';
|
|
6
|
+
return ms + 'ms';
|
|
7
|
+
}
|
|
8
|
+
const BOLD = '\x1b[1m';
|
|
9
|
+
const GREEN = '\x1b[32m';
|
|
10
|
+
const RED = '\x1b[31m';
|
|
11
|
+
const RESET = '\x1b[0m';
|
|
12
|
+
const suiteDefs = [
|
|
13
|
+
{ name: 'basic', file: './test_basic.js', tags: [] },
|
|
14
|
+
{ name: 'url', file: './test_url.js', tags: [] },
|
|
15
|
+
{ name: 'wasm-basic', file: './test_wasm_basic.js', tags: ['wasm'] },
|
|
16
|
+
{ name: 'wasm-types', file: './test_wasm_types.js', tags: ['wasm'] },
|
|
17
|
+
{ name: 'wasm-import-global', file: './test_wasm_import_global.js', tags: ['wasm'] },
|
|
18
|
+
{ name: 'wasm-sjlj', file: './test_wasm_sjlj.js', tags: ['wasm'] },
|
|
19
|
+
{ name: 'wasm-frame-encoding', file: './test_frame_encoding.js', tags: ['wasm'] },
|
|
20
|
+
{ name: 'mupdf-wasm', file: './test_mupdf_wasm.js', tags: ['wasm', 'mupdf'] },
|
|
21
|
+
{ name: 'mupdf-twice', file: './test_mupdf_twice.js', tags: ['wasm', 'mupdf'] },
|
|
22
|
+
{ name: 'mupdf-render', file: './test_mupdf_render.js', tags: ['wasm', 'mupdf'] },
|
|
23
|
+
{ name: 'ffi', file: './test_ffi.js', tags: [] },
|
|
24
|
+
{ name: 'net-fetch', file: './test_net_fetch.js', tags: ['net'] },
|
|
25
|
+
{ name: 'net-websocket', file: './test_net_websocket.js', tags: ['net'] },
|
|
26
|
+
{ name: 'net-event', file: './test_net_event.js', tags: ['net'] },
|
|
27
|
+
{ name: 'http-import', file: './test_http_import.js', tags: ['net'] },
|
|
28
|
+
{ name: 'fetch-cache', file: './test_fetch_cache.js', tags: ['net'] },
|
|
29
|
+
{ name: 'polyfill', file: './test_polyfill.js', tags: [] },
|
|
30
|
+
{ name: 'brotli', file: './test_brotli.js', tags: [] },
|
|
31
|
+
];
|
|
32
|
+
async function main() {
|
|
33
|
+
const filter = scriptArgs.length > 1 ? scriptArgs[1] : '';
|
|
34
|
+
std.printf('%s====== QuickWin Test Runner ======%s\n', BOLD, RESET);
|
|
35
|
+
if (filter)
|
|
36
|
+
std.printf('Filter: %s\n', filter);
|
|
37
|
+
const results = [];
|
|
38
|
+
let totalOk = 0, totalFail = 0;
|
|
39
|
+
for (const def of suiteDefs) {
|
|
40
|
+
if (filter) {
|
|
41
|
+
if (filter.startsWith('-')) {
|
|
42
|
+
const excludeTag = filter.slice(1);
|
|
43
|
+
if (def.tags.indexOf(excludeTag) >= 0)
|
|
44
|
+
continue;
|
|
45
|
+
}
|
|
46
|
+
else {
|
|
47
|
+
if (def.name.indexOf(filter) < 0)
|
|
48
|
+
continue;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
let mod;
|
|
52
|
+
try {
|
|
53
|
+
mod = await import(def.file);
|
|
54
|
+
}
|
|
55
|
+
catch (e) {
|
|
56
|
+
continue;
|
|
57
|
+
}
|
|
58
|
+
if (!mod.suite)
|
|
59
|
+
continue;
|
|
60
|
+
const t = new Tester();
|
|
61
|
+
std.printf('\n%s--- %s ---%s\n', BOLD, def.name, RESET);
|
|
62
|
+
const suiteStart = Date.now();
|
|
63
|
+
try {
|
|
64
|
+
await mod.suite.run(t);
|
|
65
|
+
}
|
|
66
|
+
catch (e) {
|
|
67
|
+
std.printf(' %sSUITE FAILED:%s %s\n', RED, RESET, String(e));
|
|
68
|
+
t.fail++;
|
|
69
|
+
}
|
|
70
|
+
const suiteElapsed = Date.now() - suiteStart;
|
|
71
|
+
t.summary();
|
|
72
|
+
results.push({ name: def.name, ok: t.ok, fail: t.fail, elapsed: suiteElapsed });
|
|
73
|
+
totalOk += t.ok;
|
|
74
|
+
totalFail += t.fail;
|
|
75
|
+
}
|
|
76
|
+
const color = totalFail > 0 ? RED : GREEN;
|
|
77
|
+
std.printf('\n%s====== Test Results ======%s\n', BOLD, RESET);
|
|
78
|
+
for (const r of results) {
|
|
79
|
+
const c = r.fail > 0 ? RED : GREEN;
|
|
80
|
+
std.printf(' %s%-18s %s%d/%d passed %s(%s)%s\n', c, r.name, RESET, r.ok, r.ok + r.fail, c, formatDuration(r.elapsed), RESET);
|
|
81
|
+
}
|
|
82
|
+
std.printf('%s====== Summary: %d/%d passed ======%s\n', color, totalOk, totalOk + totalFail, RESET);
|
|
83
|
+
if (totalFail > 0)
|
|
84
|
+
std.exit(1);
|
|
85
|
+
}
|
|
86
|
+
main();
|
package/test/run.ts
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import * as std from 'std'
|
|
2
|
+
import { Tester } from './test_helper.js'
|
|
3
|
+
|
|
4
|
+
declare var scriptArgs: string[]
|
|
5
|
+
|
|
6
|
+
function formatDuration(ms: number): string {
|
|
7
|
+
if (ms >= 1000) return (ms / 1000).toFixed(2) + 's'
|
|
8
|
+
return ms + 'ms'
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
const BOLD = '\x1b[1m'
|
|
12
|
+
const GREEN = '\x1b[32m'
|
|
13
|
+
const RED = '\x1b[31m'
|
|
14
|
+
const RESET = '\x1b[0m'
|
|
15
|
+
|
|
16
|
+
const suiteDefs = [
|
|
17
|
+
{ name: 'basic', file: './test_basic.js', tags: [] },
|
|
18
|
+
{ name: 'url', file: './test_url.js', tags: [] },
|
|
19
|
+
{ name: 'wasm-basic', file: './test_wasm_basic.js', tags: ['wasm'] },
|
|
20
|
+
{ name: 'wasm-types', file: './test_wasm_types.js', tags: ['wasm'] },
|
|
21
|
+
{ name: 'wasm-import-global', file: './test_wasm_import_global.js', tags: ['wasm'] },
|
|
22
|
+
{ name: 'wasm-sjlj', file: './test_wasm_sjlj.js', tags: ['wasm'] },
|
|
23
|
+
{ name: 'wasm-frame-encoding', file: './test_frame_encoding.js', tags: ['wasm'] },
|
|
24
|
+
{ name: 'mupdf-wasm', file: './test_mupdf_wasm.js', tags: ['wasm', 'mupdf'] },
|
|
25
|
+
{ name: 'mupdf-twice', file: './test_mupdf_twice.js', tags: ['wasm', 'mupdf'] },
|
|
26
|
+
{ name: 'mupdf-render', file: './test_mupdf_render.js', tags: ['wasm', 'mupdf'] },
|
|
27
|
+
{ name: 'ffi', file: './test_ffi.js', tags: [] },
|
|
28
|
+
{ name: 'net-fetch', file: './test_net_fetch.js', tags: ['net'] },
|
|
29
|
+
{ name: 'net-websocket', file: './test_net_websocket.js', tags: ['net'] },
|
|
30
|
+
{ name: 'net-event', file: './test_net_event.js', tags: ['net'] },
|
|
31
|
+
{ name: 'http-import', file: './test_http_import.js', tags: ['net'] },
|
|
32
|
+
{ name: 'fetch-cache', file: './test_fetch_cache.js', tags: ['net'] },
|
|
33
|
+
{ name: 'polyfill', file: './test_polyfill.js', tags: [] },
|
|
34
|
+
{ name: 'brotli', file: './test_brotli.js', tags: [] },
|
|
35
|
+
]
|
|
36
|
+
|
|
37
|
+
async function main(): Promise<void> {
|
|
38
|
+
const filter = scriptArgs.length > 1 ? scriptArgs[1] : ''
|
|
39
|
+
|
|
40
|
+
std.printf('%s====== QuickWin Test Runner ======%s\n', BOLD, RESET)
|
|
41
|
+
if (filter) std.printf('Filter: %s\n', filter)
|
|
42
|
+
|
|
43
|
+
const results: { name: string, ok: number, fail: number, elapsed: number }[] = []
|
|
44
|
+
let totalOk = 0, totalFail = 0
|
|
45
|
+
|
|
46
|
+
for (const def of suiteDefs) {
|
|
47
|
+
if (filter) {
|
|
48
|
+
if (filter.startsWith('-')) {
|
|
49
|
+
const excludeTag = filter.slice(1)
|
|
50
|
+
if (def.tags.indexOf(excludeTag) >= 0) continue
|
|
51
|
+
} else {
|
|
52
|
+
if (def.name.indexOf(filter) < 0) continue
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
let mod: any
|
|
57
|
+
try {
|
|
58
|
+
mod = await import(def.file)
|
|
59
|
+
} catch (e) {
|
|
60
|
+
continue
|
|
61
|
+
}
|
|
62
|
+
if (!mod.suite) continue
|
|
63
|
+
|
|
64
|
+
const t = new Tester()
|
|
65
|
+
std.printf('\n%s--- %s ---%s\n', BOLD, def.name, RESET)
|
|
66
|
+
const suiteStart = Date.now()
|
|
67
|
+
try {
|
|
68
|
+
await mod.suite.run(t)
|
|
69
|
+
} catch (e) {
|
|
70
|
+
std.printf(' %sSUITE FAILED:%s %s\n', RED, RESET, String(e))
|
|
71
|
+
t.fail++
|
|
72
|
+
}
|
|
73
|
+
const suiteElapsed = Date.now() - suiteStart
|
|
74
|
+
t.summary()
|
|
75
|
+
results.push({ name: def.name, ok: t.ok, fail: t.fail, elapsed: suiteElapsed })
|
|
76
|
+
totalOk += t.ok
|
|
77
|
+
totalFail += t.fail
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const color = totalFail > 0 ? RED : GREEN
|
|
81
|
+
std.printf('\n%s====== Test Results ======%s\n', BOLD, RESET)
|
|
82
|
+
for (const r of results) {
|
|
83
|
+
const c = r.fail > 0 ? RED : GREEN
|
|
84
|
+
std.printf(' %s%-18s %s%d/%d passed %s(%s)%s\n', c, r.name, RESET, r.ok, r.ok + r.fail, c, formatDuration(r.elapsed), RESET)
|
|
85
|
+
}
|
|
86
|
+
std.printf('%s====== Summary: %d/%d passed ======%s\n', color, totalOk, totalOk + totalFail, RESET)
|
|
87
|
+
if (totalFail > 0) std.exit(1)
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
main()
|
package/test/sjlj.wasm
ADDED
|
Binary file
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import * as brotli from 'brotli';
|
|
2
|
+
function abToString(buf) {
|
|
3
|
+
const bytes = new Uint8Array(buf);
|
|
4
|
+
let s = '';
|
|
5
|
+
for (let i = 0; i < bytes.length; i++)
|
|
6
|
+
s += String.fromCharCode(bytes[i]);
|
|
7
|
+
return s;
|
|
8
|
+
}
|
|
9
|
+
// pre-compressed with brotli CLI: "hello brotli! this is a test of decompression"
|
|
10
|
+
const COMPRESSED_HEX = 'a16001c0ef4cb07142bdbbe12588185985d055995ca6b1bdb52083f4028dc6e59c8660a24efa701549c30701';
|
|
11
|
+
function hexToArrayBuffer(hex) {
|
|
12
|
+
const bytes = new Uint8Array(hex.length / 2);
|
|
13
|
+
for (let i = 0; i < hex.length; i += 2)
|
|
14
|
+
bytes[i / 2] = parseInt(hex.substring(i, i + 2), 16);
|
|
15
|
+
return bytes.buffer;
|
|
16
|
+
}
|
|
17
|
+
export const suite = {
|
|
18
|
+
name: 'brotli',
|
|
19
|
+
run: (t) => {
|
|
20
|
+
t.section('decompress function exists');
|
|
21
|
+
t.checkTrue('decompress is function', typeof brotli.decompress === 'function');
|
|
22
|
+
t.section('decompress known string');
|
|
23
|
+
const compressed = hexToArrayBuffer(COMPRESSED_HEX);
|
|
24
|
+
const decompressed = brotli.decompress(compressed);
|
|
25
|
+
t.checkTrue('returns ArrayBuffer', decompressed instanceof ArrayBuffer);
|
|
26
|
+
const result = abToString(decompressed);
|
|
27
|
+
t.check('roundtrip', 'hello brotli! this is a test of decompression', result);
|
|
28
|
+
t.section('invalid data throws');
|
|
29
|
+
let threw = false;
|
|
30
|
+
try {
|
|
31
|
+
brotli.decompress(new ArrayBuffer(10));
|
|
32
|
+
}
|
|
33
|
+
catch (e) {
|
|
34
|
+
threw = true;
|
|
35
|
+
}
|
|
36
|
+
t.checkTrue('throws on invalid brotli data', threw);
|
|
37
|
+
t.section('non-ArrayBuffer throws');
|
|
38
|
+
let threw2 = false;
|
|
39
|
+
try {
|
|
40
|
+
;
|
|
41
|
+
brotli.decompress('not a buffer');
|
|
42
|
+
}
|
|
43
|
+
catch (e) {
|
|
44
|
+
threw2 = true;
|
|
45
|
+
}
|
|
46
|
+
t.checkTrue('throws on non-ArrayBuffer', threw2);
|
|
47
|
+
}
|
|
48
|
+
};
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import * as brotli from 'brotli'
|
|
2
|
+
import { Tester } from './test_helper.js'
|
|
3
|
+
|
|
4
|
+
function abToString(buf: ArrayBuffer): string {
|
|
5
|
+
const bytes = new Uint8Array(buf)
|
|
6
|
+
let s = ''
|
|
7
|
+
for (let i = 0; i < bytes.length; i++) s += String.fromCharCode(bytes[i])
|
|
8
|
+
return s
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
// pre-compressed with brotli CLI: "hello brotli! this is a test of decompression"
|
|
12
|
+
const COMPRESSED_HEX = 'a16001c0ef4cb07142bdbbe12588185985d055995ca6b1bdb52083f4028dc6e59c8660a24efa701549c30701'
|
|
13
|
+
|
|
14
|
+
function hexToArrayBuffer(hex: string): ArrayBuffer {
|
|
15
|
+
const bytes = new Uint8Array(hex.length / 2)
|
|
16
|
+
for (let i = 0; i < hex.length; i += 2)
|
|
17
|
+
bytes[i / 2] = parseInt(hex.substring(i, i + 2), 16)
|
|
18
|
+
return bytes.buffer as ArrayBuffer
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export const suite = {
|
|
22
|
+
name: 'brotli',
|
|
23
|
+
run: (t: Tester) => {
|
|
24
|
+
t.section('decompress function exists')
|
|
25
|
+
t.checkTrue('decompress is function', typeof brotli.decompress === 'function')
|
|
26
|
+
|
|
27
|
+
t.section('decompress known string')
|
|
28
|
+
const compressed = hexToArrayBuffer(COMPRESSED_HEX)
|
|
29
|
+
const decompressed = brotli.decompress(compressed)
|
|
30
|
+
t.checkTrue('returns ArrayBuffer', decompressed instanceof ArrayBuffer)
|
|
31
|
+
const result = abToString(decompressed)
|
|
32
|
+
t.check('roundtrip', 'hello brotli! this is a test of decompression', result)
|
|
33
|
+
|
|
34
|
+
t.section('invalid data throws')
|
|
35
|
+
let threw = false
|
|
36
|
+
try {
|
|
37
|
+
brotli.decompress(new ArrayBuffer(10))
|
|
38
|
+
} catch (e) {
|
|
39
|
+
threw = true
|
|
40
|
+
}
|
|
41
|
+
t.checkTrue('throws on invalid brotli data', threw)
|
|
42
|
+
|
|
43
|
+
t.section('non-ArrayBuffer throws')
|
|
44
|
+
let threw2 = false
|
|
45
|
+
try {
|
|
46
|
+
;(brotli as any).decompress('not a buffer')
|
|
47
|
+
} catch (e) {
|
|
48
|
+
threw2 = true
|
|
49
|
+
}
|
|
50
|
+
t.checkTrue('throws on non-ArrayBuffer', threw2)
|
|
51
|
+
}
|
|
52
|
+
}
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import '../lib/fetch.js';
|
|
2
|
+
import * as std from 'std';
|
|
3
|
+
import * as os from 'os';
|
|
4
|
+
function getCacheDir() {
|
|
5
|
+
const url = import.meta.url;
|
|
6
|
+
let path = url.slice(7);
|
|
7
|
+
if (path.length >= 3 && path[1] === ':')
|
|
8
|
+
path = path.slice(1);
|
|
9
|
+
const idx = path.lastIndexOf('/');
|
|
10
|
+
if (idx < 0)
|
|
11
|
+
return '_cache';
|
|
12
|
+
const buildDir = path.slice(0, idx);
|
|
13
|
+
const idx2 = buildDir.lastIndexOf('/');
|
|
14
|
+
if (idx2 < 0)
|
|
15
|
+
return '_cache';
|
|
16
|
+
return buildDir.slice(0, idx2 + 1) + '_cache';
|
|
17
|
+
}
|
|
18
|
+
export const suite = {
|
|
19
|
+
name: 'fetch-cache',
|
|
20
|
+
run: async (t) => {
|
|
21
|
+
function assert(name, ok) {
|
|
22
|
+
if (ok) {
|
|
23
|
+
t.ok++;
|
|
24
|
+
std.printf(' PASS: %s\n', name);
|
|
25
|
+
}
|
|
26
|
+
else {
|
|
27
|
+
t.fail++;
|
|
28
|
+
std.printf(' FAIL: %s\n', name);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
const cacheDir = getCacheDir();
|
|
32
|
+
const trackedUrls = [];
|
|
33
|
+
t.section('__httpCache__ API exists');
|
|
34
|
+
assert('__httpCache__ is defined', typeof __httpCache__ !== 'undefined');
|
|
35
|
+
if (!__httpCache__)
|
|
36
|
+
return;
|
|
37
|
+
assert('readMeta is function', typeof __httpCache__.readMeta === 'function');
|
|
38
|
+
assert('readBody is function', typeof __httpCache__.readBody === 'function');
|
|
39
|
+
assert('writeCache is function', typeof __httpCache__.writeCache === 'function');
|
|
40
|
+
assert('writeMeta is function', typeof __httpCache__.writeMeta === 'function');
|
|
41
|
+
assert('cacheKey is function', typeof __httpCache__.cacheKey === 'function');
|
|
42
|
+
t.section('writeCache + readMeta + readBody');
|
|
43
|
+
const fakeUrl = 'https://test.local/cache-test';
|
|
44
|
+
trackedUrls.push(fakeUrl);
|
|
45
|
+
const testBody = 'hello cache test!';
|
|
46
|
+
__httpCache__.writeCache(fakeUrl, 60, testBody);
|
|
47
|
+
const metaStr = __httpCache__.readMeta(fakeUrl);
|
|
48
|
+
assert('meta written', metaStr !== null);
|
|
49
|
+
if (metaStr) {
|
|
50
|
+
const meta = JSON.parse(metaStr);
|
|
51
|
+
assert('meta has storedAt', typeof meta.storedAt === 'number');
|
|
52
|
+
assert('meta has maxAge', meta.maxAge === 60);
|
|
53
|
+
}
|
|
54
|
+
const fullMeta = JSON.stringify({
|
|
55
|
+
storedAt: Math.floor(Date.now() / 1000),
|
|
56
|
+
maxAge: 60,
|
|
57
|
+
status: 200,
|
|
58
|
+
statusText: 'OK',
|
|
59
|
+
headers: { 'content-type': 'text/plain' },
|
|
60
|
+
});
|
|
61
|
+
__httpCache__.writeMeta(fakeUrl, fullMeta);
|
|
62
|
+
const metaStr2 = __httpCache__.readMeta(fakeUrl);
|
|
63
|
+
assert('meta overwritten', metaStr2 !== null);
|
|
64
|
+
if (metaStr2) {
|
|
65
|
+
const meta2 = JSON.parse(metaStr2);
|
|
66
|
+
assert('meta has status', meta2.status === 200);
|
|
67
|
+
assert('meta has headers', meta2.headers['content-type'] === 'text/plain');
|
|
68
|
+
}
|
|
69
|
+
const bodyAb = __httpCache__.readBody(fakeUrl);
|
|
70
|
+
assert('body read as ArrayBuffer', bodyAb !== null);
|
|
71
|
+
if (bodyAb) {
|
|
72
|
+
const view = new Uint8Array(bodyAb);
|
|
73
|
+
let str = '';
|
|
74
|
+
for (let i = 0; i < view.length; i++)
|
|
75
|
+
str += String.fromCharCode(view[i]);
|
|
76
|
+
assert('body content correct', str === testBody);
|
|
77
|
+
}
|
|
78
|
+
t.section('cacheKey');
|
|
79
|
+
const key = __httpCache__.cacheKey('https://example.com/test.js');
|
|
80
|
+
assert('cacheKey returns 16 hex chars', /^[0-9a-f]{16}$/.test(key));
|
|
81
|
+
t.section('fetch with caching');
|
|
82
|
+
const cacheTestUrl = 'https://httpbin.org/cache/60';
|
|
83
|
+
trackedUrls.push(cacheTestUrl);
|
|
84
|
+
const resp1 = await fetch(cacheTestUrl);
|
|
85
|
+
assert('first fetch ok', resp1.ok);
|
|
86
|
+
const body1 = await resp1.text();
|
|
87
|
+
assert('first fetch body non-empty', body1.length > 0);
|
|
88
|
+
const cachedMeta = __httpCache__.readMeta(cacheTestUrl);
|
|
89
|
+
assert('cached meta exists', cachedMeta !== null);
|
|
90
|
+
if (cachedMeta) {
|
|
91
|
+
const m = JSON.parse(cachedMeta);
|
|
92
|
+
assert('cached status = 200', m.status === 200);
|
|
93
|
+
assert('cached maxAge = 60', m.maxAge === 60);
|
|
94
|
+
}
|
|
95
|
+
const cachedBody = __httpCache__.readBody(cacheTestUrl);
|
|
96
|
+
assert('cached body exists', cachedBody !== null);
|
|
97
|
+
if (cachedBody) {
|
|
98
|
+
assert('cached body matches', cachedBody.byteLength > 0);
|
|
99
|
+
}
|
|
100
|
+
t.section('second fetch hits cache');
|
|
101
|
+
const resp2 = await fetch(cacheTestUrl);
|
|
102
|
+
assert('second fetch ok', resp2.ok);
|
|
103
|
+
const body2 = await resp2.text();
|
|
104
|
+
assert('second fetch body same length', body2.length === body1.length);
|
|
105
|
+
t.section('timing: network vs cache');
|
|
106
|
+
const timingUrl = 'https://httpbin.org/cache/60?t=' + String(Date.now());
|
|
107
|
+
trackedUrls.push(timingUrl);
|
|
108
|
+
const t0 = Date.now();
|
|
109
|
+
const rNet = await fetch(timingUrl);
|
|
110
|
+
const t1 = Date.now();
|
|
111
|
+
const timeNet = t1 - t0;
|
|
112
|
+
assert('network fetch ok', rNet.ok);
|
|
113
|
+
const bodyNet = await rNet.text();
|
|
114
|
+
assert('network body non-empty', bodyNet.length > 0);
|
|
115
|
+
const t2 = Date.now();
|
|
116
|
+
const rCache = await fetch(timingUrl);
|
|
117
|
+
const t3 = Date.now();
|
|
118
|
+
const timeCache = t3 - t2;
|
|
119
|
+
assert('cache fetch ok', rCache.ok);
|
|
120
|
+
const bodyCache = await rCache.text();
|
|
121
|
+
assert('cache body same length', bodyCache.length === bodyNet.length);
|
|
122
|
+
assert('cache faster than network (' + timeCache + 'ms vs ' + timeNet + 'ms)', timeCache < timeNet);
|
|
123
|
+
t.section('cleanup');
|
|
124
|
+
for (const url of trackedUrls) {
|
|
125
|
+
const key = __httpCache__.cacheKey(url);
|
|
126
|
+
os.remove(cacheDir + '/' + key + '.meta');
|
|
127
|
+
os.remove(cacheDir + '/' + key + '.body');
|
|
128
|
+
assert('no meta leftover for ' + key, __httpCache__.readMeta(url) === null);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
};
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import '../lib/fetch.js'
|
|
2
|
+
import * as std from 'std'
|
|
3
|
+
import * as os from 'os'
|
|
4
|
+
import { Tester } from './test_helper.js'
|
|
5
|
+
|
|
6
|
+
function getCacheDir(): string {
|
|
7
|
+
const url = import.meta.url
|
|
8
|
+
let path = url.slice(7)
|
|
9
|
+
if (path.length >= 3 && path[1] === ':') path = path.slice(1)
|
|
10
|
+
const idx = path.lastIndexOf('/')
|
|
11
|
+
if (idx < 0) return '_cache'
|
|
12
|
+
const buildDir = path.slice(0, idx)
|
|
13
|
+
const idx2 = buildDir.lastIndexOf('/')
|
|
14
|
+
if (idx2 < 0) return '_cache'
|
|
15
|
+
return buildDir.slice(0, idx2 + 1) + '_cache'
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export const suite = {
|
|
19
|
+
name: 'fetch-cache',
|
|
20
|
+
run: async (t: Tester) => {
|
|
21
|
+
function assert(name: string, ok: boolean): void {
|
|
22
|
+
if (ok) { t.ok++; std.printf(' PASS: %s\n', name) }
|
|
23
|
+
else { t.fail++; std.printf(' FAIL: %s\n', name) }
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const cacheDir = getCacheDir()
|
|
27
|
+
const trackedUrls: string[] = []
|
|
28
|
+
|
|
29
|
+
t.section('__httpCache__ API exists')
|
|
30
|
+
assert('__httpCache__ is defined', typeof __httpCache__ !== 'undefined')
|
|
31
|
+
if (!__httpCache__) return
|
|
32
|
+
|
|
33
|
+
assert('readMeta is function', typeof __httpCache__.readMeta === 'function')
|
|
34
|
+
assert('readBody is function', typeof __httpCache__.readBody === 'function')
|
|
35
|
+
assert('writeCache is function', typeof __httpCache__.writeCache === 'function')
|
|
36
|
+
assert('writeMeta is function', typeof __httpCache__.writeMeta === 'function')
|
|
37
|
+
assert('cacheKey is function', typeof __httpCache__.cacheKey === 'function')
|
|
38
|
+
|
|
39
|
+
t.section('writeCache + readMeta + readBody')
|
|
40
|
+
const fakeUrl = 'https://test.local/cache-test'
|
|
41
|
+
trackedUrls.push(fakeUrl)
|
|
42
|
+
const testBody = 'hello cache test!'
|
|
43
|
+
__httpCache__.writeCache(fakeUrl, 60, testBody)
|
|
44
|
+
|
|
45
|
+
const metaStr = __httpCache__.readMeta(fakeUrl)
|
|
46
|
+
assert('meta written', metaStr !== null)
|
|
47
|
+
if (metaStr) {
|
|
48
|
+
const meta = JSON.parse(metaStr)
|
|
49
|
+
assert('meta has storedAt', typeof meta.storedAt === 'number')
|
|
50
|
+
assert('meta has maxAge', meta.maxAge === 60)
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const fullMeta = JSON.stringify({
|
|
54
|
+
storedAt: Math.floor(Date.now() / 1000),
|
|
55
|
+
maxAge: 60,
|
|
56
|
+
status: 200,
|
|
57
|
+
statusText: 'OK',
|
|
58
|
+
headers: { 'content-type': 'text/plain' },
|
|
59
|
+
})
|
|
60
|
+
__httpCache__.writeMeta(fakeUrl, fullMeta)
|
|
61
|
+
|
|
62
|
+
const metaStr2 = __httpCache__.readMeta(fakeUrl)
|
|
63
|
+
assert('meta overwritten', metaStr2 !== null)
|
|
64
|
+
if (metaStr2) {
|
|
65
|
+
const meta2 = JSON.parse(metaStr2)
|
|
66
|
+
assert('meta has status', meta2.status === 200)
|
|
67
|
+
assert('meta has headers', meta2.headers['content-type'] === 'text/plain')
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const bodyAb = __httpCache__.readBody(fakeUrl)
|
|
71
|
+
assert('body read as ArrayBuffer', bodyAb !== null)
|
|
72
|
+
if (bodyAb) {
|
|
73
|
+
const view = new Uint8Array(bodyAb)
|
|
74
|
+
let str = ''
|
|
75
|
+
for (let i = 0; i < view.length; i++) str += String.fromCharCode(view[i])
|
|
76
|
+
assert('body content correct', str === testBody)
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
t.section('cacheKey')
|
|
80
|
+
const key = __httpCache__.cacheKey('https://example.com/test.js')
|
|
81
|
+
assert('cacheKey returns 16 hex chars', /^[0-9a-f]{16}$/.test(key))
|
|
82
|
+
|
|
83
|
+
t.section('fetch with caching')
|
|
84
|
+
const cacheTestUrl = 'https://httpbin.org/cache/60'
|
|
85
|
+
trackedUrls.push(cacheTestUrl)
|
|
86
|
+
|
|
87
|
+
const resp1 = await fetch(cacheTestUrl)
|
|
88
|
+
assert('first fetch ok', resp1.ok)
|
|
89
|
+
const body1 = await resp1.text()
|
|
90
|
+
assert('first fetch body non-empty', body1.length > 0)
|
|
91
|
+
|
|
92
|
+
const cachedMeta = __httpCache__.readMeta(cacheTestUrl)
|
|
93
|
+
assert('cached meta exists', cachedMeta !== null)
|
|
94
|
+
if (cachedMeta) {
|
|
95
|
+
const m = JSON.parse(cachedMeta)
|
|
96
|
+
assert('cached status = 200', m.status === 200)
|
|
97
|
+
assert('cached maxAge = 60', m.maxAge === 60)
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
const cachedBody = __httpCache__.readBody(cacheTestUrl)
|
|
101
|
+
assert('cached body exists', cachedBody !== null)
|
|
102
|
+
if (cachedBody) {
|
|
103
|
+
assert('cached body matches', cachedBody.byteLength > 0)
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
t.section('second fetch hits cache')
|
|
107
|
+
const resp2 = await fetch(cacheTestUrl)
|
|
108
|
+
assert('second fetch ok', resp2.ok)
|
|
109
|
+
const body2 = await resp2.text()
|
|
110
|
+
assert('second fetch body same length', body2.length === body1.length)
|
|
111
|
+
|
|
112
|
+
t.section('timing: network vs cache')
|
|
113
|
+
const timingUrl = 'https://httpbin.org/cache/60?t=' + String(Date.now())
|
|
114
|
+
trackedUrls.push(timingUrl)
|
|
115
|
+
|
|
116
|
+
const t0 = Date.now()
|
|
117
|
+
const rNet = await fetch(timingUrl)
|
|
118
|
+
const t1 = Date.now()
|
|
119
|
+
const timeNet = t1 - t0
|
|
120
|
+
assert('network fetch ok', rNet.ok)
|
|
121
|
+
const bodyNet = await rNet.text()
|
|
122
|
+
assert('network body non-empty', bodyNet.length > 0)
|
|
123
|
+
|
|
124
|
+
const t2 = Date.now()
|
|
125
|
+
const rCache = await fetch(timingUrl)
|
|
126
|
+
const t3 = Date.now()
|
|
127
|
+
const timeCache = t3 - t2
|
|
128
|
+
assert('cache fetch ok', rCache.ok)
|
|
129
|
+
const bodyCache = await rCache.text()
|
|
130
|
+
assert('cache body same length', bodyCache.length === bodyNet.length)
|
|
131
|
+
assert('cache faster than network (' + timeCache + 'ms vs ' + timeNet + 'ms)', timeCache < timeNet)
|
|
132
|
+
|
|
133
|
+
t.section('cleanup')
|
|
134
|
+
for (const url of trackedUrls) {
|
|
135
|
+
const key = __httpCache__.cacheKey(url)
|
|
136
|
+
os.remove(cacheDir + '/' + key + '.meta')
|
|
137
|
+
os.remove(cacheDir + '/' + key + '.body')
|
|
138
|
+
assert('no meta leftover for ' + key, __httpCache__.readMeta(url) === null)
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
}
|