@mintlify/previewing 4.0.1037 → 4.0.1039

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.
@@ -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;
@@ -46,6 +77,45 @@ export const run = async (argv) => {
46
77
  res.status(502).json({ error: 'search proxy failed' });
47
78
  }
48
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
+ });
49
119
  app.all('*', (req, res) => requestHandler(req, res));
50
120
  const onChange = () => {
51
121
  io.emit('reload');