@heybox/hb-sdk 0.8.0 → 0.8.1-alpha.10
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/CHANGELOG.md +84 -0
- package/README.md +80 -7
- package/dist/cli-chunks/{build-PYCNacya.cjs → build-CtqIXxDL.cjs} +16 -10
- package/dist/cli-chunks/{context-m2W2XbL0.cjs → context-t57bMfbr.cjs} +17 -3
- package/dist/cli-chunks/{create-BdAg3WGA.cjs → create-BDOPSacf.cjs} +1 -1
- package/dist/cli-chunks/{dev-CyZuw7Yn.cjs → dev-VxjconYc.cjs} +628 -147
- package/dist/cli-chunks/{doctor-DU8rCfUF.cjs → doctor-BkfYdlnZ.cjs} +1 -1
- package/dist/cli-chunks/{index-MMW2ibQm.cjs → index-DDrytLTX.cjs} +87 -15
- package/dist/cli-chunks/{index-DATObqzK.cjs → index-lLaHzwfT.cjs} +2 -2
- package/dist/cli-chunks/{index.esm-BiAaAUFC.cjs → index.esm-Dy4qRhuo.cjs} +9 -8
- package/dist/cli-chunks/{login-B3TThMss.cjs → login-D2EX4R5_.cjs} +2 -2
- package/dist/cli-chunks/{project-vite-BQj8YLI4.cjs → project-vite-CFhz2THK.cjs} +1 -1
- package/dist/cli-chunks/{remote-DNvI7tHH.cjs → remote-CfESZxO2.cjs} +41 -28
- package/dist/cli-chunks/{runtime-gate-BEFp1w_s.cjs → runtime-gate-BWb8QIcX.cjs} +0 -107
- package/dist/cli-chunks/{runtime-permission-env-CtL8rsjB.cjs → runtime-permission-env-BtTDFqEL.cjs} +304 -23
- package/dist/cli-chunks/{session-DjBkjaF8.cjs → session-Dkhwjqhm.cjs} +1 -1
- package/dist/cli-chunks/{skill-cR_wnaw2.cjs → skill-DOveC5jt.cjs} +83 -27
- package/dist/cli-chunks/{version-yEn1E2Bg.cjs → version-C4nE66sX.cjs} +1 -1
- package/dist/cli.cjs +1 -1
- package/dist/devtools/browser-dev-host/assets/browser-dev-host-BM9Qo_Ta.js +101 -0
- package/dist/devtools/browser-dev-host/assets/desktop-app-launch-C2I333Yn.js +6 -0
- package/dist/devtools/browser-dev-host/assets/index-Bnb6MMTv.js +567 -0
- package/dist/devtools/browser-dev-host/assets/index-D-aNERAr.css +1 -0
- package/dist/devtools/browser-dev-host/index.html +3 -3
- package/dist/index.cjs.js +1970 -1567
- package/dist/index.esm.js +1969 -1568
- package/dist/miniapp-publish.cjs.js +61 -0
- package/dist/miniapp-publish.esm.js +59 -1
- package/dist/protocol.cjs.js +64 -13
- package/dist/protocol.esm.js +64 -13
- package/dist/templates/vanilla-vite-js/README.md.ejs +1 -1
- package/dist/templates/vanilla-vite-js/package.json.ejs +1 -0
- package/dist/templates/vanilla-vite-js/vite.config.js +1 -1
- package/dist/vite.cjs.js +1333 -18
- package/dist/vite.esm.js +1334 -20
- package/package.json +9 -11
- package/skill/SKILL.md +11 -3
- package/skill/references/api-protocol.md +8 -4
- package/skill/references/api-root.md +328 -210
- package/skill/references/cli.md +11 -3
- package/skill/references/examples.md +13 -1
- package/skill/references/recipes.md +162 -0
- package/skill/references/safety-boundaries.md +9 -3
- package/skill/skill.json +5 -5
- package/types/core/client.d.ts +9 -1
- package/types/core/sdk.d.ts +6 -0
- package/types/core/singleton.d.ts +6 -0
- package/types/devtools/device-logs.d.ts +48 -0
- package/types/index.d.ts +5 -1
- package/types/miniapp-manifest/companion-directory.d.ts +2 -0
- package/types/miniapp-manifest/companion-executable.d.ts +4 -0
- package/types/miniapp-manifest/companion-types.d.ts +38 -0
- package/types/miniapp-manifest/companions.d.ts +17 -0
- package/types/miniapp-manifest/index.d.ts +2 -0
- package/types/miniapp-manifest/node.d.ts +9 -0
- package/types/miniapp-manifest/permissions.d.ts +1 -1
- package/types/miniapp-manifest/schema.d.ts +5 -1
- package/types/miniapp-publish/index.d.ts +17 -2
- package/types/modules/companion/index.d.ts +57 -0
- package/types/modules/environment/index.d.ts +70 -0
- package/types/modules/network/index.d.ts +1 -4
- package/types/modules/network/observability.d.ts +0 -1
- package/types/protocol/dev-session.d.ts +8 -1
- package/types/protocol.d.ts +1 -1
- package/types/vite/index.d.ts +11 -4
- package/dist/devtools/browser-dev-host/assets/browser-dev-host-TzYf9L6C.js +0 -99
- package/dist/devtools/browser-dev-host/assets/index-C5MZZDa5.js +0 -567
- package/dist/devtools/browser-dev-host/assets/index-P-ra4m1y.css +0 -1
- package/dist/devtools/browser-dev-host/assets/workbench-state-BwV7bm4n.js +0 -5
|
@@ -1,24 +1,25 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
var node_crypto = require('node:crypto');
|
|
3
4
|
var fs = require('node:fs');
|
|
4
5
|
var os = require('node:os');
|
|
5
6
|
var path = require('node:path');
|
|
6
7
|
var net = require('node:net');
|
|
7
|
-
var node_crypto = require('node:crypto');
|
|
8
8
|
var promises = require('node:dns/promises');
|
|
9
9
|
var node_http = require('node:http');
|
|
10
10
|
var node_module = require('node:module');
|
|
11
|
-
var context = require('./context-
|
|
12
|
-
var runtimePermissionEnv = require('./runtime-permission-env-
|
|
11
|
+
var context = require('./context-t57bMfbr.cjs');
|
|
12
|
+
var runtimePermissionEnv = require('./runtime-permission-env-BtTDFqEL.cjs');
|
|
13
13
|
var undici = require('undici');
|
|
14
14
|
var fs$1 = require('node:fs/promises');
|
|
15
|
-
var index = require('./index-
|
|
15
|
+
var index = require('./index-DDrytLTX.cjs');
|
|
16
16
|
var require$$0$1 = require('crypto');
|
|
17
17
|
var require$$0 = require('buffer');
|
|
18
|
-
var session = require('./session-
|
|
18
|
+
var session = require('./session-Dkhwjqhm.cjs');
|
|
19
19
|
var browser = require('./browser-RAy8e8cV.cjs');
|
|
20
|
-
var projectVite = require('./project-vite-
|
|
21
|
-
var version = require('./version-
|
|
20
|
+
var projectVite = require('./project-vite-CFhz2THK.cjs');
|
|
21
|
+
var version = require('./version-C4nE66sX.cjs');
|
|
22
|
+
require('node:zlib');
|
|
22
23
|
|
|
23
24
|
var _documentCurrentScript = typeof document !== 'undefined' ? document.currentScript : null;
|
|
24
25
|
class Locked extends Error {
|
|
@@ -183,6 +184,82 @@ async function getPorts(options) {
|
|
|
183
184
|
throw new Error('No available ports found');
|
|
184
185
|
}
|
|
185
186
|
|
|
187
|
+
/**
|
|
188
|
+
* network.request 诊断日志的纯脱敏工具。
|
|
189
|
+
*
|
|
190
|
+
* @remarks
|
|
191
|
+
* 放在 `core` 而非 `modules/network`:无错误类型依赖,供 SDK 错误文案、
|
|
192
|
+
* modules observability、devtools Browser Dev Host / proxy 共用,并满足 boundary
|
|
193
|
+
*(devtools 不得直接 import runtime capability modules)。
|
|
194
|
+
*/
|
|
195
|
+
/** 敏感字段名:header / query / body key 共用。 */
|
|
196
|
+
const SENSITIVE_KEY_PATTERN$1 = /(authorization|cookie|password|passwd|secret|token|pkey|private[_-]?key|access[_-]?key|client[_-]?secret|api[_-]?key|apikey|session|jwt|refresh[_-]?token|csrf)/i;
|
|
197
|
+
/**
|
|
198
|
+
* 脱敏请求 URL:去掉 userinfo,并将 query / fragment 中的敏感参数值替换为 `[redacted]`。
|
|
199
|
+
*/
|
|
200
|
+
function sanitizeNetworkRequestUrl(url) {
|
|
201
|
+
if (!url) {
|
|
202
|
+
return url;
|
|
203
|
+
}
|
|
204
|
+
try {
|
|
205
|
+
const parsed = new URL(url);
|
|
206
|
+
parsed.username = '';
|
|
207
|
+
parsed.password = '';
|
|
208
|
+
redactUrlSearchParams(parsed.searchParams);
|
|
209
|
+
if (parsed.hash && parsed.hash.length > 1) {
|
|
210
|
+
const hashBody = parsed.hash.slice(1);
|
|
211
|
+
if (hashBody.includes('=')) {
|
|
212
|
+
const hashParams = new URLSearchParams(hashBody);
|
|
213
|
+
if (redactUrlSearchParams(hashParams)) {
|
|
214
|
+
parsed.hash = hashParams.toString();
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
return parsed.toString();
|
|
219
|
+
}
|
|
220
|
+
catch {
|
|
221
|
+
return redactSensitivePairsInRawText(url);
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
/**
|
|
225
|
+
* 诊断 message 中的 URL 片段做脱敏,避免 error.message 二次泄漏 query secrets。
|
|
226
|
+
*/
|
|
227
|
+
function sanitizeDiagnosticMessage(message) {
|
|
228
|
+
if (!message) {
|
|
229
|
+
return message;
|
|
230
|
+
}
|
|
231
|
+
return message.replace(/https?:\/\/[^\s]+/gi, matched => sanitizeNetworkRequestUrl(matched.replace(/[),.;]+$/g, '')));
|
|
232
|
+
}
|
|
233
|
+
function isSensitiveNetworkKey(key) {
|
|
234
|
+
return SENSITIVE_KEY_PATTERN$1.test(key.trim());
|
|
235
|
+
}
|
|
236
|
+
function redactUrlSearchParams(params) {
|
|
237
|
+
let changed = false;
|
|
238
|
+
for (const key of [...params.keys()]) {
|
|
239
|
+
if (isSensitiveNetworkKey(key)) {
|
|
240
|
+
params.set(key, '[redacted]');
|
|
241
|
+
changed = true;
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
return changed;
|
|
245
|
+
}
|
|
246
|
+
function redactSensitivePairsInRawText(text) {
|
|
247
|
+
return text.replace(/([?&#]|^|[?&])([^=&#\s]+)=([^&#\s]*)/g, (full, prefix, key, value) => {
|
|
248
|
+
if (!isSensitiveNetworkKey(key)) {
|
|
249
|
+
return full;
|
|
250
|
+
}
|
|
251
|
+
return `${prefix}${key}=[redacted]`;
|
|
252
|
+
});
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
const DEVICE_LOG_PATH = '/__hb_sdk__/device-logs';
|
|
256
|
+
const registryKey = Symbol.for('@heybox/hb-sdk/device-logs');
|
|
257
|
+
const registry = globalThis;
|
|
258
|
+
const stores = (registry[registryKey] ??= new Map());
|
|
259
|
+
function getDeviceLogs(appUrl) {
|
|
260
|
+
return stores.get(Number(new URL(appUrl).port));
|
|
261
|
+
}
|
|
262
|
+
|
|
186
263
|
/** Dev Shell 读取局域网 Dev Session endpoint 的 query 参数。 */
|
|
187
264
|
const MINI_PROGRAM_DEV_CONTEXT_QUERY_PARAM = 'dev_context_url';
|
|
188
265
|
/** Browser Dev Host 创建和读取 Dev Session 的固定路径。 */
|
|
@@ -546,78 +623,100 @@ function readErrorMessage(error) {
|
|
|
546
623
|
return error instanceof Error ? error.message : String(error);
|
|
547
624
|
}
|
|
548
625
|
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
626
|
+
const PATHNAME_MAX_DECODE_ROUNDS = 4;
|
|
627
|
+
const OFFICIAL_ROOT_DOMAINS = ['xiaoheihe.cn', 'max-c.com', 'debugmode.cn', 'maxjia.com'];
|
|
628
|
+
const DENIED_REQUEST_HEADERS = new Set([
|
|
629
|
+
'connection',
|
|
630
|
+
'content-length',
|
|
631
|
+
'host',
|
|
632
|
+
'keep-alive',
|
|
633
|
+
'proxy-connection',
|
|
634
|
+
'te',
|
|
635
|
+
'trailer',
|
|
636
|
+
'transfer-encoding',
|
|
637
|
+
'upgrade',
|
|
638
|
+
]);
|
|
639
|
+
/** 任意 origin 都不得通过公开 network.request 访问的 Runtime 私有路径。 */
|
|
640
|
+
const PUBLIC_NETWORK_PROTECTED_PATH_PREFIXES = ['/user_miniprogram/runtime/auth', '/user_miniprogram/openapi/v1'];
|
|
641
|
+
/** 官方 origin 上会签发、恢复或校验账号 credential 的私有路径。 */
|
|
642
|
+
const PUBLIC_NETWORK_OFFICIAL_ACCOUNT_SENSITIVE_PATH_PREFIXES = [
|
|
643
|
+
'/account/restore_login',
|
|
644
|
+
'/account/check_login',
|
|
645
|
+
'/account/login',
|
|
646
|
+
'/account/get_login_code',
|
|
647
|
+
'/account/login_code',
|
|
648
|
+
'/account/get_qrcode_url',
|
|
649
|
+
'/account/qr_state',
|
|
650
|
+
'/account/google_2fa_verify',
|
|
651
|
+
'/account/open/login',
|
|
652
|
+
'/account/captcha',
|
|
653
|
+
'/account/get_verify_code',
|
|
654
|
+
'/account/send_verify_code',
|
|
655
|
+
'/account/verify_code',
|
|
656
|
+
];
|
|
657
|
+
/** 判断公开网络能力是否命中 Runtime 或官方账号敏感路径。 */
|
|
658
|
+
function isProtectedMiniProgramNetworkPath(pathname, official) {
|
|
659
|
+
const normalized = normalizePublicNetworkPathname(pathname);
|
|
660
|
+
if (normalized === null)
|
|
661
|
+
return true;
|
|
662
|
+
if (matchesPathPrefix(normalized, PUBLIC_NETWORK_PROTECTED_PATH_PREFIXES))
|
|
663
|
+
return true;
|
|
664
|
+
return official && matchesPathPrefix(normalized, PUBLIC_NETWORK_OFFICIAL_ACCOUNT_SENSITIVE_PATH_PREFIXES);
|
|
665
|
+
}
|
|
666
|
+
/** 使用 Runtime 唯一的官方域名分类规则判断请求目标。 */
|
|
667
|
+
function isOfficialMiniProgramNetworkUrl(url) {
|
|
668
|
+
const hostname = url.hostname.toLowerCase().replace(/\.$/, '');
|
|
669
|
+
return OFFICIAL_ROOT_DOMAINS.some((rootDomain) => hostname === rootDomain || hostname.endsWith(`.${rootDomain}`));
|
|
670
|
+
}
|
|
671
|
+
/** 判断公开网络能力是否禁止调用方设置该请求头。 */
|
|
672
|
+
function isForbiddenMiniProgramNetworkRequestHeader(name) {
|
|
673
|
+
const normalized = name.trim().toLowerCase();
|
|
674
|
+
return DENIED_REQUEST_HEADERS.has(normalized) || /^(?:x-heybox|x-xhh)-(?:access-token|auth|authorization|pkey|session|token)$/.test(normalized);
|
|
675
|
+
}
|
|
676
|
+
function normalizePublicNetworkPathname(pathname) {
|
|
677
|
+
let decoded = pathname;
|
|
678
|
+
for (let round = 0; round < PATHNAME_MAX_DECODE_ROUNDS; round += 1) {
|
|
679
|
+
let next;
|
|
680
|
+
try {
|
|
681
|
+
next = decodeURIComponent(decoded);
|
|
682
|
+
}
|
|
683
|
+
catch {
|
|
684
|
+
return null;
|
|
685
|
+
}
|
|
686
|
+
if (next === decoded)
|
|
687
|
+
return normalizeDecodedPathname(decoded);
|
|
688
|
+
decoded = next;
|
|
565
689
|
}
|
|
566
690
|
try {
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
parsed.password = '';
|
|
570
|
-
redactUrlSearchParams(parsed.searchParams);
|
|
571
|
-
if (parsed.hash && parsed.hash.length > 1) {
|
|
572
|
-
const hashBody = parsed.hash.slice(1);
|
|
573
|
-
if (hashBody.includes('=')) {
|
|
574
|
-
const hashParams = new URLSearchParams(hashBody);
|
|
575
|
-
if (redactUrlSearchParams(hashParams)) {
|
|
576
|
-
parsed.hash = hashParams.toString();
|
|
577
|
-
}
|
|
578
|
-
}
|
|
579
|
-
}
|
|
580
|
-
return parsed.toString();
|
|
691
|
+
if (decodeURIComponent(decoded) !== decoded)
|
|
692
|
+
return null;
|
|
581
693
|
}
|
|
582
694
|
catch {
|
|
583
|
-
return
|
|
584
|
-
}
|
|
585
|
-
}
|
|
586
|
-
/**
|
|
587
|
-
* 诊断 message 中的 URL 片段做脱敏,避免 error.message 二次泄漏 query secrets。
|
|
588
|
-
*/
|
|
589
|
-
function sanitizeDiagnosticMessage(message) {
|
|
590
|
-
if (!message) {
|
|
591
|
-
return message;
|
|
695
|
+
return null;
|
|
592
696
|
}
|
|
593
|
-
return
|
|
697
|
+
return normalizeDecodedPathname(decoded);
|
|
594
698
|
}
|
|
595
|
-
function
|
|
596
|
-
return
|
|
699
|
+
function matchesPathPrefix(pathname, prefixes) {
|
|
700
|
+
return prefixes.some((prefix) => pathname === prefix || pathname.startsWith(`${prefix}/`));
|
|
597
701
|
}
|
|
598
|
-
function
|
|
599
|
-
|
|
600
|
-
for (const
|
|
601
|
-
if (
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
702
|
+
function normalizeDecodedPathname(pathname) {
|
|
703
|
+
const segments = [];
|
|
704
|
+
for (const segment of pathname.replace(/\\/g, '/').split('/')) {
|
|
705
|
+
if (!segment || segment === '.')
|
|
706
|
+
continue;
|
|
707
|
+
if (segment === '..')
|
|
708
|
+
segments.pop();
|
|
709
|
+
else
|
|
710
|
+
segments.push(segment);
|
|
605
711
|
}
|
|
606
|
-
return
|
|
607
|
-
}
|
|
608
|
-
function redactSensitivePairsInRawText(text) {
|
|
609
|
-
return text.replace(/([?&#]|^|[?&])([^=&#\s]+)=([^&#\s]*)/g, (full, prefix, key, value) => {
|
|
610
|
-
if (!isSensitiveNetworkKey(key)) {
|
|
611
|
-
return full;
|
|
612
|
-
}
|
|
613
|
-
return `${prefix}${key}=[redacted]`;
|
|
614
|
-
});
|
|
712
|
+
return `/${segments.join('/')}`;
|
|
615
713
|
}
|
|
616
714
|
|
|
617
715
|
const MAX_BROWSER_DEV_HOST_NETWORK_REQUEST_BODY = 2 * 1024 * 1024;
|
|
618
716
|
const MAX_BROWSER_DEV_HOST_NETWORK_RESPONSE_BODY = 5 * 1024 * 1024;
|
|
619
717
|
const DENIED_NETWORK_TARGETS = createDeniedNetworkTargets();
|
|
620
|
-
const
|
|
718
|
+
const CREDENTIAL_RESPONSE_HEADER_RE = /^(?:authentication-info|authorization|cookie|cookie2|proxy-authenticate|proxy-authentication-info|proxy-authorization|set-cookie|set-cookie2|www-authenticate|pkey|x-pkey|x-access-token|x-auth-token|x-api-key)$/i;
|
|
719
|
+
const HOST_CONTROLLED_REQUEST_HEADERS = new Set(['origin', 'referer', 'user-agent']);
|
|
621
720
|
async function handleBrowserDevHostNetworkProxy(request, response, fetchImpl, lookupHostname, options) {
|
|
622
721
|
const cors = options.cors === true;
|
|
623
722
|
if (request.method === 'OPTIONS') {
|
|
@@ -636,7 +735,7 @@ async function handleBrowserDevHostNetworkProxy(request, response, fetchImpl, lo
|
|
|
636
735
|
payload = await readJsonRequestBody(request);
|
|
637
736
|
const requestAbort = createBrowserDevHostRequestAbortScope(request, response);
|
|
638
737
|
try {
|
|
639
|
-
const result = await requestBrowserDevHostNetwork(payload, fetchImpl, lookupHostname, options.allowedHttpOrigins, requestAbort.signal);
|
|
738
|
+
const result = await requestBrowserDevHostNetwork(payload, fetchImpl, lookupHostname, options.allowedHttpOrigins, requestAbort.signal, options.permission);
|
|
640
739
|
if (typeof result.status === 'number' && (result.status < 200 || result.status >= 300)) {
|
|
641
740
|
logBrowserDevHostNetworkProxyDiagnostic('http_status', payload, result);
|
|
642
741
|
}
|
|
@@ -692,11 +791,12 @@ function writeNetworkJsonResponse(response, status, body, cors) {
|
|
|
692
791
|
writeJsonResponse(response, status, body);
|
|
693
792
|
}
|
|
694
793
|
const BROWSER_DEV_HOST_NETWORK_MULTIPART_UNSUPPORTED_MESSAGE = 'network.request 不支持 multipart/form-data。App Host 客户端代发仅支持 application/x-www-form-urlencoded(form)与 application/json;请将 data 编码为 form 字符串(例如 new URLSearchParams({...}).toString())并设置 Content-Type: application/x-www-form-urlencoded。文件上传请使用专用上传能力,勿手写 multipart boundary。';
|
|
695
|
-
async function requestBrowserDevHostNetwork(payload, fetchImpl, lookupHostname, allowedHttpOrigins, requestSignal) {
|
|
794
|
+
async function requestBrowserDevHostNetwork(payload, fetchImpl, lookupHostname, allowedHttpOrigins, requestSignal, permission) {
|
|
696
795
|
if (!isRecord$2(payload) || typeof payload.url !== 'string' || !payload.url.trim()) {
|
|
697
796
|
throw createBrowserDevHostError(400, 'INVALID_NETWORK_REQUEST', 'network.request url 必须是非空字符串');
|
|
698
797
|
}
|
|
699
798
|
const url = createBrowserDevHostNetworkUrl(payload.url, payload.params);
|
|
799
|
+
assertMiniProgramNetworkPolicy(url, permission);
|
|
700
800
|
const method = typeof payload.method === 'string' && payload.method.trim() ? payload.method.trim().toUpperCase() : 'GET';
|
|
701
801
|
const headers = normalizeBrowserDevHostNetworkHeaders(payload.headers);
|
|
702
802
|
assertSupportedBrowserDevHostNetworkRequestShape(headers, payload.data);
|
|
@@ -729,7 +829,7 @@ async function requestBrowserDevHostNetwork(payload, fetchImpl, lookupHostname,
|
|
|
729
829
|
data: contentType.includes('application/json') ? safeJsonParse(text) : text,
|
|
730
830
|
status: response.status,
|
|
731
831
|
statusText: response.statusText,
|
|
732
|
-
headers: Object.fromEntries([...response.headers.entries()].filter(([name]) => !
|
|
832
|
+
headers: Object.fromEntries([...response.headers.entries()].filter(([name]) => !isCredentialResponseHeader(name))),
|
|
733
833
|
};
|
|
734
834
|
}
|
|
735
835
|
catch (error) {
|
|
@@ -824,9 +924,9 @@ function isDeniedNetworkAddress(address, family) {
|
|
|
824
924
|
}
|
|
825
925
|
return true;
|
|
826
926
|
}
|
|
827
|
-
function
|
|
927
|
+
function isCredentialResponseHeader(name) {
|
|
828
928
|
const normalized = name.trim().toLowerCase();
|
|
829
|
-
return
|
|
929
|
+
return CREDENTIAL_RESPONSE_HEADER_RE.test(normalized) || normalized.startsWith('x-heybox-') || normalized.startsWith('x-xhh-');
|
|
830
930
|
}
|
|
831
931
|
function createPinnedNetworkDispatcher(pinnedTargets) {
|
|
832
932
|
const connect = undici.buildConnector({});
|
|
@@ -899,31 +999,29 @@ function normalizeBrowserDevHostNetworkHeaders(headers) {
|
|
|
899
999
|
return normalized;
|
|
900
1000
|
}
|
|
901
1001
|
Object.entries(headers).forEach(([key, value]) => {
|
|
902
|
-
if (
|
|
1002
|
+
if (isForbiddenMiniProgramNetworkRequestHeader(key)) {
|
|
903
1003
|
throw createBrowserDevHostError(400, 'INVALID_NETWORK_REQUEST', `network.request 不允许设置敏感请求头: ${key}`);
|
|
904
1004
|
}
|
|
905
|
-
if (typeof value === 'string' && !
|
|
1005
|
+
if (typeof value === 'string' && !isHostManagedRequestHeader(key)) {
|
|
906
1006
|
normalized.set(key, value);
|
|
907
1007
|
}
|
|
908
1008
|
});
|
|
909
1009
|
return normalized;
|
|
910
1010
|
}
|
|
911
|
-
function
|
|
912
|
-
|
|
1011
|
+
function isHostManagedRequestHeader(name) {
|
|
1012
|
+
const normalized = name.trim().toLowerCase();
|
|
1013
|
+
return HOST_CONTROLLED_REQUEST_HEADERS.has(normalized);
|
|
913
1014
|
}
|
|
914
|
-
function
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
'
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
'
|
|
923
|
-
|
|
924
|
-
'transfer-encoding',
|
|
925
|
-
'upgrade',
|
|
926
|
-
].includes(key.toLowerCase());
|
|
1015
|
+
function assertMiniProgramNetworkPolicy(url, permission) {
|
|
1016
|
+
if (!permission)
|
|
1017
|
+
return;
|
|
1018
|
+
const official = isOfficialMiniProgramNetworkUrl(url);
|
|
1019
|
+
if (official && !permission.useOfficialDomain) {
|
|
1020
|
+
throw createBrowserDevHostError(403, 'PERMISSION_DENIED', '当前小程序暂不支持访问官方域名');
|
|
1021
|
+
}
|
|
1022
|
+
if (isProtectedMiniProgramNetworkPath(url.pathname, official)) {
|
|
1023
|
+
throw createBrowserDevHostError(403, 'PERMISSION_DENIED', 'network.request 不允许访问 Runtime 或账号敏感接口');
|
|
1024
|
+
}
|
|
927
1025
|
}
|
|
928
1026
|
/**
|
|
929
1027
|
* 与 App Host 对齐:Mock 不再放行 multipart,避免“本地成功、上线失败”。
|
|
@@ -1062,7 +1160,8 @@ function createDevSessionRoutes(options) {
|
|
|
1062
1160
|
}
|
|
1063
1161
|
try {
|
|
1064
1162
|
const payload = await readJsonRequestBody(request);
|
|
1065
|
-
const
|
|
1163
|
+
const runtimePermissions = context.createMobileDevSessionRuntimePermissions(await options.readDevContext());
|
|
1164
|
+
const validated = validateDevSessionCreatePayload(payload, options.allowedMiniUrlOrigins, runtimePermissions);
|
|
1066
1165
|
reapExpiredDevSessions(devSessions, expiredDevSessions, options.now());
|
|
1067
1166
|
const replacedTokens = findDevSessionTokensForOrigin(devSessions, validated.miniUrlOrigin);
|
|
1068
1167
|
if (devSessions.size - replacedTokens.length >= MAX_ACTIVE_DEV_SESSIONS) {
|
|
@@ -1081,6 +1180,7 @@ function createDevSessionRoutes(options) {
|
|
|
1081
1180
|
expiresAt,
|
|
1082
1181
|
miniUrlOrigin: validated.miniUrlOrigin,
|
|
1083
1182
|
networkRequestEnabled: validated.networkRequestEnabled,
|
|
1183
|
+
useOfficialDomain: validated.useOfficialDomain,
|
|
1084
1184
|
serialized: JSON.stringify(snapshot),
|
|
1085
1185
|
});
|
|
1086
1186
|
try {
|
|
@@ -1129,7 +1229,7 @@ function createDevSessionRoutes(options) {
|
|
|
1129
1229
|
},
|
|
1130
1230
|
};
|
|
1131
1231
|
}
|
|
1132
|
-
function validateDevSessionCreatePayload(payload, allowedMiniUrlOrigins) {
|
|
1232
|
+
function validateDevSessionCreatePayload(payload, allowedMiniUrlOrigins, runtimePermissions) {
|
|
1133
1233
|
if (!isRecord$2(payload) || Object.keys(payload).some((key) => !['mini_url_origin', 'runtime_permissions'].includes(key))) {
|
|
1134
1234
|
throw new TypeError('Dev Session body is invalid');
|
|
1135
1235
|
}
|
|
@@ -1149,18 +1249,16 @@ function validateDevSessionCreatePayload(payload, allowedMiniUrlOrigins) {
|
|
|
1149
1249
|
!allowedMiniUrlOrigins.has(miniUrlOrigin.origin)) {
|
|
1150
1250
|
throw new TypeError('mini_url_origin must be an allowed LAN origin');
|
|
1151
1251
|
}
|
|
1152
|
-
const parsedPermissions = runtimePermissionEnv.parseMiniProgramRuntimePermissions(
|
|
1252
|
+
const parsedPermissions = runtimePermissionEnv.parseMiniProgramRuntimePermissions(runtimePermissions);
|
|
1153
1253
|
if (!parsedPermissions.valid) {
|
|
1154
1254
|
throw new TypeError('runtime_permissions is invalid');
|
|
1155
1255
|
}
|
|
1156
1256
|
const networkPermission = parsedPermissions.permissions.network;
|
|
1157
|
-
if (networkPermission?.config.useOfficialDomain !== false) {
|
|
1158
|
-
throw new TypeError('Dev Session does not allow official domains');
|
|
1159
|
-
}
|
|
1160
1257
|
return {
|
|
1161
1258
|
miniUrlOrigin: miniUrlOrigin.origin,
|
|
1162
|
-
networkRequestEnabled: networkPermission
|
|
1163
|
-
|
|
1259
|
+
networkRequestEnabled: networkPermission?.status === 'enabled',
|
|
1260
|
+
useOfficialDomain: networkPermission?.status === 'enabled' && networkPermission.config.useOfficialDomain === true,
|
|
1261
|
+
runtimePermissions: JSON.parse(JSON.stringify(runtimePermissions)),
|
|
1164
1262
|
};
|
|
1165
1263
|
}
|
|
1166
1264
|
async function handleTokenNetworkProxy(request, response, token, runtime) {
|
|
@@ -1190,6 +1288,7 @@ async function handleTokenNetworkProxy(request, response, token, runtime) {
|
|
|
1190
1288
|
await handleBrowserDevHostNetworkProxy(request, response, runtime.fetchImpl, runtime.lookupHostname, {
|
|
1191
1289
|
allowedHttpOrigins: new Set([resolved.session.miniUrlOrigin]),
|
|
1192
1290
|
cors: true,
|
|
1291
|
+
permission: { useOfficialDomain: resolved.session.useOfficialDomain },
|
|
1193
1292
|
});
|
|
1194
1293
|
}
|
|
1195
1294
|
function resolveDevSession(token, devSessions, expiredDevSessions, now) {
|
|
@@ -1851,8 +1950,8 @@ const DEFAULT_CHANNEL_TRANSPORT = {
|
|
|
1851
1950
|
};
|
|
1852
1951
|
const IPV4_LOOPBACK_ADDRESSES = new net.BlockList();
|
|
1853
1952
|
IPV4_LOOPBACK_ADDRESSES.addSubnet('127.0.0.0', 8, 'ipv4');
|
|
1854
|
-
const { WebSocket, WebSocketServer } = node_module.createRequire((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('cli-chunks/dev-
|
|
1855
|
-
const { parse: parseHtml } = node_module.createRequire((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('cli-chunks/dev-
|
|
1953
|
+
const { WebSocket, WebSocketServer } = node_module.createRequire((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('cli-chunks/dev-VxjconYc.cjs', document.baseURI).href)))('ws');
|
|
1954
|
+
const { parse: parseHtml } = node_module.createRequire((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('cli-chunks/dev-VxjconYc.cjs', document.baseURI).href)))('parse5');
|
|
1856
1955
|
const BROWSER_DEV_HOST_ROOT_CANDIDATES = [
|
|
1857
1956
|
path.resolve(__dirname, 'devtools/browser-dev-host'),
|
|
1858
1957
|
path.resolve(__dirname, '../devtools/browser-dev-host'),
|
|
@@ -1886,6 +1985,7 @@ async function startBrowserDevHostServer(options) {
|
|
|
1886
1985
|
mobileLaunchRedirect: initialMobileLaunchRedirect,
|
|
1887
1986
|
nativeAppLaunchUnavailableReason: options.nativeAppLaunchUnavailableReason,
|
|
1888
1987
|
runtimePermissions: options.runtimePermissions,
|
|
1988
|
+
devReadiness: options.devReadiness,
|
|
1889
1989
|
};
|
|
1890
1990
|
let bootstrapJson = serializeBrowserDevHostBootstrap(bootstrap);
|
|
1891
1991
|
const serverRuntime = createBrowserDevHostServer(root, {
|
|
@@ -1906,6 +2006,8 @@ async function startBrowserDevHostServer(options) {
|
|
|
1906
2006
|
loginAvailable: options.loginService !== undefined,
|
|
1907
2007
|
cliAuth: options.cliAuth,
|
|
1908
2008
|
readDevContext: () => bootstrap.devContext ?? { source: 'anonymous', runtimePermissions: bootstrap.runtimePermissions },
|
|
2009
|
+
readMobileDevSessionContext: options.readMobileDevSessionContext ??
|
|
2010
|
+
(() => Promise.resolve(bootstrap.devContext ?? { source: 'anonymous', runtimePermissions: bootstrap.runtimePermissions })),
|
|
1909
2011
|
readLanAddresses: () => bootstrap.lanAddresses,
|
|
1910
2012
|
mobileLaunchRedirectRegistry,
|
|
1911
2013
|
onMobileDevSessionCreated: options.onMobileDevSessionCreated,
|
|
@@ -1950,6 +2052,8 @@ async function startBrowserDevHostServer(options) {
|
|
|
1950
2052
|
return closePromise;
|
|
1951
2053
|
},
|
|
1952
2054
|
updateBootstrap: (next) => {
|
|
2055
|
+
if (next.devReadiness !== undefined)
|
|
2056
|
+
bootstrap.devReadiness = next.devReadiness;
|
|
1953
2057
|
if (next.devContext !== undefined)
|
|
1954
2058
|
bootstrap.devContext = next.devContext;
|
|
1955
2059
|
if (next.lanAddresses !== undefined) {
|
|
@@ -2073,6 +2177,7 @@ function createBrowserDevHostServer(root, runtime) {
|
|
|
2073
2177
|
fetchImpl: runtime.fetchImpl,
|
|
2074
2178
|
lookupHostname: runtime.lookupHostname,
|
|
2075
2179
|
now: runtime.now,
|
|
2180
|
+
readDevContext: runtime.readMobileDevSessionContext,
|
|
2076
2181
|
onCreated: ({ expiresAt, miniUrlOrigin, token }, request) => {
|
|
2077
2182
|
const origin = new URL(readRequestOrigin(request));
|
|
2078
2183
|
const hostname = origin.hostname.startsWith('[') && origin.hostname.endsWith(']') ? origin.hostname.slice(1, -1) : origin.hostname;
|
|
@@ -2172,6 +2277,44 @@ function createBrowserDevHostServer(root, runtime) {
|
|
|
2172
2277
|
writeSerializedJsonResponse(response, 200, runtime.readBootstrapJson());
|
|
2173
2278
|
return;
|
|
2174
2279
|
}
|
|
2280
|
+
if (pathname === '/__hb_sdk__/sandbox') {
|
|
2281
|
+
if (!isValidSensitiveRequest(request, loopbackPeer, runtime.sessionController.csrfToken)) {
|
|
2282
|
+
writeForbidden(response);
|
|
2283
|
+
return;
|
|
2284
|
+
}
|
|
2285
|
+
if (request.method !== 'POST') {
|
|
2286
|
+
writeMethodNotAllowed(response, 'POST');
|
|
2287
|
+
return;
|
|
2288
|
+
}
|
|
2289
|
+
try {
|
|
2290
|
+
const input = await readJsonRequestBody(request);
|
|
2291
|
+
if (!isRecord$2(input) || typeof input.device !== 'string' || typeof input.path !== 'string') {
|
|
2292
|
+
writeBadRequest(response);
|
|
2293
|
+
return;
|
|
2294
|
+
}
|
|
2295
|
+
const store = getDeviceLogs(runtime.appUrl);
|
|
2296
|
+
writeJsonResponse(response, 200, store ? await store.listSandbox(input.device, input.path) : { error: '设备服务不可用' });
|
|
2297
|
+
}
|
|
2298
|
+
catch {
|
|
2299
|
+
writeBadRequest(response);
|
|
2300
|
+
}
|
|
2301
|
+
return;
|
|
2302
|
+
}
|
|
2303
|
+
if (pathname === DEVICE_LOG_PATH) {
|
|
2304
|
+
const origin = request.headers.origin;
|
|
2305
|
+
if (!loopbackPeer || !hasNumericLoopbackHost(request) ||
|
|
2306
|
+
(origin && origin !== `http://${request.headers.host}`)) {
|
|
2307
|
+
writeForbidden(response);
|
|
2308
|
+
return;
|
|
2309
|
+
}
|
|
2310
|
+
if (request.method !== 'GET') {
|
|
2311
|
+
writeMethodNotAllowed(response, 'GET');
|
|
2312
|
+
return;
|
|
2313
|
+
}
|
|
2314
|
+
const store = getDeviceLogs(runtime.appUrl);
|
|
2315
|
+
writeJsonResponse(response, 200, store ? store.query(requestUrl.searchParams) : { epoch: '', items: [], devices: [], nextCursor: 0, hasMore: false, gap: false });
|
|
2316
|
+
return;
|
|
2317
|
+
}
|
|
2175
2318
|
if (pathname === '/session/status') {
|
|
2176
2319
|
if (!loopbackPeer || !hasNumericLoopbackHost(request)) {
|
|
2177
2320
|
writeForbidden(response);
|
|
@@ -10902,7 +11045,7 @@ function assertNodeRuntimeCapabilities(ctx, settings) {
|
|
|
10902
11045
|
function createNodeRealtimeDriverRegistry() {
|
|
10903
11046
|
const registry = createRuntimeRealtimeDriverRegistry();
|
|
10904
11047
|
registry.registerLazy('node-websocket', async () => {
|
|
10905
|
-
const mod = await Promise.resolve().then(function () { return require('./index.esm-
|
|
11048
|
+
const mod = await Promise.resolve().then(function () { return require('./index.esm-Dy4qRhuo.cjs'); });
|
|
10906
11049
|
return mod.createNodeWebSocketRuntimeDriver();
|
|
10907
11050
|
});
|
|
10908
11051
|
return registry;
|
|
@@ -13785,9 +13928,10 @@ function readHeyboxWebV4BrowserFingerprint() {
|
|
|
13785
13928
|
if (typeof document === 'undefined' || typeof navigator === 'undefined') {
|
|
13786
13929
|
return '';
|
|
13787
13930
|
}
|
|
13931
|
+
let webgl = null;
|
|
13788
13932
|
try {
|
|
13789
13933
|
const canvas = document.createElement('canvas');
|
|
13790
|
-
|
|
13934
|
+
webgl = canvas.getContext('webgl');
|
|
13791
13935
|
const debugInfo = webgl?.getExtension('WEBGL_debug_renderer_info');
|
|
13792
13936
|
let renderer = debugInfo ? String(webgl?.getParameter(debugInfo.UNMASKED_RENDERER_WEBGL) ?? '') : '';
|
|
13793
13937
|
if (!renderer) {
|
|
@@ -13806,6 +13950,15 @@ function readHeyboxWebV4BrowserFingerprint() {
|
|
|
13806
13950
|
catch {
|
|
13807
13951
|
return `${navigator.userAgent ?? ''}${navigator.platform ?? ''}${navigator.language ?? ''}`;
|
|
13808
13952
|
}
|
|
13953
|
+
finally {
|
|
13954
|
+
// 签名请求会反复读取种子,及时释放临时上下文,避免耗尽浏览器的 WebGL 配额。
|
|
13955
|
+
try {
|
|
13956
|
+
webgl?.getExtension('WEBGL_lose_context')?.loseContext();
|
|
13957
|
+
}
|
|
13958
|
+
catch {
|
|
13959
|
+
// 浏览器可能已丢失上下文;清理失败不改变本次签名种子或原降级语义。
|
|
13960
|
+
}
|
|
13961
|
+
}
|
|
13809
13962
|
}
|
|
13810
13963
|
/**
|
|
13811
13964
|
* 根据 pathname、时间和 nonce 计算 Web V4 hkey。
|
|
@@ -14862,7 +15015,7 @@ function containsSecret(value, secret) {
|
|
|
14862
15015
|
|
|
14863
15016
|
const LAUNCH_CORS_HEADERS = {
|
|
14864
15017
|
'access-control-allow-methods': 'GET',
|
|
14865
|
-
'access-control-allow-origin': '
|
|
15018
|
+
'access-control-allow-origin': 'https://www.xiaoheihe.cn',
|
|
14866
15019
|
'access-control-allow-private-network': 'true',
|
|
14867
15020
|
'cache-control': 'no-store',
|
|
14868
15021
|
};
|
|
@@ -14877,6 +15030,10 @@ function tryHandleMiniProgramDevLaunchRequest(request, response, readSource) {
|
|
|
14877
15030
|
if (requestUrl.pathname !== MINI_PROGRAM_DEV_LAUNCH_PATH || requestUrl.search !== '') {
|
|
14878
15031
|
return false;
|
|
14879
15032
|
}
|
|
15033
|
+
if (!isAllowedLaunchOrigin(request.headers.origin)) {
|
|
15034
|
+
writeLaunchResponse(response, 403);
|
|
15035
|
+
return true;
|
|
15036
|
+
}
|
|
14880
15037
|
if (request.method !== 'GET' && request.method !== 'OPTIONS') {
|
|
14881
15038
|
response.writeHead(405, { allow: 'GET', ...LAUNCH_CORS_HEADERS });
|
|
14882
15039
|
response.end();
|
|
@@ -14911,6 +15068,16 @@ function createLaunchSnapshot(source, requestOrigin) {
|
|
|
14911
15068
|
const localDevLaunchToken = source.localDevLaunchToken?.trim();
|
|
14912
15069
|
if (localDevLaunchToken)
|
|
14913
15070
|
snapshot.local_dev_launch_token = localDevLaunchToken;
|
|
15071
|
+
if (source.devSessionId)
|
|
15072
|
+
snapshot.dev_session_id = source.devSessionId;
|
|
15073
|
+
if (source.expiresAt)
|
|
15074
|
+
snapshot.expires_at = source.expiresAt;
|
|
15075
|
+
const companionManifestSha256 = source.companionManifestSha256?.trim();
|
|
15076
|
+
if (companionManifestSha256)
|
|
15077
|
+
snapshot.companion_manifest_sha256 = companionManifestSha256;
|
|
15078
|
+
const companionArtifactAccessToken = source.companionArtifactAccessToken?.trim();
|
|
15079
|
+
if (companionArtifactAccessToken)
|
|
15080
|
+
snapshot.companion_artifact_access_token = companionArtifactAccessToken;
|
|
14914
15081
|
const storedContextUrl = source.devContextUrlsByMiniUrlOrigin.get(requestOrigin.origin);
|
|
14915
15082
|
if (storedContextUrl) {
|
|
14916
15083
|
snapshot.dev_context_url = projectStoredDevContextUrl(storedContextUrl, requestOrigin.hostname);
|
|
@@ -14954,6 +15121,9 @@ function readNumericRequestOrigin(request) {
|
|
|
14954
15121
|
function isAllowedLaunchPeer(address) {
|
|
14955
15122
|
return isLoopbackOrPrivateLanAddress(address);
|
|
14956
15123
|
}
|
|
15124
|
+
function isAllowedLaunchOrigin(origin) {
|
|
15125
|
+
return origin === undefined || origin === 'https://www.xiaoheihe.cn';
|
|
15126
|
+
}
|
|
14957
15127
|
function writeLaunchResponse(response, status, body) {
|
|
14958
15128
|
response.writeHead(status, {
|
|
14959
15129
|
...LAUNCH_CORS_HEADERS,
|
|
@@ -14970,6 +15140,10 @@ const LOCAL_DEV_URL_HOST = '127.0.0.1';
|
|
|
14970
15140
|
// 与 Heybox H5 Vite 插件共享,让 hb-sdk 统一管理启动输出。
|
|
14971
15141
|
const MANAGED_DEV_OUTPUT_ENV = 'HEYBOX_DEV_SERVER_MANAGED_OUTPUT';
|
|
14972
15142
|
const VITE_LOG_LEVEL = 'warn';
|
|
15143
|
+
const COMPANION_DEV_MANIFEST_PATH = '/__hb_sdk__/companion/manifest.json';
|
|
15144
|
+
const COMPANION_DEV_MANIFEST_MAX_BYTES = 1024 * 1024;
|
|
15145
|
+
const COMPANION_ARTIFACT_ACCESS_TOKEN_ENV = 'HB_SDK_COMPANION_ARTIFACT_ACCESS_TOKEN';
|
|
15146
|
+
const COMPANION_ARTIFACT_ACCESS_TOKEN_HEADER = 'x-hb-sdk-companion-dev-token';
|
|
14973
15147
|
async function runDevCommand(options, runtime = {}) {
|
|
14974
15148
|
const logger = runtime.logger ?? index.createCliLogger();
|
|
14975
15149
|
const fetchImpl = runtime.fetchImpl ?? fetch;
|
|
@@ -14991,6 +15165,9 @@ async function runDevCommand(options, runtime = {}) {
|
|
|
14991
15165
|
port: appPort,
|
|
14992
15166
|
};
|
|
14993
15167
|
const projectRoot = projectVite.findProjectRoot(runtime.cwd ?? process.cwd());
|
|
15168
|
+
const companionArtifactAccessToken = (runtime.randomBytes ?? node_crypto.randomBytes)(32).toString('base64url');
|
|
15169
|
+
const devSessionId = node_crypto.randomBytes(16).toString('hex');
|
|
15170
|
+
runtimePermissionEnv.readMiniappPlatformsFromPackageJson(projectRoot);
|
|
14994
15171
|
const { context: devLaunchContext, miniProgram } = await loadDevLaunchContext({
|
|
14995
15172
|
env: runtime.env,
|
|
14996
15173
|
fetchImpl,
|
|
@@ -14998,22 +15175,59 @@ async function runDevCommand(options, runtime = {}) {
|
|
|
14998
15175
|
projectRoot,
|
|
14999
15176
|
timeoutMs: runtime.devContextTimeoutMs ?? DEFAULT_DEV_CONTEXT_TIMEOUT_MS,
|
|
15000
15177
|
});
|
|
15001
|
-
|
|
15002
|
-
|
|
15003
|
-
const appServer = await runDevStartupTask(logger, '正在创建 Vite 开发服务器', () => runtimePermissionEnv.withMiniappPlatformCspEnv(context.shouldSkipMiniappPlatformCsp(devLaunchContext.runtimePermissions), () => withManagedDevOutputEnv(() => vite.createServer(createViteServerOptions(projectRoot, resolvedOptions)), runtime.env), runtime.env), '已创建 Vite 开发服务器');
|
|
15178
|
+
let currentDevContext = devLaunchContext;
|
|
15179
|
+
let launchRuntimePermissions = context.createBrowserDevRuntimePermissions(devLaunchContext.runtimePermissions, context.createBrowserDevPermissionState(devLaunchContext));
|
|
15004
15180
|
const mobileDevContextUrlsByOrigin = new Map();
|
|
15181
|
+
let currentCompanionManifestSha256;
|
|
15005
15182
|
let currentLocalDevLaunchToken = devLaunchContext.localDevLaunchToken;
|
|
15006
|
-
|
|
15007
|
-
|
|
15008
|
-
|
|
15009
|
-
|
|
15010
|
-
|
|
15011
|
-
|
|
15012
|
-
|
|
15013
|
-
|
|
15014
|
-
|
|
15015
|
-
|
|
15183
|
+
let currentExpiresAt;
|
|
15184
|
+
let tokenSession;
|
|
15185
|
+
const readLaunchSource = () => ({
|
|
15186
|
+
companionArtifactAccessToken,
|
|
15187
|
+
companionManifestSha256: currentCompanionManifestSha256,
|
|
15188
|
+
devContextUrlsByMiniUrlOrigin: mobileDevContextUrlsByOrigin,
|
|
15189
|
+
localDevLaunchToken: currentLocalDevLaunchToken,
|
|
15190
|
+
devSessionId,
|
|
15191
|
+
expiresAt: currentExpiresAt,
|
|
15192
|
+
miniProgramId: devLaunchContext.miniProgramId,
|
|
15193
|
+
runtimePermissions: launchRuntimePermissions,
|
|
15194
|
+
sdkVersion: version.HB_SDK_VERSION,
|
|
15016
15195
|
});
|
|
15196
|
+
const launchPlugin = createMiniProgramDevLaunchVitePlugin(readLaunchSource, async (request, response) => {
|
|
15197
|
+
if (request.url !== MINI_PROGRAM_DEV_LAUNCH_PATH || request.headers[COMPANION_ARTIFACT_ACCESS_TOKEN_HEADER] === undefined)
|
|
15198
|
+
return false;
|
|
15199
|
+
response.setHeader('Cache-Control', 'no-store');
|
|
15200
|
+
const token = request.headers[COMPANION_ARTIFACT_ACCESS_TOKEN_HEADER];
|
|
15201
|
+
const remote = request.socket.remoteAddress;
|
|
15202
|
+
if (typeof token !== 'string' || !/^[A-Za-z0-9_-]{43}$/.test(token) ||
|
|
15203
|
+
!node_crypto.timingSafeEqual(Buffer.from(token), Buffer.from(companionArtifactAccessToken)) ||
|
|
15204
|
+
!['127.0.0.1', '::1', '::ffff:127.0.0.1'].includes(remote ?? '') ||
|
|
15205
|
+
!/^(127\.0\.0\.1|\[::1\]):\d{1,5}$/.test(request.headers.host ?? '')) {
|
|
15206
|
+
response.writeHead(403).end();
|
|
15207
|
+
return true;
|
|
15208
|
+
}
|
|
15209
|
+
if (request.method !== 'GET') {
|
|
15210
|
+
response.writeHead(405, { Allow: 'GET' }).end();
|
|
15211
|
+
return true;
|
|
15212
|
+
}
|
|
15213
|
+
try {
|
|
15214
|
+
const issued = await tokenSession?.getTicket(currentCompanionManifestSha256);
|
|
15215
|
+
if (!issued)
|
|
15216
|
+
response.writeHead(503).end();
|
|
15217
|
+
else
|
|
15218
|
+
response.writeHead(200, { 'Content-Type': 'application/json', 'X-Content-Type-Options': 'nosniff' }).end(JSON.stringify(createLaunchSnapshot({
|
|
15219
|
+
...readLaunchSource(), localDevLaunchToken: issued.token, expiresAt: issued.expiresAt,
|
|
15220
|
+
}, new URL(`http://${request.headers.host}`))));
|
|
15221
|
+
}
|
|
15222
|
+
catch (error) {
|
|
15223
|
+
const status = error instanceof index.CliError && (error.httpStatus === 401 || error.httpStatus === 403) ? error.httpStatus : 503;
|
|
15224
|
+
logger.debug(`本地开发会话续票失败:${index.readRedactedErrorMessage(error, { verbose: true })}`);
|
|
15225
|
+
response.writeHead(status).end();
|
|
15226
|
+
}
|
|
15227
|
+
return true;
|
|
15228
|
+
});
|
|
15229
|
+
const vite = await runDevStartupTask(logger, '正在加载项目 Vite', () => projectVite.loadProjectVite(projectRoot), '已加载项目 Vite');
|
|
15230
|
+
const appServer = await runDevStartupTask(logger, '正在创建 Vite 开发服务器', () => runtimePermissionEnv.withMiniappPlatformCspEnv(context.shouldSkipMiniappPlatformCsp(devLaunchContext.runtimePermissions), () => withCompanionArtifactAccessTokenEnv(companionArtifactAccessToken, () => withManagedDevOutputEnv(() => vite.createServer(createViteServerOptions(projectRoot, resolvedOptions, launchPlugin)), runtime.env), runtime.env), runtime.env), '已创建 Vite 开发服务器');
|
|
15017
15231
|
await runDevStartupTask(logger, '正在启动 Vite 开发服务器', () => appServer.listen(appPort), 'Vite 开发服务器已启动');
|
|
15018
15232
|
const appUrl = await runDevStartupTask(logger, '正在探测小程序入口', () => resolveViteAppUrl(appServer, resolvedOptions, fetchImpl), '已确定小程序入口');
|
|
15019
15233
|
const buildLanAddresses = () => createLanAddressCandidates({
|
|
@@ -15032,7 +15246,7 @@ async function runDevCommand(options, runtime = {}) {
|
|
|
15032
15246
|
try {
|
|
15033
15247
|
browserDevHost = await runDevStartupTask(logger, '正在启动浏览器调试页服务', () => startBrowserDevHostServer({
|
|
15034
15248
|
appUrl,
|
|
15035
|
-
devContext:
|
|
15249
|
+
devContext: currentDevContext,
|
|
15036
15250
|
fetchImpl,
|
|
15037
15251
|
getPort: getPortImpl,
|
|
15038
15252
|
defaultLanAddressId: lanAddresses[0]?.id,
|
|
@@ -15047,6 +15261,30 @@ async function runDevCommand(options, runtime = {}) {
|
|
|
15047
15261
|
lanAddresses: buildLanAddresses(),
|
|
15048
15262
|
});
|
|
15049
15263
|
},
|
|
15264
|
+
readMobileDevSessionContext: async () => {
|
|
15265
|
+
try {
|
|
15266
|
+
const refreshed = await loadDevLaunchContext({
|
|
15267
|
+
env: runtime.env,
|
|
15268
|
+
fetchImpl,
|
|
15269
|
+
logger,
|
|
15270
|
+
projectRoot,
|
|
15271
|
+
timeoutMs: runtime.devContextTimeoutMs ?? DEFAULT_DEV_CONTEXT_TIMEOUT_MS,
|
|
15272
|
+
});
|
|
15273
|
+
currentDevContext = withCurrentLocalDevLaunchToken(refreshed.context, currentLocalDevLaunchToken);
|
|
15274
|
+
launchRuntimePermissions = context.createMobileDevSessionRuntimePermissions(currentDevContext);
|
|
15275
|
+
browserDevHost.updateBootstrap({
|
|
15276
|
+
devContext: currentDevContext,
|
|
15277
|
+
miniProgram: refreshed.miniProgram,
|
|
15278
|
+
});
|
|
15279
|
+
return currentDevContext;
|
|
15280
|
+
}
|
|
15281
|
+
catch (error) {
|
|
15282
|
+
currentDevContext = { source: 'anonymous' };
|
|
15283
|
+
launchRuntimePermissions = { schema_version: 1, entries: [] };
|
|
15284
|
+
browserDevHost.updateBootstrap({ devContext: currentDevContext });
|
|
15285
|
+
throw error;
|
|
15286
|
+
}
|
|
15287
|
+
},
|
|
15050
15288
|
port: options.browserDevHostPort ?? DEFAULT_BROWSER_DEV_HOST_PORT,
|
|
15051
15289
|
root: runtime.browserDevHostRoot,
|
|
15052
15290
|
rootCandidates: runtime.browserDevHostRootCandidates,
|
|
@@ -15059,39 +15297,70 @@ async function runDevCommand(options, runtime = {}) {
|
|
|
15059
15297
|
}
|
|
15060
15298
|
closers.push(() => browserDevHost.close());
|
|
15061
15299
|
if (devLaunchContext.source === 'remote' && devLaunchContext.miniProgramId) {
|
|
15062
|
-
|
|
15300
|
+
tokenSession = startLocalDevLaunchTokenSession({
|
|
15063
15301
|
env: runtime.env,
|
|
15064
15302
|
fetchImpl,
|
|
15065
15303
|
logger,
|
|
15304
|
+
manifestUrl: new URL(COMPANION_DEV_MANIFEST_PATH, appUrl).toString(),
|
|
15305
|
+
companionArtifactAccessToken,
|
|
15306
|
+
companionApproved: hasCompanionApproval(devLaunchContext.runtimePermissions),
|
|
15066
15307
|
miniProgramId: devLaunchContext.miniProgramId,
|
|
15067
|
-
|
|
15308
|
+
refreshPermissions: async () => {
|
|
15309
|
+
const refreshed = await loadDevLaunchContext({ env: runtime.env, fetchImpl, logger, projectRoot,
|
|
15310
|
+
timeoutMs: runtime.devContextTimeoutMs ?? DEFAULT_DEV_CONTEXT_TIMEOUT_MS });
|
|
15311
|
+
if (refreshed.context.miniProgramId !== devLaunchContext.miniProgramId)
|
|
15312
|
+
throw new Error('项目绑定已变更,请重新启动调试');
|
|
15313
|
+
devLaunchContext.runtimePermissions = refreshed.context.runtimePermissions;
|
|
15314
|
+
currentDevContext = withCurrentLocalDevLaunchToken(devLaunchContext, currentLocalDevLaunchToken);
|
|
15315
|
+
launchRuntimePermissions = context.createBrowserDevRuntimePermissions(devLaunchContext.runtimePermissions, context.createBrowserDevPermissionState(devLaunchContext));
|
|
15316
|
+
browserDevHost.updateBootstrap({ devContext: currentDevContext });
|
|
15317
|
+
return hasCompanionApproval(devLaunchContext.runtimePermissions);
|
|
15318
|
+
},
|
|
15319
|
+
onToken: (localDevLaunchToken, companionManifestSha256, expiresAt) => {
|
|
15320
|
+
currentExpiresAt = expiresAt;
|
|
15321
|
+
currentCompanionManifestSha256 = companionManifestSha256;
|
|
15068
15322
|
currentLocalDevLaunchToken = localDevLaunchToken;
|
|
15069
|
-
|
|
15070
|
-
source: 'remote',
|
|
15071
|
-
miniProgramId: devLaunchContext.miniProgramId,
|
|
15072
|
-
...(localDevLaunchToken ? { localDevLaunchToken } : {}),
|
|
15073
|
-
runtimePermissions: devLaunchContext.runtimePermissions,
|
|
15074
|
-
};
|
|
15323
|
+
currentDevContext = withCurrentLocalDevLaunchToken(currentDevContext, localDevLaunchToken);
|
|
15075
15324
|
browserDevHost.updateBootstrap({
|
|
15076
|
-
devContext:
|
|
15325
|
+
devContext: currentDevContext,
|
|
15077
15326
|
lanAddresses: buildLanAddresses(),
|
|
15078
15327
|
});
|
|
15079
15328
|
},
|
|
15329
|
+
onReadiness: devReadiness => browserDevHost.updateBootstrap({ devReadiness }),
|
|
15080
15330
|
projectRoot,
|
|
15081
15331
|
timeoutMs: runtime.devContextTimeoutMs ?? DEFAULT_DEV_CONTEXT_TIMEOUT_MS,
|
|
15082
15332
|
});
|
|
15083
15333
|
closers.push(tokenSession.close);
|
|
15084
15334
|
}
|
|
15085
|
-
|
|
15335
|
+
else {
|
|
15336
|
+
const reason = '登录或小程序绑定不可用,请完成 CLI 登录和项目绑定';
|
|
15337
|
+
browserDevHost.updateBootstrap({
|
|
15338
|
+
devReadiness: {
|
|
15339
|
+
browser: { state: 'ready' },
|
|
15340
|
+
mobile: { state: 'blocked', reason },
|
|
15341
|
+
companion: { state: 'blocked', reason },
|
|
15342
|
+
},
|
|
15343
|
+
});
|
|
15344
|
+
}
|
|
15345
|
+
logger.success('Web / Browser Fake 调试已就绪');
|
|
15086
15346
|
logger.info(`调试页:${browserDevHost.url}`);
|
|
15087
15347
|
logger.info(`小程序地址:${appUrl}`);
|
|
15088
|
-
logger.info('
|
|
15348
|
+
logger.info('手机与 PC Companion 的就绪状态请查看调试页');
|
|
15089
15349
|
if (options.open !== false) {
|
|
15090
15350
|
logger.debug('正在打开浏览器调试页');
|
|
15091
15351
|
void openUrl(browserDevHost.url);
|
|
15092
15352
|
}
|
|
15093
15353
|
installShutdownHandlers(closers, runtime.process ?? process);
|
|
15094
15354
|
}
|
|
15355
|
+
function withCurrentLocalDevLaunchToken(context, localDevLaunchToken) {
|
|
15356
|
+
return {
|
|
15357
|
+
source: context.source,
|
|
15358
|
+
...(context.miniProgramId ? { miniProgramId: context.miniProgramId } : {}),
|
|
15359
|
+
...(localDevLaunchToken ? { localDevLaunchToken } : {}),
|
|
15360
|
+
...(context.runtimePermissions !== undefined ? { runtimePermissions: context.runtimePermissions } : {}),
|
|
15361
|
+
...(context.warning ? { warning: context.warning } : {}),
|
|
15362
|
+
};
|
|
15363
|
+
}
|
|
15095
15364
|
function runDevStartupTask(logger, text, action, successText) {
|
|
15096
15365
|
if (!logger.verbose) {
|
|
15097
15366
|
return action();
|
|
@@ -15165,10 +15434,26 @@ async function loadRemoteDevContextWithTimeout(options) {
|
|
|
15165
15434
|
}
|
|
15166
15435
|
}
|
|
15167
15436
|
}
|
|
15168
|
-
function
|
|
15437
|
+
function createMiniProgramDevLaunchVitePlugin(getSnapshot, handleSession) {
|
|
15438
|
+
return {
|
|
15439
|
+
name: 'heybox-mini-program-dev-launch',
|
|
15440
|
+
enforce: 'pre',
|
|
15441
|
+
configureServer(server) {
|
|
15442
|
+
server.middlewares.use(async (request, response, next) => {
|
|
15443
|
+
if (handleSession && await handleSession(request, response))
|
|
15444
|
+
return;
|
|
15445
|
+
if (!tryHandleMiniProgramDevLaunchRequest(request, response, getSnapshot))
|
|
15446
|
+
next();
|
|
15447
|
+
});
|
|
15448
|
+
},
|
|
15449
|
+
};
|
|
15450
|
+
}
|
|
15451
|
+
function createViteServerOptions(projectRoot, options, launchPlugin) {
|
|
15169
15452
|
const configFile = resolveProjectViteConfig(projectRoot);
|
|
15170
15453
|
const server = {
|
|
15171
|
-
|
|
15454
|
+
// launch.json 由 mini-program-dev-launch plugin 自己处理 PNA/CORS 预检;
|
|
15455
|
+
// Vite 通用 CORS 会抢先返回 `*`,缺少 Access-Control-Allow-Private-Network。
|
|
15456
|
+
cors: false,
|
|
15172
15457
|
hmr: {
|
|
15173
15458
|
clientPort: options.port ?? DEFAULT_APP_PORT,
|
|
15174
15459
|
},
|
|
@@ -15180,11 +15465,13 @@ function createViteServerOptions(projectRoot, options) {
|
|
|
15180
15465
|
return {
|
|
15181
15466
|
configFile,
|
|
15182
15467
|
logLevel: VITE_LOG_LEVEL,
|
|
15468
|
+
plugins: [launchPlugin],
|
|
15183
15469
|
server,
|
|
15184
15470
|
};
|
|
15185
15471
|
}
|
|
15186
15472
|
return {
|
|
15187
15473
|
logLevel: VITE_LOG_LEVEL,
|
|
15474
|
+
plugins: [launchPlugin],
|
|
15188
15475
|
root: projectRoot,
|
|
15189
15476
|
server,
|
|
15190
15477
|
};
|
|
@@ -15298,6 +15585,19 @@ async function withManagedDevOutputEnv(action, env = process.env) {
|
|
|
15298
15585
|
}
|
|
15299
15586
|
}
|
|
15300
15587
|
}
|
|
15588
|
+
async function withCompanionArtifactAccessTokenEnv(token, action, env = process.env) {
|
|
15589
|
+
const previous = env[COMPANION_ARTIFACT_ACCESS_TOKEN_ENV];
|
|
15590
|
+
env[COMPANION_ARTIFACT_ACCESS_TOKEN_ENV] = token;
|
|
15591
|
+
try {
|
|
15592
|
+
return await action();
|
|
15593
|
+
}
|
|
15594
|
+
finally {
|
|
15595
|
+
if (previous === undefined)
|
|
15596
|
+
delete env[COMPANION_ARTIFACT_ACCESS_TOKEN_ENV];
|
|
15597
|
+
else
|
|
15598
|
+
env[COMPANION_ARTIFACT_ACCESS_TOKEN_ENV] = previous;
|
|
15599
|
+
}
|
|
15600
|
+
}
|
|
15301
15601
|
function installShutdownHandlers(closers, processLike) {
|
|
15302
15602
|
const shutdown = async () => {
|
|
15303
15603
|
await closeAll(closers);
|
|
@@ -15318,28 +15618,94 @@ function startLocalDevLaunchTokenSession(options) {
|
|
|
15318
15618
|
let expirationTimer;
|
|
15319
15619
|
let hasReportedTokenFailure = false;
|
|
15320
15620
|
let refreshTimer;
|
|
15621
|
+
let companionApproved = options.companionApproved;
|
|
15622
|
+
let permissionsRefreshAt = Date.now() + 30_000;
|
|
15623
|
+
let publishedToken;
|
|
15624
|
+
let publishedDigest;
|
|
15625
|
+
let ticket;
|
|
15626
|
+
let pending;
|
|
15627
|
+
const getTicket = async (companionManifestSha256) => {
|
|
15628
|
+
if (closed)
|
|
15629
|
+
return undefined;
|
|
15630
|
+
if (ticket && (companionManifestSha256 === undefined || ticket.companionManifestSha256 === companionManifestSha256) && ticket.refreshAt > Date.now())
|
|
15631
|
+
return ticket;
|
|
15632
|
+
return issueTicket(companionManifestSha256);
|
|
15633
|
+
};
|
|
15634
|
+
const issueTicket = (companionManifestSha256) => {
|
|
15635
|
+
if (pending && pending.digest === companionManifestSha256)
|
|
15636
|
+
return pending.promise;
|
|
15637
|
+
const promise = issueLocalDevLaunchToken({
|
|
15638
|
+
...options,
|
|
15639
|
+
...(companionManifestSha256 ? { companionManifestSha256 } : {}),
|
|
15640
|
+
})
|
|
15641
|
+
.then(issued => {
|
|
15642
|
+
ticket = issued;
|
|
15643
|
+
return issued;
|
|
15644
|
+
}).finally(() => { if (pending?.promise === promise)
|
|
15645
|
+
pending = undefined; });
|
|
15646
|
+
pending = { digest: companionManifestSha256, promise };
|
|
15647
|
+
return promise;
|
|
15648
|
+
};
|
|
15321
15649
|
const refresh = async () => {
|
|
15322
15650
|
try {
|
|
15323
|
-
|
|
15651
|
+
if (options.refreshPermissions && Date.now() >= permissionsRefreshAt) {
|
|
15652
|
+
companionApproved = await options.refreshPermissions();
|
|
15653
|
+
permissionsRefreshAt = Date.now() + 30_000;
|
|
15654
|
+
}
|
|
15655
|
+
let manifest;
|
|
15656
|
+
let snapshotError;
|
|
15657
|
+
try {
|
|
15658
|
+
manifest = await loadRemoteDevContextWithTimeout({
|
|
15659
|
+
fetchImpl: options.fetchImpl,
|
|
15660
|
+
timeoutMs: options.timeoutMs,
|
|
15661
|
+
load: fetchImpl => readCompanionManifest(options.manifestUrl, options.companionArtifactAccessToken, fetchImpl),
|
|
15662
|
+
});
|
|
15663
|
+
}
|
|
15664
|
+
catch (error) {
|
|
15665
|
+
snapshotError = error;
|
|
15666
|
+
}
|
|
15667
|
+
const digest = manifest?.digest;
|
|
15668
|
+
const issued = await getTicket(digest);
|
|
15669
|
+
if (!issued)
|
|
15670
|
+
return;
|
|
15324
15671
|
if (closed)
|
|
15325
15672
|
return;
|
|
15326
|
-
|
|
15673
|
+
if (publishedToken !== issued.token || publishedDigest !== digest) {
|
|
15674
|
+
publishedToken = issued.token;
|
|
15675
|
+
publishedDigest = digest;
|
|
15676
|
+
options.onToken(issued.token, digest, issued.expiresAt);
|
|
15677
|
+
}
|
|
15678
|
+
options.onReadiness({
|
|
15679
|
+
browser: { state: 'ready' },
|
|
15680
|
+
mobile: { state: 'ready' },
|
|
15681
|
+
companion: snapshotError
|
|
15682
|
+
? { state: snapshotError instanceof Error && snapshotError.message.includes('artifact-updating') ? 'pending' : 'blocked', reason: safeCompanionFailureReason(snapshotError) }
|
|
15683
|
+
: manifest?.artifacts.length
|
|
15684
|
+
? companionApproved
|
|
15685
|
+
? { state: 'ready', digest, artifacts: manifest.artifacts, reason: '本地产物和启动票已就绪,PC 授权将在打开后验证' }
|
|
15686
|
+
: { state: 'blocked', reason: 'Companion 未获 COA 批准或本地权限声明未开启' }
|
|
15687
|
+
: { state: 'not-configured', reason: '未配置本地 Companion 产物' },
|
|
15688
|
+
});
|
|
15689
|
+
if (snapshotError)
|
|
15690
|
+
options.logger.debug(`PC Companion 快照校验失败:${index.readRedactedErrorMessage(snapshotError, { verbose: true })}`);
|
|
15327
15691
|
const ttl = Math.max(0, issued.expiresAt - Date.now());
|
|
15328
15692
|
scheduleExpiration(ttl);
|
|
15329
|
-
schedule(Math.max(1_000, Math.floor(ttl * 0.8)));
|
|
15693
|
+
schedule(Math.min(2_000, Math.max(1_000, Math.floor(ttl * 0.8))));
|
|
15330
15694
|
if (hasReportedTokenFailure) {
|
|
15331
15695
|
hasReportedTokenFailure = false;
|
|
15332
|
-
options.logger.success('
|
|
15696
|
+
options.logger.success('本地调试凭证已恢复');
|
|
15333
15697
|
}
|
|
15334
15698
|
}
|
|
15335
15699
|
catch (error) {
|
|
15336
15700
|
if (closed)
|
|
15337
15701
|
return;
|
|
15338
15702
|
if (!hasReportedTokenFailure) {
|
|
15339
|
-
options.logger.warn('
|
|
15703
|
+
options.logger.warn('本地调试凭证获取失败,将自动重试;Web / Browser Fake 仍可使用');
|
|
15340
15704
|
}
|
|
15341
15705
|
hasReportedTokenFailure = true;
|
|
15342
|
-
options.
|
|
15706
|
+
options.onReadiness({ browser: { state: 'ready' }, mobile: { state: 'blocked', reason: '调试凭证暂不可用' },
|
|
15707
|
+
companion: { state: 'blocked', reason: safeCompanionFailureReason(error) } });
|
|
15708
|
+
options.logger.debug(`本地调试凭证获取失败,详细原因:${index.readRedactedErrorMessage(error, { verbose: true })}`);
|
|
15343
15709
|
schedule(options.timeoutMs);
|
|
15344
15710
|
}
|
|
15345
15711
|
};
|
|
@@ -15353,13 +15719,20 @@ function startLocalDevLaunchTokenSession(options) {
|
|
|
15353
15719
|
if (expirationTimer)
|
|
15354
15720
|
clearTimeout(expirationTimer);
|
|
15355
15721
|
expirationTimer = setTimeout(() => {
|
|
15356
|
-
if (!closed)
|
|
15357
|
-
options.onToken(undefined);
|
|
15722
|
+
if (!closed) {
|
|
15723
|
+
options.onToken(undefined, undefined);
|
|
15724
|
+
publishedToken = undefined;
|
|
15725
|
+
options.onReadiness({ browser: { state: 'ready' }, mobile: { state: 'blocked', reason: '调试凭证已过期' },
|
|
15726
|
+
companion: { state: 'blocked', reason: '调试凭证已过期' } });
|
|
15727
|
+
}
|
|
15358
15728
|
}, Math.max(1, ttl));
|
|
15359
15729
|
expirationTimer.unref?.();
|
|
15360
15730
|
};
|
|
15361
|
-
|
|
15731
|
+
options.onReadiness({ browser: { state: 'ready' }, mobile: { state: 'pending' }, companion: { state: 'pending' } });
|
|
15732
|
+
const ready = refresh();
|
|
15362
15733
|
return {
|
|
15734
|
+
getTicket,
|
|
15735
|
+
ready,
|
|
15363
15736
|
close: () => {
|
|
15364
15737
|
closed = true;
|
|
15365
15738
|
if (expirationTimer)
|
|
@@ -15373,22 +15746,128 @@ async function issueLocalDevLaunchToken(options) {
|
|
|
15373
15746
|
const issued = await loadRemoteDevContextWithTimeout({
|
|
15374
15747
|
fetchImpl: options.fetchImpl,
|
|
15375
15748
|
timeoutMs: options.timeoutMs,
|
|
15376
|
-
load: (fetchImpl) =>
|
|
15377
|
-
|
|
15378
|
-
|
|
15379
|
-
|
|
15380
|
-
|
|
15381
|
-
|
|
15749
|
+
load: async (fetchImpl) => {
|
|
15750
|
+
const result = await context.createLocalDevLaunchToken({
|
|
15751
|
+
cwd: options.projectRoot,
|
|
15752
|
+
env: options.env,
|
|
15753
|
+
fetchImpl,
|
|
15754
|
+
miniProgramId: options.miniProgramId,
|
|
15755
|
+
...(options.companionManifestSha256
|
|
15756
|
+
? { companionManifestSha256: options.companionManifestSha256 }
|
|
15757
|
+
: {}),
|
|
15758
|
+
});
|
|
15759
|
+
return { result };
|
|
15760
|
+
},
|
|
15382
15761
|
});
|
|
15383
|
-
const token = typeof issued.token === 'string' ? issued.token.trim() : '';
|
|
15762
|
+
const token = typeof issued.result.token === 'string' ? issued.result.token.trim() : '';
|
|
15384
15763
|
if (!token) {
|
|
15385
15764
|
throw new Error('响应缺少 token');
|
|
15386
15765
|
}
|
|
15387
|
-
const expiresAt = normalizeExpiresAt(issued.expires_at);
|
|
15766
|
+
const expiresAt = normalizeExpiresAt(issued.result.expires_at);
|
|
15388
15767
|
if (!expiresAt || expiresAt <= Date.now()) {
|
|
15389
15768
|
throw new Error('响应缺少有效 expires_at');
|
|
15390
15769
|
}
|
|
15391
|
-
return { expiresAt, token
|
|
15770
|
+
return { ...(options.companionManifestSha256 ? { companionManifestSha256: options.companionManifestSha256 } : {}), expiresAt, token,
|
|
15771
|
+
refreshAt: Date.now() + Math.max(1, (expiresAt - Date.now()) * 0.8) };
|
|
15772
|
+
}
|
|
15773
|
+
function safeCompanionFailureReason(error) {
|
|
15774
|
+
const message = index.readRedactedErrorMessage(error, { verbose: true });
|
|
15775
|
+
if (/artifact-updating/.test(message))
|
|
15776
|
+
return '本地 Companion 产物准备中';
|
|
15777
|
+
if (/artifact-missing/.test(message))
|
|
15778
|
+
return '本地 Companion 产物目录不存在,请先构建产物';
|
|
15779
|
+
if (/snapshot-limit/.test(message))
|
|
15780
|
+
return '本地快照数量或大小已达上限,请结束会话后重启 dev';
|
|
15781
|
+
if (/invalid-manifest/.test(message))
|
|
15782
|
+
return '本地 Manifest 或权限配置无效';
|
|
15783
|
+
if (/invalid-artifact/.test(message))
|
|
15784
|
+
return '本地产物或原生入口校验失败';
|
|
15785
|
+
if (/404|not found/i.test(message))
|
|
15786
|
+
return '调试接口尚未部署或当前登录环境不匹配';
|
|
15787
|
+
if (/403|forbidden|permission|授权|权限/i.test(message))
|
|
15788
|
+
return 'Companion 授权未通过,请检查 COA 批准与开发者身份';
|
|
15789
|
+
if (/401|登录/i.test(message))
|
|
15790
|
+
return 'CLI 登录态已失效,请重新登录';
|
|
15791
|
+
if (/manifest|快照|ZIP|ENOENT/i.test(message))
|
|
15792
|
+
return '本地 Companion ZIP 或 Manifest 校验失败,请检查产物与配置';
|
|
15793
|
+
return 'PC Companion 暂不可用,请检查网络、登录环境和调试接口';
|
|
15794
|
+
}
|
|
15795
|
+
function hasCompanionApproval(value) {
|
|
15796
|
+
if (!value || typeof value !== 'object' || !('entries' in value) || !Array.isArray(value.entries))
|
|
15797
|
+
return false;
|
|
15798
|
+
return value.entries.some(entry => entry?.key === 'companion' && entry?.status === 'enabled');
|
|
15799
|
+
}
|
|
15800
|
+
async function readCompanionManifest(manifestUrl, companionArtifactAccessToken, fetchImpl) {
|
|
15801
|
+
try {
|
|
15802
|
+
const response = await fetchImpl(manifestUrl, {
|
|
15803
|
+
cache: 'no-store',
|
|
15804
|
+
credentials: 'omit',
|
|
15805
|
+
headers: {
|
|
15806
|
+
accept: 'application/json',
|
|
15807
|
+
[COMPANION_ARTIFACT_ACCESS_TOKEN_HEADER]: companionArtifactAccessToken,
|
|
15808
|
+
},
|
|
15809
|
+
method: 'GET',
|
|
15810
|
+
redirect: 'error',
|
|
15811
|
+
referrerPolicy: 'no-referrer',
|
|
15812
|
+
});
|
|
15813
|
+
if (response.status === 404) {
|
|
15814
|
+
return undefined;
|
|
15815
|
+
}
|
|
15816
|
+
if (!response.ok) {
|
|
15817
|
+
const code = response.headers.get('x-hb-sdk-companion-error');
|
|
15818
|
+
const safeCode = ['artifact-updating', 'artifact-missing', 'snapshot-limit', 'invalid-manifest', 'invalid-artifact'].includes(code ?? '') ? code : '';
|
|
15819
|
+
throw new Error(`Companion manifest request failed with status ${response.status} ${safeCode}`);
|
|
15820
|
+
}
|
|
15821
|
+
if (!response.headers.get('content-type')?.toLowerCase().includes('application/json')) {
|
|
15822
|
+
throw new Error('Companion manifest response is not JSON');
|
|
15823
|
+
}
|
|
15824
|
+
const contentLength = Number(response.headers.get('content-length'));
|
|
15825
|
+
if (Number.isFinite(contentLength) && contentLength > COMPANION_DEV_MANIFEST_MAX_BYTES) {
|
|
15826
|
+
throw new Error('Companion manifest response is too large');
|
|
15827
|
+
}
|
|
15828
|
+
const bytes = await readBoundedCompanionManifestBytes(response, COMPANION_DEV_MANIFEST_MAX_BYTES);
|
|
15829
|
+
const value = JSON.parse(bytes.toString('utf8'));
|
|
15830
|
+
const artifacts = Object.entries(value.companions ?? {}).flatMap(([alias, definition]) => {
|
|
15831
|
+
if (!/^[a-z][a-z0-9-]{0,31}$/.test(alias) || !definition || typeof definition !== 'object') {
|
|
15832
|
+
throw new Error('Companion invalid-manifest');
|
|
15833
|
+
}
|
|
15834
|
+
return Object.keys(definition.targets ?? {}).map(target => {
|
|
15835
|
+
if (target !== 'windows-x64' && target !== 'macos-arm64')
|
|
15836
|
+
throw new Error('Companion invalid-manifest');
|
|
15837
|
+
return { alias, target };
|
|
15838
|
+
});
|
|
15839
|
+
});
|
|
15840
|
+
return { digest: node_crypto.createHash('sha256').update(bytes).digest('hex'), artifacts };
|
|
15841
|
+
}
|
|
15842
|
+
catch (error) {
|
|
15843
|
+
if (error instanceof Error)
|
|
15844
|
+
throw error;
|
|
15845
|
+
throw new Error('Companion manifest request failed');
|
|
15846
|
+
}
|
|
15847
|
+
}
|
|
15848
|
+
async function readBoundedCompanionManifestBytes(response, maxBytes) {
|
|
15849
|
+
if (!response.body)
|
|
15850
|
+
return Buffer.alloc(0);
|
|
15851
|
+
const reader = response.body.getReader();
|
|
15852
|
+
const chunks = [];
|
|
15853
|
+
let total = 0;
|
|
15854
|
+
try {
|
|
15855
|
+
for (;;) {
|
|
15856
|
+
const { done, value } = await reader.read();
|
|
15857
|
+
if (done)
|
|
15858
|
+
break;
|
|
15859
|
+
total += value.byteLength;
|
|
15860
|
+
if (total > maxBytes) {
|
|
15861
|
+
await reader.cancel('Companion manifest response is too large');
|
|
15862
|
+
throw new Error('Companion manifest response is too large');
|
|
15863
|
+
}
|
|
15864
|
+
chunks.push(value);
|
|
15865
|
+
}
|
|
15866
|
+
}
|
|
15867
|
+
finally {
|
|
15868
|
+
reader.releaseLock();
|
|
15869
|
+
}
|
|
15870
|
+
return Buffer.concat(chunks.map(chunk => Buffer.from(chunk)), total);
|
|
15392
15871
|
}
|
|
15393
15872
|
function normalizeExpiresAt(value) {
|
|
15394
15873
|
if (typeof value !== 'number' || !Number.isFinite(value) || value <= 0)
|
|
@@ -15501,8 +15980,10 @@ function isPrivateIPv4(address) {
|
|
|
15501
15980
|
|
|
15502
15981
|
var dev = /*#__PURE__*/Object.freeze({
|
|
15503
15982
|
__proto__: null,
|
|
15983
|
+
createMiniProgramDevLaunchVitePlugin: createMiniProgramDevLaunchVitePlugin,
|
|
15504
15984
|
normalizeExpiresAt: normalizeExpiresAt,
|
|
15505
|
-
runDevCommand: runDevCommand
|
|
15985
|
+
runDevCommand: runDevCommand,
|
|
15986
|
+
startLocalDevLaunchTokenSession: startLocalDevLaunchTokenSession
|
|
15506
15987
|
});
|
|
15507
15988
|
|
|
15508
15989
|
exports.RuntimeError = RuntimeError;
|