wirejs-deploy-amplify-basic 0.0.18-alpha → 0.0.20-alpha
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.
|
@@ -6,7 +6,9 @@ import { JSDOM } from 'jsdom';
|
|
|
6
6
|
import { useJSDOM } from 'wirejs-dom/v2';
|
|
7
7
|
import { Context, CookieJar } from 'wirejs-resources';
|
|
8
8
|
|
|
9
|
+
|
|
9
10
|
const SSR_ROOT = path.join(path.dirname(new URL(import.meta.url).pathname), 'ssr');
|
|
11
|
+
let API_URL = undefined;
|
|
10
12
|
|
|
11
13
|
const logger = {
|
|
12
14
|
log(...items) {
|
|
@@ -23,6 +25,17 @@ const logger = {
|
|
|
23
25
|
}
|
|
24
26
|
};
|
|
25
27
|
|
|
28
|
+
try {
|
|
29
|
+
const backendConfigModule = await import('./config.js');
|
|
30
|
+
const backendConfig = backendConfigModule.default;
|
|
31
|
+
logger.log("backend config found", backendConfig);
|
|
32
|
+
if (backendConfig.apiUrl) {
|
|
33
|
+
API_URL = backendConfig.apiUrl;
|
|
34
|
+
}
|
|
35
|
+
} catch {
|
|
36
|
+
logger.log("No backend API config found.");
|
|
37
|
+
}
|
|
38
|
+
|
|
26
39
|
/**
|
|
27
40
|
* Compare two strings by length for sorting in order of increasing length.
|
|
28
41
|
*
|
|
@@ -161,6 +174,61 @@ async function trySSRPath(req, res) {
|
|
|
161
174
|
return true;
|
|
162
175
|
}
|
|
163
176
|
|
|
177
|
+
/**
|
|
178
|
+
*
|
|
179
|
+
* @param {http.IncomingMessage} request
|
|
180
|
+
* @returns
|
|
181
|
+
*/
|
|
182
|
+
async function postData(request) {
|
|
183
|
+
return new Promise((resolve, reject) => {
|
|
184
|
+
const buffer = [];
|
|
185
|
+
const timeout = setTimeout(() => {
|
|
186
|
+
reject("Post data not received.");
|
|
187
|
+
}, 5000);
|
|
188
|
+
request.on('data', data => buffer.push(data));
|
|
189
|
+
request.on('end', () => {
|
|
190
|
+
if (!timeout) return;
|
|
191
|
+
clearTimeout(timeout);
|
|
192
|
+
resolve(buffer.join(''));
|
|
193
|
+
});
|
|
194
|
+
});
|
|
195
|
+
};
|
|
196
|
+
|
|
197
|
+
async function tryAPIPath(req, res) {
|
|
198
|
+
if (!API_URL) {
|
|
199
|
+
logger.error('Tried to proxy without API_URL config.');
|
|
200
|
+
return false;
|
|
201
|
+
}
|
|
202
|
+
return proxyRequest(req, res, API_URL);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
async function proxyRequest(req, res, targetUrl) {
|
|
206
|
+
const headers = { ...req.headers };
|
|
207
|
+
const method = req.method;
|
|
208
|
+
const body = req.method === 'POST' ? await postData(req) : null;
|
|
209
|
+
|
|
210
|
+
const fetchOptions = { method, headers, body };
|
|
211
|
+
|
|
212
|
+
try {
|
|
213
|
+
const response = await fetch(targetUrl, fetchOptions);
|
|
214
|
+
const responseBody = await response.text();
|
|
215
|
+
const responseHeaders = response.headers.raw();
|
|
216
|
+
|
|
217
|
+
res.statusCode = response.status;
|
|
218
|
+
Object.keys(responseHeaders).forEach((header) => {
|
|
219
|
+
res.setHeader(header, responseHeaders[header]);
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
res.end(responseBody);
|
|
223
|
+
} catch (error) {
|
|
224
|
+
logger.error('Error while proxying request:', error);
|
|
225
|
+
res.statusCode = 500;
|
|
226
|
+
res.end('Internal Server Error');
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
return true;
|
|
230
|
+
}
|
|
231
|
+
|
|
164
232
|
/**
|
|
165
233
|
*
|
|
166
234
|
* @param {http.IncomingMessage} req
|
|
@@ -172,6 +240,7 @@ async function handleRequest(req, res) {
|
|
|
172
240
|
|
|
173
241
|
if (await trySSRScriptPath(req, res)) return;
|
|
174
242
|
if (await trySSRPath(req, res)) return;
|
|
243
|
+
if (await tryAPIPath(req, res)) return;
|
|
175
244
|
|
|
176
245
|
// if we've made it this far, we don't have what you're looking for
|
|
177
246
|
res.statusCode = '404';
|
package/build.js
CHANGED
|
@@ -158,11 +158,11 @@ if (action === 'prebuild') {
|
|
|
158
158
|
const configJSON = JSON.stringify({
|
|
159
159
|
apiUrl
|
|
160
160
|
});
|
|
161
|
+
const configJS = `const config = ${configJSON};\nexport default config;`;
|
|
162
|
+
|
|
163
|
+
await fs.promises.writeFile(path.join(PROJECT_API_DIR, 'config.js'), configJS);
|
|
164
|
+
await fs.promises.writeFile(path.join(COMPUTE_DIR, 'config.js'), configJS);
|
|
161
165
|
|
|
162
|
-
await fs.promises.writeFile(
|
|
163
|
-
path.join(PROJECT_API_DIR, 'config.js'),
|
|
164
|
-
`const config = ${configJSON};\nexport default config;`
|
|
165
|
-
);
|
|
166
166
|
console.log("inject-backend done");
|
|
167
167
|
} else if (action === 'build-hosting-artifacts') {
|
|
168
168
|
console.log("starting build-hosting-artifacts");
|