jazz-run 0.7.35-guest-auth.5 → 0.7.35-guest-auth.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,8 +1,8 @@
1
1
 
2
- > jazz-run@0.7.35-guest-auth.3 build /Users/anselm/jazz/jazz/packages/jazz-run
2
+ > jazz-run@0.7.35-guest-auth.5 build /Users/anselm/jazz/jazz/packages/jazz-run
3
3
  > npm run lint && rm -rf ./dist && tsc --sourceMap --outDir dist
4
4
 
5
5
 
6
- > jazz-run@0.7.35-guest-auth.3 lint
6
+ > jazz-run@0.7.35-guest-auth.5 lint
7
7
  > eslint . --ext ts,tsx
8
8
 
@@ -0,0 +1,4 @@
1
+
2
+ > jazz-run@0.7.35-guest-auth.5 lint /Users/anselm/jazz/jazz/packages/jazz-run
3
+ > eslint . --ext ts,tsx
4
+
package/CHANGELOG.md CHANGED
@@ -1,43 +1,27 @@
1
- # jazz-autosub
1
+ # jazz-run
2
2
 
3
- ## 0.7.35-guest-auth.5
3
+ ## 0.7.35-guest-auth.6
4
4
 
5
5
  ### Patch Changes
6
6
 
7
7
  - Updated dependencies
8
- - cojson@0.7.35-guest-auth.5
9
- - cojson-transport-ws@0.7.35-guest-auth.5
10
- - jazz-tools@0.7.35-guest-auth.5
8
+ - jazz-tools@0.7.35-guest-auth.6
11
9
 
12
- ## 0.7.35-guest-auth.4
10
+ ## 0.7.35
13
11
 
14
12
  ### Patch Changes
15
13
 
16
- - Updated dependencies
17
- - jazz-tools@0.7.35-guest-auth.4
18
-
19
- ## 0.7.35-guest-auth.3
20
-
21
- ### Patch Changes
22
-
23
- - Updated dependencies
24
- - jazz-tools@0.7.35-guest-auth.3
25
-
26
- ## 0.7.35-unique.2
27
-
28
- ### Patch Changes
29
-
30
- - Updated dependencies
31
- - jazz-tools@0.7.35-unique.2
32
- - cojson@0.7.35-unique.2
33
- - cojson-transport-ws@0.7.35-unique.2
34
-
35
- ## 0.7.35-new-auth.0
36
-
37
- ### Patch Changes
38
-
39
- - Updated dependencies
40
- - jazz-tools@0.7.35-new-auth.0
14
+ - 403b430: Added sync command to start a local sync server
15
+ - f350e90: Added a priority system for the sync messages
16
+ - Updated dependencies [49a8b54]
17
+ - Updated dependencies [35bbcd9]
18
+ - Updated dependencies [6f80282]
19
+ - Updated dependencies [35bbcd9]
20
+ - Updated dependencies [f350e90]
21
+ - jazz-tools@0.7.35
22
+ - cojson@0.7.35
23
+ - cojson-storage-sqlite@0.7.35
24
+ - cojson-transport-ws@0.7.35
41
25
 
42
26
  ## 0.7.34
43
27
 
package/dist/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  #!/usr/bin/env node
2
+ /* istanbul ignore file -- @preserve */
2
3
  import { Command, Options } from "@effect/cli";
3
4
  import { NodeContext, NodeRuntime } from "@effect/platform-node";
4
5
  import { Console, Effect } from "effect";
@@ -6,6 +7,7 @@ import { createWebSocketPeer } from "cojson-transport-ws";
6
7
  import { WebSocket } from "ws";
7
8
  import { Account, WasmCrypto, createJazzContext, isControlledAccount, } from "jazz-tools";
8
9
  import { fixedCredentialsAuth, randomSessionProvider } from "jazz-tools/src/internal";
10
+ import { startSync } from "./startSync.js";
9
11
  const jazzTools = Command.make("jazz-tools");
10
12
  const name = Options.text("name").pipe(Options.withAlias("n"));
11
13
  const peer = Options.text("peer")
@@ -53,7 +55,7 @@ JAZZ_WORKER_SECRET=${account._raw.agentSecret}
53
55
  });
54
56
  const accountBase = Command.make("account");
55
57
  const account = accountBase.pipe(Command.withSubcommands([accountCreate]));
56
- const command = jazzTools.pipe(Command.withSubcommands([account]));
58
+ const command = jazzTools.pipe(Command.withSubcommands([account, startSync]));
57
59
  const cli = Command.run(command, {
58
60
  name: "Jazz CLI Tools",
59
61
  version: "v0.7.0",
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AACA,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,aAAa,CAAC;AAC/C,OAAO,EAAE,WAAW,EAAE,WA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C,CAAC,CAAC;AACP,CAAC,CACJ,CAAC;AAEF,MAAM,WAAW,GAAG,OAAO,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;AAE5C,MAAM,OAAO,GAAG,WAAW,CAAC,IAAI,CAAC,OAAO,CAAC,eAAe,CAAC,CAAC,aAAa,CAAC,CAAC,CAAC,CAAC;AAE3E,MAAM,OAAO,GAAG,SAAS,CAAC,IAAI,CAAC,OAAO,CAAC,eAAe,CAAC,CAAC,OAAO,EAAE,SAAS,CAAC,CAAC,CAAC,CAAC;AAE9E,MAAM,GAAG,GAAG,OAAO,CAAC,GAAG,CAAC,OAAO,EAAE;IAC7B,IAAI,EAAE,gBAAgB;IACtB,OAAO,EAAE,QAAQ;CACpB,CAAC,CAAC;AAEH,MAAM,CAAC,OAAO,CAAC,GAAG,EAAE,CAAC,GAAG,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,CACxC,MAAM,CAAC,OAAO,CAAC,WAAW,CAAC,KAAK,CAAC,EACjC,WAAW,CAAC,OAAO,CACtB,CAAC"}
@@ -0,0 +1,60 @@
1
+ /* istanbul ignore file -- @preserve */
2
+ import { Command, Options } from "@effect/cli";
3
+ import { ControlledAgent, LocalNode, WasmCrypto, } from "cojson";
4
+ import { WebSocketServer } from "ws";
5
+ import { createWebSocketPeer } from "cojson-transport-ws";
6
+ import { Effect } from "effect";
7
+ import { SQLiteStorage } from "cojson-storage-sqlite";
8
+ import { dirname } from "node:path";
9
+ import { mkdir } from "node:fs/promises";
10
+ const port = Options.text("port")
11
+ .pipe(Options.withAlias("p"))
12
+ .pipe(Options.withDescription("Select a different port for the WebSocket server. Default is 4200"))
13
+ .pipe(Options.withDefault("4200"));
14
+ const inMemory = Options.boolean("in-memory").pipe(Options.withDescription("Use an in-memory storage instead of file-based"));
15
+ const db = Options.file("db")
16
+ .pipe(Options.withDescription("The path to the file where to store the data. Default is 'sync-db/storage.db'"))
17
+ .pipe(Options.withDefault("sync-db/storage.db"));
18
+ export const startSync = Command.make("sync", { port, inMemory, db }, ({ port, inMemory, db }) => {
19
+ return Effect.gen(function* () {
20
+ const crypto = yield* Effect.promise(() => WasmCrypto.create());
21
+ const wss = new WebSocketServer({ port: parseInt(port) });
22
+ console.log("COJSON sync server listening on port " + wss.options.port);
23
+ const agentSecret = crypto.newRandomAgentSecret();
24
+ const agentID = crypto.getAgentID(agentSecret);
25
+ const localNode = new LocalNode(new ControlledAgent(agentSecret, crypto), crypto.newRandomSessionID(agentID), crypto);
26
+ if (!inMemory) {
27
+ yield* Effect.promise(() => mkdir(dirname(db), { recursive: true }));
28
+ const storage = yield* Effect.promise(() => SQLiteStorage.asPeer({ filename: db }));
29
+ localNode.syncManager.addPeer(storage);
30
+ }
31
+ wss.on("connection", function connection(ws, req) {
32
+ // ping/pong for the connection liveness
33
+ const pinging = setInterval(() => {
34
+ ws.send(JSON.stringify({
35
+ type: "ping",
36
+ time: Date.now(),
37
+ dc: "unknown",
38
+ }));
39
+ }, 1500);
40
+ ws.on("close", () => {
41
+ clearInterval(pinging);
42
+ });
43
+ const clientAddress = req.headers["x-forwarded-for"]
44
+ ?.split(",")[0]
45
+ ?.trim() || req.socket.remoteAddress;
46
+ const clientId = clientAddress + "@" + new Date().toISOString();
47
+ localNode.syncManager.addPeer(createWebSocketPeer({
48
+ id: clientId,
49
+ role: "client",
50
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
51
+ websocket: ws, // TODO: fix types
52
+ expectPings: false,
53
+ }));
54
+ ws.on("error", (e) => console.error(`Error on connection ${clientId}:`, e));
55
+ });
56
+ // Keep the server up
57
+ yield* Effect.never;
58
+ });
59
+ });
60
+ //# sourceMappingURL=startSync.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"startSync.js","sourceRoot":"","sources":["../src/startSync.ts"],"names":[],"mappings":"AAAA,uCAAuC;AACvC,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,aAAa,CAAC;AAC/C,OAAO,EACH,eAAe,EACf,SAA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CAAC;YACN,CAAC,EAAE,IAAI,CAAC,CAAC;YAET,EAAE,CAAC,EAAE,CAAC,OAAO,EAAE,GAAG,EAAE;gBAChB,aAAa,CAAC,OAAO,CAAC,CAAC;YAC3B,CAAC,CAAC,CAAC;YAEH,MAAM,aAAa,GACd,GAAG,CAAC,OAAO,CAAC,iBAAiB,CAAwB;gBAClD,EAAE,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;gBACf,EAAE,IAAI,EAAE,IAAI,GAAG,CAAC,MAAM,CAAC,aAAa,CAAC;YAE7C,MAAM,QAAQ,GAAG,aAAa,GAAG,GAAG,GAAG,IAAI,IAAI,EAAE,CAAC,WAAW,EAAE,CAAC;YAEhE,SAAS,CAAC,WAAW,CAAC,OAAO,CACzB,mBAAmB,CAAC;gBAChB,EAAE,EAAE,QAAQ;gBACZ,IAAI,EAAE,QAAQ;gBACd,8DAA8D;gBAC9D,SAAS,EAAE,EAAS,EAAE,kBAAkB;gBACxC,WAAW,EAAE,KAAK;aACrB,CAAC,CACL,CAAC;YAEF,EAAE,CAAC,EAAE,CAAC,OAAO,EAAE,CAAC,CAAC,EAAE,EAAE,CACjB,OAAO,CAAC,KAAK,CAAC,uBAAuB,QAAQ,GAAG,EAAE,CAAC,CAAC,CACvD,CAAC;QACN,CAAC,CAAC,CAAC;QAEH,qBAAqB;QACrB,KAAK,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC;IACxB,CAAC,CAAC,CAAC;AACP,CAAC,CACJ,CAAC"}
package/package.json CHANGED
@@ -3,19 +3,20 @@
3
3
  "bin": "./dist/index.js",
4
4
  "type": "module",
5
5
  "license": "MIT",
6
- "version": "0.7.35-guest-auth.5",
6
+ "version": "0.7.35-guest-auth.6",
7
7
  "dependencies": {
8
- "effect": "^3.6.5",
9
8
  "@effect/cli": "^0.41.2",
10
- "@effect/schema": "^0.71.1",
11
9
  "@effect/platform-node": "^0.57.2",
12
10
  "@effect/printer": "^0.34.5",
13
11
  "@effect/printer-ansi": "^0.34.5",
12
+ "@effect/schema": "^0.71.1",
14
13
  "@effect/typeclass": "^0.25.5",
14
+ "effect": "^3.6.5",
15
15
  "ws": "^8.14.2",
16
16
  "cojson": "0.7.35-guest-auth.5",
17
17
  "cojson-transport-ws": "0.7.35-guest-auth.5",
18
- "jazz-tools": "0.7.35-guest-auth.5"
18
+ "jazz-tools": "0.7.35-guest-auth.6",
19
+ "cojson-storage-sqlite": "0.7.35-guest-auth.5"
19
20
  },
20
21
  "devDependencies": {
21
22
  "@types/ws": "^8.5.5",
package/src/index.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  #!/usr/bin/env node
2
+ /* istanbul ignore file -- @preserve */
2
3
  import { Command, Options } from "@effect/cli";
3
4
  import { NodeContext, NodeRuntime } from "@effect/platform-node";
4
5
  import { Console, Effect } from "effect";
@@ -11,6 +12,7 @@ import {
11
12
  isControlledAccount,
12
13
  } from "jazz-tools";
13
14
  import { fixedCredentialsAuth, randomSessionProvider } from "jazz-tools/src/internal";
15
+ import { startSync } from "./startSync.js";
14
16
 
15
17
  const jazzTools = Command.make("jazz-tools");
16
18
 
@@ -86,7 +88,7 @@ const accountBase = Command.make("account");
86
88
 
87
89
  const account = accountBase.pipe(Command.withSubcommands([accountCreate]));
88
90
 
89
- const command = jazzTools.pipe(Command.withSubcommands([account]));
91
+ const command = jazzTools.pipe(Command.withSubcommands([account, startSync]));
90
92
 
91
93
  const cli = Command.run(command, {
92
94
  name: "Jazz CLI Tools",
@@ -0,0 +1,113 @@
1
+ /* istanbul ignore file -- @preserve */
2
+ import { Command, Options } from "@effect/cli";
3
+ import {
4
+ ControlledAgent,
5
+ LocalNode,
6
+ WasmCrypto,
7
+ } from "cojson";
8
+ import { WebSocketServer } from "ws";
9
+
10
+ import { createWebSocketPeer } from "cojson-transport-ws";
11
+ import { Effect } from "effect";
12
+ import { SQLiteStorage } from "cojson-storage-sqlite";
13
+ import { dirname } from "node:path";
14
+ import { mkdir } from "node:fs/promises";
15
+
16
+ const port = Options.text("port")
17
+ .pipe(Options.withAlias("p"))
18
+ .pipe(
19
+ Options.withDescription(
20
+ "Select a different port for the WebSocket server. Default is 4200",
21
+ ),
22
+ )
23
+ .pipe(Options.withDefault("4200"));
24
+
25
+ const inMemory = Options.boolean("in-memory").pipe(
26
+ Options.withDescription("Use an in-memory storage instead of file-based"),
27
+ );
28
+
29
+ const db = Options.file("db")
30
+ .pipe(
31
+ Options.withDescription(
32
+ "The path to the file where to store the data. Default is 'sync-db/storage.db'",
33
+ ),
34
+ )
35
+ .pipe(Options.withDefault("sync-db/storage.db"));
36
+
37
+ export const startSync = Command.make(
38
+ "sync",
39
+ { port, inMemory, db },
40
+ ({ port, inMemory, db }) => {
41
+ return Effect.gen(function* () {
42
+ const crypto = yield* Effect.promise(() => WasmCrypto.create());
43
+
44
+ const wss = new WebSocketServer({ port: parseInt(port) });
45
+
46
+ console.log(
47
+ "COJSON sync server listening on port " + wss.options.port,
48
+ );
49
+
50
+ const agentSecret = crypto.newRandomAgentSecret();
51
+ const agentID = crypto.getAgentID(agentSecret);
52
+
53
+ const localNode = new LocalNode(
54
+ new ControlledAgent(agentSecret, crypto),
55
+ crypto.newRandomSessionID(agentID),
56
+ crypto,
57
+ );
58
+
59
+ if (!inMemory) {
60
+ yield* Effect.promise(() =>
61
+ mkdir(dirname(db), { recursive: true }),
62
+ );
63
+
64
+ const storage = yield* Effect.promise(() =>
65
+ SQLiteStorage.asPeer({ filename: db }),
66
+ );
67
+
68
+ localNode.syncManager.addPeer(storage);
69
+ }
70
+
71
+ wss.on("connection", function connection(ws, req) {
72
+ // ping/pong for the connection liveness
73
+ const pinging = setInterval(() => {
74
+ ws.send(
75
+ JSON.stringify({
76
+ type: "ping",
77
+ time: Date.now(),
78
+ dc: "unknown",
79
+ }),
80
+ );
81
+ }, 1500);
82
+
83
+ ws.on("close", () => {
84
+ clearInterval(pinging);
85
+ });
86
+
87
+ const clientAddress =
88
+ (req.headers["x-forwarded-for"] as string | undefined)
89
+ ?.split(",")[0]
90
+ ?.trim() || req.socket.remoteAddress;
91
+
92
+ const clientId = clientAddress + "@" + new Date().toISOString();
93
+
94
+ localNode.syncManager.addPeer(
95
+ createWebSocketPeer({
96
+ id: clientId,
97
+ role: "client",
98
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
99
+ websocket: ws as any, // TODO: fix types
100
+ expectPings: false,
101
+ }),
102
+ );
103
+
104
+ ws.on("error", (e) =>
105
+ console.error(`Error on connection ${clientId}:`, e),
106
+ );
107
+ });
108
+
109
+ // Keep the server up
110
+ yield* Effect.never;
111
+ });
112
+ },
113
+ );