@ohos-ports/stdlib-process-read-stdin 0.0.0-stage → 0.2.3-beta.0

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 ADDED
@@ -0,0 +1,177 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
package/NOTICE ADDED
@@ -0,0 +1 @@
1
+ Copyright (c) 2016-2026 The Stdlib Authors.
package/README.md CHANGED
@@ -1,3 +1,243 @@
1
- # Temporary Holding Version
1
+ <!--
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
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
+
22
+ <details>
23
+ <summary>
24
+ About stdlib...
25
+ </summary>
26
+ <p>We believe in a future in which the web is a preferred environment for numerical computation. To help realize this future, we've built stdlib. stdlib is a standard library, with an emphasis on numerical and scientific computation, written in JavaScript (and C) for execution in browsers and in Node.js.</p>
27
+ <p>The library is fully decomposable, being architected in such a way that you can swap out and mix and match APIs and functionality to cater to your exact preferences and use cases.</p>
28
+ <p>When you use stdlib, you can be absolutely certain that you are using the most thorough, rigorous, well-written, studied, documented, tested, measured, and high-quality code out there.</p>
29
+ <p>To join us in bringing numerical computing to the web, get started by checking us out on <a href="https://github.com/stdlib-js/stdlib">GitHub</a>, and please consider <a href="https://opencollective.com/stdlib">financially supporting stdlib</a>. We greatly appreciate your continued support!</p>
30
+ </details>
31
+
32
+ # stdin
33
+
34
+ [![NPM version][npm-image]][npm-url] [![Build Status][test-image]][test-url] [![Coverage Status][coverage-image]][coverage-url] <!-- [![dependencies][dependencies-image]][dependencies-url] -->
35
+
36
+ > Read data from [`stdin`][@stdlib/streams/node/stdin].
37
+
38
+ <section class="installation">
39
+
40
+ ## Installation
41
+
42
+ ```bash
43
+ npm install @stdlib/process-read-stdin
44
+ ```
45
+
46
+ </section>
47
+
48
+ <section class="usage">
49
+
50
+ ## Usage
51
+
52
+ ```javascript
53
+ var stdin = require( '@stdlib/process-read-stdin' );
54
+ ```
55
+
56
+ #### stdin( \[encoding,] clbk )
57
+
58
+ Reads data from [`stdin`][@stdlib/streams/node/stdin].
59
+
60
+ <!-- run-disable -->
61
+
62
+ ```javascript
63
+ function onRead( error, data ) {
64
+ if ( error ) {
65
+ return console.error( 'Error: %s', error.message );
66
+ }
67
+ console.log( data.toString() );
68
+ // => '...'
69
+ }
70
+
71
+ stdin( onRead );
72
+ ```
73
+
74
+ By default, returned `data` is a [`Buffer`][buffer]. To return a `string` of a specified encoding, provide an `encoding` parameter.
75
+
76
+ <!-- run-disable -->
77
+
78
+ ```javascript
79
+ function onRead( error, data ) {
80
+ if ( error ) {
81
+ return console.error( 'Error: %s', error.message );
82
+ }
83
+ console.log( data );
84
+ // => '...'
85
+ }
86
+
87
+ stdin( 'utf8', onRead );
88
+ ```
89
+
90
+ 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).
91
+
92
+ <!-- run-disable -->
93
+
94
+ ```javascript
95
+ var stream = require( '@stdlib/streams-node-stdin' );
96
+
97
+ function onRead( error, data ) {
98
+ if ( error ) {
99
+ return console.error( 'Error: %s', error.message );
100
+ }
101
+ console.log( data );
102
+ // => ''
103
+ }
104
+
105
+ stream.isTTY = true;
106
+
107
+ stdin( 'utf8', onRead );
108
+ ```
109
+
110
+ </section>
111
+
112
+ <!-- /.usage -->
113
+
114
+ <section class="examples">
115
+
116
+ ## Examples
117
+
118
+ <!-- run-disable -->
119
+
120
+ <!-- eslint no-undef: "error" -->
121
+
122
+ ```javascript
123
+ var string2buffer = require( '@stdlib/buffer-from-string' );
124
+ var stream = require( '@stdlib/streams-node-stdin' );
125
+ var stdin = require( '@stdlib/process-read-stdin' );
126
+
127
+ function onRead( error, data ) {
128
+ if ( error ) {
129
+ console.error( 'Error: %s', error.message );
130
+ } else {
131
+ console.log( data.toString() );
132
+ // => 'beep boop'
133
+ }
134
+ }
135
+
136
+ // Fake not being in a terminal context:
137
+ stream.isTTY = false;
138
+
139
+ // Provide a callback to consume all data from `stdin`:
140
+ stdin( onRead );
141
+
142
+ // Push some data to `stdin`:
143
+ stream.push( string2buffer( 'beep' ) );
144
+ stream.push( string2buffer( ' ' ) );
145
+ stream.push( string2buffer( 'boop' ) );
146
+
147
+ // End the stream:
148
+ stream.push( null );
149
+ ```
150
+
151
+ </section>
152
+
153
+ <!-- /.examples -->
154
+
155
+ <!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
156
+
157
+ <section class="related">
158
+
159
+ </section>
160
+
161
+ <!-- /.related -->
162
+
163
+ <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
164
+
165
+
166
+ <section class="main-repo" >
167
+
168
+ * * *
169
+
170
+ ## Notice
171
+
172
+ This package is part of [stdlib][stdlib], a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more.
173
+
174
+ For more information on the project, filing bug reports and feature requests, and guidance on how to develop [stdlib][stdlib], see the main project [repository][stdlib].
175
+
176
+ #### Community
177
+
178
+ [![Chat][chat-image]][chat-url]
179
+
180
+ ---
181
+
182
+ ## License
183
+
184
+ See [LICENSE][stdlib-license].
185
+
186
+
187
+ ## Copyright
188
+
189
+ Copyright &copy; 2016-2026. The Stdlib [Authors][stdlib-authors].
190
+
191
+ </section>
192
+
193
+ <!-- /.stdlib -->
194
+
195
+ <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
196
+
197
+ <section class="links">
198
+
199
+ [npm-image]: http://img.shields.io/npm/v/@stdlib/process-read-stdin.svg
200
+ [npm-url]: https://npmjs.org/package/@stdlib/process-read-stdin
201
+
202
+ [test-image]: https://github.com/stdlib-js/process-read-stdin/actions/workflows/test.yml/badge.svg?branch=v0.2.3
203
+ [test-url]: https://github.com/stdlib-js/process-read-stdin/actions/workflows/test.yml?query=branch:v0.2.3
204
+
205
+ [coverage-image]: https://img.shields.io/codecov/c/github/stdlib-js/process-read-stdin/main.svg
206
+ [coverage-url]: https://codecov.io/github/stdlib-js/process-read-stdin?branch=main
207
+
208
+ <!--
209
+
210
+ [dependencies-image]: https://img.shields.io/david/stdlib-js/process-read-stdin.svg
211
+ [dependencies-url]: https://david-dm.org/stdlib-js/process-read-stdin/main
212
+
213
+ -->
214
+
215
+ [chat-image]: https://img.shields.io/badge/zulip-join_chat-brightgreen.svg
216
+ [chat-url]: https://stdlib.zulipchat.com
217
+
218
+ [stdlib]: https://github.com/stdlib-js/stdlib
219
+
220
+ [stdlib-authors]: https://github.com/stdlib-js/stdlib/graphs/contributors
221
+
222
+ [umd]: https://github.com/umdjs/umd
223
+ [es-module]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules
224
+
225
+ [deno-url]: https://github.com/stdlib-js/process-read-stdin/tree/deno
226
+ [deno-readme]: https://github.com/stdlib-js/process-read-stdin/blob/deno/README.md
227
+ [umd-url]: https://github.com/stdlib-js/process-read-stdin/tree/umd
228
+ [umd-readme]: https://github.com/stdlib-js/process-read-stdin/blob/umd/README.md
229
+ [esm-url]: https://github.com/stdlib-js/process-read-stdin/tree/esm
230
+ [esm-readme]: https://github.com/stdlib-js/process-read-stdin/blob/esm/README.md
231
+ [branches-url]: https://github.com/stdlib-js/process-read-stdin/blob/main/branches.md
232
+
233
+ [stdlib-license]: https://raw.githubusercontent.com/stdlib-js/process-read-stdin/main/LICENSE
234
+
235
+ [buffer]: https://nodejs.org/api/buffer.html
236
+
237
+ [tty]: https://nodejs.org/api/tty.html#tty_tty
238
+
239
+ [@stdlib/streams/node/stdin]: https://www.npmjs.com/package/@stdlib/streams-node-stdin
240
+
241
+ </section>
242
+
243
+ <!-- /.links -->
package/SECURITY.md ADDED
@@ -0,0 +1,5 @@
1
+ # Security
2
+
3
+ > Policy for reporting security vulnerabilities.
4
+
5
+ See the security policy [in the main project repository](https://github.com/stdlib-js/stdlib/security).
@@ -0,0 +1,3 @@
1
+ /// <reference path="../docs/types/index.d.ts" />
2
+ import stdin from '../docs/types/index';
3
+ export = stdin;
package/dist/index.js ADDED
@@ -0,0 +1,5 @@
1
+ "use strict";var m=function(r,e){return function(){return e||r((e={exports:{}}).exports,e),e.exports}};var l=m(function(T,s){
2
+ var d=require('@stdlib/assert-is-function/dist'),b=require('@stdlib/assert-is-string/dist').isPrimitive,q=require('@stdlib/buffer-ctor/dist'),a=require('@stdlib/buffer-from-string/dist'),t=require('@stdlib/streams-node-stdin/dist'),h=require('@stdlib/utils-next-tick/dist'),o=require('@stdlib/error-tools-fmtprodmsg/dist');function k(){var r,e,i,u;if(arguments.length===1)r=arguments[0];else{if(i=arguments[0],!b(i))throw new TypeError(o('0nj6q',i));r=arguments[1]}if(!d(r))throw new TypeError(o('0nj2b',r));if(i&&t.setEncoding(i),t.isTTY)return h(f);e=[],u=0,t.on("readable",c),t.on("error",g),t.on("end",v);function f(){if(i)return r(null,"");r(null,a(""))}function c(){for(var n;n=t.read(),n!==null;)typeof n=="string"&&(n=a(n)),e.push(n),u+=n.length}function g(n){r(n)}function v(){if(i)return r(null,e.join(""));r(null,q.concat(e,u))}}s.exports=k
3
+ });var p=l();module.exports=p;
4
+ /** @license Apache-2.0 */
5
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../lib/main.js", "../lib/index.js"],
4
+ "sourcesContent": ["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar isFunction = require( '@stdlib/assert-is-function' );\nvar isString = require( '@stdlib/assert-is-string' ).isPrimitive;\nvar Buffer = require( '@stdlib/buffer-ctor' );\nvar string2buffer = require( '@stdlib/buffer-from-string' );\nvar stream = require( '@stdlib/streams-node-stdin' );\nvar nextTick = require( '@stdlib/utils-next-tick' );\nvar format = require( '@stdlib/string-format' );\n\n\n// MAIN //\n\n/**\n* Reads data from `stdin`.\n*\n* @param {(string|null)} [encoding] - string encoding. If set, data will be returned as an encoded `string`.\n* @param {Function} clbk - callback to be invoked upon reading all data from `stdin`\n* @throws {TypeError} `encoding` argument must be a string\n* @throws {TypeError} callback argument must be a function\n* @returns {void}\n*\n* @example\n* function onRead( error, data ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( data.toString() );\n* // => '...'\n* }\n*\n* stdin( onRead );\n*\n* @example\n* function onRead( error, data ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( data );\n* // => '...'\n* }\n*\n* stdin( 'utf8', onRead );\n*/\nfunction stdin() {\n\tvar clbk;\n\tvar data;\n\tvar enc;\n\tvar len;\n\n\tif ( arguments.length === 1 ) {\n\t\tclbk = arguments[ 0 ];\n\t} else {\n\t\tenc = arguments[ 0 ];\n\t\tif ( !isString( enc ) ) {\n\t\t\tthrow new TypeError( format( 'invalid argument. Encoding argument must be a string. Value: `%s`.', enc ) );\n\t\t}\n\t\tclbk = arguments[ 1 ];\n\t}\n\tif ( !isFunction( clbk ) ) {\n\t\tthrow new TypeError( format( 'invalid argument. Callback argument must be a function. Value: `%s`.', clbk ) );\n\t}\n\tif ( enc ) {\n\t\tstream.setEncoding( enc );\n\t}\n\t// 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`.\n\tif ( stream.isTTY ) {\n\t\treturn nextTick( onTick );\n\t}\n\tdata = [];\n\tlen = 0;\n\n\tstream.on( 'readable', onReadable );\n\tstream.on( 'error', onError );\n\tstream.on( 'end', onEnd );\n\n\t/**\n\t* Callback invoked after the next tick.\n\t*\n\t* @private\n\t* @returns {void}\n\t*/\n\tfunction onTick() {\n\t\tif ( enc ) {\n\t\t\treturn clbk( null, '' );\n\t\t}\n\t\tclbk( null, string2buffer( '' ) );\n\t}\n\n\t/**\n\t* Callback invoked once the stream has data to consume.\n\t*\n\t* @private\n\t*/\n\tfunction onReadable() {\n\t\tvar chunk;\n\t\twhile ( true ) {\n\t\t\tchunk = stream.read();\n\t\t\tif ( chunk === null ) {\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tif ( typeof chunk === 'string' ) {\n\t\t\t\tchunk = string2buffer( chunk );\n\t\t\t}\n\t\t\tdata.push( chunk );\n\t\t\tlen += chunk.length;\n\t\t}\n\t}\n\n\t/**\n\t* Callback invoked upon encountering a stream error.\n\t*\n\t* @private\n\t* @param {Error} error - error object\n\t*/\n\tfunction onError( error ) {\n\t\tclbk( error );\n\t}\n\n\t/**\n\t* Callback invoked after all data has been consumed.\n\t*\n\t* @private\n\t* @returns {void}\n\t*/\n\tfunction onEnd() {\n\t\tif ( enc ) {\n\t\t\t// Return a string...\n\t\t\treturn clbk( null, data.join( '' ) );\n\t\t}\n\t\t// Return a buffer...\n\t\tclbk( null, Buffer.concat( data, len ) );\n\t}\n}\n\n\n// EXPORTS //\n\nmodule.exports = stdin;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Read data from `stdin`.\n*\n* @module @stdlib/process-read-stdin\n*\n* @example\n* var stdin = require( '@stdlib/process-read-stdin' );\n*\n* function onRead( error, data ) {\n* if ( error ) {\n* throw error;\n* }\n* console.log( data.toString() );\n* // returns '...'\n* }\n*\n* stdin( onRead );\n*/\n\n// MODULES //\n\nvar main = require( './main.js' );\n\n\n// EXPORTS //\n\nmodule.exports = main;\n"],
5
+ "mappings": "uGAAA,IAAAA,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAa,QAAS,4BAA6B,EACnDC,EAAW,QAAS,0BAA2B,EAAE,YACjDC,EAAS,QAAS,qBAAsB,EACxCC,EAAgB,QAAS,4BAA6B,EACtDC,EAAS,QAAS,4BAA6B,EAC/CC,EAAW,QAAS,yBAA0B,EAC9CC,EAAS,QAAS,uBAAwB,EAoC9C,SAASC,GAAQ,CAChB,IAAIC,EACAC,EACAC,EACAC,EAEJ,GAAK,UAAU,SAAW,EACzBH,EAAO,UAAW,CAAE,MACd,CAEN,GADAE,EAAM,UAAW,CAAE,EACd,CAACT,EAAUS,CAAI,EACnB,MAAM,IAAI,UAAWJ,EAAQ,qEAAsEI,CAAI,CAAE,EAE1GF,EAAO,UAAW,CAAE,CACrB,CACA,GAAK,CAACR,EAAYQ,CAAK,EACtB,MAAM,IAAI,UAAWF,EAAQ,uEAAwEE,CAAK,CAAE,EAM7G,GAJKE,GACJN,EAAO,YAAaM,CAAI,EAGpBN,EAAO,MACX,OAAOC,EAAUO,CAAO,EAEzBH,EAAO,CAAC,EACRE,EAAM,EAENP,EAAO,GAAI,WAAYS,CAAW,EAClCT,EAAO,GAAI,QAASU,CAAQ,EAC5BV,EAAO,GAAI,MAAOW,CAAM,EAQxB,SAASH,GAAS,CACjB,GAAKF,EACJ,OAAOF,EAAM,KAAM,EAAG,EAEvBA,EAAM,KAAML,EAAe,EAAG,CAAE,CACjC,CAOA,SAASU,GAAa,CAErB,QADIG,EAEHA,EAAQZ,EAAO,KAAK,EACfY,IAAU,MAGV,OAAOA,GAAU,WACrBA,EAAQb,EAAea,CAAM,GAE9BP,EAAK,KAAMO,CAAM,EACjBL,GAAOK,EAAM,MAEf,CAQA,SAASF,EAASG,EAAQ,CACzBT,EAAMS,CAAM,CACb,CAQA,SAASF,GAAQ,CAChB,GAAKL,EAEJ,OAAOF,EAAM,KAAMC,EAAK,KAAM,EAAG,CAAE,EAGpCD,EAAM,KAAMN,EAAO,OAAQO,EAAME,CAAI,CAAE,CACxC,CACD,CAKAZ,EAAO,QAAUQ,ICrHjB,IAAIW,EAAO,IAKX,OAAO,QAAUA",
6
+ "names": ["require_main", "__commonJSMin", "exports", "module", "isFunction", "isString", "Buffer", "string2buffer", "stream", "nextTick", "format", "stdin", "clbk", "data", "enc", "len", "onTick", "onReadable", "onError", "onEnd", "chunk", "error", "main"]
7
+ }
@@ -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;
package/lib/browser.js ADDED
@@ -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( '@stdlib/assert-is-function' );
24
+ var format = require( '@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;
package/lib/index.js ADDED
@@ -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( '@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;
package/lib/main.js ADDED
@@ -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( '@stdlib/assert-is-function' );
24
+ var isString = require( '@stdlib/assert-is-string' ).isPrimitive;
25
+ var Buffer = require( '@stdlib/buffer-ctor' );
26
+ var string2buffer = require( '@stdlib/buffer-from-string' );
27
+ var stream = require( '@stdlib/streams-node-stdin' );
28
+ var nextTick = require( '@stdlib/utils-next-tick' );
29
+ var format = require( '@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;
package/package.json CHANGED
@@ -1,6 +1,80 @@
1
1
  {
2
2
  "name": "@ohos-ports/stdlib-process-read-stdin",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.2.3-beta.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
+ "lib": "./lib",
21
+ "dist": "./dist"
22
+ },
23
+ "types": "./docs/types",
24
+ "scripts": {},
25
+ "homepage": "https://stdlib.io",
26
+ "repository": {
27
+ "type": "git",
28
+ "url": "https://github.com/ohos-ports/ohos-ports.git",
29
+ "directory": "ports/stdlib-process-read-stdin/0.2.3"
30
+ },
31
+ "bugs": {
32
+ "url": "https://github.com/ohos-ports/ohos-ports/issues"
33
+ },
34
+ "dependencies": {
35
+ "@stdlib/assert-is-function": "^0.2.3",
36
+ "@stdlib/assert-is-string": "^0.2.3",
37
+ "@stdlib/buffer-ctor": "^0.2.2",
38
+ "@stdlib/buffer-from-string": "^0.2.2",
39
+ "@stdlib/streams-node-stdin": "^0.2.3",
40
+ "@stdlib/string-format": "^0.2.3",
41
+ "@stdlib/utils-next-tick": "^0.2.3",
42
+ "@stdlib/error-tools-fmtprodmsg": "^0.2.3"
43
+ },
44
+ "devDependencies": {},
45
+ "engines": {
46
+ "node": ">=0.10.0",
47
+ "npm": ">2.7.0"
48
+ },
49
+ "os": [
50
+ "aix",
51
+ "darwin",
52
+ "freebsd",
53
+ "linux",
54
+ "macos",
55
+ "openbsd",
56
+ "sunos",
57
+ "win32",
58
+ "windows",
59
+ "openharmony"
60
+ ],
61
+ "keywords": [
62
+ "stdlib",
63
+ "stdutils",
64
+ "stdutil",
65
+ "utils",
66
+ "util",
67
+ "utilities",
68
+ "utility",
69
+ "process",
70
+ "stdin",
71
+ "tty",
72
+ "input",
73
+ "data",
74
+ "stream"
75
+ ],
76
+ "funding": {
77
+ "type": "opencollective",
78
+ "url": "https://opencollective.com/stdlib"
79
+ }
80
+ }