Skip to content

Commit 3ab492c

Browse files
authored
chore(gatsby): upgrade socket.io (#29765)
* chore(gatsby): upgrade socket.io * fix types
1 parent f8be41a commit 3ab492c

File tree

5 files changed

+143
-153
lines changed

5 files changed

+143
-153
lines changed

packages/gatsby/package.json

+3-4
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,8 @@
138138
"shallow-compare": "^1.2.2",
139139
"signal-exit": "^3.0.3",
140140
"slugify": "^1.4.4",
141-
"socket.io": "2.3.0",
142-
"socket.io-client": "2.3.0",
141+
"socket.io": "3.1.1",
142+
"socket.io-client": "3.1.1",
143143
"source-map": "^0.7.3",
144144
"source-map-support": "^0.5.19",
145145
"st": "^2.0.0",
@@ -175,7 +175,6 @@
175175
"@types/reach__router": "^1.3.5",
176176
"@types/semver": "^7.1.0",
177177
"@types/signal-exit": "^3.0.0",
178-
"@types/socket.io": "^2.1.4",
179178
"@types/string-similarity": "^3.0.0",
180179
"@types/tmp": "^0.2.0",
181180
"@types/webpack-virtual-modules": "^0.1.0",
@@ -261,4 +260,4 @@
261260
"yargs": {
262261
"boolean-negation": false
263262
}
264-
}
263+
}

packages/gatsby/src/commands/develop.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import execa from "execa"
88
import chokidar from "chokidar"
99
import getRandomPort from "detect-port"
1010
import { detectPortInUseAndPrompt } from "../utils/detect-port-in-use-and-prompt"
11-
import socket from "socket.io"
11+
import { Server as SocketIO } from "socket.io"
1212
import fs from "fs-extra"
1313
import onExit from "signal-exit"
1414
import {
@@ -344,7 +344,13 @@ module.exports = async (program: IProgram): Promise<void> => {
344344
: http.createServer()
345345
statusServer.listen(statusServerPort)
346346

347-
const io = socket(statusServer)
347+
const io = new SocketIO(statusServer, {
348+
// whitelist all (https://github.com/expressjs/cors#configuration-options)
349+
cors: {
350+
origin: true,
351+
},
352+
cookie: true,
353+
})
348354

349355
const handleChildProcessIPC = (msg): void => {
350356
if (msg.type === `HEARTBEAT`) return

packages/gatsby/src/utils/__tests__/websocket-manager.ts

+21-12
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ jest.mock(`fs-extra`, () => {
1919
}
2020
})
2121

22+
// we mock it to make tests faster
23+
jest.mock(`gatsby-cli/lib/reporter`, () => {
24+
return {}
25+
})
26+
2227
const INTERVAL_TIMEOUT = 500
2328
const TEST_TIMEOUT = 30000
2429

@@ -49,9 +54,13 @@ describe(`websocket-manager`, () => {
4954
let websocketManager: WebsocketManager
5055
let httpServerAddr
5156

52-
async function getClientSocket(): Promise<typeof io.Socket> {
57+
function getClientSocket(): typeof io.Socket {
58+
return io.default(`http://127.0.0.1:${httpServerAddr.port}`)
59+
}
60+
61+
function getClientSocketAndWaitForConnect(): Promise<typeof io.Socket> {
5362
return new Promise(resolve => {
54-
const clientSocket = io.default(`http://127.0.0.1:${httpServerAddr.port}`)
63+
const clientSocket = getClientSocket()
5564
clientSocket.on(`connect`, () => {
5665
resolve(clientSocket)
5766
})
@@ -149,7 +158,7 @@ describe(`websocket-manager`, () => {
149158
`Can connect`,
150159
async () => {
151160
expect.assertions(1)
152-
const clientSocket = await getClientSocket()
161+
const clientSocket = await getClientSocketAndWaitForConnect()
153162
expect(clientSocket.connected).toBe(true)
154163
clientSocket.disconnect()
155164
},
@@ -170,7 +179,7 @@ describe(`websocket-manager`, () => {
170179
async () => {
171180
expect.assertions(3)
172181

173-
const clientSocket = await getClientSocket()
182+
const clientSocket = await getClientSocketAndWaitForConnect()
174183

175184
expect(websocketManager.activePaths).toEqual(new Set())
176185

@@ -202,8 +211,8 @@ describe(`websocket-manager`, () => {
202211
`track individual clients`,
203212
async () => {
204213
expect.assertions(5)
205-
const clientSocket1 = await getClientSocket()
206-
const clientSocket2 = await getClientSocket()
214+
const clientSocket1 = await getClientSocketAndWaitForConnect()
215+
const clientSocket2 = await getClientSocketAndWaitForConnect()
207216
expect(websocketManager.activePaths).toEqual(new Set())
208217

209218
let activePathAdjustedPromise = waitUntil(done => {
@@ -261,7 +270,7 @@ describe(`websocket-manager`, () => {
261270
async function registerPathnameAndGetPath(
262271
pathname: string
263272
): Promise<string> {
264-
const clientSocket = await getClientSocket()
273+
const clientSocket = await getClientSocketAndWaitForConnect()
265274

266275
if (websocketManager.activePaths.size > 0) {
267276
throw new Error(`There was client connected already`)
@@ -465,7 +474,7 @@ describe(`websocket-manager`, () => {
465474
`Client can receive page query update`,
466475
async () => {
467476
expect.assertions(1)
468-
const clientSocket = await getClientSocket()
477+
const clientSocket = await getClientSocketAndWaitForConnect()
469478

470479
const pageQueryId = `/blog/`
471480
const result = {
@@ -512,7 +521,7 @@ describe(`websocket-manager`, () => {
512521
`Client can receive static query update`,
513522
async () => {
514523
expect.assertions(1)
515-
const clientSocket = await getClientSocket()
524+
const clientSocket = await getClientSocketAndWaitForConnect()
516525

517526
const staticQueryId = `12345`
518527
const result = {
@@ -553,7 +562,7 @@ describe(`websocket-manager`, () => {
553562
it(`Emits errors to display by clients`, async done => {
554563
expect.assertions(1)
555564

556-
const clientSocket = await getClientSocket()
565+
const clientSocket = await getClientSocketAndWaitForConnect()
557566

558567
function handler(msg): void {
559568
if (
@@ -575,7 +584,7 @@ describe(`websocket-manager`, () => {
575584
it(`Emits stored errors to new clients`, async done => {
576585
expect.assertions(1)
577586

578-
const clientSocket = await getClientSocket()
587+
const clientSocket = getClientSocket()
579588

580589
function handler(msg): void {
581590
if (
@@ -597,7 +606,7 @@ describe(`websocket-manager`, () => {
597606
it(`Can clear errors by emitting empty "overlayError" msg`, async done => {
598607
expect.assertions(1)
599608

600-
const clientSocket = await getClientSocket()
609+
const clientSocket = await getClientSocketAndWaitForConnect()
601610

602611
function handler(msg): void {
603612
if (

packages/gatsby/src/utils/websocket-manager.ts

+16-13
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import telemetry from "gatsby-telemetry"
1010
import url from "url"
1111
import { createHash } from "crypto"
1212
import { findPageByPath } from "./find-page-by-path"
13-
import socketIO from "socket.io"
13+
import { Server as SocketIO, Socket } from "socket.io"
1414

1515
export interface IPageQueryResult {
1616
id: string
@@ -31,7 +31,7 @@ function hashPaths(paths: Array<string>): Array<string> {
3131

3232
interface IClientInfo {
3333
activePath: string | null
34-
socket: socketIO.Socket
34+
socket: Socket
3535
}
3636

3737
export class WebsocketManager {
@@ -40,19 +40,22 @@ export class WebsocketManager {
4040
errors: Map<string, string> = new Map()
4141
pageResults: PageResultsMap = new Map()
4242
staticQueryResults: QueryResultsMap = new Map()
43-
websocket: socketIO.Server | undefined
44-
45-
init = ({
46-
server,
47-
}: {
48-
server: HTTPSServer | HTTPServer
49-
}): socketIO.Server => {
50-
this.websocket = socketIO(server, {
43+
websocket: SocketIO | undefined
44+
45+
init = ({ server }: { server: HTTPSServer | HTTPServer }): SocketIO => {
46+
// make typescript happy, else it complained about this.websocket being undefined
47+
const websocket = new SocketIO(server, {
5148
// we see ping-pong timeouts on gatsby-cloud when socket.io is running for a while
5249
// increasing it should help
5350
// @see https://github.com/socketio/socket.io/issues/3259#issuecomment-448058937
5451
pingTimeout: 30000,
52+
// whitelist all (https://github.com/expressjs/cors#configuration-options)
53+
cors: {
54+
origin: true,
55+
},
56+
cookie: true,
5557
})
58+
this.websocket = websocket
5659

5760
const updateServerActivePaths = (): void => {
5861
const serverActivePaths = new Set<string>()
@@ -64,7 +67,7 @@ export class WebsocketManager {
6467
this.activePaths = serverActivePaths
6568
}
6669

67-
this.websocket.on(`connection`, socket => {
70+
websocket.on(`connection`, socket => {
6871
const clientInfo: IClientInfo = {
6972
activePath: null,
7073
socket,
@@ -148,10 +151,10 @@ export class WebsocketManager {
148151
this.emitStalePageDataPathsFromStaticQueriesAssignment.bind(this)
149152
)
150153

151-
return this.websocket
154+
return websocket
152155
}
153156

154-
getSocket = (): socketIO.Server | undefined => this.websocket
157+
getSocket = (): SocketIO | undefined => this.websocket
155158

156159
emitStaticQueryData = (data: IStaticQueryResult): void => {
157160
this.staticQueryResults.set(data.id, data)

0 commit comments

Comments
 (0)