@laplace.live/ws 6.3.4 → 6.3.6

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,9 +0,0 @@
1
- import { Buffer } from 'buffer'
2
- import { inflate } from 'pako'
3
-
4
- import { BrotliDecode } from './brotli.ts'
5
-
6
- const inflateAsync = (d: Buffer) => Buffer.from(inflate(d))
7
- const brotliDecompressAsync = (d: Buffer) => Buffer.from(BrotliDecode(Int8Array.from(d)))
8
-
9
- export const inflates = { inflateAsync, brotliDecompressAsync, Buffer }
@@ -1,11 +0,0 @@
1
- import { promisify } from 'node:util'
2
- import { brotliDecompress, inflate } from 'node:zlib'
3
- import { Buffer } from 'buffer'
4
-
5
- const inflateAsync = promisify<Parameters<typeof inflate>[0], Parameters<Parameters<typeof inflate>[2]>[1]>(inflate)
6
- const brotliDecompressAsync = promisify<
7
- Parameters<typeof brotliDecompress>[0],
8
- Parameters<Parameters<typeof brotliDecompress>[1]>[1]
9
- >(brotliDecompress)
10
-
11
- export const inflates = { inflateAsync, brotliDecompressAsync, Buffer }
package/src/tcp.ts DELETED
@@ -1,53 +0,0 @@
1
- import net, { type Socket } from 'node:net'
2
-
3
- import type { Inflates } from './buffer.ts'
4
-
5
- import { Live, type LiveOptions } from './common.ts'
6
-
7
- export type TCPOptions = LiveOptions & { host?: string; port?: number }
8
-
9
- export class LiveTCPBase extends Live {
10
- socket: Socket
11
- buffer: Buffer
12
- i: number
13
-
14
- constructor(
15
- inflates: Inflates,
16
- roomid: number,
17
- { host = 'broadcastlv.chat.bilibili.com', port = 2243, ...options }: TCPOptions = {}
18
- ) {
19
- const socket = net.connect(port, host)
20
- const send = (data: Buffer) => {
21
- socket.write(data)
22
- }
23
- const close = () => this.socket.end()
24
-
25
- super(inflates, roomid, { send, close, ...options })
26
-
27
- this.i = 0
28
- this.buffer = Buffer.alloc(0)
29
-
30
- socket.on('ready', () => this.emit('open'))
31
- socket.on('close', () => this.emit('close'))
32
- socket.on('error', (...params) => this.emit('_error', ...params))
33
- socket.on('data', buffer => {
34
- this.buffer = Buffer.concat([this.buffer, buffer])
35
- this.splitBuffer()
36
- })
37
- this.socket = socket
38
- }
39
-
40
- splitBuffer() {
41
- while (this.buffer.length >= 4 && this.buffer.readInt32BE(0) <= this.buffer.length) {
42
- const size = this.buffer.readInt32BE(0)
43
- const pack = this.buffer.slice(0, size)
44
- this.buffer = this.buffer.slice(size)
45
- this.i++
46
- if (this.i > 5) {
47
- this.i = 0
48
- this.buffer = Buffer.from(this.buffer)
49
- }
50
- this.emit('message', pack)
51
- }
52
- }
53
- }
package/src/ws-client.ts DELETED
@@ -1,63 +0,0 @@
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 DELETED
@@ -1,64 +0,0 @@
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
- }