@logux/server 0.14.0 → 0.16.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.
@@ -1,5 +1,5 @@
1
1
  import { ClientNode, TestPair } from '@logux/core'
2
- import cookie from 'cookie'
2
+ import { stringifyCookie } from 'cookie'
3
3
  import { setTimeout } from 'node:timers/promises'
4
4
 
5
5
  import { filterMeta } from '../filter-meta/index.js'
@@ -34,9 +34,9 @@ export class TestClient {
34
34
  this.node.setLocalHeaders(opts.headers)
35
35
  }
36
36
  if (opts.cookie) {
37
- this.pair.right.ws.upgradeReq.headers.cookie = Object.keys(opts.cookie)
38
- .map(i => cookie.serialize(i, opts.cookie[i]))
39
- .join('; ')
37
+ this.pair.right.ws.upgradeReq.headers.cookie = stringifyCookie(
38
+ opts.cookie
39
+ )
40
40
  }
41
41
  server.unbind.push(() => {
42
42
  this.node.destroy()
@@ -46,7 +46,7 @@ export class TestClient {
46
46
  async collect(test) {
47
47
  let added = []
48
48
  let unbind = this.node.log.on('add', (action, meta) => {
49
- if (!meta.id.includes(` ${this.nodeId} `)) {
49
+ if (meta.id.split(' ')[1] !== this.nodeId) {
50
50
  added.push(action)
51
51
  }
52
52
  })
@@ -67,8 +67,8 @@ export class TestClient {
67
67
  })
68
68
 
69
69
  this.server.addClient(this.pair.right)
70
- this.node.connection.connect()
71
- this.node.waitFor('synchronized').then(() => {
70
+ void this.node.connection.connect()
71
+ void this.node.waitFor('synchronized').then(() => {
72
72
  this.node.throwsError = true
73
73
  unbind()
74
74
  resolve()
@@ -136,7 +136,7 @@ export class TestClient {
136
136
  async received(test) {
137
137
  let actions = []
138
138
  let unbind = this.log.on('add', (action, meta) => {
139
- if (!meta.id.includes(` ${this.nodeId} `)) {
139
+ if (meta.id.split(' ')[1] !== this.nodeId) {
140
140
  actions.push(action)
141
141
  }
142
142
  })
@@ -1,7 +1,7 @@
1
- import type { TestLog, TestTime } from '@logux/core'
1
+ import type { Log, TestLog, TestTime } from '@logux/core'
2
2
 
3
- import { BaseServer } from '../base-server/index.js'
4
3
  import type {
4
+ BaseServer,
5
5
  BaseServerOptions,
6
6
  Logger,
7
7
  ServerMeta
@@ -9,8 +9,10 @@ import type {
9
9
  import type { LoggerOptions } from '../server/index.js'
10
10
  import type { TestClient, TestClientOptions } from '../test-client/index.js'
11
11
 
12
- export interface TestServerOptions
13
- extends Omit<BaseServerOptions, 'minSubprotocol' | 'subprotocol'> {
12
+ export interface TestServerOptions extends Omit<
13
+ BaseServerOptions,
14
+ 'minSubprotocol' | 'subprotocol'
15
+ > {
14
16
  /**
15
17
  * Disable built-in auth.
16
18
  */
@@ -46,8 +48,9 @@ export interface TestServerOptions
46
48
  * ```
47
49
  */
48
50
  export class TestServer<
49
- Headers extends object = unknown
50
- > extends BaseServer<Headers> {
51
+ Headers extends object = unknown,
52
+ ClientData extends object = unknown
53
+ > extends BaseServer<Headers, Log<ServerMeta>, ClientData> {
51
54
  /**
52
55
  * fetch() compatible API to test HTTP endpoints.
53
56
  *
@@ -34,8 +34,8 @@ export class TestServer extends BaseServer {
34
34
 
35
35
  this.fetch = this.fetch.bind(this)
36
36
 
37
- this.on('fatal', () => {
38
- this.destroy()
37
+ this.on('fatal', async () => {
38
+ await this.destroy()
39
39
  })
40
40
  }
41
41
 
@@ -72,7 +72,8 @@ export class TestServer extends BaseServer {
72
72
  if (e.message === 'Server undid action') {
73
73
  if (e.action.reason !== reason) {
74
74
  throw new Error(
75
- `Undo was with ${e.action.reason} reason, not ${reason}`
75
+ `Undo was with ${e.action.reason} reason, not ${reason}`,
76
+ { cause: e }
76
77
  )
77
78
  }
78
79
  } else {
@@ -0,0 +1,28 @@
1
+ const PARAM = /:(\w+)/g
2
+ const SPECIAL = /[.*+?^${}()|[\]\\]/g
3
+
4
+ function escape(text) {
5
+ return text.replace(SPECIAL, '\\$&')
6
+ }
7
+
8
+ export function createPattern(pattern) {
9
+ let names = []
10
+ let source = ''
11
+ let last = 0
12
+ for (let param of pattern.matchAll(PARAM)) {
13
+ source += escape(pattern.slice(last, param.index)) + '([^/]+)'
14
+ names.push(param[1])
15
+ last = param.index + param[0].length
16
+ }
17
+ let regexp = new RegExp('^' + source + escape(pattern.slice(last)) + '$')
18
+
19
+ return url => {
20
+ let match = regexp.exec(url)
21
+ if (!match) return null
22
+ let params = {}
23
+ for (let i = 0; i < names.length; i++) {
24
+ if (names[i] !== '__proto__') params[names[i]] = match[i + 1]
25
+ }
26
+ return params
27
+ }
28
+ }