|
1 |
| -let chunks = [] |
2 |
| -let last_post = 0 |
| 1 | +let chunks = []; |
| 2 | +let last_post = 0; |
3 | 3 |
|
4 | 4 | function print(tty) {
|
5 | 5 | if (tty.output && tty.output.length > 0) {
|
6 |
| - chunks.push(tty.output) |
7 |
| - tty.output = [] |
8 |
| - const now = performance.now() |
| 6 | + chunks.push(tty.output); |
| 7 | + tty.output = []; |
| 8 | + const now = performance.now(); |
9 | 9 | if (now - last_post > 100) {
|
10 |
| - post() |
11 |
| - last_post = now |
| 10 | + post(); |
| 11 | + last_post = now; |
12 | 12 | }
|
13 | 13 | }
|
14 | 14 | }
|
15 | 15 |
|
16 | 16 | function post() {
|
17 |
| - self.postMessage(chunks) |
18 |
| - chunks = [] |
| 17 | + self.postMessage(chunks); |
| 18 | + chunks = []; |
19 | 19 | }
|
20 | 20 |
|
21 | 21 | function make_tty_ops() {
|
22 | 22 | return {
|
23 | 23 | put_char(tty, val) {
|
24 | 24 | if (val !== null) {
|
25 |
| - tty.output.push(val) |
| 25 | + tty.output.push(val); |
26 | 26 | }
|
27 | 27 | if (val === null || val === 10) {
|
28 |
| - print(tty) |
| 28 | + print(tty); |
29 | 29 | }
|
30 | 30 | },
|
31 | 31 | flush(tty) {
|
32 |
| - print(tty) |
| 32 | + print(tty); |
33 | 33 | },
|
34 |
| - } |
| 34 | + }; |
35 | 35 | }
|
36 | 36 |
|
37 | 37 | function setupStreams(FS, TTY) {
|
38 |
| - let mytty = FS.makedev(FS.createDevice.major++, 0) |
39 |
| - let myttyerr = FS.makedev(FS.createDevice.major++, 0) |
40 |
| - TTY.register(mytty, make_tty_ops()) |
41 |
| - TTY.register(myttyerr, make_tty_ops()) |
42 |
| - FS.mkdev('/dev/mytty', mytty) |
43 |
| - FS.mkdev('/dev/myttyerr', myttyerr) |
44 |
| - FS.unlink('/dev/stdin') |
45 |
| - FS.unlink('/dev/stdout') |
46 |
| - FS.unlink('/dev/stderr') |
47 |
| - FS.symlink('/dev/mytty', '/dev/stdin') |
48 |
| - FS.symlink('/dev/mytty', '/dev/stdout') |
49 |
| - FS.symlink('/dev/myttyerr', '/dev/stderr') |
50 |
| - FS.closeStream(0) |
51 |
| - FS.closeStream(1) |
52 |
| - FS.closeStream(2) |
53 |
| - FS.open('/dev/stdin', 0) |
54 |
| - FS.open('/dev/stdout', 1) |
55 |
| - FS.open('/dev/stderr', 1) |
| 38 | + let mytty = FS.makedev(FS.createDevice.major++, 0); |
| 39 | + let myttyerr = FS.makedev(FS.createDevice.major++, 0); |
| 40 | + TTY.register(mytty, make_tty_ops()); |
| 41 | + TTY.register(myttyerr, make_tty_ops()); |
| 42 | + FS.mkdev('/dev/mytty', mytty); |
| 43 | + FS.mkdev('/dev/myttyerr', myttyerr); |
| 44 | + FS.unlink('/dev/stdin'); |
| 45 | + FS.unlink('/dev/stdout'); |
| 46 | + FS.unlink('/dev/stderr'); |
| 47 | + FS.symlink('/dev/mytty', '/dev/stdin'); |
| 48 | + FS.symlink('/dev/mytty', '/dev/stdout'); |
| 49 | + FS.symlink('/dev/myttyerr', '/dev/stderr'); |
| 50 | + FS.closeStream(0); |
| 51 | + FS.closeStream(1); |
| 52 | + FS.closeStream(2); |
| 53 | + FS.open('/dev/stdin', 0); |
| 54 | + FS.open('/dev/stdout', 1); |
| 55 | + FS.open('/dev/stderr', 1); |
56 | 56 | }
|
57 | 57 |
|
58 | 58 | async function get(url, mode) {
|
59 |
| - const r = await fetch(url) |
| 59 | + const r = await fetch(url); |
60 | 60 | if (r.ok) {
|
61 | 61 | if (mode === 'text') {
|
62 |
| - return await r.text() |
| 62 | + return await r.text(); |
63 | 63 | } else {
|
64 | 64 | const blob = await r.blob();
|
65 | 65 | let buffer = await blob.arrayBuffer();
|
66 |
| - return btoa(new Uint8Array(buffer).reduce((data, byte) => data + String.fromCharCode(byte), '')) |
| 66 | + return btoa(new Uint8Array(buffer).reduce((data, byte) => data + String.fromCharCode(byte), '')); |
67 | 67 | }
|
68 | 68 | } else {
|
69 |
| - let text = await r.text() |
70 |
| - console.error('unexpected response', r, text) |
71 |
| - throw new Error(`${r.status}: ${text}`) |
| 69 | + let text = await r.text(); |
| 70 | + console.error('unexpected response', r, text); |
| 71 | + throw new Error(`${r.status}: ${text}`); |
72 | 72 | }
|
73 | 73 | }
|
74 | 74 |
|
75 | 75 | self.onmessage = async ({data}) => {
|
76 |
| - const {version} = data |
77 |
| - self.postMessage(`Downloading repo v${version} archive to get tests...\n`) |
78 |
| - const zip_url = `https://githubproxy.samuelcolvin.workers.dev/samuelcolvin/pydantic-core/archive/refs/tags/v${version}.zip` |
| 76 | + const {version} = data; |
| 77 | + self.postMessage(`Downloading repo v${version} archive to get tests...\n`); |
| 78 | + const zip_url = `https://githubproxy.samuelcolvin.workers.dev/samuelcolvin/pydantic-core/archive/refs/tags/v${version}.zip`; |
79 | 79 | try {
|
80 |
| - const [python_code, tests_zip,] = await Promise.all([ |
| 80 | + const [python_code, tests_zip] = await Promise.all([ |
81 | 81 | get(`./run_tests.py?v=${Date.now()}`, 'text'),
|
82 | 82 | // e4cf2e2 commit matches the pydantic-core wheel being used, so tests should pass
|
83 | 83 | get(zip_url, 'blob'),
|
84 |
| - importScripts('https://cdn.jsdelivr.net/pyodide/v0.21.0a3/full/pyodide.js') |
85 |
| - ]) |
| 84 | + importScripts('https://cdn.jsdelivr.net/pyodide/v0.21.0a3/full/pyodide.js'), |
| 85 | + ]); |
86 | 86 |
|
87 |
| - const pyodide = await loadPyodide() |
88 |
| - const {FS} = pyodide |
89 |
| - setupStreams(FS, pyodide._module.TTY) |
90 |
| - FS.mkdir('/test_dir') |
91 |
| - FS.chdir('/test_dir') |
92 |
| - await pyodide.loadPackage(['micropip', 'pytest', 'pytz']) |
93 |
| - await pyodide.runPythonAsync(python_code, {globals: pyodide.toPy({version, tests_zip})}) |
94 |
| - post() |
| 87 | + const pyodide = await loadPyodide(); |
| 88 | + const {FS} = pyodide; |
| 89 | + setupStreams(FS, pyodide._module.TTY); |
| 90 | + FS.mkdir('/test_dir'); |
| 91 | + FS.chdir('/test_dir'); |
| 92 | + await pyodide.loadPackage(['micropip', 'pytest', 'pytz']); |
| 93 | + await pyodide.runPythonAsync(python_code, {globals: pyodide.toPy({version, tests_zip})}); |
| 94 | + post(); |
95 | 95 | } catch (err) {
|
96 |
| - console.error(err) |
97 |
| - self.postMessage(`Error: ${err}\n`) |
| 96 | + console.error(err); |
| 97 | + self.postMessage(`Error: ${err}\n`); |
98 | 98 | }
|
99 |
| -} |
| 99 | +}; |
0 commit comments