@routr/common 2.31.3 → 2.31.5

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.
@@ -1,9 +1,9 @@
1
1
  import { SipURI } from "./types";
2
2
  /**
3
3
  * The caller identity Routr rewrites toward a Trunk travels in its own
4
- * Record-Route as parameters, so the dialog's later requests can be mapped back
5
- * without state. Shared by the processor, which writes and reads them, and the
6
- * trace sanitizer, which must rewrite inside them.
4
+ * Record-Route, as parameters or in the URI user part, so the dialog's later
5
+ * requests can be mapped back without state. Shared by the processor, which
6
+ * writes and reads them, and the trace sanitizer, which must rewrite inside them.
7
7
  */
8
8
  /**
9
9
  * The Record-Route parameter names: [r]ewritten [f]rom or [c]allee (To),
@@ -22,20 +22,51 @@ export declare const IDENTITY_PARAMETER_NAMES: string[];
22
22
  export interface Identity {
23
23
  user?: string;
24
24
  host: string;
25
+ port?: number;
25
26
  userParam?: string;
26
27
  }
28
+ /**
29
+ * The port a SIP URI names, or undefined when it names none (the EdgePort's
30
+ * converter spells "no port" as -1, an unset field reads 0).
31
+ *
32
+ * @param {SipURI} uri - The URI
33
+ * @return {number | undefined} The port
34
+ */
35
+ export declare const portOf: (uri: SipURI) => number | undefined;
27
36
  /**
28
37
  * Encodes an identity as base64url, which keeps it a SIP token (RFC 3261
29
- * section 25.1): no quoting in the header, and it survives every hop unchanged.
38
+ * section 25.1) and unreserved in a URI user part (section 25.1, "unreserved"):
39
+ * no quoting or escaping, and it survives every hop unchanged.
30
40
  *
31
- * @param {SipURI} uri - The URI whose user, host and user parameter to encode
32
- * @return {string} The base64url value
41
+ * @param {SipURI} uri - The URI whose user, host, port and user parameter to
42
+ * encode; the port only when it names one
43
+ * @return {string} The base64url value of user@host[:port][;user=...]
33
44
  */
34
45
  export declare const encodeIdentity: (uri: SipURI) => string;
35
46
  /**
36
- * Decodes an identity parameter value.
47
+ * Decodes an identity parameter value, user@host[:port][;user=...].
37
48
  *
38
49
  * @param {string} value - The base64url value
39
50
  * @return {Identity | undefined} The identity, or undefined when it is not one
40
51
  */
41
52
  export declare const decodeIdentity: (value?: string) => Identity | undefined;
53
+ /**
54
+ * Reads the identity mapping from a Route URI user part of the form
55
+ * rfo.<b64>.rfn.<b64>.rco.<b64>.rcn.<b64>: each pair optional, in that order.
56
+ * The URI is what a user agent keeps of a Record-Route entry (RFC 3261 section
57
+ * 12.1.2), and "." is not in the base64url alphabet.
58
+ *
59
+ * @param {string} user - The URI user part
60
+ * @return {Record<string, string> | undefined} The base64url values by
61
+ * parameter name, or undefined when the user part is not of this form
62
+ */
63
+ export declare const decodeIdentityUser: (user?: string) => Record<string, string> | undefined;
64
+ /**
65
+ * Writes the identity mapping as a Route URI user part, the form
66
+ * decodeIdentityUser reads: rfo.<b64>.rfn.<b64>.rco.<b64>.rcn.<b64>, in that
67
+ * order, absent pairs left out.
68
+ *
69
+ * @param {Record<string, string>} values - The base64url values by parameter name
70
+ * @return {string | undefined} The user part, or undefined when there are none
71
+ */
72
+ export declare const encodeIdentityUser: (values: Record<string, string>) => string | undefined;
package/dist/identity.js CHANGED
@@ -1,11 +1,11 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.decodeIdentity = exports.encodeIdentity = exports.IDENTITY_PARAMETER_NAMES = exports.IDENTITY_PARAMETERS = void 0;
3
+ exports.encodeIdentityUser = exports.decodeIdentityUser = exports.decodeIdentity = exports.encodeIdentity = exports.portOf = exports.IDENTITY_PARAMETER_NAMES = exports.IDENTITY_PARAMETERS = void 0;
4
4
  /**
5
5
  * The caller identity Routr rewrites toward a Trunk travels in its own
6
- * Record-Route as parameters, so the dialog's later requests can be mapped back
7
- * without state. Shared by the processor, which writes and reads them, and the
8
- * trace sanitizer, which must rewrite inside them.
6
+ * Record-Route, as parameters or in the URI user part, so the dialog's later
7
+ * requests can be mapped back without state. Shared by the processor, which
8
+ * writes and reads them, and the trace sanitizer, which must rewrite inside them.
9
9
  */
10
10
  /**
11
11
  * The Record-Route parameter names: [r]ewritten [f]rom or [c]allee (To),
@@ -20,25 +20,38 @@ exports.IDENTITY_PARAMETERS = {
20
20
  };
21
21
  /** Every identity parameter name, in the order above. */
22
22
  exports.IDENTITY_PARAMETER_NAMES = Object.values(exports.IDENTITY_PARAMETERS);
23
+ /**
24
+ * The port a SIP URI names, or undefined when it names none (the EdgePort's
25
+ * converter spells "no port" as -1, an unset field reads 0).
26
+ *
27
+ * @param {SipURI} uri - The URI
28
+ * @return {number | undefined} The port
29
+ */
30
+ const portOf = (uri) => uri.port > 0 ? uri.port : undefined;
31
+ exports.portOf = portOf;
23
32
  /**
24
33
  * Encodes an identity as base64url, which keeps it a SIP token (RFC 3261
25
- * section 25.1): no quoting in the header, and it survives every hop unchanged.
34
+ * section 25.1) and unreserved in a URI user part (section 25.1, "unreserved"):
35
+ * no quoting or escaping, and it survives every hop unchanged.
26
36
  *
27
- * @param {SipURI} uri - The URI whose user, host and user parameter to encode
28
- * @return {string} The base64url value
37
+ * @param {SipURI} uri - The URI whose user, host, port and user parameter to
38
+ * encode; the port only when it names one
39
+ * @return {string} The base64url value of user@host[:port][;user=...]
29
40
  */
30
41
  const encodeIdentity = (uri) => {
31
42
  var _a;
32
- return Buffer.from(`${(_a = uri.user) !== null && _a !== void 0 ? _a : ""}@${uri.host}${uri.userParam ? `;user=${uri.userParam}` : ""}`).toString("base64url");
43
+ const port = (0, exports.portOf)(uri);
44
+ return Buffer.from(`${(_a = uri.user) !== null && _a !== void 0 ? _a : ""}@${uri.host}${port ? `:${port}` : ""}${uri.userParam ? `;user=${uri.userParam}` : ""}`).toString("base64url");
33
45
  };
34
46
  exports.encodeIdentity = encodeIdentity;
35
47
  /**
36
- * Decodes an identity parameter value.
48
+ * Decodes an identity parameter value, user@host[:port][;user=...].
37
49
  *
38
50
  * @param {string} value - The base64url value
39
51
  * @return {Identity | undefined} The identity, or undefined when it is not one
40
52
  */
41
53
  const decodeIdentity = (value) => {
54
+ var _a;
42
55
  if (!value)
43
56
  return undefined;
44
57
  const decoded = Buffer.from(value, "base64url").toString("utf8");
@@ -47,10 +60,45 @@ const decodeIdentity = (value) => {
47
60
  if (at < 0)
48
61
  return undefined;
49
62
  const user = address.slice(0, at);
50
- return {
51
- user: user || undefined,
52
- host: address.slice(at + 1),
53
- userParam: userParam || undefined
54
- };
63
+ const hostPort = address.slice(at + 1);
64
+ // An IPv6 host keeps its brackets, so its port follows the last "]:"
65
+ const [, host = hostPort, port] = (_a = hostPort.match(/^(\[[^\]]*\]|[^:]*)(?::(\d+))?$/)) !== null && _a !== void 0 ? _a : [];
66
+ return Object.assign(Object.assign({ user: user || undefined, host }, (port ? { port: Number(port) } : {})), { userParam: userParam || undefined });
55
67
  };
56
68
  exports.decodeIdentity = decodeIdentity;
69
+ /**
70
+ * Reads the identity mapping from a Route URI user part of the form
71
+ * rfo.<b64>.rfn.<b64>.rco.<b64>.rcn.<b64>: each pair optional, in that order.
72
+ * The URI is what a user agent keeps of a Record-Route entry (RFC 3261 section
73
+ * 12.1.2), and "." is not in the base64url alphabet.
74
+ *
75
+ * @param {string} user - The URI user part
76
+ * @return {Record<string, string> | undefined} The base64url values by
77
+ * parameter name, or undefined when the user part is not of this form
78
+ */
79
+ const decodeIdentityUser = (user) => {
80
+ const parts = (user !== null && user !== void 0 ? user : "").split(".");
81
+ if (parts.length < 2 || parts.length % 2)
82
+ return undefined;
83
+ const pairs = parts
84
+ .filter((_, i) => i % 2 === 0)
85
+ .map((name, i) => [name, parts[i * 2 + 1]]);
86
+ const order = pairs.map(([name]) => exports.IDENTITY_PARAMETER_NAMES.indexOf(name));
87
+ const valid = order.every((o, i) => o >= 0 && (i === 0 || o > order[i - 1])) &&
88
+ pairs.every(([, value]) => /^[A-Za-z0-9_-]+$/.test(value));
89
+ return valid ? Object.fromEntries(pairs) : undefined;
90
+ };
91
+ exports.decodeIdentityUser = decodeIdentityUser;
92
+ /**
93
+ * Writes the identity mapping as a Route URI user part, the form
94
+ * decodeIdentityUser reads: rfo.<b64>.rfn.<b64>.rco.<b64>.rcn.<b64>, in that
95
+ * order, absent pairs left out.
96
+ *
97
+ * @param {Record<string, string>} values - The base64url values by parameter name
98
+ * @return {string | undefined} The user part, or undefined when there are none
99
+ */
100
+ const encodeIdentityUser = (values) => {
101
+ const pairs = exports.IDENTITY_PARAMETER_NAMES.filter((name) => values[name]).map((name) => `${name}.${values[name]}`);
102
+ return pairs.length > 0 ? pairs.join(".") : undefined;
103
+ };
104
+ exports.encodeIdentityUser = encodeIdentityUser;
@@ -99,7 +99,29 @@ function applyRewrites(record, rewrites) {
99
99
  // (";rfo=...;rfn=..." in a hop's diff); base64url has no quote, so they are
100
100
  // safe to match in the JSON text
101
101
  const encodedInText = new RegExp(`\\b(${ENCODED_IDENTITY_PARAMETERS.join("|")})=([A-Za-z0-9_-]+)`, "g");
102
- return JSON.parse(rewrite(text.replace(encodedInText, (_m, name, value) => `${name}=${reencode(value, rewrite)}`)));
102
+ // And in a Route or Record-Route URI user part kept as text
103
+ // ("<sip:rfo....rcn...@host>")
104
+ const names = ENCODED_IDENTITY_PARAMETERS.join("|");
105
+ const encodedUserInText = new RegExp(`\\b((?:${names})\\.[A-Za-z0-9_-]+(?:\\.(?:${names})\\.[A-Za-z0-9_-]+)*)@`, "g");
106
+ return JSON.parse(rewrite(text
107
+ .replace(encodedInText, (_m, name, value) => `${name}=${reencode(value, rewrite)}`)
108
+ .replace(encodedUserInText, (_m, user) => `${reencodeUser(user, rewrite)}@`)));
109
+ }
110
+ /**
111
+ * Rewrites the identities inside a URI user part that holds Routr's identity
112
+ * mapping (rfo.<b64>.rfn.<b64>...); any other user part is returned as is.
113
+ *
114
+ * @param {string} user - The URI user part
115
+ * @param {Function} rewrite - The literal rewrites
116
+ * @return {string} The rewritten user part
117
+ */
118
+ function reencodeUser(user, rewrite) {
119
+ const values = (0, identity_1.decodeIdentityUser)(user);
120
+ if (!values)
121
+ return user;
122
+ return Object.entries(values)
123
+ .map(([name, value]) => `${name}.${reencode(value, rewrite)}`)
124
+ .join(".");
103
125
  }
104
126
  /**
105
127
  * Decodes a base64url identity parameter, rewrites it and encodes it again.
@@ -123,7 +145,8 @@ function reencode(value, rewrite) {
123
145
  // survive sanitizing; they are decoded, rewritten and encoded again.
124
146
  const ENCODED_IDENTITY_PARAMETERS = identity_1.IDENTITY_PARAMETER_NAMES;
125
147
  /**
126
- * Rewrites the identity parameters wherever they are a field of the record.
148
+ * Rewrites the identity parameters wherever they are a field of the record, or
149
+ * a URI user part.
127
150
  *
128
151
  * @param {T} value - Any part of the record
129
152
  * @param {Function} rewrite - The literal rewrites
@@ -139,6 +162,10 @@ function rewriteEncodedIdentities(value, rewrite) {
139
162
  if (ENCODED_IDENTITY_PARAMETERS.includes(k) && typeof v === "string") {
140
163
  return [k, reencode(v, rewrite)];
141
164
  }
165
+ // A Route or Record-Route URI whose user part holds the mapping
166
+ if (k === "user" && typeof v === "string") {
167
+ return [k, reencodeUser(v, rewrite)];
168
+ }
142
169
  return [k, rewriteEncodedIdentities(v, rewrite)];
143
170
  }));
144
171
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@routr/common",
3
- "version": "2.31.3",
3
+ "version": "2.31.5",
4
4
  "description": "Common package",
5
5
  "author": "Pedro Sanders <psanders@fonoster.com>",
6
6
  "homepage": "https://github.com/fonoster/routr#readme",
@@ -61,5 +61,5 @@
61
61
  "devDependencies": {
62
62
  "@types/js-yaml": "^4.0.5"
63
63
  },
64
- "gitHead": "7178a8d1ad402c3a00b31b55c8b63ecd2527c975"
64
+ "gitHead": "c0b54abe054aee1c28d6bc5c3402ba4bedba31a9"
65
65
  }