many fixes

This commit is contained in:
Moon Man 2024-01-28 07:48:59 -05:00
parent b704065ad5
commit 8dab4d57fa
3 changed files with 31 additions and 30 deletions

28
dist/bert.js vendored
View File

@ -108,7 +108,7 @@ export class Bert {
buffer[0] = Types.STRING; buffer[0] = Types.STRING;
buffer.writeUInt16BE(obj.length, 1); buffer.writeUInt16BE(obj.length, 1);
const len = buffer.write(obj, 3); const len = buffer.write(obj, 3);
return Buffer.from(buffer, 3 + len); return Buffer.from(buffer, 0, 3 + len);
} }
}; };
encode_boolean = (obj, buffer) => { encode_boolean = (obj, buffer) => {
@ -129,13 +129,13 @@ export class Bert {
if (isInteger && obj >= 0 && obj < 256) { if (isInteger && obj >= 0 && obj < 256) {
buffer[0] = Types.SMALL_INTEGER; buffer[0] = Types.SMALL_INTEGER;
buffer.writeUInt8(obj, 1); buffer.writeUInt8(obj, 1);
return Buffer.from(buffer, 2); return Buffer.from(buffer, 0, 2);
} }
// 4 byte int... // 4 byte int...
if (isInteger && obj >= -134217728 && obj <= 134217727) { if (isInteger && obj >= -134217728 && obj <= 134217727) {
buffer[0] = Types.INTEGER; buffer[0] = Types.INTEGER;
buffer.writeInt32BE(obj, 1); buffer.writeInt32BE(obj, 1);
return Buffer.from(buffer, 5); return Buffer.from(buffer, 0, 5);
} }
// Bignum... // Bignum...
const numBuffer = Buffer.alloc(buffer.length); const numBuffer = Buffer.alloc(buffer.length);
@ -156,20 +156,20 @@ export class Bert {
buffer[0] = Types.SMALL_BIG; buffer[0] = Types.SMALL_BIG;
buffer.writeUInt8(offset - 1, 1); buffer.writeUInt8(offset - 1, 1);
numBuffer.copy(buffer, 2, 0, offset); numBuffer.copy(buffer, 2, 0, offset);
return Buffer.from(buffer, 2 + offset); return Buffer.from(buffer, 0, 2 + offset);
} }
else { else {
buffer[0] = Types.LARGE_BIG; buffer[0] = Types.LARGE_BIG;
buffer.writeUInt32BE(offset - 1, 1); buffer.writeUInt32BE(offset - 1, 1);
numBuffer.copy(buffer, 5, 0, offset); numBuffer.copy(buffer, 5, 0, offset);
return Buffer.from(buffer, 5 + offset); return Buffer.from(buffer, 0, 5 + offset);
} }
}; };
encode_float = (obj, buffer) => { encode_float = (obj, buffer) => {
// float... // float...
buffer[0] = Types.NEW_FLOAT; buffer[0] = Types.NEW_FLOAT;
buffer.writeDoubleBE(obj, 1); buffer.writeDoubleBE(obj, 1);
return Buffer.from(buffer, 9); return Buffer.from(buffer, 0, 9);
}; };
encode_object = (obj, buffer) => { encode_object = (obj, buffer) => {
// Check if it's an atom, binary, or tuple... // Check if it's an atom, binary, or tuple...
@ -196,13 +196,13 @@ export class Bert {
buffer[0] = Types.ATOM; buffer[0] = Types.ATOM;
buffer.writeUInt16BE(obj.value.length, 1); buffer.writeUInt16BE(obj.value.length, 1);
const len = buffer.write(obj.value, 3); const len = buffer.write(obj.value, 3);
return Buffer.from(buffer, 3 + len); return Buffer.from(buffer, 0, 3 + len);
}; };
encode_binary = (obj, buffer) => { encode_binary = (obj, buffer) => {
buffer[0] = Types.BINARY; buffer[0] = Types.BINARY;
buffer.writeUInt32BE(obj.length, 1); buffer.writeUInt32BE(obj.length, 1);
obj.copy(buffer, 5); obj.copy(buffer, 5);
return Buffer.from(buffer, 5 + obj.length); return Buffer.from(buffer, 0, 5 + obj.length);
}; };
encode_undefined = (_obj, buffer) => { encode_undefined = (_obj, buffer) => {
return this.#encode(null, buffer); return this.#encode(null, buffer);
@ -211,12 +211,12 @@ export class Bert {
if (obj.length < 256) { if (obj.length < 256) {
buffer[0] = Types.SMALL_TUPLE; buffer[0] = Types.SMALL_TUPLE;
buffer.writeUInt8(obj.length, 1); buffer.writeUInt8(obj.length, 1);
buffer = Buffer.from(buffer, 2); buffer = Buffer.from(buffer, 0, 2);
} }
else { else {
buffer[0] = Types.LARGE_TUPLE; buffer[0] = Types.LARGE_TUPLE;
buffer.writeUInt32BE(obj.length, 1); buffer.writeUInt32BE(obj.length, 1);
buffer = buffer.slice(5); buffer = Buffer.from(buffer, 0, 5);
} }
for (let i = 0; i < obj.length; ++i) { for (let i = 0; i < obj.length; ++i) {
buffer = this.#encode(obj[i], buffer); buffer = this.#encode(obj[i], buffer);
@ -226,22 +226,22 @@ export class Bert {
encode_array = (obj, buffer) => { encode_array = (obj, buffer) => {
if (obj.length === 0) { if (obj.length === 0) {
buffer[0] = Types.NIL; buffer[0] = Types.NIL;
return Buffer.from(buffer, 1); return Buffer.from(buffer, 0, 1);
} }
buffer[0] = Types.LIST; buffer[0] = Types.LIST;
buffer.writeUInt32BE(obj.length, 1); buffer.writeUInt32BE(obj.length, 1);
buffer = Buffer.from(buffer, 5); buffer = Buffer.from(buffer, 0, 5);
for (let i = 0; i < obj.length; ++i) { for (let i = 0; i < obj.length; ++i) {
buffer = this.#encode(obj[i], buffer); buffer = this.#encode(obj[i], buffer);
} }
buffer[0] = Types.NIL; buffer[0] = Types.NIL;
return Buffer.from(buffer, 1); return Buffer.from(buffer, 0, 1);
}; };
encode_map = (obj, buffer) => { encode_map = (obj, buffer) => {
const keys = Object.keys(obj); const keys = Object.keys(obj);
buffer[0] = Types.MAP; buffer[0] = Types.MAP;
buffer.writeUInt32BE(keys.length, 1); buffer.writeUInt32BE(keys.length, 1);
buffer = Buffer.from(buffer, 5); buffer = Buffer.from(buffer, 0, 5);
for (let i = 0; i < keys.length; ++i) { for (let i = 0; i < keys.length; ++i) {
const key = this.mapKeyAsAtom ? this.toAtom(keys[i]) : keys[i]; const key = this.mapKeyAsAtom ? this.toAtom(keys[i]) : keys[i];
buffer = this.#encode(key, buffer); buffer = this.#encode(key, buffer);

View File

@ -1,6 +1,6 @@
{ {
"name": "noderl", "name": "noderl",
"version": "1.0.0", "version": "0.0.1",
"type": "module", "type": "module",
"description": "Erlang-node.js bridge", "description": "Erlang-node.js bridge",
"main": "./dist/main.js", "main": "./dist/main.js",

View File

@ -124,11 +124,12 @@ export class Bert {
encode_string = (obj: string, buffer: Buffer) => { encode_string = (obj: string, buffer: Buffer) => {
if (this.convention === Lang.ELIXIR) { if (this.convention === Lang.ELIXIR) {
return this.encode_binary(Buffer.from(obj), buffer); return this.encode_binary(Buffer.from(obj), buffer);
} else { }
else {
buffer[0] = Types.STRING; buffer[0] = Types.STRING;
buffer.writeUInt16BE(obj.length, 1); buffer.writeUInt16BE(obj.length, 1);
const len = buffer.write(obj, 3); const len = buffer.write(obj, 3);
return Buffer.from(buffer, 3 + len); return Buffer.from(buffer, 0, 3 + len);
} }
}; };
@ -152,14 +153,14 @@ export class Bert {
if (isInteger && obj >= 0 && obj < 256) { if (isInteger && obj >= 0 && obj < 256) {
buffer[0] = Types.SMALL_INTEGER; buffer[0] = Types.SMALL_INTEGER;
buffer.writeUInt8(obj, 1); buffer.writeUInt8(obj, 1);
return Buffer.from(buffer, 2); return Buffer.from(buffer, 0, 2);
} }
// 4 byte int... // 4 byte int...
if (isInteger && obj >= -134217728 && obj <= 134217727) { if (isInteger && obj >= -134217728 && obj <= 134217727) {
buffer[0] = Types.INTEGER; buffer[0] = Types.INTEGER;
buffer.writeInt32BE(obj, 1); buffer.writeInt32BE(obj, 1);
return Buffer.from(buffer, 5); return Buffer.from(buffer, 0, 5);
} }
// Bignum... // Bignum...
@ -182,12 +183,12 @@ export class Bert {
buffer[0] = Types.SMALL_BIG; buffer[0] = Types.SMALL_BIG;
buffer.writeUInt8(offset - 1, 1); buffer.writeUInt8(offset - 1, 1);
numBuffer.copy(buffer, 2, 0, offset); numBuffer.copy(buffer, 2, 0, offset);
return Buffer.from(buffer, 2 + offset); return Buffer.from(buffer, 0, 2 + offset);
} else { } else {
buffer[0] = Types.LARGE_BIG; buffer[0] = Types.LARGE_BIG;
buffer.writeUInt32BE(offset - 1, 1); buffer.writeUInt32BE(offset - 1, 1);
numBuffer.copy(buffer, 5, 0, offset); numBuffer.copy(buffer, 5, 0, offset);
return Buffer.from(buffer, 5 + offset); return Buffer.from(buffer, 0, 5 + offset);
} }
}; };
@ -195,7 +196,7 @@ export class Bert {
// float... // float...
buffer[0] = Types.NEW_FLOAT; buffer[0] = Types.NEW_FLOAT;
buffer.writeDoubleBE(obj, 1); buffer.writeDoubleBE(obj, 1);
return Buffer.from(buffer, 9); return Buffer.from(buffer, 0, 9);
}; };
encode_object = (obj: any, buffer: Buffer) => { encode_object = (obj: any, buffer: Buffer) => {
@ -226,14 +227,14 @@ export class Bert {
buffer[0] = Types.ATOM; buffer[0] = Types.ATOM;
buffer.writeUInt16BE(obj.value.length, 1); buffer.writeUInt16BE(obj.value.length, 1);
const len = buffer.write(obj.value, 3); const len = buffer.write(obj.value, 3);
return Buffer.from(buffer, 3 + len); return Buffer.from(buffer, 0, 3 + len);
}; };
encode_binary = (obj: Buffer, buffer: Buffer) => { encode_binary = (obj: Buffer, buffer: Buffer) => {
buffer[0] = Types.BINARY; buffer[0] = Types.BINARY;
buffer.writeUInt32BE(obj.length, 1); buffer.writeUInt32BE(obj.length, 1);
obj.copy(buffer, 5); obj.copy(buffer, 5);
return Buffer.from(buffer, 5 + obj.length); return Buffer.from(buffer, 0, 5 + obj.length);
}; };
encode_undefined = (_obj: Buffer, buffer: Buffer) => { encode_undefined = (_obj: Buffer, buffer: Buffer) => {
@ -244,11 +245,11 @@ export class Bert {
if (obj.length < 256) { if (obj.length < 256) {
buffer[0] = Types.SMALL_TUPLE; buffer[0] = Types.SMALL_TUPLE;
buffer.writeUInt8(obj.length, 1); buffer.writeUInt8(obj.length, 1);
buffer = Buffer.from(buffer, 2); buffer = Buffer.from(buffer, 0, 2);
} else { } else {
buffer[0] = Types.LARGE_TUPLE; buffer[0] = Types.LARGE_TUPLE;
buffer.writeUInt32BE(obj.length, 1); buffer.writeUInt32BE(obj.length, 1);
buffer = buffer.slice(5); buffer = Buffer.from(buffer, 0, 5);
} }
for (let i = 0; i < obj.length; ++i) { for (let i = 0; i < obj.length; ++i) {
@ -261,25 +262,25 @@ export class Bert {
encode_array = (obj: any[], buffer: Buffer) => { encode_array = (obj: any[], buffer: Buffer) => {
if (obj.length === 0) { if (obj.length === 0) {
buffer[0] = Types.NIL; buffer[0] = Types.NIL;
return Buffer.from(buffer, 1); return Buffer.from(buffer, 0, 1);
} }
buffer[0] = Types.LIST; buffer[0] = Types.LIST;
buffer.writeUInt32BE(obj.length, 1); buffer.writeUInt32BE(obj.length, 1);
buffer = Buffer.from(buffer, 5); buffer = Buffer.from(buffer, 0, 5);
for (let i = 0; i < obj.length; ++i) { for (let i = 0; i < obj.length; ++i) {
buffer = this.#encode(obj[i], buffer); buffer = this.#encode(obj[i], buffer);
} }
buffer[0] = Types.NIL; buffer[0] = Types.NIL;
return Buffer.from(buffer, 1); return Buffer.from(buffer, 0, 1);
}; };
encode_map = (obj: Record<string, any>, buffer: Buffer) => { encode_map = (obj: Record<string, any>, buffer: Buffer) => {
const keys = Object.keys(obj); const keys = Object.keys(obj);
buffer[0] = Types.MAP; buffer[0] = Types.MAP;
buffer.writeUInt32BE(keys.length, 1); buffer.writeUInt32BE(keys.length, 1);
buffer = Buffer.from(buffer, 5); buffer = Buffer.from(buffer, 0, 5);
for (let i = 0; i < keys.length; ++i) { for (let i = 0; i < keys.length; ++i) {
const key = this.mapKeyAsAtom ? this.toAtom(keys[i]) : keys[i]; const key = this.mapKeyAsAtom ? this.toAtom(keys[i]) : keys[i];