Skip to content

Commit bced454

Browse files
authored
fix(realtime): manipulate URLs using URL object (#1769)
1 parent 0deca0c commit bced454

File tree

2 files changed

+89
-4
lines changed

2 files changed

+89
-4
lines changed

packages/core/realtime-js/src/lib/transformers.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -251,8 +251,21 @@ export const toTimestampString = (value: RecordValue): RecordValue => {
251251
}
252252

253253
export const httpEndpointURL = (socketUrl: string): string => {
254-
let url = socketUrl
255-
url = url.replace(/^ws/i, 'http')
256-
url = url.replace(/(\/socket\/websocket|\/socket|\/websocket)\/?$/i, '')
257-
return url.replace(/\/+$/, '') + '/api/broadcast'
254+
const wsUrl = new URL(socketUrl)
255+
256+
wsUrl.protocol = wsUrl.protocol.replace(/^ws/i, 'http')
257+
258+
wsUrl.pathname = wsUrl.pathname
259+
.replace(/\/+$/, '') // remove all trailing slashes
260+
.replace(/\/socket\/websocket$/i, '') // remove the socket/websocket path
261+
.replace(/\/socket$/i, '') // remove the socket path
262+
.replace(/\/websocket$/i, '') // remove the websocket path
263+
264+
if (wsUrl.pathname === '' || wsUrl.pathname === '/') {
265+
wsUrl.pathname = '/api/broadcast'
266+
} else {
267+
wsUrl.pathname = wsUrl.pathname + '/api/broadcast'
268+
}
269+
270+
return wsUrl.href
258271
}

packages/core/realtime-js/test/transformers.test.ts

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
convertChangeData,
77
convertColumn,
88
toArray,
9+
httpEndpointURL,
910
toJson,
1011
toTimestampString,
1112
} from '../src/lib/transformers'
@@ -150,3 +151,74 @@ test('toArray with non-array strings', () => {
150151
assert.strictEqual(toArray('missing_closing', 'text'), 'missing_closing')
151152
assert.strictEqual(toArray('missing_opening}', 'text'), 'missing_opening}')
152153
})
154+
155+
test('httpEndpointURL', () => {
156+
// Test basic ws to http conversion
157+
assert.strictEqual(
158+
httpEndpointURL('ws://example.com/socket/websocket'),
159+
'http://example.com/api/broadcast'
160+
)
161+
162+
// Test wss to https conversion
163+
assert.strictEqual(
164+
httpEndpointURL('wss://example.com/socket/websocket'),
165+
'https://example.com/api/broadcast'
166+
)
167+
168+
// Test with /socket path
169+
assert.strictEqual(httpEndpointURL('ws://example.com/socket'), 'http://example.com/api/broadcast')
170+
171+
// Test with /websocket path
172+
assert.strictEqual(
173+
httpEndpointURL('ws://example.com/websocket'),
174+
'http://example.com/api/broadcast'
175+
)
176+
177+
// Test with trailing slash
178+
assert.strictEqual(
179+
httpEndpointURL('ws://example.com/socket/websocket/'),
180+
'http://example.com/api/broadcast'
181+
)
182+
183+
// Test with port number
184+
assert.strictEqual(
185+
httpEndpointURL('ws://example.com:8080/socket/websocket'),
186+
'http://example.com:8080/api/broadcast'
187+
)
188+
189+
// Test with path prefix
190+
assert.strictEqual(
191+
httpEndpointURL('ws://example.com/prefix/socket/websocket'),
192+
'http://example.com/prefix/api/broadcast'
193+
)
194+
195+
// Test with query parameters
196+
assert.strictEqual(
197+
httpEndpointURL('ws://example.com/socket/websocket?apikey=test'),
198+
'http://example.com/api/broadcast?apikey=test'
199+
)
200+
201+
// Test already http protocol (should remain unchanged)
202+
assert.strictEqual(
203+
httpEndpointURL('http://example.com/socket/websocket'),
204+
'http://example.com/api/broadcast'
205+
)
206+
207+
// Test already https protocol (should remain unchanged)
208+
assert.strictEqual(
209+
httpEndpointURL('https://example.com/socket/websocket'),
210+
'https://example.com/api/broadcast'
211+
)
212+
213+
// Test with multiple trailing slashes
214+
assert.strictEqual(
215+
httpEndpointURL('ws://example.com/socket/websocket///'),
216+
'http://example.com/api/broadcast'
217+
)
218+
219+
// Test with no websocket-specific paths
220+
assert.strictEqual(
221+
httpEndpointURL('ws://example.com/some/path'),
222+
'http://example.com/some/path/api/broadcast'
223+
)
224+
})

0 commit comments

Comments
 (0)