utilium 1.7.17 → 1.8.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/dist/internal/struct.d.ts +1 -3
- package/dist/struct.js +6 -2
- package/package.json +1 -1
- package/src/internal/struct.ts +1 -5
- package/src/struct.ts +7 -2
@@ -2,11 +2,8 @@ import type { ClassLike } from '../types.js';
|
|
2
2
|
import type * as primitive from './primitives.js';
|
3
3
|
declare global {
|
4
4
|
interface SymbolConstructor {
|
5
|
-
/** User-defined size */
|
6
5
|
readonly size: unique symbol;
|
7
|
-
/** User-defined serialization */
|
8
6
|
readonly serialize: unique symbol;
|
9
|
-
/** User-defined deserialization */
|
10
7
|
readonly deserialize: unique symbol;
|
11
8
|
}
|
12
9
|
}
|
@@ -68,6 +65,7 @@ export interface Static<T extends Metadata = Metadata> {
|
|
68
65
|
}
|
69
66
|
export interface StaticLike<T extends Metadata = Metadata> extends ClassLike {
|
70
67
|
[Symbol.metadata]?: _DecoratorMetadata<T> | null;
|
68
|
+
new (): unknown;
|
71
69
|
}
|
72
70
|
export declare function isValidMetadata<T extends Metadata = Metadata>(arg: unknown): arg is DecoratorMetadata & {
|
73
71
|
struct: T;
|
package/dist/struct.js
CHANGED
@@ -229,8 +229,10 @@ export function deserialize(instance, _buffer) {
|
|
229
229
|
for (let i = 0; i < Math.abs(length); i++) {
|
230
230
|
let object = length != -1 ? instance[member.name] : instance;
|
231
231
|
const key = length != -1 ? i : member.name;
|
232
|
+
const isNullish = object[key] === null || object[key] === undefined;
|
233
|
+
const needsAllocation = isNullish && isStatic(member.type) && member.type[Symbol.metadata].struct.isDynamic;
|
232
234
|
offset = nextOffset;
|
233
|
-
if (!isInstance(object[key]))
|
235
|
+
if (!isInstance(object[key]) && !needsAllocation)
|
234
236
|
nextOffset += sizeof(member.type);
|
235
237
|
if (typeof instance[member.name] == 'string') {
|
236
238
|
instance[member.name] =
|
@@ -240,7 +242,9 @@ export function deserialize(instance, _buffer) {
|
|
240
242
|
continue;
|
241
243
|
}
|
242
244
|
if (!primitive.isType(member.type)) {
|
243
|
-
if (
|
245
|
+
if (needsAllocation && isStatic(member.type))
|
246
|
+
object[key] ??= new member.type();
|
247
|
+
else if (isNullish)
|
244
248
|
continue;
|
245
249
|
deserialize(object[key], new Uint8Array(buffer.subarray(offset)));
|
246
250
|
nextOffset += sizeof(object[key]);
|
package/package.json
CHANGED
package/src/internal/struct.ts
CHANGED
@@ -3,13 +3,8 @@ import type * as primitive from './primitives.js';
|
|
3
3
|
|
4
4
|
declare global {
|
5
5
|
interface SymbolConstructor {
|
6
|
-
/** User-defined size */
|
7
6
|
readonly size: unique symbol;
|
8
|
-
|
9
|
-
/** User-defined serialization */
|
10
7
|
readonly serialize: unique symbol;
|
11
|
-
|
12
|
-
/** User-defined deserialization */
|
13
8
|
readonly deserialize: unique symbol;
|
14
9
|
}
|
15
10
|
}
|
@@ -102,6 +97,7 @@ export interface Static<T extends Metadata = Metadata> {
|
|
102
97
|
|
103
98
|
export interface StaticLike<T extends Metadata = Metadata> extends ClassLike {
|
104
99
|
[Symbol.metadata]?: _DecoratorMetadata<T> | null;
|
100
|
+
new (): unknown;
|
105
101
|
}
|
106
102
|
|
107
103
|
export function isValidMetadata<T extends Metadata = Metadata>(
|
package/src/struct.ts
CHANGED
@@ -318,8 +318,12 @@ export function deserialize(instance: unknown, _buffer: ArrayBufferLike | ArrayB
|
|
318
318
|
let object = length != -1 ? instance[member.name] : instance;
|
319
319
|
const key = length != -1 ? i : member.name;
|
320
320
|
|
321
|
+
const isNullish = object[key] === null || object[key] === undefined;
|
322
|
+
|
323
|
+
const needsAllocation = isNullish && isStatic(member.type) && member.type[Symbol.metadata].struct.isDynamic;
|
324
|
+
|
321
325
|
offset = nextOffset;
|
322
|
-
if (!isInstance(object[key])) nextOffset += sizeof(member.type);
|
326
|
+
if (!isInstance(object[key]) && !needsAllocation) nextOffset += sizeof(member.type);
|
323
327
|
|
324
328
|
if (typeof instance[member.name] == 'string') {
|
325
329
|
instance[member.name] =
|
@@ -330,7 +334,8 @@ export function deserialize(instance: unknown, _buffer: ArrayBufferLike | ArrayB
|
|
330
334
|
}
|
331
335
|
|
332
336
|
if (!primitive.isType(member.type)) {
|
333
|
-
if (
|
337
|
+
if (needsAllocation && isStatic(member.type)) object[key] ??= new member.type();
|
338
|
+
else if (isNullish) continue;
|
334
339
|
|
335
340
|
deserialize(object[key], new Uint8Array(buffer.subarray(offset)));
|
336
341
|
nextOffset += sizeof(object[key]);
|