Repository navigation
Ship a Node entry in the npm package #4
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
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
1ea8f33
Ship a Node entry in the package: dist/node.js exports runNode and is…
Project516 23534b0
Read a damaged image's file as missing, and report a missing JDK imag…
Project516 b59c74f
Open a module image for each read instead of keeping its descriptor
Project516 80b6b8f
Cut image reads at the file's end, read an unreadable image's file as…
Project516 86775e8
Ship TypeScript declarations for the Node entry, checked against src/…
Project516 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
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,55 @@ | ||
| # Using java-web under Node | ||
|
|
||
| The package's Node entry runs Java on Node 23.6 or newer, against the same | ||
| packaged JDK image (`dist/boot.pack`, `dist/jdk/`) that pages use. The host file | ||
| system is the VFS root, and Java sees the host's environment. | ||
|
|
||
| ## Command | ||
|
|
||
| ```sh | ||
| npx java-web [options] Main [args] | ||
| npx java-web [options] -jar app.jar [args] | ||
| ``` | ||
|
|
||
| | Option | Meaning | | ||
| |---|---| | ||
| | `-cp`, `-classpath`, `--class-path` | class path, `:`-separated (default `.`) | | ||
| | `-Dkey=value` | a system property | | ||
| | `-ea`, `-da`, `-esa` and the rest | assertions, as with `java` | | ||
| | `--enable-preview` | preview features | | ||
| | `--add-exports`, `--add-opens`, `--add-reads`, `--add-modules`, `--module-path`, `-p` | module options, as with `java` | | ||
| | `--locale-data` | locales other than English (see [support.md](support.md#locales)) | | ||
| | `--max-rss MB` | exit when resident memory passes this (default 2048) | | ||
| | `--jdk DIR` | an exploded JDK image instead of the packaged one | | ||
| | `--debug` | log VM events | | ||
|
|
||
| Other `-X` and HotSpot-only options are ignored. | ||
|
|
||
| ## From JS | ||
|
|
||
| ```js | ||
| import { runNode } from "java-web"; | ||
|
|
||
| const out = []; | ||
| const code = await runNode({ | ||
| classpath: ["classes"], | ||
| mainClass: "Main", // or jar: "app.jar" | ||
| args: ["a", "b"], | ||
| stdout: (bytes) => out.push(bytes), | ||
| }); | ||
| ``` | ||
|
|
||
| The package ships TypeScript declarations (`dist/node.d.ts`). `runNode` | ||
| resolves to the exit code. Its options: | ||
|
|
||
| - `classpath`, `mainClass` or `jar`, `args`, `props` (system properties) | ||
| - `stdout`, `stderr` (byte callbacks; default the process's), `input` (fixed stdin bytes) | ||
| - `env` (what `System.getenv()` returns; default the host's environment) | ||
| - `natives`: Java `native` methods in JS, as in the browser ([browser.md](browser.md)) | ||
| - `assertions` (the flags, for example `["-ea"]`), `enablePreview`, `localeData` | ||
| - `hostName` (what `InetAddress.getLocalHost()` reports), `maxRssMb`, `jdk`, `debug` | ||
| - `fetch`: sockets stay inside the VM, and `http:` requests go to this `fetch`, as in the browser | ||
|
|
||
| `startLibrary(options)` starts a VM in library mode, and `libraryOf(vm)` is its | ||
| `lib`, used as `cheerpjRunLibrary`'s result is in the browser. `vm.halt(0)` | ||
| stops it. |
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
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,68 @@ | ||
| // The JDK image as `pnpm build` packages it in dist/, read from disk under | ||
| // Node: boot.pack holds the index and the files a typical start reads, and | ||
| // every other file is read at its offset in jdk/<module>.jar. | ||
|
|
||
| import * as fs from "node:fs"; | ||
| import * as path from "node:path"; | ||
| import { addIndex, listIndex, parsePack } from "./browser/http.ts"; | ||
| import type { Pack } from "./browser/http.ts"; | ||
| import type { Backend, Stat } from "./fs.ts"; | ||
|
|
||
| export class ImageBackend implements Backend { | ||
| readonly writable = false; | ||
| private readonly dir: string; | ||
| private readonly pack: Pack; | ||
|
|
||
| // dir holds boot.pack and jdk/; localeData adds jdk.localedata's files | ||
| constructor(dir: string, localeData = false) { | ||
| this.dir = path.resolve(dir); | ||
| if (!fs.existsSync(path.join(this.dir, "jdk", "java.base.jar"))) { | ||
| throw new Error(`no packaged JDK image in ${this.dir}: boot.pack needs jdk/ next to it`); | ||
| } | ||
| const buf = fs.readFileSync(path.join(this.dir, "boot.pack")); | ||
| this.pack = parsePack(buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.byteLength)); | ||
| if (localeData) addIndex(this.pack, fs.readFileSync(path.join(this.dir, "jdk", "jdk.localedata.txt"), "utf8")); | ||
| } | ||
|
|
||
| stat(p: string): Stat | null { | ||
| const size = this.pack.index!.get(p); | ||
| return size === undefined ? null : { dir: size < 0, size: Math.max(size, 0), mtime: 0 }; | ||
| } | ||
|
|
||
| read(p: string): Uint8Array | null { | ||
| const size = this.pack.index!.get(p); | ||
| return size === undefined || size < 0 ? null : this.readRange(p, 0, size); | ||
| } | ||
|
|
||
| readRange(p: string, off: number, len: number): Uint8Array | null { | ||
| const size = this.pack.index!.get(p); | ||
| if (size === undefined || size < 0) return null; | ||
| len = Math.max(0, Math.min(len, size - off)); | ||
| const packed = this.pack.files.get(p); | ||
| if (packed) return packed.subarray(off, off + len); | ||
| const at = this.pack.offsets.get(p); | ||
| if (!at) return null; | ||
| const out = new Uint8Array(len); | ||
| let n = 0; | ||
| try { | ||
| const fd = fs.openSync(path.join(this.dir, "jdk", at.image), "r"); | ||
| try { | ||
| while (n < len) { | ||
| const got = fs.readSync(fd, out, n, len - n, at.at + off + n); | ||
| if (got === 0) break; | ||
| n += got; | ||
| } | ||
| } finally { | ||
| fs.closeSync(fd); | ||
| } | ||
| } catch { | ||
| return null; | ||
| } | ||
| // a short or unreadable image is damaged; the file reads as missing rather than cut off | ||
| return n === len ? out : null; | ||
| } | ||
|
|
||
| list(p: string): string[] | null { | ||
| return listIndex(this.pack.index!, p); | ||
| } | ||
| } | ||
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
Oops, something went wrong.
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.