java-bridge 2.1.3 → 2.1.4

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.
@@ -1,30 +1,5 @@
1
- import {
2
- getClassFields,
3
- getField,
4
- getStaticField,
5
- Java,
6
- JavaOptions,
7
- setField,
8
- setStaticField,
9
- } from '../native';
10
- import {
11
- JavaClassInstance,
12
- JavaClassType,
13
- JavaConstructor,
14
- JavaVersion,
15
- } from './definitions';
16
- import { getJavaLibPath, getNativeLibPath } from './nativeLib';
17
-
18
- /**
19
- * The static java instance
20
- */
21
- let javaInstance: Java | null = null;
22
-
23
- interface ImportedJavaClass {
24
- 'class.proxy': object;
25
- new (...args: any[]): any;
26
- }
27
-
1
+ import { Java, JavaOptions } from '../native';
2
+ import { JavaClass, JavaClassConstructorType, JavaVersion, UnknownJavaClassType } from './definitions';
28
3
  /**
29
4
  * Options for creating the Java VM.
30
5
  */
@@ -42,7 +17,6 @@ export interface JVMOptions extends JavaOptions {
42
17
  */
43
18
  opts?: Array<string> | null;
44
19
  }
45
-
46
20
  /**
47
21
  * Ensure the java vm is created.
48
22
  * If the jvm is already created, this does nothing.
@@ -78,43 +52,7 @@ export interface JVMOptions extends JavaOptions {
78
52
  *
79
53
  * @param options the options to use when creating the jvm
80
54
  */
81
- export function ensureJvm(options?: JVMOptions): void {
82
- if (!javaInstance) {
83
- javaInstance = new Java(
84
- options?.libPath,
85
- options?.version,
86
- options?.opts,
87
- options,
88
- getJavaLibPath(),
89
- getNativeLibPath()
90
- );
91
- }
92
- }
93
-
94
- function defineFields(object: Record<string, any>, getStatic: boolean): void {
95
- for (const field of getClassFields(object['class.proxy'], getStatic)) {
96
- const getter = (): any =>
97
- getStatic
98
- ? getStaticField(object, field.name)
99
- : getField(object, field.name);
100
- if (field.isFinal) {
101
- Object.defineProperty(object, field.name, {
102
- get: getter,
103
- enumerable: true,
104
- });
105
- } else {
106
- Object.defineProperty(object, field.name, {
107
- get: getter,
108
- set: (value: any) =>
109
- getStatic
110
- ? setStaticField(object, field.name, value)
111
- : setField(object, field.name, value),
112
- enumerable: true,
113
- });
114
- }
115
- }
116
- }
117
-
55
+ export declare function ensureJvm(options?: JVMOptions): void;
118
56
  /**
119
57
  * Import a class.
120
58
  * Returns the constructor of the class to be created.
@@ -180,47 +118,11 @@ function defineFields(object: Record<string, any>, getStatic: boolean): void {
180
118
  * @param classname the name of the class to resolve
181
119
  * @return the java class constructor
182
120
  */
183
- export function importClass<T extends JavaClassType = JavaClassType>(
184
- classname: string
185
- ): JavaConstructor<T> {
186
- ensureJvm();
187
- const constructor = javaInstance!.importClass(
188
- classname
189
- ) as ImportedJavaClass;
190
- defineFields(constructor, true);
191
-
192
- constructor.constructor = function (...args: any[]) {
193
- const object = new constructor.prototype.constructor(...args);
194
- defineFields(object, false);
195
-
196
- return object;
197
- };
198
-
199
- return constructor as unknown as JavaConstructor<T>;
200
- }
201
-
121
+ export declare function importClass<T extends JavaClassConstructorType = UnknownJavaClassType>(classname: string): T;
202
122
  /**
203
123
  * @inheritDoc importClass
204
124
  */
205
- export async function importClassAsync<T extends JavaClassType = JavaClassType>(
206
- classname: string
207
- ): Promise<JavaConstructor<T>> {
208
- ensureJvm();
209
- const constructor = (await javaInstance!.importClassAsync(
210
- classname
211
- )) as ImportedJavaClass;
212
- defineFields(constructor, true);
213
-
214
- constructor.constructor = function (...args: any[]) {
215
- const object = new constructor.prototype.constructor(...args);
216
- defineFields(object, false);
217
-
218
- return object;
219
- };
220
-
221
- return constructor as unknown as JavaConstructor<T>;
222
- }
223
-
125
+ export declare function importClassAsync<T extends JavaClassConstructorType = UnknownJavaClassType>(classname: string): Promise<T>;
224
126
  /**
225
127
  * Append a single or multiple jars to the class path.
226
128
  *
@@ -248,15 +150,11 @@ export async function importClassAsync<T extends JavaClassType = JavaClassType>(
248
150
  *
249
151
  * @param path the path(s) to add
250
152
  */
251
- export function appendClasspath(path: string | string[]): void {
252
- ensureJvm();
253
- javaInstance!.appendClasspath(path);
254
- }
255
-
153
+ export declare function appendClasspath(path: string | string[]): void;
256
154
  /**
257
155
  * Check if `this_obj` is instance of `other`.
258
156
  * This uses the native java `instanceof` operator.
259
- * You may want to use this if {@link JavaClassInstance.instanceOf}
157
+ * You may want to use this if {@link JavaClass.instanceOf}
260
158
  * is overridden, as that method itself does not override
261
159
  * any method defined in the specific java class named 'instanceOf'.
262
160
  *
@@ -287,14 +185,7 @@ export function appendClasspath(path: string | string[]): void {
287
185
  * @param other the class or class name to check against
288
186
  * @return true if `this_obj` is an instance of `other`
289
187
  */
290
- export function isInstanceOf<T extends typeof JavaClassInstance>(
291
- this_obj: JavaClassInstance,
292
- other: string | T
293
- ): boolean {
294
- ensureJvm();
295
- return javaInstance!.isInstanceOf(this_obj, other);
296
- }
297
-
188
+ export declare function isInstanceOf<T extends JavaClassConstructorType>(this_obj: JavaClass, other: string | T): boolean;
298
189
  /**
299
190
  * Methods for altering and querying the class path.
300
191
  * @example
@@ -306,25 +197,18 @@ export function isInstanceOf<T extends typeof JavaClassInstance>(
306
197
  * assert.equal(classpath.get().length, 1);
307
198
  * assert.equal(classpath.get()[0], '/path/to/jar.jar');
308
199
  */
309
- export namespace classpath {
200
+ export declare namespace classpath {
310
201
  /**
311
202
  * @inheritDoc appendClasspath
312
203
  */
313
- export function append(path: string | string[]): void {
314
- appendClasspath(path);
315
- }
316
-
204
+ function append(path: string | string[]): void;
317
205
  /**
318
206
  * Get the loaded jars in the class path
319
207
  *
320
208
  * @returns a list of the loaded jars
321
209
  */
322
- export function get(): string[] {
323
- ensureJvm();
324
- return javaInstance!.loadedJars;
325
- }
210
+ function get(): string[];
326
211
  }
327
-
328
212
  /**
329
213
  * A callback for any output redirected from stdout/stderr from the java process.
330
214
  *
@@ -332,8 +216,7 @@ export namespace classpath {
332
216
  * This is null if the output was valid. This will probably never be set.
333
217
  * @param data the data that was converted. This is unset if <code>err</code> is set.
334
218
  */
335
- export type StdoutCallback = (err: Error | null, data?: string) => void;
336
-
219
+ export declare type StdoutCallback = (err: Error | null, data?: string) => void;
337
220
  /**
338
221
  * The class guarding the stdout redirect.
339
222
  * Keep this instance in scope to not lose the redirect.
@@ -381,7 +264,6 @@ export interface StdoutRedirectGuard {
381
264
  * @param callback the callback
382
265
  */
383
266
  on(event: 'stdout' | 'stderr', callback: StdoutCallback | null): void;
384
-
385
267
  /**
386
268
  * Reset this <code>StdoutRedirectGuard</code> instance.
387
269
  * After this call, the stdout/stderr will no longer
@@ -390,7 +272,6 @@ export interface StdoutRedirectGuard {
390
272
  */
391
273
  reset(): void;
392
274
  }
393
-
394
275
  /**
395
276
  * A namespace containing methods for redirecting the stdout/stderr of the java process.
396
277
  *
@@ -398,7 +279,7 @@ export interface StdoutRedirectGuard {
398
279
  * * {@link StdoutRedirectGuard}
399
280
  * * {@link stdout.enableRedirect}
400
281
  */
401
- export namespace stdout {
282
+ export declare namespace stdout {
402
283
  /**
403
284
  * Enable stdout/stderr redirection.
404
285
  *
@@ -452,15 +333,8 @@ export namespace stdout {
452
333
  * @param stderr the callback to be called when stderr is received
453
334
  * @returns a <code>StdoutRedirectGuard</code> instance. Keep this instance in scope to not lose the redirect.
454
335
  */
455
- export function enableRedirect(
456
- stdout?: StdoutCallback | null,
457
- stderr?: StdoutCallback | null
458
- ): StdoutRedirectGuard {
459
- ensureJvm();
460
- return javaInstance!.setStdoutCallbacks(stdout, stderr);
461
- }
336
+ function enableRedirect(stdout?: StdoutCallback | null, stderr?: StdoutCallback | null): StdoutRedirectGuard;
462
337
  }
463
-
464
338
  /**
465
339
  * The class for implementing java interfaces.
466
340
  * Keep this instance in scope to not destroy the java object.
@@ -505,7 +379,6 @@ export interface JavaInterfaceProxy {
505
379
  */
506
380
  reset(): void;
507
381
  }
508
-
509
382
  /**
510
383
  * An interface proxy method.
511
384
  * Any arguments passed to this method are values converted from java values.
@@ -514,11 +387,7 @@ export interface JavaInterfaceProxy {
514
387
  * @param args the arguments passed from the java process
515
388
  * @return the value to pass back to the java process
516
389
  */
517
- export type ProxyMethod = (...args: any[]) => any;
518
- type InternalProxyRecord = Parameters<
519
- typeof Java.prototype.createInterfaceProxy
520
- >[1];
521
-
390
+ export declare type ProxyMethod = (...args: any[]) => any;
522
391
  /**
523
392
  * Create a new java interface proxy.
524
393
  * This allows you to implement java interfaces in javascript.
@@ -626,46 +495,9 @@ type InternalProxyRecord = Parameters<
626
495
  * @param methods the methods to implement.
627
496
  * @returns a proxy class to pass back to the java process
628
497
  */
629
- export function newProxy(
630
- interfaceName: string,
631
- methods: Record<string, ProxyMethod>
632
- ): JavaInterfaceProxy {
633
- ensureJvm();
634
- const proxyMethods: InternalProxyRecord = Object.create(null);
635
-
636
- for (const [name, method] of Object.entries(methods)) {
637
- proxyMethods[name] = (
638
- err: null | Error,
639
- callback: (err: Error | null, data?: any | null) => void,
640
- ...args: any[]
641
- ): void => {
642
- if (err) {
643
- throw err;
644
- }
645
-
646
- try {
647
- const res = method(...args);
648
- callback(null, res);
649
- } catch (e: any) {
650
- if (e instanceof Error) {
651
- callback(e);
652
- } else {
653
- callback(new Error(e.toString()));
654
- }
655
- }
656
- };
657
- }
658
-
659
- return javaInstance!.createInterfaceProxy(
660
- interfaceName,
661
- proxyMethods
662
- ) as JavaInterfaceProxy;
663
- }
664
-
498
+ export declare function newProxy(interfaceName: string, methods: Record<string, ProxyMethod>): JavaInterfaceProxy;
665
499
  /**
666
500
  * Get the static java instance.
667
501
  * This has no real use, all important methods are exported explicitly.
668
502
  */
669
- export function getJavaInstance(): Java | null {
670
- return javaInstance;
671
- }
503
+ export declare function getJavaInstance(): Java | null;
@@ -0,0 +1,2 @@
1
+ export declare function getNativeLibPath(): string;
2
+ export declare function getJavaLibPath(): string;
@@ -0,0 +1 @@
1
+ export {};
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "java-bridge",
3
- "version": "2.1.3",
3
+ "version": "2.1.4",
4
4
  "main": "dist/index.prod.min.js",
5
- "typings": "ts-src/index.ts",
5
+ "types": "dist/ts-src/index.d.ts",
6
6
  "description": "A bridge between Node.js and Java APIs",
7
7
  "repository": {
8
8
  "type": "git",
@@ -14,10 +14,10 @@
14
14
  },
15
15
  "homepage": "https://github.com/MarkusJx/node-java-bridge#readme",
16
16
  "files": [
17
- "ts-src",
18
17
  "dist/*.js",
19
- "java-src/build/libs/*.jar",
20
- "native.d.ts"
18
+ "dist/*.map",
19
+ "dist/**/*.d.ts",
20
+ "java-src/build/libs/*.jar"
21
21
  ],
22
22
  "napi": {
23
23
  "name": "java",
@@ -49,7 +49,7 @@
49
49
  "postbuild": "npm run build:ts && npm run build:java",
50
50
  "build:napi": "napi build --platform --release --js native.js --dts native.d.ts",
51
51
  "build:napi:debug": "napi build --platform --js native.js --dts native.d.ts",
52
- "build:ts": "webpack build",
52
+ "build:ts": "webpack build && cpy native.d.ts dist",
53
53
  "build:java": "run-script-os",
54
54
  "build:java:darwin:linux": "cd java-src && chmod +x gradlew && ./gradlew shadowJar",
55
55
  "build:java:win32": "cd java-src && .\\gradlew.bat shadowJar",
@@ -66,7 +66,6 @@
66
66
  "docs": "typedoc --out docs ts-src/index.ts"
67
67
  },
68
68
  "dependencies": {
69
- "@types/glob": "^8.0.0",
70
69
  "chalk": "^5.0.1",
71
70
  "glob": "^8.0.3",
72
71
  "ora": "^6.1.2",
@@ -76,12 +75,14 @@
76
75
  "devDependencies": {
77
76
  "@napi-rs/cli": "^2.11.4",
78
77
  "@types/chai": "^4.3.3",
78
+ "@types/glob": "^8.0.0",
79
79
  "@types/mocha": "^9.1.1",
80
80
  "@types/node": "^18.7.15",
81
81
  "@types/semver": "^7.3.12",
82
82
  "@types/webpack-node-externals": "^2.5.3",
83
83
  "@types/yargs": "^17.0.12",
84
84
  "chai": "^4.3.6",
85
+ "cpy-cli": "^4.2.0",
85
86
  "expose-gc": "^1.0.0",
86
87
  "mocha": "^10.0.0",
87
88
  "nanobench": "^3.0.0",
@@ -94,16 +95,16 @@
94
95
  "ts-loader": "^9.3.1",
95
96
  "ts-node": "^10.9.1",
96
97
  "tslib": "^2.4.0",
97
- "typedoc": "^0.23.13",
98
+ "typedoc": "^0.23.14",
98
99
  "webpack": "^5.74.0",
99
100
  "webpack-cli": "^4.10.0",
100
101
  "webpack-node-externals": "^3.0.0"
101
102
  },
102
103
  "optionalDependencies": {
103
- "java-bridge-win32-x64-msvc": "2.1.3",
104
- "java-bridge-darwin-x64": "2.1.3",
105
- "java-bridge-linux-x64-gnu": "2.1.3",
106
- "java-bridge-darwin-arm64": "2.1.3",
107
- "java-bridge-win32-ia32-msvc": "2.1.3"
104
+ "java-bridge-win32-x64-msvc": "2.1.4",
105
+ "java-bridge-darwin-x64": "2.1.4",
106
+ "java-bridge-linux-x64-gnu": "2.1.4",
107
+ "java-bridge-darwin-arm64": "2.1.4",
108
+ "java-bridge-win32-ia32-msvc": "2.1.4"
108
109
  }
109
110
  }