Skip to content

Commit f2c9113

Browse files
committed
synchronous single data
1 parent 305b697 commit f2c9113

File tree

3 files changed

+270
-173
lines changed

3 files changed

+270
-173
lines changed

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

Lines changed: 49 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,22 @@
1-
import { CborMajorType } from "./cbor";
1+
import { fromUtf8 } from "@smithy/util-utf8";
22

3+
const USE_BUFFER = typeof Buffer !== "undefined";
4+
const USE_TEXT_ENCODER = typeof TextEncoder !== "undefined";
5+
6+
type BufferWithUtf8Write = Buffer & {
7+
utf8Write(str: string, index: number): number;
8+
};
9+
10+
/**
11+
*
12+
* Data container for synchronous encoding.
13+
*
14+
*/
315
export class ByteVector {
416
private data: Uint8Array = new Uint8Array();
517
private dataView: DataView = new DataView(this.data.buffer, 0, 0);
618
private cursor: number = 0;
19+
private textEncoder: TextEncoder | null = USE_TEXT_ENCODER ? new TextEncoder() : null;
720

821
public constructor(private initialSize: number = 1_000_000) {
922
this.resize(initialSize);
@@ -18,44 +31,70 @@ export class ByteVector {
1831
}
1932
}
2033

21-
public writeSeries(bytes: Uint8Array) {
34+
public writeBytes(bytes: Uint8Array) {
2235
if (this.cursor + bytes.length >= this.data.length) {
2336
this.resize(this.cursor + bytes.length + this.initialSize);
2437
}
2538
this.data.set(bytes, this.cursor);
2639
this.cursor += bytes.byteLength;
2740
}
2841

29-
public writeUnsignedInt(major: CborMajorType, bitSize: 16 | 32 | 64, value: number | bigint) {
42+
public writeUnsignedInt(major: number, bitSize: 16 | 32 | 64, value: number | bigint) {
3043
if (this.cursor + bitSize / 8 >= this.data.length) {
3144
this.resize(this.cursor + bitSize / 8 + this.initialSize);
3245
}
3346
const dv = byteVector.getDataView();
3447
switch (bitSize) {
3548
case 16:
36-
this.write((major << 5) | 25);
49+
this.write(major);
3750
dv.setUint16(byteVector.getCursor(), Number(value) as number);
3851
this.cursor += 2;
3952
break;
4053
case 32:
41-
this.write((major << 5) | 26);
54+
this.write(major);
4255
dv.setUint32(byteVector.getCursor(), Number(value) as number);
4356
this.cursor += 4;
4457
break;
4558
case 64:
46-
this.write((major << 5) | 27);
59+
this.write(major);
4760
dv.setBigUint64(byteVector.getCursor(), BigInt(value) as bigint);
4861
this.cursor += 8;
4962
break;
5063
}
5164
}
5265

66+
public writeFloat64(major: number, value: number) {
67+
if (this.cursor + 8 >= this.data.length) {
68+
this.resize(this.cursor + 8 + this.initialSize);
69+
}
70+
const dv = byteVector.getDataView();
71+
this.write(major);
72+
dv.setFloat64(this.cursor, value);
73+
this.cursor += 8;
74+
}
75+
76+
public writeString(str: string) {
77+
if (this.cursor + str.length * 4 > this.data.length) {
78+
this.resize(this.cursor + str.length * 4 + this.initialSize);
79+
}
80+
if (USE_BUFFER && (this.data as BufferWithUtf8Write).utf8Write) {
81+
this.cursor += (this.data as BufferWithUtf8Write).utf8Write(str, this.cursor);
82+
} else if (USE_TEXT_ENCODER && this.textEncoder?.encodeInto) {
83+
this.cursor += this.textEncoder.encodeInto(str, this.data.subarray(this.cursor)).written;
84+
} else {
85+
const bytes = fromUtf8(str);
86+
this.writeBytes(bytes);
87+
}
88+
}
89+
5390
public toUint8Array(): Uint8Array {
5491
const out = new Uint8Array(this.cursor);
55-
out.set(this.data.slice(0, this.cursor), 0);
56-
this.data = new Uint8Array(this.initialSize);
92+
out.set(this.data.subarray(0, this.cursor), 0);
5793
this.cursor = 0;
58-
this.dataView = new DataView(this.data.buffer, 0, this.initialSize);
94+
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+
}
5998
return out;
6099
}
61100

@@ -70,7 +109,7 @@ export class ByteVector {
70109
// eslint-disable-next-line @typescript-eslint/no-unused-vars
71110
private resize(size: number) {
72111
const data = this.data;
73-
this.data = typeof Buffer !== "undefined" ? new Uint8Array(size) : new Uint8Array(size);
112+
this.data = USE_BUFFER ? Buffer.allocUnsafeSlow(size) : new Uint8Array(size);
74113
if (data) {
75114
this.data.set(data, 0);
76115
}
@@ -79,17 +118,3 @@ export class ByteVector {
79118
}
80119

81120
export const byteVector = new ByteVector();
82-
83-
export function join(byteArrays: Uint8Array[]): Uint8Array {
84-
let length = 0;
85-
for (const arr of byteArrays) {
86-
length += arr.length;
87-
}
88-
let offset = 0;
89-
const joined = new Uint8Array(length);
90-
for (const arr of byteArrays) {
91-
joined.set(arr, offset);
92-
offset += arr.length;
93-
}
94-
return joined;
95-
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { toUtf8 } from "@smithy/util-utf8";
2+
3+
const USE_TEXT_DECODER = typeof TextDecoder !== "undefined";
4+
5+
/**
6+
* Data container for synchronous decoding.
7+
*/
8+
export class DecodeView {
9+
public payload = new Uint8Array();
10+
public dataView = new DataView(this.payload.buffer, 0, this.payload.length);
11+
private textDecoder = USE_TEXT_DECODER ? new TextDecoder() : null;
12+
13+
public constructor(payload: Uint8Array) {
14+
this.set(payload);
15+
}
16+
17+
public set(payload: Uint8Array) {
18+
this.payload = payload;
19+
this.dataView = new DataView(this.payload.buffer, 0, this.payload.length);
20+
}
21+
22+
public toUtf8(bytes: Uint8Array, at: number, to: number): string {
23+
if (this.textDecoder) {
24+
return this.textDecoder.decode(bytes.subarray(at, to));
25+
}
26+
return toUtf8(bytes.subarray(at, to));
27+
}
28+
}
29+
30+
export const decodeView = new DecodeView(new Uint8Array());

0 commit comments

Comments
 (0)