@rotu/structview 0.6.1 → 0.7.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.
Files changed (62) hide show
  1. package/README.md +3 -3
  2. package/esm/_dnt.test_shims.d.ts.map +1 -0
  3. package/esm/bigendian.d.ts.map +1 -0
  4. package/esm/bigendian_test.d.ts.map +1 -0
  5. package/esm/deps/jsr.io/@std/assert/1.0.15/almost_equals.d.ts.map +1 -0
  6. package/esm/deps/jsr.io/@std/assert/1.0.15/array_includes.d.ts.map +1 -0
  7. package/esm/deps/jsr.io/@std/assert/1.0.15/assert.d.ts.map +1 -0
  8. package/esm/deps/jsr.io/@std/assert/1.0.15/assertion_error.d.ts.map +1 -0
  9. package/esm/deps/jsr.io/@std/assert/1.0.15/equal.d.ts.map +1 -0
  10. package/esm/deps/jsr.io/@std/assert/1.0.15/equals.d.ts.map +1 -0
  11. package/esm/deps/jsr.io/@std/assert/1.0.15/exists.d.ts.map +1 -0
  12. package/esm/deps/jsr.io/@std/assert/1.0.15/fail.d.ts.map +1 -0
  13. package/esm/deps/jsr.io/@std/assert/1.0.15/false.d.ts.map +1 -0
  14. package/esm/deps/jsr.io/@std/assert/1.0.15/greater.d.ts.map +1 -0
  15. package/esm/deps/jsr.io/@std/assert/1.0.15/greater_or_equal.d.ts.map +1 -0
  16. package/esm/deps/jsr.io/@std/assert/1.0.15/instance_of.d.ts.map +1 -0
  17. package/esm/deps/jsr.io/@std/assert/1.0.15/is_error.d.ts.map +1 -0
  18. package/esm/deps/jsr.io/@std/assert/1.0.15/less.d.ts.map +1 -0
  19. package/esm/deps/jsr.io/@std/assert/1.0.15/less_or_equal.d.ts.map +1 -0
  20. package/esm/deps/jsr.io/@std/assert/1.0.15/match.d.ts.map +1 -0
  21. package/esm/deps/jsr.io/@std/assert/1.0.15/mod.d.ts.map +1 -0
  22. package/esm/deps/jsr.io/@std/assert/1.0.15/not_equals.d.ts.map +1 -0
  23. package/esm/deps/jsr.io/@std/assert/1.0.15/not_instance_of.d.ts.map +1 -0
  24. package/esm/deps/jsr.io/@std/assert/1.0.15/not_match.d.ts.map +1 -0
  25. package/esm/deps/jsr.io/@std/assert/1.0.15/not_strict_equals.d.ts.map +1 -0
  26. package/esm/deps/jsr.io/@std/assert/1.0.15/object_match.d.ts.map +1 -0
  27. package/esm/deps/jsr.io/@std/assert/1.0.15/rejects.d.ts.map +1 -0
  28. package/esm/deps/jsr.io/@std/assert/1.0.15/strict_equals.d.ts.map +1 -0
  29. package/esm/deps/jsr.io/@std/assert/1.0.15/string_includes.d.ts.map +1 -0
  30. package/esm/deps/jsr.io/@std/assert/1.0.15/throws.d.ts.map +1 -0
  31. package/esm/deps/jsr.io/@std/assert/1.0.15/unimplemented.d.ts.map +1 -0
  32. package/esm/deps/jsr.io/@std/assert/1.0.15/unreachable.d.ts.map +1 -0
  33. package/esm/deps/jsr.io/@std/internal/1.0.12/build_message.d.ts.map +1 -0
  34. package/esm/deps/jsr.io/@std/internal/1.0.12/diff.d.ts.map +1 -0
  35. package/esm/deps/jsr.io/@std/internal/1.0.12/diff_str.d.ts.map +1 -0
  36. package/esm/deps/jsr.io/@std/internal/1.0.12/format.d.ts.map +1 -0
  37. package/esm/deps/jsr.io/@std/internal/1.0.12/styles.d.ts.map +1 -0
  38. package/esm/deps/jsr.io/@std/internal/1.0.12/types.d.ts.map +1 -0
  39. package/esm/mod.d.ts +161 -0
  40. package/esm/mod.d.ts.map +1 -0
  41. package/esm/mod.js +378 -0
  42. package/esm/mod_test.d.ts.map +1 -0
  43. package/esm/package.json +3 -0
  44. package/package.json +18 -11
  45. package/.github/workflows/deno.yml +0 -32
  46. package/.github/workflows/publish.yml +0 -20
  47. package/.vscode/settings.json +0 -4
  48. package/_dist/bigendian.d.ts +0 -32
  49. package/_dist/bigendian.d.ts.map +0 -1
  50. package/_dist/mod.d.ts +0 -135
  51. package/_dist/mod.d.ts.map +0 -1
  52. package/bigendian.js +0 -122
  53. package/bigendian.js.map +0 -1
  54. package/bigendian.ts +0 -136
  55. package/bigendian_test.ts +0 -70
  56. package/deno.json +0 -18
  57. package/deno.lock +0 -23
  58. package/mod.js +0 -336
  59. package/mod.js.map +0 -1
  60. package/mod.ts +0 -487
  61. package/mod_bench.ts +0 -98
  62. package/mod_test.ts +0 -319
package/esm/mod.js ADDED
@@ -0,0 +1,378 @@
1
+ /**
2
+ * Strongly typed classes for accessing binary data in a structured way
3
+ * @module
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
+ // deno-lint-ignore no-explicit-any
377
+ return StructArray;
378
+ }
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mod_test.d.ts","sourceRoot":"","sources":["../src/mod_test.ts"],"names":[],"mappings":""}
@@ -0,0 +1,3 @@
1
+ {
2
+ "type": "module"
3
+ }
package/package.json CHANGED
@@ -1,18 +1,25 @@
1
1
  {
2
2
  "name": "@rotu/structview",
3
- "version": "0.6.1",
4
- "homepage": "https://jsr.io/@rotu/structview",
5
- "type": "module",
6
- "dependencies": {},
3
+ "version": "0.7.1+dnt",
4
+ "description": "Read and write structured binary data with typesafe views",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "https://github.com/rotu/structview"
8
+ },
9
+ "license": "MIT",
10
+ "module": "./esm/mod.js",
7
11
  "exports": {
8
12
  ".": {
9
- "types": "./_dist/mod.d.ts",
10
- "default": "./mod.js"
11
- },
12
- "./bigendian": {
13
- "types": "./_dist/bigendian.d.ts",
14
- "default": "./bigendian.js"
13
+ "import": "./esm/mod.js"
15
14
  }
16
15
  },
17
- "_jsr_revision": 11
16
+ "scripts": {
17
+ "test": "node test_runner.js"
18
+ },
19
+ "devDependencies": {
20
+ "@types/node": "^20.9.0",
21
+ "picocolors": "^1.0.0",
22
+ "@deno/shim-deno": "~0.18.0"
23
+ },
24
+ "_generatedBy": "dnt@dev"
18
25
  }
@@ -1,32 +0,0 @@
1
- name: Deno
2
-
3
- on:
4
- push:
5
- branches: ["main"]
6
- pull_request:
7
- branches: ["main"]
8
-
9
- permissions:
10
- contents: read
11
-
12
- jobs:
13
- test:
14
- runs-on: ubuntu-latest
15
-
16
- steps:
17
- - name: Setup repo
18
- uses: actions/checkout@v4
19
-
20
- - name: Setup Deno
21
- uses: denoland/setup-deno@v2
22
- with:
23
- deno-version: vx.x.x
24
-
25
- - name: Verify formatting
26
- run: deno fmt --check
27
-
28
- - name: Run linter
29
- run: deno lint
30
-
31
- - name: Run tests
32
- run: deno test -A
@@ -1,20 +0,0 @@
1
- name: Publish
2
- on:
3
- push:
4
- branches:
5
- - main
6
-
7
- jobs:
8
- publish:
9
- runs-on: ubuntu-latest
10
- permissions:
11
- contents: read
12
- id-token: write
13
- steps:
14
- - uses: actions/checkout@v4
15
- - name: Setup Deno
16
- uses: denoland/setup-deno@v2
17
- with:
18
- deno-version: vx.x.x
19
- - name: Publish package
20
- run: deno publish
@@ -1,4 +0,0 @@
1
- {
2
- "deno.enable": true,
3
- "editor.formatOnSaveMode": "file"
4
- }
@@ -1,32 +0,0 @@
1
- /**
2
- * Property descriptor factories for big-endian numbers
3
- * @module
4
- */ import { type StructPropertyDescriptor } from "./mod.js";
5
- /**
6
- * Field for a big-endian 16-bit unsigned integer
7
- */ export declare function u16be(fieldOffset: number): StructPropertyDescriptor<number>;
8
- /**
9
- * Field for a big-endian 32-bit unsigned integer
10
- */ export declare function u32be(fieldOffset: number): StructPropertyDescriptor<number>;
11
- /**
12
- * Field for a big-endian 64-bit unsigned integer
13
- */ export declare function u64be(fieldOffset: number): StructPropertyDescriptor<bigint>;
14
- /**
15
- * Field for a big-endian 16-bit signed integer
16
- */ export declare function i16be(fieldOffset: number): StructPropertyDescriptor<number>;
17
- /**
18
- * Field for a big-endian 32-bit signed integer
19
- */ export declare function i32be(fieldOffset: number): StructPropertyDescriptor<number>;
20
- /**
21
- * Field for a big-endian 64-bit signed integer
22
- */ export declare function i64be(fieldOffset: number): StructPropertyDescriptor<bigint>;
23
- /**
24
- * Field for a big-endian 16-bit binary float (float16_t)
25
- */ export declare function f16be(fieldOffset: number): StructPropertyDescriptor<number>;
26
- /**
27
- * Field for a big-endian 32-bit binary float (float32_t)
28
- */ export declare function f32be(fieldOffset: number): StructPropertyDescriptor<number>;
29
- /**
30
- * Field for a big-endian 64-bit binary float (float64_t)
31
- */ export declare function f64be(fieldOffset: number): StructPropertyDescriptor<number>;
32
- //# sourceMappingURL=bigendian.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"bigendian.d.ts","sources":["../bigendian.ts"],"names":[],"mappings":"AAAA;;;CAGC,GAED,SAAyB,KAAK,wBAAwB,mBAAkB;AAExE;;CAEC,GACD,OAAO,iBAAS,MAAM,aAAa,MAAM,GAAG,yBAAyB,MAAM;AAW3E;;CAEC,GACD,OAAO,iBAAS,MAAM,aAAa,MAAM,GAAG,yBAAyB,MAAM;AAW3E;;CAEC,GACD,OAAO,iBAAS,MAAM,aAAa,MAAM,GAAG,yBAAyB,MAAM;AAW3E;;CAEC,GACD,OAAO,iBAAS,MAAM,aAAa,MAAM,GAAG,yBAAyB,MAAM;AAW3E;;CAEC,GACD,OAAO,iBAAS,MAAM,aAAa,MAAM,GAAG,yBAAyB,MAAM;AAW3E;;CAEC,GACD,OAAO,iBAAS,MAAM,aAAa,MAAM,GAAG,yBAAyB,MAAM;AAY3E;;CAEC,GACD,OAAO,iBAAS,MAAM,aAAa,MAAM,GAAG,yBAAyB,MAAM;AAY3E;;CAEC,GACD,OAAO,iBAAS,MAAM,aAAa,MAAM,GAAG,yBAAyB,MAAM;AAY3E;;CAEC,GACD,OAAO,iBAAS,MAAM,aAAa,MAAM,GAAG,yBAAyB,MAAM"}
package/_dist/mod.d.ts DELETED
@@ -1,135 +0,0 @@
1
- /**
2
- * Strongly typed classes for accessing binary data in a structured way
3
- * @module
4
- */ declare const dataViewSymbol: unique symbol;
5
- type AnyStruct = {
6
- get [dataViewSymbol](): DataView;
7
- };
8
- type Constructor<T> = {
9
- new(...args: any[]): T;
10
- };
11
- type SubclassWithProperties<Ctor extends Constructor<object>, Mixin> = {
12
- [K in keyof Ctor]: Ctor[K];
13
- } & {
14
- new(...args: ConstructorParameters<Ctor>): InstanceType<Ctor> & {
15
- -readonly [K in keyof Mixin]: Mixin[K];
16
- };
17
- };
18
- type TPropertyDescriptor<T> = {
19
- enumerable?: boolean;
20
- configurable?: boolean;
21
- get?(): T;
22
- set?(t: T): undefined;
23
- value?: T;
24
- writable?: boolean;
25
- };
26
- type MixinFromProps<Props extends object> = {
27
- [K in keyof Props]: Props[K] extends TPropertyDescriptor<infer V> ? V : unknown;
28
- };
29
- /**
30
- * Get the underlying DataView of a struct
31
- * @param struct
32
- * @returns
33
- */ export declare function structDataView(struct: AnyStruct): DataView;
34
- /**
35
- * Type of a property descriptor for a struct
36
- */ export type StructPropertyDescriptor<T> = ThisType<AnyStruct> & TPropertyDescriptor<T>;
37
- /**
38
- * Define a descriptor based on a dataview of the struct
39
- * @param fieldGetter function which, given a dataview, returns
40
- * @returns
41
- */ export declare function fromDataView<Fn extends (dv: DataView) => unknown>(fieldGetter: Fn): StructPropertyDescriptor<ReturnType<Fn>>;
42
- /**
43
- * Field for a 8-bit unsigned integer
44
- */ export declare function u8(fieldOffset: number): StructPropertyDescriptor<number>;
45
- /**
46
- * Field for a little-endian 16-bit unsigned integer
47
- */ export declare function u16(fieldOffset: number): StructPropertyDescriptor<number>;
48
- /**
49
- * Field for a little-endian 32-bit unsigned integer
50
- */ export declare function u32(fieldOffset: number): StructPropertyDescriptor<number>;
51
- /**
52
- * Field for a little-endian 64-bit unsigned integer
53
- */ export declare function u64(fieldOffset: number): StructPropertyDescriptor<bigint>;
54
- /**
55
- * Field for a little-endian 8-bit signed integer
56
- */ export declare function i8(fieldOffset: number): StructPropertyDescriptor<number>;
57
- /**
58
- * Field for a little-endian 16-bit signed integer
59
- */ export declare function i16(fieldOffset: number): StructPropertyDescriptor<number>;
60
- /**
61
- * Field for a little-endian 32-bit signed integer
62
- */ export declare function i32(fieldOffset: number): StructPropertyDescriptor<number>;
63
- /**
64
- * Field for a little-endian 64-bit signed integer
65
- */ export declare function i64(fieldOffset: number): StructPropertyDescriptor<bigint>;
66
- /**
67
- * Field for a little-endian 16-bit binary float (float16_t)
68
- */ export declare function f16(fieldOffset: number): StructPropertyDescriptor<number>;
69
- /**
70
- * Field for a little-endian 32-bit binary float (float32_t)
71
- */ export declare function f32(fieldOffset: number): StructPropertyDescriptor<number>;
72
- /**
73
- * Field for a little-endian 64-bit binary float (float64_t)
74
- */ export declare function f64(fieldOffset: number): StructPropertyDescriptor<number>;
75
- /**
76
- * Field for a UTF-8 fixed-length string
77
- */ export declare function string(fieldOffset: number, byteLength: number): StructPropertyDescriptor<string>;
78
- /**
79
- * Field for a boolean stored in a byte (0 = false, nonzero = true)
80
- * True will be stored as 1
81
- */ export declare function bool(fieldOffset: number): StructPropertyDescriptor<boolean>;
82
- type StructConstructor<T extends object> = {
83
- new(arg: {
84
- buffer: ArrayBufferLike;
85
- byteOffset?: number;
86
- byteLength?: number;
87
- }): T;
88
- };
89
- /**
90
- * Field for an embedded struct
91
- * @param ctor constructor for the inner struct
92
- * @param byteOffset where the inner struct starts relative to the outer struct
93
- * @param bytelength the length in bytes of the inner struct
94
- * @returns property descriptor for a struct
95
- */ export declare function substruct<T extends object>(ctor: StructConstructor<T>, byteOffset?: number, bytelength?: number): StructPropertyDescriptor<T>;
96
- /**
97
- * Base class for a structured binary object
98
- * Note there are no predeclared string-keyed properties - all property names are reserved for user-defined fields
99
- */ export declare class Struct {
100
- [dataViewSymbol]: DataView;
101
- static toDataView(o: Struct): DataView;
102
- /**
103
- * Create a new Struct
104
- * @param arg options for creating the struct.
105
- * If options has a `.buffer` property, we will use that as the backing memory (e.g. any TypedArray or DataView).
106
- * If options has no `.buffer` property but has a `.byteLength`, we will allocate a new buffer for the object.
107
- */ constructor(arg: {
108
- buffer: ArrayBufferLike;
109
- byteOffset?: number;
110
- byteLength?: number;
111
- } | {
112
- buffer?: undefined;
113
- byteOffset?: number;
114
- byteLength: number;
115
- });
116
- }
117
- /**
118
- * Subclass struct by adding the given property descriptors
119
- * @param propertyDescriptors properties to add to subclass instances
120
- * @returns A new class, inheriting from `Struct`, with the new property descriptors added
121
- */ export declare function defineStruct<const Props extends PropertyDescriptorMap>(propertyDescriptors: Props): SubclassWithProperties<typeof Struct, MixinFromProps<Props>>;
122
- /**
123
- * Create a new struct subclass for an array of structs
124
- * @param arrayOptions
125
- * @returns A new class, inheriting from `Struct` whose elements are statically typed structs
126
- */ export declare function defineArray<Ctor extends StructConstructor<object>>(arrayOptions: {
127
- /** Constructor for an object view of each element*/ struct: Ctor;
128
- /** Number of bytes between the start of consecutive elements */ byteStride: number;
129
- /** Total number of elements in the array (not bytes). If omitted, the array length will depend on the size of its underlying buffer */ length?: number;
130
- }): StructConstructor<{
131
- get length(): number;
132
- element(i: number): InstanceType<Ctor>;
133
- [i: number]: InstanceType<Ctor>;
134
- } & Iterable<InstanceType<Ctor>>>;
135
- //# sourceMappingURL=mod.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"mod.d.ts","sources":["../mod.ts"],"names":[],"mappings":"AAAA;;;CAGC,GAED,cAAM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