@siphon-project/control 0.2.0 → 0.4.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.
package/dist/index.js CHANGED
@@ -112,6 +112,11 @@ var SipVerb = {
112
112
  Hangup: "hangup",
113
113
  Refer: "refer",
114
114
  Route: "route",
115
+ /**
116
+ * Ring targets as B-legs while the caller stays unanswered and the app keeps
117
+ * the channel. Channel-addressed, unlike `originate`, which creates one.
118
+ */
119
+ Dial: "dial",
115
120
  SetHeader: "set_header",
116
121
  GetHeader: "get_header",
117
122
  RemoveHeader: "remove_header",
@@ -126,7 +131,11 @@ var SipVerb = {
126
131
  Hold: "hold",
127
132
  Unhold: "unhold",
128
133
  StreamStart: "stream_start",
129
- StreamStop: "stream_stop"
134
+ StreamStop: "stream_stop",
135
+ /** Record the call's decoded audio to a wav file (siphon-rtp backend only). */
136
+ RecordStart: "record_start",
137
+ /** Stop one recording by id, or every recording on the call. */
138
+ RecordStop: "record_stop"
130
139
  };
131
140
  function sipEventKind(name) {
132
141
  return name;
@@ -625,6 +634,9 @@ function routeTargetToWire(target) {
625
634
  if (target.timeout !== void 0) {
626
635
  object.timeout = target.timeout;
627
636
  }
637
+ if (target.rerouteAfterProgress === true) {
638
+ object.reroute_after_progress = true;
639
+ }
628
640
  return object;
629
641
  }
630
642
  function stringValue(result) {
@@ -636,6 +648,91 @@ function stringValue(result) {
636
648
  }
637
649
  return null;
638
650
  }
651
+ function originateArgs(channel, to, media, options) {
652
+ const args = { channel, to };
653
+ if ("anchor" in media) {
654
+ args.media = true;
655
+ if (media.profile !== void 0) args.profile = media.profile;
656
+ if (media.wsUri !== void 0) args.ws_uri = media.wsUri;
657
+ } else if ("sdp" in media) {
658
+ args.sdp = media.sdp;
659
+ } else {
660
+ args.body = media.body;
661
+ args.content_type = media.contentType;
662
+ }
663
+ if (!options) return args;
664
+ if (options.from !== void 0) args.from = options.from;
665
+ if (options.fromDisplay !== void 0) args.from_display = options.fromDisplay;
666
+ if (options.toDisplay !== void 0) args.to_display = options.toDisplay;
667
+ if (options.nextHop !== void 0) args.next_hop = options.nextHop;
668
+ if (options.pAssertedIdentity !== void 0) {
669
+ args.p_asserted_identity = options.pAssertedIdentity;
670
+ }
671
+ if (options.privacy !== void 0) args.privacy = options.privacy;
672
+ if (options.headers !== void 0) args.headers = options.headers;
673
+ if (options.timeout !== void 0) args.timeout = options.timeout;
674
+ if (options.onLost !== void 0) args.on_lost = options.onLost;
675
+ if (options.vars !== void 0) args.vars = options.vars;
676
+ if (options.sessionTimer !== void 0) {
677
+ const timer = {};
678
+ const { expires, minSe, refresher } = options.sessionTimer;
679
+ if (expires !== void 0) timer.expires = expires;
680
+ if (minSe !== void 0) timer.min_se = minSe;
681
+ if (refresher !== void 0) timer.refresher = refresher;
682
+ args.session_timer = timer;
683
+ }
684
+ return args;
685
+ }
686
+ function dialTargetToWire(target) {
687
+ const uri = target.uri;
688
+ const aor = target.aor;
689
+ const nextHop = target.nextHop;
690
+ const headers = target.headers;
691
+ if (typeof uri === "string" && typeof aor === "string") {
692
+ throw new TypeError(
693
+ 'a dial target names "uri" or "aor", never both \u2014 siphon reads the aor and ignores the uri beside it, so this would place a different call'
694
+ );
695
+ }
696
+ if (typeof aor === "string") {
697
+ if (nextHop !== void 0) {
698
+ throw new TypeError(
699
+ `an aor target takes no "nextHop": each of its branches routes over its own contact's captured flow`
700
+ );
701
+ }
702
+ const object2 = { aor };
703
+ if (headers !== void 0) object2.headers = headers;
704
+ return object2;
705
+ }
706
+ if (typeof uri !== "string") {
707
+ throw new TypeError('a dial target requires a string "uri" or "aor"');
708
+ }
709
+ if (nextHop === void 0 && headers === void 0) return uri;
710
+ const object = { uri };
711
+ if (nextHop !== void 0) object.next_hop = nextHop;
712
+ if (headers !== void 0) object.headers = headers;
713
+ return object;
714
+ }
715
+ function dialArgs(targets, options) {
716
+ const args = { targets: targets.map(dialTargetToWire) };
717
+ if (!options) return args;
718
+ if (options.strategy !== void 0) args.strategy = options.strategy;
719
+ if (options.timeout !== void 0) args.timeout = options.timeout;
720
+ if (options.headers !== void 0) args.headers = options.headers;
721
+ return args;
722
+ }
723
+ function recordStartArgs(options) {
724
+ const args = {};
725
+ if (!options) return args;
726
+ if (options.direction !== void 0) args.direction = options.direction;
727
+ if (options.channels !== void 0) args.channels = options.channels;
728
+ if (options.maxDurationMs !== void 0) args.max_duration_ms = options.maxDurationMs;
729
+ if (options.silenceMs !== void 0) args.silence_ms = options.silenceMs;
730
+ if (options.path !== void 0) args.path = options.path;
731
+ return args;
732
+ }
733
+ function recordStopArgs(recordingId) {
734
+ return recordingId === void 0 ? {} : { recording_id: recordingId };
735
+ }
639
736
  function playArgs(source, options) {
640
737
  const args = {};
641
738
  if ("file" in source) {
@@ -817,7 +914,9 @@ var Call = class {
817
914
  *
818
915
  * `targets` is a non-empty list of carriers tried cheapest-first: each entry
819
916
  * is a bare URI string or a {@link RouteTargetObject}
820
- * (`{uri, nextHop?, headers?, timeout?}`). `strategy` defaults to
917
+ * (`{uri, nextHop?, headers?, timeout?, rerouteAfterProgress?}`). A target
918
+ * that has shown progress keeps the call past its `timeout` unless it sets
919
+ * `rerouteAfterProgress`. `strategy` defaults to
821
920
  * `"sequential"` (v1 supports only sequential/single — anything else rejects
822
921
  * with `code === "unsupported_verb"`). `headers` is applied to every
823
922
  * attempt's B-leg INVITE.
@@ -836,6 +935,58 @@ var Call = class {
836
935
  }
837
936
  return this.sip(SipVerb.Route, args);
838
937
  }
938
+ /**
939
+ * Ring `targets` as B-legs while the caller stays **unanswered** and this
940
+ * application keeps the channel.
941
+ *
942
+ * The difference from {@link Call.route} is who holds the call afterwards.
943
+ * `route` hands it back to siphon, so the app gets `StasisEnd{reason: routed}`
944
+ * and loses it; there is then no way to say "ring the extension, and if nobody
945
+ * answers, voicemail" without answering the caller first — which starts
946
+ * billing before anyone picks up, records an unanswered call as answered, and
947
+ * denies the caller the callee's own ringback.
948
+ *
949
+ * The first 2xx answers the caller with the winner's SDP and the pair becomes
950
+ * an ordinary two-leg call, still owned by this app. A failure or timeout
951
+ * arrives as a `DialFailed` event with the caller still ringing and still
952
+ * parked, so the app decides what happens next.
953
+ *
954
+ * Each target is a URI (dialed as written) or an `{aor}` (forked to every
955
+ * registered contact over its own flow) — see {@link DialTarget}, and note
956
+ * that a target naming both, or neither, throws before anything is sent.
957
+ *
958
+ * ```ts
959
+ * const dialing = await call.dial(
960
+ * [{ aor: "sip:204@pbx.example" }],
961
+ * { strategy: "sequential", timeout: 20 },
962
+ * );
963
+ * ```
964
+ *
965
+ * Rejects with `code === "not_found"` (the call is gone, or no target yielded
966
+ * a branch — an AoR nobody has registered), `"invalid_state"` (already
967
+ * answered, which is what this verb exists to avoid), `"bad_request"` (an
968
+ * empty or malformed target list) or `"unsupported_verb"` (a strategy siphon
969
+ * does not implement).
970
+ */
971
+ async dial(targets, options) {
972
+ const result = await this.sip(SipVerb.Dial, dialArgs(targets, options));
973
+ const text = (name) => {
974
+ const value = result?.[name];
975
+ return typeof value === "string" ? value : void 0;
976
+ };
977
+ const count = (name) => {
978
+ const value = result?.[name];
979
+ return typeof value === "number" ? value : void 0;
980
+ };
981
+ return {
982
+ // The server echoes the channel back; fall back to the one addressed
983
+ // rather than returning an empty id.
984
+ channel: text("channel") ?? this.channelId,
985
+ targets: count("targets"),
986
+ strategy: text("strategy"),
987
+ timeout: count("timeout")
988
+ };
989
+ }
839
990
  /**
840
991
  * Accept a *pending inbound* REFER (surfaced as a `TransferRequested` event)
841
992
  * and run the transfer. `target` overrides the Refer-To URI, `nextHop` steers
@@ -1045,6 +1196,47 @@ var Call = class {
1045
1196
  async streamStop() {
1046
1197
  await this.sip(SipVerb.StreamStop, {});
1047
1198
  }
1199
+ /**
1200
+ * Record this call's decoded audio to a wav file.
1201
+ *
1202
+ * The reply names the `recordingId` a later {@link Call.recordStop}
1203
+ * addresses. It is **not** the file: `RecordingFinished` fires when the file
1204
+ * is *closed* and names its path, which is what an app that mails the audio
1205
+ * has to wait for — acting on this reply races a half-written file.
1206
+ *
1207
+ * `maxDurationMs` and `silenceMs` are the two stop conditions a voicemail
1208
+ * greeting announces, and the engine evaluates both where the decoded audio
1209
+ * already is. Not SIPREC: this writes a file and works on a single-leg,
1210
+ * engine-terminated call, which is what a voicemail box is. siphon-rtp
1211
+ * backend only — rtpengine / rtpproxy reject with
1212
+ * `code === "unsupported_verb"`; a call with no anchored media session
1213
+ * rejects with `"not_found"`.
1214
+ */
1215
+ async recordStart(options) {
1216
+ const result = await this.sip(
1217
+ SipVerb.RecordStart,
1218
+ recordStartArgs(options)
1219
+ );
1220
+ const text = (name) => {
1221
+ const value = result?.[name];
1222
+ return typeof value === "string" ? value : void 0;
1223
+ };
1224
+ return {
1225
+ channel: text("channel") ?? this.channelId,
1226
+ recordingId: text("recording_id")
1227
+ };
1228
+ }
1229
+ /**
1230
+ * Stop the recording named by `recordingId`, or **every** recording on this
1231
+ * call when it is left out.
1232
+ *
1233
+ * Resolves once the engine has accepted the stop; the file is not closed yet.
1234
+ * `RecordingFinished` says that, and carries the path and the reason
1235
+ * (`stopped`, `max_duration`, `silence`, `call_ended`, `error`).
1236
+ */
1237
+ async recordStop(recordingId) {
1238
+ await this.sip(SipVerb.RecordStop, recordStopArgs(recordingId));
1239
+ }
1048
1240
  // --- escape hatch + events --------------------------------------------
1049
1241
  /** Send an arbitrary SIP-adapter verb + args and return the raw result. */
1050
1242
  async command(verb, args) {
@@ -1198,6 +1390,42 @@ var SipClient = class _SipClient {
1198
1390
  describe() {
1199
1391
  return this.client.describe();
1200
1392
  }
1393
+ /**
1394
+ * Place an outbound call under a caller-supplied channel id.
1395
+ *
1396
+ * The one verb that *creates* a channel rather than addressing one, which is
1397
+ * why it lives here and not on {@link Call}. It resolves as soon as the
1398
+ * INVITE is on the wire — the call is `calling`, and the answer, failure or
1399
+ * timeout arrives later as an event on the channel.
1400
+ *
1401
+ * ```ts
1402
+ * const call = await client.originate(
1403
+ * "wake-up-42",
1404
+ * "sip:1001@pbx.example",
1405
+ * { anchor: true },
1406
+ * { from: "sip:alarm@pbx.example", timeout: 20 },
1407
+ * );
1408
+ * ```
1409
+ */
1410
+ async originate(channel, to, media, options) {
1411
+ const result = await this.client.command(
1412
+ MODULE_SIP,
1413
+ "originate",
1414
+ null,
1415
+ originateArgs(channel, to, media, options)
1416
+ );
1417
+ const text = (name) => {
1418
+ const value = result?.[name];
1419
+ return typeof value === "string" ? value : void 0;
1420
+ };
1421
+ return {
1422
+ // The server echoes the id back; fall back to the one we asked for
1423
+ // rather than returning an empty channel a caller cannot address.
1424
+ channel: text("channel") ?? channel,
1425
+ callId: text("call_id"),
1426
+ sipCallId: text("sip_call_id")
1427
+ };
1428
+ }
1201
1429
  /** Send a raw command on any module (the generic escape hatch). */
1202
1430
  command(module, verb, target, args) {
1203
1431
  return this.client.command(module, verb, target, args);
@@ -1246,6 +1474,6 @@ var SipServer = class _SipServer {
1246
1474
  }
1247
1475
  };
1248
1476
 
1249
- export { Call, CallStream, ControlClient, ControlError, ControlServer, DESCRIBE, EventStream, GET_VAR, HELLO, MODULE_SIP, MODULE_SMPP, MODULE_SS7, PROTOCOL_VERSION, RESYNC, SET_VAR, SUBPROTOCOL, SipClient, SipServer, SipVerb, encodeCommand, isBridgeFinal, isTransferFinal, parseInboundFrame, sipEventKind, transferOutcome };
1477
+ export { Call, CallStream, ControlClient, ControlError, ControlServer, DESCRIBE, EventStream, GET_VAR, HELLO, MODULE_SIP, MODULE_SMPP, MODULE_SS7, PROTOCOL_VERSION, RESYNC, SET_VAR, SUBPROTOCOL, SipClient, SipServer, SipVerb, dialArgs, encodeCommand, isBridgeFinal, isTransferFinal, originateArgs, parseInboundFrame, recordStartArgs, recordStopArgs, sipEventKind, transferOutcome };
1250
1478
  //# sourceMappingURL=index.js.map
1251
1479
  //# sourceMappingURL=index.js.map