@heybox/hb-sdk 0.7.4-alpha.4 → 0.7.4-alpha.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. package/CHANGELOG.md +18 -0
  2. package/README.md +19 -1
  3. package/dist/cli-chunks/{build-BqGFRLe-.cjs → build-DF74IHGR.cjs} +2 -2
  4. package/dist/cli-chunks/{context-DTr3hRTv.cjs → context-QemmshdY.cjs} +26 -1
  5. package/dist/cli-chunks/{create-OkiiiH4I.cjs → create-Ch9m8w8M.cjs} +1 -1
  6. package/dist/cli-chunks/dev-D2DwLxXW.cjs +15682 -0
  7. package/dist/cli-chunks/{doctor-BO0vkDny.cjs → doctor-B9xJ2j20.cjs} +1 -1
  8. package/dist/cli-chunks/{index-CZuvk1Rg.cjs → index-Cv36miWx.cjs} +83 -83
  9. package/dist/cli-chunks/{index-CyEqCbeu.cjs → index-DxQgKWRT.cjs} +31 -32
  10. package/dist/cli-chunks/index.esm-DZQrllrR.cjs +5212 -0
  11. package/dist/cli-chunks/{login-CuAGltGs.cjs → login-DEKhX1Vf.cjs} +2 -2
  12. package/dist/cli-chunks/{project-vite-BUY9DgYo.cjs → project-vite-D_NNB_Ib.cjs} +1 -1
  13. package/dist/cli-chunks/{remote-tPMcxgpi.cjs → remote-B_Bn4v81.cjs} +4 -4
  14. package/dist/cli-chunks/{session-BUjFX2U2.cjs → session-C2zY7XHn.cjs} +2 -1
  15. package/dist/cli.cjs +1 -1
  16. package/dist/devtools/browser-dev-host/index.html +128 -151
  17. package/dist/devtools/browser-dev-host/main.js +2591 -345
  18. package/dist/index.cjs.js +1187 -3
  19. package/dist/index.esm.js +1187 -3
  20. package/dist/protocol.cjs.js +3 -0
  21. package/dist/protocol.esm.js +3 -1
  22. package/dist/vite.cjs.js +1 -1
  23. package/dist/vite.esm.js +1 -1
  24. package/package.json +16 -8
  25. package/skill/SKILL.md +7 -6
  26. package/skill/references/api-protocol.md +6 -3
  27. package/skill/references/api-root.md +6 -2
  28. package/skill/references/cli.md +16 -20
  29. package/skill/references/examples.md +2 -2
  30. package/skill/references/recipes.md +37 -0
  31. package/skill/references/safety-boundaries.md +1 -1
  32. package/skill/references/smoke-evaluation.md +1 -1
  33. package/skill/scripts/sync-references.mjs +6 -6
  34. package/skill/skill.json +4 -4
  35. package/types/index.d.ts +1 -1
  36. package/types/modules/share/copy-link.d.ts +16 -0
  37. package/types/modules/share/extra.d.ts +3 -0
  38. package/types/modules/share/index.d.ts +8 -2
  39. package/types/modules/share/types.d.ts +11 -0
  40. package/types/protocol/capabilities.d.ts +1 -1
  41. package/types/protocol/dev-session.d.ts +13 -0
  42. package/types/protocol.d.ts +2 -2
  43. package/dist/cli-chunks/dev-j7W9R7ew.cjs +0 -2769
package/dist/index.esm.js CHANGED
@@ -564,6 +564,7 @@ const AUTH_LOGIN_METHOD = 'auth.login';
564
564
  const USER_GET_INFO_METHOD = 'user.getInfo';
565
565
  const USER_REVOKE_AUTHORIZATION_METHOD = 'user.revokeAuthorization';
566
566
  const USER_GET_STEAM_GAME_LIST_METHOD = 'user.getSteamGameList';
567
+ const SHARE_COPY_LINK_METHOD = 'share.copyLink';
567
568
  const SHARE_SHOW_SHARE_MENU_METHOD = 'share.showShareMenu';
568
569
  const SHARE_SCREENSHOT_METHOD = 'share.screenshot';
569
570
  const VIEWPORT_GET_WINDOW_INFO_METHOD = 'viewport.getWindowInfo';
@@ -667,7 +668,7 @@ function createMessageId() {
667
668
  /** 构建时替换为当前发布包的实际版本。 */
668
669
  const HB_SDK_VERSION = typeof undefined === 'string'
669
670
  ? undefined
670
- : '0.7.4-alpha.4';
671
+ : '0.7.4-alpha.6';
671
672
 
672
673
  const DEFAULT_TIMEOUT = 10000;
673
674
  const HANDSHAKE_RETRY_INTERVAL = 250;
@@ -1044,6 +1045,1184 @@ function createCloudModule(requester) {
1044
1045
  };
1045
1046
  }
1046
1047
 
1048
+ /**
1049
+ * 复制当前小程序的通用分享链接。
1050
+ *
1051
+ * @param requester 底层 bridge 请求能力。
1052
+ * @param options 分享扩展数据;不传时复制默认首页链接。
1053
+ * @returns 已写入系统剪贴板的完整分享链接。
1054
+ * @throws {HbMiniProgramSDKError} 当参数、bridge、父容器或剪贴板能力调用失败时抛出。
1055
+ */
1056
+ function copyLink(requester, options) {
1057
+ return requester.request(SHARE_COPY_LINK_METHOD, options);
1058
+ }
1059
+
1060
+ // TextEncoder and TextDecoder are standardized in whatwg encoding:
1061
+ // https://encoding.spec.whatwg.org/
1062
+ // and available in all the modern browsers:
1063
+ // https://caniuse.com/textencoder
1064
+ // They are available in Node.js since v12 LTS as well:
1065
+ // https://nodejs.org/api/globals.html#textencoder
1066
+ new TextEncoder();
1067
+ const CHUNK_SIZE = 4096;
1068
+ function utf8DecodeJs(bytes, inputOffset, byteLength) {
1069
+ let offset = inputOffset;
1070
+ const end = offset + byteLength;
1071
+ const units = [];
1072
+ let result = "";
1073
+ while (offset < end) {
1074
+ const byte1 = bytes[offset++];
1075
+ if ((byte1 & 0x80) === 0) {
1076
+ // 1 byte
1077
+ units.push(byte1);
1078
+ }
1079
+ else if ((byte1 & 0xe0) === 0xc0) {
1080
+ // 2 bytes
1081
+ const byte2 = bytes[offset++] & 0x3f;
1082
+ units.push(((byte1 & 0x1f) << 6) | byte2);
1083
+ }
1084
+ else if ((byte1 & 0xf0) === 0xe0) {
1085
+ // 3 bytes
1086
+ const byte2 = bytes[offset++] & 0x3f;
1087
+ const byte3 = bytes[offset++] & 0x3f;
1088
+ units.push(((byte1 & 0x1f) << 12) | (byte2 << 6) | byte3);
1089
+ }
1090
+ else if ((byte1 & 0xf8) === 0xf0) {
1091
+ // 4 bytes
1092
+ const byte2 = bytes[offset++] & 0x3f;
1093
+ const byte3 = bytes[offset++] & 0x3f;
1094
+ const byte4 = bytes[offset++] & 0x3f;
1095
+ let unit = ((byte1 & 0x07) << 0x12) | (byte2 << 0x0c) | (byte3 << 0x06) | byte4;
1096
+ if (unit > 0xffff) {
1097
+ unit -= 0x10000;
1098
+ units.push(((unit >>> 10) & 0x3ff) | 0xd800);
1099
+ unit = 0xdc00 | (unit & 0x3ff);
1100
+ }
1101
+ units.push(unit);
1102
+ }
1103
+ else {
1104
+ units.push(byte1);
1105
+ }
1106
+ if (units.length >= CHUNK_SIZE) {
1107
+ result += String.fromCharCode(...units);
1108
+ units.length = 0;
1109
+ }
1110
+ }
1111
+ if (units.length > 0) {
1112
+ result += String.fromCharCode(...units);
1113
+ }
1114
+ return result;
1115
+ }
1116
+ const sharedTextDecoder = new TextDecoder();
1117
+ // This threshold should be determined by benchmarking, which might vary in engines and input data.
1118
+ // Run `npx ts-node benchmark/decode-string.ts` for details.
1119
+ const TEXT_DECODER_THRESHOLD = 200;
1120
+ function utf8DecodeTD(bytes, inputOffset, byteLength) {
1121
+ const stringBytes = bytes.subarray(inputOffset, inputOffset + byteLength);
1122
+ return sharedTextDecoder.decode(stringBytes);
1123
+ }
1124
+ function utf8Decode(bytes, inputOffset, byteLength) {
1125
+ if (byteLength > TEXT_DECODER_THRESHOLD) {
1126
+ return utf8DecodeTD(bytes, inputOffset, byteLength);
1127
+ }
1128
+ else {
1129
+ return utf8DecodeJs(bytes, inputOffset, byteLength);
1130
+ }
1131
+ }
1132
+
1133
+ /**
1134
+ * ExtData is used to handle Extension Types that are not registered to ExtensionCodec.
1135
+ */
1136
+ class ExtData {
1137
+ type;
1138
+ data;
1139
+ constructor(type, data) {
1140
+ this.type = type;
1141
+ this.data = data;
1142
+ }
1143
+ }
1144
+
1145
+ class DecodeError extends Error {
1146
+ constructor(message) {
1147
+ super(message);
1148
+ // fix the prototype chain in a cross-platform way
1149
+ const proto = Object.create(DecodeError.prototype);
1150
+ Object.setPrototypeOf(this, proto);
1151
+ Object.defineProperty(this, "name", {
1152
+ configurable: true,
1153
+ enumerable: false,
1154
+ value: DecodeError.name,
1155
+ });
1156
+ }
1157
+ }
1158
+
1159
+ // Integer Utility
1160
+ const UINT32_MAX = 4294967295;
1161
+ function setInt64(view, offset, value) {
1162
+ const high = Math.floor(value / 4294967296);
1163
+ const low = value; // high bits are truncated by DataView
1164
+ view.setUint32(offset, high);
1165
+ view.setUint32(offset + 4, low);
1166
+ }
1167
+ function getInt64(view, offset) {
1168
+ const high = view.getInt32(offset);
1169
+ const low = view.getUint32(offset + 4);
1170
+ return high * 4294967296 + low;
1171
+ }
1172
+ function getUint64(view, offset) {
1173
+ const high = view.getUint32(offset);
1174
+ const low = view.getUint32(offset + 4);
1175
+ return high * 4294967296 + low;
1176
+ }
1177
+
1178
+ // https://github.com/msgpack/msgpack/blob/master/spec.md#timestamp-extension-type
1179
+ const EXT_TIMESTAMP = -1;
1180
+ const TIMESTAMP32_MAX_SEC = 0x100000000 - 1; // 32-bit unsigned int
1181
+ const TIMESTAMP64_MAX_SEC = 0x400000000 - 1; // 34-bit unsigned int
1182
+ function encodeTimeSpecToTimestamp({ sec, nsec }) {
1183
+ if (sec >= 0 && nsec >= 0 && sec <= TIMESTAMP64_MAX_SEC) {
1184
+ // Here sec >= 0 && nsec >= 0
1185
+ if (nsec === 0 && sec <= TIMESTAMP32_MAX_SEC) {
1186
+ // timestamp 32 = { sec32 (unsigned) }
1187
+ const rv = new Uint8Array(4);
1188
+ const view = new DataView(rv.buffer);
1189
+ view.setUint32(0, sec);
1190
+ return rv;
1191
+ }
1192
+ else {
1193
+ // timestamp 64 = { nsec30 (unsigned), sec34 (unsigned) }
1194
+ const secHigh = sec / 0x100000000;
1195
+ const secLow = sec & 0xffffffff;
1196
+ const rv = new Uint8Array(8);
1197
+ const view = new DataView(rv.buffer);
1198
+ // nsec30 | secHigh2
1199
+ view.setUint32(0, (nsec << 2) | (secHigh & 0x3));
1200
+ // secLow32
1201
+ view.setUint32(4, secLow);
1202
+ return rv;
1203
+ }
1204
+ }
1205
+ else {
1206
+ // timestamp 96 = { nsec32 (unsigned), sec64 (signed) }
1207
+ const rv = new Uint8Array(12);
1208
+ const view = new DataView(rv.buffer);
1209
+ view.setUint32(0, nsec);
1210
+ setInt64(view, 4, sec);
1211
+ return rv;
1212
+ }
1213
+ }
1214
+ function encodeDateToTimeSpec(date) {
1215
+ const msec = date.getTime();
1216
+ const sec = Math.floor(msec / 1e3);
1217
+ const nsec = (msec - sec * 1e3) * 1e6;
1218
+ // Normalizes { sec, nsec } to ensure nsec is unsigned.
1219
+ const nsecInSec = Math.floor(nsec / 1e9);
1220
+ return {
1221
+ sec: sec + nsecInSec,
1222
+ nsec: nsec - nsecInSec * 1e9,
1223
+ };
1224
+ }
1225
+ function encodeTimestampExtension(object) {
1226
+ if (object instanceof Date) {
1227
+ const timeSpec = encodeDateToTimeSpec(object);
1228
+ return encodeTimeSpecToTimestamp(timeSpec);
1229
+ }
1230
+ else {
1231
+ return null;
1232
+ }
1233
+ }
1234
+ function decodeTimestampToTimeSpec(data) {
1235
+ const view = new DataView(data.buffer, data.byteOffset, data.byteLength);
1236
+ // data may be 32, 64, or 96 bits
1237
+ switch (data.byteLength) {
1238
+ case 4: {
1239
+ // timestamp 32 = { sec32 }
1240
+ const sec = view.getUint32(0);
1241
+ const nsec = 0;
1242
+ return { sec, nsec };
1243
+ }
1244
+ case 8: {
1245
+ // timestamp 64 = { nsec30, sec34 }
1246
+ const nsec30AndSecHigh2 = view.getUint32(0);
1247
+ const secLow32 = view.getUint32(4);
1248
+ const sec = (nsec30AndSecHigh2 & 0x3) * 0x100000000 + secLow32;
1249
+ const nsec = nsec30AndSecHigh2 >>> 2;
1250
+ return { sec, nsec };
1251
+ }
1252
+ case 12: {
1253
+ // timestamp 96 = { nsec32 (unsigned), sec64 (signed) }
1254
+ const sec = getInt64(view, 4);
1255
+ const nsec = view.getUint32(0);
1256
+ return { sec, nsec };
1257
+ }
1258
+ default:
1259
+ throw new DecodeError(`Unrecognized data size for timestamp (expected 4, 8, or 12): ${data.length}`);
1260
+ }
1261
+ }
1262
+ function decodeTimestampExtension(data) {
1263
+ const timeSpec = decodeTimestampToTimeSpec(data);
1264
+ return new Date(timeSpec.sec * 1e3 + timeSpec.nsec / 1e6);
1265
+ }
1266
+ const timestampExtension = {
1267
+ type: EXT_TIMESTAMP,
1268
+ encode: encodeTimestampExtension,
1269
+ decode: decodeTimestampExtension,
1270
+ };
1271
+
1272
+ // ExtensionCodec to handle MessagePack extensions
1273
+ class ExtensionCodec {
1274
+ static defaultCodec = new ExtensionCodec();
1275
+ // ensures ExtensionCodecType<X> matches ExtensionCodec<X>
1276
+ // this will make type errors a lot more clear
1277
+ // eslint-disable-next-line @typescript-eslint/naming-convention
1278
+ __brand;
1279
+ // built-in extensions
1280
+ builtInEncoders = [];
1281
+ builtInDecoders = [];
1282
+ // custom extensions
1283
+ encoders = [];
1284
+ decoders = [];
1285
+ constructor() {
1286
+ this.register(timestampExtension);
1287
+ }
1288
+ register({ type, encode, decode, }) {
1289
+ if (type >= 0) {
1290
+ // custom extensions
1291
+ this.encoders[type] = encode;
1292
+ this.decoders[type] = decode;
1293
+ }
1294
+ else {
1295
+ // built-in extensions
1296
+ const index = -1 - type;
1297
+ this.builtInEncoders[index] = encode;
1298
+ this.builtInDecoders[index] = decode;
1299
+ }
1300
+ }
1301
+ tryToEncode(object, context) {
1302
+ // built-in extensions
1303
+ for (let i = 0; i < this.builtInEncoders.length; i++) {
1304
+ const encodeExt = this.builtInEncoders[i];
1305
+ if (encodeExt != null) {
1306
+ const data = encodeExt(object, context);
1307
+ if (data != null) {
1308
+ const type = -1 - i;
1309
+ return new ExtData(type, data);
1310
+ }
1311
+ }
1312
+ }
1313
+ // custom extensions
1314
+ for (let i = 0; i < this.encoders.length; i++) {
1315
+ const encodeExt = this.encoders[i];
1316
+ if (encodeExt != null) {
1317
+ const data = encodeExt(object, context);
1318
+ if (data != null) {
1319
+ const type = i;
1320
+ return new ExtData(type, data);
1321
+ }
1322
+ }
1323
+ }
1324
+ if (object instanceof ExtData) {
1325
+ // to keep ExtData as is
1326
+ return object;
1327
+ }
1328
+ return null;
1329
+ }
1330
+ decode(data, type, context) {
1331
+ const decodeExt = type < 0 ? this.builtInDecoders[-1 - type] : this.decoders[type];
1332
+ if (decodeExt) {
1333
+ return decodeExt(data, type, context);
1334
+ }
1335
+ else {
1336
+ // decode() does not fail, returns ExtData instead.
1337
+ return new ExtData(type, data);
1338
+ }
1339
+ }
1340
+ }
1341
+
1342
+ function isArrayBufferLike(buffer) {
1343
+ return (buffer instanceof ArrayBuffer || (typeof SharedArrayBuffer !== "undefined" && buffer instanceof SharedArrayBuffer));
1344
+ }
1345
+ function ensureUint8Array(buffer) {
1346
+ if (buffer instanceof Uint8Array) {
1347
+ return buffer;
1348
+ }
1349
+ else if (ArrayBuffer.isView(buffer)) {
1350
+ return new Uint8Array(buffer.buffer, buffer.byteOffset, buffer.byteLength);
1351
+ }
1352
+ else if (isArrayBufferLike(buffer)) {
1353
+ return new Uint8Array(buffer);
1354
+ }
1355
+ else {
1356
+ // ArrayLike<number>
1357
+ return Uint8Array.from(buffer);
1358
+ }
1359
+ }
1360
+
1361
+ function prettyByte(byte) {
1362
+ return `${byte < 0 ? "-" : ""}0x${Math.abs(byte).toString(16).padStart(2, "0")}`;
1363
+ }
1364
+
1365
+ const DEFAULT_MAX_KEY_LENGTH = 16;
1366
+ const DEFAULT_MAX_LENGTH_PER_KEY = 16;
1367
+ class CachedKeyDecoder {
1368
+ hit = 0;
1369
+ miss = 0;
1370
+ caches;
1371
+ maxKeyLength;
1372
+ maxLengthPerKey;
1373
+ constructor(maxKeyLength = DEFAULT_MAX_KEY_LENGTH, maxLengthPerKey = DEFAULT_MAX_LENGTH_PER_KEY) {
1374
+ this.maxKeyLength = maxKeyLength;
1375
+ this.maxLengthPerKey = maxLengthPerKey;
1376
+ // avoid `new Array(N)`, which makes a sparse array,
1377
+ // because a sparse array is typically slower than a non-sparse array.
1378
+ this.caches = [];
1379
+ for (let i = 0; i < this.maxKeyLength; i++) {
1380
+ this.caches.push([]);
1381
+ }
1382
+ }
1383
+ canBeCached(byteLength) {
1384
+ return byteLength > 0 && byteLength <= this.maxKeyLength;
1385
+ }
1386
+ find(bytes, inputOffset, byteLength) {
1387
+ const records = this.caches[byteLength - 1];
1388
+ FIND_CHUNK: for (const record of records) {
1389
+ const recordBytes = record.bytes;
1390
+ for (let j = 0; j < byteLength; j++) {
1391
+ if (recordBytes[j] !== bytes[inputOffset + j]) {
1392
+ continue FIND_CHUNK;
1393
+ }
1394
+ }
1395
+ return record.str;
1396
+ }
1397
+ return null;
1398
+ }
1399
+ store(bytes, value) {
1400
+ const records = this.caches[bytes.length - 1];
1401
+ const record = { bytes, str: value };
1402
+ if (records.length >= this.maxLengthPerKey) {
1403
+ // `records` are full!
1404
+ // Set `record` to an arbitrary position.
1405
+ records[(Math.random() * records.length) | 0] = record;
1406
+ }
1407
+ else {
1408
+ records.push(record);
1409
+ }
1410
+ }
1411
+ decode(bytes, inputOffset, byteLength) {
1412
+ const cachedValue = this.find(bytes, inputOffset, byteLength);
1413
+ if (cachedValue != null) {
1414
+ this.hit++;
1415
+ return cachedValue;
1416
+ }
1417
+ this.miss++;
1418
+ const str = utf8DecodeJs(bytes, inputOffset, byteLength);
1419
+ // Ensure to copy a slice of bytes because the bytes may be a NodeJS Buffer and Buffer#slice() returns a reference to its internal ArrayBuffer.
1420
+ const slicedCopyOfBytes = Uint8Array.prototype.slice.call(bytes, inputOffset, inputOffset + byteLength);
1421
+ this.store(slicedCopyOfBytes, str);
1422
+ return str;
1423
+ }
1424
+ }
1425
+
1426
+ const STATE_ARRAY = "array";
1427
+ const STATE_MAP_KEY = "map_key";
1428
+ const STATE_MAP_VALUE = "map_value";
1429
+ const mapKeyConverter = (key) => {
1430
+ if (typeof key === "string" || typeof key === "number") {
1431
+ return key;
1432
+ }
1433
+ throw new DecodeError("The type of key must be string or number but " + typeof key);
1434
+ };
1435
+ class StackPool {
1436
+ stack = [];
1437
+ stackHeadPosition = -1;
1438
+ get length() {
1439
+ return this.stackHeadPosition + 1;
1440
+ }
1441
+ top() {
1442
+ return this.stack[this.stackHeadPosition];
1443
+ }
1444
+ pushArrayState(size) {
1445
+ const state = this.getUninitializedStateFromPool();
1446
+ state.type = STATE_ARRAY;
1447
+ state.position = 0;
1448
+ state.size = size;
1449
+ state.array = new Array(size);
1450
+ }
1451
+ pushMapState(size) {
1452
+ const state = this.getUninitializedStateFromPool();
1453
+ state.type = STATE_MAP_KEY;
1454
+ state.readCount = 0;
1455
+ state.size = size;
1456
+ state.map = {};
1457
+ }
1458
+ getUninitializedStateFromPool() {
1459
+ this.stackHeadPosition++;
1460
+ if (this.stackHeadPosition === this.stack.length) {
1461
+ const partialState = {
1462
+ type: undefined,
1463
+ size: 0,
1464
+ array: undefined,
1465
+ position: 0,
1466
+ readCount: 0,
1467
+ map: undefined,
1468
+ key: null,
1469
+ };
1470
+ this.stack.push(partialState);
1471
+ }
1472
+ return this.stack[this.stackHeadPosition];
1473
+ }
1474
+ release(state) {
1475
+ const topStackState = this.stack[this.stackHeadPosition];
1476
+ if (topStackState !== state) {
1477
+ throw new Error("Invalid stack state. Released state is not on top of the stack.");
1478
+ }
1479
+ if (state.type === STATE_ARRAY) {
1480
+ const partialState = state;
1481
+ partialState.size = 0;
1482
+ partialState.array = undefined;
1483
+ partialState.position = 0;
1484
+ partialState.type = undefined;
1485
+ }
1486
+ if (state.type === STATE_MAP_KEY || state.type === STATE_MAP_VALUE) {
1487
+ const partialState = state;
1488
+ partialState.size = 0;
1489
+ partialState.map = undefined;
1490
+ partialState.readCount = 0;
1491
+ partialState.type = undefined;
1492
+ }
1493
+ this.stackHeadPosition--;
1494
+ }
1495
+ reset() {
1496
+ this.stack.length = 0;
1497
+ this.stackHeadPosition = -1;
1498
+ }
1499
+ }
1500
+ const HEAD_BYTE_REQUIRED = -1;
1501
+ const EMPTY_VIEW = new DataView(new ArrayBuffer(0));
1502
+ const EMPTY_BYTES = new Uint8Array(EMPTY_VIEW.buffer);
1503
+ try {
1504
+ // IE11: The spec says it should throw RangeError,
1505
+ // IE11: but in IE11 it throws TypeError.
1506
+ EMPTY_VIEW.getInt8(0);
1507
+ }
1508
+ catch (e) {
1509
+ if (!(e instanceof RangeError)) {
1510
+ throw new Error("This module is not supported in the current JavaScript engine because DataView does not throw RangeError on out-of-bounds access");
1511
+ }
1512
+ }
1513
+ const MORE_DATA = new RangeError("Insufficient data");
1514
+ const sharedCachedKeyDecoder = new CachedKeyDecoder();
1515
+ class Decoder {
1516
+ extensionCodec;
1517
+ context;
1518
+ useBigInt64;
1519
+ rawStrings;
1520
+ maxStrLength;
1521
+ maxBinLength;
1522
+ maxArrayLength;
1523
+ maxMapLength;
1524
+ maxExtLength;
1525
+ keyDecoder;
1526
+ mapKeyConverter;
1527
+ totalPos = 0;
1528
+ pos = 0;
1529
+ view = EMPTY_VIEW;
1530
+ bytes = EMPTY_BYTES;
1531
+ headByte = HEAD_BYTE_REQUIRED;
1532
+ stack = new StackPool();
1533
+ entered = false;
1534
+ constructor(options) {
1535
+ this.extensionCodec = options?.extensionCodec ?? ExtensionCodec.defaultCodec;
1536
+ this.context = options?.context; // needs a type assertion because EncoderOptions has no context property when ContextType is undefined
1537
+ this.useBigInt64 = options?.useBigInt64 ?? false;
1538
+ this.rawStrings = options?.rawStrings ?? false;
1539
+ this.maxStrLength = options?.maxStrLength ?? UINT32_MAX;
1540
+ this.maxBinLength = options?.maxBinLength ?? UINT32_MAX;
1541
+ this.maxArrayLength = options?.maxArrayLength ?? UINT32_MAX;
1542
+ this.maxMapLength = options?.maxMapLength ?? UINT32_MAX;
1543
+ this.maxExtLength = options?.maxExtLength ?? UINT32_MAX;
1544
+ this.keyDecoder = options?.keyDecoder !== undefined ? options.keyDecoder : sharedCachedKeyDecoder;
1545
+ this.mapKeyConverter = options?.mapKeyConverter ?? mapKeyConverter;
1546
+ }
1547
+ clone() {
1548
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
1549
+ return new Decoder({
1550
+ extensionCodec: this.extensionCodec,
1551
+ context: this.context,
1552
+ useBigInt64: this.useBigInt64,
1553
+ rawStrings: this.rawStrings,
1554
+ maxStrLength: this.maxStrLength,
1555
+ maxBinLength: this.maxBinLength,
1556
+ maxArrayLength: this.maxArrayLength,
1557
+ maxMapLength: this.maxMapLength,
1558
+ maxExtLength: this.maxExtLength,
1559
+ keyDecoder: this.keyDecoder,
1560
+ });
1561
+ }
1562
+ reinitializeState() {
1563
+ this.totalPos = 0;
1564
+ this.headByte = HEAD_BYTE_REQUIRED;
1565
+ this.stack.reset();
1566
+ // view, bytes, and pos will be re-initialized in setBuffer()
1567
+ }
1568
+ setBuffer(buffer) {
1569
+ const bytes = ensureUint8Array(buffer);
1570
+ this.bytes = bytes;
1571
+ this.view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
1572
+ this.pos = 0;
1573
+ }
1574
+ appendBuffer(buffer) {
1575
+ if (this.headByte === HEAD_BYTE_REQUIRED && !this.hasRemaining(1)) {
1576
+ this.setBuffer(buffer);
1577
+ }
1578
+ else {
1579
+ const remainingData = this.bytes.subarray(this.pos);
1580
+ const newData = ensureUint8Array(buffer);
1581
+ // concat remainingData + newData
1582
+ const newBuffer = new Uint8Array(remainingData.length + newData.length);
1583
+ newBuffer.set(remainingData);
1584
+ newBuffer.set(newData, remainingData.length);
1585
+ this.setBuffer(newBuffer);
1586
+ }
1587
+ }
1588
+ hasRemaining(size) {
1589
+ return this.view.byteLength - this.pos >= size;
1590
+ }
1591
+ createExtraByteError(posToShow) {
1592
+ const { view, pos } = this;
1593
+ return new RangeError(`Extra ${view.byteLength - pos} of ${view.byteLength} byte(s) found at buffer[${posToShow}]`);
1594
+ }
1595
+ /**
1596
+ * @throws {@link DecodeError}
1597
+ * @throws {@link RangeError}
1598
+ */
1599
+ decode(buffer) {
1600
+ if (this.entered) {
1601
+ const instance = this.clone();
1602
+ return instance.decode(buffer);
1603
+ }
1604
+ try {
1605
+ this.entered = true;
1606
+ this.reinitializeState();
1607
+ this.setBuffer(buffer);
1608
+ const object = this.doDecodeSync();
1609
+ if (this.hasRemaining(1)) {
1610
+ throw this.createExtraByteError(this.pos);
1611
+ }
1612
+ return object;
1613
+ }
1614
+ finally {
1615
+ this.entered = false;
1616
+ }
1617
+ }
1618
+ *decodeMulti(buffer) {
1619
+ if (this.entered) {
1620
+ const instance = this.clone();
1621
+ yield* instance.decodeMulti(buffer);
1622
+ return;
1623
+ }
1624
+ try {
1625
+ this.entered = true;
1626
+ this.reinitializeState();
1627
+ this.setBuffer(buffer);
1628
+ while (this.hasRemaining(1)) {
1629
+ yield this.doDecodeSync();
1630
+ }
1631
+ }
1632
+ finally {
1633
+ this.entered = false;
1634
+ }
1635
+ }
1636
+ async decodeAsync(stream) {
1637
+ if (this.entered) {
1638
+ const instance = this.clone();
1639
+ return instance.decodeAsync(stream);
1640
+ }
1641
+ try {
1642
+ this.entered = true;
1643
+ let decoded = false;
1644
+ let object;
1645
+ for await (const buffer of stream) {
1646
+ if (decoded) {
1647
+ this.entered = false;
1648
+ throw this.createExtraByteError(this.totalPos);
1649
+ }
1650
+ this.appendBuffer(buffer);
1651
+ try {
1652
+ object = this.doDecodeSync();
1653
+ decoded = true;
1654
+ }
1655
+ catch (e) {
1656
+ if (!(e instanceof RangeError)) {
1657
+ throw e; // rethrow
1658
+ }
1659
+ // fallthrough
1660
+ }
1661
+ this.totalPos += this.pos;
1662
+ }
1663
+ if (decoded) {
1664
+ if (this.hasRemaining(1)) {
1665
+ throw this.createExtraByteError(this.totalPos);
1666
+ }
1667
+ return object;
1668
+ }
1669
+ const { headByte, pos, totalPos } = this;
1670
+ throw new RangeError(`Insufficient data in parsing ${prettyByte(headByte)} at ${totalPos} (${pos} in the current buffer)`);
1671
+ }
1672
+ finally {
1673
+ this.entered = false;
1674
+ }
1675
+ }
1676
+ decodeArrayStream(stream) {
1677
+ return this.decodeMultiAsync(stream, true);
1678
+ }
1679
+ decodeStream(stream) {
1680
+ return this.decodeMultiAsync(stream, false);
1681
+ }
1682
+ async *decodeMultiAsync(stream, isArray) {
1683
+ if (this.entered) {
1684
+ const instance = this.clone();
1685
+ yield* instance.decodeMultiAsync(stream, isArray);
1686
+ return;
1687
+ }
1688
+ try {
1689
+ this.entered = true;
1690
+ let isArrayHeaderRequired = isArray;
1691
+ let arrayItemsLeft = -1;
1692
+ for await (const buffer of stream) {
1693
+ if (isArray && arrayItemsLeft === 0) {
1694
+ throw this.createExtraByteError(this.totalPos);
1695
+ }
1696
+ this.appendBuffer(buffer);
1697
+ if (isArrayHeaderRequired) {
1698
+ arrayItemsLeft = this.readArraySize();
1699
+ isArrayHeaderRequired = false;
1700
+ this.complete();
1701
+ }
1702
+ try {
1703
+ while (true) {
1704
+ yield this.doDecodeSync();
1705
+ if (--arrayItemsLeft === 0) {
1706
+ break;
1707
+ }
1708
+ }
1709
+ }
1710
+ catch (e) {
1711
+ if (!(e instanceof RangeError)) {
1712
+ throw e; // rethrow
1713
+ }
1714
+ // fallthrough
1715
+ }
1716
+ this.totalPos += this.pos;
1717
+ }
1718
+ }
1719
+ finally {
1720
+ this.entered = false;
1721
+ }
1722
+ }
1723
+ doDecodeSync() {
1724
+ DECODE: while (true) {
1725
+ const headByte = this.readHeadByte();
1726
+ let object;
1727
+ if (headByte >= 0xe0) {
1728
+ // negative fixint (111x xxxx) 0xe0 - 0xff
1729
+ object = headByte - 0x100;
1730
+ }
1731
+ else if (headByte < 0xc0) {
1732
+ if (headByte < 0x80) {
1733
+ // positive fixint (0xxx xxxx) 0x00 - 0x7f
1734
+ object = headByte;
1735
+ }
1736
+ else if (headByte < 0x90) {
1737
+ // fixmap (1000 xxxx) 0x80 - 0x8f
1738
+ const size = headByte - 0x80;
1739
+ if (size !== 0) {
1740
+ this.pushMapState(size);
1741
+ this.complete();
1742
+ continue DECODE;
1743
+ }
1744
+ else {
1745
+ object = {};
1746
+ }
1747
+ }
1748
+ else if (headByte < 0xa0) {
1749
+ // fixarray (1001 xxxx) 0x90 - 0x9f
1750
+ const size = headByte - 0x90;
1751
+ if (size !== 0) {
1752
+ this.pushArrayState(size);
1753
+ this.complete();
1754
+ continue DECODE;
1755
+ }
1756
+ else {
1757
+ object = [];
1758
+ }
1759
+ }
1760
+ else {
1761
+ // fixstr (101x xxxx) 0xa0 - 0xbf
1762
+ const byteLength = headByte - 0xa0;
1763
+ object = this.decodeString(byteLength, 0);
1764
+ }
1765
+ }
1766
+ else if (headByte === 0xc0) {
1767
+ // nil
1768
+ object = null;
1769
+ }
1770
+ else if (headByte === 0xc2) {
1771
+ // false
1772
+ object = false;
1773
+ }
1774
+ else if (headByte === 0xc3) {
1775
+ // true
1776
+ object = true;
1777
+ }
1778
+ else if (headByte === 0xca) {
1779
+ // float 32
1780
+ object = this.readF32();
1781
+ }
1782
+ else if (headByte === 0xcb) {
1783
+ // float 64
1784
+ object = this.readF64();
1785
+ }
1786
+ else if (headByte === 0xcc) {
1787
+ // uint 8
1788
+ object = this.readU8();
1789
+ }
1790
+ else if (headByte === 0xcd) {
1791
+ // uint 16
1792
+ object = this.readU16();
1793
+ }
1794
+ else if (headByte === 0xce) {
1795
+ // uint 32
1796
+ object = this.readU32();
1797
+ }
1798
+ else if (headByte === 0xcf) {
1799
+ // uint 64
1800
+ if (this.useBigInt64) {
1801
+ object = this.readU64AsBigInt();
1802
+ }
1803
+ else {
1804
+ object = this.readU64();
1805
+ }
1806
+ }
1807
+ else if (headByte === 0xd0) {
1808
+ // int 8
1809
+ object = this.readI8();
1810
+ }
1811
+ else if (headByte === 0xd1) {
1812
+ // int 16
1813
+ object = this.readI16();
1814
+ }
1815
+ else if (headByte === 0xd2) {
1816
+ // int 32
1817
+ object = this.readI32();
1818
+ }
1819
+ else if (headByte === 0xd3) {
1820
+ // int 64
1821
+ if (this.useBigInt64) {
1822
+ object = this.readI64AsBigInt();
1823
+ }
1824
+ else {
1825
+ object = this.readI64();
1826
+ }
1827
+ }
1828
+ else if (headByte === 0xd9) {
1829
+ // str 8
1830
+ const byteLength = this.lookU8();
1831
+ object = this.decodeString(byteLength, 1);
1832
+ }
1833
+ else if (headByte === 0xda) {
1834
+ // str 16
1835
+ const byteLength = this.lookU16();
1836
+ object = this.decodeString(byteLength, 2);
1837
+ }
1838
+ else if (headByte === 0xdb) {
1839
+ // str 32
1840
+ const byteLength = this.lookU32();
1841
+ object = this.decodeString(byteLength, 4);
1842
+ }
1843
+ else if (headByte === 0xdc) {
1844
+ // array 16
1845
+ const size = this.readU16();
1846
+ if (size !== 0) {
1847
+ this.pushArrayState(size);
1848
+ this.complete();
1849
+ continue DECODE;
1850
+ }
1851
+ else {
1852
+ object = [];
1853
+ }
1854
+ }
1855
+ else if (headByte === 0xdd) {
1856
+ // array 32
1857
+ const size = this.readU32();
1858
+ if (size !== 0) {
1859
+ this.pushArrayState(size);
1860
+ this.complete();
1861
+ continue DECODE;
1862
+ }
1863
+ else {
1864
+ object = [];
1865
+ }
1866
+ }
1867
+ else if (headByte === 0xde) {
1868
+ // map 16
1869
+ const size = this.readU16();
1870
+ if (size !== 0) {
1871
+ this.pushMapState(size);
1872
+ this.complete();
1873
+ continue DECODE;
1874
+ }
1875
+ else {
1876
+ object = {};
1877
+ }
1878
+ }
1879
+ else if (headByte === 0xdf) {
1880
+ // map 32
1881
+ const size = this.readU32();
1882
+ if (size !== 0) {
1883
+ this.pushMapState(size);
1884
+ this.complete();
1885
+ continue DECODE;
1886
+ }
1887
+ else {
1888
+ object = {};
1889
+ }
1890
+ }
1891
+ else if (headByte === 0xc4) {
1892
+ // bin 8
1893
+ const size = this.lookU8();
1894
+ object = this.decodeBinary(size, 1);
1895
+ }
1896
+ else if (headByte === 0xc5) {
1897
+ // bin 16
1898
+ const size = this.lookU16();
1899
+ object = this.decodeBinary(size, 2);
1900
+ }
1901
+ else if (headByte === 0xc6) {
1902
+ // bin 32
1903
+ const size = this.lookU32();
1904
+ object = this.decodeBinary(size, 4);
1905
+ }
1906
+ else if (headByte === 0xd4) {
1907
+ // fixext 1
1908
+ object = this.decodeExtension(1, 0);
1909
+ }
1910
+ else if (headByte === 0xd5) {
1911
+ // fixext 2
1912
+ object = this.decodeExtension(2, 0);
1913
+ }
1914
+ else if (headByte === 0xd6) {
1915
+ // fixext 4
1916
+ object = this.decodeExtension(4, 0);
1917
+ }
1918
+ else if (headByte === 0xd7) {
1919
+ // fixext 8
1920
+ object = this.decodeExtension(8, 0);
1921
+ }
1922
+ else if (headByte === 0xd8) {
1923
+ // fixext 16
1924
+ object = this.decodeExtension(16, 0);
1925
+ }
1926
+ else if (headByte === 0xc7) {
1927
+ // ext 8
1928
+ const size = this.lookU8();
1929
+ object = this.decodeExtension(size, 1);
1930
+ }
1931
+ else if (headByte === 0xc8) {
1932
+ // ext 16
1933
+ const size = this.lookU16();
1934
+ object = this.decodeExtension(size, 2);
1935
+ }
1936
+ else if (headByte === 0xc9) {
1937
+ // ext 32
1938
+ const size = this.lookU32();
1939
+ object = this.decodeExtension(size, 4);
1940
+ }
1941
+ else {
1942
+ throw new DecodeError(`Unrecognized type byte: ${prettyByte(headByte)}`);
1943
+ }
1944
+ this.complete();
1945
+ const stack = this.stack;
1946
+ while (stack.length > 0) {
1947
+ // arrays and maps
1948
+ const state = stack.top();
1949
+ if (state.type === STATE_ARRAY) {
1950
+ state.array[state.position] = object;
1951
+ state.position++;
1952
+ if (state.position === state.size) {
1953
+ object = state.array;
1954
+ stack.release(state);
1955
+ }
1956
+ else {
1957
+ continue DECODE;
1958
+ }
1959
+ }
1960
+ else if (state.type === STATE_MAP_KEY) {
1961
+ if (object === "__proto__") {
1962
+ throw new DecodeError("The key __proto__ is not allowed");
1963
+ }
1964
+ state.key = this.mapKeyConverter(object);
1965
+ state.type = STATE_MAP_VALUE;
1966
+ continue DECODE;
1967
+ }
1968
+ else {
1969
+ // it must be `state.type === State.MAP_VALUE` here
1970
+ state.map[state.key] = object;
1971
+ state.readCount++;
1972
+ if (state.readCount === state.size) {
1973
+ object = state.map;
1974
+ stack.release(state);
1975
+ }
1976
+ else {
1977
+ state.key = null;
1978
+ state.type = STATE_MAP_KEY;
1979
+ continue DECODE;
1980
+ }
1981
+ }
1982
+ }
1983
+ return object;
1984
+ }
1985
+ }
1986
+ readHeadByte() {
1987
+ if (this.headByte === HEAD_BYTE_REQUIRED) {
1988
+ this.headByte = this.readU8();
1989
+ // console.log("headByte", prettyByte(this.headByte));
1990
+ }
1991
+ return this.headByte;
1992
+ }
1993
+ complete() {
1994
+ this.headByte = HEAD_BYTE_REQUIRED;
1995
+ }
1996
+ readArraySize() {
1997
+ const headByte = this.readHeadByte();
1998
+ switch (headByte) {
1999
+ case 0xdc:
2000
+ return this.readU16();
2001
+ case 0xdd:
2002
+ return this.readU32();
2003
+ default: {
2004
+ if (headByte < 0xa0) {
2005
+ return headByte - 0x90;
2006
+ }
2007
+ else {
2008
+ throw new DecodeError(`Unrecognized array type byte: ${prettyByte(headByte)}`);
2009
+ }
2010
+ }
2011
+ }
2012
+ }
2013
+ pushMapState(size) {
2014
+ if (size > this.maxMapLength) {
2015
+ throw new DecodeError(`Max length exceeded: map length (${size}) > maxMapLengthLength (${this.maxMapLength})`);
2016
+ }
2017
+ this.stack.pushMapState(size);
2018
+ }
2019
+ pushArrayState(size) {
2020
+ if (size > this.maxArrayLength) {
2021
+ throw new DecodeError(`Max length exceeded: array length (${size}) > maxArrayLength (${this.maxArrayLength})`);
2022
+ }
2023
+ this.stack.pushArrayState(size);
2024
+ }
2025
+ decodeString(byteLength, headerOffset) {
2026
+ if (!this.rawStrings || this.stateIsMapKey()) {
2027
+ return this.decodeUtf8String(byteLength, headerOffset);
2028
+ }
2029
+ return this.decodeBinary(byteLength, headerOffset);
2030
+ }
2031
+ /**
2032
+ * @throws {@link RangeError}
2033
+ */
2034
+ decodeUtf8String(byteLength, headerOffset) {
2035
+ if (byteLength > this.maxStrLength) {
2036
+ throw new DecodeError(`Max length exceeded: UTF-8 byte length (${byteLength}) > maxStrLength (${this.maxStrLength})`);
2037
+ }
2038
+ if (this.bytes.byteLength < this.pos + headerOffset + byteLength) {
2039
+ throw MORE_DATA;
2040
+ }
2041
+ const offset = this.pos + headerOffset;
2042
+ let object;
2043
+ if (this.stateIsMapKey() && this.keyDecoder?.canBeCached(byteLength)) {
2044
+ object = this.keyDecoder.decode(this.bytes, offset, byteLength);
2045
+ }
2046
+ else {
2047
+ object = utf8Decode(this.bytes, offset, byteLength);
2048
+ }
2049
+ this.pos += headerOffset + byteLength;
2050
+ return object;
2051
+ }
2052
+ stateIsMapKey() {
2053
+ if (this.stack.length > 0) {
2054
+ const state = this.stack.top();
2055
+ return state.type === STATE_MAP_KEY;
2056
+ }
2057
+ return false;
2058
+ }
2059
+ /**
2060
+ * @throws {@link RangeError}
2061
+ */
2062
+ decodeBinary(byteLength, headOffset) {
2063
+ if (byteLength > this.maxBinLength) {
2064
+ throw new DecodeError(`Max length exceeded: bin length (${byteLength}) > maxBinLength (${this.maxBinLength})`);
2065
+ }
2066
+ if (!this.hasRemaining(byteLength + headOffset)) {
2067
+ throw MORE_DATA;
2068
+ }
2069
+ const offset = this.pos + headOffset;
2070
+ const object = this.bytes.subarray(offset, offset + byteLength);
2071
+ this.pos += headOffset + byteLength;
2072
+ return object;
2073
+ }
2074
+ decodeExtension(size, headOffset) {
2075
+ if (size > this.maxExtLength) {
2076
+ throw new DecodeError(`Max length exceeded: ext length (${size}) > maxExtLength (${this.maxExtLength})`);
2077
+ }
2078
+ const extType = this.view.getInt8(this.pos + headOffset);
2079
+ const data = this.decodeBinary(size, headOffset + 1 /* extType */);
2080
+ return this.extensionCodec.decode(data, extType, this.context);
2081
+ }
2082
+ lookU8() {
2083
+ return this.view.getUint8(this.pos);
2084
+ }
2085
+ lookU16() {
2086
+ return this.view.getUint16(this.pos);
2087
+ }
2088
+ lookU32() {
2089
+ return this.view.getUint32(this.pos);
2090
+ }
2091
+ readU8() {
2092
+ const value = this.view.getUint8(this.pos);
2093
+ this.pos++;
2094
+ return value;
2095
+ }
2096
+ readI8() {
2097
+ const value = this.view.getInt8(this.pos);
2098
+ this.pos++;
2099
+ return value;
2100
+ }
2101
+ readU16() {
2102
+ const value = this.view.getUint16(this.pos);
2103
+ this.pos += 2;
2104
+ return value;
2105
+ }
2106
+ readI16() {
2107
+ const value = this.view.getInt16(this.pos);
2108
+ this.pos += 2;
2109
+ return value;
2110
+ }
2111
+ readU32() {
2112
+ const value = this.view.getUint32(this.pos);
2113
+ this.pos += 4;
2114
+ return value;
2115
+ }
2116
+ readI32() {
2117
+ const value = this.view.getInt32(this.pos);
2118
+ this.pos += 4;
2119
+ return value;
2120
+ }
2121
+ readU64() {
2122
+ const value = getUint64(this.view, this.pos);
2123
+ this.pos += 8;
2124
+ return value;
2125
+ }
2126
+ readI64() {
2127
+ const value = getInt64(this.view, this.pos);
2128
+ this.pos += 8;
2129
+ return value;
2130
+ }
2131
+ readU64AsBigInt() {
2132
+ const value = this.view.getBigUint64(this.pos);
2133
+ this.pos += 8;
2134
+ return value;
2135
+ }
2136
+ readI64AsBigInt() {
2137
+ const value = this.view.getBigInt64(this.pos);
2138
+ this.pos += 8;
2139
+ return value;
2140
+ }
2141
+ readF32() {
2142
+ const value = this.view.getFloat32(this.pos);
2143
+ this.pos += 4;
2144
+ return value;
2145
+ }
2146
+ readF64() {
2147
+ const value = this.view.getFloat64(this.pos);
2148
+ this.pos += 8;
2149
+ return value;
2150
+ }
2151
+ }
2152
+
2153
+ /**
2154
+ * It decodes a single MessagePack object in a buffer.
2155
+ *
2156
+ * This is a synchronous decoding function.
2157
+ * See other variants for asynchronous decoding: {@link decodeAsync}, {@link decodeMultiStream}, or {@link decodeArrayStream}.
2158
+ *
2159
+ * @throws {@link RangeError} if the buffer is incomplete, including the case where the buffer is empty.
2160
+ * @throws {@link DecodeError} if the buffer contains invalid data.
2161
+ */
2162
+ function decode(buffer, options) {
2163
+ const decoder = new Decoder(options);
2164
+ return decoder.decode(buffer);
2165
+ }
2166
+
2167
+ /** 读取小程序启动时携带的分享数据。 */
2168
+ function readMiniProgramShareExtra(launchHref) {
2169
+ if (!launchHref)
2170
+ return undefined;
2171
+ try {
2172
+ const value = new URL(launchHref).searchParams.get('extra');
2173
+ if (!value || value.length > 256 || value.length % 4 === 1 || !/^[A-Za-z0-9_-]+$/.test(value)) {
2174
+ return undefined;
2175
+ }
2176
+ const bytes = decodeBase64Url(value);
2177
+ if (bytes.byteLength > 128)
2178
+ return undefined;
2179
+ const decoded = decode(bytes);
2180
+ assertExtraValue(decoded);
2181
+ return decoded;
2182
+ }
2183
+ catch {
2184
+ return undefined;
2185
+ }
2186
+ }
2187
+ function assertExtraValue(value, depth = 0, seen = new WeakSet()) {
2188
+ if (depth > 8)
2189
+ throw new Error('extra nesting depth exceeded');
2190
+ if (value === null || typeof value === 'string' || typeof value === 'boolean')
2191
+ return;
2192
+ if (typeof value === 'number') {
2193
+ if (Number.isFinite(value))
2194
+ return;
2195
+ throw new Error('extra number must be finite');
2196
+ }
2197
+ if (typeof value !== 'object')
2198
+ throw new Error('extra value must be JSON-compatible');
2199
+ if (seen.has(value))
2200
+ throw new Error('extra value must not contain cycles');
2201
+ seen.add(value);
2202
+ if (Array.isArray(value)) {
2203
+ if (value.length > 64)
2204
+ throw new Error('extra array is too large');
2205
+ for (const item of value)
2206
+ assertExtraValue(item, depth + 1, seen);
2207
+ seen.delete(value);
2208
+ return;
2209
+ }
2210
+ const prototype = Object.getPrototypeOf(value);
2211
+ if (prototype !== Object.prototype && prototype !== null)
2212
+ throw new Error('extra object must be a plain object');
2213
+ const values = Object.values(value);
2214
+ if (values.length > 64)
2215
+ throw new Error('extra object has too many fields');
2216
+ for (const item of values)
2217
+ assertExtraValue(item, depth + 1, seen);
2218
+ seen.delete(value);
2219
+ }
2220
+ function decodeBase64Url(value) {
2221
+ const normalized = value.replaceAll('-', '+').replaceAll('_', '/');
2222
+ const padded = normalized.padEnd(normalized.length + ((4 - (normalized.length % 4)) % 4), '=');
2223
+ return Uint8Array.from(atob(padded), (character) => character.charCodeAt(0));
2224
+ }
2225
+
1047
2226
  /**
1048
2227
  * 截图并唤起分享。
1049
2228
  *
@@ -1074,8 +2253,10 @@ function showShareMenu(requester, options) {
1074
2253
  * @param requester 底层 bridge 请求能力。
1075
2254
  * @returns 面向业务层的分享模块对象。
1076
2255
  */
1077
- function createShareModule(requester) {
2256
+ function createShareModule(requester, launchHref) {
1078
2257
  return {
2258
+ copyLink: options => copyLink(requester, options),
2259
+ getExtra: () => readMiniProgramShareExtra(launchHref),
1079
2260
  showShareMenu: options => showShareMenu(requester, options),
1080
2261
  screenshot: options => screenshot(requester, options),
1081
2262
  };
@@ -1585,10 +2766,11 @@ class MiniProgramSDK {
1585
2766
  /** 云端数据相关开放能力。 */
1586
2767
  cloud;
1587
2768
  constructor(options) {
2769
+ const launchHref = (options?.selfWindow ?? getGlobalWindow())?.location.href;
1588
2770
  this.client = new MiniProgramBridgeClient(options);
1589
2771
  this.auth = createAuthModule(this.client);
1590
2772
  this.user = createUserModule(this.client);
1591
- this.share = createShareModule(this.client);
2773
+ this.share = createShareModule(this.client, launchHref);
1592
2774
  this.viewport = createViewportModule(this.client);
1593
2775
  this.storage = createStorageModule(this.client);
1594
2776
  this.network = createNetworkModule(this.client);
@@ -1704,6 +2886,8 @@ const user = {
1704
2886
  };
1705
2887
  /** 默认 SDK 实例的分享模块。 */
1706
2888
  const share = {
2889
+ copyLink: (options) => getDefaultSDK().share.copyLink(options),
2890
+ getExtra: () => getDefaultSDK().share.getExtra(),
1707
2891
  showShareMenu: (options) => getDefaultSDK().share.showShareMenu(options),
1708
2892
  screenshot: (options) => getDefaultSDK().share.screenshot(options),
1709
2893
  };