document-drive 1.0.0-websockets.1 → 1.0.1

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,65 +1,82 @@
1
- import stringify from 'json-stringify-deterministic';
2
- import { gql, requestGraphql } from '../../../utils/graphql';
1
+ import stringify from "json-stringify-deterministic";
2
+ import { gql, requestGraphql } from "../../../utils/graphql";
3
+ import { logger } from "../../../utils/logger";
3
4
  import {
4
- BaseDocumentDriveServer,
5
- Listener,
6
- ListenerRevision,
7
- StrandUpdate
8
- } from '../../types';
9
- import { ITransmitter } from './types';
10
- import { logger } from '../../../utils/logger';
5
+ IBaseDocumentDriveServer,
6
+ Listener,
7
+ ListenerRevision,
8
+ StrandUpdate,
9
+ } from "../../types";
10
+ import { ITransmitter, StrandUpdateSource } from "./types";
11
11
 
12
12
  export class SwitchboardPushTransmitter implements ITransmitter {
13
- private drive: BaseDocumentDriveServer;
14
- private listener: Listener;
15
- private targetURL: string;
13
+ private drive: IBaseDocumentDriveServer;
14
+ private listener: Listener;
15
+ private targetURL: string;
16
16
 
17
- constructor(listener: Listener, drive: BaseDocumentDriveServer) {
18
- this.listener = listener;
19
- this.drive = drive;
20
- this.targetURL = listener.callInfo!.data!;
21
- }
17
+ constructor(listener: Listener, drive: IBaseDocumentDriveServer) {
18
+ this.listener = listener;
19
+ this.drive = drive;
20
+ this.targetURL = listener.callInfo!.data!;
21
+ }
22
22
 
23
- async transmit(strands: StrandUpdate[]): Promise<ListenerRevision[]> {
24
- // Send Graphql mutation to switchboard
25
- try {
26
- const { pushUpdates } = await requestGraphql<{
27
- pushUpdates: ListenerRevision[];
28
- }>(
29
- this.targetURL,
30
- gql`
31
- mutation pushUpdates($strands: [InputStrandUpdate!]) {
32
- pushUpdates(strands: $strands) {
33
- driveId
34
- documentId
35
- scope
36
- branch
37
- status
38
- revision
39
- error
40
- }
41
- }
42
- `,
43
- {
44
- strands: strands.map(strand => ({
45
- ...strand,
46
- operations: strand.operations.map(op => ({
47
- ...op,
48
- input: stringify(op.input)
49
- }))
50
- }))
51
- }
52
- );
23
+ async transmit(
24
+ strands: StrandUpdate[],
25
+ source: StrandUpdateSource,
26
+ ): Promise<ListenerRevision[]> {
27
+ if (
28
+ source.type === "trigger" &&
29
+ source.trigger.data?.url === this.targetURL
30
+ ) {
31
+ return strands.map((strand) => ({
32
+ driveId: strand.driveId,
33
+ documentId: strand.documentId,
34
+ scope: strand.scope,
35
+ branch: strand.branch,
36
+ status: "SUCCESS",
37
+ revision: strand.operations.at(-1)?.index ?? -1,
38
+ }));
39
+ }
53
40
 
54
- if (!pushUpdates) {
55
- throw new Error("Couldn't update listener revision");
41
+ // Send Graphql mutation to switchboard
42
+ try {
43
+ const { pushUpdates } = await requestGraphql<{
44
+ pushUpdates: ListenerRevision[];
45
+ }>(
46
+ this.targetURL,
47
+ gql`
48
+ mutation pushUpdates($strands: [InputStrandUpdate!]) {
49
+ pushUpdates(strands: $strands) {
50
+ driveId
51
+ documentId
52
+ scope
53
+ branch
54
+ status
55
+ revision
56
+ error
56
57
  }
58
+ }
59
+ `,
60
+ {
61
+ strands: strands.map((strand) => ({
62
+ ...strand,
63
+ operations: strand.operations.map((op) => ({
64
+ ...op,
65
+ input: stringify(op.input),
66
+ })),
67
+ })),
68
+ },
69
+ );
70
+
71
+ if (!pushUpdates) {
72
+ throw new Error("Couldn't update listener revision");
73
+ }
57
74
 
58
- return pushUpdates;
59
- } catch (e) {
60
- logger.error(e);
61
- throw e;
62
- }
63
- return [];
75
+ return pushUpdates;
76
+ } catch (e) {
77
+ logger.error(e);
78
+ throw e;
64
79
  }
80
+ return [];
81
+ }
65
82
  }
@@ -1,33 +1,27 @@
1
1
  import {
2
- PullResponderTriggerData,
3
- SubscriptionTriggerData,
4
- Trigger
5
- } from 'document-model-libs/document-drive';
6
- import { ListenerRevision, StrandUpdate } from '../..';
2
+ PullResponderTriggerData,
3
+ Trigger,
4
+ } from "document-model-libs/document-drive";
5
+ import { ListenerRevision, StrandUpdate } from "../..";
7
6
 
8
- export interface ITransmitter {
9
- transmit(strands: StrandUpdate[]): Promise<ListenerRevision[]>;
10
- disconnect?(): Promise<void>;
11
- }
7
+ export type StrandUpdateSource =
8
+ | {
9
+ type: "local";
10
+ }
11
+ | { type: "trigger"; trigger: Trigger };
12
12
 
13
- export interface ITriggerTransmitter extends ITransmitter {
14
- processAcknowledge(
15
- driveId: string,
16
- listenerId: string,
17
- revisions: ListenerRevision[]
18
- ): Promise<boolean>
13
+ export interface ITransmitter {
14
+ transmit?(
15
+ strands: StrandUpdate[],
16
+ source: StrandUpdateSource,
17
+ ): Promise<ListenerRevision[]>;
18
+ disconnect?(): Promise<void>;
19
19
  }
20
-
21
20
  export interface InternalTransmitterService extends ITransmitter {
22
- getName(): string;
21
+ getName(): string;
23
22
  }
24
23
 
25
- export type PullResponderTrigger = Omit<Trigger, 'data' | 'type'> & {
26
- data: PullResponderTriggerData;
27
- type: 'PullResponder';
28
- };
29
-
30
- export type SubscriptionTrigger = Omit<Trigger, 'data' | 'type'> & {
31
- data: SubscriptionTriggerData;
32
- type: 'Subscription';
24
+ export type PullResponderTrigger = Omit<Trigger, "data" | "type"> & {
25
+ data: PullResponderTriggerData;
26
+ type: "PullResponder";
33
27
  };