@mintlify/previewing 4.0.1036 → 4.0.1038

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.
@@ -2,4 +2,5 @@ import { ArgumentsCamelCase } from 'yargs';
2
2
  export declare const run: (argv: ArgumentsCamelCase & {
3
3
  needsUpdate: boolean;
4
4
  fileImportsMap?: Map<string, Set<string>>;
5
+ apiUrl?: string;
5
6
  }) => Promise<void>;
@@ -11,6 +11,37 @@ import listener, { initializeImportCache } from './listener/index.js';
11
11
  import { refreshTrackedReferencedFiles } from './listener/referencedFiles.js';
12
12
  import { getLocalNetworkIp } from './network.js';
13
13
  import { setupNext } from './setupNext.js';
14
+ const PROXY_RESPONSE_HEADER_DENYLIST = new Set([
15
+ 'connection',
16
+ 'keep-alive',
17
+ 'proxy-authenticate',
18
+ 'proxy-authorization',
19
+ 'te',
20
+ 'trailer',
21
+ 'transfer-encoding',
22
+ 'upgrade',
23
+ 'content-encoding',
24
+ 'content-length',
25
+ ]);
26
+ const ASSISTANT_MESSAGE_JSON_LIMIT = '5mb';
27
+ function applyUpstreamProxyHeaders(upstream, res) {
28
+ const headers = upstream.headers;
29
+ const hasGetSetCookie = typeof headers.getSetCookie === 'function';
30
+ const setCookies = hasGetSetCookie ? headers.getSetCookie() : undefined;
31
+ headers.forEach((value, key) => {
32
+ const lower = key.toLowerCase();
33
+ if (PROXY_RESPONSE_HEADER_DENYLIST.has(lower))
34
+ return;
35
+ if (lower === 'set-cookie' && hasGetSetCookie)
36
+ return;
37
+ res.setHeader(key, value);
38
+ });
39
+ if (setCookies?.length) {
40
+ for (const cookie of setCookies) {
41
+ res.appendHeader('Set-Cookie', cookie);
42
+ }
43
+ }
44
+ }
14
45
  export const run = async (argv) => {
15
46
  const port = argv.port || '3000';
16
47
  const currentPort = parseInt(port, 10) || 3000;
@@ -19,8 +50,72 @@ export const run = async (argv) => {
19
50
  const io = new SocketServer(server);
20
51
  const requestHandler = await setupNext();
21
52
  const localIp = getLocalNetworkIp();
53
+ const apiUrl = argv.apiUrl ?? process.env.MINTLIFY_API_URL ?? 'http://localhost:5000';
54
+ const accessToken = argv.accessToken;
55
+ const subdomain = argv.subdomain;
56
+ process.env.MINTLIFY_CLI_ACCESS_TOKEN = accessToken ?? '';
57
+ process.env.MINTLIFY_API_URL = apiUrl;
58
+ if (subdomain)
59
+ process.env.MINTLIFY_CLI_SUBDOMAIN = subdomain;
22
60
  // next-server is bugged, public files added after starting aren't served
23
61
  app.use('/', express.static(NEXT_PUBLIC_PATH));
62
+ app.post('/_mintlify/api-public/search/:subdomain', express.json(), async (req, res) => {
63
+ const targetSubdomain = subdomain ?? req.params.subdomain;
64
+ try {
65
+ const upstream = await fetch(`${apiUrl}/api/cli/${targetSubdomain}/search`, {
66
+ method: 'POST',
67
+ headers: {
68
+ 'Content-Type': 'application/json',
69
+ ...(accessToken ? { Authorization: `Bearer ${accessToken}` } : {}),
70
+ },
71
+ body: JSON.stringify(req.body),
72
+ });
73
+ const data = await upstream.json();
74
+ res.status(upstream.status).json(data);
75
+ }
76
+ catch {
77
+ res.status(502).json({ error: 'search proxy failed' });
78
+ }
79
+ });
80
+ app.post('/_mintlify/api-public/assistant/:subdomain/message', express.json({ limit: ASSISTANT_MESSAGE_JSON_LIMIT }), async (req, res) => {
81
+ const targetSubdomain = subdomain ?? req.params.subdomain;
82
+ try {
83
+ const upstream = await fetch(`${apiUrl}/api/cli/${targetSubdomain}/assistant/message`, {
84
+ method: 'POST',
85
+ headers: {
86
+ 'Content-Type': 'application/json',
87
+ ...(accessToken ? { Authorization: `Bearer ${accessToken}` } : {}),
88
+ },
89
+ body: JSON.stringify(req.body),
90
+ });
91
+ res.status(upstream.status);
92
+ applyUpstreamProxyHeaders(upstream, res);
93
+ if (upstream.body) {
94
+ const reader = upstream.body.getReader();
95
+ const pump = async () => {
96
+ const { done, value } = await reader.read();
97
+ if (done) {
98
+ res.end();
99
+ return;
100
+ }
101
+ res.write(value);
102
+ return pump();
103
+ };
104
+ await pump();
105
+ }
106
+ else {
107
+ res.end();
108
+ }
109
+ }
110
+ catch {
111
+ if (!res.headersSent) {
112
+ res.status(502).json({ error: 'assistant proxy failed' });
113
+ }
114
+ else {
115
+ res.destroy();
116
+ }
117
+ }
118
+ });
24
119
  app.all('*', (req, res) => requestHandler(req, res));
25
120
  const onChange = () => {
26
121
  io.emit('reload');