wirejs-deploy-amplify-basic 0.0.17-alpha → 0.0.19-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
|
*
|
|
@@ -121,8 +134,9 @@ async function trySSRPath(req, res) {
|
|
|
121
134
|
|
|
122
135
|
try {
|
|
123
136
|
useJSDOM(JSDOM);
|
|
124
|
-
|
|
125
|
-
await
|
|
137
|
+
const self = {};
|
|
138
|
+
const moduleData = await fs.promises.readFile(srcPath);
|
|
139
|
+
eval(`${moduleData}`);
|
|
126
140
|
const module = self.exports;
|
|
127
141
|
if (typeof module.generate === 'function') {
|
|
128
142
|
const doc = await module.generate(context);
|
|
@@ -160,6 +174,60 @@ async function trySSRPath(req, res) {
|
|
|
160
174
|
return true;
|
|
161
175
|
}
|
|
162
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 handleApiResponse(req, res) {
|
|
198
|
+
if (!API_URL) {
|
|
199
|
+
logger.error('Tried to proxy without API_URL config.');
|
|
200
|
+
res.statusCode = 500;
|
|
201
|
+
res.end('500 - Internal Server Error');
|
|
202
|
+
}
|
|
203
|
+
return proxyRequest(req, res, API_URL);
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
async function proxyRequest(req, res, targetUrl) {
|
|
207
|
+
const headers = { ...req.headers };
|
|
208
|
+
const method = req.method;
|
|
209
|
+
const body = req.method === 'POST' ? await postData(req) : null;
|
|
210
|
+
|
|
211
|
+
const fetchOptions = { method, headers, body };
|
|
212
|
+
|
|
213
|
+
try {
|
|
214
|
+
const response = await fetch(targetUrl, fetchOptions);
|
|
215
|
+
const responseBody = await response.text();
|
|
216
|
+
const responseHeaders = response.headers.raw();
|
|
217
|
+
|
|
218
|
+
res.statusCode = response.status;
|
|
219
|
+
Object.keys(responseHeaders).forEach((header) => {
|
|
220
|
+
res.setHeader(header, responseHeaders[header]);
|
|
221
|
+
});
|
|
222
|
+
|
|
223
|
+
res.end(responseBody);
|
|
224
|
+
} catch (error) {
|
|
225
|
+
logger.error('Error while proxying request:', error);
|
|
226
|
+
res.statusCode = 500;
|
|
227
|
+
res.end('Internal Server Error');
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
163
231
|
/**
|
|
164
232
|
*
|
|
165
233
|
* @param {http.IncomingMessage} req
|
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");
|