@rotu/structview 0.7.3 → 0.9.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/esm/mod.js CHANGED
@@ -2,376 +2,5 @@
2
2
  * Strongly typed classes for accessing binary data in a structured way
3
3
  * @module
4
4
  */
5
- var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
6
- if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
7
- if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
8
- return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
9
- };
10
- var _a;
11
- const dataViewSymbol = Symbol.for("Struct.dataview");
12
- /**
13
- * Get the underlying DataView of a struct
14
- * @param struct
15
- * @returns
16
- */
17
- export function structDataView(struct) {
18
- const result = struct[dataViewSymbol];
19
- if (!(result instanceof DataView)) {
20
- throw new TypeError("not a struct");
21
- }
22
- return struct[dataViewSymbol];
23
- }
24
- /**
25
- * Helper method to create a view of a contiguous subregion of a Struct's memory
26
- * @param struct
27
- * @param start byte offset to start
28
- * @param end byte offset of the end of the subrange
29
- * @returns region of the given struct.
30
- */
31
- function structBytes(struct, start, end) {
32
- const dv = structDataView(struct);
33
- start ??= 0;
34
- end ??= dv.byteLength;
35
- console.assert(start <= end);
36
- console.assert(end <= dv.byteLength);
37
- return new Uint8Array(dv.buffer, dv.byteOffset + start, end - start);
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
- /**
54
- * Field for a 8-bit unsigned integer
55
- */
56
- export function u8(fieldOffset) {
57
- return {
58
- enumerable: true,
59
- get() {
60
- return structDataView(this).getUint8(fieldOffset);
61
- },
62
- set(value) {
63
- structDataView(this).setUint8(fieldOffset, value);
64
- },
65
- };
66
- }
67
- /**
68
- * Field for a little-endian 16-bit unsigned integer
69
- */
70
- export function u16(fieldOffset) {
71
- return {
72
- enumerable: true,
73
- get() {
74
- return structDataView(this).getUint16(fieldOffset, true);
75
- },
76
- set(value) {
77
- structDataView(this).setUint16(fieldOffset, value, true);
78
- },
79
- };
80
- }
81
- /**
82
- * Field for a little-endian 32-bit unsigned integer
83
- */
84
- export function u32(fieldOffset) {
85
- return {
86
- enumerable: true,
87
- get() {
88
- return structDataView(this).getUint32(fieldOffset, true);
89
- },
90
- set(value) {
91
- structDataView(this).setUint32(fieldOffset, value, true);
92
- },
93
- };
94
- }
95
- /**
96
- * Field for a little-endian 64-bit unsigned integer
97
- */
98
- export function u64(fieldOffset) {
99
- return {
100
- enumerable: true,
101
- get() {
102
- return structDataView(this).getBigUint64(fieldOffset, true);
103
- },
104
- set(value) {
105
- structDataView(this).setBigUint64(fieldOffset, value, true);
106
- },
107
- };
108
- }
109
- /**
110
- * Field for a little-endian 8-bit signed integer
111
- */
112
- export function i8(fieldOffset) {
113
- return {
114
- enumerable: true,
115
- get() {
116
- return structDataView(this).getInt8(fieldOffset);
117
- },
118
- set(value) {
119
- structDataView(this).setInt8(fieldOffset, value);
120
- },
121
- };
122
- }
123
- /**
124
- * Field for a little-endian 16-bit signed integer
125
- */
126
- export function i16(fieldOffset) {
127
- return {
128
- enumerable: true,
129
- get() {
130
- return structDataView(this).getInt16(fieldOffset, true);
131
- },
132
- set(value) {
133
- structDataView(this).setInt16(fieldOffset, value, true);
134
- },
135
- };
136
- }
137
- /**
138
- * Field for a little-endian 32-bit signed integer
139
- */
140
- export function i32(fieldOffset) {
141
- return {
142
- enumerable: true,
143
- get() {
144
- return structDataView(this).getInt32(fieldOffset, true);
145
- },
146
- set(value) {
147
- structDataView(this).setInt32(fieldOffset, value, true);
148
- },
149
- };
150
- }
151
- /**
152
- * Field for a little-endian 64-bit signed integer
153
- */
154
- export function i64(fieldOffset) {
155
- return {
156
- enumerable: true,
157
- get() {
158
- return structDataView(this).getBigInt64(fieldOffset, true);
159
- },
160
- set(value) {
161
- structDataView(this).setBigInt64(fieldOffset, value, true);
162
- },
163
- };
164
- }
165
- /**
166
- * Field for a little-endian 16-bit binary float (float16_t)
167
- */
168
- export function f16(fieldOffset) {
169
- if (typeof DataView.prototype.getFloat16 !== "function" ||
170
- typeof DataView.prototype.setFloat16 !== "function") {
171
- throw new TypeError("float16 is not supported in this environment");
172
- }
173
- return {
174
- enumerable: true,
175
- get() {
176
- return structDataView(this).getFloat16(fieldOffset, true);
177
- },
178
- set(value) {
179
- structDataView(this).setFloat16(fieldOffset, value, true);
180
- },
181
- };
182
- }
183
- /**
184
- * Field for a little-endian 32-bit binary float (float32_t)
185
- */
186
- export function f32(fieldOffset) {
187
- return {
188
- enumerable: true,
189
- get() {
190
- return structDataView(this).getFloat32(fieldOffset, true);
191
- },
192
- set(value) {
193
- structDataView(this).setFloat32(fieldOffset, value, true);
194
- },
195
- };
196
- }
197
- /**
198
- * Field for a little-endian 64-bit binary float (float64_t)
199
- */
200
- export function f64(fieldOffset) {
201
- return {
202
- enumerable: true,
203
- get() {
204
- return structDataView(this).getFloat64(fieldOffset, true);
205
- },
206
- set(value) {
207
- structDataView(this).setFloat64(fieldOffset, value, true);
208
- },
209
- };
210
- }
211
- /**
212
- * Field for a UTF-8 fixed-length string
213
- */
214
- export function string(fieldOffset, byteLength) {
215
- const TEXT_DECODER = new TextDecoder();
216
- const TEXT_ENCODER = new TextEncoder();
217
- return {
218
- enumerable: true,
219
- get() {
220
- const str = TEXT_DECODER.decode(structBytes(this, fieldOffset, fieldOffset + byteLength));
221
- // trim all trailing null characters
222
- return str.replace(/\0+$/, "");
223
- },
224
- set(value) {
225
- const bytes = structBytes(this, fieldOffset, fieldOffset + byteLength);
226
- bytes.fill(0);
227
- TEXT_ENCODER.encodeInto(value, bytes);
228
- },
229
- };
230
- }
231
- /**
232
- * Field for a boolean stored in a byte (0 = false, nonzero = true)
233
- * True will be stored as 1
234
- */
235
- export function bool(fieldOffset) {
236
- return {
237
- enumerable: true,
238
- get() {
239
- return Boolean(structDataView(this).getUint8(fieldOffset));
240
- },
241
- set(value) {
242
- structDataView(this).setUint8(fieldOffset, value ? 1 : 0);
243
- },
244
- };
245
- }
246
- /**
247
- * Field for an embedded struct
248
- * @param ctor constructor for the inner struct
249
- * @param byteOffset where the inner struct starts relative to the outer struct
250
- * @param bytelength the length in bytes of the inner struct
251
- * @returns property descriptor for a struct
252
- */
253
- export function substruct(ctor, byteOffset, bytelength) {
254
- return fromDataView(function (dv) {
255
- const offset2 = dv.byteOffset + (byteOffset ?? 0);
256
- const bytelength2 = bytelength ?? (dv.byteLength - (byteOffset ?? 0));
257
- return Reflect.construct(ctor, [{
258
- buffer: dv.buffer,
259
- byteOffset: offset2,
260
- byteLength: bytelength2,
261
- }]);
262
- });
263
- }
264
- /**
265
- * Base class for a structured binary object
266
- * Note there are no predeclared string-keyed properties - all property names are reserved for user-defined fields
267
- */
268
- export class Struct {
269
- get [(_a = dataViewSymbol, Symbol.toStringTag)]() {
270
- return Struct.name;
271
- }
272
- static toDataView(o) {
273
- return o[dataViewSymbol];
274
- }
275
- /**
276
- * Create a new Struct
277
- * @param arg options for creating the struct.
278
- * If options has a `.buffer` property, we will use that as the backing memory (e.g. any TypedArray or DataView).
279
- * If options has no `.buffer` property but has a `.byteLength`, we will allocate a new buffer for the object.
280
- */
281
- constructor(arg) {
282
- Object.defineProperty(this, _a, {
283
- enumerable: true,
284
- configurable: true,
285
- writable: true,
286
- value: void 0
287
- });
288
- if (typeof arg !== "object" || arg === null) {
289
- throw new TypeError("Expected argument to be an object");
290
- }
291
- Object.preventExtensions(this);
292
- if (arg.buffer) {
293
- this[dataViewSymbol] = new DataView(arg.buffer, arg.byteOffset, arg.byteLength);
294
- }
295
- else if (typeof arg.byteLength === "number") {
296
- this[dataViewSymbol] = new DataView(new ArrayBuffer(arg.byteLength + (arg.byteOffset ?? 0)), arg.byteOffset, arg.byteLength);
297
- }
298
- else {
299
- throw new TypeError("Must provide either {buffer} or {byteLength}");
300
- }
301
- }
302
- }
303
- /**
304
- * Subclass a type by adding the given property descriptors
305
- * @param ctor constructor for the base class
306
- * @param propertyDescriptors properties to add to subclass instances
307
- * @returns A new class, inheriting from the base class, with the new property descriptors added
308
- */
309
- function subclassWithProperties(ctor, propertyDescriptors) {
310
- var _b;
311
- return (_b = class extends ctor {
312
- },
313
- (() => {
314
- Object.defineProperties(_b.prototype, propertyDescriptors);
315
- })(),
316
- _b);
317
- }
318
- /**
319
- * Subclass struct by adding the given property descriptors
320
- * @param propertyDescriptors properties to add to subclass instances
321
- * @returns A new class, inheriting from `Struct`, with the new property descriptors added
322
- */
323
- export function defineStruct(propertyDescriptors) {
324
- return subclassWithProperties(Struct, propertyDescriptors);
325
- }
326
- /**
327
- * Create a new struct subclass for an array of structs
328
- * @param arrayOptions
329
- * @returns A new class, inheriting from `Struct` whose elements are statically typed structs
330
- */
331
- export function defineArray(arrayOptions) {
332
- var _StructArray_struct, _StructArray_length, _StructArray_byteStride;
333
- const { struct, byteStride, length } = arrayOptions;
334
- class StructArray extends Struct {
335
- constructor() {
336
- super(...arguments);
337
- _StructArray_struct.set(this, struct);
338
- _StructArray_length.set(this, length);
339
- _StructArray_byteStride.set(this, byteStride
340
- /**
341
- * Number of items in the array
342
- */
343
- );
344
- }
345
- /**
346
- * Number of items in the array
347
- */
348
- get length() {
349
- if (typeof __classPrivateFieldGet(this, _StructArray_length, "f") === "number") {
350
- return __classPrivateFieldGet(this, _StructArray_length, "f");
351
- }
352
- return structDataView(this).byteLength / __classPrivateFieldGet(this, _StructArray_byteStride, "f");
353
- }
354
- /**
355
- * A view of the item at the given index
356
- * @param index
357
- * @returns a new struct instance viewing the item at the given index
358
- */
359
- item(index) {
360
- const ctor = __classPrivateFieldGet(this, _StructArray_struct, "f");
361
- return new ctor(structBytes(this, __classPrivateFieldGet(this, _StructArray_byteStride, "f") * index, __classPrivateFieldGet(this, _StructArray_byteStride, "f") * (index + 1)));
362
- }
363
- /** @deprecated use item() instead */
364
- element(index) {
365
- return this.item(index);
366
- }
367
- /**
368
- * Iterate over the items in the array
369
- */
370
- *[(_StructArray_struct = new WeakMap(), _StructArray_length = new WeakMap(), _StructArray_byteStride = new WeakMap(), Symbol.iterator)]() {
371
- for (let i = 0; i < this.length; ++i) {
372
- yield this.item(i);
373
- }
374
- }
375
- }
376
- return StructArray;
377
- }
5
+ export * from "./core.js";
6
+ export * from "./fields.js";
package/esm/types.d.ts ADDED
@@ -0,0 +1,41 @@
1
+ /**
2
+ * Helper types for this library
3
+ * @module
4
+ */
5
+ import type { dataViewSymbol } from "./core.js";
6
+ export type AnyStruct = {
7
+ readonly [dataViewSymbol]: DataView;
8
+ };
9
+ export type Constructor<T> = {
10
+ new (...args: any[]): T;
11
+ };
12
+ export type SubclassWithProperties<Ctor extends Constructor<object>, Mixin> = {
13
+ [K in keyof Ctor]: Ctor[K];
14
+ } & {
15
+ new (...args: ConstructorParameters<Ctor>): InstanceType<Ctor> & {
16
+ [K in keyof Mixin]: Mixin[K];
17
+ };
18
+ };
19
+ export type TPropertyDescriptor<T> = {
20
+ enumerable?: boolean;
21
+ configurable?: boolean;
22
+ get?(): T;
23
+ set?(t: T): undefined;
24
+ value?: T;
25
+ writable?: boolean;
26
+ };
27
+ export type MixinFromProps<Props extends object> = {
28
+ -readonly [K in keyof Props]: Props[K] extends TPropertyDescriptor<infer V> ? V : unknown;
29
+ };
30
+ /**
31
+ * Type of a property descriptor for a struct
32
+ */
33
+ export type StructPropertyDescriptor<T> = ThisType<AnyStruct> & TPropertyDescriptor<T>;
34
+ export type StructConstructor<T extends object> = {
35
+ new (arg: {
36
+ readonly buffer: ArrayBufferLike;
37
+ readonly byteOffset: number;
38
+ readonly byteLength: number;
39
+ }): T;
40
+ };
41
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"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AAC,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"}
package/esm/types.js ADDED
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Helper types for this library
3
+ * @module
4
+ */
5
+ export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rotu/structview",
3
- "version": "0.7.3+dnt",
3
+ "version": "0.9.0+dnt",
4
4
  "description": "Read and write structured binary data with typesafe views",
5
5
  "repository": {
6
6
  "type": "git",
@@ -19,6 +19,7 @@
19
19
  "devDependencies": {
20
20
  "@types/node": "^20.9.0",
21
21
  "picocolors": "^1.0.0",
22
+ "uint8array-extras": "^1.5.0",
22
23
  "@deno/shim-deno": "~0.18.0"
23
24
  },
24
25
  "_generatedBy": "dnt@dev"
@@ -1 +0,0 @@
1
- {"version":3,"file":"bigendian.d.ts","sourceRoot":"","sources":["../src/bigendian.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAkB,KAAK,wBAAwB,EAAE,MAAM,UAAU,CAAA;AAExE;;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;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"}