@laplace.live/ws 6.3.1

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/src/extra.ts ADDED
@@ -0,0 +1,33 @@
1
+ type GET_DANMU_INFO = {
2
+ code: number
3
+ message: string
4
+ ttl: number
5
+ data: {
6
+ business_id: number
7
+ group: string
8
+ host_list: {
9
+ host: string
10
+ port: number
11
+ wss_port: number
12
+ ws_port: number
13
+ }[]
14
+ max_delay: number
15
+ refresh_rate: number
16
+ refresh_row_factor: number
17
+ token: string
18
+ }
19
+ }
20
+
21
+ export const getConf = async (roomid: number) => {
22
+ // eslint-disable-next-line @typescript-eslint/no-var-requires
23
+ const raw = await fetch(`https://api.live.bilibili.com/xlive/web-room/v1/index/getDanmuInfo?id=${roomid}`).then(w => w.json()) as GET_DANMU_INFO
24
+ const { data: { token: key, host_list: [{ host }] } } = raw
25
+ const address = `wss://${host}/sub`
26
+ return { key, host, address, raw }
27
+ }
28
+
29
+ export const getRoomid = async (short: number) => {
30
+ // eslint-disable-next-line @typescript-eslint/no-var-requires
31
+ const { data: { room_id } } = await fetch(`https://api.live.bilibili.com/room/v1/Room/mobileRoomInit?id=${short}`).then(w => w.json())
32
+ return room_id
33
+ }
@@ -0,0 +1,191 @@
1
+ import { describe, expect, it } from 'bun:test'
2
+ import { once } from 'events'
3
+
4
+ import { KeepLiveWS as KeepLiveWSBrowser, LiveWS as LiveWSBrowser } from './browser'
5
+ import { getConf, getRoomid, KeepLiveTCP, KeepLiveWS, LiveTCP, LiveWS } from './index'
6
+
7
+ const TIMEOUT = 1000 * 25
8
+ const watch = (
9
+ live:
10
+ | LiveWS
11
+ | LiveTCP
12
+ | KeepLiveWS
13
+ | KeepLiveTCP
14
+ | InstanceType<typeof LiveWSBrowser>
15
+ | InstanceType<typeof KeepLiveWSBrowser>
16
+ ) =>
17
+ setTimeout(() => {
18
+ if (!live.closed) {
19
+ live.close()
20
+ }
21
+ }, TIMEOUT)
22
+
23
+ describe('extra', () => {
24
+ it('getRoomid', async () => {
25
+ const roomid = await getRoomid(255)
26
+ expect(roomid).toBe(48743)
27
+ })
28
+ })
29
+
30
+ Object.entries({
31
+ LiveWS,
32
+ LiveTCP,
33
+ KeepLiveWS,
34
+ KeepLiveTCP,
35
+ LiveWSBrowser,
36
+ KeepLiveWSBrowser,
37
+ }).forEach(([name, Live]) => {
38
+ describe(name, () => {
39
+ describe('Connect', () => {
40
+ it('online', async () => {
41
+ const live = new Live(12235923)
42
+ watch(live)
43
+ const [online] = await once(live, 'heartbeat')
44
+ live.close()
45
+ expect(online).toBeGreaterThan(0)
46
+ })
47
+ it('roomid must be number', () => {
48
+ expect(() => new (Live as any)('12235923')).toThrow()
49
+ })
50
+ it('roomid can not be NaN', () => {
51
+ expect(() => new (Live as any)(NaN)).toThrow()
52
+ })
53
+ })
54
+ describe('properties', () => {
55
+ describe('roomid', () => {
56
+ Object.entries({
57
+ Mea: 12235923,
58
+ nana: 21304638,
59
+ fubuki: 11588230,
60
+ }).forEach(([roomName, roomid]) => {
61
+ it(`roomid ${roomName}`, async () => {
62
+ const live = new Live(roomid)
63
+ watch(live)
64
+ await once(live, 'live')
65
+ live.close()
66
+ expect(live.roomid).toBe(roomid)
67
+ })
68
+ })
69
+ })
70
+ it('online', async () => {
71
+ const live = new Live(12235923)
72
+ watch(live)
73
+ const [online] = await once(live, 'heartbeat')
74
+ live.close()
75
+ expect(online).toBe(live.online)
76
+ })
77
+ it('closed', async () => {
78
+ const live = new Live(12235923)
79
+ watch(live)
80
+ expect(live.closed).toBe(false)
81
+ await once(live, 'live')
82
+ live.close()
83
+ expect(live.closed).toBe(true)
84
+ })
85
+ })
86
+ describe('functions', () => {
87
+ it('close', async () => {
88
+ const live = new Live(12235923)
89
+ watch(live)
90
+ await once(live, 'heartbeat')
91
+ const close = await new Promise(resolve => {
92
+ live.on('close', () => resolve('closed'))
93
+ live.close()
94
+ })
95
+ expect(close).toBe('closed')
96
+ })
97
+ it('getOnline', async () => {
98
+ const live = new Live(12235923)
99
+ watch(live)
100
+ await once(live, 'live')
101
+ const online = await live.getOnline()
102
+ live.close()
103
+ expect(online).toBeGreaterThan(0)
104
+ })
105
+ if (name.includes('Keep')) {
106
+ it('no error relay', async () => {
107
+ const live = new Live(12235923) as KeepLiveWS | KeepLiveTCP
108
+ watch(live)
109
+ await once(live, 'live')
110
+ await new Promise<void>((resolve, reject) => {
111
+ live.once('error', reject)
112
+ live.connection.emit('error', new Error('This shold not be caught'))
113
+ setTimeout(resolve, 1000)
114
+ })
115
+ live.close()
116
+ })
117
+ }
118
+ if (name.includes('Keep')) {
119
+ it('close and reopen', async () => {
120
+ const live = new Live(12235923) as KeepLiveWS | KeepLiveTCP
121
+ watch(live)
122
+ await once(live, 'live')
123
+ live.connection.close()
124
+ await once(live, 'live')
125
+ live.close()
126
+ })
127
+ } else {
128
+ it('close on error', async () => {
129
+ const live = new Live(12235923)
130
+ watch(live)
131
+ await once(live, 'heartbeat')
132
+ const close = await new Promise(resolve => {
133
+ live.on('close', () => resolve('closed'))
134
+ live.on('error', () => {})
135
+ live.emit('_error', Error())
136
+ })
137
+ expect(close).toBe('closed')
138
+ })
139
+ }
140
+ })
141
+ describe('options', () => {
142
+ it('protover: 1', async () => {
143
+ const live = new Live(12235923, { protover: 1 })
144
+ watch(live)
145
+ const [online] = await once(live, 'heartbeat')
146
+ live.close()
147
+ expect(online).toBeGreaterThan(0)
148
+ })
149
+ it('protover: 3', async () => {
150
+ const live = new Live(12235923, { protover: 3 })
151
+ watch(live)
152
+ const [online] = await once(live, 'heartbeat')
153
+ live.close()
154
+ expect(online).toBeGreaterThan(0)
155
+ })
156
+ if (name.includes('WS')) {
157
+ it('address', async () => {
158
+ const L = Live as typeof LiveWS | typeof KeepLiveWS
159
+ const live = new L(12235923, {
160
+ address: 'wss://broadcastlv.chat.bilibili.com/sub',
161
+ })
162
+ watch(live)
163
+ const [online] = await once(live, 'heartbeat')
164
+ live.close()
165
+ expect(online).toBeGreaterThan(0)
166
+ })
167
+ } else if (name.includes('TCP')) {
168
+ it('host, port', async () => {
169
+ const live = new Live(12235923, {
170
+ host: 'broadcastlv.chat.bilibili.com',
171
+ port: 2243,
172
+ })
173
+ watch(live)
174
+ const [online] = await once(live, 'heartbeat')
175
+ live.close()
176
+ expect(online).toBeGreaterThan(0)
177
+ })
178
+ } else {
179
+ throw new Error('no options test')
180
+ }
181
+ it('key: token', async () => {
182
+ const { key, host, address } = await getConf(12235923)
183
+ const live = new Live(12235923, { key, host, address })
184
+ watch(live)
185
+ const [online] = await once(live, 'heartbeat')
186
+ live.close()
187
+ expect(online).toBeGreaterThan(0)
188
+ })
189
+ })
190
+ })
191
+ })
package/src/index.ts ADDED
@@ -0,0 +1,35 @@
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'
5
+
6
+ export type { LiveOptions } from './common'
7
+ export type { TCPOptions } from './tcp'
8
+ export type { WSOptions } from './ws'
9
+
10
+ export { relayEvent } from './common'
11
+ export { getConf, getRoomid } from './extra'
12
+
13
+ export class LiveWS extends LiveWSBase {
14
+ constructor(roomid: number, opts?: WSOptions) {
15
+ super(inflates, roomid, opts)
16
+ }
17
+ }
18
+
19
+ export class LiveTCP extends LiveTCPBase {
20
+ constructor(roomid: number, opts?: TCPOptions) {
21
+ super(inflates, roomid, opts)
22
+ }
23
+ }
24
+
25
+ export class KeepLiveWS extends KeepLive<typeof LiveWSBase> {
26
+ constructor(roomid: number, opts?: WSOptions) {
27
+ super(LiveWSBase, inflates, roomid, opts)
28
+ }
29
+ }
30
+
31
+ export class KeepLiveTCP extends KeepLive<typeof LiveTCPBase> {
32
+ constructor(roomid: number, opts?: TCPOptions) {
33
+ super(LiveTCPBase, inflates, roomid, opts)
34
+ }
35
+ }