@routr/common 2.16.0 → 2.19.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (89) hide show
  1. package/README.md +1 -1
  2. package/dist/connect/config.d.ts +5 -1
  3. package/dist/connect/mappers/agent.js +1 -0
  4. package/dist/connect/mappers/domain.js +1 -0
  5. package/dist/connect/mappers/peer.js +1 -0
  6. package/dist/connect/mappers/trunk.js +1 -0
  7. package/dist/connect/protos/agents.proto +7 -0
  8. package/dist/connect/protos/domains.proto +7 -0
  9. package/dist/connect/protos/peers.proto +7 -0
  10. package/dist/connect/protos/trace.proto +28 -0
  11. package/dist/connect/protos/trunks.proto +7 -0
  12. package/dist/connect/schemas/agent.d.ts +10 -0
  13. package/dist/connect/schemas/agent.js +8 -0
  14. package/dist/connect/schemas/domain.d.ts +10 -0
  15. package/dist/connect/schemas/domain.js +8 -0
  16. package/dist/connect/schemas/peer.d.ts +10 -0
  17. package/dist/connect/schemas/peer.js +8 -0
  18. package/dist/connect/schemas/trunk.d.ts +10 -0
  19. package/dist/connect/schemas/trunk.js +8 -0
  20. package/dist/connect/types.d.ts +7 -0
  21. package/dist/index.d.ts +1 -0
  22. package/dist/index.js +2 -1
  23. package/dist/protos/processor.proto +4 -0
  24. package/dist/trace/compat.d.ts +37 -0
  25. package/dist/trace/compat.js +131 -0
  26. package/dist/trace/diff.d.ts +48 -0
  27. package/dist/trace/diff.js +152 -0
  28. package/dist/trace/index.d.ts +11 -0
  29. package/dist/trace/index.js +68 -0
  30. package/dist/trace/metadata.d.ts +28 -0
  31. package/dist/trace/metadata.js +75 -0
  32. package/dist/trace/normalize.d.ts +11 -0
  33. package/dist/trace/normalize.js +39 -0
  34. package/dist/trace/redact.d.ts +19 -0
  35. package/dist/trace/redact.js +52 -0
  36. package/dist/trace/rules/facts.d.ts +43 -0
  37. package/dist/trace/rules/facts.js +117 -0
  38. package/dist/trace/rules/index.d.ts +34 -0
  39. package/dist/trace/rules/index.js +153 -0
  40. package/dist/trace/rules/sip/ack-2xx.d.ts +2 -0
  41. package/dist/trace/rules/sip/ack-2xx.js +56 -0
  42. package/dist/trace/rules/sip/cancel-matches.d.ts +2 -0
  43. package/dist/trace/rules/sip/cancel-matches.js +46 -0
  44. package/dist/trace/rules/sip/credentials-stripped.d.ts +2 -0
  45. package/dist/trace/rules/sip/credentials-stripped.js +41 -0
  46. package/dist/trace/rules/sip/cseq-ack.d.ts +2 -0
  47. package/dist/trace/rules/sip/cseq-ack.js +40 -0
  48. package/dist/trace/rules/sip/cseq-monotonic.d.ts +2 -0
  49. package/dist/trace/rules/sip/cseq-monotonic.js +50 -0
  50. package/dist/trace/rules/sip/max-forwards.d.ts +2 -0
  51. package/dist/trace/rules/sip/max-forwards.js +47 -0
  52. package/dist/trace/rules/sip/private-address.d.ts +2 -0
  53. package/dist/trace/rules/sip/private-address.js +48 -0
  54. package/dist/trace/rules/sip/record-route-self.d.ts +2 -0
  55. package/dist/trace/rules/sip/record-route-self.js +46 -0
  56. package/dist/trace/rules/sip/register-ok.d.ts +2 -0
  57. package/dist/trace/rules/sip/register-ok.js +52 -0
  58. package/dist/trace/rules/sip/retransmission.d.ts +2 -0
  59. package/dist/trace/rules/sip/retransmission.js +47 -0
  60. package/dist/trace/rules/sip/route-self-removed.d.ts +2 -0
  61. package/dist/trace/rules/sip/route-self-removed.js +38 -0
  62. package/dist/trace/rules/sip/via.d.ts +2 -0
  63. package/dist/trace/rules/sip/via.js +50 -0
  64. package/dist/trace/rules/sip-connect/from-e164-or-anonymous.d.ts +2 -0
  65. package/dist/trace/rules/sip-connect/from-e164-or-anonymous.js +43 -0
  66. package/dist/trace/rules/sip-connect/pai-e164.d.ts +2 -0
  67. package/dist/trace/rules/sip-connect/pai-e164.js +44 -0
  68. package/dist/trace/rules/sip-connect/pai-single.d.ts +2 -0
  69. package/dist/trace/rules/sip-connect/pai-single.js +41 -0
  70. package/dist/trace/rules/sip-connect/privacy.d.ts +2 -0
  71. package/dist/trace/rules/sip-connect/privacy.js +50 -0
  72. package/dist/trace/rules/sip-connect/request-uri-e164.d.ts +2 -0
  73. package/dist/trace/rules/sip-connect/request-uri-e164.js +44 -0
  74. package/dist/trace/rules/sip-connect/scope.d.ts +11 -0
  75. package/dist/trace/rules/sip-connect/scope.js +19 -0
  76. package/dist/trace/rules/types.d.ts +34 -0
  77. package/dist/trace/rules/types.js +13 -0
  78. package/dist/trace/sanitize.d.ts +40 -0
  79. package/dist/trace/sanitize.js +85 -0
  80. package/dist/trace/schema.d.ts +338 -0
  81. package/dist/trace/schema.js +197 -0
  82. package/dist/trace/status.d.ts +15 -0
  83. package/dist/trace/status.js +102 -0
  84. package/dist/trace/types.d.ts +126 -0
  85. package/dist/trace/types.js +82 -0
  86. package/dist/trace/validate.d.ts +56 -0
  87. package/dist/trace/validate.js +140 -0
  88. package/dist/types.d.ts +2 -0
  89. package/package.json +2 -2
@@ -0,0 +1,48 @@
1
+ import { Address, SIPMessage, SipURI, Via } from "../types";
2
+ import { MessageDiff } from "./types";
3
+ /**
4
+ * Renders a SIP URI the way it appears on the wire.
5
+ *
6
+ * @param {SipURI} uri - Parsed URI
7
+ * @return {string} e.g. sip:+1555@sip.provider.net:5060;transport=udp;user=phone
8
+ */
9
+ export declare function formatUri(uri?: SipURI): string;
10
+ /**
11
+ * Renders a name-addr, e.g. "John" <sip:1001@sip.local>.
12
+ *
13
+ * @param {Address} address - Parsed address
14
+ * @return {string} The rendering
15
+ */
16
+ export declare function formatAddress(address?: Address): string;
17
+ /**
18
+ * Renders a Via header value.
19
+ *
20
+ * @param {Via} via - Parsed Via
21
+ * @return {string} e.g. SIP/2.0/UDP 10.0.0.1:5060;branch=z9hG4bK-1;rport
22
+ */
23
+ export declare function formatVia(via: Via): string;
24
+ /**
25
+ * Flattens a message into header name → values, rendered as on the wire.
26
+ * Multi-valued headers (Via, Route, Record-Route) keep one entry per line.
27
+ * The body is summarized by its media lines so SDP changes show up.
28
+ *
29
+ * @param {SIPMessage} message - Parsed message
30
+ * @return {Record<string, string[]>} Headers by canonical name
31
+ */
32
+ export declare function flattenHeaders(message: SIPMessage): Record<string, string[]>;
33
+ /**
34
+ * Computes what a component changed between the message it received and
35
+ * the one it returned.
36
+ *
37
+ * @param {SIPMessage} before - Message as it entered the component
38
+ * @param {SIPMessage} after - Message as it left the component
39
+ * @return {MessageDiff} Added, removed and changed headers
40
+ */
41
+ export declare function diffMessages(before: SIPMessage, after: SIPMessage): MessageDiff;
42
+ /**
43
+ * True when a diff carries no change at all.
44
+ *
45
+ * @param {MessageDiff} diff - A diff
46
+ * @return {boolean} Whether it is empty
47
+ */
48
+ export declare function isEmptyDiff(diff: MessageDiff): boolean;
@@ -0,0 +1,152 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.formatUri = formatUri;
4
+ exports.formatAddress = formatAddress;
5
+ exports.formatVia = formatVia;
6
+ exports.flattenHeaders = flattenHeaders;
7
+ exports.diffMessages = diffMessages;
8
+ exports.isEmptyDiff = isEmptyDiff;
9
+ /**
10
+ * Renders a SIP URI the way it appears on the wire.
11
+ *
12
+ * @param {SipURI} uri - Parsed URI
13
+ * @return {string} e.g. sip:+1555@sip.provider.net:5060;transport=udp;user=phone
14
+ */
15
+ function formatUri(uri) {
16
+ if (!uri)
17
+ return "";
18
+ const scheme = uri.secure ? "sips" : "sip";
19
+ const user = uri.user ? `${uri.user}@` : "";
20
+ const port = uri.port ? `:${uri.port}` : "";
21
+ const params = [
22
+ uri.transportParam ? `transport=${uri.transportParam.toLowerCase()}` : "",
23
+ uri.userParam ? `user=${uri.userParam}` : "",
24
+ uri.lrParam ? "lr" : "",
25
+ uri.methodParam ? `method=${uri.methodParam}` : ""
26
+ ]
27
+ .filter(Boolean)
28
+ .map((p) => `;${p}`)
29
+ .join("");
30
+ return `${scheme}:${user}${uri.host}${port}${params}`;
31
+ }
32
+ /**
33
+ * Renders a name-addr, e.g. "John" <sip:1001@sip.local>.
34
+ *
35
+ * @param {Address} address - Parsed address
36
+ * @return {string} The rendering
37
+ */
38
+ function formatAddress(address) {
39
+ if (!address)
40
+ return "";
41
+ const name = address.displayName ? `"${address.displayName}" ` : "";
42
+ return `${name}<${formatUri(address.uri)}>`;
43
+ }
44
+ /**
45
+ * Renders a Via header value.
46
+ *
47
+ * @param {Via} via - Parsed Via
48
+ * @return {string} e.g. SIP/2.0/UDP 10.0.0.1:5060;branch=z9hG4bK-1;rport
49
+ */
50
+ function formatVia(via) {
51
+ const params = [
52
+ via.branch ? `branch=${via.branch}` : "",
53
+ via.received ? `received=${via.received}` : "",
54
+ via.rPort ? `rport=${via.rPort}` : via.rPortFlag ? "rport" : ""
55
+ ]
56
+ .filter(Boolean)
57
+ .map((p) => `;${p}`)
58
+ .join("");
59
+ return `SIP/2.0/${via.transport} ${via.host}:${via.port}${params}`;
60
+ }
61
+ const withParams = (address, parameters) => formatAddress(address) +
62
+ Object.entries(parameters || {})
63
+ .map(([k, v]) => (v === "" ? `;${k}` : `;${k}=${v}`))
64
+ .join("");
65
+ /**
66
+ * Flattens a message into header name → values, rendered as on the wire.
67
+ * Multi-valued headers (Via, Route, Record-Route) keep one entry per line.
68
+ * The body is summarized by its media lines so SDP changes show up.
69
+ *
70
+ * @param {SIPMessage} message - Parsed message
71
+ * @return {Record<string, string[]>} Headers by canonical name
72
+ */
73
+ function flattenHeaders(message) {
74
+ var _a, _b;
75
+ const headers = {};
76
+ const put = (name, value) => {
77
+ if (value === undefined || value === null || value === "")
78
+ return;
79
+ headers[name] = (headers[name] || []).concat(String(value));
80
+ };
81
+ (message.via || []).forEach((via) => put("Via", formatVia(via)));
82
+ (message.route || []).forEach((r) => put("Route", withParams(r.address, r.parameters)));
83
+ (message.recordRoute || []).forEach((r) => put("Record-Route", withParams(r.address, r.parameters)));
84
+ if (message.from) {
85
+ put("From", withParams(message.from.address, Object.assign(Object.assign({}, (message.from.tag ? { tag: message.from.tag } : {})), message.from.parameters)));
86
+ }
87
+ if (message.to) {
88
+ put("To", withParams(message.to.address, Object.assign(Object.assign({}, (message.to.tag ? { tag: message.to.tag } : {})), message.to.parameters)));
89
+ }
90
+ if (message.contact)
91
+ put("Contact", formatAddress(message.contact.address));
92
+ put("Max-Forwards", (_a = message.maxForwards) === null || _a === void 0 ? void 0 : _a.maxForwards);
93
+ put("Expires", (_b = message.expires) === null || _b === void 0 ? void 0 : _b.expires);
94
+ if (message.authorization) {
95
+ put("Authorization", `Digest username="${message.authorization.username}", realm="${message.authorization.realm}"`);
96
+ }
97
+ if (message.wwwAuthenticate) {
98
+ put("WWW-Authenticate", `Digest realm="${message.wwwAuthenticate.realm}", nonce="${message.wwwAuthenticate.nonce}"`);
99
+ }
100
+ ;
101
+ (message.extensions || []).forEach((ext) => put(ext.name, ext.value));
102
+ const media = (message.body || "")
103
+ .split(/\r?\n/)
104
+ .filter((line) => /^(m|c)=/.test(line));
105
+ if (media.length)
106
+ put("Body", media.join(" "));
107
+ return headers;
108
+ }
109
+ const join = (values) => values.join(", ");
110
+ /**
111
+ * Computes what a component changed between the message it received and
112
+ * the one it returned.
113
+ *
114
+ * @param {SIPMessage} before - Message as it entered the component
115
+ * @param {SIPMessage} after - Message as it left the component
116
+ * @return {MessageDiff} Added, removed and changed headers
117
+ */
118
+ function diffMessages(before, after) {
119
+ const a = flattenHeaders(before);
120
+ const b = flattenHeaders(after);
121
+ const diff = { headers: { added: {}, removed: {}, changed: {} } };
122
+ Object.keys(b)
123
+ .filter((name) => !(name in a))
124
+ .forEach((name) => (diff.headers.added[name] = join(b[name])));
125
+ Object.keys(a)
126
+ .filter((name) => !(name in b))
127
+ .forEach((name) => (diff.headers.removed[name] = join(a[name])));
128
+ Object.keys(a)
129
+ .filter((name) => name in b && join(a[name]) !== join(b[name]))
130
+ .forEach((name) => (diff.headers.changed[name] = {
131
+ from: join(a[name]),
132
+ to: join(b[name])
133
+ }));
134
+ const uriBefore = formatUri(before.requestUri);
135
+ const uriAfter = formatUri(after.requestUri);
136
+ if (uriBefore && uriAfter && uriBefore !== uriAfter) {
137
+ diff.requestUri = { from: uriBefore, to: uriAfter };
138
+ }
139
+ return diff;
140
+ }
141
+ /**
142
+ * True when a diff carries no change at all.
143
+ *
144
+ * @param {MessageDiff} diff - A diff
145
+ * @return {boolean} Whether it is empty
146
+ */
147
+ function isEmptyDiff(diff) {
148
+ return (!diff.requestUri &&
149
+ Object.keys(diff.headers.added).length === 0 &&
150
+ Object.keys(diff.headers.removed).length === 0 &&
151
+ Object.keys(diff.headers.changed).length === 0);
152
+ }
@@ -0,0 +1,11 @@
1
+ export * from "./types";
2
+ export * from "./schema";
3
+ export * from "./validate";
4
+ export * from "./redact";
5
+ export * from "./diff";
6
+ export * from "./status";
7
+ export * from "./metadata";
8
+ export * from "./normalize";
9
+ export * from "./compat";
10
+ export * from "./sanitize";
11
+ export * as Rules from "./rules";
@@ -0,0 +1,68 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
19
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
20
+ };
21
+ var __importStar = (this && this.__importStar) || (function () {
22
+ var ownKeys = function(o) {
23
+ ownKeys = Object.getOwnPropertyNames || function (o) {
24
+ var ar = [];
25
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
26
+ return ar;
27
+ };
28
+ return ownKeys(o);
29
+ };
30
+ return function (mod) {
31
+ if (mod && mod.__esModule) return mod;
32
+ var result = {};
33
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
34
+ __setModuleDefault(result, mod);
35
+ return result;
36
+ };
37
+ })();
38
+ Object.defineProperty(exports, "__esModule", { value: true });
39
+ exports.Rules = void 0;
40
+ /*
41
+ * Copyright (C) 2026 by Fonoster Inc (https://fonoster.com)
42
+ * http://github.com/fonoster/routr
43
+ *
44
+ * This file is part of Routr.
45
+ *
46
+ * Licensed under the MIT License (the "License");
47
+ * you may not use this file except in compliance with
48
+ * the License. You may obtain a copy of the License at
49
+ *
50
+ * https://opensource.org/licenses/MIT
51
+ *
52
+ * Unless required by applicable law or agreed to in writing, software
53
+ * distributed under the License is distributed on an "AS IS" BASIS,
54
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
55
+ * See the License for the specific language governing permissions and
56
+ * limitations under the License.
57
+ */
58
+ __exportStar(require("./types"), exports);
59
+ __exportStar(require("./schema"), exports);
60
+ __exportStar(require("./validate"), exports);
61
+ __exportStar(require("./redact"), exports);
62
+ __exportStar(require("./diff"), exports);
63
+ __exportStar(require("./status"), exports);
64
+ __exportStar(require("./metadata"), exports);
65
+ __exportStar(require("./normalize"), exports);
66
+ __exportStar(require("./compat"), exports);
67
+ __exportStar(require("./sanitize"), exports);
68
+ exports.Rules = __importStar(require("./rules"));
@@ -0,0 +1,28 @@
1
+ export interface SplitMetadata {
2
+ hop: Record<string, string>;
3
+ rest: Record<string, string>;
4
+ flag?: string;
5
+ }
6
+ /**
7
+ * Separates the trace keys a component put in its response metadata from
8
+ * the metadata that continues to the EdgePort.
9
+ *
10
+ * @param {Record<string, string>} metadata - Response metadata, if any
11
+ * @return {SplitMetadata} Annotations, remaining metadata and the flag
12
+ */
13
+ export declare function splitTraceMetadata(metadata?: Record<string, string>): SplitMetadata;
14
+ /**
15
+ * Returns the metadata without any trace.* key, or undefined when nothing
16
+ * is left.
17
+ *
18
+ * @param {Record<string, string>} metadata - Response metadata, if any
19
+ * @return {Record<string, string> | undefined} Metadata for the EdgePort
20
+ */
21
+ export declare function withoutTraceMetadata(metadata?: Record<string, string>): Record<string, string> | undefined;
22
+ /**
23
+ * Builds the metadata a processor returns to annotate its hop in the trace.
24
+ *
25
+ * @param {Record<string, string | number | undefined>} annotations - Keys without the prefix
26
+ * @return {Record<string, string>} Metadata entries, prefix added, undefined values skipped
27
+ */
28
+ export declare function traceAnnotations(annotations: Record<string, string | number | undefined>): Record<string, string>;
@@ -0,0 +1,75 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.splitTraceMetadata = splitTraceMetadata;
4
+ exports.withoutTraceMetadata = withoutTraceMetadata;
5
+ exports.traceAnnotations = traceAnnotations;
6
+ /*
7
+ * Copyright (C) 2026 by Fonoster Inc (https://fonoster.com)
8
+ * http://github.com/fonoster/routr
9
+ *
10
+ * This file is part of Routr.
11
+ *
12
+ * Licensed under the MIT License (the "License");
13
+ * you may not use this file except in compliance with
14
+ * the License. You may obtain a copy of the License at
15
+ *
16
+ * https://opensource.org/licenses/MIT
17
+ *
18
+ * Unless required by applicable law or agreed to in writing, software
19
+ * distributed under the License is distributed on an "AS IS" BASIS,
20
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21
+ * See the License for the specific language governing permissions and
22
+ * limitations under the License.
23
+ */
24
+ const types_1 = require("./types");
25
+ /**
26
+ * Separates the trace keys a component put in its response metadata from
27
+ * the metadata that continues to the EdgePort.
28
+ *
29
+ * @param {Record<string, string>} metadata - Response metadata, if any
30
+ * @return {SplitMetadata} Annotations, remaining metadata and the flag
31
+ */
32
+ function splitTraceMetadata(metadata) {
33
+ const hop = {};
34
+ const rest = {};
35
+ let flag;
36
+ Object.entries(metadata !== null && metadata !== void 0 ? metadata : {}).forEach(([key, value]) => {
37
+ if (key === types_1.TRACE_FLAG_METADATA_KEY) {
38
+ flag = value;
39
+ }
40
+ else if (key.startsWith(types_1.TRACE_METADATA_PREFIX)) {
41
+ hop[key.slice(types_1.TRACE_METADATA_PREFIX.length)] = value;
42
+ }
43
+ else {
44
+ rest[key] = value;
45
+ }
46
+ });
47
+ return Object.assign({ hop, rest }, (flag !== undefined ? { flag } : {}));
48
+ }
49
+ /**
50
+ * Returns the metadata without any trace.* key, or undefined when nothing
51
+ * is left.
52
+ *
53
+ * @param {Record<string, string>} metadata - Response metadata, if any
54
+ * @return {Record<string, string> | undefined} Metadata for the EdgePort
55
+ */
56
+ function withoutTraceMetadata(metadata) {
57
+ if (!metadata)
58
+ return undefined;
59
+ const { rest } = splitTraceMetadata(metadata);
60
+ return rest;
61
+ }
62
+ /**
63
+ * Builds the metadata a processor returns to annotate its hop in the trace.
64
+ *
65
+ * @param {Record<string, string | number | undefined>} annotations - Keys without the prefix
66
+ * @return {Record<string, string>} Metadata entries, prefix added, undefined values skipped
67
+ */
68
+ function traceAnnotations(annotations) {
69
+ const out = {};
70
+ Object.entries(annotations).forEach(([key, value]) => {
71
+ if (value !== undefined)
72
+ out[types_1.TRACE_METADATA_PREFIX + key] = String(value);
73
+ });
74
+ return out;
75
+ }
@@ -0,0 +1,11 @@
1
+ import { SIPMessage } from "../types";
2
+ /**
3
+ * Returns a copy of the message with the values that legitimately differ
4
+ * between two runs of the same scenario blanked: Via branches, From and To
5
+ * tags, and the nonces of a digest exchange. Everything else a component
6
+ * produces is deterministic for the same input and configuration.
7
+ *
8
+ * @param {SIPMessage} message - A message as recorded or as returned live
9
+ * @return {SIPMessage} A copy safe to diff against another run
10
+ */
11
+ export declare function normalizeMessage(message: SIPMessage): SIPMessage;
@@ -0,0 +1,39 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.normalizeMessage = normalizeMessage;
4
+ /**
5
+ * Returns a copy of the message with the values that legitimately differ
6
+ * between two runs of the same scenario blanked: Via branches, From and To
7
+ * tags, and the nonces of a digest exchange. Everything else a component
8
+ * produces is deterministic for the same input and configuration.
9
+ *
10
+ * @param {SIPMessage} message - A message as recorded or as returned live
11
+ * @return {SIPMessage} A copy safe to diff against another run
12
+ */
13
+ function normalizeMessage(message) {
14
+ var _a, _b;
15
+ const copy = JSON.parse(JSON.stringify(message));
16
+ (copy.via || []).forEach((v) => {
17
+ if (v.branch)
18
+ v.branch = "*";
19
+ });
20
+ if ((_a = copy.from) === null || _a === void 0 ? void 0 : _a.tag)
21
+ copy.from.tag = "*";
22
+ if ((_b = copy.to) === null || _b === void 0 ? void 0 : _b.tag)
23
+ copy.to.tag = "*";
24
+ if (copy.wwwAuthenticate) {
25
+ if (copy.wwwAuthenticate.nonce)
26
+ copy.wwwAuthenticate.nonce = "*";
27
+ if (copy.wwwAuthenticate.opaque)
28
+ copy.wwwAuthenticate.opaque = "*";
29
+ }
30
+ if (copy.authorization) {
31
+ if (copy.authorization.nonce)
32
+ copy.authorization.nonce = "*";
33
+ if (copy.authorization.cNonce)
34
+ copy.authorization.cNonce = "*";
35
+ if (copy.authorization.response)
36
+ copy.authorization.response = "*";
37
+ }
38
+ return copy;
39
+ }
@@ -0,0 +1,19 @@
1
+ import { MessageRequest, SIPMessage } from "../types";
2
+ export declare const REDACTED = "REDACTED";
3
+ /**
4
+ * Returns a deep copy of the message with credentials replaced by REDACTED.
5
+ * The digest `response` is the hash of the password and the only secret in a
6
+ * SIP Digest exchange; nonce, realm and username stay so the exchange remains
7
+ * readable.
8
+ *
9
+ * @param {SIPMessage} message - Message to copy
10
+ * @return {SIPMessage} A redacted copy
11
+ */
12
+ export declare function redactMessage(message: SIPMessage): SIPMessage;
13
+ /**
14
+ * Returns a deep copy of the request with its message redacted.
15
+ *
16
+ * @param {MessageRequest} request - Request to copy
17
+ * @return {MessageRequest} A redacted copy
18
+ */
19
+ export declare function redactRequest(request: MessageRequest): MessageRequest;
@@ -0,0 +1,52 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.REDACTED = void 0;
4
+ exports.redactMessage = redactMessage;
5
+ exports.redactRequest = redactRequest;
6
+ exports.REDACTED = "REDACTED";
7
+ // Header names whose value is a credential or a hash of one
8
+ const SECRET_HEADERS = new Set([
9
+ "authorization",
10
+ "proxy-authorization",
11
+ "x-gateway-auth"
12
+ ]);
13
+ /**
14
+ * Returns a deep copy of the message with credentials replaced by REDACTED.
15
+ * The digest `response` is the hash of the password and the only secret in a
16
+ * SIP Digest exchange; nonce, realm and username stay so the exchange remains
17
+ * readable.
18
+ *
19
+ * @param {SIPMessage} message - Message to copy
20
+ * @return {SIPMessage} A redacted copy
21
+ */
22
+ function redactMessage(message) {
23
+ var _a, _b, _c, _d;
24
+ const copy = JSON.parse(JSON.stringify(message));
25
+ if (copy.authorization) {
26
+ copy.authorization.response = exports.REDACTED;
27
+ if (copy.authorization.cNonce)
28
+ copy.authorization.cNonce = exports.REDACTED;
29
+ }
30
+ copy.extensions = (copy.extensions || []).map((ext) => SECRET_HEADERS.has(ext.name.toLowerCase())
31
+ ? Object.assign(Object.assign({}, ext), { value: exports.REDACTED }) : ext);
32
+ [(_a = copy.from) === null || _a === void 0 ? void 0 : _a.address, (_b = copy.to) === null || _b === void 0 ? void 0 : _b.address, (_c = copy.contact) === null || _c === void 0 ? void 0 : _c.address]
33
+ .concat((copy.route || []).map((r) => r.address))
34
+ .concat((copy.recordRoute || []).map((r) => r.address))
35
+ .forEach((address) => {
36
+ var _a;
37
+ if ((_a = address === null || address === void 0 ? void 0 : address.uri) === null || _a === void 0 ? void 0 : _a.userPassword)
38
+ address.uri.userPassword = exports.REDACTED;
39
+ });
40
+ if ((_d = copy.requestUri) === null || _d === void 0 ? void 0 : _d.userPassword)
41
+ copy.requestUri.userPassword = exports.REDACTED;
42
+ return copy;
43
+ }
44
+ /**
45
+ * Returns a deep copy of the request with its message redacted.
46
+ *
47
+ * @param {MessageRequest} request - Request to copy
48
+ * @return {MessageRequest} A redacted copy
49
+ */
50
+ function redactRequest(request) {
51
+ return Object.assign(Object.assign({}, JSON.parse(JSON.stringify(Object.assign(Object.assign({}, request), { message: undefined })))), { message: redactMessage(request.message) });
52
+ }
@@ -0,0 +1,43 @@
1
+ import { MessageRequest, SIPMessage, Via } from "../../types";
2
+ import { TraceRecord } from "../types";
3
+ export declare const inbound: (r: TraceRecord) => SIPMessage;
4
+ export declare const outbound: (r: TraceRecord) => SIPMessage | undefined;
5
+ export declare const isRequest: (r: TraceRecord) => boolean;
6
+ export declare const isResponse: (r: TraceRecord) => boolean;
7
+ export declare const isForwarded: (r: TraceRecord) => boolean;
8
+ export declare const isRelayed: (r: TraceRecord) => boolean;
9
+ export declare const isInDialog: (r: TraceRecord) => boolean;
10
+ export declare const statusOf: (r: TraceRecord) => number | undefined;
11
+ export declare const headerValues: (m: SIPMessage | undefined, name: string) => string[];
12
+ export declare const cseqOf: (m: SIPMessage | undefined) => {
13
+ number: number;
14
+ method: string;
15
+ } | undefined;
16
+ export declare const topVia: (m: SIPMessage | undefined) => Via | undefined;
17
+ export declare const topBranch: (m: SIPMessage | undefined) => string | undefined;
18
+ export declare const isExternalHost: (req: MessageRequest, host?: string) => boolean;
19
+ /**
20
+ * Whether a host is one of the proxy's own internal interfaces: a listening
21
+ * point, the socket the message came in on, or an address in localnets.
22
+ *
23
+ * @param {MessageRequest} req - The recorded request
24
+ * @param {string} host - Host to test
25
+ * @return {boolean} True for an internal interface
26
+ */
27
+ export declare const isInternalHost: (req: MessageRequest, host?: string) => boolean;
28
+ export declare const isOwnHost: (req: MessageRequest, host?: string) => boolean;
29
+ export declare const requestsIn: (call: TraceRecord[]) => TraceRecord[];
30
+ /**
31
+ * Identifies the party that sent a request: its From URI and tag. Both are
32
+ * needed, a SIPp-style endpoint may use the same tag on both sides.
33
+ *
34
+ * @param {TraceRecord} r - A request record
35
+ * @return {string} A key equal for every request from the same party
36
+ */
37
+ export declare const directionOf: (r: TraceRecord) => string;
38
+ export declare const isE164: (user?: string) => boolean;
39
+ export declare const uriOf: (value: string) => {
40
+ user?: string;
41
+ host?: string;
42
+ params: string;
43
+ } | undefined;
@@ -0,0 +1,117 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.uriOf = exports.isE164 = exports.directionOf = exports.requestsIn = exports.isOwnHost = exports.isInternalHost = exports.isExternalHost = exports.topBranch = exports.topVia = exports.cseqOf = exports.headerValues = exports.statusOf = exports.isInDialog = exports.isRelayed = exports.isForwarded = exports.isResponse = exports.isRequest = exports.outbound = exports.inbound = void 0;
4
+ const types_1 = require("../types");
5
+ const status_1 = require("../status");
6
+ /*
7
+ * Facts a rule may read: the SIP messages, the processor contract fields
8
+ * (sender, listening points, external addresses, localnets, receivedOn)
9
+ * and what the dispatcher measured. Never a hop's metadata.
10
+ */
11
+ const inbound = (r) => r.request.message;
12
+ exports.inbound = inbound;
13
+ const outbound = (r) => { var _a; return (_a = r.response) === null || _a === void 0 ? void 0 : _a.message; };
14
+ exports.outbound = outbound;
15
+ const isRequest = (r) => r.kind === types_1.TraceKind.REQUEST;
16
+ exports.isRequest = isRequest;
17
+ const isResponse = (r) => r.kind === types_1.TraceKind.RESPONSE;
18
+ exports.isResponse = isResponse;
19
+ const isForwarded = (r) => (0, exports.isRequest)(r) && r.outcome === types_1.TraceOutcome.FORWARDED && !!r.response;
20
+ exports.isForwarded = isForwarded;
21
+ const isRelayed = (r) => (0, exports.isResponse)(r) && r.outcome === types_1.TraceOutcome.RELAYED && !!r.response;
22
+ exports.isRelayed = isRelayed;
23
+ const isInDialog = (r) => { var _a; return !!((_a = (0, exports.inbound)(r).to) === null || _a === void 0 ? void 0 : _a.tag); };
24
+ exports.isInDialog = isInDialog;
25
+ const statusOf = (r) => {
26
+ var _a, _b;
27
+ return (_a = r.status) !== null && _a !== void 0 ? _a : (0, status_1.statusCodeOf)((0, exports.isResponse)(r) ? (0, exports.inbound)(r).responseType : (_b = (0, exports.outbound)(r)) === null || _b === void 0 ? void 0 : _b.responseType);
28
+ };
29
+ exports.statusOf = statusOf;
30
+ const headerValues = (m, name) => ((m === null || m === void 0 ? void 0 : m.extensions) || [])
31
+ .filter((e) => e.name.toLowerCase() === name.toLowerCase())
32
+ .map((e) => e.value);
33
+ exports.headerValues = headerValues;
34
+ const cseqOf = (m) => {
35
+ const raw = (0, exports.headerValues)(m, "CSeq")[0];
36
+ if (!raw)
37
+ return undefined;
38
+ const [n, method] = raw.trim().split(/\s+/);
39
+ const number = parseInt(n, 10);
40
+ return isNaN(number) ? undefined : { number, method: method || "" };
41
+ };
42
+ exports.cseqOf = cseqOf;
43
+ const topVia = (m) => { var _a; return (_a = m === null || m === void 0 ? void 0 : m.via) === null || _a === void 0 ? void 0 : _a[0]; };
44
+ exports.topVia = topVia;
45
+ const topBranch = (m) => { var _a; return (_a = (0, exports.topVia)(m)) === null || _a === void 0 ? void 0 : _a.branch; };
46
+ exports.topBranch = topBranch;
47
+ const isExternalHost = (req, host) => !!host && (req.externalAddrs || []).includes(host);
48
+ exports.isExternalHost = isExternalHost;
49
+ /**
50
+ * Whether a host is one of the proxy's own internal interfaces: a listening
51
+ * point, the socket the message came in on, or an address in localnets.
52
+ *
53
+ * @param {MessageRequest} req - The recorded request
54
+ * @param {string} host - Host to test
55
+ * @return {boolean} True for an internal interface
56
+ */
57
+ const isInternalHost = (req, host) => {
58
+ var _a;
59
+ return !!host &&
60
+ ((req.listeningPoints || []).some((lp) => lp.host === host) ||
61
+ ((_a = req.receivedOn) === null || _a === void 0 ? void 0 : _a.host) === host ||
62
+ (req.localnets || []).some((net) => inNet(host, net)));
63
+ };
64
+ exports.isInternalHost = isInternalHost;
65
+ const isOwnHost = (req, host) => (0, exports.isExternalHost)(req, host) || (0, exports.isInternalHost)(req, host);
66
+ exports.isOwnHost = isOwnHost;
67
+ const inNet = (host, net) => {
68
+ const [base, bitsRaw] = net.split("/");
69
+ const bits = bitsRaw === undefined ? 32 : parseInt(bitsRaw, 10);
70
+ const a = ipv4(host);
71
+ const b = ipv4(base);
72
+ if (a === undefined || b === undefined || isNaN(bits))
73
+ return host === base;
74
+ const mask = bits === 0 ? 0 : (~0 << (32 - bits)) >>> 0;
75
+ return (a & mask) >>> 0 === (b & mask) >>> 0;
76
+ };
77
+ const ipv4 = (s) => {
78
+ const parts = s.split(".");
79
+ if (parts.length !== 4)
80
+ return undefined;
81
+ let n = 0;
82
+ // eslint-disable-next-line no-loops/no-loops
83
+ for (const p of parts) {
84
+ const v = parseInt(p, 10);
85
+ if (isNaN(v) || v < 0 || v > 255)
86
+ return undefined;
87
+ n = (n << 8) | v;
88
+ }
89
+ return n >>> 0;
90
+ };
91
+ const requestsIn = (call) => call.filter(exports.isRequest);
92
+ exports.requestsIn = requestsIn;
93
+ /**
94
+ * Identifies the party that sent a request: its From URI and tag. Both are
95
+ * needed, a SIPp-style endpoint may use the same tag on both sides.
96
+ *
97
+ * @param {TraceRecord} r - A request record
98
+ * @return {string} A key equal for every request from the same party
99
+ */
100
+ const directionOf = (r) => {
101
+ var _a, _b, _c, _d;
102
+ const from = (0, exports.inbound)(r).from;
103
+ const uri = (_a = from === null || from === void 0 ? void 0 : from.address) === null || _a === void 0 ? void 0 : _a.uri;
104
+ return `${(_b = uri === null || uri === void 0 ? void 0 : uri.user) !== null && _b !== void 0 ? _b : ""}@${(_c = uri === null || uri === void 0 ? void 0 : uri.host) !== null && _c !== void 0 ? _c : ""};${(_d = from === null || from === void 0 ? void 0 : from.tag) !== null && _d !== void 0 ? _d : ""}`;
105
+ };
106
+ exports.directionOf = directionOf;
107
+ // Whether an E.164 number: a plus sign and 7 to 15 digits
108
+ const isE164 = (user) => !!user && /^\+[1-9][0-9]{6,14}$/.test(user);
109
+ exports.isE164 = isE164;
110
+ // Parses `<sip:user@host;params>` or `sip:user@host` out of a header value
111
+ const uriOf = (value) => {
112
+ const m = /<?sips?:(?:([^@>;]*)@)?([^>;?]*)([^>]*)>?/.exec(value);
113
+ if (!m)
114
+ return undefined;
115
+ return { user: m[1], host: m[2], params: m[3] || "" };
116
+ };
117
+ exports.uriOf = uriOf;