@sinch/functions-runtime 0.5.17 → 0.5.19

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.
@@ -17,7 +17,7 @@ import {
17
17
  isVoiceV1Callback,
18
18
  secretsLoader,
19
19
  setupRequestHandler
20
- } from "../chunk-VEB6FXNI.js";
20
+ } from "../chunk-DX63ULIO.js";
21
21
 
22
22
  // ../../node_modules/dotenv/package.json
23
23
  var require_package = __commonJS({
@@ -18129,7 +18129,7 @@ var require_stringify = __commonJS({
18129
18129
  return typeof v === "string" || typeof v === "number" || typeof v === "boolean" || typeof v === "symbol" || typeof v === "bigint";
18130
18130
  };
18131
18131
  var sentinel = {};
18132
- var stringify = function stringify2(object, prefix, generateArrayPrefix, commaRoundTrip, allowEmptyArrays, strictNullHandling, skipNulls, encodeDotInKeys, encoder, filter2, sort, allowDots, serializeDate, format, formatter, encodeValuesOnly, charset, sideChannel) {
18132
+ var stringify2 = function stringify3(object, prefix, generateArrayPrefix, commaRoundTrip, allowEmptyArrays, strictNullHandling, skipNulls, encodeDotInKeys, encoder, filter2, sort, allowDots, serializeDate, format, formatter, encodeValuesOnly, charset, sideChannel) {
18133
18133
  var obj = object;
18134
18134
  var tmpSc = sideChannel;
18135
18135
  var step = 0;
@@ -18205,7 +18205,7 @@ var require_stringify = __commonJS({
18205
18205
  sideChannel.set(object, step);
18206
18206
  var valueSideChannel = getSideChannel();
18207
18207
  valueSideChannel.set(sentinel, sideChannel);
18208
- pushToArray(values, stringify2(
18208
+ pushToArray(values, stringify3(
18209
18209
  value,
18210
18210
  keyPrefix,
18211
18211
  generateArrayPrefix,
@@ -18322,7 +18322,7 @@ var require_stringify = __commonJS({
18322
18322
  if (options.skipNulls && value === null) {
18323
18323
  continue;
18324
18324
  }
18325
- pushToArray(keys, stringify(
18325
+ pushToArray(keys, stringify2(
18326
18326
  value,
18327
18327
  key,
18328
18328
  generateArrayPrefix,
@@ -18646,13 +18646,13 @@ var require_parse = __commonJS({
18646
18646
  var require_lib2 = __commonJS({
18647
18647
  "../../node_modules/qs/lib/index.js"(exports, module) {
18648
18648
  "use strict";
18649
- var stringify = require_stringify();
18649
+ var stringify2 = require_stringify();
18650
18650
  var parse2 = require_parse();
18651
18651
  var formats = require_formats();
18652
18652
  module.exports = {
18653
18653
  formats,
18654
18654
  parse: parse2,
18655
- stringify
18655
+ stringify: stringify2
18656
18656
  };
18657
18657
  }
18658
18658
  });
@@ -20413,7 +20413,7 @@ var require_dist = __commonJS({
20413
20413
  exports.compile = compile;
20414
20414
  exports.match = match;
20415
20415
  exports.pathToRegexp = pathToRegexp;
20416
- exports.stringify = stringify;
20416
+ exports.stringify = stringify2;
20417
20417
  var DEFAULT_DELIMITER = "/";
20418
20418
  var NOOP_VALUE = (value) => value;
20419
20419
  var ID_START = /^[$_\p{ID_Start}]$/u;
@@ -20743,7 +20743,7 @@ var require_dist = __commonJS({
20743
20743
  }
20744
20744
  return value;
20745
20745
  }
20746
- function stringify(data) {
20746
+ function stringify2(data) {
20747
20747
  return stringifyTokens(data.tokens);
20748
20748
  }
20749
20749
  function isNameSafe(name) {
@@ -23916,7 +23916,7 @@ var require_response = __commonJS({
23916
23916
  var escape2 = app.get("json escape");
23917
23917
  var replacer = app.get("json replacer");
23918
23918
  var spaces = app.get("json spaces");
23919
- var body = stringify(obj, replacer, spaces, escape2);
23919
+ var body = stringify2(obj, replacer, spaces, escape2);
23920
23920
  if (!this.get("Content-Type")) {
23921
23921
  this.set("Content-Type", "application/json");
23922
23922
  }
@@ -23927,7 +23927,7 @@ var require_response = __commonJS({
23927
23927
  var escape2 = app.get("json escape");
23928
23928
  var replacer = app.get("json replacer");
23929
23929
  var spaces = app.get("json spaces");
23930
- var body = stringify(obj, replacer, spaces, escape2);
23930
+ var body = stringify2(obj, replacer, spaces, escape2);
23931
23931
  var callback = this.req.query[app.get("jsonp callback name")];
23932
23932
  if (!this.get("Content-Type")) {
23933
23933
  this.set("X-Content-Type-Options", "nosniff");
@@ -24235,7 +24235,7 @@ var require_response = __commonJS({
24235
24235
  }
24236
24236
  file.pipe(res2);
24237
24237
  }
24238
- function stringify(value, replacer, spaces, escape2) {
24238
+ function stringify2(value, replacer, spaces, escape2) {
24239
24239
  var json = replacer || spaces ? JSON.stringify(value, replacer, spaces) : JSON.stringify(value);
24240
24240
  if (escape2 && typeof json === "string") {
24241
24241
  json = json.replace(/[<>&]/g, function(c) {
@@ -59962,6 +59962,34 @@ function collectCredentials(value, found = /* @__PURE__ */ new Set()) {
59962
59962
  return found;
59963
59963
  }
59964
59964
 
59965
+ // ../voice-v2-preview/dist/core/json-order.js
59966
+ var ORDER = /* @__PURE__ */ Symbol("matchOrder");
59967
+ function tagOrder(obj, order) {
59968
+ Object.defineProperty(obj, ORDER, { value: order, enumerable: false });
59969
+ return obj;
59970
+ }
59971
+ function stringify(value) {
59972
+ if (value === void 0) {
59973
+ return "null";
59974
+ }
59975
+ if (value === null || typeof value !== "object") {
59976
+ return JSON.stringify(value);
59977
+ }
59978
+ if (Array.isArray(value)) {
59979
+ return `[${value.map((entry) => stringify(entry)).join(",")}]`;
59980
+ }
59981
+ const record = value;
59982
+ const order = value[ORDER] ?? Object.keys(record);
59983
+ const parts = [];
59984
+ for (const key of order) {
59985
+ const entry = record[key];
59986
+ if (entry === void 0)
59987
+ continue;
59988
+ parts.push(`${JSON.stringify(key)}:${stringify(entry)}`);
59989
+ }
59990
+ return `{${parts.join(",")}}`;
59991
+ }
59992
+
59965
59993
  // ../voice-v2-preview/dist/wire/http.js
59966
59994
  var DEFAULT_HOST = "https://voice.api.sinch.com";
59967
59995
  var FETCH_TIMEOUT_MS = 1e4;
@@ -60012,7 +60040,7 @@ var HttpTransport = class {
60012
60040
  const response = await this.fetchImpl(url2, {
60013
60041
  method,
60014
60042
  headers: this.authHeaders,
60015
- body: JSON.stringify(body),
60043
+ body: stringify(body),
60016
60044
  signal: AbortSignal.timeout(FETCH_TIMEOUT_MS)
60017
60045
  });
60018
60046
  if (!response.ok) {
@@ -60264,6 +60292,14 @@ function resolvePrompt(prompt) {
60264
60292
  }
60265
60293
  return prompt.build();
60266
60294
  }
60295
+ function assertCompilablePattern(pattern) {
60296
+ try {
60297
+ new RegExp(pattern);
60298
+ } catch (err) {
60299
+ const hint = pattern === "*" ? " Use '\\*' to match the DTMF star tone." : " Escape a regular-expression metacharacter you mean literally, e.g. \\* for a literal *.";
60300
+ throw new Error(`Voice v2 menu match key '${pattern}' is not a valid regular expression.${hint} (${err.message})`);
60301
+ }
60302
+ }
60267
60303
  var MenuItemBuilder = class {
60268
60304
  item = {};
60269
60305
  matchOrder = [];
@@ -60342,13 +60378,20 @@ var MenuItemBuilder = class {
60342
60378
  *
60343
60379
  * Keys may be literal digits or regular expressions, and the platform evaluates them in
60344
60380
  * definition order with first match winning — so the order these are written in is part of
60345
- * the behaviour, not decoration. {@link build} refuses to emit a menu whose keys JSON
60346
- * cannot carry in that order; see the error it throws.
60381
+ * the behaviour, not decoration. {@link build} emits `matches` tagged with that order so it
60382
+ * survives `JSON.stringify`, which would otherwise hoist integer-like keys first.
60347
60383
  *
60348
- * @param pattern - Digits, or a regular expression source
60384
+ * The platform compiles every key as a regular expression (anchored, `^(?:key)$`), so a bare
60385
+ * metacharacter such as `*` or `+` is not a literal DTMF tone — it is an invalid pattern that
60386
+ * fails at deploy time. `match` rejects those up front instead. Escape a literal you mean, e.g.
60387
+ * `\*` for the star tone.
60388
+ *
60389
+ * @param pattern - Digits, `#`, or a regular expression source
60349
60390
  * @param flow - Commands to run on a match
60391
+ * @throws If `pattern` does not compile as a regular expression
60350
60392
  */
60351
60393
  match(pattern, flow) {
60394
+ assertCompilablePattern(pattern);
60352
60395
  if (!(pattern in this.matches)) {
60353
60396
  this.matchOrder.push(pattern);
60354
60397
  }
@@ -60364,20 +60407,11 @@ var MenuItemBuilder = class {
60364
60407
  this.item.onFail = resolveFlow(flow);
60365
60408
  return this;
60366
60409
  }
60367
- /**
60368
- * Emit the menu.
60369
- *
60370
- * @throws If the `matches` keys cannot be serialised in the order they were written
60371
- */
60410
+ /** Emit the menu. */
60372
60411
  build() {
60373
60412
  const item = { ...this.item };
60374
60413
  if (this.matchOrder.length > 0) {
60375
- const emitted = Object.keys(this.matches);
60376
- const reordered = emitted.some((key, index) => key !== this.matchOrder[index]);
60377
- if (reordered) {
60378
- throw new Error(`Voice v2 menu matches are evaluated in definition order, but JSON cannot carry these keys in the order they were written (${this.matchOrder.join(", ")} would be sent as ${emitted.join(", ")}). Numeric keys always sort first, ascending. Write them in ascending numeric order, or use a regular-expression key such as '^1$'.`);
60379
- }
60380
- item.matches = { ...this.matches };
60414
+ item.matches = tagOrder({ ...this.matches }, this.matchOrder);
60381
60415
  }
60382
60416
  return item;
60383
60417
  }
@@ -61882,7 +61916,7 @@ async function handleVoiceWebhook(userFunction, context, event, logger) {
61882
61916
  return formatCustomResponse(result2);
61883
61917
  }
61884
61918
  const body = await context.voice.onCall({ fallback: () => result2 })(event);
61885
- return body === void 0 ? { statusCode: 204 } : { statusCode: 200, headers: { "Content-Type": "application/json" }, body };
61919
+ return body === void 0 ? { statusCode: 204 } : { statusCode: 200, headers: { "Content-Type": "application/json" }, body: stringify(body) };
61886
61920
  }
61887
61921
  function isVoiceResult(result2) {
61888
61922
  return Array.isArray(result2) || result2 instanceof CommandBuilder || typeof result2 === "object" && Array.isArray(result2.commands);
@@ -62085,6 +62119,8 @@ function setupRequestHandler(app, options = {}) {
62085
62119
  const isHtmlContent = contentType?.includes("text/html");
62086
62120
  if (response.body === void 0) {
62087
62121
  res.end();
62122
+ } else if (isJsonContent && typeof response.body === "string") {
62123
+ res.send(response.body);
62088
62124
  } else if (isJsonContent) {
62089
62125
  res.json(response.body);
62090
62126
  } else if (isHtmlContent) {
@@ -71338,6 +71374,7 @@ export {
71338
71374
  createUniversalConfig,
71339
71375
  REDACTED,
71340
71376
  redactCredentials,
71377
+ stringify,
71341
71378
  DEFAULT_HOST,
71342
71379
  DEFAULT_SAMPLE_RATE,
71343
71380
  DEFAULT_VOICE,
@@ -71739,4 +71776,4 @@ serve-static/index.js:
71739
71776
  * MIT Licensed
71740
71777
  *)
71741
71778
  */
71742
- //# sourceMappingURL=chunk-VEB6FXNI.js.map
71779
+ //# sourceMappingURL=chunk-DX63ULIO.js.map