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
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## Node.
/node_modules
/lib
/dist

## Others.
/docs
/coverage
/.cache
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- Modernize eslint.
- Use prettier.
- Prepare environment for TS.
- Rewrite tests to TS (#958).

### 3.12.0

Expand Down
3 changes: 0 additions & 3 deletions jest.config.js

This file was deleted.

18 changes: 18 additions & 0 deletions jest.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const config = {
verbose: true,
preset: 'ts-jest',
testEnvironment: 'node',
testRegex: 'src/test/test-.*\\.ts',
transform: {
'^.+\\.ts$': [
'ts-jest',
{
tsconfig: 'tsconfig.json',
},
],
},
coveragePathIgnorePatterns: ['src/test'],
cacheDirectory: '.cache/jest',
};

export default config;
22 changes: 19 additions & 3 deletions npm-scripts.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,31 @@ async function run() {
switch (task) {
case 'grammar': {
grammar();

break;
}

case 'lint': {
lint();

break;
}

case 'lint:fix': {
lint(true);

break;
}

case 'test': {
test();

break;
}

case 'coverage': {
coverage();

break;
}

Expand Down Expand Up @@ -72,6 +82,7 @@ async function run() {

// eslint-disable-next-line no-console
console.log('update tryit-jssip and JsSIP website');

break;
}

Expand All @@ -92,9 +103,14 @@ function lint(fix = false) {
function test() {
logInfo('test()');

// TODO: remove when tests are written in TS.
buildTypescript();
executeCmd('jest');
executeCmd(`jest --silent false --detectOpenHandles ${taskArgs}`);
}

function coverage() {
logInfo('coverage()');

executeCmd(`jest --coverage ${taskArgs}`);
executeCmd('open-cli coverage/lcov-report/index.html');
}

function grammar() {
Expand Down
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"lint": "node npm-scripts.mjs lint",
"lint:fix": "node npm-scripts.mjs lint:fix",
"test": "node npm-scripts.mjs test",
"coverage": "node npm-scripts.mjs coverage",
"build": "node npm-scripts.mjs build",
"typescript:build": "node npm-scripts.mjs typescript:build",
"release": "node npm-scripts.js release"
Expand All @@ -50,6 +51,7 @@
"@eslint/js": "^9.39.2",
"@types/debug": "^4.1.12",
"@types/events": "^3.0.3",
"@types/jest": "^30.0.0",
"@types/node": "^25.0.10",
"cpx": "^1.5.0",
"esbuild": "^0.27.2",
Expand All @@ -59,8 +61,10 @@
"eslint-plugin-prettier": "^5.5.5",
"globals": "^17.0.0",
"jest": "^30.2.0",
"open-cli": "^8.0.0",
"pegjs": "^0.7.0",
"prettier": "^3.8.1",
"ts-jest": "^29.4.6",
"typescript": "^5.9.3",
"typescript-eslint": "^8.53.1"
}
Expand Down
2 changes: 1 addition & 1 deletion src/NameAddrHeader.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export class NameAddrHeader {

constructor(uri: URI, display_name?: string, parameters?: Parameters);

setParam(key: string, value?: string): void;
setParam(key: string, value?: string | number | null): void;

// eslint-disable-next-line @typescript-eslint/no-explicit-any
getParam<T = any>(key: string): T;
Expand Down
4 changes: 2 additions & 2 deletions src/URI.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export class URI {
headers?: Headers
);

setParam(key: string, value?: string): void;
setParam(key: string, value?: string | number | null): void;

getParam<T = unknown>(key: string): T;

Expand All @@ -45,7 +45,7 @@ export class URI {

toString(): string;

toAor(): string;
toAor(show_port?: boolean): string;

static parse(uri: string): Grammar | undefined;
}
58 changes: 58 additions & 0 deletions src/test/include/LoopSocket.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// LoopSocket send message itself.
// Used P2P logic: message call-id is modified in each leg.

import type { Socket } from '../../Socket';

export default class LoopSocket implements Socket {
url = 'ws://localhost:12345';
via_transport = 'WS';
sip_uri = 'sip:localhost:12345;transport=ws';

connect(): void {
setTimeout(() => {
this.onconnect();
}, 0);
}

disconnect(): void {}

send(message: string): boolean {
const new_message = this.modifyCallId(message);

setTimeout(() => {
this.ondata(new_message);
}, 0);

return true;
}

isConnected(): boolean {
return true;
}

isConnecting(): boolean {
return false;
}

onconnect(): void {}

ondisconnect(): void {}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
ondata<T>(_event: T): void {}

// Call-ID: add or drop word '_second'.
private modifyCallId(message: string): string {
const begin = message.indexOf('Call-ID');
const end = message.indexOf('\r', begin);
let callId = message.substring(begin + 9, end);

if (callId.endsWith('_second')) {
callId = callId.substring(0, callId.length - 7);
} else {
callId += '_second';
}

return `${message.substring(0, begin)}Call-ID: ${callId}${message.substring(end)}`;
}
}
10 changes: 6 additions & 4 deletions src/test/include/common.js → src/test/include/common.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
/* eslint no-console: 0*/
/* eslint no-console: 0 */

// Show uncaught errors.
process.on('uncaughtException', function (error) {
process.on('uncaughtException', function (error: Error) {
console.error('uncaught exception:');
console.error(error.stack);
process.exit(1);
});

// Define global.WebSocket.
global.WebSocket = function () {
(globalThis as Record<string, unknown>)['WebSocket'] = function (this: {
close: () => void;
}) {
this.close = function () {};
};

// Define global.navigator for bowser module.
global.navigator = {
(globalThis as Record<string, unknown>)['navigator'] = {
userAgent: '',
};
56 changes: 56 additions & 0 deletions src/test/include/consts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const SOCKET_DESCRIPTION: Record<string, any> = {
via_transport: 'WS',
sip_uri: 'sip:localhost:12345;transport=ws',
url: 'ws://localhost:12345',
};

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const UA_CONFIGURATION: Record<string, any> = {
uri: 'sip:f%61keUA@jssip.net',
password: '1234ññññ',
display_name: 'Fake UA ð→€ł !!!',
authorization_user: 'fakeUA',
instance_id: 'uuid:8f1fa16a-1165-4a96-8341-785b1ef24f12',
registrar_server: 'registrar.jssip.NET:6060;TRansport=TCP',
register_expires: 600,
register: false,
connection_recovery_min_interval: 2,
connection_recovery_max_interval: 30,
use_preloaded_route: true,
no_answer_timeout: 60000,
session_timers: true,
};

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const UA_CONFIGURATION_AFTER_START: Record<string, any> = {
uri: 'sip:fakeUA@jssip.net',
password: '1234ññññ',
display_name: 'Fake UA ð→€ł !!!',
authorization_user: 'fakeUA',
instance_id: '8f1fa16a-1165-4a96-8341-785b1ef24f12', // Without 'uuid:'.
registrar_server: 'sip:registrar.jssip.net:6060;transport=tcp',
register_expires: 600,
register: false,
use_preloaded_route: true,
no_answer_timeout: 60000 * 1000, // Internally converted to miliseconds.
session_timers: true,
};

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const UA_TRANSPORT_AFTER_START: Record<string, any> = {
sockets: [
{
socket: {
via_transport: 'WS',
sip_uri: 'sip:localhost:12345;transport=ws',
url: 'ws://localhost:12345',
},
weight: 0,
},
],
recovery_options: {
min_interval: 2,
max_interval: 30,
},
};
42 changes: 0 additions & 42 deletions src/test/include/loopSocket.js

This file was deleted.

54 changes: 0 additions & 54 deletions src/test/include/testUA.js

This file was deleted.

Loading