@logux/server 0.10.4 → 0.10.8

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.
@@ -327,7 +327,7 @@ interface ChannelFilter<Headers extends object> {
327
327
  ctx: Context<{}, Headers>,
328
328
  action: Readonly<Action>,
329
329
  meta: Readonly<ServerMeta>
330
- ): boolean
330
+ ): boolean | Promise<boolean>
331
331
  }
332
332
 
333
333
  /**
@@ -597,7 +597,7 @@ interface GetProcessor {
597
597
  }
598
598
 
599
599
  interface PostProcessor {
600
- isValid(command: object): false
600
+ isValid(command: object): boolean
601
601
  command(command: object, request: object): Promise<void>
602
602
  }
603
603
 
@@ -1140,7 +1140,7 @@ export class BaseServer<
1140
1140
  * @param action New action.
1141
1141
  * @param meta Action’s metadata.
1142
1142
  */
1143
- sendAction(action: Action, meta: ServerMeta): void
1143
+ sendAction(action: Action, meta: ServerMeta): void | Promise<void>
1144
1144
 
1145
1145
  /**
1146
1146
  * Send `logux/subscribed` if client was not already subscribed.
@@ -1,12 +1,12 @@
1
1
  import { ServerConnection, MemoryStore, Log, parseId } from '@logux/core'
2
2
  import { LoguxNotFoundError } from '@logux/actions'
3
3
  import { createNanoEvents } from 'nanoevents'
4
+ import { WebSocketServer } from 'ws'
4
5
  import { promises as fs } from 'fs'
5
6
  import { fileURLToPath } from 'url'
6
7
  import { join, dirname } from 'path'
7
8
  import UrlPattern from 'url-pattern'
8
9
  import { nanoid } from 'nanoid'
9
- import WebSocket from 'ws'
10
10
 
11
11
  import { bindControlServer } from '../bind-control-server/index.js'
12
12
  import { bindBackendProxy } from '../bind-backend-proxy/index.js'
@@ -378,12 +378,12 @@ export class BaseServer {
378
378
  if (!this.authenticator) {
379
379
  throw new Error('You must set authentication callback by server.auth()')
380
380
  }
381
- this.http = await createHttpServer(this.options)
382
- this.ws = new WebSocket.Server({ server: this.http })
381
+ this.httpServer = await createHttpServer(this.options)
382
+ this.ws = new WebSocketServer({ server: this.httpServer })
383
383
  if (!this.options.server) {
384
384
  await new Promise((resolve, reject) => {
385
385
  this.ws.on('error', reject)
386
- this.http.listen(this.options.port, this.options.host, resolve)
386
+ this.httpServer.listen(this.options.port, this.options.host, resolve)
387
387
  })
388
388
  }
389
389
  bindControlServer(this, this.httpListener)
@@ -395,12 +395,12 @@ export class BaseServer {
395
395
  this.ws.close()
396
396
  })
397
397
  )
398
- if (this.http) {
398
+ if (this.httpServer) {
399
399
  this.unbind.push(
400
400
  () =>
401
401
  new Promise(resolve => {
402
- this.http.on('close', resolve)
403
- this.http.close()
402
+ this.httpServer.on('close', resolve)
403
+ this.httpServer.close()
404
404
  })
405
405
  )
406
406
  }
@@ -543,7 +543,7 @@ export class BaseServer {
543
543
  }
544
544
  }
545
545
 
546
- sendAction(action, meta) {
546
+ async sendAction(action, meta) {
547
547
  let from = parseId(meta.id).clientId
548
548
  let ignoreClients = new Set(meta.excludeClients || [])
549
549
  ignoreClients.add(from)
@@ -594,7 +594,7 @@ export class BaseServer {
594
594
  let filter = subscriber.filter
595
595
  if (typeof filter === 'function') {
596
596
  if (!ctx) ctx = this.createContext(action, meta)
597
- filter = filter(ctx, action, meta)
597
+ filter = await filter(ctx, action, meta)
598
598
  }
599
599
  let client = this.clientIds.get(clientId)
600
600
  if (filter && client) {
@@ -18,7 +18,7 @@ function isValidBody(body) {
18
18
 
19
19
  export function bindControlServer(app, custom) {
20
20
  let masks = app.options.controlMask.split(/,\s*/).map(i => ip.cidrSubnet(i))
21
- app.http.on('request', async (req, res) => {
21
+ app.httpServer.on('request', async (req, res) => {
22
22
  let urlString = req.url
23
23
  if (/^\/\w+%3F/.test(urlString)) urlString = decodeURIComponent(urlString)
24
24
  let reqUrl = new URL(urlString, 'http://localhost')
@@ -1,6 +1,6 @@
1
- import { createColors } from 'nanocolors'
2
1
  import stripAnsi from 'strip-ansi'
3
2
  import yyyymmdd from 'yyyy-mm-dd'
3
+ import pico from 'picocolors'
4
4
  import path from 'path'
5
5
  import os from 'os'
6
6
 
@@ -170,7 +170,7 @@ function prettyStackTrace(c, stack, basepath) {
170
170
  }
171
171
 
172
172
  export function humanFormatter(options) {
173
- let c = createColors(options.color)
173
+ let c = pico.createColors(options.color)
174
174
  this.color = c.isColorSupported
175
175
  let basepath = options.basepath || process.cwd()
176
176
  if (basepath.slice(-1) !== path.sep) basepath += path.sep
@@ -1,5 +1,5 @@
1
- import { bold, cyan, yellow, green, red } from 'nanocolors'
2
1
  import dotenv from 'dotenv'
2
+ import pico from 'picocolors'
3
3
 
4
4
  export function loadOptions(spec, process, env) {
5
5
  let rawCliArgs = gatherCliArgs(process.argv)
@@ -72,7 +72,9 @@ function parseValues(spec, args) {
72
72
  parsed[key] = parsingResult[1]
73
73
  } else {
74
74
  throw Error(
75
- `Failed to parse ${bold(key)} argument value. \n${parsingResult[0]}`
75
+ `Failed to parse ${pico.bold(key)} argument value. \n${
76
+ parsingResult[0]
77
+ }`
76
78
  )
77
79
  }
78
80
  }
@@ -104,8 +106,8 @@ function composeHelp(spec, argv) {
104
106
  let nameColumnLength = Math.max(...options.map(it => it.full.length))
105
107
  let envColumnLength = Math.max(...options.map(it => it.env.length))
106
108
 
107
- let composeName = name => yellow(name.padEnd(nameColumnLength + 2))
108
- let composeEnv = env => cyan(env.padEnd(envColumnLength + 2))
109
+ let composeName = name => pico.yellow(name.padEnd(nameColumnLength + 2))
110
+ let composeEnv = env => pico.cyan(env.padEnd(envColumnLength + 2))
109
111
  let composeOptionHelp = option => {
110
112
  return (
111
113
  composeName(option.full) + composeEnv(option.env) + option.description
@@ -118,7 +120,7 @@ function composeHelp(spec, argv) {
118
120
  let lastPart = pathParts[pathParts.length - 1]
119
121
  examples = [
120
122
  '',
121
- bold('Examples:'),
123
+ pico.bold('Examples:'),
122
124
  ...spec.examples.map(i => i.replace('$0', lastPart))
123
125
  ]
124
126
  }
@@ -126,7 +128,7 @@ function composeHelp(spec, argv) {
126
128
  return [
127
129
  'Start Logux Server',
128
130
  '',
129
- bold('Options:'),
131
+ pico.bold('Options:'),
130
132
  ...options.map(option => composeOptionHelp(option)),
131
133
  ...examples
132
134
  ].join('\n')
@@ -153,10 +155,9 @@ function toKebabCase(word) {
153
155
 
154
156
  export function oneOf(options, rawValue) {
155
157
  if (!options.includes(rawValue)) {
158
+ let opt = JSON.stringify(options)
156
159
  return [
157
- `Expected ${green('one of ' + JSON.stringify(options))}, got ${red(
158
- rawValue
159
- )}`,
160
+ `Expected ${pico.green('one of ' + opt)}, got ${pico.red(rawValue)}`,
160
161
  null
161
162
  ]
162
163
  } else {
@@ -167,7 +168,7 @@ export function oneOf(options, rawValue) {
167
168
  export function number(rawValue) {
168
169
  let parsed = Number.parseInt(rawValue, 10)
169
170
  if (Number.isNaN(parsed)) {
170
- return [`Expected ${green('number')}, got ${red(rawValue)}`, null]
171
+ return [`Expected ${pico.green('number')}, got ${pico.red(rawValue)}`, null]
171
172
  } else {
172
173
  return [null, parsed]
173
174
  }
@@ -175,7 +176,7 @@ export function number(rawValue) {
175
176
 
176
177
  export function string(rawValue) {
177
178
  if (typeof rawValue !== 'string') {
178
- return [`Expected ${green('string')}, got ${red(rawValue)}`, null]
179
+ return [`Expected ${pico.green('string')}, got ${pico.red(rawValue)}`, null]
179
180
  } else {
180
181
  return [null, rawValue]
181
182
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@logux/server",
3
- "version": "0.10.4",
3
+ "version": "0.10.8",
4
4
  "description": "Build own Logux server or make proxy between WebSocket and HTTP backend on any language",
5
5
  "keywords": [
6
6
  "logux",
@@ -29,22 +29,21 @@
29
29
  "dependencies": {
30
30
  "@logux/actions": "^0.2.1",
31
31
  "@logux/core": "^0.7.2",
32
- "@types/node-fetch": "^2.5.10",
33
32
  "JSONStream": "^1.3.5",
34
33
  "cookie": "^0.4.1",
35
34
  "dotenv": "^10.0.0",
36
- "globby": "^11.0.4",
35
+ "fast-glob": "^3.2.7",
37
36
  "ip": "^1.1.5",
38
- "nanocolors": "^0.2.10",
39
- "nanodelay": "^2.0.1",
40
- "nanoevents": "^6.0.1",
41
- "nanoid": "^3.1.28",
37
+ "nanodelay": "^2.0.2",
38
+ "nanoevents": "^6.0.2",
39
+ "nanoid": "^3.1.30",
42
40
  "node-fetch": "^2.6.1",
41
+ "picocolors": "^1.0.0",
43
42
  "pino": "^6.13.3",
44
43
  "semver": "^7.3.5",
45
44
  "strip-ansi": "^7.0.1",
46
45
  "url-pattern": "^1.0.3",
47
- "ws": "^7.5.0",
46
+ "ws": "^8.3.0",
48
47
  "yyyy-mm-dd": "^1.0.2"
49
48
  }
50
49
  }
package/server/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { join, relative } from 'path'
2
- import globby from 'globby'
3
- import { bgRed, black } from 'nanocolors'
2
+ import pico from 'picocolors'
3
+ import glob from 'fast-glob'
4
4
 
5
5
  import { createReporter } from '../create-reporter/index.js'
6
6
  import { BaseServer } from '../base-server/index.js'
@@ -63,14 +63,15 @@ export class Server extends BaseServer {
63
63
  )
64
64
  if (help) {
65
65
  process.stdout.write(help + '\n')
66
- process.exit(0)
67
- } else {
68
- try {
69
- return Object.assign(defaults, options)
70
- } catch (e) {
71
- process.stderr.write(`${bgRed(black(' FATAL '))} ${e.message}\n`)
72
- process.exit(1)
73
- }
66
+ return process.exit(0)
67
+ }
68
+ try {
69
+ return Object.assign(defaults, options)
70
+ } catch (e) {
71
+ process.stderr.write(
72
+ `${pico.bgRed(pico.black(' FATAL '))} ${e.message}\n`
73
+ )
74
+ return process.exit(1)
74
75
  }
75
76
  }
76
77
 
@@ -133,7 +134,7 @@ export class Server extends BaseServer {
133
134
  async autoloadModules(
134
135
  files = ['modules/*/index.js', 'modules/*.js', '!**/*.{test,spec}.js']
135
136
  ) {
136
- let matches = await globby(files, {
137
+ let matches = await glob(files, {
137
138
  cwd: this.options.root,
138
139
  absolute: true,
139
140
  onlyFiles: true
@@ -30,6 +30,10 @@ export class TestServer extends BaseServer {
30
30
  }
31
31
  if (opts.auth !== false) this.auth(() => true)
32
32
  this.testUsers = {}
33
+
34
+ this.on('fatal', () => {
35
+ this.destroy()
36
+ })
33
37
  }
34
38
 
35
39
  async connect(userId, opts = {}) {