@yume-chan/struct 0.0.0-20240714132542
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/CHANGELOG.md +131 -0
- package/LICENSE +21 -0
- package/README.md +747 -0
- package/esm/basic/definition.d.ts +44 -0
- package/esm/basic/definition.d.ts.map +1 -0
- package/esm/basic/definition.js +24 -0
- package/esm/basic/definition.js.map +1 -0
- package/esm/basic/field-value.d.ts +39 -0
- package/esm/basic/field-value.d.ts.map +1 -0
- package/esm/basic/field-value.js +46 -0
- package/esm/basic/field-value.js.map +1 -0
- package/esm/basic/index.d.ts +6 -0
- package/esm/basic/index.d.ts.map +1 -0
- package/esm/basic/index.js +6 -0
- package/esm/basic/index.js.map +1 -0
- package/esm/basic/options.d.ts +10 -0
- package/esm/basic/options.d.ts.map +1 -0
- package/esm/basic/options.js +4 -0
- package/esm/basic/options.js.map +1 -0
- package/esm/basic/stream.d.ts +25 -0
- package/esm/basic/stream.d.ts.map +1 -0
- package/esm/basic/stream.js +8 -0
- package/esm/basic/stream.js.map +1 -0
- package/esm/basic/struct-value.d.ts +30 -0
- package/esm/basic/struct-value.d.ts.map +1 -0
- package/esm/basic/struct-value.js +62 -0
- package/esm/basic/struct-value.js.map +1 -0
- package/esm/index.d.ts +15 -0
- package/esm/index.d.ts.map +1 -0
- package/esm/index.js +7 -0
- package/esm/index.js.map +1 -0
- package/esm/struct.d.ts +151 -0
- package/esm/struct.d.ts.map +1 -0
- package/esm/struct.js +255 -0
- package/esm/struct.js.map +1 -0
- package/esm/sync-promise.d.ts +13 -0
- package/esm/sync-promise.d.ts.map +1 -0
- package/esm/sync-promise.js +71 -0
- package/esm/sync-promise.js.map +1 -0
- package/esm/types/bigint.d.ts +25 -0
- package/esm/types/bigint.d.ts.map +1 -0
- package/esm/types/bigint.js +46 -0
- package/esm/types/bigint.js.map +1 -0
- package/esm/types/buffer/base.d.ts +69 -0
- package/esm/types/buffer/base.d.ts.map +1 -0
- package/esm/types/buffer/base.js +101 -0
- package/esm/types/buffer/base.js.map +1 -0
- package/esm/types/buffer/fixed-length.d.ts +9 -0
- package/esm/types/buffer/fixed-length.d.ts.map +1 -0
- package/esm/types/buffer/fixed-length.js +7 -0
- package/esm/types/buffer/fixed-length.js.map +1 -0
- package/esm/types/buffer/index.d.ts +4 -0
- package/esm/types/buffer/index.d.ts.map +1 -0
- package/esm/types/buffer/index.js +4 -0
- package/esm/types/buffer/index.js.map +1 -0
- package/esm/types/buffer/variable-length.d.ts +46 -0
- package/esm/types/buffer/variable-length.d.ts.map +1 -0
- package/esm/types/buffer/variable-length.js +88 -0
- package/esm/types/buffer/variable-length.js.map +1 -0
- package/esm/types/index.d.ts +4 -0
- package/esm/types/index.d.ts.map +1 -0
- package/esm/types/index.js +4 -0
- package/esm/types/index.js.map +1 -0
- package/esm/types/number.d.ts +28 -0
- package/esm/types/number.d.ts.map +1 -0
- package/esm/types/number.js +88 -0
- package/esm/types/number.js.map +1 -0
- package/esm/utils.d.ts +48 -0
- package/esm/utils.d.ts.map +1 -0
- package/esm/utils.js +18 -0
- package/esm/utils.js.map +1 -0
- package/package.json +49 -0
- package/src/basic/definition.ts +68 -0
- package/src/basic/field-value.ts +71 -0
- package/src/basic/index.ts +5 -0
- package/src/basic/options.ts +19 -0
- package/src/basic/stream.ts +34 -0
- package/src/basic/struct-value.ts +85 -0
- package/src/index.ts +18 -0
- package/src/struct.ts +705 -0
- package/src/sync-promise.ts +131 -0
- package/src/types/bigint.ts +120 -0
- package/src/types/buffer/base.ts +195 -0
- package/src/types/buffer/fixed-length.ts +22 -0
- package/src/types/buffer/index.ts +3 -0
- package/src/types/buffer/variable-length.ts +199 -0
- package/src/types/index.ts +3 -0
- package/src/types/number.ts +159 -0
- package/src/utils.ts +94 -0
- package/tsconfig.build.json +3 -0
- package/tsconfig.build.tsbuildinfo +1 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fixed-length.js","sourceRoot":"","sources":["../../../src/types/buffer/fixed-length.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,yBAAyB,EAAE,MAAM,WAAW,CAAC;AAMtD,MAAM,OAAO,oCAKX,SAAQ,yBAKT;IACG,OAAO;QACH,OAAO,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC;IAC/B,CAAC;CACJ"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/types/buffer/index.ts"],"names":[],"mappings":"AAAA,cAAc,WAAW,CAAC;AAC1B,cAAc,mBAAmB,CAAC;AAClC,cAAc,sBAAsB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/types/buffer/index.ts"],"names":[],"mappings":"AAAA,cAAc,WAAW,CAAC;AAC1B,cAAc,mBAAmB,CAAC;AAClC,cAAc,sBAAsB,CAAC"}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import type { StructFieldDefinition, StructOptions, StructValue } from "../../basic/index.js";
|
|
2
|
+
import { StructFieldValue } from "../../basic/index.js";
|
|
3
|
+
import type { KeysOfType } from "../../utils.js";
|
|
4
|
+
import type { BufferFieldConverter } from "./base.js";
|
|
5
|
+
import { BufferLikeFieldDefinition, BufferLikeFieldValue } from "./base.js";
|
|
6
|
+
export type LengthField<TFields> = KeysOfType<TFields, number | string>;
|
|
7
|
+
export interface VariableLengthBufferLikeFieldOptions<TFields extends object = object, TLengthField extends LengthField<TFields> = any> {
|
|
8
|
+
/**
|
|
9
|
+
* The name of the field that contains the length of the buffer.
|
|
10
|
+
*
|
|
11
|
+
* This field must be a `number` or `string` (can't be `bigint`) field.
|
|
12
|
+
*/
|
|
13
|
+
lengthField: TLengthField;
|
|
14
|
+
/**
|
|
15
|
+
* If the `lengthField` refers to a string field,
|
|
16
|
+
* what radix to use when converting the string to a number.
|
|
17
|
+
*
|
|
18
|
+
* @default 10
|
|
19
|
+
*/
|
|
20
|
+
lengthFieldRadix?: number;
|
|
21
|
+
}
|
|
22
|
+
export declare class VariableLengthBufferLikeFieldDefinition<TConverter extends BufferFieldConverter = BufferFieldConverter, TOptions extends VariableLengthBufferLikeFieldOptions = VariableLengthBufferLikeFieldOptions, TTypeScriptType = TConverter["TTypeScriptType"]> extends BufferLikeFieldDefinition<TConverter, TOptions, TOptions["lengthField"], TTypeScriptType> {
|
|
23
|
+
getSize(): number;
|
|
24
|
+
protected getDeserializeSize(struct: StructValue): number;
|
|
25
|
+
create(options: Readonly<StructOptions>, struct: StructValue, value: TTypeScriptType, array?: Uint8Array): VariableLengthBufferLikeStructFieldValue<this>;
|
|
26
|
+
}
|
|
27
|
+
export declare class VariableLengthBufferLikeStructFieldValue<TDefinition extends VariableLengthBufferLikeFieldDefinition = VariableLengthBufferLikeFieldDefinition> extends BufferLikeFieldValue<TDefinition> {
|
|
28
|
+
#private;
|
|
29
|
+
protected length: number | undefined;
|
|
30
|
+
protected lengthFieldValue: VariableLengthBufferLikeFieldLengthValue;
|
|
31
|
+
constructor(definition: TDefinition, options: Readonly<StructOptions>, struct: StructValue, value: TDefinition["TValue"], array?: Uint8Array);
|
|
32
|
+
getSize(): number;
|
|
33
|
+
set(value: unknown): void;
|
|
34
|
+
}
|
|
35
|
+
type VariableLengthBufferLikeFieldValueLike = StructFieldValue<StructFieldDefinition<VariableLengthBufferLikeFieldOptions<any, any>, any, any>>;
|
|
36
|
+
export declare class VariableLengthBufferLikeFieldLengthValue extends StructFieldValue<StructFieldDefinition<unknown, unknown, PropertyKey>> {
|
|
37
|
+
protected originalValue: StructFieldValue<StructFieldDefinition<unknown, unknown, PropertyKey>>;
|
|
38
|
+
protected bufferValue: VariableLengthBufferLikeFieldValueLike;
|
|
39
|
+
constructor(originalValue: StructFieldValue<StructFieldDefinition<unknown, unknown, PropertyKey>>, bufferValue: VariableLengthBufferLikeFieldValueLike);
|
|
40
|
+
getSize(): number;
|
|
41
|
+
get(): string | number;
|
|
42
|
+
set(): void;
|
|
43
|
+
serialize(array: Uint8Array, offset: number): void;
|
|
44
|
+
}
|
|
45
|
+
export {};
|
|
46
|
+
//# sourceMappingURL=variable-length.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"variable-length.d.ts","sourceRoot":"","sources":["../../../src/types/buffer/variable-length.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EACR,qBAAqB,EACrB,aAAa,EACb,WAAW,EACd,MAAM,sBAAsB,CAAC;AAC9B,OAAO,EAAE,gBAAgB,EAAE,MAAM,sBAAsB,CAAC;AACxD,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAC;AAEjD,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,WAAW,CAAC;AACtD,OAAO,EAAE,yBAAyB,EAAE,oBAAoB,EAAE,MAAM,WAAW,CAAC;AAE5E,MAAM,MAAM,WAAW,CAAC,OAAO,IAAI,UAAU,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,CAAC,CAAC;AAExE,MAAM,WAAW,oCAAoC,CACjD,OAAO,SAAS,MAAM,GAAG,MAAM,EAC/B,YAAY,SAAS,WAAW,CAAC,OAAO,CAAC,GAAG,GAAG;IAE/C;;;;OAIG;IACH,WAAW,EAAE,YAAY,CAAC;IAE1B;;;;;OAKG;IACH,gBAAgB,CAAC,EAAE,MAAM,CAAC;CAC7B;AAED,qBAAa,uCAAuC,CAChD,UAAU,SAAS,oBAAoB,GAAG,oBAAoB,EAC9D,QAAQ,SACJ,oCAAoC,GAAG,oCAAoC,EAC/E,eAAe,GAAG,UAAU,CAAC,iBAAiB,CAAC,CACjD,SAAQ,yBAAyB,CAC/B,UAAU,EACV,QAAQ,EACR,QAAQ,CAAC,aAAa,CAAC,EACvB,eAAe,CAClB;IACY,OAAO,IAAI,MAAM;cAIP,kBAAkB,CAAC,MAAM,EAAE,WAAW;IAQhD,MAAM,CACX,OAAO,EAAE,QAAQ,CAAC,aAAa,CAAC,EAChC,MAAM,E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
|
2
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
3
|
+
import { StructFieldValue } from "../../basic/index.js";
|
|
4
|
+
import { BufferLikeFieldDefinition, BufferLikeFieldValue } from "./base.js";
|
|
5
|
+
export class VariableLengthBufferLikeFieldDefinition extends BufferLikeFieldDefinition {
|
|
6
|
+
getSize() {
|
|
7
|
+
return 0;
|
|
8
|
+
}
|
|
9
|
+
getDeserializeSize(struct) {
|
|
10
|
+
let value = struct.value[this.options.lengthField];
|
|
11
|
+
if (typeof value === "string") {
|
|
12
|
+
value = Number.parseInt(value, this.options.lengthFieldRadix ?? 10);
|
|
13
|
+
}
|
|
14
|
+
return value;
|
|
15
|
+
}
|
|
16
|
+
create(options, struct, value, array) {
|
|
17
|
+
return new VariableLengthBufferLikeStructFieldValue(this, options, struct, value, array);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
export class VariableLengthBufferLikeStructFieldValue extends BufferLikeFieldValue {
|
|
21
|
+
length;
|
|
22
|
+
lengthFieldValue;
|
|
23
|
+
// eslint-disable-next-line @typescript-eslint/max-params
|
|
24
|
+
constructor(definition, options, struct, value, array) {
|
|
25
|
+
super(definition, options, struct, value, array);
|
|
26
|
+
if (array) {
|
|
27
|
+
this.length = array.length;
|
|
28
|
+
}
|
|
29
|
+
// Patch the associated length field.
|
|
30
|
+
const lengthField = this.definition.options.lengthField;
|
|
31
|
+
const originalValue = struct.get(lengthField);
|
|
32
|
+
this.lengthFieldValue = new VariableLengthBufferLikeFieldLengthValue(originalValue, this);
|
|
33
|
+
struct.set(lengthField, this.lengthFieldValue);
|
|
34
|
+
}
|
|
35
|
+
#tryGetSize() {
|
|
36
|
+
const length = this.definition.converter.getSize(this.value);
|
|
37
|
+
if (length !== undefined && length < 0) {
|
|
38
|
+
throw new Error("Invalid length");
|
|
39
|
+
}
|
|
40
|
+
return length;
|
|
41
|
+
}
|
|
42
|
+
getSize() {
|
|
43
|
+
if (this.length === undefined) {
|
|
44
|
+
// first try to get the size from the converter
|
|
45
|
+
this.length = this.#tryGetSize();
|
|
46
|
+
}
|
|
47
|
+
if (this.length === undefined) {
|
|
48
|
+
// The converter doesn't know the size, so convert the value to a buffer to get its size
|
|
49
|
+
this.array = this.definition.converter.toBuffer(this.value);
|
|
50
|
+
this.length = this.array.length;
|
|
51
|
+
}
|
|
52
|
+
return this.length;
|
|
53
|
+
}
|
|
54
|
+
set(value) {
|
|
55
|
+
super.set(value);
|
|
56
|
+
this.array = undefined;
|
|
57
|
+
this.length = undefined;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
export class VariableLengthBufferLikeFieldLengthValue extends StructFieldValue {
|
|
61
|
+
originalValue;
|
|
62
|
+
bufferValue;
|
|
63
|
+
constructor(originalValue, bufferValue) {
|
|
64
|
+
super(originalValue.definition, originalValue.options, originalValue.struct, 0);
|
|
65
|
+
this.originalValue = originalValue;
|
|
66
|
+
this.bufferValue = bufferValue;
|
|
67
|
+
}
|
|
68
|
+
getSize() {
|
|
69
|
+
return this.originalValue.getSize();
|
|
70
|
+
}
|
|
71
|
+
get() {
|
|
72
|
+
let value = this.bufferValue.getSize();
|
|
73
|
+
const originalValue = this.originalValue.get();
|
|
74
|
+
if (typeof originalValue === "string") {
|
|
75
|
+
value = value.toString(this.bufferValue.definition.options.lengthFieldRadix ?? 10);
|
|
76
|
+
}
|
|
77
|
+
return value;
|
|
78
|
+
}
|
|
79
|
+
set() {
|
|
80
|
+
// Ignore setting
|
|
81
|
+
// It will always be in sync with the buffer size
|
|
82
|
+
}
|
|
83
|
+
serialize(array, offset) {
|
|
84
|
+
this.originalValue.set(this.get());
|
|
85
|
+
this.originalValue.serialize(array, offset);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
//# sourceMappingURL=variable-length.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"variable-length.js","sourceRoot":"","sources":["../../../src/types/buffer/variable-length.ts"],"names":[],"mappings":"AAAA,+DAA+D;AAC/D,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Aa,CAAC,OAAO,EACrB,aAAa,CAAC,MAAM,EACpB,CAAC,CACJ,CAAC;QACF,IAAI,CAAC,aAAa,GAAG,aAAa,CAAC;QACnC,IAAI,CAAC,WAAW,GAAG,WAAW,CAAC;IACnC,CAAC;IAEQ,OAAO;QACZ,OAAO,IAAI,CAAC,aAAa,CAAC,OAAO,EAAE,CAAC;IACxC,CAAC;IAEQ,GAAG;QACR,IAAI,KAAK,GAAoB,IAAI,CAAC,WAAW,CAAC,OAAO,EAAE,CAAC;QAExD,MAAM,aAAa,GAAG,IAAI,CAAC,aAAa,CAAC,GAAG,EAAE,CAAC;QAC/C,IAAI,OAAO,aAAa,KAAK,QAAQ,EAAE,CAAC;YACpC,KAAK,GAAG,KAAK,CAAC,QAAQ,CAClB,IAAI,CAAC,WAAW,CAAC,UAAU,CAAC,OAAO,CAAC,gBAAgB,IAAI,EAAE,CAC7D,CAAC;QACN,CAAC;QAED,OAAO,KAAK,CAAC;IACjB,CAAC;IAEQ,GAAG;QACR,iBAAiB;QACjB,iDAAiD;IACrD,CAAC;IAED,SAAS,CAAC,KAAiB,EAAE,MAAc;QACvC,IAAI,CAAC,aAAa,CAAC,GAAG,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,CAAC;QACnC,IAAI,CAAC,aAAa,CAAC,SAAS,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC;IAChD,CAAC;CACJ"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/types/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAC;AAC5B,cAAc,mBAAmB,CAAC;AAClC,cAAc,aAAa,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/types/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAC;AAC5B,cAAc,mBAAmB,CAAC;AAClC,cAAc,aAAa,CAAC"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { AsyncExactReadable, ExactReadable, StructOptions, StructValue } from "../basic/index.js";
|
|
2
|
+
import { StructFieldDefinition, StructFieldValue } from "../basic/index.js";
|
|
3
|
+
export interface NumberFieldVariant {
|
|
4
|
+
signed: boolean;
|
|
5
|
+
size: number;
|
|
6
|
+
deserialize(array: Uint8Array, littleEndian: boolean): number;
|
|
7
|
+
serialize(array: Uint8Array, offset: number, value: number, littleEndian: boolean): void;
|
|
8
|
+
}
|
|
9
|
+
export declare namespace NumberFieldVariant {
|
|
10
|
+
const Uint8: NumberFieldVariant;
|
|
11
|
+
const Int8: NumberFieldVariant;
|
|
12
|
+
const Uint16: NumberFieldVariant;
|
|
13
|
+
const Int16: NumberFieldVariant;
|
|
14
|
+
const Uint32: NumberFieldVariant;
|
|
15
|
+
const Int32: NumberFieldVariant;
|
|
16
|
+
}
|
|
17
|
+
export declare class NumberFieldDefinition<TVariant extends NumberFieldVariant = NumberFieldVariant, TTypeScriptType = number> extends StructFieldDefinition<void, TTypeScriptType> {
|
|
18
|
+
readonly variant: TVariant;
|
|
19
|
+
constructor(variant: TVariant, typescriptType?: TTypeScriptType);
|
|
20
|
+
getSize(): number;
|
|
21
|
+
create(options: Readonly<StructOptions>, struct: StructValue, value: TTypeScriptType): NumberFieldValue<this>;
|
|
22
|
+
deserialize(options: Readonly<StructOptions>, stream: ExactReadable, struct: StructValue): NumberFieldValue<this>;
|
|
23
|
+
deserialize(options: Readonly<StructOptions>, stream: AsyncExactReadable, struct: StructValue): Promise<NumberFieldValue<this>>;
|
|
24
|
+
}
|
|
25
|
+
export declare class NumberFieldValue<TDefinition extends NumberFieldDefinition<NumberFieldVariant, unknown>> extends StructFieldValue<TDefinition> {
|
|
26
|
+
serialize(array: Uint8Array, offset: number): void;
|
|
27
|
+
}
|
|
28
|
+
//# sourceMappingURL=number.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"number.d.ts","sourceRoot":"","sources":["../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AAM,EAAE,WAAW,GACpB,gBAAgB,CAAC,IAAI,CAAC;IAChB,WAAW,CAChB,OAAO,EAAE,QAAQ,CAAC,aAAa,CAAC,EAChC,MAAM,EAAE,kBAAkB,EAC1B,MAAM,EAAE,WAAW,GACpB,OAAO,CAAC,gBAAgB,CAAC,IAAI,CAAC,CAAC;CAkBrC;AAED,qBAAa,gBAAgB,CACzB,WAAW,SAAS,qBAAqB,CAAC,kBAAkB,EAAE,OAAO,CAAC,CACxE,SAAQ,gBAAgB,CAAC,WAAW,CAAC;IACnC,SAAS,CAAC,KAAK,EAAE,UAAU,EAAE,MAAM,EAAE,MAAM,GAAG,IAAI;CAQrD"}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { getInt16, getInt32, getInt8, getUint16, getUint32, setInt16, setInt32, setUint16, setUint32, } from "@yume-chan/no-data-view";
|
|
2
|
+
import { StructFieldDefinition, StructFieldValue } from "../basic/index.js";
|
|
3
|
+
import { SyncPromise } from "../sync-promise.js";
|
|
4
|
+
export var NumberFieldVariant;
|
|
5
|
+
(function (NumberFieldVariant) {
|
|
6
|
+
NumberFieldVariant.Uint8 = {
|
|
7
|
+
signed: false,
|
|
8
|
+
size: 1,
|
|
9
|
+
deserialize(array) {
|
|
10
|
+
return array[0];
|
|
11
|
+
},
|
|
12
|
+
serialize(array, offset, value) {
|
|
13
|
+
array[offset] = value;
|
|
14
|
+
},
|
|
15
|
+
};
|
|
16
|
+
NumberFieldVariant.Int8 = {
|
|
17
|
+
signed: true,
|
|
18
|
+
size: 1,
|
|
19
|
+
deserialize(array) {
|
|
20
|
+
return getInt8(array, 0);
|
|
21
|
+
},
|
|
22
|
+
serialize(array, offset, value) {
|
|
23
|
+
array[offset] = value;
|
|
24
|
+
},
|
|
25
|
+
};
|
|
26
|
+
NumberFieldVariant.Uint16 = {
|
|
27
|
+
signed: false,
|
|
28
|
+
size: 2,
|
|
29
|
+
deserialize(array, littleEndian) {
|
|
30
|
+
return getUint16(array, 0, littleEndian);
|
|
31
|
+
},
|
|
32
|
+
serialize: setUint16,
|
|
33
|
+
};
|
|
34
|
+
NumberFieldVariant.Int16 = {
|
|
35
|
+
signed: true,
|
|
36
|
+
size: 2,
|
|
37
|
+
deserialize(array, littleEndian) {
|
|
38
|
+
return getInt16(array, 0, littleEndian);
|
|
39
|
+
},
|
|
40
|
+
serialize: setInt16,
|
|
41
|
+
};
|
|
42
|
+
NumberFieldVariant.Uint32 = {
|
|
43
|
+
signed: false,
|
|
44
|
+
size: 4,
|
|
45
|
+
deserialize(array, littleEndian) {
|
|
46
|
+
return getUint32(array, 0, littleEndian);
|
|
47
|
+
},
|
|
48
|
+
serialize: setUint32,
|
|
49
|
+
};
|
|
50
|
+
NumberFieldVariant.Int32 = {
|
|
51
|
+
signed: true,
|
|
52
|
+
size: 4,
|
|
53
|
+
deserialize(array, littleEndian) {
|
|
54
|
+
return getInt32(array, 0, littleEndian);
|
|
55
|
+
},
|
|
56
|
+
serialize: setInt32,
|
|
57
|
+
};
|
|
58
|
+
})(NumberFieldVariant || (NumberFieldVariant = {}));
|
|
59
|
+
export class NumberFieldDefinition extends StructFieldDefinition {
|
|
60
|
+
variant;
|
|
61
|
+
constructor(variant, typescriptType) {
|
|
62
|
+
void typescriptType;
|
|
63
|
+
super();
|
|
64
|
+
this.variant = variant;
|
|
65
|
+
}
|
|
66
|
+
getSize() {
|
|
67
|
+
return this.variant.size;
|
|
68
|
+
}
|
|
69
|
+
create(options, struct, value) {
|
|
70
|
+
return new NumberFieldValue(this, options, struct, value);
|
|
71
|
+
}
|
|
72
|
+
deserialize(options, stream, struct) {
|
|
73
|
+
return SyncPromise.try(() => {
|
|
74
|
+
return stream.readExactly(this.getSize());
|
|
75
|
+
})
|
|
76
|
+
.then((array) => {
|
|
77
|
+
const value = this.variant.deserialize(array, options.littleEndian);
|
|
78
|
+
return this.create(options, struct, value);
|
|
79
|
+
})
|
|
80
|
+
.valueOrPromise();
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
export class NumberFieldValue extends StructFieldValue {
|
|
84
|
+
serialize(array, offset) {
|
|
85
|
+
this.definition.variant.serialize(array, offset, this.value, this.options.littleEndian);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
//# sourceMappingURL=number.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"number.js","sourceRoot":"","sources":["../../src/types/number.ts"],"names":[],"mappings":"AAAA,OAAO,EACH,QAAQ,EACR,QAAQ,EACR,OAAO,EACP,SAAS,EACT,SAAS,EACT,QAAQ,EACR,QAAQ,EACR,SAAS,EACT,SAAS,GACZ,MAAM,yBAAyB,CAAC;AAQjC,OAAO,EAAE,qBAAqB,EAAE,gBAAgB,EAAE,MAAM,mBAAmB,CAAC;AAC5E,OAAO,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAC;AAejD,MAAM,KAAW,kBAAkB,CA0DlC;AA1DD,WAAiB,kBAAkB;IAClB,wBAAK,GAAuB;QACrC,MAAM,EAAE,KAAK;QACb,IAAI,EAAE,CAAC;QACP,WAAW,CAAC,KAAK;YACb,OAAO,KAAK,CAAC,CAAC,CAAE,CAAC;QACrB,CAAC;QACD,SAAS,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK;YAC1B,KAAK,CAAC,MAAM,CAAC,GAAG,KAAK,CAAC;QAC1B,CAAC;KACJ,CAAC;IAEW,uBAAI,GAAuB;QACpC,MAAM,EAAE,IAAI;QACZ,IAAI,EAAE,CAAC;QACP,WAAW,CAAC,KAAK;YACb,OAAO,OAAO,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC;QAC7B,CAAC;QACD,SAAS,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK;YAC1B,KAAK,CAAC,MAAM,CAAC,GAAG,KAAK,CAAC;QAC1B,CAAC;KACJ,CAAC;IAEW,yBAAM,GAAuB;QACtC,MAAM,EAAE,KAAK;QACb,IAAI,EAAE,CAAC;QACP,WAAW,CAAC,KAAK,EAAE,YAAY;YAC3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,CAAC;IAYQ,WAAW,CAChB,OAAgC,EAChC,MAA0C,EAC1C,MAAmB;QAEnB,OAAO,WAAW,CAAC,GAAG,CAAC,GAAG,EAAE;YACxB,OAAO,MAAM,CAAC,WAAW,CAAC,IAAI,CAAC,OAAO,EAAE,CAAC,CAAC;QAC9C,CAAC,CAAC;aACG,IAAI,CAAC,CAAC,KAAK,EAAE,EAAE;YACZ,MAAM,KAAK,GAAG,IAAI,CAAC,OAAO,CAAC,WAAW,CAClC,KAAK,EACL,OAAO,CAAC,YAAY,CACvB,CAAC;YACF,OAAO,IAAI,CAAC,MAAM,CAAC,OAAO,EAAE,MAAM,EAAE,KAAc,CAAC,CAAC;QACxD,CAAC,CAAC;aACD,cAAc,EAAE,CAAC;IAC1B,CAAC;CACJ;AAED,MAAM,OAAO,gBAEX,SAAQ,gBAA6B;IACnC,SAAS,CAAC,KAAiB,EAAE,MAAc;QACvC,IAAI,CAAC,UAAU,CAAC,OAAO,CAAC,SAAS,CAC7B,KAAK,EACL,MAAM,EACN,IAAI,CAAC,KAAc,EACnB,IAAI,CAAC,OAAO,CAAC,YAAY,CAC5B,CAAC;IACN,CAAC;CACJ"}
|
package/esm/utils.d.ts
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* When evaluating a very complex generic type alias,
|
|
3
|
+
* tell TypeScript to use `T`, instead of current type alias' name, as the result type name
|
|
4
|
+
*
|
|
5
|
+
* Example:
|
|
6
|
+
*
|
|
7
|
+
* ```ts
|
|
8
|
+
* type WithIdentity<T> = Identity<SomeType<T>>;
|
|
9
|
+
* type WithoutIdentity<T> = SomeType<T>;
|
|
10
|
+
*
|
|
11
|
+
* type WithIdentityResult = WithIdentity<number>;
|
|
12
|
+
* // Hover on this one shows `SomeType<number>`
|
|
13
|
+
*
|
|
14
|
+
* type WithoutIdentityResult = WithoutIdentity<number>;
|
|
15
|
+
* // Hover on this one shows `WithoutIdentity<number>`
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
export type Identity<T> = T;
|
|
19
|
+
/**
|
|
20
|
+
* Collapse an intersection type (`{ foo: string } & { bar: number }`) to a simple type (`{ foo: string, bar: number }`)
|
|
21
|
+
*/
|
|
22
|
+
export type Evaluate<T> = T extends infer U ? {
|
|
23
|
+
[K in keyof U]: U[K];
|
|
24
|
+
} : never;
|
|
25
|
+
/**
|
|
26
|
+
* Overwrite fields in `TBase` with fields in `TNew`
|
|
27
|
+
*/
|
|
28
|
+
export type Overwrite<TBase extends object, TNew extends object> = Evaluate<Omit<TBase, keyof TNew> & TNew>;
|
|
29
|
+
/**
|
|
30
|
+
* Remove fields with `never` type
|
|
31
|
+
*/
|
|
32
|
+
export type OmitNever<T> = Pick<T, {
|
|
33
|
+
[K in keyof T]: [T[K]] extends [never] ? never : K;
|
|
34
|
+
}[keyof T]>;
|
|
35
|
+
/**
|
|
36
|
+
* Extract keys of fields in `T` that has type `TValue`
|
|
37
|
+
*/
|
|
38
|
+
export type KeysOfType<T, TValue> = {
|
|
39
|
+
[TKey in keyof T]: T[TKey] extends TValue ? TKey : never;
|
|
40
|
+
}[keyof T];
|
|
41
|
+
export type ValueOrPromise<T> = T | PromiseLike<T>;
|
|
42
|
+
/**
|
|
43
|
+
* Returns a (fake) value of the given type.
|
|
44
|
+
*/
|
|
45
|
+
export declare function placeholder<T>(): T;
|
|
46
|
+
export declare function encodeUtf8(input: string): Uint8Array;
|
|
47
|
+
export declare function decodeUtf8(buffer: ArrayBufferView | ArrayBuffer): string;
|
|
48
|
+
//# sourceMappingURL=utils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,MAAM,QAAQ,CAAC,CAAC,IAAI,CAAC,CAAC;AAE5B;;GAEG;AACH,MAAM,MAAM,QAAQ,CAAC,CAAC,IAAI,CAAC,SAAS,MAAM,CAAC,GAAG;KAAG,CAAC,IAAI,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;CAAE,GAAG,KAAK,CAAC;AAE/E;;GAEG;AACH,MAAM,MAAM,SAAS,CAAC,KAAK,SAAS,MAAM,EAAE,IAAI,SAAS,MAAM,IAAI,QAAQ,CACvE,IAAI,CAAC,KAAK,EAAE,MAAM,IAAI,CAAC,GAAG,IAAI,CACjC,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,SAAS,CAAC,CAAC,IAAI,IAAI,CAC3B,CAAC,EACD;KAAG,CAAC,IAAI,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,KAAK,CAAC,GAAG,KAAK,GAAG,CAAC;CAAE,CAAC,MAAM,CAAC,CAAC,CAClE,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,UAAU,CAAC,CAAC,EAAE,MAAM,IAAI;KAC/B,IAAI,IAAI,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,SAAS,MAAM,GAAG,IAAI,GAAG,KAAK;CAC3D,CAAC,MAAM,CAAC,CAAC,CAAC;AAEX,MAAM,MAAM,cAAc,CAAC,CAAC,IAAI,CAAC,GAAG,WAAW,CAAC,CAAC,CAAC,CAAC;AAEnD;;GAEG;AACH,wBAAgB,WAAW,CAAC,CAAC,KAAK,CAAC,CAElC;AAgCD,wBAAgB,UAAU,CAAC,KAAK,EAAE,MAAM,GAAG,UAAU,CAEpD;AAED,wBAAgB,UAAU,CAAC,MAAM,EAAE,eAAe,GAAG,WAAW,GAAG,MAAM,CAIxE"}
|
package/esm/utils.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns a (fake) value of the given type.
|
|
3
|
+
*/
|
|
4
|
+
export function placeholder() {
|
|
5
|
+
return undefined;
|
|
6
|
+
}
|
|
7
|
+
const { TextEncoder, TextDecoder } = globalThis;
|
|
8
|
+
const SharedEncoder = new TextEncoder();
|
|
9
|
+
const SharedDecoder = new TextDecoder();
|
|
10
|
+
export function encodeUtf8(input) {
|
|
11
|
+
return SharedEncoder.encode(input);
|
|
12
|
+
}
|
|
13
|
+
export function decodeUtf8(buffer) {
|
|
14
|
+
// `TextDecoder` has internal states in stream mode,
|
|
15
|
+
// but this method is not for stream mode, so the instance can be reused
|
|
16
|
+
return SharedDecoder.decode(buffer);
|
|
17
|
+
}
|
|
18
|
+
//# sourceMappingURL=utils.js.map
|
package/esm/utils.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAgDA;;GAEG;AACH,MAAM,UAAU,WAAW;IACvB,OAAO,SAAyB,CAAC;AACrC,CAAC;AA2BD,MAAM,EAAE,WAAW,EAAE,WAAW,EAAE,GAAG,UAAwC,CAAC;AAE9E,MAAM,aAAa,GAAG,IAAI,WAAW,EAAE,CAAC;AACxC,MAAM,aAAa,GAAG,IAAI,WAAW,EAAE,CAAC;AAExC,MAAM,UAAU,UAAU,CAAC,KAAa;IACpC,OAAO,aAAa,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;AACvC,CAAC;AAED,MAAM,UAAU,UAAU,CAAC,MAAqC;IAC5D,oDAAoD;IACpD,wEAAwE;IACxE,OAAO,aAAa,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;AACxC,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@yume-chan/struct",
|
|
3
|
+
"version": "0.0.0-20240714132542",
|
|
4
|
+
"description": "C-style structure serializer and deserializer.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"structure",
|
|
7
|
+
"serialization",
|
|
8
|
+
"deserialization",
|
|
9
|
+
"typescript"
|
|
10
|
+
],
|
|
11
|
+
"license": "MIT",
|
|
12
|
+
"author": {
|
|
13
|
+
"name": "Simon Chan",
|
|
14
|
+
"email": "cnsimonchan@live.com",
|
|
15
|
+
"url": "https://chensi.moe/blog"
|
|
16
|
+
},
|
|
17
|
+
"homepage": "https://github.com/yume-chan/ya-webadb/tree/main/packages/struct#readme",
|
|
18
|
+
"repository": {
|
|
19
|
+
"type": "git",
|
|
20
|
+
"url": "git+https://github.com/yume-chan/ya-webadb.git",
|
|
21
|
+
"directory": "packages/struct"
|
|
22
|
+
},
|
|
23
|
+
"bugs": {
|
|
24
|
+
"url": "https://github.com/yume-chan/ya-webadb/issues"
|
|
25
|
+
},
|
|
26
|
+
"type": "module",
|
|
27
|
+
"main": "esm/index.js",
|
|
28
|
+
"types": "esm/index.d.ts",
|
|
29
|
+
"sideEffects": false,
|
|
30
|
+
"dependencies": {
|
|
31
|
+
"@yume-chan/no-data-view": "0.0.0-20240714132542"
|
|
32
|
+
},
|
|
33
|
+
"devDependencies": {
|
|
34
|
+
"@jest/globals": "^30.0.0-alpha.4",
|
|
35
|
+
"@yume-chan/eslint-config": "^1.0.0",
|
|
36
|
+
"@yume-chan/tsconfig": "^1.0.0",
|
|
37
|
+
"cross-env": "^7.0.3",
|
|
38
|
+
"jest": "^30.0.0-alpha.4",
|
|
39
|
+
"prettier": "^3.3.3",
|
|
40
|
+
"ts-jest": "^29.2.2",
|
|
41
|
+
"typescript": "^5.5.3"
|
|
42
|
+
},
|
|
43
|
+
"scripts": {
|
|
44
|
+
"build": "tsc -b tsconfig.build.json",
|
|
45
|
+
"build:watch": "tsc -b tsconfig.build.json",
|
|
46
|
+
"test": "cross-env NODE_OPTIONS=\"--experimental-vm-modules --no-warnings\" TS_JEST_DISABLE_VER_CHECKER=true jest --coverage",
|
|
47
|
+
"lint": "run-eslint && prettier src/**/*.ts --write --tab-width 4"
|
|
48
|
+
}
|
|
49
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import type { StructFieldValue } from "./field-value.js";
|
|
2
|
+
import type { StructOptions } from "./options.js";
|
|
3
|
+
import type { AsyncExactReadable, ExactReadable } from "./stream.js";
|
|
4
|
+
import type { StructValue } from "./struct-value.js";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* A field definition defines how to deserialize a field.
|
|
8
|
+
*
|
|
9
|
+
* @template TOptions TypeScript type of this definition's `options`.
|
|
10
|
+
* @template TValue TypeScript type of this field.
|
|
11
|
+
* @template TOmitInitKey Optionally remove some fields from the init type. Should be a union of string literal types.
|
|
12
|
+
*/
|
|
13
|
+
export abstract class StructFieldDefinition<
|
|
14
|
+
TOptions = void,
|
|
15
|
+
TValue = unknown,
|
|
16
|
+
TOmitInitKey extends PropertyKey = never,
|
|
17
|
+
> {
|
|
18
|
+
/**
|
|
19
|
+
* When `T` is a type initiated `StructFieldDefinition`,
|
|
20
|
+
* use `T['TValue']` to retrieve its `TValue` type parameter.
|
|
21
|
+
*/
|
|
22
|
+
readonly TValue!: TValue;
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* When `T` is a type initiated `StructFieldDefinition`,
|
|
26
|
+
* use `T['TOmitInitKey']` to retrieve its `TOmitInitKey` type parameter.
|
|
27
|
+
*/
|
|
28
|
+
readonly TOmitInitKey!: TOmitInitKey;
|
|
29
|
+
|
|
30
|
+
readonly options: TOptions;
|
|
31
|
+
|
|
32
|
+
constructor(options: TOptions) {
|
|
33
|
+
this.options = options;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* When implemented in derived classes, returns the size (or minimal size if it's dynamic) of this field.
|
|
38
|
+
*
|
|
39
|
+
* Actual size can be retrieved from `StructFieldValue#getSize`
|
|
40
|
+
*/
|
|
41
|
+
abstract getSize(): number;
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* When implemented in derived classes, creates a `StructFieldValue` from a given `value`.
|
|
45
|
+
*/
|
|
46
|
+
abstract create(
|
|
47
|
+
options: Readonly<StructOptions>,
|
|
48
|
+
structValue: StructValue,
|
|
49
|
+
value: TValue,
|
|
50
|
+
): StructFieldValue<this>;
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* When implemented in derived classes,It must be synchronous (returns a value) or asynchronous (returns a `Promise`) depending
|
|
54
|
+
* on the type of `stream`. reads and creates a `StructFieldValue` from `stream`.
|
|
55
|
+
*
|
|
56
|
+
* `SyncPromise` can be used to simplify implementation.
|
|
57
|
+
*/
|
|
58
|
+
abstract deserialize(
|
|
59
|
+
options: Readonly<StructOptions>,
|
|
60
|
+
stream: ExactReadable,
|
|
61
|
+
structValue: StructValue,
|
|
62
|
+
): StructFieldValue<this>;
|
|
63
|
+
abstract deserialize(
|
|
64
|
+
options: Readonly<StructOptions>,
|
|
65
|
+
stream: AsyncExactReadable,
|
|
66
|
+
struct: StructValue,
|
|
67
|
+
): Promise<StructFieldValue<this>>;
|
|
68
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import type { StructFieldDefinition } from "./definition.js";
|
|
2
|
+
import type { StructOptions } from "./options.js";
|
|
3
|
+
import type { StructValue } from "./struct-value.js";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* A field value defines how to serialize a field.
|
|
7
|
+
*
|
|
8
|
+
* It may contains extra metadata about the value which are essential or
|
|
9
|
+
* helpful for the serialization process.
|
|
10
|
+
*/
|
|
11
|
+
export abstract class StructFieldValue<
|
|
12
|
+
TDefinition extends StructFieldDefinition<unknown, unknown, PropertyKey>,
|
|
13
|
+
> {
|
|
14
|
+
/** Gets the definition associated with this runtime value */
|
|
15
|
+
readonly definition: TDefinition;
|
|
16
|
+
|
|
17
|
+
/** Gets the options of the associated `Struct` */
|
|
18
|
+
readonly options: Readonly<StructOptions>;
|
|
19
|
+
|
|
20
|
+
/** Gets the associated `Struct` instance */
|
|
21
|
+
readonly struct: StructValue;
|
|
22
|
+
|
|
23
|
+
get hasCustomAccessors(): boolean {
|
|
24
|
+
return (
|
|
25
|
+
this.get !== StructFieldValue.prototype.get ||
|
|
26
|
+
this.set !== StructFieldValue.prototype.set
|
|
27
|
+
);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
protected value: TDefinition["TValue"];
|
|
31
|
+
|
|
32
|
+
constructor(
|
|
33
|
+
definition: TDefinition,
|
|
34
|
+
options: Readonly<StructOptions>,
|
|
35
|
+
struct: StructValue,
|
|
36
|
+
value: TDefinition["TValue"],
|
|
37
|
+
) {
|
|
38
|
+
this.definition = definition;
|
|
39
|
+
this.options = options;
|
|
40
|
+
this.struct = struct;
|
|
41
|
+
this.value = value;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Gets size of this field. By default, it returns its `definition`'s size.
|
|
46
|
+
*
|
|
47
|
+
* When overridden in derived classes, can have custom logic to calculate the actual size.
|
|
48
|
+
*/
|
|
49
|
+
getSize(): number {
|
|
50
|
+
return this.definition.getSize();
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* When implemented in derived classes, reads current field's value.
|
|
55
|
+
*/
|
|
56
|
+
get(): TDefinition["TValue"] {
|
|
57
|
+
return this.value as never;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* When implemented in derived classes, updates current field's value.
|
|
62
|
+
*/
|
|
63
|
+
set(value: TDefinition["TValue"]): void {
|
|
64
|
+
this.value = value;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* When implemented in derived classes, serializes this field into `array` at `offset`
|
|
69
|
+
*/
|
|
70
|
+
abstract serialize(array: Uint8Array, offset: number): void;
|
|
71
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export interface StructOptions {
|
|
2
|
+
/**
|
|
3
|
+
* Whether all multi-byte fields in this struct are little-endian encoded.
|
|
4
|
+
*
|
|
5
|
+
* @default false
|
|
6
|
+
*/
|
|
7
|
+
littleEndian: boolean;
|
|
8
|
+
|
|
9
|
+
// TODO: StructOptions: investigate whether this is necessary
|
|
10
|
+
// I can't think about any other options which need to be struct wide.
|
|
11
|
+
// Even endianness can be set on a per-field basis (because it's not meaningful
|
|
12
|
+
// for some field types like `Uint8Array`, and very rarely, a struct may contain
|
|
13
|
+
// mixed endianness).
|
|
14
|
+
// It's just more common and a little more convenient to have it here.
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export const StructDefaultOptions: Readonly<StructOptions> = {
|
|
18
|
+
littleEndian: false,
|
|
19
|
+
};
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { ValueOrPromise } from "../utils.js";
|
|
2
|
+
|
|
3
|
+
// TODO: allow over reading (returning a `Uint8Array`, an `offset` and a `length`) to avoid copying
|
|
4
|
+
|
|
5
|
+
export class ExactReadableEndedError extends Error {
|
|
6
|
+
constructor() {
|
|
7
|
+
super("ExactReadable ended");
|
|
8
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export interface ExactReadable {
|
|
13
|
+
readonly position: number;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Read data from the underlying data source.
|
|
17
|
+
*
|
|
18
|
+
* The stream must return exactly `length` bytes or data. If that's not possible
|
|
19
|
+
* (due to end of file or other error condition), it must throw an {@link ExactReadableEndedError}.
|
|
20
|
+
*/
|
|
21
|
+
readExactly(length: number): Uint8Array;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface AsyncExactReadable {
|
|
25
|
+
readonly position: number;
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Read data from the underlying data source.
|
|
29
|
+
*
|
|
30
|
+
* The stream must return exactly `length` bytes or data. If that's not possible
|
|
31
|
+
* (due to end of file or other error condition), it must throw an {@link ExactReadableEndedError}.
|
|
32
|
+
*/
|
|
33
|
+
readExactly(length: number): ValueOrPromise<Uint8Array>;
|
|
34
|
+
}
|