Skip to content

Commit

Permalink
Improved reliability of strictAssert
Browse files Browse the repository at this point in the history
  • Loading branch information
jamiebuilds-signal committed Sep 14, 2022
1 parent 1a54d43 commit 64a4d2e
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 8 deletions.
2 changes: 1 addition & 1 deletion ts/components/StandaloneRegistration.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ export const StandaloneRegistration = ({
return;
}

strictAssert(number && code, 'Missing number or code');
strictAssert(number != null && code.length > 0, 'Missing number or code');

try {
await registerSingleDevice(number, code);
Expand Down
2 changes: 1 addition & 1 deletion ts/services/profiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ async function doGetProfile(c: ConversationModel): Promise<void> {
let accessKey = c.get('accessKey');
if (profileKey) {
strictAssert(
profileKeyVersion && accessKey,
profileKeyVersion != null && accessKey != null,
'profileKeyVersion and accessKey are derived from profileKey'
);

Expand Down
2 changes: 1 addition & 1 deletion ts/test-node/updater/differential_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ describe('updater/differential', () => {

server.listen(0, () => {
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}`;

callback();
Expand Down
2 changes: 1 addition & 1 deletion ts/textsecure/MessageReceiver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2108,7 +2108,7 @@ export default class MessageReceiver

if (msg.flags && msg.flags & Proto.DataMessage.Flags.PROFILE_KEY_UPDATE) {
strictAssert(
msg.profileKey && msg.profileKey.length > 0,
msg.profileKey != null && msg.profileKey.length > 0,
'PROFILE_KEY_UPDATE without profileKey'
);

Expand Down
25 changes: 21 additions & 4 deletions ts/util/assert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,28 @@ export function assert(condition: unknown, message: string): asserts condition {
/**
* 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
): asserts condition {
if (!condition) {
): asserts value is T;

export function strictAssert(condition: unknown, message: string): void {
if (condition === false || condition == null) {
throw new Error(message);
}
}
Expand Down

0 comments on commit 64a4d2e

Please sign in to comment.