From 9ffac514c966515c38e59deb0391c62062a45a5e Mon Sep 17 00:00:00 2001 From: Andrew Chou Date: Tue, 1 Nov 2022 22:34:18 -0400 Subject: [PATCH] chore: add types for b4a (#44) Co-authored-by: Seth Vincent --- types/modules.d.ts | 147 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 146 insertions(+), 1 deletion(-) diff --git a/types/modules.d.ts b/types/modules.d.ts index 1f2f3949..2d12ee66 100644 --- a/types/modules.d.ts +++ b/types/modules.d.ts @@ -912,4 +912,149 @@ declare module 'random-access-file' { export = RandomAccessFile } -declare module 'b4a' +// This only covers the Node definitions. Including the browser ones is a nice-to-have but out of scope for this project. +declare module 'b4a' { + export function isBuffer(value: any): boolean + + export function isEncoding(encoding: string): boolean + + export function alloc(...args: Parameters): Buffer + + export function allocUnsafe(size: number): Buffer + + export function allocUnsafeSlow(size: number): Buffer + + export function byteLength( + ...args: Parameters + ): Buffer + + export function compare(...args: Parameters): number + + export function concat(...args: Parameters): Buffer + + export function copy( + buffer: Parameters[0], + ...copyArgs: Parameters + ): Buffer + + export function equals(a: Buffer, b: Buffer): boolean + + export function fill( + buffer: Parameters[0], + ...fillArgs: Parameters + ): Buffer + + /** + * Ideally would do something like: + * + * export function from(...args: Parameters): Buffer + * + * but due to a TS limitation, this only returns the param types of the last function overload + * https://github.com/microsoft/TypeScript/issues/32164 + */ + export function from( + arrayBuffer: WithImplicitCoercion, + byteOffset?: number, + length?: number + ): Buffer + export function from(data: Uint8Array | ReadonlyArray): Buffer + export function from( + data: WithImplicitCoercion | string> + ): Buffer + export function from( + str: + | WithImplicitCoercion + | { + [Symbol.toPrimitive](hint: 'string'): string + }, + encoding?: BufferEncoding + ): Buffer + + export function includes( + buffer: Parameters[0], + ...includesArgs: Parameters + ): boolean + + export function indexOf( + buffer: Parameters[0], + ...indexOfArgs: Parameters + ): number + + export function lastIndexOf( + buffer: Parameters[0], + ...lastIndexOfArgs: Parameters + ): number + + export function swap16(buffer: Parameters[0]): Buffer + + export function swap32(buffer: Parameters[0]): Buffer + + export function swap64(buffer: Parameters[0]): Buffer + + export function toBuffer( + buffer: + | Buffer + | { + /** + * Ideally would do something like: + * + * buffer: Parameters[0] + * + * but due to a TS limitation, this only returns the param type of the last function overload + * https://github.com/microsoft/TypeScript/issues/32164 + */ + buffer: + | WithImplicitCoercion + | Uint8Array + | ReadonlyArray + | WithImplicitCoercion | string> + byteOffset?: number + byteLength?: number + } + ): Buffer + + export function toString( + buffer: Parameters[0], + ...toStringArgs: Parameters + ): string + + export function writeDoubleLE( + buffer: Parameters[0], + ...writeDoubleLEArgs: Parameters + ): number + + export function writeFloatLE( + buffer: Parameters[0], + ...writeFloatLEArgs: Parameters + ): number + + export function writeUInt32LE( + buffer: Parameters[0], + ...writeUInt32LEArgs: Parameters + ): number + + export function writeInt32LE( + buffer: Parameters[0], + ...writeInt32LEArgs: Parameters + ): number + + export function readDoubleLE( + buffer: Parameters[0], + ...readDoubleLEArgs: Parameters + ): number + + export function readFloatLE( + buffer: Parameters[0], + ...readFloatLEArgs: Parameters + ): number + + export function readUInt32LE( + buffer: Parameters[0], + ...readUInt32LEArgs: Parameters + ): number + + export function readInt32LE( + buffer: Parameters[0], + ...readInt32LEArgs: Parameters + ): number +} \ No newline at end of file