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.
@@ -0,0 +1,795 @@
1
+ /**
2
+ * AMF0 类型定义
3
+ *
4
+ * AMF0 是 AMF 协议的第一个版本
5
+ * 支持基本的数据类型如数字、字符串、对象、数组等
6
+ */
7
+ export declare const AMF0: {
8
+ /**
9
+ * 根据 JavaScript 值推断对应的 AMF0 类型
10
+ *
11
+ * @param value - 要推断类型的值
12
+ * @returns 对应的 AMF0 类型
13
+ * @throws 如果无法推断类型则抛出错误
14
+ */
15
+ infer(value: any): AMFType;
16
+ /**
17
+ * 根据类型 ID 获取对应的 AMF0 类型
18
+ *
19
+ * @param id - 类型的数字标识符
20
+ * @returns 对应的 AMF0 类型
21
+ * @throws 如果找不到对应的类型则抛出错误
22
+ */
23
+ fromId(id: number): AMFType;
24
+ /** 数字类型 (0x00) - 64位双精度浮点数 */
25
+ NUMBER: AMFType;
26
+ /** 布尔类型 (0x01) */
27
+ BOOLEAN: AMFType;
28
+ /** 字符串类型 (0x02) - 最大 65535 字节 */
29
+ STRING: AMFType;
30
+ /** 对象类型 (0x03) - 匿名对象 */
31
+ OBJECT: AMFType;
32
+ /** MovieClip 类型 (0x04) - Flash 专用,通常不使用 */
33
+ MOVIECLIP: AMFType;
34
+ /** 空值类型 (0x05) */
35
+ NULL: AMFType;
36
+ /** 未定义类型 (0x06) */
37
+ UNDEFINED: AMFType;
38
+ /** 引用类型 (0x07) - 引用之前序列化的对象 */
39
+ REFERENCE: AMFType;
40
+ /** ECMA 数组类型 (0x08) - 关联数组/对象 */
41
+ ECMA_ARRAY: AMFType;
42
+ /** 对象结束标记 (0x09) */
43
+ OBJECT_END: AMFType;
44
+ /** 严格数组类型 (0x0A) - 数字索引数组 */
45
+ STRICT_ARRAY: AMFType;
46
+ /** 日期类型 (0x0B) */
47
+ DATE: AMFType;
48
+ /** 长字符串类型 (0x0C) - 超过 65535 字节的字符串 */
49
+ LONG_STRING: AMFType;
50
+ /** 不支持的类型 (0x0D) */
51
+ UNSUPPORTED: AMFType;
52
+ /** XML 类型 (0x0F) */
53
+ XML: AMFType;
54
+ /** 类型化对象 (0x10) - 有类名的对象 */
55
+ TYPED_OBJECT: AMFType;
56
+ /** AMF3 对象标记 (0x11) - 表示后续数据使用 AMF3 格式 */
57
+ AMF3_OBJECT: AMFType;
58
+ };
59
+
60
+ /**
61
+ * AMF3 类型定义
62
+ *
63
+ * AMF3 是 AMF 协议的第三个版本(跳过了 AMF2)
64
+ * 相比 AMF0,它更加紧凑,支持更多的数据类型
65
+ * 包括字节数组、向量等
66
+ */
67
+ export declare const AMF3: {
68
+ /**
69
+ * 根据 JavaScript 值推断对应的 AMF3 类型
70
+ *
71
+ * @param value - 要推断类型的值
72
+ * @returns 对应的 AMF3 类型
73
+ * @throws 如果无法推断类型则抛出错误
74
+ */
75
+ infer(value: any): AMFType;
76
+ /**
77
+ * 根据类型 ID 获取对应的 AMF3 类型
78
+ *
79
+ * @param id - 类型的数字标识符
80
+ * @returns 对应的 AMF3 类型
81
+ * @throws 如果找不到对应的类型则抛出错误
82
+ */
83
+ fromId(id: number): AMFType;
84
+ /** 未定义类型 (0x00) */
85
+ UNDEFINED: AMFType;
86
+ /** 空值类型 (0x01) */
87
+ NULL: AMFType;
88
+ /** 布尔假值 (0x02) */
89
+ FALSE: AMFType;
90
+ /** 布尔真值 (0x03) */
91
+ TRUE: AMFType;
92
+ /** 整数类型 (0x04) - 29位有符号整数 */
93
+ INTEGER: AMFType;
94
+ /** 双精度浮点数类型 (0x05) */
95
+ DOUBLE: AMFType;
96
+ /** 字符串类型 (0x06) */
97
+ STRING: AMFType;
98
+ /** XML 文档类型 (0x07) */
99
+ XML_DOC: AMFType;
100
+ /** 日期类型 (0x08) */
101
+ DATE: AMFType;
102
+ /** 数组类型 (0x09) */
103
+ ARRAY: AMFType;
104
+ /** 对象类型 (0x0A) */
105
+ OBJECT: AMFType;
106
+ /** XML 类型 (0x0B) */
107
+ XML: AMFType;
108
+ /** 字节数组类型 (0x0C) */
109
+ BYTE_ARRAY: AMFType;
110
+ /** 整数向量类型 (0x0D) */
111
+ VECTOR_INT: AMFType;
112
+ /** 无符号整数向量类型 (0x0E) */
113
+ VECTOR_UINT: AMFType;
114
+ /** 双精度浮点数向量类型 (0x0F) */
115
+ VECTOR_DOUBLE: AMFType;
116
+ /** 对象向量类型 (0x10) */
117
+ VECTOR_OBJECT: AMFType;
118
+ /** 字典类型 (0x11) */
119
+ DICTIONARY: AMFType;
120
+ };
121
+
122
+ /**
123
+ * AMF3 解码器
124
+ */
125
+ export declare class AMFDecoder extends Reader {
126
+ /** AMF3 外部化对象注册表 */
127
+ static amf3Externalizables: Record<string, ExternalizableReader>;
128
+ /** AMF3 字符串引用表 */
129
+ private amf3StringReferences;
130
+ /** AMF3 对象引用表 */
131
+ private amf3ObjectReferences;
132
+ /** AMF3 Trait 引用表 */
133
+ private amf3TraitReferences;
134
+ /**
135
+ * 注册外部化类型
136
+ */
137
+ static register(className: string, cls: ExternalizableReader): void;
138
+ constructor(data: Uint8Array);
139
+ /**
140
+ * 解码一个 AMF3 值
141
+ */
142
+ decode(): any;
143
+ /**
144
+ * 按 AMF3 类型对象解码(兼容接口)
145
+ */
146
+ deserialize(type: number | AMFType): any;
147
+ /**
148
+ * 按类型 ID 分发解码
149
+ */
150
+ private readByTypeId;
151
+ /**
152
+ * 读取 AMF3 字符串
153
+ *
154
+ * todo 字符串过长时可能有bug? 当前读长读,用的是u29的方式
155
+ */
156
+ private readAMF3String;
157
+ /**
158
+ * 读取 AMF3 日期
159
+ */
160
+ private readAMF3Date;
161
+ /**
162
+ * 读取 AMF3 数组
163
+ */
164
+ private readAMF3Array;
165
+ /**
166
+ * 读取 AMF3 对象 Trait
167
+ */
168
+ private readAMF3ObjectTrait;
169
+ /**
170
+ * 读取 AMF3 对象
171
+ */
172
+ private readAMF3Object;
173
+ /**
174
+ * 读取 AMF3 ByteArray
175
+ */
176
+ private readAMF3ByteArray;
177
+ /**
178
+ * 向量解码通用逻辑
179
+ */
180
+ private readAMF3Vector;
181
+ /**
182
+ * 读取 VECTOR_INT
183
+ */
184
+ private readAMF3VectorInt;
185
+ /**
186
+ * 读取 VECTOR_UINT
187
+ */
188
+ private readAMF3VectorUInt;
189
+ /**
190
+ * 读取 VECTOR_DOUBLE
191
+ */
192
+ private readAMF3VectorDouble;
193
+ /**
194
+ * 读取 VECTOR_OBJECT
195
+ */
196
+ private readAMF3VectorObject;
197
+ /**
198
+ * 读取 DICTIONARY
199
+ */
200
+ private readAMF3Dictionary;
201
+ }
202
+
203
+ /**
204
+ * AMF3 编码器
205
+ */
206
+ export declare class AMFEncoder extends Writer {
207
+ /** AMF3 对象引用表 */
208
+ private amf3ObjectReferences;
209
+ /** AMF3 字符串引用表 */
210
+ private amf3StringReferences;
211
+ constructor();
212
+ /**
213
+ * 编码一个 AMF3 值(会写入类型标记)
214
+ */
215
+ writeObject(value: any): void;
216
+ /**
217
+ * 兼容旧接口,含义与 writeObject 相同
218
+ */
219
+ encode(value: any): void;
220
+ /**
221
+ * 编码值主体(无类型标记)
222
+ */
223
+ serialize(value: any): void;
224
+ /**
225
+ * 推断值的 AMF3 类型
226
+ */
227
+ private inferType;
228
+ /**
229
+ * 编码一个完整值(含类型标记与引用处理)
230
+ */
231
+ private encodeValue;
232
+ /**
233
+ * 按类型写入值主体(不包含类型标记)
234
+ */
235
+ private writeByType;
236
+ /**
237
+ * 写入 AMF3 内联字符串(无类型标记)
238
+ *
239
+ * 说明:
240
+ * 这里保持与原 lib 行为一致,不在“无类型字符串”分支中做字符串引用表复用。
241
+ */
242
+ private writeInlineString;
243
+ /**
244
+ * 写入 AMF3 数组(密集数组或关联数组)
245
+ */
246
+ private writeArray;
247
+ /**
248
+ * 写入 AMF3 对象
249
+ */
250
+ private writeObjectValue;
251
+ /**
252
+ * 写入 AMF3 ByteArray
253
+ */
254
+ private writeByteArray;
255
+ }
256
+
257
+ /**
258
+ * 二进制数据读取器模块
259
+ *
260
+ * 本模块提供了从二进制数据中读取各种数据类型的功能
261
+ * 专为浏览器环境设计,使用 Uint8Array 和 DataView 替代 Node.js 的 Buffer
262
+ */
263
+ /**
264
+ * AMF 头部信息接口
265
+ *
266
+ * 在 AMF3 中,许多类型使用一个特殊的头部来表示是定义还是引用
267
+ */
268
+ declare interface AMFHeader {
269
+ /** 是否是定义(true)还是引用(false) */
270
+ isDef: boolean;
271
+ /** 头部的值(引用索引或数据长度) */
272
+ value: number;
273
+ }
274
+
275
+ /**
276
+ * AMF3 对象特征类
277
+ *
278
+ * 用于描述 AMF3 对象的元数据信息
279
+ * 包括类名、是否为动态对象、是否可外部化、以及静态字段列表
280
+ *
281
+ * 在 AMF3 中,对象特征可以被引用以减少数据大小
282
+ * 当多个相同类型的对象被序列化时,只需要序列化一次特征信息
283
+ */
284
+ export declare class AMFTrait {
285
+ /** 对象的类名(可以为空表示匿名对象) */
286
+ name: string;
287
+ /**
288
+ * 是否为动态对象
289
+ * 动态对象可以有任意的属性,而非动态对象只能有预定义的静态属性
290
+ */
291
+ dynamic: boolean;
292
+ /**
293
+ * 是否可外部化
294
+ * 可外部化对象需要自己控制序列化和反序列化过程
295
+ */
296
+ externalizable: boolean;
297
+ /** 静态字段名列表 */
298
+ staticFields: string[];
299
+ /**
300
+ * 创建一个 AMF3 对象特征
301
+ *
302
+ * @param name - 对象的类名
303
+ * @param dynamic - 是否为动态对象
304
+ * @param externalizable - 是否可外部化
305
+ */
306
+ constructor(name: string, dynamic: boolean, externalizable: boolean);
307
+ }
308
+
309
+ /**
310
+ * AMF 类型定义模块
311
+ *
312
+ * 本模块定义了 AMF0 和 AMF3 协议中所有的数据类型
313
+ * AMF (Action Message Format) 是 Adobe 开发的一种二进制数据序列化格式
314
+ * 主要用于 Flash/Flex 应用与服务器之间的数据交换
315
+ */
316
+ /**
317
+ * AMF 类型基类
318
+ *
319
+ * 每种 AMF 数据类型都由这个类的实例表示
320
+ * 包含类型的 ID、名称、是否可引用等信息
321
+ * 以及编码和解码的方法
322
+ */
323
+ export declare class AMFType {
324
+ /** 类型的数字标识符 */
325
+ id: number;
326
+ /** 类型的名称(用于调试和错误信息) */
327
+ name: string;
328
+ /**
329
+ * 是否可引用
330
+ * 某些复杂类型(如对象、数组)在 AMF 中可以被引用
331
+ * 以避免重复序列化相同的对象
332
+ */
333
+ referencable: boolean;
334
+ /**
335
+ * 编码函数
336
+ * 由具体的类型实现设置
337
+ * this 上下文指向 AMFEncoder 实例
338
+ */
339
+ encode: (this: any, value: any) => void;
340
+ /**
341
+ * 解码函数
342
+ * 由具体的类型实现设置
343
+ * this 上下文指向 AMFDecoder 实例
344
+ */
345
+ decode: (this: any) => any;
346
+ /**
347
+ * 创建一个新的 AMF 类型
348
+ *
349
+ * @param id - 类型的数字标识符
350
+ * @param name - 类型的名称
351
+ * @param referencable - 是否可引用,默认为 false
352
+ */
353
+ constructor(id: number, name: string, referencable?: boolean);
354
+ }
355
+
356
+ /**
357
+ * 可外部化对象基类
358
+ *
359
+ * 继承此类的对象可以完全控制自己的序列化和反序列化过程。
360
+ * 必须实现 write() 实例方法和 read() 静态方法。
361
+ *
362
+ * 可外部化对象需要通过 AMFDecoder.register() 注册,
363
+ * 以便解码器知道如何反序列化它们。
364
+ *
365
+ * @example
366
+ * ```typescript
367
+ * class CustomData extends Externalizable {
368
+ * public data: Uint8Array;
369
+ *
370
+ * constructor() {
371
+ * super('com.example.CustomData');
372
+ * this.data = new Uint8Array(0);
373
+ * }
374
+ *
375
+ * // 自定义序列化
376
+ * write(encoder: AMFEncoder): void {
377
+ * encoder.writeObject(this.data.length);
378
+ * for (const byte of this.data) {
379
+ * encoder.writeObject(byte);
380
+ * }
381
+ * }
382
+ *
383
+ * // 自定义反序列化(静态方法)
384
+ * static read(decoder: AMFDecoder): CustomData {
385
+ * const instance = new CustomData();
386
+ * const length = decoder.decode();
387
+ * instance.data = new Uint8Array(length);
388
+ * for (let i = 0; i < length; i++) {
389
+ * instance.data[i] = decoder.decode();
390
+ * }
391
+ * return instance;
392
+ * }
393
+ * }
394
+ * ```
395
+ */
396
+ export declare class Externalizable extends Serializable {
397
+ /**
398
+ * 创建一个可外部化对象
399
+ *
400
+ * @param externalizableName - 对象的类名
401
+ */
402
+ constructor(externalizableName?: string);
403
+ /**
404
+ * 将对象写入编码器
405
+ *
406
+ * 子类必须覆盖此方法来实现自定义的序列化逻辑。
407
+ * 写入的数据必须能够被对应的 read() 静态方法正确读取。
408
+ *
409
+ * @param encoder - AMF 编码器实例
410
+ * @throws 如果子类没有实现此方法
411
+ */
412
+ write(encoder: AMFEncoder): void;
413
+ /**
414
+ * 从解码器读取并创建对象实例
415
+ *
416
+ * 子类必须定义一个同名的静态方法来实现自定义的反序列化逻辑。
417
+ * 此方法应该读取 write() 方法写入的数据并返回新的对象实例。
418
+ *
419
+ * 注意:这是一个静态方法,子类需要像这样定义:
420
+ * static read(decoder: AMFDecoder): YourClass { ... }
421
+ *
422
+ * @param decoder - AMF 解码器实例
423
+ * @returns 反序列化后的对象实例
424
+ * @throws 如果子类没有实现此方法
425
+ */
426
+ static read(decoder: AMFDecoder): Externalizable;
427
+ }
428
+
429
+ /**
430
+ * 可外部化对象静态读取接口
431
+ */
432
+ declare interface ExternalizableReader {
433
+ read(decoder: AMFDecoder): any;
434
+ }
435
+
436
+ /**
437
+ * 强制类型值类
438
+ *
439
+ * 允许用户强制指定一个值应该被编码为特定的 AMF 类型
440
+ * 而不是由编码器自动推断类型
441
+ *
442
+ * @example
443
+ * ```typescript
444
+ * // 强制将数字编码为 AMF3 的 DOUBLE 类型而不是 INTEGER
445
+ * const forcedDouble = new ForcedTypeValue(42, AMF3.DOUBLE);
446
+ * encoder.writeObject(forcedDouble);
447
+ * ```
448
+ */
449
+ export declare class ForcedTypeValue {
450
+ /** 实际要编码的值 */
451
+ value: any;
452
+ /** 强制使用的 AMF 类型 */
453
+ type: AMFType;
454
+ /**
455
+ * 创建一个强制类型值
456
+ *
457
+ * @param value - 实际要编码的值
458
+ * @param type - 强制使用的 AMF 类型
459
+ */
460
+ constructor(value: any, type: AMFType);
461
+ }
462
+
463
+ /**
464
+ * 二进制数据读取器类
465
+ *
466
+ * 提供从 Uint8Array 中读取各种二进制数据类型的方法
467
+ * 所有多字节数据都使用大端序(Big Endian)读取,这是 AMF 协议的要求
468
+ *
469
+ * @example
470
+ * ```typescript
471
+ * const data = new Uint8Array([0x00, 0x01, 0x02, 0x03]);
472
+ * const reader = new Reader(data);
473
+ * const byte = reader.readByte(); // 0x00
474
+ * const uint16 = reader.readUInt16BE(); // 0x0102
475
+ * ```
476
+ */
477
+ declare class Reader {
478
+ /** 要读取的二进制数据 */
479
+ protected data: Uint8Array;
480
+ /** 用于读取多字节数据的 DataView */
481
+ protected view: DataView;
482
+ /** 当前读取位置 */
483
+ protected position: number;
484
+ /**
485
+ * 创建一个新的读取器
486
+ *
487
+ * @param data - 要读取的二进制数据
488
+ */
489
+ constructor(data: Uint8Array);
490
+ /**
491
+ * 获取当前读取位置
492
+ *
493
+ * @returns 当前位置(字节偏移量)
494
+ */
495
+ getPosition(): number;
496
+ /**
497
+ * 设置读取位置
498
+ *
499
+ * @param pos - 新的位置
500
+ */
501
+ setPosition(pos: number): void;
502
+ /**
503
+ * 获取剩余可读字节数
504
+ *
505
+ * @returns 剩余字节数
506
+ */
507
+ getBytesAvailable(): number;
508
+ /**
509
+ * 检查是否还有足够的字节可读
510
+ *
511
+ * @param length - 需要的字节数
512
+ * @throws 如果剩余字节不足
513
+ */
514
+ private checkAvailable;
515
+ /**
516
+ * 读取一个或多个字节
517
+ *
518
+ * @param length - 要读取的字节数,默认为 1
519
+ * @param alwaysReturnArray - 即使只读取一个字节也返回 Uint8Array
520
+ * @returns 如果 length 为 1 且 alwaysReturnArray 为 false,返回单个数字;否则返回 Uint8Array
521
+ */
522
+ readByte(length?: number, alwaysReturnArray?: boolean): number | Uint8Array;
523
+ /**
524
+ * 读取一个无符号 8 位整数
525
+ *
526
+ * @returns 0-255 范围内的整数
527
+ */
528
+ readUInt8(): number;
529
+ /**
530
+ * 读取一个有符号 8 位整数
531
+ *
532
+ * @returns -128 到 127 范围内的整数
533
+ */
534
+ readInt8(): number;
535
+ /**
536
+ * 读取一个大端序无符号 16 位整数
537
+ *
538
+ * @returns 0-65535 范围内的整数
539
+ */
540
+ readUInt16BE(): number;
541
+ /**
542
+ * 读取一个大端序有符号 16 位整数
543
+ *
544
+ * @returns -32768 到 32767 范围内的整数
545
+ */
546
+ readInt16BE(): number;
547
+ /**
548
+ * 读取一个大端序无符号 32 位整数
549
+ *
550
+ * @returns 0-4294967295 范围内的整数
551
+ */
552
+ readUInt32BE(): number;
553
+ /**
554
+ * 读取一个大端序有符号 32 位整数
555
+ *
556
+ * @returns -2147483648 到 2147483647 范围内的整数
557
+ */
558
+ readInt32BE(): number;
559
+ /**
560
+ * 读取一个大端序 64 位双精度浮点数
561
+ *
562
+ * @returns 双精度浮点数
563
+ */
564
+ readDoubleBE(): number;
565
+ /**
566
+ * 读取一个 AMF0 格式的字符串
567
+ *
568
+ * AMF0 字符串格式:2字节长度 + UTF-8 编码的字符串数据
569
+ *
570
+ * @returns 解码后的字符串
571
+ */
572
+ readString(): string;
573
+ /**
574
+ * 读取指定长度的 UTF-8 字符串
575
+ *
576
+ * @param length - 字符串的字节长度
577
+ * @returns 解码后的字符串
578
+ */
579
+ readUTF8String(length: number): string;
580
+ /**
581
+ * 读取 AMF3 的变长整数(Int29)
582
+ *
583
+ * AMF3 使用一种可变长度编码来表示 29 位有符号整数
584
+ * 每个字节的最高位表示是否还有更多字节:
585
+ * - 0xxxxxxx: 单字节,值 0-127
586
+ * - 1xxxxxxx 0xxxxxxx: 双字节
587
+ * - 1xxxxxxx 1xxxxxxx 0xxxxxxx: 三字节
588
+ * - 1xxxxxxx 1xxxxxxx 1xxxxxxx xxxxxxxx: 四字节(最后一个字节使用全部 8 位)
589
+ *
590
+ * @returns 29 位有符号整数,范围 -268435456 到 536870911
591
+ */
592
+ readInt29(): number;
593
+ /**
594
+ * 读取 AMF3 头部信息
595
+ *
596
+ * AMF3 中很多类型使用一个头部来区分是定义还是引用:
597
+ * - 最低位为 0: 这是一个引用,高位存储引用索引
598
+ * - 最低位为 1: 这是一个定义,高位存储数据长度或其他信息
599
+ *
600
+ * @returns AMF 头部信息对象
601
+ */
602
+ readAMFHeader(): AMFHeader;
603
+ }
604
+
605
+ /**
606
+ * 可序列化对象基类
607
+ *
608
+ * JavaScript 对象或类如果想要被序列化为"命名对象"(带有类名的对象),
609
+ * 应该继承这个类。
610
+ *
611
+ * 编码器会序列化对象的所有字段,除非:
612
+ * 1. 字段名以双下划线(__)开头
613
+ * 2. 对象定义了 getSerializableFields() 方法来指定要序列化的字段
614
+ *
615
+ * @example
616
+ * ```typescript
617
+ * class User extends Serializable {
618
+ * public name: string;
619
+ * public age: number;
620
+ * private __password: string; // 不会被序列化
621
+ *
622
+ * constructor() {
623
+ * super('com.example.User');
624
+ * this.name = '';
625
+ * this.age = 0;
626
+ * this.__password = '';
627
+ * }
628
+ * }
629
+ * ```
630
+ */
631
+ export declare class Serializable {
632
+ /**
633
+ * 对象的类名
634
+ * 如果为空或未定义,对象将被视为匿名对象
635
+ */
636
+ __class?: string;
637
+ /**
638
+ * 是否为动态对象
639
+ * - true 或 undefined: 动态编码(key-value 交替写入)
640
+ * - false: 静态编码(先写所有 key,再写所有 value)
641
+ */
642
+ __dynamic?: boolean;
643
+ /**
644
+ * 创建一个可序列化对象
645
+ *
646
+ * @param serializableName - 对象的类名(可选)
647
+ * @param dynamic - 是否为动态对象(可选,默认 true)
648
+ */
649
+ constructor(serializableName?: string, dynamic?: boolean);
650
+ /**
651
+ * 获取应该被序列化的字段列表
652
+ *
653
+ * 子类可以覆盖此方法来控制哪些字段会被序列化
654
+ * 注意:即使字段在此列表中,以双下划线开头的字段仍然会被忽略
655
+ *
656
+ * @returns 要序列化的字段名数组,如果返回 undefined 则序列化所有字段
657
+ *
658
+ * @example
659
+ * ```typescript
660
+ * getSerializableFields(): string[] {
661
+ * return ['name', 'age', 'email'];
662
+ * }
663
+ * ```
664
+ */
665
+ getSerializableFields?(): string[];
666
+ }
667
+
668
+ /**
669
+ * 二进制数据写入器模块
670
+ *
671
+ * 本模块提供了向二进制缓冲区写入各种数据类型的功能
672
+ * 专为浏览器环境设计,使用 Uint8Array 和 DataView 替代 Node.js 的 Buffer
673
+ */
674
+ /**
675
+ * 二进制数据写入器类
676
+ *
677
+ * 提供向动态增长的缓冲区写入各种二进制数据类型的方法
678
+ * 所有多字节数据都使用大端序(Big Endian)写入,这是 AMF 协议的要求
679
+ *
680
+ * @example
681
+ * ```typescript
682
+ * const writer = new Writer();
683
+ * writer.writeByte(0x01);
684
+ * writer.writeUInt16BE(0x0203);
685
+ * writer.writeDoubleBE(3.14159);
686
+ * const result = writer.getBuffer();
687
+ * ```
688
+ */
689
+ declare class Writer {
690
+ /** 内部缓冲区数组,用于动态收集写入的数据 */
691
+ private chunks;
692
+ /** 当前已写入的总字节数 */
693
+ private totalLength;
694
+ /**
695
+ * 创建一个新的写入器
696
+ */
697
+ constructor();
698
+ /**
699
+ * 获取当前已写入的字节数
700
+ *
701
+ * @returns 已写入的总字节数
702
+ */
703
+ getLength(): number;
704
+ /**
705
+ * 获取写入的所有数据
706
+ *
707
+ * 将所有写入的数据块合并为一个 Uint8Array
708
+ *
709
+ * @returns 包含所有写入数据的 Uint8Array
710
+ */
711
+ getBuffer(): Uint8Array;
712
+ /**
713
+ * 清空写入器,重置到初始状态
714
+ */
715
+ clear(): void;
716
+ /**
717
+ * 写入数据到缓冲区
718
+ *
719
+ * 支持多种数据类型:
720
+ * - 数字(0-255):作为单个字节写入
721
+ * - 数字数组:每个元素作为一个字节写入
722
+ * - Uint8Array:直接写入
723
+ * - 字符串:作为 UTF-8 编码写入
724
+ *
725
+ * @param value - 要写入的值
726
+ * @throws 如果值的类型不支持
727
+ */
728
+ write(value: number | number[] | Uint8Array | string): void;
729
+ /**
730
+ * 写入单个字节
731
+ *
732
+ * @param value - 0-255 范围内的整数
733
+ */
734
+ writeByte(value: number): void;
735
+ /**
736
+ * 写入字节数组
737
+ *
738
+ * @param bytes - 字节数组或 Uint8Array
739
+ */
740
+ writeBytes(bytes: number[] | Uint8Array): void;
741
+ /**
742
+ * 写入一个大端序无符号 16 位整数
743
+ *
744
+ * @param value - 0-65535 范围内的整数
745
+ */
746
+ writeUInt16BE(value: number): void;
747
+ /**
748
+ * 写入一个大端序有符号 16 位整数
749
+ *
750
+ * @param value - -32768 到 32767 范围内的整数
751
+ */
752
+ writeInt16BE(value: number): void;
753
+ /**
754
+ * 写入一个大端序无符号 32 位整数
755
+ *
756
+ * @param value - 0-4294967295 范围内的整数
757
+ */
758
+ writeUInt32BE(value: number): void;
759
+ /**
760
+ * 写入一个大端序有符号 32 位整数
761
+ *
762
+ * @param value - -2147483648 到 2147483647 范围内的整数
763
+ */
764
+ writeInt32BE(value: number): void;
765
+ /**
766
+ * 写入一个大端序 64 位双精度浮点数
767
+ *
768
+ * @param value - 双精度浮点数
769
+ */
770
+ writeDoubleBE(value: number): void;
771
+ /**
772
+ * 写入一个 AMF0 格式的字符串
773
+ *
774
+ * AMF0 字符串格式:2字节长度(大端序)+ UTF-8 编码的字符串数据
775
+ *
776
+ * @param value - 要写入的字符串
777
+ */
778
+ writeString(value: string): void;
779
+ /**
780
+ * 写入 AMF3 的变长整数(Int29)
781
+ *
782
+ * AMF3 使用一种可变长度编码来表示 29 位有符号整数
783
+ * 每个字节的最高位表示是否还有更多字节:
784
+ * - 值 0-127: 单字节 (0xxxxxxx)
785
+ * - 值 128-16383: 双字节 (1xxxxxxx 0xxxxxxx)
786
+ * - 值 16384-2097151: 三字节 (1xxxxxxx 1xxxxxxx 0xxxxxxx)
787
+ * - 值 2097152-536870911: 四字节 (1xxxxxxx 1xxxxxxx 1xxxxxxx xxxxxxxx)
788
+ *
789
+ * @param value - 29 位有符号整数,范围 -268435456 到 536870911
790
+ * @throws 如果值超出范围
791
+ */
792
+ writeInt29(value: number): void;
793
+ }
794
+
795
+ export { }