js4j 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -33,6 +33,7 @@ A Node.js implementation of [py4j](https://www.py4j.org/) — a bridge between J
33
33
  - [createJavaProxy](#createjavaproxy)
34
34
  - [CallbackServer](#callbackserver)
35
35
  - [ProxyPool](#proxypool)
36
+ - [launchGateway](#launchgateway)
36
37
  - [Errors](#errors)
37
38
  - [TypeScript](#typescript)
38
39
  - [Credits](#credits)
@@ -668,6 +669,46 @@ const items: any[] = await list.toArray();
668
669
 
669
670
  ---
670
671
 
672
+ ### launchGateway
673
+
674
+ Spawn a Java process that runs a `GatewayServer` and connect a `JavaGateway` to it automatically. Useful when Node.js is responsible for starting the JVM rather than the other way around.
675
+
676
+ ```js
677
+ const { launchGateway } = require('js4j');
678
+
679
+ const { gateway, kill } = await launchGateway({
680
+ classpath: '/usr/share/py4j/py4j.jar:java/build',
681
+ mainClass: 'com.example.MyApp',
682
+ });
683
+
684
+ const result = await gateway.entry_point.doSomething();
685
+ await kill();
686
+ ```
687
+
688
+ #### Options
689
+
690
+ | Option | Type | Default | Description |
691
+ |---|---|---|---|
692
+ | `classpath` | `string` | *required* | Java classpath string (e.g. `'py4j.jar:.'`). |
693
+ | `mainClass` | `string` | *required* | Fully-qualified main class to launch. |
694
+ | `host` | `string` | `'127.0.0.1'` | Host to connect to after the process starts. |
695
+ | `port` | `number` | `25333` | Port to connect to. |
696
+ | `jvmArgs` | `string[]` | `[]` | Extra JVM flags (e.g. `['-Xmx512m', '-ea']`). |
697
+ | `args` | `string[]` | `[]` | Arguments passed to the main class. |
698
+ | `readyPattern` | `RegExp \| string \| null` | `/GATEWAY_STARTED/` | Pattern matched against stdout to detect when the server is ready. Set to `null` to skip stdout checking and rely only on port polling. |
699
+ | `timeout` | `number` | `30000` | Maximum milliseconds to wait for the server to become ready. |
700
+ | `gatewayOptions` | `GatewayParametersOptions` | `{}` | Extra options forwarded to `GatewayParameters` (e.g. `authToken`). |
701
+
702
+ #### Return value
703
+
704
+ | Property | Type | Description |
705
+ |---|---|---|
706
+ | `gateway` | `JavaGateway` | A connected `JavaGateway` instance. |
707
+ | `process` | `ChildProcess` | The spawned Java child process. |
708
+ | `kill` | `() => Promise<void>` | Shuts down the gateway and kills the Java process. |
709
+
710
+ ---
711
+
671
712
  ## Running Tests
672
713
 
673
714
  ```bash
package/index.d.ts CHANGED
@@ -372,6 +372,62 @@ export declare class CallbackServer {
372
372
  readonly proxyPool: ProxyPool;
373
373
  }
374
374
 
375
+ // ---------------------------------------------------------------------------
376
+ // Launcher
377
+ // ---------------------------------------------------------------------------
378
+
379
+ import { ChildProcess } from 'child_process';
380
+
381
+ export interface LaunchGatewayOptions {
382
+ /** Java classpath (e.g. '/path/to/py4j.jar:.') */
383
+ classpath: string;
384
+ /** Fully-qualified main class to launch (e.g. 'com.example.MyApp') */
385
+ mainClass: string;
386
+ /** Gateway host. Default: '127.0.0.1' */
387
+ host?: string;
388
+ /** Gateway port. Default: 25333 */
389
+ port?: number;
390
+ /** Extra JVM flags (e.g. ['-Xmx512m']). Default: [] */
391
+ jvmArgs?: string[];
392
+ /** Extra arguments passed to the main class. Default: [] */
393
+ args?: string[];
394
+ /**
395
+ * Pattern to match in process stdout that signals the server is ready.
396
+ * Set to null to skip stdout checking and rely only on port polling.
397
+ * Default: /GATEWAY_STARTED/
398
+ */
399
+ readyPattern?: RegExp | string | null;
400
+ /** Maximum milliseconds to wait for the server to become ready. Default: 30000 */
401
+ timeout?: number;
402
+ /** Extra options forwarded to GatewayParameters. */
403
+ gatewayOptions?: GatewayParametersOptions;
404
+ }
405
+
406
+ export interface LaunchGatewayResult {
407
+ /** The spawned Java child process. */
408
+ process: ChildProcess;
409
+ /** A connected JavaGateway instance. */
410
+ gateway: JavaGateway;
411
+ /**
412
+ * Shut down the gateway and kill the Java process.
413
+ * Equivalent to calling `gateway.shutdown()` then `process.kill()`.
414
+ */
415
+ kill: () => Promise<void>;
416
+ }
417
+
418
+ /**
419
+ * Spawn a Java GatewayServer process and connect a JavaGateway to it.
420
+ *
421
+ * @example
422
+ * const { gateway, kill } = await launchGateway({
423
+ * classpath: '/usr/share/py4j/py4j.jar:java/build',
424
+ * mainClass: 'com.example.MyApp',
425
+ * });
426
+ * const result = await gateway.entry_point.doSomething();
427
+ * await kill();
428
+ */
429
+ export declare function launchGateway(options: LaunchGatewayOptions): Promise<LaunchGatewayResult>;
430
+
375
431
  // ---------------------------------------------------------------------------
376
432
  // Factory functions
377
433
  // ---------------------------------------------------------------------------
package/index.js CHANGED
@@ -40,6 +40,7 @@ const {
40
40
  Js4JAuthenticationError,
41
41
  } = require('./src/exceptions');
42
42
  const protocol = require('./src/protocol');
43
+ const { launchGateway } = require('./src/launcher');
43
44
 
44
45
  module.exports = {
45
46
  // Main gateway classes
@@ -73,6 +74,9 @@ module.exports = {
73
74
  Js4JNetworkError,
74
75
  Js4JAuthenticationError,
75
76
 
77
+ // Launcher
78
+ launchGateway,
79
+
76
80
  // Low-level protocol (for advanced use)
77
81
  protocol,
78
82
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "js4j",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "description": "A Node.js implementation of py4j — bridge between JavaScript and Java via the Py4J gateway protocol",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",