-
Notifications
You must be signed in to change notification settings - Fork 1
/
pyodide.js
57 lines (48 loc) · 1.27 KB
/
pyodide.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
const crypto = require('node:crypto')
const fs = require('node:fs')
const { loadPyodide } = require('pyodide')
const { once } = require('./utils.js')
async function installPackages(/**@type {import('pyodide').PyodideInterface}*/ pyodide) {
await pyodide.loadPackage(['Brotli', 'fonttools'], {
messageCallback: () => {}
})
}
/**
* @type {() => Promise<import('pyodide').PyodideInterface>}
*/
const preparePyodide = once(
async function(options) {
const pyodide = await loadPyodide(options)
await installPackages(pyodide)
return pyodide
}
)
class PyodideFile {
static of(options) {
return new PyodideFile(options)
}
constructor(options) {
if (!new.target) {
return this.constructor.of(options)
}
this.pyodide = options.pyodide
this.id = options.id ?? crypto.randomUUID()
this.filename = options.filename ?? this.id
}
async upload(fontFile = this.filename) {
fontFile = (typeof fontFile === 'string' || fontFile instanceof URL)
? await fs.promises.readFile(fontFile)
: fontFile
return this.pyodide.FS.writeFile(this.id, fontFile)
}
download() {
return this.pyodide.FS.readFile(this.id)
}
delete() {
this.pyodide.FS.unlink(this.id)
}
}
module.exports = {
preparePyodide,
PyodideFile
}