Skip to content

Commit 420a9bc

Browse files
committed
fix: check storage.subscribe return value before calling onSubscribe callback
1 parent f5db0c8 commit 420a9bc

3 files changed

Lines changed: 58 additions & 11 deletions

File tree

src/stream_manager.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,12 @@ export class StreamManager<Context extends unknown> {
105105
}
106106
}
107107

108-
this.#storage.subscribe(uid, channel)
108+
const subscribed = this.#storage.subscribe(uid, channel)
109+
110+
if (!subscribed) {
111+
return false
112+
}
113+
109114
onSubscribe?.({ uid, channel, context: context! })
110115

111116
return true

tests/stream_manager.spec.ts

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,41 @@ test.group('StreamManager', () => {
110110
})
111111

112112
test('should subscribe to a channel', async ({ assert }) => {
113+
const socket = new Socket()
114+
const manager = new StreamManager()
115+
const request = new IncomingMessage(socket)
116+
const response = new ServerResponse(request)
117+
118+
const stream = manager.createStream({
119+
uid: randomUUID(),
120+
request,
121+
response,
122+
context: {},
123+
})
124+
125+
assert.isTrue(await manager.subscribe({ uid: stream.getUid(), channel: 'foo', context: {} }))
126+
})
127+
128+
test('should return false when subscribing with non-existent uid', async ({ assert }) => {
129+
const manager = new StreamManager()
130+
131+
assert.isFalse(await manager.subscribe({ uid: randomUUID(), channel: 'foo', context: {} }))
132+
})
133+
134+
test('should not call onSubscribe when uid does not exist', async ({ assert }) => {
113135
const manager = new StreamManager()
136+
let subscribed = false
114137

115-
assert.isTrue(await manager.subscribe({ uid: randomUUID(), channel: 'foo', context: {} }))
138+
await manager.subscribe({
139+
uid: randomUUID(),
140+
channel: 'foo',
141+
context: {},
142+
onSubscribe() {
143+
subscribed = true
144+
},
145+
})
146+
147+
assert.isFalse(subscribed)
116148
})
117149

118150
test('should not subscribe to a channel if not authorized', async ({ assert }) => {
@@ -124,11 +156,21 @@ test.group('StreamManager', () => {
124156
})
125157

126158
test('should call onSubscribe callback', async ({ assert }) => {
159+
const socket = new Socket()
127160
const manager = new StreamManager()
161+
const request = new IncomingMessage(socket)
162+
const response = new ServerResponse(request)
128163
let subscribed = false
129164

130-
await manager.subscribe({
165+
const stream = manager.createStream({
131166
uid: randomUUID(),
167+
request,
168+
response,
169+
context: {},
170+
})
171+
172+
await manager.subscribe({
173+
uid: stream.getUid(),
132174
channel: 'foo',
133175
context: {},
134176
onSubscribe() {

tests/transmit.spec.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,13 @@ test.group('Transmit', () => {
6262
transport: null,
6363
})
6464

65-
const uid = randomUUID()
65+
const stream = makeStream(transmit)
6666

6767
transmit.authorize('channel1', () => {
6868
return true
6969
})
7070

71-
const authorized = await transmit.subscribe({ channel: 'channel1', uid })
71+
const authorized = await transmit.subscribe({ channel: 'channel1', uid: stream.getUid() })
7272

7373
assert.isTrue(authorized)
7474
})
@@ -94,14 +94,14 @@ test.group('Transmit', () => {
9494
transport: null,
9595
})
9696

97-
const uid = randomUUID()
97+
const stream = makeStream(transmit)
9898

9999
transmit.authorize<{ id: string }>('channel/:id', (_context, params) => {
100100
return params.id === '1'
101101
})
102102

103-
const authorized = await transmit.subscribe({ channel: 'channel/1', uid })
104-
const refused = await transmit.subscribe({ channel: 'channel/2', uid })
103+
const authorized = await transmit.subscribe({ channel: 'channel/1', uid: stream.getUid() })
104+
const refused = await transmit.subscribe({ channel: 'channel/2', uid: stream.getUid() })
105105

106106
assert.isTrue(authorized)
107107
assert.isFalse(refused)
@@ -138,18 +138,18 @@ test.group('Transmit', () => {
138138
transport: null,
139139
})
140140

141-
const uid = randomUUID()
141+
const stream = makeStream(transmit)
142142
let subscribed = false
143143

144144
transmit.on('subscribe', (params) => {
145145
subscribed = true
146146

147-
assert.equal(params.uid, uid)
147+
assert.equal(params.uid, stream.getUid())
148148
assert.equal(params.channel, 'users/1')
149149
})
150150

151151
await transmit.subscribe({
152-
uid,
152+
uid: stream.getUid(),
153153
channel: 'users/1',
154154
})
155155

0 commit comments

Comments
 (0)