jazz-run 0.8.15 → 0.8.16

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.
@@ -1,95 +1,93 @@
1
+ import { createServer } from "http";
1
2
  import { ControlledAgent, LocalNode, WasmCrypto } from "cojson";
2
3
  import { WebSocketServer } from "ws";
3
- import { createServer } from "http";
4
4
 
5
- import { createWebSocketPeer } from "cojson-transport-ws";
6
- import { SQLiteStorage } from "cojson-storage-sqlite";
7
- import { dirname } from "node:path";
8
5
  import { mkdir } from "node:fs/promises";
6
+ import { dirname } from "node:path";
7
+ import { SQLiteStorage } from "cojson-storage-sqlite";
8
+ import { createWebSocketPeer } from "cojson-transport-ws";
9
9
 
10
10
  export const startSyncServer = async ({
11
- port,
12
- inMemory,
13
- db,
11
+ port,
12
+ inMemory,
13
+ db,
14
14
  }: {
15
- port: string | undefined;
16
- inMemory: boolean;
17
- db: string;
15
+ port: string | undefined;
16
+ inMemory: boolean;
17
+ db: string;
18
18
  }) => {
19
- const crypto = await WasmCrypto.create();
19
+ const crypto = await WasmCrypto.create();
20
20
 
21
- const server = createServer((req, res) => {
22
- if (req.url === "/health") {
23
- res.writeHead(200);
24
- res.end("ok");
25
- }
21
+ const server = createServer((req, res) => {
22
+ if (req.url === "/health") {
23
+ res.writeHead(200);
24
+ res.end("ok");
25
+ }
26
+ });
27
+ const wss = new WebSocketServer({ noServer: true });
28
+
29
+ const agentSecret = crypto.newRandomAgentSecret();
30
+ const agentID = crypto.getAgentID(agentSecret);
31
+
32
+ const localNode = new LocalNode(
33
+ new ControlledAgent(agentSecret, crypto),
34
+ crypto.newRandomSessionID(agentID),
35
+ crypto,
36
+ );
37
+
38
+ if (!inMemory) {
39
+ await mkdir(dirname(db), { recursive: true });
40
+
41
+ const storage = await SQLiteStorage.asPeer({ filename: db });
42
+
43
+ localNode.syncManager.addPeer(storage);
44
+ }
45
+
46
+ wss.on("connection", function connection(ws, req) {
47
+ // ping/pong for the connection liveness
48
+ const pinging = setInterval(() => {
49
+ ws.send(
50
+ JSON.stringify({
51
+ type: "ping",
52
+ time: Date.now(),
53
+ dc: "unknown",
54
+ }),
55
+ );
56
+ }, 1500);
57
+
58
+ ws.on("close", () => {
59
+ clearInterval(pinging);
26
60
  });
27
- const wss = new WebSocketServer({ noServer: true });
28
-
29
- const agentSecret = crypto.newRandomAgentSecret();
30
- const agentID = crypto.getAgentID(agentSecret);
31
61
 
32
- const localNode = new LocalNode(
33
- new ControlledAgent(agentSecret, crypto),
34
- crypto.newRandomSessionID(agentID),
35
- crypto,
62
+ const clientAddress =
63
+ (req.headers["x-forwarded-for"] as string | undefined)
64
+ ?.split(",")[0]
65
+ ?.trim() || req.socket.remoteAddress;
66
+
67
+ const clientId = clientAddress + "@" + new Date().toISOString();
68
+
69
+ localNode.syncManager.addPeer(
70
+ createWebSocketPeer({
71
+ id: clientId,
72
+ role: "client",
73
+ websocket: ws,
74
+ expectPings: false,
75
+ batchingByDefault: false,
76
+ }),
36
77
  );
37
78
 
38
- if (!inMemory) {
39
- await mkdir(dirname(db), { recursive: true })
79
+ ws.on("error", (e) => console.error(`Error on connection ${clientId}:`, e));
80
+ });
40
81
 
41
- const storage = await SQLiteStorage.asPeer({ filename: db });
42
-
43
- localNode.syncManager.addPeer(storage);
82
+ server.on("upgrade", function upgrade(req, socket, head) {
83
+ if (req.url !== "/health") {
84
+ wss.handleUpgrade(req, socket, head, function done(ws) {
85
+ wss.emit("connection", ws, req);
86
+ });
44
87
  }
88
+ });
45
89
 
46
- wss.on("connection", function connection(ws, req) {
47
- // ping/pong for the connection liveness
48
- const pinging = setInterval(() => {
49
- ws.send(
50
- JSON.stringify({
51
- type: "ping",
52
- time: Date.now(),
53
- dc: "unknown",
54
- }),
55
- );
56
- }, 1500);
57
-
58
- ws.on("close", () => {
59
- clearInterval(pinging);
60
- });
61
-
62
- const clientAddress =
63
- (req.headers["x-forwarded-for"] as string | undefined)
64
- ?.split(",")[0]
65
- ?.trim() || req.socket.remoteAddress;
66
-
67
- const clientId = clientAddress + "@" + new Date().toISOString();
68
-
69
- localNode.syncManager.addPeer(
70
- createWebSocketPeer({
71
- id: clientId,
72
- role: "client",
73
- websocket: ws,
74
- expectPings: false,
75
- batchingByDefault: false,
76
- }),
77
- );
78
-
79
- ws.on("error", (e) =>
80
- console.error(`Error on connection ${clientId}:`, e),
81
- );
82
- });
83
-
84
- server.on("upgrade", function upgrade(req, socket, head) {
85
- if (req.url !== "/health") {
86
- wss.handleUpgrade(req, socket, head, function done(ws) {
87
- wss.emit("connection", ws, req);
88
- });
89
- }
90
- });
91
-
92
- server.listen(port ? parseInt(port) : undefined);
90
+ server.listen(port ? parseInt(port) : undefined);
93
91
 
94
- return server;
92
+ return server;
95
93
  };
@@ -1,32 +1,42 @@
1
- import { describe, onTestFinished, it, expect } from "vitest";
2
- import { startSyncServer } from "../startSyncServer.js";
1
+ import { describe, expect, it, onTestFinished } from "vitest";
3
2
  import { createWorkerAccount } from "../createWorkerAccount.js";
3
+ import { startSyncServer } from "../startSyncServer.js";
4
4
 
5
5
  describe("createWorkerAccount - integration tests", () => {
6
- it("should create a worker account using the local sync server", async () => {
7
- // Pass port: undefined to let the server choose a random port
8
- const server = await startSyncServer({ port: undefined, inMemory: true, db: '' });
9
-
10
- onTestFinished(() => {
11
- server.close()
12
- });
6
+ it("should create a worker account using the local sync server", async () => {
7
+ // Pass port: undefined to let the server choose a random port
8
+ const server = await startSyncServer({
9
+ port: undefined,
10
+ inMemory: true,
11
+ db: "",
12
+ });
13
13
 
14
- const address = server.address();
14
+ onTestFinished(() => {
15
+ server.close();
16
+ });
15
17
 
16
- if (typeof address !== 'object' || address === null) {
17
- throw new Error('Server address is not an object');
18
- }
18
+ const address = server.address();
19
19
 
20
- const { accountId, agentSecret } = await createWorkerAccount({ name: "test", peer: `ws://localhost:${address.port}` });
20
+ if (typeof address !== "object" || address === null) {
21
+ throw new Error("Server address is not an object");
22
+ }
21
23
 
22
- expect(accountId).toBeDefined();
23
- expect(agentSecret).toBeDefined();
24
+ const { accountId, agentSecret } = await createWorkerAccount({
25
+ name: "test",
26
+ peer: `ws://localhost:${address.port}`,
24
27
  });
25
28
 
26
- it("should create a worker account using the Jazz cloud", async () => {
27
- const { accountId, agentSecret } = await createWorkerAccount({ name: "test", peer: `wss://cloud.jazz.tools` });
29
+ expect(accountId).toBeDefined();
30
+ expect(agentSecret).toBeDefined();
31
+ });
28
32
 
29
- expect(accountId).toBeDefined();
30
- expect(agentSecret).toBeDefined();
33
+ it("should create a worker account using the Jazz cloud", async () => {
34
+ const { accountId, agentSecret } = await createWorkerAccount({
35
+ name: "test",
36
+ peer: `wss://cloud.jazz.tools`,
31
37
  });
38
+
39
+ expect(accountId).toBeDefined();
40
+ expect(agentSecret).toBeDefined();
41
+ });
32
42
  });
package/tsconfig.json CHANGED
@@ -11,5 +11,5 @@
11
11
  "noUncheckedIndexedAccess": true,
12
12
  "esModuleInterop": true
13
13
  },
14
- "include": ["./src/**/*"],
14
+ "include": ["./src/**/*"]
15
15
  }
package/.eslintrc.cjs DELETED
@@ -1,24 +0,0 @@
1
- module.exports = {
2
- extends: [
3
- "eslint:recommended",
4
- "plugin:@typescript-eslint/recommended",
5
- "plugin:require-extensions/recommended",
6
- "prettier"
7
- ],
8
- parser: "@typescript-eslint/parser",
9
- plugins: ["@typescript-eslint", "require-extensions"],
10
- parserOptions: {
11
- project: "./tsconfig.json",
12
- tsconfigRootDir: __dirname,
13
- },
14
- ignorePatterns: [".eslint.cjs"],
15
- root: true,
16
- rules: {
17
- "no-unused-vars": "off",
18
- "@typescript-eslint/no-unused-vars": [
19
- "error",
20
- { argsIgnorePattern: "^_", varsIgnorePattern: "^_" },
21
- ],
22
- "@typescript-eslint/no-floating-promises": "error",
23
- },
24
- }
package/.prettierrc.js DELETED
@@ -1,9 +0,0 @@
1
- /** @type {import("prettier").Config} */
2
- const config = {
3
- trailingComma: "all",
4
- tabWidth: 4,
5
- semi: true,
6
- singleQuote: false,
7
- };
8
-
9
- export default config;