Skip to content

Commit

Permalink
feat: process stdio streams for Deno (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mesteery authored Aug 22, 2021
1 parent 16b89d8 commit cdd4fc9
Show file tree
Hide file tree
Showing 22 changed files with 139 additions and 104 deletions.
4 changes: 2 additions & 2 deletions nodelibs/deno/assert.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from 'https://deno.land/std@0.103.0/node/assert.ts';
export { default } from 'https://deno.land/std@0.103.0/node/assert.ts';
export * from 'https://deno.land/std@0.105.0/node/assert.ts';
export { default } from 'https://deno.land/std@0.105.0/node/assert.ts';
4 changes: 2 additions & 2 deletions nodelibs/deno/buffer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export * from 'https://deno.land/std@0.103.0/node/buffer.ts';
import { default as buffer } from 'https://deno.land/std@0.103.0/node/buffer.ts';
export * from 'https://deno.land/std@0.105.0/node/buffer.ts';
import { default as buffer } from 'https://deno.land/std@0.105.0/node/buffer.ts';
export const kMaxLength = 4294967296;
buffer.kMaxLength = kMaxLength;
export default buffer;
4 changes: 2 additions & 2 deletions nodelibs/deno/child_process.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from 'https://deno.land/std@0.103.0/node/child_process.ts';
export { default } from 'https://deno.land/std@0.103.0/node/child_process.ts';
export * from 'https://deno.land/std@0.105.0/node/child_process.ts';
export { default } from 'https://deno.land/std@0.105.0/node/child_process.ts';
4 changes: 2 additions & 2 deletions nodelibs/deno/console.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from 'https://deno.land/std@0.103.0/node/console.ts';
export { default } from 'https://deno.land/std@0.103.0/node/console.ts';
export * from 'https://deno.land/std@0.105.0/node/console.ts';
export { default } from 'https://deno.land/std@0.105.0/node/console.ts';
4 changes: 2 additions & 2 deletions nodelibs/deno/crypto.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// TODO: update this when deno_std/node will export webcrypto (when Node.js Web Crypto API is stable)
export * from 'https://deno.land/std@0.103.0/node/crypto.ts';
export * from 'https://deno.land/std@0.105.0/node/crypto.ts';
export const webcrypto = globalThis.crypto;
import crypto from 'https://deno.land/std@0.103.0/node/crypto.ts';
import crypto from 'https://deno.land/std@0.105.0/node/crypto.ts';
export default {
...crypto,
webcrypto: globalThis.crypto,
Expand Down
4 changes: 2 additions & 2 deletions nodelibs/deno/events.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from 'https://deno.land/std@0.103.0/node/events.ts';
export { default } from 'https://deno.land/std@0.103.0/node/events.ts';
export * from 'https://deno.land/std@0.105.0/node/events.ts';
export { default } from 'https://deno.land/std@0.105.0/node/events.ts';
6 changes: 3 additions & 3 deletions nodelibs/deno/fs.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { notImplemented } from 'https://deno.land/std@0.103.0/node/_utils.ts';
export * from 'https://deno.land/std@0.103.0/node/fs.ts';
import fs from 'https://deno.land/std@0.103.0/node/fs.ts';
import { notImplemented } from 'https://deno.land/std@0.105.0/node/_utils.ts';
export * from 'https://deno.land/std@0.105.0/node/fs.ts';
import fs from 'https://deno.land/std@0.105.0/node/fs.ts';
import * as promises from './fs/promises.ts';

export { promises }
Expand Down
4 changes: 2 additions & 2 deletions nodelibs/deno/module.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from 'https://deno.land/std@0.103.0/node/module.ts';
export { default } from 'https://deno.land/std@0.103.0/node/module.ts';
export * from 'https://deno.land/std@0.105.0/node/module.ts';
export { default } from 'https://deno.land/std@0.105.0/node/module.ts';
4 changes: 2 additions & 2 deletions nodelibs/deno/os.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from 'https://deno.land/std@0.103.0/node/os.ts';
export { default } from 'https://deno.land/std@0.103.0/node/os.ts';
export * from 'https://deno.land/std@0.105.0/node/os.ts';
export { default } from 'https://deno.land/std@0.105.0/node/os.ts';
4 changes: 2 additions & 2 deletions nodelibs/deno/path.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from 'https://deno.land/std@0.103.0/node/path.ts';
export { default } from 'https://deno.land/std@0.103.0/node/path.ts';
export * from 'https://deno.land/std@0.105.0/node/path.ts';
export { default } from 'https://deno.land/std@0.105.0/node/path.ts';
2 changes: 1 addition & 1 deletion nodelibs/deno/path/posix.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { posix } from 'https://deno.land/std@0.103.0/node/path.ts';
import { posix } from 'https://deno.land/std@0.105.0/node/path.ts';

export const {
resolve,
Expand Down
2 changes: 1 addition & 1 deletion nodelibs/deno/path/win32.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { win32 } from 'https://deno.land/std@0.103.0/node/path.ts';
import { win32 } from 'https://deno.land/std@0.105.0/node/path.ts';

export const {
resolve,
Expand Down
163 changes: 99 additions & 64 deletions nodelibs/deno/process.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
import { notImplemented } from 'https://deno.land/std@0.103.0/node/_utils.ts';
import { fileURLToPath } from 'https://deno.land/std@0.103.0/node/url.ts';
import { notImplemented } from 'https://deno.land/std@0.105.0/node/_utils.ts';
import { fileURLToPath } from 'https://deno.land/std@0.105.0/node/url.ts';
import { Readable, Writable } from 'https://deno.land/std@0.105.0/node/stream.ts';
import { Buffer } from 'https://deno.land/std@0.105.0/node/buffer.ts';

/** https://nodejs.org/api/process.html#process_process_arch */
export const arch = Deno.build.arch;
Expand Down Expand Up @@ -63,6 +65,72 @@ export function nextTick<T extends Array<unknown>>(
}
}


interface _Readable extends Readable {
get isTTY(): boolean;
destroySoon: Readable['destroy'],
fd: number;
_isStdio: false | undefined
};

interface _Writable extends Writable {
get isTTY(): boolean;
get columns(): number;
get rows(): number
destroySoon: Writable['destroy'],
fd: number;
_isStdio: true,
};

// https://github.com/nodejs/node/blob/00738314828074243c9a52a228ab4c68b04259ef/lib/internal/bootstrap/switches/is_main_thread.js#L41
function createWritableStdioStream(writer: typeof Deno.stdout): _Writable {
const stream = new Writable({
write(buf: Uint8Array, enc: string, cb) {
writer.writeSync(buf instanceof Uint8Array ? buf : Buffer.from(buf, enc));
cb();
},
destroy(err, cb) {
cb(err);
this._undestroy();
if (!this._writableState.emitClose) {
queueMicrotask(() => this.emit('close'));
}
},
}) as _Writable;
stream.fd = writer.rid;
stream.destroySoon = stream.destroy;
stream._isStdio = true;
stream.once('close', () => writer.close());
Object.defineProperties(stream, {
columns: {
enumerable: true,
configurable: true,
get(): number {
return Deno.consoleSize(writer.rid).columns;
},
},
rows: {
enumerable: true,
configurable: true,
get(): number {
return Deno.consoleSize(writer.rid).rows;
},
},
isTTY: {
enumerable: true,
configurable: true,
get(): boolean {
return Deno.isatty(writer.rid);
},
}
});
return stream;
}

let stdin: _Readable;
let stdout: _Writable;
let stderr: _Writable;

/** https://nodejs.org/api/process.html#process_process */
// @deprecated `import { process } from 'process'` for backwards compatibility with old deno versions
export const process = {
Expand All @@ -74,71 +142,38 @@ export const process = {
platform,
version,
versions,
get stderr() {
return {
fd: Deno.stderr.rid,
get isTTY(): boolean {
return Deno.isatty(this.fd);
},
pipe(_destination: Deno.Writer, _options: { end: boolean }): void {
// TODO(JayHelton): to be implemented
notImplemented('stderr pipe');
},
// deno-lint-ignore ban-types
write(_chunk: string | Uint8Array, _callback: Function): void {
// TODO(JayHelton): to be implemented
notImplemented('stderr write');
},
// deno-lint-ignore ban-types
on(_event: string, _callback: Function): void {
// TODO(JayHelton): to be implemented
notImplemented('stderr on');
},
};
},
get stdin() {
return {
fd: Deno.stdin.rid,
get isTTY(): boolean {
return Deno.isatty(this.fd);
},
read(_size: number): void {
// TODO(JayHelton): to be implemented
notImplemented('stdin read');
},
// deno-lint-ignore ban-types
on(_event: string, _callback: Function): void {
// TODO(JayHelton): to be implemented
notImplemented('stdin on');
},
};
if (!stdin) {
stdin = new Readable({
// @ts-ignore
read(this: Readable, size: number) {
const p = Buffer.alloc(size || 16 * 1024);
const length = Deno.stdin.readSync(p);
this.push(length === null ? null : p.slice(0, length));
}
}) as _Readable;
stdin.fd = Deno.stdin.rid;
Object.defineProperty(stdin, 'isTTY', {
enumerable: true,
configurable: true,
get() {
return Deno.isatty(Deno.stdin.rid);
},
});
}
return stdin;
},
get stderr() {
if (!stderr) {
stderr = createWritableStdioStream(Deno.stderr);
}
return stderr;
},
get stdout() {
return {
fd: Deno.stdout.rid,
get columns (): number {
return Deno.consoleSize(Deno.stdout.rid).columns;
},
get rows (): number {
return Deno.consoleSize(Deno.stdout.rid).rows;
},
get isTTY(): boolean {
return Deno.isatty(this.fd);
},
pipe(_destination: Deno.Writer, _options: { end: boolean }): void {
// TODO(JayHelton): to be implemented
notImplemented('stdout pipe');
},
// deno-lint-ignore ban-types
write(_chunk: string | Uint8Array, _callback: Function): void {
Deno.stdout.writeSync(typeof _chunk === 'string' ? new TextEncoder().encode(_chunk) : _chunk);
},
// deno-lint-ignore ban-types
on(_event: string, _callback: Function): void {
// TODO(JayHelton): to be implemented
notImplemented('stdout on');
},
};
if (!stdout) {
stdout = createWritableStdioStream(Deno.stdout);
}
return stdout;
},

/** https://nodejs.org/api/process.html#process_process_events */
Expand Down
4 changes: 2 additions & 2 deletions nodelibs/deno/querystring.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from 'https://deno.land/std@0.103.0/node/querystring.ts';
export { default } from 'https://deno.land/std@0.103.0/node/querystring.ts';
export * from 'https://deno.land/std@0.105.0/node/querystring.ts';
export { default } from 'https://deno.land/std@0.105.0/node/querystring.ts';
4 changes: 2 additions & 2 deletions nodelibs/deno/stream.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from 'https://deno.land/std@0.103.0/node/stream.ts';
export { default } from 'https://deno.land/std@0.103.0/node/stream.ts';
export * from 'https://deno.land/std@0.105.0/node/stream.ts';
export { default } from 'https://deno.land/std@0.105.0/node/stream.ts';
4 changes: 2 additions & 2 deletions nodelibs/deno/string_decoder.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from 'https://deno.land/std@0.103.0/node/string_decoder.ts';
export { default } from 'https://deno.land/std@0.103.0/node/string_decoder.ts';
export * from 'https://deno.land/std@0.105.0/node/string_decoder.ts';
export { default } from 'https://deno.land/std@0.105.0/node/string_decoder.ts';
4 changes: 2 additions & 2 deletions nodelibs/deno/timers.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from 'https://deno.land/std@0.103.0/node/timers.ts';
export { default } from 'https://deno.land/std@0.103.0/node/timers.ts';
export * from 'https://deno.land/std@0.105.0/node/timers.ts';
export { default } from 'https://deno.land/std@0.105.0/node/timers.ts';
4 changes: 2 additions & 2 deletions nodelibs/deno/tty.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from 'https://deno.land/std@0.103.0/node/tty.ts';
export { default } from 'https://deno.land/std@0.103.0/node/tty.ts';
export * from 'https://deno.land/std@0.105.0/node/tty.ts';
export { default } from 'https://deno.land/std@0.105.0/node/tty.ts';
4 changes: 2 additions & 2 deletions nodelibs/deno/url.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from 'https://deno.land/std@0.103.0/node/url.ts';
export { default } from 'https://deno.land/std@0.103.0/node/url.ts';
export * from 'https://deno.land/std@0.105.0/node/url.ts';
export { default } from 'https://deno.land/std@0.105.0/node/url.ts';
4 changes: 2 additions & 2 deletions nodelibs/deno/util.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from 'https://deno.land/std@0.103.0/node/util.ts';
export { default } from 'https://deno.land/std@0.103.0/node/util.ts';
export * from 'https://deno.land/std@0.105.0/node/util.ts';
export { default } from 'https://deno.land/std@0.105.0/node/util.ts';
4 changes: 2 additions & 2 deletions nodelibs/deno/util/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { types } from 'https://deno.land/std@0.103.0/node/util.ts';
export { types as default } from 'https://deno.land/std@0.103.0/node/util.ts';
import { types } from 'https://deno.land/std@0.105.0/node/util.ts';
export { types as default } from 'https://deno.land/std@0.105.0/node/util.ts';

export const {
// isExternal,
Expand Down
2 changes: 1 addition & 1 deletion src-browser/process.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ export var title = process.title;
export var umask = process.umask;
export var version = process.version;
export var versions = process.versions;
export var platform = 'browser';
export var platform = 'browser';

0 comments on commit cdd4fc9

Please sign in to comment.