@absolutejs/sync 1.7.4 → 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.
- package/dist/engine/index.d.ts +1 -1
- package/dist/engine/index.js +86 -26
- package/dist/engine/index.js.map +4 -4
- package/dist/engine/sandbox.d.ts +79 -10
- package/dist/engine/syncEngine.d.ts +18 -0
- package/dist/index.js +86 -26
- package/dist/index.js.map +4 -4
- package/package.json +1 -1
package/dist/engine/index.d.ts
CHANGED
|
@@ -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';
|
package/dist/engine/index.js
CHANGED
|
@@ -1093,7 +1093,7 @@ var loadIsolatedJsc = async () => {
|
|
|
1093
1093
|
}
|
|
1094
1094
|
};
|
|
1095
1095
|
var wrap = (source) => `
|
|
1096
|
-
function (args, ctx
|
|
1096
|
+
function (__callId, args, ctx) {
|
|
1097
1097
|
const userFn = (${source});
|
|
1098
1098
|
if (typeof userFn !== 'function') {
|
|
1099
1099
|
throw new Error(
|
|
@@ -1102,30 +1102,52 @@ var wrap = (source) => `
|
|
|
1102
1102
|
);
|
|
1103
1103
|
}
|
|
1104
1104
|
const actions = {
|
|
1105
|
-
insert: (table, data) => __dispatch('insert', table, data),
|
|
1106
|
-
update: (table, data) => __dispatch('update', table, data),
|
|
1107
|
-
delete: (table, row) => __dispatch('delete', table, row),
|
|
1108
|
-
change: (collection, change) => __dispatch('change', collection, change)
|
|
1105
|
+
insert: (table, data) => __dispatch(__callId, 'insert', table, data),
|
|
1106
|
+
update: (table, data) => __dispatch(__callId, 'update', table, data),
|
|
1107
|
+
delete: (table, row) => __dispatch(__callId, 'delete', table, row),
|
|
1108
|
+
change: (collection, change) => __dispatch(__callId, 'change', collection, change)
|
|
1109
1109
|
};
|
|
1110
1110
|
return userFn(args, ctx, actions);
|
|
1111
1111
|
}
|
|
1112
1112
|
`;
|
|
1113
1113
|
var compile = async (source, config) => {
|
|
1114
|
-
const { createIsolate } = await loadIsolatedJsc();
|
|
1114
|
+
const { createIsolate, Reference } = await loadIsolatedJsc();
|
|
1115
1115
|
const isolate = await createIsolate({
|
|
1116
1116
|
backend: config.backend ?? "auto",
|
|
1117
1117
|
memoryLimit: config.memoryLimit ?? 32
|
|
1118
1118
|
});
|
|
1119
1119
|
const context = await isolate.createContext();
|
|
1120
|
+
const callMap = new Map;
|
|
1121
|
+
const dispatch = new Reference((callId, op, ...rest) => {
|
|
1122
|
+
const a = callMap.get(callId);
|
|
1123
|
+
if (a === undefined) {
|
|
1124
|
+
throw new Error(`__dispatch invoked for orphan callId ${String(callId)}`);
|
|
1125
|
+
}
|
|
1126
|
+
switch (op) {
|
|
1127
|
+
case "insert":
|
|
1128
|
+
return a.insert(rest[0], rest[1]);
|
|
1129
|
+
case "update":
|
|
1130
|
+
return a.update(rest[0], rest[1]);
|
|
1131
|
+
case "delete":
|
|
1132
|
+
return a.delete(rest[0], rest[1]);
|
|
1133
|
+
case "change":
|
|
1134
|
+
return a.change(rest[0], rest[1]);
|
|
1135
|
+
default:
|
|
1136
|
+
throw new Error(`unknown sandbox action op: ${String(op)}`);
|
|
1137
|
+
}
|
|
1138
|
+
});
|
|
1139
|
+
await context.setGlobal("__dispatch", dispatch);
|
|
1120
1140
|
const callable = await context.compileCallable(wrap(source));
|
|
1121
1141
|
return {
|
|
1122
1142
|
callable,
|
|
1143
|
+
callMap,
|
|
1123
1144
|
context,
|
|
1124
1145
|
isolate,
|
|
1146
|
+
nextCallId: 1,
|
|
1125
1147
|
timeoutMs: config.timeout ?? 5000
|
|
1126
1148
|
};
|
|
1127
1149
|
};
|
|
1128
|
-
var makeSandboxedHandler = (source, config = {}) => {
|
|
1150
|
+
var makeSandboxedHandler = (source, config = {}, metricsHook) => {
|
|
1129
1151
|
let pending;
|
|
1130
1152
|
const getCompiled = async () => {
|
|
1131
1153
|
if (pending !== undefined) {
|
|
@@ -1138,27 +1160,62 @@ var makeSandboxedHandler = (source, config = {}) => {
|
|
|
1138
1160
|
return pending;
|
|
1139
1161
|
};
|
|
1140
1162
|
return async (args, ctx, actions) => {
|
|
1141
|
-
const { Reference } = await loadIsolatedJsc();
|
|
1142
1163
|
const compiled = await getCompiled();
|
|
1143
|
-
const
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
return actions.change(rest[0], rest[1]);
|
|
1153
|
-
default:
|
|
1154
|
-
throw new Error(`unknown sandbox action op: ${String(op)}`);
|
|
1164
|
+
const callId = compiled.nextCallId++;
|
|
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);
|
|
1155
1173
|
}
|
|
1156
|
-
}
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1174
|
+
}
|
|
1175
|
+
const startedAt = performance.now();
|
|
1176
|
+
const id = makeRandomId();
|
|
1177
|
+
try {
|
|
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()
|
|
1200
|
+
});
|
|
1201
|
+
throw error;
|
|
1202
|
+
} finally {
|
|
1203
|
+
compiled.callMap.delete(callId);
|
|
1204
|
+
}
|
|
1160
1205
|
};
|
|
1161
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
|
+
};
|
|
1162
1219
|
|
|
1163
1220
|
// src/engine/syncEngine.ts
|
|
1164
1221
|
class UnauthorizedError extends Error {
|
|
@@ -2147,7 +2204,10 @@ var createSyncEngine = (options = {}) => {
|
|
|
2147
2204
|
}
|
|
2148
2205
|
mutations.set(mutation.name, mutation);
|
|
2149
2206
|
if (mutation.sandboxedHandler !== undefined) {
|
|
2150
|
-
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
|
+
}));
|
|
2151
2211
|
}
|
|
2152
2212
|
},
|
|
2153
2213
|
registerWriter: (table, writer) => {
|
|
@@ -2771,5 +2831,5 @@ export {
|
|
|
2771
2831
|
CdcConsumerSlowError
|
|
2772
2832
|
};
|
|
2773
2833
|
|
|
2774
|
-
//# debugId=
|
|
2834
|
+
//# debugId=8409EFFBE552287464756E2164756E21
|
|
2775
2835
|
//# sourceMappingURL=index.js.map
|