From cdd4fc9d5cb55a73d540c670516d3eb6ea945d22 Mon Sep 17 00:00:00 2001 From: Mestery Date: Sun, 22 Aug 2021 20:10:52 +0200 Subject: [PATCH] feat: process stdio streams for Deno (#27) --- nodelibs/deno/assert.ts | 4 +- nodelibs/deno/buffer.ts | 4 +- nodelibs/deno/child_process.ts | 4 +- nodelibs/deno/console.ts | 4 +- nodelibs/deno/crypto.ts | 4 +- nodelibs/deno/events.ts | 4 +- nodelibs/deno/fs.ts | 6 +- nodelibs/deno/module.ts | 4 +- nodelibs/deno/os.ts | 4 +- nodelibs/deno/path.ts | 4 +- nodelibs/deno/path/posix.ts | 2 +- nodelibs/deno/path/win32.ts | 2 +- nodelibs/deno/process.ts | 163 +++++++++++++++++++------------- nodelibs/deno/querystring.ts | 4 +- nodelibs/deno/stream.ts | 4 +- nodelibs/deno/string_decoder.ts | 4 +- nodelibs/deno/timers.ts | 4 +- nodelibs/deno/tty.ts | 4 +- nodelibs/deno/url.ts | 4 +- nodelibs/deno/util.ts | 4 +- nodelibs/deno/util/types.ts | 4 +- src-browser/process.js | 2 +- 22 files changed, 139 insertions(+), 104 deletions(-) diff --git a/nodelibs/deno/assert.ts b/nodelibs/deno/assert.ts index 7747382..0b8348d 100644 --- a/nodelibs/deno/assert.ts +++ b/nodelibs/deno/assert.ts @@ -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'; diff --git a/nodelibs/deno/buffer.ts b/nodelibs/deno/buffer.ts index 324eb0a..293b24f 100644 --- a/nodelibs/deno/buffer.ts +++ b/nodelibs/deno/buffer.ts @@ -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; diff --git a/nodelibs/deno/child_process.ts b/nodelibs/deno/child_process.ts index 6f169ba..bccc4c2 100644 --- a/nodelibs/deno/child_process.ts +++ b/nodelibs/deno/child_process.ts @@ -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'; diff --git a/nodelibs/deno/console.ts b/nodelibs/deno/console.ts index 72c5406..e1e6205 100644 --- a/nodelibs/deno/console.ts +++ b/nodelibs/deno/console.ts @@ -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'; diff --git a/nodelibs/deno/crypto.ts b/nodelibs/deno/crypto.ts index 3df7bee..ffefc62 100644 --- a/nodelibs/deno/crypto.ts +++ b/nodelibs/deno/crypto.ts @@ -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, diff --git a/nodelibs/deno/events.ts b/nodelibs/deno/events.ts index d80fa40..e09faae 100644 --- a/nodelibs/deno/events.ts +++ b/nodelibs/deno/events.ts @@ -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'; diff --git a/nodelibs/deno/fs.ts b/nodelibs/deno/fs.ts index 2cf8f25..522948d 100644 --- a/nodelibs/deno/fs.ts +++ b/nodelibs/deno/fs.ts @@ -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 } diff --git a/nodelibs/deno/module.ts b/nodelibs/deno/module.ts index 8706892..3135e87 100644 --- a/nodelibs/deno/module.ts +++ b/nodelibs/deno/module.ts @@ -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'; diff --git a/nodelibs/deno/os.ts b/nodelibs/deno/os.ts index 681b675..46603cf 100644 --- a/nodelibs/deno/os.ts +++ b/nodelibs/deno/os.ts @@ -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'; diff --git a/nodelibs/deno/path.ts b/nodelibs/deno/path.ts index 699ac9b..7e4ce84 100644 --- a/nodelibs/deno/path.ts +++ b/nodelibs/deno/path.ts @@ -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'; diff --git a/nodelibs/deno/path/posix.ts b/nodelibs/deno/path/posix.ts index 1f7119d..20c624b 100644 --- a/nodelibs/deno/path/posix.ts +++ b/nodelibs/deno/path/posix.ts @@ -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, diff --git a/nodelibs/deno/path/win32.ts b/nodelibs/deno/path/win32.ts index ee2a8b9..d4948ed 100644 --- a/nodelibs/deno/path/win32.ts +++ b/nodelibs/deno/path/win32.ts @@ -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, diff --git a/nodelibs/deno/process.ts b/nodelibs/deno/process.ts index a7ee44e..4aae9bc 100644 --- a/nodelibs/deno/process.ts +++ b/nodelibs/deno/process.ts @@ -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; @@ -63,6 +65,72 @@ export function nextTick>( } } + +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 = { @@ -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 */ diff --git a/nodelibs/deno/querystring.ts b/nodelibs/deno/querystring.ts index 89ab9eb..acd0adf 100644 --- a/nodelibs/deno/querystring.ts +++ b/nodelibs/deno/querystring.ts @@ -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'; diff --git a/nodelibs/deno/stream.ts b/nodelibs/deno/stream.ts index 8fe83e4..756b4f2 100644 --- a/nodelibs/deno/stream.ts +++ b/nodelibs/deno/stream.ts @@ -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'; diff --git a/nodelibs/deno/string_decoder.ts b/nodelibs/deno/string_decoder.ts index 097db5b..1a24d27 100644 --- a/nodelibs/deno/string_decoder.ts +++ b/nodelibs/deno/string_decoder.ts @@ -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'; diff --git a/nodelibs/deno/timers.ts b/nodelibs/deno/timers.ts index 64276ad..5533959 100644 --- a/nodelibs/deno/timers.ts +++ b/nodelibs/deno/timers.ts @@ -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'; diff --git a/nodelibs/deno/tty.ts b/nodelibs/deno/tty.ts index 13f8d0d..24b2332 100644 --- a/nodelibs/deno/tty.ts +++ b/nodelibs/deno/tty.ts @@ -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'; diff --git a/nodelibs/deno/url.ts b/nodelibs/deno/url.ts index 88a805a..c09a43c 100644 --- a/nodelibs/deno/url.ts +++ b/nodelibs/deno/url.ts @@ -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'; diff --git a/nodelibs/deno/util.ts b/nodelibs/deno/util.ts index 2fd2393..268c4a8 100644 --- a/nodelibs/deno/util.ts +++ b/nodelibs/deno/util.ts @@ -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'; diff --git a/nodelibs/deno/util/types.ts b/nodelibs/deno/util/types.ts index f46025c..4f60f95 100644 --- a/nodelibs/deno/util/types.ts +++ b/nodelibs/deno/util/types.ts @@ -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, diff --git a/src-browser/process.js b/src-browser/process.js index ecc2a7f..96a6e3d 100644 --- a/src-browser/process.js +++ b/src-browser/process.js @@ -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'; \ No newline at end of file +export var platform = 'browser';