@hubspot/cms-dev-server 0.3.3 → 0.3.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/createDevServer.js +14 -1
- package/dist/proxyPage/proxyPage.js +1 -1
- package/dist/routes/createDevServerRoutes.js +4 -0
- package/dist/routes/proxyPathPageResourceRedirect.js +36 -0
- package/dist/routes/proxyPathPageRouteHandler.js +43 -0
- package/dist/shadowDevServer/createShadowDevServerRoutes.js +58 -0
- package/dist/shadowDevServer/startShadowDevServer.js +32 -0
- package/package.json +14 -8
package/dist/createDevServer.js
CHANGED
|
@@ -4,6 +4,7 @@ import express from 'express';
|
|
|
4
4
|
import { spawn } from 'child_process';
|
|
5
5
|
import { normalizePath } from 'vite';
|
|
6
6
|
import createDevServerRoutes from './routes/createDevServerRoutes.js';
|
|
7
|
+
import startShadowDevServer from './shadowDevServer/startShadowDevServer.js';
|
|
7
8
|
import { getProjectNameFromHSConfig } from './lib/proxyHelpers.js';
|
|
8
9
|
import createViteDevServer from './createViteDevServer.js';
|
|
9
10
|
import { debug } from './lib/debug.js';
|
|
@@ -60,7 +61,8 @@ Local proxy functionality will not work without setting up authentication.`);
|
|
|
60
61
|
expressServer.locals.projectName = getProjectNameFromHSConfig(path.join(webRoot, '../hsproject.json'));
|
|
61
62
|
expressServer.locals.projectComponentPath = webRootProjectComponentFolder;
|
|
62
63
|
expressServer.use(BASE_PATH, await createDevServerRoutes(viteServer, storybook ? STORYBOOK_PORT : null));
|
|
63
|
-
expressServer.listen(PORT);
|
|
64
|
+
const httpServer = expressServer.listen(PORT);
|
|
65
|
+
startShadowDevServer(PORT);
|
|
64
66
|
let extraServerInfo = '';
|
|
65
67
|
if (authedPortalId) {
|
|
66
68
|
extraServerInfo += `, hubid=${authedPortalId}`;
|
|
@@ -96,5 +98,16 @@ HubSpot CMS dev server hosting: ${expressServer.locals.projectName}/${expressSer
|
|
|
96
98
|
}
|
|
97
99
|
storybookProcess.stderr.pipe(process.stderr);
|
|
98
100
|
}
|
|
101
|
+
// Return hook to shut down server gracefully
|
|
102
|
+
return {
|
|
103
|
+
close: () => {
|
|
104
|
+
const httpServerClosePromise = new Promise(resolve => {
|
|
105
|
+
httpServer.close(() => {
|
|
106
|
+
resolve(null);
|
|
107
|
+
});
|
|
108
|
+
});
|
|
109
|
+
return Promise.allSettled([httpServerClosePromise, viteServer.close()]);
|
|
110
|
+
},
|
|
111
|
+
};
|
|
99
112
|
}
|
|
100
113
|
export default createDevServer;
|
|
@@ -13,7 +13,7 @@ import { fakeRenderJsComponent } from './utils.js';
|
|
|
13
13
|
import renderLocalError from './renderLocalError.js';
|
|
14
14
|
import fetchPreviewInfo from './fetchPreviewInfo.js';
|
|
15
15
|
import { buildStaticEnvConfig, } from '@hubspot/cms-components/config';
|
|
16
|
-
const makeProxyPreviewRequest = (accountId, urlToProxy, previewKey, contentId) => {
|
|
16
|
+
export const makeProxyPreviewRequest = (accountId, urlToProxy, previewKey, contentId) => {
|
|
17
17
|
const url = new URL(urlToProxy);
|
|
18
18
|
console.log(`Proxying request to ${url.href}`, {
|
|
19
19
|
previewKey,
|
|
@@ -9,6 +9,8 @@ import proxyPageResourceRedirect from './proxyPageResourceRedirect.js';
|
|
|
9
9
|
import homeRouteHandler from './homeRouteHandler.js';
|
|
10
10
|
import { debug } from '../lib/debug.js';
|
|
11
11
|
import { VALID_PROXY_DOMAIN_SUFFIXES } from '../proxyPage/constants.js';
|
|
12
|
+
import proxyPathPageRouteHandler from './proxyPathPageRouteHandler.js';
|
|
13
|
+
import proxyPathPageResourceRedirect from './proxyPathPageResourceRedirect.js';
|
|
12
14
|
const STORYBOOK_DOMAINS = VALID_PROXY_DOMAIN_SUFFIXES.map(domain => `storybook.${domain}`);
|
|
13
15
|
const createDevServerRoutes = async (viteServer, storybookPort) => {
|
|
14
16
|
const devServerApp = Router();
|
|
@@ -25,11 +27,13 @@ const createDevServerRoutes = async (viteServer, storybookPort) => {
|
|
|
25
27
|
}));
|
|
26
28
|
}
|
|
27
29
|
devServerApp.use(viteServer.middlewares);
|
|
30
|
+
devServerApp.get('/proxy', proxyPathPageRouteHandler);
|
|
28
31
|
devServerApp.get('/partials/', linksToAllPartialsRouteHandler);
|
|
29
32
|
devServerApp.get('/partial/:partial', partialRouteHandler);
|
|
30
33
|
devServerApp.get('/modules/', linksToAllModulesRouteHandler);
|
|
31
34
|
devServerApp.get('/module/:moduleName', moduleRouteHandler);
|
|
32
35
|
devServerApp.get('/', homeRouteHandler);
|
|
36
|
+
devServerApp.get('/*', proxyPathPageResourceRedirect);
|
|
33
37
|
devServerApp.get('/*', proxyPageResourceRedirect);
|
|
34
38
|
devServerApp.get('/*', proxyPageRouteHandler);
|
|
35
39
|
return devServerApp;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import fetch from 'node-fetch';
|
|
2
|
+
const HCMS_PATH = '/_hcms/';
|
|
3
|
+
const HS_FS_PATH = '/hs-fs/';
|
|
4
|
+
const HUB_FS_PATH = '/hubfs/';
|
|
5
|
+
// From(ish) https://git.hubteam.com/HubSpot/cloudflare-workers/blob/master/worker-lib/src/Constants.ts
|
|
6
|
+
const isInternalCMSRoute = (req) => req.path.startsWith(HCMS_PATH) ||
|
|
7
|
+
req.path.startsWith(HS_FS_PATH) ||
|
|
8
|
+
req.path.startsWith(HUB_FS_PATH);
|
|
9
|
+
export default function proxyPathPageResourceRedirect(req, res, next) {
|
|
10
|
+
const isAHtmlRequest = req.accepts().includes('text/html');
|
|
11
|
+
// only proxy when referer's path is /proxy && has page query param
|
|
12
|
+
let proxyPageUrl;
|
|
13
|
+
let refererUrl;
|
|
14
|
+
try {
|
|
15
|
+
refererUrl = new URL(req.headers.referer);
|
|
16
|
+
proxyPageUrl = new URL(new URL(req.headers.referer).searchParams.get('page'));
|
|
17
|
+
}
|
|
18
|
+
catch (e) {
|
|
19
|
+
next();
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
if (!refererUrl.pathname.startsWith('/proxy')) {
|
|
23
|
+
next();
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
// handle anchor links, unless internal CMS route
|
|
27
|
+
if (isAHtmlRequest && !isInternalCMSRoute(req)) {
|
|
28
|
+
const encodedPageUrl = encodeURIComponent(`${proxyPageUrl.origin}${req.url}`);
|
|
29
|
+
res.redirect(`/proxy?page=${encodedPageUrl}`);
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
const urlToProxy = `https://${proxyPageUrl.host}${req.url}`;
|
|
33
|
+
fetch(urlToProxy)
|
|
34
|
+
.then(response => response.body.pipe(res))
|
|
35
|
+
.catch(err => console.log(err));
|
|
36
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
/* hs-eslint ignored failing-rules */
|
|
3
|
+
/* eslint-disable consistent-return */
|
|
4
|
+
import { renderOnServer } from '@hubspot/cms-js-internal';
|
|
5
|
+
import fetchContentMetadata from '../proxyPage/fetchContentMetadata.js';
|
|
6
|
+
import proxyPage from '../proxyPage/proxyPage.js';
|
|
7
|
+
import { consoleWarn } from '../proxyPage/utils.js';
|
|
8
|
+
export default async function proxyPathPageRouteHandler(request, response, next) {
|
|
9
|
+
const { viteServer, webRoot, devServerHost, devServerPort, projectName, projectComponentPath, } = request.app.locals;
|
|
10
|
+
if (!request.accepts().includes('text/html')) {
|
|
11
|
+
return response.sendStatus(404);
|
|
12
|
+
}
|
|
13
|
+
// render proxy index page if not supplied with query param
|
|
14
|
+
if (!request.query.page) {
|
|
15
|
+
const { html: responseHtml } = await renderOnServer(_jsxs("div", { children: [_jsx("h2", { children: "Local Proxy" }), _jsxs("form", { action: "/proxy", children: [_jsx("label", { htmlFor: "page", style: { display: 'block' }, children: "Page URL" }), _jsx("input", { style: { width: '300px' }, name: "page", type: "text", placeholder: "https://somecmspage.com/path" }), _jsx("button", { style: { display: 'block' }, children: "Proxy" })] })] }), null, null);
|
|
16
|
+
response.status(200).set({ 'Content-Type': 'text/html' }).end(responseHtml);
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
// parse proxy page URL from query param
|
|
20
|
+
let proxyPageUrl;
|
|
21
|
+
try {
|
|
22
|
+
proxyPageUrl = new URL(request.query.page);
|
|
23
|
+
}
|
|
24
|
+
catch (e) {
|
|
25
|
+
const message = 'Please provide a valid page query parameter, e.g., http://localhost:3000/proxy?page=https://yourdomain.com/path';
|
|
26
|
+
consoleWarn(message);
|
|
27
|
+
response.status(400).send(message);
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
// validate proxy page URL query params
|
|
31
|
+
if (proxyPageUrl.searchParams.has('hs_preview')) {
|
|
32
|
+
const message = `Can't make a proxy request, you cannot proxy URLs that include internal query params like hs_preview`;
|
|
33
|
+
consoleWarn(message);
|
|
34
|
+
return response.status(400).send(message);
|
|
35
|
+
}
|
|
36
|
+
try {
|
|
37
|
+
const { portalId, contentId, hublet } = await fetchContentMetadata(proxyPageUrl.href);
|
|
38
|
+
await proxyPage(response, portalId, hublet, contentId, webRoot, viteServer, devServerHost, devServerPort, projectName, projectComponentPath, proxyPageUrl.href, proxyPageUrl.href);
|
|
39
|
+
}
|
|
40
|
+
catch (e) {
|
|
41
|
+
next(e);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { Router } from 'express';
|
|
2
|
+
import { getAccountId } from '@hubspot/cli-lib/lib/config.js';
|
|
3
|
+
import cors from 'cors';
|
|
4
|
+
import { findLocalRenderPlaceholders } from '../lib/proxyHelpers.js';
|
|
5
|
+
import { makeProxyPreviewRequest } from '../proxyPage/proxyPage.js';
|
|
6
|
+
import fetchPreviewInfo from '../proxyPage/fetchPreviewInfo.js';
|
|
7
|
+
function getShadowDevServerCheckHandler(devServerPort) {
|
|
8
|
+
return async function shadowDevServerCheckHandler(request, response) {
|
|
9
|
+
const { query } = request;
|
|
10
|
+
if (query) {
|
|
11
|
+
const { hostName, pathName, protocol, contentId, portalId: portalIdString, } = query;
|
|
12
|
+
const pageUrl = `${protocol}//${hostName}${pathName}`;
|
|
13
|
+
const portalId = parseInt(portalIdString, 10);
|
|
14
|
+
let accountId;
|
|
15
|
+
let hasJSBuildingBlocks = false;
|
|
16
|
+
let localProxyUrl;
|
|
17
|
+
try {
|
|
18
|
+
accountId = getAccountId(portalId);
|
|
19
|
+
}
|
|
20
|
+
catch (e) {
|
|
21
|
+
response
|
|
22
|
+
.status(500)
|
|
23
|
+
.end(`Unable to find account for hubid = ${portalId}. Please check your hubspot.config.yaml`);
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
try {
|
|
27
|
+
const previewInfo = await fetchPreviewInfo(accountId, portalId, contentId);
|
|
28
|
+
const { previewKey } = previewInfo;
|
|
29
|
+
const pageHtml = await makeProxyPreviewRequest(accountId, pageUrl, previewKey, contentId);
|
|
30
|
+
const localRenderPlaceholders = findLocalRenderPlaceholders(pageHtml);
|
|
31
|
+
if (localRenderPlaceholders.length) {
|
|
32
|
+
hasJSBuildingBlocks = true;
|
|
33
|
+
localProxyUrl = `http://${hostName}.hslocal.net:${devServerPort}${pathName}`;
|
|
34
|
+
}
|
|
35
|
+
response
|
|
36
|
+
.status(200)
|
|
37
|
+
.set({ 'Content-Type': 'application/json' })
|
|
38
|
+
.json({ hasJSBuildingBlocks, localProxyUrl });
|
|
39
|
+
}
|
|
40
|
+
catch (error) {
|
|
41
|
+
response
|
|
42
|
+
.status(500)
|
|
43
|
+
.end(`Failed proxy render of page id = ${contentId} hub id = ${portalId}\n\n${error.stack}`);
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
else {
|
|
48
|
+
response
|
|
49
|
+
.status(500)
|
|
50
|
+
.end(`Missing pageUrl param, cannot check for proxied page without a pageUrl`);
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
export default async function createShadowDevServerRoutes(devServerPort) {
|
|
55
|
+
const shadowDevServerRoutes = Router();
|
|
56
|
+
shadowDevServerRoutes.get('/check-if-local-dev-server', cors(), getShadowDevServerCheckHandler(devServerPort));
|
|
57
|
+
return shadowDevServerRoutes;
|
|
58
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import net from 'net';
|
|
2
|
+
import express from 'express';
|
|
3
|
+
import createShadowDevServerRoutes from './createShadowDevServerRoutes.js';
|
|
4
|
+
const SHADOW_PORT = 1442;
|
|
5
|
+
const testNetServer = net.createServer();
|
|
6
|
+
export default async function startShadowDevServer(devServerPort) {
|
|
7
|
+
const portIsTaken = await new Promise((res, rej) => {
|
|
8
|
+
console.log(`Starting proxy link server on port ${SHADOW_PORT}`);
|
|
9
|
+
testNetServer.once('error', err => {
|
|
10
|
+
if (err['code'] === 'EADDRINUSE') {
|
|
11
|
+
console.error(`Port ${SHADOW_PORT} is in use. HubSpot is unable to automatically create proxy links in the Sprocket Menu`);
|
|
12
|
+
res(true);
|
|
13
|
+
}
|
|
14
|
+
else {
|
|
15
|
+
rej(err);
|
|
16
|
+
}
|
|
17
|
+
});
|
|
18
|
+
testNetServer.once('listening', () => {
|
|
19
|
+
testNetServer.close();
|
|
20
|
+
});
|
|
21
|
+
testNetServer.once('close', () => {
|
|
22
|
+
res(false);
|
|
23
|
+
});
|
|
24
|
+
testNetServer.listen(SHADOW_PORT);
|
|
25
|
+
});
|
|
26
|
+
if (portIsTaken) {
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
const shadowDevServer = express();
|
|
30
|
+
shadowDevServer.listen(SHADOW_PORT);
|
|
31
|
+
shadowDevServer.use('/', await createShadowDevServerRoutes(devServerPort));
|
|
32
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hubspot/cms-dev-server",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.4",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
@@ -8,21 +8,24 @@
|
|
|
8
8
|
},
|
|
9
9
|
"scripts": {
|
|
10
10
|
"build": "tsc --build && yarn run copy-html && yarn build-types && node scripts/build.mjs",
|
|
11
|
+
"build:watch": "nodemon --exec 'yarn build'",
|
|
11
12
|
"copy-html": "copyfiles -u 1 'src/html/**' 'dist'",
|
|
12
13
|
"build-types": "tsc --project tsconfig.types.json",
|
|
13
14
|
"clean": "tsc --build --clean",
|
|
14
|
-
"dev": "nodemon --exec '
|
|
15
|
-
"dev-inspect": "nodemon --exec '
|
|
15
|
+
"dev": "nodemon --exec 'yarn start'",
|
|
16
|
+
"dev-inspect": "nodemon --exec 'yarn start-inspect'",
|
|
16
17
|
"es-watch": "esbuild --outfile=dist-esbuild/runWhenDeveloping.cjs --bundle --watch --platform=node --external:fsevents --external:vite --external:react-refresh runWhenDeveloping.ts",
|
|
17
18
|
"package": "npm run build && npm pack",
|
|
18
19
|
"prepare": "npm run build",
|
|
19
20
|
"runBuild": "node dist/run.js",
|
|
21
|
+
"start": "ts-node-esm --transpileOnly runWhenDeveloping.ts",
|
|
22
|
+
"start-inspect": "node --loader ts-node/esm --inspect runWhenDeveloping.ts",
|
|
20
23
|
"test": "NODE_ENV=test vitest run"
|
|
21
24
|
},
|
|
22
25
|
"dependencies": {
|
|
23
26
|
"@hubspot/cli-lib": "^3.0.12",
|
|
24
|
-
"@hubspot/cms-components": "^0.3.
|
|
25
|
-
"@hubspot/cms-js-internal": "^0.3.
|
|
27
|
+
"@hubspot/cms-components": "^0.3.4",
|
|
28
|
+
"@hubspot/cms-js-internal": "^0.3.4",
|
|
26
29
|
"@storybook/addon-actions": "^7.0.0-beta.19",
|
|
27
30
|
"@storybook/addon-essentials": "^7.0.0-beta.19",
|
|
28
31
|
"@storybook/addon-interactions": "^7.0.0-beta.19",
|
|
@@ -33,6 +36,7 @@
|
|
|
33
36
|
"@storybook/testing-library": "^0.0.13",
|
|
34
37
|
"@vitejs/plugin-react": "^3.0.0",
|
|
35
38
|
"ansi-to-html": "0.7.x",
|
|
39
|
+
"cors": "^2.8.5",
|
|
36
40
|
"debug": "^4.3.4",
|
|
37
41
|
"express": "^4.17.3",
|
|
38
42
|
"http-proxy-middleware": "^2.0.6",
|
|
@@ -44,17 +48,19 @@
|
|
|
44
48
|
"vite-plugin-inspect": "^0.4.3"
|
|
45
49
|
},
|
|
46
50
|
"devDependencies": {
|
|
51
|
+
"@testing-library/dom": "^8.20.0",
|
|
47
52
|
"@types/express": "^4.17.13",
|
|
48
53
|
"@types/react": "^18.0.0",
|
|
49
54
|
"@types/react-dom": "^18.0.0",
|
|
50
55
|
"copyfiles": "^2.4.1",
|
|
56
|
+
"node-fetch": "^3.3.0",
|
|
51
57
|
"nodemon": "^2.0.15",
|
|
52
58
|
"ts-node": "^10.7.0",
|
|
53
59
|
"vitest": "0.25.8"
|
|
54
60
|
},
|
|
55
|
-
"main": "./dist/
|
|
61
|
+
"main": "./dist/createDevServer.js",
|
|
56
62
|
"exports": {
|
|
57
|
-
".": "./dist/
|
|
63
|
+
".": "./dist/createDevServer.js",
|
|
58
64
|
"./storybook": {
|
|
59
65
|
"types": "./types/storybook/index.d.ts",
|
|
60
66
|
"default": "./dist/storybook/index.js"
|
|
@@ -85,5 +91,5 @@
|
|
|
85
91
|
"publishConfig": {
|
|
86
92
|
"access": "public"
|
|
87
93
|
},
|
|
88
|
-
"gitHead": "
|
|
94
|
+
"gitHead": "ca8cb386c6ffeea1474ef31811be525db2c69c97"
|
|
89
95
|
}
|