Skip to content
This repository has been archived by the owner on May 27, 2022. It is now read-only.

Commit

Permalink
update typescript runtime to use util for TextEncoder/Decoder, return…
Browse files Browse the repository at this point in the history
… null for Unit (instead of undefined), handle more than double space needed for a new byte array insert (ensureBufferWillHandleSize)
  • Loading branch information
danprinz authored and ma2bd committed Dec 24, 2020
1 parent 1fd02a9 commit 9064157
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
6 changes: 4 additions & 2 deletions serde-generate/runtime/typescript/serde/binaryDeserializer.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { Deserializer } from './deserializer';
import util from 'util';

export abstract class BinaryDeserializer implements Deserializer {
private static readonly BIG_32 = BigInt(32);
private static readonly BIG_64 = BigInt(64);
private static readonly textDecoder: TextDecoder = new TextDecoder();
private static readonly textDecoder =
typeof window === 'undefined' ? new util.TextDecoder() : new TextDecoder();
public buffer: ArrayBuffer;
public offset: number;

Expand Down Expand Up @@ -49,7 +51,7 @@ export abstract class BinaryDeserializer implements Deserializer {
}

public deserializeUnit(): null {
return;
return null;
}

public deserializeU8(): number {
Expand Down
13 changes: 7 additions & 6 deletions serde-generate/runtime/typescript/serde/binarySerializer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Serializer } from './serializer';
import util from 'util';

export abstract class BinarySerializer implements Serializer {
private static readonly BIG_32 = BigInt(32);
Expand All @@ -10,7 +11,8 @@ export abstract class BinarySerializer implements Serializer {
private static readonly BIG_32Fs = BigInt('4294967295');
private static readonly BIG_64Fs = BigInt('18446744073709551615');

private static readonly textEncoder: TextEncoder = new TextEncoder();
private static readonly textEncoder =
typeof window === 'undefined' ? new util.TextEncoder() : new TextEncoder();

private buffer: ArrayBuffer;
private offset: number;
Expand All @@ -21,12 +23,11 @@ export abstract class BinarySerializer implements Serializer {
}

private ensureBufferWillHandleSize(bytes: number) {
if (this.offset + bytes <= this.buffer.byteLength) {
return;
while (this.buffer.byteLength < this.offset + bytes) {
const newBuffer = new ArrayBuffer(this.buffer.byteLength * 2);
new Uint8Array(newBuffer).set(new Uint8Array(this.buffer));
this.buffer = newBuffer;
}
const newBuffer = new ArrayBuffer(this.buffer.byteLength * 2);
new Uint8Array(newBuffer).set(new Uint8Array(this.buffer));
this.buffer = newBuffer;
}

protected serialize(values: Uint8Array) {
Expand Down

0 comments on commit 9064157

Please sign in to comment.