forked from automerge/automerge-repo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
81 lines (65 loc) · 2.1 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// @ts-check
import fs from "fs"
import express from "express"
import { WebSocketServer } from "ws"
import { Repo } from "@automerge/automerge-repo"
import { NodeWSServerAdapter } from "@automerge/automerge-repo-network-websocket"
import { NodeFSStorageAdapter } from "@automerge/automerge-repo-storage-nodefs"
import os from "os"
export class Server {
/** @type WebSocketServer */
#socket
/** @type ReturnType<import("express").Express["listen"]> */
#server
/** @type {((value: any) => void)[]} */
#readyResolvers = []
#isReady = false
constructor() {
const dir = "automerge-sync-server-data"
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir)
}
var hostname = os.hostname()
this.#socket = new WebSocketServer({ noServer: true })
const PORT =
process.env.PORT !== undefined ? parseInt(process.env.PORT) : 3030
const app = express()
app.use(express.static("public"))
const config = {
network: [new NodeWSServerAdapter(this.#socket)],
storage: new NodeFSStorageAdapter(dir),
/** @ts-ignore @type {(import("@automerge/automerge-repo").PeerId)} */
peerId: `storage-server-${hostname}`,
// Since this is a server, we don't share generously — meaning we only sync documents they already
// know about and can ask for by ID.
sharePolicy: async () => false,
}
const serverRepo = new Repo(config)
app.get("/", (req, res) => {
res.send(`👍 @automerge/example-sync-server is running`)
})
this.#server = app.listen(PORT, () => {
console.log(`Listening on port ${PORT}`)
this.#isReady = true
this.#readyResolvers.forEach((resolve) => resolve(true))
})
this.#server.on("upgrade", (request, socket, head) => {
this.#socket.handleUpgrade(request, socket, head, (socket) => {
this.#socket.emit("connection", socket, request)
})
})
}
async ready() {
if (this.#isReady) {
return true
}
return new Promise((resolve) => {
this.#readyResolvers.push(resolve)
})
}
close() {
this.#socket.close()
this.#server.close()
}
}
new Server()