epos 1.2.4 → 1.2.5

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "epos",
3
- "version": "1.2.4",
3
+ "version": "1.2.5",
4
4
  "author": "imkost",
5
5
  "description": "",
6
6
  "keywords": [],
@@ -11,18 +11,29 @@ import * as $ws from 'ws'
11
11
  const $server = {
12
12
  async init(dir = '/Users/imkost/z/epos') {
13
13
  this._dir = dir
14
- this._wsPort = 2076
15
- this._httpPort = 2077
14
+ this._port = 4322
16
15
  this._maxFiles = 10_000
17
16
  this._pkgs = {} // { [path]: { name, dir, watcher } }
18
- await this._start()
17
+ const httpServer = await this._startHttpServer()
18
+ await this._startMainWatcher()
19
+ this._wss = await this._startWebSocketServer(httpServer)
20
+ console.log('⚡ running')
19
21
  },
20
22
 
21
- async _start() {
22
- const watcherReady = Promise.withResolvers()
23
- const httpServerReady = Promise.withResolvers()
23
+ async _startWebSocketServer(httpServer) {
24
+ // TODO: handle port in use error
25
+ const wss = new $ws.WebSocketServer({ server: httpServer })
26
+ wss.on('error', e => {
27
+ console.warn('##############')
28
+ })
29
+ wss.on('open', () => {
30
+ console.warn('connection')
31
+ })
32
+ return wss
33
+ },
24
34
 
25
- const wss = new $ws.WebSocketServer({ port: this._wsPort })
35
+ async _startMainWatcher() {
36
+ const watcherReady = Promise.withResolvers()
26
37
  const watcher = $chokidar.watch(this._dir, { ignored: this._ignored })
27
38
 
28
39
  // initial scan
@@ -47,7 +58,7 @@ const $server = {
47
58
  watcher.on('add', async path => {
48
59
  const isEposManifest = variants.has($path.basename(path))
49
60
  if (!isEposManifest) return
50
- const pkg = await this._createPkgWatcher(path, wss)
61
+ const pkg = await this._createPkgWatcher(path)
51
62
  if (!pkg) return
52
63
  this._pkgs[path] = pkg
53
64
  })
@@ -59,7 +70,11 @@ const $server = {
59
70
  delete this._pkgs[path]
60
71
  })
61
72
 
62
- // static server
73
+ await watcherReady.promise
74
+ },
75
+
76
+ async _startHttpServer() {
77
+ const httpServerReady = Promise.withResolvers()
63
78
  const httpServer = $http.createServer(async (req, res) => {
64
79
  const pkgName = req.url.split('/')[1]
65
80
  const pkg = Object.values(this._pkgs).find(p => p.name === pkgName)
@@ -85,17 +100,19 @@ const $server = {
85
100
  }
86
101
  }
87
102
  })
88
- httpServer.listen(this._httpPort, () => {
103
+ httpServer.listen(this._port, () => {
89
104
  httpServerReady.resolve()
90
105
  })
91
106
 
92
- await watcherReady.promise
93
- await httpServerReady.promise
107
+ httpServer.on('error', e => {
108
+ console.warn(e)
109
+ })
94
110
 
95
- console.log('⚡ running')
111
+ await httpServerReady.promise
112
+ return httpServer
96
113
  },
97
114
 
98
- async _createPkgWatcher(manifestPath, wss) {
115
+ async _createPkgWatcher(manifestPath) {
99
116
  const isJson = manifestPath.endsWith('.json')
100
117
  const content = await $fs.readFile(manifestPath, 'utf-8')
101
118
  const manifest = isJson ? JSON.parse(content) : $yaml.load(content)
@@ -110,7 +127,7 @@ const $server = {
110
127
 
111
128
  watcher.on('all', (event, path) => {
112
129
  const data = JSON.stringify({ name: pkgName, path: $path.relative(dir, path) })
113
- for (const client of wss.clients) {
130
+ for (const client of this._wss.clients) {
114
131
  if (client.readyState !== 1) continue
115
132
  client.send(data)
116
133
  }