utilium 1.7.4 → 1.7.5

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/struct.js CHANGED
@@ -58,9 +58,11 @@ function _memberLength(struct, length, name) {
58
58
  }
59
59
  /** Compute the size of a struct including dynamically sized members */
60
60
  function _structSize() {
61
- const { staticSize, members } = this[symbol_metadata(this)].struct;
61
+ const { staticSize, members } = this.constructor[symbol_metadata(this.constructor)].struct;
62
62
  let size = staticSize;
63
63
  for (const [name, { type, length: key }] of members) {
64
+ if (typeof key != 'string')
65
+ continue;
64
66
  size += sizeof(type) * _memberLength(this, key, name);
65
67
  }
66
68
  return size;
@@ -123,7 +125,7 @@ export function serialize(instance) {
123
125
  const view = new DataView(buffer.buffer);
124
126
  // for unions we should write members in ascending last modified order, but we don't have that info.
125
127
  for (const [name, { type, length: rawLength, offset }] of members) {
126
- const length = _memberLength(instance.constructor, rawLength, name);
128
+ const length = _memberLength(instance, rawLength, name);
127
129
  for (let i = 0; i < (length || 1); i++) {
128
130
  const iOff = offset + sizeof(type) * i;
129
131
  let value = length > 0 ? instance[name][i] : instance[name];
@@ -174,7 +176,7 @@ export function deserialize(instance, _buffer) {
174
176
  const { options, members } = instance.constructor[symbol_metadata(instance.constructor)].struct;
175
177
  const view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength);
176
178
  for (const [name, { type, offset, length: rawLength }] of members) {
177
- const length = _memberLength(instance.constructor, rawLength, name);
179
+ const length = _memberLength(instance, rawLength, name);
178
180
  for (let i = 0; i < (length || 1); i++) {
179
181
  let object = length > 0 ? instance[name] : instance;
180
182
  const key = length > 0 ? i : name, iOff = offset + sizeof(type) * i;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "utilium",
3
- "version": "1.7.4",
3
+ "version": "1.7.5",
4
4
  "description": "Typescript utilities",
5
5
  "funding": {
6
6
  "type": "individual",
package/src/struct.ts CHANGED
@@ -2,6 +2,7 @@ import { toUint8Array } from './buffer.js';
2
2
  import * as primitive from './internal/primitives.js';
3
3
  import type {
4
4
  DecoratorContext,
5
+ Instance,
5
6
  InstanceLike,
6
7
  Member,
7
8
  MemberContext,
@@ -66,7 +67,7 @@ export function align(value: number, alignment: number): number {
66
67
  * @param name The name of the array field— only used for errors
67
68
  */
68
69
  function _memberLength<T extends Metadata>(
69
- struct: Static<T>,
70
+ struct: Instance<T>,
70
71
  length: string | number | undefined,
71
72
  name: string
72
73
  ): number {
@@ -86,12 +87,13 @@ function _memberLength<T extends Metadata>(
86
87
  }
87
88
 
88
89
  /** Compute the size of a struct including dynamically sized members */
89
- function _structSize<T extends Metadata>(this: Static<T>) {
90
- const { staticSize, members } = this[symbol_metadata(this)].struct;
90
+ function _structSize<T extends Metadata>(this: Instance<T>) {
91
+ const { staticSize, members } = this.constructor[symbol_metadata(this.constructor)].struct;
91
92
 
92
93
  let size = staticSize;
93
94
 
94
95
  for (const [name, { type, length: key }] of members) {
96
+ if (typeof key != 'string') continue;
95
97
  size += sizeof(type) * _memberLength(this, key, name);
96
98
  }
97
99
 
@@ -168,7 +170,7 @@ export function serialize(instance: unknown): Uint8Array {
168
170
 
169
171
  // for unions we should write members in ascending last modified order, but we don't have that info.
170
172
  for (const [name, { type, length: rawLength, offset }] of members) {
171
- const length = _memberLength(instance.constructor, rawLength, name);
173
+ const length = _memberLength(instance, rawLength, name);
172
174
  for (let i = 0; i < (length || 1); i++) {
173
175
  const iOff = offset + sizeof(type) * i;
174
176
 
@@ -233,7 +235,7 @@ export function deserialize(instance: unknown, _buffer: ArrayBufferLike | ArrayB
233
235
  const view = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength);
234
236
 
235
237
  for (const [name, { type, offset, length: rawLength }] of members) {
236
- const length = _memberLength(instance.constructor, rawLength, name);
238
+ const length = _memberLength(instance, rawLength, name);
237
239
  for (let i = 0; i < (length || 1); i++) {
238
240
  let object = length > 0 ? instance[name] : instance;
239
241
  const key = length > 0 ? i : name,