@yume-chan/struct 0.0.5 → 0.0.10
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/CHANGELOG.json +23 -0
- package/CHANGELOG.md +16 -0
- package/LICENSE +21 -21
- package/README.md +720 -643
- package/dts/basic/context.d.ts +10 -17
- package/dts/basic/context.d.ts.map +1 -1
- package/dts/basic/definition.d.ts +23 -23
- package/dts/basic/definition.d.ts.map +1 -1
- package/dts/basic/field-value.d.ts +38 -0
- package/dts/basic/field-value.d.ts.map +1 -0
- package/dts/basic/index.d.ts +2 -2
- package/dts/basic/index.d.ts.map +1 -1
- package/dts/basic/struct-value.d.ts +24 -0
- package/dts/basic/struct-value.d.ts.map +1 -0
- package/dts/index.d.ts +1 -0
- package/dts/index.d.ts.map +1 -1
- package/dts/struct.d.ts +44 -47
- package/dts/struct.d.ts.map +1 -1
- package/dts/syncbird.d.ts +20 -0
- package/dts/syncbird.d.ts.map +1 -0
- package/dts/types/array-buffer.d.ts +22 -20
- package/dts/types/array-buffer.d.ts.map +1 -1
- package/dts/types/fixed-length-array-buffer.d.ts +1 -3
- package/dts/types/fixed-length-array-buffer.d.ts.map +1 -1
- package/dts/types/index.d.ts +0 -1
- package/dts/types/index.d.ts.map +1 -1
- package/dts/types/number.d.ts +11 -10
- package/dts/types/number.d.ts.map +1 -1
- package/dts/types/variable-length-array-buffer.d.ts +21 -19
- package/dts/types/variable-length-array-buffer.d.ts.map +1 -1
- package/dts/{types/utils.d.ts → utils.d.ts} +3 -0
- package/dts/utils.d.ts.map +1 -0
- package/esm/basic/context.js +1 -1
- package/esm/basic/context.js.map +1 -1
- package/esm/basic/definition.js +9 -11
- package/esm/basic/definition.js.map +1 -1
- package/esm/basic/field-value.js +35 -0
- package/esm/basic/field-value.js.map +1 -0
- package/esm/basic/index.js +2 -2
- package/esm/basic/index.js.map +1 -1
- package/esm/basic/struct-value.js +36 -0
- package/esm/basic/struct-value.js.map +1 -0
- package/esm/index.js +1 -0
- package/esm/index.js.map +1 -1
- package/esm/struct.js +103 -176
- package/esm/struct.js.map +1 -1
- package/esm/syncbird.js +25 -0
- package/esm/syncbird.js.map +1 -0
- package/esm/types/array-buffer.js +83 -107
- package/esm/types/array-buffer.js.map +1 -1
- package/esm/types/fixed-length-array-buffer.js +5 -15
- package/esm/types/fixed-length-array-buffer.js.map +1 -1
- package/esm/types/index.js +0 -1
- package/esm/types/index.js.map +1 -1
- package/esm/types/number.js +35 -53
- package/esm/types/number.js.map +1 -1
- package/esm/types/variable-length-array-buffer.js +57 -68
- package/esm/types/variable-length-array-buffer.js.map +1 -1
- package/esm/utils.js +17 -0
- package/esm/utils.js.map +1 -0
- package/package.json +47 -50
- package/src/basic/context.ts +32 -40
- package/src/basic/definition.ts +72 -69
- package/src/basic/{runtime-value.ts → field-value.ts} +67 -72
- package/src/basic/index.ts +4 -4
- package/src/basic/struct-value.ts +39 -0
- package/src/index.ts +5 -4
- package/src/struct.ts +595 -579
- package/src/syncbird.ts +53 -0
- package/src/types/array-buffer.ts +194 -171
- package/src/types/fixed-length-array-buffer.ts +17 -27
- package/src/types/index.ts +4 -5
- package/src/types/number.ts +114 -101
- package/src/types/variable-length-array-buffer.ts +142 -121
- package/src/{types/utils.ts → utils.ts} +62 -47
- package/cjs/basic/context.js +0 -7
- package/cjs/basic/context.js.map +0 -1
- package/cjs/basic/definition.js +0 -26
- package/cjs/basic/definition.js.map +0 -1
- package/cjs/basic/index.js +0 -8
- package/cjs/basic/index.js.map +0 -1
- package/cjs/basic/runtime-object.js +0 -34
- package/cjs/basic/runtime-object.js.map +0 -1
- package/cjs/basic/runtime-value.js +0 -41
- package/cjs/basic/runtime-value.js.map +0 -1
- package/cjs/index.js +0 -10
- package/cjs/index.js.map +0 -1
- package/cjs/struct.js +0 -259
- package/cjs/struct.js.map +0 -1
- package/cjs/types/array-buffer.js +0 -137
- package/cjs/types/array-buffer.js.map +0 -1
- package/cjs/types/fixed-length-array-buffer.js +0 -21
- package/cjs/types/fixed-length-array-buffer.js.map +0 -1
- package/cjs/types/index.js +0 -9
- package/cjs/types/index.js.map +0 -1
- package/cjs/types/number.js +0 -68
- package/cjs/types/number.js.map +0 -1
- package/cjs/types/utils.js +0 -11
- package/cjs/types/utils.js.map +0 -1
- package/cjs/types/variable-length-array-buffer.js +0 -85
- package/cjs/types/variable-length-array-buffer.js.map +0 -1
- package/dts/basic/runtime-object.d.ts +0 -17
- package/dts/basic/runtime-object.d.ts.map +0 -1
- package/dts/basic/runtime-value.d.ts +0 -39
- package/dts/basic/runtime-value.d.ts.map +0 -1
- package/dts/types/utils.d.ts.map +0 -1
- package/esm/basic/runtime-object.js +0 -28
- package/esm/basic/runtime-object.js.map +0 -1
- package/esm/basic/runtime-value.js +0 -38
- package/esm/basic/runtime-value.js.map +0 -1
- package/esm/types/utils.js +0 -7
- package/esm/types/utils.js.map +0 -1
- package/src/basic/context.spec.ts +0 -9
- package/src/basic/definition.spec.ts +0 -26
- package/src/basic/runtime-object.spec.ts +0 -38
- package/src/basic/runtime-object.ts +0 -36
- package/src/basic/runtime-value.spec.ts +0 -71
- package/src/types/array-buffer.spec.ts +0 -88
- package/src/types/number.spec.ts +0 -262
- package/tsconfig.cjs.json +0 -11
- package/tsconfig.esm.json +0 -15
package/src/types/number.spec.ts
DELETED
|
@@ -1,262 +0,0 @@
|
|
|
1
|
-
import { StructDefaultOptions, StructDeserializationContext, StructSerializationContext } from '../basic';
|
|
2
|
-
import { NumberFieldDefinition, NumberFieldRuntimeValue, NumberFieldType } from './number';
|
|
3
|
-
|
|
4
|
-
describe('Types', () => {
|
|
5
|
-
describe('Number', () => {
|
|
6
|
-
describe('NumberFieldType', () => {
|
|
7
|
-
it('Int8 validation', () => {
|
|
8
|
-
const key = 'Int8';
|
|
9
|
-
expect(NumberFieldType[key]).toHaveProperty('size', 1);
|
|
10
|
-
expect(NumberFieldType[key]).toHaveProperty('dataViewGetter', 'get' + key);
|
|
11
|
-
expect(NumberFieldType[key]).toHaveProperty('dataViewSetter', 'set' + key);
|
|
12
|
-
});
|
|
13
|
-
|
|
14
|
-
it('Uint8 validation', () => {
|
|
15
|
-
const key = 'Uint8';
|
|
16
|
-
expect(NumberFieldType[key]).toHaveProperty('size', 1);
|
|
17
|
-
expect(NumberFieldType[key]).toHaveProperty('dataViewGetter', 'get' + key);
|
|
18
|
-
expect(NumberFieldType[key]).toHaveProperty('dataViewSetter', 'set' + key);
|
|
19
|
-
});
|
|
20
|
-
|
|
21
|
-
it('Int16 validation', () => {
|
|
22
|
-
const key = 'Int16';
|
|
23
|
-
expect(NumberFieldType[key]).toHaveProperty('size', 2);
|
|
24
|
-
expect(NumberFieldType[key]).toHaveProperty('dataViewGetter', 'get' + key);
|
|
25
|
-
expect(NumberFieldType[key]).toHaveProperty('dataViewSetter', 'set' + key);
|
|
26
|
-
});
|
|
27
|
-
|
|
28
|
-
it('Uint16 validation', () => {
|
|
29
|
-
const key = 'Uint16';
|
|
30
|
-
expect(NumberFieldType[key]).toHaveProperty('size', 2);
|
|
31
|
-
expect(NumberFieldType[key]).toHaveProperty('dataViewGetter', 'get' + key);
|
|
32
|
-
expect(NumberFieldType[key]).toHaveProperty('dataViewSetter', 'set' + key);
|
|
33
|
-
});
|
|
34
|
-
|
|
35
|
-
it('Int32 validation', () => {
|
|
36
|
-
const key = 'Int32';
|
|
37
|
-
expect(NumberFieldType[key]).toHaveProperty('size', 4);
|
|
38
|
-
expect(NumberFieldType[key]).toHaveProperty('dataViewGetter', 'get' + key);
|
|
39
|
-
expect(NumberFieldType[key]).toHaveProperty('dataViewSetter', 'set' + key);
|
|
40
|
-
});
|
|
41
|
-
|
|
42
|
-
it('Uint32 validation', () => {
|
|
43
|
-
const key = 'Uint32';
|
|
44
|
-
expect(NumberFieldType[key]).toHaveProperty('size', 4);
|
|
45
|
-
expect(NumberFieldType[key]).toHaveProperty('dataViewGetter', 'get' + key);
|
|
46
|
-
expect(NumberFieldType[key]).toHaveProperty('dataViewSetter', 'set' + key);
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
it('Int64 validation', () => {
|
|
50
|
-
const key = 'Int64';
|
|
51
|
-
expect(NumberFieldType[key]).toHaveProperty('size', 8);
|
|
52
|
-
expect(NumberFieldType[key]).toHaveProperty('dataViewGetter', 'getBig' + key);
|
|
53
|
-
expect(NumberFieldType[key]).toHaveProperty('dataViewSetter', 'setBig' + key);
|
|
54
|
-
});
|
|
55
|
-
|
|
56
|
-
it('Uint64 validation', () => {
|
|
57
|
-
const key = 'Uint64';
|
|
58
|
-
expect(NumberFieldType[key]).toHaveProperty('size', 8);
|
|
59
|
-
expect(NumberFieldType[key]).toHaveProperty('dataViewGetter', 'getBig' + key);
|
|
60
|
-
expect(NumberFieldType[key]).toHaveProperty('dataViewSetter', 'setBig' + key);
|
|
61
|
-
});
|
|
62
|
-
});
|
|
63
|
-
|
|
64
|
-
describe('NumberFieldDefinition', () => {
|
|
65
|
-
describe('getSize', () => {
|
|
66
|
-
it('should return 1 for int8', () => {
|
|
67
|
-
expect(new NumberFieldDefinition(NumberFieldType.Int8).getSize()).toBe(1);
|
|
68
|
-
});
|
|
69
|
-
|
|
70
|
-
it('should return 1 for uint8', () => {
|
|
71
|
-
expect(new NumberFieldDefinition(NumberFieldType.Uint8).getSize()).toBe(1);
|
|
72
|
-
});
|
|
73
|
-
|
|
74
|
-
it('should return 2 for int16', () => {
|
|
75
|
-
expect(new NumberFieldDefinition(NumberFieldType.Int16).getSize()).toBe(2);
|
|
76
|
-
});
|
|
77
|
-
|
|
78
|
-
it('should return 2 for uint16', () => {
|
|
79
|
-
expect(new NumberFieldDefinition(NumberFieldType.Uint16).getSize()).toBe(2);
|
|
80
|
-
});
|
|
81
|
-
|
|
82
|
-
it('should return 4 for int32', () => {
|
|
83
|
-
expect(new NumberFieldDefinition(NumberFieldType.Int32).getSize()).toBe(4);
|
|
84
|
-
});
|
|
85
|
-
|
|
86
|
-
it('should return 4 for uint32', () => {
|
|
87
|
-
expect(new NumberFieldDefinition(NumberFieldType.Uint32).getSize()).toBe(4);
|
|
88
|
-
});
|
|
89
|
-
|
|
90
|
-
it('should return 8 for int64', () => {
|
|
91
|
-
expect(new NumberFieldDefinition(NumberFieldType.Int64).getSize()).toBe(8);
|
|
92
|
-
});
|
|
93
|
-
|
|
94
|
-
it('should return 8 for uint64', () => {
|
|
95
|
-
expect(new NumberFieldDefinition(NumberFieldType.Uint64).getSize()).toBe(8);
|
|
96
|
-
});
|
|
97
|
-
});
|
|
98
|
-
|
|
99
|
-
describe('deserialize', () => {
|
|
100
|
-
it('should deserialize Uint8', async () => {
|
|
101
|
-
const read = jest.fn((length: number) => new Uint8Array([1, 2, 3, 4]).buffer);
|
|
102
|
-
const context: StructDeserializationContext = {
|
|
103
|
-
read,
|
|
104
|
-
decodeUtf8(buffer) { throw new Error(''); },
|
|
105
|
-
encodeUtf8(input) { throw new Error(''); },
|
|
106
|
-
};
|
|
107
|
-
|
|
108
|
-
const definition = new NumberFieldDefinition(NumberFieldType.Uint8);
|
|
109
|
-
const value = await definition.deserialize(
|
|
110
|
-
StructDefaultOptions,
|
|
111
|
-
context,
|
|
112
|
-
{}
|
|
113
|
-
);
|
|
114
|
-
|
|
115
|
-
expect(value.get()).toBe(1);
|
|
116
|
-
expect(read).toBeCalledTimes(1);
|
|
117
|
-
expect(read).lastCalledWith(NumberFieldType.Uint8.size);
|
|
118
|
-
});
|
|
119
|
-
|
|
120
|
-
it('should deserialize Uint16', async () => {
|
|
121
|
-
const read = jest.fn((length: number) => new Uint8Array([1, 2, 3, 4]).buffer);
|
|
122
|
-
const context: StructDeserializationContext = {
|
|
123
|
-
read,
|
|
124
|
-
decodeUtf8(buffer) { throw new Error(''); },
|
|
125
|
-
encodeUtf8(input) { throw new Error(''); },
|
|
126
|
-
};
|
|
127
|
-
|
|
128
|
-
const definition = new NumberFieldDefinition(NumberFieldType.Uint16);
|
|
129
|
-
const value = await definition.deserialize(
|
|
130
|
-
StructDefaultOptions,
|
|
131
|
-
context,
|
|
132
|
-
{}
|
|
133
|
-
);
|
|
134
|
-
|
|
135
|
-
expect(value.get()).toBe((1 << 8) | 2);
|
|
136
|
-
expect(read).toBeCalledTimes(1);
|
|
137
|
-
expect(read).lastCalledWith(NumberFieldType.Uint16.size);
|
|
138
|
-
});
|
|
139
|
-
|
|
140
|
-
it('should deserialize Uint16LE', async () => {
|
|
141
|
-
const read = jest.fn((length: number) => new Uint8Array([1, 2, 3, 4]).buffer);
|
|
142
|
-
const context: StructDeserializationContext = {
|
|
143
|
-
read,
|
|
144
|
-
decodeUtf8(buffer) { throw new Error(''); },
|
|
145
|
-
encodeUtf8(input) { throw new Error(''); },
|
|
146
|
-
};
|
|
147
|
-
|
|
148
|
-
const definition = new NumberFieldDefinition(NumberFieldType.Uint16);
|
|
149
|
-
const value = await definition.deserialize(
|
|
150
|
-
{ ...StructDefaultOptions, littleEndian: true },
|
|
151
|
-
context,
|
|
152
|
-
{}
|
|
153
|
-
);
|
|
154
|
-
|
|
155
|
-
expect(value.get()).toBe((2 << 8) | 1);
|
|
156
|
-
expect(read).toBeCalledTimes(1);
|
|
157
|
-
expect(read).lastCalledWith(NumberFieldType.Uint16.size);
|
|
158
|
-
});
|
|
159
|
-
});
|
|
160
|
-
});
|
|
161
|
-
|
|
162
|
-
describe('NumberRuntimeValue', () => {
|
|
163
|
-
describe('getSize', () => {
|
|
164
|
-
it('should return 1 for int8', () => {
|
|
165
|
-
const context: StructSerializationContext = {
|
|
166
|
-
encodeUtf8(input) { throw new Error(''); },
|
|
167
|
-
};
|
|
168
|
-
const definition = new NumberFieldDefinition(NumberFieldType.Int8);
|
|
169
|
-
const runtimeValue = definition.createValue(StructDefaultOptions, context, {}, 42);
|
|
170
|
-
|
|
171
|
-
expect(runtimeValue.getSize()).toBe(1);
|
|
172
|
-
});
|
|
173
|
-
|
|
174
|
-
it('should return 1 for uint8', () => {
|
|
175
|
-
const context: StructSerializationContext = {
|
|
176
|
-
encodeUtf8(input) { throw new Error(''); },
|
|
177
|
-
};
|
|
178
|
-
const definition = new NumberFieldDefinition(NumberFieldType.Uint8);
|
|
179
|
-
const runtimeValue = definition.createValue(StructDefaultOptions, context, {}, 42);
|
|
180
|
-
|
|
181
|
-
expect(runtimeValue.getSize()).toBe(1);
|
|
182
|
-
});
|
|
183
|
-
|
|
184
|
-
it('should return 2 for int16', () => {
|
|
185
|
-
const context: StructSerializationContext = {
|
|
186
|
-
encodeUtf8(input) { throw new Error(''); },
|
|
187
|
-
};
|
|
188
|
-
const definition = new NumberFieldDefinition(NumberFieldType.Int16);
|
|
189
|
-
const runtimeValue = definition.createValue(StructDefaultOptions, context, {}, 42);
|
|
190
|
-
|
|
191
|
-
expect(runtimeValue.getSize()).toBe(2);
|
|
192
|
-
});
|
|
193
|
-
|
|
194
|
-
it('should return 2 for uint16', () => {
|
|
195
|
-
const context: StructSerializationContext = {
|
|
196
|
-
encodeUtf8(input) { throw new Error(''); },
|
|
197
|
-
};
|
|
198
|
-
const definition = new NumberFieldDefinition(NumberFieldType.Uint16);
|
|
199
|
-
const runtimeValue = definition.createValue(StructDefaultOptions, context, {}, 42);
|
|
200
|
-
|
|
201
|
-
expect(runtimeValue.getSize()).toBe(2);
|
|
202
|
-
});
|
|
203
|
-
|
|
204
|
-
it('should return 4 for int32', () => {
|
|
205
|
-
const context: StructSerializationContext = {
|
|
206
|
-
encodeUtf8(input) { throw new Error(''); },
|
|
207
|
-
};
|
|
208
|
-
const definition = new NumberFieldDefinition(NumberFieldType.Int32);
|
|
209
|
-
const runtimeValue = definition.createValue(StructDefaultOptions, context, {}, 42);
|
|
210
|
-
|
|
211
|
-
expect(runtimeValue.getSize()).toBe(4);
|
|
212
|
-
});
|
|
213
|
-
|
|
214
|
-
it('should return 4 for uint32', () => {
|
|
215
|
-
const context: StructSerializationContext = {
|
|
216
|
-
encodeUtf8(input) { throw new Error(''); },
|
|
217
|
-
};
|
|
218
|
-
const definition = new NumberFieldDefinition(NumberFieldType.Uint32);
|
|
219
|
-
const runtimeValue = definition.createValue(StructDefaultOptions, context, {}, 42);
|
|
220
|
-
|
|
221
|
-
expect(runtimeValue.getSize()).toBe(4);
|
|
222
|
-
});
|
|
223
|
-
|
|
224
|
-
it('should return 8 for int64', () => {
|
|
225
|
-
const context: StructSerializationContext = {
|
|
226
|
-
encodeUtf8(input) { throw new Error(''); },
|
|
227
|
-
};
|
|
228
|
-
const definition = new NumberFieldDefinition(NumberFieldType.Int64);
|
|
229
|
-
const runtimeValue = definition.createValue(StructDefaultOptions, context, {}, BigInt(42));
|
|
230
|
-
|
|
231
|
-
expect(runtimeValue.getSize()).toBe(8);
|
|
232
|
-
});
|
|
233
|
-
|
|
234
|
-
it('should return 8 for uint64', () => {
|
|
235
|
-
const context: StructSerializationContext = {
|
|
236
|
-
encodeUtf8(input) { throw new Error(''); },
|
|
237
|
-
};
|
|
238
|
-
const definition = new NumberFieldDefinition(NumberFieldType.Uint64);
|
|
239
|
-
const runtimeValue = definition.createValue(StructDefaultOptions, context, {}, BigInt(42));
|
|
240
|
-
|
|
241
|
-
expect(runtimeValue.getSize()).toBe(8);
|
|
242
|
-
});
|
|
243
|
-
});
|
|
244
|
-
|
|
245
|
-
describe('serialize', () => {
|
|
246
|
-
it('should serialize uint8', () => {
|
|
247
|
-
const context: StructSerializationContext = {
|
|
248
|
-
encodeUtf8(input) { throw new Error(''); },
|
|
249
|
-
};
|
|
250
|
-
const definition = new NumberFieldDefinition(NumberFieldType.Int8);
|
|
251
|
-
const runtimeValue = definition.createValue(StructDefaultOptions, context, {}, 42);
|
|
252
|
-
|
|
253
|
-
const array = new Uint8Array(10);
|
|
254
|
-
const dataView = new DataView(array.buffer);
|
|
255
|
-
runtimeValue.serialize(dataView, 2);
|
|
256
|
-
|
|
257
|
-
expect(Array.from(array)).toEqual([0, 0, 42, 0, 0, 0, 0, 0, 0, 0]);
|
|
258
|
-
});
|
|
259
|
-
});
|
|
260
|
-
});
|
|
261
|
-
});
|
|
262
|
-
});
|
package/tsconfig.cjs.json
DELETED
package/tsconfig.esm.json
DELETED