@arcticnotes/node-wsh 0.0.7
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/LICENSE +18 -0
- package/README.md +40 -0
- package/lib/index.js +1 -0
- package/lib/node/node-wsh.js +235 -0
- package/lib/wsh/host.js +215 -0
- package/lib/wsh/json2.js +534 -0
- package/package.json +26 -0
- package/test/test.js +20 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
Copyright 2025 ArcticNotes.com
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
4
|
+
this software and associated documentation files (the "Software"), to deal in
|
|
5
|
+
the Software without restriction, including without limitation the rights to use,
|
|
6
|
+
copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
|
|
7
|
+
Software, and to permit persons to whom the Software is furnished to do so,
|
|
8
|
+
subject to the following conditions:
|
|
9
|
+
|
|
10
|
+
The above copyright notice and this permission notice shall be included in all
|
|
11
|
+
copies or substantial portions of the Software.
|
|
12
|
+
|
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
|
15
|
+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
16
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
17
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
18
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
|
|
2
|
+
# Node-WSH Bridge
|
|
3
|
+
|
|
4
|
+
This is a Node.js libary that runs Windows Scripting Host (WSH) as a child process and exposes the resources from the
|
|
5
|
+
WSH world to the Node.js world through serialized communication over the standard input and output channels. Obviously,
|
|
6
|
+
it only works on Windows.
|
|
7
|
+
|
|
8
|
+
## Usage
|
|
9
|
+
|
|
10
|
+
To install:
|
|
11
|
+
|
|
12
|
+
```console
|
|
13
|
+
$ npm install @arcticnotes/node-wsh
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
In JavaScript code:
|
|
17
|
+
|
|
18
|
+
```javascript
|
|
19
|
+
import {WindowsScriptingHost} from '@arcticnotes/node-wsh';
|
|
20
|
+
|
|
21
|
+
const wsh = await WindowsScriptingHost.connect();
|
|
22
|
+
const WScript = wsh.WScript;
|
|
23
|
+
console.log(WScript.Version);
|
|
24
|
+
await wsh.disconnect();
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## Dependencies
|
|
28
|
+
|
|
29
|
+
* Node.js release 20.x or higher is required.
|
|
30
|
+
* Windows Scripting Host is required. Version compatibility is unclear. Please report issues.
|
|
31
|
+
* Version 5.812 is known to work.
|
|
32
|
+
* Dependency libraries included by `package.json`:
|
|
33
|
+
* `@arcticnotes/syncline`. See https://github.com/arcticnotes/syncline.
|
|
34
|
+
|
|
35
|
+
## License
|
|
36
|
+
|
|
37
|
+
This library is shared under the [MIT License](https://opensource.org/license/mit). See the `LICENSE` file for details.
|
|
38
|
+
|
|
39
|
+
This library includes `json2.js`, an implementation of JSON, donated by Douglas Crockford (the designer of JSON) to the
|
|
40
|
+
public domain. The license aforementioned does not apply to this file.
|
package/lib/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { WindowsScriptingHost} from './node/node-wsh.js';
|
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
import PATH from 'node:path';
|
|
2
|
+
import { Syncline} from "@arcticnotes/syncline";
|
|
3
|
+
|
|
4
|
+
const COMMAND = 'cscript.exe';
|
|
5
|
+
const ARGS = [ '//E:jscript', '//NoLogo'];
|
|
6
|
+
const SCRIPT_FILE = PATH.join( PATH.dirname( import.meta.dirname), 'wsh', 'host.js');
|
|
7
|
+
const PROXY = Symbol();
|
|
8
|
+
const TRACE_REF = 1;
|
|
9
|
+
|
|
10
|
+
export class WindowsScriptingHost {
|
|
11
|
+
|
|
12
|
+
static async connect( options = {}) {
|
|
13
|
+
const command = options.command || COMMAND;
|
|
14
|
+
const args = options.args || ARGS;
|
|
15
|
+
const scriptFile = options.scriptFile || SCRIPT_FILE;
|
|
16
|
+
const trace = options.trace || 0;
|
|
17
|
+
return new WindowsScriptingHost( await Syncline.spawn( command, [ ...args, scriptFile], { trace}), options);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
#syncline;
|
|
21
|
+
#proxies;
|
|
22
|
+
#WScript;
|
|
23
|
+
#GetObject;
|
|
24
|
+
#Enumerator;
|
|
25
|
+
|
|
26
|
+
constructor( syncline, options) {
|
|
27
|
+
this.#syncline = syncline;
|
|
28
|
+
this.#syncline.on( 'stderr', line => console.log( 'wsh:', line));
|
|
29
|
+
this.#syncline.on( 'stdout', line => console.log( 'wsh:', line));
|
|
30
|
+
this.#proxies = new Proxies( syncline, options);
|
|
31
|
+
this.#WScript = this.#proxies.getOrCreateObject( 0);
|
|
32
|
+
this.#GetObject = this.#proxies.getOrCreateFunction( 1);
|
|
33
|
+
this.#Enumerator = this.#proxies.getOrCreateFunction( 2);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
get WScript() {
|
|
37
|
+
return this.#WScript;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
get GetObject() {
|
|
41
|
+
return this.#GetObject;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
get Enumerator() {
|
|
45
|
+
return this.#Enumerator;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
async disconnect() {
|
|
49
|
+
await this.#syncline.close();
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
class Proxies {
|
|
54
|
+
|
|
55
|
+
#syncline;
|
|
56
|
+
#trace;
|
|
57
|
+
#finalizer = new FinalizationRegistry( this.#finalized.bind( this));
|
|
58
|
+
#ref2proxy = new Map();
|
|
59
|
+
#proxy2ref = new Map();
|
|
60
|
+
|
|
61
|
+
#objectHandler = {
|
|
62
|
+
|
|
63
|
+
proxies: this,
|
|
64
|
+
|
|
65
|
+
get( target, prop) {
|
|
66
|
+
if( prop === Symbol.toPrimitive)
|
|
67
|
+
return () => `ref#${ this.proxies.#proxy2ref.get( target[ PROXY])}`;
|
|
68
|
+
const encodedTarget = this.proxies.#encode( target[ PROXY]);
|
|
69
|
+
const encodedProp = this.proxies.#encode( prop);
|
|
70
|
+
const output = JSON.parse( this.proxies.#syncline.exchange( JSON.stringify( [ 'get', encodedTarget, encodedProp])));
|
|
71
|
+
switch( output[ 0]) {
|
|
72
|
+
case 'value': return this.proxies.#decode( output[ 1]);
|
|
73
|
+
case 'error': throw new Error( output[ 1]);
|
|
74
|
+
default: throw new Error( `unknown status: ${ output[ 0]}`);
|
|
75
|
+
}
|
|
76
|
+
},
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
#functionHandler = {
|
|
80
|
+
|
|
81
|
+
proxies: this,
|
|
82
|
+
|
|
83
|
+
get( target, prop) {
|
|
84
|
+
if( prop === Symbol.toPrimitive)
|
|
85
|
+
return () => `ref#${ this.proxies.#proxy2ref.get( target[ PROXY])}`;
|
|
86
|
+
return undefined;
|
|
87
|
+
},
|
|
88
|
+
|
|
89
|
+
apply( target, thisArg, argumentList) {
|
|
90
|
+
const encodedTarget = this.proxies.#encode( target[ PROXY]);
|
|
91
|
+
const encodedThisArg = this.proxies.#encode( thisArg);
|
|
92
|
+
const encodedArgumentList = this.proxies.#encode( argumentList);
|
|
93
|
+
const output = JSON.parse( this.proxies.#syncline.exchange( JSON.stringify( [ 'apply', encodedTarget, encodedThisArg, encodedArgumentList])));
|
|
94
|
+
switch( output[ 0]) {
|
|
95
|
+
case 'value': return this.proxies.#decode( output[ 1]);
|
|
96
|
+
case 'error': throw new Error( output[ 1]);
|
|
97
|
+
default: throw new Error( `unknown status: ${ output[ 0]}`);
|
|
98
|
+
}
|
|
99
|
+
},
|
|
100
|
+
|
|
101
|
+
construct( target, argumentList) {
|
|
102
|
+
const encodedTarget = this.proxies.#encode( target[ PROXY]);
|
|
103
|
+
const encodedArgumentList = this.proxies.#encode( argumentList);
|
|
104
|
+
const output = JSON.parse( this.proxies.#syncline.exchange( JSON.stringify( [ 'construct', encodedTarget, encodedArgumentList])));
|
|
105
|
+
switch( output[ 0]) {
|
|
106
|
+
case 'value': return this.proxies.#decode( output[ 1]);
|
|
107
|
+
case 'error': throw new Error( output[ 1]);
|
|
108
|
+
default: throw new Error( `unknown status: ${ output[ 0]}`);
|
|
109
|
+
}
|
|
110
|
+
},
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
constructor( syncline, options) {
|
|
114
|
+
this.#syncline = syncline;
|
|
115
|
+
this.#trace = options.trace;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
getOrCreateObject( ref) {
|
|
119
|
+
return this.#getOrCreate( ref, this.#objectHandler);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
getOrCreateFunction( ref) {
|
|
123
|
+
return this.#getOrCreate( ref, this.#functionHandler);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
#getOrCreate( ref, handler) {
|
|
127
|
+
const existingProxy = this.#ref2proxy.get( ref);
|
|
128
|
+
if( existingProxy)
|
|
129
|
+
return existingProxy;
|
|
130
|
+
|
|
131
|
+
const target = handler === this.#objectHandler? new RemoteObject(): function() {};
|
|
132
|
+
const newProxy = new Proxy( target, handler);
|
|
133
|
+
target[ PROXY] = newProxy;
|
|
134
|
+
this.#ref2proxy.set( ref, newProxy);
|
|
135
|
+
this.#proxy2ref.set( newProxy, ref);
|
|
136
|
+
this.#finalizer.register( newProxy, ref);
|
|
137
|
+
return newProxy;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
#finalized( ref) {
|
|
141
|
+
const output = this.#syncline.exchange( JSON.stringify( [ 'unref', ref]));
|
|
142
|
+
if( this.#trace >= TRACE_REF)
|
|
143
|
+
switch( output[ 0]) {
|
|
144
|
+
case 'error':
|
|
145
|
+
console.log( `failed to unref: ${ ref}`);
|
|
146
|
+
break;
|
|
147
|
+
case 'done':
|
|
148
|
+
console.log( `unreferenced: ${ ref}`);
|
|
149
|
+
break;
|
|
150
|
+
default:
|
|
151
|
+
console.log( `unknown response: ${ output[ 0]}`);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
#encode( decoded) {
|
|
156
|
+
switch( typeof decoded) {
|
|
157
|
+
case 'boolean':
|
|
158
|
+
case 'number':
|
|
159
|
+
case 'string':
|
|
160
|
+
return decoded;
|
|
161
|
+
case 'undefined':
|
|
162
|
+
return { type: 'undefined'};
|
|
163
|
+
case 'object':
|
|
164
|
+
if( decoded === null)
|
|
165
|
+
return decoded;
|
|
166
|
+
if( decoded instanceof Array) {
|
|
167
|
+
const encoded = [];
|
|
168
|
+
for( const item of decoded)
|
|
169
|
+
encoded.push( this.#encode( item));
|
|
170
|
+
return encoded;
|
|
171
|
+
}
|
|
172
|
+
if( decoded instanceof RemoteObject) {
|
|
173
|
+
const objref = this.#proxy2ref.get( decoded);
|
|
174
|
+
if( objref === undefined)
|
|
175
|
+
throw new Error( `remote object reference not found: ${ decoded}`);
|
|
176
|
+
return { type: 'objref', value: objref};
|
|
177
|
+
}
|
|
178
|
+
const encoded = { type: 'object', value: {}};
|
|
179
|
+
for( const [ name, value] of Object.entries( decoded))
|
|
180
|
+
encoded.value[ name] = this.#encode( value);
|
|
181
|
+
return encoded;
|
|
182
|
+
case 'function':
|
|
183
|
+
const funref = this.#proxy2ref.get( decoded);
|
|
184
|
+
if( funref === undefined)
|
|
185
|
+
throw new Error( `functions from node are disallowed: ${ decoded}`);
|
|
186
|
+
return { type: 'funref', value: funref};
|
|
187
|
+
case 'bigint':
|
|
188
|
+
case 'symbol':
|
|
189
|
+
default:
|
|
190
|
+
throw new Error( `unsupported value: ${ decoded}`);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
#decode( encoded) {
|
|
195
|
+
switch( typeof encoded) {
|
|
196
|
+
case 'boolean':
|
|
197
|
+
case 'number':
|
|
198
|
+
case 'string':
|
|
199
|
+
return encoded;
|
|
200
|
+
case 'object':
|
|
201
|
+
if( encoded === null)
|
|
202
|
+
return encoded;
|
|
203
|
+
if( encoded instanceof Array) {
|
|
204
|
+
const decoded = [];
|
|
205
|
+
for( const item of encoded)
|
|
206
|
+
decoded.push( this.#decode( item));
|
|
207
|
+
return decoded;
|
|
208
|
+
}
|
|
209
|
+
switch( encoded.type) {
|
|
210
|
+
case 'undefined':
|
|
211
|
+
return undefined;
|
|
212
|
+
case 'object':
|
|
213
|
+
const decoded = {};
|
|
214
|
+
for( const [ name, value] of Object.entries( encoded.value))
|
|
215
|
+
decoded[ name] = this.#decode( value);
|
|
216
|
+
return decoded;
|
|
217
|
+
case 'objref':
|
|
218
|
+
return this.getOrCreateObject( encoded.value);
|
|
219
|
+
case 'funref':
|
|
220
|
+
return this.getOrCreateFunction( encoded.value);
|
|
221
|
+
default:
|
|
222
|
+
throw new Error( `illegal value: ${ encoded}`);
|
|
223
|
+
}
|
|
224
|
+
case 'undefined':
|
|
225
|
+
case 'function':
|
|
226
|
+
case 'bigint':
|
|
227
|
+
case 'symbol':
|
|
228
|
+
default:
|
|
229
|
+
throw new Error( `illegal value: ${ encoded}`);
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
class RemoteObject {
|
|
235
|
+
}
|
package/lib/wsh/host.js
ADDED
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
// This file is in JScript, not in JavaScript. It is executed in Windows Scripting Host (WSH).
|
|
2
|
+
|
|
3
|
+
var FSO = new ActiveXObject( 'Scripting.FileSystemObject');
|
|
4
|
+
var OBJECT_TOSTRING = Object.toString();
|
|
5
|
+
var REFERENCES = { // must match node-wsh.js
|
|
6
|
+
'0': { type: 'obj', value: WScript},
|
|
7
|
+
'1': { type: 'fun', value: GetObject},
|
|
8
|
+
'2': { type: 'fun', value: Enumerator}
|
|
9
|
+
};
|
|
10
|
+
var nextRefId = 3;
|
|
11
|
+
|
|
12
|
+
eval( FSO.OpenTextFile( FSO.BuildPath( FSO.GetParentFolderName( WScript.ScriptFullName), 'json2.js')).ReadAll());
|
|
13
|
+
|
|
14
|
+
function decode( encoded) {
|
|
15
|
+
var decoded;
|
|
16
|
+
var item;
|
|
17
|
+
var i;
|
|
18
|
+
switch( typeof encoded) {
|
|
19
|
+
case 'boolean':
|
|
20
|
+
case 'number':
|
|
21
|
+
case 'string':
|
|
22
|
+
return encoded;
|
|
23
|
+
case 'object':
|
|
24
|
+
if( encoded === null)
|
|
25
|
+
return encoded;
|
|
26
|
+
if( encoded instanceof Array) {
|
|
27
|
+
decoded = [];
|
|
28
|
+
for( i = 0; i < encoded.length; i++)
|
|
29
|
+
decoded.push( decode( encoded[ i]));
|
|
30
|
+
return decoded;
|
|
31
|
+
}
|
|
32
|
+
switch( encoded.type) {
|
|
33
|
+
case 'undefined':
|
|
34
|
+
return undefined;
|
|
35
|
+
case 'object':
|
|
36
|
+
decoded = {};
|
|
37
|
+
for( i in encoded.value)
|
|
38
|
+
decoded[ i] = decode( encoded.value[ i]);
|
|
39
|
+
return decoded;
|
|
40
|
+
case 'objref':
|
|
41
|
+
item = REFERENCES[ encoded.value];
|
|
42
|
+
if( item === undefined)
|
|
43
|
+
throw new Error( 'reference not found: ' + encoded.value);
|
|
44
|
+
if( item.type !== 'obj')
|
|
45
|
+
throw new Error( 'reference type mismatch: ' + encoded.value);
|
|
46
|
+
return item.value;
|
|
47
|
+
case 'funref':
|
|
48
|
+
item = REFERENCES[ encoded.value];
|
|
49
|
+
if( item === undefined)
|
|
50
|
+
throw new Error( 'reference not found: ' + encoded.value);
|
|
51
|
+
if( item.type === 'potential-method')
|
|
52
|
+
throw new Error( 'potentially a method, cannot be evaluated standalone: ' + encoded.value);
|
|
53
|
+
if( item.type !== 'fun')
|
|
54
|
+
throw new Error( 'reference type mismatch: ' + encoded.value);
|
|
55
|
+
return item.value;
|
|
56
|
+
default:
|
|
57
|
+
throw new Error( 'unknown object type: ' + encoded.type);
|
|
58
|
+
}
|
|
59
|
+
case 'undefined':
|
|
60
|
+
case 'symbol':
|
|
61
|
+
case 'bigint':
|
|
62
|
+
case 'function':
|
|
63
|
+
default:
|
|
64
|
+
throw new Error( 'illegal data type: ' + typeof encoded);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function encode( decoded) {
|
|
69
|
+
var encoded;
|
|
70
|
+
var i;
|
|
71
|
+
switch( typeof decoded) {
|
|
72
|
+
case 'boolean':
|
|
73
|
+
case 'number':
|
|
74
|
+
case 'string':
|
|
75
|
+
return decoded;
|
|
76
|
+
case 'undefined':
|
|
77
|
+
return { type: 'undefined'};
|
|
78
|
+
case 'object':
|
|
79
|
+
if( decoded === null)
|
|
80
|
+
return decoded;
|
|
81
|
+
if( decoded instanceof Array) {
|
|
82
|
+
encoded = [];
|
|
83
|
+
for( i = 0; i < decoded.length; i++)
|
|
84
|
+
encoded.push( encode( decoded[ i]));
|
|
85
|
+
return encoded;
|
|
86
|
+
}
|
|
87
|
+
if( decoded.constructor && decoded.constructor.toString() === OBJECT_TOSTRING) {
|
|
88
|
+
encoded = { type: 'object', value: {}};
|
|
89
|
+
for( i in decoded)
|
|
90
|
+
encoded.value[ i] = encode( decoded[ i]);
|
|
91
|
+
return encoded;
|
|
92
|
+
}
|
|
93
|
+
for( i in REFERENCES)
|
|
94
|
+
if( REFERENCES[ i].value === decoded)
|
|
95
|
+
return { type: 'objref', value: i};
|
|
96
|
+
i = '' + nextRefId++;
|
|
97
|
+
REFERENCES[ i] = { type: 'obj', value: decoded};
|
|
98
|
+
return { type: 'objref', value: i};
|
|
99
|
+
case 'function':
|
|
100
|
+
for( i in REFERENCES)
|
|
101
|
+
if( REFERENCES[ i].value === decoded)
|
|
102
|
+
return { type: 'funref', value: i};
|
|
103
|
+
i = '' + nextRefId++;
|
|
104
|
+
REFERENCES[ i] = { type: 'fun', value: decoded};
|
|
105
|
+
return { type: 'funref', value: i};
|
|
106
|
+
case 'symbol':
|
|
107
|
+
case 'bigint':
|
|
108
|
+
default:
|
|
109
|
+
throw new Error( 'unsupported data type: ' + typeof decoded);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function encodePotentialMethod( target, prop) {
|
|
114
|
+
var i;
|
|
115
|
+
var item;
|
|
116
|
+
for( i in REFERENCES) {
|
|
117
|
+
item = REFERENCES[ i];
|
|
118
|
+
if( item.type === 'potential-method' && item.target === target, item.prop === prop)
|
|
119
|
+
return { type: 'funref', value: i};
|
|
120
|
+
}
|
|
121
|
+
i = '' + nextRefId++;
|
|
122
|
+
REFERENCES[ i] = { type: 'potential-method', target: target, prop: prop};
|
|
123
|
+
return { type: 'funref', value: i};
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function decodePotentialMethod( encoded) {
|
|
127
|
+
var item;
|
|
128
|
+
if( typeof encoded === 'object' && encoded.type === 'funref') {
|
|
129
|
+
item = REFERENCES[ encoded.value];
|
|
130
|
+
if( item.type === 'potential-method')
|
|
131
|
+
return { 'potential-method': true, target: item.target, prop: item.prop};
|
|
132
|
+
}
|
|
133
|
+
return { 'potential-method': false, value: decode( encoded)}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
( function() {
|
|
137
|
+
var input;
|
|
138
|
+
var output;
|
|
139
|
+
var target;
|
|
140
|
+
var prop;
|
|
141
|
+
var thisArg;
|
|
142
|
+
var args;
|
|
143
|
+
while( !WScript.StdIn.AtEndOfLine)
|
|
144
|
+
try {
|
|
145
|
+
input = JSON.parse( WScript.StdIn.ReadLine());
|
|
146
|
+
switch( input[ 0]) {
|
|
147
|
+
case 'unref': // [ 'unref', ref] => [ 'done']
|
|
148
|
+
if( REFERENCES[ input[ 1]] === undefined)
|
|
149
|
+
throw new Error( 'unknown ref: ' + input[ 1]);
|
|
150
|
+
delete REFERENCES[ input[ i]];
|
|
151
|
+
output = [ 'done'];
|
|
152
|
+
break;
|
|
153
|
+
case 'get': // [ 'get', target, prop] => [ 'value', value] | [ 'potential-method']
|
|
154
|
+
target = decode( input[ 1]);
|
|
155
|
+
prop = decode( input[ 2]);
|
|
156
|
+
try {
|
|
157
|
+
output = [ 'value', encode( target[ prop])];
|
|
158
|
+
} catch( error) {
|
|
159
|
+
// could be a method
|
|
160
|
+
if( typeof target[ prop] !== 'unknown')
|
|
161
|
+
throw error;
|
|
162
|
+
output = [ 'value', encodePotentialMethod( target, prop)];
|
|
163
|
+
}
|
|
164
|
+
break;
|
|
165
|
+
case 'apply': // [ 'apply', target, thisArg, argumentList] => [ 'value', value]
|
|
166
|
+
target = decodePotentialMethod( input[ 1]);
|
|
167
|
+
thisArg = decode( input[ 2]);
|
|
168
|
+
args = decode( input[ 3]);
|
|
169
|
+
if( target[ 'potential-method']) {
|
|
170
|
+
if( thisArg === undefined)
|
|
171
|
+
throw new Error( 'potentially a method, can only be used as such');
|
|
172
|
+
if( thisArg !== target.target)
|
|
173
|
+
throw new Error( 'potentially a method, can only be used as such');
|
|
174
|
+
switch( args.length) {
|
|
175
|
+
case 0: output = [ 'value', encode( target.target[ target.prop]())]; break;
|
|
176
|
+
case 1: output = [ 'value', encode( target.target[ target.prop]( args[ 0]))]; break;
|
|
177
|
+
case 2: output = [ 'value', encode( target.target[ target.prop]( args[ 0], args[ 1]))]; break;
|
|
178
|
+
case 3: output = [ 'value', encode( target.target[ target.prop]( args[ 0], args[ 1], args[ 2]))]; break;
|
|
179
|
+
case 4: output = [ 'value', encode( target.target[ target.prop]( args[ 0], args[ 1], args[ 2], args[ 3]))]; break;
|
|
180
|
+
case 5: output = [ 'value', encode( target.target[ target.prop]( args[ 0], args[ 1], args[ 2], args[ 3], args[ 4]))]; break;
|
|
181
|
+
case 6: output = [ 'value', encode( target.target[ target.prop]( args[ 0], args[ 1], args[ 2], args[ 3], args[ 4], args[ 5]))]; break;
|
|
182
|
+
case 7: output = [ 'value', encode( target.target[ target.prop]( args[ 0], args[ 1], args[ 2], args[ 3], args[ 4], args[ 5], args[ 6]))]; break;
|
|
183
|
+
case 8: output = [ 'value', encode( target.target[ target.prop]( args[ 0], args[ 1], args[ 2], args[ 3], args[ 4], args[ 5], args[ 6], args[ 7]))]; break;
|
|
184
|
+
case 9: output = [ 'value', encode( target.target[ target.prop]( args[ 0], args[ 1], args[ 2], args[ 3], args[ 4], args[ 5], args[ 6], args[ 7], args[ 8]))]; break;
|
|
185
|
+
default: throw new Error( 'too many arguments');
|
|
186
|
+
}
|
|
187
|
+
} else
|
|
188
|
+
output = [ 'value', encode( target.value.apply( thisArg, args))];
|
|
189
|
+
break;
|
|
190
|
+
case 'construct': // [ 'construct', target, argumentList] => [ 'value', value]
|
|
191
|
+
target = decode( input[ 1]);
|
|
192
|
+
args = decode( input[ 2]);
|
|
193
|
+
switch( args.length) {
|
|
194
|
+
case 0: output = [ 'value', encode( new target())]; break;
|
|
195
|
+
case 1: output = [ 'value', encode( new target( args[ 0]))]; break;
|
|
196
|
+
case 2: output = [ 'value', encode( new target( args[ 0], args[ 1]))]; break;
|
|
197
|
+
case 3: output = [ 'value', encode( new target( args[ 0], args[ 1], args[ 2]))]; break;
|
|
198
|
+
case 4: output = [ 'value', encode( new target( args[ 0], args[ 1], args[ 2], args[ 3]))]; break;
|
|
199
|
+
case 5: output = [ 'value', encode( new target( args[ 0], args[ 1], args[ 2], args[ 3], args[ 4]))]; break;
|
|
200
|
+
case 6: output = [ 'value', encode( new target( args[ 0], args[ 1], args[ 2], args[ 3], args[ 4], args[ 5]))]; break;
|
|
201
|
+
case 7: output = [ 'value', encode( new target( args[ 0], args[ 1], args[ 2], args[ 3], args[ 4], args[ 5], args[ 6]))]; break;
|
|
202
|
+
case 8: output = [ 'value', encode( new target( args[ 0], args[ 1], args[ 2], args[ 3], args[ 4], args[ 5], args[ 6], args[ 7]))]; break;
|
|
203
|
+
case 9: output = [ 'value', encode( new target( args[ 0], args[ 1], args[ 2], args[ 3], args[ 4], args[ 5], args[ 6], args[ 7], args[ 8]))]; break;
|
|
204
|
+
default: throw new Error( 'too many arguments');
|
|
205
|
+
}
|
|
206
|
+
break;
|
|
207
|
+
default:
|
|
208
|
+
throw new Error( 'unknown command: ' + input[ 0]);
|
|
209
|
+
}
|
|
210
|
+
} catch( error) {
|
|
211
|
+
output = [ 'error', '' + error.message];
|
|
212
|
+
} finally {
|
|
213
|
+
WScript.StdOut.WriteLine( JSON.stringify( output));
|
|
214
|
+
}
|
|
215
|
+
})();
|
package/lib/wsh/json2.js
ADDED
|
@@ -0,0 +1,534 @@
|
|
|
1
|
+
// This file is downloaded from
|
|
2
|
+
// https://github.com/douglascrockford/JSON-js/blob/master/json2.js
|
|
3
|
+
//==============================================================================
|
|
4
|
+
|
|
5
|
+
// json2.js
|
|
6
|
+
// 2023-05-10
|
|
7
|
+
// Public Domain.
|
|
8
|
+
// NO WARRANTY EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK.
|
|
9
|
+
|
|
10
|
+
// USE YOUR OWN COPY. IT IS EXTREMELY UNWISE TO LOAD CODE FROM SERVERS YOU DO
|
|
11
|
+
// NOT CONTROL.
|
|
12
|
+
|
|
13
|
+
// This file creates a global JSON object containing two methods: stringify
|
|
14
|
+
// and parse. This file provides the ES5 JSON capability to ES3 systems.
|
|
15
|
+
// If a project might run on IE8 or earlier, then this file should be included.
|
|
16
|
+
// This file does nothing on ES5 systems.
|
|
17
|
+
|
|
18
|
+
// JSON.stringify(value, replacer, space)
|
|
19
|
+
// value any JavaScript value, usually an object or array.
|
|
20
|
+
// replacer an optional parameter that determines how object
|
|
21
|
+
// values are stringified for objects. It can be a
|
|
22
|
+
// function or an array of strings.
|
|
23
|
+
// space an optional parameter that specifies the indentation
|
|
24
|
+
// of nested structures. If it is omitted, the text will
|
|
25
|
+
// be packed without extra whitespace. If it is a number,
|
|
26
|
+
// it will specify the number of spaces to indent at each
|
|
27
|
+
// level. If it is a string (such as "\t" or " "),
|
|
28
|
+
// it contains the characters used to indent at each level.
|
|
29
|
+
// This method produces a JSON text from a JavaScript value.
|
|
30
|
+
// When an object value is found, if the object contains a toJSON
|
|
31
|
+
// method, its toJSON method will be called and the result will be
|
|
32
|
+
// stringified. A toJSON method does not serialize: it returns the
|
|
33
|
+
// value represented by the name/value pair that should be serialized,
|
|
34
|
+
// or undefined if nothing should be serialized. The toJSON method
|
|
35
|
+
// will be passed the key associated with the value, and this will be
|
|
36
|
+
// bound to the value.
|
|
37
|
+
|
|
38
|
+
// For example, this would serialize Dates as ISO strings.
|
|
39
|
+
|
|
40
|
+
// Date.prototype.toJSON = function (key) {
|
|
41
|
+
// function f(n) {
|
|
42
|
+
// // Format integers to have at least two digits.
|
|
43
|
+
// return (n < 10)
|
|
44
|
+
// ? "0" + n
|
|
45
|
+
// : n;
|
|
46
|
+
// }
|
|
47
|
+
// return this.getUTCFullYear() + "-" +
|
|
48
|
+
// f(this.getUTCMonth() + 1) + "-" +
|
|
49
|
+
// f(this.getUTCDate()) + "T" +
|
|
50
|
+
// f(this.getUTCHours()) + ":" +
|
|
51
|
+
// f(this.getUTCMinutes()) + ":" +
|
|
52
|
+
// f(this.getUTCSeconds()) + "Z";
|
|
53
|
+
// };
|
|
54
|
+
|
|
55
|
+
// You can provide an optional replacer method. It will be passed the
|
|
56
|
+
// key and value of each member, with this bound to the containing
|
|
57
|
+
// object. The value that is returned from your method will be
|
|
58
|
+
// serialized. If your method returns undefined, then the member will
|
|
59
|
+
// be excluded from the serialization.
|
|
60
|
+
|
|
61
|
+
// If the replacer parameter is an array of strings, then it will be
|
|
62
|
+
// used to select the members to be serialized. It filters the results
|
|
63
|
+
// such that only members with keys listed in the replacer array are
|
|
64
|
+
// stringified.
|
|
65
|
+
|
|
66
|
+
// Values that do not have JSON representations, such as undefined or
|
|
67
|
+
// functions, will not be serialized. Such values in objects will be
|
|
68
|
+
// dropped; in arrays they will be replaced with null. You can use
|
|
69
|
+
// a replacer function to replace those with JSON values.
|
|
70
|
+
|
|
71
|
+
// JSON.stringify(undefined) returns undefined.
|
|
72
|
+
|
|
73
|
+
// The optional space parameter produces a stringification of the
|
|
74
|
+
// value that is filled with line breaks and indentation to make it
|
|
75
|
+
// easier to read.
|
|
76
|
+
|
|
77
|
+
// If the space parameter is a non-empty string, then that string will
|
|
78
|
+
// be used for indentation. If the space parameter is a number, then
|
|
79
|
+
// the indentation will be that many spaces.
|
|
80
|
+
|
|
81
|
+
// Example:
|
|
82
|
+
|
|
83
|
+
// text = JSON.stringify(["e", {pluribus: "unum"}]);
|
|
84
|
+
// // text is '["e",{"pluribus":"unum"}]'
|
|
85
|
+
|
|
86
|
+
// text = JSON.stringify(["e", {pluribus: "unum"}], null, "\t");
|
|
87
|
+
// // text is '[\n\t"e",\n\t{\n\t\t"pluribus": "unum"\n\t}\n]'
|
|
88
|
+
|
|
89
|
+
// text = JSON.stringify([new Date()], function (key, value) {
|
|
90
|
+
// return this[key] instanceof Date
|
|
91
|
+
// ? "Date(" + this[key] + ")"
|
|
92
|
+
// : value;
|
|
93
|
+
// });
|
|
94
|
+
// // text is '["Date(---current time---)"]'
|
|
95
|
+
|
|
96
|
+
// JSON.parse(text, reviver)
|
|
97
|
+
// This method parses a JSON text to produce an object or array.
|
|
98
|
+
// It can throw a SyntaxError exception.
|
|
99
|
+
|
|
100
|
+
// The optional reviver parameter is a function that can filter and
|
|
101
|
+
// transform the results. It receives each of the keys and values,
|
|
102
|
+
// and its return value is used instead of the original value.
|
|
103
|
+
// If it returns what it received, then the structure is not modified.
|
|
104
|
+
// If it returns undefined then the member is deleted.
|
|
105
|
+
|
|
106
|
+
// Example:
|
|
107
|
+
|
|
108
|
+
// // Parse the text. Values that look like ISO date strings will
|
|
109
|
+
// // be converted to Date objects.
|
|
110
|
+
|
|
111
|
+
// myData = JSON.parse(text, function (key, value) {
|
|
112
|
+
// var a;
|
|
113
|
+
// if (typeof value === "string") {
|
|
114
|
+
// a =
|
|
115
|
+
// /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}(?:\.\d*)?)Z$/.exec(value);
|
|
116
|
+
// if (a) {
|
|
117
|
+
// return new Date(Date.UTC(
|
|
118
|
+
// +a[1], +a[2] - 1, +a[3], +a[4], +a[5], +a[6]
|
|
119
|
+
// ));
|
|
120
|
+
// }
|
|
121
|
+
// return value;
|
|
122
|
+
// }
|
|
123
|
+
// });
|
|
124
|
+
|
|
125
|
+
// myData = JSON.parse(
|
|
126
|
+
// "[\"Date(09/09/2001)\"]",
|
|
127
|
+
// function (key, value) {
|
|
128
|
+
// var d;
|
|
129
|
+
// if (
|
|
130
|
+
// typeof value === "string"
|
|
131
|
+
// && value.slice(0, 5) === "Date("
|
|
132
|
+
// && value.slice(-1) === ")"
|
|
133
|
+
// ) {
|
|
134
|
+
// d = new Date(value.slice(5, -1));
|
|
135
|
+
// if (d) {
|
|
136
|
+
// return d;
|
|
137
|
+
// }
|
|
138
|
+
// }
|
|
139
|
+
// return value;
|
|
140
|
+
// }
|
|
141
|
+
// );
|
|
142
|
+
|
|
143
|
+
// This is a reference implementation. You are free to copy, modify, or
|
|
144
|
+
// redistribute.
|
|
145
|
+
|
|
146
|
+
/*jslint
|
|
147
|
+
eval, for, this
|
|
148
|
+
*/
|
|
149
|
+
|
|
150
|
+
/*property
|
|
151
|
+
JSON, apply, call, charCodeAt, getUTCDate, getUTCFullYear, getUTCHours,
|
|
152
|
+
getUTCMinutes, getUTCMonth, getUTCSeconds, hasOwnProperty, join,
|
|
153
|
+
lastIndex, length, parse, prototype, push, replace, slice, stringify,
|
|
154
|
+
test, toJSON, toString, valueOf
|
|
155
|
+
*/
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
// Create a JSON object only if one does not already exist. We create the
|
|
159
|
+
// methods in a closure to avoid creating global variables.
|
|
160
|
+
|
|
161
|
+
if (typeof JSON !== "object") {
|
|
162
|
+
JSON = {};
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
(function () {
|
|
166
|
+
"use strict";
|
|
167
|
+
|
|
168
|
+
var rx_one = /^[\],:{}\s]*$/;
|
|
169
|
+
var rx_two = /\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g;
|
|
170
|
+
var rx_three = /"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g;
|
|
171
|
+
var rx_four = /(?:^|:|,)(?:\s*\[)+/g;
|
|
172
|
+
var rx_escapable = /[\\"\u0000-\u001f\u007f-\u009f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g;
|
|
173
|
+
var rx_dangerous = /[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g;
|
|
174
|
+
|
|
175
|
+
function f(n) {
|
|
176
|
+
// Format integers to have at least two digits.
|
|
177
|
+
return (n < 10)
|
|
178
|
+
? "0" + n
|
|
179
|
+
: n;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
function this_value() {
|
|
183
|
+
return this.valueOf();
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
if (typeof Date.prototype.toJSON !== "function") {
|
|
187
|
+
|
|
188
|
+
Date.prototype.toJSON = function () {
|
|
189
|
+
|
|
190
|
+
return isFinite(this.valueOf())
|
|
191
|
+
? (
|
|
192
|
+
this.getUTCFullYear()
|
|
193
|
+
+ "-"
|
|
194
|
+
+ f(this.getUTCMonth() + 1)
|
|
195
|
+
+ "-"
|
|
196
|
+
+ f(this.getUTCDate())
|
|
197
|
+
+ "T"
|
|
198
|
+
+ f(this.getUTCHours())
|
|
199
|
+
+ ":"
|
|
200
|
+
+ f(this.getUTCMinutes())
|
|
201
|
+
+ ":"
|
|
202
|
+
+ f(this.getUTCSeconds())
|
|
203
|
+
+ "Z"
|
|
204
|
+
)
|
|
205
|
+
: null;
|
|
206
|
+
};
|
|
207
|
+
|
|
208
|
+
Boolean.prototype.toJSON = this_value;
|
|
209
|
+
Number.prototype.toJSON = this_value;
|
|
210
|
+
String.prototype.toJSON = this_value;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
var gap;
|
|
214
|
+
var indent;
|
|
215
|
+
var meta;
|
|
216
|
+
var rep;
|
|
217
|
+
|
|
218
|
+
|
|
219
|
+
function quote(string) {
|
|
220
|
+
|
|
221
|
+
// If the string contains no control characters, no quote characters, and no
|
|
222
|
+
// backslash characters, then we can safely slap some quotes around it.
|
|
223
|
+
// Otherwise we must also replace the offending characters with safe escape
|
|
224
|
+
// sequences.
|
|
225
|
+
|
|
226
|
+
rx_escapable.lastIndex = 0;
|
|
227
|
+
return rx_escapable.test(string)
|
|
228
|
+
? "\"" + string.replace(rx_escapable, function (a) {
|
|
229
|
+
var c = meta[a];
|
|
230
|
+
return typeof c === "string"
|
|
231
|
+
? c
|
|
232
|
+
: "\\u" + ("0000" + a.charCodeAt(0).toString(16)).slice(-4);
|
|
233
|
+
}) + "\""
|
|
234
|
+
: "\"" + string + "\"";
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
|
|
238
|
+
function str(key, holder) {
|
|
239
|
+
|
|
240
|
+
// Produce a string from holder[key].
|
|
241
|
+
|
|
242
|
+
var i; // The loop counter.
|
|
243
|
+
var k; // The member key.
|
|
244
|
+
var v; // The member value.
|
|
245
|
+
var length;
|
|
246
|
+
var mind = gap;
|
|
247
|
+
var partial;
|
|
248
|
+
var value = holder[key];
|
|
249
|
+
|
|
250
|
+
// If the value has a toJSON method, call it to obtain a replacement value.
|
|
251
|
+
|
|
252
|
+
if (
|
|
253
|
+
value
|
|
254
|
+
&& typeof value === "object"
|
|
255
|
+
&& typeof value.toJSON === "function"
|
|
256
|
+
) {
|
|
257
|
+
value = value.toJSON(key);
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
// If we were called with a replacer function, then call the replacer to
|
|
261
|
+
// obtain a replacement value.
|
|
262
|
+
|
|
263
|
+
if (typeof rep === "function") {
|
|
264
|
+
value = rep.call(holder, key, value);
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
// What happens next depends on the value's type.
|
|
268
|
+
|
|
269
|
+
switch (typeof value) {
|
|
270
|
+
case "string":
|
|
271
|
+
return quote(value);
|
|
272
|
+
|
|
273
|
+
case "number":
|
|
274
|
+
|
|
275
|
+
// JSON numbers must be finite. Encode non-finite numbers as null.
|
|
276
|
+
|
|
277
|
+
return (isFinite(value))
|
|
278
|
+
? String(value)
|
|
279
|
+
: "null";
|
|
280
|
+
|
|
281
|
+
case "boolean":
|
|
282
|
+
case "null":
|
|
283
|
+
|
|
284
|
+
// If the value is a boolean or null, convert it to a string. Note:
|
|
285
|
+
// typeof null does not produce "null". The case is included here in
|
|
286
|
+
// the remote chance that this gets fixed someday.
|
|
287
|
+
|
|
288
|
+
return String(value);
|
|
289
|
+
|
|
290
|
+
// If the type is "object", we might be dealing with an object or an array or
|
|
291
|
+
// null.
|
|
292
|
+
|
|
293
|
+
case "object":
|
|
294
|
+
|
|
295
|
+
// Due to a specification blunder in ECMAScript, typeof null is "object",
|
|
296
|
+
// so watch out for that case.
|
|
297
|
+
|
|
298
|
+
if (!value) {
|
|
299
|
+
return "null";
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
// Make an array to hold the partial results of stringifying this object value.
|
|
303
|
+
|
|
304
|
+
gap += indent;
|
|
305
|
+
partial = [];
|
|
306
|
+
|
|
307
|
+
// Is the value an array?
|
|
308
|
+
|
|
309
|
+
if (Object.prototype.toString.apply(value) === "[object Array]") {
|
|
310
|
+
|
|
311
|
+
// The value is an array. Stringify every element. Use null as a placeholder
|
|
312
|
+
// for non-JSON values.
|
|
313
|
+
|
|
314
|
+
length = value.length;
|
|
315
|
+
for (i = 0; i < length; i += 1) {
|
|
316
|
+
partial[i] = str(i, value) || "null";
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
// Join all of the elements together, separated with commas, and wrap them in
|
|
320
|
+
// brackets.
|
|
321
|
+
|
|
322
|
+
v = partial.length === 0
|
|
323
|
+
? "[]"
|
|
324
|
+
: gap
|
|
325
|
+
? (
|
|
326
|
+
"[\n"
|
|
327
|
+
+ gap
|
|
328
|
+
+ partial.join(",\n" + gap)
|
|
329
|
+
+ "\n"
|
|
330
|
+
+ mind
|
|
331
|
+
+ "]"
|
|
332
|
+
)
|
|
333
|
+
: "[" + partial.join(",") + "]";
|
|
334
|
+
gap = mind;
|
|
335
|
+
return v;
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
// If the replacer is an array, use it to select the members to be stringified.
|
|
339
|
+
|
|
340
|
+
if (rep && typeof rep === "object") {
|
|
341
|
+
length = rep.length;
|
|
342
|
+
for (i = 0; i < length; i += 1) {
|
|
343
|
+
if (typeof rep[i] === "string") {
|
|
344
|
+
k = rep[i];
|
|
345
|
+
v = str(k, value);
|
|
346
|
+
if (v) {
|
|
347
|
+
partial.push(quote(k) + (
|
|
348
|
+
(gap)
|
|
349
|
+
? ": "
|
|
350
|
+
: ":"
|
|
351
|
+
) + v);
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
} else {
|
|
356
|
+
|
|
357
|
+
// Otherwise, iterate through all of the keys in the object.
|
|
358
|
+
|
|
359
|
+
for (k in value) {
|
|
360
|
+
if (Object.prototype.hasOwnProperty.call(value, k)) {
|
|
361
|
+
v = str(k, value);
|
|
362
|
+
if (v) {
|
|
363
|
+
partial.push(quote(k) + (
|
|
364
|
+
(gap)
|
|
365
|
+
? ": "
|
|
366
|
+
: ":"
|
|
367
|
+
) + v);
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
// Join all of the member texts together, separated with commas,
|
|
374
|
+
// and wrap them in braces.
|
|
375
|
+
|
|
376
|
+
v = partial.length === 0
|
|
377
|
+
? "{}"
|
|
378
|
+
: gap
|
|
379
|
+
? "{\n" + gap + partial.join(",\n" + gap) + "\n" + mind + "}"
|
|
380
|
+
: "{" + partial.join(",") + "}";
|
|
381
|
+
gap = mind;
|
|
382
|
+
return v;
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
// If the JSON object does not yet have a stringify method, give it one.
|
|
387
|
+
|
|
388
|
+
if (typeof JSON.stringify !== "function") {
|
|
389
|
+
meta = { // table of character substitutions
|
|
390
|
+
"\b": "\\b",
|
|
391
|
+
"\t": "\\t",
|
|
392
|
+
"\n": "\\n",
|
|
393
|
+
"\f": "\\f",
|
|
394
|
+
"\r": "\\r",
|
|
395
|
+
"\"": "\\\"",
|
|
396
|
+
"\\": "\\\\"
|
|
397
|
+
};
|
|
398
|
+
JSON.stringify = function (value, replacer, space) {
|
|
399
|
+
|
|
400
|
+
// The stringify method takes a value and an optional replacer, and an optional
|
|
401
|
+
// space parameter, and returns a JSON text. The replacer can be a function
|
|
402
|
+
// that can replace values, or an array of strings that will select the keys.
|
|
403
|
+
// A default replacer method can be provided. Use of the space parameter can
|
|
404
|
+
// produce text that is more easily readable.
|
|
405
|
+
|
|
406
|
+
var i;
|
|
407
|
+
gap = "";
|
|
408
|
+
indent = "";
|
|
409
|
+
|
|
410
|
+
// If the space parameter is a number, make an indent string containing that
|
|
411
|
+
// many spaces.
|
|
412
|
+
|
|
413
|
+
if (typeof space === "number") {
|
|
414
|
+
for (i = 0; i < space; i += 1) {
|
|
415
|
+
indent += " ";
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
// If the space parameter is a string, it will be used as the indent string.
|
|
419
|
+
|
|
420
|
+
} else if (typeof space === "string") {
|
|
421
|
+
indent = space;
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
// If there is a replacer, it must be a function or an array.
|
|
425
|
+
// Otherwise, throw an error.
|
|
426
|
+
|
|
427
|
+
rep = replacer;
|
|
428
|
+
if (replacer && typeof replacer !== "function" && (
|
|
429
|
+
typeof replacer !== "object"
|
|
430
|
+
|| typeof replacer.length !== "number"
|
|
431
|
+
)) {
|
|
432
|
+
throw new Error("JSON.stringify");
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
// Make a fake root object containing our value under the key of "".
|
|
436
|
+
// Return the result of stringifying the value.
|
|
437
|
+
|
|
438
|
+
return str("", {"": value});
|
|
439
|
+
};
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
|
|
443
|
+
// If the JSON object does not yet have a parse method, give it one.
|
|
444
|
+
|
|
445
|
+
if (typeof JSON.parse !== "function") {
|
|
446
|
+
JSON.parse = function (text, reviver) {
|
|
447
|
+
|
|
448
|
+
// The parse method takes a text and an optional reviver function, and returns
|
|
449
|
+
// a JavaScript value if the text is a valid JSON text.
|
|
450
|
+
|
|
451
|
+
var j;
|
|
452
|
+
|
|
453
|
+
function walk(holder, key) {
|
|
454
|
+
|
|
455
|
+
// The walk method is used to recursively walk the resulting structure so
|
|
456
|
+
// that modifications can be made.
|
|
457
|
+
|
|
458
|
+
var k;
|
|
459
|
+
var v;
|
|
460
|
+
var value = holder[key];
|
|
461
|
+
if (value && typeof value === "object") {
|
|
462
|
+
for (k in value) {
|
|
463
|
+
if (Object.prototype.hasOwnProperty.call(value, k)) {
|
|
464
|
+
v = walk(value, k);
|
|
465
|
+
if (v !== undefined) {
|
|
466
|
+
value[k] = v;
|
|
467
|
+
} else {
|
|
468
|
+
delete value[k];
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
return reviver.call(holder, key, value);
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
|
|
477
|
+
// Parsing happens in four stages. In the first stage, we replace certain
|
|
478
|
+
// Unicode characters with escape sequences. JavaScript handles many characters
|
|
479
|
+
// incorrectly, either silently deleting them, or treating them as line endings.
|
|
480
|
+
|
|
481
|
+
text = String(text);
|
|
482
|
+
rx_dangerous.lastIndex = 0;
|
|
483
|
+
if (rx_dangerous.test(text)) {
|
|
484
|
+
text = text.replace(rx_dangerous, function (a) {
|
|
485
|
+
return (
|
|
486
|
+
"\\u"
|
|
487
|
+
+ ("0000" + a.charCodeAt(0).toString(16)).slice(-4)
|
|
488
|
+
);
|
|
489
|
+
});
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
// In the second stage, we run the text against regular expressions that look
|
|
493
|
+
// for non-JSON patterns. We are especially concerned with "()" and "new"
|
|
494
|
+
// because they can cause invocation, and "=" because it can cause mutation.
|
|
495
|
+
// But just to be safe, we want to reject all unexpected forms.
|
|
496
|
+
|
|
497
|
+
// We split the second stage into 4 regexp operations in order to work around
|
|
498
|
+
// crippling inefficiencies in IE's and Safari's regexp engines. First we
|
|
499
|
+
// replace the JSON backslash pairs with "@" (a non-JSON character). Second, we
|
|
500
|
+
// replace all simple value tokens with "]" characters. Third, we delete all
|
|
501
|
+
// open brackets that follow a colon or comma or that begin the text. Finally,
|
|
502
|
+
// we look to see that the remaining characters are only whitespace or "]" or
|
|
503
|
+
// "," or ":" or "{" or "}". If that is so, then the text is safe for eval.
|
|
504
|
+
|
|
505
|
+
if (
|
|
506
|
+
rx_one.test(
|
|
507
|
+
text
|
|
508
|
+
.replace(rx_two, "@")
|
|
509
|
+
.replace(rx_three, "]")
|
|
510
|
+
.replace(rx_four, "")
|
|
511
|
+
)
|
|
512
|
+
) {
|
|
513
|
+
|
|
514
|
+
// In the third stage we use the eval function to compile the text into a
|
|
515
|
+
// JavaScript structure. The "{" operator is subject to a syntactic ambiguity
|
|
516
|
+
// in JavaScript: it can begin a block or an object literal. We wrap the text
|
|
517
|
+
// in parens to eliminate the ambiguity.
|
|
518
|
+
|
|
519
|
+
j = eval("(" + text + ")");
|
|
520
|
+
|
|
521
|
+
// In the optional fourth stage, we recursively walk the new structure, passing
|
|
522
|
+
// each name/value pair to a reviver function for possible transformation.
|
|
523
|
+
|
|
524
|
+
return (typeof reviver === "function")
|
|
525
|
+
? walk({"": j}, "")
|
|
526
|
+
: j;
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
// If the text is not JSON parseable, then a SyntaxError is thrown.
|
|
530
|
+
|
|
531
|
+
throw new SyntaxError("JSON.parse");
|
|
532
|
+
};
|
|
533
|
+
}
|
|
534
|
+
}());
|
package/package.json
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@arcticnotes/node-wsh",
|
|
3
|
+
"version": "0.0.7",
|
|
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
|
+
"author": "Paul <paul@arcticnotes.com>",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"keywords": [
|
|
8
|
+
"wsh",
|
|
9
|
+
"windows-scripting-host"
|
|
10
|
+
],
|
|
11
|
+
"homepage": "https://github.com/arcticnotes/node-wsh#readme",
|
|
12
|
+
"repository": {
|
|
13
|
+
"type": "git",
|
|
14
|
+
"url": "git+https://github.com/arcticnotes/node-wsh.git"
|
|
15
|
+
},
|
|
16
|
+
"bugs": {
|
|
17
|
+
"url": "https://github.com/arcticnotes/node-wsh/issues"
|
|
18
|
+
},
|
|
19
|
+
"type": "module",
|
|
20
|
+
"exports": {
|
|
21
|
+
".": "./lib/index.js"
|
|
22
|
+
},
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"@arcticnotes/syncline": "0.0.3"
|
|
25
|
+
}
|
|
26
|
+
}
|
package/test/test.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
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
|
+
});
|