geonix 1.8.20 → 1.9.0

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": "geonix",
3
- "version": "1.8.20",
3
+ "version": "1.9.0",
4
4
  "type": "module",
5
5
  "description": "",
6
6
  "bin": {
package/src/Service.js CHANGED
@@ -32,11 +32,14 @@ export class Service {
32
32
 
33
33
  }
34
34
 
35
- // ---------------------------------------------------------------------------------------------
35
+ // ---------------------------------------------------------------------------------------------
36
36
 
37
37
  #me = {}
38
+ #options = {}
38
39
 
39
40
  async #start(options = {}) {
41
+ this.#options = options
42
+
40
43
  await webserver.waitUntilReady()
41
44
  await connection.waitUntilReady()
42
45
 
@@ -145,6 +148,11 @@ export class Service {
145
148
 
146
149
  let handlers = (Array.isArray(this[endpoint]) ? this[endpoint] : [this[endpoint]])
147
150
 
151
+ if (Array.isArray(this.#options?.handlers?.before))
152
+ handlers = [...this.#options?.handlers?.before, ...handlers]
153
+ if (Array.isArray(this.#options?.handlers?.after))
154
+ handlers = [...handlers, ...this.#options?.handlers?.after]
155
+
148
156
  for (let n = 0; n < handlers.length; n++)
149
157
  handlers[n] = handlers[n].bind(this)
150
158
 
File without changes
@@ -1,29 +1,36 @@
1
1
  import { Service, Remote, registry } from '../exports.js'
2
2
  import { sleep } from '../src/Util.js'
3
3
 
4
- class First extends Service {
5
-
6
- getTime() {
7
- return new Date().toISOString()
4
+ class BaseService extends Service {
5
+
6
+ static start(options = {}) {
7
+ super.start({
8
+ ...options,
9
+ handlers: {
10
+ before: [
11
+ (req, res, next) => {
12
+ console.log('before')
13
+ next()
14
+ }
15
+ ],
16
+ after: [(error, req, res, next) => {
17
+ if (error) {
18
+ res.send({ status: 'rawr', error: error.message })
19
+ }
20
+ }]
21
+ }
22
+ })
8
23
  }
9
24
 
10
25
  }
11
26
 
12
- class Second extends Service {
13
-
14
- first = Remote('First')
27
+ class First extends BaseService {
15
28
 
16
- async onStart() {
17
- while (true) {
18
- console.clear()
19
- console.log('Time =', await this.first.getTime())
20
- console.log('REGISTRY =', registry.getEntries())
21
-
22
- await sleep(1000)
23
- }
29
+ 'GET /wow'(req, res) {
30
+ throw Error('wow')
24
31
  }
25
32
 
26
33
  }
27
34
 
28
- First.start({ port: 30001 })
29
- Second.start({ port: 30002 })
35
+
36
+ First.start()
@@ -0,0 +1,43 @@
1
+ import { readFile } from 'fs/promises';
2
+ import { Service, Stream, getReadable } from '../exports.js';
3
+
4
+ process.env.LOCAL_PORT = 1234
5
+
6
+ class DownloadServiceDebuggingSession extends Service {
7
+
8
+ #transferCounter = 1
9
+
10
+ onStart() {
11
+ console.log("Starting service...");
12
+ }
13
+
14
+ 'GET /api/download/local' = [
15
+ async (_, res) => {
16
+ const id = this.#transferCounter++
17
+
18
+ console.log(`transfer=${id} begin`)
19
+ const result = await this.getLocalFile()
20
+ console.log(`transfer=${id} stream=${JSON.stringify(result)}`)
21
+ const stream = await getReadable(result)
22
+ console.log(`transfer=${id}`)
23
+ stream.pipe(res)
24
+
25
+ let counter = 0;
26
+ stream.on('data', (chunk) => {
27
+ counter += chunk.length
28
+ // console.log(`transfer=${id} sent=${(counter / 1024 / 1024).toFixed(2).padStart()}`)
29
+ })
30
+
31
+ stream.on('end', () => {
32
+ console.log(`transfer=${id} end`)
33
+ })
34
+ }
35
+ ]
36
+
37
+ getLocalFile = async () => {
38
+ const buffer = await readFile("./test150.txt")
39
+ return Stream(buffer)
40
+ }
41
+ }
42
+
43
+ DownloadServiceDebuggingSession.start()