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