Improved reliability of strictAssert

This commit is contained in:
jamiebuilds-signal 2022-09-14 09:40:50 -07:00 committed by GitHub
parent 1a54d438c2
commit 64a4d2e717
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 25 additions and 8 deletions

View file

@ -184,7 +184,7 @@ export const StandaloneRegistration = ({
return; return;
} }
strictAssert(number && code, 'Missing number or code'); strictAssert(number != null && code.length > 0, 'Missing number or code');
try { try {
await registerSingleDevice(number, code); await registerSingleDevice(number, code);

View file

@ -242,7 +242,7 @@ async function doGetProfile(c: ConversationModel): Promise<void> {
let accessKey = c.get('accessKey'); let accessKey = c.get('accessKey');
if (profileKey) { if (profileKey) {
strictAssert( strictAssert(
profileKeyVersion && accessKey, profileKeyVersion != null && accessKey != null,
'profileKeyVersion and accessKey are derived from profileKey' 'profileKeyVersion and accessKey are derived from profileKey'
); );

View file

@ -163,7 +163,7 @@ describe('updater/differential', () => {
server.listen(0, () => { server.listen(0, () => {
const addr = server.address(); const addr = server.address();
strictAssert(typeof addr === 'object' && addr, 'node.js apis'); strictAssert(typeof addr === 'object' && addr != null, 'node.js apis');
baseUrl = `http://127.0.0.1:${addr.port}`; baseUrl = `http://127.0.0.1:${addr.port}`;
callback(); callback();

View file

@ -2108,7 +2108,7 @@ export default class MessageReceiver
if (msg.flags && msg.flags & Proto.DataMessage.Flags.PROFILE_KEY_UPDATE) { if (msg.flags && msg.flags & Proto.DataMessage.Flags.PROFILE_KEY_UPDATE) {
strictAssert( strictAssert(
msg.profileKey && msg.profileKey.length > 0, msg.profileKey != null && msg.profileKey.length > 0,
'PROFILE_KEY_UPDATE without profileKey' 'PROFILE_KEY_UPDATE without profileKey'
); );

View file

@ -38,11 +38,28 @@ export function assert(condition: unknown, message: string): asserts condition {
/** /**
* Throws an error if the condition is falsy, regardless of environment. * Throws an error if the condition is falsy, regardless of environment.
*/ */
export function strictAssert(
condition: unknown, /**
* Asserts an expression is true.
*
* @param value - An expression to assert is true.
* @param message - An optional message for the assertion error thrown.
*/
export function strictAssert(value: boolean, message: string): asserts value;
/**
* Asserts a nullable value is non-null.
*
* @param value - A nullable value to assert is non-null.
* @param message - An optional message for the assertion error thrown.
*/
export function strictAssert<T>(
value: T | null | undefined,
message: string message: string
): asserts condition { ): asserts value is T;
if (!condition) {
export function strictAssert(condition: unknown, message: string): void {
if (condition === false || condition == null) {
throw new Error(message); throw new Error(message);
} }
} }