Commit e05c991 for jssip.net
commit e05c9918913b390c359a12ad9c876d976a6e7646
Author: José Luis Millán <jmillan@aliax.net>
Date: Thu Dec 3 17:40:04 2026 +0100
Remove UA shims
diff --git a/npm-scripts.mjs b/npm-scripts.mjs
index 9c6ea33..8266b79 100644
--- a/npm-scripts.mjs
+++ b/npm-scripts.mjs
@@ -216,6 +216,66 @@ function buildTypescript() {
// Copy manual .d.ts files to lib/ until code is moved to TS and declaration files
// are automatically created.
executeCmd("cpx 'src/**/**.d.ts' lib/");
+
+ replacePlaceholders();
+}
+
+/**
+ * Writes what `package.json` holds into the built output, in place of the
+ * placeholders `src/JsSIP.ts` declares. This is what keeps the library from
+ * having to read the manifest at run time, which neither an ES module nor a
+ * bundle can do as easily as CommonJS can.
+ */
+function replacePlaceholders() {
+ logInfo('replacePlaceholders()');
+
+ const values = {
+ __JSSIP_NAME__: pkg.title,
+ __JSSIP_VERSION__: pkg.version,
+ };
+
+ const replaced = new Set();
+
+ const files = fs.readdirSync('lib', {
+ withFileTypes: true,
+ recursive: true,
+ });
+
+ for (const file of files) {
+ if (!file.isFile()) {
+ continue;
+ }
+
+ // NOTE: dirent.parentPath is only available in Node >= 20.
+ const filePath = path.join(file.parentPath ?? 'lib', file.name);
+ const text = fs.readFileSync(filePath, { encoding: 'utf8' });
+
+ let result = text;
+
+ for (const [placeholder, value] of Object.entries(values)) {
+ if (result.includes(placeholder)) {
+ replaced.add(placeholder);
+ result = result.split(placeholder).join(value);
+ }
+ }
+
+ if (result !== text) {
+ fs.writeFileSync(filePath, result, { encoding: 'utf8' });
+ }
+ }
+
+ // A placeholder nothing carries means it was renamed in the sources, and
+ // the built library would ship the literal text in place of the value.
+ for (const placeholder of Object.keys(values)) {
+ if (!replaced.has(placeholder)) {
+ // eslint-disable-next-line no-console
+ console.error(
+ `npm-scripts.mjs [ERROR] replacePlaceholders() | no built file carries ${placeholder}`
+ );
+
+ process.exit(1);
+ }
+ }
}
function deleteLib() {
diff --git a/src/Dialog.ts b/src/Dialog.ts
index 2d421f3..85a57d0 100644
--- a/src/Dialog.ts
+++ b/src/Dialog.ts
@@ -1,8 +1,11 @@
+import type { Notifier } from './Notifier';
+import type { RTCSession } from './RTCSession';
+import type { Subscriber } from './Subscriber';
+import type { UA } from './UA';
import * as JsSIP_C from './Constants';
import { DialogRequestSender } from './Dialog/RequestSender';
import type { DialogRequestSenderEventHandlers } from './Dialog/RequestSender';
import { Logger } from './Logger';
-import type { RequestSenderUA } from './RequestSender';
import * as SIPMessage from './SIPMessage';
import type { IncomingMessage, IncomingRequest } from './SIPMessage';
import * as Transactions from './Transactions';
@@ -61,28 +64,15 @@ export type DialogId = {
toString(): string;
};
-/**
- * The `UA` surface a dialog depends on.
- *
- * TODO: Replace with `import type { UA } from './UA'` once `UA` is ported to
- * TypeScript.
- */
-export interface DialogUA extends RequestSenderUA {
- newDialog(dialog: Dialog): void;
- destroyDialog(dialog: Dialog): void;
-}
-
/**
* The dialog usage that owns the dialog and gets its in-dialog requests.
*
- * TODO: Replace with a `RTCSession | Subscriber | Notifier` union once those
- * modules are ported to TypeScript.
+ * @remarks
+ * - The three of them expose `ua` publicly only so that the dialog can read
+ * it back out of its owner, which the event based rework removes the need
+ * for. See TODO.md.
*/
-export interface DialogOwner {
- readonly ua: DialogUA;
-
- receiveRequest(request: IncomingRequest): void;
-}
+export type DialogOwner = RTCSession | Subscriber | Notifier;
/**
* Options accepted by `sendRequest()`.
@@ -148,7 +138,7 @@ export class Dialog {
}
#owner: DialogOwner;
- #ua: DialogUA;
+ #ua: UA;
#uac_pending_reply = false;
#uas_pending_reply = false;
@@ -231,7 +221,7 @@ export class Dialog {
/**
* `Dialog/RequestSender` reads it.
*/
- get ua(): DialogUA {
+ get ua(): UA {
return this.#ua;
}
diff --git a/src/Dialog/RequestSender.ts b/src/Dialog/RequestSender.ts
index d7e3d59..5adceb8 100644
--- a/src/Dialog/RequestSender.ts
+++ b/src/Dialog/RequestSender.ts
@@ -1,5 +1,6 @@
+import type { UA } from '../UA';
import * as JsSIP_C from '../Constants';
-import type { Dialog, DialogUA } from '../Dialog';
+import type { Dialog } from '../Dialog';
import { RequestSender } from '../RequestSender';
import type { IncomingResponse, OutgoingRequest } from '../SIPMessage';
import * as Transactions from '../Transactions';
@@ -29,7 +30,7 @@ const DEFAULT_EVENT_HANDLERS: DialogRequestSenderEventHandlers = {
export class DialogRequestSender {
#dialog: Dialog;
- #ua: DialogUA;
+ #ua: UA;
#request: OutgoingRequest;
#eventHandlers: DialogRequestSenderEventHandlers;
diff --git a/src/IncomingMessageBuilder.ts b/src/IncomingMessageBuilder.ts
index 734b98b..47cd9eb 100644
--- a/src/IncomingMessageBuilder.ts
+++ b/src/IncomingMessageBuilder.ts
@@ -1,3 +1,4 @@
+import type { UA } from './UA';
import { Logger } from './Logger';
import * as Grammar from './Grammar';
import NameAddrHeader = require('./NameAddrHeader');
@@ -7,7 +8,6 @@ import {
IncomingMessageParams,
IncomingRequest,
IncomingResponse,
- UALike,
} from './SIPMessage';
import type { IncomingHeaders } from './SIPMessage';
@@ -275,7 +275,7 @@ export class IncomingMessageBuilder {
* missing.
*/
buildRequest(
- ua: UALike,
+ ua: UA,
data: string,
method: string,
ruri: URI,
diff --git a/src/JsSIP.d.ts b/src/JsSIP.d.ts
deleted file mode 100644
index fbf377e..0000000
--- a/src/JsSIP.d.ts
+++ /dev/null
@@ -1,20 +0,0 @@
-import { Debug } from 'debug';
-
-import * as C from './Constants';
-import * as Exceptions from './Exceptions';
-import * as Grammar from './Grammar';
-import * as Utils from './Utils';
-
-export { C, Exceptions, Grammar, Utils };
-
-import URI = require('./URI');
-import NameAddrHeader = require('./NameAddrHeader');
-
-export { UA } from './UA';
-export { URI, NameAddrHeader };
-export { WebSocketInterface } from './WebSocketInterface';
-export type { Socket, WeightedSocket } from './Socket';
-
-export const debug: Debug;
-export const name: string;
-export const version: string;
diff --git a/src/JsSIP.js b/src/JsSIP.js
deleted file mode 100644
index 035cb00..0000000
--- a/src/JsSIP.js
+++ /dev/null
@@ -1,36 +0,0 @@
-const pkg = require('../package.json');
-const C = require('./Constants');
-const Exceptions = require('./Exceptions');
-const Utils = require('./Utils');
-const { UA } = require('./UA');
-const URI = require('./URI');
-const NameAddrHeader = require('./NameAddrHeader');
-const Grammar = require('./Grammar');
-const { WebSocketInterface } = require('./WebSocketInterface');
-const debug = require('debug')('JsSIP');
-const { RTCSession } = require('./RTCSession');
-
-debug('version %s', pkg.version);
-
-/**
- * Expose the JsSIP module.
- */
-module.exports = {
- C,
- Exceptions,
- Utils,
- UA,
- URI,
- NameAddrHeader,
- WebSocketInterface,
- Grammar,
- RTCSession,
- // Expose the debug module.
- debug: require('debug'),
- get name() {
- return pkg.title;
- },
- get version() {
- return pkg.version;
- },
-};
diff --git a/src/JsSIP.ts b/src/JsSIP.ts
new file mode 100644
index 0000000..6adbbe4
--- /dev/null
+++ b/src/JsSIP.ts
@@ -0,0 +1,44 @@
+import debug from 'debug';
+
+import * as C from './Constants';
+import * as Exceptions from './Exceptions';
+import * as Grammar from './Grammar';
+import { RTCSession } from './RTCSession';
+import { UA } from './UA';
+import * as Utils from './Utils';
+import { WebSocketInterface } from './WebSocketInterface';
+import NameAddrHeader = require('./NameAddrHeader');
+import URI = require('./URI');
+
+export { C, Exceptions, Grammar, Utils };
+export { NameAddrHeader, RTCSession, UA, URI, WebSocketInterface };
+export type { Socket, WeightedSocket } from './Socket';
+
+/**
+ * The `debug` module the library logs through, so that an application can
+ * turn the logging on and off.
+ *
+ * @example
+ * ```ts
+ * JsSIP.debug.enable('JsSIP:*');
+ * ```
+ */
+export { debug };
+
+// The two values below are placeholders that `npm-scripts.mjs` rewrites in
+// `lib/` with what `package.json` holds, so that nothing has to read the
+// manifest at run time. Renaming one here without renaming it there would
+// ship the literal text as the value, so the build fails when a placeholder
+// is found in no built file.
+
+/**
+ * Name of the library.
+ */
+export const name: string = '__JSSIP_NAME__';
+
+/**
+ * Version of the library.
+ */
+export const version: string = '__JSSIP_VERSION__';
+
+debug('JsSIP')('version %s', version);
diff --git a/src/Message.ts b/src/Message.ts
index a6fb323..7263e18 100644
--- a/src/Message.ts
+++ b/src/Message.ts
@@ -1,3 +1,4 @@
+import type { UA } from './UA';
import { EventEmitter } from 'events';
import * as JsSIP_C from './Constants';
@@ -5,7 +6,6 @@ import type { Direction, Originator } from './Constants';
import { NotSupportedError } from './Exceptions';
import { Logger } from './Logger';
import { RequestSender } from './RequestSender';
-import type { RequestSenderUA } from './RequestSender';
import * as SIPMessage from './SIPMessage';
import type {
IncomingRequest,
@@ -107,26 +107,6 @@ export type RejectOptions = {
reason_phrase?: string;
};
-/**
- * The `UA` surface a message depends on.
- *
- * TODO: Replace with `import type { UA } from './UA'` once `UA` is ported to
- * TypeScript.
- */
-export interface MessageUA extends RequestSenderUA {
- normalizeTarget(target: string | URI): URI | undefined;
-
- newMessage(
- message: Message,
- data: {
- originator: Originator;
- message: Message;
- request: SIPMessage.OutgoingRequest | IncomingRequest;
- }
- ): void;
- destroyMessage(message: Message): void;
-}
-
/**
* Sends and receives SIP MESSAGE requests, so instant messages outside of a
* session.
@@ -142,7 +122,7 @@ export interface MessageUA extends RequestSenderUA {
* ```
*/
export class Message extends EventEmitter<MessageEventMap> {
- #ua: MessageUA;
+ #ua: UA;
#request: SIPMessage.OutgoingRequest | IncomingRequest | null;
#closed: boolean;
#direction: Direction | null;
@@ -156,7 +136,7 @@ export class Message extends EventEmitter<MessageEventMap> {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
#data: any;
- constructor(ua: MessageUA) {
+ constructor(ua: UA) {
super();
this.#ua = ua;
diff --git a/src/Notifier.ts b/src/Notifier.ts
index bb3af7e..809d0c2 100644
--- a/src/Notifier.ts
+++ b/src/Notifier.ts
@@ -1,8 +1,9 @@
+import type { UA } from './UA';
import { EventEmitter } from 'events';
import * as JsSIP_C from './Constants';
import { Dialog } from './Dialog';
-import type { DialogId, DialogOwner, DialogUA } from './Dialog';
+import type { DialogId } from './Dialog';
import { InvalidStateError } from './Exceptions';
import { Logger } from './Logger';
import type { IncomingRequest } from './SIPMessage';
@@ -113,18 +114,6 @@ export type NotifierOptions = {
defaultExpires?: number;
};
-/**
- * The `UA` surface a notifier depends on.
- *
- * TODO: Replace with `import type { UA } from './UA'` once `UA` is ported to
- * TypeScript.
- */
-export interface NotifierUA extends DialogUA {
- readonly contact: DialogUA['contact'] & {
- toString(): string;
- };
-}
-
/**
* Accepts a subscription and reports the event the peer asked about, per
* [RFC 6665](https://datatracker.ietf.org/doc/html/rfc6665).
@@ -135,10 +124,7 @@ export interface NotifierUA extends DialogUA {
* - Register the listeners first and call {@link Notifier.start} last, since
* `start()` reports the SUBSCRIBE that created the notifier.
*/
-export class Notifier
- extends EventEmitter<NotifierEventMap>
- implements DialogOwner
-{
+export class Notifier extends EventEmitter<NotifierEventMap> {
/**
* Answer a SUBSCRIBE the UA cannot dispatch to an existing dialog,
* invoking the callback when it is one a notifier could serve.
@@ -211,7 +197,7 @@ export class Notifier
*
* @internal
*/
- readonly ua: NotifierUA;
+ readonly ua: UA;
#initial_subscribe: IncomingRequest;
#dialog: Dialog | null = null;
@@ -236,7 +222,7 @@ export class Notifier
* not pass {@link Notifier.checkSubscribe}.
*/
constructor(
- ua: NotifierUA,
+ ua: UA,
subscribe: IncomingRequest,
contentType: string,
{ extraHeaders, allowEvents, pending, defaultExpires }: NotifierOptions = {}
diff --git a/src/Options.ts b/src/Options.ts
index 132f819..5f37d1d 100644
--- a/src/Options.ts
+++ b/src/Options.ts
@@ -1,3 +1,4 @@
+import type { UA } from './UA';
import { EventEmitter } from 'events';
import * as JsSIP_C from './Constants';
@@ -5,7 +6,6 @@ import type { Direction, Originator } from './Constants';
import { NotSupportedError } from './Exceptions';
import { Logger } from './Logger';
import { RequestSender } from './RequestSender';
-import type { RequestSenderUA } from './RequestSender';
import * as SIPMessage from './SIPMessage';
import type { IncomingRequest, IncomingResponse } from './SIPMessage';
import * as Utils from './Utils';
@@ -94,27 +94,6 @@ export type RejectOptions = {
reason_phrase?: string;
};
-/**
- * The `UA` surface an OPTIONS applicant depends on.
- *
- * TODO: Replace with `import type { UA } from './UA'` once `UA` is ported to
- * TypeScript.
- */
-export interface OptionsUA extends RequestSenderUA {
- normalizeTarget(target: string | URI): URI | undefined;
-
- newOptions(
- options: Options,
- data: {
- originator: Originator;
- message: Options;
- request: SIPMessage.OutgoingRequest | IncomingRequest;
- }
- ): void;
-
- destroyOptions(options: Options): void;
-}
-
/**
* SIP OPTIONS capabilities querying.
*
@@ -129,7 +108,7 @@ export interface OptionsUA extends RequestSenderUA {
* ```
*/
export class Options extends EventEmitter<OptionsEventMap> {
- #ua: OptionsUA;
+ #ua: UA;
#request: SIPMessage.OutgoingRequest | IncomingRequest | null;
#closed: boolean;
#direction: Direction | null;
@@ -143,7 +122,7 @@ export class Options extends EventEmitter<OptionsEventMap> {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
#data: any;
- constructor(ua: OptionsUA) {
+ constructor(ua: UA) {
super();
this.#ua = ua;
diff --git a/src/Parser.ts b/src/Parser.ts
index ce848ca..6d2ef65 100644
--- a/src/Parser.ts
+++ b/src/Parser.ts
@@ -1,7 +1,8 @@
+import type { UA } from './UA';
import { Logger } from './Logger';
import * as Grammar from './Grammar';
import { IncomingMessageBuilder } from './IncomingMessageBuilder';
-import type { IncomingRequest, IncomingResponse, UALike } from './SIPMessage';
+import type { IncomingRequest, IncomingResponse } from './SIPMessage';
const logger = new Logger('Parser');
@@ -10,7 +11,7 @@ const logger = new Logger('Parser');
*/
export const parseMessage = (
data: string,
- ua: UALike
+ ua: UA
): IncomingRequest | IncomingResponse | undefined => {
let bodyStart: number;
let headerEnd = data.indexOf('\r\n');
diff --git a/src/RTCSession.ts b/src/RTCSession.ts
index 6a80521..fea5bee 100644
--- a/src/RTCSession.ts
+++ b/src/RTCSession.ts
@@ -4,11 +4,7 @@ import * as sdp_transform from 'sdp-transform';
import * as JsSIP_C from './Constants';
import type { Direction, Originator } from './Constants';
import { Dialog, Status as DialogStatus } from './Dialog';
-import type {
- DialogOwner,
- DialogSendRequestOptions,
- DialogType,
-} from './Dialog';
+import type { DialogSendRequestOptions, DialogType } from './Dialog';
import * as Exceptions from './Exceptions';
import { Logger } from './Logger';
import { RequestSender } from './RequestSender';
@@ -414,10 +410,7 @@ export type RTCSessionEventHandlers = {
[K in keyof RTCSessionEventMap]: (...args: RTCSessionEventMap[K]) => void;
};
-export class RTCSession
- extends EventEmitter<RTCSessionEventMap>
- implements DialogOwner
-{
+export class RTCSession extends EventEmitter<RTCSessionEventMap> {
/**
* The constants above, reachable without the instance.
*/
diff --git a/src/Registrator.ts b/src/Registrator.ts
index 167d98c..dbf84f5 100644
--- a/src/Registrator.ts
+++ b/src/Registrator.ts
@@ -1,7 +1,7 @@
+import type { UA } from './UA';
import * as JsSIP_C from './Constants';
import { Logger } from './Logger';
import { RequestSender } from './RequestSender';
-import type { RequestSenderUA } from './RequestSender';
import * as SIPMessage from './SIPMessage';
import type { IncomingResponse } from './SIPMessage';
import * as Utils from './Utils';
@@ -48,33 +48,6 @@ export type UnregisteredEvent = {
cause?: JsSIP_C.causes | null;
};
-/**
- * The `UA` surface a registrator depends on.
- *
- * TODO: Replace with `import type { UA } from './UA'` once `UA` is ported to
- * TypeScript.
- */
-export interface RegistratorUA extends RequestSenderUA {
- readonly configuration: RequestSenderUA['configuration'] & {
- registrar_server: URI;
- register_expires: number;
- instance_id: string | null;
- register_from_tag_trail: string | (() => string);
- };
- readonly contact: RequestSenderUA['contact'] & {
- uri: URI;
- toString(): string;
- };
-
- listeners(event: string): unknown[];
- emit(event: string): boolean;
-
- registered(data: RegisteredEvent): void;
- registrationRefreshed(data: RegisteredEvent): void;
- registrationFailed(data: UnregisteredEvent): void;
- unregistered(data: UnregisteredEvent): void;
-}
-
/**
* Manages the UA registration procedures.
*
@@ -82,7 +55,7 @@ export interface RegistratorUA extends RequestSenderUA {
* - Reachable through `ua.registrator`. Applications do not build one.
*/
export class Registrator {
- #ua: RegistratorUA;
+ #ua: UA;
#registrar: URI;
#expires: number;
#call_id: string;
@@ -99,7 +72,7 @@ export class Registrator {
// Forced to 1.
readonly #reg_id = 1;
- constructor(ua: RegistratorUA) {
+ constructor(ua: UA) {
this.#ua = ua;
this.#registrar = ua.configuration.registrar_server;
diff --git a/src/RequestSender.ts b/src/RequestSender.ts
index 2a7daf6..3d5cbcf 100644
--- a/src/RequestSender.ts
+++ b/src/RequestSender.ts
@@ -1,3 +1,4 @@
+import type { UA } from './UA';
import * as JsSIP_C from './Constants';
import { DigestAuthentication } from './DigestAuthentication';
import type { DigestChallenge } from './DigestAuthentication';
@@ -7,26 +8,6 @@ import * as Transactions from './Transactions';
const logger = new Logger('RequestSender');
-/**
- * The `UA` surface a request sender depends on.
- *
- * TODO: Replace with `import type { UA } from './UA'` once `UA` is ported to
- * TypeScript.
- */
-export interface RequestSenderUA extends Transactions.TransactionUA {
- readonly C: { STATUS_USER_CLOSED: number };
- readonly status: number;
- readonly configuration: Transactions.TransactionUA['configuration'] & {
- authorization_user: string | null;
- password: string | null;
- realm: string | null;
- ha1: string | null;
- authorization_jwt: string | null;
- };
-
- set(parameter: string, value: unknown): boolean;
-}
-
/**
* The callbacks a request sender reports to the applicant. Every one of them is
* optional; the ones left out fall back to `DEFAULT_EVENT_HANDLERS`.
@@ -63,7 +44,7 @@ export class RequestSender {
*/
clientTransaction?: ClientTransaction;
- #ua: RequestSenderUA;
+ #ua: UA;
#eventHandlers: RequestSenderEventHandlers;
#method: string;
#request: OutgoingRequest;
@@ -72,7 +53,7 @@ export class RequestSender {
#staled = false;
constructor(
- ua: RequestSenderUA,
+ ua: UA,
request: OutgoingRequest,
eventHandlers: Partial<RequestSenderEventHandlers>
) {
diff --git a/src/SIPMessage.ts b/src/SIPMessage.ts
index bbd2781..48a93ff 100644
--- a/src/SIPMessage.ts
+++ b/src/SIPMessage.ts
@@ -1,3 +1,4 @@
+import type { UA } from './UA';
import type { EventEmitter } from 'events';
import * as JsSIP_C from './Constants';
@@ -173,37 +174,6 @@ export interface ServerTransactionLike extends EventEmitter {
): void;
}
-/**
- * The `UA` surface SIP messages depend on.
- *
- * TODO: Replace with `import type { UA } from './UA'` once `UA` is ported to
- * TypeScript. Note that `UA.d.ts` currently declares neither `configuration`
- * nor `transport`, even though `UA` exposes both as public getters, so that
- * gap must be closed first.
- *
- * TODO: The `configuration` shape below is `Config.Configuration` plus the
- * `jssip_id` that `UA` adds, so it can only be replaced once `UA` is ported.
- * Note it is not the `UAConfiguration` input type declared in `UA.d.ts`.
- *
- * @internal
- */
-export interface UALike {
- readonly configuration: {
- uri: URI;
- display_name?: string | null;
- jssip_id: string;
- session_timers?: boolean;
- use_preloaded_route?: boolean;
- user_agent?: string | null;
- extra_headers?: string[] | null;
- };
- readonly contact: {
- pub_gruu?: string | null;
- temp_gruu?: string | null;
- };
- readonly transport: Transport;
-}
-
/**
* Holds a SIP request to be sent.
*/
@@ -211,7 +181,7 @@ export class OutgoingRequest {
/**
* The UA that sends the request.
*/
- ua: UALike;
+ ua: UA;
/**
* Header fields set by the library, indexed by their capitalized name. A
* header field may appear several times, hence the array.
@@ -280,7 +250,7 @@ export class OutgoingRequest {
constructor(
method: string,
ruri: URI,
- ua: UALike,
+ ua: UA,
params?: OutgoingRequestParams | null,
extraHeaders?: string[] | null,
body?: string
@@ -591,7 +561,7 @@ export class InitialOutgoingInviteRequest extends OutgoingRequest {
constructor(
ruri: URI,
- ua: UALike,
+ ua: UA,
params?: OutgoingRequestParams | null,
extraHeaders?: string[] | null,
body?: string
@@ -728,7 +698,7 @@ export type IncomingRequestParams = IncomingMessageParams & {
/**
* The UA that received the request.
*/
- ua: UALike;
+ ua: UA;
/**
* Request-URI, so the address the request was sent to.
*/
@@ -1107,7 +1077,7 @@ export class IncomingRequest extends IncomingMessage {
/**
* The UA that received the request.
*/
- ua: UALike;
+ ua: UA;
/**
* Request URI of the SIP message.
*/
diff --git a/src/Subscriber.ts b/src/Subscriber.ts
index 07a98dd..f45e1c9 100644
--- a/src/Subscriber.ts
+++ b/src/Subscriber.ts
@@ -1,8 +1,9 @@
+import type { UA } from './UA';
import { EventEmitter } from 'events';
import * as JsSIP_C from './Constants';
import { Dialog } from './Dialog';
-import type { DialogId, DialogOwner, DialogUA } from './Dialog';
+import type { DialogId } from './Dialog';
import { InvalidStateError } from './Exceptions';
import * as Grammar from './Grammar';
import { Logger } from './Logger';
@@ -156,22 +157,6 @@ export type SubscriberOptions = {
extraHeaders?: string[] | null;
};
-/**
- * The `UA` surface a subscriber depends on.
- *
- * TODO: Replace with `import type { UA } from './UA'` once `UA` is ported to
- * TypeScript.
- */
-export interface SubscriberUA extends DialogUA {
- readonly contact: DialogUA['contact'] & {
- toString(): string;
- };
-
- normalizeTarget(target: string | URI): URI | undefined;
- newSubscriber(subscriber: Subscriber): void;
- destroySubscriber(subscriber: Subscriber): void;
-}
-
// A SUBSCRIBE held back until the dialog exists.
type QueuedSubscribe = {
body: string | null;
@@ -187,10 +172,7 @@ type QueuedSubscribe = {
* - Applications get one from `UA.subscribe()`, and nothing is sent until
* {@link Subscriber.subscribe} is called.
*/
-export class Subscriber
- extends EventEmitter<SubscriberEventMap>
- implements DialogOwner
-{
+export class Subscriber extends EventEmitter<SubscriberEventMap> {
/**
* The constants above, reachable without the instance.
*/
@@ -203,7 +185,7 @@ export class Subscriber
*
* @internal
*/
- readonly ua: SubscriberUA;
+ readonly ua: UA;
#target: URI;
#expires: number;
@@ -249,7 +231,7 @@ export class Subscriber
* is not a valid target.
*/
constructor(
- ua: SubscriberUA,
+ ua: UA,
target: string | URI,
eventName: string,
accept: string,
diff --git a/src/Transactions.ts b/src/Transactions.ts
index c6d2cf1..83e9e93 100644
--- a/src/Transactions.ts
+++ b/src/Transactions.ts
@@ -1,3 +1,4 @@
+import type { UA } from './UA';
import { EventEmitter } from 'events';
import * as JsSIP_C from './Constants';
@@ -64,27 +65,10 @@ export interface TransactionRegistry {
ist: Record<string, InviteServerTransaction>;
}
-/**
- * The `UA` surface transactions depend on.
- *
- * TODO: Replace with `import type { UA } from './UA'` once `UA` is ported to
- * TypeScript. It is declared structurally (on top of the `UALike` that
- * `SIPMessage` already needs) so that this module does not depend on the
- * still JavaScript `UA` module.
- */
-export interface TransactionUA extends SIPMessage.UALike {
- readonly configuration: SIPMessage.UALike['configuration'] & {
- via_host: string;
- };
-
- newTransaction(transaction: Transaction): void;
- destroyTransaction(transaction: Transaction): void;
-}
-
export class NonInviteClientTransaction extends EventEmitter {
type: Type;
id: string;
- ua: TransactionUA;
+ ua: UA;
transport: Transport;
request: SIPMessage.OutgoingRequest;
eventHandlers: ClientTransactionEventHandlers;
@@ -98,7 +82,7 @@ export class NonInviteClientTransaction extends EventEmitter {
K?: ReturnType<typeof setTimeout>;
constructor(
- ua: TransactionUA,
+ ua: UA,
transport: Transport,
request: SIPMessage.OutgoingRequest,
eventHandlers: ClientTransactionEventHandlers
@@ -201,7 +185,7 @@ export class NonInviteClientTransaction extends EventEmitter {
export class InviteClientTransaction extends EventEmitter {
type: Type;
id: string;
- ua: TransactionUA;
+ ua: UA;
transport: Transport;
request: SIPMessage.OutgoingRequest;
eventHandlers: ClientTransactionEventHandlers;
@@ -216,7 +200,7 @@ export class InviteClientTransaction extends EventEmitter {
M?: ReturnType<typeof setTimeout>;
constructor(
- ua: TransactionUA,
+ ua: UA,
transport: Transport,
request: SIPMessage.OutgoingRequest,
eventHandlers: ClientTransactionEventHandlers
@@ -409,7 +393,7 @@ export class AckClientTransaction extends EventEmitter {
eventHandlers: ClientTransactionEventHandlers;
constructor(
- ua: TransactionUA,
+ ua: UA,
transport: Transport,
request: SIPMessage.OutgoingRequest,
eventHandlers: ClientTransactionEventHandlers
@@ -443,7 +427,7 @@ export class AckClientTransaction extends EventEmitter {
export class NonInviteServerTransaction extends EventEmitter {
type: Type;
id: string;
- ua: TransactionUA;
+ ua: UA;
transport: Transport;
request: SIPMessage.IncomingRequest;
last_response: string;
@@ -457,7 +441,7 @@ export class NonInviteServerTransaction extends EventEmitter {
J?: ReturnType<typeof setTimeout>;
constructor(
- ua: TransactionUA,
+ ua: UA,
transport: Transport,
request: SIPMessage.IncomingRequest
) {
@@ -561,7 +545,7 @@ export class NonInviteServerTransaction extends EventEmitter {
export class InviteServerTransaction extends EventEmitter {
type: Type;
id: string;
- ua: TransactionUA;
+ ua: UA;
transport: Transport;
request: SIPMessage.IncomingRequest;
last_response: string;
@@ -578,7 +562,7 @@ export class InviteServerTransaction extends EventEmitter {
L?: ReturnType<typeof setTimeout>;
constructor(
- ua: TransactionUA,
+ ua: UA,
transport: Transport,
request: SIPMessage.IncomingRequest
) {
diff --git a/src/UA.ts b/src/UA.ts
index dd0c91b..91bc410 100644
--- a/src/UA.ts
+++ b/src/UA.ts
@@ -1096,8 +1096,11 @@ export class UA extends EventEmitter<UAEventMap> {
replaces.from_tag,
replaces.to_tag
);
- if (dialog) {
- session = dialog.owner as RTCSession;
+ // Only a call can be replaced. A Replaces naming the
+ // dialog of a subscription is answered as the one it
+ // asks for not existing.
+ if (dialog?.owner instanceof RTCSession) {
+ session = dialog.owner;
if (!session.isEnded()) {
session.receiveRequest(request);
} else {
diff --git a/src/sanityCheck.ts b/src/sanityCheck.ts
index 2c1a933..d704f31 100644
--- a/src/sanityCheck.ts
+++ b/src/sanityCheck.ts
@@ -1,23 +1,13 @@
+import type { UA } from './UA';
import * as JsSIP_C from './Constants';
import { Logger } from './Logger';
import { IncomingRequest, IncomingResponse } from './SIPMessage';
-import type { IncomingMessage, UALike } from './SIPMessage';
-import type { TransactionRegistry } from './Transactions';
+import type { IncomingMessage } from './SIPMessage';
import type { Transport } from './Transport';
import * as Utils from './Utils';
const logger = new Logger('sanityCheck');
-/**
- * The `UA` surface the sanity checks depend on.
- *
- * TODO: Replace with `import type { UA } from './UA'` once `UA` is ported to
- * TypeScript.
- */
-export interface SanityCheckUA extends UALike {
- readonly transactions: TransactionRegistry;
-}
-
/**
* A single check. Returns `false` to drop the message, `true` to let it
* through.
@@ -25,7 +15,7 @@ export interface SanityCheckUA extends UALike {
type Check<M extends IncomingMessage> = (
message: M,
transport: Transport,
- ua: SanityCheckUA
+ ua: UA
) => boolean;
// Checks for requests and responses.
@@ -47,7 +37,7 @@ const responses: Check<IncomingResponse>[] = [
export const sanityCheck = (
message: IncomingRequest | IncomingResponse,
- ua: SanityCheckUA,
+ ua: UA,
transport: Transport
): boolean => {
for (const check of all) {
@@ -108,7 +98,7 @@ function rfc3261_8_2_2_1(
function rfc3261_16_3_4(
message: IncomingRequest,
transport: Transport,
- ua: SanityCheckUA
+ ua: UA
): boolean {
if (!message.to_tag) {
if (message.call_id?.substring(0, 5) === ua.configuration.jssip_id) {
@@ -140,7 +130,7 @@ function rfc3261_18_3_request(
function rfc3261_8_2_2_2(
message: IncomingRequest,
transport: Transport,
- ua: SanityCheckUA
+ ua: UA
): boolean {
const fromTag = message.from_tag;
const call_id = message.call_id;
diff --git a/src/test/test-Dialog.ts b/src/test/test-Dialog.ts
index c8d9fdf..f136cd2 100644
--- a/src/test/test-Dialog.ts
+++ b/src/test/test-Dialog.ts
@@ -7,11 +7,11 @@ import TestSocket from './include/TestSocket';
import { startUA, stopUA } from './include/ua';
import { Dialog, DialogError, DialogErrorReason } from '../Dialog';
-import type { DialogOwner, DialogUA } from '../Dialog';
+import type { DialogOwner } from '../Dialog';
import { UA, WebSocketInterface } from '../JsSIP';
import type { Notifier } from '../Notifier';
import * as Parser from '../Parser';
-import type { IncomingMessage, UALike } from '../SIPMessage';
+import type { IncomingMessage } from '../SIPMessage';
const AOR = 'sip:alice@example.com';
const REMOTE = '<sip:bob@example.com>';
@@ -22,15 +22,15 @@ describe('Dialog.Create()', () => {
// that `Create()` registers them with.
const dialogs: Dialog[] = [];
- // `satisfies` rather than a cast over the whole object, so that the
- // members `DialogOwner` names stay checked. Only the breadth of `DialogUA`
- // is stubbed out.
+ // `DialogOwner` is the union of the three real usages, so a stand-in has
+ // to be cast. It carries only what `Create()` reads of its owner, which
+ // is the UA to register the new dialog with. That the usages really do
+ // provide it is checked where each of them calls `Create()`.
const owner = {
ua: {
newDialog: (dialog: Dialog) => dialogs.push(dialog),
- } as unknown as DialogUA,
- receiveRequest: () => {},
- } satisfies DialogOwner;
+ },
+ } as unknown as DialogOwner;
let ua: UA;
@@ -55,9 +55,7 @@ describe('Dialog.Create()', () => {
extraHeaders: ['Event: weather', 'Expires: 3600', ...fields],
});
- // A started UA does satisfy UALike, but no exported type says so: see
- // the TODO on 'Configuration', which UA mutates after loading it.
- const message = Parser.parseMessage(data, ua as unknown as UALike);
+ const message = Parser.parseMessage(data, ua);
if (!message) {
throw new Error('parseMessage() did not return a message');
diff --git a/src/test/test-JsSIP.ts b/src/test/test-JsSIP.ts
index 4314628..71536ea 100644
--- a/src/test/test-JsSIP.ts
+++ b/src/test/test-JsSIP.ts
@@ -1,16 +1,46 @@
import './include/common';
-import { name, version } from '../JsSIP';
+import * as JsSIP from '../JsSIP';
-// eslint-disable-next-line @typescript-eslint/no-require-imports
-const pkg = require('../../package.json');
-
-describe('Properties', () => {
- test('should have a name property', () => {
- expect(name).toEqual(pkg.title);
+describe('the JsSIP module', () => {
+ /* NOTE: `name` and `version` are placeholders in the sources, which the
+ * build writes the `package.json` values into. Only their presence can be
+ * checked here; that the values arrive is checked by the build itself,
+ * which fails when a placeholder reaches no built file.
+ */
+ test('exposes everything an application reaches for', () => {
+ expect(Object.keys(JsSIP).sort()).toEqual([
+ 'C',
+ 'Exceptions',
+ 'Grammar',
+ 'NameAddrHeader',
+ 'RTCSession',
+ 'UA',
+ 'URI',
+ 'Utils',
+ 'WebSocketInterface',
+ 'debug',
+ 'name',
+ 'version',
+ ]);
});
- test('should have a version property', () => {
- expect(version).toEqual(pkg.version);
+ test('the classes are constructible and the namespaces filled', () => {
+ expect(typeof JsSIP.UA).toBe('function');
+ expect(typeof JsSIP.RTCSession).toBe('function');
+ expect(typeof JsSIP.WebSocketInterface).toBe('function');
+
+ expect(new JsSIP.URI('sip', 'alice', 'example.com').toString()).toBe(
+ 'sip:alice@example.com'
+ );
+
+ expect(JsSIP.C.causes.BUSY).toBe('Busy');
+ expect(typeof JsSIP.Grammar.parse).toBe('function');
+ expect(typeof JsSIP.Utils.newTag).toBe('function');
+ expect(typeof JsSIP.Exceptions.InvalidStateError).toBe('function');
+
+ // The `debug` factory itself, so that an application can turn the
+ // logging on.
+ expect(typeof JsSIP.debug.enable).toBe('function');
});
});
diff --git a/src/test/test-parser.ts b/src/test/test-parser.ts
index 5af6fb4..cf4abd8 100644
--- a/src/test/test-parser.ts
+++ b/src/test/test-parser.ts
@@ -4,7 +4,6 @@ import * as consts from './include/consts';
import { Grammar, NameAddrHeader, UA, URI, WebSocketInterface } from '../JsSIP';
import * as Parser from '../Parser';
import { expectDefined } from './include/expect';
-import type { UALike } from '../SIPMessage';
describe('parser', () => {
test('parse URI', () => {
@@ -434,9 +433,7 @@ P-Preferred-Identity: "Cullen Jennings" <sip:fluffy@cisco.com>\r\n\r\n';
})
);
- // A started UA does satisfy UALike, but no exported type says so: see the
- // TODO on 'Configuration', which UA mutates after loading it.
- const message = Parser.parseMessage(data, ua as unknown as UALike);
+ const message = Parser.parseMessage(data, ua);
if (!message) {
throw new Error('parseMessage() did not return a message');
diff --git a/typedoc.config.mjs b/typedoc.config.mjs
index a577849..8f8dd85 100644
--- a/typedoc.config.mjs
+++ b/typedoc.config.mjs
@@ -46,6 +46,7 @@ const config = {
intentionallyNotExported: [
// Ported but not documented yet.
'Transport',
+ 'UA',
// Declared in the hand written `Grammar.d.ts`, which is excluded above.
'Grammar',
'ViaHeader',