Skip to content

Commit 06647df

Browse files
authored
Merge pull request #8 from msgpack/encode_returns_uint8array
Make encode() return Uint8Array
2 parents f2bdffa + 3d5010a commit 06647df

File tree

4 files changed

+41
-4
lines changed

4 files changed

+41
-4
lines changed

src/encode.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { ExtensionCodec, ExtensionCodecType } from "./ExtensionCodec";
22
import { Encoder } from "./Encoder";
3+
import { WritableBuffer } from "./utils/WritableBuffer";
34

45
export type EncodeOptions = Readonly<{
56
maxDepth: number;
@@ -8,14 +9,14 @@ export type EncodeOptions = Readonly<{
89

910
export const DEFAULT_MAX_DEPTH = 100;
1011

11-
export function encode(value: unknown, options: Partial<EncodeOptions> = {}): Array<number> {
12-
const output: Array<number> = [];
12+
export function encode(value: unknown, options: Partial<EncodeOptions> = {}): Uint8Array {
13+
const output = new WritableBuffer();
1314

1415
const context = new Encoder(
1516
options.maxDepth || DEFAULT_MAX_DEPTH,
1617
options.extensionCodec || ExtensionCodec.defaultCodec,
1718
);
1819
context.encode(output, value, 1);
1920

20-
return output;
21+
return output.toUint8Array();
2122
}

src/utils/WritableBuffer.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { Writable } from "../Writable";
2+
3+
// Experimental `push(...bytes)`-able buffer
4+
// Unfortunately, it is always slower than Array<number>.
5+
6+
export class WritableBuffer implements Writable<number> {
7+
private length = 0;
8+
private buffer = new Uint8Array(128);
9+
10+
push(...bytes: ReadonlyArray<number>): void {
11+
const offset = this.length;
12+
const bytesLen = bytes.length;
13+
const newLength = offset + bytesLen;
14+
15+
if (this.buffer.length < newLength) {
16+
this.grow(newLength);
17+
}
18+
19+
const buffer = this.buffer;
20+
for (let i = 0; i < bytesLen; i++) {
21+
buffer[offset + i] = bytes[i];
22+
}
23+
this.length = newLength;
24+
}
25+
26+
grow(newLength: number) {
27+
const newBuffer = new Uint8Array(newLength * 2);
28+
newBuffer.set(this.buffer);
29+
this.buffer = newBuffer;
30+
}
31+
32+
toUint8Array(): Uint8Array {
33+
return this.buffer.subarray(0, this.length);
34+
}
35+
}

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"compilerOptions": {
33
/* Basic Options */
4-
"target": "es5", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */
4+
"target": "es2019", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */
55
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
66
"lib": ["es2019", "dom"], /* Specify library files to be included in the compilation. */
77
// "allowJs": true, /* Allow javascript files to be compiled. */

tsconfig.production.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"extends": "./tsconfig.json",
33
"compilerOptions": {
4+
"target": "es5",
45
"outDir": "./dist",
56
"declaration": true,
67
"noEmitOnError": true,

0 commit comments

Comments
 (0)