Skip to content

Use same-origin resources for compiler bundles. #1038

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

Merged
merged 2 commits into from
May 18, 2025
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
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
*.res linguist-language=ReScript
*.resi linguist-language=ReScript

/public/playground-bundles/** binary linguist-vendored
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,12 @@ lib/
.vercel

src/**/*.mjs
scripts/**/*.mjs
scripts/gendocs.mjs
scripts/generate_*.mjs

# Generated via generate-llms script
public/llms/manual/**/llm*.txt
public/llms/react/**/llm*.txt
pages/docs/**/**/llms.mdx

public/playground-bundles/
1 change: 1 addition & 0 deletions next.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const config = {
ENV: process.env.NODE_ENV,
VERSION_LATEST: process.env.VERSION_LATEST,
VERSION_NEXT: process.env.VERSION_NEXT,
VERCEL: process.env.VERCEL,
},
swcMinify: false,
webpack: (config, options) => {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
"test": "node scripts/test-examples.mjs && node scripts/test-hrefs.mjs",
"reanalyze": "reanalyze -all-cmt .",
"update-index": "npm run generate-llms && node scripts/extract-indices.mjs && node scripts/extract-tocs.mjs && node scripts/extract-syntax.mjs && node scripts/generate_feed.mjs > public/blog/feed.xml",
"sync-bundles": "node scripts/sync-playground-bundles.mjs",
"generate-llms": "node scripts/generate_llms.mjs",
"generate-resources": "node scripts/generate_resources.mjs"
},
Expand Down
43 changes: 43 additions & 0 deletions scripts/sync-playground-bundles.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import * as path from "node:path";
import * as fs from "node:fs";
import * as childProcess from "node:child_process";
import { Readable } from "node:stream";
import * as stream from "node:stream/promises";

const bucketUrl = new URL("https://cdn.rescript-lang.org");

const bundlesDir = path.join(import.meta.dirname, "../public/playground-bundles");
const versions = await fetch(new URL("/playground-bundles/versions.json", bucketUrl))
.then(res => res.json());

for (const version of versions) {
const versionDir = path.join(bundlesDir, version);
const compilerFile = path.join(versionDir, "compiler.js");
if (fs.existsSync(compilerFile)) {
console.log(`%s has already been synced.`, version);
continue;
}

console.group(`Syncing %s...`, version);
{
console.log(`Downloading archive file...`);
const res = await fetch(new URL(`/playground-bundles/${version}.tar.zst`, bucketUrl));
if (!res.ok) {
console.error(await res.text());
continue;
}

const archiveFile = path.join(bundlesDir, `${version}.tar.zst`);
const fileStream = fs.createWriteStream(archiveFile);
await stream.finished(Readable.fromWeb(res.body).pipe(fileStream));

console.log("Extracting archive...");
fs.mkdirSync(versionDir, { recursive: true });
childProcess.execSync(`tar --zstd -xf "${archiveFile}" -C "${versionDir}"`);

console.log("Cleaning up...");
fs.unlinkSync(archiveFile);

console.groupEnd();
}
}
24 changes: 8 additions & 16 deletions src/Try.res
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,14 @@ let default = props => {

let getStaticProps: Next.GetStaticProps.t<props, _> = async _ => {
let versions = {
let response = await Webapi.Fetch.fetch("https://cdn.rescript-lang.org/")
let text = await Webapi.Fetch.Response.text(response)
text
->String.split("\n")
->Array.filterMap(line => {
switch line->String.startsWith("<a href") {
| true =>
// Adapted from https://semver.org/
let semverRe = /v(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?/
switch RegExp.exec(semverRe, line) {
| Some(result) => RegExp.Result.fullMatch(result)->Some
| None => None
}
| false => None
}
})
let response = await Webapi.Fetch.fetch(
"https://cdn.rescript-lang.org/playground-bundles/versions.json",
)
let json = await Webapi.Fetch.Response.json(response)
json
->JSON.Decode.array
->Option.getExn
->Array.map(json => json->JSON.Decode.string->Option.getExn)
}

{"props": {versions: versions}}
Expand Down
12 changes: 8 additions & 4 deletions src/common/CompilerManagerHook.res
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,18 @@ module LoadScript = {
}

module CdnMeta = {
let getCompilerUrl = (version): string =>
`https://cdn.rescript-lang.org/${Semver.toString(version)}/compiler.js`
let baseUrl = switch Node.Process.env->Dict.get("VERCEL") {
| Some(_) => "https://cdn.rescript-lang.org"
| None => "/playground-bundles"
}

let getCompilerUrl = (version): string => `${baseUrl}/${Semver.toString(version)}/compiler.js`

let getLibraryCmijUrl = (version, libraryName: string): string =>
`https://cdn.rescript-lang.org/${Semver.toString(version)}/${libraryName}/cmij.js`
`${baseUrl}/${Semver.toString(version)}/${libraryName}/cmij.js`

let getStdlibRuntimeUrl = (version, filename) =>
`https://cdn.rescript-lang.org/${Semver.toString(version)}/compiler-builtins/stdlib/${filename}`
`${baseUrl}/${Semver.toString(version)}/compiler-builtins/stdlib/${filename}`
}

module FinalResult = {
Expand Down