Merge pull request #1242 from vincentbavitz/ts-constants

Partially Move Constants to TypeScript
pull/1248/head
Vince 5 years ago committed by GitHub
commit 2a3d0331f3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -13,14 +13,14 @@ const NUM_SEND_CONNECTIONS = 3;
const getTTLForType = type => {
switch (type) {
case 'pairing-request':
return window.libsession.Constants.TTL_DEFAULT.PAIRING_REQUEST;
case 'device-unpairing':
return 4 * 24 * 60 * 60 * 1000; // 4 days for device unpairing
return window.libsession.Constants.TTL_DEFAULT.DEVICE_UNPAIRING;
case 'onlineBroadcast':
return 60 * 1000; // 1 minute for online broadcast message
case 'pairing-request':
return 2 * 60 * 1000; // 2 minutes for pairing requests
return window.libsession.Constants.TTL_DEFAULT.ONLINE_BROADCAST;
default:
return 24 * 60 * 60 * 1000; // 1 day default for any other message
return window.libsession.Constants.TTL_DEFAULT.REGULAR_MESSAGE;
}
};

@ -0,0 +1,13 @@
import { NumberUtils } from './utils';
// Default TTL
export const TTL_DEFAULT = {
PAIRING_REQUEST: NumberUtils.timeAsMs(2, 'minutes'),
DEVICE_UNPAIRING: NumberUtils.timeAsMs(4, 'days'),
SESSION_REQUEST: NumberUtils.timeAsMs(4, 'days'),
SESSION_ESTABLISHED: NumberUtils.timeAsMs(2, 'days'),
END_SESSION_MESSAGE: NumberUtils.timeAsMs(4, 'days'),
TYPING_MESSAGE: NumberUtils.timeAsMs(1, 'minute'),
ONLINE_BROADCAST: NumberUtils.timeAsMs(1, 'minute'),
REGULAR_MESSAGE: NumberUtils.timeAsMs(2, 'days'),
};

@ -3,7 +3,8 @@ import * as Protocols from './protocols';
import * as Types from './types';
import * as Utils from './utils';
import * as Sending from './sending';
import * as Constants from './constants';
export * from './instance';
export { Messages, Utils, Protocols, Types, Sending };
export { Messages, Utils, Protocols, Types, Sending, Constants };

@ -1,5 +1,6 @@
import { Message } from '../Message';
import { SignalService } from '../../../../protobuf';
import { Constants } from '../../..';
export abstract class ContentMessage extends Message {
public plainTextBuffer(): Uint8Array {
@ -14,7 +15,6 @@ export abstract class ContentMessage extends Message {
* this value can be used in all child classes
*/
protected getDefaultTTL(): number {
// 1 day default for any other message
return 24 * 60 * 60 * 1000;
return Constants.TTL_DEFAULT.REGULAR_MESSAGE;
}
}

@ -1,9 +1,10 @@
import { SessionRequestMessage } from './SessionRequestMessage';
import { SignalService } from '../../../../protobuf';
import { Constants } from '../../..';
export class EndSessionMessage extends SessionRequestMessage {
public ttl(): number {
return 4 * 24 * 60 * 60 * 1000; // 4 days
return Constants.TTL_DEFAULT.END_SESSION_MESSAGE;
}
protected contentProto(): SignalService.Content {

@ -2,6 +2,7 @@ import { ContentMessage } from './ContentMessage';
import { SignalService } from '../../../../protobuf';
import * as crypto from 'crypto';
import { MessageParams } from '../Message';
import { Constants } from '../../..';
export class SessionEstablishedMessage extends ContentMessage {
public readonly padding: Buffer;
@ -18,7 +19,7 @@ export class SessionEstablishedMessage extends ContentMessage {
this.padding = crypto.randomBytes(paddingLength);
}
public ttl(): number {
return 5 * 60 * 1000;
return Constants.TTL_DEFAULT.SESSION_ESTABLISHED;
}
protected contentProto(): SignalService.Content {

@ -1,6 +1,7 @@
import { ContentMessage } from './ContentMessage';
import { SignalService } from '../../../../protobuf';
import { MessageParams } from '../Message';
import { Constants } from '../../..';
export interface PreKeyBundleType {
identityKey: Uint8Array;
@ -17,7 +18,6 @@ interface SessionRequestParams extends MessageParams {
}
export class SessionRequestMessage extends ContentMessage {
public static readonly ttl = 4 * 24 * 60 * 60 * 1000; // 4 days
private readonly preKeyBundle: PreKeyBundleType;
constructor(params: SessionRequestParams) {
@ -25,8 +25,14 @@ export class SessionRequestMessage extends ContentMessage {
this.preKeyBundle = params.preKeyBundle;
}
public static defaultTTL(): number {
// Gets the default TTL for Session Request, as
// public static readonly ttl: number <-- cannot be assigned a non-literal
return Constants.TTL_DEFAULT.SESSION_REQUEST;
}
public ttl(): number {
return SessionRequestMessage.ttl;
return SessionRequestMessage.defaultTTL();
}
protected getPreKeyBundleMessage(): SignalService.PreKeyBundleMessage {

@ -1,9 +1,9 @@
import { ContentMessage } from './ContentMessage';
import { SignalService } from '../../../../protobuf';
import { TextEncoder } from 'util';
import { MessageParams } from '../Message';
import { StringUtils } from '../../../utils';
import { PubKey } from '../../../types';
import { Constants } from '../../..';
interface TypingMessageParams extends MessageParams {
isTyping: boolean;
@ -26,7 +26,7 @@ export class TypingMessage extends ContentMessage {
}
public ttl(): number {
return 60 * 1000; // 1 minute for typing indicators
return Constants.TTL_DEFAULT.TYPING_MESSAGE;
}
protected contentProto(): SignalService.Content {

@ -1,9 +1,10 @@
import { DataMessage } from './DataMessage';
import { SignalService } from '../../../../../protobuf';
import { Constants } from '../../../..';
export class DeviceUnlinkMessage extends DataMessage {
public ttl(): number {
return 4 * 24 * 60 * 60 * 1000; // 4 days for device unlinking
return Constants.TTL_DEFAULT.DEVICE_UNPAIRING;
}
public dataProto(): SignalService.DataMessage {

@ -1,6 +1,7 @@
import { ContentMessage } from '../ContentMessage';
import { SignalService } from '../../../../../protobuf';
import { MessageParams } from '../../Message';
import { Constants } from '../../../..';
export interface DeviceLinkMessageParams extends MessageParams {
primaryDevicePubKey: string;
secondaryDevicePubKey: string;
@ -20,7 +21,7 @@ export class DeviceLinkRequestMessage extends ContentMessage {
}
public ttl(): number {
return 2 * 60 * 1000; // 2 minutes for pairing requests
return Constants.TTL_DEFAULT.PAIRING_REQUEST;
}
protected getDataMessage(): SignalService.DataMessage | undefined {

@ -81,7 +81,7 @@ export class SessionProtocol {
const now = Date.now();
const sentTimestamps = Object.entries(this.sentSessionsTimestamp);
const promises = sentTimestamps.map(async ([device, sent]) => {
const expireTime = sent + SessionRequestMessage.ttl;
const expireTime = sent + SessionRequestMessage.defaultTTL();
// Check if we need to send a session request
if (now < expireTime) {
return;

@ -0,0 +1,47 @@
type TimeUnit =
| 'second'
| 'seconds'
| 'minute'
| 'minutes'
| 'hour'
| 'hours'
| 'day'
| 'days';
export const timeAsMs = (value: number, unit: TimeUnit) => {
// Converts a time to milliseconds
// Valid units: second, minute, hour, day
const unitAsSingular = unit.replace(new RegExp('s?$'), '');
switch (unitAsSingular) {
case 'second':
return value * 1000;
case 'minute':
return value * 60 * 1000;
case 'hour':
return value * 60 * 60 * 1000;
case 'day':
return value * 24 * 60 * 60 * 1000;
default:
return value;
}
};
export const msAsUnit = (value: number, unit: TimeUnit) => {
// Converts milliseconds to your preferred unit
// Valid units: second(s), minute(s), hour(s), day(s)
const unitAsSingular = unit.replace(new RegExp('s?$'), '');
switch (unitAsSingular) {
case 'second':
return value / 1000;
case 'minute':
return value / 60 / 1000;
case 'hour':
return value / 60 / 60 / 1000;
case 'day':
return value / 24 / 60 / 60 / 1000;
default:
return value;
}
};

@ -2,6 +2,7 @@ import * as MessageUtils from './Messages';
import * as GroupUtils from './Groups';
import * as SyncMessageUtils from './SyncMessage';
import * as StringUtils from './String';
import * as NumberUtils from './Number';
import * as PromiseUtils from './Promise';
import * as ProtobufUtils from './Protobuf';
@ -14,6 +15,7 @@ export {
SyncMessageUtils,
GroupUtils,
StringUtils,
NumberUtils,
PromiseUtils,
ProtobufUtils,
};

@ -52,10 +52,9 @@ describe('MessageEncrypter', () => {
it('should return a MEDIUM_GROUP_CIPHERTEXT envelope type', async () => {
const data = crypto.randomBytes(10);
sandbox.stub(
Ratchet,
'encryptWithSenderKey'
).resolves({ ciphertext: '' });
sandbox
.stub(Ratchet, 'encryptWithSenderKey')
.resolves({ ciphertext: '' });
const result = await MessageEncrypter.encrypt(
TestUtils.generateFakePubKey(),

@ -9,6 +9,7 @@ import {
import { SignalService } from '../../../protobuf';
import { TextEncoder } from 'util';
import { toNumber } from 'lodash';
import { Constants } from '../../../session';
describe('ChatMessage', () => {
it('can create empty message with just a timestamp', () => {
@ -128,11 +129,11 @@ describe('ChatMessage', () => {
expect(firstAttachment?.url).to.be.deep.equal('url');
});
it('ttl of 1 day', () => {
it('correct ttl', () => {
const message = new ChatMessage({
timestamp: Date.now(),
});
expect(message.ttl()).to.equal(24 * 60 * 60 * 1000);
expect(message.ttl()).to.equal(Constants.TTL_DEFAULT.REGULAR_MESSAGE);
});
it('has an identifier', () => {

@ -9,6 +9,7 @@ import { TextEncoder } from 'util';
import { TestUtils } from '../../test-utils';
import { StringUtils } from '../../../session/utils';
import { PubKey } from '../../../session/types';
import { Constants } from '../../../session';
describe('ClosedGroupChatMessage', () => {
let groupId: PubKey;
@ -44,7 +45,7 @@ describe('ClosedGroupChatMessage', () => {
.to.be.equal(chatMessage.timestamp);
});
it('ttl of 1 day', () => {
it('correct ttl', () => {
const chatMessage = new ChatMessage({
timestamp: Date.now(),
});
@ -52,7 +53,7 @@ describe('ClosedGroupChatMessage', () => {
groupId,
chatMessage,
});
expect(message.ttl()).to.equal(24 * 60 * 60 * 1000);
expect(message.ttl()).to.equal(Constants.TTL_DEFAULT.REGULAR_MESSAGE);
});
it('has an identifier', () => {

@ -7,6 +7,7 @@ import {
} from '../../../session/messages/outgoing';
import { SignalService } from '../../../protobuf';
import { LokiProfile } from '../../../types/Message';
import { Constants } from '../../../session';
describe('DeviceLinkMessage', () => {
let linkRequestMessage: DeviceLinkRequestMessage;
@ -115,9 +116,13 @@ describe('DeviceLinkMessage', () => {
});
});
it('ttl of 2 minutes', () => {
expect(linkRequestMessage.ttl()).to.equal(2 * 60 * 1000);
expect(linkGrantMessage.ttl()).to.equal(2 * 60 * 1000);
it('correct ttl', () => {
expect(linkRequestMessage.ttl()).to.equal(
Constants.TTL_DEFAULT.PAIRING_REQUEST
);
expect(linkGrantMessage.ttl()).to.equal(
Constants.TTL_DEFAULT.PAIRING_REQUEST
);
});
it('has an identifier', () => {

@ -3,6 +3,7 @@ import { beforeEach } from 'mocha';
import { DeviceUnlinkMessage } from '../../../session/messages/outgoing';
import { SignalService } from '../../../protobuf';
import { Constants } from '../../../session';
describe('DeviceUnlinkMessage', () => {
let message: DeviceUnlinkMessage;
@ -21,8 +22,8 @@ describe('DeviceUnlinkMessage', () => {
);
});
it('ttl of 4 days', () => {
expect(message.ttl()).to.equal(4 * 24 * 60 * 60 * 1000);
it('correct ttl', () => {
expect(message.ttl()).to.equal(Constants.TTL_DEFAULT.DEVICE_UNPAIRING);
});
it('has an identifier', () => {

@ -4,6 +4,7 @@ import { beforeEach } from 'mocha';
import { EndSessionMessage } from '../../../session/messages/outgoing';
import { SignalService } from '../../../protobuf';
import { TextEncoder } from 'util';
import { Constants } from '../../../session';
describe('EndSessionMessage', () => {
let message: EndSessionMessage;
@ -64,8 +65,8 @@ describe('EndSessionMessage', () => {
expect(decoded.dataMessage).to.have.deep.property('body', 'TERMINATE');
});
it('ttl of 4 days', () => {
expect(message.ttl()).to.equal(4 * 24 * 60 * 60 * 1000);
it('correct ttl', () => {
expect(message.ttl()).to.equal(Constants.TTL_DEFAULT.END_SESSION_MESSAGE);
});
it('has an identifier', () => {

@ -3,6 +3,7 @@ import { beforeEach } from 'mocha';
import { GroupInvitationMessage } from '../../../session/messages/outgoing';
import { SignalService } from '../../../protobuf';
import { Constants } from '../../../session';
describe('GroupInvitationMessage', () => {
let message: GroupInvitationMessage;
@ -38,8 +39,8 @@ describe('GroupInvitationMessage', () => {
);
});
it('ttl of 1 day', () => {
expect(message.ttl()).to.equal(24 * 60 * 60 * 1000);
it('correct ttl', () => {
expect(message.ttl()).to.equal(Constants.TTL_DEFAULT.REGULAR_MESSAGE);
});
it('has an identifier', () => {

@ -7,6 +7,7 @@ import {
} from '../../../session/messages/outgoing';
import { SignalService } from '../../../protobuf';
import { toNumber } from 'lodash';
import { Constants } from '../../../session';
describe('ReceiptMessage', () => {
let readMessage: ReadReceiptMessage;
@ -42,9 +43,11 @@ describe('ReceiptMessage', () => {
expect(decodedTimestamps).to.deep.equal(timestamps);
});
it('ttl of 1 day', () => {
expect(readMessage.ttl()).to.equal(24 * 60 * 60 * 1000);
expect(deliveryMessage.ttl()).to.equal(24 * 60 * 60 * 1000);
it('correct ttl', () => {
expect(readMessage.ttl()).to.equal(Constants.TTL_DEFAULT.REGULAR_MESSAGE);
expect(deliveryMessage.ttl()).to.equal(
Constants.TTL_DEFAULT.REGULAR_MESSAGE
);
});
it('has an identifier', () => {

@ -3,6 +3,7 @@ import { beforeEach } from 'mocha';
import { SessionEstablishedMessage } from '../../../session/messages/outgoing';
import { SignalService } from '../../../protobuf';
import { Constants } from '../../../session';
describe('SessionEstablishedMessage', () => {
let message: SessionEstablishedMessage;
@ -21,8 +22,8 @@ describe('SessionEstablishedMessage', () => {
);
});
it('ttl of 5 minutes', () => {
expect(message.ttl()).to.equal(5 * 60 * 1000);
it('correct ttl', () => {
expect(message.ttl()).to.equal(Constants.TTL_DEFAULT.SESSION_ESTABLISHED);
});
it('has an identifier', () => {

@ -4,6 +4,7 @@ import { beforeEach } from 'mocha';
import { SessionRequestMessage } from '../../../session/messages/outgoing';
import { SignalService } from '../../../protobuf';
import { TextDecoder, TextEncoder } from 'util';
import { Constants } from '../../../session';
describe('SessionRequestMessage', () => {
let message: SessionRequestMessage;
@ -64,8 +65,8 @@ describe('SessionRequestMessage', () => {
);
});
it('ttl of 4 days', () => {
expect(message.ttl()).to.equal(4 * 24 * 60 * 60 * 1000);
it('correct ttl', () => {
expect(message.ttl()).to.equal(Constants.TTL_DEFAULT.SESSION_REQUEST);
});
it('has an identifier', () => {

@ -7,6 +7,7 @@ import Long from 'long';
import { toNumber } from 'lodash';
import { StringUtils } from '../../../session/utils';
import { TestUtils } from '../../test-utils';
import { Constants } from '../../../session';
describe('TypingMessage', () => {
it('has Action.STARTED if isTyping = true', () => {
@ -79,12 +80,12 @@ describe('TypingMessage', () => {
);
});
it('ttl of 1 minute', () => {
it('correct ttl', () => {
const message = new TypingMessage({
timestamp: Date.now(),
isTyping: true,
});
expect(message.ttl()).to.equal(60 * 1000);
expect(message.ttl()).to.equal(Constants.TTL_DEFAULT.TYPING_MESSAGE);
});
it('has an identifier', () => {

@ -124,7 +124,7 @@ describe('SessionProtocol', () => {
});
// Set the time just before expiry
clock.tick(SessionRequestMessage.ttl - 100);
clock.tick(SessionRequestMessage.defaultTTL() - 100);
await SessionProtocol.checkSessionRequestExpiry();
expect(getItemById.calledWith('sentSessionsTimestamp'));
@ -140,7 +140,7 @@ describe('SessionProtocol', () => {
});
// Expire the request
clock.tick(SessionRequestMessage.ttl + 100);
clock.tick(SessionRequestMessage.defaultTTL() + 100);
await SessionProtocol.checkSessionRequestExpiry();
expect(getItemById.calledWith('sentSessionsTimestamp'));
@ -159,7 +159,7 @@ describe('SessionProtocol', () => {
sandbox.stub(SessionProtocol, 'sendSessionRequestIfNeeded').resolves();
// Expire the request
clock.tick(SessionRequestMessage.ttl + 100);
clock.tick(SessionRequestMessage.defaultTTL() + 100);
await SessionProtocol.checkSessionRequestExpiry();
expect(getItemById.calledWith('sentSessionsTimestamp'));

Loading…
Cancel
Save