mirror of
https://github.com/signalapp/libsignal.git
synced 2026-04-26 01:35:22 +02:00
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.
48 lines
1.1 KiB
TypeScript
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);
|
|
}
|
|
}
|