Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions packages/fxa-settings/src/lib/channels/pairing-channel.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ let mockChannel: {
_channelId?: string;
_channelKey?: Uint8Array;
_connection?: unknown;
closed?: boolean;
};

jest.mock(
Expand Down Expand Up @@ -56,6 +57,7 @@ describe('PairingChannelClient', () => {
// 'testkey1' — the same bytes VALID_KEY decodes to.
_channelId: CHAN,
_channelKey: new TextEncoder().encode('testkey1'),
closed: false,
};
jest.clearAllMocks();
client = new PairingChannelClient();
Expand Down Expand Up @@ -186,6 +188,16 @@ describe('PairingChannelClient', () => {
await expect(client.send('msg')).rejects.toThrow('Not connected');
});

it('throws CONNECTION_CLOSED when the channel is torn down', async () => {
await client.open(SERVER, CHAN, VALID_KEY);
mockChannel.closed = true;

await expect(client.send('pair:supp:authorize')).rejects.toMatchObject({
errno: 1006,
});
expect(mockChannel.send).not.toHaveBeenCalled();
});

it('throws for empty message', async () => {
await client.open(SERVER, CHAN, VALID_KEY);
await expect(client.send('')).rejects.toThrow('malformed message');
Expand Down
6 changes: 6 additions & 0 deletions packages/fxa-settings/src/lib/channels/pairing-channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@ type PairingChannelSocket = {
* socket goes away, and the package's `close()` dereferences it unguarded.
*/
_connection?: unknown;
/** True once fxa-pairing-channel has torn the WebSocket down. */
readonly closed?: boolean;
};

export class PairingChannelClient extends EventTarget {
Expand Down Expand Up @@ -291,6 +293,10 @@ export class PairingChannelClient extends EventTarget {
console.warn('No pairing channel!');
throw new PairingChannelError('NOT_CONNECTED');
}
if (this.channel.closed) {
console.warn('Pairing channel is closed!');
throw new PairingChannelError('CONNECTION_CLOSED');
}
if (!message) {
console.warn('No message!');
throw new PairingChannelError('INVALID_OUTBOUND_MESSAGE');
Expand Down