utilium 0.3.1 → 0.3.3
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/objects.js +4 -0
- package/dist/struct.d.ts +43 -43
- package/dist/struct.js +12 -8
- package/package.json +1 -1
- package/src/struct.ts +13 -6
package/dist/objects.js
CHANGED
@@ -33,6 +33,8 @@ export function isJSON(str) {
|
|
33
33
|
}
|
34
34
|
}
|
35
35
|
export class FileMap {
|
36
|
+
path;
|
37
|
+
fs;
|
36
38
|
get [Symbol.toStringTag]() {
|
37
39
|
return 'FileMap';
|
38
40
|
}
|
@@ -69,6 +71,7 @@ export class FileMap {
|
|
69
71
|
* A Map overlaying a JSON file
|
70
72
|
*/
|
71
73
|
export class JSONFileMap extends FileMap {
|
74
|
+
options;
|
72
75
|
get [Symbol.toStringTag]() {
|
73
76
|
return 'JSONFileMap';
|
74
77
|
}
|
@@ -124,6 +127,7 @@ export class JSONFileMap extends FileMap {
|
|
124
127
|
* A Map overlaying a folder
|
125
128
|
*/
|
126
129
|
export class FolderMap extends FileMap {
|
130
|
+
options;
|
127
131
|
get [Symbol.toStringTag]() {
|
128
132
|
return 'FolderMap';
|
129
133
|
}
|
package/dist/struct.d.ts
CHANGED
@@ -23,7 +23,7 @@ export declare function struct(options?: Partial<StructOptions>): (target: Class
|
|
23
23
|
/**
|
24
24
|
* Decorates a class member to be serialized
|
25
25
|
*/
|
26
|
-
export declare function member(type: ValidPrimitiveType | ClassLike, length?: number): (target: object,
|
26
|
+
export declare function member(type: ValidPrimitiveType | ClassLike, length?: number): (target: object, { name }: ClassMemberDecoratorContext) => void;
|
27
27
|
/**
|
28
28
|
* Serializes a struct into a Uint8Array
|
29
29
|
*/
|
@@ -39,87 +39,87 @@ export declare function deserialize(instance: unknown, _buffer: ArrayBuffer | Ar
|
|
39
39
|
*/
|
40
40
|
export declare const types: {
|
41
41
|
int8: {
|
42
|
-
(length?: number): (target: object, context
|
43
|
-
(target: object, context
|
42
|
+
(length?: number): (target: object, context: ClassMemberDecoratorContext) => void;
|
43
|
+
(target: object, context: ClassMemberDecoratorContext): void;
|
44
44
|
};
|
45
45
|
int16: {
|
46
|
-
(length?: number): (target: object, context
|
47
|
-
(target: object, context
|
46
|
+
(length?: number): (target: object, context: ClassMemberDecoratorContext) => void;
|
47
|
+
(target: object, context: ClassMemberDecoratorContext): void;
|
48
48
|
};
|
49
49
|
int32: {
|
50
|
-
(length?: number): (target: object, context
|
51
|
-
(target: object, context
|
50
|
+
(length?: number): (target: object, context: ClassMemberDecoratorContext) => void;
|
51
|
+
(target: object, context: ClassMemberDecoratorContext): void;
|
52
52
|
};
|
53
53
|
int64: {
|
54
|
-
(length?: number): (target: object, context
|
55
|
-
(target: object, context
|
54
|
+
(length?: number): (target: object, context: ClassMemberDecoratorContext) => void;
|
55
|
+
(target: object, context: ClassMemberDecoratorContext): void;
|
56
56
|
};
|
57
57
|
uint8: {
|
58
|
-
(length?: number): (target: object, context
|
59
|
-
(target: object, context
|
58
|
+
(length?: number): (target: object, context: ClassMemberDecoratorContext) => void;
|
59
|
+
(target: object, context: ClassMemberDecoratorContext): void;
|
60
60
|
};
|
61
61
|
uint16: {
|
62
|
-
(length?: number): (target: object, context
|
63
|
-
(target: object, context
|
62
|
+
(length?: number): (target: object, context: ClassMemberDecoratorContext) => void;
|
63
|
+
(target: object, context: ClassMemberDecoratorContext): void;
|
64
64
|
};
|
65
65
|
uint32: {
|
66
|
-
(length?: number): (target: object, context
|
67
|
-
(target: object, context
|
66
|
+
(length?: number): (target: object, context: ClassMemberDecoratorContext) => void;
|
67
|
+
(target: object, context: ClassMemberDecoratorContext): void;
|
68
68
|
};
|
69
69
|
uint64: {
|
70
|
-
(length?: number): (target: object, context
|
71
|
-
(target: object, context
|
70
|
+
(length?: number): (target: object, context: ClassMemberDecoratorContext) => void;
|
71
|
+
(target: object, context: ClassMemberDecoratorContext): void;
|
72
72
|
};
|
73
73
|
float32: {
|
74
|
-
(length?: number): (target: object, context
|
75
|
-
(target: object, context
|
74
|
+
(length?: number): (target: object, context: ClassMemberDecoratorContext) => void;
|
75
|
+
(target: object, context: ClassMemberDecoratorContext): void;
|
76
76
|
};
|
77
77
|
float64: {
|
78
|
-
(length?: number): (target: object, context
|
79
|
-
(target: object, context
|
78
|
+
(length?: number): (target: object, context: ClassMemberDecoratorContext) => void;
|
79
|
+
(target: object, context: ClassMemberDecoratorContext): void;
|
80
80
|
};
|
81
81
|
Int8: {
|
82
|
-
(length?: number): (target: object, context
|
83
|
-
(target: object, context
|
82
|
+
(length?: number): (target: object, context: ClassMemberDecoratorContext) => void;
|
83
|
+
(target: object, context: ClassMemberDecoratorContext): void;
|
84
84
|
};
|
85
85
|
Int16: {
|
86
|
-
(length?: number): (target: object, context
|
87
|
-
(target: object, context
|
86
|
+
(length?: number): (target: object, context: ClassMemberDecoratorContext) => void;
|
87
|
+
(target: object, context: ClassMemberDecoratorContext): void;
|
88
88
|
};
|
89
89
|
Int32: {
|
90
|
-
(length?: number): (target: object, context
|
91
|
-
(target: object, context
|
90
|
+
(length?: number): (target: object, context: ClassMemberDecoratorContext) => void;
|
91
|
+
(target: object, context: ClassMemberDecoratorContext): void;
|
92
92
|
};
|
93
93
|
Int64: {
|
94
|
-
(length?: number): (target: object, context
|
95
|
-
(target: object, context
|
94
|
+
(length?: number): (target: object, context: ClassMemberDecoratorContext) => void;
|
95
|
+
(target: object, context: ClassMemberDecoratorContext): void;
|
96
96
|
};
|
97
97
|
Uint8: {
|
98
|
-
(length?: number): (target: object, context
|
99
|
-
(target: object, context
|
98
|
+
(length?: number): (target: object, context: ClassMemberDecoratorContext) => void;
|
99
|
+
(target: object, context: ClassMemberDecoratorContext): void;
|
100
100
|
};
|
101
101
|
Uint16: {
|
102
|
-
(length?: number): (target: object, context
|
103
|
-
(target: object, context
|
102
|
+
(length?: number): (target: object, context: ClassMemberDecoratorContext) => void;
|
103
|
+
(target: object, context: ClassMemberDecoratorContext): void;
|
104
104
|
};
|
105
105
|
Uint32: {
|
106
|
-
(length?: number): (target: object, context
|
107
|
-
(target: object, context
|
106
|
+
(length?: number): (target: object, context: ClassMemberDecoratorContext) => void;
|
107
|
+
(target: object, context: ClassMemberDecoratorContext): void;
|
108
108
|
};
|
109
109
|
Uint64: {
|
110
|
-
(length?: number): (target: object, context
|
111
|
-
(target: object, context
|
110
|
+
(length?: number): (target: object, context: ClassMemberDecoratorContext) => void;
|
111
|
+
(target: object, context: ClassMemberDecoratorContext): void;
|
112
112
|
};
|
113
113
|
Float32: {
|
114
|
-
(length?: number): (target: object, context
|
115
|
-
(target: object, context
|
114
|
+
(length?: number): (target: object, context: ClassMemberDecoratorContext) => void;
|
115
|
+
(target: object, context: ClassMemberDecoratorContext): void;
|
116
116
|
};
|
117
117
|
Float64: {
|
118
|
-
(length?: number): (target: object, context
|
119
|
-
(target: object, context
|
118
|
+
(length?: number): (target: object, context: ClassMemberDecoratorContext) => void;
|
119
|
+
(target: object, context: ClassMemberDecoratorContext): void;
|
120
120
|
};
|
121
121
|
char: {
|
122
|
-
(length?: number): (target: object, context
|
123
|
-
(target: object, context
|
122
|
+
(length?: number): (target: object, context: ClassMemberDecoratorContext) => void;
|
123
|
+
(target: object, context: ClassMemberDecoratorContext): void;
|
124
124
|
};
|
125
125
|
};
|
package/dist/struct.js
CHANGED
@@ -51,7 +51,7 @@ export function align(value, alignment) {
|
|
51
51
|
export function struct(options = {}) {
|
52
52
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
53
53
|
return function (target, _) {
|
54
|
-
target[init]
|
54
|
+
target[init] ||= [];
|
55
55
|
let size = 0;
|
56
56
|
const members = new Map();
|
57
57
|
for (const { name, type, length } of target[init]) {
|
@@ -74,14 +74,18 @@ export function struct(options = {}) {
|
|
74
74
|
* Decorates a class member to be serialized
|
75
75
|
*/
|
76
76
|
export function member(type, length) {
|
77
|
-
return function (target,
|
78
|
-
var _a;
|
79
|
-
let name = typeof context == 'object' ? context.name : context;
|
77
|
+
return function (target, { name }) {
|
80
78
|
if (typeof name == 'symbol') {
|
81
79
|
console.warn('Symbol used for struct member name will be coerced to string: ' + name.toString());
|
82
80
|
name = name.toString();
|
83
81
|
}
|
84
|
-
(
|
82
|
+
if (typeof target != 'object' || typeof target != 'function') {
|
83
|
+
throw new TypeError('Invalid member for struct field');
|
84
|
+
}
|
85
|
+
if (!('constructor' in target)) {
|
86
|
+
throw new TypeError('Invalid member for struct field');
|
87
|
+
}
|
88
|
+
target.constructor[init] ||= [];
|
85
89
|
target.constructor[init].push({ name, type, length });
|
86
90
|
};
|
87
91
|
}
|
@@ -147,7 +151,7 @@ export function deserialize(instance, _buffer) {
|
|
147
151
|
continue;
|
148
152
|
}
|
149
153
|
if (length > 0) {
|
150
|
-
object
|
154
|
+
object ||= [];
|
151
155
|
}
|
152
156
|
const Type = capitalize(type);
|
153
157
|
const fn = ('get' + Type);
|
@@ -164,13 +168,13 @@ export function deserialize(instance, _buffer) {
|
|
164
168
|
}
|
165
169
|
}
|
166
170
|
function _member(type) {
|
167
|
-
function
|
171
|
+
function decorator(targetOrLength, context) {
|
168
172
|
if (typeof targetOrLength == 'number') {
|
169
173
|
return member(type, targetOrLength);
|
170
174
|
}
|
171
175
|
return member(type)(targetOrLength, context);
|
172
176
|
}
|
173
|
-
return
|
177
|
+
return decorator;
|
174
178
|
}
|
175
179
|
/**
|
176
180
|
* Shortcut types
|
package/package.json
CHANGED
package/src/struct.ts
CHANGED
@@ -133,13 +133,20 @@ export function struct(options: Partial<StructOptions> = {}) {
|
|
133
133
|
* Decorates a class member to be serialized
|
134
134
|
*/
|
135
135
|
export function member(type: ValidPrimitiveType | ClassLike, length?: number) {
|
136
|
-
return function (target: object,
|
137
|
-
let name = typeof context == 'object' ? context.name : context;
|
136
|
+
return function (target: object, { name }: ClassMemberDecoratorContext) {
|
138
137
|
if (typeof name == 'symbol') {
|
139
138
|
console.warn('Symbol used for struct member name will be coerced to string: ' + name.toString());
|
140
139
|
name = name.toString();
|
141
140
|
}
|
142
141
|
|
142
|
+
if (typeof target != 'object' || typeof target != 'function') {
|
143
|
+
throw new TypeError('Invalid member for struct field');
|
144
|
+
}
|
145
|
+
|
146
|
+
if (!('constructor' in target)) {
|
147
|
+
throw new TypeError('Invalid member for struct field');
|
148
|
+
}
|
149
|
+
|
143
150
|
target.constructor[init] ||= [];
|
144
151
|
target.constructor[init].push({ name, type, length } satisfies MemberInit);
|
145
152
|
};
|
@@ -244,16 +251,16 @@ export function deserialize(instance: unknown, _buffer: ArrayBuffer | ArrayBuffe
|
|
244
251
|
}
|
245
252
|
|
246
253
|
function _member<T extends ValidPrimitiveType>(type: T) {
|
247
|
-
function
|
248
|
-
function
|
249
|
-
function
|
254
|
+
function decorator(length?: number): (target: object, context: ClassMemberDecoratorContext) => void;
|
255
|
+
function decorator(target: object, context: ClassMemberDecoratorContext): void;
|
256
|
+
function decorator(targetOrLength: object | number, context?: ClassMemberDecoratorContext) {
|
250
257
|
if (typeof targetOrLength == 'number') {
|
251
258
|
return member(type, targetOrLength);
|
252
259
|
}
|
253
260
|
|
254
261
|
return member(type)(targetOrLength, context);
|
255
262
|
}
|
256
|
-
return
|
263
|
+
return decorator;
|
257
264
|
}
|
258
265
|
|
259
266
|
/**
|