diff --git a/assembly/buffer/index.ts b/assembly/buffer/index.ts index 91be52d..8804f1a 100644 --- a/assembly/buffer/index.ts +++ b/assembly/buffer/index.ts @@ -2,6 +2,14 @@ import { BLOCK_MAXSIZE, BLOCK, BLOCK_OVERHEAD } from "rt/common"; import { E_INVALIDLENGTH, E_INDEXOUTOFRANGE } from "util/error"; import { Uint8Array } from "typedarray"; +export const kMaxLength = constants.MAX_LENGTH; +export const kMaxStringLength = constants.MAX_STRING_LENGTH; + +export namespace constants { + export const MAX_LENGTH = i32.MAX_VALUE; // (2 ^ 31) - 1 (~2GB) + export const MAX_STRING_LENGTH = BLOCK_MAXSIZE >>> alignof(); +} + export class Buffer extends Uint8Array { constructor(size: i32) { super(size); @@ -250,7 +258,7 @@ export class Buffer extends Uint8Array { } return this; } - + swap32(): Buffer { let dataLength = this.dataLength; // Make sure dataLength is divisible by 4 @@ -263,7 +271,7 @@ export class Buffer extends Uint8Array { } return this; } - + swap64(): Buffer { let dataLength = this.dataLength; // Make sure dataLength is divisible by 8 diff --git a/assembly/node.d.ts b/assembly/node.d.ts index 419384a..78b98df 100644 --- a/assembly/node.d.ts +++ b/assembly/node.d.ts @@ -95,3 +95,19 @@ declare namespace Buffer { export function decodeUnsafe(ptr: usize, byteLength: i32): string; } } + +declare module "buffer" { + export type TranscodeEncoding = "ascii" | "utf8" | "utf16le" | "ucs2" | "latin1" | "binary"; + + export const kMaxLength: i32; + export const kStringMaxLength: i32; + + export const constants: { + MAX_LENGTH: i32; + MAX_STRING_LENGTH: i32; + }; + + // To export the buffer, we must obtain the `typeof Buffer` + const BuffType: typeof Buffer; + export { BuffType as Buffer }; +}