Files
libsignal/node/ts/zkgroup/internal/ByteArray.ts
Alex Konradi bddca7da60 Throw JS deserialization errors of correct type
Bind the `this` value for the deserialization check callbacks when invoking 
them so that the Rust code can access properties on the Native JS module. 
Without this, the Rust code is called with a "global object" as the context, 
which means it fails to construct a LibSignalErrorBase and has to fall back to 
an untyped error message.
2024-03-28 15:51:41 -04:00

48 lines
1.1 KiB
TypeScript

//
// Copyright 2020-2021 Signal Messenger, LLC.
// SPDX-License-Identifier: AGPL-3.0-only
//
import { LibSignalErrorBase } from '../../Errors';
import * as Native from '../../../Native';
export const UNCHECKED_AND_UNCLONED: unique symbol = Symbol();
export default class ByteArray {
contents: Buffer;
protected constructor(
contents: Buffer,
checkValid: ((contents: Buffer) => void) | typeof UNCHECKED_AND_UNCLONED
) {
if (checkValid === UNCHECKED_AND_UNCLONED) {
this.contents = contents;
} else {
checkValid.call(Native, contents);
this.contents = Buffer.from(contents);
}
}
protected static checkLength(
expectedLength: number
): (contents: Buffer) => void {
return (contents) => {
if (contents.length !== expectedLength) {
throw new LibSignalErrorBase(
`Length of array supplied was ${contents.length} expected ${expectedLength}`,
undefined,
this.name
);
}
};
}
public getContents(): Buffer {
return this.contents;
}
public serialize(): Buffer {
return Buffer.from(this.contents);
}
}