@arcote.tech/arc-host 0.7.15 → 0.7.17
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/index.js +48 -122
- package/dist/index.js.map +4 -4
- package/dist/src/create-server.d.ts.map +1 -1
- package/dist/src/index.d.ts +1 -1
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/middleware/index.d.ts +1 -1
- package/dist/src/middleware/index.d.ts.map +1 -1
- package/dist/src/middleware/live-query.integration.test.d.ts +2 -0
- package/dist/src/middleware/live-query.integration.test.d.ts.map +1 -0
- package/dist/src/middleware/ws.d.ts +1 -30
- package/dist/src/middleware/ws.d.ts.map +1 -1
- package/dist/src/types.d.ts +16 -14
- package/dist/src/types.d.ts.map +1 -1
- package/package.json +4 -4
- package/src/create-server.ts +0 -8
- package/src/index.ts +1 -2
- package/src/middleware/index.ts +1 -2
- package/src/middleware/live-query.integration.test.ts +432 -0
- package/src/middleware/ws.ts +63 -186
- package/src/types.ts +13 -15
- package/dist/src/middleware/view-subscription.integration.test.d.ts +0 -2
- package/dist/src/middleware/view-subscription.integration.test.d.ts.map +0 -1
- package/dist/src/middleware/ws.test.d.ts +0 -2
- package/dist/src/middleware/ws.test.d.ts.map +0 -1
- package/src/middleware/view-subscription.integration.test.ts +0 -307
- package/src/middleware/ws.test.ts +0 -95
package/dist/index.js
CHANGED
|
@@ -4782,66 +4782,14 @@ function arcHttpHandlers(ch, cm) {
|
|
|
4782
4782
|
];
|
|
4783
4783
|
}
|
|
4784
4784
|
// src/middleware/ws.ts
|
|
4785
|
-
import {
|
|
4786
|
-
|
|
4787
|
-
ScopedStore,
|
|
4788
|
-
checkItemMatchesWhere
|
|
4789
|
-
} from "@arcote.tech/arc";
|
|
4790
|
-
var viewSubscribers = new Map;
|
|
4785
|
+
import { LiveQuery } from "@arcote.tech/arc";
|
|
4786
|
+
var clientQuerySubs = new Map;
|
|
4791
4787
|
function cleanupClientSubs(clientId) {
|
|
4792
|
-
const
|
|
4793
|
-
|
|
4794
|
-
for (const
|
|
4795
|
-
|
|
4796
|
-
|
|
4797
|
-
}
|
|
4798
|
-
}
|
|
4799
|
-
}
|
|
4800
|
-
function filterChangeForSubscriber(change, restrictions) {
|
|
4801
|
-
const newMatches = change.newRow !== null && (restrictions === null || checkItemMatchesWhere(change.newRow, restrictions));
|
|
4802
|
-
if (newMatches) {
|
|
4803
|
-
return { type: "set", id: change.id, item: change.newRow };
|
|
4804
|
-
}
|
|
4805
|
-
const oldMatches = change.oldRow !== null && (restrictions === null || checkItemMatchesWhere(change.oldRow, restrictions));
|
|
4806
|
-
if (oldMatches) {
|
|
4807
|
-
return { type: "delete", id: change.id, item: null };
|
|
4808
|
-
}
|
|
4809
|
-
return null;
|
|
4810
|
-
}
|
|
4811
|
-
function broadcastViewChanges(committed, connectionManager) {
|
|
4812
|
-
const byStore = new Map;
|
|
4813
|
-
for (const change of committed) {
|
|
4814
|
-
let list = byStore.get(change.store);
|
|
4815
|
-
if (!list) {
|
|
4816
|
-
list = [];
|
|
4817
|
-
byStore.set(change.store, list);
|
|
4818
|
-
}
|
|
4819
|
-
list.push(change);
|
|
4820
|
-
}
|
|
4821
|
-
for (const [viewName, changes] of byStore) {
|
|
4822
|
-
const subs = viewSubscribers.get(viewName);
|
|
4823
|
-
if (!subs || subs.size === 0)
|
|
4824
|
-
continue;
|
|
4825
|
-
for (const sub of subs.values()) {
|
|
4826
|
-
const filtered = [];
|
|
4827
|
-
for (const change of changes) {
|
|
4828
|
-
const wireChange = filterChangeForSubscriber(change, sub.restrictions);
|
|
4829
|
-
if (wireChange)
|
|
4830
|
-
filtered.push(wireChange);
|
|
4831
|
-
}
|
|
4832
|
-
if (filtered.length === 0)
|
|
4833
|
-
continue;
|
|
4834
|
-
if (sub.buffer) {
|
|
4835
|
-
sub.buffer.push(...filtered);
|
|
4836
|
-
continue;
|
|
4837
|
-
}
|
|
4838
|
-
connectionManager.sendToClient(sub.clientId, {
|
|
4839
|
-
type: "view-changes",
|
|
4840
|
-
element: viewName,
|
|
4841
|
-
scope: sub.scope,
|
|
4842
|
-
changes: filtered
|
|
4843
|
-
});
|
|
4844
|
-
}
|
|
4788
|
+
const subs = clientQuerySubs.get(clientId);
|
|
4789
|
+
if (subs) {
|
|
4790
|
+
for (const live of subs.values())
|
|
4791
|
+
live.stop();
|
|
4792
|
+
clientQuerySubs.delete(clientId);
|
|
4845
4793
|
}
|
|
4846
4794
|
}
|
|
4847
4795
|
function scopeAuthHandler() {
|
|
@@ -4940,82 +4888,62 @@ function executeCommandHandler() {
|
|
|
4940
4888
|
return true;
|
|
4941
4889
|
};
|
|
4942
4890
|
}
|
|
4943
|
-
function
|
|
4891
|
+
function querySubscriptionHandler() {
|
|
4944
4892
|
return async (client, message, ctx) => {
|
|
4945
|
-
if (message.type === "subscribe-
|
|
4946
|
-
const {
|
|
4893
|
+
if (message.type === "subscribe-query") {
|
|
4894
|
+
const { subscriptionId, descriptor } = message;
|
|
4947
4895
|
const scope = message.scope ?? "default";
|
|
4948
4896
|
const scopeToken = client.scopeTokens.get(scope) ?? null;
|
|
4949
4897
|
let rawToken = scopeToken?.raw ?? null;
|
|
4950
4898
|
if (!rawToken && client.scopeTokens.size > 0) {
|
|
4951
4899
|
rawToken = client.scopeTokens.values().next().value.raw;
|
|
4952
4900
|
}
|
|
4953
|
-
|
|
4954
|
-
|
|
4955
|
-
|
|
4956
|
-
|
|
4957
|
-
|
|
4958
|
-
|
|
4959
|
-
|
|
4960
|
-
|
|
4961
|
-
}
|
|
4962
|
-
|
|
4963
|
-
|
|
4964
|
-
|
|
4965
|
-
|
|
4966
|
-
|
|
4967
|
-
|
|
4968
|
-
|
|
4969
|
-
|
|
4970
|
-
|
|
4971
|
-
scope,
|
|
4972
|
-
items: []
|
|
4973
|
-
});
|
|
4974
|
-
return true;
|
|
4975
|
-
}
|
|
4976
|
-
const subscriber = {
|
|
4977
|
-
clientId: client.id,
|
|
4978
|
-
scope,
|
|
4979
|
-
restrictions,
|
|
4980
|
-
buffer: []
|
|
4981
|
-
};
|
|
4982
|
-
if (!viewSubscribers.has(elementName)) {
|
|
4983
|
-
viewSubscribers.set(elementName, new Map);
|
|
4901
|
+
clientQuerySubs.get(client.id)?.get(subscriptionId)?.stop();
|
|
4902
|
+
const live = new LiveQuery(ctx.contextHandler.getModel(), descriptor, scope, rawToken, (update) => {
|
|
4903
|
+
if (update.type === "changes") {
|
|
4904
|
+
ctx.connectionManager.sendToClient(client.id, {
|
|
4905
|
+
type: "query-changes",
|
|
4906
|
+
subscriptionId,
|
|
4907
|
+
changes: update.changes
|
|
4908
|
+
});
|
|
4909
|
+
} else {
|
|
4910
|
+
ctx.connectionManager.sendToClient(client.id, {
|
|
4911
|
+
type: "query-snapshot",
|
|
4912
|
+
subscriptionId,
|
|
4913
|
+
result: update.result ?? null
|
|
4914
|
+
});
|
|
4915
|
+
}
|
|
4916
|
+
});
|
|
4917
|
+
if (!clientQuerySubs.has(client.id)) {
|
|
4918
|
+
clientQuerySubs.set(client.id, new Map);
|
|
4984
4919
|
}
|
|
4985
|
-
|
|
4920
|
+
clientQuerySubs.get(client.id).set(subscriptionId, live);
|
|
4986
4921
|
try {
|
|
4987
|
-
const
|
|
4988
|
-
const items = restrictions ? await new ScopedStore(store, restrictions, false).find({}) : await store.find({});
|
|
4922
|
+
const result = await live.start();
|
|
4989
4923
|
ctx.connectionManager.sendToClient(client.id, {
|
|
4990
|
-
type: "
|
|
4991
|
-
|
|
4992
|
-
|
|
4993
|
-
items
|
|
4924
|
+
type: "query-snapshot",
|
|
4925
|
+
subscriptionId,
|
|
4926
|
+
result: result ?? null
|
|
4994
4927
|
});
|
|
4928
|
+
live.flush();
|
|
4995
4929
|
} catch (err) {
|
|
4996
|
-
|
|
4997
|
-
|
|
4930
|
+
live.stop();
|
|
4931
|
+
clientQuerySubs.get(client.id)?.delete(subscriptionId);
|
|
4932
|
+
console.error(`[Arc] Query subscription error (${descriptor?.element}.${descriptor?.method}):`, err);
|
|
4998
4933
|
ctx.connectionManager.sendToClient(client.id, {
|
|
4999
4934
|
type: "error",
|
|
5000
|
-
message: `Failed to subscribe to
|
|
5001
|
-
});
|
|
5002
|
-
return true;
|
|
5003
|
-
}
|
|
5004
|
-
const buffered = subscriber.buffer;
|
|
5005
|
-
subscriber.buffer = null;
|
|
5006
|
-
if (buffered.length > 0) {
|
|
5007
|
-
ctx.connectionManager.sendToClient(client.id, {
|
|
5008
|
-
type: "view-changes",
|
|
5009
|
-
element: elementName,
|
|
5010
|
-
scope,
|
|
5011
|
-
changes: buffered
|
|
4935
|
+
message: `Failed to subscribe to query "${descriptor?.element}.${descriptor?.method}"`
|
|
5012
4936
|
});
|
|
5013
4937
|
}
|
|
5014
4938
|
return true;
|
|
5015
4939
|
}
|
|
5016
|
-
if (message.type === "unsubscribe-
|
|
5017
|
-
const
|
|
5018
|
-
|
|
4940
|
+
if (message.type === "unsubscribe-query") {
|
|
4941
|
+
const subs = clientQuerySubs.get(client.id);
|
|
4942
|
+
const live = subs?.get(message.subscriptionId);
|
|
4943
|
+
if (live) {
|
|
4944
|
+
live.stop();
|
|
4945
|
+
subs.delete(message.subscriptionId);
|
|
4946
|
+
}
|
|
5019
4947
|
return true;
|
|
5020
4948
|
}
|
|
5021
4949
|
return false;
|
|
@@ -5027,7 +4955,7 @@ function arcWsHandlers() {
|
|
|
5027
4955
|
syncEventsHandler(),
|
|
5028
4956
|
requestSyncHandler(),
|
|
5029
4957
|
executeCommandHandler(),
|
|
5030
|
-
|
|
4958
|
+
querySubscriptionHandler()
|
|
5031
4959
|
];
|
|
5032
4960
|
}
|
|
5033
4961
|
// src/create-server.ts
|
|
@@ -5040,7 +4968,6 @@ async function createArcServer(config) {
|
|
|
5040
4968
|
const cronScheduler = new CronScheduler(contextHandler);
|
|
5041
4969
|
cronScheduler.start();
|
|
5042
4970
|
const connectionManager = new ConnectionManager;
|
|
5043
|
-
contextHandler.getEventPublisher().onViewChanges((changes) => broadcastViewChanges(changes, connectionManager));
|
|
5044
4971
|
function buildCorsHeaders(req) {
|
|
5045
4972
|
const origin = req?.headers.get("Origin") || "*";
|
|
5046
4973
|
return {
|
|
@@ -5223,11 +5150,11 @@ function hostLiveModel(context, dbAdapterFactory) {
|
|
|
5223
5150
|
});
|
|
5224
5151
|
}
|
|
5225
5152
|
export {
|
|
5226
|
-
viewSubscriptionHandler,
|
|
5227
5153
|
syncEventsHandler,
|
|
5228
5154
|
scopeAuthHandler,
|
|
5229
5155
|
routeHandler,
|
|
5230
5156
|
requestSyncHandler,
|
|
5157
|
+
querySubscriptionHandler,
|
|
5231
5158
|
queryHandler,
|
|
5232
5159
|
hostLiveModel,
|
|
5233
5160
|
healthHandler,
|
|
@@ -5240,7 +5167,6 @@ export {
|
|
|
5240
5167
|
cleanupClientSubs,
|
|
5241
5168
|
canTokenReceiveEvent,
|
|
5242
5169
|
canTokenEmitEvent,
|
|
5243
|
-
broadcastViewChanges,
|
|
5244
5170
|
arcWsHandlers,
|
|
5245
5171
|
arcHttpHandlers,
|
|
5246
5172
|
CronScheduler,
|
|
@@ -5248,5 +5174,5 @@ export {
|
|
|
5248
5174
|
ConnectionManager
|
|
5249
5175
|
};
|
|
5250
5176
|
|
|
5251
|
-
//# debugId=
|
|
5177
|
+
//# debugId=5FB48DB48B7AE0DA64756E2164756E21
|
|
5252
5178
|
//# sourceMappingURL=index.js.map
|