mimium-web 4.0.0-alpha → 4.0.0-alpha.1
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/mimium_web.js +4 -4
- package/mimium_web_bg.wasm +0 -0
- package/package.json +5 -3
- package/snippets/mimium-lang-53ba5794920c42b3/inline0.js +181 -0
- package/snippets/mimium-lang-53ba5794920c42b3/src/utils/fileloader.cjs +189 -0
- package/snippets/mimium-lang-bc3c111c42789f9d/src/utils/fileloader.cjs +189 -0
package/mimium_web.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { preload_mimium_lib_cache, read_file } from './snippets/mimium-lang-
|
|
1
|
+
import { preload_mimium_lib_cache, read_file } from './snippets/mimium-lang-bc3c111c42789f9d/src/utils/fileloader.cjs';
|
|
2
2
|
|
|
3
3
|
let wasm;
|
|
4
4
|
|
|
@@ -587,7 +587,7 @@ function __wbg_get_imports() {
|
|
|
587
587
|
const ret = arg0.open();
|
|
588
588
|
return ret;
|
|
589
589
|
};
|
|
590
|
-
imports.wbg.
|
|
590
|
+
imports.wbg.__wbg_preloadmimiumlibcache_02ba785a202b39ee = function() { return handleError(function (arg0, arg1) {
|
|
591
591
|
const ret = preload_mimium_lib_cache(getStringFromWasm0(arg0, arg1));
|
|
592
592
|
return ret;
|
|
593
593
|
}, arguments) };
|
|
@@ -598,7 +598,7 @@ function __wbg_get_imports() {
|
|
|
598
598
|
const ret = arg0.queueMicrotask;
|
|
599
599
|
return ret;
|
|
600
600
|
};
|
|
601
|
-
imports.wbg.
|
|
601
|
+
imports.wbg.__wbg_readfile_d9e969093a8ac499 = function() { return handleError(function (arg0, arg1, arg2) {
|
|
602
602
|
const ret = read_file(getStringFromWasm0(arg1, arg2));
|
|
603
603
|
const ptr1 = passStringToWasm0(ret, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
604
604
|
const len1 = WASM_VECTOR_LEN;
|
|
@@ -663,7 +663,7 @@ function __wbg_get_imports() {
|
|
|
663
663
|
const ret = false;
|
|
664
664
|
return ret;
|
|
665
665
|
};
|
|
666
|
-
imports.wbg.
|
|
666
|
+
imports.wbg.__wbindgen_closure_wrapper2147 = function(arg0, arg1, arg2) {
|
|
667
667
|
const ret = makeMutClosure(arg0, arg1, 650, __wbg_adapter_28);
|
|
668
668
|
return ret;
|
|
669
669
|
};
|
package/mimium_web_bg.wasm
CHANGED
|
Binary file
|
package/package.json
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
"Tomoya Matsuura <me@matsuuratomoya.com>"
|
|
6
6
|
],
|
|
7
7
|
"description": "mimium wasm binding package.",
|
|
8
|
-
"version": "4.0.0-alpha",
|
|
8
|
+
"version": "4.0.0-alpha.1",
|
|
9
9
|
"license": "MPL-2.0",
|
|
10
10
|
"repository": {
|
|
11
11
|
"type": "git",
|
|
@@ -14,11 +14,13 @@
|
|
|
14
14
|
"files": [
|
|
15
15
|
"mimium_web_bg.wasm",
|
|
16
16
|
"mimium_web.js",
|
|
17
|
-
"mimium_web.d.ts"
|
|
17
|
+
"mimium_web.d.ts",
|
|
18
|
+
"snippets",
|
|
19
|
+
"snippets/**"
|
|
18
20
|
],
|
|
19
21
|
"main": "mimium_web.js",
|
|
20
22
|
"types": "mimium_web.d.ts",
|
|
21
23
|
"sideEffects": [
|
|
22
24
|
"./snippets/*"
|
|
23
25
|
]
|
|
24
|
-
}
|
|
26
|
+
}
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
|
|
2
|
+
const isNode = typeof process !== "undefined" && !!(process.versions && process.versions.node);
|
|
3
|
+
const fs = isNode && typeof require === "function" ? require("fs") : null;
|
|
4
|
+
|
|
5
|
+
const DEFAULT_GITHUB_LIB_BASE = "https://raw.githubusercontent.com/mimium-org/mimium-rs/";
|
|
6
|
+
const DEFAULT_GITHUB_TAG = "dev";
|
|
7
|
+
const LIB_FILES = [
|
|
8
|
+
"core.mmm",
|
|
9
|
+
"delay.mmm",
|
|
10
|
+
"env.mmm",
|
|
11
|
+
"filter.mmm",
|
|
12
|
+
"math.mmm",
|
|
13
|
+
"noise.mmm",
|
|
14
|
+
"osc.mmm",
|
|
15
|
+
"reactive.mmm",
|
|
16
|
+
"reverb.mmm",
|
|
17
|
+
];
|
|
18
|
+
|
|
19
|
+
const memoryCache = new Map();
|
|
20
|
+
let lastPreloadBaseUrl = "";
|
|
21
|
+
|
|
22
|
+
function normalizePath(path) {
|
|
23
|
+
if (typeof path !== "string") {
|
|
24
|
+
return "";
|
|
25
|
+
}
|
|
26
|
+
const slash = path.replace(/\\/g, "/").trim();
|
|
27
|
+
const withoutPrefix = slash
|
|
28
|
+
.replace(/^\/+/, "")
|
|
29
|
+
.replace(/^\.\//, "")
|
|
30
|
+
.replace(/^lib\//, "");
|
|
31
|
+
const collapsed = withoutPrefix
|
|
32
|
+
.split("/")
|
|
33
|
+
.filter((seg) => seg.length > 0 && seg !== ".")
|
|
34
|
+
.join("/");
|
|
35
|
+
return collapsed;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function putMemoryAliases(filename, content) {
|
|
39
|
+
const normalized = normalizePath(filename);
|
|
40
|
+
if (!normalized) {
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
memoryCache.set(normalized, content);
|
|
44
|
+
memoryCache.set(`./${normalized}`, content);
|
|
45
|
+
memoryCache.set(`lib/${normalized}`, content);
|
|
46
|
+
memoryCache.set(`/lib/${normalized}`, content);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export function read_file(path) {
|
|
50
|
+
if (isNode && fs) {
|
|
51
|
+
return fs.readFileSync(path, { encoding: "utf8" });
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const normalized = normalizePath(path);
|
|
55
|
+
const cached =
|
|
56
|
+
memoryCache.get(path) ??
|
|
57
|
+
memoryCache.get(normalized) ??
|
|
58
|
+
memoryCache.get(`./${normalized}`) ??
|
|
59
|
+
memoryCache.get(`lib/${normalized}`) ??
|
|
60
|
+
memoryCache.get(`/lib/${normalized}`);
|
|
61
|
+
|
|
62
|
+
if (cached !== undefined) {
|
|
63
|
+
return cached;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
throw new Error(
|
|
67
|
+
`Include target not found in browser cache: ${path}. Call preload_mimium_lib_cache() before compile().`
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export function get_env(key) {
|
|
72
|
+
if (isNode) {
|
|
73
|
+
return process.env[key];
|
|
74
|
+
}
|
|
75
|
+
return undefined;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
async function getOpfsLibDirectory() {
|
|
79
|
+
const cacheDirName = getCacheDirName();
|
|
80
|
+
if (!globalThis.navigator || !navigator.storage || !navigator.storage.getDirectory) {
|
|
81
|
+
return null;
|
|
82
|
+
}
|
|
83
|
+
const root = await navigator.storage.getDirectory();
|
|
84
|
+
return root.getDirectoryHandle(cacheDirName, { create: true });
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function getTagFromBaseUrl(baseUrl) {
|
|
88
|
+
const normalized = (baseUrl || "").replace(/\/+$/, "");
|
|
89
|
+
const marker = "raw.githubusercontent.com/mimium-org/mimium-rs/";
|
|
90
|
+
const markerIdx = normalized.indexOf(marker);
|
|
91
|
+
if (markerIdx < 0) {
|
|
92
|
+
return DEFAULT_GITHUB_TAG;
|
|
93
|
+
}
|
|
94
|
+
const suffix = normalized.slice(markerIdx + marker.length);
|
|
95
|
+
const tag = suffix.split("/")[0];
|
|
96
|
+
return tag || DEFAULT_GITHUB_TAG;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function getCacheDirName() {
|
|
100
|
+
return `mimium-lib-${getTagFromBaseUrl(globalThis.__mimium_lib_base_url || "")}`;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
async function readFromOpfs(filename) {
|
|
104
|
+
const dir = await getOpfsLibDirectory();
|
|
105
|
+
if (!dir) {
|
|
106
|
+
return null;
|
|
107
|
+
}
|
|
108
|
+
try {
|
|
109
|
+
const handle = await dir.getFileHandle(filename, { create: false });
|
|
110
|
+
const file = await handle.getFile();
|
|
111
|
+
return await file.text();
|
|
112
|
+
} catch {
|
|
113
|
+
return null;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
async function writeToOpfs(filename, content) {
|
|
118
|
+
const dir = await getOpfsLibDirectory();
|
|
119
|
+
if (!dir) {
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
const handle = await dir.getFileHandle(filename, { create: true });
|
|
123
|
+
const writable = await handle.createWritable();
|
|
124
|
+
await writable.write(content);
|
|
125
|
+
await writable.close();
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
async function fetchLibFile(baseUrl, filename) {
|
|
129
|
+
const url = `${baseUrl}${filename}`;
|
|
130
|
+
const response = await fetch(url, { cache: "no-cache" });
|
|
131
|
+
if (!response.ok) {
|
|
132
|
+
throw new Error(`Failed to fetch ${url}: ${response.status} ${response.statusText}`);
|
|
133
|
+
}
|
|
134
|
+
return response.text();
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
export async function preload_mimium_lib_cache(base_url) {
|
|
138
|
+
const baseUrlCandidate =
|
|
139
|
+
base_url && base_url.length > 0
|
|
140
|
+
? base_url
|
|
141
|
+
: `${DEFAULT_GITHUB_LIB_BASE}${DEFAULT_GITHUB_TAG}/lib/`;
|
|
142
|
+
const baseUrl = baseUrlCandidate.endsWith("/") ? baseUrlCandidate : `${baseUrlCandidate}/`;
|
|
143
|
+
globalThis.__mimium_lib_base_url = baseUrl;
|
|
144
|
+
lastPreloadBaseUrl = baseUrl;
|
|
145
|
+
|
|
146
|
+
if (isNode) {
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
for (const filename of LIB_FILES) {
|
|
151
|
+
const fromMemory = memoryCache.get(filename);
|
|
152
|
+
if (fromMemory !== undefined) {
|
|
153
|
+
putMemoryAliases(filename, fromMemory);
|
|
154
|
+
continue;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
const fromOpfs = await readFromOpfs(filename);
|
|
158
|
+
if (fromOpfs !== null) {
|
|
159
|
+
putMemoryAliases(filename, fromOpfs);
|
|
160
|
+
continue;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
const fetched = await fetchLibFile(baseUrl, filename);
|
|
164
|
+
putMemoryAliases(filename, fetched);
|
|
165
|
+
await writeToOpfs(filename, fetched);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
export function __mimium_test_put_cache(path, content) {
|
|
170
|
+
putMemoryAliases(path, content);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
export function __mimium_test_clear_cache() {
|
|
174
|
+
memoryCache.clear();
|
|
175
|
+
lastPreloadBaseUrl = "";
|
|
176
|
+
delete globalThis.__mimium_lib_base_url;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
export function __mimium_test_get_last_preload_base_url() {
|
|
180
|
+
return lastPreloadBaseUrl;
|
|
181
|
+
}
|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
let fs = null;
|
|
2
|
+
const isNode = typeof process !== "undefined" && !!(process.versions && process.versions.node);
|
|
3
|
+
if (isNode) {
|
|
4
|
+
fs = require("fs");
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
const DEFAULT_GITHUB_LIB_BASE = "https://raw.githubusercontent.com/mimium-org/mimium-rs/";
|
|
8
|
+
const DEFAULT_GITHUB_TAG = "dev";
|
|
9
|
+
const LIB_FILES = [
|
|
10
|
+
"core.mmm",
|
|
11
|
+
"delay.mmm",
|
|
12
|
+
"env.mmm",
|
|
13
|
+
"filter.mmm",
|
|
14
|
+
"math.mmm",
|
|
15
|
+
"noise.mmm",
|
|
16
|
+
"osc.mmm",
|
|
17
|
+
"reactive.mmm",
|
|
18
|
+
"reverb.mmm",
|
|
19
|
+
];
|
|
20
|
+
|
|
21
|
+
const memoryCache = new Map();
|
|
22
|
+
let lastPreloadBaseUrl = "";
|
|
23
|
+
|
|
24
|
+
function normalizePath(path) {
|
|
25
|
+
if (typeof path !== "string") {
|
|
26
|
+
return "";
|
|
27
|
+
}
|
|
28
|
+
const slash = path.replace(/\\/g, "/").trim();
|
|
29
|
+
const withoutPrefix = slash
|
|
30
|
+
.replace(/^\/+/, "")
|
|
31
|
+
.replace(/^\.\//, "")
|
|
32
|
+
.replace(/^lib\//, "");
|
|
33
|
+
const collapsed = withoutPrefix
|
|
34
|
+
.split("/")
|
|
35
|
+
.filter((seg) => seg.length > 0 && seg !== ".")
|
|
36
|
+
.join("/");
|
|
37
|
+
return collapsed;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function putMemoryAliases(filename, content) {
|
|
41
|
+
const normalized = normalizePath(filename);
|
|
42
|
+
if (!normalized) {
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
memoryCache.set(normalized, content);
|
|
46
|
+
memoryCache.set(`./${normalized}`, content);
|
|
47
|
+
memoryCache.set(`lib/${normalized}`, content);
|
|
48
|
+
memoryCache.set(`/lib/${normalized}`, content);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function read_file(path) {
|
|
52
|
+
if (isNode) {
|
|
53
|
+
return fs.readFileSync(path, { encoding: "utf8" });
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const normalized = normalizePath(path);
|
|
57
|
+
const cached =
|
|
58
|
+
memoryCache.get(path) ??
|
|
59
|
+
memoryCache.get(normalized) ??
|
|
60
|
+
memoryCache.get(`./${normalized}`) ??
|
|
61
|
+
memoryCache.get(`lib/${normalized}`) ??
|
|
62
|
+
memoryCache.get(`/lib/${normalized}`);
|
|
63
|
+
|
|
64
|
+
if (cached !== undefined) {
|
|
65
|
+
return cached;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
throw new Error(
|
|
69
|
+
`Include target not found in browser cache: ${path}. Call preload_mimium_lib_cache() before compile().`
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
exports.read_file = read_file;
|
|
73
|
+
|
|
74
|
+
function get_env(key) {
|
|
75
|
+
if (isNode) {
|
|
76
|
+
return process.env[key];
|
|
77
|
+
}
|
|
78
|
+
return undefined;
|
|
79
|
+
}
|
|
80
|
+
exports.get_env = get_env;
|
|
81
|
+
|
|
82
|
+
async function getOpfsLibDirectory() {
|
|
83
|
+
const cacheDirName = getCacheDirName();
|
|
84
|
+
if (!globalThis.navigator || !navigator.storage || !navigator.storage.getDirectory) {
|
|
85
|
+
return null;
|
|
86
|
+
}
|
|
87
|
+
const root = await navigator.storage.getDirectory();
|
|
88
|
+
return root.getDirectoryHandle(cacheDirName, { create: true });
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function getTagFromBaseUrl(baseUrl) {
|
|
92
|
+
const normalized = (baseUrl || "").replace(/\/+$/, "");
|
|
93
|
+
const marker = "raw.githubusercontent.com/mimium-org/mimium-rs/";
|
|
94
|
+
const markerIdx = normalized.indexOf(marker);
|
|
95
|
+
if (markerIdx < 0) {
|
|
96
|
+
return DEFAULT_GITHUB_TAG;
|
|
97
|
+
}
|
|
98
|
+
const suffix = normalized.slice(markerIdx + marker.length);
|
|
99
|
+
const tag = suffix.split("/")[0];
|
|
100
|
+
return tag || DEFAULT_GITHUB_TAG;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function getCacheDirName() {
|
|
104
|
+
return `mimium-lib-${getTagFromBaseUrl(globalThis.__mimium_lib_base_url || "")}`;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
async function readFromOpfs(filename) {
|
|
108
|
+
const dir = await getOpfsLibDirectory();
|
|
109
|
+
if (!dir) {
|
|
110
|
+
return null;
|
|
111
|
+
}
|
|
112
|
+
try {
|
|
113
|
+
const handle = await dir.getFileHandle(filename, { create: false });
|
|
114
|
+
const file = await handle.getFile();
|
|
115
|
+
return await file.text();
|
|
116
|
+
} catch {
|
|
117
|
+
return null;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
async function writeToOpfs(filename, content) {
|
|
122
|
+
const dir = await getOpfsLibDirectory();
|
|
123
|
+
if (!dir) {
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
const handle = await dir.getFileHandle(filename, { create: true });
|
|
127
|
+
const writable = await handle.createWritable();
|
|
128
|
+
await writable.write(content);
|
|
129
|
+
await writable.close();
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
async function fetchLibFile(baseUrl, filename) {
|
|
133
|
+
const url = `${baseUrl}${filename}`;
|
|
134
|
+
const response = await fetch(url, { cache: "no-cache" });
|
|
135
|
+
if (!response.ok) {
|
|
136
|
+
throw new Error(`Failed to fetch ${url}: ${response.status} ${response.statusText}`);
|
|
137
|
+
}
|
|
138
|
+
return response.text();
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
async function preload_mimium_lib_cache(base_url) {
|
|
142
|
+
const baseUrlCandidate =
|
|
143
|
+
base_url && base_url.length > 0
|
|
144
|
+
? base_url
|
|
145
|
+
: `${DEFAULT_GITHUB_LIB_BASE}${DEFAULT_GITHUB_TAG}/lib/`;
|
|
146
|
+
const baseUrl = baseUrlCandidate.endsWith("/") ? baseUrlCandidate : `${baseUrlCandidate}/`;
|
|
147
|
+
globalThis.__mimium_lib_base_url = baseUrl;
|
|
148
|
+
lastPreloadBaseUrl = baseUrl;
|
|
149
|
+
|
|
150
|
+
if (isNode) {
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
for (const filename of LIB_FILES) {
|
|
155
|
+
const fromMemory = memoryCache.get(filename);
|
|
156
|
+
if (fromMemory !== undefined) {
|
|
157
|
+
putMemoryAliases(filename, fromMemory);
|
|
158
|
+
continue;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
const fromOpfs = await readFromOpfs(filename);
|
|
162
|
+
if (fromOpfs !== null) {
|
|
163
|
+
putMemoryAliases(filename, fromOpfs);
|
|
164
|
+
continue;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
const fetched = await fetchLibFile(baseUrl, filename);
|
|
168
|
+
putMemoryAliases(filename, fetched);
|
|
169
|
+
await writeToOpfs(filename, fetched);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
exports.preload_mimium_lib_cache = preload_mimium_lib_cache;
|
|
173
|
+
|
|
174
|
+
function __mimium_test_put_cache(path, content) {
|
|
175
|
+
putMemoryAliases(path, content);
|
|
176
|
+
}
|
|
177
|
+
exports.__mimium_test_put_cache = __mimium_test_put_cache;
|
|
178
|
+
|
|
179
|
+
function __mimium_test_clear_cache() {
|
|
180
|
+
memoryCache.clear();
|
|
181
|
+
lastPreloadBaseUrl = "";
|
|
182
|
+
delete globalThis.__mimium_lib_base_url;
|
|
183
|
+
}
|
|
184
|
+
exports.__mimium_test_clear_cache = __mimium_test_clear_cache;
|
|
185
|
+
|
|
186
|
+
function __mimium_test_get_last_preload_base_url() {
|
|
187
|
+
return lastPreloadBaseUrl;
|
|
188
|
+
}
|
|
189
|
+
exports.__mimium_test_get_last_preload_base_url = __mimium_test_get_last_preload_base_url;
|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
let fs = null;
|
|
2
|
+
const isNode = typeof process !== "undefined" && !!(process.versions && process.versions.node);
|
|
3
|
+
if (isNode) {
|
|
4
|
+
fs = require("fs");
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
const DEFAULT_GITHUB_LIB_BASE = "https://raw.githubusercontent.com/mimium-org/mimium-rs/";
|
|
8
|
+
const DEFAULT_GITHUB_TAG = "dev";
|
|
9
|
+
const LIB_FILES = [
|
|
10
|
+
"core.mmm",
|
|
11
|
+
"delay.mmm",
|
|
12
|
+
"env.mmm",
|
|
13
|
+
"filter.mmm",
|
|
14
|
+
"math.mmm",
|
|
15
|
+
"noise.mmm",
|
|
16
|
+
"osc.mmm",
|
|
17
|
+
"reactive.mmm",
|
|
18
|
+
"reverb.mmm",
|
|
19
|
+
];
|
|
20
|
+
|
|
21
|
+
const memoryCache = new Map();
|
|
22
|
+
let lastPreloadBaseUrl = "";
|
|
23
|
+
|
|
24
|
+
function normalizePath(path) {
|
|
25
|
+
if (typeof path !== "string") {
|
|
26
|
+
return "";
|
|
27
|
+
}
|
|
28
|
+
const slash = path.replace(/\\/g, "/").trim();
|
|
29
|
+
const withoutPrefix = slash
|
|
30
|
+
.replace(/^\/+/, "")
|
|
31
|
+
.replace(/^\.\//, "")
|
|
32
|
+
.replace(/^lib\//, "");
|
|
33
|
+
const collapsed = withoutPrefix
|
|
34
|
+
.split("/")
|
|
35
|
+
.filter((seg) => seg.length > 0 && seg !== ".")
|
|
36
|
+
.join("/");
|
|
37
|
+
return collapsed;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function putMemoryAliases(filename, content) {
|
|
41
|
+
const normalized = normalizePath(filename);
|
|
42
|
+
if (!normalized) {
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
memoryCache.set(normalized, content);
|
|
46
|
+
memoryCache.set(`./${normalized}`, content);
|
|
47
|
+
memoryCache.set(`lib/${normalized}`, content);
|
|
48
|
+
memoryCache.set(`/lib/${normalized}`, content);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function read_file(path) {
|
|
52
|
+
if (isNode) {
|
|
53
|
+
return fs.readFileSync(path, { encoding: "utf8" });
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const normalized = normalizePath(path);
|
|
57
|
+
const cached =
|
|
58
|
+
memoryCache.get(path) ??
|
|
59
|
+
memoryCache.get(normalized) ??
|
|
60
|
+
memoryCache.get(`./${normalized}`) ??
|
|
61
|
+
memoryCache.get(`lib/${normalized}`) ??
|
|
62
|
+
memoryCache.get(`/lib/${normalized}`);
|
|
63
|
+
|
|
64
|
+
if (cached !== undefined) {
|
|
65
|
+
return cached;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
throw new Error(
|
|
69
|
+
`Include target not found in browser cache: ${path}. Call preload_mimium_lib_cache() before compile().`
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
exports.read_file = read_file;
|
|
73
|
+
|
|
74
|
+
function get_env(key) {
|
|
75
|
+
if (isNode) {
|
|
76
|
+
return process.env[key];
|
|
77
|
+
}
|
|
78
|
+
return undefined;
|
|
79
|
+
}
|
|
80
|
+
exports.get_env = get_env;
|
|
81
|
+
|
|
82
|
+
async function getOpfsLibDirectory() {
|
|
83
|
+
const cacheDirName = getCacheDirName();
|
|
84
|
+
if (!globalThis.navigator || !navigator.storage || !navigator.storage.getDirectory) {
|
|
85
|
+
return null;
|
|
86
|
+
}
|
|
87
|
+
const root = await navigator.storage.getDirectory();
|
|
88
|
+
return root.getDirectoryHandle(cacheDirName, { create: true });
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function getTagFromBaseUrl(baseUrl) {
|
|
92
|
+
const normalized = (baseUrl || "").replace(/\/+$/, "");
|
|
93
|
+
const marker = "raw.githubusercontent.com/mimium-org/mimium-rs/";
|
|
94
|
+
const markerIdx = normalized.indexOf(marker);
|
|
95
|
+
if (markerIdx < 0) {
|
|
96
|
+
return DEFAULT_GITHUB_TAG;
|
|
97
|
+
}
|
|
98
|
+
const suffix = normalized.slice(markerIdx + marker.length);
|
|
99
|
+
const tag = suffix.split("/")[0];
|
|
100
|
+
return tag || DEFAULT_GITHUB_TAG;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function getCacheDirName() {
|
|
104
|
+
return `mimium-lib-${getTagFromBaseUrl(globalThis.__mimium_lib_base_url || "")}`;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
async function readFromOpfs(filename) {
|
|
108
|
+
const dir = await getOpfsLibDirectory();
|
|
109
|
+
if (!dir) {
|
|
110
|
+
return null;
|
|
111
|
+
}
|
|
112
|
+
try {
|
|
113
|
+
const handle = await dir.getFileHandle(filename, { create: false });
|
|
114
|
+
const file = await handle.getFile();
|
|
115
|
+
return await file.text();
|
|
116
|
+
} catch {
|
|
117
|
+
return null;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
async function writeToOpfs(filename, content) {
|
|
122
|
+
const dir = await getOpfsLibDirectory();
|
|
123
|
+
if (!dir) {
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
const handle = await dir.getFileHandle(filename, { create: true });
|
|
127
|
+
const writable = await handle.createWritable();
|
|
128
|
+
await writable.write(content);
|
|
129
|
+
await writable.close();
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
async function fetchLibFile(baseUrl, filename) {
|
|
133
|
+
const url = `${baseUrl}${filename}`;
|
|
134
|
+
const response = await fetch(url, { cache: "no-cache" });
|
|
135
|
+
if (!response.ok) {
|
|
136
|
+
throw new Error(`Failed to fetch ${url}: ${response.status} ${response.statusText}`);
|
|
137
|
+
}
|
|
138
|
+
return response.text();
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
async function preload_mimium_lib_cache(base_url) {
|
|
142
|
+
const baseUrlCandidate =
|
|
143
|
+
base_url && base_url.length > 0
|
|
144
|
+
? base_url
|
|
145
|
+
: `${DEFAULT_GITHUB_LIB_BASE}${DEFAULT_GITHUB_TAG}/lib/`;
|
|
146
|
+
const baseUrl = baseUrlCandidate.endsWith("/") ? baseUrlCandidate : `${baseUrlCandidate}/`;
|
|
147
|
+
globalThis.__mimium_lib_base_url = baseUrl;
|
|
148
|
+
lastPreloadBaseUrl = baseUrl;
|
|
149
|
+
|
|
150
|
+
if (isNode) {
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
for (const filename of LIB_FILES) {
|
|
155
|
+
const fromMemory = memoryCache.get(filename);
|
|
156
|
+
if (fromMemory !== undefined) {
|
|
157
|
+
putMemoryAliases(filename, fromMemory);
|
|
158
|
+
continue;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
const fromOpfs = await readFromOpfs(filename);
|
|
162
|
+
if (fromOpfs !== null) {
|
|
163
|
+
putMemoryAliases(filename, fromOpfs);
|
|
164
|
+
continue;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
const fetched = await fetchLibFile(baseUrl, filename);
|
|
168
|
+
putMemoryAliases(filename, fetched);
|
|
169
|
+
await writeToOpfs(filename, fetched);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
exports.preload_mimium_lib_cache = preload_mimium_lib_cache;
|
|
173
|
+
|
|
174
|
+
function __mimium_test_put_cache(path, content) {
|
|
175
|
+
putMemoryAliases(path, content);
|
|
176
|
+
}
|
|
177
|
+
exports.__mimium_test_put_cache = __mimium_test_put_cache;
|
|
178
|
+
|
|
179
|
+
function __mimium_test_clear_cache() {
|
|
180
|
+
memoryCache.clear();
|
|
181
|
+
lastPreloadBaseUrl = "";
|
|
182
|
+
delete globalThis.__mimium_lib_base_url;
|
|
183
|
+
}
|
|
184
|
+
exports.__mimium_test_clear_cache = __mimium_test_clear_cache;
|
|
185
|
+
|
|
186
|
+
function __mimium_test_get_last_preload_base_url() {
|
|
187
|
+
return lastPreloadBaseUrl;
|
|
188
|
+
}
|
|
189
|
+
exports.__mimium_test_get_last_preload_base_url = __mimium_test_get_last_preload_base_url;
|