document-drive 1.0.0-websockets → 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
package/README.md ADDED
@@ -0,0 +1 @@
1
+ # Document Drive
package/package.json CHANGED
@@ -1,89 +1,75 @@
1
1
  {
2
- "name": "document-drive",
3
- "version": "1.0.0-websockets",
4
- "license": "AGPL-3.0-only",
5
- "type": "module",
6
- "module": "./src/index.ts",
7
- "types": "./src/index.ts",
8
- "exports": {
9
- ".": "./src/index.ts",
10
- "./server": "./src/server/index.ts",
11
- "./storage": "./src/storage/index.ts",
12
- "./storage/browser": "./src/storage/browser.ts",
13
- "./storage/filesystem": "./src/storage/filesystem.ts",
14
- "./storage/memory": "./src/storage/memory.ts",
15
- "./storage/prisma": "./src/storage/prisma.ts",
16
- "./cache/redis": "./src/cache/redis.ts",
17
- "./cache/memory": "./src/cache/memory.ts",
18
- "./queue/redis": "./src/queue/redis.ts",
19
- "./queue/base": "./src/queue/base.ts",
20
- "./utils": "./src/utils/index.ts",
21
- "./utils/graphql": "./src/utils/graphql.ts",
22
- "./logger": "./src/utils/logger.ts"
23
- },
24
- "files": [
25
- "./src"
26
- ],
27
- "scripts": {
28
- "check-types": "tsc --noemit --emitDeclarationOnly false --project tsconfig.json",
29
- "lint": "eslint src --ext .js,.jsx,.ts,.tsx && yarn check-types",
30
- "lint:fix": "eslint src --ext .js,.jsx,.ts,.tsx --fix",
31
- "format": "prettier . --write",
32
- "release": "semantic-release",
33
- "test": "vitest run --coverage",
34
- "test:watch": "vitest watch"
35
- },
36
- "peerDependencies": {
37
- "document-model": "^1.4.0",
38
- "document-model-libs": "^1.60.1"
39
- },
40
- "optionalDependencies": {
41
- "@prisma/client": "5.14.0",
42
- "graphql-ws": "^5.16.0",
43
- "isomorphic-ws": "^5.0.0",
44
- "localforage": "^1.10.0",
45
- "redis": "^4.6.13",
46
- "sequelize": "^6.35.2",
47
- "sqlite3": "^5.1.7",
48
- "ws": "^8.17.0"
49
- },
50
- "dependencies": {
51
- "exponential-backoff": "^3.1.1",
52
- "graphql": "^16.8.1",
53
- "graphql-request": "^6.1.0",
54
- "json-stringify-deterministic": "^1.0.12",
55
- "nanoevents": "^9.0.0",
56
- "sanitize-filename": "^1.6.3",
57
- "uuid": "^9.0.1"
58
- },
59
- "devDependencies": {
60
- "@commitlint/cli": "^18.6.1",
61
- "@commitlint/config-conventional": "^18.6.3",
62
- "@prisma/client": "5.11.0",
63
- "@semantic-release/changelog": "^6.0.3",
64
- "@semantic-release/git": "^10.0.1",
65
- "@total-typescript/ts-reset": "^0.5.1",
66
- "@types/node": "^20.12.7",
67
- "@types/uuid": "^9.0.8",
68
- "@types/ws": "^8.5.10",
69
- "@typescript-eslint/eslint-plugin": "^6.21.0",
70
- "@typescript-eslint/parser": "^6.21.0",
71
- "@vitest/coverage-v8": "^1.4.0",
72
- "document-model": "^1.4.0",
73
- "document-model-libs": "^1.60.1",
74
- "eslint": "^8.57.0",
75
- "eslint-config-prettier": "^9.1.0",
76
- "fake-indexeddb": "^5.0.2",
77
- "localforage": "^1.10.0",
78
- "msw": "^2.2.13",
79
- "prettier": "^3.2.5",
80
- "prettier-plugin-organize-imports": "^3.2.4",
81
- "prisma": "^5.14.0",
82
- "semantic-release": "^23.0.8",
83
- "sequelize": "^6.37.2",
84
- "sqlite3": "^5.1.7",
85
- "typescript": "^5.4.4",
86
- "vitest": "^1.6.0"
87
- },
88
- "packageManager": "pnpm@9.1.4+sha256.30a1801ac4e723779efed13a21f4c39f9eb6c9fbb4ced101bce06b422593d7c9"
89
- }
2
+ "name": "document-drive",
3
+ "version": "1.0.0",
4
+ "license": "AGPL-3.0-only",
5
+ "type": "module",
6
+ "module": "./src/index.ts",
7
+ "types": "./src/index.ts",
8
+ "exports": {
9
+ ".": "./src/index.ts",
10
+ "./server": "./src/server/index.ts",
11
+ "./storage": "./src/storage/index.ts",
12
+ "./storage/browser": "./src/storage/browser.ts",
13
+ "./storage/filesystem": "./src/storage/filesystem.ts",
14
+ "./storage/memory": "./src/storage/memory.ts",
15
+ "./storage/prisma": "./src/storage/prisma.ts",
16
+ "./cache/redis": "./src/cache/redis.ts",
17
+ "./cache/memory": "./src/cache/memory.ts",
18
+ "./queue/redis": "./src/queue/redis.ts",
19
+ "./queue/base": "./src/queue/base.ts",
20
+ "./utils": "./src/utils/index.ts",
21
+ "./utils/graphql": "./src/utils/graphql.ts",
22
+ "./utils/migrations": "./src/utils/migrations.ts",
23
+ "./logger": "./src/utils/logger.ts"
24
+ },
25
+ "files": [
26
+ "./src"
27
+ ],
28
+ "peerDependencies": {
29
+ "document-model": "^2.1.0",
30
+ "document-model-libs": "^1.92.0"
31
+ },
32
+ "optionalDependencies": {
33
+ "@prisma/client": "^5.18.0",
34
+ "localforage": "^1.10.0",
35
+ "redis": "^4.6.15",
36
+ "sequelize": "^6.37.3",
37
+ "sqlite3": "^5.1.7"
38
+ },
39
+ "dependencies": {
40
+ "change-case": "^5.4.4",
41
+ "exponential-backoff": "^3.1.1",
42
+ "graphql": "^16.9.0",
43
+ "graphql-request": "^6.1.0",
44
+ "json-stringify-deterministic": "^1.0.12",
45
+ "nanoevents": "^9.0.0",
46
+ "sanitize-filename": "^1.6.3",
47
+ "uuid": "^9.0.1"
48
+ },
49
+ "devDependencies": {
50
+ "@prisma/client": "5.17.0",
51
+ "@types/node": "^20.14.11",
52
+ "@types/uuid": "^9.0.8",
53
+ "document-model": "2.2.0",
54
+ "document-model-libs": "1.93.1",
55
+ "fake-indexeddb": "^5.0.2",
56
+ "localforage": "^1.10.0",
57
+ "msw": "^2.3.1",
58
+ "prisma": "^5.18.0",
59
+ "sequelize": "^6.37.2",
60
+ "sqlite3": "^5.1.7",
61
+ "webdriverio": "^9.0.9",
62
+ "vitest-fetch-mock": "^0.3.0"
63
+ },
64
+ "scripts": {
65
+ "check-types": "tsc --noEmit",
66
+ "postlint": "npm run check-types",
67
+ "lint": "eslint",
68
+ "release": "semantic-release",
69
+ "test": "vitest run --coverage --exclude \"test/flaky/**\"",
70
+ "test:watch": "vitest watch",
71
+ "clean": "rimraf dist",
72
+ "clean:node_modules": "rimraf node_modules",
73
+ "build": "prisma generate"
74
+ }
75
+ }
@@ -1,2 +1,2 @@
1
- export * from './memory';
2
- export * from './types';
1
+ export * from "./memory";
2
+ export * from "./types";
@@ -2,23 +2,32 @@ import { Document } from "document-model/document";
2
2
  import { ICache } from "./types";
3
3
 
4
4
  class InMemoryCache implements ICache {
5
- private cache = new Map<string, Map<string, Document>>();
5
+ private cache = new Map<string, Map<string, Document>>();
6
6
 
7
- async setDocument(drive: string, id: string, document: Document) {
8
- if (!this.cache.has(drive)) {
9
- this.cache.set(drive, new Map());
10
- }
11
- this.cache.get(drive)?.set(id, document);
12
- return true;
7
+ async setDocument(drive: string, id: string, document: Document) {
8
+ const global = document.operations.global.map((e) => {
9
+ delete e.resultingState;
10
+ return e;
11
+ });
12
+ const local = document.operations.local.map((e) => {
13
+ delete e.resultingState;
14
+ return e;
15
+ });
16
+ const doc = { ...document, operations: { global, local } };
17
+ if (!this.cache.has(drive)) {
18
+ this.cache.set(drive, new Map());
13
19
  }
20
+ this.cache.get(drive)?.set(id, doc);
21
+ return true;
22
+ }
14
23
 
15
- async deleteDocument(drive: string, id: string) {
16
- return this.cache.get(drive)?.delete(id) ?? false;
17
- }
24
+ async deleteDocument(drive: string, id: string) {
25
+ return this.cache.get(drive)?.delete(id) ?? false;
26
+ }
18
27
 
19
- async getDocument(drive: string, id: string) {
20
- return this.cache.get(drive)?.get(id);
21
- }
28
+ async getDocument(drive: string, id: string) {
29
+ return this.cache.get(drive)?.get(id);
30
+ }
22
31
  }
23
32
 
24
33
  export default InMemoryCache;
@@ -1,29 +1,56 @@
1
1
  import { Document } from "document-model/document";
2
- import { ICache } from "./types";
3
2
  import type { RedisClientType } from "redis";
4
- import { logger } from "../utils/logger";
3
+ import { ICache } from "./types";
5
4
 
6
5
  class RedisCache implements ICache {
7
- private redis: RedisClientType;
6
+ private redis: RedisClientType;
7
+ private timeoutInSeconds: number;
8
8
 
9
- constructor(redis: RedisClientType) {
10
- this.redis = redis;
11
- this.redis.flushAll().catch(logger.error);
12
- }
9
+ constructor(
10
+ redis: RedisClientType,
11
+ timeoutInSeconds: number | undefined = 5 * 60,
12
+ ) {
13
+ this.redis = redis;
14
+ this.timeoutInSeconds = timeoutInSeconds;
15
+ }
13
16
 
14
- async setDocument(drive: string, id: string, document: Document) {
15
- return (await this.redis.hSet(drive, id, JSON.stringify(document))) > 0;
16
- }
17
+ private static _getId(drive: string, id: string) {
18
+ return `cache:${drive}:${id}`;
19
+ }
17
20
 
18
- async getDocument(drive: string, id: string) {
19
- const doc = await this.redis.hGet(drive, id);
21
+ async setDocument(drive: string, id: string, document: Document) {
22
+ const global = document.operations.global.map((e) => {
23
+ delete e.resultingState;
24
+ return e;
25
+ });
26
+ const local = document.operations.local.map((e) => {
27
+ delete e.resultingState;
28
+ return e;
29
+ });
30
+ const doc = { ...document, operations: { global, local } };
31
+ const redisId = RedisCache._getId(drive, id);
32
+ const result = await this.redis.set(redisId, JSON.stringify(doc), {
33
+ EX: this.timeoutInSeconds ? this.timeoutInSeconds : undefined,
34
+ });
20
35
 
21
- return doc ? JSON.parse(doc) as Document : undefined;
36
+ if (result === "OK") {
37
+ return true;
22
38
  }
23
39
 
24
- async deleteDocument(drive: string, id: string) {
25
- return (await this.redis.hDel(drive, id)) > 0;
26
- }
40
+ return false;
41
+ }
42
+
43
+ async getDocument(drive: string, id: string) {
44
+ const redisId = RedisCache._getId(drive, id);
45
+ const doc = await this.redis.get(redisId);
46
+
47
+ return doc ? (JSON.parse(doc) as Document) : undefined;
48
+ }
49
+
50
+ async deleteDocument(drive: string, id: string) {
51
+ const redisId = RedisCache._getId(drive, id);
52
+ return (await this.redis.del(redisId)) > 0;
53
+ }
27
54
  }
28
55
 
29
56
  export default RedisCache;
@@ -1,9 +1,9 @@
1
1
  import type { Document } from "document-model/document";
2
2
 
3
3
  export interface ICache {
4
- setDocument(drive: string, id: string, document: Document): Promise<boolean>
5
- getDocument(drive: string, id: string): Promise<Document | undefined>
4
+ setDocument(drive: string, id: string, document: Document): Promise<boolean>;
5
+ getDocument(drive: string, id: string): Promise<Document | undefined>;
6
6
 
7
- // @returns — true if a document existed and has been removed, or false if the document is not cached.
8
- deleteDocument(drive: string, id: string): Promise<boolean>
7
+ // @returns — true if a document existed and has been removed, or false if the document is not cached.
8
+ deleteDocument(drive: string, id: string): Promise<boolean>;
9
9
  }
package/src/index.ts CHANGED
@@ -1,3 +1,6 @@
1
- export * from './server';
2
- export * from './storage';
3
- export * from './utils';
1
+ export * from "./server";
2
+ export * from "./server/error";
3
+ export * from "./storage";
4
+ export * from "./utils";
5
+
6
+ export const test = "test";