Skip to content

Commit 5649ff2

Browse files
committed
Define Error subclasses differently for better TS definitions
Custom exception classes are now defined in a more idiomatic way - this approach was adopted from https://stackoverflow.com/a/68201883 In the generated JavaScript code, this commit only causes cosmetic changes that don't affect the observable behavior. However, it improves the quality of the generated TypeScript definitions - see the comparison below. Before: ```ts static EOFError: { new (bytesReq: number, bytesAvail: number): { name: string; bytesReq: number; bytesAvail: number; message: string; stack?: string; }; captureStackTrace(targetObject: object, constructorOpt?: Function): void; prepareStackTrace(err: Error, stackTraces: NodeJS.CallSite[]): any; stackTraceLimit: number; }; ``` Now: ```ts class EOFError extends Error { name: string; bytesReq: number; bytesAvail: number; /** * @param bytesReq The number of bytes requested. * @param bytesAvail The number of bytes available. */ constructor(bytesReq: number, bytesAvail: number); } ```
1 parent 17430ba commit 5649ff2

1 file changed

Lines changed: 59 additions & 57 deletions

File tree

KaitaiStream.ts

Lines changed: 59 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -977,7 +977,57 @@ class KaitaiStream {
977977
// Internal implementation details
978978
// ========================================================================
979979

980-
public static EOFError = class EOFError extends Error {
980+
/**
981+
* Ensures that we have at least `length` bytes left in the stream.
982+
* If not, throws an EOFError.
983+
*
984+
* @param length Number of bytes to require.
985+
* @throws {KaitaiStream.EOFError}
986+
*/
987+
protected ensureBytesLeft(length: number): void {
988+
if (this.pos + length > this.size) {
989+
throw new KaitaiStream.EOFError(length, this.size - this.pos);
990+
}
991+
}
992+
993+
/**
994+
* Maps a Uint8Array into the KaitaiStream buffer.
995+
* Nice for quickly reading in data.
996+
*
997+
* @param length Number of elements to map.
998+
* @returns A Uint8Array to the KaitaiStream backing buffer.
999+
*/
1000+
protected mapUint8Array(length: number): Uint8Array {
1001+
length |= 0;
1002+
1003+
this.ensureBytesLeft(length);
1004+
1005+
const arr = new Uint8Array(this._buffer, this.byteOffset + this.pos, length);
1006+
this.pos += length;
1007+
return arr;
1008+
}
1009+
1010+
/**
1011+
* Creates an array from an array of character codes.
1012+
* Uses String.fromCharCode in chunks for memory efficiency and then concatenates
1013+
* the resulting string chunks.
1014+
*
1015+
* @param array Array of character codes.
1016+
* @returns String created from the character codes.
1017+
*/
1018+
protected static createStringFromArray(array: Uint8Array): string {
1019+
const chunk_size = 0x8000;
1020+
const chunks = [];
1021+
for (let i = 0; i < array.length; i += chunk_size) {
1022+
const chunk = array.subarray(i, i + chunk_size);
1023+
chunks.push(String.fromCharCode.apply(null, chunk));
1024+
}
1025+
return chunks.join("");
1026+
}
1027+
}
1028+
1029+
namespace KaitaiStream {
1030+
export class EOFError extends Error {
9811031
public name = "EOFError";
9821032
public bytesReq: number;
9831033
public bytesAvail: number;
@@ -998,7 +1048,7 @@ class KaitaiStream {
9981048
/**
9991049
* Unused since Kaitai Struct Compiler v0.9+ - compatibility with older versions.
10001050
*/
1001-
public static UnexpectedDataError = class UnexpectedDataError extends Error {
1051+
export class UnexpectedDataError extends Error {
10021052
public name = "UnexpectedDataError";
10031053
public expected: any;
10041054
public actual: any;
@@ -1016,7 +1066,7 @@ class KaitaiStream {
10161066
}
10171067
};
10181068

1019-
public static UndecidedEndiannessError = class UndecidedEndiannessError extends Error {
1069+
export class UndecidedEndiannessError extends Error {
10201070
public name = "UndecidedEndiannessError";
10211071

10221072
public constructor() {
@@ -1026,7 +1076,7 @@ class KaitaiStream {
10261076
}
10271077
};
10281078

1029-
public static ValidationNotEqualError = class ValidationNotEqualError extends Error {
1079+
export class ValidationNotEqualError extends Error {
10301080
public name = "ValidationNotEqualError";
10311081
public expected: any;
10321082
public actual: any;
@@ -1044,7 +1094,7 @@ class KaitaiStream {
10441094
}
10451095
};
10461096

1047-
public static ValidationLessThanError = class ValidationLessThanError extends Error {
1097+
export class ValidationLessThanError extends Error {
10481098
public name = "ValidationLessThanError";
10491099
public min: any;
10501100
public actual: any;
@@ -1062,7 +1112,7 @@ class KaitaiStream {
10621112
}
10631113
};
10641114

1065-
public static ValidationGreaterThanError = class ValidationGreaterThanError extends Error {
1115+
export class ValidationGreaterThanError extends Error {
10661116
public name = "ValidationGreaterThanError";
10671117
public max: any;
10681118
public actual: any;
@@ -1080,7 +1130,7 @@ class KaitaiStream {
10801130
}
10811131
};
10821132

1083-
public static ValidationNotAnyOfError = class ValidationNotAnyOfError extends Error {
1133+
export class ValidationNotAnyOfError extends Error {
10841134
public name = "ValidationNotAnyOfError";
10851135
public actual: any;
10861136

@@ -1095,7 +1145,7 @@ class KaitaiStream {
10951145
}
10961146
};
10971147

1098-
public static ValidationNotInEnumError = class ValidationNotInEnumError extends Error {
1148+
export class ValidationNotInEnumError extends Error {
10991149
public name = "ValidationNotInEnumError";
11001150
public actual: any;
11011151

@@ -1110,7 +1160,7 @@ class KaitaiStream {
11101160
}
11111161
};
11121162

1113-
public static ValidationExprError = class ValidationExprError extends Error {
1163+
export class ValidationExprError extends Error {
11141164
public name = "ValidationExprError";
11151165
public actual: any;
11161166

@@ -1124,54 +1174,6 @@ class KaitaiStream {
11241174
this.actual = actual;
11251175
}
11261176
};
1127-
1128-
/**
1129-
* Ensures that we have at least `length` bytes left in the stream.
1130-
* If not, throws an EOFError.
1131-
*
1132-
* @param length Number of bytes to require.
1133-
* @throws {KaitaiStream.EOFError}
1134-
*/
1135-
protected ensureBytesLeft(length: number): void {
1136-
if (this.pos + length > this.size) {
1137-
throw new KaitaiStream.EOFError(length, this.size - this.pos);
1138-
}
1139-
}
1140-
1141-
/**
1142-
* Maps a Uint8Array into the KaitaiStream buffer.
1143-
* Nice for quickly reading in data.
1144-
*
1145-
* @param length Number of elements to map.
1146-
* @returns A Uint8Array to the KaitaiStream backing buffer.
1147-
*/
1148-
protected mapUint8Array(length: number): Uint8Array {
1149-
length |= 0;
1150-
1151-
this.ensureBytesLeft(length);
1152-
1153-
const arr = new Uint8Array(this._buffer, this.byteOffset + this.pos, length);
1154-
this.pos += length;
1155-
return arr;
1156-
}
1157-
1158-
/**
1159-
* Creates an array from an array of character codes.
1160-
* Uses String.fromCharCode in chunks for memory efficiency and then concatenates
1161-
* the resulting string chunks.
1162-
*
1163-
* @param array Array of character codes.
1164-
* @returns String created from the character codes.
1165-
*/
1166-
protected static createStringFromArray(array: Uint8Array): string {
1167-
const chunk_size = 0x8000;
1168-
const chunks = [];
1169-
for (let i = 0; i < array.length; i += chunk_size) {
1170-
const chunk = array.subarray(i, i + chunk_size);
1171-
chunks.push(String.fromCharCode.apply(null, chunk));
1172-
}
1173-
return chunks.join("");
1174-
}
11751177
}
11761178

11771179
export default KaitaiStream;

0 commit comments

Comments
 (0)