Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
kuhe committed Jun 4, 2024
1 parent 1c81e51 commit 437ec09
Show file tree
Hide file tree
Showing 7 changed files with 272 additions and 204 deletions.
18 changes: 11 additions & 7 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 } from "./cbor-types";

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

Expand All @@ -13,8 +15,8 @@ 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;

Expand Down Expand Up @@ -88,12 +90,13 @@ export class ByteVector {
}

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);
const { buffer, byteOffset, byteLength } = this.data;
this.dataView = new DataView(buffer, byteOffset, byteLength);
}
return out;
}
Expand All @@ -109,11 +112,12 @@ export class ByteVector {
// 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);
const { buffer, byteOffset, byteLength } = this.data;
this.dataView = new DataView(buffer, byteOffset, byteLength);
}
}

Expand Down
11 changes: 7 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,8 @@ export class DecodeView {

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

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

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

0 comments on commit 437ec09

Please sign in to comment.