Skip to content

Commit

Permalink
Fix stdin
Browse files Browse the repository at this point in the history
  • Loading branch information
ElvishJerricco committed Aug 1, 2018
1 parent ee2caa7 commit 79830be
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 5 deletions.
2 changes: 1 addition & 1 deletion example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"scripts": {
"build": "tsc",
"install": "npm run build",
"test": "npm run build && webpack && node build/main.js"
"test": "npm run build && webpack && (echo bar | node build/main.js)"
},
"author": "Will Fancher",
"dependencies": {
Expand Down
8 changes: 6 additions & 2 deletions example/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ class JSaddleDevice implements Device {

configureFileSystem({ "/jsaddle": new JSaddleDevice() }, (err, fs) => {
console.log(err);
let buf = Buffer.from("hi\n");
fs.write(1, buf, 0, buf.length, null, () => {});
let buf = Buffer.from("foo\n");
fs.write(1, buf, 0, buf.length, null, () => {
fs.read(0, buf, 0, 4, null, (err, n, buf) => {
console.log({ err: err, n: n, buf: buf && buf.toString() });
});
});
});
3 changes: 3 additions & 0 deletions example/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,8 @@ module.exports = {
resolve: {
// Using file:../kernel in package.json requires this
symlinks: false
},
node: {
process: false
}
};
7 changes: 5 additions & 2 deletions kernel/src/stdio_handles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ if (process && !(process as any).browser) {

onData(buf: Buffer): void {
const reqs = this.requests;
this.requests = <[Request]> [];
let nextBuf: Buffer | null = null;
for (const req of reqs) {
let req: Request;
while (req = this.requests.shift()) {
if (buf.length > req.length) {
nextBuf = buf.slice(req.length);
buf = buf.slice(0, req.length);
Expand All @@ -92,6 +92,9 @@ if (process && !(process as any).browser) {
const copied = buf.copy(req.buffer, req.offset);
req.cb(undefined, copied, req.buffer);

if (!nextBuf || nextBuf.length == 0) {
break;
}
buf = nextBuf;
}

Expand Down

0 comments on commit 79830be

Please sign in to comment.