Skip to content

Commit

Permalink
repo,etc: workaround TextDecoder memory leak
Browse files Browse the repository at this point in the history
nodejs/node#32424

This is needed for Node < 13.12.0
  • Loading branch information
yoursunny committed Apr 11, 2020
1 parent c8040b9 commit 96a4c08
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 9 deletions.
3 changes: 2 additions & 1 deletion packages/keychain/src/cert/validity-period.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import { Decoder, Encodable, Encoder, EvDecoder, Extension } from "@ndn/tlv";
import { TT } from "./an";

const timestampRe = /^(\d{4})(\d{2})(\d{2})T(\d{2})(\d{2})(\d{2})$/;
const textDecoder = new TextDecoder(); // https://github.com/nodejs/node/issues/32424 workaround

function decodeTimestamp(value: Uint8Array): Date {
const str = new TextDecoder().decode(value);
const str = textDecoder.decode(value);
const match = timestampRe.exec(str);
if (!match) {
throw new Error("invalid ISO8601 compact timestamp");
Expand Down
4 changes: 3 additions & 1 deletion packages/ndncert/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ export async function makeInterestParams(json: unknown, aesKey?: CryptoKey): Pro
return encoder.output;
}

const textDecoder = new TextDecoder(); // https://github.com/nodejs/node/issues/32424 workaround

export async function readDataPayload(payload: Uint8Array, aesKey?: CryptoKey): Promise<unknown> {
let body = payload;
if (aesKey) {
Expand All @@ -44,7 +46,7 @@ export async function readDataPayload(payload: Uint8Array, aesKey?: CryptoKey):
}
body = new Uint8Array(await crypto.subtle.decrypt({ name: "AES-CBC", iv }, aesKey, encrypted));
}
return JSON.parse(new TextDecoder().decode(body));
return JSON.parse(textDecoder.decode(body));
}

/**
Expand Down
4 changes: 3 additions & 1 deletion packages/nfdmgmt/src/control-response.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { Decoder, EvDecoder } from "@ndn/tlv";

const textDecoder = new TextDecoder(); // https://github.com/nodejs/node/issues/32424 workaround

const EVD = new EvDecoder<ControlResponse>("ControlResponse", 0x65)
.add(0x66, (t, { nni }) => t.statusCode = nni)
.add(0x67, (t, { value }) => t.statusText = new TextDecoder().decode(value))
.add(0x67, (t, { value }) => t.statusText = textDecoder.decode(value))
.setIsCritical(() => false);

/** NFD Management ControlResponse struct (decoding only). */
Expand Down
4 changes: 3 additions & 1 deletion packages/packet/src/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ const CHARCODE_PERIOD = ".".charCodeAt(0);

export type ComponentLike = Component | string;

const textDecoder = new TextDecoder(); // https://github.com/nodejs/node/issues/32424 workaround

/**
* Name component.
* This type is immutable.
Expand All @@ -34,7 +36,7 @@ export class Component {

/** TLV-VALUE interpreted as UTF-8 string. */
public get text(): string {
return new TextDecoder().decode(this.value);
return textDecoder.decode(this.value);
}

public static decodeFrom(decoder: Decoder): Component {
Expand Down
16 changes: 11 additions & 5 deletions packages/repo/src/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ export interface Record {
export type Db = LevelUp<EncodingDown<Name, Record>, AbstractIterator<Name, Record>>;
export type DbChain = LevelUpChain<Name, Record>;

const textDecoder = new TextDecoder(); // https://github.com/nodejs/node/issues/32424 workaround

export function openDb(db: AbstractLevelDOWN): Db {
return level(EncodingDown<Name, Record>(db, {
keyEncoding: {
Expand All @@ -34,12 +36,16 @@ export function openDb(db: AbstractLevelDOWN): Db {
},
decode(stored: Buffer): Record {
const { decoder, after } = new Decoder(stored).read();
const record = JSON.parse(new TextDecoder().decode(after)) as Record;
let data: Data|undefined;
const record = JSON.parse(textDecoder.decode(after)) as Record;
Object.defineProperties(record, {
data: { get() {
return (data = data ?? decoder.decode(Data));
} },
data: {
configurable: true,
get() {
const value = decoder.decode(Data);
Object.defineProperty(record, "data", { value });
return value;
},
},
name: {
configurable: true,
/* istanbul ignore next */
Expand Down

0 comments on commit 96a4c08

Please sign in to comment.