Skip to content

Commit 616d8be

Browse files
radaretrufae
authored andcommitted
Add fixes from the spawn implementation in typescript
1 parent 8c145ec commit 616d8be

File tree

2 files changed

+31
-53
lines changed

2 files changed

+31
-53
lines changed

typescript/r2pipe/spawn.ts

Lines changed: 7 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,10 @@ export class R2PipeSpawn extends R2PipeBase {
5858
}
5959

6060
function pipeCmdOutput(r2, proc, data) {
61-
r2.running = true;
6261
let len = data.length;
6362

6463
if (r2.pipeQueue.length < 1) {
65-
return new Error('r2pipe error: No pending commands for incomming data');
64+
return new Error('r2pipe error: No pending commands for incoming data');
6665
}
6766

6867
if (data.length > 1 && data[0] === 0x00) {
@@ -71,8 +70,7 @@ function pipeCmdOutput(r2, proc, data) {
7170
if (data[len - 1] !== 0x00) {
7271
r2.pipeQueue[0].result += data.toString();
7372
data = "";
74-
// return;
75-
/// return r2.pipeQueue[0].result;
73+
return;
7674
}
7775

7876
while (data[len - 1] == 0x00) {
@@ -93,8 +91,9 @@ function pipeCmdOutput(r2, proc, data) {
9391
} catch (e) {
9492
r2.pipeQueue[0].cb(e, null);
9593
}
94+
} else {
95+
r2.running = false;
9696
}
97-
r2.running = false;
9897
}
9998

10099
function r2bind(child, cb, r2cmd) {
@@ -118,14 +117,14 @@ function r2bind(child, cb, r2cmd) {
118117
},
119118
/* Run cmd and return plaintext output */
120119
cmd: (command, commandCallback) => {
121-
// s = util.cleanCmd(s);
122120
r2.pipeQueue.push({
123121
cmd: command,
124122
cb: commandCallback,
125123
result: '',
126124
error: null
127125
});
128-
if (r2.pipeQueue.length === 1) {
126+
if (!r2.running && r2.pipeQueue.length === 1) {
127+
r2.running = true;
129128
child.stdin.write(command + '\n');
130129
}
131130
},
@@ -142,48 +141,10 @@ function r2bind(child, cb, r2cmd) {
142141
}
143142
};
144143

145-
/* handle SDTERR message */
146-
if (child.stderr !== null) {
147-
child.stderr.on('data', function (data) {
148-
/* Set as running for connect & launch methods */
149-
if (typeof errmsg === 'string') {
150-
errmsg += data.toString();
151-
if (errmsg.length > 1024 * 32) {
152-
errmsg = null;
153-
}
154-
}
155-
if (!r2.running && (typeof r2cmd !== 'string')) {
156-
r2.running = true;
157-
if (typeof cb === 'function') {
158-
cb(null, r2);
159-
} else {
160-
throw new Error('Callback in .cmd() is not a function');
161-
}
162-
}
163-
});
164-
}
165-
166-
/* handle STDOUT nessages */
144+
/* handle STDOUT messages */
167145
if (child.stdout !== null) {
168146
child.stdout.on('data', data => {
169-
if (r2.running) {
170-
console.error("race");
171-
}
172147
pipeCmdOutput(r2, child, data);
173-
/*
174-
// console.log("received data: " + data);
175-
// Set as running for pipe method
176-
if (running) {
177-
console.log("RUNING");
178-
if (typeof r2cmd === 'string') {
179-
pipeCmdOutput.bind(r2)(child, data, cb);
180-
}
181-
} else {
182-
console.log("not RUNING");
183-
running = true;
184-
cb(null, r2);
185-
}
186-
*/
187148
});
188149
} else {
189150
cb(null, r2); // Callback for connect

typescript/test-http.ts

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,32 @@
22
// node --insecure-http-parser test-http.js
33

44
import * as r2pipe from "./dist/index.js";
5+
import { R2PipeCmdInterface } from "./dist/base.js";
56
// import * as r2pipe from "r2pipe-ts";
67

7-
async function main() : Promise<string> {
8+
const R2_SERVER_URL = process.env.R2_SERVER_URL || "http://127.0.0.1:9090";
9+
10+
async function main(): Promise<string> {
811
console.log("Hello R2Pipe for TypeScript");
9-
const r2 = await r2pipe.open("http://127.0.0.1:9090");
10-
const res = await r2.cmd("?E Hello TypeScript");
11-
console.log(res);
12-
await r2.quit();
13-
return "Done";
12+
let r2: R2PipeCmdInterface | null = null;
13+
14+
try {
15+
r2 = await r2pipe.open(R2_SERVER_URL);
16+
const res: string = await r2.cmd("?E Hello TypeScript");
17+
console.log(res);
18+
return "Done";
19+
} catch (error) {
20+
if (error instanceof Error) {
21+
console.error(`Error connecting to radare2 server at ${R2_SERVER_URL}:`, error.message);
22+
} else {
23+
console.error("Unknown error occurred:", error);
24+
}
25+
throw error;
26+
} finally {
27+
if (r2) {
28+
await r2.quit();
29+
}
30+
}
1431
}
1532

16-
main().then(console.log).catch(console.error);
33+
main().catch(console.error);

0 commit comments

Comments
 (0)