Commit 39835fc for jssip.net

commit 39835fc4fca522d67ae167cfacb8286c02b82954
Author: José Luis Millán <jmillan@aliax.net>
Date:   Thu Dec 3 12:46:38 2026 +0100

    TS: RTCSession/[Info|ReferNotifier]

diff --git a/src/RTCSession.js b/src/RTCSession.js
index 051f382..59c6b79 100644
--- a/src/RTCSession.js
+++ b/src/RTCSession.js
@@ -13,8 +13,10 @@ const SIPMessage = require('./SIPMessage');
 const { Dialog, Status: DialogStatus } = require('./Dialog');
 const { RequestSender } = require('./RequestSender');
 const RTCSession_DTMF = require('./RTCSession/DTMF');
-const RTCSession_Info = require('./RTCSession/Info');
-const RTCSession_ReferNotifier = require('./RTCSession/ReferNotifier');
+const { Info: RTCSession_Info } = require('./RTCSession/Info');
+const {
+	ReferNotifier: RTCSession_ReferNotifier,
+} = require('./RTCSession/ReferNotifier');
 const RTCSession_ReferSubscriber = require('./RTCSession/ReferSubscriber');
 const URI = require('./URI');

diff --git a/src/RTCSession/Info.js b/src/RTCSession/Info.js
deleted file mode 100644
index 8c57bb9..0000000
--- a/src/RTCSession/Info.js
+++ /dev/null
@@ -1,98 +0,0 @@
-const EventEmitter = require('events').EventEmitter;
-const JsSIP_C = require('../Constants');
-const Exceptions = require('../Exceptions');
-const Utils = require('../Utils');
-
-module.exports = class Info extends EventEmitter {
-	constructor(session) {
-		super();
-
-		this._session = session;
-		this._direction = null;
-		this._contentType = null;
-		this._body = null;
-	}
-
-	get contentType() {
-		return this._contentType;
-	}
-
-	get body() {
-		return this._body;
-	}
-
-	send(contentType, body, options = {}) {
-		this._direction = 'outgoing';
-
-		if (contentType === undefined) {
-			throw new TypeError('Not enough arguments');
-		}
-
-		// Check RTCSession Status.
-		if (
-			this._session.status !== this._session.C.STATUS_CONFIRMED &&
-			this._session.status !== this._session.C.STATUS_WAITING_FOR_ACK
-		) {
-			throw new Exceptions.InvalidStateError(this._session.status);
-		}
-
-		this._contentType = contentType;
-		this._body = body;
-
-		const extraHeaders = Utils.cloneArray(options.extraHeaders);
-
-		extraHeaders.push(`Content-Type: ${contentType}`);
-
-		this._session.newInfo({
-			originator: 'local',
-			info: this,
-			request: this.request,
-		});
-
-		this._session.sendRequest(JsSIP_C.INFO, {
-			extraHeaders,
-			eventHandlers: {
-				onSuccessResponse: response => {
-					this.emit('succeeded', {
-						originator: 'remote',
-						response,
-					});
-				},
-				onErrorResponse: response => {
-					this.emit('failed', {
-						originator: 'remote',
-						response,
-					});
-				},
-				onTransportError: () => {
-					this._session.onTransportError();
-				},
-				onRequestTimeout: () => {
-					this._session.onRequestTimeout();
-				},
-				onDialogError: () => {
-					this._session.onDialogError();
-				},
-			},
-			body,
-		});
-	}
-
-	init_incoming(request) {
-		this._direction = 'incoming';
-		this.request = request;
-
-		request.reply(200);
-
-		this._contentType = request.hasHeader('Content-Type')
-			? request.getHeader('Content-Type').toLowerCase()
-			: undefined;
-		this._body = request.body;
-
-		this._session.newInfo({
-			originator: 'remote',
-			info: this,
-			request,
-		});
-	}
-};
diff --git a/src/RTCSession/Info.ts b/src/RTCSession/Info.ts
new file mode 100644
index 0000000..67d90e8
--- /dev/null
+++ b/src/RTCSession/Info.ts
@@ -0,0 +1,195 @@
+import { EventEmitter } from 'events';
+
+import * as JsSIP_C from '../Constants';
+import type { Direction, Originator } from '../Constants';
+import type { DialogSendRequestOptions } from '../Dialog';
+import { InvalidStateError } from '../Exceptions';
+import type { IncomingRequest, IncomingResponse } from '../SIPMessage';
+import * as Utils from '../Utils';
+
+/**
+ * Reported when the INFO got a final response.
+ */
+export type InfoEvent = {
+	originator: Originator;
+	response: IncomingResponse;
+};
+
+/**
+ * The events an {@link Info} emits.
+ */
+export type InfoEventMap = {
+	succeeded: (event: InfoEvent) => void;
+	failed: (event: InfoEvent) => void;
+};
+
+/**
+ * Options accepted by `send()`.
+ */
+export type SendInfoOptions = {
+	/**
+	 * Whole `Name: value` header field lines to add to the INFO.
+	 */
+	extraHeaders?: string[] | null;
+};
+
+/**
+ * The `RTCSession` surface an info depends on.
+ *
+ * TODO: Replace with `import type { RTCSession } from '../RTCSession'` once
+ * `RTCSession` is ported to TypeScript.
+ */
+export interface InfoSession {
+	readonly status: number;
+	readonly C: {
+		STATUS_CONFIRMED: number;
+		STATUS_WAITING_FOR_ACK: number;
+	};
+
+	sendRequest(method: string, options?: DialogSendRequestOptions): void;
+	newInfo(data: {
+		originator: Originator;
+		info: Info;
+		request: IncomingRequest | undefined;
+	}): void;
+	onTransportError(): void;
+	onRequestTimeout(): void;
+	onDialogError(): void;
+}
+
+/**
+ * Sends and receives SIP INFO requests within a session, so application data
+ * carried along an established call.
+ */
+export class Info extends EventEmitter {
+	#session: InfoSession;
+	#direction: Direction | null;
+	#contentType?: string;
+	#body?: string;
+
+	/**
+	 * The received INFO request, only set on an incoming one.
+	 */
+	request?: IncomingRequest;
+
+	constructor(session: InfoSession) {
+		super();
+
+		this.#session = session;
+		this.#direction = null;
+	}
+
+	/**
+	 * Whether the INFO was sent or received.
+	 */
+	get direction(): Direction | null {
+		return this.#direction;
+	}
+
+	/**
+	 * Value of the Content-Type header field.
+	 */
+	get contentType(): string | undefined {
+		return this.#contentType;
+	}
+
+	/**
+	 * Body of the INFO.
+	 */
+	get body(): string | undefined {
+		return this.#body;
+	}
+
+	/**
+	 * Send an INFO request within the session.
+	 *
+	 * @throws
+	 * - `TypeError` if `contentType` is not given.
+	 * - {@link InvalidStateError} if the session is neither confirmed nor
+	 *   waiting for the ACK.
+	 */
+	send(
+		contentType: string,
+		body?: string,
+		options: SendInfoOptions = {}
+	): void {
+		this.#direction = 'outgoing';
+
+		if (contentType === undefined) {
+			throw new TypeError('Not enough arguments');
+		}
+
+		// Check RTCSession Status.
+		if (
+			this.#session.status !== this.#session.C.STATUS_CONFIRMED &&
+			this.#session.status !== this.#session.C.STATUS_WAITING_FOR_ACK
+		) {
+			throw new InvalidStateError(this.#session.status);
+		}
+
+		this.#contentType = contentType;
+		this.#body = body;
+
+		const extraHeaders = Utils.cloneArray(options.extraHeaders);
+
+		extraHeaders.push(`Content-Type: ${contentType}`);
+
+		this.#session.newInfo({
+			originator: 'local',
+			info: this,
+			request: this.request,
+		});
+
+		this.#session.sendRequest(JsSIP_C.INFO, {
+			extraHeaders,
+			eventHandlers: {
+				onSuccessResponse: response => {
+					this.emit('succeeded', {
+						originator: 'remote',
+						response,
+					});
+				},
+				onErrorResponse: response => {
+					this.emit('failed', {
+						originator: 'remote',
+						response,
+					});
+				},
+				onTransportError: () => {
+					this.#session.onTransportError();
+				},
+				onRequestTimeout: () => {
+					this.#session.onRequestTimeout();
+				},
+				onDialogError: () => {
+					this.#session.onDialogError();
+				},
+			},
+			body,
+		});
+	}
+
+	/**
+	 * Take over a received INFO request, answering it with a 200.
+	 *
+	 * @remarks
+	 * - Called by the `RTCSession`. The application must not call it.
+	 */
+	init_incoming(request: IncomingRequest): void {
+		this.#direction = 'incoming';
+		this.request = request;
+
+		request.reply(200);
+
+		this.#contentType = request.hasHeader('Content-Type')
+			? request.getHeader('Content-Type')!.toLowerCase()
+			: undefined;
+		this.#body = request.body;
+
+		this.#session.newInfo({
+			originator: 'remote',
+			info: this,
+			request,
+		});
+	}
+}
diff --git a/src/RTCSession/ReferNotifier.js b/src/RTCSession/ReferNotifier.js
deleted file mode 100644
index 8369e65..0000000
--- a/src/RTCSession/ReferNotifier.js
+++ /dev/null
@@ -1,56 +0,0 @@
-const { Logger } = require('../Logger');
-const JsSIP_C = require('../Constants');
-
-const logger = new Logger('RTCSession:ReferNotifier');
-
-const C = {
-	event_type: 'refer',
-	body_type: 'message/sipfrag;version=2.0',
-	expires: 300,
-};
-
-module.exports = class ReferNotifier {
-	constructor(session, id, expires) {
-		this._session = session;
-		this._id = id;
-		this._expires = expires || C.expires;
-		this._active = true;
-
-		// The creation of a Notifier results in an immediate NOTIFY.
-		this.notify(100);
-	}
-
-	notify(code, reason) {
-		logger.debug('notify()');
-
-		if (this._active === false) {
-			return;
-		}
-
-		reason = reason || JsSIP_C.REASON_PHRASE[code] || '';
-
-		let state;
-
-		if (code >= 200) {
-			state = 'terminated;reason=noresource';
-		} else {
-			state = `active;expires=${this._expires}`;
-		}
-
-		// Put this in a try/catch block.
-		this._session.sendRequest(JsSIP_C.NOTIFY, {
-			extraHeaders: [
-				`Event: ${C.event_type};id=${this._id}`,
-				`Subscription-State: ${state}`,
-				`Content-Type: ${C.body_type}`,
-			],
-			body: `SIP/2.0 ${code} ${reason}`,
-			eventHandlers: {
-				// If a negative response is received, subscription is canceled.
-				onErrorResponse() {
-					this._active = false;
-				},
-			},
-		});
-	}
-};
diff --git a/src/RTCSession/ReferNotifier.ts b/src/RTCSession/ReferNotifier.ts
new file mode 100644
index 0000000..da24da4
--- /dev/null
+++ b/src/RTCSession/ReferNotifier.ts
@@ -0,0 +1,91 @@
+import * as JsSIP_C from '../Constants';
+import type { DialogSendRequestOptions } from '../Dialog';
+import { Logger } from '../Logger';
+
+const logger = new Logger('RTCSession:ReferNotifier');
+
+const C = {
+	event_type: 'refer',
+	body_type: 'message/sipfrag;version=2.0',
+	expires: 300,
+};
+
+/**
+ * The `RTCSession` surface a refer notifier depends on.
+ *
+ * TODO: Replace with `import type { RTCSession } from '../RTCSession'` once
+ * `RTCSession` is ported to TypeScript.
+ */
+export interface ReferNotifierSession {
+	sendRequest(method: string, options?: DialogSendRequestOptions): void;
+}
+
+/**
+ * Reports the progress of a transfer to the peer that asked for it, by
+ * sending a NOTIFY per
+ * [RFC 3515](https://datatracker.ietf.org/doc/html/rfc3515).
+ */
+export class ReferNotifier {
+	#session: ReferNotifierSession;
+	#id: number;
+	#expires: number;
+	#active = true;
+
+	/**
+	 * @param session - The session the transfer was asked on.
+	 * @param id - Value of the `id` parameter of the Event header field, which
+	 *   ties the notifications to the REFER.
+	 * @param expires - How long (in seconds) the subscription lasts.
+	 */
+	constructor(session: ReferNotifierSession, id: number, expires?: number) {
+		this.#session = session;
+		this.#id = id;
+		this.#expires = expires || C.expires;
+
+		// The creation of a Notifier results in an immediate NOTIFY.
+		this.notify(100);
+	}
+
+	/**
+	 * Report the given status code to the peer.
+	 *
+	 * @param code - Status code reached by the transfer. From 200 on, the
+	 *   subscription is terminated.
+	 * @param reason - Reason phrase. The one
+	 *   {@link Constants.REASON_PHRASE} gives for the status code when not
+	 *   given.
+	 */
+	notify(code: number, reason?: string): void {
+		logger.debug('notify()');
+
+		if (this.#active === false) {
+			return;
+		}
+
+		reason = reason || JsSIP_C.REASON_PHRASE[code] || '';
+
+		let state;
+
+		if (code >= 200) {
+			state = 'terminated;reason=noresource';
+		} else {
+			state = `active;expires=${this.#expires}`;
+		}
+
+		// Put this in a try/catch block.
+		this.#session.sendRequest(JsSIP_C.NOTIFY, {
+			extraHeaders: [
+				`Event: ${C.event_type};id=${this.#id}`,
+				`Subscription-State: ${state}`,
+				`Content-Type: ${C.body_type}`,
+			],
+			body: `SIP/2.0 ${code} ${reason}`,
+			eventHandlers: {
+				// If a negative response is received, subscription is canceled.
+				onErrorResponse: () => {
+					this.#active = false;
+				},
+			},
+		});
+	}
+}