收口宿主上下文查询契约
共享 HostBridge 契约新增宿主上下文 query 字段和值 移动壳和 H5 运行态改用共享 query 契约 桌面壳和微信壳门禁反查共享 query 契约 微信请求头和 H5 路由保留字段跟随共享契约 同步宿主上下文契约文档和共享记忆
This commit is contained in:
@@ -690,6 +690,22 @@ function extractStringConst(source, constName) {
|
||||
return match[1];
|
||||
}
|
||||
|
||||
function extractTsStringObject(source, exportName) {
|
||||
const match = source.match(
|
||||
new RegExp(`export const ${exportName}\\s*=\\s*\\{([\\s\\S]*?)\\}\\s*as const;`),
|
||||
);
|
||||
if (!match) {
|
||||
throw new Error(`unable to read ${exportName}`);
|
||||
}
|
||||
|
||||
return Object.fromEntries(
|
||||
[...match[1].matchAll(/([A-Za-z0-9_]+):\s*'([^']+)'/g)].map((entry) => [
|
||||
entry[1],
|
||||
entry[2],
|
||||
]),
|
||||
);
|
||||
}
|
||||
|
||||
function extractDocumentCapabilityList(source, marker) {
|
||||
const markerIndex = source.indexOf(marker);
|
||||
if (markerIndex === -1) {
|
||||
@@ -938,6 +954,10 @@ function assertHttpsDomain(value, label) {
|
||||
}
|
||||
|
||||
function assertWechatMiniProgramRouteParity() {
|
||||
const sharedContractSource = fs.readFileSync(
|
||||
sharedHostBridgeContractPath,
|
||||
'utf8',
|
||||
);
|
||||
const appConfig = JSON.parse(fs.readFileSync('miniprogram/app.json', 'utf8'));
|
||||
const runtimeConfig = requireCommonJsModule('miniprogram/config.js');
|
||||
const protocol = requireCommonJsModule('miniprogram/host-bridge/protocol.js');
|
||||
@@ -945,6 +965,14 @@ function assertWechatMiniProgramRouteParity() {
|
||||
'miniprogram/host-bridge/webView.js',
|
||||
'utf8',
|
||||
);
|
||||
const webViewShellSource = fs.readFileSync(
|
||||
'miniprogram/shell/webView.js',
|
||||
'utf8',
|
||||
);
|
||||
const appPageRoutesSource = fs.readFileSync(
|
||||
'src/routing/appPageRoutes.ts',
|
||||
'utf8',
|
||||
);
|
||||
const h5HostBridgeSource = fs.readFileSync(
|
||||
'src/services/host-bridge/hostBridge.ts',
|
||||
'utf8',
|
||||
@@ -1013,15 +1041,72 @@ function assertWechatMiniProgramRouteParity() {
|
||||
}
|
||||
|
||||
const sourceQuery = runtimeConfig.WEB_VIEW_SOURCE_QUERY ?? {};
|
||||
const sharedRuntimeContextQueryKey = extractTsStringObject(
|
||||
sharedContractSource,
|
||||
'HOST_BRIDGE_RUNTIME_CONTEXT_QUERY_KEY',
|
||||
);
|
||||
const sharedPreservedRuntimeContextQueryKeys = extractTsStringArray(
|
||||
sharedContractSource,
|
||||
'HOST_BRIDGE_PRESERVED_RUNTIME_CONTEXT_QUERY_KEYS',
|
||||
);
|
||||
const sharedWechatSourceQuery = extractTsStringObject(
|
||||
sharedContractSource,
|
||||
'HOST_BRIDGE_WECHAT_MINI_PROGRAM_SOURCE_QUERY',
|
||||
);
|
||||
assertSameList(
|
||||
Object.keys(sourceQuery),
|
||||
Object.keys(sharedWechatSourceQuery),
|
||||
'wechat web-view source query keys',
|
||||
);
|
||||
if (
|
||||
sourceQuery.clientType !== 'mini_program' ||
|
||||
sourceQuery.clientRuntime !== 'wechat_mini_program' ||
|
||||
Object.keys(sourceQuery).some(
|
||||
(key) => key !== 'clientType' && key !== 'clientRuntime',
|
||||
Object.entries(sharedWechatSourceQuery).some(
|
||||
([key, value]) => sourceQuery[key] !== value,
|
||||
)
|
||||
) {
|
||||
throw new Error('wechat web-view source query drifted from HostBridge runtime markers');
|
||||
}
|
||||
assertSameList(
|
||||
sharedPreservedRuntimeContextQueryKeys,
|
||||
[
|
||||
sharedRuntimeContextQueryKey.clientType,
|
||||
sharedRuntimeContextQueryKey.clientRuntime,
|
||||
sharedRuntimeContextQueryKey.miniProgramEnv,
|
||||
],
|
||||
'preserved HostBridge runtime context query keys',
|
||||
);
|
||||
if (
|
||||
!appPageRoutesSource.includes(
|
||||
'HOST_BRIDGE_PRESERVED_RUNTIME_CONTEXT_QUERY_KEYS',
|
||||
) ||
|
||||
!appPageRoutesSource.includes(
|
||||
'APP_RUNTIME_CONTEXT_QUERY_KEYS =\n HOST_BRIDGE_PRESERVED_RUNTIME_CONTEXT_QUERY_KEYS',
|
||||
)
|
||||
) {
|
||||
throw new Error('H5 app routes must preserve HostBridge runtime context keys from shared contract');
|
||||
}
|
||||
if (
|
||||
!h5HostBridgeSource.includes('HOST_BRIDGE_RUNTIME_CONTEXT_QUERY_KEY') ||
|
||||
h5HostBridgeSource.includes("params.get('clientType')") ||
|
||||
h5HostBridgeSource.includes("params.get('clientRuntime')") ||
|
||||
h5HostBridgeSource.includes("params.get('hostCapabilities')") ||
|
||||
h5HostBridgeSource.includes("params.get('miniProgramEnv')")
|
||||
) {
|
||||
throw new Error('H5 HostBridge runtime parser must read query keys from shared contract');
|
||||
}
|
||||
for (const snippet of [
|
||||
"readWebViewSourceQueryValue('clientType')",
|
||||
"readWebViewSourceQueryValue('clientRuntime')",
|
||||
]) {
|
||||
if (!webViewShellSource.includes(snippet)) {
|
||||
throw new Error('wechat request headers must read runtime markers from WEB_VIEW_SOURCE_QUERY');
|
||||
}
|
||||
}
|
||||
if (
|
||||
webViewShellSource.includes('MINI_PROGRAM_CLIENT_TYPE') ||
|
||||
webViewShellSource.includes('MINI_PROGRAM_CLIENT_RUNTIME')
|
||||
) {
|
||||
throw new Error('wechat request runtime markers must not be duplicated outside WEB_VIEW_SOURCE_QUERY');
|
||||
}
|
||||
}
|
||||
|
||||
function assertHostBridgeLayerLayout() {
|
||||
|
||||
Reference in New Issue
Block a user