New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@yume-chan/struct

Package Overview
Dependencies
Maintainers
1
Versions
27
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@yume-chan/struct - npm Package Compare versions

Comparing version 0.0.20 to 0.0.21

6

CHANGELOG.json

@@ -5,2 +5,8 @@ {

{
"version": "0.0.21",
"tag": "@yume-chan/struct_v0.0.21",
"date": "Fri, 25 Aug 2023 14:05:18 GMT",
"comments": {}
},
{
"version": "0.0.20",

@@ -7,0 +13,0 @@ "tag": "@yume-chan/struct_v0.0.20",

7

CHANGELOG.md
# Change Log - @yume-chan/struct
This log was last generated on Mon, 05 Jun 2023 02:51:41 GMT and should not be manually modified.
This log was last generated on Fri, 25 Aug 2023 14:05:18 GMT and should not be manually modified.
## 0.0.21
Fri, 25 Aug 2023 14:05:18 GMT
_Version update only_
## 0.0.20

@@ -6,0 +11,0 @@ Mon, 05 Jun 2023 02:51:41 GMT

3

esm/struct.d.ts

@@ -59,3 +59,2 @@ import type { AsyncExactReadable, ExactReadable, StructFieldDefinition, StructOptions } from "./basic/index.js";

concat<TOther extends Struct<any, any, any, any>>(other: TOther): Struct<TFields & TOther["TFields"], TOmitInitKey | TOther["TOmitInitKey"], TExtra & TOther["TExtra"], TPostDeserialized>;
private number;
/**

@@ -85,3 +84,2 @@ * Appends an `int8` field to the `Struct`

uint32<TName extends PropertyKey, TTypeScriptType = number>(name: TName, typeScriptType?: TTypeScriptType): Struct<Evaluate<TFields & Record<TName, TTypeScriptType>>, TOmitInitKey, TExtra, TPostDeserialized>;
private bigint;
/**

@@ -99,3 +97,2 @@ * Appends an `int64` field to the `Struct`

uint64<TName extends PropertyKey, TTypeScriptType = BigIntFieldType["TTypeScriptType"]>(name: TName, typeScriptType?: TTypeScriptType): Struct<Evaluate<TFields & Record<TName, TTypeScriptType>>, TOmitInitKey, TExtra, TPostDeserialized>;
private arrayBufferLike;
uint8Array: BoundArrayBufferLikeFieldDefinitionCreator<TFields, TOmitInitKey, TExtra, TPostDeserialized, Uint8ArrayBufferFieldSubType>;

@@ -102,0 +99,0 @@ string: BoundArrayBufferLikeFieldDefinitionCreator<TFields, TOmitInitKey, TExtra, TPostDeserialized, StringBufferFieldSubType>;

@@ -71,3 +71,3 @@ import { ExactReadableEndedError, STRUCT_VALUE_SYMBOL, StructDefaultOptions, StructValue, } from "./basic/index.js";

}
number(name, type, typeScriptType) {
#number(name, type, typeScriptType) {
return this.field(name, new NumberFieldDefinition(type, typeScriptType));

@@ -79,3 +79,3 @@ }

int8(name, typeScriptType) {
return this.number(name, NumberFieldType.Int8, typeScriptType);
return this.#number(name, NumberFieldType.Int8, typeScriptType);
}

@@ -86,3 +86,3 @@ /**

uint8(name, typeScriptType) {
return this.number(name, NumberFieldType.Uint8, typeScriptType);
return this.#number(name, NumberFieldType.Uint8, typeScriptType);
}

@@ -93,3 +93,3 @@ /**

int16(name, typeScriptType) {
return this.number(name, NumberFieldType.Int16, typeScriptType);
return this.#number(name, NumberFieldType.Int16, typeScriptType);
}

@@ -100,3 +100,3 @@ /**

uint16(name, typeScriptType) {
return this.number(name, NumberFieldType.Uint16, typeScriptType);
return this.#number(name, NumberFieldType.Uint16, typeScriptType);
}

@@ -107,3 +107,3 @@ /**

int32(name, typeScriptType) {
return this.number(name, NumberFieldType.Int32, typeScriptType);
return this.#number(name, NumberFieldType.Int32, typeScriptType);
}

@@ -114,5 +114,5 @@ /**

uint32(name, typeScriptType) {
return this.number(name, NumberFieldType.Uint32, typeScriptType);
return this.#number(name, NumberFieldType.Uint32, typeScriptType);
}
bigint(name, type, typeScriptType) {
#bigint(name, type, typeScriptType) {
return this.field(name, new BigIntFieldDefinition(type, typeScriptType));

@@ -126,3 +126,3 @@ }

int64(name, typeScriptType) {
return this.bigint(name, BigIntFieldType.Int64, typeScriptType);
return this.#bigint(name, BigIntFieldType.Int64, typeScriptType);
}

@@ -135,5 +135,5 @@ /**

uint64(name, typeScriptType) {
return this.bigint(name, BigIntFieldType.Uint64, typeScriptType);
return this.#bigint(name, BigIntFieldType.Uint64, typeScriptType);
}
arrayBufferLike = (name, type, options) => {
#arrayBufferLike = (name, type, options) => {
if ("length" in options) {

@@ -147,6 +147,6 @@ return this.field(name, new FixedLengthBufferLikeFieldDefinition(type, options));

uint8Array = (name, options, typeScriptType) => {
return this.arrayBufferLike(name, Uint8ArrayBufferFieldSubType.Instance, options, typeScriptType);
return this.#arrayBufferLike(name, Uint8ArrayBufferFieldSubType.Instance, options, typeScriptType);
};
string = (name, options, typeScriptType) => {
return this.arrayBufferLike(name, StringBufferFieldSubType.Instance, options, typeScriptType);
return this.#arrayBufferLike(name, StringBufferFieldSubType.Instance, options, typeScriptType);
};

@@ -153,0 +153,0 @@ /**

import { getBigInt64, getBigUint64, setBigInt64, setBigUint64, } from "@yume-chan/dataview-bigint-polyfill/esm/fallback.js";
import { StructFieldDefinition, StructFieldValue } from "../basic/index.js";
import { SyncPromise } from "../sync-promise.js";
class BigIntFieldType {
export class BigIntFieldType {
TTypeScriptType;

@@ -17,3 +17,2 @@ size;

}
export { BigIntFieldType };
export class BigIntFieldDefinition extends StructFieldDefinition {

@@ -20,0 +19,0 @@ type;

@@ -17,3 +17,3 @@ import { StructFieldDefinition, StructFieldValue } from "../../basic/index.js";

/** An `BufferFieldSubType` that's actually an `Uint8Array` */
class Uint8ArrayBufferFieldSubType extends BufferFieldSubType {
export class Uint8ArrayBufferFieldSubType extends BufferFieldSubType {
static Instance = new Uint8ArrayBufferFieldSubType();

@@ -33,5 +33,4 @@ constructor() {

}
export { Uint8ArrayBufferFieldSubType };
/** An `BufferFieldSubType` that converts between `Uint8Array` and `string` */
class StringBufferFieldSubType extends BufferFieldSubType {
export class StringBufferFieldSubType extends BufferFieldSubType {
static Instance = new StringBufferFieldSubType();

@@ -51,3 +50,2 @@ toBuffer(value) {

}
export { StringBufferFieldSubType };
export const EMPTY_UINT8_ARRAY = new Uint8Array(0);

@@ -54,0 +52,0 @@ export class BufferLikeFieldDefinition extends StructFieldDefinition {

@@ -82,3 +82,3 @@ import { StructFieldDefinition, StructFieldValue } from "../basic/index.js";

};
})(NumberFieldType = NumberFieldType || (NumberFieldType = {}));
})(NumberFieldType || (NumberFieldType = {}));
export class NumberFieldDefinition extends StructFieldDefinition {

@@ -85,0 +85,0 @@ type;

{
"name": "@yume-chan/struct",
"version": "0.0.20",
"version": "0.0.21",
"description": "C-style structure serializer and deserializer.",

@@ -30,15 +30,15 @@ "keywords": [

"dependencies": {
"@yume-chan/dataview-bigint-polyfill": "^0.0.20",
"tslib": "^2.5.2"
"@yume-chan/dataview-bigint-polyfill": "^0.0.21",
"tslib": "^2.6.0"
},
"devDependencies": {
"@jest/globals": "^29.5.0",
"@jest/globals": "^29.6.1",
"@yume-chan/eslint-config": "^1.0.0",
"@yume-chan/tsconfig": "^1.0.0",
"cross-env": "^7.0.3",
"eslint": "^8.41.0",
"eslint": "^8.44.0",
"jest": "^29.5.0",
"prettier": "^2.8.8",
"ts-jest": "^29.0.4",
"typescript": "^5.0.3"
"prettier": "^3.0.0",
"ts-jest": "^29.1.1",
"typescript": "^5.1.6"
},

@@ -45,0 +45,0 @@ "scripts": {

@@ -16,3 +16,3 @@ import type { StructFieldValue } from "./field-value.js";

TValue = unknown,
TOmitInitKey extends PropertyKey = never
TOmitInitKey extends PropertyKey = never,
> {

@@ -23,3 +23,3 @@ /**

*/
public readonly TValue!: TValue;
readonly TValue!: TValue;

@@ -30,7 +30,7 @@ /**

*/
public readonly TOmitInitKey!: TOmitInitKey;
readonly TOmitInitKey!: TOmitInitKey;
public readonly options: TOptions;
readonly options: TOptions;
public constructor(options: TOptions) {
constructor(options: TOptions) {
this.options = options;

@@ -44,3 +44,3 @@ }

*/
public abstract getSize(): number;
abstract getSize(): number;

@@ -50,6 +50,6 @@ /**

*/
public abstract create(
abstract create(
options: Readonly<StructOptions>,
structValue: StructValue,
value: TValue
value: TValue,
): StructFieldValue<this>;

@@ -63,12 +63,12 @@

*/
public abstract deserialize(
abstract deserialize(
options: Readonly<StructOptions>,
stream: ExactReadable,
structValue: StructValue
structValue: StructValue,
): StructFieldValue<this>;
public abstract deserialize(
abstract deserialize(
options: Readonly<StructOptions>,
stream: AsyncExactReadable,
struct: StructValue
struct: StructValue,
): Promise<StructFieldValue<this>>;
}

@@ -16,14 +16,14 @@ import type { StructFieldDefinition } from "./definition.js";

any
> = StructFieldDefinition<any, any, any>
> = StructFieldDefinition<any, any, any>,
> {
/** Gets the definition associated with this runtime value */
public readonly definition: TDefinition;
readonly definition: TDefinition;
/** Gets the options of the associated `Struct` */
public readonly options: Readonly<StructOptions>;
readonly options: Readonly<StructOptions>;
/** Gets the associated `Struct` instance */
public readonly struct: StructValue;
readonly struct: StructValue;
public get hasCustomAccessors(): boolean {
get hasCustomAccessors(): boolean {
return (

@@ -37,7 +37,7 @@ this.get !== StructFieldValue.prototype.get ||

public constructor(
constructor(
definition: TDefinition,
options: Readonly<StructOptions>,
struct: StructValue,
value: TDefinition["TValue"]
value: TDefinition["TValue"],
) {

@@ -55,3 +55,3 @@ this.definition = definition;

*/
public getSize(): number {
getSize(): number {
return this.definition.getSize();

@@ -63,3 +63,3 @@ }

*/
public get(): TDefinition["TValue"] {
get(): TDefinition["TValue"] {
return this.value;

@@ -71,3 +71,3 @@ }

*/
public set(value: TDefinition["TValue"]): void {
set(value: TDefinition["TValue"]): void {
this.value = value;

@@ -79,3 +79,3 @@ }

*/
public abstract serialize(dataView: DataView, offset: number): void;
abstract serialize(dataView: DataView, offset: number): void;
}

@@ -6,3 +6,3 @@ import type { ValueOrPromise } from "../utils.js";

export class ExactReadableEndedError extends Error {
public constructor() {
constructor() {
super("ExactReadable ended");

@@ -9,0 +9,0 @@ Object.setPrototypeOf(this, new.target.prototype);

@@ -17,5 +17,5 @@ import type { StructFieldValue } from "./field-value.js";

*/
public readonly value: Record<PropertyKey, unknown>;
readonly value: Record<PropertyKey, unknown>;
public constructor(prototype: object) {
constructor(prototype: object) {
// PERF: `Object.create(extra)` is 50% faster

@@ -39,3 +39,3 @@ // than `Object.defineProperties(this.value, extra)`

*/
public set(name: PropertyKey, fieldValue: StructFieldValue): void {
set(name: PropertyKey, fieldValue: StructFieldValue): void {
this.fieldValues[name] = fieldValue;

@@ -66,5 +66,5 @@

*/
public get(name: PropertyKey): StructFieldValue {
get(name: PropertyKey): StructFieldValue {
return this.fieldValues[name]!;
}
}

@@ -53,3 +53,3 @@ import type {

TFieldName extends PropertyKey,
TDefinition extends StructFieldDefinition<any, any, any>
TDefinition extends StructFieldDefinition<any, any, any>,
> = Identity<

@@ -74,3 +74,3 @@ Struct<

TExtra extends object,
TPostDeserialized
TPostDeserialized,
> {

@@ -89,3 +89,3 @@ /**

TType extends BufferFieldSubType<any, any>,
TTypeScriptType = TType["TTypeScriptType"]
TTypeScriptType = TType["TTypeScriptType"],
>(

@@ -95,3 +95,3 @@ name: TName,

options: FixedLengthBufferLikeFieldOptions,
typeScriptType?: TTypeScriptType
typeScriptType?: TTypeScriptType,
): AddFieldDescriptor<

@@ -116,3 +116,3 @@ TFields,

TOptions extends VariableLengthBufferLikeFieldOptions<TFields>,
TTypeScriptType = TType["TTypeScriptType"]
TTypeScriptType = TType["TTypeScriptType"],
>(

@@ -122,3 +122,3 @@ name: TName,

options: TOptions,
typeScriptType?: TTypeScriptType
typeScriptType?: TTypeScriptType,
): AddFieldDescriptor<

@@ -142,3 +142,3 @@ TFields,

TPostDeserialized,
TType extends BufferFieldSubType<any, any>
TType extends BufferFieldSubType<any, any>,
> {

@@ -148,3 +148,3 @@ <TName extends PropertyKey, TTypeScriptType = TType["TTypeScriptType"]>(

options: FixedLengthBufferLikeFieldOptions,
typeScriptType?: TTypeScriptType
typeScriptType?: TTypeScriptType,
): AddFieldDescriptor<

@@ -170,7 +170,7 @@ TFields,

>,
TTypeScriptType = TType["TTypeScriptType"]
TTypeScriptType = TType["TTypeScriptType"],
>(
name: TName,
options: TOptions,
typeScriptType?: TTypeScriptType
typeScriptType?: TTypeScriptType,
): AddFieldDescriptor<

@@ -192,3 +192,3 @@ TFields,

this: TFields,
object: TFields
object: TFields,
) => TPostDeserialized;

@@ -199,3 +199,3 @@

TExtra extends object,
TPostDeserialized
TPostDeserialized,
> = TPostDeserialized extends undefined

@@ -206,3 +206,3 @@ ? Overwrite<TExtra, TFields>

export class StructDeserializeError extends Error {
public constructor(message: string) {
constructor(message: string) {
super(message);

@@ -214,5 +214,5 @@ Object.setPrototypeOf(this, new.target.prototype);

export class StructNotEnoughDataError extends StructDeserializeError {
public constructor() {
constructor() {
super(
"The underlying readable was ended before the struct was fully deserialized"
"The underlying readable was ended before the struct was fully deserialized",
);

@@ -223,3 +223,3 @@ }

export class StructEmptyError extends StructDeserializeError {
public constructor() {
constructor() {
super("The underlying readable doesn't contain any more struct");

@@ -233,3 +233,3 @@ }

TExtra extends object = Record<never, never>,
TPostDeserialized = undefined
TPostDeserialized = undefined,
> implements

@@ -240,11 +240,11 @@ StructLike<

{
public readonly TFields!: TFields;
readonly TFields!: TFields;
public readonly TOmitInitKey!: TOmitInitKey;
readonly TOmitInitKey!: TOmitInitKey;
public readonly TExtra!: TExtra;
readonly TExtra!: TExtra;
public readonly TInit!: Evaluate<Omit<TFields, TOmitInitKey>>;
readonly TInit!: Evaluate<Omit<TFields, TOmitInitKey>>;
public readonly TDeserializeResult!: StructDeserializedResult<
readonly TDeserializeResult!: StructDeserializedResult<
TFields,

@@ -255,3 +255,3 @@ TExtra,

public readonly options: Readonly<StructOptions>;
readonly options: Readonly<StructOptions>;

@@ -262,3 +262,3 @@ #size = 0;

*/
public get size() {
get size() {
return this.#size;

@@ -269,7 +269,7 @@ }

name: PropertyKey,
definition: StructFieldDefinition<any, any, any>
definition: StructFieldDefinition<any, any, any>,
][] = [];
public get fields(): readonly [
get fields(): readonly [
name: PropertyKey,
definition: StructFieldDefinition<any, any, any>
definition: StructFieldDefinition<any, any, any>,
][] {

@@ -283,3 +283,3 @@ return this.#fields;

public constructor(options?: Partial<Readonly<StructOptions>>) {
constructor(options?: Partial<Readonly<StructOptions>>) {
this.options = { ...StructDefaultOptions, ...options };

@@ -291,8 +291,8 @@ }

*/
public field<
field<
TName extends PropertyKey,
TDefinition extends StructFieldDefinition<any, any, any>
TDefinition extends StructFieldDefinition<any, any, any>,
>(
name: TName,
definition: TDefinition
definition: TDefinition,
): AddFieldDescriptor<

@@ -311,3 +311,3 @@ TFields,

throw new Error(
`This struct already have a field with name '${nameString}'`
`This struct already have a field with name '${nameString}'`,
);

@@ -329,4 +329,4 @@ }

*/
public concat<TOther extends Struct<any, any, any, any>>(
other: TOther
concat<TOther extends Struct<any, any, any, any>>(
other: TOther,
): Struct<

@@ -344,3 +344,3 @@ TFields & TOther["TFields"],

this.#extra,
Object.getOwnPropertyDescriptors(other.#extra)
Object.getOwnPropertyDescriptors(other.#extra),
);

@@ -350,10 +350,10 @@ return this as any;

private number<
#number<
TName extends PropertyKey,
TType extends NumberFieldType = NumberFieldType,
TTypeScriptType = number
TTypeScriptType = number,
>(name: TName, type: TType, typeScriptType?: TTypeScriptType) {
return this.field(
name,
new NumberFieldDefinition(type, typeScriptType)
new NumberFieldDefinition(type, typeScriptType),
);

@@ -365,7 +365,7 @@ }

*/
public int8<TName extends PropertyKey, TTypeScriptType = number>(
int8<TName extends PropertyKey, TTypeScriptType = number>(
name: TName,
typeScriptType?: TTypeScriptType
typeScriptType?: TTypeScriptType,
) {
return this.number(name, NumberFieldType.Int8, typeScriptType);
return this.#number(name, NumberFieldType.Int8, typeScriptType);
}

@@ -376,7 +376,7 @@

*/
public uint8<TName extends PropertyKey, TTypeScriptType = number>(
uint8<TName extends PropertyKey, TTypeScriptType = number>(
name: TName,
typeScriptType?: TTypeScriptType
typeScriptType?: TTypeScriptType,
) {
return this.number(name, NumberFieldType.Uint8, typeScriptType);
return this.#number(name, NumberFieldType.Uint8, typeScriptType);
}

@@ -387,7 +387,7 @@

*/
public int16<TName extends PropertyKey, TTypeScriptType = number>(
int16<TName extends PropertyKey, TTypeScriptType = number>(
name: TName,
typeScriptType?: TTypeScriptType
typeScriptType?: TTypeScriptType,
) {
return this.number(name, NumberFieldType.Int16, typeScriptType);
return this.#number(name, NumberFieldType.Int16, typeScriptType);
}

@@ -398,7 +398,7 @@

*/
public uint16<TName extends PropertyKey, TTypeScriptType = number>(
uint16<TName extends PropertyKey, TTypeScriptType = number>(
name: TName,
typeScriptType?: TTypeScriptType
typeScriptType?: TTypeScriptType,
) {
return this.number(name, NumberFieldType.Uint16, typeScriptType);
return this.#number(name, NumberFieldType.Uint16, typeScriptType);
}

@@ -409,7 +409,7 @@

*/
public int32<TName extends PropertyKey, TTypeScriptType = number>(
int32<TName extends PropertyKey, TTypeScriptType = number>(
name: TName,
typeScriptType?: TTypeScriptType
typeScriptType?: TTypeScriptType,
) {
return this.number(name, NumberFieldType.Int32, typeScriptType);
return this.#number(name, NumberFieldType.Int32, typeScriptType);
}

@@ -420,17 +420,17 @@

*/
public uint32<TName extends PropertyKey, TTypeScriptType = number>(
uint32<TName extends PropertyKey, TTypeScriptType = number>(
name: TName,
typeScriptType?: TTypeScriptType
typeScriptType?: TTypeScriptType,
) {
return this.number(name, NumberFieldType.Uint32, typeScriptType);
return this.#number(name, NumberFieldType.Uint32, typeScriptType);
}
private bigint<
#bigint<
TName extends PropertyKey,
TType extends BigIntFieldType = BigIntFieldType,
TTypeScriptType = TType["TTypeScriptType"]
TTypeScriptType = TType["TTypeScriptType"],
>(name: TName, type: TType, typeScriptType?: TTypeScriptType) {
return this.field(
name,
new BigIntFieldDefinition(type, typeScriptType)
new BigIntFieldDefinition(type, typeScriptType),
);

@@ -444,7 +444,7 @@ }

*/
public int64<
int64<
TName extends PropertyKey,
TTypeScriptType = BigIntFieldType["TTypeScriptType"]
TTypeScriptType = BigIntFieldType["TTypeScriptType"],
>(name: TName, typeScriptType?: TTypeScriptType) {
return this.bigint(name, BigIntFieldType.Int64, typeScriptType);
return this.#bigint(name, BigIntFieldType.Int64, typeScriptType);
}

@@ -457,10 +457,10 @@

*/
public uint64<
uint64<
TName extends PropertyKey,
TTypeScriptType = BigIntFieldType["TTypeScriptType"]
TTypeScriptType = BigIntFieldType["TTypeScriptType"],
>(name: TName, typeScriptType?: TTypeScriptType) {
return this.bigint(name, BigIntFieldType.Uint64, typeScriptType);
return this.#bigint(name, BigIntFieldType.Uint64, typeScriptType);
}
private arrayBufferLike: ArrayBufferLikeFieldCreator<
#arrayBufferLike: ArrayBufferLikeFieldCreator<
TFields,

@@ -475,3 +475,3 @@ TOmitInitKey,

| FixedLengthBufferLikeFieldOptions
| VariableLengthBufferLikeFieldOptions
| VariableLengthBufferLikeFieldOptions,
): any => {

@@ -481,3 +481,3 @@ if ("length" in options) {

name,
new FixedLengthBufferLikeFieldDefinition(type, options)
new FixedLengthBufferLikeFieldDefinition(type, options),
);

@@ -487,3 +487,3 @@ } else {

name,
new VariableLengthBufferLikeFieldDefinition(type, options)
new VariableLengthBufferLikeFieldDefinition(type, options),
);

@@ -493,3 +493,3 @@ }

public uint8Array: BoundArrayBufferLikeFieldDefinitionCreator<
uint8Array: BoundArrayBufferLikeFieldDefinitionCreator<
TFields,

@@ -501,11 +501,11 @@ TOmitInitKey,

> = (name: PropertyKey, options: any, typeScriptType: any): any => {
return this.arrayBufferLike(
return this.#arrayBufferLike(
name,
Uint8ArrayBufferFieldSubType.Instance,
options,
typeScriptType
typeScriptType,
);
};
public string: BoundArrayBufferLikeFieldDefinitionCreator<
string: BoundArrayBufferLikeFieldDefinitionCreator<
TFields,

@@ -517,7 +517,7 @@ TOmitInitKey,

> = (name: PropertyKey, options: any, typeScriptType: any): any => {
return this.arrayBufferLike(
return this.#arrayBufferLike(
name,
StringBufferFieldSubType.Instance,
options,
typeScriptType
typeScriptType,
);

@@ -536,3 +536,3 @@ };

*/
public extra<
extra<
T extends Record<

@@ -542,9 +542,9 @@ // This trick disallows any keys that are already in `TValue`

never
>
>,
>(
value: T & ThisType<Overwrite<Overwrite<TExtra, T>, TFields>>
value: T & ThisType<Overwrite<Overwrite<TExtra, T>, TFields>>,
): Struct<TFields, TOmitInitKey, Overwrite<TExtra, T>, TPostDeserialized> {
Object.defineProperties(
this.#extra,
Object.getOwnPropertyDescriptors(value)
Object.getOwnPropertyDescriptors(value),
);

@@ -560,4 +560,4 @@ return this as any;

*/
public postDeserialize(
callback: StructPostDeserialized<TFields, never>
postDeserialize(
callback: StructPostDeserialized<TFields, never>,
): Struct<TFields, TOmitInitKey, TExtra, never>;

@@ -570,4 +570,4 @@ /**

*/
public postDeserialize(
callback?: StructPostDeserialized<TFields, void>
postDeserialize(
callback?: StructPostDeserialized<TFields, void>,
): Struct<TFields, TOmitInitKey, TExtra, undefined>;

@@ -580,6 +580,6 @@ /**

*/
public postDeserialize<TPostSerialize>(
callback?: StructPostDeserialized<TFields, TPostSerialize>
postDeserialize<TPostSerialize>(
callback?: StructPostDeserialized<TFields, TPostSerialize>,
): Struct<TFields, TOmitInitKey, TExtra, TPostSerialize>;
public postDeserialize(callback?: StructPostDeserialized<TFields, any>) {
postDeserialize(callback?: StructPostDeserialized<TFields, any>) {
this.#postDeserialized = callback;

@@ -592,10 +592,10 @@ return this as any;

*/
public deserialize(
stream: ExactReadable
deserialize(
stream: ExactReadable,
): StructDeserializedResult<TFields, TExtra, TPostDeserialized>;
public deserialize(
stream: AsyncExactReadable
deserialize(
stream: AsyncExactReadable,
): Promise<StructDeserializedResult<TFields, TExtra, TPostDeserialized>>;
public deserialize(
stream: ExactReadable | AsyncExactReadable
deserialize(
stream: ExactReadable | AsyncExactReadable,
): ValueOrPromise<

@@ -612,3 +612,3 @@ StructDeserializedResult<TFields, TExtra, TPostDeserialized>

.then(() =>
definition.deserialize(this.options, stream, structValue)
definition.deserialize(this.options, stream, structValue),
)

@@ -629,3 +629,3 @@ .then(

}
}
},
);

@@ -653,10 +653,10 @@ }

public serialize(init: Evaluate<Omit<TFields, TOmitInitKey>>): Uint8Array;
public serialize(
serialize(init: Evaluate<Omit<TFields, TOmitInitKey>>): Uint8Array;
serialize(
init: Evaluate<Omit<TFields, TOmitInitKey>>,
output: Uint8Array
output: Uint8Array,
): number;
public serialize(
serialize(
init: Evaluate<Omit<TFields, TOmitInitKey>>,
output?: Uint8Array
output?: Uint8Array,
): Uint8Array | number {

@@ -678,3 +678,3 @@ let structValue: StructValue;

structValue,
(init as any)[name]
(init as any)[name],
);

@@ -704,3 +704,3 @@ structValue.set(name, fieldValue);

output.byteOffset,
output.byteLength
output.byteLength,
);

@@ -707,0 +707,0 @@ let offset = 0;

@@ -10,3 +10,3 @@ export interface SyncPromise<T> {

| null
| undefined
| undefined,
): SyncPromise<TResult1 | TResult2>;

@@ -61,7 +61,7 @@

public constructor(promise: PromiseLike<T>) {
constructor(promise: PromiseLike<T>) {
this.#promise = promise;
}
public then<TResult1 = T, TResult2 = never>(
then<TResult1 = T, TResult2 = never>(
onfulfilled?:

@@ -74,10 +74,10 @@ | ((value: T) => TResult1 | PromiseLike<TResult1>)

| null
| undefined
| undefined,
) {
return new PendingSyncPromise<TResult1 | TResult2>(
this.#promise.then(onfulfilled, onrejected)
this.#promise.then(onfulfilled, onrejected),
);
}
public valueOrPromise(): T | PromiseLike<T> {
valueOrPromise(): T | PromiseLike<T> {
return this.#promise;

@@ -90,11 +90,11 @@ }

public constructor(value: T) {
constructor(value: T) {
this.#value = value;
}
public then<TResult1 = T>(
then<TResult1 = T>(
onfulfilled?:
| ((value: T) => TResult1 | PromiseLike<TResult1>)
| null
| undefined
| undefined,
) {

@@ -107,3 +107,3 @@ if (!onfulfilled) {

public valueOrPromise(): T | PromiseLike<T> {
valueOrPromise(): T | PromiseLike<T> {
return this.#value;

@@ -116,7 +116,7 @@ }

public constructor(reason: any) {
constructor(reason: any) {
this.#reason = reason;
}
public then<TResult1 = T, TResult2 = never>(
then<TResult1 = T, TResult2 = never>(
onfulfilled?:

@@ -129,3 +129,3 @@ | ((value: T) => TResult1 | PromiseLike<TResult1>)

| null
| undefined
| undefined,
) {

@@ -138,5 +138,5 @@ if (!onrejected) {

public valueOrPromise(): T | PromiseLike<T> {
valueOrPromise(): T | PromiseLike<T> {
throw this.#reason;
}
}

@@ -21,3 +21,3 @@ import {

byteOffset: number,
littleEndian: boolean | undefined
littleEndian: boolean | undefined,
) => bigint;

@@ -29,18 +29,18 @@

value: bigint,
littleEndian: boolean | undefined
littleEndian: boolean | undefined,
) => void;
export class BigIntFieldType {
public readonly TTypeScriptType!: bigint;
readonly TTypeScriptType!: bigint;
public readonly size: number;
readonly size: number;
public readonly getter: DataViewBigInt64Getter;
readonly getter: DataViewBigInt64Getter;
public readonly setter: DataViewBigInt64Setter;
readonly setter: DataViewBigInt64Setter;
public constructor(
constructor(
size: number,
getter: DataViewBigInt64Getter,
setter: DataViewBigInt64Setter
setter: DataViewBigInt64Setter,
) {

@@ -52,13 +52,5 @@ this.size = size;

public static readonly Int64 = new BigIntFieldType(
8,
getBigInt64,
setBigInt64
);
static readonly Int64 = new BigIntFieldType(8, getBigInt64, setBigInt64);
public static readonly Uint64 = new BigIntFieldType(
8,
getBigUint64,
setBigUint64
);
static readonly Uint64 = new BigIntFieldType(8, getBigUint64, setBigUint64);
}

@@ -68,7 +60,7 @@

TType extends BigIntFieldType = BigIntFieldType,
TTypeScriptType = TType["TTypeScriptType"]
TTypeScriptType = TType["TTypeScriptType"],
> extends StructFieldDefinition<void, TTypeScriptType> {
public readonly type: TType;
readonly type: TType;
public constructor(type: TType, typescriptType?: TTypeScriptType) {
constructor(type: TType, typescriptType?: TTypeScriptType) {
void typescriptType;

@@ -79,10 +71,10 @@ super();

public getSize(): number {
getSize(): number {
return this.type.size;
}
public create(
create(
options: Readonly<StructOptions>,
struct: StructValue,
value: TTypeScriptType
value: TTypeScriptType,
): BigIntFieldValue<this> {

@@ -92,16 +84,16 @@ return new BigIntFieldValue(this, options, struct, value);

public override deserialize(
override deserialize(
options: Readonly<StructOptions>,
stream: ExactReadable,
struct: StructValue
struct: StructValue,
): BigIntFieldValue<this>;
public override deserialize(
override deserialize(
options: Readonly<StructOptions>,
stream: AsyncExactReadable,
struct: StructValue
struct: StructValue,
): Promise<BigIntFieldValue<this>>;
public override deserialize(
override deserialize(
options: Readonly<StructOptions>,
stream: ExactReadable | AsyncExactReadable,
struct: StructValue
struct: StructValue,
): ValueOrPromise<BigIntFieldValue<this>> {

@@ -115,3 +107,3 @@ return SyncPromise.try(() => {

array.byteOffset,
array.byteLength
array.byteLength,
);

@@ -126,5 +118,5 @@ const value = this.type.getter(view, 0, options.littleEndian);

export class BigIntFieldValue<
TDefinition extends BigIntFieldDefinition<BigIntFieldType, any>
TDefinition extends BigIntFieldDefinition<BigIntFieldType, any>,
> extends StructFieldValue<TDefinition> {
public serialize(dataView: DataView, offset: number): void {
serialize(dataView: DataView, offset: number): void {
this.definition.type.setter(

@@ -134,5 +126,5 @@ dataView,

this.value,
this.options.littleEndian
this.options.littleEndian,
);
}
}

@@ -23,5 +23,5 @@ import type {

TValue = unknown,
TTypeScriptType = TValue
TTypeScriptType = TValue,
> {
public readonly TTypeScriptType!: TTypeScriptType;
readonly TTypeScriptType!: TTypeScriptType;

@@ -34,6 +34,6 @@ /**

*/
public abstract toBuffer(value: TValue): Uint8Array;
abstract toBuffer(value: TValue): Uint8Array;
/** When implemented in derived classes, converts the `Uint8Array` to a type-specific value */
public abstract toValue(array: Uint8Array): TValue;
abstract toValue(array: Uint8Array): TValue;

@@ -47,3 +47,3 @@ /**

*/
public abstract getSize(value: TValue): number;
abstract getSize(value: TValue): number;
}

@@ -53,5 +53,5 @@

export class Uint8ArrayBufferFieldSubType<
TTypeScriptType = Uint8Array
TTypeScriptType = Uint8Array,
> extends BufferFieldSubType<Uint8Array, TTypeScriptType> {
public static readonly Instance = new Uint8ArrayBufferFieldSubType();
static readonly Instance = new Uint8ArrayBufferFieldSubType();

@@ -62,11 +62,11 @@ protected constructor() {

public toBuffer(value: Uint8Array): Uint8Array {
toBuffer(value: Uint8Array): Uint8Array {
return value;
}
public toValue(buffer: Uint8Array): Uint8Array {
toValue(buffer: Uint8Array): Uint8Array {
return buffer;
}
public getSize(value: Uint8Array): number {
getSize(value: Uint8Array): number {
return value.byteLength;

@@ -78,15 +78,15 @@ }

export class StringBufferFieldSubType<
TTypeScriptType = string
TTypeScriptType = string,
> extends BufferFieldSubType<string, TTypeScriptType> {
public static readonly Instance = new StringBufferFieldSubType();
static readonly Instance = new StringBufferFieldSubType();
public toBuffer(value: string): Uint8Array {
toBuffer(value: string): Uint8Array {
return encodeUtf8(value);
}
public toValue(array: Uint8Array): string {
toValue(array: Uint8Array): string {
return decodeUtf8(array);
}
public getSize(): number {
getSize(): number {
// Return `-1`, so `BufferLikeFieldDefinition` will

@@ -108,7 +108,7 @@ // convert this `value` into an `Uint8Array` (and cache the result),

TOmitInitKey extends PropertyKey = never,
TTypeScriptType = TType["TTypeScriptType"]
TTypeScriptType = TType["TTypeScriptType"],
> extends StructFieldDefinition<TOptions, TTypeScriptType, TOmitInitKey> {
public readonly type: TType;
readonly type: TType;
public constructor(type: TType, options: TOptions) {
constructor(type: TType, options: TOptions) {
super(options);

@@ -126,7 +126,7 @@ this.type = type;

*/
public create(
create(
options: Readonly<StructOptions>,
struct: StructValue,
value: TType["TTypeScriptType"],
array?: Uint8Array
array?: Uint8Array,
): BufferLikeFieldValue<this> {

@@ -136,16 +136,16 @@ return new BufferLikeFieldValue(this, options, struct, value, array);

public override deserialize(
override deserialize(
options: Readonly<StructOptions>,
stream: ExactReadable,
struct: StructValue
struct: StructValue,
): BufferLikeFieldValue<this>;
public override deserialize(
override deserialize(
options: Readonly<StructOptions>,
stream: AsyncExactReadable,
struct: StructValue
struct: StructValue,
): Promise<BufferLikeFieldValue<this>>;
public override deserialize(
override deserialize(
options: Readonly<StructOptions>,
stream: ExactReadable | AsyncExactReadable,
struct: StructValue
struct: StructValue,
): ValueOrPromise<BufferLikeFieldValue<this>> {

@@ -173,7 +173,7 @@ return SyncPromise.try(() => {

any
>
>,
> extends StructFieldValue<TDefinition> {
protected array: Uint8Array | undefined;
public constructor(
constructor(
definition: TDefinition,

@@ -183,3 +183,3 @@ options: Readonly<StructOptions>,

value: TDefinition["TValue"],
array?: Uint8Array
array?: Uint8Array,
) {

@@ -190,3 +190,3 @@ super(definition, options, struct, value);

public override set(value: TDefinition["TValue"]): void {
override set(value: TDefinition["TValue"]): void {
super.set(value);

@@ -198,3 +198,3 @@ // When value changes, clear the cached `array`

public serialize(dataView: DataView, offset: number): void {
serialize(dataView: DataView, offset: number): void {
if (!this.array) {

@@ -207,5 +207,5 @@ this.array = this.definition.type.toBuffer(this.value);

dataView.byteOffset,
dataView.byteLength
dataView.byteLength,
).set(this.array, offset);
}
}

@@ -10,8 +10,9 @@ import type { BufferFieldSubType } from "./base.js";

TType extends BufferFieldSubType = BufferFieldSubType,
TOptions extends FixedLengthBufferLikeFieldOptions = FixedLengthBufferLikeFieldOptions,
TTypeScriptType = TType["TTypeScriptType"]
TOptions extends
FixedLengthBufferLikeFieldOptions = FixedLengthBufferLikeFieldOptions,
TTypeScriptType = TType["TTypeScriptType"],
> extends BufferLikeFieldDefinition<TType, TOptions, never, TTypeScriptType> {
public getSize(): number {
getSize(): number {
return this.options.length;
}
}

@@ -16,3 +16,3 @@ import type {

TFields = object,
TLengthField extends LengthField<TFields> = any
TLengthField extends LengthField<TFields> = any,
> {

@@ -37,4 +37,5 @@ /**

TType extends BufferFieldSubType = BufferFieldSubType,
TOptions extends VariableLengthBufferLikeFieldOptions = VariableLengthBufferLikeFieldOptions,
TTypeScriptType = TType["TTypeScriptType"]
TOptions extends
VariableLengthBufferLikeFieldOptions = VariableLengthBufferLikeFieldOptions,
TTypeScriptType = TType["TTypeScriptType"],
> extends BufferLikeFieldDefinition<

@@ -46,3 +47,3 @@ TType,

> {
public getSize(): number {
getSize(): number {
return 0;

@@ -59,7 +60,7 @@ }

public override create(
override create(
options: Readonly<StructOptions>,
struct: StructValue,
value: TTypeScriptType,
array?: Uint8Array
array?: Uint8Array,
): VariableLengthBufferLikeStructFieldValue<this> {

@@ -71,3 +72,3 @@ return new VariableLengthBufferLikeStructFieldValue(

value,
array
array,
);

@@ -78,3 +79,4 @@ }

export class VariableLengthBufferLikeStructFieldValue<
TDefinition extends VariableLengthBufferLikeFieldDefinition = VariableLengthBufferLikeFieldDefinition
TDefinition extends
VariableLengthBufferLikeFieldDefinition = VariableLengthBufferLikeFieldDefinition,
> extends BufferLikeFieldValue<TDefinition> {

@@ -85,3 +87,3 @@ protected length: number | undefined;

public constructor(
constructor(
definition: TDefinition,

@@ -91,3 +93,3 @@ options: Readonly<StructOptions>,

value: TDefinition["TValue"],
array?: Uint8Array
array?: Uint8Array,
) {

@@ -106,3 +108,3 @@ super(definition, options, struct, value, array);

originalValue,
this
this,
);

@@ -112,3 +114,3 @@ struct.set(lengthField, this.lengthFieldValue);

public override getSize() {
override getSize() {
if (this.length === undefined) {

@@ -125,3 +127,3 @@ this.length = this.definition.type.getSize(this.value);

public override set(value: unknown) {
override set(value: unknown) {
super.set(value);

@@ -143,5 +145,5 @@ this.array = undefined;

public constructor(
constructor(
originalField: StructFieldValue,
arrayBufferField: VariableLengthBufferLikeFieldValueLike
arrayBufferField: VariableLengthBufferLikeFieldValueLike,
) {

@@ -152,3 +154,3 @@ super(

originalField.struct,
0
0,
);

@@ -159,7 +161,7 @@ this.originalField = originalField;

public override getSize() {
override getSize() {
return this.originalField.getSize();
}
public override get() {
override get() {
let value: string | number = this.bufferField.getSize();

@@ -170,3 +172,3 @@

value = value.toString(
this.bufferField.definition.options.lengthFieldRadix ?? 10
this.bufferField.definition.options.lengthFieldRadix ?? 10,
);

@@ -178,3 +180,3 @@ }

public override set() {
override set() {
// Ignore setting

@@ -181,0 +183,0 @@ // It will always be in sync with the buffer size

@@ -19,3 +19,3 @@ import type {

value: number,
littleEndian: boolean
littleEndian: boolean,
): void;

@@ -113,7 +113,7 @@ }

TType extends NumberFieldType = NumberFieldType,
TTypeScriptType = number
TTypeScriptType = number,
> extends StructFieldDefinition<void, TTypeScriptType> {
public readonly type: TType;
readonly type: TType;
public constructor(type: TType, typescriptType?: TTypeScriptType) {
constructor(type: TType, typescriptType?: TTypeScriptType) {
void typescriptType;

@@ -124,10 +124,10 @@ super();

public getSize(): number {
getSize(): number {
return this.type.size;
}
public create(
create(
options: Readonly<StructOptions>,
struct: StructValue,
value: TTypeScriptType
value: TTypeScriptType,
): NumberFieldValue<this> {

@@ -137,16 +137,16 @@ return new NumberFieldValue(this, options, struct, value);

public override deserialize(
override deserialize(
options: Readonly<StructOptions>,
stream: ExactReadable,
struct: StructValue
struct: StructValue,
): NumberFieldValue<this>;
public override deserialize(
override deserialize(
options: Readonly<StructOptions>,
stream: AsyncExactReadable,
struct: StructValue
struct: StructValue,
): Promise<NumberFieldValue<this>>;
public override deserialize(
override deserialize(
options: Readonly<StructOptions>,
stream: ExactReadable | AsyncExactReadable,
struct: StructValue
struct: StructValue,
): ValueOrPromise<NumberFieldValue<this>> {

@@ -159,3 +159,3 @@ return SyncPromise.try(() => {

array,
options.littleEndian
options.littleEndian,
);

@@ -169,5 +169,5 @@ return this.create(options, struct, value as any);

export class NumberFieldValue<
TDefinition extends NumberFieldDefinition<NumberFieldType, any>
TDefinition extends NumberFieldDefinition<NumberFieldType, any>,
> extends StructFieldValue<TDefinition> {
public serialize(dataView: DataView, offset: number): void {
serialize(dataView: DataView, offset: number): void {
this.definition.type.serialize(

@@ -177,5 +177,5 @@ dataView,

this.value,
this.options.littleEndian
this.options.littleEndian,
);
}
}

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc