@logux/server 0.12.2 → 0.12.4

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.
@@ -1,4 +1,4 @@
1
- import { ServerMeta } from '../base-server/index.js'
1
+ import type { ServerMeta } from '../base-server/index.js'
2
2
 
3
3
  /**
4
4
  * Remove all non-allowed keys from meta.
@@ -1,11 +1,11 @@
1
- import stripAnsi from 'strip-ansi'
2
- import yyyymmdd from 'yyyy-mm-dd'
3
- import pico from 'picocolors'
1
+ import { once } from 'events'
4
2
  import os from 'os'
3
+ import pico from 'picocolors'
4
+ import pino from 'pino'
5
5
  import abstractTransport from 'pino-abstract-transport'
6
6
  import { Transform } from 'stream'
7
- import pino from 'pino'
8
- import { once } from 'events'
7
+ import stripAnsi from 'strip-ansi'
8
+ import yyyymmdd from 'yyyy-mm-dd'
9
9
 
10
10
  import { mulberry32, onceXmur3 } from './utils.js'
11
11
 
@@ -15,19 +15,19 @@ const SEPARATOR = os.EOL + os.EOL
15
15
  const NEXT_LINE = os.EOL === '\n' ? '\r\v' : os.EOL
16
16
 
17
17
  const PARAMS_BLACKLIST = {
18
- v: true,
19
- msg: true,
18
+ component: true,
20
19
  err: true,
21
- pid: true,
22
20
  hint: true,
23
- note: true,
24
- name: true,
25
- time: true,
21
+ hostname: true,
26
22
  level: true,
27
23
  listen: true,
24
+ msg: true,
25
+ name: true,
26
+ note: true,
27
+ pid: true,
28
28
  server: true,
29
- hostname: true,
30
- component: true
29
+ time: true,
30
+ v: true
31
31
  }
32
32
 
33
33
  const LABELS = {
package/index.d.ts CHANGED
@@ -1,35 +1,35 @@
1
- export { Action } from '@logux/core'
2
-
3
1
  export {
2
+ addSyncMap,
3
+ addSyncMapFilter,
4
+ ChangedAt,
5
+ NoConflictResolution,
6
+ SyncMapData,
7
+ WithoutTime,
8
+ WithTime
9
+ } from './add-sync-map/index.js'
10
+ export { ALLOWED_META } from './allowed-meta/index.js'
11
+ export {
12
+ BaseServer,
4
13
  BaseServerOptions,
14
+ Logger,
5
15
  SendBackActions,
6
- BaseServer,
7
16
  ServerMeta,
8
- wasNot403,
9
- Logger
17
+ wasNot403
10
18
  } from './base-server/index.js'
19
+ export { ChannelContext, Context } from './context/index.js'
20
+ export { filterMeta } from './filter-meta/index.js'
11
21
  export {
12
- ResponseError,
13
- request,
22
+ del,
23
+ get,
14
24
  patch,
15
25
  post,
16
- get,
17
26
  put,
18
- del
27
+ request,
28
+ ResponseError
19
29
  } from './request/index.js'
20
- export {
21
- NoConflictResolution,
22
- addSyncMapFilter,
23
- WithoutTime,
24
- SyncMapData,
25
- addSyncMap,
26
- ChangedAt,
27
- WithTime
28
- } from './add-sync-map/index.js'
29
- export { TestServer, TestServerOptions } from './test-server/index.js'
30
- export { TestClient, LoguxActionError } from './test-client/index.js'
31
- export { Context, ChannelContext } from './context/index.js'
32
- export { Server, ServerOptions } from './server/index.js'
33
30
  export { ServerClient } from './server-client/index.js'
34
- export { ALLOWED_META } from './allowed-meta/index.js'
35
- export { filterMeta } from './filter-meta/index.js'
31
+ export { Server, ServerOptions } from './server/index.js'
32
+ export { LoguxActionError, TestClient } from './test-client/index.js'
33
+ export { TestServer, TestServerOptions } from './test-server/index.js'
34
+
35
+ export { Action } from '@logux/core'
package/index.js CHANGED
@@ -1,22 +1,22 @@
1
1
  export {
2
- ResponseError,
3
- request,
4
- patch,
5
- post,
6
- get,
7
- put,
8
- del
9
- } from './request/index.js'
10
- export {
11
- NoConflictResolution,
12
- addSyncMapFilter,
13
2
  addSyncMap,
14
- ChangedAt
3
+ addSyncMapFilter,
4
+ ChangedAt,
5
+ NoConflictResolution
15
6
  } from './add-sync-map/index.js'
16
- export { BaseServer, wasNot403 } from './base-server/index.js'
17
7
  export { ALLOWED_META } from './allowed-meta/index.js'
18
- export { TestServer } from './test-server/index.js'
19
- export { TestClient } from './test-client/index.js'
20
- export { filterMeta } from './filter-meta/index.js'
8
+ export { BaseServer, wasNot403 } from './base-server/index.js'
21
9
  export { Context } from './context/index.js'
10
+ export { filterMeta } from './filter-meta/index.js'
11
+ export {
12
+ del,
13
+ get,
14
+ patch,
15
+ post,
16
+ put,
17
+ request,
18
+ ResponseError
19
+ } from './request/index.js'
22
20
  export { Server } from './server/index.js'
21
+ export { TestClient } from './test-client/index.js'
22
+ export { TestServer } from './test-server/index.js'
@@ -99,9 +99,9 @@ function mapArgs(parsedCliArgs, argsSpec) {
99
99
  function composeHelp(spec, argv) {
100
100
  let options = Object.entries(spec.options).map(([name, option]) => ({
101
101
  alias: option.alias ? composeCliAliasName(option.alias) : '',
102
- full: composeCliFullName(name),
102
+ description: option.description,
103
103
  env: spec.envPrefix ? composeEnvName(spec.envPrefix, name) : '',
104
- description: option.description
104
+ full: composeCliFullName(name)
105
105
  }))
106
106
  let nameColumnLength = Math.max(...options.map(it => it.full.length))
107
107
  let envColumnLength = Math.max(...options.map(it => it.env.length))
@@ -1,6 +1,7 @@
1
1
  import { resolve } from 'path'
2
+ import { describe, expect, it } from 'vitest'
2
3
 
3
- import { loadOptions, oneOf, number } from './index.js'
4
+ import { loadOptions, number, oneOf } from './index.js'
4
5
 
5
6
  function fakeProcess(argv, env = {}) {
6
7
  return { argv, env }
@@ -10,13 +11,13 @@ describe('loadOptions', () => {
10
11
  it('returns help', () => {
11
12
  let [help, options] = loadOptions(
12
13
  {
14
+ examples: ['$0'],
13
15
  options: {
14
16
  port: {
15
17
  description: 'port',
16
18
  parse: number
17
19
  }
18
- },
19
- examples: ['$0']
20
+ }
20
21
  },
21
22
  fakeProcess(['node', 'test/test.js', '--help'])
22
23
  )
@@ -43,13 +44,13 @@ describe('loadOptions', () => {
43
44
  it('uses env for options', () => {
44
45
  let [, options] = loadOptions(
45
46
  {
47
+ envPrefix: 'LOGUX',
46
48
  options: {
47
49
  port: {
48
50
  description: 'port',
49
51
  parse: number
50
52
  }
51
- },
52
- envPrefix: 'LOGUX'
53
+ }
53
54
  },
54
55
  fakeProcess([], {
55
56
  LOGUX_PORT: '31337'
@@ -63,13 +64,13 @@ describe('loadOptions', () => {
63
64
  it('uses dotenv file for options', () => {
64
65
  let [, options] = loadOptions(
65
66
  {
67
+ envPrefix: 'LOGUX',
66
68
  options: {
67
69
  port: {
68
70
  description: 'port',
69
71
  parse: number
70
72
  }
71
- },
72
- envPrefix: 'LOGUX'
73
+ }
73
74
  },
74
75
  fakeProcess([], {}),
75
76
  {
@@ -83,13 +84,13 @@ describe('loadOptions', () => {
83
84
  it('composes correct env and CLI names for argument with complex name', () => {
84
85
  let [, options] = loadOptions(
85
86
  {
87
+ envPrefix: 'LOGUX',
86
88
  options: {
87
89
  somePort: {
88
90
  description: 'port',
89
91
  parse: number
90
92
  }
91
- },
92
- envPrefix: 'LOGUX'
93
+ }
93
94
  },
94
95
  fakeProcess(['--some-port', '1'], {
95
96
  LOGUX_SOME_PORT: '1'
@@ -103,15 +104,15 @@ describe('loadOptions', () => {
103
104
  it('uses combined options', () => {
104
105
  let [, options] = loadOptions(
105
106
  {
107
+ envPrefix: 'LOGUX',
106
108
  options: {
107
- key: {
108
- description: 'port'
109
- },
110
109
  cert: {
111
110
  description: 'cert'
111
+ },
112
+ key: {
113
+ description: 'port'
112
114
  }
113
- },
114
- envPrefix: 'LOGUX'
115
+ }
115
116
  },
116
117
  fakeProcess(['', '--key', './key.pem'], { LOGUX_CERT: './cert.pem' })
117
118
  )
@@ -122,19 +123,19 @@ describe('loadOptions', () => {
122
123
 
123
124
  it('uses arg and env in given priority', () => {
124
125
  let optionsSpec = {
126
+ envPrefix: 'LOGUX',
125
127
  options: {
126
- port: {
127
- description: 'port',
128
- parse: number
128
+ cert: {
129
+ description: 'cert'
129
130
  },
130
131
  key: {
131
132
  description: 'key'
132
133
  },
133
- cert: {
134
- description: 'cert'
134
+ port: {
135
+ description: 'port',
136
+ parse: number
135
137
  }
136
- },
137
- envPrefix: 'LOGUX'
138
+ }
138
139
  }
139
140
 
140
141
  let [, options1] = loadOptions(
@@ -171,12 +172,12 @@ describe('loadOptions', () => {
171
172
  let [, options] = loadOptions(
172
173
  {
173
174
  options: {
175
+ key: {
176
+ description: 'key'
177
+ },
174
178
  port: {
175
179
  alias: 'p',
176
180
  description: 'port'
177
- },
178
- key: {
179
- description: 'key'
180
181
  }
181
182
  }
182
183
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@logux/server",
3
- "version": "0.12.2",
3
+ "version": "0.12.4",
4
4
  "description": "Build own Logux server or make proxy between WebSocket and HTTP backend on any language",
5
5
  "keywords": [
6
6
  "logux",
@@ -15,7 +15,7 @@
15
15
  ],
16
16
  "author": "Andrey Sitnik <andrey@sitnik.ru>",
17
17
  "license": "MIT",
18
- "homepage": "https://logux.io/",
18
+ "homepage": "https://logux.org/",
19
19
  "repository": "logux/server",
20
20
  "engines": {
21
21
  "node": "^14.0.0 || ^16.0.0 || >=18.0.0"
@@ -27,24 +27,24 @@
27
27
  "./package.json": "./package.json"
28
28
  },
29
29
  "dependencies": {
30
- "@logux/actions": "^0.3.0",
31
- "@logux/core": "^0.8.0",
30
+ "@logux/actions": "^0.3.1",
31
+ "@logux/core": "^0.8.4",
32
32
  "JSONStream": "^1.3.5",
33
33
  "cookie": "^0.5.0",
34
- "dotenv": "^16.0.2",
35
- "fast-glob": "^3.2.11",
34
+ "dotenv": "^16.3.1",
35
+ "fast-glob": "^3.3.1",
36
36
  "ip": "^1.1.8",
37
37
  "nanodelay": "^2.0.2",
38
- "nanoevents": "^7.0.1",
39
- "nanoid": "^4.0.0",
40
- "node-fetch": "^2.6.7",
38
+ "nanoevents": "^8.0.0",
39
+ "nanoid": "^4.0.2",
40
+ "node-fetch": "^3.3.2",
41
41
  "picocolors": "^1.0.0",
42
- "pino": "8.5.0",
42
+ "pino": "8.15.0",
43
43
  "pino-abstract-transport": "^1.0.0",
44
- "semver": "^7.3.7",
45
- "strip-ansi": "^7.0.1",
44
+ "semver": "^7.5.4",
45
+ "strip-ansi": "^7.1.0",
46
46
  "url-pattern": "^1.0.3",
47
- "ws": "^8.8.1",
47
+ "ws": "^8.13.0",
48
48
  "yyyy-mm-dd": "^1.0.2"
49
49
  }
50
50
  }
@@ -1,4 +1,5 @@
1
- import fetch, { RequestInit } from 'node-fetch'
1
+ import type { RequestInit } from 'node-fetch'
2
+ import type fetch from 'node-fetch'
2
3
 
3
4
  /**
4
5
  * Throwing this error in `accessAndProcess` or `accessAndLoad`
@@ -1,12 +1,13 @@
1
- import { jest } from '@jest/globals'
1
+ import { spy } from 'nanospy'
2
+ import { expect, it } from 'vitest'
2
3
 
3
- import { ResponseError, request, patch, post, get, put, del } from '../index.js'
4
+ import { del, get, patch, post, put, request, ResponseError } from '../index.js'
4
5
 
5
6
  it('has error', () => {
6
7
  let error = new ResponseError(
7
8
  403,
8
9
  '/a',
9
- { method: 'GET', body: 'body' },
10
+ { body: 'body', method: 'GET' },
10
11
  'answer'
11
12
  )
12
13
  expect(error.name).toEqual('ResponseError')
@@ -24,73 +25,98 @@ it('throws an error on non-2xx response', async () => {
24
25
  }
25
26
  }
26
27
  await expect(
27
- request('/a', { method: 'GET', body: 'body' }, fetch)
28
+ request('/a', { body: 'body', method: 'GET' }, fetch)
28
29
  ).rejects.toThrow(
29
- new ResponseError(403, '/a', { method: 'GET', body: 'body' }, 'answer')
30
+ new ResponseError(403, '/a', { body: 'body', method: 'GET' }, 'answer')
30
31
  )
31
32
  })
32
33
 
33
34
  it('parses JSON body', async () => {
34
35
  async function fetch() {
35
36
  return {
36
- status: 200,
37
37
  async json() {
38
38
  return { answer: '1' }
39
- }
39
+ },
40
+ status: 200
40
41
  }
41
42
  }
42
43
  expect(await request('/a', { method: 'GET' }, fetch)).toEqual({ answer: '1' })
43
44
  })
44
45
 
45
46
  it('has shortcut for GET', async () => {
46
- let fetch = jest.fn(async () => ({
47
- status: 200,
47
+ let fetch = spy(async () => ({
48
48
  async json() {
49
49
  return { answer: '1' }
50
- }
50
+ },
51
+ status: 200
51
52
  }))
52
53
 
53
54
  expect(await get('/a', { headers: { a: 1 } }, fetch)).toEqual({ answer: '1' })
54
- expect(fetch).toHaveBeenCalledWith('/a', {
55
- method: 'GET',
56
- headers: { a: 1 }
57
- })
55
+ expect(fetch.calls).toEqual([
56
+ [
57
+ '/a',
58
+ {
59
+ headers: { a: 1 },
60
+ method: 'GET'
61
+ }
62
+ ]
63
+ ])
58
64
  })
59
65
 
60
66
  it('has shortcut for POST', async () => {
61
- let fetch = jest.fn(async () => ({ status: 204 }))
67
+ let fetch = spy(async () => ({ status: 204 }))
62
68
  await post('/a', { headers: { a: 1 } }, fetch)
63
- expect(fetch).toHaveBeenCalledWith('/a', {
64
- method: 'POST',
65
- headers: { a: 1 }
66
- })
69
+ expect(fetch.calls).toEqual([
70
+ [
71
+ '/a',
72
+ {
73
+ headers: { a: 1 },
74
+ method: 'POST'
75
+ }
76
+ ]
77
+ ])
67
78
  })
68
79
 
69
80
  it('has shortcut for PUT', async () => {
70
- let fetch = jest.fn(async () => ({ status: 204 }))
81
+ let fetch = spy(async () => ({ status: 204 }))
71
82
  await put('/a', { headers: { a: 1 } }, fetch)
72
- expect(fetch).toHaveBeenCalledWith('/a', {
73
- method: 'PUT',
74
- headers: { a: 1 }
75
- })
83
+ expect(fetch.calls).toEqual([
84
+ [
85
+ '/a',
86
+ {
87
+ headers: { a: 1 },
88
+ method: 'PUT'
89
+ }
90
+ ]
91
+ ])
76
92
  })
77
93
 
78
94
  it('has shortcut for PATCH', async () => {
79
- let fetch = jest.fn(async () => ({ status: 204 }))
95
+ let fetch = spy(async () => ({ status: 204 }))
80
96
  await patch('/a', { headers: { a: 1 } }, fetch)
81
- expect(fetch).toHaveBeenCalledWith('/a', {
82
- method: 'PATCH',
83
- headers: { a: 1 }
84
- })
97
+ expect(fetch.calls).toEqual([
98
+ [
99
+ '/a',
100
+ {
101
+ headers: { a: 1 },
102
+ method: 'PATCH'
103
+ }
104
+ ]
105
+ ])
85
106
  })
86
107
 
87
108
  it('has shortcut for DELETE', async () => {
88
- let fetch = jest.fn(async () => ({ status: 204 }))
109
+ let fetch = spy(async () => ({ status: 204 }))
89
110
  await del('/a', { headers: { a: 1 } }, fetch)
90
- expect(fetch).toHaveBeenCalledWith('/a', {
91
- method: 'DELETE',
92
- headers: { a: 1 }
93
- })
111
+ expect(fetch.calls).toEqual([
112
+ [
113
+ '/a',
114
+ {
115
+ headers: { a: 1 },
116
+ method: 'DELETE'
117
+ }
118
+ ]
119
+ ])
94
120
  })
95
121
 
96
122
  it('works without option object', async () => {
package/server/index.d.ts CHANGED
@@ -1,22 +1,26 @@
1
- import { BaseServer, Logger, BaseServerOptions } from '../base-server/index.js'
1
+ import {
2
+ BaseServer,
3
+ type BaseServerOptions,
4
+ type Logger
5
+ } from '../base-server/index.js'
2
6
 
3
7
  export interface LoggerOptions {
4
- /**
5
- * Logger message format.
6
- */
7
- type?: 'human' | 'json'
8
-
9
8
  /**
10
9
  * Stream to be used by logger to write log.
11
10
  */
12
11
  stream?: {
13
- write(str: string): void
14
-
15
12
  /**
16
13
  * Used by pino to synchronously write log messages on application failure.
17
14
  */
18
15
  flushSync?(): void
16
+
17
+ write(str: string): void
19
18
  }
19
+
20
+ /**
21
+ * Logger message format.
22
+ */
23
+ type?: 'human' | 'json'
20
24
  }
21
25
 
22
26
  export interface ServerOptions extends BaseServerOptions {
@@ -73,6 +77,20 @@ export interface ServerOptions extends BaseServerOptions {
73
77
  * ```
74
78
  */
75
79
  export class Server<Headers extends object = {}> extends BaseServer<Headers> {
80
+ /**
81
+ * Server options.
82
+ *
83
+ * ```js
84
+ * console.log('Server options', server.options.subprotocol)
85
+ * ```
86
+ */
87
+ options: ServerOptions
88
+
89
+ /**
90
+ * @param opts Server options.
91
+ */
92
+ constructor(opts: ServerOptions)
93
+
76
94
  /**
77
95
  * Load options from command-line arguments and/or environment.
78
96
  *
@@ -95,20 +113,6 @@ export class Server<Headers extends object = {}> extends BaseServer<Headers> {
95
113
  defaults: ServerOptions
96
114
  ): ServerOptions
97
115
 
98
- /**
99
- * @param opts Server options.
100
- */
101
- constructor(opts: ServerOptions)
102
-
103
- /**
104
- * Server options.
105
- *
106
- * ```js
107
- * console.log('Server options', server.options.subprotocol)
108
- * ```
109
- */
110
- options: ServerOptions
111
-
112
116
  /**
113
117
  * Load module creators and apply to the server. By default, it will load
114
118
  * files from `modules/*`.
@@ -119,5 +123,5 @@ export class Server<Headers extends object = {}> extends BaseServer<Headers> {
119
123
  *
120
124
  * @param files Pattern for module files.
121
125
  */
122
- autoloadModules(files?: string[] | string): Promise<void>
126
+ autoloadModules(files?: string | string[]): Promise<void>
123
127
  }