maybe fix length issue
This commit is contained in:
parent
8dab4d57fa
commit
f2bd5e4795
|
@ -134,7 +134,7 @@ export declare class Bert {
|
||||||
readonly [Symbol.unscopables]?: boolean | undefined;
|
readonly [Symbol.unscopables]?: boolean | undefined;
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
encode: (obj: any, noCopy?: boolean) => Buffer;
|
encode: (obj: any, copy?: boolean) => Buffer;
|
||||||
decode: (buffer: Buffer) => any;
|
decode: (buffer: Buffer) => any;
|
||||||
encode_string: (obj: string, buffer: Buffer) => Buffer;
|
encode_string: (obj: string, buffer: Buffer) => Buffer;
|
||||||
encode_boolean: (obj: boolean, buffer: Buffer) => any;
|
encode_boolean: (obj: boolean, buffer: Buffer) => any;
|
||||||
|
|
|
@ -38,18 +38,18 @@ export class Bert {
|
||||||
toAtom = toAtom;
|
toAtom = toAtom;
|
||||||
toTuple = toTuple;
|
toTuple = toTuple;
|
||||||
#encode = (obj, buffer) => this[`encode_${typeof obj}`](obj, buffer);
|
#encode = (obj, buffer) => this[`encode_${typeof obj}`](obj, buffer);
|
||||||
encode = (obj, noCopy = false) => {
|
encode = (obj, copy = true) => {
|
||||||
const tailBuffer = this.#encode(obj, Buffer.from(this.outputBuffer, 1));
|
const tailBuffer = this.#encode(obj, Buffer.from(this.outputBuffer, 1));
|
||||||
if (tailBuffer.length === 0) {
|
if (tailBuffer.length === 0) {
|
||||||
throw new Error("Bert encode a too big term, encoding buffer overflow");
|
throw new Error("Bert encode a too big term, encoding buffer overflow");
|
||||||
}
|
}
|
||||||
else if (noCopy) {
|
else if (copy) {
|
||||||
return Buffer.from(this.outputBuffer, 0, this.outputBuffer.length - tailBuffer.length);
|
const ret = Buffer.alloc(tailBuffer.length);
|
||||||
|
this.outputBuffer.copy(ret, 0, 0, ret.length);
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
const res = Buffer.alloc(this.outputBuffer.length - tailBuffer.length);
|
return Buffer.from(this.outputBuffer, 0, tailBuffer.length);
|
||||||
this.outputBuffer.copy(res, 0, 0, res.length);
|
|
||||||
return res;
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
#decode = (buffer) => {
|
#decode = (buffer) => {
|
||||||
|
@ -106,7 +106,7 @@ export class Bert {
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
buffer[0] = Types.STRING;
|
buffer[0] = Types.STRING;
|
||||||
buffer.writeUInt16BE(obj.length, 1);
|
buffer.writeUInt16BE(Buffer.byteLength(obj, "utf-8"), 1);
|
||||||
const len = buffer.write(obj, 3);
|
const len = buffer.write(obj, 3);
|
||||||
return Buffer.from(buffer, 0, 3 + len);
|
return Buffer.from(buffer, 0, 3 + len);
|
||||||
}
|
}
|
||||||
|
@ -201,8 +201,8 @@ export class Bert {
|
||||||
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);
|
const len = obj.copy(buffer, 5);
|
||||||
return Buffer.from(buffer, 0, 5 + obj.length);
|
return Buffer.from(buffer, 0, 5 + len);
|
||||||
};
|
};
|
||||||
encode_undefined = (_obj, buffer) => {
|
encode_undefined = (_obj, buffer) => {
|
||||||
return this.#encode(null, buffer);
|
return this.#encode(null, buffer);
|
||||||
|
|
23
src/bert.ts
23
src/bert.ts
|
@ -50,20 +50,23 @@ export class Bert {
|
||||||
#encode = (obj: any, buffer: Buffer) =>
|
#encode = (obj: any, buffer: Buffer) =>
|
||||||
(this as any)[`encode_${typeof obj}`](obj, buffer);
|
(this as any)[`encode_${typeof obj}`](obj, buffer);
|
||||||
|
|
||||||
encode = (obj: any, noCopy = false) => {
|
encode = (obj: any, copy = true) => {
|
||||||
const tailBuffer = this.#encode(obj, Buffer.from(this.outputBuffer, 1));
|
const tailBuffer = this.#encode(obj, Buffer.from(this.outputBuffer, 1));
|
||||||
|
|
||||||
if (tailBuffer.length === 0) {
|
if (tailBuffer.length === 0) {
|
||||||
throw new Error("Bert encode a too big term, encoding buffer overflow");
|
throw new Error("Bert encode a too big term, encoding buffer overflow");
|
||||||
} else if (noCopy) {
|
}
|
||||||
|
else if (copy) {
|
||||||
|
const ret = Buffer.alloc(tailBuffer.length);
|
||||||
|
this.outputBuffer.copy(ret, 0, 0, ret.length);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
else {
|
||||||
return Buffer.from(
|
return Buffer.from(
|
||||||
this.outputBuffer,
|
this.outputBuffer,
|
||||||
0,
|
0,
|
||||||
this.outputBuffer.length - tailBuffer.length,
|
tailBuffer.length,
|
||||||
);
|
);
|
||||||
} else {
|
|
||||||
const res = Buffer.alloc(this.outputBuffer.length - tailBuffer.length);
|
|
||||||
this.outputBuffer.copy(res, 0, 0, res.length);
|
|
||||||
return res;
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -127,7 +130,7 @@ export class Bert {
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
buffer[0] = Types.STRING;
|
buffer[0] = Types.STRING;
|
||||||
buffer.writeUInt16BE(obj.length, 1);
|
buffer.writeUInt16BE(Buffer.byteLength(obj, "utf-8"), 1);
|
||||||
const len = buffer.write(obj, 3);
|
const len = buffer.write(obj, 3);
|
||||||
return Buffer.from(buffer, 0, 3 + len);
|
return Buffer.from(buffer, 0, 3 + len);
|
||||||
}
|
}
|
||||||
|
@ -233,8 +236,8 @@ export class Bert {
|
||||||
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);
|
const len = obj.copy(buffer, 5);
|
||||||
return Buffer.from(buffer, 0, 5 + obj.length);
|
return Buffer.from(buffer, 0, 5 + len);
|
||||||
};
|
};
|
||||||
|
|
||||||
encode_undefined = (_obj: Buffer, buffer: Buffer) => {
|
encode_undefined = (_obj: Buffer, buffer: Buffer) => {
|
||||||
|
|
Loading…
Reference in New Issue