@ohos-ports/stdlib-process 0.0.0-stage → 0.4.1-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +177 -0
- package/NOTICE +1 -0
- package/README.md +220 -2
- package/SECURITY.md +5 -0
- package/argv/README.md +104 -0
- package/argv/docs/repl.txt +22 -0
- package/argv/docs/types/index.d.ts +33 -0
- package/argv/docs/types/test.ts +28 -0
- package/argv/lib/browser.js +28 -0
- package/argv/lib/index.js +40 -0
- package/argv/lib/main.js +43 -0
- package/argv/package.json +70 -0
- package/chdir/README.md +124 -0
- package/chdir/docs/repl.txt +25 -0
- package/chdir/docs/types/index.d.ts +42 -0
- package/chdir/docs/types/test.ts +45 -0
- package/chdir/lib/index.js +42 -0
- package/chdir/lib/main.js +52 -0
- package/chdir/lib/polyfill.js +42 -0
- package/chdir/package.json +73 -0
- package/cwd/README.md +140 -0
- package/cwd/bin/cli +60 -0
- package/cwd/docs/repl.txt +17 -0
- package/cwd/docs/types/index.d.ts +35 -0
- package/cwd/docs/types/test.ts +33 -0
- package/cwd/docs/usage.txt +8 -0
- package/cwd/etc/cli_opts.json +14 -0
- package/cwd/lib/index.js +40 -0
- package/cwd/lib/main.js +44 -0
- package/cwd/lib/polyfill.js +40 -0
- package/cwd/package.json +75 -0
- package/docs/types/index.d.ts +225 -0
- package/env/README.md +104 -0
- package/env/docs/repl.txt +12 -0
- package/env/docs/types/index.d.ts +32 -0
- package/env/docs/types/test.ts +28 -0
- package/env/lib/browser.js +28 -0
- package/env/lib/index.js +39 -0
- package/env/lib/main.js +42 -0
- package/env/package.json +72 -0
- package/exec-path/README.md +90 -0
- package/exec-path/docs/repl.txt +13 -0
- package/exec-path/docs/types/index.d.ts +36 -0
- package/exec-path/docs/types/test.ts +28 -0
- package/exec-path/lib/browser.js +23 -0
- package/exec-path/lib/index.js +55 -0
- package/exec-path/lib/main.js +46 -0
- package/exec-path/package.json +72 -0
- package/getegid/README.md +107 -0
- package/getegid/docs/repl.txt +20 -0
- package/getegid/docs/types/index.d.ts +38 -0
- package/getegid/docs/types/test.ts +33 -0
- package/getegid/lib/index.js +39 -0
- package/getegid/lib/main.js +45 -0
- package/getegid/lib/native.js +44 -0
- package/getegid/lib/polyfill.js +40 -0
- package/getegid/package.json +76 -0
- package/geteuid/README.md +111 -0
- package/geteuid/docs/repl.txt +20 -0
- package/geteuid/docs/types/index.d.ts +38 -0
- package/geteuid/docs/types/test.ts +33 -0
- package/geteuid/lib/index.js +39 -0
- package/geteuid/lib/main.js +45 -0
- package/geteuid/lib/native.js +44 -0
- package/geteuid/lib/polyfill.js +40 -0
- package/geteuid/package.json +76 -0
- package/getgid/README.md +107 -0
- package/getgid/docs/repl.txt +19 -0
- package/getgid/docs/types/index.d.ts +38 -0
- package/getgid/docs/types/test.ts +33 -0
- package/getgid/lib/index.js +39 -0
- package/getgid/lib/main.js +45 -0
- package/getgid/lib/native.js +44 -0
- package/getgid/lib/polyfill.js +40 -0
- package/getgid/package.json +75 -0
- package/getuid/README.md +111 -0
- package/getuid/docs/repl.txt +19 -0
- package/getuid/docs/types/index.d.ts +38 -0
- package/getuid/docs/types/test.ts +33 -0
- package/getuid/lib/index.js +39 -0
- package/getuid/lib/main.js +45 -0
- package/getuid/lib/native.js +44 -0
- package/getuid/lib/polyfill.js +40 -0
- package/getuid/package.json +75 -0
- package/lib/index.js +150 -0
- package/node-version/README.md +135 -0
- package/node-version/bin/cli +60 -0
- package/node-version/docs/repl.txt +12 -0
- package/node-version/docs/types/index.d.ts +42 -0
- package/node-version/docs/types/test.ts +28 -0
- package/node-version/docs/usage.txt +8 -0
- package/node-version/etc/cli_opts.json +14 -0
- package/node-version/lib/browser.js +23 -0
- package/node-version/lib/index.js +60 -0
- package/node-version/lib/main.js +43 -0
- package/node-version/lib/process.js +28 -0
- package/node-version/package.json +73 -0
- package/package.json +121 -4
- package/read-stdin/README.md +152 -0
- package/read-stdin/docs/types/index.d.ts +108 -0
- package/read-stdin/docs/types/test.ts +73 -0
- package/read-stdin/lib/browser.js +66 -0
- package/read-stdin/lib/index.js +47 -0
- package/read-stdin/lib/main.js +159 -0
- package/read-stdin/package.json +67 -0
- package/umask/README.md +281 -0
- package/umask/bin/cli +84 -0
- package/umask/docs/repl.txt +133 -0
- package/umask/docs/types/index.d.ts +69 -0
- package/umask/docs/types/test.ts +62 -0
- package/umask/docs/usage.txt +9 -0
- package/umask/etc/cli_opts.json +22 -0
- package/umask/lib/browser.js +133 -0
- package/umask/lib/from_symbolic.js +218 -0
- package/umask/lib/index.js +40 -0
- package/umask/lib/main.js +128 -0
- package/umask/lib/octal_codes.json +10 -0
- package/umask/lib/to_symbolic.js +57 -0
- package/umask/package.json +78 -0
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
<!--
|
|
2
|
+
|
|
3
|
+
@license Apache-2.0
|
|
4
|
+
|
|
5
|
+
Copyright (c) 2018 The Stdlib Authors.
|
|
6
|
+
|
|
7
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
8
|
+
you may not use this file except in compliance with the License.
|
|
9
|
+
You may obtain a copy of the License at
|
|
10
|
+
|
|
11
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
12
|
+
|
|
13
|
+
Unless required by applicable law or agreed to in writing, software
|
|
14
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
15
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
16
|
+
See the License for the specific language governing permissions and
|
|
17
|
+
limitations under the License.
|
|
18
|
+
|
|
19
|
+
-->
|
|
20
|
+
|
|
21
|
+
# stdin
|
|
22
|
+
|
|
23
|
+
> Read data from [`stdin`][@stdlib/streams/node/stdin].
|
|
24
|
+
|
|
25
|
+
<section class="usage">
|
|
26
|
+
|
|
27
|
+
## Usage
|
|
28
|
+
|
|
29
|
+
```javascript
|
|
30
|
+
var stdin = require( '@stdlib/process/read-stdin' );
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
#### stdin( \[encoding,] clbk )
|
|
34
|
+
|
|
35
|
+
Reads data from [`stdin`][@stdlib/streams/node/stdin].
|
|
36
|
+
|
|
37
|
+
<!-- run-disable -->
|
|
38
|
+
|
|
39
|
+
```javascript
|
|
40
|
+
function onRead( error, data ) {
|
|
41
|
+
if ( error ) {
|
|
42
|
+
return console.error( 'Error: %s', error.message );
|
|
43
|
+
}
|
|
44
|
+
console.log( data.toString() );
|
|
45
|
+
// => '...'
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
stdin( onRead );
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
By default, returned `data` is a [`Buffer`][buffer]. To return a `string` of a specified encoding, provide an `encoding` parameter.
|
|
52
|
+
|
|
53
|
+
<!-- run-disable -->
|
|
54
|
+
|
|
55
|
+
```javascript
|
|
56
|
+
function onRead( error, data ) {
|
|
57
|
+
if ( error ) {
|
|
58
|
+
return console.error( 'Error: %s', error.message );
|
|
59
|
+
}
|
|
60
|
+
console.log( data );
|
|
61
|
+
// => '...'
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
stdin( 'utf8', onRead );
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
When a file's calling Node.js process is running in a [TTY][tty] context (i.e., no [`stdin`][@stdlib/streams/node/stdin]), `data` will either be an empty [`Buffer`][buffer] (no encoding provided) or an empty `string` (encoding provided).
|
|
68
|
+
|
|
69
|
+
<!-- run-disable -->
|
|
70
|
+
|
|
71
|
+
```javascript
|
|
72
|
+
var stream = require( '@stdlib/streams/node/stdin' );
|
|
73
|
+
|
|
74
|
+
function onRead( error, data ) {
|
|
75
|
+
if ( error ) {
|
|
76
|
+
return console.error( 'Error: %s', error.message );
|
|
77
|
+
}
|
|
78
|
+
console.log( data );
|
|
79
|
+
// => ''
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
stream.isTTY = true;
|
|
83
|
+
|
|
84
|
+
stdin( 'utf8', onRead );
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
</section>
|
|
88
|
+
|
|
89
|
+
<!-- /.usage -->
|
|
90
|
+
|
|
91
|
+
<section class="examples">
|
|
92
|
+
|
|
93
|
+
## Examples
|
|
94
|
+
|
|
95
|
+
<!-- run-disable -->
|
|
96
|
+
|
|
97
|
+
<!-- eslint no-undef: "error" -->
|
|
98
|
+
|
|
99
|
+
```javascript
|
|
100
|
+
var string2buffer = require( '@stdlib/buffer/from-string' );
|
|
101
|
+
var stream = require( '@stdlib/streams/node/stdin' );
|
|
102
|
+
var stdin = require( '@stdlib/process/read-stdin' );
|
|
103
|
+
|
|
104
|
+
function onRead( error, data ) {
|
|
105
|
+
if ( error ) {
|
|
106
|
+
console.error( 'Error: %s', error.message );
|
|
107
|
+
} else {
|
|
108
|
+
console.log( data.toString() );
|
|
109
|
+
// => 'beep boop'
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// Fake not being in a terminal context:
|
|
114
|
+
stream.isTTY = false;
|
|
115
|
+
|
|
116
|
+
// Provide a callback to consume all data from `stdin`:
|
|
117
|
+
stdin( onRead );
|
|
118
|
+
|
|
119
|
+
// Push some data to `stdin`:
|
|
120
|
+
stream.push( string2buffer( 'beep' ) );
|
|
121
|
+
stream.push( string2buffer( ' ' ) );
|
|
122
|
+
stream.push( string2buffer( 'boop' ) );
|
|
123
|
+
|
|
124
|
+
// End the stream:
|
|
125
|
+
stream.push( null );
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
</section>
|
|
129
|
+
|
|
130
|
+
<!-- /.examples -->
|
|
131
|
+
|
|
132
|
+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
|
|
133
|
+
|
|
134
|
+
<section class="related">
|
|
135
|
+
|
|
136
|
+
</section>
|
|
137
|
+
|
|
138
|
+
<!-- /.related -->
|
|
139
|
+
|
|
140
|
+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
|
|
141
|
+
|
|
142
|
+
<section class="links">
|
|
143
|
+
|
|
144
|
+
[buffer]: https://nodejs.org/api/buffer.html
|
|
145
|
+
|
|
146
|
+
[tty]: https://nodejs.org/api/tty.html#tty_tty
|
|
147
|
+
|
|
148
|
+
[@stdlib/streams/node/stdin]: https://www.npmjs.com/package/@stdlib/streams-node-stdin
|
|
149
|
+
|
|
150
|
+
</section>
|
|
151
|
+
|
|
152
|
+
<!-- /.links -->
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* @license Apache-2.0
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2021 The Stdlib Authors.
|
|
5
|
+
*
|
|
6
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
7
|
+
* you may not use this file except in compliance with the License.
|
|
8
|
+
* You may obtain a copy of the License at
|
|
9
|
+
*
|
|
10
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
11
|
+
*
|
|
12
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
13
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
14
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
15
|
+
* See the License for the specific language governing permissions and
|
|
16
|
+
* limitations under the License.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
// TypeScript Version: 4.1
|
|
20
|
+
|
|
21
|
+
/// <reference types="node"/>
|
|
22
|
+
|
|
23
|
+
import { Buffer } from 'buffer';
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Callback invoked upon reading all data from `stdin`.
|
|
27
|
+
*
|
|
28
|
+
* @param err - error object
|
|
29
|
+
* @param data - data contents
|
|
30
|
+
*/
|
|
31
|
+
type Callback = ( err: Error | null, data: Buffer | string ) => void;
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Interface for reading from `stdin`.
|
|
35
|
+
*/
|
|
36
|
+
interface StdIn {
|
|
37
|
+
/**
|
|
38
|
+
* Reads data from `stdin`.
|
|
39
|
+
*
|
|
40
|
+
* @param encoding - string encoding. If set, data will be returned as an encoded `string`
|
|
41
|
+
* @param clbk - callback to be invoked upon reading all data from `stdin`
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* function onRead( error, data ) {
|
|
45
|
+
* if ( error ) {
|
|
46
|
+
* throw error;
|
|
47
|
+
* }
|
|
48
|
+
* console.log( data );
|
|
49
|
+
* // => '...'
|
|
50
|
+
* }
|
|
51
|
+
*
|
|
52
|
+
* stdin( 'utf8', onRead );
|
|
53
|
+
*/
|
|
54
|
+
( encoding: string | null, clbk: Callback ): void;
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Reads data from `stdin`.
|
|
58
|
+
*
|
|
59
|
+
* @param clbk - callback to be invoked upon reading all data from `stdin`
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
62
|
+
* function onRead( error, data ) {
|
|
63
|
+
* if ( error ) {
|
|
64
|
+
* throw error;
|
|
65
|
+
* }
|
|
66
|
+
* console.log( data.toString() );
|
|
67
|
+
* // => '...'
|
|
68
|
+
* }
|
|
69
|
+
*
|
|
70
|
+
* stdin( onRead );
|
|
71
|
+
*/
|
|
72
|
+
( clbk: Callback ): void;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Reads data from `stdin`.
|
|
77
|
+
*
|
|
78
|
+
* @param encoding - string encoding. If set, data will be returned as an encoded `string`
|
|
79
|
+
* @param clbk - callback to be invoked upon reading all data from `stdin`
|
|
80
|
+
*
|
|
81
|
+
* @example
|
|
82
|
+
* function onRead( error, data ) {
|
|
83
|
+
* if ( error ) {
|
|
84
|
+
* throw error;
|
|
85
|
+
* }
|
|
86
|
+
* console.log( data.toString() );
|
|
87
|
+
* // => '...'
|
|
88
|
+
* }
|
|
89
|
+
*
|
|
90
|
+
* stdin( onRead );
|
|
91
|
+
*
|
|
92
|
+
* @example
|
|
93
|
+
* function onRead( error, data ) {
|
|
94
|
+
* if ( error ) {
|
|
95
|
+
* throw error;
|
|
96
|
+
* }
|
|
97
|
+
* console.log( data );
|
|
98
|
+
* // => '...'
|
|
99
|
+
* }
|
|
100
|
+
*
|
|
101
|
+
* stdin( 'utf8', onRead );
|
|
102
|
+
*/
|
|
103
|
+
declare var stdin: StdIn;
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
// EXPORTS //
|
|
107
|
+
|
|
108
|
+
export = stdin;
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* @license Apache-2.0
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2021 The Stdlib Authors.
|
|
5
|
+
*
|
|
6
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
7
|
+
* you may not use this file except in compliance with the License.
|
|
8
|
+
* You may obtain a copy of the License at
|
|
9
|
+
*
|
|
10
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
11
|
+
*
|
|
12
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
13
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
14
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
15
|
+
* See the License for the specific language governing permissions and
|
|
16
|
+
* limitations under the License.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
import stdin = require( './index' );
|
|
20
|
+
|
|
21
|
+
const onRead = ( error: Error | null, data: string | Buffer ) => {
|
|
22
|
+
if ( error || !data ) {
|
|
23
|
+
throw error;
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
// TESTS //
|
|
29
|
+
|
|
30
|
+
// The function does not have a return value...
|
|
31
|
+
{
|
|
32
|
+
stdin( onRead ); // $ExpectType void
|
|
33
|
+
stdin( 'utf-8', onRead ); // $ExpectType void
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// The compiler throws an error if the function is provided an `encoding` argument which is not a string or null...
|
|
37
|
+
{
|
|
38
|
+
stdin( 123, onRead ); // $ExpectError
|
|
39
|
+
stdin( false, onRead ); // $ExpectError
|
|
40
|
+
stdin( true, onRead ); // $ExpectError
|
|
41
|
+
stdin( undefined, onRead ); // $ExpectError
|
|
42
|
+
stdin( [], onRead ); // $ExpectError
|
|
43
|
+
stdin( {}, onRead ); // $ExpectError
|
|
44
|
+
stdin( ( x: number ): number => x, onRead ); // $ExpectError
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// The compiler throws an error if the function is provided a last argument which is not a function with the expected signature...
|
|
48
|
+
{
|
|
49
|
+
stdin( 'abc' ); // $ExpectError
|
|
50
|
+
stdin( 1 ); // $ExpectError
|
|
51
|
+
stdin( false ); // $ExpectError
|
|
52
|
+
stdin( true ); // $ExpectError
|
|
53
|
+
stdin( null ); // $ExpectError
|
|
54
|
+
stdin( undefined ); // $ExpectError
|
|
55
|
+
stdin( [] ); // $ExpectError
|
|
56
|
+
stdin( {} ); // $ExpectError
|
|
57
|
+
stdin( ( x: number ): number => x ); // $ExpectError
|
|
58
|
+
|
|
59
|
+
stdin( 'utf-8', 'abc' ); // $ExpectError
|
|
60
|
+
stdin( 'utf-8', 1 ); // $ExpectError
|
|
61
|
+
stdin( 'utf-8', false ); // $ExpectError
|
|
62
|
+
stdin( 'utf-8', true ); // $ExpectError
|
|
63
|
+
stdin( 'utf-8', null ); // $ExpectError
|
|
64
|
+
stdin( 'utf-8', undefined ); // $ExpectError
|
|
65
|
+
stdin( 'utf-8', [] ); // $ExpectError
|
|
66
|
+
stdin( 'utf-8', {} ); // $ExpectError
|
|
67
|
+
stdin( 'utf-8', ( x: number ): number => x ); // $ExpectError
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// The compiler throws an error if the function is provided an unsupported number of arguments...
|
|
71
|
+
{
|
|
72
|
+
stdin(); // $ExpectError
|
|
73
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license Apache-2.0
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2018 The Stdlib Authors.
|
|
5
|
+
*
|
|
6
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
7
|
+
* you may not use this file except in compliance with the License.
|
|
8
|
+
* You may obtain a copy of the License at
|
|
9
|
+
*
|
|
10
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
11
|
+
*
|
|
12
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
13
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
14
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
15
|
+
* See the License for the specific language governing permissions and
|
|
16
|
+
* limitations under the License.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
'use strict';
|
|
20
|
+
|
|
21
|
+
// MODULES //
|
|
22
|
+
|
|
23
|
+
var isFunction = require( '@ohos-ports/stdlib-assert/is-function' );
|
|
24
|
+
var format = require( '@ohos-ports/stdlib-string/format' );
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
// MAIN //
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Browser shim.
|
|
31
|
+
*
|
|
32
|
+
* @private
|
|
33
|
+
* @param {(string|null)} [encoding] - string encoding
|
|
34
|
+
* @param {Callback} clbk - callback to be invoked upon completion
|
|
35
|
+
* @throws {TypeError} callback argument must be a function
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* function onRead( error ) {
|
|
39
|
+
* if ( error ) {
|
|
40
|
+
* return console.error( error.message );
|
|
41
|
+
* }
|
|
42
|
+
* }
|
|
43
|
+
*
|
|
44
|
+
* stdin( onRead );
|
|
45
|
+
*/
|
|
46
|
+
function stdin() {
|
|
47
|
+
var clbk = arguments[ arguments.length-1 ];
|
|
48
|
+
if ( !isFunction( clbk ) ) {
|
|
49
|
+
throw new TypeError( format( 'invalid argument. Callback argument must be a function. Value: `%s`.', clbk ) );
|
|
50
|
+
}
|
|
51
|
+
setTimeout( onTimeout, 0 );
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Callback invoked upon a timeout.
|
|
55
|
+
*
|
|
56
|
+
* @private
|
|
57
|
+
*/
|
|
58
|
+
function onTimeout() {
|
|
59
|
+
clbk( new Error( 'invalid operation. The environment does not support reading from `stdin`.' ) );
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
// EXPORTS //
|
|
65
|
+
|
|
66
|
+
module.exports = stdin;
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license Apache-2.0
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2018 The Stdlib Authors.
|
|
5
|
+
*
|
|
6
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
7
|
+
* you may not use this file except in compliance with the License.
|
|
8
|
+
* You may obtain a copy of the License at
|
|
9
|
+
*
|
|
10
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
11
|
+
*
|
|
12
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
13
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
14
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
15
|
+
* See the License for the specific language governing permissions and
|
|
16
|
+
* limitations under the License.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
'use strict';
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Read data from `stdin`.
|
|
23
|
+
*
|
|
24
|
+
* @module @stdlib/process/read-stdin
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* var stdin = require( '@ohos-ports/stdlib-process/read-stdin' );
|
|
28
|
+
*
|
|
29
|
+
* function onRead( error, data ) {
|
|
30
|
+
* if ( error ) {
|
|
31
|
+
* throw error;
|
|
32
|
+
* }
|
|
33
|
+
* console.log( data.toString() );
|
|
34
|
+
* // returns '...'
|
|
35
|
+
* }
|
|
36
|
+
*
|
|
37
|
+
* stdin( onRead );
|
|
38
|
+
*/
|
|
39
|
+
|
|
40
|
+
// MODULES //
|
|
41
|
+
|
|
42
|
+
var main = require( './main.js' );
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
// EXPORTS //
|
|
46
|
+
|
|
47
|
+
module.exports = main;
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license Apache-2.0
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2018 The Stdlib Authors.
|
|
5
|
+
*
|
|
6
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
7
|
+
* you may not use this file except in compliance with the License.
|
|
8
|
+
* You may obtain a copy of the License at
|
|
9
|
+
*
|
|
10
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
11
|
+
*
|
|
12
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
13
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
14
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
15
|
+
* See the License for the specific language governing permissions and
|
|
16
|
+
* limitations under the License.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
'use strict';
|
|
20
|
+
|
|
21
|
+
// MODULES //
|
|
22
|
+
|
|
23
|
+
var isFunction = require( '@ohos-ports/stdlib-assert/is-function' );
|
|
24
|
+
var isString = require( '@ohos-ports/stdlib-assert/is-string' ).isPrimitive;
|
|
25
|
+
var Buffer = require( '@ohos-ports/stdlib-buffer/ctor' );
|
|
26
|
+
var string2buffer = require( '@ohos-ports/stdlib-buffer/from-string' );
|
|
27
|
+
var stream = require( '@ohos-ports/stdlib-streams/node/stdin' );
|
|
28
|
+
var nextTick = require( '@ohos-ports/stdlib-utils/next-tick' );
|
|
29
|
+
var format = require( '@ohos-ports/stdlib-string/format' );
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
// MAIN //
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Reads data from `stdin`.
|
|
36
|
+
*
|
|
37
|
+
* @param {(string|null)} [encoding] - string encoding. If set, data will be returned as an encoded `string`.
|
|
38
|
+
* @param {Function} clbk - callback to be invoked upon reading all data from `stdin`
|
|
39
|
+
* @throws {TypeError} `encoding` argument must be a string
|
|
40
|
+
* @throws {TypeError} callback argument must be a function
|
|
41
|
+
* @returns {void}
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* function onRead( error, data ) {
|
|
45
|
+
* if ( error ) {
|
|
46
|
+
* throw error;
|
|
47
|
+
* }
|
|
48
|
+
* console.log( data.toString() );
|
|
49
|
+
* // => '...'
|
|
50
|
+
* }
|
|
51
|
+
*
|
|
52
|
+
* stdin( onRead );
|
|
53
|
+
*
|
|
54
|
+
* @example
|
|
55
|
+
* function onRead( error, data ) {
|
|
56
|
+
* if ( error ) {
|
|
57
|
+
* throw error;
|
|
58
|
+
* }
|
|
59
|
+
* console.log( data );
|
|
60
|
+
* // => '...'
|
|
61
|
+
* }
|
|
62
|
+
*
|
|
63
|
+
* stdin( 'utf8', onRead );
|
|
64
|
+
*/
|
|
65
|
+
function stdin() {
|
|
66
|
+
var clbk;
|
|
67
|
+
var data;
|
|
68
|
+
var enc;
|
|
69
|
+
var len;
|
|
70
|
+
|
|
71
|
+
if ( arguments.length === 1 ) {
|
|
72
|
+
clbk = arguments[ 0 ];
|
|
73
|
+
} else {
|
|
74
|
+
enc = arguments[ 0 ];
|
|
75
|
+
if ( !isString( enc ) ) {
|
|
76
|
+
throw new TypeError( format( 'invalid argument. Encoding argument must be a string. Value: `%s`.', enc ) );
|
|
77
|
+
}
|
|
78
|
+
clbk = arguments[ 1 ];
|
|
79
|
+
}
|
|
80
|
+
if ( !isFunction( clbk ) ) {
|
|
81
|
+
throw new TypeError( format( 'invalid argument. Callback argument must be a function. Value: `%s`.', clbk ) );
|
|
82
|
+
}
|
|
83
|
+
if ( enc ) {
|
|
84
|
+
stream.setEncoding( enc );
|
|
85
|
+
}
|
|
86
|
+
// If the calling file is being run as a script and not as part of a pipeline, we should not expect to receive anything on `stdin`.
|
|
87
|
+
if ( stream.isTTY ) {
|
|
88
|
+
return nextTick( onTick );
|
|
89
|
+
}
|
|
90
|
+
data = [];
|
|
91
|
+
len = 0;
|
|
92
|
+
|
|
93
|
+
stream.on( 'readable', onReadable );
|
|
94
|
+
stream.on( 'error', onError );
|
|
95
|
+
stream.on( 'end', onEnd );
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Callback invoked after the next tick.
|
|
99
|
+
*
|
|
100
|
+
* @private
|
|
101
|
+
* @returns {void}
|
|
102
|
+
*/
|
|
103
|
+
function onTick() {
|
|
104
|
+
if ( enc ) {
|
|
105
|
+
return clbk( null, '' );
|
|
106
|
+
}
|
|
107
|
+
clbk( null, string2buffer( '' ) );
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Callback invoked once the stream has data to consume.
|
|
112
|
+
*
|
|
113
|
+
* @private
|
|
114
|
+
*/
|
|
115
|
+
function onReadable() {
|
|
116
|
+
var chunk;
|
|
117
|
+
while ( true ) {
|
|
118
|
+
chunk = stream.read();
|
|
119
|
+
if ( chunk === null ) {
|
|
120
|
+
break;
|
|
121
|
+
}
|
|
122
|
+
if ( typeof chunk === 'string' ) {
|
|
123
|
+
chunk = string2buffer( chunk );
|
|
124
|
+
}
|
|
125
|
+
data.push( chunk );
|
|
126
|
+
len += chunk.length;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Callback invoked upon encountering a stream error.
|
|
132
|
+
*
|
|
133
|
+
* @private
|
|
134
|
+
* @param {Error} error - error object
|
|
135
|
+
*/
|
|
136
|
+
function onError( error ) {
|
|
137
|
+
clbk( error );
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Callback invoked after all data has been consumed.
|
|
142
|
+
*
|
|
143
|
+
* @private
|
|
144
|
+
* @returns {void}
|
|
145
|
+
*/
|
|
146
|
+
function onEnd() {
|
|
147
|
+
if ( enc ) {
|
|
148
|
+
// Return a string...
|
|
149
|
+
return clbk( null, data.join( '' ) );
|
|
150
|
+
}
|
|
151
|
+
// Return a buffer...
|
|
152
|
+
clbk( null, Buffer.concat( data, len ) );
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
// EXPORTS //
|
|
158
|
+
|
|
159
|
+
module.exports = stdin;
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@stdlib/process/read-stdin",
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"description": "Read data from stdin.",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"author": {
|
|
7
|
+
"name": "The Stdlib Authors",
|
|
8
|
+
"url": "https://github.com/stdlib-js/stdlib/graphs/contributors"
|
|
9
|
+
},
|
|
10
|
+
"contributors": [
|
|
11
|
+
{
|
|
12
|
+
"name": "The Stdlib Authors",
|
|
13
|
+
"url": "https://github.com/stdlib-js/stdlib/graphs/contributors"
|
|
14
|
+
}
|
|
15
|
+
],
|
|
16
|
+
"main": "./lib",
|
|
17
|
+
"browser": "./lib/browser.js",
|
|
18
|
+
"directories": {
|
|
19
|
+
"doc": "./docs",
|
|
20
|
+
"example": "./examples",
|
|
21
|
+
"lib": "./lib",
|
|
22
|
+
"test": "./test"
|
|
23
|
+
},
|
|
24
|
+
"types": "./docs/types",
|
|
25
|
+
"scripts": {},
|
|
26
|
+
"homepage": "https://github.com/stdlib-js/stdlib",
|
|
27
|
+
"repository": {
|
|
28
|
+
"type": "git",
|
|
29
|
+
"url": "git://github.com/stdlib-js/stdlib.git"
|
|
30
|
+
},
|
|
31
|
+
"bugs": {
|
|
32
|
+
"url": "https://github.com/stdlib-js/stdlib/issues"
|
|
33
|
+
},
|
|
34
|
+
"dependencies": {},
|
|
35
|
+
"devDependencies": {},
|
|
36
|
+
"engines": {
|
|
37
|
+
"node": ">=0.10.0",
|
|
38
|
+
"npm": ">2.7.0"
|
|
39
|
+
},
|
|
40
|
+
"os": [
|
|
41
|
+
"aix",
|
|
42
|
+
"darwin",
|
|
43
|
+
"freebsd",
|
|
44
|
+
"linux",
|
|
45
|
+
"macos",
|
|
46
|
+
"openbsd",
|
|
47
|
+
"sunos",
|
|
48
|
+
"win32",
|
|
49
|
+
"windows",
|
|
50
|
+
"openharmony"
|
|
51
|
+
],
|
|
52
|
+
"keywords": [
|
|
53
|
+
"stdlib",
|
|
54
|
+
"stdutils",
|
|
55
|
+
"stdutil",
|
|
56
|
+
"utils",
|
|
57
|
+
"util",
|
|
58
|
+
"utilities",
|
|
59
|
+
"utility",
|
|
60
|
+
"process",
|
|
61
|
+
"stdin",
|
|
62
|
+
"tty",
|
|
63
|
+
"input",
|
|
64
|
+
"data",
|
|
65
|
+
"stream"
|
|
66
|
+
]
|
|
67
|
+
}
|