@sinch/functions-runtime 0.5.19 → 0.5.21

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-DX63ULIO.js";
20
+ } from "../chunk-GHTTRPRF.js";
21
21
 
22
22
  // ../../node_modules/dotenv/package.json
23
23
  var require_package = __commonJS({
@@ -59995,6 +59995,7 @@ var DEFAULT_HOST = "https://voice.api.sinch.com";
59995
59995
  var FETCH_TIMEOUT_MS = 1e4;
59996
59996
  var HttpTransport = class {
59997
59997
  projectId;
59998
+ serviceId;
59998
59999
  host;
59999
60000
  fetchImpl;
60000
60001
  // The Access Key pair is held in ECMAScript private fields, not TypeScript `private` ones.
@@ -60006,6 +60007,7 @@ var HttpTransport = class {
60006
60007
  #keySecret;
60007
60008
  constructor(options) {
60008
60009
  this.projectId = options.projectId;
60010
+ this.serviceId = options.serviceId;
60009
60011
  this.#keyId = options.keyId;
60010
60012
  this.#keySecret = options.keySecret;
60011
60013
  this.host = (options.host ?? DEFAULT_HOST).replace(/\/$/, "");
@@ -60021,7 +60023,8 @@ var HttpTransport = class {
60021
60023
  return { transport: "HttpTransport", projectId: this.projectId, host: this.host };
60022
60024
  }
60023
60025
  async createCall(body) {
60024
- const response = await this.request("POST", "calls", body);
60026
+ const path6 = this.serviceId ? `calls?serviceId=${encodeURIComponent(this.serviceId)}` : "calls";
60027
+ const response = await this.request("POST", path6, body);
60025
60028
  return response;
60026
60029
  }
60027
60030
  async patchCall(target, commands2) {
@@ -60804,6 +60807,9 @@ var Client = class {
60804
60807
  projectId: options.projectId ?? env.PROJECT_ID ?? context?.config?.projectId ?? "",
60805
60808
  keyId: options.keyId ?? env.PROJECT_ID_API_KEY ?? "",
60806
60809
  keySecret: options.keySecret ?? env.PROJECT_ID_API_SECRET ?? "",
60810
+ ...(options.serviceId ?? env.VOICE_SERVICE_ID) !== void 0 && {
60811
+ serviceId: options.serviceId ?? env.VOICE_SERVICE_ID
60812
+ },
60807
60813
  ...options.host !== void 0 && { host: options.host },
60808
60814
  ...options.fetch !== void 0 && { fetch: options.fetch }
60809
60815
  });
@@ -61221,9 +61227,23 @@ function attachVoiceClient(context, v1) {
61221
61227
  });
61222
61228
  return context;
61223
61229
  }
61230
+ function bindHandler(handler, context, request) {
61231
+ return handler ? (event) => handler(event, context, request) : void 0;
61232
+ }
61224
61233
  function onCall(handlers) {
61225
- return async (context, event) => {
61226
- const response = await context.voice.onCall(handlers)(event);
61234
+ return async (context, event, request) => {
61235
+ const bind2 = (handler) => bindHandler(handler, context, request);
61236
+ const bound = {
61237
+ incoming: bind2(handlers.incoming),
61238
+ answered: bind2(handlers.answered),
61239
+ manage: bind2(handlers.manage),
61240
+ completed: bind2(handlers.completed),
61241
+ fallback: bind2(handlers.fallback),
61242
+ ...handlers.webhooks && {
61243
+ webhooks: Object.fromEntries(Object.entries(handlers.webhooks).map(([name, handler]) => [name, (event2) => handler(event2, context, request)]))
61244
+ }
61245
+ };
61246
+ const response = await context.voice.onCall(bound)(event);
61227
61247
  if (!response) {
61228
61248
  return void 0;
61229
61249
  }
@@ -61900,12 +61920,12 @@ async function handleVoiceV1Callback(functionName, userFunction, context, callba
61900
61920
  }
61901
61921
  return formatSvamlResponse(result2, functionName);
61902
61922
  }
61903
- async function handleVoiceWebhook(userFunction, context, event, logger) {
61923
+ async function handleVoiceWebhook(userFunction, context, event, request, logger) {
61904
61924
  const handler = userFunction[VOICE_WEBHOOK_HANDLER];
61905
61925
  if (!handler || typeof handler !== "function") {
61906
61926
  throw new Error(`No export '${VOICE_WEBHOOK_HANDLER}' found for Voice v2 event '${event?.event}'. Export one built with onCall({ incoming: ... }), or drop VOICE_SERVICE_ID to stay on the v1 ice/ace/pie/dice callbacks.`);
61907
61927
  }
61908
- const result2 = await handler(context, event);
61928
+ const result2 = await handler(context, event, request);
61909
61929
  if (logger) {
61910
61930
  logger("Function result:", result2);
61911
61931
  }
@@ -62084,6 +62104,14 @@ function setupRequestHandler(app, options = {}) {
62084
62104
  onRequestStart({ functionName, req });
62085
62105
  const context = buildContext(req);
62086
62106
  const userFunction = await Promise.resolve(loadUserFunction());
62107
+ const request = {
62108
+ method: req.method,
62109
+ path: req.path,
62110
+ query: req.query,
62111
+ headers: req.headers,
62112
+ body: req.body,
62113
+ params: req.params
62114
+ };
62087
62115
  let response;
62088
62116
  if (isVoiceV1Callback(functionName)) {
62089
62117
  const validation = validateVoiceV1Request(req.body);
@@ -62096,16 +62124,8 @@ function setupRequestHandler(app, options = {}) {
62096
62124
  }
62097
62125
  response = await handleVoiceV1Callback(functionName, userFunction, context, req.body, logger);
62098
62126
  } else if (functionName === VOICE_WEBHOOK_HANDLER && isVoiceWebhook(req.body)) {
62099
- response = await handleVoiceWebhook(userFunction, context, req.body, logger);
62127
+ response = await handleVoiceWebhook(userFunction, context, req.body, request, logger);
62100
62128
  } else {
62101
- const request = {
62102
- method: req.method,
62103
- path: req.path,
62104
- query: req.query,
62105
- headers: req.headers,
62106
- body: req.body,
62107
- params: req.params
62108
- };
62109
62129
  response = await handleCustomEndpoint(functionName, userFunction, context, request, logger);
62110
62130
  }
62111
62131
  res.status(response.statusCode);
@@ -70136,10 +70156,17 @@ var {
70136
70156
  // src/tunnel/webhook-config.ts
70137
70157
  var import_sdk_core2 = __toESM(require_dist11(), 1);
70138
70158
  import { randomBytes } from "crypto";
70139
- var SINCH_FN_URL_PATTERN = /\.fn(-\w+)?\.sinch\.com/;
70159
+ var SINCH_FN_URL_PATTERN = /\.fn(-\w+)?\.sinch\.(?:com|io)/;
70140
70160
  function isOurWebhook(target) {
70141
70161
  return !!target && SINCH_FN_URL_PATTERN.test(target);
70142
70162
  }
70163
+ var FUNCTIONS_DOMAIN_DEFAULT = "fn.sinch.com";
70164
+ function deployedWebhookTarget(functionName) {
70165
+ const injected = process.env.FUNCTION_URL;
70166
+ if (injected) return `${injected.replace(/\/+$/, "")}/webhook/conversation`;
70167
+ const domain = process.env.SINCH_FUNCTIONS_DOMAIN || FUNCTIONS_DOMAIN_DEFAULT;
70168
+ return `https://${functionName}.${domain}/webhook/conversation`;
70169
+ }
70143
70170
  function isTunnelUrl(target) {
70144
70171
  return !!target && target.includes("tunnel.fn");
70145
70172
  }
@@ -70207,7 +70234,7 @@ async function cleanupConversationWebhook(config) {
70207
70234
  if (!restoreTarget || isTunnelUrl(restoreTarget)) {
70208
70235
  const functionName = process.env.FUNCTION_NAME || process.env.FUNCTION_ID;
70209
70236
  if (functionName) {
70210
- restoreTarget = `https://${functionName}.fn-dev.sinch.com/webhook/conversation`;
70237
+ restoreTarget = deployedWebhookTarget(functionName);
70211
70238
  console.log(`\u{1F527} Derived restore target from env: ${restoreTarget}`);
70212
70239
  } else {
70213
70240
  console.log("\u26A0\uFE0F Cannot restore webhook \u2014 no FUNCTION_NAME or FUNCTION_ID available");
@@ -71776,4 +71803,4 @@ serve-static/index.js:
71776
71803
  * MIT Licensed
71777
71804
  *)
71778
71805
  */
71779
- //# sourceMappingURL=chunk-DX63ULIO.js.map
71806
+ //# sourceMappingURL=chunk-GHTTRPRF.js.map