Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
[Chore] switch to global
  • Loading branch information
jtenner committed Jul 23, 2019
commit 10a5c54945865baa7886c8027cf856c8d2aece72
5 changes: 2 additions & 3 deletions assembly/buffer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { Uint8Array } from "typedarray";

const BUFFER_INSPECT_HEADER_START: string = "<Buffer ";

export let INSPECT_MAX_BYTES: i32 = 50;

export class Buffer extends Uint8Array {
constructor(size: i32) {
super(size);
Expand Down Expand Up @@ -47,11 +49,8 @@ export class Buffer extends Uint8Array {
return load<i8>(this.dataStart + usize(offset));
}

public INSPECT_MAX_BYTES: i32 = 50;

inspect(): string {
let byteLength = this.byteLength;
let INSPECT_MAX_BYTES = this.INSPECT_MAX_BYTES;
if (INSPECT_MAX_BYTES == 0 || byteLength == 0) return "<Buffer >";

// Calculate if an elipsis will be in the string
Expand Down
16 changes: 16 additions & 0 deletions assembly/node.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,19 @@ declare class Buffer extends Uint8Array {
/** Inspect a buffer. */
inspect(): string;
}

declare module "buffer" {
/**
* The maximum number of bytes to inspect on a buffer.
*
* @example
* import { INSPECT_MAX_BYTES } from "buffer";
* // @ts-ignore: This is treated like a global
* INSPECT_MAX_BYTES = <i32>10;
*/
export var INSPECT_MAX_BYTES: i32;

// To export the buffer, we must obtain the `typeof Buffer`
const BuffType: typeof Buffer;
export { BuffType as Buffer };
}
3 changes: 2 additions & 1 deletion tests/buffer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* });
*/
import { BLOCK_MAXSIZE } from "rt/common";
import { INSPECT_MAX_BYTES } from "buffer";

describe("buffer", () => {
test("#constructor", () => {
Expand Down Expand Up @@ -95,7 +96,7 @@ describe("buffer", () => {
for (let i = 0; i < 16; i++) buff[i] = i;
let result = buff.inspect();
expect<string>(result).toBe("<Buffer 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f>");
buff.INSPECT_MAX_BYTES = 5;
INSPECT_MAX_BYTES = 5;
result = buff.inspect();
expect<string>(result).toBe("<Buffer 00 01 02 03 04...>");

Expand Down