@routr/processor 2.32.2 → 2.33.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,29 @@
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
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, or any number when the reference country
18
+ * is empty or unknown, is written +E.164 with no prefix
19
+ *
20
+ * The prefix goes in front of the formatted number. A user that is not a
21
+ * phone number is returned as it is, with no prefix.
22
+ *
23
+ * @param {string} user - the user part of a URI, as routed
24
+ * @param {FormatNumberOptions} options - the format, the prefix and the
25
+ * country of the Number the call leaves from
26
+ * @return {FormattedNumber} the user to send and whether ";user=phone" still
27
+ * describes it
28
+ */
29
+ export declare const formatNumber: (user: string, options: FormatNumberOptions) => FormattedNumber;
@@ -0,0 +1,82 @@
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
+ /**
37
+ * Writes a phone number the way a Trunk's carrier asks for it.
38
+ *
39
+ * E164 +442079460018
40
+ * E164_WITHOUT_PLUS 442079460018
41
+ * NATIONAL 02079460018: the digits of the national format, trunk
42
+ * prefix included where the country uses one, for a
43
+ * number of the reference country. A number of another
44
+ * calling code, or any number when the reference country
45
+ * is empty or unknown, is written +E.164 with no prefix
46
+ *
47
+ * The prefix goes in front of the formatted number. A user that is not a
48
+ * phone number is returned as it is, with no prefix.
49
+ *
50
+ * @param {string} user - the user part of a URI, as routed
51
+ * @param {FormatNumberOptions} options - the format, the prefix and the
52
+ * country of the Number the call leaves from
53
+ * @return {FormattedNumber} the user to send and whether ";user=phone" still
54
+ * describes it
55
+ */
56
+ const formatNumber = (user, options) => {
57
+ const { format, prefix = "" } = options;
58
+ const country = countryOf(options.countryIsoCode);
59
+ const untouched = { user, keepsUserPhone: true };
60
+ if (!user || !format || format === common_1.CommonTypes.NumberFormat.UNSPECIFIED) {
61
+ return untouched;
62
+ }
63
+ const number = read(user, country);
64
+ if (!number)
65
+ return untouched;
66
+ const written = (user) => ({
67
+ user,
68
+ keepsUserPhone: user.startsWith("+")
69
+ });
70
+ if (format === common_1.CommonTypes.NumberFormat.E164)
71
+ return written(prefix + number.number);
72
+ if (format === common_1.CommonTypes.NumberFormat.E164_WITHOUT_PLUS) {
73
+ return written(prefix + number.number.slice(1));
74
+ }
75
+ // NATIONAL means something only inside the reference country's own plan
76
+ if (!country ||
77
+ (0, libphonenumber_js_1.getCountryCallingCode)(country) !== number.countryCallingCode) {
78
+ return written(number.number);
79
+ }
80
+ return written(prefix + number.formatNational().replace(/\D/g, ""));
81
+ };
82
+ 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.2",
3
+ "version": "2.33.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.2",
18
+ "@routr/common": "^2.33.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": "4781133850af7e3cf9daa3b538c385836422d27e"
40
+ "gitHead": "ae7d4c7b239c7ba0c9283c010cb16f388980fa5d"
40
41
  }