From 8b1726578d0e419b86f6155eefc1bed301a9e25d Mon Sep 17 00:00:00 2001 From: Ward Peeters Date: Mon, 16 May 2022 23:02:19 +0200 Subject: [PATCH 1/7] chore(gatsby): remove other services --- .../src/gatsby-node.js | 2 +- packages/gatsby/src/commands/develop.ts | 252 +----------------- packages/gatsby/src/commands/types.ts | 1 - .../internal-data-bridge/gatsby-node.js | 2 +- packages/gatsby/src/redux/reducers/program.ts | 1 - .../src/services/start-webpack-server.ts | 2 +- packages/gatsby/src/utils/develop-proxy.ts | 84 ------ packages/gatsby/src/utils/telemetry-server.ts | 68 ----- 8 files changed, 11 insertions(+), 401 deletions(-) delete mode 100644 packages/gatsby/src/utils/develop-proxy.ts delete mode 100644 packages/gatsby/src/utils/telemetry-server.ts diff --git a/packages/gatsby-plugin-netlify-cms/src/gatsby-node.js b/packages/gatsby-plugin-netlify-cms/src/gatsby-node.js index 16f7c5300ab13..37afc0412ed92 100644 --- a/packages/gatsby-plugin-netlify-cms/src/gatsby-node.js +++ b/packages/gatsby-plugin-netlify-cms/src/gatsby-node.js @@ -173,7 +173,7 @@ exports.onCreateWebpackConfig = ( messages: [ `Netlify CMS is running at ${ program.https ? `https://` : `http://` - }${program.host}:${program.proxyPort}/${publicPathClean}/`, + }${program.host}:${program.port}/${publicPathClean}/`, ], }, }), diff --git a/packages/gatsby/src/commands/develop.ts b/packages/gatsby/src/commands/develop.ts index 77dac27e248bb..047ed48832943 100644 --- a/packages/gatsby/src/commands/develop.ts +++ b/packages/gatsby/src/commands/develop.ts @@ -1,28 +1,15 @@ // NOTE(@mxstbr): Do not use the reporter in this file, as that has side-effects on import which break structured logging import path from "path" -import http from "http" -import https from "https" import tmp from "tmp" import { ChildProcess } from "child_process" import execa from "execa" -import chokidar from "chokidar" -import getRandomPort from "detect-port" import { detectPortInUseAndPrompt } from "../utils/detect-port-in-use-and-prompt" -import { Server as SocketIO } from "socket.io" import fs from "fs-extra" import onExit from "signal-exit" -import { - isCI, - slash, - createServiceLock, - getService, - updateInternalSiteMetadata, - UnlockFn, - uuid, -} from "gatsby-core-utils" +import { v4 } from "gatsby-core-utils/uuid" +import { slash } from "gatsby-core-utils/path" import reporter from "gatsby-cli/lib/reporter" import { getSslCert } from "../utils/get-ssl-cert" -import { IProxyControls, startDevelopProxy } from "../utils/develop-proxy" import { IProgram, IDebugInfo } from "./types" import { flush as telemetryFlush } from "gatsby-telemetry" @@ -41,34 +28,6 @@ const requireUncached = (file: string): any => { } } -// Heuristics for gatsby-config.js, as not all changes to it require a full restart to take effect -const doesConfigChangeRequireRestart = ( - lastConfig: Record, - newConfig: Record -): boolean => { - // Ignore changes to siteMetadata - const replacer = (_, v): string | void => { - if (typeof v === `function` || v instanceof RegExp) { - return v.toString() - } else { - return v - } - } - - const oldConfigString = JSON.stringify( - { ...lastConfig, siteMetadata: null }, - replacer - ) - const newConfigString = JSON.stringify( - { ...newConfig, siteMetadata: null }, - replacer - ) - - if (oldConfigString === newConfigString) return false - - return true -} - // Return a user-supplied port otherwise the default Node.js debugging port const getDebugPort = (port?: number): number => port ?? 9229 @@ -200,7 +159,7 @@ const REGEX_IP = module.exports = async (program: IProgram): Promise => { global.__GATSBY = { - buildId: uuid.v4(), + buildId: v4(), root: program.directory, } @@ -208,9 +167,6 @@ module.exports = async (program: IProgram): Promise => { // So we want to early just force it to a number to ensure we always act on a correct type. program.port = parseInt(program.port + ``, 10) const developProcessPath = slash(require.resolve(`./develop-process`)) - const telemetryServerPath = slash( - require.resolve(`../utils/telemetry-server`) - ) try { program.port = await detectPortInUseAndPrompt(program.port) @@ -224,27 +180,15 @@ module.exports = async (program: IProgram): Promise => { // Run the actual develop server on a random port, and the proxy on the program port // which users will access - const proxyPort = program.port const debugInfo = getDebugInfo(program) const rootFile = (file: string): string => path.join(program.directory, file) // Require gatsby-config.js before accessing process.env, to enable the user to change // environment variables from the config file. - let lastConfig = requireUncached(rootFile(`gatsby-config`)) - - // INTERNAL_STATUS_PORT allows for setting the websocket port used for monitoring - // when the browser should prompt the user to restart the develop process. - // This port is randomized by default and in most cases should never be required to configure. - // It is exposed for environments where port access needs to be explicit, such as with Docker. - // As the port is meant for internal usage only, any attempt to interface with features - // it exposes via third-party software is not supported. - const [statusServerPort, developPort, telemetryServerPort] = - await Promise.all([ - getRandomPort(process.env.INTERNAL_STATUS_PORT), - getRandomPort(), - getRandomPort(), - ]) + requireUncached(rootFile(`gatsby-config`)) + + const developPort = program.port // In order to enable custom ssl, --cert-file --key-file and -https flags must all be // used together @@ -284,21 +228,12 @@ module.exports = async (program: IProgram): Promise => { } } - // NOTE(@mxstbr): We need to start the develop proxy before the develop process to ensure - // codesandbox detects the right port to expose by default - const proxy = startDevelopProxy({ - proxyPort: proxyPort, - targetPort: developPort, - program, - }) - const developProcess = new ControllableScript( ` const cmd = require(${JSON.stringify(developProcessPath)}); const args = ${JSON.stringify({ ...program, port: developPort, - proxyPort, // Don't pass SSL options down to the develop process, it should always use HTTP ssl: null, debugInfo, @@ -308,106 +243,17 @@ module.exports = async (program: IProgram): Promise => { debugInfo ) - const telemetryServerProcess = new ControllableScript( - `require(${JSON.stringify(telemetryServerPath)}).default(${JSON.stringify( - telemetryServerPort - )})`, - null - ) - - let unlocks: Array = [] - if (!isCI()) { - const statusUnlock = await createServiceLock( - program.directory, - `developstatusserver`, - { - port: statusServerPort, - } - ) - const developUnlock = await createServiceLock( - program.directory, - `developproxy`, - { - port: proxyPort, - } - ) - const telemetryUnlock = await createServiceLock( - program.directory, - `telemetryserver`, - { - port: telemetryServerPort, - } - ) - await updateInternalSiteMetadata({ - name: program.sitePackageJson.name, - sitePath: program.directory, - pid: process.pid, - lastRun: Date.now(), - }) - - if (!statusUnlock || !developUnlock) { - const data = await getService(program.directory, `developproxy`) - const port = data?.port || 8000 - console.error( - `Looks like develop for this site is already running, can you visit ${ - program.ssl ? `https:` : `http:` - }//localhost:${port} ? If it is not, try again in five seconds!` - ) - process.exit(1) - } - - unlocks = unlocks.concat([statusUnlock, developUnlock, telemetryUnlock]) - } - - const statusServer = program.ssl - ? https.createServer(program.ssl) - : http.createServer() - statusServer.listen(statusServerPort) - - const io = new SocketIO(statusServer, { - // whitelist all (https://github.com/expressjs/cors#configuration-options) - cors: { - origin: true, - }, - cookie: true, - }) - const handleChildProcessIPC = (msg): void => { if (msg.type === `HEARTBEAT`) return if (process.send) { // Forward IPC process.send(msg) } - - io.emit(`structured-log`, msg) - - if ( - msg.type === `LOG_ACTION` && - msg.action.type === `SET_STATUS` && - msg.action.payload === `SUCCESS` - ) { - proxy.serveSite() - } } - io.on(`connection`, socket => { - socket.on(`develop:restart`, async respond => { - isRestarting = true - proxy.serveRestartingScreen() - // respond() responds to the client, which in our case prompts it to reload the page to show the restarting screen - if (respond) respond(`develop:is-starting`) - await developProcess.stop() - developProcess.start() - developProcess.onMessage(handleChildProcessIPC) - isRestarting = false - }) - }) - developProcess.start() developProcess.onMessage(handleChildProcessIPC) - telemetryServerProcess.start() - // Plugins can call `process.exit` which would be sent to `develop-process` (child process) // This needs to be propagated back to the parent process developProcess.onExit( @@ -440,58 +286,10 @@ module.exports = async (program: IProgram): Promise => { } ) - const files = [ - rootFile(`gatsby-config.js`), - rootFile(`gatsby-node.js`), - rootFile(`gatsby-config.ts`), - rootFile(`gatsby-node.ts`), - ] - const GATSBY_CONFIG_REGEX = /^gatsby-config\.[jt]s$/ - let watcher: chokidar.FSWatcher - - if (!isCI()) { - watcher = chokidar.watch(files).on(`change`, filePath => { - const file = path.basename(filePath) - - if (file.match(GATSBY_CONFIG_REGEX)) { - const newConfig = requireUncached(rootFile(`gatsby-config`)) - - if (!doesConfigChangeRequireRestart(lastConfig, newConfig)) { - lastConfig = newConfig - return - } - - lastConfig = newConfig - } - - console.warn( - `develop process needs to be restarted to apply the changes to ${file}` - ) - io.emit(`structured-log`, { - type: `LOG_ACTION`, - action: { - type: `DEVELOP`, - payload: `RESTART_REQUIRED`, - dirtyFile: file, - }, - }) - }) - } - - // route ipc messaging to the original develop process - process.on(`message`, msg => { - developProcess.send(msg) - }) - process.on(`SIGINT`, async () => { await shutdownServices( { developProcess, - telemetryServerProcess, - unlocks, - statusServer, - proxy, - watcher, }, `SIGINT` ) @@ -503,11 +301,6 @@ module.exports = async (program: IProgram): Promise => { await shutdownServices( { developProcess, - telemetryServerProcess, - unlocks, - statusServer, - proxy, - watcher, }, `SIGTERM` ) @@ -519,11 +312,6 @@ module.exports = async (program: IProgram): Promise => { shutdownServices( { developProcess, - telemetryServerProcess, - unlocks, - statusServer, - proxy, - watcher, }, signal as NodeJS.Signals ) @@ -531,23 +319,11 @@ module.exports = async (program: IProgram): Promise => { } interface IShutdownServicesOptions { - statusServer: https.Server | http.Server developProcess: ControllableScript - proxy: IProxyControls - unlocks: Array - watcher: chokidar.FSWatcher - telemetryServerProcess: ControllableScript } function shutdownServices( - { - statusServer, - developProcess, - proxy, - unlocks, - watcher, - telemetryServerProcess, - }: IShutdownServicesOptions, + { developProcess }: IShutdownServicesOptions, signal: NodeJS.Signals ): Promise { try { @@ -555,19 +331,7 @@ function shutdownServices( } catch (e) { // nop } - const services = [ - developProcess.stop(signal), - telemetryServerProcess.stop(), - watcher?.close(), - new Promise(resolve => statusServer.close(resolve)), - new Promise(resolve => proxy.server.close(resolve)), - ] - - unlocks.forEach(unlock => { - if (unlock) { - services.push(unlock()) - } - }) + const services = [developProcess.stop(signal)] return Promise.all(services) .catch(() => {}) diff --git a/packages/gatsby/src/commands/types.ts b/packages/gatsby/src/commands/types.ts index 1cc9c692aef7c..36c8c8f80e407 100644 --- a/packages/gatsby/src/commands/types.ts +++ b/packages/gatsby/src/commands/types.ts @@ -19,7 +19,6 @@ export interface IProgram { open: boolean openTracingConfigFile: string port: number - proxyPort: number host: string report: Reporter [`cert-file`]?: string diff --git a/packages/gatsby/src/internal-plugins/internal-data-bridge/gatsby-node.js b/packages/gatsby/src/internal-plugins/internal-data-bridge/gatsby-node.js index 50a6943b1dc3e..73b0ed335adba 100644 --- a/packages/gatsby/src/internal-plugins/internal-data-bridge/gatsby-node.js +++ b/packages/gatsby/src/internal-plugins/internal-data-bridge/gatsby-node.js @@ -80,7 +80,7 @@ exports.sourceNodes = ({ siteMetadata: { ...configCopy.siteMetadata, }, - port: program.proxyPort, + port: program.port, host: program.host, ...configCopy, } diff --git a/packages/gatsby/src/redux/reducers/program.ts b/packages/gatsby/src/redux/reducers/program.ts index 07bcbbd70ffa6..6fae530c98943 100644 --- a/packages/gatsby/src/redux/reducers/program.ts +++ b/packages/gatsby/src/redux/reducers/program.ts @@ -9,7 +9,6 @@ const initialState: IStateProgram = { open: false, openTracingConfigFile: ``, port: 80, - proxyPort: 80, host: `localhost`, sitePackageJson: {}, extensions: [], diff --git a/packages/gatsby/src/services/start-webpack-server.ts b/packages/gatsby/src/services/start-webpack-server.ts index 2f35c46626538..444256798a896 100644 --- a/packages/gatsby/src/services/start-webpack-server.ts +++ b/packages/gatsby/src/services/start-webpack-server.ts @@ -74,7 +74,7 @@ export async function startWebpackServer({ const urls = prepareUrls( program.https ? `https` : `http`, program.host, - program.proxyPort + program.port ) const isSuccessful = !stats.hasErrors() diff --git a/packages/gatsby/src/utils/develop-proxy.ts b/packages/gatsby/src/utils/develop-proxy.ts deleted file mode 100644 index dc1aaef7c1735..0000000000000 --- a/packages/gatsby/src/utils/develop-proxy.ts +++ /dev/null @@ -1,84 +0,0 @@ -import http from "http" -import https from "https" -import httpProxy from "http-proxy" -import fs from "fs-extra" -import { getServices } from "gatsby-core-utils" -import restartingScreen from "./restarting-screen" -import { IProgram } from "../commands/types" - -export interface IProxyControls { - serveRestartingScreen: () => void - serveSite: () => void - server: https.Server | http.Server -} - -const noop = (): void => {} - -export const startDevelopProxy = (input: { - proxyPort: number - targetPort: number - program: IProgram -}): IProxyControls => { - let shouldServeRestartingScreen = false - - const proxy = httpProxy.createProxyServer({ - target: `http://localhost:${input.targetPort}`, - changeOrigin: true, - preserveHeaderKeyCase: true, - autoRewrite: true, - ws: true, - }) - - // Noop on proxy errors, as this throws a bunch of "Socket hang up" - // ones whenever the page is refreshed - proxy.on(`error`, noop) - - const app: http.RequestListener = (req, res): void => { - // Add a route at localhost:8000/___services for service discovery - if (req.url === `/___services`) { - getServices(input.program.directory).then(services => { - res.setHeader(`Content-Type`, `application/json`) - res.end(JSON.stringify(services)) - }) - return - } - - if (req.url === `/socket.io/socket.io.js`) { - res.setHeader(`Content-Type`, `application/javascript`) - res.end( - fs.readFileSync(require.resolve(`socket.io-client/dist/socket.io.js`)) - ) - return - } - - if ( - shouldServeRestartingScreen || - req.url === `/___debug-restarting-screen` - ) { - res.end(restartingScreen) - return - } - - proxy.web(req, res) - } - - const server = input.program.ssl - ? https.createServer(input.program.ssl, app) - : http.createServer(app) - - server.on(`upgrade`, function (req, socket, head) { - proxy.ws(req, socket, head) - }) - - server.listen(input.proxyPort, input.program.host) - - return { - server, - serveRestartingScreen: (): void => { - shouldServeRestartingScreen = true - }, - serveSite: (): void => { - shouldServeRestartingScreen = false - }, - } -} diff --git a/packages/gatsby/src/utils/telemetry-server.ts b/packages/gatsby/src/utils/telemetry-server.ts deleted file mode 100644 index 65bdda74dcead..0000000000000 --- a/packages/gatsby/src/utils/telemetry-server.ts +++ /dev/null @@ -1,68 +0,0 @@ -/* - * This exposes gatsby-telemetry functions over HTTP. POST an array of arguments to the path. - * For example: - * curl -X POST http://localhost:2345/setVersion - * -H "Content-Type: application/json" - * -d "[\"1.2.3\"]" - */ -import express from "express" -import bodyParser from "body-parser" -import cors from "cors" -import { - setDefaultComponentId, - trackCli, - trackError, - startBackgroundUpdate, -} from "gatsby-telemetry" - -setDefaultComponentId(`gatsby-admin`) - -// These routes will exist in the API at the keys, e.g. -// http://localhost:1234/trackEvent -const ROUTES = { - trackEvent: trackCli, - trackError, -} - -const app = express() - -app.use(cors()) - -// Overview over all possible routes at / -app.get(`/`, (_, res) => { - res.set(`Content-Type`, `text/html`) - res.send( - `
    - ${Object.keys(ROUTES) - .map(route => `
  • /${route}
  • `) - .join(`\n`)} -
` - ) -}) - -Object.keys(ROUTES).map(route => { - app.post(`/${route}`, bodyParser.json(), (req, res) => { - if (!req.body || !Array.isArray(req.body)) { - res.json({ - status: `error`, - error: `Please provide a body array with the arguments for the function.`, - }) - return - } - - try { - ROUTES[route](...req.body) - } catch (err) { - console.error(err) - res.json({ status: `error`, error: err.message }) - return - } - - res.json({ status: `success` }) - }) -}) - -export default function startTelemetryServer(port: number): void { - startBackgroundUpdate() - app.listen(port) -} From 8f2aee466f7fa34273be99c64bacae3cd7110002 Mon Sep 17 00:00:00 2001 From: Ward Peeters Date: Tue, 17 May 2022 08:46:59 +0200 Subject: [PATCH 2/7] update --- packages/gatsby/src/commands/develop.ts | 2 ++ packages/gatsby/src/commands/types.ts | 2 ++ packages/gatsby/src/redux/reducers/program.ts | 1 + packages/gatsby/src/utils/graphql-typegen/file-writes.ts | 2 +- 4 files changed, 6 insertions(+), 1 deletion(-) diff --git a/packages/gatsby/src/commands/develop.ts b/packages/gatsby/src/commands/develop.ts index 047ed48832943..65e8a43f4a650 100644 --- a/packages/gatsby/src/commands/develop.ts +++ b/packages/gatsby/src/commands/develop.ts @@ -234,6 +234,8 @@ module.exports = async (program: IProgram): Promise => { const args = ${JSON.stringify({ ...program, port: developPort, + // TODO(v5): remove + proxyPort: developPort, // Don't pass SSL options down to the develop process, it should always use HTTP ssl: null, debugInfo, diff --git a/packages/gatsby/src/commands/types.ts b/packages/gatsby/src/commands/types.ts index 36c8c8f80e407..f495700bac099 100644 --- a/packages/gatsby/src/commands/types.ts +++ b/packages/gatsby/src/commands/types.ts @@ -19,6 +19,8 @@ export interface IProgram { open: boolean openTracingConfigFile: string port: number + // TODO(v5): remove + proxyPort: number host: string report: Reporter [`cert-file`]?: string diff --git a/packages/gatsby/src/redux/reducers/program.ts b/packages/gatsby/src/redux/reducers/program.ts index 6fae530c98943..07bcbbd70ffa6 100644 --- a/packages/gatsby/src/redux/reducers/program.ts +++ b/packages/gatsby/src/redux/reducers/program.ts @@ -9,6 +9,7 @@ const initialState: IStateProgram = { open: false, openTracingConfigFile: ``, port: 80, + proxyPort: 80, host: `localhost`, sitePackageJson: {}, extensions: [], diff --git a/packages/gatsby/src/utils/graphql-typegen/file-writes.ts b/packages/gatsby/src/utils/graphql-typegen/file-writes.ts index 459552d07b2d3..04a4b80f6d5f5 100644 --- a/packages/gatsby/src/utils/graphql-typegen/file-writes.ts +++ b/packages/gatsby/src/utils/graphql-typegen/file-writes.ts @@ -31,7 +31,7 @@ export async function writeGraphQLConfig( endpoints: { default: { url: `${program.https ? `https://` : `http://`}${program.host}:${ - program.proxyPort + program.port }/___graphql`, }, }, From 594f9cc67bc76456b5267452944186ac1fc04412 Mon Sep 17 00:00:00 2001 From: Ward Peeters Date: Mon, 20 Jun 2022 17:50:49 +0200 Subject: [PATCH 3/7] remove ___services fetch --- packages/gatsby/cache-dir/app.js | 48 ------- .../gatsby/src/utils/restarting-screen.ts | 128 ------------------ 2 files changed, 176 deletions(-) delete mode 100644 packages/gatsby/src/utils/restarting-screen.ts diff --git a/packages/gatsby/cache-dir/app.js b/packages/gatsby/cache-dir/app.js index 86247fc2404bb..f6dd93d47d556 100644 --- a/packages/gatsby/cache-dir/app.js +++ b/packages/gatsby/cache-dir/app.js @@ -1,7 +1,6 @@ /* global HAS_REACT_18 */ import React from "react" import ReactDOM from "react-dom" -import io from "socket.io-client" import socketIo from "./socketIo" import emitter from "./emitter" @@ -93,53 +92,6 @@ apiRunnerAsync(`onClientEntry`).then(() => { }) } - fetch(`/___services`) - .then(res => res.json()) - .then(services => { - if (services.developstatusserver) { - let isRestarting = false - const parentSocket = io( - `${window.location.protocol}//${window.location.hostname}:${services.developstatusserver.port}` - ) - - parentSocket.on(`structured-log`, msg => { - if ( - !isRestarting && - msg.type === `LOG_ACTION` && - msg.action.type === `DEVELOP` && - msg.action.payload === `RESTART_REQUIRED` && - window.confirm( - `The develop process needs to be restarted for the changes to ${msg.action.dirtyFile} to be applied.\nDo you want to restart the develop process now?` - ) - ) { - isRestarting = true - parentSocket.emit(`develop:restart`, () => { - window.location.reload() - }) - } - - if ( - isRestarting && - msg.type === `LOG_ACTION` && - msg.action.type === `SET_STATUS` && - msg.action.payload === `SUCCESS` - ) { - isRestarting = false - window.location.reload() - } - }) - - // Prevents certain browsers spamming XHR 'ERR_CONNECTION_REFUSED' - // errors within the console, such as when exiting the develop process. - parentSocket.on(`disconnect`, () => { - console.warn( - `[socket.io] Disconnected. Unable to perform health-check.` - ) - parentSocket.close() - }) - } - }) - /** * Service Workers are persistent by nature. They stick around, * serving a cached version of the site if they aren't removed. diff --git a/packages/gatsby/src/utils/restarting-screen.ts b/packages/gatsby/src/utils/restarting-screen.ts deleted file mode 100644 index e42095a351b34..0000000000000 --- a/packages/gatsby/src/utils/restarting-screen.ts +++ /dev/null @@ -1,128 +0,0 @@ -// noop fn to be used as a tagged template literal for code highlighting and prettier formatting -const html = (html: TemplateStringsArray): string => html.join(`\n`) - -export default html` - - - Restarting... - - - - -
- -

Gatsby is restarting the develop server...

- - -
- - -` From d8c47fd1b0aeaaec757d7316cad5a555aa7b7504 Mon Sep 17 00:00:00 2001 From: Ward Peeters Date: Tue, 21 Jun 2022 00:35:18 +0200 Subject: [PATCH 4/7] bring back ipc messaging --- packages/gatsby/src/commands/develop.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/packages/gatsby/src/commands/develop.ts b/packages/gatsby/src/commands/develop.ts index 65e8a43f4a650..5339b6b3de9ae 100644 --- a/packages/gatsby/src/commands/develop.ts +++ b/packages/gatsby/src/commands/develop.ts @@ -288,6 +288,11 @@ module.exports = async (program: IProgram): Promise => { } ) + // route ipc messaging to the original develop process + process.on(`message`, msg => { + developProcess.send(msg) + }) + process.on(`SIGINT`, async () => { await shutdownServices( { From f31a617603ee73d0d0762b8747ba45b770cec934 Mon Sep 17 00:00:00 2001 From: Ward Peeters Date: Fri, 24 Jun 2022 11:36:13 +0200 Subject: [PATCH 5/7] improve test handling --- .../__tests__/create-node-manifest.test.js | 44 +++++++++++++------ integration-tests/node-manifest/package.json | 6 ++- .../node-manifest/utils/get-gatsby-process.js | 38 ++++++---------- 3 files changed, 48 insertions(+), 40 deletions(-) diff --git a/integration-tests/node-manifest/__tests__/create-node-manifest.test.js b/integration-tests/node-manifest/__tests__/create-node-manifest.test.js index 916124cc2f952..4b11677273b6f 100644 --- a/integration-tests/node-manifest/__tests__/create-node-manifest.test.js +++ b/integration-tests/node-manifest/__tests__/create-node-manifest.test.js @@ -54,16 +54,25 @@ describe(`Node Manifest API in "gatsby ${gatsbyCommandName}"`, () => { return urling(`http://localhost:${port}`) } else if (gatsbyCommandName === `build`) { // for gatsby build wait for the process to exit - return new Promise(resolve => - gatsbyProcess.on(`exit`, () => { - gatsbyProcess.kill() - resolve() - }) - ) + return gatsbyProcess } }) - afterAll(() => gatsbyProcess.kill()) + afterAll(() => { + return new Promise(resolve => { + if (!gatsbyProcess || gatsbyProcess.killed) { + return resolve() + } + + gatsbyProcess.on(`exit`, () => { + setImmediate(() => { + resolve() + }) + }) + + gatsbyProcess.kill() + }) + }) it(`Creates an accurate node manifest when using the ownerNodeId argument in createPage`, async () => { const manifestFileContents = await getManifestContents(1) @@ -166,16 +175,25 @@ describe(`Node Manifest API in "gatsby ${gatsbyCommandName}"`, () => { await urling(`http://localhost:${port}`) } else if (gatsbyCommandName === `build`) { // for gatsby build wait for the process to exit - return new Promise(resolve => { - gatsbyProcess.on(`exit`, () => { - gatsbyProcess.kill() + return gatsbyProcess + } + }) + + afterAll(() => { + return new Promise(resolve => { + if (!gatsbyProcess || gatsbyProcess.killed) { + return resolve() + } + + gatsbyProcess.on(`exit`, () => { + setImmediate(() => { resolve() }) }) - } - }) - afterAll(() => gatsbyProcess.kill()) + gatsbyProcess.kill() + }) + }) it(`Limits the number of node manifest files written to disk to 500`, async () => { const nodeManifestFiles = fs.readdirSync(manifestDir) diff --git a/integration-tests/node-manifest/package.json b/integration-tests/node-manifest/package.json index cab6ffe4b199e..d870f3010852a 100644 --- a/integration-tests/node-manifest/package.json +++ b/integration-tests/node-manifest/package.json @@ -4,7 +4,7 @@ "description": "A test site for Gatsby's node manifest API", "main": "index.js", "scripts": { - "test": "gatsby clean && GATSBY_COMMAND_NAME=build jest --runInBand && gatsby clean && GATSBY_COMMAND_NAME=develop jest --runInBand" + "test": "cross-env GATSBY_COMMAND_NAME=build jest --runInBand && cross-env GATSBY_COMMAND_NAME=develop jest --runInBand" }, "author": "Tyler Barnes ", "license": "ISC", @@ -14,9 +14,11 @@ "react-dom": "^17.0.2" }, "devDependencies": { + "cross-env": "^7.0.3", + "execa": "^5.1.1", "fs-extra": "^10.0.0", "jest": "^27.2.1", "rimraf": "^3.0.2", "urling": "^1.0.7" } -} \ No newline at end of file +} diff --git a/integration-tests/node-manifest/utils/get-gatsby-process.js b/integration-tests/node-manifest/utils/get-gatsby-process.js index d51bb68a8155c..1a5ebd3852ef9 100644 --- a/integration-tests/node-manifest/utils/get-gatsby-process.js +++ b/integration-tests/node-manifest/utils/get-gatsby-process.js @@ -1,29 +1,17 @@ -const { spawn } = require(`child_process`) +const execa = require(`execa`) const path = require(`path`) -const gatsbyBin = path.join(`node_modules`, `.bin`, `gatsby`) +function spawnGatsbyProcess(command = `develop`, env = {}) { + return execa(process.execPath, [`./node_modules/gatsby/cli.js`, command], { + stdio: [`inherit`, `inherit`, `inherit`], + env: { + ...process.env, + NODE_ENV: command === `develop` ? `development` : `production`, + ...env, + }, + }) +} -exports.spawnGatsbyProcess = (command = `develop`, env = {}) => - spawn( - gatsbyBin, - [command, ...(command === `develop` ? ["-H", "localhost"] : [])], - { - stdio: [`inherit`, `inherit`, `inherit`, `inherit`], - env: { - ...process.env, - NODE_ENV: command === `develop` ? `development` : `production`, - ...env, - }, - } - ) - - exports.runGatsbyClean = () => - spawn( - gatsbyBin, - ['clean'], - { - stdio: [`inherit`, `inherit`, `inherit`, `inherit`], - env: { ...process.env }, - }, - ) +exports.spawnGatsbyProcess = spawnGatsbyProcess +exports.runGatsbyClean = () => spawnGatsbyProcess("clean") From b77d2100180310134d2b9afff68076db4c53a26e Mon Sep 17 00:00:00 2001 From: Ward Peeters Date: Wed, 6 Jul 2022 16:16:28 +0200 Subject: [PATCH 6/7] update test --- .../__tests__/create-node-manifest.test.js | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/integration-tests/node-manifest/__tests__/create-node-manifest.test.js b/integration-tests/node-manifest/__tests__/create-node-manifest.test.js index 4b11677273b6f..00333d5a37ad8 100644 --- a/integration-tests/node-manifest/__tests__/create-node-manifest.test.js +++ b/integration-tests/node-manifest/__tests__/create-node-manifest.test.js @@ -60,7 +60,11 @@ describe(`Node Manifest API in "gatsby ${gatsbyCommandName}"`, () => { afterAll(() => { return new Promise(resolve => { - if (!gatsbyProcess || gatsbyProcess.killed) { + if ( + !gatsbyProcess || + gatsbyProcess.killed || + gatsbyProcess.exitCode !== null + ) { return resolve() } @@ -181,7 +185,11 @@ describe(`Node Manifest API in "gatsby ${gatsbyCommandName}"`, () => { afterAll(() => { return new Promise(resolve => { - if (!gatsbyProcess || gatsbyProcess.killed) { + if ( + !gatsbyProcess || + gatsbyProcess.killed || + gatsbyProcess.exitCode !== null + ) { return resolve() } From 06e5fee907ebacbda53b0822dd834c48deaed15f Mon Sep 17 00:00:00 2001 From: Ward Peeters Date: Tue, 12 Jul 2022 14:11:50 +0200 Subject: [PATCH 7/7] update --- .../src/gatsby-node.js | 3 ++- packages/gatsby/package.json | 2 -- yarn.lock | 19 +++---------------- 3 files changed, 5 insertions(+), 19 deletions(-) diff --git a/packages/gatsby-plugin-netlify-cms/src/gatsby-node.js b/packages/gatsby-plugin-netlify-cms/src/gatsby-node.js index 37afc0412ed92..196bfa290eb20 100644 --- a/packages/gatsby-plugin-netlify-cms/src/gatsby-node.js +++ b/packages/gatsby-plugin-netlify-cms/src/gatsby-node.js @@ -170,10 +170,11 @@ exports.onCreateWebpackConfig = ( new FriendlyErrorsPlugin({ clearConsole: false, compilationSuccessInfo: { + // TODO(v5): change proxyPort back in port messages: [ `Netlify CMS is running at ${ program.https ? `https://` : `http://` - }${program.host}:${program.port}/${publicPathClean}/`, + }${program.host}:${program.proxyPort}/${publicPathClean}/`, ], }, }), diff --git a/packages/gatsby/package.json b/packages/gatsby/package.json index 02c71b819659d..f7e54c5441ab6 100644 --- a/packages/gatsby/package.json +++ b/packages/gatsby/package.json @@ -48,7 +48,6 @@ "babel-preset-gatsby": "^2.19.0-next.2", "better-opn": "^2.1.1", "bluebird": "^3.7.2", - "body-parser": "^1.19.0", "browserslist": "^4.17.5", "cache-manager": "^2.11.1", "chalk": "^4.1.2", @@ -109,7 +108,6 @@ "graphql-compose": "^9.0.7", "graphql-playground-middleware-express": "^1.7.22", "hasha": "^5.2.2", - "http-proxy": "^1.18.1", "invariant": "^2.2.4", "is-relative": "^1.0.0", "is-relative-url": "^3.0.0", diff --git a/yarn.lock b/yarn.lock index 1887b60a2839f..b8d3888b322d2 100644 --- a/yarn.lock +++ b/yarn.lock @@ -6632,7 +6632,7 @@ body-parser@1.19.0: raw-body "2.4.0" type-is "~1.6.17" -body-parser@^1.19.0, body-parser@^1.20.0: +body-parser@^1.20.0: version "1.20.0" resolved "https://registry.yarnpkg.com/body-parser/-/body-parser-1.20.0.tgz#3de69bd89011c11573d7bfee6a64f11b6bd27cc5" integrity sha512-DfJ+q6EPcGKZD1QWUjSpqp+Q7bDQTsQIF4zfUAtZ6qk+H/3/QRhg9CEp39ss+/T2vw0+HaidC0ecJj/DRLIaKg== @@ -10554,7 +10554,7 @@ eventemitter3@^3.1.0: resolved "https://registry.yarnpkg.com/eventemitter3/-/eventemitter3-3.1.2.tgz#2d3d48f9c346698fce83a85d7d664e98535df6e7" integrity sha512-tvtQIeLVHjDkJYnzf2dgVMxfuSGJeM/7UCG17TT4EumTfNtF+0nebF/4zWOIkCreAbtNqhGEboB6BWrwqNaw4Q== -eventemitter3@^4.0.0, eventemitter3@^4.0.4: +eventemitter3@^4.0.4: version "4.0.7" resolved "https://registry.yarnpkg.com/eventemitter3/-/eventemitter3-4.0.7.tgz#2de9b68f6528d5644ef5c59526a1b4a07306169f" integrity sha512-8guHBZCwKnFhYdHr2ysuRWErTwhoN2X8XELRlrRwpmfeY2jjuUN4taQMsULKUVo1K4DvZl+0pgfyoysHxvmvEw== @@ -11220,7 +11220,7 @@ flush-write-stream@^1.0.0, flush-write-stream@^1.0.2: inherits "^2.0.1" readable-stream "^2.0.4" -follow-redirects@^1.0.0, follow-redirects@^1.10.0: +follow-redirects@^1.10.0: version "1.13.0" resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.13.0.tgz#b42e8d93a2a7eea5ed88633676d6597bc8e384db" integrity sha512-aq6gF1BEKje4a9i9+5jimNFIpq4Q1WiwBToeRK5NvZBd/TRsmW8BsJfOEGkr76TbOyPVD3OVDN910EcUNtRYEA== @@ -12870,15 +12870,6 @@ http-proxy-agent@^4.0.1: agent-base "6" debug "4" -http-proxy@^1.18.1: - version "1.18.1" - resolved "https://registry.yarnpkg.com/http-proxy/-/http-proxy-1.18.1.tgz#401541f0534884bbf95260334e72f88ee3976549" - integrity sha512-7mz/721AbnJwIVbnaSv1Cz3Am0ZLT/UBwkC92VlxhXv/k/BBQfM2fXElQNC27BVGr0uwUpplYPQM9LnaBMR5NQ== - dependencies: - eventemitter3 "^4.0.0" - follow-redirects "^1.0.0" - requires-port "^1.0.0" - http-response-object@^1.0.0, http-response-object@^1.0.1, http-response-object@^1.1.0: version "1.1.0" resolved "https://registry.yarnpkg.com/http-response-object/-/http-response-object-1.1.0.tgz#a7c4e75aae82f3bb4904e4f43f615673b4d518c3" @@ -21512,10 +21503,6 @@ require-package-name@^2.0.1: version "2.0.1" resolved "https://registry.yarnpkg.com/require-package-name/-/require-package-name-2.0.1.tgz#c11e97276b65b8e2923f75dabf5fb2ef0c3841b9" -requires-port@^1.0.0: - version "1.0.0" - resolved "https://registry.yarnpkg.com/requires-port/-/requires-port-1.0.0.tgz#925d2601d39ac485e091cf0da5c6e694dc3dcaff" - reselect@^4.0.0: version "4.0.0" resolved "https://registry.yarnpkg.com/reselect/-/reselect-4.0.0.tgz#f2529830e5d3d0e021408b246a206ef4ea4437f7"