forked from denoland/deno
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: implement node:tty (denoland#20892)
Fixes denoland#21012 Closes denoland#20855 Fixes denoland#20890 Fixes denoland#20611 Fixes denoland#20336 Fixes `create-svelte` from denoland#17248 Fixes more reports here: - denoland#6529 (comment) - denoland#6529 (comment) - denoland#6529 (comment)
- Loading branch information
1 parent
8b54f0f
commit f62a652
Showing
14 changed files
with
325 additions
and
177 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// deno-fmt-ignore-file | ||
// deno-lint-ignore-file | ||
|
||
// Copyright Joyent and Node contributors. All rights reserved. MIT license. | ||
// Taken from Node 18.12.1 | ||
// This file is automatically generated by `tools/node_compat/setup.ts`. Do not modify this file manually. | ||
|
||
'use strict'; | ||
require('../common'); | ||
|
||
// This test ensures that Node.js doesn't crash on `process.stdin.emit("end")`. | ||
// https://github.com/nodejs/node/issues/1068 | ||
|
||
process.stdin.emit('end'); |
74 changes: 74 additions & 0 deletions
74
cli/tests/node_compat/test/parallel/test-ttywrap-invalid-fd.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// deno-fmt-ignore-file | ||
// deno-lint-ignore-file | ||
|
||
// Copyright Joyent and Node contributors. All rights reserved. MIT license. | ||
// Taken from Node 18.12.1 | ||
// This file is automatically generated by `tools/node_compat/setup.ts`. Do not modify this file manually. | ||
|
||
// Flags: --expose-internals | ||
'use strict'; | ||
|
||
// const common = require('../common'); | ||
const tty = require('tty'); | ||
// const { internalBinding } = require('internal/test/binding'); | ||
// const { | ||
// UV_EBADF, | ||
// UV_EINVAL | ||
// } = internalBinding('uv'); | ||
const assert = require('assert'); | ||
|
||
assert.throws( | ||
() => new tty.WriteStream(-1), | ||
{ | ||
code: 'ERR_INVALID_FD', | ||
name: 'RangeError', | ||
message: '"fd" must be a positive integer: -1' | ||
} | ||
); | ||
|
||
// { | ||
// const info = { | ||
// code: common.isWindows ? 'EBADF' : 'EINVAL', | ||
// message: common.isWindows ? 'bad file descriptor' : 'invalid argument', | ||
// errno: common.isWindows ? UV_EBADF : UV_EINVAL, | ||
// syscall: 'uv_tty_init' | ||
// }; | ||
|
||
// const suffix = common.isWindows ? | ||
// 'EBADF (bad file descriptor)' : 'EINVAL (invalid argument)'; | ||
// const message = `TTY initialization failed: uv_tty_init returned ${suffix}`; | ||
|
||
// assert.throws( | ||
// () => { | ||
// common.runWithInvalidFD((fd) => { | ||
// new tty.WriteStream(fd); | ||
// }); | ||
// }, { | ||
// code: 'ERR_TTY_INIT_FAILED', | ||
// name: 'SystemError', | ||
// message, | ||
// info | ||
// } | ||
// ); | ||
|
||
// assert.throws( | ||
// () => { | ||
// common.runWithInvalidFD((fd) => { | ||
// new tty.ReadStream(fd); | ||
// }); | ||
// }, { | ||
// code: 'ERR_TTY_INIT_FAILED', | ||
// name: 'SystemError', | ||
// message, | ||
// info | ||
// }); | ||
// } | ||
|
||
assert.throws( | ||
() => new tty.ReadStream(-1), | ||
{ | ||
code: 'ERR_INVALID_FD', | ||
name: 'RangeError', | ||
message: '"fd" must be a positive integer: -1' | ||
} | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. | ||
|
||
use deno_core::error::AnyError; | ||
use deno_core::op2; | ||
use deno_core::OpState; | ||
use deno_core::ResourceHandle; | ||
use deno_core::ResourceHandleFd; | ||
|
||
#[repr(u32)] | ||
enum HandleType { | ||
#[allow(dead_code)] | ||
Tcp = 0, | ||
Tty, | ||
#[allow(dead_code)] | ||
Udp, | ||
File, | ||
Pipe, | ||
Unknown, | ||
} | ||
|
||
#[op2(fast)] | ||
pub fn op_node_guess_handle_type( | ||
state: &mut OpState, | ||
rid: u32, | ||
) -> Result<u32, AnyError> { | ||
let handle = state.resource_table.get_handle(rid)?; | ||
|
||
let handle_type = match handle { | ||
ResourceHandle::Fd(handle) => guess_handle_type(handle), | ||
_ => HandleType::Unknown, | ||
}; | ||
|
||
Ok(handle_type as u32) | ||
} | ||
|
||
#[cfg(windows)] | ||
fn guess_handle_type(handle: ResourceHandleFd) -> HandleType { | ||
use winapi::um::consoleapi::GetConsoleMode; | ||
use winapi::um::fileapi::GetFileType; | ||
use winapi::um::winbase::FILE_TYPE_CHAR; | ||
use winapi::um::winbase::FILE_TYPE_DISK; | ||
use winapi::um::winbase::FILE_TYPE_PIPE; | ||
|
||
// SAFETY: Call to win32 fileapi. `handle` is a valid fd. | ||
match unsafe { GetFileType(handle) } { | ||
FILE_TYPE_DISK => HandleType::File, | ||
FILE_TYPE_CHAR => { | ||
let mut mode = 0; | ||
// SAFETY: Call to win32 consoleapi. `handle` is a valid fd. | ||
// `mode` is a valid pointer. | ||
if unsafe { GetConsoleMode(handle, &mut mode) } == 1 { | ||
HandleType::Tty | ||
} else { | ||
HandleType::File | ||
} | ||
} | ||
FILE_TYPE_PIPE => HandleType::Pipe, | ||
_ => HandleType::Unknown, | ||
} | ||
} | ||
|
||
#[cfg(unix)] | ||
fn guess_handle_type(handle: ResourceHandleFd) -> HandleType { | ||
use std::io::IsTerminal; | ||
// SAFETY: The resource remains open for the duration of borrow_raw. | ||
if unsafe { std::os::fd::BorrowedFd::borrow_raw(handle).is_terminal() } { | ||
return HandleType::Tty; | ||
} | ||
|
||
// SAFETY: It is safe to zero-initialize a `libc::stat` struct. | ||
let mut s = unsafe { std::mem::zeroed() }; | ||
// SAFETY: Call to libc | ||
if unsafe { libc::fstat(handle, &mut s) } == 1 { | ||
return HandleType::Unknown; | ||
} | ||
|
||
match s.st_mode & 61440 { | ||
libc::S_IFREG | libc::S_IFCHR => HandleType::File, | ||
libc::S_IFIFO => HandleType::Pipe, | ||
libc::S_IFSOCK => HandleType::Tcp, | ||
_ => HandleType::Unknown, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.