@laplace.live/ws 6.3.1 → 6.3.3
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.
- package/package.json +3 -2
- package/src/browser.ts +6 -6
- package/src/common.ts +1 -1
- package/src/index.test.ts +2 -2
- package/src/index.ts +9 -9
- package/src/inflate/browser.ts +1 -1
- package/src/tcp.ts +10 -5
- package/src/ws-client.ts +63 -0
- package/src/ws-node.ts +64 -0
- package/src/ws.ts +0 -61
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@laplace.live/ws",
|
|
3
|
-
"version": "6.3.
|
|
3
|
+
"version": "6.3.3",
|
|
4
4
|
"description": "Bilibili Live WebSocket/TCP API",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./src/index.ts",
|
|
@@ -10,6 +10,8 @@
|
|
|
10
10
|
"browser": "./src/browser.ts",
|
|
11
11
|
"default": "./src/index.ts"
|
|
12
12
|
},
|
|
13
|
+
"./server": "./src/index.ts",
|
|
14
|
+
"./client": "./src/browser.ts",
|
|
13
15
|
"./browser": "./src/browser.ts"
|
|
14
16
|
},
|
|
15
17
|
"scripts": {
|
|
@@ -39,7 +41,6 @@
|
|
|
39
41
|
"provenance": true
|
|
40
42
|
},
|
|
41
43
|
"dependencies": {
|
|
42
|
-
"isomorphic-ws": "^5.0.0",
|
|
43
44
|
"ws": "^8.19.0"
|
|
44
45
|
},
|
|
45
46
|
"devDependencies": {
|
package/src/browser.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import { KeepLive } from './common'
|
|
2
|
-
import { inflates } from './inflate/browser'
|
|
3
|
-
import { LiveWSBase, type WSOptions } from './ws'
|
|
1
|
+
import { KeepLive } from './common.ts'
|
|
2
|
+
import { inflates } from './inflate/browser.ts'
|
|
3
|
+
import { LiveWSBase, type WSOptions } from './ws-client.ts'
|
|
4
4
|
|
|
5
|
-
export type { LiveOptions } from './common'
|
|
6
|
-
export type { WSOptions } from './ws'
|
|
5
|
+
export type { LiveOptions } from './common.ts'
|
|
6
|
+
export type { WSOptions } from './ws-client.ts'
|
|
7
7
|
|
|
8
|
-
export { relayEvent } from './common'
|
|
8
|
+
export { relayEvent } from './common.ts'
|
|
9
9
|
|
|
10
10
|
export class LiveWS extends LiveWSBase {
|
|
11
11
|
constructor(roomid: number, opts?: WSOptions) {
|
package/src/common.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { EventEmitter } from 'events'
|
|
2
2
|
|
|
3
|
-
import { encoder, type Inflates, makeDecoder } from './buffer'
|
|
3
|
+
import { encoder, type Inflates, makeDecoder } from './buffer.ts'
|
|
4
4
|
|
|
5
5
|
export type LiveOptions = { protover?: 1 | 2 | 3; key?: string; authBody?: any; uid?: number; buvid?: string }
|
|
6
6
|
|
package/src/index.test.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { describe, expect, it } from 'bun:test'
|
|
2
2
|
import { once } from 'events'
|
|
3
3
|
|
|
4
|
-
import { KeepLiveWS as KeepLiveWSBrowser, LiveWS as LiveWSBrowser } from './browser'
|
|
5
|
-
import { getConf, getRoomid, KeepLiveTCP, KeepLiveWS, LiveTCP, LiveWS } from './index'
|
|
4
|
+
import { KeepLiveWS as KeepLiveWSBrowser, LiveWS as LiveWSBrowser } from './browser.ts'
|
|
5
|
+
import { getConf, getRoomid, KeepLiveTCP, KeepLiveWS, LiveTCP, LiveWS } from './index.ts'
|
|
6
6
|
|
|
7
7
|
const TIMEOUT = 1000 * 25
|
|
8
8
|
const watch = (
|
package/src/index.ts
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
import { KeepLive } from './common'
|
|
2
|
-
import { inflates } from './inflate/node'
|
|
3
|
-
import { LiveTCPBase, type TCPOptions } from './tcp'
|
|
4
|
-
import { LiveWSBase, type WSOptions } from './ws'
|
|
1
|
+
import { KeepLive } from './common.ts'
|
|
2
|
+
import { inflates } from './inflate/node.ts'
|
|
3
|
+
import { LiveTCPBase, type TCPOptions } from './tcp.ts'
|
|
4
|
+
import { LiveWSBase, type WSOptions } from './ws-node.ts'
|
|
5
5
|
|
|
6
|
-
export type { LiveOptions } from './common'
|
|
7
|
-
export type { TCPOptions } from './tcp'
|
|
8
|
-
export type { WSOptions } from './ws'
|
|
6
|
+
export type { LiveOptions } from './common.ts'
|
|
7
|
+
export type { TCPOptions } from './tcp.ts'
|
|
8
|
+
export type { WSOptions } from './ws-node.ts'
|
|
9
9
|
|
|
10
|
-
export { relayEvent } from './common'
|
|
11
|
-
export { getConf, getRoomid } from './extra'
|
|
10
|
+
export { relayEvent } from './common.ts'
|
|
11
|
+
export { getConf, getRoomid } from './extra.ts'
|
|
12
12
|
|
|
13
13
|
export class LiveWS extends LiveWSBase {
|
|
14
14
|
constructor(roomid: number, opts?: WSOptions) {
|
package/src/inflate/browser.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Buffer } from 'buffer'
|
|
2
2
|
import { inflate } from 'pako'
|
|
3
3
|
|
|
4
|
-
import { BrotliDecode } from './brotli'
|
|
4
|
+
import { BrotliDecode } from './brotli.ts'
|
|
5
5
|
|
|
6
6
|
const inflateAsync = (d: Buffer) => Buffer.from(inflate(d))
|
|
7
7
|
const brotliDecompressAsync = (d: Buffer) => Buffer.from(BrotliDecode(Int8Array.from(d)))
|
package/src/tcp.ts
CHANGED
|
@@ -1,16 +1,21 @@
|
|
|
1
|
-
import net, { Socket } from 'net'
|
|
1
|
+
import net, { type Socket } from 'node:net'
|
|
2
2
|
|
|
3
|
-
import { Inflates } from './buffer'
|
|
4
|
-
import { LiveOptions, Live } from './common'
|
|
3
|
+
import type { Inflates } from './buffer.ts'
|
|
5
4
|
|
|
6
|
-
|
|
5
|
+
import { Live, type LiveOptions } from './common.ts'
|
|
6
|
+
|
|
7
|
+
export type TCPOptions = LiveOptions & { host?: string; port?: number }
|
|
7
8
|
|
|
8
9
|
export class LiveTCPBase extends Live {
|
|
9
10
|
socket: Socket
|
|
10
11
|
buffer: Buffer
|
|
11
12
|
i: number
|
|
12
13
|
|
|
13
|
-
constructor(
|
|
14
|
+
constructor(
|
|
15
|
+
inflates: Inflates,
|
|
16
|
+
roomid: number,
|
|
17
|
+
{ host = 'broadcastlv.chat.bilibili.com', port = 2243, ...options }: TCPOptions = {}
|
|
18
|
+
) {
|
|
14
19
|
const socket = net.connect(port, host)
|
|
15
20
|
const send = (data: Buffer) => {
|
|
16
21
|
socket.write(data)
|
package/src/ws-client.ts
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { EventEmitter } from 'events'
|
|
2
|
+
|
|
3
|
+
import type { Inflates } from './buffer.ts'
|
|
4
|
+
|
|
5
|
+
import { Live, type LiveOptions } from './common.ts'
|
|
6
|
+
|
|
7
|
+
export type WSOptions = LiveOptions & { address?: string }
|
|
8
|
+
|
|
9
|
+
class WSWrapper extends EventEmitter {
|
|
10
|
+
ws: WebSocket
|
|
11
|
+
|
|
12
|
+
constructor(address: string, inflates: Inflates) {
|
|
13
|
+
super()
|
|
14
|
+
|
|
15
|
+
const ws = new WebSocket(address)
|
|
16
|
+
this.ws = ws
|
|
17
|
+
|
|
18
|
+
ws.binaryType = 'arraybuffer'
|
|
19
|
+
ws.onopen = () => this.emit('open')
|
|
20
|
+
ws.onmessage = ({ data }) => this.emit('message', inflates.Buffer.from(data as ArrayBuffer))
|
|
21
|
+
ws.onerror = () => this.emit('error')
|
|
22
|
+
ws.onclose = () => this.emit('close')
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
get readyState() {
|
|
26
|
+
return this.ws.readyState
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
send(data: Buffer) {
|
|
30
|
+
this.ws.send(data)
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
close(code?: number, reason?: string) {
|
|
34
|
+
this.ws.close(code, reason)
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export class LiveWSBase extends Live {
|
|
39
|
+
ws: WSWrapper
|
|
40
|
+
|
|
41
|
+
constructor(
|
|
42
|
+
inflates: Inflates,
|
|
43
|
+
roomid: number,
|
|
44
|
+
{ address = 'wss://broadcastlv.chat.bilibili.com/sub', ...options }: WSOptions = {}
|
|
45
|
+
) {
|
|
46
|
+
const ws = new WSWrapper(address, inflates)
|
|
47
|
+
const send = (data: Buffer) => {
|
|
48
|
+
if (ws.readyState === 1) {
|
|
49
|
+
ws.send(data)
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
const close = () => this.ws.close()
|
|
53
|
+
|
|
54
|
+
super(inflates, roomid, { send, close, ...options })
|
|
55
|
+
|
|
56
|
+
ws.on('open', (...params) => this.emit('open', ...params))
|
|
57
|
+
ws.on('message', data => this.emit('message', data as Buffer))
|
|
58
|
+
ws.on('close', (code, reason) => this.emit('close', code, reason))
|
|
59
|
+
ws.on('error', error => this.emit('_error', error))
|
|
60
|
+
|
|
61
|
+
this.ws = ws
|
|
62
|
+
}
|
|
63
|
+
}
|
package/src/ws-node.ts
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import type { Agent } from 'node:http'
|
|
2
|
+
import { EventEmitter } from 'events'
|
|
3
|
+
import WS from 'ws'
|
|
4
|
+
|
|
5
|
+
import type { Inflates } from './buffer.ts'
|
|
6
|
+
|
|
7
|
+
import { Live, type LiveOptions } from './common.ts'
|
|
8
|
+
|
|
9
|
+
export type WSOptions = LiveOptions & { address?: string; agent?: Agent }
|
|
10
|
+
|
|
11
|
+
class WSWrapper extends EventEmitter {
|
|
12
|
+
ws: WS
|
|
13
|
+
|
|
14
|
+
constructor(address: string, ...args: any[]) {
|
|
15
|
+
super()
|
|
16
|
+
|
|
17
|
+
const ws = new WS(address, ...args)
|
|
18
|
+
this.ws = ws
|
|
19
|
+
|
|
20
|
+
ws.onopen = () => this.emit('open')
|
|
21
|
+
ws.onmessage = ({ data }) => this.emit('message', data)
|
|
22
|
+
ws.onerror = () => this.emit('error')
|
|
23
|
+
ws.onclose = () => this.emit('close')
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
get readyState() {
|
|
27
|
+
return this.ws.readyState
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
send(data: Buffer) {
|
|
31
|
+
this.ws.send(data)
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
close(code?: number, data?: string) {
|
|
35
|
+
this.ws.close(code, data)
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export class LiveWSBase extends Live {
|
|
40
|
+
ws: WSWrapper
|
|
41
|
+
|
|
42
|
+
constructor(
|
|
43
|
+
inflates: Inflates,
|
|
44
|
+
roomid: number,
|
|
45
|
+
{ address = 'wss://broadcastlv.chat.bilibili.com/sub', agent, ...options }: WSOptions = {}
|
|
46
|
+
) {
|
|
47
|
+
const ws = new WSWrapper(address, { agent })
|
|
48
|
+
const send = (data: Buffer) => {
|
|
49
|
+
if (ws.readyState === 1) {
|
|
50
|
+
ws.send(data)
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
const close = () => this.ws.close()
|
|
54
|
+
|
|
55
|
+
super(inflates, roomid, { send, close, ...options })
|
|
56
|
+
|
|
57
|
+
ws.on('open', (...params) => this.emit('open', ...params))
|
|
58
|
+
ws.on('message', data => this.emit('message', data as Buffer))
|
|
59
|
+
ws.on('close', (code, reason) => this.emit('close', code, reason))
|
|
60
|
+
ws.on('error', error => this.emit('_error', error))
|
|
61
|
+
|
|
62
|
+
this.ws = ws
|
|
63
|
+
}
|
|
64
|
+
}
|
package/src/ws.ts
DELETED
|
@@ -1,61 +0,0 @@
|
|
|
1
|
-
import { EventEmitter } from 'events'
|
|
2
|
-
import { Agent } from 'http'
|
|
3
|
-
import IsomorphicWebSocket from 'isomorphic-ws'
|
|
4
|
-
|
|
5
|
-
import { Inflates } from './buffer'
|
|
6
|
-
import { LiveOptions, Live } from './common'
|
|
7
|
-
|
|
8
|
-
export type WSOptions = LiveOptions & { address?: string, agent?: Agent }
|
|
9
|
-
|
|
10
|
-
export const isNode = !!IsomorphicWebSocket.Server
|
|
11
|
-
|
|
12
|
-
class WebSocket extends EventEmitter {
|
|
13
|
-
ws: IsomorphicWebSocket
|
|
14
|
-
|
|
15
|
-
constructor(address: string, inflates: Inflates, ...args: any[]) {
|
|
16
|
-
super()
|
|
17
|
-
|
|
18
|
-
const ws = new IsomorphicWebSocket(address, ...(isNode ? args : []))
|
|
19
|
-
this.ws = ws
|
|
20
|
-
|
|
21
|
-
ws.onopen = () => this.emit('open')
|
|
22
|
-
ws.onmessage = isNode ? ({ data }) => this.emit('message', data) : async ({ data }) => this.emit('message', inflates.Buffer.from(await new Response(data as unknown as InstanceType<typeof Blob>).arrayBuffer()))
|
|
23
|
-
ws.onerror = () => this.emit('error')
|
|
24
|
-
ws.onclose = () => this.emit('close')
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
get readyState() {
|
|
28
|
-
return this.ws.readyState
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
send(data: Buffer) {
|
|
32
|
-
this.ws.send(data)
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
close(code?: number, data?: string) {
|
|
36
|
-
this.ws.close(code, data)
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
export class LiveWSBase extends Live {
|
|
41
|
-
ws: InstanceType<typeof WebSocket>
|
|
42
|
-
|
|
43
|
-
constructor(inflates: Inflates, roomid: number, { address = 'wss://broadcastlv.chat.bilibili.com/sub', agent, ...options }: WSOptions = {}) {
|
|
44
|
-
const ws = new WebSocket(address, inflates, { agent })
|
|
45
|
-
const send = (data: Buffer) => {
|
|
46
|
-
if (ws.readyState === 1) {
|
|
47
|
-
ws.send(data)
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
const close = () => this.ws.close()
|
|
51
|
-
|
|
52
|
-
super(inflates, roomid, { send, close, ...options })
|
|
53
|
-
|
|
54
|
-
ws.on('open', (...params) => this.emit('open', ...params))
|
|
55
|
-
ws.on('message', data => this.emit('message', data as Buffer))
|
|
56
|
-
ws.on('close', (code, reason) => this.emit('close', code, reason))
|
|
57
|
-
ws.on('error', error => this.emit('_error', error))
|
|
58
|
-
|
|
59
|
-
this.ws = ws
|
|
60
|
-
}
|
|
61
|
-
}
|