@arcticnotes/node-wsh 0.0.7 → 0.0.9

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,3 +1,4 @@
1
+ import EventEmitter from 'node:events';
1
2
  import PATH from 'node:path';
2
3
  import { Syncline} from "@arcticnotes/syncline";
3
4
 
@@ -7,7 +8,7 @@ const SCRIPT_FILE = PATH.join( PATH.dirname( import.meta.dirname), 'wsh', 'host.
7
8
  const PROXY = Symbol();
8
9
  const TRACE_REF = 1;
9
10
 
10
- export class WindowsScriptingHost {
11
+ export class WindowsScriptingHost extends EventEmitter {
11
12
 
12
13
  static async connect( options = {}) {
13
14
  const command = options.command || COMMAND;
@@ -24,15 +25,25 @@ export class WindowsScriptingHost {
24
25
  #Enumerator;
25
26
 
26
27
  constructor( syncline, options) {
28
+ super();
27
29
  this.#syncline = syncline;
28
30
  this.#syncline.on( 'stderr', line => console.log( 'wsh:', line));
29
31
  this.#syncline.on( 'stdout', line => console.log( 'wsh:', line));
30
- this.#proxies = new Proxies( syncline, options);
32
+ this.#proxies = new Proxies( syncline, options, this);
31
33
  this.#WScript = this.#proxies.getOrCreateObject( 0);
32
34
  this.#GetObject = this.#proxies.getOrCreateFunction( 1);
33
35
  this.#Enumerator = this.#proxies.getOrCreateFunction( 2);
34
36
  }
35
37
 
38
+ get remoteObjects() {
39
+ const proxies = this.#proxies;
40
+ return {
41
+ get count() {
42
+ return proxies.count;
43
+ },
44
+ };
45
+ }
46
+
36
47
  get WScript() {
37
48
  return this.#WScript;
38
49
  }
@@ -54,6 +65,7 @@ class Proxies {
54
65
 
55
66
  #syncline;
56
67
  #trace;
68
+ #eventEmitter;
57
69
  #finalizer = new FinalizationRegistry( this.#finalized.bind( this));
58
70
  #ref2proxy = new Map();
59
71
  #proxy2ref = new Map();
@@ -74,6 +86,18 @@ class Proxies {
74
86
  default: throw new Error( `unknown status: ${ output[ 0]}`);
75
87
  }
76
88
  },
89
+
90
+ set( target, prop, value) {
91
+ const encodedTarget = this.proxies.#encode( target[ PROXY]);
92
+ const encodedProp = this.proxies.#encode( prop);
93
+ const encodedValue = this.proxies.#encode( value);
94
+ const output = JSON.parse( this.proxies.#syncline.exchange( JSON.stringify( [ 'set', encodedTarget, encodedProp, encodedValue])));
95
+ switch( output[ 0]) {
96
+ case 'set': return;
97
+ case 'error': throw new Error( output[ 1]);
98
+ default: throw new Error( `unknown status: ${ output[ 0]}`);
99
+ }
100
+ },
77
101
  };
78
102
 
79
103
  #functionHandler = {
@@ -110,9 +134,14 @@ class Proxies {
110
134
  },
111
135
  };
112
136
 
113
- constructor( syncline, options) {
137
+ constructor( syncline, options, eventEmitter) {
114
138
  this.#syncline = syncline;
115
139
  this.#trace = options.trace;
140
+ this.#eventEmitter = eventEmitter;
141
+ }
142
+
143
+ get count() {
144
+ return this.#ref2proxy.size;
116
145
  }
117
146
 
118
147
  getOrCreateObject( ref) {
@@ -134,6 +163,7 @@ class Proxies {
134
163
  this.#ref2proxy.set( ref, newProxy);
135
164
  this.#proxy2ref.set( newProxy, ref);
136
165
  this.#finalizer.register( newProxy, ref);
166
+ this.#eventEmitter.emit( 'ref', ref, newProxy);
137
167
  return newProxy;
138
168
  }
139
169
 
@@ -150,6 +180,7 @@ class Proxies {
150
180
  default:
151
181
  console.log( `unknown response: ${ output[ 0]}`);
152
182
  }
183
+ this.#eventEmitter.emit( 'unref', ref);
153
184
  }
154
185
 
155
186
  #encode( decoded) {
package/lib/wsh/host.js CHANGED
@@ -138,6 +138,7 @@ function decodePotentialMethod( encoded) {
138
138
  var output;
139
139
  var target;
140
140
  var prop;
141
+ var value;
141
142
  var thisArg;
142
143
  var args;
143
144
  while( !WScript.StdIn.AtEndOfLine)
@@ -162,6 +163,13 @@ function decodePotentialMethod( encoded) {
162
163
  output = [ 'value', encodePotentialMethod( target, prop)];
163
164
  }
164
165
  break;
166
+ case 'set': // [ 'set', target, prop, value] => [ 'set']
167
+ target = decode( input[ 1]);
168
+ prop = decode( input[ 2]);
169
+ value = decode( input[ 3]);
170
+ target[ prop] = value;
171
+ output = [ 'set'];
172
+ break;
165
173
  case 'apply': // [ 'apply', target, thisArg, argumentList] => [ 'value', value]
166
174
  target = decodePotentialMethod( input[ 1]);
167
175
  thisArg = decode( input[ 2]);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@arcticnotes/node-wsh",
3
- "version": "0.0.7",
3
+ "version": "0.0.9",
4
4
  "description": "A Node.js package that runs Windows Scripting Host (WSH) as a child process and exposes the resources from the WSH world to the Node.js world",
5
5
  "author": "Paul <paul@arcticnotes.com>",
6
6
  "license": "MIT",
@@ -22,5 +22,8 @@
22
22
  },
23
23
  "dependencies": {
24
24
  "@arcticnotes/syncline": "0.0.3"
25
- }
25
+ },
26
+ "files": [
27
+ "/lib/"
28
+ ]
26
29
  }
package/test/test.js DELETED
@@ -1,20 +0,0 @@
1
- import ASSERT from 'node:assert/strict';
2
- import TEST from 'node:test';
3
- import { WindowsScriptingHost} from '@arcticnotes/node-wsh';
4
-
5
- TEST( 'smoke-test', async() => {
6
- const wsh = await WindowsScriptingHost.connect();
7
- try {
8
- const { WScript, GetObject, Enumerator} = wsh;
9
- console.log( WScript.Version);
10
- ASSERT.equal( typeof WScript.Version, 'string');
11
- const procs = GetObject( "winmgmts:\\\\.\\root\\cimv2").ExecQuery( 'SELECT ProcessId, Name FROM Win32_Process');
12
- for( const enumerator = new Enumerator( procs); !enumerator.atEnd(); enumerator.moveNext()) {
13
- console.log( `${ enumerator.item().ProcessId}: ${ enumerator.item().Name}`);
14
- ASSERT.equal( typeof enumerator.item().ProcessId, 'number');
15
- ASSERT.equal( typeof enumerator.item().Name, 'string');
16
- }
17
- } finally {
18
- await wsh.disconnect();
19
- }
20
- });