Fix streaming using deprecated url.parse instead of WHATWG URL API (#36973)
This commit is contained in:
parent
f98d8157d6
commit
7b343c9567
@ -3,6 +3,7 @@
|
|||||||
import fs from 'node:fs';
|
import fs from 'node:fs';
|
||||||
import http from 'node:http';
|
import http from 'node:http';
|
||||||
import path from 'node:path';
|
import path from 'node:path';
|
||||||
|
import querystring from 'node:querystring';
|
||||||
import url from 'node:url';
|
import url from 'node:url';
|
||||||
|
|
||||||
import cors from 'cors';
|
import cors from 'cors';
|
||||||
@ -92,6 +93,19 @@ const parseJSON = (json, req) => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parses the query string from a request object.
|
||||||
|
* @param {Request?} req
|
||||||
|
*/
|
||||||
|
const parseQueryString = (req) => {
|
||||||
|
if (!req?.url) {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
const url = new URL(req.url, "http://./");
|
||||||
|
const qs = url.search.slice(1);
|
||||||
|
return querystring.parse(qs);
|
||||||
|
}
|
||||||
|
|
||||||
// Used for priming the counters/gauges for the various metrics that are
|
// Used for priming the counters/gauges for the various metrics that are
|
||||||
// per-channel
|
// per-channel
|
||||||
const CHANNEL_NAMES = [
|
const CHANNEL_NAMES = [
|
||||||
@ -393,8 +407,8 @@ const startServer = async () => {
|
|||||||
*/
|
*/
|
||||||
const accountFromRequest = (req) => new Promise((resolve, reject) => {
|
const accountFromRequest = (req) => new Promise((resolve, reject) => {
|
||||||
const authorization = req.headers.authorization;
|
const authorization = req.headers.authorization;
|
||||||
const location = req.url ? url.parse(req.url, true) : undefined;
|
const query = parseQueryString(req);
|
||||||
const accessToken = location?.query.access_token || req.headers['sec-websocket-protocol'];
|
const accessToken = query?.access_token || req.headers['sec-websocket-protocol'];
|
||||||
|
|
||||||
if (!authorization && !accessToken) {
|
if (!authorization && !accessToken) {
|
||||||
reject(new AuthenticationError('Missing access token'));
|
reject(new AuthenticationError('Missing access token'));
|
||||||
@ -1300,8 +1314,8 @@ const startServer = async () => {
|
|||||||
* @param {import('pino').Logger} log
|
* @param {import('pino').Logger} log
|
||||||
*/
|
*/
|
||||||
function onConnection(ws, req, log) {
|
function onConnection(ws, req, log) {
|
||||||
// Note: url.parse could throw, which would terminate the connection, so we
|
// In case the handler throws, which would terminate the connection,
|
||||||
// increment the connected clients metric straight away when we establish
|
// increment the connected clients metric straight away when it establishes
|
||||||
// the connection, without waiting:
|
// the connection, without waiting:
|
||||||
metrics.connectedClients.labels({ type: 'websocket' }).inc();
|
metrics.connectedClients.labels({ type: 'websocket' }).inc();
|
||||||
|
|
||||||
@ -1383,11 +1397,10 @@ const startServer = async () => {
|
|||||||
|
|
||||||
subscribeWebsocketToSystemChannel(session);
|
subscribeWebsocketToSystemChannel(session);
|
||||||
|
|
||||||
// Parse the URL for the connection arguments (if supplied), url.parse can throw:
|
// Parse the URL for the connection arguments (if supplied)
|
||||||
const location = req.url && url.parse(req.url, true);
|
const query = parseQueryString(req);
|
||||||
|
if (query && query.stream) {
|
||||||
if (location && location.query.stream) {
|
subscribeWebsocketToChannel(session, firstParam(query.stream), query);
|
||||||
subscribeWebsocketToChannel(session, firstParam(location.query.stream), location.query);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user