|
|
@ -15,7 +15,6 @@ import { |
|
|
|
|
|
|
|
|
|
|
|
class Access extends EventEmitter { |
|
|
|
class Access extends EventEmitter { |
|
|
|
private ps: ChildProcess; |
|
|
|
private ps: ChildProcess; |
|
|
|
private queue: string[] = []; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
constructor({ |
|
|
|
constructor({ |
|
|
|
eventEmitterOptions = {}, |
|
|
|
eventEmitterOptions = {}, |
|
|
@ -50,19 +49,10 @@ class Access extends EventEmitter { |
|
|
|
...restSpawnOptions, |
|
|
|
...restSpawnOptions, |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
let stderr = ''; |
|
|
|
|
|
|
|
let stdout = ''; |
|
|
|
let stdout = ''; |
|
|
|
|
|
|
|
|
|
|
|
ps.stderr?.setEncoding('utf-8').on('data', (chunk: string) => { |
|
|
|
ps.stderr?.setEncoding('utf-8').on('data', (chunk: string) => { |
|
|
|
stderr += chunk; |
|
|
|
sherr(`anvil-access-module daemon stderr: ${chunk}`); |
|
|
|
|
|
|
|
|
|
|
|
const scriptId: string | undefined = this.queue[0]; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (scriptId) { |
|
|
|
|
|
|
|
sherr(`${Access.event(scriptId, 'stderr')}: ${stderr}`); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
stderr = ''; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
ps.stdout?.setEncoding('utf-8').on('data', (chunk: string) => { |
|
|
|
ps.stdout?.setEncoding('utf-8').on('data', (chunk: string) => { |
|
|
@ -73,9 +63,10 @@ class Access extends EventEmitter { |
|
|
|
// 1. ~a is the shorthand for -(a + 1)
|
|
|
|
// 1. ~a is the shorthand for -(a + 1)
|
|
|
|
// 2. negatives are evaluated to true
|
|
|
|
// 2. negatives are evaluated to true
|
|
|
|
while (~nindex) { |
|
|
|
while (~nindex) { |
|
|
|
const scriptId = this.queue.shift(); |
|
|
|
const scriptId = stdout.substring(0, 36); |
|
|
|
|
|
|
|
const output = stdout.substring(36, nindex); |
|
|
|
|
|
|
|
|
|
|
|
if (scriptId) this.emit(scriptId, stdout.substring(0, nindex)); |
|
|
|
if (scriptId) this.emit(scriptId, output); |
|
|
|
|
|
|
|
|
|
|
|
stdout = stdout.substring(nindex + 1); |
|
|
|
stdout = stdout.substring(nindex + 1); |
|
|
|
nindex = stdout.indexOf('\n'); |
|
|
|
nindex = stdout.indexOf('\n'); |
|
|
@ -97,15 +88,12 @@ class Access extends EventEmitter { |
|
|
|
this.stop(); |
|
|
|
this.stop(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private static event(scriptId: string, category: 'stderr'): string { |
|
|
|
public interact<T>(operation: string, ...args: string[]) { |
|
|
|
return `${scriptId}-${category}`; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public interact<T>(command: string, ...args: string[]) { |
|
|
|
|
|
|
|
const { stdin } = this.ps; |
|
|
|
const { stdin } = this.ps; |
|
|
|
|
|
|
|
|
|
|
|
const scriptId = uuid(); |
|
|
|
const scriptId = uuid(); |
|
|
|
const script = `${command} ${args.join(' ')}\n`; |
|
|
|
const command = `${operation} ${args.join(' ')}`; |
|
|
|
|
|
|
|
const script = `${scriptId} ${command}\n`; |
|
|
|
|
|
|
|
|
|
|
|
const promise = new Promise<T>((resolve, reject) => { |
|
|
|
const promise = new Promise<T>((resolve, reject) => { |
|
|
|
this.once(scriptId, (data) => { |
|
|
|
this.once(scriptId, (data) => { |
|
|
@ -123,7 +111,6 @@ class Access extends EventEmitter { |
|
|
|
|
|
|
|
|
|
|
|
shvar({ scriptId, script }, 'Access interact: '); |
|
|
|
shvar({ scriptId, script }, 'Access interact: '); |
|
|
|
|
|
|
|
|
|
|
|
this.queue.push(scriptId); |
|
|
|
|
|
|
|
stdin?.write(script); |
|
|
|
stdin?.write(script); |
|
|
|
|
|
|
|
|
|
|
|
return promise; |
|
|
|
return promise; |
|
|
|