@lingxia/bridge 0.15.0 → 0.17.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.
@@ -45,6 +45,17 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
45
45
  if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
46
46
  }
47
47
  };
48
+ var __rest = (this && this.__rest) || function (s, e) {
49
+ var t = {};
50
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
51
+ t[p] = s[p];
52
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
53
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
54
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
55
+ t[p[i]] = s[p[i]];
56
+ }
57
+ return t;
58
+ };
48
59
  var __values = (this && this.__values) || function(o) {
49
60
  var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
50
61
  if (m) return m.call(o);
@@ -84,7 +95,8 @@ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
84
95
  import { BRIDGE_ERROR } from "./types";
85
96
  import { toBridgeError, toNativeError } from "./invocation";
86
97
  import { installNativeComponentCoverageMonitor } from "./nativecomponents/coverage-monitor";
87
- import { BRIDGE_CONFIG, getCommunicationMethod, getPlatformOS, isAndroid, isHarmony, isIOS, isMacOS, isWindows, isDesktop, isMobile, isApple, isDevSession, isRunner, } from "./runtime-env";
98
+ import { consumeV3Bootstrap, } from "./protocol-v3";
99
+ import { BRIDGE_CONFIG, getCommunicationMethod, getDisplayLanguage, getPlatformOS, subscribeDisplayLanguage, isAndroid, isHarmony, isIOS, isMacOS, isWindows, isDesktop, isMobile, isApple, isDevSession, isRunner, } from "./runtime-env";
88
100
  var NATIVE_HANDLER_NAME = "LingXia";
89
101
  var GLOBAL_RECEIVER_NAME = "__LingXiaRecvMessage";
90
102
  var DEFAULT_TIMEOUT_MS = 5000;
@@ -98,8 +110,28 @@ var OUTBOX_LIMIT = 256;
98
110
  var APPLE_DOWNSTREAM_URL = BRIDGE_CONFIG.appleDownstreamURL;
99
111
  var APPLE_RECONNECT_BASE_MS = 200;
100
112
  var APPLE_RECONNECT_MAX_MS = 2000;
113
+ var BOUND_CONSOLE_MESSAGE_CHARS = 12 * 1024;
114
+ // Native drops a larger frame before it reaches the bridge, so a request that
115
+ // exceeds this would otherwise only surface as a timeout. Mirrors
116
+ // MAX_WEB_MESSAGE_BYTES in the webview crate.
117
+ var MAX_NATIVE_MESSAGE_BYTES = 64 * 1024;
118
+ // Keep framework diagnostics and console forwarding on the original methods.
119
+ // RequiredV3 installs a page-facing wrapper later; using it here would recurse
120
+ // and could accidentally serialize the bound envelope into another log.
121
+ var nativeConsole = {
122
+ log: console.log.bind(console),
123
+ warn: console.warn.bind(console),
124
+ error: console.error.bind(console),
125
+ info: (console.info || console.log).bind(console),
126
+ debug: (console.debug || console.log).bind(console),
127
+ group: (console.group || console.log).bind(console),
128
+ groupEnd: (console.groupEnd || (function () { })).bind(console),
129
+ };
101
130
  var debugFlags = { data: false, proto: false, all: false };
102
131
  var earlyNativeMessages = [];
132
+ // Fixed once, during init. The V3 secret remains captured only by the codec.
133
+ var protocolMode = { kind: "v2" };
134
+ var protocolInitialized = false;
103
135
  // Plain-object equivalent of `new Proxy(debugFlags, ...)`. Avoids referencing
104
136
  // the `Proxy` global so the module loads on older WebViews (Android 5.x stock
105
137
  // WebView is Chromium 37–39, which predates Chromium 49's Proxy support).
@@ -114,32 +146,79 @@ function createDebugObject(flags) {
114
146
  },
115
147
  set: function (value) {
116
148
  flags[key] = !!value;
117
- console.log("[LX] ".concat(key, ": ").concat(value));
149
+ nativeConsole.log("[LX] ".concat(key, ": ").concat(value));
118
150
  },
119
151
  });
120
152
  });
121
153
  return target;
122
154
  }
155
+ function drainHostEarlyNativeMessages() {
156
+ var e_1, _a;
157
+ if (typeof window === "undefined")
158
+ return;
159
+ var hostQueue = window.__LingXiaEarlyNativeMessages;
160
+ if (!Array.isArray(hostQueue) || hostQueue.length === 0)
161
+ return;
162
+ window.__LingXiaEarlyNativeMessages = [];
163
+ try {
164
+ for (var hostQueue_1 = __values(hostQueue), hostQueue_1_1 = hostQueue_1.next(); !hostQueue_1_1.done; hostQueue_1_1 = hostQueue_1.next()) {
165
+ var message = hostQueue_1_1.value;
166
+ if (typeof message === "string") {
167
+ earlyNativeMessages.push(message);
168
+ }
169
+ }
170
+ }
171
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
172
+ finally {
173
+ try {
174
+ if (hostQueue_1_1 && !hostQueue_1_1.done && (_a = hostQueue_1.return)) _a.call(hostQueue_1);
175
+ }
176
+ finally { if (e_1) throw e_1.error; }
177
+ }
178
+ }
123
179
  function installEarlyReceiver() {
180
+ var e_2, _a;
124
181
  if (typeof window === "undefined")
125
182
  return;
126
- if (typeof window[GLOBAL_RECEIVER_NAME] === "function")
183
+ drainHostEarlyNativeMessages();
184
+ var existing = window[GLOBAL_RECEIVER_NAME];
185
+ if (typeof existing === "function") {
186
+ if (earlyNativeMessages.length === 0)
187
+ return;
188
+ var queued = earlyNativeMessages.splice(0, earlyNativeMessages.length);
189
+ try {
190
+ for (var queued_1 = __values(queued), queued_1_1 = queued_1.next(); !queued_1_1.done; queued_1_1 = queued_1.next()) {
191
+ var message = queued_1_1.value;
192
+ try {
193
+ existing(message);
194
+ }
195
+ catch (err) {
196
+ warn("Failed to replay queued native message:", err);
197
+ }
198
+ }
199
+ }
200
+ catch (e_2_1) { e_2 = { error: e_2_1 }; }
201
+ finally {
202
+ try {
203
+ if (queued_1_1 && !queued_1_1.done && (_a = queued_1.return)) _a.call(queued_1);
204
+ }
205
+ finally { if (e_2) throw e_2.error; }
206
+ }
127
207
  return;
208
+ }
128
209
  window[GLOBAL_RECEIVER_NAME] = function (message) {
129
210
  earlyNativeMessages.push(message);
130
211
  };
131
212
  }
132
- function activateReceiver(receiver) {
133
- var e_1, _a;
134
- if (typeof window === "undefined")
135
- return;
136
- window[GLOBAL_RECEIVER_NAME] = receiver;
213
+ function flushEarlyNativeMessages(receiver) {
214
+ var e_3, _a;
215
+ drainHostEarlyNativeMessages();
137
216
  if (earlyNativeMessages.length === 0)
138
217
  return;
139
218
  var queued = earlyNativeMessages.splice(0, earlyNativeMessages.length);
140
219
  try {
141
- for (var queued_1 = __values(queued), queued_1_1 = queued_1.next(); !queued_1_1.done; queued_1_1 = queued_1.next()) {
142
- var message = queued_1_1.value;
220
+ for (var queued_2 = __values(queued), queued_2_1 = queued_2.next(); !queued_2_1.done; queued_2_1 = queued_2.next()) {
221
+ var message = queued_2_1.value;
143
222
  try {
144
223
  receiver(message);
145
224
  }
@@ -148,14 +227,25 @@ function activateReceiver(receiver) {
148
227
  }
149
228
  }
150
229
  }
151
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
230
+ catch (e_3_1) { e_3 = { error: e_3_1 }; }
152
231
  finally {
153
232
  try {
154
- if (queued_1_1 && !queued_1_1.done && (_a = queued_1.return)) _a.call(queued_1);
233
+ if (queued_2_1 && !queued_2_1.done && (_a = queued_2.return)) _a.call(queued_2);
155
234
  }
156
- finally { if (e_1) throw e_1.error; }
235
+ finally { if (e_3) throw e_3.error; }
157
236
  }
158
237
  }
238
+ function activateReceiver(receiver) {
239
+ if (typeof window === "undefined")
240
+ return;
241
+ // Drain the inject-script queue on every inbound call so late
242
+ // `__LingXiaEarlyNativeMessages` items flush without rebinding.
243
+ window[GLOBAL_RECEIVER_NAME] = function (message) {
244
+ flushEarlyNativeMessages(receiver);
245
+ receiver(message);
246
+ };
247
+ flushEarlyNativeMessages(receiver);
248
+ }
159
249
  function isDebugEnabled(flag) {
160
250
  return debugFlags.all || debugFlags[flag];
161
251
  }
@@ -175,21 +265,21 @@ function log() {
175
265
  !debugFlags.data) {
176
266
  return;
177
267
  }
178
- console.log.apply(console, __spreadArray([LOG_PREFIX], __read(args), false));
268
+ nativeConsole.log.apply(nativeConsole, __spreadArray([LOG_PREFIX], __read(args), false));
179
269
  }
180
270
  function warn() {
181
271
  var args = [];
182
272
  for (var _i = 0; _i < arguments.length; _i++) {
183
273
  args[_i] = arguments[_i];
184
274
  }
185
- console.warn.apply(console, __spreadArray([LOG_PREFIX], __read(args), false));
275
+ nativeConsole.warn.apply(nativeConsole, __spreadArray([LOG_PREFIX], __read(args), false));
186
276
  }
187
277
  function error() {
188
278
  var args = [];
189
279
  for (var _i = 0; _i < arguments.length; _i++) {
190
280
  args[_i] = arguments[_i];
191
281
  }
192
- console.error.apply(console, __spreadArray([LOG_PREFIX], __read(args), false));
282
+ nativeConsole.error.apply(nativeConsole, __spreadArray([LOG_PREFIX], __read(args), false));
193
283
  }
194
284
  function safeStringify(obj, space) {
195
285
  var seen = new WeakSet();
@@ -238,7 +328,15 @@ var appleLastFrameSeq = 0;
238
328
  // than a couple of heartbeats means the connection is silently dead and we
239
329
  // reconnect proactively instead of waiting for a read error that may never come.
240
330
  var APPLE_DOWNSTREAM_STALE_MS = 35000;
331
+ // A downstream that has never delivered a frame gets this long to connect.
332
+ var APPLE_DOWNSTREAM_CONNECT_TIMEOUT_MS = 10000;
241
333
  var appleLastFrameAt = 0;
334
+ var appleDownstreamAttemptAt = 0;
335
+ // Names this page load to the host transport, so a reloaded document never
336
+ // resumes the sequence of the one it replaced.
337
+ var APPLE_DOWNSTREAM_DOCUMENT = "".concat(Date.now().toString(36)).concat(Math.random()
338
+ .toString(36)
339
+ .slice(2, 10));
242
340
  var appleWatchdogTimer = null;
243
341
  var portInitState = {
244
342
  listenerInstalled: false,
@@ -302,7 +400,7 @@ function disposeAppleDownstream(reason) {
302
400
  resetHandshakeState(reason, true);
303
401
  }
304
402
  function closeActiveChannelsFromTransport(reason) {
305
- var e_2, _a;
403
+ var e_4, _a;
306
404
  try {
307
405
  for (var _b = __values(Array.from(activeChannels.entries())), _c = _b.next(); !_c.done; _c = _b.next()) {
308
406
  var _d = __read(_c.value, 2), id = _d[0], channel_1 = _d[1];
@@ -310,16 +408,16 @@ function closeActiveChannelsFromTransport(reason) {
310
408
  channel_1._emitClose(BRIDGE_ERROR.STREAM_CLOSED, reason);
311
409
  }
312
410
  }
313
- catch (e_2_1) { e_2 = { error: e_2_1 }; }
411
+ catch (e_4_1) { e_4 = { error: e_4_1 }; }
314
412
  finally {
315
413
  try {
316
414
  if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
317
415
  }
318
- finally { if (e_2) throw e_2.error; }
416
+ finally { if (e_4) throw e_4.error; }
319
417
  }
320
418
  }
321
419
  function rejectAllPendingForTransport(reason) {
322
- var e_3, _a, e_4, _b;
420
+ var e_5, _a, e_6, _b;
323
421
  try {
324
422
  for (var _c = __values(Array.from(pendingReq.keys())), _d = _c.next(); !_d.done; _d = _c.next()) {
325
423
  var id = _d.value;
@@ -329,12 +427,12 @@ function rejectAllPendingForTransport(reason) {
329
427
  });
330
428
  }
331
429
  }
332
- catch (e_3_1) { e_3 = { error: e_3_1 }; }
430
+ catch (e_5_1) { e_5 = { error: e_5_1 }; }
333
431
  finally {
334
432
  try {
335
433
  if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
336
434
  }
337
- finally { if (e_3) throw e_3.error; }
435
+ finally { if (e_5) throw e_5.error; }
338
436
  }
339
437
  try {
340
438
  for (var _e = __values(Array.from(pendingChannels.keys())), _f = _e.next(); !_f.done; _f = _e.next()) {
@@ -345,18 +443,20 @@ function rejectAllPendingForTransport(reason) {
345
443
  });
346
444
  }
347
445
  }
348
- catch (e_4_1) { e_4 = { error: e_4_1 }; }
446
+ catch (e_6_1) { e_6 = { error: e_6_1 }; }
349
447
  finally {
350
448
  try {
351
449
  if (_f && !_f.done && (_b = _e.return)) _b.call(_e);
352
450
  }
353
- finally { if (e_4) throw e_4.error; }
451
+ finally { if (e_6) throw e_6.error; }
354
452
  }
355
453
  closeActiveChannelsFromTransport(reason);
356
454
  }
357
455
  function resetHandshakeState(reason, rejectPending) {
358
456
  clearHandshakeTimer();
359
- handshakeSessionId = null;
457
+ if (protocolMode.kind !== "required-v3")
458
+ handshakeSessionId = null;
459
+ helloAcknowledged = false;
360
460
  handshakeDone = false;
361
461
  helloSent = false;
362
462
  handshakeRetryCount = 0;
@@ -417,7 +517,7 @@ function handleAppleDownstreamFrame(frame) {
417
517
  }
418
518
  function runAppleDownstream() {
419
519
  return __awaiter(this, void 0, void 0, function () {
420
- var controller, separator, url, response, reader, decoder, buffered, _a, value, done, tail, e_5;
520
+ var controller, separator, url, response, reader, decoder, buffered, _a, value, done, tail, e_7;
421
521
  return __generator(this, function (_b) {
422
522
  switch (_b.label) {
423
523
  case 0:
@@ -427,7 +527,8 @@ function runAppleDownstream() {
427
527
  controller = new AbortController();
428
528
  appleDownstreamAbortController = controller;
429
529
  separator = APPLE_DOWNSTREAM_URL.includes("?") ? "&" : "?";
430
- url = "".concat(APPLE_DOWNSTREAM_URL).concat(separator, "from=").concat(appleLastFrameSeq);
530
+ url = "".concat(APPLE_DOWNSTREAM_URL).concat(separator, "from=").concat(appleLastFrameSeq, "&doc=").concat(APPLE_DOWNSTREAM_DOCUMENT);
531
+ appleDownstreamAttemptAt = Date.now();
431
532
  return [4 /*yield*/, fetch(url, {
432
533
  method: "GET",
433
534
  cache: "no-store",
@@ -482,10 +583,10 @@ function runAppleDownstream() {
482
583
  }
483
584
  return [3 /*break*/, 8];
484
585
  case 6:
485
- e_5 = _b.sent();
586
+ e_7 = _b.sent();
486
587
  if (controller.signal.aborted)
487
588
  return [2 /*return*/];
488
- throw e_5;
589
+ throw e_7;
489
590
  case 7:
490
591
  try {
491
592
  reader.releaseLock();
@@ -588,8 +689,18 @@ function startAppleWatchdog() {
588
689
  appleWatchdogTimer = setInterval(function () {
589
690
  // No `connected` guard: a reconnect that hung before delivering a frame
590
691
  // also goes stale, and forcing a reconnect aborts and retries it.
591
- if (appleLastFrameAt === 0)
692
+ if (appleLastFrameAt === 0) {
693
+ // The first downstream has produced nothing yet. A fetch whose response
694
+ // never arrives raises no error, and a page that stays in-window (under
695
+ // a splash, say) never sees the visibility handler either.
696
+ if (appleDownstreamTask &&
697
+ appleDownstreamAttemptAt !== 0 &&
698
+ Date.now() - appleDownstreamAttemptAt > APPLE_DOWNSTREAM_CONNECT_TIMEOUT_MS) {
699
+ warn("Apple downstream did not connect, forcing reconnect");
700
+ forceDownstreamReconnect();
701
+ }
592
702
  return;
703
+ }
593
704
  if (Date.now() - appleLastFrameAt > APPLE_DOWNSTREAM_STALE_MS) {
594
705
  warn("Apple downstream stale (no heartbeat), forcing reconnect");
595
706
  forceDownstreamReconnect();
@@ -637,44 +748,133 @@ function getMessagePort() {
637
748
  }
638
749
  return portInitState.promise;
639
750
  }
751
+ function encodeForNative(message) {
752
+ if (protocolMode.kind === "blocked")
753
+ return null;
754
+ if (protocolMode.kind === "v2")
755
+ return message;
756
+ if (!message || typeof message !== "object")
757
+ return null;
758
+ var _a = message, _v = _a.v, kind = _a.kind, payload = __rest(_a, ["v", "kind"]);
759
+ if (typeof kind !== "string")
760
+ return null;
761
+ var encoded = protocolMode.codec.encode(kind, payload);
762
+ return encoded.ok ? encoded.value : null;
763
+ }
640
764
  function postToNative(message) {
641
765
  var _a, _b, _c;
766
+ var encodedMessage = encodeForNative(message);
767
+ if (!encodedMessage)
768
+ return null;
642
769
  var kind = message.kind;
643
770
  if (kind === "req" || kind === "notify")
644
771
  log("postToNative: ".concat(kind, " ").concat(message.method));
645
772
  if (isDebugEnabled("proto"))
646
- console.log("→", JSON.stringify(message, null, 2));
773
+ // Log the payload before RequiredV3 adds its document secret. The bound
774
+ // console wrapper posts what it prints, which would re-enter this function.
775
+ nativeConsole.log("→", JSON.stringify(message, null, 2));
647
776
  try {
777
+ var messageString = stringifyForNative(encodedMessage);
778
+ var bytes = utf8ByteLength(messageString);
779
+ if (bytes > MAX_NATIVE_MESSAGE_BYTES) {
780
+ return {
781
+ code: BRIDGE_ERROR.MESSAGE_TOO_LARGE,
782
+ message: "Bridge message of ".concat(bytes, " bytes exceeds the ").concat(MAX_NATIVE_MESSAGE_BYTES, "-byte native limit"),
783
+ };
784
+ }
648
785
  if (communicationMethod === "webkit") {
649
- (_b = (_a = window.webkit) === null || _a === void 0 ? void 0 : _a.messageHandlers[NATIVE_HANDLER_NAME]) === null || _b === void 0 ? void 0 : _b.postMessage(message);
650
- return;
786
+ (_b = (_a = window.webkit) === null || _a === void 0 ? void 0 : _a.messageHandlers[NATIVE_HANDLER_NAME]) === null || _b === void 0 ? void 0 : _b.postMessage(messageString);
787
+ return null;
651
788
  }
652
- var messageString = stringifyForNative(message);
653
789
  if (communicationMethod === MESSAGE_PORT_TYPE && messagePort) {
654
790
  messagePort.postMessage(messageString);
655
- return;
791
+ return null;
656
792
  }
657
793
  if ((communicationMethod === JS_INTERFACE_TYPE ||
658
794
  communicationMethod === WEB_MESSAGE_TYPE) &&
659
795
  ((_c = window.LingXiaProxy) === null || _c === void 0 ? void 0 : _c.postMessage)) {
660
796
  window.LingXiaProxy.postMessage(messageString);
661
- return;
797
+ return null;
662
798
  }
663
799
  warn("Transport not ready");
664
800
  }
665
801
  catch (e) {
666
- error("Send error:", e);
802
+ if (protocolMode.kind === "required-v3")
803
+ error("Send error");
804
+ else
805
+ error("Send error:", e);
806
+ }
807
+ return null;
808
+ }
809
+ function formatBoundConsoleMessage(args) {
810
+ var message = args
811
+ .map(function (arg) {
812
+ if (typeof arg === "string")
813
+ return arg;
814
+ try {
815
+ var encoded = stringifyForNative(arg);
816
+ return typeof encoded === "string" ? encoded : String(arg);
817
+ }
818
+ catch (_a) {
819
+ return "[Unserializable]";
820
+ }
821
+ })
822
+ .join(" ");
823
+ return message.length <= BOUND_CONSOLE_MESSAGE_CHARS
824
+ ? message
825
+ : "".concat(message.slice(0, BOUND_CONSOLE_MESSAGE_CHARS), "\u2026");
826
+ }
827
+ /**
828
+ * BrowserControl console is an authenticated auxiliary V3 frame. Platform
829
+ * console hooks are disabled for BrowserRelaxed WebViews, so this is the only
830
+ * path that can reach the native log delegate for a control document.
831
+ */
832
+ function installBoundV3Console() {
833
+ var e_8, _a;
834
+ if (protocolMode.kind !== "required-v3")
835
+ return;
836
+ var methods = ["log", "error", "warn", "info", "debug"];
837
+ var _loop_1 = function (level) {
838
+ var original = nativeConsole[level];
839
+ console[level] = function () {
840
+ var args = [];
841
+ for (var _i = 0; _i < arguments.length; _i++) {
842
+ args[_i] = arguments[_i];
843
+ }
844
+ original.apply(void 0, __spreadArray([], __read(args), false));
845
+ // Native repeats Active session, WebMessageContext, generation and rate
846
+ // checks. Avoid sending pre-handshake output that can never be admitted.
847
+ if (!handshakeDone)
848
+ return;
849
+ postToNative({
850
+ kind: "console",
851
+ __lingxia_console__: true,
852
+ level: level,
853
+ message: formatBoundConsoleMessage(args),
854
+ });
855
+ };
856
+ };
857
+ try {
858
+ for (var methods_1 = __values(methods), methods_1_1 = methods_1.next(); !methods_1_1.done; methods_1_1 = methods_1.next()) {
859
+ var level = methods_1_1.value;
860
+ _loop_1(level);
861
+ }
862
+ }
863
+ catch (e_8_1) { e_8 = { error: e_8_1 }; }
864
+ finally {
865
+ try {
866
+ if (methods_1_1 && !methods_1_1.done && (_a = methods_1.return)) _a.call(methods_1);
867
+ }
868
+ finally { if (e_8) throw e_8.error; }
667
869
  }
668
870
  }
669
871
  // Handshake state
670
872
  var handshakeSessionId = null;
873
+ var helloAcknowledged = false;
671
874
  var handshakeDone = false;
672
875
  var helloSent = false;
673
876
  var handshakeRetryCount = 0;
674
877
  var handshakeTimer = null;
675
- // Host method schema — populated from handshake `Ready` message.
676
- // Maps "namespace.method" → "call" | "stream".
677
- var hostMethodKinds = {};
678
878
  // Request tracking
679
879
  var requestCounter = 0;
680
880
  function createListenerBuckets() {
@@ -738,7 +938,7 @@ function createStreamHandle(id, cancelFn) {
738
938
  cancelFn();
739
939
  },
740
940
  _a._emitData = function (payload) {
741
- var e_6, _a;
941
+ var e_9, _a;
742
942
  if (done)
743
943
  return;
744
944
  if (pendingReads.length > 0) {
@@ -758,16 +958,16 @@ function createStreamHandle(id, cancelFn) {
758
958
  }
759
959
  }
760
960
  }
761
- catch (e_6_1) { e_6 = { error: e_6_1 }; }
961
+ catch (e_9_1) { e_9 = { error: e_9_1 }; }
762
962
  finally {
763
963
  try {
764
964
  if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
765
965
  }
766
- finally { if (e_6) throw e_6.error; }
966
+ finally { if (e_9) throw e_9.error; }
767
967
  }
768
968
  },
769
969
  _a._resolve = function (resultValue) {
770
- var e_7, _a;
970
+ var e_10, _a;
771
971
  if (done)
772
972
  return;
773
973
  done = true;
@@ -786,16 +986,16 @@ function createStreamHandle(id, cancelFn) {
786
986
  }
787
987
  }
788
988
  }
789
- catch (e_7_1) { e_7 = { error: e_7_1 }; }
989
+ catch (e_10_1) { e_10 = { error: e_10_1 }; }
790
990
  finally {
791
991
  try {
792
992
  if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
793
993
  }
794
- finally { if (e_7) throw e_7.error; }
994
+ finally { if (e_10) throw e_10.error; }
795
995
  }
796
996
  },
797
997
  _a._reject = function (err) {
798
- var e_8, _a;
998
+ var e_11, _a;
799
999
  if (done)
800
1000
  return;
801
1001
  done = true;
@@ -814,12 +1014,12 @@ function createStreamHandle(id, cancelFn) {
814
1014
  }
815
1015
  }
816
1016
  }
817
- catch (e_8_1) { e_8 = { error: e_8_1 }; }
1017
+ catch (e_11_1) { e_11 = { error: e_11_1 }; }
818
1018
  finally {
819
1019
  try {
820
1020
  if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
821
1021
  }
822
- finally { if (e_8) throw e_8.error; }
1022
+ finally { if (e_11) throw e_11.error; }
823
1023
  }
824
1024
  },
825
1025
  _a);
@@ -849,7 +1049,7 @@ function createChannel(id, sendFn, closeFn) {
849
1049
  sendFn(payload, outboundSeq++);
850
1050
  },
851
1051
  on: function (event, listener) {
852
- var e_9, _a;
1052
+ var e_12, _a;
853
1053
  if (event === "data") {
854
1054
  listeners.data.add(listener);
855
1055
  // Flush data buffered before this listener attached. The host can send
@@ -869,12 +1069,12 @@ function createChannel(id, sendFn, closeFn) {
869
1069
  }
870
1070
  }
871
1071
  }
872
- catch (e_9_1) { e_9 = { error: e_9_1 }; }
1072
+ catch (e_12_1) { e_12 = { error: e_12_1 }; }
873
1073
  finally {
874
1074
  try {
875
1075
  if (buffered_1_1 && !buffered_1_1.done && (_a = buffered_1.return)) _a.call(buffered_1);
876
1076
  }
877
- finally { if (e_9) throw e_9.error; }
1077
+ finally { if (e_12) throw e_12.error; }
878
1078
  }
879
1079
  }
880
1080
  }
@@ -913,7 +1113,7 @@ function createChannel(id, sendFn, closeFn) {
913
1113
  channel._emitClose(code, reason);
914
1114
  },
915
1115
  _a._emitData = function (payload) {
916
- var e_10, _a;
1116
+ var e_13, _a;
917
1117
  if (!open)
918
1118
  return;
919
1119
  if (pendingReads.length > 0) {
@@ -933,16 +1133,16 @@ function createChannel(id, sendFn, closeFn) {
933
1133
  }
934
1134
  }
935
1135
  }
936
- catch (e_10_1) { e_10 = { error: e_10_1 }; }
1136
+ catch (e_13_1) { e_13 = { error: e_13_1 }; }
937
1137
  finally {
938
1138
  try {
939
1139
  if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
940
1140
  }
941
- finally { if (e_10) throw e_10.error; }
1141
+ finally { if (e_13) throw e_13.error; }
942
1142
  }
943
1143
  },
944
1144
  _a._emitClose = function (code, reason) {
945
- var e_11, _a;
1145
+ var e_14, _a;
946
1146
  closed = true;
947
1147
  while (pendingReads.length > 0) {
948
1148
  pendingReads.shift().resolve({ done: true, value: undefined });
@@ -958,16 +1158,16 @@ function createChannel(id, sendFn, closeFn) {
958
1158
  }
959
1159
  }
960
1160
  }
961
- catch (e_11_1) { e_11 = { error: e_11_1 }; }
1161
+ catch (e_14_1) { e_14 = { error: e_14_1 }; }
962
1162
  finally {
963
1163
  try {
964
1164
  if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
965
1165
  }
966
- finally { if (e_11) throw e_11.error; }
1166
+ finally { if (e_14) throw e_14.error; }
967
1167
  }
968
1168
  },
969
1169
  _a._reject = function (err) {
970
- var e_12, _a;
1170
+ var e_15, _a;
971
1171
  closed = true;
972
1172
  while (pendingReads.length > 0) {
973
1173
  pendingReads.shift().reject(err);
@@ -983,12 +1183,12 @@ function createChannel(id, sendFn, closeFn) {
983
1183
  }
984
1184
  }
985
1185
  }
986
- catch (e_12_1) { e_12 = { error: e_12_1 }; }
1186
+ catch (e_15_1) { e_15 = { error: e_15_1 }; }
987
1187
  finally {
988
1188
  try {
989
1189
  if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
990
1190
  }
991
- finally { if (e_12) throw e_12.error; }
1191
+ finally { if (e_15) throw e_15.error; }
992
1192
  }
993
1193
  },
994
1194
  _a._markOpen = function () {
@@ -1138,6 +1338,26 @@ function removeOutboxByReqId(reqId) {
1138
1338
  }
1139
1339
  return false;
1140
1340
  }
1341
+ // UTF-8 byte length without allocating a copy of the string.
1342
+ function utf8ByteLength(value) {
1343
+ if (typeof TextEncoder === "function")
1344
+ return new TextEncoder().encode(value).length;
1345
+ var bytes = 0;
1346
+ for (var i = 0; i < value.length; i += 1) {
1347
+ var code = value.charCodeAt(i);
1348
+ if (code < 0x80)
1349
+ bytes += 1;
1350
+ else if (code < 0x800)
1351
+ bytes += 2;
1352
+ else if (code >= 0xd800 && code <= 0xdbff) {
1353
+ bytes += 4;
1354
+ i += 1;
1355
+ }
1356
+ else
1357
+ bytes += 3;
1358
+ }
1359
+ return bytes;
1360
+ }
1141
1361
  function send(msg, reqId) {
1142
1362
  var kind = msg.kind;
1143
1363
  var isHandshake = kind === "hello" || kind === "helloAck" || kind === "ready";
@@ -1157,7 +1377,16 @@ function send(msg, reqId) {
1157
1377
  }
1158
1378
  if (reqId)
1159
1379
  armPendingOperationTimer(reqId);
1160
- postToNative(msg);
1380
+ reportPostFailure(postToNative(msg), reqId);
1381
+ }
1382
+ // An oversized frame is dropped by native without a reply, so the caller would
1383
+ // otherwise learn about it only when its request timed out.
1384
+ function reportPostFailure(failure, reqId) {
1385
+ if (!failure)
1386
+ return;
1387
+ error(failure.message);
1388
+ if (reqId)
1389
+ rejectPendingOperation(reqId, failure);
1161
1390
  }
1162
1391
  function flushOutbox() {
1163
1392
  if (!canSendAppMessages())
@@ -1168,7 +1397,7 @@ function flushOutbox() {
1168
1397
  continue;
1169
1398
  if (item.reqId)
1170
1399
  armPendingOperationTimer(item.reqId);
1171
- postToNative(item.msg);
1400
+ reportPostFailure(postToNative(item.msg), item.reqId);
1172
1401
  }
1173
1402
  }
1174
1403
  function clearHandshakeTimer() {
@@ -1180,6 +1409,10 @@ function clearHandshakeTimer() {
1180
1409
  function startHandshake() {
1181
1410
  if (handshakeDone)
1182
1411
  return;
1412
+ if (!protocolInitialized)
1413
+ return;
1414
+ if (protocolMode.kind === "blocked")
1415
+ return;
1183
1416
  if (!isTransportReady())
1184
1417
  return;
1185
1418
  clearHandshakeTimer();
@@ -1188,12 +1421,12 @@ function startHandshake() {
1188
1421
  kind: "hello",
1189
1422
  nonce: BRIDGE_CONFIG.nonce || "",
1190
1423
  role: "view",
1191
- protocolsSupported: [2],
1424
+ protocolsSupported: protocolMode.kind === "required-v3" ? [3] : [2],
1192
1425
  };
1193
1426
  helloSent = true;
1194
1427
  postToNative(hello);
1195
1428
  handshakeTimer = setTimeout(function () {
1196
- var e_13, _a;
1429
+ var e_16, _a;
1197
1430
  if (handshakeDone)
1198
1431
  return;
1199
1432
  handshakeRetryCount++;
@@ -1225,17 +1458,34 @@ function startHandshake() {
1225
1458
  });
1226
1459
  }
1227
1460
  }
1228
- catch (e_13_1) { e_13 = { error: e_13_1 }; }
1461
+ catch (e_16_1) { e_16 = { error: e_16_1 }; }
1229
1462
  finally {
1230
1463
  try {
1231
1464
  if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
1232
1465
  }
1233
- finally { if (e_13) throw e_13.error; }
1466
+ finally { if (e_16) throw e_16.error; }
1234
1467
  }
1235
1468
  }
1236
1469
  }, HANDSHAKE_TIMEOUT_MS);
1237
1470
  }
1238
1471
  function parseIncoming(msg) {
1472
+ if (protocolMode.kind === "blocked")
1473
+ return null;
1474
+ if (protocolMode.kind === "required-v3") {
1475
+ var frame = typeof msg === "string" ? msg : stringifyForNative(msg);
1476
+ var parsed = protocolMode.codec.parse(frame);
1477
+ if (!parsed.ok)
1478
+ return null;
1479
+ return __assign({ v: 3, kind: parsed.value.kind }, parsed.value.payload);
1480
+ }
1481
+ if (typeof msg === "string") {
1482
+ try {
1483
+ msg = JSON.parse(msg);
1484
+ }
1485
+ catch (_a) {
1486
+ return null;
1487
+ }
1488
+ }
1239
1489
  if (!msg || typeof msg !== "object")
1240
1490
  return null;
1241
1491
  var v = msg.v;
@@ -1281,7 +1531,7 @@ function getContainerAndKey(root, pointer, autoCreate) {
1281
1531
  return { container: current, key: segments[segments.length - 1] };
1282
1532
  }
1283
1533
  function applyJsonPatch(target, ops) {
1284
- var e_14, _a, e_15, _b;
1534
+ var e_17, _a, e_18, _b;
1285
1535
  try {
1286
1536
  for (var ops_1 = __values(ops), ops_1_1 = ops_1.next(); !ops_1_1.done; ops_1_1 = ops_1.next()) {
1287
1537
  var op = ops_1_1.value;
@@ -1289,17 +1539,17 @@ function applyJsonPatch(target, ops) {
1289
1539
  var _c = getContainerAndKey(target, op.path, autoCreate), container = _c.container, key = _c.key;
1290
1540
  if (key === "$root") {
1291
1541
  try {
1292
- for (var _d = (e_15 = void 0, __values(Object.keys(target))), _e = _d.next(); !_e.done; _e = _d.next()) {
1542
+ for (var _d = (e_18 = void 0, __values(Object.keys(target))), _e = _d.next(); !_e.done; _e = _d.next()) {
1293
1543
  var k = _e.value;
1294
1544
  delete target[k];
1295
1545
  }
1296
1546
  }
1297
- catch (e_15_1) { e_15 = { error: e_15_1 }; }
1547
+ catch (e_18_1) { e_18 = { error: e_18_1 }; }
1298
1548
  finally {
1299
1549
  try {
1300
1550
  if (_e && !_e.done && (_b = _d.return)) _b.call(_d);
1301
1551
  }
1302
- finally { if (e_15) throw e_15.error; }
1552
+ finally { if (e_18) throw e_18.error; }
1303
1553
  }
1304
1554
  if (op.op !== "remove") {
1305
1555
  var v = op.value;
@@ -1329,12 +1579,12 @@ function applyJsonPatch(target, ops) {
1329
1579
  obj[key] = op.value;
1330
1580
  }
1331
1581
  }
1332
- catch (e_14_1) { e_14 = { error: e_14_1 }; }
1582
+ catch (e_17_1) { e_17 = { error: e_17_1 }; }
1333
1583
  finally {
1334
1584
  try {
1335
1585
  if (ops_1_1 && !ops_1_1.done && (_a = ops_1.return)) _a.call(ops_1);
1336
1586
  }
1337
- finally { if (e_14) throw e_14.error; }
1587
+ finally { if (e_17) throw e_17.error; }
1338
1588
  }
1339
1589
  }
1340
1590
  function notifyStateSubscribers(initial) {
@@ -1384,19 +1634,27 @@ function applySnapshotFromResult(result) {
1384
1634
  pageData = obj.state;
1385
1635
  stateRev = obj.rev;
1386
1636
  if (isDebugEnabled("data")) {
1387
- console.group("[LX] snapshot(res)");
1388
- console.log("rev:", stateRev, "state:", deepCopy(pageData));
1389
- console.groupEnd();
1637
+ nativeConsole.group("[LX] snapshot(res)");
1638
+ nativeConsole.log("rev:", stateRev, "state:", deepCopy(pageData));
1639
+ nativeConsole.groupEnd();
1390
1640
  }
1391
1641
  notifyStateSubscribers(true);
1392
1642
  return true;
1393
1643
  }
1394
1644
  function handleIncomingMessage(msg) {
1395
- var e_16, _a;
1396
- var _b, _c, _d, _e;
1645
+ var _a, _b, _c, _d;
1646
+ var candidate = msg;
1647
+ if (protocolMode.kind === "v2" && typeof candidate === "string") {
1648
+ try {
1649
+ candidate = JSON.parse(candidate);
1650
+ }
1651
+ catch (_e) {
1652
+ candidate = null;
1653
+ }
1654
+ }
1397
1655
  // Handle native component events (from Android NativeBridge.sendEventToView)
1398
- if (msg && typeof msg === "object") {
1399
- var obj = msg;
1656
+ if (protocolMode.kind === "v2" && candidate && typeof candidate === "object") {
1657
+ var obj = candidate;
1400
1658
  if (obj.type === "event" && obj.name === "nativecomponent" && obj.payload) {
1401
1659
  var payload = obj.payload;
1402
1660
  var componentId = payload.id ||
@@ -1415,40 +1673,46 @@ function handleIncomingMessage(msg) {
1415
1673
  return;
1416
1674
  }
1417
1675
  }
1418
- var message = parseIncoming(msg);
1676
+ var message = parseIncoming(candidate);
1419
1677
  if (!message) {
1420
- warn("Invalid V2 message:", msg);
1678
+ warn("Invalid bridge message");
1421
1679
  return;
1422
1680
  }
1423
1681
  switch (message.kind) {
1424
1682
  case "helloAck":
1425
- handshakeSessionId = message.sessionId;
1683
+ if (message.protocol !== (protocolMode.kind === "required-v3" ? 3 : 2) ||
1684
+ message.nonce !== (BRIDGE_CONFIG.nonce || "")) {
1685
+ warn("Invalid helloAck");
1686
+ return;
1687
+ }
1688
+ if (protocolMode.kind === "required-v3") {
1689
+ helloAcknowledged = true;
1690
+ return;
1691
+ }
1692
+ if (handshakeSessionId && message.sessionId !== handshakeSessionId) {
1693
+ warn("sessionId mismatch");
1694
+ return;
1695
+ }
1696
+ if (!handshakeSessionId)
1697
+ handshakeSessionId = message.sessionId;
1698
+ helloAcknowledged = true;
1426
1699
  return;
1427
1700
  case "ready":
1428
- if (handshakeSessionId && message.sessionId !== handshakeSessionId) {
1701
+ if (!helloAcknowledged)
1702
+ return;
1703
+ if (protocolMode.kind === "v2" &&
1704
+ handshakeSessionId &&
1705
+ message.sessionId !== handshakeSessionId) {
1429
1706
  warn("sessionId mismatch");
1430
1707
  return;
1431
1708
  }
1432
1709
  clearHandshakeTimer();
1433
1710
  handshakeDone = true;
1434
1711
  handshakeRetryCount = 0;
1435
- if (message.hostMethods) {
1436
- try {
1437
- for (var _f = __values(Object.entries(message.hostMethods)), _g = _f.next(); !_g.done; _g = _f.next()) {
1438
- var _h = __read(_g.value, 2), k = _h[0], v = _h[1];
1439
- hostMethodKinds[k] = v;
1440
- }
1441
- }
1442
- catch (e_16_1) { e_16 = { error: e_16_1 }; }
1443
- finally {
1444
- try {
1445
- if (_g && !_g.done && (_a = _f.return)) _a.call(_f);
1446
- }
1447
- finally { if (e_16) throw e_16.error; }
1448
- }
1712
+ if (isDebugEnabled("proto")) {
1713
+ log("Handshake complete, host routes:", Object.keys(message.hostMethods || {}).length +
1714
+ (message.hostChannels || []).length);
1449
1715
  }
1450
- if (isDebugEnabled("proto"))
1451
- log("Handshake complete, hostMethods:", Object.keys(hostMethodKinds).length);
1452
1716
  flushOutbox();
1453
1717
  return;
1454
1718
  case "res": {
@@ -1468,7 +1732,7 @@ function handleIncomingMessage(msg) {
1468
1732
  info.stream._resolve(message.result);
1469
1733
  return;
1470
1734
  }
1471
- (_b = info.resolve) === null || _b === void 0 ? void 0 : _b.call(info, message.result);
1735
+ (_a = info.resolve) === null || _a === void 0 ? void 0 : _a.call(info, message.result);
1472
1736
  }
1473
1737
  else {
1474
1738
  var err = toBridgeError(message.error);
@@ -1476,7 +1740,7 @@ function handleIncomingMessage(msg) {
1476
1740
  info.stream._reject(err);
1477
1741
  return;
1478
1742
  }
1479
- (_c = info.reject) === null || _c === void 0 ? void 0 : _c.call(info, err);
1743
+ (_b = info.reject) === null || _b === void 0 ? void 0 : _b.call(info, err);
1480
1744
  }
1481
1745
  return;
1482
1746
  }
@@ -1506,9 +1770,9 @@ function handleIncomingMessage(msg) {
1506
1770
  pageData = message.state || {};
1507
1771
  stateRev = message.rev;
1508
1772
  if (isDebugEnabled("data")) {
1509
- console.group("[LX] snapshot");
1510
- console.log("rev:", stateRev, "state:", deepCopy(pageData));
1511
- console.groupEnd();
1773
+ nativeConsole.group("[LX] snapshot");
1774
+ nativeConsole.log("rev:", stateRev, "state:", deepCopy(pageData));
1775
+ nativeConsole.groupEnd();
1512
1776
  }
1513
1777
  notifyStateSubscribers(true);
1514
1778
  return;
@@ -1528,9 +1792,9 @@ function handleIncomingMessage(msg) {
1528
1792
  return;
1529
1793
  }
1530
1794
  if (isDebugEnabled("data")) {
1531
- console.group("[LX] patch");
1532
- console.log("rev:", stateRev, "ops:", message.ops);
1533
- console.groupEnd();
1795
+ nativeConsole.group("[LX] patch");
1796
+ nativeConsole.log("rev:", stateRev, "ops:", message.ops);
1797
+ nativeConsole.groupEnd();
1534
1798
  }
1535
1799
  notifyStateSubscribers(false);
1536
1800
  if (message.ack)
@@ -1571,8 +1835,8 @@ function handleIncomingMessage(msg) {
1571
1835
  var pendingChannel = pendingChannels.get(message.id);
1572
1836
  if (pendingChannel) {
1573
1837
  rejectPendingChannel(message.id, {
1574
- code: (_d = message.code) !== null && _d !== void 0 ? _d : BRIDGE_ERROR.STREAM_CLOSED,
1575
- message: (_e = message.reason) !== null && _e !== void 0 ? _e : "Channel closed before opening",
1838
+ code: (_c = message.code) !== null && _c !== void 0 ? _c : BRIDGE_ERROR.STREAM_CLOSED,
1839
+ message: (_d = message.reason) !== null && _d !== void 0 ? _d : "Channel closed before opening",
1576
1840
  });
1577
1841
  return;
1578
1842
  }
@@ -1636,14 +1900,15 @@ var nativeComponentHandlers = new Map();
1636
1900
  var nativeComponentQueue = [];
1637
1901
  var nativeComponentReady = false;
1638
1902
  function hasNativeComponentHandler() {
1639
- var _a, _b, _c;
1903
+ var _a, _b, _c, _d;
1640
1904
  if (typeof window === "undefined")
1641
1905
  return false;
1642
1906
  return !!(((_b = (_a = window.webkit) === null || _a === void 0 ? void 0 : _a.messageHandlers) === null || _b === void 0 ? void 0 : _b.NativeComponent) ||
1643
- ((_c = window.NativeComponentBridge) === null || _c === void 0 ? void 0 : _c.postMessage));
1907
+ ((_c = window.NativeComponentBridge) === null || _c === void 0 ? void 0 : _c.postMessage) ||
1908
+ ((_d = window.LingXiaProxy) === null || _d === void 0 ? void 0 : _d.nativeComponentUpdate));
1644
1909
  }
1645
1910
  function postNativeComponentMessage(message) {
1646
- var _a, _b, _c;
1911
+ var _a, _b, _c, _d;
1647
1912
  try {
1648
1913
  if ((_b = (_a = window.webkit) === null || _a === void 0 ? void 0 : _a.messageHandlers) === null || _b === void 0 ? void 0 : _b.NativeComponent) {
1649
1914
  window.webkit.messageHandlers.NativeComponent.postMessage(message);
@@ -1653,6 +1918,18 @@ function postNativeComponentMessage(message) {
1653
1918
  window.NativeComponentBridge.postMessage(stringifyForNative(message));
1654
1919
  return;
1655
1920
  }
1921
+ var update = (_d = window.LingXiaProxy) === null || _d === void 0 ? void 0 : _d.nativeComponentUpdate;
1922
+ if (typeof update === "function") {
1923
+ var componentId = message.id || "island";
1924
+ var payload = stringifyForNative(__assign(__assign({}, message), { componentId: componentId }));
1925
+ try {
1926
+ update.call(window.LingXiaProxy, payload);
1927
+ }
1928
+ catch (_e) {
1929
+ update.call(window.LingXiaProxy, componentId, payload);
1930
+ }
1931
+ return;
1932
+ }
1656
1933
  }
1657
1934
  catch (e) {
1658
1935
  error("NativeComponent send error:", e);
@@ -1888,16 +2165,7 @@ export var LingXiaBridge = {
1888
2165
  }
1889
2166
  messagePort = port;
1890
2167
  port.onmessage = function (event) {
1891
- var data = event.data;
1892
- if (typeof data === "string") {
1893
- try {
1894
- data = JSON.parse(data);
1895
- }
1896
- catch (_a) {
1897
- return;
1898
- }
1899
- }
1900
- handleIncomingMessage(data);
2168
+ handleIncomingMessage(event.data);
1901
2169
  };
1902
2170
  // Some WebView MessagePort implementations (notably Android WebMessagePort)
1903
2171
  // require an explicit start() to begin dispatching onmessage events.
@@ -1909,15 +2177,16 @@ export var LingXiaBridge = {
1909
2177
  startHandshake();
1910
2178
  },
1911
2179
  _receiveEvaluateMessage: function (messageString) {
1912
- try {
1913
- if (messageString)
1914
- handleIncomingMessage(JSON.parse(messageString));
1915
- }
1916
- catch (e) {
1917
- error("Parse error:", e);
1918
- }
2180
+ if (messageString)
2181
+ handleIncomingMessage(messageString);
1919
2182
  },
1920
2183
  debug: createDebugObject(debugFlags),
2184
+ // The host language, for documents that cannot bundle `@lingxia/bridge` —
2185
+ // browser internal pages and plain-HTML lxapps load this runtime as a script.
2186
+ displayLanguage: {
2187
+ get: getDisplayLanguage,
2188
+ subscribe: subscribeDisplayLanguage,
2189
+ },
1921
2190
  platform: {
1922
2191
  isHarmony: isHarmony,
1923
2192
  isIOS: isIOS,
@@ -1985,23 +2254,23 @@ function wrapNativeStream(handle) {
1985
2254
  var eventListeners = new Set();
1986
2255
  var errorListeners = new Set();
1987
2256
  handle.on("data", function (event) {
1988
- var e_17, _a;
2257
+ var e_19, _a;
1989
2258
  try {
1990
2259
  for (var eventListeners_1 = __values(eventListeners), eventListeners_1_1 = eventListeners_1.next(); !eventListeners_1_1.done; eventListeners_1_1 = eventListeners_1.next()) {
1991
2260
  var listener = eventListeners_1_1.value;
1992
2261
  listener(event);
1993
2262
  }
1994
2263
  }
1995
- catch (e_17_1) { e_17 = { error: e_17_1 }; }
2264
+ catch (e_19_1) { e_19 = { error: e_19_1 }; }
1996
2265
  finally {
1997
2266
  try {
1998
2267
  if (eventListeners_1_1 && !eventListeners_1_1.done && (_a = eventListeners_1.return)) _a.call(eventListeners_1);
1999
2268
  }
2000
- finally { if (e_17) throw e_17.error; }
2269
+ finally { if (e_19) throw e_19.error; }
2001
2270
  }
2002
2271
  });
2003
2272
  handle.on("error", function (error) {
2004
- var e_18, _a;
2273
+ var e_20, _a;
2005
2274
  var nativeError = toNativeError(error);
2006
2275
  try {
2007
2276
  for (var errorListeners_1 = __values(errorListeners), errorListeners_1_1 = errorListeners_1.next(); !errorListeners_1_1.done; errorListeners_1_1 = errorListeners_1.next()) {
@@ -2009,12 +2278,12 @@ function wrapNativeStream(handle) {
2009
2278
  listener(nativeError);
2010
2279
  }
2011
2280
  }
2012
- catch (e_18_1) { e_18 = { error: e_18_1 }; }
2281
+ catch (e_20_1) { e_20 = { error: e_20_1 }; }
2013
2282
  finally {
2014
2283
  try {
2015
2284
  if (errorListeners_1_1 && !errorListeners_1_1.done && (_a = errorListeners_1.return)) _a.call(errorListeners_1);
2016
2285
  }
2017
- finally { if (e_18) throw e_18.error; }
2286
+ finally { if (e_20) throw e_20.error; }
2018
2287
  }
2019
2288
  });
2020
2289
  return {
@@ -2040,23 +2309,23 @@ function wrapNativeChannel(channel) {
2040
2309
  var messageListeners = new Set();
2041
2310
  var closeListeners = new Set();
2042
2311
  channel.on("data", function (message) {
2043
- var e_19, _a;
2312
+ var e_21, _a;
2044
2313
  try {
2045
2314
  for (var messageListeners_1 = __values(messageListeners), messageListeners_1_1 = messageListeners_1.next(); !messageListeners_1_1.done; messageListeners_1_1 = messageListeners_1.next()) {
2046
2315
  var listener = messageListeners_1_1.value;
2047
2316
  listener(message);
2048
2317
  }
2049
2318
  }
2050
- catch (e_19_1) { e_19 = { error: e_19_1 }; }
2319
+ catch (e_21_1) { e_21 = { error: e_21_1 }; }
2051
2320
  finally {
2052
2321
  try {
2053
2322
  if (messageListeners_1_1 && !messageListeners_1_1.done && (_a = messageListeners_1.return)) _a.call(messageListeners_1);
2054
2323
  }
2055
- finally { if (e_19) throw e_19.error; }
2324
+ finally { if (e_21) throw e_21.error; }
2056
2325
  }
2057
2326
  });
2058
2327
  channel.on("close", function (code, reason) {
2059
- var e_20, _a;
2328
+ var e_22, _a;
2060
2329
  var event = { code: code, reason: reason };
2061
2330
  try {
2062
2331
  for (var closeListeners_1 = __values(closeListeners), closeListeners_1_1 = closeListeners_1.next(); !closeListeners_1_1.done; closeListeners_1_1 = closeListeners_1.next()) {
@@ -2064,12 +2333,12 @@ function wrapNativeChannel(channel) {
2064
2333
  listener(event);
2065
2334
  }
2066
2335
  }
2067
- catch (e_20_1) { e_20 = { error: e_20_1 }; }
2336
+ catch (e_22_1) { e_22 = { error: e_22_1 }; }
2068
2337
  finally {
2069
2338
  try {
2070
2339
  if (closeListeners_1_1 && !closeListeners_1_1.done && (_a = closeListeners_1.return)) _a.call(closeListeners_1);
2071
2340
  }
2072
- finally { if (e_20) throw e_20.error; }
2341
+ finally { if (e_22) throw e_22.error; }
2073
2342
  }
2074
2343
  });
2075
2344
  return {
@@ -2112,6 +2381,15 @@ export function initBridge() {
2112
2381
  }
2113
2382
  window.__LX_BRIDGE_INIT_STATE = "initializing";
2114
2383
  try {
2384
+ if (!protocolInitialized) {
2385
+ var activation = consumeV3Bootstrap();
2386
+ protocolMode =
2387
+ activation.kind === "required"
2388
+ ? { kind: "required-v3", codec: activation.codec }
2389
+ : { kind: activation.kind === "absent" ? "v2" : "blocked" };
2390
+ protocolInitialized = true;
2391
+ }
2392
+ installBoundV3Console();
2115
2393
  log("Method: ".concat(communicationMethod));
2116
2394
  activateReceiver(LingXiaBridge._receiveEvaluateMessage);
2117
2395
  if (useAppleDownstreamTransport()) {
@@ -2121,7 +2399,15 @@ export function initBridge() {
2121
2399
  }
2122
2400
  else if (communicationMethod === MESSAGE_PORT_TYPE) {
2123
2401
  installMessagePortInitListener();
2124
- getMessagePort().catch(function (e) { return warn("Port init failed:", e); });
2402
+ var tryPort_1 = function (attempt) {
2403
+ getMessagePort().catch(function (e) {
2404
+ warn("Port init failed:", e);
2405
+ if (attempt < 3) {
2406
+ window.setTimeout(function () { return tryPort_1(attempt + 1); }, 500 * attempt);
2407
+ }
2408
+ });
2409
+ };
2410
+ tryPort_1(1);
2125
2411
  }
2126
2412
  else if (communicationMethod === "webkit" ||
2127
2413
  communicationMethod === JS_INTERFACE_TYPE ||