utilium 0.3.0 → 0.3.2
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 +27 -1
- package/dist/struct.js +29 -4
- package/package.json +1 -1
- package/src/struct.ts +31 -1
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
@@ -1,16 +1,42 @@
|
|
1
1
|
import { ClassLike } from './types.js';
|
2
2
|
export type PrimitiveType = `${'int' | 'uint'}${8 | 16 | 32 | 64}` | `float${32 | 64}`;
|
3
3
|
export type ValidPrimitiveType = PrimitiveType | Capitalize<PrimitiveType> | 'char';
|
4
|
+
/**
|
5
|
+
* Options for struct initialization
|
6
|
+
*/
|
4
7
|
export interface StructOptions {
|
5
8
|
align: number;
|
6
9
|
bigEndian: boolean;
|
7
10
|
}
|
11
|
+
/**
|
12
|
+
* Gets the size in bytes of a type
|
13
|
+
*/
|
8
14
|
export declare function sizeof(type: ValidPrimitiveType | ClassLike | object): number;
|
15
|
+
/**
|
16
|
+
* Aligns a number
|
17
|
+
*/
|
9
18
|
export declare function align(value: number, alignment: number): number;
|
19
|
+
/**
|
20
|
+
* Decorates a class as a struct
|
21
|
+
*/
|
10
22
|
export declare function struct(options?: Partial<StructOptions>): (target: ClassLike, _?: ClassDecoratorContext) => void;
|
23
|
+
/**
|
24
|
+
* Decorates a class member to be serialized
|
25
|
+
*/
|
11
26
|
export declare function member(type: ValidPrimitiveType | ClassLike, length?: number): (target: object, context?: ClassMemberDecoratorContext | string | symbol) => void;
|
27
|
+
/**
|
28
|
+
* Serializes a struct into a Uint8Array
|
29
|
+
*/
|
12
30
|
export declare function serialize(instance: unknown): Uint8Array;
|
13
|
-
|
31
|
+
/**
|
32
|
+
* Deserializes a struct from a Uint8Array
|
33
|
+
*/
|
34
|
+
export declare function deserialize(instance: unknown, _buffer: ArrayBuffer | ArrayBufferView): void;
|
35
|
+
/**
|
36
|
+
* Shortcut types
|
37
|
+
*
|
38
|
+
* Instead of writing `@member(type)` you can write `@types.type`, or `@types.type(length)` for arrays
|
39
|
+
*/
|
14
40
|
export declare const types: {
|
15
41
|
int8: {
|
16
42
|
(length?: number): (target: object, context?: string | symbol | ClassMemberDecoratorContext) => void;
|
package/dist/struct.js
CHANGED
@@ -22,6 +22,9 @@ function isInstance(arg) {
|
|
22
22
|
function isStruct(arg) {
|
23
23
|
return isInstance(arg) || isStatic(arg);
|
24
24
|
}
|
25
|
+
/**
|
26
|
+
* Gets the size in bytes of a type
|
27
|
+
*/
|
25
28
|
export function sizeof(type) {
|
26
29
|
// primitive
|
27
30
|
if (typeof type == 'string') {
|
@@ -36,13 +39,19 @@ export function sizeof(type) {
|
|
36
39
|
const meta = metadata in type ? type[metadata] : type.constructor[metadata];
|
37
40
|
return meta.size;
|
38
41
|
}
|
42
|
+
/**
|
43
|
+
* Aligns a number
|
44
|
+
*/
|
39
45
|
export function align(value, alignment) {
|
40
46
|
return Math.ceil(value / alignment) * alignment;
|
41
47
|
}
|
48
|
+
/**
|
49
|
+
* Decorates a class as a struct
|
50
|
+
*/
|
42
51
|
export function struct(options = {}) {
|
43
52
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
44
53
|
return function (target, _) {
|
45
|
-
target[init]
|
54
|
+
target[init] ||= [];
|
46
55
|
let size = 0;
|
47
56
|
const members = new Map();
|
48
57
|
for (const { name, type, length } of target[init]) {
|
@@ -61,18 +70,26 @@ export function struct(options = {}) {
|
|
61
70
|
delete target[init];
|
62
71
|
};
|
63
72
|
}
|
73
|
+
/**
|
74
|
+
* Decorates a class member to be serialized
|
75
|
+
*/
|
64
76
|
export function member(type, length) {
|
65
77
|
return function (target, context) {
|
66
|
-
var _a;
|
67
78
|
let name = typeof context == 'object' ? context.name : context;
|
68
79
|
if (typeof name == 'symbol') {
|
69
80
|
console.warn('Symbol used for struct member name will be coerced to string: ' + name.toString());
|
70
81
|
name = name.toString();
|
71
82
|
}
|
72
|
-
(
|
83
|
+
if ((typeof target != 'object' || typeof target != 'function') && !('constructor' in target)) {
|
84
|
+
throw new TypeError('Invalid member for struct field');
|
85
|
+
}
|
86
|
+
target.constructor[init] ||= [];
|
73
87
|
target.constructor[init].push({ name, type, length });
|
74
88
|
};
|
75
89
|
}
|
90
|
+
/**
|
91
|
+
* Serializes a struct into a Uint8Array
|
92
|
+
*/
|
76
93
|
export function serialize(instance) {
|
77
94
|
if (!isInstance(instance)) {
|
78
95
|
throw new TypeError('Can not serialize, not a struct instance');
|
@@ -106,6 +123,9 @@ export function serialize(instance) {
|
|
106
123
|
}
|
107
124
|
return buffer;
|
108
125
|
}
|
126
|
+
/**
|
127
|
+
* Deserializes a struct from a Uint8Array
|
128
|
+
*/
|
109
129
|
export function deserialize(instance, _buffer) {
|
110
130
|
if (!isInstance(instance)) {
|
111
131
|
throw new TypeError('Can not deserialize, not a struct instance');
|
@@ -129,7 +149,7 @@ export function deserialize(instance, _buffer) {
|
|
129
149
|
continue;
|
130
150
|
}
|
131
151
|
if (length > 0) {
|
132
|
-
object
|
152
|
+
object ||= [];
|
133
153
|
}
|
134
154
|
const Type = capitalize(type);
|
135
155
|
const fn = ('get' + Type);
|
@@ -154,4 +174,9 @@ function _member(type) {
|
|
154
174
|
}
|
155
175
|
return _;
|
156
176
|
}
|
177
|
+
/**
|
178
|
+
* Shortcut types
|
179
|
+
*
|
180
|
+
* Instead of writing `@member(type)` you can write `@types.type`, or `@types.type(length)` for arrays
|
181
|
+
*/
|
157
182
|
export const types = Object.fromEntries(validPrimitiveTypes.map(t => [t, _member(t)]));
|
package/package.json
CHANGED
package/src/struct.ts
CHANGED
@@ -30,6 +30,9 @@ interface MemberInit {
|
|
30
30
|
|
31
31
|
const init = Symbol('struct_init');
|
32
32
|
|
33
|
+
/**
|
34
|
+
* Options for struct initialization
|
35
|
+
*/
|
33
36
|
export interface StructOptions {
|
34
37
|
align: number;
|
35
38
|
bigEndian: boolean;
|
@@ -71,6 +74,9 @@ function isStruct(arg: unknown): arg is Instance | Static {
|
|
71
74
|
return isInstance(arg) || isStatic(arg);
|
72
75
|
}
|
73
76
|
|
77
|
+
/**
|
78
|
+
* Gets the size in bytes of a type
|
79
|
+
*/
|
74
80
|
export function sizeof(type: ValidPrimitiveType | ClassLike | object): number {
|
75
81
|
// primitive
|
76
82
|
if (typeof type == 'string') {
|
@@ -89,10 +95,16 @@ export function sizeof(type: ValidPrimitiveType | ClassLike | object): number {
|
|
89
95
|
return meta.size;
|
90
96
|
}
|
91
97
|
|
98
|
+
/**
|
99
|
+
* Aligns a number
|
100
|
+
*/
|
92
101
|
export function align(value: number, alignment: number): number {
|
93
102
|
return Math.ceil(value / alignment) * alignment;
|
94
103
|
}
|
95
104
|
|
105
|
+
/**
|
106
|
+
* Decorates a class as a struct
|
107
|
+
*/
|
96
108
|
export function struct(options: Partial<StructOptions> = {}) {
|
97
109
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
98
110
|
return function (target: ClassLike, _?: ClassDecoratorContext) {
|
@@ -117,6 +129,9 @@ export function struct(options: Partial<StructOptions> = {}) {
|
|
117
129
|
};
|
118
130
|
}
|
119
131
|
|
132
|
+
/**
|
133
|
+
* Decorates a class member to be serialized
|
134
|
+
*/
|
120
135
|
export function member(type: ValidPrimitiveType | ClassLike, length?: number) {
|
121
136
|
return function (target: object, context?: ClassMemberDecoratorContext | string | symbol) {
|
122
137
|
let name = typeof context == 'object' ? context.name : context;
|
@@ -125,11 +140,18 @@ export function member(type: ValidPrimitiveType | ClassLike, length?: number) {
|
|
125
140
|
name = name.toString();
|
126
141
|
}
|
127
142
|
|
143
|
+
if ((typeof target != 'object' || typeof target != 'function') && !('constructor' in target)) {
|
144
|
+
throw new TypeError('Invalid member for struct field');
|
145
|
+
}
|
146
|
+
|
128
147
|
target.constructor[init] ||= [];
|
129
148
|
target.constructor[init].push({ name, type, length } satisfies MemberInit);
|
130
149
|
};
|
131
150
|
}
|
132
151
|
|
152
|
+
/**
|
153
|
+
* Serializes a struct into a Uint8Array
|
154
|
+
*/
|
133
155
|
export function serialize(instance: unknown): Uint8Array {
|
134
156
|
if (!isInstance(instance)) {
|
135
157
|
throw new TypeError('Can not serialize, not a struct instance');
|
@@ -172,7 +194,10 @@ export function serialize(instance: unknown): Uint8Array {
|
|
172
194
|
return buffer;
|
173
195
|
}
|
174
196
|
|
175
|
-
|
197
|
+
/**
|
198
|
+
* Deserializes a struct from a Uint8Array
|
199
|
+
*/
|
200
|
+
export function deserialize(instance: unknown, _buffer: ArrayBuffer | ArrayBufferView) {
|
176
201
|
if (!isInstance(instance)) {
|
177
202
|
throw new TypeError('Can not deserialize, not a struct instance');
|
178
203
|
}
|
@@ -235,4 +260,9 @@ function _member<T extends ValidPrimitiveType>(type: T) {
|
|
235
260
|
return _;
|
236
261
|
}
|
237
262
|
|
263
|
+
/**
|
264
|
+
* Shortcut types
|
265
|
+
*
|
266
|
+
* Instead of writing `@member(type)` you can write `@types.type`, or `@types.type(length)` for arrays
|
267
|
+
*/
|
238
268
|
export const types = Object.fromEntries(validPrimitiveTypes.map(t => [t, _member(t)])) as { [K in ValidPrimitiveType]: ReturnType<typeof _member<K>> };
|