jazz-run 0.8.15 → 0.8.16
Sign up to get free protection for your applications and to get access to all the features.
- package/CHANGELOG.md +389 -378
- package/dist/createWorkerAccount.js +39 -17
- package/dist/createWorkerAccount.js.map +1 -1
- package/dist/index.js.map +1 -1
- package/dist/startSyncServer.js +4 -4
- package/dist/startSyncServer.js.map +1 -1
- package/dist/test/createWorkerAccount.test.js +17 -7
- package/dist/test/createWorkerAccount.test.js.map +1 -1
- package/package.json +9 -12
- package/src/createWorkerAccount.ts +126 -95
- package/src/index.ts +42 -46
- package/src/startSyncServer.ts +75 -77
- package/src/test/createWorkerAccount.test.ts +30 -20
- package/tsconfig.json +1 -1
- package/.eslintrc.cjs +0 -24
- package/.prettierrc.js +0 -9
package/src/startSyncServer.ts
CHANGED
@@ -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
|
-
|
12
|
-
|
13
|
-
|
11
|
+
port,
|
12
|
+
inMemory,
|
13
|
+
db,
|
14
14
|
}: {
|
15
|
-
|
16
|
-
|
17
|
-
|
15
|
+
port: string | undefined;
|
16
|
+
inMemory: boolean;
|
17
|
+
db: string;
|
18
18
|
}) => {
|
19
|
-
|
19
|
+
const crypto = await WasmCrypto.create();
|
20
20
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
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
|
33
|
-
|
34
|
-
|
35
|
-
|
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
|
-
|
39
|
-
|
79
|
+
ws.on("error", (e) => console.error(`Error on connection ${clientId}:`, e));
|
80
|
+
});
|
40
81
|
|
41
|
-
|
42
|
-
|
43
|
-
|
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
|
-
|
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
|
-
|
92
|
+
return server;
|
95
93
|
};
|
@@ -1,32 +1,42 @@
|
|
1
|
-
import { describe,
|
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
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
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
|
-
|
14
|
+
onTestFinished(() => {
|
15
|
+
server.close();
|
16
|
+
});
|
15
17
|
|
16
|
-
|
17
|
-
throw new Error('Server address is not an object');
|
18
|
-
}
|
18
|
+
const address = server.address();
|
19
19
|
|
20
|
-
|
20
|
+
if (typeof address !== "object" || address === null) {
|
21
|
+
throw new Error("Server address is not an object");
|
22
|
+
}
|
21
23
|
|
22
|
-
|
23
|
-
|
24
|
+
const { accountId, agentSecret } = await createWorkerAccount({
|
25
|
+
name: "test",
|
26
|
+
peer: `ws://localhost:${address.port}`,
|
24
27
|
});
|
25
28
|
|
26
|
-
|
27
|
-
|
29
|
+
expect(accountId).toBeDefined();
|
30
|
+
expect(agentSecret).toBeDefined();
|
31
|
+
});
|
28
32
|
|
29
|
-
|
30
|
-
|
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
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
|
-
}
|