@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,6 +1,6 @@
1
1
  import { ClientNode, TestPair } from '@logux/core'
2
- import { delay } from 'nanodelay'
3
2
  import cookie from 'cookie'
3
+ import { delay } from 'nanodelay'
4
4
 
5
5
  import { filterMeta } from '../filter-meta/index.js'
6
6
 
@@ -43,6 +43,18 @@ export class TestClient {
43
43
  })
44
44
  }
45
45
 
46
+ async collect(test) {
47
+ let added = []
48
+ let unbind = this.node.log.on('add', (action, meta) => {
49
+ if (!meta.id.includes(` ${this.nodeId} `)) {
50
+ added.push(action)
51
+ }
52
+ })
53
+ await test()
54
+ unbind()
55
+ return added
56
+ }
57
+
46
58
  connect() {
47
59
  return new Promise((resolve, reject) => {
48
60
  this.node.throwsError = false
@@ -69,18 +81,6 @@ export class TestClient {
69
81
  return this.pair.wait('right')
70
82
  }
71
83
 
72
- async collect(test) {
73
- let added = []
74
- let unbind = this.node.log.on('add', (action, meta) => {
75
- if (!meta.id.includes(` ${this.nodeId} `)) {
76
- added.push(action)
77
- }
78
- })
79
- await test()
80
- unbind()
81
- return added
82
- }
83
-
84
84
  process(action, meta) {
85
85
  return this.collect(async () => {
86
86
  return new Promise((resolve, reject) => {
@@ -133,10 +133,23 @@ export class TestClient {
133
133
  })
134
134
  }
135
135
 
136
+ async received(test) {
137
+ let actions = []
138
+ let unbind = this.log.on('add', (action, meta) => {
139
+ if (!meta.id.includes(` ${this.nodeId} `)) {
140
+ actions.push(action)
141
+ }
142
+ })
143
+ await test()
144
+ await delay(1)
145
+ unbind()
146
+ return actions
147
+ }
148
+
136
149
  async subscribe(channel, filter, since) {
137
150
  let action = channel
138
151
  if (typeof channel === 'string') {
139
- action = { type: 'logux/subscribe', channel }
152
+ action = { channel, type: 'logux/subscribe' }
140
153
  }
141
154
  if (filter) {
142
155
  action.filter = filter
@@ -151,24 +164,11 @@ export class TestClient {
151
164
  unsubscribe(channel, filter) {
152
165
  let action = channel
153
166
  if (typeof channel === 'string') {
154
- action = { type: 'logux/unsubscribe', channel }
167
+ action = { channel, type: 'logux/unsubscribe' }
155
168
  }
156
169
  if (filter) {
157
170
  action.filter = filter
158
171
  }
159
172
  return this.process(action)
160
173
  }
161
-
162
- async received(test) {
163
- let actions = []
164
- let unbind = this.log.on('add', (action, meta) => {
165
- if (!meta.id.includes(` ${this.nodeId} `)) {
166
- actions.push(action)
167
- }
168
- })
169
- await test()
170
- await delay(1)
171
- unbind()
172
- return actions
173
- }
174
174
  }
@@ -1,19 +1,16 @@
1
- import { TestTime, TestLog } from '@logux/core'
1
+ import type { TestLog, TestTime } from '@logux/core'
2
2
 
3
- import { TestClient, TestClientOptions } from '../test-client/index.js'
4
- import {
3
+ import { BaseServer } from '../base-server/index.js'
4
+ import type {
5
5
  BaseServerOptions,
6
- BaseServer,
7
- ServerMeta,
8
- Logger
6
+ Logger,
7
+ ServerMeta
9
8
  } from '../base-server/index.js'
10
- import { LoggerOptions } from '../server/index.js'
9
+ import type { LoggerOptions } from '../server/index.js'
10
+ import type { TestClient, TestClientOptions } from '../test-client/index.js'
11
11
 
12
12
  export interface TestServerOptions
13
13
  extends Omit<BaseServerOptions, 'subprotocol' | 'supports'> {
14
- subprotocol?: string
15
- supports?: string
16
-
17
14
  /**
18
15
  * Disable built-in auth.
19
16
  */
@@ -23,6 +20,10 @@ export interface TestServerOptions
23
20
  * Logger with custom settings.
24
21
  */
25
22
  logger?: Logger | LoggerOptions
23
+
24
+ subprotocol?: string
25
+
26
+ supports?: string
26
27
  }
27
28
 
28
29
  /**
@@ -48,9 +49,13 @@ export class TestServer<
48
49
  Headers extends object = {}
49
50
  > extends BaseServer<Headers> {
50
51
  /**
51
- * @param opts The limit subset of server options.
52
+ * Server actions log, with methods to check actions inside.
53
+ *
54
+ * ```js
55
+ * server.log.actions() //=> […]
56
+ * ```
52
57
  */
53
- constructor(opts?: TestServerOptions)
58
+ log: TestLog<ServerMeta>
54
59
 
55
60
  /**
56
61
  * Time replacement without variable parts like current timestamp.
@@ -58,13 +63,9 @@ export class TestServer<
58
63
  time: TestTime
59
64
 
60
65
  /**
61
- * Server actions log, with methods to check actions inside.
62
- *
63
- * ```js
64
- * server.log.actions() //=> […]
65
- * ```
66
+ * @param opts The limit subset of server options.
66
67
  */
67
- log: TestLog<ServerMeta>
68
+ constructor(opts?: TestServerOptions)
68
69
 
69
70
  /**
70
71
  * Create and connect client.
@@ -80,24 +81,6 @@ export class TestServer<
80
81
  */
81
82
  connect(userId: string, opts?: TestClientOptions): Promise<TestClient>
82
83
 
83
- /**
84
- * Try to connect client and throw an error is client didn’t received
85
- * `Wrong Cregentials` message from the server.
86
- *
87
- * ```js
88
- * server = new TestServer()
89
- * await server.expectWrongCredentials('10')
90
- * ```
91
- *
92
- * @param userId User ID.
93
- * @param opts Other options.
94
- * @returns Promise until check.
95
- */
96
- expectWrongCredentials(
97
- userId: string,
98
- opts?: TestClientOptions
99
- ): Promise<void>
100
-
101
84
  /**
102
85
  * Call callback and throw an error if there was no `Action was denied`
103
86
  * during callback.
@@ -112,6 +95,18 @@ export class TestServer<
112
95
  */
113
96
  expectDenied(test: () => any | Promise<any>): Promise<void>
114
97
 
98
+ /**
99
+ * Call callback and throw an error if there was no error during
100
+ * server processing.
101
+ *
102
+ * @param text RegExp or string of error message.
103
+ * @param test Callback with subscripting or action sending.
104
+ */
105
+ expectError(
106
+ text: RegExp | string,
107
+ test: () => any | Promise<any>
108
+ ): Promise<void>
109
+
115
110
  /**
116
111
  * Call callback and throw an error if there was no `logux/undo` in return
117
112
  * with specific reason.
@@ -128,14 +123,20 @@ export class TestServer<
128
123
  expectUndo(reason: string, test: () => any | Promise<any>): Promise<void>
129
124
 
130
125
  /**
131
- * Call callback and throw an error if there was no error during
132
- * server processing.
126
+ * Try to connect client and throw an error is client didn’t received
127
+ * `Wrong Cregentials` message from the server.
133
128
  *
134
- * @param text RegExp or string of error message.
135
- * @param test Callback with subscripting or action sending.
129
+ * ```js
130
+ * server = new TestServer()
131
+ * await server.expectWrongCredentials('10')
132
+ * ```
133
+ *
134
+ * @param userId User ID.
135
+ * @param opts Other options.
136
+ * @returns Promise until check.
136
137
  */
137
- expectError(
138
- text: string | RegExp,
139
- test: () => any | Promise<any>
138
+ expectWrongCredentials(
139
+ userId: string,
140
+ opts?: TestClientOptions
140
141
  ): Promise<void>
141
142
  }
@@ -1,7 +1,7 @@
1
1
  import { TestTime } from '@logux/core'
2
2
 
3
- import { createReporter } from '../create-reporter/index.js'
4
3
  import { BaseServer } from '../base-server/index.js'
4
+ import { createReporter } from '../create-reporter/index.js'
5
5
  import { TestClient } from '../test-client/index.js'
6
6
 
7
7
  export class TestServer extends BaseServer {
@@ -12,20 +12,20 @@ export class TestServer extends BaseServer {
12
12
 
13
13
  opts.time.lastId += 1
14
14
  super({
15
+ id: `${opts.time.lastId}`,
15
16
  subprotocol: '0.0.0',
16
17
  supports: '0.0.0',
17
- id: `${opts.time.lastId}`,
18
18
  ...opts
19
19
  })
20
20
  if (opts.logger) {
21
21
  this.on('report', createReporter(opts))
22
22
  } else {
23
23
  this.logger = {
24
- fatal: () => {},
24
+ debug: () => {},
25
25
  error: () => {},
26
- warn: () => {},
26
+ fatal: () => {},
27
27
  info: () => {},
28
- debug: () => {}
28
+ warn: () => {}
29
29
  }
30
30
  }
31
31
  if (opts.auth !== false) this.auth(() => true)
@@ -42,21 +42,24 @@ export class TestServer extends BaseServer {
42
42
  return client
43
43
  }
44
44
 
45
- async expectWrongCredentials(userId, opts = {}) {
45
+ async expectDenied(test) {
46
+ await this.expectUndo('denied', test)
47
+ }
48
+
49
+ async expectError(text, test) {
46
50
  try {
47
- await this.connect(userId, opts)
48
- throw new Error('Credentials passed')
51
+ await test()
52
+ throw new Error('Actions passed without error')
49
53
  } catch (e) {
50
- if (e.message !== 'Wrong credentials') {
54
+ if (
55
+ (typeof text === 'string' && e.message !== text) ||
56
+ (text instanceof RegExp && !text.test(e.message))
57
+ ) {
51
58
  throw e
52
59
  }
53
60
  }
54
61
  }
55
62
 
56
- async expectDenied(test) {
57
- await this.expectUndo('denied', test)
58
- }
59
-
60
63
  async expectUndo(reason, test) {
61
64
  try {
62
65
  await test()
@@ -75,15 +78,12 @@ export class TestServer extends BaseServer {
75
78
  }
76
79
  }
77
80
 
78
- async expectError(text, test) {
81
+ async expectWrongCredentials(userId, opts = {}) {
79
82
  try {
80
- await test()
81
- throw new Error('Actions passed without error')
83
+ await this.connect(userId, opts)
84
+ throw new Error('Credentials passed')
82
85
  } catch (e) {
83
- if (
84
- (typeof text === 'string' && e.message !== text) ||
85
- (text instanceof RegExp && !text.test(e.message))
86
- ) {
86
+ if (e.message !== 'Wrong credentials') {
87
87
  throw e
88
88
  }
89
89
  }
package/vite.config.ts ADDED
@@ -0,0 +1,13 @@
1
+ import { defineConfig } from 'vitest/config'
2
+
3
+ export default defineConfig({
4
+ test: {
5
+ coverage: {
6
+ exclude: ['node_modules', 'server/index.js', '*/*.test.*'],
7
+ lines: 100,
8
+ provider: 'v8'
9
+ },
10
+ environment: 'node',
11
+ exclude: ['node_modules', 'test/servers']
12
+ }
13
+ })