@absolutejs/sync 1.7.5 → 1.7.6

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';
@@ -1147,7 +1147,7 @@ var compile = async (source, config) => {
1147
1147
  timeoutMs: config.timeout ?? 5000
1148
1148
  };
1149
1149
  };
1150
- var makeSandboxedHandler = (source, config = {}) => {
1150
+ var makeSandboxedHandler = (source, config = {}, metricsHook) => {
1151
1151
  let pending;
1152
1152
  const getCompiled = async () => {
1153
1153
  if (pending !== undefined) {
@@ -1163,15 +1163,59 @@ var makeSandboxedHandler = (source, config = {}) => {
1163
1163
  const compiled = await getCompiled();
1164
1164
  const callId = compiled.nextCallId++;
1165
1165
  compiled.callMap.set(callId, actions);
1166
+ if (metricsHook === undefined) {
1167
+ try {
1168
+ return await compiled.callable.call([callId, args, ctx], {
1169
+ timeout: compiled.timeoutMs
1170
+ });
1171
+ } finally {
1172
+ compiled.callMap.delete(callId);
1173
+ }
1174
+ }
1175
+ const startedAt = performance.now();
1176
+ const id = makeRandomId();
1166
1177
  try {
1167
- return await compiled.callable.call([callId, args, ctx], {
1168
- timeout: compiled.timeoutMs
1178
+ const { result, metrics } = await compiled.callable.callWithMetrics([callId, args, ctx], { timeout: compiled.timeoutMs });
1179
+ fireMetrics(metricsHook.onMetrics, {
1180
+ cpuMs: metrics.cpuMs,
1181
+ durationMs: performance.now() - startedAt,
1182
+ heapBytes: metrics.heapBytes,
1183
+ id,
1184
+ mutationName: metricsHook.mutationName,
1185
+ ok: true,
1186
+ timestamp: Date.now()
1187
+ });
1188
+ return result;
1189
+ } catch (error) {
1190
+ fireMetrics(metricsHook.onMetrics, {
1191
+ cpuMs: 0,
1192
+ durationMs: performance.now() - startedAt,
1193
+ errorMessage: error instanceof Error ? error.message : String(error),
1194
+ errorName: error instanceof Error ? error.name : "Error",
1195
+ heapBytes: 0,
1196
+ id,
1197
+ mutationName: metricsHook.mutationName,
1198
+ ok: false,
1199
+ timestamp: Date.now()
1169
1200
  });
1201
+ throw error;
1170
1202
  } finally {
1171
1203
  compiled.callMap.delete(callId);
1172
1204
  }
1173
1205
  };
1174
1206
  };
1207
+ var makeRandomId = () => `hm_${Date.now().toString(36)}${Math.random().toString(36).slice(2, 10)}`;
1208
+ var fireMetrics = (hook, record) => {
1209
+ let outcome;
1210
+ try {
1211
+ outcome = hook(record);
1212
+ } catch {
1213
+ return;
1214
+ }
1215
+ if (outcome instanceof Promise) {
1216
+ outcome.catch(() => {});
1217
+ }
1218
+ };
1175
1219
 
1176
1220
  // src/engine/syncEngine.ts
1177
1221
  class UnauthorizedError extends Error {
@@ -2160,7 +2204,10 @@ var createSyncEngine = (options = {}) => {
2160
2204
  }
2161
2205
  mutations.set(mutation.name, mutation);
2162
2206
  if (mutation.sandboxedHandler !== undefined) {
2163
- sandboxRunners.set(mutation.name, makeSandboxedHandler(mutation.sandboxedHandler, mutation.sandbox));
2207
+ sandboxRunners.set(mutation.name, makeSandboxedHandler(mutation.sandboxedHandler, mutation.sandbox, options.handlerMetrics === undefined ? undefined : {
2208
+ mutationName: mutation.name,
2209
+ onMetrics: options.handlerMetrics
2210
+ }));
2164
2211
  }
2165
2212
  },
2166
2213
  registerWriter: (table, writer) => {
@@ -2784,5 +2831,5 @@ export {
2784
2831
  CdcConsumerSlowError
2785
2832
  };
2786
2833
 
2787
- //# debugId=8D6F90BB348646A664756E2164756E21
2834
+ //# debugId=8409EFFBE552287464756E2164756E21
2788
2835
  //# sourceMappingURL=index.js.map