@absolutejs/sync 1.7.7 → 1.7.8

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.
@@ -42,7 +42,7 @@ export { defineSchedule } from './schedule';
42
42
  export type { ScheduleContext, ScheduleDefinition } from './schedule';
43
43
  export { defineMutation } from './mutation';
44
44
  export type { MutationActions, MutationDefinition, MutationHandler, TableWriter, TransactionRunner } from './mutation';
45
- export type { HandlerMetricsHook, HandlerMetricsRecord, SandboxConfig } from './sandbox';
45
+ export type { BridgeFetchConfig, BridgeFetchEndpoint, BridgeFetchResponse, HandlerMetricsHook, HandlerMetricsRecord, SandboxConfig } from './sandbox';
46
46
  export { exponentialBackoff, isSerializationFailure, RetriesExhaustedError } from './retry';
47
47
  export type { ExponentialBackoffOptions, RetryPolicy } from './retry';
48
48
  export { CdcConsumerSlowError, createSyncEngine, MissedChangesError, SchemaError, UnauthorizedError } from './syncEngine';
@@ -1106,12 +1106,13 @@ var wrap = (source) => `
1106
1106
  update: (table, data) => __dispatch(__callId, 'update', table, data),
1107
1107
  delete: (table, row) => __dispatch(__callId, 'delete', table, row),
1108
1108
  change: (collection, change) => __dispatch(__callId, 'change', collection, change),
1109
- now: () => __dispatch(__callId, 'now')
1109
+ now: () => __dispatch(__callId, 'now'),
1110
+ fetch: (url, init) => __dispatch(__callId, 'fetch', url, init)
1110
1111
  };
1111
1112
  return userFn(args, ctx, actions);
1112
1113
  }
1113
1114
  `;
1114
- var compile = async (source, config) => {
1115
+ var compile = async (source, config, bridgeFetch) => {
1115
1116
  const { createIsolate, Reference } = await loadIsolatedJsc();
1116
1117
  const isolate = await createIsolate({
1117
1118
  backend: config.backend ?? "auto",
@@ -1135,6 +1136,8 @@ var compile = async (source, config) => {
1135
1136
  return a.change(rest[0], rest[1]);
1136
1137
  case "now":
1137
1138
  return a.now();
1139
+ case "fetch":
1140
+ return runBridgeFetch(bridgeFetch, rest[0], rest[1]);
1138
1141
  default:
1139
1142
  throw new Error(`unknown sandbox action op: ${String(op)}`);
1140
1143
  }
@@ -1150,8 +1153,57 @@ var compile = async (source, config) => {
1150
1153
  timeoutMs: config.timeout ?? 5000
1151
1154
  };
1152
1155
  };
1153
- var makeSandboxedHandler = (source, config = {}, metricsHook) => {
1156
+ var runBridgeFetch = async (config, url, init) => {
1157
+ if (config === undefined) {
1158
+ throw new Error("actions.fetch called but the engine has no `bridgeFetch` config \u2014 " + "pass `bridgeFetch: { ... }` to createSyncEngine.");
1159
+ }
1160
+ let parsed;
1161
+ try {
1162
+ parsed = new URL(url);
1163
+ } catch {
1164
+ throw new Error(`actions.fetch: invalid URL "${String(url)}"`);
1165
+ }
1166
+ const endpoint = config[parsed.hostname] ?? (Object.prototype.hasOwnProperty.call(config, "*") ? config["*"] : undefined);
1167
+ if (endpoint === undefined) {
1168
+ throw new Error(`actions.fetch: hostname "${parsed.hostname}" is not allowlisted in bridgeFetch config`);
1169
+ }
1170
+ const headers = { ...endpoint.headers ?? {} };
1171
+ if (init?.headers !== undefined) {
1172
+ const incoming = init.headers;
1173
+ for (const [name, value] of Object.entries(incoming)) {
1174
+ if (name.toLowerCase() === "authorization")
1175
+ continue;
1176
+ headers[name] = value;
1177
+ }
1178
+ }
1179
+ if (endpoint.authorization !== undefined) {
1180
+ let auth;
1181
+ try {
1182
+ auth = await endpoint.authorization();
1183
+ } catch {
1184
+ throw new Error("actions.fetch: authorization callback failed");
1185
+ }
1186
+ headers.Authorization = auth;
1187
+ }
1188
+ const response = await fetch(url, { ...init, headers });
1189
+ const responseHeaders = {};
1190
+ response.headers.forEach((value, name) => {
1191
+ responseHeaders[name] = value;
1192
+ });
1193
+ const body = await response.text();
1194
+ return {
1195
+ body,
1196
+ headers: responseHeaders,
1197
+ ok: response.ok,
1198
+ status: response.status,
1199
+ statusText: response.statusText,
1200
+ url: response.url
1201
+ };
1202
+ };
1203
+ var makeSandboxedHandler = (source, config = {}, engineExtras) => {
1154
1204
  let pending;
1205
+ const metricsHook = engineExtras?.metricsHook;
1206
+ const bridgeFetch = engineExtras?.bridgeFetch;
1155
1207
  const getCompiled = async () => {
1156
1208
  if (pending !== undefined) {
1157
1209
  const compiled = await pending;
@@ -1159,7 +1211,7 @@ var makeSandboxedHandler = (source, config = {}, metricsHook) => {
1159
1211
  return compiled;
1160
1212
  pending = undefined;
1161
1213
  }
1162
- pending = compile(source, config);
1214
+ pending = compile(source, config, bridgeFetch);
1163
1215
  return pending;
1164
1216
  };
1165
1217
  return async (args, ctx, actions) => {
@@ -2208,9 +2260,12 @@ var createSyncEngine = (options = {}) => {
2208
2260
  }
2209
2261
  mutations.set(mutation.name, mutation);
2210
2262
  if (mutation.sandboxedHandler !== undefined) {
2211
- sandboxRunners.set(mutation.name, makeSandboxedHandler(mutation.sandboxedHandler, mutation.sandbox, options.handlerMetrics === undefined ? undefined : {
2212
- mutationName: mutation.name,
2213
- onMetrics: options.handlerMetrics
2263
+ sandboxRunners.set(mutation.name, makeSandboxedHandler(mutation.sandboxedHandler, mutation.sandbox, {
2264
+ bridgeFetch: options.bridgeFetch,
2265
+ metricsHook: options.handlerMetrics === undefined ? undefined : {
2266
+ mutationName: mutation.name,
2267
+ onMetrics: options.handlerMetrics
2268
+ }
2214
2269
  }));
2215
2270
  }
2216
2271
  },
@@ -2835,5 +2890,5 @@ export {
2835
2890
  CdcConsumerSlowError
2836
2891
  };
2837
2892
 
2838
- //# debugId=40AF85BFFA0EA08364756E2164756E21
2893
+ //# debugId=E4250C6CF5A6208E64756E2164756E21
2839
2894
  //# sourceMappingURL=index.js.map