convert to typescript
This commit is contained in:
parent
0cdadb6df2
commit
308c2a323c
|
@ -0,0 +1,11 @@
|
||||||
|
{
|
||||||
|
"extends": [
|
||||||
|
"eslint:recommended",
|
||||||
|
"plugin:@typescript-eslint/recommended",
|
||||||
|
"prettier",
|
||||||
|
"plugin:prettier/recommended"
|
||||||
|
],
|
||||||
|
"parser": "@typescript-eslint/parser",
|
||||||
|
"plugins": ["@typescript-eslint"],
|
||||||
|
"root": true
|
||||||
|
}
|
|
@ -0,0 +1,2 @@
|
||||||
|
node_modules
|
||||||
|
dist
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,30 @@
|
||||||
|
{
|
||||||
|
"name": "noderl",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"type": "module",
|
||||||
|
"description": "",
|
||||||
|
"main": "./dist/main.js",
|
||||||
|
"types": "./dist/main.d.ts",
|
||||||
|
"scripts": {
|
||||||
|
"test": "echo \"Error: no test specified\" && exit 1"
|
||||||
|
},
|
||||||
|
"author": "",
|
||||||
|
"license": "ISC",
|
||||||
|
"files": [
|
||||||
|
"dist/**"
|
||||||
|
],
|
||||||
|
"devDependencies": {
|
||||||
|
"@types/node": "^20.11.8",
|
||||||
|
"@typescript-eslint/eslint-plugin": "^6.19.1",
|
||||||
|
"@typescript-eslint/parser": "^6.19.1",
|
||||||
|
"eslint": "^8.56.0",
|
||||||
|
"eslint-config-prettier": "^9.1.0",
|
||||||
|
"eslint-config-standard": "^17.1.0",
|
||||||
|
"eslint-plugin-import": "^2.29.1",
|
||||||
|
"eslint-plugin-n": "^16.6.2",
|
||||||
|
"eslint-plugin-prettier": "^5.1.3",
|
||||||
|
"eslint-plugin-promise": "^6.1.1",
|
||||||
|
"prettier": "^3.2.4",
|
||||||
|
"typescript": "^5.3.3"
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,509 @@
|
||||||
|
export const Types = {
|
||||||
|
BERT_START: 131,
|
||||||
|
SMALL_ATOM: 115,
|
||||||
|
ATOM: 100,
|
||||||
|
BINARY: 109,
|
||||||
|
SMALL_INTEGER: 97,
|
||||||
|
INTEGER: 98,
|
||||||
|
SMALL_BIG: 110,
|
||||||
|
LARGE_BIG: 111,
|
||||||
|
FLOAT: 99,
|
||||||
|
STRING: 107,
|
||||||
|
LIST: 108,
|
||||||
|
SMALL_TUPLE: 104,
|
||||||
|
LARGE_TUPLE: 105, // TODO: Implement.
|
||||||
|
MAP: 116,
|
||||||
|
NIL: 106,
|
||||||
|
NEW_FLOAT: 70,
|
||||||
|
ZERO: 0,
|
||||||
|
};
|
||||||
|
|
||||||
|
export const Lang = {
|
||||||
|
ELIXIR: 0,
|
||||||
|
ERLANG: 1,
|
||||||
|
};
|
||||||
|
|
||||||
|
export class Bert {
|
||||||
|
private allBinariesAsString;
|
||||||
|
private mapKeyAsAtom;
|
||||||
|
private decodeUndefinedValues;
|
||||||
|
private convention;
|
||||||
|
private outputBuffer;
|
||||||
|
|
||||||
|
constructor(
|
||||||
|
allBinariesAsString = false,
|
||||||
|
mapKeyAsAtom = true,
|
||||||
|
decodeUndefinedValues = true,
|
||||||
|
convention = Lang.ELIXIR,
|
||||||
|
) {
|
||||||
|
this.allBinariesAsString = allBinariesAsString;
|
||||||
|
this.mapKeyAsAtom = mapKeyAsAtom;
|
||||||
|
this.decodeUndefinedValues = decodeUndefinedValues;
|
||||||
|
this.convention = convention;
|
||||||
|
this.outputBuffer = Buffer.alloc(10000000);
|
||||||
|
this.outputBuffer[0] = Types.BERT_START;
|
||||||
|
}
|
||||||
|
|
||||||
|
toAtom = toAtom;
|
||||||
|
toTuple = toTuple;
|
||||||
|
|
||||||
|
#encode = (obj: any, buffer: Buffer) =>
|
||||||
|
(this as any)[`encode_${typeof obj}`](obj, buffer);
|
||||||
|
|
||||||
|
encode = (obj: any, noCopy = false) => {
|
||||||
|
const tailBuffer = this.#encode(obj, Buffer.from(this.outputBuffer, 1));
|
||||||
|
if (tailBuffer.length === 0) {
|
||||||
|
throw new Error("Bert encode a too big term, encoding buffer overflow");
|
||||||
|
} else if (noCopy) {
|
||||||
|
return Buffer.from(
|
||||||
|
this.outputBuffer,
|
||||||
|
0,
|
||||||
|
this.outputBuffer.length - tailBuffer.length,
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
const res = Buffer.alloc(this.outputBuffer.length - tailBuffer.length);
|
||||||
|
this.outputBuffer.copy(res, 0, 0, res.length);
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
#decode = (buffer: Buffer): any => {
|
||||||
|
const t = buffer[0];
|
||||||
|
buffer = Buffer.from(buffer, 1);
|
||||||
|
|
||||||
|
switch (t) {
|
||||||
|
case Types.SMALL_ATOM:
|
||||||
|
return this.decode_atom(buffer, 1);
|
||||||
|
case Types.ATOM:
|
||||||
|
return this.decode_atom(buffer, 2);
|
||||||
|
case Types.BINARY:
|
||||||
|
return this.decode_binary(buffer);
|
||||||
|
case Types.SMALL_INTEGER:
|
||||||
|
return this.decode_integer(buffer, 1, true);
|
||||||
|
case Types.INTEGER:
|
||||||
|
return this.decode_integer(buffer, 4);
|
||||||
|
case Types.SMALL_BIG:
|
||||||
|
return this.decode_big(buffer, 1);
|
||||||
|
case Types.LARGE_BIG:
|
||||||
|
return this.decode_big(buffer, 4);
|
||||||
|
case Types.FLOAT:
|
||||||
|
return this.decode_float(buffer);
|
||||||
|
case Types.NEW_FLOAT:
|
||||||
|
return this.decode_new_float(buffer);
|
||||||
|
case Types.STRING:
|
||||||
|
return this.decode_string(buffer);
|
||||||
|
case Types.LIST:
|
||||||
|
return this.decode_list(buffer);
|
||||||
|
case Types.SMALL_TUPLE:
|
||||||
|
return this.decode_tuple(buffer, 1);
|
||||||
|
// case Types.LARGE_TUPLE:
|
||||||
|
// return this.decode_large_tuple(buffer, 4)
|
||||||
|
case Types.NIL:
|
||||||
|
return this.decode_nil(buffer);
|
||||||
|
case Types.MAP:
|
||||||
|
return this.decode_map(buffer);
|
||||||
|
default:
|
||||||
|
throw new Error(`Unexpected BERT type: ${t}`);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
decode = (buffer: Buffer) => {
|
||||||
|
if (buffer[0] !== Types.BERT_START) {
|
||||||
|
throw new Error("Not a valid BERT");
|
||||||
|
}
|
||||||
|
|
||||||
|
const obj = this.#decode(Buffer.from(buffer, 1));
|
||||||
|
|
||||||
|
if (obj.rest.length !== 0) {
|
||||||
|
throw new Error("Invalid BERT");
|
||||||
|
}
|
||||||
|
|
||||||
|
return obj.value;
|
||||||
|
};
|
||||||
|
|
||||||
|
encode_string = (obj: string, buffer: Buffer) => {
|
||||||
|
if (this.convention === Lang.ELIXIR) {
|
||||||
|
return this.encode_binary(Buffer.from(obj), buffer);
|
||||||
|
} else {
|
||||||
|
buffer[0] = Types.STRING;
|
||||||
|
buffer.writeUInt16BE(obj.length, 1);
|
||||||
|
const len = buffer.write(obj, 3);
|
||||||
|
return Buffer.from(buffer, 3 + len);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
encode_boolean = (obj: boolean, buffer: Buffer) => {
|
||||||
|
if (obj) {
|
||||||
|
return this.#encode(this.toAtom("true"), buffer);
|
||||||
|
} else {
|
||||||
|
return this.#encode(this.toAtom("false"), buffer);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
encode_number = (obj: number, buffer: Buffer) => {
|
||||||
|
const isInteger = obj % 1 === 0;
|
||||||
|
|
||||||
|
// Handle floats...
|
||||||
|
if (!isInteger) {
|
||||||
|
return this.encode_float(obj, buffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Small int...
|
||||||
|
if (isInteger && obj >= 0 && obj < 256) {
|
||||||
|
buffer[0] = Types.SMALL_INTEGER;
|
||||||
|
buffer.writeUInt8(obj, 1);
|
||||||
|
return Buffer.from(buffer, 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 4 byte int...
|
||||||
|
if (isInteger && obj >= -134217728 && obj <= 134217727) {
|
||||||
|
buffer[0] = Types.INTEGER;
|
||||||
|
buffer.writeInt32BE(obj, 1);
|
||||||
|
return Buffer.from(buffer, 5);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Bignum...
|
||||||
|
const numBuffer = Buffer.alloc(buffer.length);
|
||||||
|
if (obj < 0) {
|
||||||
|
obj *= -1;
|
||||||
|
numBuffer[0] = 1;
|
||||||
|
} else {
|
||||||
|
numBuffer[0] = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
let offset = 1;
|
||||||
|
while (obj !== 0) {
|
||||||
|
numBuffer[offset] = obj % 256;
|
||||||
|
obj = Math.floor(obj / 256);
|
||||||
|
offset++;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (offset < 256) {
|
||||||
|
buffer[0] = Types.SMALL_BIG;
|
||||||
|
buffer.writeUInt8(offset - 1, 1);
|
||||||
|
numBuffer.copy(buffer, 2, 0, offset);
|
||||||
|
return Buffer.from(buffer, 2 + offset);
|
||||||
|
} else {
|
||||||
|
buffer[0] = Types.LARGE_BIG;
|
||||||
|
buffer.writeUInt32BE(offset - 1, 1);
|
||||||
|
numBuffer.copy(buffer, 5, 0, offset);
|
||||||
|
return Buffer.from(buffer, 5 + offset);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
encode_float = (obj: number, buffer: Buffer) => {
|
||||||
|
// float...
|
||||||
|
buffer[0] = Types.NEW_FLOAT;
|
||||||
|
buffer.writeDoubleBE(obj, 1);
|
||||||
|
return Buffer.from(buffer, 9);
|
||||||
|
};
|
||||||
|
|
||||||
|
encode_object = (obj: any, buffer: Buffer) => {
|
||||||
|
// Check if it's an atom, binary, or tuple...
|
||||||
|
if (obj === null) {
|
||||||
|
const undefinedAtom =
|
||||||
|
this.convention === Lang.ELIXIR ? "nil" : "undefined";
|
||||||
|
return this.#encode(this.toAtom(undefinedAtom), buffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Buffer.isBuffer(obj)) {
|
||||||
|
return this.encode_binary(obj, buffer);
|
||||||
|
}
|
||||||
|
if (Array.isArray(obj)) {
|
||||||
|
return this.encode_array(obj, buffer);
|
||||||
|
}
|
||||||
|
if (obj.type === "Atom") {
|
||||||
|
return this.encode_atom(obj, buffer);
|
||||||
|
}
|
||||||
|
if (obj.type === "Tuple") {
|
||||||
|
return this.encode_tuple(obj, buffer);
|
||||||
|
}
|
||||||
|
// Treat the object as an associative array...
|
||||||
|
return this.encode_map(obj, buffer);
|
||||||
|
};
|
||||||
|
|
||||||
|
encode_atom = (obj: any, buffer: Buffer) => {
|
||||||
|
buffer[0] = Types.ATOM;
|
||||||
|
buffer.writeUInt16BE(obj.value.length, 1);
|
||||||
|
const len = buffer.write(obj.value, 3);
|
||||||
|
return Buffer.from(buffer, 3 + len);
|
||||||
|
};
|
||||||
|
|
||||||
|
encode_binary = (obj: Buffer, buffer: Buffer) => {
|
||||||
|
buffer[0] = Types.BINARY;
|
||||||
|
buffer.writeUInt32BE(obj.length, 1);
|
||||||
|
obj.copy(buffer, 5);
|
||||||
|
return Buffer.from(buffer, 5 + obj.length);
|
||||||
|
};
|
||||||
|
|
||||||
|
encode_undefined = (_obj: Buffer, buffer: Buffer) => {
|
||||||
|
return this.#encode(null, buffer);
|
||||||
|
};
|
||||||
|
|
||||||
|
encode_tuple = (obj: Buffer, buffer: Buffer) => {
|
||||||
|
if (obj.length < 256) {
|
||||||
|
buffer[0] = Types.SMALL_TUPLE;
|
||||||
|
buffer.writeUInt8(obj.length, 1);
|
||||||
|
buffer = Buffer.from(buffer, 2);
|
||||||
|
} else {
|
||||||
|
buffer[0] = Types.LARGE_TUPLE;
|
||||||
|
buffer.writeUInt32BE(obj.length, 1);
|
||||||
|
buffer = buffer.slice(5);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (let i = 0; i < obj.length; ++i) {
|
||||||
|
buffer = this.#encode(obj[i], buffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
return buffer;
|
||||||
|
};
|
||||||
|
|
||||||
|
encode_array = (obj: any[], buffer: Buffer) => {
|
||||||
|
if (obj.length === 0) {
|
||||||
|
buffer[0] = Types.NIL;
|
||||||
|
return Buffer.from(buffer, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
buffer[0] = Types.LIST;
|
||||||
|
buffer.writeUInt32BE(obj.length, 1);
|
||||||
|
buffer = Buffer.from(buffer, 5);
|
||||||
|
|
||||||
|
for (let i = 0; i < obj.length; ++i) {
|
||||||
|
buffer = this.#encode(obj[i], buffer);
|
||||||
|
}
|
||||||
|
buffer[0] = Types.NIL;
|
||||||
|
return Buffer.from(buffer, 1);
|
||||||
|
};
|
||||||
|
|
||||||
|
encode_map = (obj: Record<string, any>, buffer: Buffer) => {
|
||||||
|
const keys = Object.keys(obj);
|
||||||
|
buffer[0] = Types.MAP;
|
||||||
|
buffer.writeUInt32BE(keys.length, 1);
|
||||||
|
buffer = Buffer.from(buffer, 5);
|
||||||
|
|
||||||
|
for (let i = 0; i < keys.length; ++i) {
|
||||||
|
const key = this.mapKeyAsAtom ? this.toAtom(keys[i]) : keys[i];
|
||||||
|
buffer = this.#encode(key, buffer);
|
||||||
|
buffer = this.#encode(obj[keys[i]], buffer);
|
||||||
|
}
|
||||||
|
return buffer;
|
||||||
|
};
|
||||||
|
|
||||||
|
decode_atom = (buffer: Buffer, count: 1 | 2 | 4) => {
|
||||||
|
const size = this.bytesToInt(buffer, count, true);
|
||||||
|
buffer = Buffer.from(buffer, count);
|
||||||
|
let value: any = buffer.toString("utf8", 0, size);
|
||||||
|
if (value === "true") {
|
||||||
|
value = true;
|
||||||
|
} else if (value === "false") {
|
||||||
|
value = false;
|
||||||
|
} else if (
|
||||||
|
this.decodeUndefinedValues &&
|
||||||
|
this.convention === Lang.ELIXIR &&
|
||||||
|
value === "nil"
|
||||||
|
) {
|
||||||
|
value = null;
|
||||||
|
} else if (
|
||||||
|
this.decodeUndefinedValues &&
|
||||||
|
this.convention === Lang.ERLANG &&
|
||||||
|
value === "undefined"
|
||||||
|
) {
|
||||||
|
value = null;
|
||||||
|
} else {
|
||||||
|
value = this.toAtom(value);
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
value,
|
||||||
|
rest: Buffer.from(buffer, size),
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
decode_binary = (buffer: Buffer) => {
|
||||||
|
const size = this.bytesToInt(buffer, 4, true);
|
||||||
|
buffer = Buffer.from(buffer, 4);
|
||||||
|
const bin = Buffer.alloc(size);
|
||||||
|
buffer.copy(bin, 0, 0, size);
|
||||||
|
return {
|
||||||
|
value:
|
||||||
|
this.convention === Lang.ELIXIR && this.allBinariesAsString
|
||||||
|
? bin.toString()
|
||||||
|
: bin,
|
||||||
|
rest: Buffer.from(buffer, size),
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
decode_integer = (buffer: Buffer, count: 1 | 2 | 4, unsigned = false) => {
|
||||||
|
return {
|
||||||
|
value: this.bytesToInt(buffer, count, unsigned),
|
||||||
|
rest: Buffer.from(buffer, count),
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
decode_big = (buffer: Buffer, count: 1 | 2 | 4) => {
|
||||||
|
const size = this.bytesToInt(buffer, count, false);
|
||||||
|
buffer = Buffer.from(buffer, count);
|
||||||
|
|
||||||
|
let num = 0;
|
||||||
|
const isNegative = buffer[0] === 1;
|
||||||
|
|
||||||
|
buffer = Buffer.from(buffer, 1);
|
||||||
|
|
||||||
|
for (let i = size - 1; i >= 0; --i) {
|
||||||
|
const n = buffer[i];
|
||||||
|
if (num === 0) {
|
||||||
|
num = n;
|
||||||
|
} else {
|
||||||
|
num = num * 256 + n;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (isNegative) {
|
||||||
|
num = num * -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
value: num,
|
||||||
|
rest: Buffer.from(buffer, size),
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
decode_float = (buffer: Buffer) => {
|
||||||
|
const size = 31;
|
||||||
|
|
||||||
|
return {
|
||||||
|
value: parseFloat(buffer.toString("utf8", 0, size)),
|
||||||
|
rest: Buffer.from(buffer, size),
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
decode_new_float = (buffer: Buffer) => {
|
||||||
|
return {
|
||||||
|
value: buffer.readDoubleBE(0),
|
||||||
|
rest: Buffer.from(buffer, 8),
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
decode_string = (buffer: Buffer) => {
|
||||||
|
const size = this.bytesToInt(buffer, 2, true);
|
||||||
|
|
||||||
|
buffer = Buffer.from(buffer, 2);
|
||||||
|
|
||||||
|
return {
|
||||||
|
value: buffer.toString("utf8", 0, size),
|
||||||
|
rest: Buffer.from(buffer, size),
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
decode_list = (buffer: Buffer) => {
|
||||||
|
const arr = [];
|
||||||
|
const size = this.bytesToInt(buffer, 4, true);
|
||||||
|
buffer = Buffer.from(buffer, 4);
|
||||||
|
|
||||||
|
for (let i = 0; i < size; ++i) {
|
||||||
|
const el = this.#decode(buffer);
|
||||||
|
arr.push(el.value);
|
||||||
|
buffer = el.rest;
|
||||||
|
}
|
||||||
|
|
||||||
|
const lastChar = buffer[0];
|
||||||
|
|
||||||
|
if (lastChar !== Types.NIL) {
|
||||||
|
throw new Error("List does not end with NIL");
|
||||||
|
}
|
||||||
|
|
||||||
|
buffer = Buffer.from(buffer, 1);
|
||||||
|
|
||||||
|
return {
|
||||||
|
value: arr,
|
||||||
|
rest: buffer,
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
decode_map = (buffer: Buffer) => {
|
||||||
|
const map: Record<string, any> = {};
|
||||||
|
const size = this.bytesToInt(buffer, 4, true);
|
||||||
|
|
||||||
|
buffer = Buffer.from(buffer, 4);
|
||||||
|
|
||||||
|
for (let i = 0; i < size; ++i) {
|
||||||
|
let el = this.#decode(buffer);
|
||||||
|
const key = el.value;
|
||||||
|
el = this.#decode(el.rest);
|
||||||
|
const value = el.value;
|
||||||
|
map[key] = value;
|
||||||
|
buffer = el.rest;
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
value: map,
|
||||||
|
rest: buffer,
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
decode_tuple = (buffer: Buffer, count: 1 | 2 | 4) => {
|
||||||
|
const arr = [];
|
||||||
|
const size = this.bytesToInt(buffer, count, true);
|
||||||
|
buffer = Buffer.from(buffer, count);
|
||||||
|
for (let i = 0; i < size; ++i) {
|
||||||
|
const el = this.#decode(buffer);
|
||||||
|
arr.push(el.value);
|
||||||
|
buffer = el.rest;
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
value: this.toTuple(arr),
|
||||||
|
rest: buffer,
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
decode_nil = (buffer: Buffer) => {
|
||||||
|
// nil is an empty list
|
||||||
|
return {
|
||||||
|
value: [],
|
||||||
|
rest: buffer,
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
bytesToInt = (buffer: Buffer, length: 1 | 2 | 4, unsigned: boolean) => {
|
||||||
|
switch (length) {
|
||||||
|
case 1:
|
||||||
|
return unsigned ? buffer.readUInt8(0) : buffer.readInt8(0);
|
||||||
|
case 2:
|
||||||
|
return unsigned ? buffer.readUInt16BE(0) : buffer.readInt16BE(0);
|
||||||
|
case 4:
|
||||||
|
return unsigned ? buffer.readUInt32BE(0) : buffer.readInt32BE(0);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
binary_to_list = (str: string) => {
|
||||||
|
const ret = [];
|
||||||
|
for (let i = 0; i < str.length; ++i) ret.push(str[i]);
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert object to atom.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
export const toAtom = (str: string) => ({
|
||||||
|
type: "Atom",
|
||||||
|
value: str,
|
||||||
|
toString: () => str,
|
||||||
|
});
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert array of items to tuple.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
export const toTuple = (arr: any[]) => ({
|
||||||
|
...arr,
|
||||||
|
type: "Tuple",
|
||||||
|
length: arr.length,
|
||||||
|
value: arr,
|
||||||
|
toString: () =>
|
||||||
|
"{" +
|
||||||
|
arr
|
||||||
|
.filter((i) => i !== "")
|
||||||
|
.map((i) => i.toString())
|
||||||
|
.join(", ") +
|
||||||
|
"}",
|
||||||
|
});
|
|
@ -0,0 +1,55 @@
|
||||||
|
import { Bert } from "./bert.js";
|
||||||
|
import { Duplex } from "node:stream";
|
||||||
|
|
||||||
|
const bert = new Bert();
|
||||||
|
|
||||||
|
let termLen: number | undefined;
|
||||||
|
let termBin: Buffer;
|
||||||
|
|
||||||
|
const fakeWrite = () => undefined;
|
||||||
|
process.stdout.write = fakeWrite as any;
|
||||||
|
|
||||||
|
class Port extends Duplex {
|
||||||
|
constructor() {
|
||||||
|
super({ objectMode: true });
|
||||||
|
}
|
||||||
|
|
||||||
|
_read() {
|
||||||
|
{
|
||||||
|
let term;
|
||||||
|
if (termLen === undefined && null !== (termBin = process.stdin.read(4))) {
|
||||||
|
termLen = bert.bytesToInt(termBin, 4, true);
|
||||||
|
}
|
||||||
|
if (
|
||||||
|
termLen !== undefined &&
|
||||||
|
null !== (term = process.stdin.read(termLen))
|
||||||
|
) {
|
||||||
|
termLen = undefined;
|
||||||
|
this.push(bert.decode(term));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
_write(obj: Buffer, _encoding: any, callback: Function) {
|
||||||
|
let term: Buffer;
|
||||||
|
try {
|
||||||
|
term = bert.encode(obj, true);
|
||||||
|
} catch (error) {
|
||||||
|
console.error(error);
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
const len = Buffer.alloc(4);
|
||||||
|
len.writeUInt32BE(term.length, 0);
|
||||||
|
process.stdout.write = this._write as any;
|
||||||
|
process.stdout.write(len);
|
||||||
|
process.stdout.write(term, callback as any);
|
||||||
|
process.stdout.write = fakeWrite as any;
|
||||||
|
}
|
||||||
|
|
||||||
|
// When all the data is done passing, it stops.
|
||||||
|
_final() {
|
||||||
|
this.push(null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export const port = new Port();
|
|
@ -0,0 +1,109 @@
|
||||||
|
{
|
||||||
|
"compilerOptions": {
|
||||||
|
/* Visit https://aka.ms/tsconfig to read more about this file */
|
||||||
|
|
||||||
|
/* Projects */
|
||||||
|
// "incremental": true, /* Save .tsbuildinfo files to allow for incremental compilation of projects. */
|
||||||
|
// "composite": true, /* Enable constraints that allow a TypeScript project to be used with project references. */
|
||||||
|
// "tsBuildInfoFile": "./.tsbuildinfo", /* Specify the path to .tsbuildinfo incremental compilation file. */
|
||||||
|
// "disableSourceOfProjectReferenceRedirect": true, /* Disable preferring source files instead of declaration files when referencing composite projects. */
|
||||||
|
// "disableSolutionSearching": true, /* Opt a project out of multi-project reference checking when editing. */
|
||||||
|
// "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */
|
||||||
|
|
||||||
|
/* Language and Environment */
|
||||||
|
"target": "ESNext", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
|
||||||
|
// "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */
|
||||||
|
// "jsx": "preserve", /* Specify what JSX code is generated. */
|
||||||
|
// "experimentalDecorators": true, /* Enable experimental support for legacy experimental decorators. */
|
||||||
|
// "emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */
|
||||||
|
// "jsxFactory": "", /* Specify the JSX factory function used when targeting React JSX emit, e.g. 'React.createElement' or 'h'. */
|
||||||
|
// "jsxFragmentFactory": "", /* Specify the JSX Fragment reference used for fragments when targeting React JSX emit e.g. 'React.Fragment' or 'Fragment'. */
|
||||||
|
// "jsxImportSource": "", /* Specify module specifier used to import the JSX factory functions when using 'jsx: react-jsx*'. */
|
||||||
|
// "reactNamespace": "", /* Specify the object invoked for 'createElement'. This only applies when targeting 'react' JSX emit. */
|
||||||
|
// "noLib": true, /* Disable including any library files, including the default lib.d.ts. */
|
||||||
|
// "useDefineForClassFields": true, /* Emit ECMAScript-standard-compliant class fields. */
|
||||||
|
// "moduleDetection": "auto", /* Control what method is used to detect module-format JS files. */
|
||||||
|
|
||||||
|
/* Modules */
|
||||||
|
"module": "ESNext", /* Specify what module code is generated. */
|
||||||
|
"rootDir": "./src", /* Specify the root folder within your source files. */
|
||||||
|
"moduleResolution": "node", /* Specify how TypeScript looks up a file from a given module specifier. */
|
||||||
|
// "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */
|
||||||
|
// "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */
|
||||||
|
// "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */
|
||||||
|
// "typeRoots": [], /* Specify multiple folders that act like './node_modules/@types'. */
|
||||||
|
// "types": [], /* Specify type package names to be included without being referenced in a source file. */
|
||||||
|
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
|
||||||
|
// "moduleSuffixes": [], /* List of file name suffixes to search when resolving a module. */
|
||||||
|
// "allowImportingTsExtensions": true, /* Allow imports to include TypeScript file extensions. Requires '--moduleResolution bundler' and either '--noEmit' or '--emitDeclarationOnly' to be set. */
|
||||||
|
// "resolvePackageJsonExports": true, /* Use the package.json 'exports' field when resolving package imports. */
|
||||||
|
// "resolvePackageJsonImports": true, /* Use the package.json 'imports' field when resolving imports. */
|
||||||
|
// "customConditions": [], /* Conditions to set in addition to the resolver-specific defaults when resolving imports. */
|
||||||
|
// "resolveJsonModule": true, /* Enable importing .json files. */
|
||||||
|
// "allowArbitraryExtensions": true, /* Enable importing files with any extension, provided a declaration file is present. */
|
||||||
|
// "noResolve": true, /* Disallow 'import's, 'require's or '<reference>'s from expanding the number of files TypeScript should add to a project. */
|
||||||
|
|
||||||
|
/* JavaScript Support */
|
||||||
|
// "allowJs": true, /* Allow JavaScript files to be a part of your program. Use the 'checkJS' option to get errors from these files. */
|
||||||
|
// "checkJs": true, /* Enable error reporting in type-checked JavaScript files. */
|
||||||
|
// "maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from 'node_modules'. Only applicable with 'allowJs'. */
|
||||||
|
|
||||||
|
/* Emit */
|
||||||
|
"declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */
|
||||||
|
// "declarationMap": true, /* Create sourcemaps for d.ts files. */
|
||||||
|
// "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */
|
||||||
|
// "sourceMap": true, /* Create source map files for emitted JavaScript files. */
|
||||||
|
// "inlineSourceMap": true, /* Include sourcemap files inside the emitted JavaScript. */
|
||||||
|
// "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If 'declaration' is true, also designates a file that bundles all .d.ts output. */
|
||||||
|
"outDir": "./dist", /* Specify an output folder for all emitted files. */
|
||||||
|
// "removeComments": true, /* Disable emitting comments. */
|
||||||
|
// "noEmit": true, /* Disable emitting files from a compilation. */
|
||||||
|
// "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */
|
||||||
|
// "importsNotUsedAsValues": "remove", /* Specify emit/checking behavior for imports that are only used for types. */
|
||||||
|
// "downlevelIteration": true, /* Emit more compliant, but verbose and less performant JavaScript for iteration. */
|
||||||
|
// "sourceRoot": "", /* Specify the root path for debuggers to find the reference source code. */
|
||||||
|
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
|
||||||
|
// "inlineSources": true, /* Include source code in the sourcemaps inside the emitted JavaScript. */
|
||||||
|
// "emitBOM": true, /* Emit a UTF-8 Byte Order Mark (BOM) in the beginning of output files. */
|
||||||
|
// "newLine": "crlf", /* Set the newline character for emitting files. */
|
||||||
|
// "stripInternal": true, /* Disable emitting declarations that have '@internal' in their JSDoc comments. */
|
||||||
|
// "noEmitHelpers": true, /* Disable generating custom helper functions like '__extends' in compiled output. */
|
||||||
|
// "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */
|
||||||
|
// "preserveConstEnums": true, /* Disable erasing 'const enum' declarations in generated code. */
|
||||||
|
// "declarationDir": "./", /* Specify the output directory for generated declaration files. */
|
||||||
|
// "preserveValueImports": true, /* Preserve unused imported values in the JavaScript output that would otherwise be removed. */
|
||||||
|
|
||||||
|
/* Interop Constraints */
|
||||||
|
// "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */
|
||||||
|
// "verbatimModuleSyntax": true, /* Do not transform or elide any imports or exports not marked as type-only, ensuring they are written in the output file's format based on the 'module' setting. */
|
||||||
|
// "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */
|
||||||
|
"esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */
|
||||||
|
// "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */
|
||||||
|
"forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */
|
||||||
|
|
||||||
|
/* Type Checking */
|
||||||
|
"strict": true, /* Enable all strict type-checking options. */
|
||||||
|
// "noImplicitAny": true, /* Enable error reporting for expressions and declarations with an implied 'any' type. */
|
||||||
|
// "strictNullChecks": true, /* When type checking, take into account 'null' and 'undefined'. */
|
||||||
|
// "strictFunctionTypes": true, /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */
|
||||||
|
// "strictBindCallApply": true, /* Check that the arguments for 'bind', 'call', and 'apply' methods match the original function. */
|
||||||
|
// "strictPropertyInitialization": true, /* Check for class properties that are declared but not set in the constructor. */
|
||||||
|
// "noImplicitThis": true, /* Enable error reporting when 'this' is given the type 'any'. */
|
||||||
|
// "useUnknownInCatchVariables": true, /* Default catch clause variables as 'unknown' instead of 'any'. */
|
||||||
|
// "alwaysStrict": true, /* Ensure 'use strict' is always emitted. */
|
||||||
|
// "noUnusedLocals": true, /* Enable error reporting when local variables aren't read. */
|
||||||
|
// "noUnusedParameters": true, /* Raise an error when a function parameter isn't read. */
|
||||||
|
// "exactOptionalPropertyTypes": true, /* Interpret optional property types as written, rather than adding 'undefined'. */
|
||||||
|
// "noImplicitReturns": true, /* Enable error reporting for codepaths that do not explicitly return in a function. */
|
||||||
|
// "noFallthroughCasesInSwitch": true, /* Enable error reporting for fallthrough cases in switch statements. */
|
||||||
|
// "noUncheckedIndexedAccess": true, /* Add 'undefined' to a type when accessed using an index. */
|
||||||
|
// "noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an override modifier. */
|
||||||
|
// "noPropertyAccessFromIndexSignature": true, /* Enforces using indexed accessors for keys declared using an indexed type. */
|
||||||
|
// "allowUnusedLabels": true, /* Disable error reporting for unused labels. */
|
||||||
|
// "allowUnreachableCode": true, /* Disable error reporting for unreachable code. */
|
||||||
|
|
||||||
|
/* Completeness */
|
||||||
|
// "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */
|
||||||
|
"skipLibCheck": true /* Skip type checking all .d.ts files. */
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue