Skip to content

Commit

Permalink
fix: default to nodes config for global ca
Browse files Browse the repository at this point in the history
  • Loading branch information
DylanPiercey committed Feb 26, 2024
1 parent bea6faa commit 9c1d247
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 52 deletions.
73 changes: 47 additions & 26 deletions src/node_modules/@internal/micro-frame-component/node.marko
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import path from "path";
import https from "https";
import fetch from "make-fetch-happen";

static const { ca } = https.globalAgent.options;
static const cachePath = path.resolve("node_modules/.cache/fetch");
static const strictSSL = process.env.NODE_TLS_REJECT_UNAUTHORIZED !== "0";

static function internalFetch(url, options) {
return fetch(url, { ...options, ca, cachePath, strictSSL });
}
static async function fetchStream(input, out) {
const global = out.global;
let origin;
Expand All @@ -12,36 +15,43 @@ static async function fetchStream(input, out) {
if (global.platform) {
const url = global.url;
if (!url) {
throw new Error("Please assign $global.platform.url with WHATWG URL object compatible with @marko/run.");
throw new Error(
"Please assign $global.platform.url with WHATWG URL object compatible with @marko/run.",
);
}
const request = global.request;
if (!request || !request.headers) {
throw new Error("Please assign $global.platform.request with WHATWG request object compatible with @marko/run.");
throw new Error(
"Please assign $global.platform.request with WHATWG request object compatible with @marko/run.",
);
}
incomingHeaders = Object.fromEntries(request.headers);
const forwardedProto = request.headers.get("x-forwarded-proto");
const forwardedHost = request.headers.get("x-forwarded-host");
origin = forwardedHost && forwardedProto ? `${forwardedProto}://${forwardedHost}` : url.origin;
origin =
forwardedHost && forwardedProto
? `${forwardedProto}://${forwardedHost}`
: url.origin;
} else {
const incomingMessage = out.stream && (out.stream.req || out.stream.request) || out.global.req || out.global.request;
const incomingMessage =
(out.stream && (out.stream.req || out.stream.request)) ||
out.global.req ||
out.global.request;
if (!incomingMessage) {
throw new Error("Could not get request from stream/global. Please assign out.global.req with proper request object.");
throw new Error(
"Could not get request from stream/global. Please assign out.global.req with proper request object.",
);
}
incomingHeaders = incomingMessage.headers;
const protocol =
incomingHeaders["x-forwarded-proto"] ||
incomingMessage.protocol;
const host =
incomingHeaders["x-forwarded-host"] ||
incomingHeaders.host;
incomingHeaders["x-forwarded-proto"] || incomingMessage.protocol;
const host = incomingHeaders["x-forwarded-host"] || incomingHeaders.host;
origin = `${protocol}://${host}`;
}
const url = new URL(input.src, origin);
const { cache } = input;
const headers = {
Expand All @@ -51,14 +61,21 @@ static async function fetchStream(input, out) {
};
const res = await (input.fetch
? input.fetch(url, { cache, headers }, fetch)
? input.fetch(
url,
{
cache,
headers,
},
internalFetch,
)
: fetch(url, {
cache,
headers,
cachePath,
strictSSL,
})
);
cache,
headers,
ca,
cachePath,
strictSSL,
}));
if (!res.ok) throw new Error(res.statusText);
Expand All @@ -72,10 +89,8 @@ static async function fetchStream(input, out) {
<div id=component.id class=input.class style=input.style data-src=input.src>
<if(input.loading)>
<${input.loading}/>
<!-- output a comment used as a marker to detect where the loading content starts so it can be removed -->
$!{`<!--${component.id}-->`}
<!-- output a comment used as a marker to detect where the loading content starts so it can be removed -->$!{`<!--${component.id}-->`}
</if>

<!--
We put the streamed html in a preserved fragment.
This allows Marko to avoid diffing that section.
Expand All @@ -89,7 +104,9 @@ static async function fetchStream(input, out) {
<if(done)>
<if(input.loading)>
<!-- Remove all of the <@loading> content after we've received all the data -->
$ out.script(`((e,t,d)=>{t=document.getElementById(e);do{t.removeChild(d=t.firstChild)}while(d.data!==e)})(${JSON.stringify(component.id)});`);
$ out.script(
`((e,t,d)=>{t=document.getElementById(e);do{t.removeChild(d=t.firstChild)}while(d.data!==e)})(${JSON.stringify(component.id)});`,
);
</if>
</if>
<else>
Expand All @@ -100,7 +117,11 @@ static async function fetchStream(input, out) {
<@catch|err|>
<if(input.catch)>
<!-- Remove everything in the container and render our catch handler -->
<script>document.getElementById(${JSON.stringify(component.id)}).textContent=""</script>
<script>
document.getElementById(
${JSON.stringify(component.id)},
).textContent = "";
</script>
<${input.catch}(err)/>
</if>
<else>
Expand Down
76 changes: 50 additions & 26 deletions src/node_modules/@internal/stream-source-component/node.marko
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import fetch from "make-fetch-happen";
import https from "https";
import path from "path";
import { getSource } from "../../../util/stream";

static const { ca } = https.globalAgent.options;
static const cachePath = path.resolve("node_modules/.cache/fetch");

static const strictSSL = process.env.NODE_TLS_REJECT_UNAUTHORIZED !== "0";
static function internalFetch(url, options) {
return fetch(url, { ...options, ca, cachePath, strictSSL });
}
$ const request = async () => {
const global = out.global;
let origin;
Expand All @@ -12,58 +16,79 @@ $ const request = async () => {
if (global.platform) {
const url = global.url;
if (!url) {
throw new Error("Please assign $global.platform.url with WHATWG URL object compatible with @marko/run.");
throw new Error(
"Please assign $global.platform.url with WHATWG URL object compatible with @marko/run.",
);
}
const request = global.request;
if (!request || !request.headers) {
throw new Error("Please assign $global.platform.request with WHATWG request object compatible with @marko/run.");
throw new Error(
"Please assign $global.platform.request with WHATWG request object compatible with @marko/run.",
);
}
incomingHeaders = Object.fromEntries(request.headers);
const forwardedProto = request.headers.get("x-forwarded-proto");
const forwardedHost = request.headers.get("x-forwarded-host");
origin = forwardedHost && forwardedProto ? `${forwardedProto}://${forwardedHost}` : url.origin;
origin =
forwardedHost && forwardedProto
? `${forwardedProto}://${forwardedHost}`
: url.origin;
} else {
const incomingMessage = out.stream && (out.stream.req || out.stream.request) || out.global.req || out.global.request;
const incomingMessage =
(out.stream && (out.stream.req || out.stream.request)) ||
out.global.req ||
out.global.request;
if (!incomingMessage) {
throw new Error("Could not get request from stream/global. Please assign out.global.req with proper request object.");
throw new Error(
"Could not get request from stream/global. Please assign out.global.req with proper request object.",
);
}
incomingHeaders = incomingMessage.headers;
const protocol =
incomingHeaders["x-forwarded-proto"] ||
incomingMessage.protocol;
const host =
incomingHeaders["x-forwarded-host"] ||
incomingHeaders.host;
incomingHeaders["x-forwarded-proto"] || incomingMessage.protocol;
const host = incomingHeaders["x-forwarded-host"] || incomingHeaders.host;
origin = `${protocol}://${host}`;
}
const url = new URL(input.src, origin);
const res = await (input.fetch || fetch)(url, {
cachePath,
cache: input.cache,
strictSSL: process.env.NODE_TLS_REJECT_UNAUTHORIZED !== "0",
headers: {
...incomingHeaders,
...input.headers
}
})
const { cache } = input;
const headers = {
...incomingHeaders,
...input.headers,
};
const res = await (input.fetch
? input.fetch(
url,
{
cache,
headers,
},
internalFetch,
)
: fetch(url, {
cache,
headers,
ca,
cachePath,
strictSSL,
}));
if (!res.ok) throw new Error(res.statusText);
return res;
}
};
$ const streamSource = getSource(input.name, out);

<div id=component.id data-src=input.src>
$ out.bf("@_", component, true);
<await(request()) client-reorder timeout=input.timeout>
<@then|{ body }|>
<await(streamSource.run(input.parser(body[Symbol.asyncIterator]()))) client-reorder>
<await(
streamSource.run(input.parser(body[Symbol.asyncIterator]())),
) client-reorder>
<@catch|err|>
$ streamSource.close(err);
</@catch>
Expand All @@ -75,4 +100,3 @@ $ const streamSource = getSource(input.name, out);
</await>
$ out.ef();
</div>

0 comments on commit 9c1d247

Please sign in to comment.