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;
|
||||
};
|
||||
};
|
||||
encode: (obj: any, noCopy?: boolean) => Buffer;
|
||||
encode: (obj: any, copy?: boolean) => Buffer;
|
||||
decode: (buffer: Buffer) => any;
|
||||
encode_string: (obj: string, buffer: Buffer) => Buffer;
|
||||
encode_boolean: (obj: boolean, buffer: Buffer) => any;
|
||||
|
|
|
@ -38,18 +38,18 @@ export class Bert {
|
|||
toAtom = toAtom;
|
||||
toTuple = toTuple;
|
||||
#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));
|
||||
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 if (copy) {
|
||||
const ret = Buffer.alloc(tailBuffer.length);
|
||||
this.outputBuffer.copy(ret, 0, 0, ret.length);
|
||||
return ret;
|
||||
}
|
||||
else {
|
||||
const res = Buffer.alloc(this.outputBuffer.length - tailBuffer.length);
|
||||
this.outputBuffer.copy(res, 0, 0, res.length);
|
||||
return res;
|
||||
return Buffer.from(this.outputBuffer, 0, tailBuffer.length);
|
||||
}
|
||||
};
|
||||
#decode = (buffer) => {
|
||||
|
@ -106,7 +106,7 @@ export class Bert {
|
|||
}
|
||||
else {
|
||||
buffer[0] = Types.STRING;
|
||||
buffer.writeUInt16BE(obj.length, 1);
|
||||
buffer.writeUInt16BE(Buffer.byteLength(obj, "utf-8"), 1);
|
||||
const len = buffer.write(obj, 3);
|
||||
return Buffer.from(buffer, 0, 3 + len);
|
||||
}
|
||||
|
@ -201,8 +201,8 @@ export class Bert {
|
|||
encode_binary = (obj, buffer) => {
|
||||
buffer[0] = Types.BINARY;
|
||||
buffer.writeUInt32BE(obj.length, 1);
|
||||
obj.copy(buffer, 5);
|
||||
return Buffer.from(buffer, 0, 5 + obj.length);
|
||||
const len = obj.copy(buffer, 5);
|
||||
return Buffer.from(buffer, 0, 5 + len);
|
||||
};
|
||||
encode_undefined = (_obj, 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) =>
|
||||
(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));
|
||||
|
||||
if (tailBuffer.length === 0) {
|
||||
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(
|
||||
this.outputBuffer,
|
||||
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 {
|
||||
buffer[0] = Types.STRING;
|
||||
buffer.writeUInt16BE(obj.length, 1);
|
||||
buffer.writeUInt16BE(Buffer.byteLength(obj, "utf-8"), 1);
|
||||
const len = buffer.write(obj, 3);
|
||||
return Buffer.from(buffer, 0, 3 + len);
|
||||
}
|
||||
|
@ -233,8 +236,8 @@ export class Bert {
|
|||
encode_binary = (obj: Buffer, buffer: Buffer) => {
|
||||
buffer[0] = Types.BINARY;
|
||||
buffer.writeUInt32BE(obj.length, 1);
|
||||
obj.copy(buffer, 5);
|
||||
return Buffer.from(buffer, 0, 5 + obj.length);
|
||||
const len = obj.copy(buffer, 5);
|
||||
return Buffer.from(buffer, 0, 5 + len);
|
||||
};
|
||||
|
||||
encode_undefined = (_obj: Buffer, buffer: Buffer) => {
|
||||
|
|
Loading…
Reference in New Issue