Skip to content

Commit

Permalink
Type workaround
Browse files Browse the repository at this point in the history
  • Loading branch information
whitphx committed Jan 27, 2025
1 parent 6658857 commit 20e9f08
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 27 deletions.
8 changes: 4 additions & 4 deletions packages/kernel/src/file.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { describe, it, expect, beforeEach } from "vitest";
import { writeFileWithParents, renameWithParents } from "./file";

describe("writeFileWithParents()", () => {
let pyodide: PyodideInterface;
let pyodide: PyodideInterface & { FS: any }; // XXX: This is a temporary workaround to fix the type error.

beforeEach(async () => {
pyodide = await loadPyodide({
Expand All @@ -30,7 +30,7 @@ describe("writeFileWithParents()", () => {

expect(pyodide.FS.analyzePath(path).exists).toBe(true);
expect(pyodide.FS.readFile(path, { encoding: "utf8" })).toEqual(
"# Test",
"# Test"
);
}
});
Expand All @@ -45,7 +45,7 @@ describe("writeFileWithParents()", () => {
});

describe("renameWithParents", () => {
let pyodide: PyodideInterface;
let pyodide: PyodideInterface & { FS: any }; // XXX: This is a temporary workaround to fix the type error.

beforeEach(async () => {
pyodide = await loadPyodide({
Expand All @@ -70,7 +70,7 @@ describe("renameWithParents", () => {
expect(pyodide.FS.analyzePath(oldPath).exists).toBe(false);
expect(pyodide.FS.analyzePath(newPath).exists).toBe(true);
expect(pyodide.FS.readFile(newPath, { encoding: "utf8" })).toEqual(
"# Test",
"# Test"
);
});
});
Expand Down
15 changes: 9 additions & 6 deletions packages/kernel/src/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,18 @@ export const getAppHomeDir = (appId: string): string =>

export const resolveAppPath = (
appId: string | undefined,
filePath: string,
filePath: string
): string => {
if (appId == null) {
return path.resolve(globalHomeDir, filePath);
}
return path.resolve(getAppHomeDir(appId), filePath);
};

function ensureParent(pyodide: PyodideInterface, filePath: string): void {
function ensureParent(
pyodide: PyodideInterface & { FS: any }, // XXX: This is a temporary workaround to fix the type error.
filePath: string
): void {
const normalized = path.normalize(filePath);

const dirPath = path.dirname(normalized);
Expand Down Expand Up @@ -45,19 +48,19 @@ function ensureParent(pyodide: PyodideInterface, filePath: string): void {
}

export function writeFileWithParents(
pyodide: PyodideInterface,
pyodide: PyodideInterface & { FS: any }, // XXX: This is a temporary workaround to fix the type error.
filePath: string,
data: string | ArrayBufferView,
opts?: Parameters<PyodideInterface["FS"]["writeFile"]>[2],
opts?: unknown
): void {
ensureParent(pyodide, filePath);
pyodide.FS.writeFile(filePath, data, opts);
}

export function renameWithParents(
pyodide: PyodideInterface,
pyodide: PyodideInterface & { FS: any }, // XXX: This is a temporary workaround to fix the type error.
oldPath: string,
newPath: string,
newPath: string
): void {
ensureParent(pyodide, newPath);
pyodide.FS.rename(oldPath, newPath);
Expand Down
34 changes: 17 additions & 17 deletions packages/kernel/src/worker-runtime.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/// <reference lib="WebWorker" />

import type Pyodide from "pyodide";
import type { PyodideInterface } from "pyodide";
import type { PyProxy, PyBuffer } from "pyodide/ffi";
import { PromiseDelegate } from "@stlite/common";
import {
Expand Down Expand Up @@ -39,9 +39,9 @@ if (typeof global !== "undefined" && typeof global.self === "undefined") {
}

function dispatchModuleAutoLoading(
pyodide: Pyodide.PyodideInterface,
pyodide: PyodideInterface,
postMessage: PostMessageFn,
sources: string[],
sources: string[]
): void {
const autoLoadPromise = tryModuleAutoLoad(pyodide, postMessage, sources);
// `autoInstallPromise` will be awaited in the script_runner on the Python side.
Expand All @@ -54,13 +54,13 @@ script_runner.moduleAutoLoadPromise = __moduleAutoLoadPromise__
`);
}

let initPyodidePromise: Promise<Pyodide.PyodideInterface> | null = null;
let initPyodidePromise: Promise<PyodideInterface> | null = null;

export function startWorkerEnv(
defaultPyodideUrl: string,
postMessage: PostMessageFn,
presetInitialData?: Partial<WorkerInitialData>,
appId?: string,
appId?: string
) {
function postProgressMessage(message: string): void {
postMessage({
Expand All @@ -71,7 +71,7 @@ export function startWorkerEnv(
});
}

let pyodide: Pyodide.PyodideInterface;
let pyodide: PyodideInterface & { FS: any }; // XXX: This is a temporary workaround to fix the type error.

let httpServer: PyProxy;

Expand Down Expand Up @@ -164,7 +164,7 @@ export function startWorkerEnv(
pyodide.FS.mount(
pyodide.FS.filesystems.NODEFS,
{ root: path },
mountpoint,
mountpoint
);
});
}
Expand Down Expand Up @@ -193,14 +193,14 @@ export function startWorkerEnv(
if (path.endsWith(".py")) {
pythonFilePaths.push(path);
}
}),
})
);

// Unpack archives
postProgressMessage("Unpacking archives.");
await Promise.all(
archives.map(async (archive) => {
let buffer: Parameters<Pyodide.PyodideInterface["unpackArchive"]>[0];
let buffer: Parameters<PyodideInterface["unpackArchive"]>[0];
if ("url" in archive) {
console.debug(`Fetch an archive from ${archive.url}`);
buffer = await fetch(archive.url).then((res) => res.arrayBuffer());
Expand All @@ -211,7 +211,7 @@ export function startWorkerEnv(

console.debug(`Unpack an archive`, { format, options });
pyodide.unpackArchive(buffer, format, options);
}),
})
);

await pyodide.loadPackage("micropip");
Expand All @@ -236,7 +236,7 @@ export function startWorkerEnv(

if (moduleAutoLoad) {
const sources = pythonFilePaths.map((path) =>
pyodide.FS.readFile(path, { encoding: "utf8" }),
pyodide.FS.readFile(path, { encoding: "utf8" })
);
dispatchModuleAutoLoading(pyodide, postMessage, sources);
}
Expand Down Expand Up @@ -334,7 +334,7 @@ def setup_loggers(streamlit_level, streamlit_message_format):
console.debug("Set the loggers");

postProgressMessage(
"Mocking some Streamlit functions for the browser environment.",
"Mocking some Streamlit functions for the browser environment."
);
console.debug("Mocking some Streamlit functions");
// Disable caching. See https://github.com/whitphx/stlite/issues/495
Expand Down Expand Up @@ -417,7 +417,7 @@ prepare(main_script_path, args)
const Server = pyodide.pyimport("stlite_lib.server.Server");
httpServer = Server(
canonicalEntrypoint,
appId ? getAppHomeDir(appId) : null,
appId ? getAppHomeDir(appId) : null
);
await httpServer.start();
console.debug("Booted up the Streamlit server");
Expand Down Expand Up @@ -497,7 +497,7 @@ prepare(main_script_path, args)
const payload = new Uint8ClampedArray(
buffer.data.buffer,
buffer.data.byteOffset,
buffer.data.byteLength,
buffer.data.byteLength
);
postMessage({
type: "websocket:message",
Expand All @@ -514,7 +514,7 @@ prepare(main_script_path, args)
},
});
}
},
}
);

reply({
Expand All @@ -538,7 +538,7 @@ prepare(main_script_path, args)
const onResponse = (
statusCode: number,
_headers: PyProxy,
_body: PyProxy,
_body: PyProxy
) => {
const headers = new Map<string, string>(_headers.toJs()); // Pyodide converts dict to LiteralMap, not Map, which can't be cloned and sent to the main thread. So we convert it to Map here. Ref: https://github.com/pyodide/pyodide/pull/4576
const body = _body.toJs();
Expand All @@ -561,7 +561,7 @@ prepare(main_script_path, args)
decodeURIComponent(request.path),
request.headers,
request.body,
onResponse,
onResponse
);
break;
}
Expand Down

0 comments on commit 20e9f08

Please sign in to comment.