@mpcore/simulator 0.4.1 → 0.4.3
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/README.md +1 -0
- package/dist/index.d.mts +280 -272
- package/dist/index.mjs +112 -54
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -4,7 +4,6 @@ import path from "node:path";
|
|
|
4
4
|
import { parseExpression } from "@babel/parser";
|
|
5
5
|
import { WEVU_NATIVE_INSTANCE_KEY } from "@weapp-core/constants";
|
|
6
6
|
import { parseDocument } from "htmlparser2";
|
|
7
|
-
import process from "node:process";
|
|
8
7
|
import vm from "node:vm";
|
|
9
8
|
//#region src/host/globals.ts
|
|
10
9
|
function createDuplicateRegistrationError(kind, id) {
|
|
@@ -583,9 +582,7 @@ function createHeadlessWx(driver) {
|
|
|
583
582
|
showToast: (option) => invokeWxApi(() => driver.showToast(option), option),
|
|
584
583
|
startPullDownRefresh: (option) => invokeWxApi(() => driver.startPullDownRefresh(), option),
|
|
585
584
|
stopPullDownRefresh: () => driver.stopPullDownRefresh(),
|
|
586
|
-
switchTab: (option) =>
|
|
587
|
-
driver.switchTab(option);
|
|
588
|
-
}, option),
|
|
585
|
+
switchTab: (option) => driver.switchTab(option),
|
|
589
586
|
uploadFile: (option) => driver.uploadFile(option),
|
|
590
587
|
removeTabBarBadge: (option) => invokeWxApi(() => driver.removeTabBarBadge(option), option),
|
|
591
588
|
setTabBarBadge: (option) => invokeWxApi(() => driver.setTabBarBadge(option), option),
|
|
@@ -594,6 +591,48 @@ function createHeadlessWx(driver) {
|
|
|
594
591
|
};
|
|
595
592
|
}
|
|
596
593
|
//#endregion
|
|
594
|
+
//#region src/runtime/runtimeGlobals.ts
|
|
595
|
+
const MINI_PROGRAM_UNAVAILABLE_GLOBAL_NAMES = [
|
|
596
|
+
"window",
|
|
597
|
+
"document",
|
|
598
|
+
"navigator",
|
|
599
|
+
"self",
|
|
600
|
+
"global",
|
|
601
|
+
"location",
|
|
602
|
+
"process",
|
|
603
|
+
"Buffer",
|
|
604
|
+
"fetch",
|
|
605
|
+
"Headers",
|
|
606
|
+
"Request",
|
|
607
|
+
"Response",
|
|
608
|
+
"AbortController",
|
|
609
|
+
"AbortSignal",
|
|
610
|
+
"XMLHttpRequest",
|
|
611
|
+
"WebSocket",
|
|
612
|
+
"URLSearchParams",
|
|
613
|
+
"Blob",
|
|
614
|
+
"File",
|
|
615
|
+
"FormData",
|
|
616
|
+
"btoa",
|
|
617
|
+
"crypto",
|
|
618
|
+
"Event",
|
|
619
|
+
"CustomEvent",
|
|
620
|
+
"localStorage",
|
|
621
|
+
"sessionStorage",
|
|
622
|
+
"setImmediate",
|
|
623
|
+
"structuredClone",
|
|
624
|
+
"queueMicrotask"
|
|
625
|
+
];
|
|
626
|
+
/**
|
|
627
|
+
* 创建不会泄漏 Node.js 或浏览器宿主能力的小程序脚本全局对象。
|
|
628
|
+
* 显式 globals 最后合并,供兼容层和平台差异测试覆盖默认值。
|
|
629
|
+
*/
|
|
630
|
+
function createMiniProgramRuntimeGlobals(availableGlobals, overrides) {
|
|
631
|
+
const globals = {};
|
|
632
|
+
for (const name of MINI_PROGRAM_UNAVAILABLE_GLOBAL_NAMES) globals[name] = void 0;
|
|
633
|
+
return Object.assign(globals, availableGlobals, overrides);
|
|
634
|
+
}
|
|
635
|
+
//#endregion
|
|
597
636
|
//#region src/browser/virtualFiles.ts
|
|
598
637
|
function normalizeBrowserFilePath(filePath) {
|
|
599
638
|
return normalize(filePath).replace(/^\/+/, "").replace(/^\.\//, "");
|
|
@@ -633,7 +672,7 @@ function resolveRequiredModulePath$1(files, importer, request) {
|
|
|
633
672
|
}
|
|
634
673
|
function createExecutionContext$1(registries, getCurrentPages, getApp, wxDriver, kernel, globals) {
|
|
635
674
|
const wx = createHeadlessWx(wxDriver);
|
|
636
|
-
return {
|
|
675
|
+
return createMiniProgramRuntimeGlobals({
|
|
637
676
|
App(definition) {
|
|
638
677
|
return registerAppDefinition(registries, definition);
|
|
639
678
|
},
|
|
@@ -649,7 +688,6 @@ function createExecutionContext$1(registries, getCurrentPages, getApp, wxDriver,
|
|
|
649
688
|
Page(definition) {
|
|
650
689
|
return registerPageDefinition(registries, definition);
|
|
651
690
|
},
|
|
652
|
-
URLSearchParams,
|
|
653
691
|
clearInterval: (handle) => kernel.scheduler.clearInterval(handle),
|
|
654
692
|
clearTimeout: (handle) => kernel.scheduler.clearTimeout(handle),
|
|
655
693
|
console: kernel.diagnostics.createConsole(),
|
|
@@ -659,9 +697,8 @@ function createExecutionContext$1(registries, getCurrentPages, getApp, wxDriver,
|
|
|
659
697
|
require: void 0,
|
|
660
698
|
setInterval: kernel.scheduler.setInterval.bind(kernel.scheduler),
|
|
661
699
|
setTimeout: kernel.scheduler.setTimeout.bind(kernel.scheduler),
|
|
662
|
-
wx
|
|
663
|
-
|
|
664
|
-
};
|
|
700
|
+
wx
|
|
701
|
+
}, globals);
|
|
665
702
|
}
|
|
666
703
|
function createBrowserModuleLoader(files, registries, getCurrentPages, getApp, wxDriver, options) {
|
|
667
704
|
const moduleCache = /* @__PURE__ */ new Map();
|
|
@@ -2199,6 +2236,22 @@ function renderBrowserPageTree(context, page) {
|
|
|
2199
2236
|
};
|
|
2200
2237
|
}
|
|
2201
2238
|
//#endregion
|
|
2239
|
+
//#region src/host/wx/navigation.ts
|
|
2240
|
+
function invokePreparedNavigationApi(prepare, option) {
|
|
2241
|
+
try {
|
|
2242
|
+
const finalize = prepare()();
|
|
2243
|
+
try {
|
|
2244
|
+
option?.success?.();
|
|
2245
|
+
} finally {
|
|
2246
|
+
if (typeof finalize === "function") finalize();
|
|
2247
|
+
}
|
|
2248
|
+
option?.complete?.();
|
|
2249
|
+
} catch (error) {
|
|
2250
|
+
option?.fail?.(error);
|
|
2251
|
+
option?.complete?.();
|
|
2252
|
+
}
|
|
2253
|
+
}
|
|
2254
|
+
//#endregion
|
|
2202
2255
|
//#region src/project/pageConfig.ts
|
|
2203
2256
|
const DEFAULT_NAVIGATION_BAR_BACKGROUND_COLOR = "#ffffff";
|
|
2204
2257
|
const DEFAULT_NAVIGATION_BAR_FRONT_COLOR = "#000000";
|
|
@@ -5406,7 +5459,7 @@ var BrowserHeadlessSession = class {
|
|
|
5406
5459
|
showToast: (option) => this.wxState.showToast(option),
|
|
5407
5460
|
startPullDownRefresh: () => this.startPullDownRefresh(),
|
|
5408
5461
|
stopPullDownRefresh: () => this.stopPullDownRefresh(),
|
|
5409
|
-
switchTab: (option) => this.
|
|
5462
|
+
switchTab: (option) => invokePreparedNavigationApi(() => this.prepareSwitchTab(option.url), option),
|
|
5410
5463
|
uploadFile: (option) => this.wxState.uploadFile(option),
|
|
5411
5464
|
removeTabBarBadge: (option) => this.removeTabBarBadge(option.index),
|
|
5412
5465
|
setTabBarBadge: (option) => this.setTabBarBadge(option.index, option.text),
|
|
@@ -5951,22 +6004,26 @@ var BrowserHeadlessSession = class {
|
|
|
5951
6004
|
return nextPage;
|
|
5952
6005
|
}
|
|
5953
6006
|
switchTab(url) {
|
|
6007
|
+
this.prepareSwitchTab(url)()();
|
|
6008
|
+
return this.currentPageInstance;
|
|
6009
|
+
}
|
|
6010
|
+
prepareSwitchTab(url) {
|
|
5954
6011
|
const target = this.resolveNavigationTarget(url);
|
|
5955
|
-
this.bootstrap(createAppLaunchOptions$1(target.normalizedRoute, target.query));
|
|
5956
6012
|
if (Object.keys(target.query).length > 0) throw new Error(`wx.switchTab() url cannot contain query in browser simulator runtime: ${url}`);
|
|
5957
6013
|
if (!this.isTabBarRoute(target.routeRecord.route)) throw new Error(`wx.switchTab() can only open a tabBar page in browser simulator runtime: ${url}`);
|
|
6014
|
+
this.bootstrap(createAppLaunchOptions$1(target.normalizedRoute, target.query));
|
|
6015
|
+
return () => this.commitSwitchTab(target);
|
|
6016
|
+
}
|
|
6017
|
+
commitSwitchTab(target) {
|
|
5958
6018
|
const current = this.currentPageInstance;
|
|
5959
6019
|
const cachedTarget = this.tabPages.get(target.routeRecord.route) ?? null;
|
|
5960
|
-
if (current === cachedTarget && current) return
|
|
6020
|
+
if (current === cachedTarget && current) return () => {};
|
|
5961
6021
|
if (current && current !== cachedTarget) {
|
|
5962
6022
|
current.onHide?.();
|
|
5963
6023
|
this.runPageComponentLifetime(current.route, "hide");
|
|
5964
6024
|
}
|
|
5965
|
-
|
|
5966
|
-
|
|
5967
|
-
this.removePageInstance(page);
|
|
5968
|
-
this.unloadPage(page);
|
|
5969
|
-
}
|
|
6025
|
+
const pagesToUnload = [...this.pages].reverse().filter((page) => !this.isTabBarRoute(stripLeadingSlash$1(page.route)));
|
|
6026
|
+
for (const page of pagesToUnload) this.removePageInstance(page);
|
|
5970
6027
|
let nextPage = cachedTarget;
|
|
5971
6028
|
let shouldRunInitialLifecycles = false;
|
|
5972
6029
|
if (!nextPage) {
|
|
@@ -5982,7 +6039,9 @@ var BrowserHeadlessSession = class {
|
|
|
5982
6039
|
nextPage.onShow?.();
|
|
5983
6040
|
this.runPageComponentLifetime(nextPage.route, "show");
|
|
5984
6041
|
}
|
|
5985
|
-
return
|
|
6042
|
+
return () => {
|
|
6043
|
+
for (const page of pagesToUnload) this.unloadPage(page);
|
|
6044
|
+
};
|
|
5986
6045
|
}
|
|
5987
6046
|
pageScrollTo(option) {
|
|
5988
6047
|
const current = this.requireCurrentPage("wx.pageScrollTo()");
|
|
@@ -7058,7 +7117,7 @@ function resolveRequiredModulePath(artifactSource, importer, request) {
|
|
|
7058
7117
|
}
|
|
7059
7118
|
function createExecutionContext(registries, getCurrentPages, getApp, wxDriver, kernel, globals, onConsole) {
|
|
7060
7119
|
const wx = createHeadlessWx(wxDriver);
|
|
7061
|
-
return {
|
|
7120
|
+
return createMiniProgramRuntimeGlobals({
|
|
7062
7121
|
App(definition) {
|
|
7063
7122
|
return registerAppDefinition(registries, definition);
|
|
7064
7123
|
},
|
|
@@ -7080,17 +7139,14 @@ function createExecutionContext(registries, getCurrentPages, getApp, wxDriver, k
|
|
|
7080
7139
|
getApp,
|
|
7081
7140
|
getCurrentPages,
|
|
7082
7141
|
globalThis: void 0,
|
|
7083
|
-
process,
|
|
7084
7142
|
require: void 0,
|
|
7085
7143
|
setInterval: kernel.scheduler.setInterval.bind(kernel.scheduler),
|
|
7086
7144
|
setTimeout: kernel.scheduler.setTimeout.bind(kernel.scheduler),
|
|
7087
7145
|
TextDecoder: globalThis.TextDecoder,
|
|
7088
7146
|
TextEncoder: globalThis.TextEncoder,
|
|
7089
7147
|
URL: globalThis.URL,
|
|
7090
|
-
|
|
7091
|
-
|
|
7092
|
-
...globals
|
|
7093
|
-
};
|
|
7148
|
+
wx
|
|
7149
|
+
}, globals);
|
|
7094
7150
|
}
|
|
7095
7151
|
function createModuleLoader(registries, getCurrentPages, getApp, wxDriver, options) {
|
|
7096
7152
|
const moduleCache = /* @__PURE__ */ new Map();
|
|
@@ -7389,7 +7445,7 @@ var HeadlessSession = class {
|
|
|
7389
7445
|
showToast: (option) => this.wxState.showToast(option),
|
|
7390
7446
|
startPullDownRefresh: () => this.startPullDownRefresh(),
|
|
7391
7447
|
stopPullDownRefresh: () => this.stopPullDownRefresh(),
|
|
7392
|
-
switchTab: (option) => this.
|
|
7448
|
+
switchTab: (option) => invokePreparedNavigationApi(() => this.prepareSwitchTab(option.url), option),
|
|
7393
7449
|
uploadFile: (option) => this.wxState.uploadFile(option),
|
|
7394
7450
|
removeTabBarBadge: (option) => this.removeTabBarBadge(option.index),
|
|
7395
7451
|
setTabBarBadge: (option) => this.setTabBarBadge(option.index, option.text),
|
|
@@ -7908,22 +7964,26 @@ var HeadlessSession = class {
|
|
|
7908
7964
|
return nextPage;
|
|
7909
7965
|
}
|
|
7910
7966
|
switchTab(url) {
|
|
7967
|
+
this.prepareSwitchTab(url)()();
|
|
7968
|
+
return this.currentPageInstance;
|
|
7969
|
+
}
|
|
7970
|
+
prepareSwitchTab(url) {
|
|
7911
7971
|
const target = this.resolveNavigationTarget(url);
|
|
7912
|
-
this.bootstrap(createAppLaunchOptions(target.normalizedRoute, target.query));
|
|
7913
7972
|
if (Object.keys(target.query).length > 0) throw new Error(`wx.switchTab() url cannot contain query in headless runtime: ${url}`);
|
|
7914
7973
|
if (!this.isTabBarRoute(target.routeRecord.route)) throw new Error(`wx.switchTab() can only open a tabBar page in headless runtime: ${url}`);
|
|
7974
|
+
this.bootstrap(createAppLaunchOptions(target.normalizedRoute, target.query));
|
|
7975
|
+
return () => this.commitSwitchTab(target);
|
|
7976
|
+
}
|
|
7977
|
+
commitSwitchTab(target) {
|
|
7915
7978
|
const current = this.currentPageInstance;
|
|
7916
7979
|
const cachedTarget = this.tabPages.get(target.routeRecord.route) ?? null;
|
|
7917
|
-
if (current === cachedTarget && current) return
|
|
7980
|
+
if (current === cachedTarget && current) return () => {};
|
|
7918
7981
|
if (current && current !== cachedTarget) {
|
|
7919
7982
|
current.onHide?.();
|
|
7920
7983
|
this.runPageComponentLifetime(current.route, "hide");
|
|
7921
7984
|
}
|
|
7922
|
-
|
|
7923
|
-
|
|
7924
|
-
this.removePageInstance(page);
|
|
7925
|
-
this.unloadPage(page);
|
|
7926
|
-
}
|
|
7985
|
+
const pagesToUnload = [...this.pages].reverse().filter((page) => !this.isTabBarRoute(stripLeadingSlash(page.route)));
|
|
7986
|
+
for (const page of pagesToUnload) this.removePageInstance(page);
|
|
7927
7987
|
let nextPage = cachedTarget;
|
|
7928
7988
|
let shouldRunInitialLifecycles = false;
|
|
7929
7989
|
if (!nextPage) {
|
|
@@ -7939,7 +7999,9 @@ var HeadlessSession = class {
|
|
|
7939
7999
|
nextPage.onShow?.();
|
|
7940
8000
|
this.runPageComponentLifetime(nextPage.route, "show");
|
|
7941
8001
|
}
|
|
7942
|
-
return
|
|
8002
|
+
return () => {
|
|
8003
|
+
for (const page of pagesToUnload) this.unloadPage(page);
|
|
8004
|
+
};
|
|
7943
8005
|
}
|
|
7944
8006
|
pageScrollTo(option) {
|
|
7945
8007
|
const current = this.requireCurrentPage("wx.pageScrollTo()");
|
|
@@ -8685,6 +8747,9 @@ var HeadlessTestingSessionHandle = class {
|
|
|
8685
8747
|
async close() {
|
|
8686
8748
|
this.session.close();
|
|
8687
8749
|
}
|
|
8750
|
+
async toolInfo() {
|
|
8751
|
+
return { version: "mpcore-simulator" };
|
|
8752
|
+
}
|
|
8688
8753
|
on(eventName, handler) {
|
|
8689
8754
|
this.session.on(eventName, handler);
|
|
8690
8755
|
return this;
|
|
@@ -8800,30 +8865,23 @@ var HeadlessTestingSessionHandle = class {
|
|
|
8800
8865
|
return page;
|
|
8801
8866
|
}
|
|
8802
8867
|
async evaluateInRuntime(evaluator, args) {
|
|
8803
|
-
const runtimeGlobals = {
|
|
8868
|
+
const runtimeGlobals = createMiniProgramRuntimeGlobals({
|
|
8869
|
+
clearInterval: globalThis.clearInterval,
|
|
8870
|
+
clearTimeout: globalThis.clearTimeout,
|
|
8804
8871
|
getApp: () => this.session.getApp(),
|
|
8805
8872
|
getCurrentPages: () => this.session.getCurrentPages(),
|
|
8873
|
+
setInterval: globalThis.setInterval,
|
|
8874
|
+
setTimeout: globalThis.setTimeout,
|
|
8875
|
+
TextDecoder: globalThis.TextDecoder,
|
|
8876
|
+
TextEncoder: globalThis.TextEncoder,
|
|
8877
|
+
URL: globalThis.URL,
|
|
8806
8878
|
wx: this.session.getWx()
|
|
8807
|
-
};
|
|
8808
|
-
|
|
8809
|
-
const
|
|
8810
|
-
|
|
8811
|
-
|
|
8812
|
-
|
|
8813
|
-
configurable: true,
|
|
8814
|
-
enumerable: false,
|
|
8815
|
-
value,
|
|
8816
|
-
writable: true
|
|
8817
|
-
});
|
|
8818
|
-
}
|
|
8819
|
-
try {
|
|
8820
|
-
const fn = typeof evaluator === "string" ? new vm.Script(`(${evaluator})`).runInThisContext() : evaluator;
|
|
8821
|
-
if (typeof fn !== "function") throw new TypeError("Headless evaluator must be a function or function source string.");
|
|
8822
|
-
return await fn(...args);
|
|
8823
|
-
} finally {
|
|
8824
|
-
for (const [name, descriptor] of previous) if (descriptor) Object.defineProperty(host, name, descriptor);
|
|
8825
|
-
else delete host[name];
|
|
8826
|
-
}
|
|
8879
|
+
}, {});
|
|
8880
|
+
runtimeGlobals.globalThis = runtimeGlobals;
|
|
8881
|
+
const source = typeof evaluator === "string" ? evaluator : Function.prototype.toString.call(evaluator);
|
|
8882
|
+
const fn = new vm.Script(`(${source})`).runInNewContext(runtimeGlobals);
|
|
8883
|
+
if (typeof fn !== "function") throw new TypeError("Headless evaluator must be a function or function source string.");
|
|
8884
|
+
return await fn(...args);
|
|
8827
8885
|
}
|
|
8828
8886
|
};
|
|
8829
8887
|
//#endregion
|
package/package.json
CHANGED