document-drive 1.0.0-websockets.1 → 1.0.0

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.
Files changed (43) hide show
  1. package/README.md +1 -0
  2. package/package.json +74 -88
  3. package/src/cache/index.ts +2 -2
  4. package/src/cache/memory.ts +22 -13
  5. package/src/cache/redis.ts +43 -16
  6. package/src/cache/types.ts +4 -4
  7. package/src/index.ts +6 -3
  8. package/src/queue/base.ts +276 -214
  9. package/src/queue/index.ts +2 -2
  10. package/src/queue/redis.ts +138 -127
  11. package/src/queue/types.ts +44 -38
  12. package/src/read-mode/errors.ts +19 -0
  13. package/src/read-mode/index.ts +125 -0
  14. package/src/read-mode/service.ts +207 -0
  15. package/src/read-mode/types.ts +108 -0
  16. package/src/server/error.ts +61 -26
  17. package/src/server/index.ts +2160 -1785
  18. package/src/server/listener/index.ts +2 -2
  19. package/src/server/listener/manager.ts +475 -437
  20. package/src/server/listener/transmitter/index.ts +4 -5
  21. package/src/server/listener/transmitter/internal.ts +77 -79
  22. package/src/server/listener/transmitter/pull-responder.ts +363 -329
  23. package/src/server/listener/transmitter/switchboard-push.ts +72 -55
  24. package/src/server/listener/transmitter/types.ts +19 -25
  25. package/src/server/types.ts +536 -349
  26. package/src/server/utils.ts +26 -27
  27. package/src/storage/base.ts +81 -0
  28. package/src/storage/browser.ts +233 -216
  29. package/src/storage/filesystem.ts +257 -256
  30. package/src/storage/index.ts +2 -1
  31. package/src/storage/memory.ts +206 -214
  32. package/src/storage/prisma.ts +575 -568
  33. package/src/storage/sequelize.ts +460 -471
  34. package/src/storage/types.ts +83 -67
  35. package/src/utils/default-drives-manager.ts +341 -0
  36. package/src/utils/document-helpers.ts +19 -18
  37. package/src/utils/graphql.ts +288 -34
  38. package/src/utils/index.ts +61 -59
  39. package/src/utils/logger.ts +39 -37
  40. package/src/utils/migrations.ts +58 -0
  41. package/src/utils/run-asap.ts +156 -0
  42. package/CHANGELOG.md +0 -818
  43. package/src/server/listener/transmitter/subscription.ts +0 -364
@@ -0,0 +1,207 @@
1
+ import type { DocumentDriveDocument } from "document-model-libs/document-drive";
2
+ import * as DocumentDrive from "document-model-libs/document-drive";
3
+ import { Document, DocumentModel } from "document-model/document";
4
+ import { GraphQLError } from "graphql";
5
+ import { DocumentModelNotFoundError } from "../server/error";
6
+ import { fetchDocument, requestPublicDrive } from "../utils/graphql";
7
+ import {
8
+ ReadDocumentNotFoundError,
9
+ ReadDriveError,
10
+ ReadDriveNotFoundError,
11
+ ReadDriveSlugNotFoundError,
12
+ } from "./errors";
13
+ import {
14
+ GetDocumentModel,
15
+ InferDocumentLocalState,
16
+ InferDocumentOperation,
17
+ InferDocumentState,
18
+ IReadModeDriveService,
19
+ ReadDrive,
20
+ ReadDriveContext,
21
+ ReadDriveOptions,
22
+ } from "./types";
23
+
24
+ export class ReadModeService implements IReadModeDriveService {
25
+ #getDocumentModel: GetDocumentModel;
26
+ #drives = new Map<
27
+ string,
28
+ { drive: Omit<ReadDrive, "readContext">; context: ReadDriveContext }
29
+ >();
30
+
31
+ constructor(getDocumentModel: GetDocumentModel) {
32
+ this.#getDocumentModel = getDocumentModel;
33
+ }
34
+
35
+ #parseGraphQLErrors(
36
+ errors: GraphQLError[],
37
+ driveId: string,
38
+ documentId?: string,
39
+ ) {
40
+ for (const error of errors) {
41
+ if (error.message === `Drive with id ${driveId} not found`) {
42
+ return new ReadDriveNotFoundError(driveId);
43
+ } else if (
44
+ documentId &&
45
+ error.message === `Document with id ${documentId} not found`
46
+ ) {
47
+ return new ReadDocumentNotFoundError(driveId, documentId);
48
+ }
49
+ }
50
+ const firstError = errors.at(0);
51
+ if (firstError) {
52
+ return firstError;
53
+ }
54
+ }
55
+
56
+ async #fetchDrive(id: string, url: string) {
57
+ const { errors, document } = await fetchDocument<DocumentDriveDocument>(
58
+ url,
59
+ id,
60
+ DocumentDrive,
61
+ );
62
+ const error = errors ? this.#parseGraphQLErrors(errors, id) : undefined;
63
+ return error || document;
64
+ }
65
+
66
+ async fetchDrive(id: string): Promise<ReadDrive | ReadDriveNotFoundError> {
67
+ const drive = this.#drives.get(id);
68
+ if (!drive) {
69
+ return new ReadDriveNotFoundError(id);
70
+ }
71
+ const document = await this.fetchDocument<DocumentDriveDocument>(
72
+ id,
73
+ id,
74
+ DocumentDrive.documentModel.id,
75
+ );
76
+ if (document instanceof Error) {
77
+ return document;
78
+ }
79
+ const result = { ...document, readContext: drive.context };
80
+ drive.drive = result;
81
+ return result;
82
+ }
83
+
84
+ async fetchDocument<D extends Document>(
85
+ driveId: string,
86
+ documentId: string,
87
+ documentType: DocumentModel<
88
+ InferDocumentState<D>,
89
+ InferDocumentOperation<D>,
90
+ InferDocumentLocalState<D>
91
+ >["documentModel"]["id"],
92
+ ): Promise<
93
+ | Document<
94
+ InferDocumentState<D>,
95
+ InferDocumentOperation<D>,
96
+ InferDocumentLocalState<D>
97
+ >
98
+ | DocumentModelNotFoundError
99
+ | ReadDriveNotFoundError
100
+ | ReadDocumentNotFoundError
101
+ > {
102
+ const drive = this.#drives.get(driveId);
103
+ if (!drive) {
104
+ return new ReadDriveNotFoundError(driveId);
105
+ }
106
+
107
+ let documentModel:
108
+ | DocumentModel<
109
+ InferDocumentState<D>,
110
+ InferDocumentOperation<D>,
111
+ InferDocumentLocalState<D>
112
+ >
113
+ | undefined = undefined;
114
+ try {
115
+ documentModel = this.#getDocumentModel(
116
+ documentType,
117
+ ) as unknown as DocumentModel<
118
+ InferDocumentState<D>,
119
+ InferDocumentOperation<D>,
120
+ InferDocumentLocalState<D>
121
+ >;
122
+ } catch (error) {
123
+ return new DocumentModelNotFoundError(documentType, error);
124
+ }
125
+
126
+ const { url } = drive.context;
127
+ const { errors, document } = await fetchDocument<D>(
128
+ url,
129
+ documentId,
130
+ documentModel,
131
+ );
132
+
133
+ if (errors) {
134
+ const error = this.#parseGraphQLErrors(errors, driveId, documentId);
135
+ if (error instanceof ReadDriveError) {
136
+ return error;
137
+ } else if (error) {
138
+ throw error;
139
+ }
140
+ }
141
+
142
+ if (!document) {
143
+ return new ReadDocumentNotFoundError(driveId, documentId);
144
+ }
145
+
146
+ return document;
147
+ }
148
+
149
+ async addReadDrive(url: string, options?: ReadDriveOptions): Promise<void> {
150
+ const { id } =
151
+ options?.expectedDriveInfo ?? (await requestPublicDrive(url));
152
+
153
+ const result = await this.#fetchDrive(id, url);
154
+ if (result instanceof Error) {
155
+ throw result;
156
+ } else if (!result) {
157
+ throw new Error(`Drive "${id}" not found at ${url}`);
158
+ }
159
+ this.#drives.set(id, {
160
+ drive: result as unknown as ReadDrive,
161
+ context: {
162
+ ...options,
163
+ url,
164
+ },
165
+ });
166
+ }
167
+
168
+ async getReadDrives() {
169
+ return Promise.resolve([...this.#drives.keys()]);
170
+ }
171
+
172
+ async getReadDrive(id: string) {
173
+ const result = this.#drives.get(id);
174
+ return Promise.resolve(
175
+ result
176
+ ? { ...result.drive, readContext: result.context }
177
+ : new ReadDriveNotFoundError(id),
178
+ );
179
+ }
180
+
181
+ async getReadDriveBySlug(
182
+ slug: string,
183
+ ): Promise<ReadDrive | ReadDriveSlugNotFoundError> {
184
+ const readDrive = [...this.#drives.values()].find(
185
+ ({ drive }) => drive.state.global.slug === slug,
186
+ );
187
+
188
+ return Promise.resolve(
189
+ readDrive
190
+ ? { ...readDrive.drive, readContext: readDrive.context }
191
+ : new ReadDriveSlugNotFoundError(slug),
192
+ );
193
+ }
194
+
195
+ getReadDriveContext(id: string) {
196
+ return Promise.resolve(
197
+ this.#drives.get(id)?.context ?? new ReadDriveNotFoundError(id),
198
+ );
199
+ }
200
+
201
+ deleteReadDrive(id: string): Promise<ReadDriveNotFoundError | undefined> {
202
+ const deleted = this.#drives.delete(id);
203
+ return Promise.resolve(
204
+ deleted ? undefined : new ReadDriveNotFoundError(id),
205
+ );
206
+ }
207
+ }
@@ -0,0 +1,108 @@
1
+ import {
2
+ DocumentDriveDocument,
3
+ ListenerFilter,
4
+ } from "document-model-libs/document-drive";
5
+ import { Action, Document, DocumentModel } from "document-model/document";
6
+ import { DocumentDriveServerMixin, RemoteDriveOptions } from "../server";
7
+ import { DocumentModelNotFoundError } from "../server/error";
8
+ import { DriveInfo } from "../utils/graphql";
9
+ import {
10
+ ReadDocumentNotFoundError,
11
+ ReadDriveNotFoundError,
12
+ ReadDriveSlugNotFoundError,
13
+ } from "./errors";
14
+
15
+ // TODO: move these types to the document-model package
16
+ export type InferDocumentState<D extends Document> =
17
+ D extends Document<infer S> ? S : never;
18
+
19
+ export type InferDocumentOperation<D extends Document> =
20
+ D extends Document<unknown, infer A> ? A : never;
21
+
22
+ export type InferDocumentLocalState<D extends Document> =
23
+ D extends Document<unknown, Action, infer L> ? L : never;
24
+
25
+ export type InferDocumentGenerics<D extends Document> = {
26
+ state: InferDocumentState<D>;
27
+ action: InferDocumentOperation<D>;
28
+ logger: InferDocumentLocalState<D>;
29
+ };
30
+
31
+ export type ReadModeDriveServerMixin =
32
+ DocumentDriveServerMixin<IReadModeDriveServer>;
33
+
34
+ export type ReadDrivesListener = (
35
+ drives: ReadDrive[],
36
+ operation: "add" | "delete",
37
+ ) => void;
38
+
39
+ export type ReadDrivesListenerUnsubscribe = () => void;
40
+
41
+ export interface IReadModeDriveServer extends IReadModeDriveService {
42
+ migrateReadDrive(
43
+ id: string,
44
+ options: RemoteDriveOptions,
45
+ ): Promise<DocumentDriveDocument | ReadDriveNotFoundError>;
46
+ onReadDrivesUpdate(
47
+ listener: ReadDrivesListener,
48
+ ): Promise<ReadDrivesListenerUnsubscribe>; // TODO: make DriveEvents extensible and reuse event emitter
49
+ }
50
+
51
+ export type ReadDriveOptions = {
52
+ expectedDriveInfo?: DriveInfo;
53
+ filter?: ListenerFilter;
54
+ };
55
+
56
+ export type ReadDriveContext = {
57
+ url: string;
58
+ } & ReadDriveOptions;
59
+
60
+ export type ReadDrive = DocumentDriveDocument & {
61
+ readContext: ReadDriveContext;
62
+ };
63
+
64
+ export type IsDocument<D extends Document> =
65
+ (<G>() => G extends D ? 1 : 2) extends <G>() => G extends Document ? 1 : 2
66
+ ? true
67
+ : false;
68
+
69
+ export interface IReadModeDriveService {
70
+ addReadDrive(url: string, options?: ReadDriveOptions): Promise<void>;
71
+
72
+ getReadDrives(): Promise<string[]>;
73
+
74
+ getReadDriveBySlug(
75
+ slug: string,
76
+ ): Promise<ReadDrive | ReadDriveSlugNotFoundError>;
77
+
78
+ getReadDrive(id: string): Promise<ReadDrive | ReadDriveNotFoundError>;
79
+
80
+ getReadDriveContext(
81
+ id: string,
82
+ ): Promise<ReadDriveContext | ReadDriveNotFoundError>;
83
+
84
+ fetchDrive(id: string): Promise<ReadDrive | ReadDriveNotFoundError>;
85
+
86
+ fetchDocument<D extends Document>(
87
+ driveId: string,
88
+ documentId: string,
89
+ documentType: DocumentModel<
90
+ InferDocumentState<D>,
91
+ InferDocumentOperation<D>,
92
+ InferDocumentLocalState<D>
93
+ >["documentModel"]["id"],
94
+ ): Promise<
95
+ | Document<
96
+ InferDocumentState<D>,
97
+ InferDocumentOperation<D>,
98
+ InferDocumentLocalState<D>
99
+ >
100
+ | DocumentModelNotFoundError
101
+ | ReadDriveNotFoundError
102
+ | ReadDocumentNotFoundError
103
+ >;
104
+
105
+ deleteReadDrive(id: string): Promise<ReadDriveNotFoundError | undefined>;
106
+ }
107
+
108
+ export type GetDocumentModel = (documentType: string) => DocumentModel;
@@ -1,35 +1,70 @@
1
- import type { Operation } from 'document-model/document';
2
- import type { ErrorStatus } from './types';
1
+ import type { Operation } from "document-model/document";
2
+ import type { ErrorStatus } from "./types";
3
3
 
4
+ export class DocumentModelNotFoundError extends Error {
5
+ constructor(
6
+ public id: string,
7
+ cause?: unknown,
8
+ ) {
9
+ super(`Document model "${id}" not found`, { cause });
10
+ }
11
+ }
4
12
  export class OperationError extends Error {
5
- status: ErrorStatus;
6
- operation: Operation | undefined;
7
-
8
- constructor(
9
- status: ErrorStatus,
10
- operation?: Operation,
11
- message?: string,
12
- cause?: unknown
13
- ) {
14
- super(message, { cause: cause ?? operation });
15
- this.status = status;
16
- this.operation = operation;
17
- }
13
+ status: ErrorStatus;
14
+ operation: Operation | undefined;
15
+
16
+ constructor(
17
+ status: ErrorStatus,
18
+ operation?: Operation,
19
+ message?: string,
20
+ cause?: unknown,
21
+ ) {
22
+ super(message, { cause: cause ?? operation });
23
+ this.status = status;
24
+ this.operation = operation;
25
+ }
18
26
  }
19
27
 
20
28
  export class ConflictOperationError extends OperationError {
21
- constructor(existingOperation: Operation, newOperation: Operation) {
22
- super(
23
- 'CONFLICT',
24
- newOperation,
25
- `Conflicting operation on index ${newOperation.index}`,
26
- { existingOperation, newOperation }
27
- );
28
- }
29
+ constructor(existingOperation: Operation, newOperation: Operation) {
30
+ super(
31
+ "CONFLICT",
32
+ newOperation,
33
+ `Conflicting operation on index ${newOperation.index}`,
34
+ { existingOperation, newOperation },
35
+ );
36
+ }
29
37
  }
30
38
 
31
39
  export class MissingOperationError extends OperationError {
32
- constructor(index: number, operation: Operation) {
33
- super('MISSING', operation, `Missing operation on index ${index}`);
34
- }
40
+ constructor(index: number, operation: Operation) {
41
+ super("MISSING", operation, `Missing operation on index ${index}`);
42
+ }
43
+ }
44
+
45
+ export class DriveAlreadyExistsError extends Error {
46
+ driveId: string;
47
+
48
+ constructor(driveId: string) {
49
+ super(`Drive already exists. ID: ${driveId}`);
50
+ this.driveId = driveId;
51
+ }
52
+ }
53
+
54
+ export class DriveNotFoundError extends Error {
55
+ driveId: string;
56
+
57
+ constructor(driveId: string) {
58
+ super(`Drive with id ${driveId} not found`);
59
+ this.driveId = driveId;
60
+ }
61
+ }
62
+
63
+ export class SynchronizationUnitNotFoundError extends Error {
64
+ syncUnitId: string;
65
+
66
+ constructor(message: string, syncUnitId: string) {
67
+ super(message);
68
+ this.syncUnitId = syncUnitId;
69
+ }
35
70
  }