@routier/core 0.1.0-rc.0 → 0.1.0-rc.1
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/collections/MemoryDataCollection.d.ts +1 -0
- package/dist/collections/index.js +19 -3
- package/dist/collections/index.js.map +1 -1
- package/dist/index.js +66 -21
- package/dist/index.js.map +1 -1
- package/dist/plugins/index.js +80 -18
- package/dist/plugins/index.js.map +1 -1
- package/package.json +1 -1
package/dist/plugins/index.js
CHANGED
|
@@ -320,6 +320,39 @@ function forEach(expression, callback) {
|
|
|
320
320
|
}
|
|
321
321
|
|
|
322
322
|
|
|
323
|
+
}),
|
|
324
|
+
"./src/performance/index.ts":
|
|
325
|
+
/*!**********************************!*\
|
|
326
|
+
!*** ./src/performance/index.ts ***!
|
|
327
|
+
\**********************************/
|
|
328
|
+
(function (__unused_webpack_module, __webpack_exports__, __webpack_require__) {
|
|
329
|
+
__webpack_require__.r(__webpack_exports__);
|
|
330
|
+
__webpack_require__.d(__webpack_exports__, {
|
|
331
|
+
measure: () => (measure),
|
|
332
|
+
now: () => (now)
|
|
333
|
+
});
|
|
334
|
+
const measurements = {};
|
|
335
|
+
const now = () => {
|
|
336
|
+
if (typeof performance !== 'undefined' && performance.now) {
|
|
337
|
+
// Browser or modern Node.js
|
|
338
|
+
return performance.now();
|
|
339
|
+
}
|
|
340
|
+
// Fallback for older environments
|
|
341
|
+
return Date.now();
|
|
342
|
+
};
|
|
343
|
+
const measure = {
|
|
344
|
+
start: (name) => {
|
|
345
|
+
measurements[name] = now();
|
|
346
|
+
},
|
|
347
|
+
end: (name) => {
|
|
348
|
+
const start = measurements[name];
|
|
349
|
+
delete measurements[name];
|
|
350
|
+
const delta = now() - start;
|
|
351
|
+
console.log(`[ROUTIER] - Performance: ${name} took ${delta}`);
|
|
352
|
+
}
|
|
353
|
+
};
|
|
354
|
+
|
|
355
|
+
|
|
323
356
|
}),
|
|
324
357
|
"./src/pipeline/TrampolinePipeline.ts":
|
|
325
358
|
/*!********************************************!*\
|
|
@@ -1073,23 +1106,27 @@ __webpack_require__.d(__webpack_exports__, {
|
|
|
1073
1106
|
OptimisticReplicationDbPlugin: () => (OptimisticReplicationDbPlugin)
|
|
1074
1107
|
});
|
|
1075
1108
|
/* ESM import */var _results__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ../../results */ "./src/results/Result.ts");
|
|
1076
|
-
/* ESM import */var
|
|
1109
|
+
/* ESM import */var _pipeline__WEBPACK_IMPORTED_MODULE_5__ = __webpack_require__(/*! ../../pipeline */ "./src/pipeline/TrampolinePipeline.ts");
|
|
1077
1110
|
/* ESM import */var _query__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ../query */ "./src/plugins/query/Query.ts");
|
|
1078
1111
|
/* ESM import */var _utilities__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../../utilities */ "./src/utilities/uuid.ts");
|
|
1079
|
-
/* ESM import */var
|
|
1112
|
+
/* ESM import */var _utilities__WEBPACK_IMPORTED_MODULE_6__ = __webpack_require__(/*! ../../utilities */ "./src/utilities/replication.ts");
|
|
1080
1113
|
/* ESM import */var _collections__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(/*! ../../collections */ "./src/collections/Changes.ts");
|
|
1114
|
+
/* ESM import */var _performance__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__(/*! ../../performance */ "./src/performance/index.ts");
|
|
1081
1115
|
|
|
1082
1116
|
|
|
1083
1117
|
7;
|
|
1084
1118
|
|
|
1085
1119
|
|
|
1086
1120
|
|
|
1121
|
+
|
|
1087
1122
|
const getMemoryPluginCollectionSize = (plugin, schema) => {
|
|
1088
1123
|
if ("getCollectionSize" in plugin && typeof plugin.getCollectionSize === "function") {
|
|
1089
1124
|
return plugin.getCollectionSize(schema.collectionName);
|
|
1090
1125
|
}
|
|
1091
1126
|
throw new Error("Cannot get size of collection for MemoryPlugin, not an instance of MemoryPlugin");
|
|
1092
1127
|
};
|
|
1128
|
+
const MAX_HYDRATION_WAIT_MS = 60_000; // 60 seconds max wait
|
|
1129
|
+
const HYDRATION_POLL_INTERVAL_MS = 10; // Check ever 10 ms
|
|
1093
1130
|
let hydrationStatus = "hydration-not-started";
|
|
1094
1131
|
class OptimisticReplicationDbPlugin {
|
|
1095
1132
|
plugins;
|
|
@@ -1117,7 +1154,6 @@ class OptimisticReplicationDbPlugin {
|
|
|
1117
1154
|
if (collectionSize === 0 && hydrationStatus === "hydration-not-started") {
|
|
1118
1155
|
// Notify the cache that the db was hydrated right away
|
|
1119
1156
|
hydrationStatus = "hydration-pending";
|
|
1120
|
-
console.log('[ROUTIER] - Optimistic Query', hydrationStatus);
|
|
1121
1157
|
// nothing is hydrated, let's try and hydrate before querying
|
|
1122
1158
|
// Memory plugin might not be hydrated, lets hydrate it for the targeted schema only,
|
|
1123
1159
|
// Other queries will do the same and hydrate if needed
|
|
@@ -1132,28 +1168,20 @@ class OptimisticReplicationDbPlugin {
|
|
|
1132
1168
|
if (sourceResult.ok === _results__WEBPACK_IMPORTED_MODULE_2__.Result.ERROR) {
|
|
1133
1169
|
// Notify that hydration failed
|
|
1134
1170
|
hydrationStatus = "hydration-error";
|
|
1135
|
-
console.log("[ROUTIER] - Hydration Error - Source Query", sourceResult);
|
|
1136
|
-
done(sourceResult);
|
|
1137
|
-
return;
|
|
1138
|
-
}
|
|
1139
|
-
if (sourceResult == null || (Array.isArray(sourceResult.data) && sourceResult.data.length === 0)) {
|
|
1140
|
-
// Notify that hydration had no data
|
|
1141
|
-
hydrationStatus = "hydration-error";
|
|
1142
|
-
console.log("[ROUTIER] - Hydration Error - No Data", sourceResult);
|
|
1143
1171
|
done(sourceResult);
|
|
1144
1172
|
return;
|
|
1145
1173
|
}
|
|
1146
|
-
|
|
1174
|
+
// Source plugin can have no data, still should succeed
|
|
1175
|
+
if (Array.isArray(sourceResult.data.value) === false) {
|
|
1147
1176
|
// Notify that hydration failed
|
|
1148
1177
|
hydrationStatus = "hydration-error";
|
|
1149
|
-
console.log("[ROUTIER] - Hydration Error - Bad Result", sourceResult);
|
|
1150
1178
|
done(_results__WEBPACK_IMPORTED_MODULE_2__.PluginEventResult.error(event.id, "Query result is not an array"));
|
|
1151
1179
|
return;
|
|
1152
1180
|
}
|
|
1153
1181
|
const changesCollection = new _collections__WEBPACK_IMPORTED_MODULE_3__.BulkPersistChanges();
|
|
1154
1182
|
const schemaChanges = changesCollection.resolve(event.operation.schema.id);
|
|
1155
1183
|
// Add the existing items into the persist payload as adds
|
|
1156
|
-
schemaChanges.adds = sourceResult.data;
|
|
1184
|
+
schemaChanges.adds = sourceResult.data.value;
|
|
1157
1185
|
readPlugin.bulkPersist({
|
|
1158
1186
|
id: (0,_utilities__WEBPACK_IMPORTED_MODULE_0__.uuid)(8),
|
|
1159
1187
|
schemas: event.schemas,
|
|
@@ -1163,7 +1191,6 @@ class OptimisticReplicationDbPlugin {
|
|
|
1163
1191
|
if (readPersistResult.ok === _results__WEBPACK_IMPORTED_MODULE_2__.Result.ERROR) {
|
|
1164
1192
|
// Notify that hydration failed
|
|
1165
1193
|
hydrationStatus = "hydration-error";
|
|
1166
|
-
console.log("[ROUTIER] - Hydration Error - Could Not Save", readPersistResult);
|
|
1167
1194
|
done(readPersistResult);
|
|
1168
1195
|
return;
|
|
1169
1196
|
}
|
|
@@ -1174,6 +1201,41 @@ class OptimisticReplicationDbPlugin {
|
|
|
1174
1201
|
});
|
|
1175
1202
|
return;
|
|
1176
1203
|
}
|
|
1204
|
+
if (hydrationStatus === "hydration-error") {
|
|
1205
|
+
done(_results__WEBPACK_IMPORTED_MODULE_2__.PluginEventResult.error(event.id, "Hydration failed, unable to query read plugin"));
|
|
1206
|
+
return;
|
|
1207
|
+
}
|
|
1208
|
+
// If hydration is pending, do not query empty collection, wait for hydration
|
|
1209
|
+
if (hydrationStatus === "hydration-pending") {
|
|
1210
|
+
const start = (0,_performance__WEBPACK_IMPORTED_MODULE_4__.now)();
|
|
1211
|
+
const pollHydrationStatus = () => {
|
|
1212
|
+
const delta = (0,_performance__WEBPACK_IMPORTED_MODULE_4__.now)() - start;
|
|
1213
|
+
if (delta > MAX_HYDRATION_WAIT_MS) {
|
|
1214
|
+
hydrationStatus = "hydration-error";
|
|
1215
|
+
done(_results__WEBPACK_IMPORTED_MODULE_2__.PluginEventResult.error(event.id, `Hydration timeout: exceeded maximum wait time of ${MAX_HYDRATION_WAIT_MS}ms`));
|
|
1216
|
+
return;
|
|
1217
|
+
}
|
|
1218
|
+
if (hydrationStatus === "hydration-error") {
|
|
1219
|
+
done(_results__WEBPACK_IMPORTED_MODULE_2__.PluginEventResult.error(event.id, "Hydration failed, unable to query read plugin"));
|
|
1220
|
+
return;
|
|
1221
|
+
}
|
|
1222
|
+
if (hydrationStatus === "hydration-success") {
|
|
1223
|
+
// Hydration completed successfully, proceed with query
|
|
1224
|
+
readPlugin.query(event, (readResult) => {
|
|
1225
|
+
if (readResult.ok === _results__WEBPACK_IMPORTED_MODULE_2__.Result.ERROR) {
|
|
1226
|
+
done(readResult);
|
|
1227
|
+
return;
|
|
1228
|
+
}
|
|
1229
|
+
done(readResult);
|
|
1230
|
+
});
|
|
1231
|
+
return;
|
|
1232
|
+
}
|
|
1233
|
+
// Still pending, check again after interval
|
|
1234
|
+
setTimeout(pollHydrationStatus, HYDRATION_POLL_INTERVAL_MS);
|
|
1235
|
+
};
|
|
1236
|
+
pollHydrationStatus();
|
|
1237
|
+
return;
|
|
1238
|
+
}
|
|
1177
1239
|
// Collection is hydrated for the targeted collection and should be in sync
|
|
1178
1240
|
readPlugin.query(event, (readResult) => {
|
|
1179
1241
|
if (readResult.ok === _results__WEBPACK_IMPORTED_MODULE_2__.Result.ERROR) {
|
|
@@ -1190,7 +1252,7 @@ class OptimisticReplicationDbPlugin {
|
|
|
1190
1252
|
}
|
|
1191
1253
|
destroy(event, done) {
|
|
1192
1254
|
try {
|
|
1193
|
-
const workPipeline = new
|
|
1255
|
+
const workPipeline = new _pipeline__WEBPACK_IMPORTED_MODULE_5__.WorkPipeline();
|
|
1194
1256
|
const plugins = [this.plugins.source, ...this.plugins.replicas];
|
|
1195
1257
|
for (let i = 0, length = plugins.length; i < length; i++) {
|
|
1196
1258
|
workPipeline.pipe((done) => plugins[i].destroy(event, done));
|
|
@@ -1209,7 +1271,7 @@ class OptimisticReplicationDbPlugin {
|
|
|
1209
1271
|
}
|
|
1210
1272
|
bulkPersist(event, done) {
|
|
1211
1273
|
try {
|
|
1212
|
-
const workPipeline = new
|
|
1274
|
+
const workPipeline = new _pipeline__WEBPACK_IMPORTED_MODULE_5__.WorkPipeline();
|
|
1213
1275
|
const deferredPlugins = [this.plugins.source, ...this.plugins.replicas];
|
|
1214
1276
|
// Since we are doing optimistic, we insert into the read plugin first and assume later plugins will succeed
|
|
1215
1277
|
// This means the read plugin will generate ids for the source plugin
|
|
@@ -1229,7 +1291,7 @@ class OptimisticReplicationDbPlugin {
|
|
|
1229
1291
|
const optimisticBulkPersistChanges = new _collections__WEBPACK_IMPORTED_MODULE_3__.BulkPersistChanges();
|
|
1230
1292
|
// make sure we swap the adds here, that way we can make sure other persist events
|
|
1231
1293
|
// don't take their additions and try to change subsequent calls
|
|
1232
|
-
(0,
|
|
1294
|
+
(0,_utilities__WEBPACK_IMPORTED_MODULE_6__.resolveBulkPersistChanges)(event, r.data, optimisticBulkPersistChanges);
|
|
1233
1295
|
for (let i = 0, length = deferredPlugins.length; i < length; i++) {
|
|
1234
1296
|
workPipeline.pipe((d) => {
|
|
1235
1297
|
const plugin = deferredPlugins[i];
|