@prenta/core 8.21.4 → 8.21.5
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/CHANGELOG.md +8 -0
- package/dist/presence/index.d.ts +9 -1
- package/dist/presence/index.d.ts.map +1 -1
- package/dist/presence/index.js +75 -13
- package/dist/presence/index.js.map +1 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,13 @@
|
|
|
1
1
|
# @actuate-media/cms-core
|
|
2
2
|
|
|
3
|
+
## 8.21.5
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- 761c0e0: Close the presence event stream before Vercel's 30-second function limit. The stream that shows who else is editing a document had no lifetime at all, so on Vercel the platform killed it mid-response and logged "Task timed out after 30 seconds" for every open document editor. It now closes cleanly after 20 seconds on Vercel and four minutes elsewhere, and clears its timers and update subscription when it does. `createSSEPresenceAdapter` takes an optional `{ maxLifetimeMs }` to override that.
|
|
8
|
+
|
|
9
|
+
The admin's presence indicator used to close its connection on the first stream error, so it never reconnected and went quiet for the rest of the editing session. It now lets the browser reconnect and keeps showing the current collaborators in the meantime. On the server, a user stays in the presence list across that reconnect, and a disconnect no longer removes a user who still has another connection open, such as a second tab or a newer connection that replaced a dropped one.
|
|
10
|
+
|
|
3
11
|
## 8.21.4
|
|
4
12
|
|
|
5
13
|
### Patch Changes
|
package/dist/presence/index.d.ts
CHANGED
|
@@ -14,7 +14,15 @@ export interface PresenceProvider {
|
|
|
14
14
|
heartbeat(documentId: string, userId: string): Promise<void>;
|
|
15
15
|
onUpdate(documentId: string, callback: (users: PresenceUser[]) => void): () => void;
|
|
16
16
|
}
|
|
17
|
-
|
|
17
|
+
/**
|
|
18
|
+
* @param options.maxLifetimeMs How long each presence stream stays open
|
|
19
|
+
* before the server closes it cleanly, in ms. Defaults to
|
|
20
|
+
* `resolveSseMaxLifetimeMs()`: 20 s on Vercel, so the stream ends
|
|
21
|
+
* before the function's `maxDuration` cap, and four minutes elsewhere.
|
|
22
|
+
*/
|
|
23
|
+
export declare function createSSEPresenceAdapter(options?: {
|
|
24
|
+
maxLifetimeMs?: number;
|
|
25
|
+
}): {
|
|
18
26
|
provider: PresenceProvider;
|
|
19
27
|
handleSSE: (request: Request, documentId: string, user: {
|
|
20
28
|
userId: string;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/presence/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/presence/index.ts"],"names":[],"mappings":"AAEA,MAAM,WAAW,YAAY;IAC3B,MAAM,EAAE,MAAM,CAAA;IACd,IAAI,EAAE,MAAM,CAAA;IACZ,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB,UAAU,EAAE,MAAM,CAAA;IAClB,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB,WAAW,EAAE,IAAI,CAAA;IACjB,QAAQ,EAAE,IAAI,CAAA;CACf;AAED,MAAM,WAAW,gBAAgB;IAC/B,IAAI,CAAC,UAAU,EAAE,MAAM,EAAE,IAAI,EAAE,IAAI,CAAC,YAAY,EAAE,aAAa,GAAG,UAAU,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC,CAAA;IAC7F,KAAK,CAAC,UAAU,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAAA;IACxD,UAAU,CAAC,UAAU,EAAE,MAAM,GAAG,OAAO,CAAC,YAAY,EAAE,CAAC,CAAA;IACvD,SAAS,CAAC,UAAU,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAAA;IAC5D,QAAQ,CAAC,UAAU,EAAE,MAAM,EAAE,QAAQ,EAAE,CAAC,KAAK,EAAE,YAAY,EAAE,KAAK,IAAI,GAAG,MAAM,IAAI,CAAA;CACpF;AAMD;;;;;GAKG;AACH,wBAAgB,wBAAwB,CAAC,OAAO,GAAE;IAAE,aAAa,CAAC,EAAE,MAAM,CAAA;CAAO,GAAG;IAClF,QAAQ,EAAE,gBAAgB,CAAA;IAC1B,SAAS,EAAE,CACT,OAAO,EAAE,OAAO,EAChB,UAAU,EAAE,MAAM,EAClB,IAAI,EAAE;QAAE,MAAM,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,MAAM,CAAA;KAAE,KACnC,QAAQ,CAAA;CACd,CA6JA"}
|
package/dist/presence/index.js
CHANGED
|
@@ -1,7 +1,19 @@
|
|
|
1
|
+
import { encodeSseEvent, resolveSseMaxLifetimeMs } from '../realtime/sse.js';
|
|
1
2
|
const STALE_TIMEOUT_MS = 60_000;
|
|
2
|
-
|
|
3
|
+
const encoder = new TextEncoder();
|
|
4
|
+
/**
|
|
5
|
+
* @param options.maxLifetimeMs How long each presence stream stays open
|
|
6
|
+
* before the server closes it cleanly, in ms. Defaults to
|
|
7
|
+
* `resolveSseMaxLifetimeMs()`: 20 s on Vercel, so the stream ends
|
|
8
|
+
* before the function's `maxDuration` cap, and four minutes elsewhere.
|
|
9
|
+
*/
|
|
10
|
+
export function createSSEPresenceAdapter(options = {}) {
|
|
3
11
|
const connections = new Map();
|
|
4
12
|
const subscribers = new Map();
|
|
13
|
+
// Open streams per document and user. A user can hold more than one:
|
|
14
|
+
// two tabs, or a reconnect that lands before the server notices the
|
|
15
|
+
// old connection dropped. They only leave when the last one goes.
|
|
16
|
+
const openStreams = new Map();
|
|
5
17
|
function broadcast(documentId) {
|
|
6
18
|
const users = Array.from(connections.get(documentId)?.values() ?? []);
|
|
7
19
|
const subs = subscribers.get(documentId);
|
|
@@ -35,8 +47,12 @@ export function createSSEPresenceAdapter() {
|
|
|
35
47
|
async join(documentId, user) {
|
|
36
48
|
if (!connections.has(documentId))
|
|
37
49
|
connections.set(documentId, new Map());
|
|
50
|
+
const docConns = connections.get(documentId);
|
|
38
51
|
const now = new Date();
|
|
39
|
-
|
|
52
|
+
// A rejoin (the browser reconnecting after the stream's bounded
|
|
53
|
+
// lifetime) keeps the original `connectedAt`.
|
|
54
|
+
const connectedAt = docConns.get(user.userId)?.connectedAt ?? now;
|
|
55
|
+
docConns.set(user.userId, { ...user, connectedAt, lastSeen: now });
|
|
40
56
|
broadcast(documentId);
|
|
41
57
|
},
|
|
42
58
|
async leave(documentId, userId) {
|
|
@@ -63,30 +79,76 @@ export function createSSEPresenceAdapter() {
|
|
|
63
79
|
},
|
|
64
80
|
};
|
|
65
81
|
function handleSSE(request, documentId, user) {
|
|
66
|
-
|
|
67
|
-
|
|
82
|
+
const maxLifetimeMs = resolveSseMaxLifetimeMs(options.maxLifetimeMs);
|
|
83
|
+
const streamKey = JSON.stringify([documentId, user.userId]);
|
|
84
|
+
let unsubscribe;
|
|
85
|
+
let interval;
|
|
86
|
+
let lifetime;
|
|
87
|
+
let torn = false;
|
|
88
|
+
// Single teardown path shared by the lifetime timer (the server
|
|
89
|
+
// closing the stream before the platform's max-duration cap) and
|
|
90
|
+
// `cancel()` (the client disconnecting). `cancel()` does not run
|
|
91
|
+
// when the controller closes itself, so without this the update
|
|
92
|
+
// subscription and the stale-sweep interval would leak on every
|
|
93
|
+
// server-initiated close.
|
|
94
|
+
const teardown = () => {
|
|
95
|
+
if (torn)
|
|
96
|
+
return;
|
|
97
|
+
torn = true;
|
|
98
|
+
if (interval)
|
|
99
|
+
clearInterval(interval);
|
|
100
|
+
if (lifetime)
|
|
101
|
+
clearTimeout(lifetime);
|
|
102
|
+
if (unsubscribe)
|
|
103
|
+
unsubscribe();
|
|
104
|
+
const remaining = (openStreams.get(streamKey) ?? 1) - 1;
|
|
105
|
+
if (remaining > 0)
|
|
106
|
+
openStreams.set(streamKey, remaining);
|
|
107
|
+
else
|
|
108
|
+
openStreams.delete(streamKey);
|
|
109
|
+
};
|
|
68
110
|
const stream = new ReadableStream({
|
|
69
111
|
start(controller) {
|
|
70
|
-
|
|
112
|
+
openStreams.set(streamKey, (openStreams.get(streamKey) ?? 0) + 1);
|
|
113
|
+
void provider.join(documentId, { userId: user.userId, name: user.name, documentId });
|
|
71
114
|
const send = (users) => {
|
|
115
|
+
if (torn)
|
|
116
|
+
return;
|
|
72
117
|
try {
|
|
73
|
-
const data =
|
|
74
|
-
|
|
118
|
+
const data = users.map((u) => ({
|
|
119
|
+
userId: u.userId,
|
|
120
|
+
name: u.name,
|
|
121
|
+
connectedAt: u.connectedAt,
|
|
122
|
+
}));
|
|
123
|
+
controller.enqueue(encoder.encode(encodeSseEvent(data, 'presence')));
|
|
75
124
|
}
|
|
76
125
|
catch {
|
|
77
126
|
/* closed stream */
|
|
78
127
|
}
|
|
79
128
|
};
|
|
80
|
-
provider.getPresent(documentId).then(send);
|
|
129
|
+
void provider.getPresent(documentId).then(send);
|
|
81
130
|
unsubscribe = provider.onUpdate(documentId, send);
|
|
82
131
|
interval = setInterval(() => expireStale(documentId), 30_000);
|
|
132
|
+
lifetime = setTimeout(() => {
|
|
133
|
+
// The user stays in the presence list: their browser's
|
|
134
|
+
// `EventSource` reconnects within a few seconds and rejoins.
|
|
135
|
+
// Calling `leave` here would make them blink out of every
|
|
136
|
+
// collaborator's indicator on each reconnect. If they never
|
|
137
|
+
// come back, the stale sweep removes them.
|
|
138
|
+
teardown();
|
|
139
|
+
try {
|
|
140
|
+
controller.close();
|
|
141
|
+
}
|
|
142
|
+
catch {
|
|
143
|
+
/* already closed */
|
|
144
|
+
}
|
|
145
|
+
}, maxLifetimeMs);
|
|
83
146
|
},
|
|
84
147
|
cancel() {
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
clearInterval(interval);
|
|
148
|
+
// Client disconnect: the user closed the tab or navigated away.
|
|
149
|
+
teardown();
|
|
150
|
+
if (!openStreams.has(streamKey))
|
|
151
|
+
void provider.leave(documentId, user.userId);
|
|
90
152
|
},
|
|
91
153
|
});
|
|
92
154
|
return new Response(stream, {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/presence/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/presence/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,uBAAuB,EAAE,MAAM,oBAAoB,CAAA;AAoB5E,MAAM,gBAAgB,GAAG,MAAM,CAAA;AAE/B,MAAM,OAAO,GAAG,IAAI,WAAW,EAAE,CAAA;AAEjC;;;;;GAKG;AACH,MAAM,UAAU,wBAAwB,CAAC,UAAsC,EAAE;IAQ/E,MAAM,WAAW,GAAG,IAAI,GAAG,EAAqC,CAAA;IAChE,MAAM,WAAW,GAAG,IAAI,GAAG,EAAgD,CAAA;IAC3E,qEAAqE;IACrE,oEAAoE;IACpE,kEAAkE;IAClE,MAAM,WAAW,GAAG,IAAI,GAAG,EAAkB,CAAA;IAE7C,SAAS,SAAS,CAAC,UAAkB;QACnC,MAAM,KAAK,GAAG,KAAK,CAAC,IAAI,CAAC,WAAW,CAAC,GAAG,CAAC,UAAU,CAAC,EAAE,MAAM,EAAE,IAAI,EAAE,CAAC,CAAA;QACrE,MAAM,IAAI,GAAG,WAAW,CAAC,GAAG,CAAC,UAAU,CAAC,CAAA;QACxC,IAAI,IAAI,EAAE,CAAC;YACT,KAAK,MAAM,EAAE,IAAI,IAAI,EAAE,CAAC;gBACtB,IAAI,CAAC;oBACH,EAAE,CAAC,KAAK,CAAC,CAAA;gBACX,CAAC;gBAAC,MAAM,CAAC;oBACP,sBAAsB;gBACxB,CAAC;YACH,CAAC;QACH,CAAC;IACH,CAAC;IAED,SAAS,WAAW,CAAC,UAAkB;QACrC,MAAM,QAAQ,GAAG,WAAW,CAAC,GAAG,CAAC,UAAU,CAAC,CAAA;QAC5C,IAAI,CAAC,QAAQ;YAAE,OAAM;QACrB,MAAM,GAAG,GAAG,IAAI,CAAC,GAAG,EAAE,CAAA;QACtB,IAAI,OAAO,GAAG,KAAK,CAAA;QACnB,KAAK,MAAM,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