wspromisify 2.10.1 → 2.10.2

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,44 +0,0 @@
1
- import { createNew } from '../utils'
2
- import mockServer from '../mock/server'
3
-
4
- import WS from 'ws'
5
- import { test } from '../suite'
6
- import { equals } from 'pepka'
7
-
8
-
9
- const addr = (port: number) => 'ws://localhost:' + port
10
-
11
- /** If an existing socket connection is provided via config. */
12
- test('existing_socket', () => new Promise(async (ff, rj) => {
13
- const {port} = await mockServer()
14
- const to = setTimeout(() => rj(), 4e4)
15
- const existing_addr = addr(port)
16
-
17
- const ws1 = await createNew({socket: new WS(existing_addr)}, port)
18
-
19
- const msg1 = {echo: true, msg: 'existing_socket!'}
20
- const response1 = await ws1.send(msg1)
21
-
22
- if(!equals(response1, msg1)) return rj('Bad echo.')
23
- await ws1.close()
24
-
25
- const ws2_0 = new WS(existing_addr)
26
-
27
- ws2_0.addEventListener('open', async () => {
28
- const ws2 = await createNew({socket: ws2_0}, port)
29
-
30
- if(ws2.socket?.readyState !== 1) return rj('Bad echo.')
31
-
32
- const msg2 = {echo: true, msg: 'existing_socket!'}
33
- const response2 = await ws2.send(msg2)
34
-
35
- if(
36
- ws2.socket?.readyState as number !== 1
37
- || !equals(response2, msg2)
38
- ) return rj('not ready.')
39
- await ws2.close()
40
-
41
- clearTimeout(to)
42
- ff()
43
- })
44
- }))
@@ -1,19 +0,0 @@
1
- import { createNew } from '../utils.js'
2
- import mockServer from '../mock/server.js'
3
- import { equals } from 'pepka'
4
- import { test } from '../suite.js'
5
-
6
- /** Sends massages if they were .send() before connection is estabilished. */
7
- test('lazy send before open queued.', () => new Promise(async (ff, rj) => {
8
- const {port} = await mockServer()
9
- let to = setTimeout(() => rj('cannot create'), 2e2)
10
- const ws = await createNew({lazy: true}, port)
11
- clearTimeout(to)
12
-
13
- const msg = {echo: true, msg: 'hello!'}
14
- to = setTimeout(() => rj('cannot send'), 2e2)
15
- const response = await ws.send(msg)
16
- clearTimeout(to)
17
-
18
- if(equals(response, msg)) ff(); else rj()
19
- }))
@@ -1,16 +0,0 @@
1
- import { createNew, timeout } from '../utils'
2
- import mockServer from '../mock/server'
3
- import { equals, wait } from 'pepka'
4
- import { test } from '../suite'
5
-
6
- /** Lazy connect. */
7
- test('lazy', timeout(2e3, async () => {
8
- const {port} = await mockServer()
9
- const ws = await createNew({ lazy: true }, port)
10
-
11
- if(ws.socket !== null) throw new Error('Socket is not open.')
12
- else {
13
- const msg = {echo: true, msg: 'hello!'}
14
- if(!equals(await ws.send(msg), msg)) throw new Error('msg\s not equal.')
15
- }
16
- }))
@@ -1,18 +0,0 @@
1
- import mockServer from '../mock/server'
2
- import { test } from '../suite'
3
- import WebSocketClient from '../../src/WSC'
4
-
5
- /** Ready method. */
6
- test('No native throws without an adapter', async () => {
7
- const {port} = await mockServer()
8
- let pass = false
9
- try {
10
- new WebSocketClient({ url: 'ws://127.0.0.1:' + port })
11
- try {
12
- if(WebSocket) pass = true
13
- } catch {}
14
- } catch {
15
- pass=true
16
- }
17
- if(!pass) throw new Error('Does not throw.')
18
- })
@@ -1,12 +0,0 @@
1
- import { createNew, timeout } from '../utils'
2
- import mockServer from '../mock/server'
3
- import { test } from '../suite'
4
-
5
- /** Ready method. */
6
- test('ready', timeout(4e3, async () => {
7
- const {port} = await mockServer()
8
- const ws = await createNew({}, port)
9
-
10
- await ws.ready()
11
- if(!ws.socket) throw new Error()
12
- }))
@@ -1,41 +0,0 @@
1
- import { createNew, timeout } from '../utils'
2
- import mockServer from '../mock/server'
3
- import { equals, wait } from 'pepka'
4
- import { test } from '../suite'
5
-
6
- /** Reconnects if connection is broken. */
7
- test('reconnect', timeout(1e4, () => async () => {
8
- const {port, shutDown} = await mockServer()
9
- const ws = await createNew({ reconnect: 1 }, port)
10
- await wait(200)
11
- await shutDown()
12
- await wait(500)
13
- await mockServer(port)
14
- await wait(600)
15
- const msg = {echo: true, msg: 'hello!'}
16
- const response = await ws.send(msg)
17
- if(!equals(response, msg)) throw new Error('not equals.')
18
- }))
19
- /** Should send messages that were queued while being disconnected right after the reconnect. */
20
- test('reconnect-queue', timeout(1e4, async () => {
21
- const {port, shutDown} = await mockServer()
22
- const ws = await createNew({ reconnect: 1, timeout: 5e3 }, port)
23
- await ws.ready()
24
- await shutDown()
25
- const msg1 = {echo: true, msg: 'hello!'}
26
- const msg2 = {echo: true, msg: 'hello 2!'}
27
- const msg3 = {echo: true, msg: 'hello 3!'}
28
- let ok = false
29
- const p = new Promise<void>((ff) => ws.send(msg1).then((res) => {
30
- if(!equals(res, msg1)) throw new Error('msg1 not equals.')
31
- else ws.send(msg2).then((res) => {
32
- if(!equals(res, msg2)) throw new Error('msg2 not equals.')
33
- else {ok=true}
34
- ff()
35
- })
36
- }))
37
- await mockServer(port)
38
- await p
39
- if(!ok) throw new Error('Not sent.')
40
- if(!equals(await ws.send(msg3), msg3)) throw new Error('not sent or msg3 not equals.')
41
- }))
@@ -1,13 +0,0 @@
1
- import { createNew, timeout } from '../utils'
2
- import mockServer from '../mock/server'
3
- import { test } from '../suite'
4
-
5
- /** Socket property check. */
6
- test('sockets', timeout(1e4, () => new Promise<void>(async (ff, rj) => {
7
- const {port} = await mockServer()
8
- const ws = await createNew({}, port)
9
-
10
- await ws.ready()
11
-
12
- if(ws.socket && !isNaN(ws.socket.readyState)) ff(); else rj()
13
- })))
@@ -1,74 +0,0 @@
1
- import { createNew, timeout } from '../utils'
2
- import mockServer from '../mock/server'
3
- import { test } from '../suite'
4
-
5
- /** Comprehensive test for stream method. */
6
- test('stream-comprehensive', timeout(1e4, () => new Promise<void>(async (ff, rj) => {
7
-
8
- const {port} = await mockServer()
9
- let to = setTimeout(() => rj('cannot create'), 2e2)
10
- const ws = await createNew({}, port)
11
- clearTimeout(to)
12
-
13
- to = setTimeout(() => rj('cannot ready'), 2e2)
14
- await ws.ready()
15
- clearTimeout(to)
16
-
17
- // Test 1: Basic stream functionality
18
- const msg1 = {stream: true, test: 'stream1', chunks: [1], delay: 10}
19
- to = setTimeout(() => rj('stream1 timeout'), 2e2)
20
-
21
- try {
22
- const stream1 = ws.stream(msg1)
23
- const result1 = await stream1.next()
24
-
25
- // Check that we got a valid response with the expected test property
26
- if (result1.done || !result1.value?.test || result1.value.test !== msg1.test) {
27
- clearTimeout(to)
28
- return rj('stream1 failed')
29
- }
30
-
31
- clearTimeout(to)
32
-
33
- // Test 2: Stream with for-await loop
34
- const msg2 = {stream: true, test: 'stream2'}
35
- to = setTimeout(() => rj('stream2 timeout'), 2e4)
36
-
37
- const stream2 = ws.stream<typeof msg2, any>(msg2)
38
- const results: any[] = []
39
-
40
- for await (const chunk of stream2) {
41
- results.push(chunk)
42
- break // We expect only one chunk
43
- }
44
-
45
- clearTimeout(to)
46
-
47
- // Test 3: Multiple concurrent streams
48
- const msg3a = {stream: true, test: 'stream3a'}
49
- const msg3b = {stream: true, test: 'stream3b'}
50
- to = setTimeout(() => rj('stream3 timeout'), 2e4)
51
-
52
- const stream3a = ws.stream(msg3a)
53
- const stream3b = ws.stream(msg3b)
54
-
55
- const result3a = await stream3a.next()
56
- const result3b = await stream3b.next()
57
-
58
- clearTimeout(to)
59
-
60
- // For streaming messages, check that we got valid chunks with the expected properties
61
- if (result3a.done || result3b.done ||
62
- !result3a.value?.test || result3a.value.test !== msg3a.test ||
63
- !result3b.value?.test || result3b.value.test !== msg3b.test) {
64
- return rj('stream3 failed')
65
- }
66
-
67
- ff()
68
- } catch (error) {
69
- clearTimeout(to)
70
- console.log('STREAM-COMPREHENSIVE TEST FAILED:', error)
71
- rj('stream comprehensive error: ' + error)
72
- }
73
- })
74
- ))
@@ -1,61 +0,0 @@
1
- import { createNew, timeout } from '../utils'
2
- import mockServer from '../mock/server'
3
- import { test } from '../suite'
4
- import { last } from 'pepka'
5
-
6
- /** Test real streaming functionality with multiple chunks. */
7
- test('stream-real', timeout(1.5e4, () => new Promise<void>(async (ff, rj) => {
8
-
9
- const {port} = await mockServer()
10
- let to = setTimeout(() => {
11
- console.log('STREAM-REAL: Timeout - cannot create')
12
- rj('cannot create')
13
- }, 2e4)
14
- const ws = await createNew({}, port)
15
- clearTimeout(to)
16
-
17
- to = setTimeout(() => rj('cannot ready'), 2e4)
18
- await ws.ready()
19
- clearTimeout(to)
20
-
21
- // Test real streaming with multiple chunks
22
- const streamMsg = {stream: true, multi: true, test: 'real-stream', chunks: [1, 2, 3], delay: 50}
23
- to = setTimeout(() => rj('stream timeout'), 5e3)
24
-
25
- try {
26
- const stream = ws.stream<typeof streamMsg, any>(streamMsg)
27
- const chunks: any[] = []
28
-
29
- for await (const chunk of stream) chunks.push(chunk)
30
-
31
- clearTimeout(to)
32
-
33
- // Verify we got all 20 chunks in perfect order
34
- if (chunks.length !== 20) {
35
- return rj(`Expected exactly 20 chunks, got ${chunks.length}`)
36
- }
37
-
38
- // Check that all chunks are present and in perfect order (1-20)
39
- for (let i = 0; i < 20; i++) {
40
- if (chunks[i].chunk !== i + 1) {
41
- return rj(`Chunk ${i} should be ${i + 1}, got ${chunks[i].chunk}`)
42
- }
43
- }
44
-
45
- // Verify last chunk has done flag
46
- if (!chunks[20 - 1].done) {
47
- return rj('Last chunk should have done flag')
48
- }
49
-
50
- // Verify last chunk has done flag
51
- // if (!last(chunks as any).done) {
52
- // return rj('Last chunk should have done flag')
53
- // }
54
- ff()
55
- } catch (error) {
56
- clearTimeout(to)
57
- console.log('STREAM-REAL TEST FAILED:', error)
58
- rj('stream real error: ' + error)
59
- }
60
- })
61
- ))
@@ -1,45 +0,0 @@
1
- import { createNew, timeout } from '../utils'
2
- import mockServer from '../mock/server'
3
- import { test } from '../suite'
4
-
5
- /** Simple test for stream method basic functionality. */
6
- test('stream-basic', timeout(7e3, () => new Promise<void>(async (ff, rj) => {
7
- const {port} = await mockServer()
8
- let to = setTimeout(() => rj('cannot create'), 2e4)
9
- const ws = await createNew({}, port)
10
- clearTimeout(to)
11
-
12
- to = setTimeout(() => rj('cannot ready'), 2e4)
13
- await ws.ready()
14
- clearTimeout(to)
15
-
16
- const msg = {stream: true, test: 'stream'}
17
- to = setTimeout(() => rj('stream timeout'), 2e4)
18
-
19
- try {
20
- const stream = ws.stream(msg)
21
-
22
- // Test that stream is an AsyncGenerator
23
- if (typeof stream[Symbol.asyncIterator] !== 'function') {
24
- clearTimeout(to)
25
- return rj('stream is not an AsyncGenerator')
26
- }
27
-
28
- // Test that we can iterate over it
29
- const iterator = stream[Symbol.asyncIterator]()
30
- const firstResult = await iterator.next()
31
-
32
- clearTimeout(to)
33
-
34
- // For streaming messages, check that we got a valid chunk with the expected properties
35
- if (!firstResult.done && firstResult.value && firstResult.value.test === msg.test) {
36
- ff()
37
- } else {
38
- rj(`stream did not return expected value. Got: ${JSON.stringify(firstResult.value)}, Expected test: ${msg.test}`)
39
- }
40
- } catch (error) {
41
- clearTimeout(to)
42
- rj('stream error: ' + error)
43
- }
44
- })
45
- ))
package/test/suite.ts DELETED
@@ -1,3 +0,0 @@
1
- import { suite as uvu_suite } from 'uvu'
2
-
3
- export const test = uvu_suite('.')
package/test/utils.ts DELETED
@@ -1,33 +0,0 @@
1
-
2
- import WebSocketClient from '../src/WSC'
3
- import {AnyFunc, AnyObject} from 'pepka'
4
- import { native_ws } from '../src/utils'
5
- import WS from 'ws'
6
-
7
- export const createNew = (config = {} as wsc.UserConfig, port: number) => new Promise<WebSocketClient>((ff, rj) => {
8
- const ws = new WebSocketClient(Object.assign({
9
- url: 'ws://127.0.0.1:' + port,
10
- // log: (...a: any[]) => console.log(...a),
11
- adapter: (host: string, protocols?: string|string[]) => new (native_ws || WS)(host, protocols)
12
- }, config))
13
- if(ws.socket?.readyState===1 || config.lazy) return ff(ws)
14
- ws.on('error', rj)
15
- ws.on('open', () => {ff(ws)})
16
- })
17
-
18
- // Inspired by tinchoz49 https://github.com/lukeed/uvu/issues/33#issuecomment-879870292
19
- export const timeout = (time: number, handler: AnyFunc) => async (context: AnyObject) => {
20
- let timer: NodeJS.Timeout
21
- try {
22
- await Promise.race([
23
- handler(context),
24
- new Promise((_resolve, reject) =>
25
- timer = setTimeout(() => reject(new Error('timeout')), time)
26
- )
27
- ])
28
- } catch(e) {
29
- throw e
30
- } finally {
31
- if(timer!) clearTimeout(timer)
32
- }
33
- }
package/tslint.json DELETED
@@ -1,25 +0,0 @@
1
- {
2
- "defaultSeverity": "error",
3
- "jsRules": {},
4
- "rules": {
5
- "semicolon": false,
6
- "whitespace": false,
7
- "quotemark": [true, "single"],
8
- "max-line-length": {
9
- "options": [120]
10
- },
11
- "new-parens": true,
12
- "no-arg": true,
13
- "no-console": {
14
- "severity": "warning",
15
- "options": [
16
- "debug",
17
- "info",
18
- "time",
19
- "timeEnd",
20
- "trace"
21
- ]
22
- }
23
- },
24
- "rulesDirectory": []
25
- }