mirror of
https://github.com/dillonkearns/elm-pages-v3-beta.git
synced 2024-12-26 13:21:42 +03:00
Resolve port-data-source functions directly instead of writing them to cache.
This commit is contained in:
parent
e0d949c008
commit
91d009e70d
@ -53,165 +53,152 @@ function lookupOrPerform(portsFile, mode, rawRequest, hasFsAccess, useCache) {
|
||||
const portsHash = (portsFile && portsFile.match(/-([^-]+)\.js$/)[1]) || "";
|
||||
const responsePath = fullPath(portsHash, request, hasFsAccess);
|
||||
|
||||
// TODO check cache expiration time and delete and go to else if expired
|
||||
if (useCache && (await checkFileExists(fs, responsePath))) {
|
||||
// console.log("Skipping request, found file.");
|
||||
resolve({ kind: "cache-response-path", value: responsePath });
|
||||
} else {
|
||||
let portBackendTask = {};
|
||||
let portBackendTaskImportError = null;
|
||||
try {
|
||||
if (portsFile === undefined) {
|
||||
throw "missing";
|
||||
}
|
||||
const portBackendTaskPath = path.resolve(portsFile);
|
||||
// On Windows, we need cannot use paths directly and instead must use a file:// URL.
|
||||
// portBackendTask = await require(url.pathToFileURL(portBackendTaskPath).href);
|
||||
portBackendTask = require(portBackendTaskPath);
|
||||
} catch (e) {
|
||||
portBackendTaskImportError = e;
|
||||
let portBackendTask = {};
|
||||
let portBackendTaskImportError = null;
|
||||
try {
|
||||
if (portsFile === undefined) {
|
||||
throw "missing";
|
||||
}
|
||||
const portBackendTaskPath = path.resolve(portsFile);
|
||||
// On Windows, we need cannot use paths directly and instead must use a file:// URL.
|
||||
// portBackendTask = await require(url.pathToFileURL(portBackendTaskPath).href);
|
||||
portBackendTask = require(portBackendTaskPath);
|
||||
} catch (e) {
|
||||
portBackendTaskImportError = e;
|
||||
}
|
||||
|
||||
if (request.url === "elm-pages-internal://port") {
|
||||
try {
|
||||
const { input, portName } = rawRequest.body.args[0];
|
||||
if (request.url === "elm-pages-internal://port") {
|
||||
try {
|
||||
const { input, portName } = rawRequest.body.args[0];
|
||||
|
||||
if (!portBackendTask[portName]) {
|
||||
if (portBackendTaskImportError === null) {
|
||||
resolve({
|
||||
kind: "response-json",
|
||||
value: jsonResponse({
|
||||
"elm-pages-internal-error": "PortNotDefined",
|
||||
}),
|
||||
});
|
||||
} else if (portBackendTaskImportError === "missing") {
|
||||
resolve({
|
||||
kind: "response-json",
|
||||
value: jsonResponse({
|
||||
"elm-pages-internal-error": "MissingPortsFile",
|
||||
}),
|
||||
});
|
||||
} else {
|
||||
resolve({
|
||||
kind: "response-json",
|
||||
value: jsonResponse({
|
||||
"elm-pages-internal-error": "ErrorInPortsFile",
|
||||
error:
|
||||
(portBackendTaskImportError &&
|
||||
portBackendTaskImportError.stack) ||
|
||||
"",
|
||||
}),
|
||||
});
|
||||
}
|
||||
} else if (typeof portBackendTask[portName] !== "function") {
|
||||
if (!portBackendTask[portName]) {
|
||||
if (portBackendTaskImportError === null) {
|
||||
resolve({
|
||||
kind: "response-json",
|
||||
value: jsonResponse({
|
||||
"elm-pages-internal-error": "ExportIsNotFunction",
|
||||
error: typeof portBackendTask[portName],
|
||||
"elm-pages-internal-error": "PortNotDefined",
|
||||
}),
|
||||
});
|
||||
} else if (portBackendTaskImportError === "missing") {
|
||||
resolve({
|
||||
kind: "response-json",
|
||||
value: jsonResponse({
|
||||
"elm-pages-internal-error": "MissingPortsFile",
|
||||
}),
|
||||
});
|
||||
} else {
|
||||
try {
|
||||
const portFunctionResult = await portBackendTask[portName](input);
|
||||
await fs.promises.writeFile(
|
||||
responsePath,
|
||||
JSON.stringify(jsonResponse(portFunctionResult))
|
||||
);
|
||||
resolve({
|
||||
kind: "cache-response-path",
|
||||
value: responsePath,
|
||||
});
|
||||
} catch (portCallError) {
|
||||
resolve({
|
||||
kind: "response-json",
|
||||
value: jsonResponse({
|
||||
"elm-pages-internal-error": "PortCallError",
|
||||
error: portCallError,
|
||||
}),
|
||||
});
|
||||
}
|
||||
resolve({
|
||||
kind: "response-json",
|
||||
value: jsonResponse({
|
||||
"elm-pages-internal-error": "ErrorInPortsFile",
|
||||
error:
|
||||
(portBackendTaskImportError &&
|
||||
portBackendTaskImportError.stack) ||
|
||||
"",
|
||||
}),
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
console.trace(error);
|
||||
reject({
|
||||
title: "BackendTask.Port Error",
|
||||
message: error.toString(),
|
||||
});
|
||||
}
|
||||
} else {
|
||||
try {
|
||||
console.time(`fetch ${request.url}`);
|
||||
const response = await fetch(request.url, {
|
||||
method: request.method,
|
||||
body: request.body,
|
||||
headers: {
|
||||
"User-Agent": "request",
|
||||
...request.headers,
|
||||
},
|
||||
...rawRequest.cacheOptions,
|
||||
});
|
||||
|
||||
console.timeEnd(`fetch ${request.url}`);
|
||||
const expectString = request.headers["elm-pages-internal"];
|
||||
|
||||
let body;
|
||||
let bodyKind;
|
||||
if (expectString === "ExpectJson") {
|
||||
try {
|
||||
body = await response.buffer();
|
||||
body = JSON.parse(body.toString("utf-8"));
|
||||
bodyKind = "json";
|
||||
} catch (error) {
|
||||
body = body.toString("utf8");
|
||||
bodyKind = "string";
|
||||
}
|
||||
} else if (
|
||||
expectString === "ExpectBytes" ||
|
||||
expectString === "ExpectBytesResponse"
|
||||
) {
|
||||
body = await response.buffer();
|
||||
try {
|
||||
body = body.toString("base64");
|
||||
bodyKind = "bytes";
|
||||
} catch (e) {
|
||||
body = body.toString("utf8");
|
||||
bodyKind = "string";
|
||||
}
|
||||
} else if (expectString === "ExpectWhatever") {
|
||||
bodyKind = "whatever";
|
||||
body = null;
|
||||
} else if (
|
||||
expectString === "ExpectResponse" ||
|
||||
expectString === "ExpectString"
|
||||
) {
|
||||
bodyKind = "string";
|
||||
body = await response.text();
|
||||
} else {
|
||||
throw `Unexpected expectString ${expectString}`;
|
||||
}
|
||||
|
||||
} else if (typeof portBackendTask[portName] !== "function") {
|
||||
resolve({
|
||||
kind: "response-json",
|
||||
value: {
|
||||
headers: Object.fromEntries(response.headers.entries()),
|
||||
statusCode: response.status,
|
||||
body,
|
||||
bodyKind,
|
||||
url: response.url,
|
||||
statusText: response.statusText,
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
console.trace("@@@ request-cache2 HTTP error", error);
|
||||
reject({
|
||||
title: "BackendTask.Http Error",
|
||||
message: `${kleur
|
||||
.yellow()
|
||||
.underline(request.url)} ${error.toString()}
|
||||
`,
|
||||
value: jsonResponse({
|
||||
"elm-pages-internal-error": "ExportIsNotFunction",
|
||||
error: typeof portBackendTask[portName],
|
||||
}),
|
||||
});
|
||||
} else {
|
||||
try {
|
||||
resolve({
|
||||
kind: "response-json",
|
||||
value: jsonResponse(await portBackendTask[portName](input)),
|
||||
});
|
||||
} catch (portCallError) {
|
||||
resolve({
|
||||
kind: "response-json",
|
||||
value: jsonResponse({
|
||||
"elm-pages-internal-error": "PortCallError",
|
||||
error: portCallError,
|
||||
}),
|
||||
});
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.trace(error);
|
||||
reject({
|
||||
title: "BackendTask.Port Error",
|
||||
message: error.toString(),
|
||||
});
|
||||
}
|
||||
} else {
|
||||
try {
|
||||
console.time(`fetch ${request.url}`);
|
||||
const response = await fetch(request.url, {
|
||||
method: request.method,
|
||||
body: request.body,
|
||||
headers: {
|
||||
"User-Agent": "request",
|
||||
...request.headers,
|
||||
},
|
||||
...rawRequest.cacheOptions,
|
||||
});
|
||||
|
||||
console.timeEnd(`fetch ${request.url}`);
|
||||
const expectString = request.headers["elm-pages-internal"];
|
||||
|
||||
let body;
|
||||
let bodyKind;
|
||||
if (expectString === "ExpectJson") {
|
||||
try {
|
||||
body = await response.buffer();
|
||||
body = JSON.parse(body.toString("utf-8"));
|
||||
bodyKind = "json";
|
||||
} catch (error) {
|
||||
body = body.toString("utf8");
|
||||
bodyKind = "string";
|
||||
}
|
||||
} else if (
|
||||
expectString === "ExpectBytes" ||
|
||||
expectString === "ExpectBytesResponse"
|
||||
) {
|
||||
body = await response.buffer();
|
||||
try {
|
||||
body = body.toString("base64");
|
||||
bodyKind = "bytes";
|
||||
} catch (e) {
|
||||
body = body.toString("utf8");
|
||||
bodyKind = "string";
|
||||
}
|
||||
} else if (expectString === "ExpectWhatever") {
|
||||
bodyKind = "whatever";
|
||||
body = null;
|
||||
} else if (
|
||||
expectString === "ExpectResponse" ||
|
||||
expectString === "ExpectString"
|
||||
) {
|
||||
bodyKind = "string";
|
||||
body = await response.text();
|
||||
} else {
|
||||
throw `Unexpected expectString ${expectString}`;
|
||||
}
|
||||
|
||||
resolve({
|
||||
kind: "response-json",
|
||||
value: {
|
||||
headers: Object.fromEntries(response.headers.entries()),
|
||||
statusCode: response.status,
|
||||
body,
|
||||
bodyKind,
|
||||
url: response.url,
|
||||
statusText: response.statusText,
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
console.trace("@@@ request-cache2 HTTP error", error);
|
||||
reject({
|
||||
title: "BackendTask.Http Error",
|
||||
message: `${kleur.yellow().underline(request.url)} ${error.toString()}
|
||||
`,
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user