-
Notifications
You must be signed in to change notification settings - Fork 303
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
switch to new index, perform full package installation in JavaScript
- Loading branch information
1 parent
a7783b1
commit 1400739
Showing
10 changed files
with
213 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
# Do not edit this file by hand. See docs/pyodide.md for info on how to generate it. | ||
# These variables are factored out here because they are being shared by the WORKSPACE files in | ||
# both edgeworker and workerd, as well as src/pyodide/BUILD.bazel | ||
PYODIDE_PACKAGE_BUCKET_URL = "https://pub-45d734c4145d4285b343833ee450ef38.r2.dev/20240412-experimental/" | ||
PYODIDE_GITHUB_RELEASE_URL = "https://github.com/cloudflare/pyodide-build-scripts/releases/download/20240412-experimental/" | ||
PYODIDE_LOCK_SHA256 = "db29ebb43fcd05cbc6fcba051ec7eb61a9a1bc4210353e29fdad57c6f9be1a5a" | ||
PYODIDE_PACKAGES_TAR_ZIP_SHA256 = "6579f114f007ac307c55c221f1b5018e30c95a3cc45b86a334bbbfa442c1bf1b" | ||
PYODIDE_ALL_WHEELS_ZIP_SHA256 = "f8a34a284a7bc2ffc44ae86a160423a8aaf8cbb88eca268e1ea9300a187cf3af" | ||
PYODIDE_PACKAGE_BUCKET_URL = "https://pub-45d734c4145d4285b343833ee450ef38.r2.dev/20240415-experimental/" | ||
PYODIDE_GITHUB_RELEASE_URL = "https://github.com/cloudflare/pyodide-build-scripts/releases/download/20240415-experimental/" | ||
PYODIDE_LOCK_SHA256 = "67d1a24edf4f3ab2cf85c736391c04763ff722bf3aebf9ea3469d96e5f51e1da" | ||
PYODIDE_PACKAGES_TAR_ZIP_SHA256 = "749967941204154e7ae866fe08f1216a3e5ee58ba6a3757231a5be0d9d4430f8" | ||
PYODIDE_ALL_WHEELS_ZIP_SHA256 = "9e7c330ee93d81d0356cc2d585f217dfee58b623ad4535282baa6e82bd063eee" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/** | ||
* This file contains code that roughly replaces pyodide.loadPackage, with workerd-specific | ||
* optimizations: | ||
* - Wheels are decompressed with a DecompressionStream instead of in Python | ||
* - Wheels are overlaid onto the site-packages dir instead of actually being copied | ||
* - Wheels are fetched from a disk cache if available. | ||
* | ||
* Note that loadPackages is only used in local dev for now, internally we use the full big bundle | ||
* that contains all the packages ready to go. | ||
*/ | ||
|
||
import { default as LOCKFILE } from "pyodide-internal:generated/pyodide-lock.json"; | ||
import { WORKERD_INDEX_URL } from "pyodide-internal:metadata"; | ||
import { SITE_PACKAGES, LOAD_WHEELS_FROM_R2, getSitePackagesPath } from "pyodide-internal:setupPackages"; | ||
import { parseTarInfo } from "pyodide-internal:tar"; | ||
import { default as DiskCache } from "pyodide-internal:disk_cache"; | ||
import { createTarFS } from "pyodide-internal:tarfs"; | ||
|
||
async function loadBundle(requirement) { | ||
// first check if the disk cache has what we want | ||
const filename = LOCKFILE["packages"][requirement]["file_name"]; | ||
const cached = DiskCache.get(filename); | ||
if (cached) { | ||
return [requirement, cached]; | ||
} | ||
|
||
// we didn't find it in the disk cache, continue with original fetch | ||
const url = new URL(WORKERD_INDEX_URL + filename); | ||
const response = await fetch(url); | ||
|
||
const arrayBuffer = await new Response(response.body.pipeThrough(new DecompressionStream("gzip"))).arrayBuffer(); | ||
|
||
DiskCache.put(filename, arrayBuffer); | ||
return [requirement, arrayBuffer]; | ||
}; | ||
|
||
/** | ||
* ArrayBufferReader wraps around an arrayBuffer in a way that tar.js is able to read from | ||
*/ | ||
class ArrayBufferReader { | ||
constructor(arrayBuffer) { | ||
this.arrayBuffer = arrayBuffer; | ||
} | ||
|
||
read(offset, buf){ | ||
// buf is a Uint8Array | ||
const size = this.arrayBuffer.byteLength; | ||
if (offset >= size || offset < 0) { | ||
return 0; | ||
} | ||
let toCopy = buf.length; | ||
if (size - offset < toCopy) { | ||
toCopy = size - offset; | ||
} | ||
buf.set(new Uint8Array(this.arrayBuffer, offset, toCopy)); | ||
return toCopy; | ||
} | ||
} | ||
|
||
export async function loadPackages(Module, requirements) { | ||
if (!LOAD_WHEELS_FROM_R2) return; | ||
|
||
let loadPromises = []; | ||
let loading = []; | ||
for (const req of requirements) { | ||
if (SITE_PACKAGES.loadedRequirements.has(req)) continue; | ||
loadPromises.push(loadBundle(req)); | ||
loading.push(req); | ||
} | ||
|
||
console.log("Loading " + loading.join(", ")); | ||
|
||
await Promise.all(loadPromises).then((buffers) => { | ||
for (const [requirement, buffer] of buffers) { | ||
const reader = new ArrayBufferReader(buffer); | ||
const [tarInfo, soFiles] = parseTarInfo(reader); | ||
SITE_PACKAGES.addSmallBundle(tarInfo, soFiles, requirement); | ||
} | ||
}); | ||
|
||
console.log("Loaded " + loading.join(", ")); | ||
|
||
const tarFS = createTarFS(Module); | ||
const path = getSitePackagesPath(Module); | ||
const info = SITE_PACKAGES.rootInfo; | ||
Module.FS.mount(tarFS, { info }, path); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.