@absolutejs/sync 1.7.6 → 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';
@@ -1105,12 +1105,14 @@ var wrap = (source) => `
1105
1105
  insert: (table, data) => __dispatch(__callId, 'insert', table, data),
1106
1106
  update: (table, data) => __dispatch(__callId, 'update', table, data),
1107
1107
  delete: (table, row) => __dispatch(__callId, 'delete', table, row),
1108
- change: (collection, change) => __dispatch(__callId, 'change', collection, change)
1108
+ change: (collection, change) => __dispatch(__callId, 'change', collection, change),
1109
+ now: () => __dispatch(__callId, 'now'),
1110
+ fetch: (url, init) => __dispatch(__callId, 'fetch', url, init)
1109
1111
  };
1110
1112
  return userFn(args, ctx, actions);
1111
1113
  }
1112
1114
  `;
1113
- var compile = async (source, config) => {
1115
+ var compile = async (source, config, bridgeFetch) => {
1114
1116
  const { createIsolate, Reference } = await loadIsolatedJsc();
1115
1117
  const isolate = await createIsolate({
1116
1118
  backend: config.backend ?? "auto",
@@ -1132,6 +1134,10 @@ var compile = async (source, config) => {
1132
1134
  return a.delete(rest[0], rest[1]);
1133
1135
  case "change":
1134
1136
  return a.change(rest[0], rest[1]);
1137
+ case "now":
1138
+ return a.now();
1139
+ case "fetch":
1140
+ return runBridgeFetch(bridgeFetch, rest[0], rest[1]);
1135
1141
  default:
1136
1142
  throw new Error(`unknown sandbox action op: ${String(op)}`);
1137
1143
  }
@@ -1147,8 +1153,57 @@ var compile = async (source, config) => {
1147
1153
  timeoutMs: config.timeout ?? 5000
1148
1154
  };
1149
1155
  };
1150
- 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) => {
1151
1204
  let pending;
1205
+ const metricsHook = engineExtras?.metricsHook;
1206
+ const bridgeFetch = engineExtras?.bridgeFetch;
1152
1207
  const getCompiled = async () => {
1153
1208
  if (pending !== undefined) {
1154
1209
  const compiled = await pending;
@@ -1156,7 +1211,7 @@ var makeSandboxedHandler = (source, config = {}, metricsHook) => {
1156
1211
  return compiled;
1157
1212
  pending = undefined;
1158
1213
  }
1159
- pending = compile(source, config);
1214
+ pending = compile(source, config, bridgeFetch);
1160
1215
  return pending;
1161
1216
  };
1162
1217
  return async (args, ctx, actions) => {
@@ -1647,7 +1702,8 @@ var createSyncEngine = (options = {}) => {
1647
1702
  }
1648
1703
  await writerFor(table).delete(row, ctx, tx);
1649
1704
  buffered.push({ table, change: { op: "delete", row } });
1650
- }
1705
+ },
1706
+ now: () => Date.now()
1651
1707
  };
1652
1708
  return { actions, buffered };
1653
1709
  };
@@ -2204,9 +2260,12 @@ var createSyncEngine = (options = {}) => {
2204
2260
  }
2205
2261
  mutations.set(mutation.name, mutation);
2206
2262
  if (mutation.sandboxedHandler !== undefined) {
2207
- sandboxRunners.set(mutation.name, makeSandboxedHandler(mutation.sandboxedHandler, mutation.sandbox, options.handlerMetrics === undefined ? undefined : {
2208
- mutationName: mutation.name,
2209
- 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
+ }
2210
2269
  }));
2211
2270
  }
2212
2271
  },
@@ -2831,5 +2890,5 @@ export {
2831
2890
  CdcConsumerSlowError
2832
2891
  };
2833
2892
 
2834
- //# debugId=8409EFFBE552287464756E2164756E21
2893
+ //# debugId=E4250C6CF5A6208E64756E2164756E21
2835
2894
  //# sourceMappingURL=index.js.map