@rotu/structview 0.9.0 → 0.10.1

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/esm/core.d.ts CHANGED
@@ -2,7 +2,7 @@
2
2
  * Tools for declaring a binary struct as a class with properties
3
3
  * @module
4
4
  */
5
- import type { AnyStruct, MixinFromProps, StructConstructor, StructPropertyDescriptor, SubclassWithProperties } from "./types.js";
5
+ import type { AnyStruct, MixinFromProps, StructConstructor, SubclassWithProperties } from "./types.js";
6
6
  export declare const dataViewSymbol: unique symbol;
7
7
  /**
8
8
  * Get the underlying DataView of a struct
@@ -17,13 +17,7 @@ export declare function structDataView(struct: AnyStruct): DataView;
17
17
  * @param end byte offset of the end of the subrange
18
18
  * @returns region of the given struct.
19
19
  */
20
- export declare function structBytes(struct: AnyStruct, start?: number, end?: number): Uint8Array<ArrayBufferLike>;
21
- /**
22
- * Define a descriptor based on a dataview of the struct
23
- * @param fieldGetter function which, given a dataview, returns
24
- * @returns
25
- */
26
- export declare function fromDataView<Fn extends (dv: DataView) => unknown>(fieldGetter: Fn): StructPropertyDescriptor<ReturnType<Fn>>;
20
+ export declare function structBytes(struct: AnyStruct, start?: number, end?: number): Uint8Array;
27
21
  /**
28
22
  * Base class for a structured binary object
29
23
  * Note there are no predeclared string-keyed properties - all property names are reserved for user-defined fields
package/esm/core.d.ts.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"core.d.ts","sourceRoot":"","sources":["../src/core.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EACV,SAAS,EAET,cAAc,EACd,iBAAiB,EACjB,wBAAwB,EACxB,sBAAsB,EACvB,MAAM,YAAY,CAAA;AAEnB,eAAO,MAAM,cAAc,eAAgC,CAAA;AAE3D;;;;GAIG;AACH,wBAAgB,cAAc,CAAC,MAAM,EAAE,SAAS,GAAG,QAAQ,CAM1D;AACD;;;;;;GAMG;AACH,wBAAgB,WAAW,CAAC,MAAM,EAAE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,EACpE,mBAAmB,EAAE,KAAK,GACzB,sBAAsB,CAAC,OAAO,MAAM,EAAE,cAAc,CAAC,KAAK,CAAC,CAAC,CAE9D;AAED;;;;GAIG;AACH,wBAAgB,WAAW,CAAC,IAAI,SAAS,MAAM,EAC7C,YAAY,EAAE;IACZ,kDAAkD;IAClD,QAAQ,CAAC,MAAM,EAAE,iBAAiB,CAAC,IAAI,CAAC,CAAA;IACxC,6DAA6D;IAC7D,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAA;IAC3B,oIAAoI;IACpI,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,CAAA;CACzB,GACA,iBAAiB,CAClB;IACE,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAA;IACvB,OAAO,CAAC,CAAC,EAAE,MAAM,GAAG,IAAI,CAAA;CACzB,GAAG,QAAQ,CAAC,IAAI,CAAC,CACnB,CA+CA"}
package/esm/core.js CHANGED
@@ -36,20 +36,6 @@ export function structBytes(struct, start, end) {
36
36
  console.assert(end <= dv.byteLength);
37
37
  return new Uint8Array(dv.buffer, dv.byteOffset + start, end - start);
38
38
  }
39
- /**
40
- * Define a descriptor based on a dataview of the struct
41
- * @param fieldGetter function which, given a dataview, returns
42
- * @returns
43
- */
44
- export function fromDataView(fieldGetter) {
45
- return {
46
- enumerable: true,
47
- get() {
48
- const dv = this[dataViewSymbol];
49
- return fieldGetter(dv);
50
- },
51
- };
52
- }
53
39
  /**
54
40
  * Base class for a structured binary object
55
41
  * Note there are no predeclared string-keyed properties - all property names are reserved for user-defined fields
package/esm/fields.d.ts CHANGED
@@ -2,7 +2,7 @@
2
2
  * Factories for property descriptors representing fields in a binary struct
3
3
  * @module
4
4
  */
5
- import type { StructConstructor, StructPropertyDescriptor } from "./types.js";
5
+ import type { StructConstructor, StructPropertyDescriptor, TypedArraySpecies } from "./types.js";
6
6
  /**
7
7
  * Field for a 8-bit unsigned integer
8
8
  */
@@ -68,6 +68,12 @@ export declare function string(fieldOffset: number, byteLength: number): StructP
68
68
  * True will be stored as 1
69
69
  */
70
70
  export declare function bool(fieldOffset: number): StructPropertyDescriptor<boolean>;
71
+ /**
72
+ * Define a descriptor based on a dataview of the struct
73
+ * @param fieldGetter function which, given a dataview, returns
74
+ * @returns
75
+ */
76
+ export declare function fromDataView<T>(fieldGetter: (dv: DataView) => T): StructPropertyDescriptor<T>;
71
77
  /**
72
78
  * Field for an embedded struct
73
79
  * @param ctor constructor for the inner struct
@@ -76,6 +82,23 @@ export declare function bool(fieldOffset: number): StructPropertyDescriptor<bool
76
82
  * @returns property descriptor for a struct
77
83
  */
78
84
  export declare function substruct<T extends object>(ctor: StructConstructor<T>, byteOffset?: number, bytelength?: number): StructPropertyDescriptor<T>;
85
+ /**
86
+ * Field for a typed array
87
+ *
88
+ * @remarks
89
+ *
90
+ * I'm not totally happy with this.
91
+ * - TypedArray does not support endianness.
92
+ * - Changing the length property of the parent struct will not change the length of the returned value. `a=x.ar; x.arlength=2;` will not change a's length (though a will still be a live view of the underlying buffer).
93
+ *
94
+ * @param fieldOffset where the array starts relative to the parent struct
95
+ */
96
+ export declare function typedArray<T>(fieldOffset: number, kwargs: {
97
+ /** length or property name for the length of the array */
98
+ readonly length: number | string | undefined;
99
+ /** TypedArray constructor */
100
+ readonly species: TypedArraySpecies<T>;
101
+ }): StructPropertyDescriptor<T>;
79
102
  /**
80
103
  * Field for a big-endian 16-bit unsigned integer
81
104
  */
@@ -1 +1 @@
1
- {"version":3,"file":"fields.d.ts","sourceRoot":"","sources":["../src/fields.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAGH,OAAO,KAAK,EAAE,iBAAiB,EAAE,wBAAwB,EAAE,MAAM,YAAY,CAAA;AAE7E;;GAEG;AACH,wBAAgB,EAAE,CAAC,WAAW,EAAE,MAAM,GAAG,wBAAwB,CAAC,MAAM,CAAC,CAUxE;AACD;;GAEG;AACH,wBAAgB,GAAG,CAAC,WAAW,EAAE,MAAM,GAAG,wBAAwB,CAAC,MAAM,CAAC,CAUz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AAC,MAAM,CAAC,CAU3E;AACD;;GAEG;AACH,wBAAgB,KAAK,CAAC,WAAW,EAAE,MAAM,GAAG,wBAAwB,CAAC,MAAM,CAAC,CAU3E;AACD;;GAEG;AACH,wBAAgB,KAAK,CAAC,WAAW,EAAE,MAAM,GAAG,wBAAwB,CAAC,MAAM,CAAC,CAU3E;AACD;;GAEG;AACH,wBAAgB,KAAK,CAAC,WAAW,EAAE,MAAM,GAAG,wBAAwB,CAAC,MAAM,CAAC,CAU3E;AACD;;GAEG;AACH,wBAAgB,KAAK,CAAC,WAAW,EAAE,MAAM,GAAG,wBAAwB,CAAC,MAAM,CAAC,CAU3E;AACD;;GAEG;AACH,wBAAgB,KAAK,CAAC,WAAW,EAAE,MAAM,GAAG,wBAAwB,CAAC,MAAM,CAAC,CAU3E;AAED;;GAEG;AACH,wBAAgB,KAAK,CAAC,WAAW,EAAE,MAAM,GAAG,wBAAwB,CAAC,MAAM,CAAC,CAgB3E;AAED;;GAEG;AACH,wBAAgB,KAAK,CAAC,WAAW,EAAE,MAAM,GAAG,wBAAwB,CAAC,MAAM,CAAC,CAU3E;AAED;;GAEG;AACH,wBAAgB,KAAK,CAAC,WAAW,EAAE,MAAM,GAAG,wBAAwB,CAAC,MAAM,CAAC,CAU3E"}
package/esm/fields.js CHANGED
@@ -2,7 +2,7 @@
2
2
  * Factories for property descriptors representing fields in a binary struct
3
3
  * @module
4
4
  */
5
- import { fromDataView, structBytes, structDataView } from "./core.js";
5
+ import { structBytes, structDataView } from "./core.js";
6
6
  /**
7
7
  * Field for a 8-bit unsigned integer
8
8
  */
@@ -243,6 +243,20 @@ export function bool(fieldOffset) {
243
243
  },
244
244
  };
245
245
  }
246
+ /**
247
+ * Define a descriptor based on a dataview of the struct
248
+ * @param fieldGetter function which, given a dataview, returns
249
+ * @returns
250
+ */
251
+ export function fromDataView(fieldGetter) {
252
+ return {
253
+ enumerable: true,
254
+ get() {
255
+ const dv = structDataView(this);
256
+ return fieldGetter(dv);
257
+ },
258
+ };
259
+ }
246
260
  /**
247
261
  * Field for an embedded struct
248
262
  * @param ctor constructor for the inner struct
@@ -261,6 +275,37 @@ export function substruct(ctor, byteOffset, bytelength) {
261
275
  }]);
262
276
  });
263
277
  }
278
+ /**
279
+ * Field for a typed array
280
+ *
281
+ * @remarks
282
+ *
283
+ * I'm not totally happy with this.
284
+ * - TypedArray does not support endianness.
285
+ * - Changing the length property of the parent struct will not change the length of the returned value. `a=x.ar; x.arlength=2;` will not change a's length (though a will still be a live view of the underlying buffer).
286
+ *
287
+ * @param fieldOffset where the array starts relative to the parent struct
288
+ */
289
+ export function typedArray(fieldOffset, kwargs) {
290
+ const { length, species } = kwargs;
291
+ return {
292
+ enumerable: true,
293
+ get() {
294
+ const dv = structDataView(this);
295
+ let lengthValue;
296
+ if (typeof length === "undefined") {
297
+ lengthValue = Math.floor((dv.byteLength - fieldOffset) / species.BYTES_PER_ELEMENT);
298
+ }
299
+ else if (typeof length === "number") {
300
+ lengthValue = length;
301
+ }
302
+ else if (typeof length === "string") {
303
+ lengthValue = Reflect.get(this, length);
304
+ }
305
+ return new species(dv.buffer, dv.byteOffset + fieldOffset, lengthValue);
306
+ },
307
+ };
308
+ }
264
309
  /**
265
310
  * Field for a big-endian 16-bit unsigned integer
266
311
  */
package/esm/types.d.ts CHANGED
@@ -38,4 +38,8 @@ export type StructConstructor<T extends object> = {
38
38
  readonly byteLength: number;
39
39
  }): T;
40
40
  };
41
+ export type TypedArraySpecies<T> = {
42
+ new (buffer: ArrayBufferLike, byteOffset?: number, length?: number): T;
43
+ readonly BYTES_PER_ELEMENT: number;
44
+ };
41
45
  //# sourceMappingURL=types.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,WAAW,CAAA;AAE/C,MAAM,MAAM,SAAS,GAAG;IACtB,QAAQ,CAAC,CAAC,c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AAmB,CAAC,MAAM,CAAC,CAAC,GACvE,CAAC,GACD,OAAO;CACZ,CAAA;AAED;;GAEG;AACH,MAAM,MAAM,wBAAwB,CAAC,CAAC,IAClC,QAAQ,CAAC,SAAS,CAAC,GACnB,mBAAmB,CAAC,CAAC,CAAC,CAAA;AAE1B,MAAM,MAAM,iBAAiB,CAAC,CAAC,SAAS,MAAM,IAAI;IAChD,KAAK,GAAG,EAAE;QACR,QAAQ,CAAC,MAAM,EAAE,eAAe,CAAA;QAChC,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAA;QAC3B,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAA;KAC5B,GAAG,CAAC,CAAA;CACN,CAAA;AAED,MAAM,MAAM,iBAAiB,CAAC,CAAC,IAAI;IACjC,KACE,MAAM,EAAE,eAAe,EACvB,UAAU,CAAC,EAAE,MAAM,EACnB,MAAM,CAAC,EAAE,MAAM,GACd,CAAC,CAAA;IACJ,QAAQ,CAAC,iBAAiB,EAAE,MAAM,CAAA;CACnC,CAAA"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rotu/structview",
3
- "version": "0.9.0+dnt",
3
+ "version": "0.10.1+dnt",
4
4
  "description": "Read and write structured binary data with typesafe views",
5
5
  "repository": {
6
6
  "type": "git",