@routr/processor 2.32.3 → 2.34.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.
@@ -41,6 +41,20 @@ export declare const isAnonymous: (uri: CommonTypes.SipURI) => boolean;
41
41
  * @return {Function} the alteration
42
42
  */
43
43
  export declare const rewriteFromWithNumber: (route: Route) => (request: MessageRequest) => MessageRequest;
44
+ /**
45
+ * Writes the numbers of a request the way the Trunk it leaves through asks
46
+ * (Route.numberFormat). The called format writes the To and Request-URI users,
47
+ * read from the request as routed (original, before normalizeToE164), so
48
+ * normalizeToE164 has no effect on them. The caller format writes the From
49
+ * user only when rewriteFromWithNumber rewrote it to the Number; an anonymous
50
+ * From stays anonymous, and a From Routr doesn't rewrite is left to
51
+ * normalizeToE164. A user that is not a phone number is sent as it is.
52
+ *
53
+ * @param {Route} route - the route; does nothing unless route.numberFormat is set
54
+ * @param {MessageRequest} original - the request before normalizeToE164
55
+ * @return {Function} the alteration
56
+ */
57
+ export declare const formatNumbers: (route: Route, original: MessageRequest) => (request: MessageRequest) => MessageRequest;
44
58
  /**
45
59
  * The identity mapping that remembers how the identity alterations changed a
46
60
  * request's From and To; addSelfRecordRoute writes it in its Record-Route URI. Both ends of the dialog copy Record-Route into the
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.swapIdentityFromRouteSet = exports.identityRewriteParameters = exports.rewriteFromWithNumber = exports.isAnonymous = exports.normalizeToE164 = exports.overwriteContactWithSenderInfo = exports.fixInvalidContact = exports.decreaseMaxForwards = exports.removeTopVia = exports.removeSelfRoutes = exports.removeAuthorization = exports.removeHeaders = exports.removeXEdgePortRef = exports.addXEdgePortRef = exports.addSelfRecordRoute = exports.applyXHeaders = exports.addRouteToPeerEdgePort = exports.addRouteToNextHop = exports.addSelfViaUsingExternalAddrs = exports.addSelfViaUsingTheRouteHeaders = exports.addSelfVia = exports.fixRequestURI = void 0;
3
+ exports.swapIdentityFromRouteSet = exports.identityRewriteParameters = exports.formatNumbers = exports.rewriteFromWithNumber = exports.isAnonymous = exports.normalizeToE164 = exports.overwriteContactWithSenderInfo = exports.fixInvalidContact = exports.decreaseMaxForwards = exports.removeTopVia = exports.removeSelfRoutes = exports.removeAuthorization = exports.removeHeaders = exports.removeXEdgePortRef = exports.addXEdgePortRef = exports.addSelfRecordRoute = exports.applyXHeaders = exports.addRouteToPeerEdgePort = exports.addRouteToNextHop = exports.addSelfViaUsingExternalAddrs = exports.addSelfViaUsingTheRouteHeaders = exports.addSelfVia = exports.fixRequestURI = void 0;
4
4
  /*
5
5
  * Copyright (C) 2026 by Fonoster Inc (https://fonoster.com)
6
6
  * http://github.com/fonoster/routr
@@ -22,6 +22,7 @@ exports.swapIdentityFromRouteSet = exports.identityRewriteParameters = exports.r
22
22
  const common_1 = require("@routr/common");
23
23
  const phone_1 = require("phone");
24
24
  const helper_1 = require("./helper");
25
+ const format_number_1 = require("./format_number");
25
26
  const { IDENTITY_PARAMETERS, encodeIdentity, encodeIdentityUser, decodeIdentity, decodeIdentityUser, portOf } = common_1.Identity;
26
27
  // Forces the requestURI to be the same as the trunk uri.
27
28
  // This is a workaround for Twilio.
@@ -378,6 +379,44 @@ const rewriteFromWithNumber = (route) => (request) => {
378
379
  return req;
379
380
  };
380
381
  exports.rewriteFromWithNumber = rewriteFromWithNumber;
382
+ // Writes a URI's user; ";user=phone" stays only when the user sent starts
383
+ // with + (or was left as it was), and a format never adds it
384
+ const writeUser = (uri, { user, keepsUserPhone }) => {
385
+ uri.user = user;
386
+ if (!keepsUserPhone && uri.userParam === "phone")
387
+ uri.userParam = "";
388
+ };
389
+ /**
390
+ * Writes the numbers of a request the way the Trunk it leaves through asks
391
+ * (Route.numberFormat). The called format writes the To and Request-URI users,
392
+ * read from the request as routed (original, before normalizeToE164), so
393
+ * normalizeToE164 has no effect on them. The caller format writes the From
394
+ * user only when rewriteFromWithNumber rewrote it to the Number; an anonymous
395
+ * From stays anonymous, and a From Routr doesn't rewrite is left to
396
+ * normalizeToE164. A user that is not a phone number is sent as it is.
397
+ *
398
+ * @param {Route} route - the route; does nothing unless route.numberFormat is set
399
+ * @param {MessageRequest} original - the request before normalizeToE164
400
+ * @return {Function} the alteration
401
+ */
402
+ const formatNumbers = (route, original) => (request) => {
403
+ var _a;
404
+ const { called, caller, countryIsoCode } = (_a = route.numberFormat) !== null && _a !== void 0 ? _a : {};
405
+ if (!called && !caller)
406
+ return request;
407
+ const req = common_1.Helper.deepCopy(request);
408
+ if (called) {
409
+ const options = Object.assign(Object.assign({}, called), { countryIsoCode });
410
+ writeUser(req.message.to.address.uri, (0, format_number_1.formatNumber)(original.message.to.address.uri.user, options));
411
+ writeUser(req.message.requestUri, (0, format_number_1.formatNumber)(original.message.requestUri.user, options));
412
+ }
413
+ const from = req.message.from.address.uri;
414
+ if (caller && route.rewriteFrom && !(0, exports.isAnonymous)(from)) {
415
+ writeUser(from, (0, format_number_1.formatNumber)(from.user, Object.assign(Object.assign({}, caller), { countryIsoCode })));
416
+ }
417
+ return req;
418
+ };
419
+ exports.formatNumbers = formatNumbers;
381
420
  const sameIdentity = (uri, identity) => {
382
421
  var _a, _b, _c;
383
422
  return !!identity &&
@@ -0,0 +1,13 @@
1
+ /**
2
+ * The E.164 numbers, as digits without the +, that a number written in national
3
+ * form may stand for in a country. A carrier writes one number several ways:
4
+ * behind an international prefix (00442079460018), with the trunk prefix 0
5
+ * (02079460018) or bare (2079460018). Each reading is a candidate; the caller
6
+ * compares them with the numbers it knows, so nothing here judges whether a
7
+ * candidate is a valid number (number plans reject real landlines).
8
+ *
9
+ * @param {string} identifier - the number as received, digits only
10
+ * @param {string} countryIsoCode - ISO 3166-1 alpha-2 code of the country, any case
11
+ * @return {string[]} the candidates; empty when the country is empty or unknown
12
+ */
13
+ export declare const e164Candidates: (identifier: string, countryIsoCode?: string) => string[];
@@ -0,0 +1,58 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.e164Candidates = void 0;
4
+ /*
5
+ * Copyright (C) 2026 by Fonoster Inc (https://fonoster.com)
6
+ * http://github.com/fonoster/routr
7
+ *
8
+ * This file is part of Routr.
9
+ *
10
+ * Licensed under the MIT License (the "License");
11
+ * you may not use this file except in compliance with
12
+ * the License. You may obtain a copy of the License at
13
+ *
14
+ * https://opensource.org/licenses/MIT
15
+ *
16
+ * Unless required by applicable law or agreed to in writing, software
17
+ * distributed under the License is distributed on an "AS IS" BASIS,
18
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19
+ * See the License for the specific language governing permissions and
20
+ * limitations under the License.
21
+ */
22
+ const phone_1 = require("phone");
23
+ // The international prefix of ITU-T E.164, as carriers forward it
24
+ const INTERNATIONAL_PREFIX = "00";
25
+ const callingCodeOf = (countryIsoCode) => {
26
+ var _a;
27
+ return (_a = phone_1.countryPhoneData.find((country) => country.alpha2 === (countryIsoCode === null || countryIsoCode === void 0 ? void 0 : countryIsoCode.toUpperCase()))) === null || _a === void 0 ? void 0 : _a.country_code;
28
+ };
29
+ /**
30
+ * The E.164 numbers, as digits without the +, that a number written in national
31
+ * form may stand for in a country. A carrier writes one number several ways:
32
+ * behind an international prefix (00442079460018), with the trunk prefix 0
33
+ * (02079460018) or bare (2079460018). Each reading is a candidate; the caller
34
+ * compares them with the numbers it knows, so nothing here judges whether a
35
+ * candidate is a valid number (number plans reject real landlines).
36
+ *
37
+ * @param {string} identifier - the number as received, digits only
38
+ * @param {string} countryIsoCode - ISO 3166-1 alpha-2 code of the country, any case
39
+ * @return {string[]} the candidates; empty when the country is empty or unknown
40
+ */
41
+ const e164Candidates = (identifier, countryIsoCode) => {
42
+ const callingCode = callingCodeOf(countryIsoCode);
43
+ if (!callingCode)
44
+ return [];
45
+ const afterInternationalPrefix = identifier.startsWith(INTERNATIONAL_PREFIX)
46
+ ? [identifier.slice(INTERNATIONAL_PREFIX.length)]
47
+ : [];
48
+ // Italy keeps the 0 in its E.164 numbers and Germany drops it, so both
49
+ // readings are candidates
50
+ const national = identifier.startsWith("0")
51
+ ? [identifier, identifier.slice(1)]
52
+ : [identifier];
53
+ return [
54
+ ...afterInternationalPrefix,
55
+ ...national.map((digits) => callingCode + digits)
56
+ ];
57
+ };
58
+ exports.e164Candidates = e164Candidates;
@@ -0,0 +1,34 @@
1
+ import { CommonTypes as CT } from "@routr/common";
2
+ export interface FormatNumberOptions extends CT.NumberFormatSetting {
3
+ countryIsoCode?: string;
4
+ }
5
+ export interface FormattedNumber {
6
+ user: string;
7
+ keepsUserPhone: boolean;
8
+ }
9
+ /**
10
+ * Writes a phone number the way a Trunk's carrier asks for it.
11
+ *
12
+ * E164 +442079460018, with no prefix
13
+ * E164_WITHOUT_PLUS 442079460018
14
+ * NATIONAL 02079460018: the digits of the national format, trunk
15
+ * prefix included where the country uses one, for a
16
+ * number of the reference country; a number of another
17
+ * calling code in international form
18
+ * INTERNATIONAL 00442079460018: the international prefix of the
19
+ * reference country, the country code and the national
20
+ * number
21
+ *
22
+ * NATIONAL and INTERNATIONAL write +E.164 with no prefix when the reference
23
+ * country is empty, unknown or has no single international prefix. The
24
+ * prefix goes in front of the formatted number, with # escaped as %23 (RFC
25
+ * 3261 section 25.1). A user that is not a phone number is returned as it is,
26
+ * with no prefix.
27
+ *
28
+ * @param {string} user - the user part of a URI, as routed
29
+ * @param {FormatNumberOptions} options - the format, the prefix and the
30
+ * country of the Number the call leaves from
31
+ * @return {FormattedNumber} the user to send and whether ";user=phone" still
32
+ * describes it
33
+ */
34
+ export declare const formatNumber: (user: string, options: FormatNumberOptions) => FormattedNumber;
@@ -0,0 +1,117 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.formatNumber = void 0;
4
+ /*
5
+ * Copyright (C) 2026 by Fonoster Inc (https://fonoster.com)
6
+ * http://github.com/fonoster/routr
7
+ *
8
+ * This file is part of Routr.
9
+ *
10
+ * Licensed under the MIT License (the "License");
11
+ * you may not use this file except in compliance with
12
+ * the License. You may obtain a copy of the License at
13
+ *
14
+ * https://opensource.org/licenses/MIT
15
+ *
16
+ * Unless required by applicable law or agreed to in writing, software
17
+ * distributed under the License is distributed on an "AS IS" BASIS,
18
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19
+ * See the License for the specific language governing permissions and
20
+ * limitations under the License.
21
+ */
22
+ const common_1 = require("@routr/common");
23
+ const libphonenumber_js_1 = require("libphonenumber-js");
24
+ const countryOf = (countryIsoCode) => {
25
+ const country = countryIsoCode === null || countryIsoCode === void 0 ? void 0 : countryIsoCode.toUpperCase();
26
+ return country && (0, libphonenumber_js_1.isSupportedCountry)(country) ? country : undefined;
27
+ };
28
+ // A user is read as a number of the reference country first (a + makes it
29
+ // international), then as an international number written without its +.
30
+ // Whatever neither reading accepts is not a phone number: an Agent's
31
+ // username, "anonymous", a short code such as 999
32
+ const read = (user, country) => [
33
+ (0, libphonenumber_js_1.parsePhoneNumberFromString)(user, country),
34
+ (0, libphonenumber_js_1.parsePhoneNumberFromString)(`+${user}`)
35
+ ].find((number) => number === null || number === void 0 ? void 0 : number.isPossible());
36
+ // The international prefix of a country, digits only, as libphonenumber-js
37
+ // writes it from there toward a number of another country code: 00 from GB,
38
+ // 011 from the US, 0011 from AU. format("IDD") writes a number of the same
39
+ // country in national form, so the probe is always of another country, and
40
+ // its own country code and national number are dropped from the end. Empty
41
+ // when the country has no single international prefix (BR, KR...).
42
+ const internationalPrefix = (country) => {
43
+ var _a;
44
+ const probe = (0, libphonenumber_js_1.parsePhoneNumberFromString)((0, libphonenumber_js_1.getCountryCallingCode)(country) === "44" ? "+12015550123" : "+442079460000");
45
+ const written = probe.format("IDD", { fromCountry: country });
46
+ const tail = probe.countryCallingCode + probe.nationalNumber;
47
+ const digits = (_a = written === null || written === void 0 ? void 0 : written.replace(/\D/g, "")) !== null && _a !== void 0 ? _a : "";
48
+ if (!written || written.startsWith("+") || !digits.endsWith(tail))
49
+ return "";
50
+ return digits.slice(0, -tail.length);
51
+ };
52
+ /**
53
+ * Writes a phone number the way a Trunk's carrier asks for it.
54
+ *
55
+ * E164 +442079460018, with no prefix
56
+ * E164_WITHOUT_PLUS 442079460018
57
+ * NATIONAL 02079460018: the digits of the national format, trunk
58
+ * prefix included where the country uses one, for a
59
+ * number of the reference country; a number of another
60
+ * calling code in international form
61
+ * INTERNATIONAL 00442079460018: the international prefix of the
62
+ * reference country, the country code and the national
63
+ * number
64
+ *
65
+ * NATIONAL and INTERNATIONAL write +E.164 with no prefix when the reference
66
+ * country is empty, unknown or has no single international prefix. The
67
+ * prefix goes in front of the formatted number, with # escaped as %23 (RFC
68
+ * 3261 section 25.1). A user that is not a phone number is returned as it is,
69
+ * with no prefix.
70
+ *
71
+ * @param {string} user - the user part of a URI, as routed
72
+ * @param {FormatNumberOptions} options - the format, the prefix and the
73
+ * country of the Number the call leaves from
74
+ * @return {FormattedNumber} the user to send and whether ";user=phone" still
75
+ * describes it
76
+ */
77
+ const formatNumber = (user, options) => {
78
+ var _a;
79
+ const { format } = options;
80
+ const prefix = ((_a = options.prefix) !== null && _a !== void 0 ? _a : "").replace(/#/g, "%23");
81
+ const country = countryOf(options.countryIsoCode);
82
+ const untouched = { user, keepsUserPhone: true };
83
+ if (!user || !format || format === common_1.CommonTypes.NumberFormat.UNSPECIFIED) {
84
+ return untouched;
85
+ }
86
+ const number = read(user, country);
87
+ if (!number)
88
+ return untouched;
89
+ const written = (user) => ({
90
+ user,
91
+ keepsUserPhone: user.startsWith("+")
92
+ });
93
+ const international = () => {
94
+ const idd = country ? internationalPrefix(country) : "";
95
+ if (!idd)
96
+ return written(number.number);
97
+ return written(prefix + idd + number.countryCallingCode + number.nationalNumber);
98
+ };
99
+ switch (format) {
100
+ case common_1.CommonTypes.NumberFormat.E164:
101
+ return written(number.number);
102
+ case common_1.CommonTypes.NumberFormat.E164_WITHOUT_PLUS:
103
+ return written(prefix + number.number.slice(1));
104
+ case common_1.CommonTypes.NumberFormat.NATIONAL:
105
+ // NATIONAL means something only inside the reference country's own plan
106
+ if (!country ||
107
+ (0, libphonenumber_js_1.getCountryCallingCode)(country) !== number.countryCallingCode) {
108
+ return international();
109
+ }
110
+ return written(prefix + number.formatNational().replace(/\D/g, ""));
111
+ case common_1.CommonTypes.NumberFormat.INTERNATIONAL:
112
+ return international();
113
+ default:
114
+ return untouched;
115
+ }
116
+ };
117
+ exports.formatNumber = formatNumber;
package/dist/index.d.ts CHANGED
@@ -6,4 +6,6 @@ export * as Target from "./target";
6
6
  export * as Extensions from "./extensions";
7
7
  export * as Alterations from "./alterations";
8
8
  export * as Helper from "./helper";
9
+ export { e164Candidates } from "./e164_candidates";
10
+ export { formatNumber, FormatNumberOptions, FormattedNumber } from "./format_number";
9
11
  export { Processor as default, MessageRequest, Response, ProcessorConfig };
package/dist/index.js CHANGED
@@ -36,7 +36,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
36
36
  return (mod && mod.__esModule) ? mod : { "default": mod };
37
37
  };
38
38
  Object.defineProperty(exports, "__esModule", { value: true });
39
- exports.Response = exports.default = exports.Helper = exports.Alterations = exports.Extensions = exports.Target = void 0;
39
+ exports.Response = exports.default = exports.formatNumber = exports.e164Candidates = exports.Helper = exports.Alterations = exports.Extensions = exports.Target = void 0;
40
40
  const processor_1 = __importDefault(require("./processor"));
41
41
  exports.default = processor_1.default;
42
42
  const response_1 = __importDefault(require("./response"));
@@ -45,3 +45,7 @@ exports.Target = __importStar(require("./target"));
45
45
  exports.Extensions = __importStar(require("./extensions"));
46
46
  exports.Alterations = __importStar(require("./alterations"));
47
47
  exports.Helper = __importStar(require("./helper"));
48
+ var e164_candidates_1 = require("./e164_candidates");
49
+ Object.defineProperty(exports, "e164Candidates", { enumerable: true, get: function () { return e164_candidates_1.e164Candidates; } });
50
+ var format_number_1 = require("./format_number");
51
+ Object.defineProperty(exports, "formatNumber", { enumerable: true, get: function () { return format_number_1.formatNumber; } });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@routr/processor",
3
- "version": "2.32.3",
3
+ "version": "2.34.0",
4
4
  "description": "Processor server and API",
5
5
  "author": "Pedro Sanders <psanders@fonoster.com>",
6
6
  "homepage": "https://github.com/fonoster/routr#readme",
@@ -15,8 +15,9 @@
15
15
  "@fonoster/logger": "0.6.0",
16
16
  "@grpc/grpc-js": "~1.14.6",
17
17
  "@grpc/proto-loader": "~0.7.10",
18
- "@routr/common": "^2.32.3",
18
+ "@routr/common": "^2.34.0",
19
19
  "fp-ts": "^2.11.8",
20
+ "libphonenumber-js": "1.11.20",
20
21
  "phone": "^3.1.68"
21
22
  },
22
23
  "directories": {
@@ -36,5 +37,5 @@
36
37
  "bugs": {
37
38
  "url": "https://github.com/fonoster/routr/issues"
38
39
  },
39
- "gitHead": "88199378914c62dbd44cf187c7892ce8435c3ca3"
40
+ "gitHead": "7d2faf42bc43b7f3f1ac7d77097ffbca1b7cf4d2"
40
41
  }