Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
kuhe committed Jun 5, 2024
1 parent 952394a commit 83ba8b5
Show file tree
Hide file tree
Showing 8 changed files with 316 additions and 338 deletions.
94 changes: 36 additions & 58 deletions packages/core/src/submodules/cbor/ByteVector.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { fromUtf8 } from "@smithy/util-utf8";

import { alloc, Uint8 } from "./cbor-types";

const USE_BUFFER = typeof Buffer !== "undefined";
const USE_TEXT_ENCODER = typeof TextEncoder !== "undefined";

Expand All @@ -13,70 +15,49 @@ type BufferWithUtf8Write = Buffer & {
*
*/
export class ByteVector {
private data: Uint8Array = new Uint8Array();
private dataView: DataView = new DataView(this.data.buffer, 0, 0);
private data: Uint8Array = alloc(0);
private dataView: DataView = new DataView(this.data.buffer, this.data.byteOffset, this.data.byteLength);
private cursor: number = 0;
private textEncoder: TextEncoder | null = USE_TEXT_ENCODER ? new TextEncoder() : null;

public constructor(private initialSize: number = 1_000_000) {
this.resize(initialSize);
}

public write(...bytes: number[]) {
for (const byte of bytes) {
if (this.cursor === this.data.length) {
this.resize(this.cursor + this.initialSize);
}
this.data[this.cursor++] = byte;
}
public write(byte: Uint8) {
this.data[this.cursor++] = byte;
}

public writeBytes(bytes: Uint8Array) {
if (this.cursor + bytes.length >= this.data.length) {
this.resize(this.cursor + bytes.length + this.initialSize);
}
this.data.set(bytes, this.cursor);
this.cursor += bytes.byteLength;
}

public writeUnsignedInt(major: number, bitSize: 16 | 32 | 64, value: number | bigint) {
if (this.cursor + bitSize / 8 >= this.data.length) {
this.resize(this.cursor + bitSize / 8 + this.initialSize);
}
const dv = byteVector.getDataView();
switch (bitSize) {
case 16:
this.write(major);
dv.setUint16(byteVector.getCursor(), Number(value) as number);
this.cursor += 2;
break;
case 32:
this.write(major);
dv.setUint32(byteVector.getCursor(), Number(value) as number);
this.cursor += 4;
break;
case 64:
this.write(major);
dv.setBigUint64(byteVector.getCursor(), BigInt(value) as bigint);
this.cursor += 8;
break;
}
public writeUint16(major: number, value: number) {
this.data[this.cursor++] = major;
this.data[this.cursor++] = value >> 8;
this.data[this.cursor++] = value & 0b1111_1111;
}

public writeUint32(major: number, value: number) {
this.data[this.cursor++] = major;
this.dataView.setUint32(this.cursor, value as number);
this.cursor += 4;
}

public writeUint64(major: number, value: number | bigint) {
this.data[this.cursor++] = major;
this.dataView.setBigUint64(this.cursor, typeof value === "bigint" ? value : BigInt(value));
this.cursor += 8;
}

public writeFloat64(major: number, value: number) {
if (this.cursor + 8 >= this.data.length) {
this.resize(this.cursor + 8 + this.initialSize);
}
const dv = byteVector.getDataView();
this.write(major);
dv.setFloat64(this.cursor, value);
this.data[this.cursor++] = major;
this.dataView.setFloat64(this.cursor, value);
this.cursor += 8;
}

public writeString(str: string) {
if (this.cursor + str.length * 4 > this.data.length) {
this.resize(this.cursor + str.length * 4 + this.initialSize);
}
if (USE_BUFFER && (this.data as BufferWithUtf8Write).utf8Write) {
this.cursor += (this.data as BufferWithUtf8Write).utf8Write(str, this.cursor);
} else if (USE_TEXT_ENCODER && this.textEncoder?.encodeInto) {
Expand All @@ -87,34 +68,31 @@ export class ByteVector {
}
}

public ensureSpace(bytes: number) {
if (this.data.byteLength - this.cursor < bytes) {
this.resize(this.data.byteLength + this.initialSize + bytes);
}
}

public toUint8Array(): Uint8Array {
const out = new Uint8Array(this.cursor);
const out = alloc(this.cursor);
out.set(this.data.subarray(0, this.cursor), 0);
this.cursor = 0;
if (this.data.length > this.initialSize) {
this.data = new Uint8Array(this.initialSize);
this.dataView = new DataView(this.data.buffer, 0, this.initialSize);
this.data = alloc(this.initialSize);
this.dataView = new DataView(this.data.buffer, this.data.byteOffset, this.data.byteLength);
}
return out;
}

public getDataView() {
return this.dataView;
}

public getCursor() {
return this.cursor;
}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
private resize(size: number) {
const data = this.data;
this.data = USE_BUFFER ? Buffer.allocUnsafeSlow(size) : new Uint8Array(size);
this.data = alloc(size);
if (data) {
this.data.set(data, 0);
}
this.dataView = new DataView(this.data.buffer, 0, size);
this.dataView = new DataView(this.data.buffer, this.data.byteOffset, this.data.byteLength);
}
}

export const byteVector = new ByteVector();
export const byteVector = new ByteVector(10_000_000);
10 changes: 6 additions & 4 deletions packages/core/src/submodules/cbor/DecodeView.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { toUtf8 } from "@smithy/util-utf8";

import { alloc } from "./cbor-types";

const USE_TEXT_DECODER = typeof TextDecoder !== "undefined";

/**
* Data container for synchronous decoding.
*/
export class DecodeView {
public payload = new Uint8Array();
public dataView = new DataView(this.payload.buffer, 0, this.payload.length);
public payload = alloc(0);
public dataView = new DataView(this.payload.buffer, this.payload.byteOffset, this.payload.byteLength);
private textDecoder = USE_TEXT_DECODER ? new TextDecoder() : null;

public constructor(payload: Uint8Array) {
Expand All @@ -16,7 +18,7 @@ export class DecodeView {

public set(payload: Uint8Array) {
this.payload = payload;
this.dataView = new DataView(this.payload.buffer, 0, this.payload.length);
this.dataView = new DataView(this.payload.buffer, this.payload.byteOffset, this.payload.byteLength);
}

public toUtf8(bytes: Uint8Array, at: number, to: number): string {
Expand All @@ -27,4 +29,4 @@ export class DecodeView {
}
}

export const decodeView = new DecodeView(new Uint8Array());
export const decodeView = new DecodeView(alloc(0));
Loading

0 comments on commit 83ba8b5

Please sign in to comment.