Skip to content

Commit 437ec09

Browse files
committed
wip
1 parent 1c81e51 commit 437ec09

File tree

7 files changed

+272
-204
lines changed

7 files changed

+272
-204
lines changed

packages/core/src/submodules/cbor/ByteVector.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { fromUtf8 } from "@smithy/util-utf8";
22

3+
import { alloc } from "./cbor-types";
4+
35
const USE_BUFFER = typeof Buffer !== "undefined";
46
const USE_TEXT_ENCODER = typeof TextEncoder !== "undefined";
57

@@ -13,8 +15,8 @@ type BufferWithUtf8Write = Buffer & {
1315
*
1416
*/
1517
export class ByteVector {
16-
private data: Uint8Array = new Uint8Array();
17-
private dataView: DataView = new DataView(this.data.buffer, 0, 0);
18+
private data: Uint8Array = alloc(0);
19+
private dataView: DataView = new DataView(this.data.buffer, this.data.byteOffset, this.data.byteLength);
1820
private cursor: number = 0;
1921
private textEncoder: TextEncoder | null = USE_TEXT_ENCODER ? new TextEncoder() : null;
2022

@@ -88,12 +90,13 @@ export class ByteVector {
8890
}
8991

9092
public toUint8Array(): Uint8Array {
91-
const out = new Uint8Array(this.cursor);
93+
const out = alloc(this.cursor);
9294
out.set(this.data.subarray(0, this.cursor), 0);
9395
this.cursor = 0;
9496
if (this.data.length > this.initialSize) {
95-
this.data = new Uint8Array(this.initialSize);
96-
this.dataView = new DataView(this.data.buffer, 0, this.initialSize);
97+
this.data = alloc(this.initialSize);
98+
const { buffer, byteOffset, byteLength } = this.data;
99+
this.dataView = new DataView(buffer, byteOffset, byteLength);
97100
}
98101
return out;
99102
}
@@ -109,11 +112,12 @@ export class ByteVector {
109112
// eslint-disable-next-line @typescript-eslint/no-unused-vars
110113
private resize(size: number) {
111114
const data = this.data;
112-
this.data = USE_BUFFER ? Buffer.allocUnsafeSlow(size) : new Uint8Array(size);
115+
this.data = alloc(size);
113116
if (data) {
114117
this.data.set(data, 0);
115118
}
116-
this.dataView = new DataView(this.data.buffer, 0, size);
119+
const { buffer, byteOffset, byteLength } = this.data;
120+
this.dataView = new DataView(buffer, byteOffset, byteLength);
117121
}
118122
}
119123

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
import { toUtf8 } from "@smithy/util-utf8";
22

3+
import { alloc } from "./cbor-types";
4+
35
const USE_TEXT_DECODER = typeof TextDecoder !== "undefined";
46

57
/**
68
* Data container for synchronous decoding.
79
*/
810
export class DecodeView {
9-
public payload = new Uint8Array();
10-
public dataView = new DataView(this.payload.buffer, 0, this.payload.length);
11+
public payload = alloc(0);
12+
public dataView = new DataView(this.payload.buffer, this.payload.byteOffset, this.payload.byteLength);
1113
private textDecoder = USE_TEXT_DECODER ? new TextDecoder() : null;
1214

1315
public constructor(payload: Uint8Array) {
@@ -16,7 +18,8 @@ export class DecodeView {
1618

1719
public set(payload: Uint8Array) {
1820
this.payload = payload;
19-
this.dataView = new DataView(this.payload.buffer, 0, this.payload.length);
21+
const { buffer, byteOffset, byteLength } = this.payload;
22+
this.dataView = new DataView(buffer, byteOffset, byteLength);
2023
}
2124

2225
public toUtf8(bytes: Uint8Array, at: number, to: number): string {
@@ -27,4 +30,4 @@ export class DecodeView {
2730
}
2831
}
2932

30-
export const decodeView = new DecodeView(new Uint8Array());
33+
export const decodeView = new DecodeView(alloc(0));

0 commit comments

Comments
 (0)