Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: default to nodes config for global ca #32

Merged
merged 2 commits into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@
id="GENERATED-2"
>
<script>
document.getElementById("s0-9-0").textContent=""
(function(e){if(e)e.textContent=""})(document.getElementById("s0-9-0"))
</script>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@
id="GENERATED-3"
>
<script>
document.getElementById("s0-10-0").textContent=""
(function(e){if(e)e.textContent=""})(document.getElementById("s0-10-0"))
</script>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
id="GENERATED-5"
>
<script>
document.getElementById("s0-10-0").textContent=""
(function(e){if(e)e.textContent=""})(document.getElementById("s0-10-0"))
</script>
</div>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
id="GENERATED-6"
>
<script>
document.getElementById("s0-11-0").textContent=""
(function(e){if(e)e.textContent=""})(document.getElementById("s0-11-0"))
</script>
</div>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@
test_html for slot_1
</p>
<script>
document.getElementById("s0-9-0").textContent=""
(function(e){if(e)e.textContent=""})(document.getElementById("s0-9-0"))
</script>
</div>
69 changes: 43 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,7 @@ 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>(function(e){if(e)e.textContent=""})(document.getElementById(${JSON.stringify(component.id)}))</script>
<${input.catch}(err)/>
</if>
<else>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ $ {
</@then>
<@catch|e|>
$ finishLoading && finishLoading();
<script>document.getElementById(${JSON.stringify(component.id)}).textContent=""</script>
<script>(function(e){if(e)e.textContent=""})(document.getElementById(${JSON.stringify(component.id)}))</script>
<${input.catch}(e)/>
</@catch>
</await>
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>

Loading