@laplace.live/ws 6.3.2 → 6.3.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@laplace.live/ws",
3
- "version": "6.3.2",
3
+ "version": "6.3.4",
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,9 @@
39
41
  "provenance": true
40
42
  },
41
43
  "dependencies": {
42
- "isomorphic-ws": "^5.0.0",
44
+ "buffer": "^6.0.3",
45
+ "events": "^3.3.0",
46
+ "pako": "^2.1.0",
43
47
  "ws": "^8.19.0"
44
48
  },
45
49
  "devDependencies": {
@@ -48,10 +52,5 @@
48
52
  "@types/pako": "^2.0.4",
49
53
  "@types/ws": "^8.18.1",
50
54
  "typescript": "^5.9.3"
51
- },
52
- "peerDependencies": {
53
- "buffer": "^6.0.3",
54
- "events": "^3.3.0",
55
- "pako": "^2.1.0"
56
55
  }
57
56
  }
package/src/browser.ts CHANGED
@@ -1,9 +1,9 @@
1
1
  import { KeepLive } from './common.ts'
2
2
  import { inflates } from './inflate/browser.ts'
3
- import { LiveWSBase, type WSOptions } from './ws.ts'
3
+ import { LiveWSBase, type WSOptions } from './ws-client.ts'
4
4
 
5
5
  export type { LiveOptions } from './common.ts'
6
- export type { WSOptions } from './ws.ts'
6
+ export type { WSOptions } from './ws-client.ts'
7
7
 
8
8
  export { relayEvent } from './common.ts'
9
9
 
package/src/index.ts CHANGED
@@ -1,11 +1,11 @@
1
1
  import { KeepLive } from './common.ts'
2
2
  import { inflates } from './inflate/node.ts'
3
3
  import { LiveTCPBase, type TCPOptions } from './tcp.ts'
4
- import { LiveWSBase, type WSOptions } from './ws.ts'
4
+ import { LiveWSBase, type WSOptions } from './ws-node.ts'
5
5
 
6
6
  export type { LiveOptions } from './common.ts'
7
7
  export type { TCPOptions } from './tcp.ts'
8
- export type { WSOptions } from './ws.ts'
8
+ export type { WSOptions } from './ws-node.ts'
9
9
 
10
10
  export { relayEvent } from './common.ts'
11
11
  export { getConf, getRoomid } from './extra.ts'
package/src/tcp.ts CHANGED
@@ -1,4 +1,4 @@
1
- import net, { type Socket } from 'net'
1
+ import net, { type Socket } from 'node:net'
2
2
 
3
3
  import type { Inflates } from './buffer.ts'
4
4
 
@@ -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
+ }
@@ -1,6 +1,6 @@
1
+ import type { Agent } from 'node:http'
1
2
  import { EventEmitter } from 'events'
2
- import type { Agent } from 'http'
3
- import IsomorphicWebSocket from 'isomorphic-ws'
3
+ import WS from 'ws'
4
4
 
5
5
  import type { Inflates } from './buffer.ts'
6
6
 
@@ -8,25 +8,17 @@ import { Live, type LiveOptions } from './common.ts'
8
8
 
9
9
  export type WSOptions = LiveOptions & { address?: string; agent?: Agent }
10
10
 
11
- export const isNode = !!IsomorphicWebSocket.Server
11
+ class WSWrapper extends EventEmitter {
12
+ ws: WS
12
13
 
13
- class WebSocket extends EventEmitter {
14
- ws: IsomorphicWebSocket
15
-
16
- constructor(address: string, inflates: Inflates, ...args: any[]) {
14
+ constructor(address: string, ...args: any[]) {
17
15
  super()
18
16
 
19
- const ws = new IsomorphicWebSocket(address, ...(isNode ? args : []))
17
+ const ws = new WS(address, ...args)
20
18
  this.ws = ws
21
19
 
22
20
  ws.onopen = () => this.emit('open')
23
- ws.onmessage = isNode
24
- ? ({ data }) => this.emit('message', data)
25
- : async ({ data }) =>
26
- this.emit(
27
- 'message',
28
- inflates.Buffer.from(await new Response(data as unknown as InstanceType<typeof Blob>).arrayBuffer())
29
- )
21
+ ws.onmessage = ({ data }) => this.emit('message', data)
30
22
  ws.onerror = () => this.emit('error')
31
23
  ws.onclose = () => this.emit('close')
32
24
  }
@@ -45,14 +37,14 @@ class WebSocket extends EventEmitter {
45
37
  }
46
38
 
47
39
  export class LiveWSBase extends Live {
48
- ws: InstanceType<typeof WebSocket>
40
+ ws: WSWrapper
49
41
 
50
42
  constructor(
51
43
  inflates: Inflates,
52
44
  roomid: number,
53
45
  { address = 'wss://broadcastlv.chat.bilibili.com/sub', agent, ...options }: WSOptions = {}
54
46
  ) {
55
- const ws = new WebSocket(address, inflates, { agent })
47
+ const ws = new WSWrapper(address, { agent })
56
48
  const send = (data: Buffer) => {
57
49
  if (ws.readyState === 1) {
58
50
  ws.send(data)