@absolutejs/sync 1.7.5 → 1.7.7

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 { SandboxConfig } from './sandbox';
45
+ export type { 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,7 +1105,8 @@ 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')
1109
1110
  };
1110
1111
  return userFn(args, ctx, actions);
1111
1112
  }
@@ -1132,6 +1133,8 @@ var compile = async (source, config) => {
1132
1133
  return a.delete(rest[0], rest[1]);
1133
1134
  case "change":
1134
1135
  return a.change(rest[0], rest[1]);
1136
+ case "now":
1137
+ return a.now();
1135
1138
  default:
1136
1139
  throw new Error(`unknown sandbox action op: ${String(op)}`);
1137
1140
  }
@@ -1147,7 +1150,7 @@ var compile = async (source, config) => {
1147
1150
  timeoutMs: config.timeout ?? 5000
1148
1151
  };
1149
1152
  };
1150
- var makeSandboxedHandler = (source, config = {}) => {
1153
+ var makeSandboxedHandler = (source, config = {}, metricsHook) => {
1151
1154
  let pending;
1152
1155
  const getCompiled = async () => {
1153
1156
  if (pending !== undefined) {
@@ -1163,15 +1166,59 @@ var makeSandboxedHandler = (source, config = {}) => {
1163
1166
  const compiled = await getCompiled();
1164
1167
  const callId = compiled.nextCallId++;
1165
1168
  compiled.callMap.set(callId, actions);
1169
+ if (metricsHook === undefined) {
1170
+ try {
1171
+ return await compiled.callable.call([callId, args, ctx], {
1172
+ timeout: compiled.timeoutMs
1173
+ });
1174
+ } finally {
1175
+ compiled.callMap.delete(callId);
1176
+ }
1177
+ }
1178
+ const startedAt = performance.now();
1179
+ const id = makeRandomId();
1166
1180
  try {
1167
- return await compiled.callable.call([callId, args, ctx], {
1168
- timeout: compiled.timeoutMs
1181
+ const { result, metrics } = await compiled.callable.callWithMetrics([callId, args, ctx], { timeout: compiled.timeoutMs });
1182
+ fireMetrics(metricsHook.onMetrics, {
1183
+ cpuMs: metrics.cpuMs,
1184
+ durationMs: performance.now() - startedAt,
1185
+ heapBytes: metrics.heapBytes,
1186
+ id,
1187
+ mutationName: metricsHook.mutationName,
1188
+ ok: true,
1189
+ timestamp: Date.now()
1190
+ });
1191
+ return result;
1192
+ } catch (error) {
1193
+ fireMetrics(metricsHook.onMetrics, {
1194
+ cpuMs: 0,
1195
+ durationMs: performance.now() - startedAt,
1196
+ errorMessage: error instanceof Error ? error.message : String(error),
1197
+ errorName: error instanceof Error ? error.name : "Error",
1198
+ heapBytes: 0,
1199
+ id,
1200
+ mutationName: metricsHook.mutationName,
1201
+ ok: false,
1202
+ timestamp: Date.now()
1169
1203
  });
1204
+ throw error;
1170
1205
  } finally {
1171
1206
  compiled.callMap.delete(callId);
1172
1207
  }
1173
1208
  };
1174
1209
  };
1210
+ var makeRandomId = () => `hm_${Date.now().toString(36)}${Math.random().toString(36).slice(2, 10)}`;
1211
+ var fireMetrics = (hook, record) => {
1212
+ let outcome;
1213
+ try {
1214
+ outcome = hook(record);
1215
+ } catch {
1216
+ return;
1217
+ }
1218
+ if (outcome instanceof Promise) {
1219
+ outcome.catch(() => {});
1220
+ }
1221
+ };
1175
1222
 
1176
1223
  // src/engine/syncEngine.ts
1177
1224
  class UnauthorizedError extends Error {
@@ -1603,7 +1650,8 @@ var createSyncEngine = (options = {}) => {
1603
1650
  }
1604
1651
  await writerFor(table).delete(row, ctx, tx);
1605
1652
  buffered.push({ table, change: { op: "delete", row } });
1606
- }
1653
+ },
1654
+ now: () => Date.now()
1607
1655
  };
1608
1656
  return { actions, buffered };
1609
1657
  };
@@ -2160,7 +2208,10 @@ var createSyncEngine = (options = {}) => {
2160
2208
  }
2161
2209
  mutations.set(mutation.name, mutation);
2162
2210
  if (mutation.sandboxedHandler !== undefined) {
2163
- sandboxRunners.set(mutation.name, makeSandboxedHandler(mutation.sandboxedHandler, mutation.sandbox));
2211
+ sandboxRunners.set(mutation.name, makeSandboxedHandler(mutation.sandboxedHandler, mutation.sandbox, options.handlerMetrics === undefined ? undefined : {
2212
+ mutationName: mutation.name,
2213
+ onMetrics: options.handlerMetrics
2214
+ }));
2164
2215
  }
2165
2216
  },
2166
2217
  registerWriter: (table, writer) => {
@@ -2784,5 +2835,5 @@ export {
2784
2835
  CdcConsumerSlowError
2785
2836
  };
2786
2837
 
2787
- //# debugId=8D6F90BB348646A664756E2164756E21
2838
+ //# debugId=40AF85BFFA0EA08364756E2164756E21
2788
2839
  //# sourceMappingURL=index.js.map