Skip to content

Commit

Permalink
fix(node): make stdio streams optional if not present on Deno namespa…
Browse files Browse the repository at this point in the history
…ce (#1829)
  • Loading branch information
bartlomieju authored Jan 17, 2022
1 parent fedb88a commit a0ae169
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 15 deletions.
16 changes: 12 additions & 4 deletions node/_process/streams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,25 +36,33 @@ export const stdin = new Readable({
emitClose: false,
read(this: Readable, size: number) {
const p = Buffer.alloc(size || 16 * 1024);

if (!Deno.stdin) {
this.destroy(
new Error("Deno.stdin is not available in this environment"),
);
return;
}

Deno.stdin.read(p).then((length) => {
this.push(length === null ? null : p.slice(0, length));
}, (error) => {
this.destroy(error);
});
},
}) as _Readable;
stdin.on("close", () => Deno.stdin.close());
stdin.fd = Deno.stdin.rid;
stdin.on("close", () => Deno.stdin?.close());
stdin.fd = Deno.stdin?.rid ?? -1;
Object.defineProperty(stdin, "isTTY", {
enumerable: true,
configurable: true,
get() {
return Deno.isatty(Deno.stdin.rid);
return Deno.isatty?.(Deno.stdin.rid);
},
});
stdin._isRawMode = false;
stdin.setRawMode = (enable) => {
Deno.setRaw(Deno.stdin.rid, enable);
Deno.setRaw?.(Deno.stdin?.rid, enable);
stdin._isRawMode = enable;
return stdin;
};
Expand Down
30 changes: 19 additions & 11 deletions node/internal/streams/readable.js
Original file line number Diff line number Diff line change
Expand Up @@ -1396,9 +1396,15 @@ export default Readable;
export { fromList as _fromList, readableFrom as from, ReadableState, wrap };

// https://github.com/nodejs/node/blob/00738314828074243c9a52a228ab4c68b04259ef/lib/internal/bootstrap/switches/is_main_thread.js#L41
function createWritableStdioStream(writer) {
function createWritableStdioStream(writer, name) {
const stream = new Writable({
write(buf, enc, cb) {
if (!writer) {
this.destroy(
new Error(`Deno.${name} is not available in this environment`),
);
return;
}
writer.writeSync(buf instanceof Uint8Array ? buf : Buffer.from(buf, enc));
cb();
},
Expand All @@ -1410,36 +1416,38 @@ function createWritableStdioStream(writer) {
}
},
});
stream.fd = writer.rid;
stream.fd = writer?.rid ?? -1;
stream.destroySoon = stream.destroy;
stream._isStdio = true;
stream.once("close", () => writer.close());
stream.once("close", () => writer?.close());
Object.defineProperties(stream, {
columns: {
enumerable: true,
configurable: true,
get: () =>
Deno.isatty(writer.rid)
? Deno.consoleSize(writer.rid).columns
Deno.isatty?.(writer?.rid)
? Deno.consoleSize?.(writer?.rid).columns
: undefined,
},
rows: {
enumerable: true,
configurable: true,
get: () =>
Deno.isatty(writer.rid) ? Deno.consoleSize(writer.rid).rows : undefined,
Deno.isatty?.(writer?.rid)
? Deno.consoleSize?.(writer?.rid).rows
: undefined,
},
isTTY: {
enumerable: true,
configurable: true,
get: () => Deno.isatty(writer.rid),
get: () => Deno.isatty?.(writer?.rid),
},
getWindowSize: {
enumerable: true,
configurable: true,
value: () =>
Deno.isatty(writer.rid)
? Object.values(Deno.consoleSize(writer.rid))
Deno.isatty?.(writer?.rid)
? Object.values(Deno.consoleSize?.(writer?.rid))
: undefined,
},
});
Expand All @@ -1448,5 +1456,5 @@ function createWritableStdioStream(writer) {

// The following are exports of the process module, they have to be instantiated here to prevent
// a circular dependency between the process module and the stream module
export const stderr = createWritableStdioStream(Deno.stderr);
export const stdout = createWritableStdioStream(Deno.stdout);
export const stderr = createWritableStdioStream(Deno.stderr, "stderr");
export const stdout = createWritableStdioStream(Deno.stdout, "stdout");

0 comments on commit a0ae169

Please sign in to comment.