@skillbuilder/miniapp-simulator 0.0.0-beta-20261023084421

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.
Files changed (220) hide show
  1. package/README.md +214 -0
  2. package/assets/phones/Nexus-5X.png +0 -0
  3. package/assets/phones/iPhone-14ProMax.png +0 -0
  4. package/assets/rednote-preview/cover/homebar-1439.jpg +0 -0
  5. package/assets/rednote-preview/cover/homebar-1679.jpg +0 -0
  6. package/assets/rednote-preview/cover/homebar-1919.jpg +0 -0
  7. package/assets/rednote-preview/cover/homebar-2284.jpg +0 -0
  8. package/assets/rednote-preview/cover/homebar-2285.jpg +0 -0
  9. package/assets/rednote-preview/cover/like.png +0 -0
  10. package/assets/rednote-preview/cover/play.png +0 -0
  11. package/assets/rednote-preview/cover/topbar-1439.jpg +0 -0
  12. package/assets/rednote-preview/cover/topbar-1679.jpg +0 -0
  13. package/assets/rednote-preview/cover/topbar-1919.jpg +0 -0
  14. package/assets/rednote-preview/cover/topbar-2284.jpg +0 -0
  15. package/assets/rednote-preview/cover/topbar-2285.jpg +0 -0
  16. package/assets/workbench/index.html +77 -0
  17. package/dist/sw/miniapp-simulator-service-worker.js +855 -0
  18. package/dist/sw/miniapp-simulator-service-worker.min.js +1 -0
  19. package/docs/consumer-guide.md +143 -0
  20. package/docs/electron-review.md +21 -0
  21. package/docs/external-cli-workbench.md +197 -0
  22. package/docs/file-system-design.md +334 -0
  23. package/docs/host-external-api.md +235 -0
  24. package/docs/iframe-service-worker.md +190 -0
  25. package/docs/local-dev.md +106 -0
  26. package/docs/npm-integration-verification.md +45 -0
  27. package/docs/remote-architecture.md +403 -0
  28. package/docs/remote-load-observability.md +268 -0
  29. package/docs/remote-usage.md +371 -0
  30. package/docs/request-design.md +247 -0
  31. package/docs/sdk-usage.md +260 -0
  32. package/docs/server-playground-https-debugging.md +150 -0
  33. package/docs/storage-api-design.md +275 -0
  34. package/docs/tracker.md +339 -0
  35. package/lib/business-line-CMb3ehRn.mjs +15 -0
  36. package/lib/client-BZtD2VtP.mjs +772 -0
  37. package/lib/index-BW85h7kT.mjs +505 -0
  38. package/lib/index.js +10686 -0
  39. package/lib/preload/webview-preload.js +37 -0
  40. package/lib/remote-client.js +11 -0
  41. package/lib/types/business-line.d.ts +7 -0
  42. package/lib/types/capabilities/constants.d.ts +28 -0
  43. package/lib/types/capabilities/csp-snippet.d.ts +32 -0
  44. package/lib/types/capabilities/launch-options.d.ts +7 -0
  45. package/lib/types/capabilities/performance-policy.d.ts +5 -0
  46. package/lib/types/capabilities/security-head-snippet.d.ts +20 -0
  47. package/lib/types/capabilities/security.d.ts +25 -0
  48. package/lib/types/capabilities/style-snippet.d.ts +2 -0
  49. package/lib/types/core/controllers/game-playtest.d.ts +9 -0
  50. package/lib/types/core/controllers/gesture.d.ts +8 -0
  51. package/lib/types/core/controllers/host-api.d.ts +19 -0
  52. package/lib/types/core/controllers/index.d.ts +11 -0
  53. package/lib/types/core/controllers/inspector.d.ts +20 -0
  54. package/lib/types/core/controllers/note-preview.d.ts +9 -0
  55. package/lib/types/core/controllers/permission-sheet.d.ts +7 -0
  56. package/lib/types/core/controllers/widget-cover.d.ts +12 -0
  57. package/lib/types/core/lifecycle.d.ts +7 -0
  58. package/lib/types/core/options.d.ts +33 -0
  59. package/lib/types/core/simulator.d.ts +9 -0
  60. package/lib/types/errors.d.ts +11 -0
  61. package/lib/types/event-bus.d.ts +15 -0
  62. package/lib/types/gesture/host-bus.d.ts +113 -0
  63. package/lib/types/gesture/index.d.ts +27 -0
  64. package/lib/types/gesture/plugins/double-tap.d.ts +10 -0
  65. package/lib/types/gesture/plugins/index.d.ts +27 -0
  66. package/lib/types/gesture/plugins/long-press.d.ts +13 -0
  67. package/lib/types/gesture/plugins/multi-touch-hint.d.ts +11 -0
  68. package/lib/types/gesture/plugins/multi-touch-switch.d.ts +18 -0
  69. package/lib/types/gesture/plugins/selection-disabler.d.ts +11 -0
  70. package/lib/types/gesture/plugins/tap.d.ts +11 -0
  71. package/lib/types/gesture/plugins/touch-emitter.d.ts +11 -0
  72. package/lib/types/gesture/plugins/wheel-scroll.d.ts +11 -0
  73. package/lib/types/gesture/receiver-runtime.d.ts +10 -0
  74. package/lib/types/gesture/receiver-runtime.impl.d.ts +6 -0
  75. package/lib/types/gesture/touch-cursor.d.ts +78 -0
  76. package/lib/types/gesture/touch-dot.d.ts +12 -0
  77. package/lib/types/gesture/types.d.ts +206 -0
  78. package/lib/types/host/shadow-host.d.ts +40 -0
  79. package/lib/types/index.d.ts +49 -0
  80. package/lib/types/injection/guest-log-mark.d.ts +7 -0
  81. package/lib/types/injection/head-builder.d.ts +4 -0
  82. package/lib/types/injection/index.d.ts +19 -0
  83. package/lib/types/injection/modules/console-forward.d.ts +2 -0
  84. package/lib/types/injection/modules/gesture.d.ts +6 -0
  85. package/lib/types/injection/modules/inspector-guest-runtime.d.ts +5 -0
  86. package/lib/types/injection/modules/inspector-pause-runtime.d.ts +6 -0
  87. package/lib/types/injection/modules/inspector.d.ts +8 -0
  88. package/lib/types/injection/modules/jsapi-channel.d.ts +9 -0
  89. package/lib/types/injection/modules/jsapi.d.ts +22 -0
  90. package/lib/types/injection/modules/render-done.d.ts +2 -0
  91. package/lib/types/injection/modules/resource-guard.d.ts +7 -0
  92. package/lib/types/injection/modules/scroll-containment.d.ts +3 -0
  93. package/lib/types/injection/modules/security.d.ts +3 -0
  94. package/lib/types/injection/modules/widget-snapshot.d.ts +13 -0
  95. package/lib/types/injection/preload-builder.d.ts +41 -0
  96. package/lib/types/injection/registry.d.ts +20 -0
  97. package/lib/types/injection/types.d.ts +43 -0
  98. package/lib/types/inspector/host-inspector.d.ts +108 -0
  99. package/lib/types/inspector/index.d.ts +8 -0
  100. package/lib/types/inspector/selector.d.ts +18 -0
  101. package/lib/types/native-ui/devtools/CapabilitiesView.d.ts +18 -0
  102. package/lib/types/native-ui/devtools/DevtoolsPanel.d.ts +24 -0
  103. package/lib/types/native-ui/devtools/LogsView.d.ts +17 -0
  104. package/lib/types/native-ui/devtools/dom.d.ts +40 -0
  105. package/lib/types/native-ui/devtools/index.d.ts +19 -0
  106. package/lib/types/native-ui/devtools/log-entry.d.ts +8 -0
  107. package/lib/types/native-ui/devtools/shortcut.d.ts +12 -0
  108. package/lib/types/native-ui/devtools/tokens.d.ts +14 -0
  109. package/lib/types/native-ui/devtools/types.d.ts +51 -0
  110. package/lib/types/native-ui/index.d.ts +17 -0
  111. package/lib/types/native-ui/note-publish/defaults.d.ts +40 -0
  112. package/lib/types/native-ui/note-publish/index.d.ts +2 -0
  113. package/lib/types/native-ui/note-publish/miniapp-note-publish.d.ts +15 -0
  114. package/lib/types/native-ui/note-publish/note-publish-host.d.ts +15 -0
  115. package/lib/types/native-ui/note-publish/view-data.d.ts +21 -0
  116. package/lib/types/native-ui/permission-sheet/defaults.d.ts +12 -0
  117. package/lib/types/native-ui/permission-sheet/index.d.ts +4 -0
  118. package/lib/types/native-ui/permission-sheet/miniapp-permission-sheet.d.ts +19 -0
  119. package/lib/types/native-ui/permission-sheet/permission-sheet-host.d.ts +23 -0
  120. package/lib/types/native-ui/permission-sheet/types.d.ts +8 -0
  121. package/lib/types/native-ui/rednote-preview/chrome-tone.d.ts +44 -0
  122. package/lib/types/native-ui/rednote-preview/defaults.d.ts +30 -0
  123. package/lib/types/native-ui/rednote-preview/half-sheet-drag.d.ts +61 -0
  124. package/lib/types/native-ui/rednote-preview/index.d.ts +2 -0
  125. package/lib/types/native-ui/rednote-preview/register.d.ts +7 -0
  126. package/lib/types/native-ui/rednote-preview/styles.d.ts +4 -0
  127. package/lib/types/native-ui/rednote-preview/xhs-note-detail-shell.d.ts +70 -0
  128. package/lib/types/native-ui/rednote-preview/xhs-note-half-sheet.d.ts +100 -0
  129. package/lib/types/native-ui/rednote-preview/xhs-profile-shell.d.ts +31 -0
  130. package/lib/types/native-ui/toast/frame-toast-host.d.ts +18 -0
  131. package/lib/types/native-ui/toast/index.d.ts +19 -0
  132. package/lib/types/presets.d.ts +11 -0
  133. package/lib/types/remote/client.d.ts +89 -0
  134. package/lib/types/remote/jsapi-config.d.ts +9 -0
  135. package/lib/types/remote/load-timing.d.ts +46 -0
  136. package/lib/types/remote/protocol.d.ts +160 -0
  137. package/lib/types/remote/review-launch.d.ts +6 -0
  138. package/lib/types/remote/runtime-entry.d.ts +1 -0
  139. package/lib/types/remote/runtime.d.ts +14 -0
  140. package/lib/types/remote/simulator-prompt.d.ts +9 -0
  141. package/lib/types/remote/widget-snapshot-guest-runtime.d.ts +1 -0
  142. package/lib/types/remote-client.d.ts +5 -0
  143. package/lib/types/renderer/base.d.ts +8 -0
  144. package/lib/types/renderer/content-host/iframe-content-host.d.ts +83 -0
  145. package/lib/types/renderer/content-host/index.d.ts +3 -0
  146. package/lib/types/renderer/content-host/types.d.ts +89 -0
  147. package/lib/types/renderer/content-host/webview-content-host.d.ts +53 -0
  148. package/lib/types/renderer/iframe.d.ts +93 -0
  149. package/lib/types/renderer/index.d.ts +2 -0
  150. package/lib/types/renderer/rednote-shell.d.ts +73 -0
  151. package/lib/types/renderer/webview.d.ts +59 -0
  152. package/lib/types/runtime/context.d.ts +31 -0
  153. package/lib/types/runtime/jsapi-modules/context.d.ts +19 -0
  154. package/lib/types/runtime/jsapi-modules/filesystem.d.ts +19 -0
  155. package/lib/types/runtime/jsapi-modules/index.d.ts +9 -0
  156. package/lib/types/runtime/jsapi-modules/interaction.d.ts +4 -0
  157. package/lib/types/runtime/jsapi-modules/location.d.ts +3 -0
  158. package/lib/types/runtime/jsapi-modules/media.d.ts +13 -0
  159. package/lib/types/runtime/jsapi-modules/network.d.ts +3 -0
  160. package/lib/types/runtime/jsapi-modules/storage.d.ts +26 -0
  161. package/lib/types/runtime/jsapi-modules/system.d.ts +4 -0
  162. package/lib/types/runtime/jsapi-modules/types.d.ts +21 -0
  163. package/lib/types/runtime/jsapi-modules/window-info.d.ts +43 -0
  164. package/lib/types/runtime/request/host-request.d.ts +13 -0
  165. package/lib/types/runtime/request/request-manager-frame.d.ts +29 -0
  166. package/lib/types/runtime/request/request-runtime.d.ts +26 -0
  167. package/lib/types/runtime/request/request-types.d.ts +26 -0
  168. package/lib/types/runtime/request/sse-text-framer.d.ts +16 -0
  169. package/lib/types/seal-config.d.ts +134 -0
  170. package/lib/types/sw/index.d.ts +9 -0
  171. package/lib/types/sw/parse-raw.d.ts +34 -0
  172. package/lib/types/sw/protocol.d.ts +17 -0
  173. package/lib/types/sw/served-entry.d.ts +9 -0
  174. package/lib/types/sw/service-worker-content-host.d.ts +11 -0
  175. package/lib/types/sw/service-worker-host.d.ts +73 -0
  176. package/lib/types/sw/service-worker-renderer.d.ts +10 -0
  177. package/lib/types/sw/worker/fs-codec.d.ts +23 -0
  178. package/lib/types/sw/worker/fs-ops.d.ts +22 -0
  179. package/lib/types/sw/worker/fs-store.d.ts +29 -0
  180. package/lib/types/sw/worker/globals.d.ts +49 -0
  181. package/lib/types/sw/worker/main.d.ts +6 -0
  182. package/lib/types/sw/worker/mime.d.ts +2 -0
  183. package/lib/types/sw/worker/mount-store.d.ts +43 -0
  184. package/lib/types/sw/worker/paths.d.ts +4 -0
  185. package/lib/types/sw/worker/serve.d.ts +13 -0
  186. package/lib/types/tracker/constants.d.ts +14 -0
  187. package/lib/types/tracker/default-tracker.d.ts +19 -0
  188. package/lib/types/tracker/index.d.ts +70 -0
  189. package/lib/types/transport/bridge-protocol.d.ts +78 -0
  190. package/lib/types/transport/iframe-transport.d.ts +39 -0
  191. package/lib/types/transport/index.d.ts +14 -0
  192. package/lib/types/transport/jsapi-bridge-messages.d.ts +36 -0
  193. package/lib/types/transport/types.d.ts +48 -0
  194. package/lib/types/transport/webview-transport.d.ts +52 -0
  195. package/lib/types/types/events.d.ts +130 -0
  196. package/lib/types/types/index.d.ts +11 -0
  197. package/lib/types/types/inspector.d.ts +172 -0
  198. package/lib/types/types/jsapi.d.ts +18 -0
  199. package/lib/types/types/options.d.ts +844 -0
  200. package/lib/types/types/renderer.d.ts +145 -0
  201. package/lib/types/utils/ancestor-scroll.d.ts +11 -0
  202. package/lib/types/utils/error-svg.d.ts +12 -0
  203. package/lib/types/utils/frameless-shell.d.ts +114 -0
  204. package/lib/types/utils/miniapp-chrome-svg.d.ts +47 -0
  205. package/lib/types/utils/package-constants.d.ts +8 -0
  206. package/lib/types/utils/screenshot.d.ts +14 -0
  207. package/lib/types/utils/simulator-pc-guard.d.ts +10 -0
  208. package/lib/types/utils/stage-fit.d.ts +6 -0
  209. package/lib/types/utils/touch-emulate.d.ts +26 -0
  210. package/lib/types/utils/zip-entry.d.ts +13 -0
  211. package/lib/types/workbench/index.d.ts +63 -0
  212. package/lib/types/workbench/preview-transition.d.ts +8 -0
  213. package/lib/types/workbench/server/artifact.d.ts +8 -0
  214. package/lib/types/workbench/server/index.d.ts +19 -0
  215. package/lib/types/workbench/server/watch.d.ts +6 -0
  216. package/lib/types/workbench/view.d.ts +36 -0
  217. package/lib/workbench-server.mjs +183 -0
  218. package/lib/workbench.js +1404 -0
  219. package/package.json +63 -0
  220. package/scripts/mock-cli-dev.mjs +52 -0
@@ -0,0 +1,855 @@
1
+ const ue = [
2
+ "fsWriteTempFile",
3
+ "fsSaveFile",
4
+ "fsWriteFile",
5
+ "fsAppendFile",
6
+ "fsReadFile",
7
+ "fsReadDir",
8
+ "fsStatFile",
9
+ "fsUnlink",
10
+ "fsMkdir",
11
+ "fsGetStorageInfo"
12
+ ], le = {
13
+ "image/png": "png",
14
+ "image/jpeg": "jpg",
15
+ "image/webp": "webp",
16
+ "image/gif": "gif",
17
+ "video/mp4": "mp4",
18
+ "video/quicktime": "mov",
19
+ "video/webm": "webm"
20
+ };
21
+ function V(e) {
22
+ const t = new Uint8Array(e);
23
+ if (A(t, [137, 80, 78, 71, 13, 10, 26, 10]))
24
+ return "image/png";
25
+ if (A(t, [255, 216, 255])) return "image/jpeg";
26
+ if (E(t, 0, "GIF87a") || E(t, 0, "GIF89a")) return "image/gif";
27
+ if (E(t, 0, "RIFF") && E(t, 8, "WEBP")) return "image/webp";
28
+ if (E(t, 4, "ftyp"))
29
+ return Y(t, 8, 12) === "qt " ? "video/quicktime" : "video/mp4";
30
+ if (A(t, [26, 69, 223, 163])) return "video/webm";
31
+ }
32
+ function fe(e) {
33
+ if (typeof e != "string" || e.length === 0)
34
+ return { error: "data is required", code: "ERR_INVALID_ARGS" };
35
+ const t = /^data:([^;,]+);base64,(.*)$/s.exec(e);
36
+ if (!t)
37
+ return { error: "only base64 data: URI is accepted", code: "ERR_INVALID_ARGS" };
38
+ const n = t[1].toLowerCase(), r = le[n];
39
+ if (!r)
40
+ return { error: `unsupported mime "${n}"`, code: "ERR_UNSUPPORTED_MIME" };
41
+ let i;
42
+ try {
43
+ i = atob(t[2]);
44
+ } catch {
45
+ return { error: "invalid base64 data", code: "ERR_INVALID_ARGS" };
46
+ }
47
+ const s = new Uint8Array(i.length);
48
+ for (let a = 0; a < i.length; a++) s[a] = i.charCodeAt(a);
49
+ return { bytes: s.buffer, mime: n, ext: r };
50
+ }
51
+ function A(e, t) {
52
+ return t.every((n, r) => e[r] === n);
53
+ }
54
+ function E(e, t, n) {
55
+ return Y(e, t, t + n.length) === n;
56
+ }
57
+ function Y(e, t, n) {
58
+ if (e.length < n) return "";
59
+ let r = "";
60
+ for (let i = t; i < n; i++) r += String.fromCharCode(e[i]);
61
+ return r;
62
+ }
63
+ const de = "miniapp-simulator-fs", he = 1, d = "files";
64
+ let b = null;
65
+ function _() {
66
+ return b || (b = new Promise((e, t) => {
67
+ const n = indexedDB.open(de, he);
68
+ n.onupgradeneeded = () => {
69
+ const r = n.result;
70
+ if (!r.objectStoreNames.contains(d)) {
71
+ const i = r.createObjectStore(d, { keyPath: ["scope", "path"] });
72
+ i.createIndex("by_scope", "scope", { unique: !1 }), i.createIndex("by_createdAt", "createdAt", { unique: !1 });
73
+ }
74
+ }, n.onsuccess = () => e(n.result), n.onerror = () => {
75
+ b = null, t(n.error ?? new Error("indexedDB open failed"));
76
+ }, n.onblocked = () => {
77
+ b = null, t(new Error("indexedDB open blocked"));
78
+ };
79
+ }), b);
80
+ }
81
+ function X(e) {
82
+ return new Promise((t, n) => {
83
+ e.onsuccess = () => t(e.result), e.onerror = () => n(e.error ?? new Error("indexedDB request failed"));
84
+ });
85
+ }
86
+ function I(e) {
87
+ return new Promise((t, n) => {
88
+ e.oncomplete = () => t(), e.onerror = () => n(e.error ?? new Error("indexedDB transaction failed")), e.onabort = () => n(e.error ?? new Error("indexedDB transaction aborted"));
89
+ });
90
+ }
91
+ function B(e, t, n, r, i) {
92
+ const s = Date.now();
93
+ return {
94
+ scope: e,
95
+ path: t,
96
+ mime: r,
97
+ bytes: new Blob([n], { type: r }),
98
+ isDir: i,
99
+ createdAt: s,
100
+ lastModified: s
101
+ };
102
+ }
103
+ async function Z(e, t, n, r = "application/octet-stream") {
104
+ const s = (await _()).transaction(d, "readwrite");
105
+ s.objectStore(d).put(B(e, t, n, r, !1)), await I(s);
106
+ }
107
+ async function L(e, t) {
108
+ const r = (await _()).transaction(d, "readwrite");
109
+ r.objectStore(d).put(B(e, t, new ArrayBuffer(0), "application/x-directory", !0)), await I(r);
110
+ }
111
+ async function S(e, t) {
112
+ const r = (await _()).transaction(d, "readonly").objectStore(d), i = await X(r.get([e, t]));
113
+ return i ? {
114
+ path: i.path,
115
+ bytes: await i.bytes.arrayBuffer(),
116
+ mime: i.mime,
117
+ isDir: i.isDir === !0,
118
+ lastModified: i.lastModified ?? i.createdAt ?? 0
119
+ } : null;
120
+ }
121
+ async function v(e) {
122
+ const t = await _();
123
+ return (await X(
124
+ t.transaction(d, "readonly").objectStore(d).index("by_scope").getAll(e)
125
+ )).map((r) => ({
126
+ path: r.path,
127
+ mime: r.mime,
128
+ size: r.isDir === !0 ? 0 : r.bytes.size,
129
+ isDir: r.isDir === !0,
130
+ lastModified: r.lastModified ?? r.createdAt ?? 0
131
+ }));
132
+ }
133
+ async function pe(e, t) {
134
+ const r = (await _()).transaction(d, "readwrite");
135
+ r.objectStore(d).delete([e, t]), await I(r);
136
+ }
137
+ async function me(e, t, n, r) {
138
+ const s = (await _()).transaction(d, "readwrite"), a = s.objectStore(d);
139
+ a.put(B(e, n, r.bytes, r.mime, !1)), a.delete([e, t]), await I(s);
140
+ }
141
+ const g = self, ge = "index.html", ye = {
142
+ html: "text/html; charset=utf-8",
143
+ htm: "text/html; charset=utf-8",
144
+ js: "text/javascript; charset=utf-8",
145
+ mjs: "text/javascript; charset=utf-8",
146
+ cjs: "text/javascript; charset=utf-8",
147
+ css: "text/css; charset=utf-8",
148
+ json: "application/json; charset=utf-8",
149
+ xml: "application/xml; charset=utf-8",
150
+ txt: "text/plain; charset=utf-8",
151
+ csv: "text/csv; charset=utf-8",
152
+ svg: "image/svg+xml",
153
+ wasm: "application/wasm",
154
+ map: "application/json; charset=utf-8"
155
+ }, we = {
156
+ png: "image/png",
157
+ jpg: "image/jpeg",
158
+ jpeg: "image/jpeg",
159
+ gif: "image/gif",
160
+ webp: "image/webp",
161
+ avif: "image/avif",
162
+ ico: "image/x-icon",
163
+ bmp: "image/bmp",
164
+ woff: "font/woff",
165
+ woff2: "font/woff2",
166
+ ttf: "font/ttf",
167
+ otf: "font/otf",
168
+ eot: "application/vnd.ms-fontobject",
169
+ mp4: "video/mp4",
170
+ webm: "video/webm",
171
+ ogg: "video/ogg",
172
+ mp3: "audio/mpeg",
173
+ wav: "audio/wav",
174
+ m4a: "audio/mp4"
175
+ };
176
+ function be(e) {
177
+ const t = e.split(".").pop()?.toLowerCase() ?? "";
178
+ return ye[t] ?? we[t] ?? "application/octet-stream";
179
+ }
180
+ function w(e) {
181
+ try {
182
+ return decodeURIComponent(e);
183
+ } catch {
184
+ return null;
185
+ }
186
+ }
187
+ function k(e) {
188
+ const t = [];
189
+ for (const n of e.replace(/\\/g, "/").split("/"))
190
+ if (!(n === "" || n === ".")) {
191
+ if (n === "..") return "";
192
+ t.push(n);
193
+ }
194
+ return t.join("/");
195
+ }
196
+ const m = /* @__PURE__ */ new Map(), q = 16;
197
+ function _e(e) {
198
+ if (!m.has(e.id) && m.size >= q) {
199
+ const n = new Error(`too many active simulator mounts (max ${q})`);
200
+ throw n.code = "ERR_MOUNT_LIMIT", n;
201
+ }
202
+ const t = /* @__PURE__ */ new Map();
203
+ for (const [n, r] of Object.entries(e.files)) {
204
+ const i = k(n);
205
+ i && t.set(i, { bytes: r, mime: be(i) });
206
+ }
207
+ return m.set(e.id, {
208
+ files: t,
209
+ injectHead: e.injectHead,
210
+ csp: e.csp,
211
+ remoteBase: e.remoteBase ?? "",
212
+ fsScope: e.fsScope ?? "",
213
+ ownerClientId: e.ownerClientId ?? ""
214
+ }), t.size;
215
+ }
216
+ function ve(e) {
217
+ m.delete(e);
218
+ }
219
+ function xe(e) {
220
+ let t = 0;
221
+ for (const [n, r] of m)
222
+ !r.ownerClientId || e.has(r.ownerClientId) || (m.delete(n), t += 1);
223
+ return t;
224
+ }
225
+ function Ee(e) {
226
+ let t;
227
+ try {
228
+ t = new URL(e).pathname;
229
+ } catch {
230
+ return null;
231
+ }
232
+ for (const [n, r] of m) {
233
+ const i = `/${n}/`, s = t.indexOf(i);
234
+ if (s === -1) continue;
235
+ let a = w(t.slice(s + i.length));
236
+ if (a === null) return null;
237
+ (a === "" || a.endsWith("/")) && (a += ge);
238
+ const o = k(a);
239
+ return o ? { app: r, filePath: o } : null;
240
+ }
241
+ return null;
242
+ }
243
+ function Re(e) {
244
+ let t;
245
+ try {
246
+ t = new URL(e).pathname;
247
+ } catch {
248
+ return null;
249
+ }
250
+ for (const [n, r] of m)
251
+ if (t.includes(`/${n}/`)) return r;
252
+ return null;
253
+ }
254
+ function Se(e, t, n) {
255
+ if (e.files.has(t)) return O(e, t);
256
+ if (e.remoteBase) {
257
+ const r = new URL(t, e.remoteBase);
258
+ return r.search = new URL(n.url).search, J(r.href);
259
+ }
260
+ return O(e, t);
261
+ }
262
+ async function De(e, t) {
263
+ try {
264
+ const n = await g.clients.get(e);
265
+ if (n) {
266
+ const r = Re(n.url);
267
+ if (r) {
268
+ const i = new URL(t.url), s = w(i.pathname), a = s === null ? "" : k(s);
269
+ if (a && r.files.has(a))
270
+ return O(r, a);
271
+ if (r.remoteBase) {
272
+ const o = new URL(i.pathname + i.search, new URL(r.remoteBase).origin);
273
+ return J(o.href);
274
+ }
275
+ }
276
+ }
277
+ } catch {
278
+ }
279
+ return fetch(t);
280
+ }
281
+ function Q(e, t) {
282
+ return new Response(e, {
283
+ status: 200,
284
+ headers: {
285
+ "Content-Type": t,
286
+ "Cache-Control": "no-store, no-cache, must-revalidate, max-age=0",
287
+ Expires: "0",
288
+ Pragma: "no-cache",
289
+ "Content-Length": String(e.byteLength)
290
+ }
291
+ });
292
+ }
293
+ function O(e, t) {
294
+ const n = e.files.get(t);
295
+ if (!n)
296
+ return new Response(`Not Found: ${t}`, {
297
+ status: 404,
298
+ headers: { "Content-Type": "text/plain; charset=utf-8" }
299
+ });
300
+ if (n.mime.startsWith("text/html")) {
301
+ const r = Pe(new TextDecoder("utf-8").decode(n.bytes), e.injectHead), i = {
302
+ "Content-Type": "text/html; charset=utf-8",
303
+ "Cache-Control": "no-store",
304
+ // 与 Runtime 文档取同一值:guest 与 Runtime 同 origin,agent cluster 归属按 origin
305
+ // 一次性确定,此处显式对齐,避免同 origin 内出现矛盾声明被浏览器忽略。
306
+ "Origin-Agent-Cluster": "?1"
307
+ };
308
+ return e.csp && (i["Content-Security-Policy"] = e.csp), new Response(r, { status: 200, headers: i });
309
+ }
310
+ return Q(n.bytes, n.mime);
311
+ }
312
+ async function J(e) {
313
+ try {
314
+ return await fetch(e, { method: "GET", credentials: "omit" });
315
+ } catch {
316
+ return new Response(`Bad Gateway: ${e}`, {
317
+ status: 502,
318
+ headers: { "Content-Type": "text/plain; charset=utf-8" }
319
+ });
320
+ }
321
+ }
322
+ function Pe(e, t) {
323
+ if (!t) return e;
324
+ const n = /<head\b[^>]*>/i.exec(e);
325
+ if (n) {
326
+ const i = n.index + n[0].length;
327
+ return e.slice(0, i) + t + e.slice(i);
328
+ }
329
+ const r = /<html\b[^>]*>/i.exec(e);
330
+ if (r) {
331
+ const i = r.index + r[0].length;
332
+ return `${e.slice(0, i)}<head>${t}</head>${e.slice(i)}`;
333
+ }
334
+ return `<head>${t}</head>${e}`;
335
+ }
336
+ const ee = 50 * 1024 * 1024, D = 2 * 1024 * 1024, U = 2 * 1024 * 1024, Ie = 200 * 1024 * 1024, Fe = 4, Te = 255, z = 1e3, N = /* @__PURE__ */ new Set(["tmp", "usr"]), F = /* @__PURE__ */ new Set(["usr"]), Me = /^[A-Za-z0-9._-]{1,64}$/, P = /* @__PURE__ */ new Map();
337
+ let G = Promise.resolve();
338
+ function T(e) {
339
+ const t = G.then(e, e);
340
+ return G = t.then(
341
+ () => {
342
+ },
343
+ () => {
344
+ }
345
+ ), t;
346
+ }
347
+ function Ae(e) {
348
+ P.delete(e);
349
+ }
350
+ async function Le(e) {
351
+ const t = (n) => ({
352
+ type: "FS_ACK",
353
+ op: e.op,
354
+ id: e.id,
355
+ reqId: e.reqId,
356
+ ...n
357
+ });
358
+ if (!m.has(e.id))
359
+ return t({ ok: !1, error: "simulator not ready", code: "ERR_NOT_MOUNTED" });
360
+ try {
361
+ const n = await Oe(e);
362
+ return t(n);
363
+ } catch {
364
+ return t({ ok: !1, code: 50101, error: "storage io error" });
365
+ }
366
+ }
367
+ async function Oe(e) {
368
+ const t = e.fsScope || e.id;
369
+ switch (e.op) {
370
+ case "fsWriteTempFile":
371
+ return Ue(t, e);
372
+ case "fsSaveFile":
373
+ return Ce(t, e);
374
+ case "fsWriteFile":
375
+ return H(t, e, !1);
376
+ case "fsAppendFile":
377
+ return H(t, e, !0);
378
+ case "fsReadFile":
379
+ return Be(t, e);
380
+ case "fsReadDir":
381
+ return ke(t, e);
382
+ case "fsStatFile":
383
+ return Ne(t, e);
384
+ case "fsUnlink":
385
+ return We(t, e);
386
+ case "fsMkdir":
387
+ return je(t, e);
388
+ case "fsGetStorageInfo":
389
+ return $e(t);
390
+ default:
391
+ return {
392
+ ok: !1,
393
+ error: `unsupported operation: ${e.op}`,
394
+ code: "ERR_UNSUPPORTED_OP"
395
+ };
396
+ }
397
+ }
398
+ async function Ue(e, t) {
399
+ const n = fe(t.data);
400
+ if ("error" in n) return { ok: !1, error: n.error, code: n.code };
401
+ const r = `tmp/${ae()}.${n.ext}`;
402
+ return await Z(e, r, n.bytes, n.mime), { ok: !0, result: { path: r, url: ce(e, r) } };
403
+ }
404
+ async function Ce(e, t) {
405
+ const n = h(t.tempFilePath, "tempFilePath");
406
+ if (n) return n;
407
+ if (t.filePath !== void 0 && t.filePath !== null) {
408
+ const s = h(t.filePath, "filePath");
409
+ if (s) return s;
410
+ }
411
+ const r = y(t.tempFilePath, e, /* @__PURE__ */ new Set(["tmp"]), !1);
412
+ if ("code" in r) return u(r);
413
+ const i = t.filePath === void 0 || t.filePath === null ? He(r.path) : y(t.filePath, e, F, !1);
414
+ return "code" in i ? u(i) : T(async () => {
415
+ const s = await S(e, r.path);
416
+ if (!s) return u({ code: 50202, message: "file not found; reason=not_found" });
417
+ if (s.isDir) return u({ code: 50205, message: "not a file" });
418
+ const a = await v(e), o = j(a);
419
+ if (o.has(i.path))
420
+ return u({ code: 50204, message: "file already exists" });
421
+ const c = ie(i.path, o);
422
+ if ("code" in c) return u(c);
423
+ const l = ne(t.id, a, s.bytes.byteLength, 0);
424
+ return l ? u(l) : (await se(e, c.missing), await me(e, r.path, i.path, s), re(t.id, s.bytes.byteLength), { ok: !0, result: { savedFilePath: ce(e, i.path) } });
425
+ });
426
+ }
427
+ async function H(e, t, n) {
428
+ const r = h(t.filePath, "filePath");
429
+ if (r) return r;
430
+ const i = te(t.data, "data", !0);
431
+ if (i) return i;
432
+ const s = h(t.encoding, "encoding");
433
+ if (s) return s;
434
+ const a = y(t.filePath, e, F, !1);
435
+ if ("code" in a) return u(a);
436
+ if (t.encoding !== "base64" && t.encoding !== "utf8")
437
+ return u({ code: 10001, message: "invalid parameter; reason=encoding_unsupported" });
438
+ const o = ze(t.data, t.encoding);
439
+ return "code" in o ? u(o) : o.bytes.byteLength > D ? u({
440
+ code: 50201,
441
+ message: "storage quota exceeded; reason=single_over_limit"
442
+ }) : T(async () => {
443
+ const c = await v(e), l = j(c), f = await S(e, a.path);
444
+ if (f?.isDir) return u({ code: 50205, message: "not a file" });
445
+ const p = ie(a.path, l);
446
+ if ("code" in p) return u(p);
447
+ const x = n ? 0 : f?.bytes.byteLength ?? 0, $ = ne(
448
+ t.id,
449
+ c,
450
+ o.bytes.byteLength,
451
+ x
452
+ );
453
+ if ($) return u($);
454
+ await se(e, p.missing);
455
+ let M = o.bytes;
456
+ return n && f && (M = Ke(f.bytes, o.bytes)), await Z(e, a.path, M, Ye(M, t.encoding)), re(t.id, o.bytes.byteLength), { ok: !0, result: { writtenBytes: o.bytes.byteLength } };
457
+ });
458
+ }
459
+ async function Be(e, t) {
460
+ const n = h(t.filePath, "filePath");
461
+ if (n) return n;
462
+ const r = h(t.encoding, "encoding");
463
+ if (r) return r;
464
+ const i = K(t.position, "position");
465
+ if (i) return i;
466
+ const s = K(t.length, "length");
467
+ if (s) return s;
468
+ const a = y(t.filePath, e, N, !1);
469
+ if ("code" in a) return u(a);
470
+ if (t.encoding !== "base64" && t.encoding !== "utf8")
471
+ return u({ code: 10001, message: "invalid parameter; reason=encoding_unsupported" });
472
+ const o = t.position === void 0 ? 0 : t.position, c = t.length === void 0 ? U : t.length;
473
+ if (!Number.isSafeInteger(o) || o < 0 || !Number.isSafeInteger(c) || c < 0 || c > U)
474
+ return u({ code: 10001, message: "invalid parameter; reason=range_invalid" });
475
+ const l = await S(e, a.path);
476
+ if (!l) return u({ code: 50202, message: "file not found" });
477
+ if (l.isDir) return u({ code: 50205, message: "not a file" });
478
+ const f = Math.min(l.bytes.byteLength, o + c), p = o >= l.bytes.byteLength ? new ArrayBuffer(0) : l.bytes.slice(o, f);
479
+ let x;
480
+ if (t.encoding === "base64")
481
+ x = Ve(p);
482
+ else
483
+ try {
484
+ x = new TextDecoder("utf-8", { fatal: !0 }).decode(p);
485
+ } catch {
486
+ return u({ code: 10001, message: "invalid parameter; reason=decode_failed" });
487
+ }
488
+ return {
489
+ ok: !0,
490
+ result: {
491
+ data: x,
492
+ bytesRead: p.byteLength,
493
+ eof: o + p.byteLength >= l.bytes.byteLength
494
+ }
495
+ };
496
+ }
497
+ async function ke(e, t) {
498
+ const n = h(t.dirPath, "dirPath");
499
+ if (n) return n;
500
+ const r = y(t.dirPath, e, N, !0);
501
+ if ("code" in r) return u(r);
502
+ const i = await v(e);
503
+ if (r.relativePath) {
504
+ const o = i.find((c) => c.path === r.path);
505
+ if (!o) return u({ code: 50202, message: "file not found" });
506
+ if (!o.isDir) return u({ code: 50206, message: "not a directory" });
507
+ }
508
+ const s = `${r.path}/`, a = i.filter((o) => {
509
+ if (!o.path.startsWith(s)) return !1;
510
+ const c = o.path.slice(s.length);
511
+ return c.length > 0 && !c.includes("/") && !R(o.path);
512
+ }).sort((o, c) => o.path.localeCompare(c.path));
513
+ return {
514
+ ok: !0,
515
+ result: {
516
+ files: a.slice(0, z).map((o) => ({
517
+ name: o.path.slice(s.length),
518
+ size: o.isDir ? 0 : o.size,
519
+ lastModified: o.lastModified,
520
+ isDir: o.isDir
521
+ })),
522
+ truncated: a.length > z
523
+ }
524
+ };
525
+ }
526
+ async function Ne(e, t) {
527
+ const n = h(t.filePath, "filePath");
528
+ if (n) return n;
529
+ const r = y(t.filePath, e, N, !1);
530
+ if ("code" in r) return u(r);
531
+ const i = (await v(e)).find((s) => s.path === r.path);
532
+ return i ? {
533
+ ok: !0,
534
+ result: {
535
+ size: i.isDir ? 0 : i.size,
536
+ lastModified: i.lastModified,
537
+ isDir: i.isDir
538
+ }
539
+ } : u({ code: 50202, message: "file not found" });
540
+ }
541
+ async function We(e, t) {
542
+ const n = h(t.filePath, "filePath");
543
+ if (n) return n;
544
+ const r = y(t.filePath, e, F, !1);
545
+ return "code" in r ? u(r) : T(async () => {
546
+ const i = await S(e, r.path);
547
+ return i ? i.isDir ? u({ code: 50205, message: "not a file" }) : (await pe(e, r.path), { ok: !0, result: {} }) : u({ code: 50202, message: "file not found" });
548
+ });
549
+ }
550
+ async function je(e, t) {
551
+ const n = h(t.dirPath, "dirPath");
552
+ if (n) return n;
553
+ if (t.recursive !== void 0 && typeof t.recursive != "boolean")
554
+ return W("recursive");
555
+ const r = y(t.dirPath, e, F, !1);
556
+ if ("code" in r) return u(r);
557
+ const i = t.recursive === !0;
558
+ return T(async () => {
559
+ const s = await v(e), a = j(s), o = a.get(r.path);
560
+ if (o)
561
+ return o.isDir ? i ? { ok: !0, result: {} } : u({ code: 50204, message: "file already exists" }) : u({ code: 50205, message: "not a file" });
562
+ const c = oe(r.path);
563
+ for (const l of c) {
564
+ const f = a.get(l);
565
+ if (f && !f.isDir)
566
+ return u({ code: 50205, message: "not a file" });
567
+ if (!f && !i)
568
+ return u({ code: 50202, message: "file not found" });
569
+ }
570
+ if (i)
571
+ for (const l of c)
572
+ a.has(l) || await L(e, l);
573
+ return await L(e, r.path), { ok: !0, result: {} };
574
+ });
575
+ }
576
+ async function $e(e) {
577
+ const t = await v(e), n = t.filter(
578
+ (i) => !i.isDir && i.path.startsWith("usr/") && !R(i.path)
579
+ ), r = t.filter(
580
+ (i) => !i.isDir && i.path.startsWith("tmp/") && !R(i.path)
581
+ );
582
+ return {
583
+ ok: !0,
584
+ result: {
585
+ usedBytes: C(n),
586
+ limitBytes: ee,
587
+ fileCount: n.length,
588
+ tmpUsedBytes: C(r),
589
+ writeChunkMaxBytes: D,
590
+ readChunkMaxBytes: U
591
+ }
592
+ };
593
+ }
594
+ function h(e, t) {
595
+ return te(e, t, !1);
596
+ }
597
+ function te(e, t, n) {
598
+ if (!(typeof e == "string" && (n || e.length > 0)))
599
+ return W(t);
600
+ }
601
+ function K(e, t) {
602
+ if (!(e === void 0 || typeof e == "number"))
603
+ return W(t);
604
+ }
605
+ function W(e) {
606
+ return { ok: !1, code: 10001, error: `invalid parameter; param=${e}` };
607
+ }
608
+ function u(e) {
609
+ return { ok: !1, code: e.code, error: e.message };
610
+ }
611
+ function y(e, t, n, r) {
612
+ const i = qe(e, t);
613
+ if (i === null)
614
+ return { code: 50203, message: "invalid path; reason=dir_not_allowed" };
615
+ if (i === "__DECODE_ERROR__")
616
+ return { code: 50203, message: "invalid path; reason=traversal" };
617
+ const s = i.indexOf("/"), o = (s === -1 ? i : i.slice(0, s)).toLowerCase(), c = s === -1 ? "" : i.slice(s + 1);
618
+ if (!n.has(o) || !r && c.length === 0)
619
+ return { code: 50203, message: "invalid path; reason=dir_not_allowed" };
620
+ if (i.includes("\0") || i.includes("\\") || c.split("/").includes(".."))
621
+ return { code: 50203, message: "invalid path; reason=traversal" };
622
+ if (new TextEncoder().encode(c).byteLength > Te)
623
+ return { code: 50203, message: "invalid path; reason=name_too_long" };
624
+ const l = c ? c.split("/") : [];
625
+ return l.length > Fe ? { code: 50203, message: "invalid path; reason=depth_exceeded" } : l.some((f) => f === "." || !Me.test(f)) ? { code: 50203, message: "invalid path; reason=name_invalid" } : l.some((f) => f.startsWith("__xhs")) ? { code: 50203, message: "invalid path; reason=reserved_name" } : {
626
+ dir: o,
627
+ relativePath: c,
628
+ path: c ? `${o}/${c}` : o
629
+ };
630
+ }
631
+ function qe(e, t) {
632
+ const n = w(e);
633
+ if (n === null) return "__DECODE_ERROR__";
634
+ const r = n.split(/[?#]/, 1)[0];
635
+ if (r.includes("\0") || r.includes("\\") || r.split("/").includes(".."))
636
+ return "__DECODE_ERROR__";
637
+ let i;
638
+ if (e.startsWith("xhsfile://"))
639
+ i = e.slice(10).split(/[?#]/, 1)[0];
640
+ else {
641
+ let a;
642
+ try {
643
+ a = new URL(e);
644
+ } catch {
645
+ return null;
646
+ }
647
+ if (a.protocol !== "http:" && a.protocol !== "https:") return null;
648
+ const o = "/_xhsfile_/", c = a.pathname.indexOf(o);
649
+ if (c === -1) return null;
650
+ const l = a.pathname.slice(c + o.length), f = l.indexOf("/");
651
+ if (f <= 0 || w(l.slice(0, f)) !== t) return null;
652
+ i = l.slice(f + 1);
653
+ }
654
+ const s = w(i);
655
+ return s === null ? "__DECODE_ERROR__" : s;
656
+ }
657
+ function ze(e, t) {
658
+ if (t === "utf8")
659
+ return e.length > D ? { code: 50201, message: "storage quota exceeded; reason=single_over_limit" } : { bytes: new TextEncoder().encode(e).buffer };
660
+ if (e.length > Math.ceil(D * 4 / 3) + 4)
661
+ return { code: 50201, message: "storage quota exceeded; reason=single_over_limit" };
662
+ if (!Ge(e))
663
+ return { code: 10001, message: "invalid parameter; reason=decode_failed" };
664
+ try {
665
+ const n = atob(e), r = new Uint8Array(n.length);
666
+ for (let i = 0; i < n.length; i++) r[i] = n.charCodeAt(i);
667
+ return { bytes: r.buffer };
668
+ } catch {
669
+ return { code: 10001, message: "invalid parameter; reason=decode_failed" };
670
+ }
671
+ }
672
+ function Ge(e) {
673
+ return e.length % 4 !== 0 ? !1 : /^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/.test(e);
674
+ }
675
+ function ne(e, t, n, r) {
676
+ if ((P.get(e) ?? 0) + n > Ie)
677
+ return { code: 50201, message: "storage quota exceeded; reason=session_limit" };
678
+ if (C(
679
+ t.filter(
680
+ (s) => !s.isDir && s.path.startsWith("usr/") && !R(s.path)
681
+ )
682
+ ) - r + n > ee)
683
+ return { code: 50201, message: "storage quota exceeded; reason=total_over_limit" };
684
+ }
685
+ function re(e, t) {
686
+ P.set(e, (P.get(e) ?? 0) + t);
687
+ }
688
+ function j(e) {
689
+ return new Map(e.map((t) => [t.path, t]));
690
+ }
691
+ function ie(e, t) {
692
+ const n = [];
693
+ for (const r of oe(e)) {
694
+ const i = t.get(r);
695
+ if (i && !i.isDir) return { code: 50205, message: "not a file" };
696
+ i || n.push(r);
697
+ }
698
+ return { missing: n };
699
+ }
700
+ async function se(e, t) {
701
+ for (const n of t) await L(e, n);
702
+ }
703
+ function oe(e) {
704
+ const t = e.split("/"), n = [];
705
+ for (let r = 2; r < t.length; r++)
706
+ n.push(t.slice(0, r).join("/"));
707
+ return n;
708
+ }
709
+ function He(e) {
710
+ const t = e.slice(e.lastIndexOf("/") + 1), n = t.lastIndexOf("."), r = n > 0 ? t.slice(n) : "", i = `${ae()}${r}`;
711
+ return { dir: "usr", relativePath: i, path: `usr/${i}` };
712
+ }
713
+ function Ke(e, t) {
714
+ const n = new Uint8Array(e.byteLength + t.byteLength);
715
+ return n.set(new Uint8Array(e), 0), n.set(new Uint8Array(t), e.byteLength), n.buffer;
716
+ }
717
+ function Ve(e) {
718
+ const t = new Uint8Array(e), n = 32768;
719
+ let r = "";
720
+ for (let i = 0; i < t.length; i += n)
721
+ r += String.fromCharCode(...t.subarray(i, i + n));
722
+ return btoa(r);
723
+ }
724
+ function Ye(e, t) {
725
+ return V(e) ?? (t === "utf8" ? "text/plain; charset=utf-8" : "application/octet-stream");
726
+ }
727
+ function C(e) {
728
+ return e.reduce((t, n) => t + n.size, 0);
729
+ }
730
+ function R(e) {
731
+ return e.split("/").some((t) => t.startsWith("__xhs"));
732
+ }
733
+ function ae() {
734
+ if (typeof crypto < "u" && typeof crypto.randomUUID == "function")
735
+ return crypto.randomUUID().replace(/-/g, "");
736
+ let e = "";
737
+ for (let t = 0; t < 32; t++) e += Math.floor(Math.random() * 16).toString(16);
738
+ return e;
739
+ }
740
+ function ce(e, t) {
741
+ return `${g.registration.scope}_xhsfile_/${encodeURIComponent(e)}/${t}`;
742
+ }
743
+ function Xe(e) {
744
+ let t;
745
+ try {
746
+ t = new URL(e).pathname;
747
+ } catch {
748
+ return null;
749
+ }
750
+ const n = "/_xhsfile_/", r = t.indexOf(n);
751
+ if (r === -1) return null;
752
+ const i = t.slice(r + n.length), s = i.indexOf("/");
753
+ if (s <= 0) return null;
754
+ const a = w(i.slice(0, s)), o = w(i.slice(s + 1));
755
+ return !a || !o || o.includes("\0") || o.includes("\\") || o.split("/").includes("..") ? null : { scope: a, path: o };
756
+ }
757
+ async function Ze(e, t) {
758
+ try {
759
+ const n = await S(e, t);
760
+ if (n && !n.isDir && !R(t)) {
761
+ const r = V(n.bytes);
762
+ if (r) return Q(n.bytes, r);
763
+ }
764
+ } catch {
765
+ }
766
+ return new Response("Not Found", {
767
+ status: 404,
768
+ headers: { "Content-Type": "text/plain; charset=utf-8" }
769
+ });
770
+ }
771
+ g.addEventListener("install", () => {
772
+ g.skipWaiting();
773
+ });
774
+ g.addEventListener("activate", (e) => {
775
+ e.waitUntil(g.clients.claim());
776
+ });
777
+ g.addEventListener("message", (e) => {
778
+ const t = e.data;
779
+ if (!t || typeof t.type != "string") return;
780
+ const n = (r) => e.ports?.[0]?.postMessage(r);
781
+ if (t.type === "MOUNT") {
782
+ const r = Qe(t, Je(e.source)).then(
783
+ (i) => n({
784
+ type: "MOUNT_ACK",
785
+ id: t.id,
786
+ ok: !0,
787
+ count: i,
788
+ swVersion: 3,
789
+ fsOps: ue
790
+ }),
791
+ (i) => {
792
+ const s = i;
793
+ n({
794
+ type: "MOUNT_ACK",
795
+ id: t.id,
796
+ ok: !1,
797
+ error: s instanceof Error ? s.message : String(i),
798
+ code: s?.code
799
+ });
800
+ }
801
+ );
802
+ e.waitUntil?.(r);
803
+ return;
804
+ }
805
+ if (t.type === "FS") {
806
+ const r = t, i = Le(r).then(
807
+ n,
808
+ (s) => (
809
+ // handleFsOp 内部已对已知错误 resolve 出准确 code;走到这里只可能是未预期的运行时异常,
810
+ // 用 ERR_UNKNOWN 如实反映(不要误标成 ERR_INVALID_ARGS 误导消费方分类)。
811
+ n({
812
+ type: "FS_ACK",
813
+ op: r.op,
814
+ id: r.id,
815
+ reqId: r.reqId,
816
+ ok: !1,
817
+ error: s instanceof Error ? s.message : String(s),
818
+ code: "ERR_UNKNOWN"
819
+ })
820
+ )
821
+ );
822
+ e.waitUntil?.(i);
823
+ return;
824
+ }
825
+ t.type === "UNMOUNT" && (Ae(t.id), ve(t.id));
826
+ });
827
+ g.addEventListener("fetch", (e) => {
828
+ if (e.request.method !== "GET") return;
829
+ const t = Xe(e.request.url);
830
+ if (t) {
831
+ e.respondWith(Ze(t.scope, t.path));
832
+ return;
833
+ }
834
+ const n = Ee(e.request.url);
835
+ if (n) {
836
+ e.respondWith(Se(n.app, n.filePath, e.request));
837
+ return;
838
+ }
839
+ const r = e.clientId || e.resultingClientId;
840
+ r && e.respondWith(De(r, e.request));
841
+ });
842
+ async function Qe(e, t) {
843
+ if (t)
844
+ try {
845
+ const n = await g.clients.matchAll({ type: "window", includeUncontrolled: !0 });
846
+ xe(new Set(n.map((r) => r.id)));
847
+ } catch {
848
+ }
849
+ return _e({ ...e, ownerClientId: t });
850
+ }
851
+ function Je(e) {
852
+ if (!e || typeof e != "object" || !("id" in e)) return "";
853
+ const t = e.id;
854
+ return typeof t == "string" ? t : "";
855
+ }