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/README.md +195 -0
- package/dist/amf.js +1024 -0
- package/dist/amf.js.map +1 -0
- package/dist/index.d.ts +795 -0
- package/package.json +39 -0
package/README.md
ADDED
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
## AMF.js (TypeScript)
|
|
2
|
+
|
|
3
|
+
一个基于 TypeScript 的 AMF 序列化库,支持 **浏览器** 和 **Node.js** 环境,当前编码器/解码器聚焦 **AMF3**。
|
|
4
|
+
|
|
5
|
+
## 特性
|
|
6
|
+
|
|
7
|
+
- `AMFEncoder` / `AMFDecoder`:AMF3 编解码
|
|
8
|
+
- **跨平台**:基于 `Uint8Array`、`DataView`、`TextEncoder`、`TextDecoder`,浏览器和 Node.js 11+ 均可使用
|
|
9
|
+
- 支持引用表(字符串/对象/Trait)、动态对象、`Externalizable`、`ByteArray`
|
|
10
|
+
- 不依赖 Node.js `Buffer` 或 Node Stream
|
|
11
|
+
|
|
12
|
+
## 安装
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
npm install amf-ts
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
或者本地安装:
|
|
19
|
+
```bash
|
|
20
|
+
npm pack
|
|
21
|
+
npm install /path/to/amf-ts-1.0.0.tgz
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## 目录说明
|
|
25
|
+
|
|
26
|
+
- `src/encoder.ts`:AMF3 编码器
|
|
27
|
+
- `src/decoder.ts`:AMF3 解码器
|
|
28
|
+
- `src/reader.ts`:二进制读取工具
|
|
29
|
+
- `src/writer.ts`:二进制写入工具
|
|
30
|
+
- `src/classes.ts`:`Serializable` / `Externalizable` / `ForcedTypeValue`
|
|
31
|
+
- `src/types.ts`:AMF 类型定义与推断
|
|
32
|
+
|
|
33
|
+
## 基础用法
|
|
34
|
+
|
|
35
|
+
```ts
|
|
36
|
+
import { AMFEncoder } from 'amf-ts';
|
|
37
|
+
import { AMFDecoder } from 'amf-ts';
|
|
38
|
+
|
|
39
|
+
const data = {
|
|
40
|
+
msg: 'hello',
|
|
41
|
+
n: 123,
|
|
42
|
+
ok: true,
|
|
43
|
+
arr: [1, 'x', false],
|
|
44
|
+
bytes: new Uint8Array([1, 2, 3]),
|
|
45
|
+
date: new Date()
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
// 编码
|
|
49
|
+
const encoder = new AMFEncoder();
|
|
50
|
+
encoder.writeObject(data);
|
|
51
|
+
const bytes: Uint8Array = encoder.getBuffer();
|
|
52
|
+
|
|
53
|
+
// 解码
|
|
54
|
+
const decoder = new AMFDecoder(bytes);
|
|
55
|
+
const result = decoder.decode();
|
|
56
|
+
console.log(result.msg, result.arr[1]);
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
## Serializable 对象
|
|
60
|
+
|
|
61
|
+
继承 `Serializable` 后,可带 `__class` 输出命名对象。
|
|
62
|
+
|
|
63
|
+
```ts
|
|
64
|
+
import { Serializable, AMFEncoder } from 'amf-ts';
|
|
65
|
+
|
|
66
|
+
class User extends Serializable {
|
|
67
|
+
name: string;
|
|
68
|
+
age: number;
|
|
69
|
+
|
|
70
|
+
constructor(name: string, age: number) {
|
|
71
|
+
super('demo.User');
|
|
72
|
+
this.name = name;
|
|
73
|
+
this.age = age;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const encoder = new AMFEncoder();
|
|
78
|
+
encoder.writeObject(new User('tom', 18));
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
如果你实现了 `getSerializableFields()`,会优先按该字段列表输出;`__` 前缀字段会被忽略。
|
|
82
|
+
|
|
83
|
+
```ts
|
|
84
|
+
import { Serializable, AMFEncoder } from 'amf-ts';
|
|
85
|
+
|
|
86
|
+
class User extends Serializable {
|
|
87
|
+
name: string;
|
|
88
|
+
age: number;
|
|
89
|
+
password: string; // 敏感字段,不想序列化
|
|
90
|
+
|
|
91
|
+
constructor(name: string, age: number, password: string) {
|
|
92
|
+
super('demo.User');
|
|
93
|
+
this.name = name;
|
|
94
|
+
this.age = age;
|
|
95
|
+
this.password = password;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// 只序列化 name 和 age,忽略 password
|
|
99
|
+
getSerializableFields(): string[] {
|
|
100
|
+
return ['name', 'age'];
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
const encoder = new AMFEncoder();
|
|
105
|
+
encoder.writeObject(new User('tom', 18, 'secret123'));
|
|
106
|
+
// 输出只包含 name 和 age,不包含 password
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
## 动态/静态编码控制
|
|
110
|
+
|
|
111
|
+
通过 `__dynamic` 属性控制对象的编码方式:
|
|
112
|
+
|
|
113
|
+
| `__class` | `__dynamic` | 编码方式 |
|
|
114
|
+
|-----------|-------------|----------|
|
|
115
|
+
| 有值 | 任意 | 静态(先写 keys,再写 values) |
|
|
116
|
+
| 空/无 | `false` | 静态 |
|
|
117
|
+
| 空/无 | `true`/`undefined` | 动态(key-value 交替) |
|
|
118
|
+
|
|
119
|
+
```ts
|
|
120
|
+
import { Serializable, AMFEncoder } from 'amf-ts';
|
|
121
|
+
|
|
122
|
+
// 匿名动态对象(默认行为)
|
|
123
|
+
const obj1 = new Serializable();
|
|
124
|
+
obj1.name = 'test';
|
|
125
|
+
|
|
126
|
+
// 匿名静态对象
|
|
127
|
+
const obj2 = new Serializable('', false);
|
|
128
|
+
obj2.name = 'test';
|
|
129
|
+
|
|
130
|
+
// 或者直接设置属性
|
|
131
|
+
const obj3 = { name: 'test', __dynamic: false,__class:'abc' };
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
## Externalizable 对象
|
|
135
|
+
|
|
136
|
+
适用于你想完全控制编码/解码格式的场景。
|
|
137
|
+
|
|
138
|
+
```ts
|
|
139
|
+
import { Externalizable, AMFEncoder, AMFDecoder } from 'amf-ts';
|
|
140
|
+
|
|
141
|
+
class CustomData extends Externalizable {
|
|
142
|
+
value: number;
|
|
143
|
+
|
|
144
|
+
constructor(value = 0) {
|
|
145
|
+
super('demo.CustomData');
|
|
146
|
+
this.value = value;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
write(encoder: AMFEncoder): void {
|
|
150
|
+
encoder.writeObject(this.value);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
static read(decoder: AMFDecoder): CustomData {
|
|
154
|
+
const obj = new CustomData();
|
|
155
|
+
obj.value = decoder.decode();
|
|
156
|
+
return obj;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
AMFDecoder.register('demo.CustomData', CustomData);
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
## 强制类型编码
|
|
164
|
+
|
|
165
|
+
当你想覆盖自动推断类型时可使用 `ForcedTypeValue`。
|
|
166
|
+
|
|
167
|
+
```ts
|
|
168
|
+
import { AMF3, ForcedTypeValue, AMFEncoder } from 'amf-ts';
|
|
169
|
+
|
|
170
|
+
const encoder = new AMFEncoder();
|
|
171
|
+
encoder.writeObject(new ForcedTypeValue(1, AMF3.DOUBLE));
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
## 浏览器使用
|
|
175
|
+
|
|
176
|
+
```html
|
|
177
|
+
<script type="module">
|
|
178
|
+
import { AMFEncoder, AMFDecoder } from '../dist/amf.js';
|
|
179
|
+
|
|
180
|
+
const encoder = new AMFEncoder();
|
|
181
|
+
encoder.writeObject({ hello: 'world' });
|
|
182
|
+
console.log(encoder.getBuffer());
|
|
183
|
+
</script>
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
## 构建
|
|
187
|
+
|
|
188
|
+
```bash
|
|
189
|
+
npm install
|
|
190
|
+
npm run build
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
输出文件:
|
|
194
|
+
- `dist/amf.js` - ES Module
|
|
195
|
+
- `dist/index.d.ts` - TypeScript 类型定义
|