-
Notifications
You must be signed in to change notification settings - Fork 138
/
preview-server.ts
159 lines (144 loc) · 5 KB
/
preview-server.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import { compile } from 'handlebars';
import * as colorette from 'colorette';
import { getPort } from 'get-port-please';
import { readFileSync, promises as fsPromises } from 'fs';
import * as path from 'path';
import { startHttpServer, startWsServer, respondWithGzip, mimeTypes } from './server';
import { isSubdir } from '../../../utils/miscellaneous';
import type { IncomingMessage } from 'http';
function getPageHTML(
htmlTemplate: string,
redocOptions: object = {},
useRedocPro: boolean,
wsPort: number,
host: string
) {
let templateSrc = readFileSync(htmlTemplate, 'utf-8');
// fix template for backward compatibility
templateSrc = templateSrc
.replace(/{?{{redocHead}}}?/, '{{{redocHead}}}')
.replace('{{redocBody}}', '{{{redocHTML}}}');
const template = compile(templateSrc);
return template({
redocHead: `
<script>
window.__REDOC_EXPORT = '${useRedocPro ? 'RedoclyReferenceDocs' : 'Redoc'}';
window.__OPENAPI_CLI_WS_PORT = ${wsPort};
window.__OPENAPI_CLI_WS_HOST = "${host}";
</script>
<script src="/simplewebsocket.min.js"></script>
<script src="/hot.js"></script>
<script src="${
useRedocPro
? 'https://cdn.redoc.ly/reference-docs/latest/redocly-reference-docs.min.js'
: 'https://cdn.redoc.ly/redoc/latest/bundles/redoc.standalone.js'
}"></script>
`,
redocHTML: `
<div id="redoc"></div>
<script>
var container = document.getElementById('redoc');
${
useRedocPro
? "window[window.__REDOC_EXPORT].setPublicPath('https://cdn.redoc.ly/reference-docs/latest/');"
: ''
}
window[window.__REDOC_EXPORT].init("/openapi.json", ${JSON.stringify(redocOptions)}, container)
</script>`,
});
}
export default async function startPreviewServer(
port: number,
host: string,
{
getBundle,
getOptions,
useRedocPro,
}: // eslint-disable-next-line @typescript-eslint/ban-types
{ getBundle: Function; getOptions: Function; useRedocPro: boolean }
) {
const defaultTemplate = path.join(__dirname, 'default.hbs');
const handler = async (request: IncomingMessage, response: any) => {
console.time(colorette.dim(`GET ${request.url}`));
const { htmlTemplate } = getOptions() || {};
if (request.url?.endsWith('/') || path.extname(request.url!) === '') {
respondWithGzip(
getPageHTML(htmlTemplate || defaultTemplate, getOptions(), useRedocPro, wsPort, host),
request,
response,
{
'Content-Type': 'text/html',
}
);
} else if (request.url === '/openapi.json') {
const bundle = await getBundle();
if (bundle === undefined) {
respondWithGzip(
JSON.stringify({
openapi: '3.0.0',
info: {
description:
'<code> Failed to generate bundle: check out console output for more details </code>',
},
paths: {},
}),
request,
response,
{
'Content-Type': 'application/json',
}
);
} else {
respondWithGzip(JSON.stringify(bundle), request, response, {
'Content-Type': 'application/json',
});
}
} else {
let filePath =
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
{
'/hot.js': path.join(__dirname, 'hot.js'),
'/oauth2-redirect.html': path.join(__dirname, 'oauth2-redirect.html'),
'/simplewebsocket.min.js': require.resolve('simple-websocket/simplewebsocket.min.js'),
}[request.url || ''];
if (!filePath) {
const basePath = htmlTemplate ? path.dirname(htmlTemplate) : process.cwd();
filePath = path.resolve(basePath, `.${request.url}`);
if (!isSubdir(basePath, filePath)) {
respondWithGzip('404 Not Found', request, response, { 'Content-Type': 'text/html' }, 404);
console.timeEnd(colorette.dim(`GET ${request.url}`));
return;
}
}
const extname = String(path.extname(filePath)).toLowerCase() as keyof typeof mimeTypes;
const contentType = mimeTypes[extname] || 'application/octet-stream';
try {
respondWithGzip(await fsPromises.readFile(filePath), request, response, {
'Content-Type': contentType,
});
} catch (e) {
if (e.code === 'ENOENT') {
respondWithGzip('404 Not Found', request, response, { 'Content-Type': 'text/html' }, 404);
} else {
respondWithGzip(
`Something went wrong: ${e.code || e.message}...\n`,
request,
response,
{},
500
);
}
}
}
console.timeEnd(colorette.dim(`GET ${request.url}`));
};
const wsPort = await getPort({ port: 32201, portRange: [32201, 32301], host });
const server = startHttpServer(port, host, handler);
server.on('listening', () => {
process.stdout.write(
`\n 🔎 Preview server running at ${colorette.blue(`http://${host}:${port}\n`)}`
);
});
return startWsServer(wsPort, host);
}