cojson-storage-do-sqlite 0.18.28

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 (39) hide show
  1. package/.turbo/turbo-build.log +4 -0
  2. package/CHANGELOG.md +9 -0
  3. package/LICENSE.txt +19 -0
  4. package/README.md +4 -0
  5. package/dist/index.d.ts +15 -0
  6. package/dist/index.d.ts.map +1 -0
  7. package/dist/index.js +37 -0
  8. package/dist/index.js.map +1 -0
  9. package/dist/tests/dosqlite.d.ts +89 -0
  10. package/dist/tests/dosqlite.d.ts.map +1 -0
  11. package/dist/tests/dosqlite.js +489 -0
  12. package/dist/tests/dosqlite.js.map +1 -0
  13. package/dist/tests/dosqlite.test.d.ts +2 -0
  14. package/dist/tests/dosqlite.test.d.ts.map +1 -0
  15. package/dist/tests/dosqlite.test.js +284 -0
  16. package/dist/tests/dosqlite.test.js.map +1 -0
  17. package/dist/tests/messagesTestUtils.d.ts +10 -0
  18. package/dist/tests/messagesTestUtils.d.ts.map +1 -0
  19. package/dist/tests/messagesTestUtils.js +42 -0
  20. package/dist/tests/messagesTestUtils.js.map +1 -0
  21. package/dist/tests/testUtils.d.ts +10 -0
  22. package/dist/tests/testUtils.d.ts.map +1 -0
  23. package/dist/tests/testUtils.js +88 -0
  24. package/dist/tests/testUtils.js.map +1 -0
  25. package/dist/tests/vitest.config.d.ts +3 -0
  26. package/dist/tests/vitest.config.d.ts.map +1 -0
  27. package/dist/tests/vitest.config.js +8 -0
  28. package/dist/tests/vitest.config.js.map +1 -0
  29. package/package.json +28 -0
  30. package/src/index.ts +60 -0
  31. package/src/tests/dosqlite.test.ts +338 -0
  32. package/src/tests/dosqlite.ts +771 -0
  33. package/src/tests/messagesTestUtils.ts +72 -0
  34. package/src/tests/testUtils.ts +112 -0
  35. package/src/tests/tsconfig.json +43 -0
  36. package/src/tests/vitest.config.ts +8 -0
  37. package/src/tests/worker-configuration.d.ts +9681 -0
  38. package/src/tests/wrangler.jsonc +20 -0
  39. package/tsconfig.json +17 -0
@@ -0,0 +1,72 @@
1
+ import type { CoValueCore, CojsonInternalTypes, SyncMessage } from "cojson";
2
+
3
+ function simplifySessions(msg: CojsonInternalTypes.CoValueKnownState) {
4
+ const count = Object.values(msg.sessions).reduce(
5
+ (acc: number, session: number) => acc + session,
6
+ 0,
7
+ );
8
+
9
+ if (msg.header) {
10
+ return `header/${count}`;
11
+ }
12
+
13
+ return "empty";
14
+ }
15
+
16
+ function simplifyNewContent(
17
+ content: CojsonInternalTypes.NewContentMessage["new"],
18
+ ) {
19
+ if (!content) {
20
+ return undefined;
21
+ }
22
+
23
+ return Object.values(content)
24
+ .map((c) => `After: ${c.after} New: ${c.newTransactions.length}`)
25
+ .join(" | ");
26
+ }
27
+
28
+ export function toSimplifiedMessages(
29
+ coValues: Record<string, CoValueCore>,
30
+ messages: {
31
+ from: "client" | "server" | "storage";
32
+ msg: SyncMessage;
33
+ }[],
34
+ ) {
35
+ function getCoValue(id: string) {
36
+ for (const [name, coValue] of Object.entries(coValues)) {
37
+ if (coValue.id === id) {
38
+ return name;
39
+ }
40
+ }
41
+
42
+ return `unknown/${id}`;
43
+ }
44
+
45
+ function toDebugString(
46
+ from: "client" | "server" | "storage",
47
+ msg: SyncMessage,
48
+ ) {
49
+ switch (msg.action) {
50
+ case "known":
51
+ return `${from} -> KNOWN ${msg.isCorrection ? "CORRECTION " : ""}${getCoValue(msg.id)} sessions: ${simplifySessions(msg)}`;
52
+ case "load":
53
+ return `${from} -> LOAD ${getCoValue(msg.id)} sessions: ${simplifySessions(msg)}`;
54
+ case "done":
55
+ return `${from} -> DONE ${getCoValue(msg.id)}`;
56
+ case "content":
57
+ return `${from} -> CONTENT ${getCoValue(msg.id)} header: ${Boolean(msg.header)} new: ${simplifyNewContent(msg.new)}`;
58
+ }
59
+ }
60
+
61
+ return messages.map((m) => toDebugString(m.from, m.msg));
62
+ }
63
+
64
+ export function debugMessages(
65
+ coValues: Record<string, CoValueCore>,
66
+ messages: {
67
+ from: "client" | "server" | "storage";
68
+ msg: SyncMessage;
69
+ }[],
70
+ ) {
71
+ console.log(toSimplifiedMessages(coValues, messages));
72
+ }
@@ -0,0 +1,112 @@
1
+ import type { LocalNode, RawCoID, SyncMessage } from "cojson";
2
+ import { StorageApiSync } from "cojson";
3
+ import { onTestFinished } from "vitest";
4
+
5
+ export function trackMessages() {
6
+ const messages: {
7
+ from: "client" | "server" | "storage";
8
+ msg: SyncMessage;
9
+ }[] = [];
10
+
11
+ const originalLoad = StorageApiSync.prototype.load;
12
+ const originalStore = StorageApiSync.prototype.store;
13
+
14
+ StorageApiSync.prototype.load = async function (id, callback, done) {
15
+ messages.push({
16
+ from: "client",
17
+ msg: {
18
+ action: "load",
19
+ id: id as RawCoID,
20
+ header: false,
21
+ sessions: {},
22
+ },
23
+ });
24
+ return originalLoad.call(
25
+ this,
26
+ id,
27
+ (msg) => {
28
+ messages.push({
29
+ from: "storage",
30
+ msg,
31
+ });
32
+ callback(msg);
33
+ },
34
+ done,
35
+ );
36
+ };
37
+
38
+ StorageApiSync.prototype.store = function (data, correctionCallback) {
39
+ messages.push({
40
+ from: "client",
41
+ msg: data,
42
+ });
43
+
44
+ return originalStore.call(this, data, (msg) => {
45
+ messages.push({
46
+ from: "storage",
47
+ msg: {
48
+ action: "known",
49
+ isCorrection: true,
50
+ ...msg,
51
+ },
52
+ });
53
+
54
+ const correctionMessages = correctionCallback(msg);
55
+
56
+ if (correctionMessages) {
57
+ for (const msg of correctionMessages) {
58
+ messages.push({
59
+ from: "client",
60
+ msg,
61
+ });
62
+ }
63
+ }
64
+
65
+ return correctionMessages;
66
+ });
67
+ };
68
+
69
+ const restore = () => {
70
+ StorageApiSync.prototype.load = originalLoad;
71
+ StorageApiSync.prototype.store = originalStore;
72
+ messages.length = 0;
73
+ };
74
+
75
+ onTestFinished(() => {
76
+ restore();
77
+ });
78
+
79
+ return {
80
+ messages,
81
+ restore,
82
+ };
83
+ }
84
+ export function waitFor(
85
+ callback: () => boolean | undefined | Promise<boolean | undefined>,
86
+ ) {
87
+ return new Promise<void>((resolve, reject) => {
88
+ const checkPassed = async () => {
89
+ try {
90
+ return { ok: await callback(), error: null };
91
+ } catch (error) {
92
+ return { ok: false, error };
93
+ }
94
+ };
95
+
96
+ let retries = 0;
97
+
98
+ const interval = setInterval(async () => {
99
+ const { ok, error } = await checkPassed();
100
+
101
+ if (ok !== false) {
102
+ clearInterval(interval);
103
+ resolve();
104
+ }
105
+
106
+ if (++retries > 10) {
107
+ clearInterval(interval);
108
+ reject(error);
109
+ }
110
+ }, 100);
111
+ });
112
+ }
@@ -0,0 +1,43 @@
1
+ {
2
+ "compilerOptions": {
3
+ /* Visit https://aka.ms/tsconfig.json to read more about this file */
4
+
5
+ /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
6
+ "target": "es2021",
7
+ /* Specify a set of bundled library declaration files that describe the target runtime environment. */
8
+ "lib": ["es2021"],
9
+ /* Specify what JSX code is generated. */
10
+ "jsx": "react-jsx",
11
+
12
+ /* Specify what module code is generated. */
13
+ "module": "es2022",
14
+ /* Specify how TypeScript looks up a file from a given module specifier. */
15
+ "moduleResolution": "Bundler",
16
+ /* Specify type package names to be included without being referenced in a source file. */
17
+ "types": ["worker-configuration.d.ts"],
18
+ /* Enable importing .json files */
19
+ "resolveJsonModule": true,
20
+
21
+ /* Allow JavaScript files to be a part of your program. Use the `checkJS` option to get errors from these files. */
22
+ "allowJs": true,
23
+ /* Enable error reporting in type-checked JavaScript files. */
24
+ "checkJs": false,
25
+
26
+ /* Disable emitting files from a compilation. */
27
+ "noEmit": true,
28
+
29
+ /* Ensure that each file can be safely transpiled without relying on other imports. */
30
+ "isolatedModules": true,
31
+ /* Allow 'import x from y' when a module doesn't have a default export. */
32
+ "allowSyntheticDefaultImports": true,
33
+ /* Ensure that casing is correct in imports. */
34
+ "forceConsistentCasingInFileNames": true,
35
+
36
+ /* Enable all strict type-checking options. */
37
+ "strict": true,
38
+
39
+ /* Skip type checking all .d.ts files. */
40
+ "skipLibCheck": true
41
+ },
42
+ "include": ["worker-configuration.d.ts", "../**/*.ts"]
43
+ }
@@ -0,0 +1,8 @@
1
+ import { defineProject } from "vitest/config";
2
+
3
+ export default defineProject({
4
+ test: {
5
+ name: "cloudflare-do-storage",
6
+ testTimeout: 10_000,
7
+ },
8
+ });