From e08ef60efd1b853d56f68127e995b99b9ad12d99 Mon Sep 17 00:00:00 2001 From: John Chadwick Date: Sun, 17 Mar 2019 23:25:23 -0700 Subject: [PATCH 1/2] Add TypeScript typings. --- KaitaiStream.d.ts | 216 ++++++++++++++++++++++++++++++++++++++++++++++ index.d.ts | 3 + package.json | 1 + 3 files changed, 220 insertions(+) create mode 100644 KaitaiStream.d.ts create mode 100644 index.d.ts diff --git a/KaitaiStream.d.ts b/KaitaiStream.d.ts new file mode 100644 index 0000000..6c084c0 --- /dev/null +++ b/KaitaiStream.d.ts @@ -0,0 +1,216 @@ +export = KaitaiStream.KaitaiStream; + +export as namespace KaitaiStream; + +declare namespace KaitaiStream { + class EOFError extends Error { + bytesReq: number; + bytesAvail: number; + constructor(bytesReq: number, bytesAvail: number); + } + + class UnexpectedDataError extends Error { + expected: any; + actual: any; + constructor(expected: any, actual: any); + } + + class UndecidedEndiannessError extends Error { + constructor(); + } + + class KaitaiStream { + pos: number; + bits: number; + bitsLeft: number; + /** + KaitaiStream is an implementation of Kaitai Struct API for JavaScript. + Based on DataStream - https://github.com/kig/DataStream.js + + @param arrayBuffer ArrayBuffer to read from. + @param byteOffset Offset from arrayBuffer beginning for the KaitaiStream. + */ + constructor(arrayBuffer: ArrayBufferLike, byteOffset?: number); + /** + Dependency configuration data. Holds urls for (optional) dynamic loading + of code dependencies from a remote server. For use by (static) processing functions. + + Caller should the supported keys to the asset urls as needed. + NOTE: `depUrls` is a static property of KaitaiStream (the factory),like the various + processing functions. It is NOT part of the prototype of instances. + */ + static depUrls: { + zlib: string; + }; + /** + Virtual byte length of the KaitaiStream backing buffer. + Updated to be max of original buffer size and last written size. + If dynamicSize is false is set to buffer size. + */ + _byteLength: number; + /** + Set/get the backing ArrayBuffer of the KaitaiStream object. + The setter updates the DataView to point to the new buffer. + */ + buffer: ArrayBuffer; + /** + Set/get the byteOffset of the KaitaiStream object. + The setter updates the DataView to point to the new byteOffset. + */ + byteOffset: number; + /** + Set/get the backing DataView of the KaitaiStream object. + The setter updates the buffer and byteOffset to point to the DataView values. + */ + dataView: DataView; + /** + Returns true if the KaitaiStream seek pointer is at the end of buffer and + there's no more data to read. + + @return True if the seek pointer is at the end of the buffer. + */ + isEof(): boolean; + /** + Sets the KaitaiStream read/write position to given position. + Clamps between 0 and KaitaiStream length. + + @param pos Position to seek to. + */ + seek(pos: number): void; + /** + Returns the byte length of the KaitaiStream object. + */ + readonly size: number; + /** + Reads an 8-bit signed int from the stream. + @return The read number. + */ + readS1(): number; + /** + Reads a 16-bit big-endian signed int from the stream. + @return The read number. + */ + readS2be(): number; + /** + Reads a 32-bit big-endian signed int from the stream. + @return The read number. + */ + readS4be(): number; + /** + Reads a 64-bit big-endian unsigned int from the stream. Note that + JavaScript does not support 64-bit integers natively, so it will + automatically upgrade internal representation to use IEEE 754 + double precision float. + @return The read number. + */ + readS8be(): number; + /** + Reads a 16-bit little-endian signed int from the stream. + @return The read number. + */ + readS2le(): number; + /** + Reads a 32-bit little-endian signed int from the stream. + @return The read number. + */ + readS4le(): number; + /** + Reads a 64-bit little-endian unsigned int from the stream. Note that + JavaScript does not support 64-bit integers natively, so it will + automatically upgrade internal representation to use IEEE 754 + double precision float. + @return The read number. + */ + readS8le(): number; + /** + Reads an 8-bit unsigned int from the stream. + @return {number} The read number. + */ + readU1(): number; + /** + Reads a 16-bit big-endian unsigned int from the stream. + @return The read number. + */ + readU2be(): number; + /** + Reads a 32-bit big-endian unsigned int from the stream. + @return The read number. + */ + readU4be(): number; + /** + Reads a 64-bit big-endian unsigned int from the stream. Note that + JavaScript does not support 64-bit integers natively, so it will + automatically upgrade internal representation to use IEEE 754 + double precision float. + @return The read number. + */ + readU8be(): number; + /** + Reads a 16-bit little-endian unsigned int from the stream. + @return The read number. + */ + readU2le(): number; + /** + Reads a 32-bit little-endian unsigned int from the stream. + @return The read number. + */ + readU4le(): number; + /** + Reads a 64-bit little-endian unsigned int from the stream. Note that + JavaScript does not support 64-bit integers natively, so it will + automatically upgrade internal representation to use IEEE 754 + double precision float. + @return The read number. + */ + readU8le(): number; + readF4be(): number; + readF8be(): number; + readF4le(): number; + readF8le(): number; + alignToByte(): void; + readBitsInt(n: number): number; + /** + Native endianness. Either KaitaiStream.BIG_ENDIAN or KaitaiStream.LITTLE_ENDIAN + depending on the platform endianness. + */ + static endianness: boolean; + readBytes(len: number): Uint8Array; + readBytesFull(): Uint8Array; + readBytesTerm(terminator: any, include: any, consume: any, eosError: any): any; + ensureFixedContents(expected: any): Uint8Array; + static bytesStripRight(data: any, padByte: any): any; + static bytesTerminate(data: any, term: any, include: any): any; + static bytesToStr(arr: Uint8Array, encoding: string): any; + static processXorOne(data: any, key: any): Uint8Array; + static processXorMany(data: any, key: any): Uint8Array; + static processRotateLeft(data: any, amount: any, groupSize: any): Uint8Array; + static processZlib(buf: Uint8Array): any; + static mod(a: any, b: any): number; + static arrayMin(arr: any): any; + static arrayMax(arr: any): any; + static byteArrayCompare(a: any, b: any): number; + /** + Maps a Uint8Array into the KaitaiStream buffer. + + Nice for quickly reading in data. + + @param length Number of elements to map. + @return Uint8Array to the KaitaiStream backing buffer. + */ + mapUint8Array(length: number): Uint8Array; + /** + Creates an array from an array of character codes. + Uses String.fromCharCode in chunks for memory efficiency and then concatenates + the resulting string chunks. + + @param array Array of character codes. + @return String created from the character codes. + **/ + static createStringFromArray(array: Uint8Array): string; + + static EOFError: typeof EOFError; + static UnexpectedDataError: typeof UnexpectedDataError; + static UndecidedEndiannessError: typeof UndecidedEndiannessError; + } +} + diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 0000000..fbf55ee --- /dev/null +++ b/index.d.ts @@ -0,0 +1,3 @@ +import KaitaiStream from './KaitaiStream'; + +export { KaitaiStream }; diff --git a/package.json b/package.json index e9f46ba..9f6e60e 100644 --- a/package.json +++ b/package.json @@ -3,6 +3,7 @@ "version": "0.8.0-SNAPSHOT.11", "description": "Kaitai Struct: runtime library for Javascript", "main": "index.js", + "types": "index.d.ts", "repository": { "type": "git", "url": "git+https://github.com/kaitai-io/kaitai_struct_javascript_runtime.git" From 07faa3d9286e80dedea533a288c8236bf1facee2 Mon Sep 17 00:00:00 2001 From: John Chadwick Date: Mon, 18 Mar 2019 08:28:53 -0700 Subject: [PATCH 2/2] Remove a couple of non-public APIs from the TypeScript definitions. --- KaitaiStream.d.ts | 18 ------------------ 1 file changed, 18 deletions(-) diff --git a/KaitaiStream.d.ts b/KaitaiStream.d.ts index 6c084c0..e922af7 100644 --- a/KaitaiStream.d.ts +++ b/KaitaiStream.d.ts @@ -189,24 +189,6 @@ declare namespace KaitaiStream { static arrayMin(arr: any): any; static arrayMax(arr: any): any; static byteArrayCompare(a: any, b: any): number; - /** - Maps a Uint8Array into the KaitaiStream buffer. - - Nice for quickly reading in data. - - @param length Number of elements to map. - @return Uint8Array to the KaitaiStream backing buffer. - */ - mapUint8Array(length: number): Uint8Array; - /** - Creates an array from an array of character codes. - Uses String.fromCharCode in chunks for memory efficiency and then concatenates - the resulting string chunks. - - @param array Array of character codes. - @return String created from the character codes. - **/ - static createStringFromArray(array: Uint8Array): string; static EOFError: typeof EOFError; static UnexpectedDataError: typeof UnexpectedDataError;