sip.js
Version:
A SIP library for JavaScript
139 lines (138 loc) • 3.59 kB
JavaScript
/**
* SIP Response Reasons
* DOC: http://www.iana.org/assignments/sip-parameters
* @internal
*/
const REASON_PHRASE = {
100: "Trying",
180: "Ringing",
181: "Call Is Being Forwarded",
182: "Queued",
183: "Session Progress",
199: "Early Dialog Terminated",
200: "OK",
202: "Accepted",
204: "No Notification",
300: "Multiple Choices",
301: "Moved Permanently",
302: "Moved Temporarily",
305: "Use Proxy",
380: "Alternative Service",
400: "Bad Request",
401: "Unauthorized",
402: "Payment Required",
403: "Forbidden",
404: "Not Found",
405: "Method Not Allowed",
406: "Not Acceptable",
407: "Proxy Authentication Required",
408: "Request Timeout",
410: "Gone",
412: "Conditional Request Failed",
413: "Request Entity Too Large",
414: "Request-URI Too Long",
415: "Unsupported Media Type",
416: "Unsupported URI Scheme",
417: "Unknown Resource-Priority",
420: "Bad Extension",
421: "Extension Required",
422: "Session Interval Too Small",
423: "Interval Too Brief",
428: "Use Identity Header",
429: "Provide Referrer Identity",
430: "Flow Failed",
433: "Anonymity Disallowed",
436: "Bad Identity-Info",
437: "Unsupported Certificate",
438: "Invalid Identity Header",
439: "First Hop Lacks Outbound Support",
440: "Max-Breadth Exceeded",
469: "Bad Info Package",
470: "Consent Needed",
478: "Unresolvable Destination",
480: "Temporarily Unavailable",
481: "Call/Transaction Does Not Exist",
482: "Loop Detected",
483: "Too Many Hops",
484: "Address Incomplete",
485: "Ambiguous",
486: "Busy Here",
487: "Request Terminated",
488: "Not Acceptable Here",
489: "Bad Event",
491: "Request Pending",
493: "Undecipherable",
494: "Security Agreement Required",
500: "Internal Server Error",
501: "Not Implemented",
502: "Bad Gateway",
503: "Service Unavailable",
504: "Server Time-out",
505: "Version Not Supported",
513: "Message Too Large",
580: "Precondition Failure",
600: "Busy Everywhere",
603: "Decline",
604: "Does Not Exist Anywhere",
606: "Not Acceptable"
};
/**
* @param size -
* @param base -
* @internal
*/
export function createRandomToken(size, base = 32) {
let token = "";
for (let i = 0; i < size; i++) {
const r = Math.floor(Math.random() * base);
token += r.toString(base);
}
return token;
}
/**
* @internal
*/
export function getReasonPhrase(code) {
return REASON_PHRASE[code] || "";
}
/**
* @internal
*/
export function newTag() {
return createRandomToken(10);
}
/**
* @param str -
* @internal
*/
export function headerize(str) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const exceptions = {
"Call-Id": "Call-ID",
Cseq: "CSeq",
"Min-Se": "Min-SE",
Rack: "RAck",
Rseq: "RSeq",
"Www-Authenticate": "WWW-Authenticate"
};
const name = str.toLowerCase().replace(/_/g, "-").split("-");
const parts = name.length;
let hname = "";
for (let part = 0; part < parts; part++) {
if (part !== 0) {
hname += "-";
}
hname += name[part].charAt(0).toUpperCase() + name[part].substring(1);
}
if (exceptions[hname]) {
hname = exceptions[hname];
}
return hname;
}
/**
* @param str -
* @internal
*/
export function utf8Length(str) {
return encodeURIComponent(str).replace(/%[A-F\d]{2}/g, "U").length;
}