nytra 0.0.3 → 0.0.4
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 +107 -0
- package/dist/Nytra.d.ts +14 -0
- package/dist/Nytra.js +468 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/package.json +5 -2
package/README.md
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
# Nytra
|
|
2
|
+
|
|
3
|
+
Fast binary serialization for JavaScript/TypeScript with decorator-based DTO support.
|
|
4
|
+
|
|
5
|
+
Nytra helps you encode/decode plain values, arrays, objects, and class instances into compact binary buffers.
|
|
6
|
+
|
|
7
|
+
## Features
|
|
8
|
+
|
|
9
|
+
- Binary encode/decode for primitives, arrays, objects, bigint, and nested data
|
|
10
|
+
- Decorator-based class schema registration
|
|
11
|
+
- Nullable fields support in DTOs
|
|
12
|
+
- Custom type IDs for compact class payloads
|
|
13
|
+
- ESM package output with TypeScript declarations
|
|
14
|
+
|
|
15
|
+
## Installation
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
npm install nytra
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## Quick Start
|
|
22
|
+
|
|
23
|
+
```ts
|
|
24
|
+
import { Nytra, Types } from "nytra";
|
|
25
|
+
|
|
26
|
+
@Nytra.registerClass(2000)
|
|
27
|
+
class Player {
|
|
28
|
+
@Nytra.registerField(0, Types.TYPE_STRING)
|
|
29
|
+
name: string;
|
|
30
|
+
|
|
31
|
+
@Nytra.registerField(1, Types.TYPE_UINT16)
|
|
32
|
+
level: number;
|
|
33
|
+
|
|
34
|
+
@Nytra.registerField(2, Types.TYPE_STRING, { nullable: true })
|
|
35
|
+
guild: string | null = null;
|
|
36
|
+
|
|
37
|
+
constructor(name: string, level: number) {
|
|
38
|
+
this.name = name;
|
|
39
|
+
this.level = level;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const original = new Player("Ari", 42);
|
|
44
|
+
const encoded = Nytra.encode(original);
|
|
45
|
+
const decoded = Nytra.decode(encoded) as Player;
|
|
46
|
+
|
|
47
|
+
console.log(decoded.name, decoded.level, decoded.guild);
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
## API Overview
|
|
51
|
+
|
|
52
|
+
- `Nytra.encode(data, typeId?)` -> `Uint8Array`
|
|
53
|
+
- `Nytra.decode(buffer)` -> decoded value
|
|
54
|
+
- `Nytra.autoguessType(data)` -> internal type id
|
|
55
|
+
- `Nytra.getTypeIdForClass(ClassCtor)` -> registered class type id
|
|
56
|
+
- `Nytra.registerClass(typeId)` -> class decorator
|
|
57
|
+
- `Nytra.registerField(position, typeOrClass, options?)` -> field decorator
|
|
58
|
+
|
|
59
|
+
### `registerField` options
|
|
60
|
+
|
|
61
|
+
- `nullable?: boolean` - marks a field as nullable in class schema
|
|
62
|
+
|
|
63
|
+
## Supported Built-in Types
|
|
64
|
+
|
|
65
|
+
Use constants from `Types`, for example:
|
|
66
|
+
|
|
67
|
+
- `TYPE_NULL`, `TYPE_BOOLEAN`
|
|
68
|
+
- `TYPE_UINT8`, `TYPE_UINT16`, `TYPE_UINT32`, `TYPE_UINT64`
|
|
69
|
+
- `TYPE_INT8`, `TYPE_INT16`, `TYPE_INT32`, `TYPE_INT64`
|
|
70
|
+
- `TYPE_FLOAT32`, `TYPE_FLOAT64`
|
|
71
|
+
- `TYPE_STRING`, `TYPE_ARRAY`, `TYPE_OBJECT`, `TYPE_JSON`, `TYPE_BIGINT`
|
|
72
|
+
|
|
73
|
+
## Development
|
|
74
|
+
|
|
75
|
+
Build:
|
|
76
|
+
|
|
77
|
+
```bash
|
|
78
|
+
npm run build
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
Run tests (Bun):
|
|
82
|
+
|
|
83
|
+
```bash
|
|
84
|
+
bun test
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
## TypeScript Decorators Setup
|
|
88
|
+
|
|
89
|
+
If you see decorator typing/runtime issues, enable modern decorators + metadata in your TS setup:
|
|
90
|
+
|
|
91
|
+
```json
|
|
92
|
+
{
|
|
93
|
+
"compilerOptions": {
|
|
94
|
+
"target": "ES2022",
|
|
95
|
+
"module": "nodenext",
|
|
96
|
+
"moduleResolution": "nodenext",
|
|
97
|
+
"lib": ["ES2022", "esnext.decorators", "esnext.decorators.metadata"]
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
> Nytra decorators use the new standard decorator context APIs (`ClassDecoratorContext`, `ClassFieldDecoratorContext`).
|
|
103
|
+
|
|
104
|
+
## License
|
|
105
|
+
|
|
106
|
+
MIT
|
|
107
|
+
|
package/dist/Nytra.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { Reader } from "./Reader.ts";
|
|
2
|
+
import { Writer } from "./Writer.ts";
|
|
3
|
+
export type RegisterFieldOptions = {
|
|
4
|
+
nullable?: boolean;
|
|
5
|
+
};
|
|
6
|
+
export declare class Nytra {
|
|
7
|
+
#private;
|
|
8
|
+
static registerField(position: number, targetTypeId?: number | Function, options?: RegisterFieldOptions): (v: undefined, ctx: ClassFieldDecoratorContext) => void;
|
|
9
|
+
static registerClass(typeId: number): <T extends Function>(decoratedClass: T, ctx: ClassDecoratorContext) => void;
|
|
10
|
+
static autoguessType(data: unknown): number;
|
|
11
|
+
static getTypeIdForClass(ctor: Function): number;
|
|
12
|
+
static encode(data: unknown, type?: number | null, withType?: boolean, writer?: Writer | null): Uint8Array;
|
|
13
|
+
static decode(data: Uint8Array | Reader, type?: number | null): unknown;
|
|
14
|
+
}
|
package/dist/Nytra.js
ADDED
|
@@ -0,0 +1,468 @@
|
|
|
1
|
+
import { Reader } from "./Reader.js";
|
|
2
|
+
import { Registry } from "./Registry.js";
|
|
3
|
+
import { Writer } from "./Writer.js";
|
|
4
|
+
import { TYPE_ARRAY, TYPE_BIGINT, TYPE_BOOLEAN, TYPE_EXTENSION, TYPE_FLOAT32, TYPE_FLOAT64, TYPE_INT16, TYPE_INT32, TYPE_INT64, TYPE_INT8, TYPE_JSON, TYPE_NULL, TYPE_OBJECT, TYPE_STRING, TYPE_STRING_16_INTERNAL, TYPE_STRING_32_INTERNAL, TYPE_UINT16, TYPE_UINT32, TYPE_UINT64, TYPE_UINT8 } from "./Types.js";
|
|
5
|
+
const MAX_UINT_32 = 2 ** 32;
|
|
6
|
+
const MAX_UINT_16 = 2 ** 16;
|
|
7
|
+
const MAX_UINT_8 = 2 ** 8;
|
|
8
|
+
const MIN_INT_32 = -(2 ** 31);
|
|
9
|
+
const MIN_INT_16 = -(2 ** 15);
|
|
10
|
+
const MIN_INT_8 = -(2 ** 8);
|
|
11
|
+
function createDecoder(cls) {
|
|
12
|
+
const meta = classMetaStore.get(cls);
|
|
13
|
+
const fields = meta?.fields;
|
|
14
|
+
return function (reader) {
|
|
15
|
+
let obj = {};
|
|
16
|
+
const nullableFields = fields.filter(([_name, meta]) => meta.options.nullable).map(([name, _meta]) => name);
|
|
17
|
+
const nullableBytes = Math.ceil(nullableFields.length / 8);
|
|
18
|
+
const nullableBitfield = nullableBytes > 0 ? reader.readBytes(nullableBytes) : [];
|
|
19
|
+
const isFieldNull = (index) => {
|
|
20
|
+
const byteIndex = Math.floor(index / 8);
|
|
21
|
+
const bitOffset = index % 8;
|
|
22
|
+
return (nullableBitfield[byteIndex] & (1 << bitOffset)) !== 0;
|
|
23
|
+
};
|
|
24
|
+
for (let [name, meta] of fields) {
|
|
25
|
+
if (nullableBytes > 0 && meta.options.nullable) {
|
|
26
|
+
const nullIndex = nullableFields.indexOf(name);
|
|
27
|
+
if (nullIndex !== -1 && isFieldNull(nullIndex)) {
|
|
28
|
+
obj[name] = null;
|
|
29
|
+
continue;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
if (typeof meta.targetTypeId === "function") {
|
|
33
|
+
const found = classMetaStore.get(meta.targetTypeId);
|
|
34
|
+
meta.targetTypeId = found ? found.typeId : TYPE_JSON;
|
|
35
|
+
}
|
|
36
|
+
let type = meta.targetTypeId;
|
|
37
|
+
if (type === TYPE_STRING) {
|
|
38
|
+
type = reader.readType();
|
|
39
|
+
}
|
|
40
|
+
obj[name] = Nytra.decode(reader, type);
|
|
41
|
+
}
|
|
42
|
+
Object.setPrototypeOf(obj, cls.prototype);
|
|
43
|
+
return obj;
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
function createEncoder(cls) {
|
|
47
|
+
const meta = classMetaStore.get(cls);
|
|
48
|
+
const cachedEncoders = [];
|
|
49
|
+
const nullableFields = [];
|
|
50
|
+
let nullableBytes = 0;
|
|
51
|
+
return function (data, writer) {
|
|
52
|
+
if (writer === null) {
|
|
53
|
+
writer = new Writer();
|
|
54
|
+
}
|
|
55
|
+
if (cachedEncoders.length == 0) {
|
|
56
|
+
meta.fields.forEach(([name, fieldMeta]) => {
|
|
57
|
+
if (fieldMeta.options.nullable) {
|
|
58
|
+
nullableFields.push(name);
|
|
59
|
+
}
|
|
60
|
+
let typeId = fieldMeta.targetTypeId;
|
|
61
|
+
if (!typeId) {
|
|
62
|
+
cachedEncoders.push({ name: name, encode: null, options: fieldMeta.options });
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
// Resolve Function → typeId hier, nicht im Loop!
|
|
66
|
+
if (typeof typeId === 'function') {
|
|
67
|
+
const found = classMetaStore.get(typeId);
|
|
68
|
+
typeId = found ? found.typeId : TYPE_JSON;
|
|
69
|
+
}
|
|
70
|
+
// Encoder-Funktion vorgebunden
|
|
71
|
+
let encodeFn;
|
|
72
|
+
if (typeId < 255) {
|
|
73
|
+
encodeFn = (value, writer) => Nytra.encode(value, typeId, false, writer);
|
|
74
|
+
}
|
|
75
|
+
else {
|
|
76
|
+
encodeFn = CustomRegistry.getEncoder(typeId);
|
|
77
|
+
}
|
|
78
|
+
cachedEncoders.push({ name: name, encode: encodeFn, options: fieldMeta.options });
|
|
79
|
+
});
|
|
80
|
+
nullableBytes = Math.ceil(nullableFields.length / 8);
|
|
81
|
+
}
|
|
82
|
+
if (nullableBytes > 0) {
|
|
83
|
+
const nullableBitfield = new Uint8Array(nullableBytes);
|
|
84
|
+
for (let i = 0; i < nullableFields.length; i++) {
|
|
85
|
+
const field = nullableFields[i];
|
|
86
|
+
if (data[field] === null) {
|
|
87
|
+
const byteIndex = Math.floor(i / 8);
|
|
88
|
+
const bitOffset = i % 8;
|
|
89
|
+
nullableBitfield[byteIndex] |= (1 << bitOffset);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
writer.writeBytes(nullableBitfield);
|
|
93
|
+
}
|
|
94
|
+
for (const encodeInfo of cachedEncoders) {
|
|
95
|
+
const value = data[encodeInfo.name];
|
|
96
|
+
if (value === null && encodeInfo.options.nullable) {
|
|
97
|
+
continue;
|
|
98
|
+
}
|
|
99
|
+
encodeInfo.encode ?
|
|
100
|
+
encodeInfo.encode(data[encodeInfo.name], writer) :
|
|
101
|
+
Nytra.encode(data[encodeInfo.name], null, true, writer);
|
|
102
|
+
}
|
|
103
|
+
return writer.toUint8Array();
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
const classMetaStore = new WeakMap();
|
|
107
|
+
const SYMBOL_FIELDS = Symbol('Nytra:fields');
|
|
108
|
+
const CustomRegistry = new Registry();
|
|
109
|
+
export class Nytra {
|
|
110
|
+
static registerField(position, targetTypeId, options = {}) {
|
|
111
|
+
const defaultOpts = {
|
|
112
|
+
nullable: false
|
|
113
|
+
};
|
|
114
|
+
Object.assign(defaultOpts, options);
|
|
115
|
+
return function (v, ctx) {
|
|
116
|
+
if (ctx.private) {
|
|
117
|
+
throw new Error('Only public fields can be registered');
|
|
118
|
+
}
|
|
119
|
+
const metadata = ctx.metadata;
|
|
120
|
+
if (typeof metadata[SYMBOL_FIELDS] === 'undefined') {
|
|
121
|
+
metadata[SYMBOL_FIELDS] = new Map();
|
|
122
|
+
}
|
|
123
|
+
const fieldMeta = {
|
|
124
|
+
position,
|
|
125
|
+
targetTypeId,
|
|
126
|
+
options: defaultOpts
|
|
127
|
+
};
|
|
128
|
+
metadata[SYMBOL_FIELDS].set(ctx.name, fieldMeta);
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
static registerClass(typeId) {
|
|
132
|
+
if (typeId < 256 || typeId > 65535) {
|
|
133
|
+
throw new Error('typeId must be in range of 256 and 65535');
|
|
134
|
+
}
|
|
135
|
+
return function (decoratedClass, ctx) {
|
|
136
|
+
const metadata = ctx.metadata;
|
|
137
|
+
const fields = metadata[SYMBOL_FIELDS] ?
|
|
138
|
+
[...metadata[SYMBOL_FIELDS].entries()].sort(([_aName, aMeta], [_bName, bMeta]) => aMeta.position - bMeta.position)
|
|
139
|
+
: [];
|
|
140
|
+
classMetaStore.set(decoratedClass, { typeId, fields });
|
|
141
|
+
CustomRegistry.register(typeId, {
|
|
142
|
+
encoder: createEncoder(decoratedClass),
|
|
143
|
+
decoder: createDecoder(decoratedClass)
|
|
144
|
+
});
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
static autoguessType(data) {
|
|
148
|
+
if (data === null) {
|
|
149
|
+
return TYPE_NULL;
|
|
150
|
+
}
|
|
151
|
+
if (typeof data === 'object') {
|
|
152
|
+
const ctor = data.constructor;
|
|
153
|
+
const found = classMetaStore.get(ctor);
|
|
154
|
+
if (found) {
|
|
155
|
+
return found.typeId;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
if (Array.isArray(data)) {
|
|
159
|
+
return TYPE_ARRAY;
|
|
160
|
+
}
|
|
161
|
+
if (typeof data === 'object') {
|
|
162
|
+
return TYPE_OBJECT;
|
|
163
|
+
}
|
|
164
|
+
if (typeof data === 'string') {
|
|
165
|
+
return TYPE_STRING;
|
|
166
|
+
}
|
|
167
|
+
if (typeof data === 'boolean') {
|
|
168
|
+
return TYPE_BOOLEAN;
|
|
169
|
+
}
|
|
170
|
+
if (typeof data === 'number') {
|
|
171
|
+
if (Number.isInteger(data)) {
|
|
172
|
+
if (data >= 0) {
|
|
173
|
+
//unsigned possible
|
|
174
|
+
if (data <= MAX_UINT_8) {
|
|
175
|
+
return TYPE_UINT8;
|
|
176
|
+
}
|
|
177
|
+
if (data <= MAX_UINT_16) {
|
|
178
|
+
return TYPE_UINT16;
|
|
179
|
+
}
|
|
180
|
+
if (data <= MAX_UINT_32) {
|
|
181
|
+
return TYPE_UINT32;
|
|
182
|
+
}
|
|
183
|
+
return TYPE_UINT64;
|
|
184
|
+
}
|
|
185
|
+
else {
|
|
186
|
+
if (data >= MIN_INT_8) {
|
|
187
|
+
return TYPE_INT8;
|
|
188
|
+
}
|
|
189
|
+
if (data >= MIN_INT_16) {
|
|
190
|
+
return TYPE_INT16;
|
|
191
|
+
}
|
|
192
|
+
if (data >= MIN_INT_32) {
|
|
193
|
+
return TYPE_INT32;
|
|
194
|
+
}
|
|
195
|
+
return TYPE_INT64;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
if (Math.fround(data) === data) {
|
|
199
|
+
return TYPE_FLOAT32;
|
|
200
|
+
}
|
|
201
|
+
return TYPE_FLOAT64;
|
|
202
|
+
}
|
|
203
|
+
if (typeof data === 'bigint') {
|
|
204
|
+
return TYPE_BIGINT;
|
|
205
|
+
}
|
|
206
|
+
return TYPE_JSON;
|
|
207
|
+
}
|
|
208
|
+
static getTypeIdForClass(ctor) {
|
|
209
|
+
const found = classMetaStore.get(ctor);
|
|
210
|
+
return found ? found.typeId : TYPE_JSON;
|
|
211
|
+
}
|
|
212
|
+
static #TEXT_ENCODER = new TextEncoder();
|
|
213
|
+
static encode(data, type = null, withType = true, writer = null) {
|
|
214
|
+
if (writer === null) {
|
|
215
|
+
writer = new Writer();
|
|
216
|
+
}
|
|
217
|
+
if (type === null) {
|
|
218
|
+
type = this.autoguessType(data);
|
|
219
|
+
}
|
|
220
|
+
if (type >= 256) {
|
|
221
|
+
const encodeFunction = CustomRegistry.getEncoder(type);
|
|
222
|
+
if (typeof encodeFunction !== 'function') {
|
|
223
|
+
throw new Error('Unknown type:' + type);
|
|
224
|
+
}
|
|
225
|
+
if (withType) {
|
|
226
|
+
writer.writeUint8(TYPE_EXTENSION);
|
|
227
|
+
writer.writeUint16(type);
|
|
228
|
+
}
|
|
229
|
+
encodeFunction(data, writer);
|
|
230
|
+
return writer.toUint8Array();
|
|
231
|
+
}
|
|
232
|
+
if (type >= TYPE_STRING) { //automatically
|
|
233
|
+
const str = data;
|
|
234
|
+
const bytes = this.#TEXT_ENCODER.encode(str);
|
|
235
|
+
const len = bytes.length;
|
|
236
|
+
if (len <= 127) {
|
|
237
|
+
writer.writeUint8(128 + len);
|
|
238
|
+
writer.writeBytes(bytes);
|
|
239
|
+
return writer.toUint8Array();
|
|
240
|
+
}
|
|
241
|
+
if (len <= 65535) {
|
|
242
|
+
writer.writeUint8(TYPE_STRING_16_INTERNAL);
|
|
243
|
+
writer.writeUint16(len);
|
|
244
|
+
writer.writeBytes(bytes);
|
|
245
|
+
return writer.toUint8Array();
|
|
246
|
+
}
|
|
247
|
+
writer.writeUint8(TYPE_STRING_32_INTERNAL);
|
|
248
|
+
writer.writeUint32(len);
|
|
249
|
+
writer.writeBytes(bytes);
|
|
250
|
+
return writer.toUint8Array();
|
|
251
|
+
}
|
|
252
|
+
switch (type) {
|
|
253
|
+
case TYPE_NULL:
|
|
254
|
+
writer.writeUint8(TYPE_NULL);
|
|
255
|
+
return writer.toUint8Array();
|
|
256
|
+
case TYPE_ARRAY: {
|
|
257
|
+
if (!Array.isArray(data)) {
|
|
258
|
+
throw new Error('Data must be an array');
|
|
259
|
+
}
|
|
260
|
+
const arr = data;
|
|
261
|
+
if (withType)
|
|
262
|
+
writer.writeUint8(TYPE_ARRAY);
|
|
263
|
+
const startIndex = writer.offset;
|
|
264
|
+
writer.setOffset(startIndex + 4); // reserve space for length
|
|
265
|
+
for (let value of arr) {
|
|
266
|
+
this.encode(value, null, true, writer);
|
|
267
|
+
}
|
|
268
|
+
const endIndex = writer.offset;
|
|
269
|
+
writer.setOffset(startIndex);
|
|
270
|
+
writer.writeUint32(endIndex - startIndex - 4);
|
|
271
|
+
writer.setOffset(endIndex);
|
|
272
|
+
return writer.toUint8Array();
|
|
273
|
+
}
|
|
274
|
+
case TYPE_OBJECT: {
|
|
275
|
+
if (typeof data !== 'object') {
|
|
276
|
+
throw new Error('Data must be an array');
|
|
277
|
+
}
|
|
278
|
+
const obj = data;
|
|
279
|
+
if (withType)
|
|
280
|
+
writer.writeUint8(TYPE_OBJECT);
|
|
281
|
+
const startIndex = writer.offset;
|
|
282
|
+
writer.setOffset(startIndex + 4);
|
|
283
|
+
let keys = Object.keys(obj);
|
|
284
|
+
for (let key of keys) {
|
|
285
|
+
this.encode(key, TYPE_STRING, true, writer);
|
|
286
|
+
this.encode(obj[key], null, true, writer);
|
|
287
|
+
}
|
|
288
|
+
const endIndex = writer.offset;
|
|
289
|
+
writer.setOffset(startIndex);
|
|
290
|
+
writer.writeUint32(endIndex - startIndex - 4);
|
|
291
|
+
writer.setOffset(endIndex);
|
|
292
|
+
return writer.toUint8Array();
|
|
293
|
+
}
|
|
294
|
+
case TYPE_UINT8: {
|
|
295
|
+
if (withType)
|
|
296
|
+
writer.writeUint8(TYPE_UINT8);
|
|
297
|
+
writer.writeUint8(data);
|
|
298
|
+
return writer.toUint8Array();
|
|
299
|
+
}
|
|
300
|
+
case TYPE_UINT16: {
|
|
301
|
+
if (withType)
|
|
302
|
+
writer.writeUint8(TYPE_UINT16);
|
|
303
|
+
writer.writeUint16(data);
|
|
304
|
+
return writer.toUint8Array();
|
|
305
|
+
}
|
|
306
|
+
case TYPE_UINT32: {
|
|
307
|
+
if (withType)
|
|
308
|
+
writer.writeUint8(TYPE_UINT32);
|
|
309
|
+
writer.writeUint32(data);
|
|
310
|
+
return writer.toUint8Array();
|
|
311
|
+
}
|
|
312
|
+
case TYPE_UINT64: {
|
|
313
|
+
if (withType)
|
|
314
|
+
writer.writeUint8(TYPE_UINT64);
|
|
315
|
+
writer.writeUint64(BigInt(data));
|
|
316
|
+
return writer.toUint8Array();
|
|
317
|
+
}
|
|
318
|
+
case TYPE_INT8: {
|
|
319
|
+
if (withType)
|
|
320
|
+
writer.writeUint8(TYPE_INT8);
|
|
321
|
+
writer.writeInt8(data);
|
|
322
|
+
return writer.toUint8Array();
|
|
323
|
+
}
|
|
324
|
+
case TYPE_INT16: {
|
|
325
|
+
if (withType)
|
|
326
|
+
writer.writeUint8(TYPE_INT16);
|
|
327
|
+
writer.writeInt16(data);
|
|
328
|
+
return writer.toUint8Array();
|
|
329
|
+
}
|
|
330
|
+
case TYPE_INT32: {
|
|
331
|
+
if (withType)
|
|
332
|
+
writer.writeUint8(TYPE_INT32);
|
|
333
|
+
writer.writeInt32(data);
|
|
334
|
+
return writer.toUint8Array();
|
|
335
|
+
}
|
|
336
|
+
case TYPE_INT64: {
|
|
337
|
+
if (withType)
|
|
338
|
+
writer.writeUint8(TYPE_INT64);
|
|
339
|
+
writer.writeInt64(BigInt(data));
|
|
340
|
+
return writer.toUint8Array();
|
|
341
|
+
}
|
|
342
|
+
case TYPE_BOOLEAN: {
|
|
343
|
+
if (withType)
|
|
344
|
+
writer.writeUint8(TYPE_BOOLEAN);
|
|
345
|
+
writer.writeUint8(data ? 1 : 0);
|
|
346
|
+
return writer.toUint8Array();
|
|
347
|
+
}
|
|
348
|
+
case TYPE_JSON: {
|
|
349
|
+
return this.encode(JSON.stringify(data), TYPE_STRING, withType, writer);
|
|
350
|
+
}
|
|
351
|
+
case TYPE_FLOAT32: {
|
|
352
|
+
if (withType)
|
|
353
|
+
writer.writeUint8(TYPE_FLOAT32);
|
|
354
|
+
writer.writeFloat32(data);
|
|
355
|
+
return writer.toUint8Array();
|
|
356
|
+
}
|
|
357
|
+
case TYPE_FLOAT64: {
|
|
358
|
+
if (withType)
|
|
359
|
+
writer.writeUint8(TYPE_FLOAT64);
|
|
360
|
+
writer.writeFloat64(data);
|
|
361
|
+
return writer.toUint8Array();
|
|
362
|
+
}
|
|
363
|
+
case TYPE_BIGINT: {
|
|
364
|
+
if (withType)
|
|
365
|
+
writer.writeUint8(TYPE_BIGINT);
|
|
366
|
+
writer.writeBigInt(BigInt(data));
|
|
367
|
+
return writer.toUint8Array();
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
throw new Error('Type unknown: ' + type);
|
|
371
|
+
}
|
|
372
|
+
static decode(data, type = null) {
|
|
373
|
+
let reader = data instanceof Reader ? data : new Reader(data);
|
|
374
|
+
if (type === null)
|
|
375
|
+
type = reader.readType();
|
|
376
|
+
if (type > 255) {
|
|
377
|
+
const decodeFunction = CustomRegistry.getDecoder(type);
|
|
378
|
+
if (typeof decodeFunction !== 'function') {
|
|
379
|
+
throw new Error('Unknown type:' + type);
|
|
380
|
+
}
|
|
381
|
+
return decodeFunction(reader);
|
|
382
|
+
}
|
|
383
|
+
if (type >= TYPE_STRING) {
|
|
384
|
+
let length = type & 0b01111111;
|
|
385
|
+
return reader.readString(length);
|
|
386
|
+
}
|
|
387
|
+
switch (type) {
|
|
388
|
+
case TYPE_STRING_16_INTERNAL: {
|
|
389
|
+
const length = reader.readUINT16();
|
|
390
|
+
return reader.readString(length);
|
|
391
|
+
}
|
|
392
|
+
case TYPE_STRING_32_INTERNAL: {
|
|
393
|
+
const length = reader.readUINT32();
|
|
394
|
+
return reader.readString(length);
|
|
395
|
+
}
|
|
396
|
+
case TYPE_BOOLEAN: {
|
|
397
|
+
return reader.readUINT8() !== 0;
|
|
398
|
+
}
|
|
399
|
+
case TYPE_JSON: {
|
|
400
|
+
const json = this.decode(reader);
|
|
401
|
+
return JSON.parse(json);
|
|
402
|
+
}
|
|
403
|
+
case TYPE_UINT8: {
|
|
404
|
+
return reader.readUINT8();
|
|
405
|
+
}
|
|
406
|
+
case TYPE_UINT16: {
|
|
407
|
+
return reader.readUINT16();
|
|
408
|
+
}
|
|
409
|
+
case TYPE_UINT32: {
|
|
410
|
+
return reader.readUINT32();
|
|
411
|
+
}
|
|
412
|
+
case TYPE_UINT64: {
|
|
413
|
+
return bigintToSafeNumber(reader.readUINT64());
|
|
414
|
+
}
|
|
415
|
+
case TYPE_INT8: {
|
|
416
|
+
return reader.readINT8();
|
|
417
|
+
}
|
|
418
|
+
case TYPE_INT16: {
|
|
419
|
+
return reader.readINT16();
|
|
420
|
+
}
|
|
421
|
+
case TYPE_INT32: {
|
|
422
|
+
return reader.readINT32();
|
|
423
|
+
}
|
|
424
|
+
case TYPE_INT64: {
|
|
425
|
+
return bigintToSafeNumber(reader.readINT64());
|
|
426
|
+
}
|
|
427
|
+
case TYPE_FLOAT64: {
|
|
428
|
+
return reader.readFloat64();
|
|
429
|
+
}
|
|
430
|
+
case TYPE_FLOAT32: {
|
|
431
|
+
return reader.readFloat32();
|
|
432
|
+
}
|
|
433
|
+
case TYPE_ARRAY: {
|
|
434
|
+
const len = reader.readUINT32();
|
|
435
|
+
const end = reader.offset + len;
|
|
436
|
+
const targetArray = [];
|
|
437
|
+
while (reader.offset < end) {
|
|
438
|
+
targetArray.push(this.decode(reader));
|
|
439
|
+
}
|
|
440
|
+
return targetArray;
|
|
441
|
+
}
|
|
442
|
+
case TYPE_OBJECT: {
|
|
443
|
+
const len = reader.readUINT32();
|
|
444
|
+
const end = reader.offset + len;
|
|
445
|
+
const targetObj = {};
|
|
446
|
+
while (reader.offset < end) {
|
|
447
|
+
const key = this.decode(reader);
|
|
448
|
+
targetObj[key] = this.decode(reader);
|
|
449
|
+
}
|
|
450
|
+
return targetObj;
|
|
451
|
+
}
|
|
452
|
+
case TYPE_BIGINT: {
|
|
453
|
+
return reader.readBigInt();
|
|
454
|
+
}
|
|
455
|
+
case TYPE_NULL: {
|
|
456
|
+
return null;
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
throw new Error('Unknown type:' + type);
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
function bigintToSafeNumber(value) {
|
|
463
|
+
if (value > BigInt(Number.MAX_SAFE_INTEGER) ||
|
|
464
|
+
value < BigInt(Number.MIN_SAFE_INTEGER)) {
|
|
465
|
+
return value;
|
|
466
|
+
}
|
|
467
|
+
return Number(value);
|
|
468
|
+
}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export { Nytra } from "./Nytra.ts";
|
|
2
2
|
export * as Types from "./Types.ts";
|
package/dist/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export { Nytra } from "./Nytra.js";
|
|
2
2
|
export * as Types from "./Types.js";
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"private": false,
|
|
3
3
|
"name": "nytra",
|
|
4
|
-
"version": "0.0.
|
|
4
|
+
"version": "0.0.4",
|
|
5
5
|
"description": "A javascript ES6 Decorator driven library to encode/decode javascript objects to/from binary optimized buffers to reduce payload on network",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"author": "",
|
|
@@ -12,7 +12,10 @@
|
|
|
12
12
|
"build": "tsc",
|
|
13
13
|
"prepublishOnly": "npm run build"
|
|
14
14
|
},
|
|
15
|
-
"files": [
|
|
15
|
+
"files": [
|
|
16
|
+
"dist",
|
|
17
|
+
"README.md"
|
|
18
|
+
],
|
|
16
19
|
"devDependencies": {
|
|
17
20
|
"@msgpack/msgpack": "^3.1.3",
|
|
18
21
|
"@types/bun": "^1.3.13",
|