From 79830bee65c766f969c14a5536c62000cad3d5f2 Mon Sep 17 00:00:00 2001 From: Will Fancher Date: Wed, 1 Aug 2018 08:37:19 -0400 Subject: [PATCH] Fix stdin --- example/package.json | 2 +- example/src/index.ts | 8 ++++++-- example/webpack.config.js | 3 +++ kernel/src/stdio_handles.ts | 7 +++++-- 4 files changed, 15 insertions(+), 5 deletions(-) diff --git a/example/package.json b/example/package.json index ad1fe29..9ef23df 100644 --- a/example/package.json +++ b/example/package.json @@ -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": { diff --git a/example/src/index.ts b/example/src/index.ts index eb2f0d4..d67fc5c 100644 --- a/example/src/index.ts +++ b/example/src/index.ts @@ -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() }); + }); + }); }); diff --git a/example/webpack.config.js b/example/webpack.config.js index fc441df..61e6378 100644 --- a/example/webpack.config.js +++ b/example/webpack.config.js @@ -12,5 +12,8 @@ module.exports = { resolve: { // Using file:../kernel in package.json requires this symlinks: false + }, + node: { + process: false } }; diff --git a/kernel/src/stdio_handles.ts b/kernel/src/stdio_handles.ts index 1c4ea94..6c686b2 100644 --- a/kernel/src/stdio_handles.ts +++ b/kernel/src/stdio_handles.ts @@ -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); @@ -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; }