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
@@ -1,5 +1,4 @@
1
- export * from './pull-responder';
2
- export * from './switchboard-push';
3
- export * from './internal';
4
- export * from './types';
5
- export * from './subscription';
1
+ export * from "./internal";
2
+ export * from "./pull-responder";
3
+ export * from "./switchboard-push";
4
+ export * from "./types";
@@ -1,96 +1,94 @@
1
- import { Document, OperationScope } from 'document-model/document';
1
+ import { Document, OperationScope } from "document-model/document";
2
+ import { logger } from "../../../utils/logger";
2
3
  import {
3
- BaseDocumentDriveServer,
4
- Listener,
5
- ListenerRevision,
6
- OperationUpdate,
7
- StrandUpdate
8
- } from '../../types';
9
- import { buildRevisionsFilter } from '../../utils';
10
- import { ITransmitter } from './types';
11
- import { logger } from '../../../utils/logger';
4
+ IBaseDocumentDriveServer,
5
+ Listener,
6
+ ListenerRevision,
7
+ OperationUpdate,
8
+ StrandUpdate,
9
+ } from "../../types";
10
+ import { buildRevisionsFilter } from "../../utils";
11
+ import { ITransmitter } from "./types";
12
12
 
13
13
  export interface IReceiver {
14
- transmit: (strands: InternalTransmitterUpdate[]) => Promise<void>;
15
- disconnect: () => Promise<void>;
14
+ transmit: (strands: InternalTransmitterUpdate[]) => Promise<void>;
15
+ disconnect: () => Promise<void>;
16
16
  }
17
17
 
18
18
  export type InternalTransmitterUpdate<
19
- T extends Document = Document,
20
- S extends OperationScope = OperationScope
19
+ T extends Document = Document,
20
+ S extends OperationScope = OperationScope,
21
21
  > = {
22
- driveId: string;
23
- documentId: string;
24
- scope: S;
25
- branch: string;
26
- operations: OperationUpdate[];
27
- state: T['state'][S];
22
+ driveId: string;
23
+ documentId: string;
24
+ scope: S;
25
+ branch: string;
26
+ operations: OperationUpdate[];
27
+ state: T["state"][S];
28
28
  };
29
29
 
30
30
  export class InternalTransmitter implements ITransmitter {
31
- private drive: BaseDocumentDriveServer;
32
- private listener: Listener;
33
- private receiver: IReceiver | undefined;
31
+ private drive: IBaseDocumentDriveServer;
32
+ private listener: Listener;
33
+ private receiver: IReceiver | undefined;
34
34
 
35
- constructor(listener: Listener, drive: BaseDocumentDriveServer) {
36
- this.listener = listener;
37
- this.drive = drive;
38
- }
39
-
40
- async transmit(strands: StrandUpdate[]): Promise<ListenerRevision[]> {
41
- if (!this.receiver) {
42
- return [];
43
- }
35
+ constructor(listener: Listener, drive: IBaseDocumentDriveServer) {
36
+ this.listener = listener;
37
+ this.drive = drive;
38
+ }
44
39
 
45
- const retrievedDocuments = new Map<string, Document>();
46
- const updates: InternalTransmitterUpdate[] = [];
47
- for (const strand of strands) {
48
- let document = retrievedDocuments.get(
49
- `${strand.driveId}:${strand.documentId}`
50
- );
51
- if (!document) {
52
- const revisions = buildRevisionsFilter(
53
- strands,
54
- strand.driveId,
55
- strand.documentId
56
- );
57
- document = await (strand.documentId
58
- ? this.drive.getDocument(
59
- strand.driveId,
60
- strand.documentId,
61
- { revisions }
62
- )
63
- : this.drive.getDrive(strand.driveId, { revisions }));
64
- retrievedDocuments.set(
65
- `${strand.driveId}:${strand.documentId}`,
66
- document
67
- );
68
- }
69
- updates.push({ ...strand, state: document.state[strand.scope] });
70
- }
71
- try {
72
- await this.receiver.transmit(updates);
73
- return strands.map(({ operations, ...s }) => ({
74
- ...s,
75
- status: 'SUCCESS',
76
- revision: operations[operations.length - 1]?.index ?? -1
77
- }));
78
- } catch (error) {
79
- logger.error(error);
80
- // TODO check which strand caused an error
81
- return strands.map(({ operations, ...s }) => ({
82
- ...s,
83
- status: 'ERROR',
84
- revision: (operations[0]?.index ?? 0) - 1
85
- }));
86
- }
40
+ async transmit(strands: StrandUpdate[]): Promise<ListenerRevision[]> {
41
+ if (!this.receiver) {
42
+ return [];
87
43
  }
88
44
 
89
- setReceiver(receiver: IReceiver) {
90
- this.receiver = receiver;
45
+ const retrievedDocuments = new Map<string, Document>();
46
+ const updates: InternalTransmitterUpdate[] = [];
47
+ for (const strand of strands) {
48
+ let document = retrievedDocuments.get(
49
+ `${strand.driveId}:${strand.documentId}`,
50
+ );
51
+ if (!document) {
52
+ const revisions = buildRevisionsFilter(
53
+ strands,
54
+ strand.driveId,
55
+ strand.documentId,
56
+ );
57
+ document = await (strand.documentId
58
+ ? this.drive.getDocument(strand.driveId, strand.documentId, {
59
+ revisions,
60
+ })
61
+ : this.drive.getDrive(strand.driveId, { revisions }));
62
+ retrievedDocuments.set(
63
+ `${strand.driveId}:${strand.documentId}`,
64
+ document,
65
+ );
66
+ }
67
+ updates.push({ ...strand, state: document.state[strand.scope] });
91
68
  }
92
-
93
- async disconnect(): Promise<void> {
94
- await this.receiver?.disconnect();
69
+ try {
70
+ await this.receiver.transmit(updates);
71
+ return strands.map(({ operations, ...s }) => ({
72
+ ...s,
73
+ status: "SUCCESS",
74
+ revision: operations[operations.length - 1]?.index ?? -1,
75
+ }));
76
+ } catch (error) {
77
+ logger.error(error);
78
+ // TODO check which strand caused an error
79
+ return strands.map(({ operations, ...s }) => ({
80
+ ...s,
81
+ status: "ERROR",
82
+ revision: (operations[0]?.index ?? 0) - 1,
83
+ }));
95
84
  }
85
+ }
86
+
87
+ setReceiver(receiver: IReceiver) {
88
+ this.receiver = receiver;
89
+ }
90
+
91
+ async disconnect(): Promise<void> {
92
+ await this.receiver?.disconnect();
93
+ }
96
94
  }