@skillbuilder/miniapp-simulator 0.0.0-beta-20261023084421 → 0.2.0

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 (37) hide show
  1. package/README.md +52 -2
  2. package/dist/sw/miniapp-simulator-service-worker.js +487 -371
  3. package/dist/sw/miniapp-simulator-service-worker.min.js +1 -1
  4. package/docs/external-cli-workbench.md +5 -7
  5. package/docs/npm-integration-verification.md +1 -11
  6. package/docs/remote-architecture.md +3 -2
  7. package/docs/worker-runtime.md +62 -0
  8. package/lib/{client-BZtD2VtP.mjs → client-CO-UjIY3.mjs} +141 -138
  9. package/lib/index-B5oixYXr.mjs +574 -0
  10. package/lib/index.js +1152 -1075
  11. package/lib/remote-client.js +2 -2
  12. package/lib/types/capabilities/constants.d.ts +1 -0
  13. package/lib/types/capabilities/csp-snippet.d.ts +2 -0
  14. package/lib/types/capabilities/security-head-snippet.d.ts +3 -2
  15. package/lib/types/index.d.ts +8 -1
  16. package/lib/types/injection/types.d.ts +2 -0
  17. package/lib/types/remote/protocol.d.ts +7 -2
  18. package/lib/types/renderer/content-host/iframe-content-host.d.ts +1 -0
  19. package/lib/types/sw/protocol.d.ts +3 -1
  20. package/lib/types/sw/served-entry.d.ts +1 -1
  21. package/lib/types/sw/service-worker-host.d.ts +1 -0
  22. package/lib/types/sw/worker/globals.d.ts +1 -0
  23. package/lib/types/sw/worker/mount-store.d.ts +5 -0
  24. package/lib/types/types/options.d.ts +8 -1
  25. package/lib/types/workbench/server/index.d.ts +1 -1
  26. package/lib/types/worker/adapters/csp-policy.d.ts +7 -0
  27. package/lib/types/worker/adapters/host-lifecycle.d.ts +5 -0
  28. package/lib/types/worker/adapters/mount-recovery.d.ts +2 -0
  29. package/lib/types/worker/adapters/package-policy.d.ts +12 -0
  30. package/lib/types/worker/adapters/page-lifecycle.d.ts +1 -0
  31. package/lib/types/worker/adapters/protocol.d.ts +49 -0
  32. package/lib/types/worker/adapters/scripts.d.ts +2 -0
  33. package/lib/types/worker/adapters/service-worker.d.ts +2 -0
  34. package/lib/workbench-server.mjs +1 -1
  35. package/lib/workbench.js +1 -1
  36. package/package.json +1 -1
  37. package/lib/index-BW85h7kT.mjs +0 -505
@@ -1,505 +0,0 @@
1
- const T = "0.1.10";
2
- var d = /* @__PURE__ */ ((e) => (e[e.INVALID_PARAM = 10001] = "INVALID_PARAM", e[e.API_UNAVAILABLE = 10002] = "API_UNAVAILABLE", e[e.URL_SCHEME_NOT_ALLOWED = 10003] = "URL_SCHEME_NOT_ALLOWED", e[e.REQUEST_TIMEOUT = 10101] = "REQUEST_TIMEOUT", e[e.REQUEST_ABORTED = 10102] = "REQUEST_ABORTED", e[e.REQUEST_FAILED = 15201] = "REQUEST_FAILED", e[e.STORAGE_IO_ERROR = 50101] = "STORAGE_IO_ERROR", e[e.STORAGE_QUOTA_EXCEEDED = 50201] = "STORAGE_QUOTA_EXCEEDED", e[e.FILE_NOT_FOUND = 50202] = "FILE_NOT_FOUND", e[e.PATH_INVALID = 50203] = "PATH_INVALID", e[e.FILE_ALREADY_EXISTS = 50204] = "FILE_ALREADY_EXISTS", e[e.NOT_A_FILE = 50205] = "NOT_A_FILE", e[e.NOT_A_DIRECTORY = 50206] = "NOT_A_DIRECTORY", e))(d || {});
3
- function f(e, t) {
4
- return Object.assign(new Error(t), { code: e });
5
- }
6
- class v {
7
- constructor() {
8
- this.buffer = "";
9
- }
10
- push(t) {
11
- return t && (this.buffer += t), this.drain(!1);
12
- }
13
- /** SSE 末尾没有空行时属于未完成帧,不向调用方发送。 */
14
- finish() {
15
- const t = this.drain(!0);
16
- return this.buffer = "", t;
17
- }
18
- drain(t) {
19
- const n = [];
20
- let r = 0, s = 0, o = 0;
21
- for (; o < this.buffer.length; ) {
22
- const i = this.buffer[o];
23
- if (i !== "\r" && i !== `
24
- `) {
25
- o += 1;
26
- continue;
27
- }
28
- let a = o + 1;
29
- if (i === "\r") {
30
- if (a === this.buffer.length && !t) break;
31
- this.buffer[a] === `
32
- ` && (a += 1);
33
- }
34
- o === s && (n.push(this.buffer.slice(r, a)), r = a), s = a, o = a;
35
- }
36
- return r > 0 && (this.buffer = this.buffer.slice(r)), n;
37
- }
38
- }
39
- function z() {
40
- let e = "";
41
- const t = (n) => {
42
- const r = [];
43
- let s = 0, o = 0, i = 0;
44
- for (; i < e.length; ) {
45
- const a = e[i];
46
- if (a !== "\r" && a !== `
47
- `) {
48
- i += 1;
49
- continue;
50
- }
51
- let c = i + 1;
52
- if (a === "\r") {
53
- if (c === e.length && !n) break;
54
- e[c] === `
55
- ` && (c += 1);
56
- }
57
- i === o && (r.push(e.slice(s, c)), s = c), o = c, i = c;
58
- }
59
- return s > 0 && (e = e.slice(s)), r;
60
- };
61
- return {
62
- push(n) {
63
- return n && (e += n), t(!1);
64
- },
65
- finish() {
66
- const n = t(!0);
67
- return e = "", n;
68
- }
69
- };
70
- }
71
- function I(e) {
72
- return e?.toLowerCase().includes("text/event-stream") ?? !1;
73
- }
74
- const E = /* @__PURE__ */ new WeakMap();
75
- async function W(e, t, n) {
76
- L(t.data);
77
- const r = E.get(e);
78
- if (r) return r(t, n);
79
- const s = await e(S(t, n.signal));
80
- if (n.signal.aborted)
81
- throw f(d.REQUEST_ABORTED, "request aborted");
82
- O(s);
83
- const o = U(s.header);
84
- return n.onHeaders({ taskId: t.taskId, statusCode: s.statusCode, header: o }), t.enableChunked && I(o["content-type"]) && (typeof s.data == "string" || D(s.data)) ? (await x(s.data, t.taskId, n), { taskId: t.taskId, statusCode: s.statusCode, data: "", header: o }) : {
85
- taskId: t.taskId,
86
- statusCode: s.statusCode,
87
- data: await N(s.data, t),
88
- header: o
89
- };
90
- }
91
- function S(e, t) {
92
- const n = e.data, r = { ...e.header }, s = e.method.toUpperCase();
93
- return s === "GET" || s === "HEAD" ? {
94
- url: w(e.url, b(n)),
95
- method: e.method,
96
- header: r,
97
- responseType: e.responseType,
98
- enableChunked: e.enableChunked,
99
- signal: t
100
- } : {
101
- url: e.url,
102
- method: e.method,
103
- header: r,
104
- data: R(n, r),
105
- responseType: e.responseType,
106
- enableChunked: e.enableChunked,
107
- signal: t
108
- };
109
- }
110
- function w(e, t) {
111
- if (!t) return e;
112
- const n = e.indexOf("#"), r = n >= 0 ? e.slice(n) : "", s = n >= 0 ? e.slice(0, n) : e;
113
- return `${s}${s.includes("?") ? "&" : "?"}${t}${r}`;
114
- }
115
- function b(e) {
116
- return e == null ? "" : typeof e == "string" ? e : typeof e != "object" || Array.isArray(e) ? String(e) : Object.entries(e).map(([t, n]) => `${encodeURIComponent(t)}=${encodeURIComponent(String(n))}`).join("&");
117
- }
118
- function R(e, t) {
119
- if (e == null || typeof e == "string") return e;
120
- let n = p(t, "content-type");
121
- return n || (n = "application/json", t["content-type"] = n), n.toLowerCase().includes("application/json") ? JSON.stringify(e) : n.toLowerCase().includes("application/x-www-form-urlencoded") ? b(e) : String(e);
122
- }
123
- function p(e, t) {
124
- const n = Object.keys(e).find((r) => r.toLowerCase() === t);
125
- return n ? String(e[n]) : void 0;
126
- }
127
- function L(e) {
128
- if (!(!(e instanceof ArrayBuffer) && !ArrayBuffer.isView(e)))
129
- throw f(
130
- d.INVALID_PARAM,
131
- "request data does not support ArrayBuffer"
132
- );
133
- }
134
- function O(e) {
135
- if (!e || typeof e != "object" || typeof e.statusCode != "number")
136
- throw f(
137
- d.REQUEST_FAILED,
138
- "host request adapter returned an invalid response"
139
- );
140
- }
141
- function D(e) {
142
- return !!(e && typeof e == "object" && "getReader" in e);
143
- }
144
- async function x(e, t, n) {
145
- const r = new v(), s = (c) => {
146
- for (const u of r.push(c)) n.onChunk({ taskId: t, chunk: u });
147
- };
148
- if (typeof e == "string") {
149
- s(e);
150
- for (const c of r.finish()) n.onChunk({ taskId: t, chunk: c });
151
- return;
152
- }
153
- const o = e.getReader(), i = new TextDecoder(), a = () => void o.cancel().catch(() => {
154
- });
155
- n.signal.addEventListener("abort", a, { once: !0 });
156
- try {
157
- for (; ; ) {
158
- if (n.signal.aborted) return;
159
- const u = await o.read();
160
- if (u.done) break;
161
- const l = typeof u.value == "string" ? u.value : i.decode(u.value, { stream: !0 });
162
- l && s(l);
163
- }
164
- const c = i.decode();
165
- c && s(c);
166
- for (const u of r.finish()) n.onChunk({ taskId: t, chunk: u });
167
- } finally {
168
- n.signal.removeEventListener("abort", a), o.releaseLock();
169
- }
170
- }
171
- function U(e) {
172
- const t = {};
173
- for (const [n, r] of Object.entries(e ?? {}))
174
- n.toLowerCase() !== "set-cookie" && (t[n.toLowerCase()] = String(r));
175
- return t;
176
- }
177
- async function N(e, t) {
178
- if (e instanceof ArrayBuffer || ArrayBuffer.isView(e))
179
- throw f(
180
- d.REQUEST_FAILED,
181
- "request response does not support ArrayBuffer"
182
- );
183
- if (t.dataType === "json" && typeof e == "string")
184
- try {
185
- return JSON.parse(e);
186
- } catch {
187
- return e;
188
- }
189
- return e;
190
- }
191
- const B = "miniapp_simulator_event", P = "skillbuilder-miniapp", A = {
192
- cn: {
193
- production: "https://apm-fe.xiaohongshu.com/api/data",
194
- development: "https://apm-fe-test.xiaohongshu.com/api/data"
195
- },
196
- sg: {
197
- production: "https://apm-fe.rnote.com/api/data",
198
- development: "https://apm-fe.rnote.com/api/data"
199
- }
200
- }, F = 10, j = 1e4;
201
- function _() {
202
- return typeof crypto < "u" && typeof crypto.randomUUID == "function" ? crypto.randomUUID() : `${Date.now().toString(16)}-${Math.random().toString(16).slice(2)}`;
203
- }
204
- function C(e, t) {
205
- const n = typeof window < "u";
206
- return {
207
- clientTime: Date.now(),
208
- context_sdkSessionId: t.sessionId,
209
- context_pageSessionId: t.pageSessionId,
210
- context_sdkSeqId: t.seqId,
211
- context_nameTracker: "wapT",
212
- context_platform: "PC",
213
- context_accountGeographicZone: t.region,
214
- context_appVersion: "discovery-0.0.0",
215
- context_osVersion: "unknown",
216
- context_deviceModel: "",
217
- context_deviceId: "",
218
- context_package: "",
219
- context_networkType: "unknow",
220
- context_networkQuality: "UNKNOWN",
221
- context_matchedPath: n ? window.location.pathname : "",
222
- context_route: n ? window.location.href : "",
223
- context_userAgent: typeof navigator < "u" ? navigator.userAgent : "",
224
- context_artifactName: P,
225
- context_artifactVersion: T,
226
- context_deviceLevel: "unknown",
227
- context_feSdkVersion: "1.3.13",
228
- context_userId: t.userId,
229
- measurement_name: B,
230
- measurement_data: {
231
- event_name: e.name,
232
- timestamp: e.timestamp,
233
- ...e.payload
234
- }
235
- };
236
- }
237
- function H(e, t) {
238
- if (!(typeof fetch != "function" || t.length === 0))
239
- try {
240
- const n = {
241
- method: "POST",
242
- headers: {
243
- "content-type": "application/json",
244
- "Biz-Type": "apm_fe",
245
- Batch: "true"
246
- },
247
- body: JSON.stringify(t)
248
- };
249
- typeof navigator < "u" && "sendBeacon" in navigator && (n.keepalive = !0), Promise.resolve(fetch(e, n)).catch(() => {
250
- });
251
- } catch {
252
- }
253
- }
254
- function q(e) {
255
- if (typeof console > "u") return;
256
- const t = e.measurement_name ?? "apm";
257
- console.groupCollapsed(`[apm] ${t}`), console.log(e), console.groupEnd();
258
- }
259
- function V(e) {
260
- const t = e.debug === !0, n = _(), r = _();
261
- let s = 0, o = [], i = null, a = 0, c = !1;
262
- const u = () => {
263
- if (c = !1, i !== null && (clearTimeout(i), i = null), o.length === 0) return;
264
- const h = o;
265
- o = [], H(e.endpoint, h);
266
- }, l = () => {
267
- if (i !== null && (clearTimeout(i), i = null), a > 0) {
268
- c = !0;
269
- return;
270
- }
271
- u();
272
- }, g = () => {
273
- i === null && (i = setTimeout(l, j));
274
- };
275
- return {
276
- sendCustomPoint(h) {
277
- const y = s++;
278
- a += 1, Promise.resolve().then(() => e.resolveUserId()).catch(() => "").then((k) => {
279
- try {
280
- const m = C(h, {
281
- sessionId: n,
282
- pageSessionId: r,
283
- seqId: y,
284
- userId: k,
285
- region: e.region
286
- });
287
- if (t) {
288
- q(m);
289
- return;
290
- }
291
- o.push(m), o.length >= F ? setTimeout(() => l()) : g();
292
- } catch {
293
- }
294
- }).finally(() => {
295
- a -= 1, a === 0 && c && u();
296
- });
297
- },
298
- flush: l
299
- };
300
- }
301
- function Q(e) {
302
- const t = e.region ?? "cn";
303
- if (e.endpoint) return e.endpoint;
304
- const n = $();
305
- return A[t][n];
306
- }
307
- function $() {
308
- if (typeof window > "u") return "production";
309
- const e = window.location.hostname.toLowerCase();
310
- return e === "localhost" || e === "127.0.0.1" || e === "0.0.0.0" || e.includes(".sit.") || // hostname.includes(".beta.") ||
311
- e.startsWith("local.") ? "development" : "production";
312
- }
313
- function G(e, t) {
314
- if (e === !1) return null;
315
- try {
316
- const n = e.region ?? "cn", r = V({
317
- endpoint: Q(e),
318
- region: n,
319
- resolveUserId: t ?? (() => Promise.resolve("")),
320
- debug: e.debug
321
- });
322
- return { send: (s) => r.sendCustomPoint(s), flush: () => r.flush() };
323
- } catch {
324
- return null;
325
- }
326
- }
327
- class X {
328
- constructor(t, n, r, s) {
329
- this.resolveContext = s, this.unsubscribers = [], this.common = {
330
- business_line: "",
331
- sdk_version: "",
332
- content_mode: "",
333
- renderer: ""
334
- };
335
- const o = G(t, r);
336
- if (!o) {
337
- this.send = () => {
338
- }, this.flush = () => {
339
- };
340
- return;
341
- }
342
- this.send = o.send, this.flush = o.flush, this.common = {
343
- ...this.common,
344
- ...t === !1 ? {} : t.context,
345
- business_line: t === !1 ? "" : t.business_line ?? "unknown"
346
- }, this.bindEvents(n), this.bindFlushOnHidden();
347
- }
348
- /** 页面隐藏时立即冲刷缓冲,避免关闭前丢数据 */
349
- bindFlushOnHidden() {
350
- if (typeof document > "u") return;
351
- const t = () => {
352
- document.visibilityState === "hidden" && this.flush();
353
- };
354
- document.addEventListener("visibilitychange", t), this.unsubscribers.push(() => document.removeEventListener("visibilitychange", t));
355
- }
356
- bindEvents(t) {
357
- this.unsubscribers.push(
358
- t.on("beforeLoad", (n) => {
359
- n.url && this.track("simulator_before_load", {
360
- url: n.url,
361
- source: n.source
362
- });
363
- })
364
- ), this.unsubscribers.push(
365
- t.on("ready", () => {
366
- this.track("simulator_ready", {});
367
- })
368
- ), this.unsubscribers.push(
369
- t.on("load", (n) => {
370
- n.url && this.track("simulator_load", { url: n.url, duration: n.duration });
371
- })
372
- ), this.unsubscribers.push(
373
- t.on("error", (n) => {
374
- this.track("simulator_load_error", {
375
- url: n.url,
376
- error_name: n.code,
377
- message: n.message
378
- });
379
- })
380
- ), this.unsubscribers.push(
381
- t.on("renderDone", (n) => {
382
- n.url && this.track("simulator_render_done", { url: n.url, duration: n.duration });
383
- })
384
- ), this.unsubscribers.push(
385
- t.on("navigation", (n) => {
386
- n.blocked && this.track("simulator_navigation_blocked", { source: n.from, url: n.to });
387
- })
388
- ), this.unsubscribers.push(
389
- t.on("guestError", (n) => {
390
- this.track("simulator_guest_error", {
391
- type: n.type,
392
- error_name: n.name,
393
- message: n.message?.slice(0, 1024),
394
- stack: n.stack?.slice(0, 1024),
395
- source: n.source,
396
- url: n.url,
397
- disposition: n.disposition,
398
- effective_directive: n.effective_directive,
399
- violated_directive: n.violated_directive,
400
- blocked_uri: n.blocked_uri,
401
- source_file: n.source_file,
402
- line_number: n.line_number,
403
- column_number: n.column_number,
404
- original_policy: n.original_policy,
405
- error_code: n.code,
406
- request_id: n.request_id,
407
- model: n.model,
408
- operation: n.operation,
409
- error_details: n.details
410
- });
411
- })
412
- ), this.unsubscribers.push(
413
- t.on("destroy", (n) => {
414
- this.track("simulator_destroy", { message: n.reason ?? "" });
415
- })
416
- );
417
- }
418
- /** 初始化公共字段并上报 simulator_init */
419
- trackInit(t) {
420
- this.common = {
421
- ...this.common,
422
- sdk_version: t.sdkVersion,
423
- content_mode: t.contentMode,
424
- renderer: t.renderer
425
- }, this.track("simulator_init", { url: t.appUrl });
426
- }
427
- /** 上报 reload 事件 */
428
- trackReload(t) {
429
- this.track("simulator_reload", { url: t });
430
- }
431
- /** srcdoc 注入完成 */
432
- trackInjectDone(t) {
433
- this.track("simulator_inject_done", {
434
- url: t.url,
435
- result: t.injected ? "injected" : "fallback",
436
- duration: t.duration
437
- });
438
- }
439
- /** 权限弹窗操作结果 */
440
- trackPermissionResult(t) {
441
- this.track("simulator_permission_result", t);
442
- }
443
- /** zip/files 解析完成 */
444
- trackParseDone(t) {
445
- this.track("simulator_parse_done", t);
446
- }
447
- /** SW 注册 + 激活结果(renderer: iframe-service-worker);scope 复用 url 字段 */
448
- trackSwRegister(t) {
449
- this.track("simulator_sw_register", t);
450
- }
451
- /** SW MOUNT(文件表挂载 + ACK)结果 */
452
- trackSwMount(t) {
453
- this.track("simulator_sw_mount", t);
454
- }
455
- /** 远程加载内部阶段;stage 保持低基数枚举,扩展字段只承载耗时与结果。 */
456
- trackLoadStage(t, n) {
457
- this.track("simulator_load_stage", { stage: t, ...n });
458
- }
459
- /** bridge 调用完成回调 */
460
- handleBridgeInvoke(t) {
461
- if (t.event === "reportTracker") return;
462
- const n = {
463
- name: t.event,
464
- result: t.success ? "success" : "fail"
465
- };
466
- t.success ? n.duration = t.duration : n.message = t.error, this.track("bridge_invoke", n);
467
- }
468
- /** 发送打点:自动合入公共字段 */
469
- track(t, n) {
470
- try {
471
- let r = {};
472
- try {
473
- r = this.resolveContext?.() ?? {};
474
- } catch {
475
- }
476
- this.send({
477
- name: t,
478
- payload: { ...this.common, ...n, ...r },
479
- timestamp: Date.now()
480
- });
481
- } catch {
482
- }
483
- }
484
- destroy() {
485
- queueMicrotask(() => this.flush());
486
- for (const t of this.unsubscribers)
487
- t();
488
- this.unsubscribers.length = 0;
489
- }
490
- }
491
- function M(e) {
492
- return !(e === void 0 || e === !1);
493
- }
494
- export {
495
- d as M,
496
- f as a,
497
- z as b,
498
- V as c,
499
- M as d,
500
- W as e,
501
- X as f,
502
- I as i,
503
- Q as r,
504
- T as v
505
- };