-
-
Notifications
You must be signed in to change notification settings - Fork 975
Open
Description
Not sure if doable, but if https://rubygems.org/downloads/ route could be allowed to make CORS requests - it would be cool. https://rubygems.org/api/ is CORS enabled.
Hoping it will allow something like this > leading to maybe future things (Opal etc...)
async function downloadGem(gemName, version = null) {
try {
// Step 1: Get gem info if version is not provided
if (!version) {
const infoResponse = await fetch(`https://rubygems.org/api/v1/gems/${gemName}.json`);
if (!infoResponse.ok) throw new Error("Failed to fetch gem info");
const info = await infoResponse.json();
version = info.version;
}
// Step 2: Construct the download URL
const gemUrl = `https://rubygems.org/downloads/${gemName}-${version}.gem`;
const response = await fetch(gemUrl);
if (!response.ok) throw new Error("Failed to download gem");
const blob = await response.blob();
// Step 3: Create a download link
const link = document.createElement("a");
link.href = URL.createObjectURL(blob);
link.download = `${gemName}-${version}.gem`;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
console.log(`Downloaded ${gemName}-${version}.gem`);
} catch (error) {
console.error("Error downloading gem:", error);
}
}
// Example usage
downloadGem("rails"); // Downloads latest version
// Or download a specific version
downloadGem("rails", "7.1.0");or for someone that is really smart - maybe even something like the below....
1. Ruby gem contains C extension
2. Compile C extension to WebAssembly (wasm)
3. Fetch gem in browser, extract Ruby files
4. Feed Ruby files to Opal
5. Feed Wasm module to JS runtime
6. Ruby code calls C functions via JS ↔ Wasm bridge
for future notes / developers if interested in last idea....
import initWabt from 'https://esm.sh/wabt';
async function compileCInBrowser() {
const wabt = await initWabt();
// Example C code
const cCode = `
#include <stdio.h>
int add(int a, int b) { return a + b; }
`;
// Step 1: Convert C -> WAT using an in-browser compiler (LLVM/Clang WASM)
// This part usually requires a WASM-compiled Clang library.
// For demonstration, we use precompiled WAT:
const wat = `
(module
(func $add (export "add") (param $a i32) (param $b i32) (result i32)
local.get $a
local.get $b
i32.add
)
)
`;
// Step 2: Compile WAT -> WASM
const wasmModule = wabt.parseWat('example.wat', wat).toBinary({}).buffer;
const wasmInstance = await WebAssembly.instantiate(wasmModule);
console.log("3 + 4 =", wasmInstance.instance.exports.add(3, 4));
}
compileCInBrowser();Cheers 👍
Metadata
Metadata
Assignees
Labels
No labels