-
Notifications
You must be signed in to change notification settings - Fork 11
feat(workers-runtime-sdk): support embedding javascript files in sdk #90
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
Open
ryanking13
wants to merge
6
commits into
main
Choose a base branch
from
gyeongjae/js-helper
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c192a03
feat(workers-runtime-sdk): support embedding javascript files in sdk
ryanking13 65b7176
chore: revert uv.lock change
ryanking13 3ed5070
chore: Update packages/runtime-sdk/ts/sdk.ts
ryanking13 91a578a
chore: test sdk directory
ryanking13 fdda13f
chore: pin esbuild version
ryanking13 be1d690
chore: tidy up comment
ryanking13 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,89 @@ | ||
| """ | ||
| Compile ts/sdk.ts -> src/workers/sdk.mjs using esbuild. | ||
|
|
||
| Usage: | ||
| python scripts/compile_js_sdk.py # compile and write | ||
| python scripts/compile_js_sdk.py --check # verify sdk.mjs is up to date | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import shutil | ||
| import subprocess | ||
| import sys | ||
| from pathlib import Path | ||
|
|
||
| RUNTIME_SDK_DIR = Path(__file__).resolve().parent.parent | ||
| TS_SOURCE = RUNTIME_SDK_DIR / "ts" / "sdk.ts" | ||
| MJS_OUTPUT = RUNTIME_SDK_DIR / "src" / "workers" / "sdk.mjs" | ||
|
|
||
| HEADER = """\ | ||
| // AUTO-GENERATED FILE - DO NOT EDIT DIRECTLY | ||
| // Source: ts/sdk.ts | ||
| // Regenerate: python scripts/compile_js_sdk.py | ||
| """ | ||
|
|
||
|
|
||
| def compile_ts() -> str: | ||
| """Compile ts/sdk.ts to JavaScript and return the output string (with header).""" | ||
| npx = shutil.which("npx") | ||
| if npx is None: | ||
| print( | ||
| "error: npx not found. Install Node.js to compile TypeScript.", | ||
| file=sys.stderr, | ||
| ) | ||
| sys.exit(1) | ||
|
|
||
| result = subprocess.run( | ||
| [ | ||
| npx, | ||
| "--yes", | ||
| "esbuild@0.28.0", | ||
| str(TS_SOURCE), | ||
| "--format=esm", | ||
| "--log-level=error", | ||
| ], | ||
| capture_output=True, | ||
| text=True, | ||
| cwd=str(RUNTIME_SDK_DIR), | ||
| check=False, | ||
| ) | ||
|
|
||
| if result.returncode != 0: | ||
| print(f"esbuild failed:\n{result.stderr}", file=sys.stderr) | ||
| sys.exit(result.returncode) | ||
|
|
||
| return HEADER + result.stdout | ||
|
|
||
|
|
||
| def main() -> None: | ||
| compiled = compile_ts() | ||
|
|
||
| if "--check" in sys.argv: | ||
| if not MJS_OUTPUT.exists(): | ||
| print( | ||
| f"error: {MJS_OUTPUT.relative_to(RUNTIME_SDK_DIR)} does not exist.", | ||
| file=sys.stderr, | ||
| ) | ||
| print("Run: python scripts/compile_js_sdk.py", file=sys.stderr) | ||
| sys.exit(1) | ||
|
|
||
| current = MJS_OUTPUT.read_text() | ||
| if current != compiled: | ||
| print( | ||
| f"error: {MJS_OUTPUT.relative_to(RUNTIME_SDK_DIR)} is out of date.", | ||
| file=sys.stderr, | ||
| ) | ||
| print("Run: python scripts/compile_js_sdk.py", file=sys.stderr) | ||
| sys.exit(1) | ||
|
|
||
| print(f"{MJS_OUTPUT.relative_to(RUNTIME_SDK_DIR)} is up to date.") | ||
| else: | ||
| MJS_OUTPUT.write_text(compiled) | ||
| print( | ||
| f"Compiled {TS_SOURCE.relative_to(RUNTIME_SDK_DIR)} -> {MJS_OUTPUT.relative_to(RUNTIME_SDK_DIR)}" | ||
| ) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() |
This file contains hidden or 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 hidden or 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,36 @@ | ||
| // AUTO-GENERATED FILE - DO NOT EDIT DIRECTLY | ||
| // Source: ts/sdk.ts | ||
| // Regenerate: python scripts/compile_js_sdk.py | ||
| const waitUntilPatched = /* @__PURE__ */ new WeakSet(); | ||
| function patchWaitUntil(ctx) { | ||
| let tag; | ||
| try { | ||
| tag = Object.prototype.toString.call(ctx); | ||
| } catch (_e) { | ||
| } | ||
| if (tag !== "[object ExecutionContext]") { | ||
| return; | ||
| } | ||
| if (waitUntilPatched.has(ctx)) { | ||
| return; | ||
| } | ||
| const origWaitUntil = ctx.waitUntil.bind(ctx); | ||
| function waitUntil(p) { | ||
| origWaitUntil( | ||
| (async function() { | ||
| if ("copy" in p) { | ||
| p = p.copy(); | ||
| } | ||
| await p; | ||
| if ("destroy" in p) { | ||
| p.destroy(); | ||
| } | ||
| })() | ||
| ); | ||
| } | ||
| ctx.waitUntil = waitUntil; | ||
| waitUntilPatched.add(ctx); | ||
| } | ||
| export { | ||
| patchWaitUntil | ||
| }; |
This file contains hidden or 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,28 @@ | ||
| """Verify that sdk.mjs is up to date with the TypeScript source.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import subprocess | ||
| import sys | ||
| from pathlib import Path | ||
|
|
||
| RUNTIME_SDK_DIR = Path(__file__).resolve().parent.parent | ||
| COMPILE_SCRIPT = RUNTIME_SDK_DIR / "scripts" / "compile_js_sdk.py" | ||
|
|
||
|
|
||
| def test_sdk_mjs_up_to_date() -> None: | ||
| """sdk.mjs must match the output of compiling ts/sdk.ts. | ||
|
|
||
| If this test fails, run: | ||
| python scripts/compile_js_sdk.py | ||
| """ | ||
| result = subprocess.run( | ||
| [sys.executable, str(COMPILE_SCRIPT), "--check"], | ||
| capture_output=True, | ||
| text=True, | ||
| cwd=str(RUNTIME_SDK_DIR), | ||
| check=False, | ||
| ) | ||
| assert result.returncode == 0, ( | ||
| f"sdk.mjs is out of date. Run: python scripts/compile_js_sdk.py\n{result.stderr}" | ||
| ) | ||
This file contains hidden or 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,41 @@ | ||
| // Javascript helper functions for workers-runtime-sdk | ||
| // This file is compiled to src/workers/sdk.mjs via scripts/compile_js_sdk.py | ||
|
|
||
| // Pyodide proxy future — supports copy/destroy for proxy lifecycle management | ||
| type PyFuture<T> = Promise<T> & { | ||
| copy(): PyFuture<T>; | ||
| destroy(): void; | ||
| }; | ||
|
|
||
| const waitUntilPatched = new WeakSet(); | ||
|
|
||
| export function patchWaitUntil(ctx: { | ||
| waitUntil: (p: Promise<void> | PyFuture<void>) => void; | ||
| }): void { | ||
| let tag; | ||
| try { | ||
| tag = Object.prototype.toString.call(ctx); | ||
| } catch (_e) {} | ||
| if (tag !== '[object ExecutionContext]') { | ||
| return; | ||
| } | ||
| if (waitUntilPatched.has(ctx)) { | ||
| return; | ||
| } | ||
| const origWaitUntil: (p: Promise<void>) => void = ctx.waitUntil.bind(ctx); | ||
| function waitUntil(p: Promise<void> | PyFuture<void>): void { | ||
| origWaitUntil( | ||
| (async function (): Promise<void> { | ||
| if ('copy' in p) { | ||
| p = p.copy(); | ||
| } | ||
| await p; | ||
| if ('destroy' in p) { | ||
| p.destroy(); | ||
| } | ||
| })() | ||
| ); | ||
| } | ||
| ctx.waitUntil = waitUntil; | ||
| waitUntilPatched.add(ctx); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.