amf-ts 1.0.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.
package/dist/amf.js ADDED
@@ -0,0 +1,1024 @@
1
+ class d {
2
+ /**
3
+ * 创建一个强制类型值
4
+ *
5
+ * @param value - 实际要编码的值
6
+ * @param type - 强制使用的 AMF 类型
7
+ */
8
+ constructor(e, t) {
9
+ this.value = e, this.type = t;
10
+ }
11
+ }
12
+ class y {
13
+ /**
14
+ * 创建一个 AMF3 对象特征
15
+ *
16
+ * @param name - 对象的类名
17
+ * @param dynamic - 是否为动态对象
18
+ * @param externalizable - 是否可外部化
19
+ */
20
+ constructor(e, t, r) {
21
+ this.name = e, this.dynamic = t, this.externalizable = r, this.staticFields = [];
22
+ }
23
+ }
24
+ class u {
25
+ /**
26
+ * 创建一个可序列化对象
27
+ *
28
+ * @param serializableName - 对象的类名(可选)
29
+ * @param dynamic - 是否为动态对象(可选,默认 true)
30
+ */
31
+ constructor(e, t) {
32
+ this.__class = e, this.__dynamic = t;
33
+ }
34
+ }
35
+ class E extends u {
36
+ /**
37
+ * 创建一个可外部化对象
38
+ *
39
+ * @param externalizableName - 对象的类名
40
+ */
41
+ constructor(e) {
42
+ super(e);
43
+ }
44
+ /**
45
+ * 将对象写入编码器
46
+ *
47
+ * 子类必须覆盖此方法来实现自定义的序列化逻辑。
48
+ * 写入的数据必须能够被对应的 read() 静态方法正确读取。
49
+ *
50
+ * @param encoder - AMF 编码器实例
51
+ * @throws 如果子类没有实现此方法
52
+ */
53
+ write(e) {
54
+ throw new Error(`可外部化对象 ${this.__class} 没有定义 write 方法!`);
55
+ }
56
+ /**
57
+ * 从解码器读取并创建对象实例
58
+ *
59
+ * 子类必须定义一个同名的静态方法来实现自定义的反序列化逻辑。
60
+ * 此方法应该读取 write() 方法写入的数据并返回新的对象实例。
61
+ *
62
+ * 注意:这是一个静态方法,子类需要像这样定义:
63
+ * static read(decoder: AMFDecoder): YourClass { ... }
64
+ *
65
+ * @param decoder - AMF 解码器实例
66
+ * @returns 反序列化后的对象实例
67
+ * @throws 如果子类没有实现此方法
68
+ */
69
+ static read(e) {
70
+ throw new Error("可外部化对象没有定义 read 方法!");
71
+ }
72
+ }
73
+ class s {
74
+ /**
75
+ * 创建一个新的 AMF 类型
76
+ *
77
+ * @param id - 类型的数字标识符
78
+ * @param name - 类型的名称
79
+ * @param referencable - 是否可引用,默认为 false
80
+ */
81
+ constructor(e, t, r = !1) {
82
+ this.encode = () => {
83
+ throw new Error(`没有为类型 ${this.name} 定义编码器`);
84
+ }, this.decode = () => {
85
+ throw new Error(`没有为类型 ${this.name} 定义解码器`);
86
+ }, this.id = e, this.name = t, this.referencable = r;
87
+ }
88
+ }
89
+ const h = {
90
+ /**
91
+ * 根据 JavaScript 值推断对应的 AMF0 类型
92
+ *
93
+ * @param value - 要推断类型的值
94
+ * @returns 对应的 AMF0 类型
95
+ * @throws 如果无法推断类型则抛出错误
96
+ */
97
+ infer(i) {
98
+ const e = typeof i;
99
+ if (i === null)
100
+ return h.NULL;
101
+ if (e === "undefined")
102
+ return h.UNDEFINED;
103
+ if (i instanceof d)
104
+ return i.type;
105
+ if (e === "number")
106
+ return h.NUMBER;
107
+ if (e === "boolean")
108
+ return h.BOOLEAN;
109
+ if (e === "string" && i.length >= 65535)
110
+ return h.LONG_STRING;
111
+ if (e === "string")
112
+ return h.STRING;
113
+ if (Object.prototype.toString.call(i) === "[object Date]")
114
+ return h.DATE;
115
+ if (i instanceof Array)
116
+ return h.STRICT_ARRAY;
117
+ if (i instanceof u)
118
+ return !i.__class || i.__class === "" ? h.OBJECT : h.TYPED_OBJECT;
119
+ if (e === "object")
120
+ return h.ECMA_ARRAY;
121
+ throw new Error(`无法推断值的 AMF0 类型: ${JSON.stringify(i)}`);
122
+ },
123
+ /**
124
+ * 根据类型 ID 获取对应的 AMF0 类型
125
+ *
126
+ * @param id - 类型的数字标识符
127
+ * @returns 对应的 AMF0 类型
128
+ * @throws 如果找不到对应的类型则抛出错误
129
+ */
130
+ fromId(i) {
131
+ for (const e of Object.keys(h)) {
132
+ const t = h[e];
133
+ if (t instanceof s && t.id === i)
134
+ return t;
135
+ }
136
+ throw new Error(`没有 ID 为 ${i} 的 AMF0 类型`);
137
+ },
138
+ /** 数字类型 (0x00) - 64位双精度浮点数 */
139
+ NUMBER: new s(0, "NUMBER"),
140
+ /** 布尔类型 (0x01) */
141
+ BOOLEAN: new s(1, "BOOLEAN"),
142
+ /** 字符串类型 (0x02) - 最大 65535 字节 */
143
+ STRING: new s(2, "STRING"),
144
+ /** 对象类型 (0x03) - 匿名对象 */
145
+ OBJECT: new s(3, "OBJECT", !0),
146
+ /** MovieClip 类型 (0x04) - Flash 专用,通常不使用 */
147
+ MOVIECLIP: new s(4, "MOVIECLIP"),
148
+ /** 空值类型 (0x05) */
149
+ NULL: new s(5, "NULL"),
150
+ /** 未定义类型 (0x06) */
151
+ UNDEFINED: new s(6, "UNDEFINED"),
152
+ /** 引用类型 (0x07) - 引用之前序列化的对象 */
153
+ REFERENCE: new s(7, "REFERENCE"),
154
+ /** ECMA 数组类型 (0x08) - 关联数组/对象 */
155
+ ECMA_ARRAY: new s(8, "ECMA_ARRAY", !0),
156
+ /** 对象结束标记 (0x09) */
157
+ OBJECT_END: new s(9, "OBJECT_END"),
158
+ /** 严格数组类型 (0x0A) - 数字索引数组 */
159
+ STRICT_ARRAY: new s(10, "STRICT_ARRAY", !0),
160
+ /** 日期类型 (0x0B) */
161
+ DATE: new s(11, "DATE"),
162
+ /** 长字符串类型 (0x0C) - 超过 65535 字节的字符串 */
163
+ LONG_STRING: new s(12, "LONG_STRING"),
164
+ /** 不支持的类型 (0x0D) */
165
+ UNSUPPORTED: new s(13, "UNSUPPORTED"),
166
+ /** XML 类型 (0x0F) */
167
+ XML: new s(15, "XML"),
168
+ /** 类型化对象 (0x10) - 有类名的对象 */
169
+ TYPED_OBJECT: new s(16, "TYPED_OBJECT", !0),
170
+ /** AMF3 对象标记 (0x11) - 表示后续数据使用 AMF3 格式 */
171
+ AMF3_OBJECT: new s(17, "AMF3_OBJECT")
172
+ }, a = {
173
+ /**
174
+ * 根据 JavaScript 值推断对应的 AMF3 类型
175
+ *
176
+ * @param value - 要推断类型的值
177
+ * @returns 对应的 AMF3 类型
178
+ * @throws 如果无法推断类型则抛出错误
179
+ */
180
+ infer(i) {
181
+ const e = typeof i;
182
+ if (i === null)
183
+ return a.NULL;
184
+ if (e === "undefined")
185
+ return a.UNDEFINED;
186
+ if (i instanceof d)
187
+ return i.type;
188
+ if (e === "boolean" && !i)
189
+ return a.FALSE;
190
+ if (e === "boolean")
191
+ return a.TRUE;
192
+ if (e === "string")
193
+ return a.STRING;
194
+ if (e === "number" && isFinite(i) && i % 1 === 0 && i < 536870911 && i > -268435456)
195
+ return a.INTEGER;
196
+ if (e === "number")
197
+ return a.DOUBLE;
198
+ if (Object.prototype.toString.call(i) === "[object Date]")
199
+ return a.DATE;
200
+ if (i instanceof Uint8Array)
201
+ return a.BYTE_ARRAY;
202
+ if (i instanceof Array)
203
+ return a.ARRAY;
204
+ if (i instanceof u || i instanceof E)
205
+ return a.OBJECT;
206
+ if (e === "object")
207
+ return a.ARRAY;
208
+ throw new Error(`无法推断值的 AMF3 类型: ${JSON.stringify(i)}`);
209
+ },
210
+ /**
211
+ * 根据类型 ID 获取对应的 AMF3 类型
212
+ *
213
+ * @param id - 类型的数字标识符
214
+ * @returns 对应的 AMF3 类型
215
+ * @throws 如果找不到对应的类型则抛出错误
216
+ */
217
+ fromId(i) {
218
+ for (const e of Object.keys(a)) {
219
+ const t = a[e];
220
+ if (t instanceof s && t.id === i)
221
+ return t;
222
+ }
223
+ throw new Error(`没有 ID 为 ${i} 的 AMF3 类型`);
224
+ },
225
+ /** 未定义类型 (0x00) */
226
+ UNDEFINED: new s(0, "UNDEFINED"),
227
+ /** 空值类型 (0x01) */
228
+ NULL: new s(1, "NULL"),
229
+ /** 布尔假值 (0x02) */
230
+ FALSE: new s(2, "FALSE"),
231
+ /** 布尔真值 (0x03) */
232
+ TRUE: new s(3, "TRUE"),
233
+ /** 整数类型 (0x04) - 29位有符号整数 */
234
+ INTEGER: new s(4, "INTEGER"),
235
+ /** 双精度浮点数类型 (0x05) */
236
+ DOUBLE: new s(5, "DOUBLE"),
237
+ /** 字符串类型 (0x06) */
238
+ STRING: new s(6, "STRING", !0),
239
+ /** XML 文档类型 (0x07) */
240
+ XML_DOC: new s(7, "XML_DOC", !0),
241
+ /** 日期类型 (0x08) */
242
+ DATE: new s(8, "DATE", !0),
243
+ /** 数组类型 (0x09) */
244
+ ARRAY: new s(9, "ARRAY", !0),
245
+ /** 对象类型 (0x0A) */
246
+ OBJECT: new s(10, "OBJECT", !0),
247
+ /** XML 类型 (0x0B) */
248
+ XML: new s(11, "XML", !0),
249
+ /** 字节数组类型 (0x0C) */
250
+ BYTE_ARRAY: new s(12, "BYTE_ARRAY", !0),
251
+ /** 整数向量类型 (0x0D) */
252
+ VECTOR_INT: new s(13, "VECTOR_INT", !0),
253
+ /** 无符号整数向量类型 (0x0E) */
254
+ VECTOR_UINT: new s(14, "VECTOR_UINT", !0),
255
+ /** 双精度浮点数向量类型 (0x0F) */
256
+ VECTOR_DOUBLE: new s(15, "VECTOR_DOUBLE", !0),
257
+ /** 对象向量类型 (0x10) */
258
+ VECTOR_OBJECT: new s(16, "VECTOR_OBJECT", !0),
259
+ /** 字典类型 (0x11) */
260
+ DICTIONARY: new s(17, "DICTIONARY", !0)
261
+ };
262
+ class b {
263
+ /**
264
+ * 创建一个新的读取器
265
+ *
266
+ * @param data - 要读取的二进制数据
267
+ */
268
+ constructor(e) {
269
+ this.data = e, this.view = new DataView(e.buffer, e.byteOffset, e.byteLength), this.position = 0;
270
+ }
271
+ /**
272
+ * 获取当前读取位置
273
+ *
274
+ * @returns 当前位置(字节偏移量)
275
+ */
276
+ getPosition() {
277
+ return this.position;
278
+ }
279
+ /**
280
+ * 设置读取位置
281
+ *
282
+ * @param pos - 新的位置
283
+ */
284
+ setPosition(e) {
285
+ this.position = e;
286
+ }
287
+ /**
288
+ * 获取剩余可读字节数
289
+ *
290
+ * @returns 剩余字节数
291
+ */
292
+ getBytesAvailable() {
293
+ return this.data.length - this.position;
294
+ }
295
+ /**
296
+ * 检查是否还有足够的字节可读
297
+ *
298
+ * @param length - 需要的字节数
299
+ * @throws 如果剩余字节不足
300
+ */
301
+ checkAvailable(e) {
302
+ if (this.position + e > this.data.length)
303
+ throw new Error(`没有足够的 ${e} 字节可读,当前位置: ${this.position},总长度: ${this.data.length}`);
304
+ }
305
+ /**
306
+ * 读取一个或多个字节
307
+ *
308
+ * @param length - 要读取的字节数,默认为 1
309
+ * @param alwaysReturnArray - 即使只读取一个字节也返回 Uint8Array
310
+ * @returns 如果 length 为 1 且 alwaysReturnArray 为 false,返回单个数字;否则返回 Uint8Array
311
+ */
312
+ readByte(e = 1, t = !1) {
313
+ if (this.checkAvailable(e), e === 1 && !t)
314
+ return this.data[this.position++];
315
+ const r = this.data.slice(this.position, this.position + e);
316
+ return this.position += e, r;
317
+ }
318
+ /**
319
+ * 读取一个无符号 8 位整数
320
+ *
321
+ * @returns 0-255 范围内的整数
322
+ */
323
+ readUInt8() {
324
+ return this.checkAvailable(1), this.data[this.position++];
325
+ }
326
+ /**
327
+ * 读取一个有符号 8 位整数
328
+ *
329
+ * @returns -128 到 127 范围内的整数
330
+ */
331
+ readInt8() {
332
+ const e = this.readUInt8();
333
+ return e > 127 ? e - 256 : e;
334
+ }
335
+ /**
336
+ * 读取一个大端序无符号 16 位整数
337
+ *
338
+ * @returns 0-65535 范围内的整数
339
+ */
340
+ readUInt16BE() {
341
+ this.checkAvailable(2);
342
+ const e = this.view.getUint16(this.position, !1);
343
+ return this.position += 2, e;
344
+ }
345
+ /**
346
+ * 读取一个大端序有符号 16 位整数
347
+ *
348
+ * @returns -32768 到 32767 范围内的整数
349
+ */
350
+ readInt16BE() {
351
+ this.checkAvailable(2);
352
+ const e = this.view.getInt16(this.position, !1);
353
+ return this.position += 2, e;
354
+ }
355
+ /**
356
+ * 读取一个大端序无符号 32 位整数
357
+ *
358
+ * @returns 0-4294967295 范围内的整数
359
+ */
360
+ readUInt32BE() {
361
+ this.checkAvailable(4);
362
+ const e = this.view.getUint32(this.position, !1);
363
+ return this.position += 4, e;
364
+ }
365
+ /**
366
+ * 读取一个大端序有符号 32 位整数
367
+ *
368
+ * @returns -2147483648 到 2147483647 范围内的整数
369
+ */
370
+ readInt32BE() {
371
+ this.checkAvailable(4);
372
+ const e = this.view.getInt32(this.position, !1);
373
+ return this.position += 4, e;
374
+ }
375
+ /**
376
+ * 读取一个大端序 64 位双精度浮点数
377
+ *
378
+ * @returns 双精度浮点数
379
+ */
380
+ readDoubleBE() {
381
+ this.checkAvailable(8);
382
+ const e = this.view.getFloat64(this.position, !1);
383
+ return this.position += 8, e;
384
+ }
385
+ /**
386
+ * 读取一个 AMF0 格式的字符串
387
+ *
388
+ * AMF0 字符串格式:2字节长度 + UTF-8 编码的字符串数据
389
+ *
390
+ * @returns 解码后的字符串
391
+ */
392
+ readString() {
393
+ const e = this.readUInt16BE();
394
+ return e === 0 ? "" : this.readUTF8String(e);
395
+ }
396
+ /**
397
+ * 读取指定长度的 UTF-8 字符串
398
+ *
399
+ * @param length - 字符串的字节长度
400
+ * @returns 解码后的字符串
401
+ */
402
+ readUTF8String(e) {
403
+ this.checkAvailable(e);
404
+ const t = this.data.slice(this.position, this.position + e);
405
+ return this.position += e, new TextDecoder("utf-8").decode(t);
406
+ }
407
+ /**
408
+ * 读取 AMF3 的变长整数(Int29)
409
+ *
410
+ * AMF3 使用一种可变长度编码来表示 29 位有符号整数
411
+ * 每个字节的最高位表示是否还有更多字节:
412
+ * - 0xxxxxxx: 单字节,值 0-127
413
+ * - 1xxxxxxx 0xxxxxxx: 双字节
414
+ * - 1xxxxxxx 1xxxxxxx 0xxxxxxx: 三字节
415
+ * - 1xxxxxxx 1xxxxxxx 1xxxxxxx xxxxxxxx: 四字节(最后一个字节使用全部 8 位)
416
+ *
417
+ * @returns 29 位有符号整数,范围 -268435456 到 536870911
418
+ */
419
+ readInt29() {
420
+ let e = 0, t = this.readUInt8();
421
+ return t < 128 ? t : (e = (t & 127) << 7, t = this.readUInt8(), t < 128 ? e |= t : (e = (e | t & 127) << 7, t = this.readUInt8(), t < 128 ? e |= t : (e = (e | t & 127) << 8, e |= this.readUInt8())), -(e & 1 << 28) | e);
422
+ }
423
+ /**
424
+ * 读取 AMF3 头部信息
425
+ *
426
+ * AMF3 中很多类型使用一个头部来区分是定义还是引用:
427
+ * - 最低位为 0: 这是一个引用,高位存储引用索引
428
+ * - 最低位为 1: 这是一个定义,高位存储数据长度或其他信息
429
+ *
430
+ * @returns AMF 头部信息对象
431
+ */
432
+ readAMFHeader() {
433
+ const e = this.readInt29(), t = (e & 1) !== 0, r = e >> 1;
434
+ return {
435
+ isDef: t,
436
+ value: r
437
+ };
438
+ }
439
+ }
440
+ const w = class w extends b {
441
+ /**
442
+ * 注册外部化类型
443
+ */
444
+ static register(e, t) {
445
+ this.amf3Externalizables[e] = t;
446
+ }
447
+ constructor(e) {
448
+ super(e), this.amf3StringReferences = [], this.amf3ObjectReferences = [], this.amf3TraitReferences = [];
449
+ }
450
+ /**
451
+ * 解码一个 AMF3 值
452
+ */
453
+ decode() {
454
+ const e = this.readUInt8();
455
+ return this.readByTypeId(e);
456
+ }
457
+ /**
458
+ * 按 AMF3 类型对象解码(兼容接口)
459
+ */
460
+ deserialize(e) {
461
+ return e instanceof s ? this.readByTypeId(e.id) : this.readByTypeId(e);
462
+ }
463
+ /**
464
+ * 按类型 ID 分发解码
465
+ */
466
+ readByTypeId(e) {
467
+ switch (e) {
468
+ case 0:
469
+ return;
470
+ case 1:
471
+ return null;
472
+ case 2:
473
+ return !1;
474
+ case 3:
475
+ return !0;
476
+ case 4:
477
+ return this.readInt29();
478
+ case 5:
479
+ return this.readDoubleBE();
480
+ case 6:
481
+ return this.readAMF3String();
482
+ case 8:
483
+ return this.readAMF3Date();
484
+ case 9:
485
+ return this.readAMF3Array();
486
+ case 10:
487
+ return this.readAMF3Object();
488
+ case 12:
489
+ return this.readAMF3ByteArray();
490
+ case 13:
491
+ return this.readAMF3VectorInt();
492
+ case 14:
493
+ return this.readAMF3VectorUInt();
494
+ case 15:
495
+ return this.readAMF3VectorDouble();
496
+ case 16:
497
+ return this.readAMF3VectorObject();
498
+ case 17:
499
+ return this.readAMF3Dictionary();
500
+ default:
501
+ throw new Error("当前 AMF3 解码器暂不支持类型 ID: " + e);
502
+ }
503
+ }
504
+ /**
505
+ * 读取 AMF3 字符串
506
+ *
507
+ * todo 字符串过长时可能有bug? 当前读长读,用的是u29的方式
508
+ */
509
+ readAMF3String() {
510
+ const e = this.readAMFHeader();
511
+ if (!e.isDef) {
512
+ const r = this.amf3StringReferences[e.value];
513
+ if (typeof r != "string")
514
+ throw new Error("无效的 AMF3 字符串引用");
515
+ return r;
516
+ }
517
+ if (e.value === 0)
518
+ return "";
519
+ const t = this.readUTF8String(e.value);
520
+ return this.amf3StringReferences.push(t), t;
521
+ }
522
+ /**
523
+ * 读取 AMF3 日期
524
+ */
525
+ readAMF3Date() {
526
+ const e = this.readAMFHeader();
527
+ if (!e.isDef) {
528
+ const r = this.amf3ObjectReferences[e.value];
529
+ if (!(r instanceof Date))
530
+ throw new Error("无效的 AMF3 日期引用");
531
+ return r;
532
+ }
533
+ const t = new Date(this.readDoubleBE());
534
+ return this.amf3ObjectReferences.push(t), t;
535
+ }
536
+ /**
537
+ * 读取 AMF3 数组
538
+ */
539
+ readAMF3Array() {
540
+ const e = this.readAMFHeader();
541
+ if (!e.isDef) {
542
+ const c = this.amf3ObjectReferences[e.value];
543
+ if (!c)
544
+ throw new Error("无效的 AMF3 数组引用");
545
+ return c;
546
+ }
547
+ const t = {};
548
+ this.amf3ObjectReferences.push(t);
549
+ const r = this.amf3ObjectReferences.length - 1;
550
+ for (; ; ) {
551
+ const c = this.readAMF3String();
552
+ if (c === "")
553
+ break;
554
+ t[c] = this.decode();
555
+ }
556
+ if (Object.keys(t).length > 0)
557
+ return t;
558
+ const n = [];
559
+ this.amf3ObjectReferences[r] = n;
560
+ for (let c = 0; c < e.value; c++)
561
+ n.push(this.decode());
562
+ return n;
563
+ }
564
+ /**
565
+ * 读取 AMF3 对象 Trait
566
+ */
567
+ readAMF3ObjectTrait(e) {
568
+ if (!(e & 1)) {
569
+ const f = this.amf3TraitReferences[e >> 1];
570
+ if (!f)
571
+ throw new Error("无效的 AMF3 Trait 引用");
572
+ return f;
573
+ }
574
+ const t = this.readAMF3String(), r = (e >> 1 & 1) === 1, n = (e >> 2 & 1) === 1, c = e >> 3, o = new y(t, n, r);
575
+ for (let f = 0; f < c; f++)
576
+ o.staticFields.push(this.readAMF3String());
577
+ return this.amf3TraitReferences.push(o), o;
578
+ }
579
+ /**
580
+ * 读取 AMF3 对象
581
+ */
582
+ readAMF3Object() {
583
+ const e = this.readAMFHeader();
584
+ if (!e.isDef) {
585
+ const n = this.amf3ObjectReferences[e.value];
586
+ if (!n)
587
+ throw new Error("无效的 AMF3 对象引用");
588
+ return n;
589
+ }
590
+ const t = this.readAMF3ObjectTrait(e.value);
591
+ if (t.externalizable) {
592
+ if (t.name === "flex.messaging.io.ArrayCollection") {
593
+ const o = this.decode();
594
+ return this.amf3ObjectReferences.push(o), o;
595
+ }
596
+ const n = w.amf3Externalizables[t.name];
597
+ if (!n)
598
+ throw new Error("未注册 AMF3 外部化类型: " + t.name);
599
+ const c = n.read(this);
600
+ return this.amf3ObjectReferences.push(c), c;
601
+ }
602
+ const r = new u(t.name || void 0);
603
+ this.amf3ObjectReferences.push(r);
604
+ for (let n = 0; n < t.staticFields.length; n++) {
605
+ const c = t.staticFields[n];
606
+ r[c] = this.decode();
607
+ }
608
+ if (t.dynamic)
609
+ for (; ; ) {
610
+ const n = this.readAMF3String();
611
+ if (n === "")
612
+ break;
613
+ r[n] = this.decode();
614
+ }
615
+ return r;
616
+ }
617
+ /**
618
+ * 读取 AMF3 ByteArray
619
+ */
620
+ readAMF3ByteArray() {
621
+ const e = this.readAMFHeader();
622
+ if (!e.isDef) {
623
+ const r = this.amf3ObjectReferences[e.value];
624
+ if (!(r instanceof Uint8Array))
625
+ throw new Error("无效的 AMF3 ByteArray 引用");
626
+ return r;
627
+ }
628
+ const t = this.readByte(e.value, !0);
629
+ return this.amf3ObjectReferences.push(t), t;
630
+ }
631
+ /**
632
+ * 向量解码通用逻辑
633
+ */
634
+ readAMF3Vector(e) {
635
+ const t = this.readAMFHeader();
636
+ if (!t.isDef) {
637
+ const n = this.amf3ObjectReferences[t.value];
638
+ if (!n)
639
+ throw new Error("无效的 AMF3 向量引用");
640
+ return n;
641
+ }
642
+ this.readUInt8();
643
+ const r = [];
644
+ this.amf3ObjectReferences.push(r);
645
+ for (let n = 0; n < t.value; n++)
646
+ r.push(e.call(this));
647
+ return r;
648
+ }
649
+ /**
650
+ * 读取 VECTOR_INT
651
+ */
652
+ readAMF3VectorInt() {
653
+ return this.readAMF3Vector(function() {
654
+ return this.readInt32BE();
655
+ });
656
+ }
657
+ /**
658
+ * 读取 VECTOR_UINT
659
+ */
660
+ readAMF3VectorUInt() {
661
+ return this.readAMF3Vector(function() {
662
+ return this.readUInt32BE();
663
+ });
664
+ }
665
+ /**
666
+ * 读取 VECTOR_DOUBLE
667
+ */
668
+ readAMF3VectorDouble() {
669
+ return this.readAMF3Vector(function() {
670
+ return this.readDoubleBE();
671
+ });
672
+ }
673
+ /**
674
+ * 读取 VECTOR_OBJECT
675
+ */
676
+ readAMF3VectorObject() {
677
+ return this.readAMF3Vector(function() {
678
+ return this.decode();
679
+ });
680
+ }
681
+ /**
682
+ * 读取 DICTIONARY
683
+ */
684
+ readAMF3Dictionary() {
685
+ const e = this.readAMFHeader();
686
+ if (!e.isDef) {
687
+ const r = this.amf3ObjectReferences[e.value];
688
+ if (!r)
689
+ throw new Error("无效的 AMF3 字典引用");
690
+ return r;
691
+ }
692
+ this.readUInt8();
693
+ const t = {};
694
+ this.amf3ObjectReferences.push(t);
695
+ for (let r = 0; r < e.value; r++) {
696
+ const n = this.decode();
697
+ t[JSON.stringify(n)] = this.decode();
698
+ }
699
+ return t;
700
+ }
701
+ };
702
+ w.amf3Externalizables = {};
703
+ let A = w;
704
+ class I {
705
+ /**
706
+ * 创建一个新的写入器
707
+ */
708
+ constructor() {
709
+ this.chunks = [], this.totalLength = 0;
710
+ }
711
+ /**
712
+ * 获取当前已写入的字节数
713
+ *
714
+ * @returns 已写入的总字节数
715
+ */
716
+ getLength() {
717
+ return this.totalLength;
718
+ }
719
+ /**
720
+ * 获取写入的所有数据
721
+ *
722
+ * 将所有写入的数据块合并为一个 Uint8Array
723
+ *
724
+ * @returns 包含所有写入数据的 Uint8Array
725
+ */
726
+ getBuffer() {
727
+ const e = new Uint8Array(this.totalLength);
728
+ let t = 0;
729
+ for (const r of this.chunks)
730
+ e.set(r, t), t += r.length;
731
+ return e;
732
+ }
733
+ /**
734
+ * 清空写入器,重置到初始状态
735
+ */
736
+ clear() {
737
+ this.chunks = [], this.totalLength = 0;
738
+ }
739
+ /**
740
+ * 写入数据到缓冲区
741
+ *
742
+ * 支持多种数据类型:
743
+ * - 数字(0-255):作为单个字节写入
744
+ * - 数字数组:每个元素作为一个字节写入
745
+ * - Uint8Array:直接写入
746
+ * - 字符串:作为 UTF-8 编码写入
747
+ *
748
+ * @param value - 要写入的值
749
+ * @throws 如果值的类型不支持
750
+ */
751
+ write(e) {
752
+ if (typeof e == "number") {
753
+ const t = new Uint8Array([e & 255]);
754
+ this.chunks.push(t), this.totalLength += 1;
755
+ } else if (Array.isArray(e)) {
756
+ const t = new Uint8Array(e.map((r) => r & 255));
757
+ this.chunks.push(t), this.totalLength += t.length;
758
+ } else if (e instanceof Uint8Array)
759
+ this.chunks.push(e), this.totalLength += e.length;
760
+ else if (typeof e == "string") {
761
+ const r = new TextEncoder().encode(e);
762
+ this.chunks.push(r), this.totalLength += r.length;
763
+ } else
764
+ throw new Error(`不知道如何写入: ${JSON.stringify(e)}`);
765
+ }
766
+ /**
767
+ * 写入单个字节
768
+ *
769
+ * @param value - 0-255 范围内的整数
770
+ */
771
+ writeByte(e) {
772
+ this.write(e & 255);
773
+ }
774
+ /**
775
+ * 写入字节数组
776
+ *
777
+ * @param bytes - 字节数组或 Uint8Array
778
+ */
779
+ writeBytes(e) {
780
+ this.write(e);
781
+ }
782
+ /**
783
+ * 写入一个大端序无符号 16 位整数
784
+ *
785
+ * @param value - 0-65535 范围内的整数
786
+ */
787
+ writeUInt16BE(e) {
788
+ const t = new Uint8Array(2);
789
+ new DataView(t.buffer).setUint16(0, e, !1), this.write(t);
790
+ }
791
+ /**
792
+ * 写入一个大端序有符号 16 位整数
793
+ *
794
+ * @param value - -32768 到 32767 范围内的整数
795
+ */
796
+ writeInt16BE(e) {
797
+ const t = new Uint8Array(2);
798
+ new DataView(t.buffer).setInt16(0, e, !1), this.write(t);
799
+ }
800
+ /**
801
+ * 写入一个大端序无符号 32 位整数
802
+ *
803
+ * @param value - 0-4294967295 范围内的整数
804
+ */
805
+ writeUInt32BE(e) {
806
+ const t = new Uint8Array(4);
807
+ new DataView(t.buffer).setUint32(0, e, !1), this.write(t);
808
+ }
809
+ /**
810
+ * 写入一个大端序有符号 32 位整数
811
+ *
812
+ * @param value - -2147483648 到 2147483647 范围内的整数
813
+ */
814
+ writeInt32BE(e) {
815
+ const t = new Uint8Array(4);
816
+ new DataView(t.buffer).setInt32(0, e, !1), this.write(t);
817
+ }
818
+ /**
819
+ * 写入一个大端序 64 位双精度浮点数
820
+ *
821
+ * @param value - 双精度浮点数
822
+ */
823
+ writeDoubleBE(e) {
824
+ const t = new Uint8Array(8);
825
+ new DataView(t.buffer).setFloat64(0, e, !1), this.write(t);
826
+ }
827
+ /**
828
+ * 写入一个 AMF0 格式的字符串
829
+ *
830
+ * AMF0 字符串格式:2字节长度(大端序)+ UTF-8 编码的字符串数据
831
+ *
832
+ * @param value - 要写入的字符串
833
+ */
834
+ writeString(e) {
835
+ const r = new TextEncoder().encode(e);
836
+ this.writeUInt16BE(r.length), this.write(r);
837
+ }
838
+ /**
839
+ * 写入 AMF3 的变长整数(Int29)
840
+ *
841
+ * AMF3 使用一种可变长度编码来表示 29 位有符号整数
842
+ * 每个字节的最高位表示是否还有更多字节:
843
+ * - 值 0-127: 单字节 (0xxxxxxx)
844
+ * - 值 128-16383: 双字节 (1xxxxxxx 0xxxxxxx)
845
+ * - 值 16384-2097151: 三字节 (1xxxxxxx 1xxxxxxx 0xxxxxxx)
846
+ * - 值 2097152-536870911: 四字节 (1xxxxxxx 1xxxxxxx 1xxxxxxx xxxxxxxx)
847
+ *
848
+ * @param value - 29 位有符号整数,范围 -268435456 到 536870911
849
+ * @throws 如果值超出范围
850
+ */
851
+ writeInt29(e) {
852
+ if (e > 536870911 || e < -268435456)
853
+ throw new RangeError(`Int29 值超出范围: ${e}`);
854
+ e &= 536870911, e < 128 ? this.write(e) : e < 16384 ? this.write([
855
+ e >> 7 & 127 | 128,
856
+ e & 127
857
+ ]) : e < 2097152 ? this.write([
858
+ e >> 14 & 127 | 128,
859
+ e >> 7 & 127 | 128,
860
+ e & 127
861
+ ]) : this.write([
862
+ e >> 22 & 127 | 128,
863
+ e >> 14 & 127 | 128,
864
+ e >> 7 & 127 | 128,
865
+ e & 255
866
+ ]);
867
+ }
868
+ }
869
+ function R(i) {
870
+ return (typeof i.getSerializableFields == "function" ? i.getSerializableFields() : Object.keys(i)).filter(function(r) {
871
+ return r.indexOf("__") !== 0;
872
+ });
873
+ }
874
+ class O extends I {
875
+ constructor() {
876
+ super(), this.amf3ObjectReferences = [], this.amf3StringReferences = [];
877
+ }
878
+ /**
879
+ * 编码一个 AMF3 值(会写入类型标记)
880
+ */
881
+ writeObject(e) {
882
+ this.encodeValue(e);
883
+ }
884
+ /**
885
+ * 兼容旧接口,含义与 writeObject 相同
886
+ */
887
+ encode(e) {
888
+ this.encodeValue(e);
889
+ }
890
+ /**
891
+ * 编码值主体(无类型标记)
892
+ */
893
+ serialize(e) {
894
+ const t = this.inferType(e);
895
+ this.writeByType(e instanceof d ? e.value : e, t);
896
+ }
897
+ /**
898
+ * 推断值的 AMF3 类型
899
+ */
900
+ inferType(e) {
901
+ return a.infer(e);
902
+ }
903
+ /**
904
+ * 编码一个完整值(含类型标记与引用处理)
905
+ */
906
+ encodeValue(e) {
907
+ const t = this.inferType(e), r = e instanceof d ? e.value : e;
908
+ if (t.referencable && r !== "") {
909
+ let n = -1;
910
+ if (t === a.STRING ? n = this.amf3StringReferences.indexOf(r) : n = this.amf3ObjectReferences.indexOf(r), n !== -1) {
911
+ this.writeByte(t.id), this.writeInt29(n << 1);
912
+ return;
913
+ }
914
+ t === a.STRING ? this.amf3StringReferences.push(r) : this.amf3ObjectReferences.push(r);
915
+ }
916
+ this.writeByte(t.id), this.writeByType(r, t);
917
+ }
918
+ /**
919
+ * 按类型写入值主体(不包含类型标记)
920
+ */
921
+ writeByType(e, t) {
922
+ switch (t.id) {
923
+ case 0:
924
+ case 1:
925
+ case 2:
926
+ case 3:
927
+ return;
928
+ case 4:
929
+ this.writeInt29(e);
930
+ return;
931
+ case 5:
932
+ this.writeDoubleBE(e);
933
+ return;
934
+ case 6:
935
+ this.writeInlineString(e);
936
+ return;
937
+ case 8:
938
+ this.writeInt29(1), this.writeDoubleBE(e.getTime());
939
+ return;
940
+ case 9:
941
+ this.writeArray(e);
942
+ return;
943
+ case 10:
944
+ this.writeObjectValue(e);
945
+ return;
946
+ case 12:
947
+ this.writeByteArray(e);
948
+ return;
949
+ default:
950
+ throw new Error("当前 AMF3 编码器暂不支持该类型: " + t.name);
951
+ }
952
+ }
953
+ /**
954
+ * 写入 AMF3 内联字符串(无类型标记)
955
+ *
956
+ * 说明:
957
+ * 这里保持与原 lib 行为一致,不在“无类型字符串”分支中做字符串引用表复用。
958
+ */
959
+ writeInlineString(e) {
960
+ const t = new TextEncoder().encode(e);
961
+ this.writeInt29(t.length << 1 | 1), this.write(t);
962
+ }
963
+ /**
964
+ * 写入 AMF3 数组(密集数组或关联数组)
965
+ */
966
+ writeArray(e) {
967
+ if (Array.isArray(e)) {
968
+ this.writeInt29(e.length << 1 | 1), this.writeInlineString("");
969
+ for (let r = 0; r < e.length; r++)
970
+ this.encodeValue(e[r]);
971
+ return;
972
+ }
973
+ this.writeInt29(1);
974
+ const t = Object.keys(e);
975
+ for (let r = 0; r < t.length; r++) {
976
+ const n = t[r];
977
+ n.indexOf("__") !== 0 && (this.writeInlineString(n), this.encodeValue(e[n]));
978
+ }
979
+ this.writeInlineString("");
980
+ }
981
+ /**
982
+ * 写入 AMF3 对象
983
+ */
984
+ writeObjectValue(e) {
985
+ if (e.__dynamic !== !1 && !e.__class) {
986
+ this.writeInt29(11), this.writeInt29(1);
987
+ const o = Object.keys(e);
988
+ for (let f = 0; f < o.length; f++) {
989
+ const l = o[f];
990
+ l.indexOf("__") !== 0 && (this.writeInlineString(l), this.encodeValue(e[l]));
991
+ }
992
+ this.writeInlineString("");
993
+ return;
994
+ }
995
+ const r = e instanceof E, n = r ? [] : R(e);
996
+ let c = n.length << 4;
997
+ if (c |= (r ? 1 : 0) << 2, c = c | 2 | 1, this.writeInt29(c), this.writeInlineString(e.__class || ""), r) {
998
+ e.write(this);
999
+ return;
1000
+ }
1001
+ for (let o = 0; o < n.length; o++)
1002
+ this.writeInlineString(n[o]);
1003
+ for (let o = 0; o < n.length; o++)
1004
+ this.encodeValue(e[n[o]]);
1005
+ }
1006
+ /**
1007
+ * 写入 AMF3 ByteArray
1008
+ */
1009
+ writeByteArray(e) {
1010
+ this.writeInt29(e.length << 1 | 1), this.write(e);
1011
+ }
1012
+ }
1013
+ export {
1014
+ h as AMF0,
1015
+ a as AMF3,
1016
+ A as AMFDecoder,
1017
+ O as AMFEncoder,
1018
+ y as AMFTrait,
1019
+ s as AMFType,
1020
+ E as Externalizable,
1021
+ d as ForcedTypeValue,
1022
+ u as Serializable
1023
+ };
1024
+ //# sourceMappingURL=amf.js.map