@lincyaw/workspace 1.0.0-rc.48 → 1.0.0-rc.49
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/package.json +5 -5
- package/src/node/authoring-clients.ts +21 -12
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lincyaw/workspace",
|
|
3
|
-
"version": "1.0.0-rc.
|
|
3
|
+
"version": "1.0.0-rc.49",
|
|
4
4
|
"description": "Presmium's workspace service (presmium serve): documents, their history, reviews and the workspace site",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"repository": {
|
|
@@ -34,12 +34,12 @@
|
|
|
34
34
|
"mime-types": "3.0.2",
|
|
35
35
|
"playwright-chromium": "1.64.1",
|
|
36
36
|
"yaml": "^2.9.1",
|
|
37
|
-
"@lincyaw/
|
|
38
|
-
"@lincyaw/
|
|
39
|
-
"@lincyaw/server": "1.0.0-rc.
|
|
37
|
+
"@lincyaw/kernel": "1.0.0-rc.49",
|
|
38
|
+
"@lincyaw/compiler": "1.0.0-rc.49",
|
|
39
|
+
"@lincyaw/server": "1.0.0-rc.49"
|
|
40
40
|
},
|
|
41
41
|
"devDependencies": {
|
|
42
42
|
"@types/mime-types": "^3.0.1",
|
|
43
|
-
"@lincyaw/mcp": "1.0.0-rc.
|
|
43
|
+
"@lincyaw/mcp": "1.0.0-rc.49"
|
|
44
44
|
}
|
|
45
45
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { randomBytes, randomUUID, createHash } from 'node:crypto'
|
|
2
|
-
import { AUTHORING_CLIENT_ACK_MAX_BYTES, AUTHORING_CLIENT_MAX_CLIENTS, AUTHORING_CLIENT_MAX_REQUESTS, AUTHORING_CLIENT_MAX_REQUEST_CHARS, AUTHORING_CLIENT_MAX_RESPONSE_CHARS, AUTHORING_CLIENT_REQUEST_TIMEOUT_MS, authoringClientActionChangesSource, authoringClientResultFits, DOCUMENT_ENTRY, fnv1a64, hasRole, isRecord, jsonResponse, problemResponse, pushStream, sseResponse } from '@lincyaw/kernel'
|
|
2
|
+
import { AUTHORING_CLIENT_ACK_MAX_BYTES, AUTHORING_CLIENT_MAX_CLIENTS, AUTHORING_CLIENT_MAX_REQUESTS, AUTHORING_CLIENT_MAX_REQUEST_CHARS, AUTHORING_CLIENT_MAX_RESPONSE_CHARS, AUTHORING_CLIENT_REQUEST_TIMEOUT_MS, authoringClientActionChangesSource, authoringClientActionMutatesDraft, authoringClientActionUsesLocalDraft, authoringClientResultFits, DOCUMENT_ENTRY, fnv1a64, hasRole, isRecord, jsonResponse, problemResponse, pushStream, sseResponse } from '@lincyaw/kernel'
|
|
3
3
|
import type { AuthoringClientAcknowledgement, AuthoringClientConnection, AuthoringClientDescriptor, AuthoringClientEvent, AuthoringClientLease, AuthoringClientOpen, AuthoringClientReport, AuthoringClientRequest, AuthoringClientRequestResult, AuthoringClients, CanonicalSource, Member, PushStream, Route } from '@lincyaw/kernel'
|
|
4
4
|
import { validateAuthoringClientAcknowledgement, validateAuthoringClientLease, validateAuthoringClientOpen, validateAuthoringClientQuery, validateAuthoringClientReport, validateAuthoringClientRequest, validateAuthoringClientRequestQuery } from '@lincyaw/kernel/node'
|
|
5
5
|
import { readAuthoringDocument } from './document-editor.ts'
|
|
@@ -86,15 +86,15 @@ export function createAuthoringClients(editor: SourceEditor, options: { reviewRe
|
|
|
86
86
|
return { ...answer, ...answer.clients.length < all.length ? { nextAfter: answer.clients.at(-1)!.clientId } : {} }
|
|
87
87
|
},
|
|
88
88
|
async request(document, request, member) {
|
|
89
|
-
|
|
89
|
+
authorize(document, member); budget(request)
|
|
90
90
|
if (!validateAuthoringClientRequest(request)) throw new RequestError(400, 'client-request-invalid')
|
|
91
91
|
if (requiresReview(request.action.type)) throw new RequestError(409, 'authoring-review-required')
|
|
92
92
|
const key = requestKey(member.id, document, request), signature = digest(request), known = requests.get(key)
|
|
93
|
-
if (known) { if (known.signature !== signature) throw new RequestError(409, 'client-request-id-reused'); return readResult(known,
|
|
93
|
+
if (known) { if (known.signature !== signature) throw new RequestError(409, 'client-request-id-reused'); return readResult(known, authoringClientActionUsesLocalDraft(request.action.type) ? undefined : current(document, member)) }
|
|
94
94
|
const client = clients.get(clientKey(member.id, request.clientId)), state = client?.value
|
|
95
95
|
if (!client || !state?.connected || state.document !== document || state.epoch !== request.epoch) throw new RequestError(409, 'client-unavailable')
|
|
96
96
|
if (state.connectionId !== request.expectedClientState.connectionId || state.snapshot.revision !== request.expectedClientState.revision) throw new RequestError(409, 'client-state-changed')
|
|
97
|
-
if (!sameSource(
|
|
97
|
+
if (!authoringClientActionUsesLocalDraft(request.action.type) && !sameSource(current(document, member), request.source) || (request.action.type === 'body-drafts.retry' ? state.snapshot.source.documentId !== request.source.documentId : !sameSource(state.snapshot.source, request.source))) throw new RequestError(409, 'client-source-changed')
|
|
98
98
|
if (!state.snapshot.actions.includes(request.action.type)) throw new RequestError(409, 'client-action-unavailable')
|
|
99
99
|
if ([...requests.values()].some(value => !terminal(value) && value.member === member.id && value.request.clientId === request.clientId)) throw new RequestError(409, 'client-busy')
|
|
100
100
|
while (requests.size >= AUTHORING_CLIENT_MAX_REQUESTS) {
|
|
@@ -110,7 +110,7 @@ export function createAuthoringClients(editor: SourceEditor, options: { reviewRe
|
|
|
110
110
|
pending.timer = setTimeout(() => finish(pending, 'expired', 'client-request-timeout'), AUTHORING_CLIENT_REQUEST_TIMEOUT_MS)
|
|
111
111
|
requests.set(key, pending)
|
|
112
112
|
client.send?.({ type: 'request', request: pending.request, expiresAt: value.expiresAt })
|
|
113
|
-
return readResult(pending,
|
|
113
|
+
return readResult(pending, authoringClientActionUsesLocalDraft(request.action.type) ? undefined : current(document, member))
|
|
114
114
|
},
|
|
115
115
|
async get(document, query, member) {
|
|
116
116
|
authorize(document, member); budget(query)
|
|
@@ -173,24 +173,33 @@ export function createAuthoringClients(editor: SourceEditor, options: { reviewRe
|
|
|
173
173
|
if (pending && Date.now() >= Date.parse(pending.value.expiresAt)) finish(pending, 'expired', 'client-request-timeout')
|
|
174
174
|
if (!pending || terminal(pending) || pending.request.expectedClientState.connectionId !== lease.connectionId) throw new RequestError(409, 'client-request-expired')
|
|
175
175
|
if (ack.state === 'executing' && pending.value.state !== 'sent' || ack.state === 'done' && pending.value.state !== 'executing') throw new RequestError(409, 'client-ack-state-invalid')
|
|
176
|
-
if ((ack.state === 'executing' || ack.state === 'done') && !sameSource(ack.snapshot.source, pending.request.source) && !(ack.state === 'done' && authoringClientActionChangesSource(pending.request.action.type) && ack.snapshot.source.documentId === pending.request.source.documentId)) throw new RequestError(409, 'client-source-changed')
|
|
177
|
-
const
|
|
178
|
-
|
|
179
|
-
if (ack.state === '
|
|
176
|
+
if ((ack.state === 'executing' || ack.state === 'done') && !sameSource(ack.snapshot.source, pending.request.source) && !(ack.state === 'executing' && pending.request.action.type === 'body-drafts.retry' && ack.snapshot.source.documentId === pending.request.source.documentId) && !(ack.state === 'done' && (authoringClientActionChangesSource(pending.request.action.type) || authoringClientActionUsesLocalDraft(pending.request.action.type)) && ack.snapshot.source.documentId === pending.request.source.documentId)) throw new RequestError(409, 'client-source-changed')
|
|
177
|
+
const localDraft = authoringClientActionUsesLocalDraft(pending.request.action.type)
|
|
178
|
+
const actual = !localDraft && (ack.state === 'executing' || ack.state === 'done') ? current(document, member) : undefined
|
|
179
|
+
if (ack.state === 'executing' && (ack.snapshot.revision !== pending.request.expectedClientState.revision || !localDraft && (!actual || !sameSource(actual, pending.request.source)))) throw new RequestError(409, 'client-state-changed')
|
|
180
|
+
if (ack.state === 'done' && !localDraft && (!actual || !sameSource(actual, ack.snapshot.source))) throw new RequestError(409, 'client-source-changed')
|
|
180
181
|
const action = pending.request.action
|
|
181
|
-
const family = action.type.startsWith('comment-drafts.') ? 'comment' : action.type.startsWith('component-drafts.') ? 'component' : undefined
|
|
182
|
+
const family = action.type.startsWith('body-drafts.') ? 'body' : action.type.startsWith('comment-drafts.') ? 'comment' : action.type.startsWith('component-drafts.') ? 'component' : undefined
|
|
182
183
|
if (ack.result && (!family || ack.state !== 'done' || ack.result.type !== action.type || ack.result.family !== family)) throw new RequestError(400, 'client-result-invalid')
|
|
183
184
|
if (family && ack.state === 'done') {
|
|
184
185
|
const result = ack.result
|
|
185
|
-
const mutation = action.type
|
|
186
|
+
const mutation = authoringClientActionMutatesDraft(action.type)
|
|
186
187
|
if (!result || (mutation ? ack.snapshot.revision <= pending.request.expectedClientState.revision : ack.snapshot.revision !== pending.request.expectedClientState.revision)) throw new RequestError(409, `client-${family}-drafts-changed`)
|
|
187
188
|
if (!authoringClientResultFits({ request: pending.request, connection: client.value, document, snapshot: ack.snapshot }, result)) throw new RequestError(413, `client-${family}-result-too-large`)
|
|
189
|
+
if (action.type === 'body-drafts.read' && (result.type !== action.type || result.draft !== action.draft
|
|
190
|
+
|| result.offset !== action.offset || result.endOffset !== result.offset + result.jsonText.length || result.endOffset > result.totalUtf16
|
|
191
|
+
|| result.jsonText.length > (action.maxChars ?? 2048))) throw new RequestError(400, 'client-result-invalid')
|
|
188
192
|
if ((action.type === 'comment-drafts.read' || action.type === 'component-drafts.read') && (result.type !== action.type || result.draft.id !== action.draft.id || result.draft.revision !== action.draft.revision || result.draft.token !== action.draft.token
|
|
189
193
|
|| action.type === 'component-drafts.read' && (result.type !== action.type || result.draft.blockId !== action.draft.blockId || result.draft.stepId !== action.draft.stepId)
|
|
190
194
|
|| result.offset !== action.offset || result.endOffset !== result.offset + result.jsonText.length || result.endOffset > result.totalUtf16
|
|
191
195
|
|| result.jsonText.length > (action.maxChars ?? 2048))) throw new RequestError(400, 'client-result-invalid')
|
|
192
|
-
if ((action.type === 'comment-drafts.inspect' || action.type === 'component-drafts.inspect') && (result.type !== action.type || result.drafts.length > (action.limit ?? 10)
|
|
196
|
+
if ((action.type === 'comment-drafts.inspect' || action.type === 'component-drafts.inspect' || action.type === 'body-drafts.inspect') && (result.type !== action.type || result.drafts.length > (action.limit ?? 10)
|
|
193
197
|
|| action.cursor && action.cursor.token !== result.token || result.next && result.next.token !== result.token)) throw new RequestError(400, 'client-result-invalid')
|
|
198
|
+
if (action.type === 'body-drafts.restore' || action.type === 'body-drafts.discard' || action.type === 'body-drafts.retry') {
|
|
199
|
+
if (result.type !== action.type || !('outcome' in result) || result.draft !== action.draft
|
|
200
|
+
|| (action.type === 'body-drafts.restore' ? !['restored', 'already-saved'].includes(result.outcome)
|
|
201
|
+
: action.type === 'body-drafts.discard' ? !['source-restored', 'backup-discarded'].includes(result.outcome) : result.outcome !== 'saved')) throw new RequestError(400, 'client-result-invalid')
|
|
202
|
+
}
|
|
194
203
|
const sameComment = (a: import('@lincyaw/kernel').ClientCommentDraftReference | null, b: import('@lincyaw/kernel').ClientCommentDraftReference | null) => a === null || b === null ? a === b
|
|
195
204
|
: a.id === b.id && a.revision === b.revision && a.token === b.token
|
|
196
205
|
if (action.type === 'comment-drafts.restore' && (result.type !== action.type || !sameComment(result.draft, action.draft) || result.composerId !== action.composerId
|