@ohos-ports/stdlib-fs 0.0.0-stage → 0.4.1-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.
Files changed (173) hide show
  1. package/LICENSE +177 -0
  2. package/NOTICE +1 -0
  3. package/README.md +230 -2
  4. package/SECURITY.md +5 -0
  5. package/append-file/README.md +239 -0
  6. package/append-file/bin/cli +110 -0
  7. package/append-file/docs/repl.txt +72 -0
  8. package/append-file/docs/types/index.d.ts +133 -0
  9. package/append-file/docs/types/test.ts +193 -0
  10. package/append-file/docs/usage.txt +10 -0
  11. package/append-file/etc/cli_opts.json +22 -0
  12. package/append-file/lib/async.js +58 -0
  13. package/append-file/lib/index.js +61 -0
  14. package/append-file/lib/sync.js +58 -0
  15. package/append-file/package.json +69 -0
  16. package/close/README.md +172 -0
  17. package/close/docs/repl.txt +45 -0
  18. package/close/docs/types/index.d.ts +135 -0
  19. package/close/docs/types/test.ts +89 -0
  20. package/close/lib/index.js +73 -0
  21. package/close/lib/main.js +68 -0
  22. package/close/lib/sync.js +65 -0
  23. package/close/package.json +64 -0
  24. package/docs/types/index.d.ts +502 -0
  25. package/exists/README.md +252 -0
  26. package/exists/bin/cli +83 -0
  27. package/exists/docs/repl.txt +42 -0
  28. package/exists/docs/types/index.d.ts +112 -0
  29. package/exists/docs/types/test.ts +87 -0
  30. package/exists/docs/usage.txt +8 -0
  31. package/exists/etc/cli_opts.json +14 -0
  32. package/exists/lib/async.js +85 -0
  33. package/exists/lib/index.js +66 -0
  34. package/exists/lib/sync.js +62 -0
  35. package/exists/package.json +69 -0
  36. package/lib/index.js +186 -0
  37. package/open/README.md +204 -0
  38. package/open/docs/repl.txt +65 -0
  39. package/open/docs/types/index.d.ts +150 -0
  40. package/open/docs/types/test.ts +129 -0
  41. package/open/lib/defaults.json +4 -0
  42. package/open/lib/index.js +63 -0
  43. package/open/lib/main.js +64 -0
  44. package/open/lib/sync.js +68 -0
  45. package/open/package.json +64 -0
  46. package/package.json +67 -4
  47. package/read-dir/README.md +232 -0
  48. package/read-dir/bin/cli +83 -0
  49. package/read-dir/docs/repl.txt +44 -0
  50. package/read-dir/docs/types/index.d.ts +99 -0
  51. package/read-dir/docs/types/test.ts +85 -0
  52. package/read-dir/docs/usage.txt +8 -0
  53. package/read-dir/etc/cli_opts.json +14 -0
  54. package/read-dir/lib/async.js +57 -0
  55. package/read-dir/lib/index.js +62 -0
  56. package/read-dir/lib/sync.js +52 -0
  57. package/read-dir/package.json +69 -0
  58. package/read-file/README.md +246 -0
  59. package/read-file/bin/cli +91 -0
  60. package/read-file/docs/repl.txt +68 -0
  61. package/read-file/docs/types/index.d.ts +138 -0
  62. package/read-file/docs/types/test.ts +147 -0
  63. package/read-file/docs/usage.txt +10 -0
  64. package/read-file/etc/cli_opts.json +21 -0
  65. package/read-file/lib/index.js +61 -0
  66. package/read-file/lib/main.js +59 -0
  67. package/read-file/lib/sync.js +61 -0
  68. package/read-file/package.json +68 -0
  69. package/read-file-list/README.md +197 -0
  70. package/read-file-list/bin/cli +100 -0
  71. package/read-file-list/docs/repl.txt +81 -0
  72. package/read-file-list/docs/types/index.d.ts +166 -0
  73. package/read-file-list/docs/types/test.ts +166 -0
  74. package/read-file-list/docs/usage.txt +10 -0
  75. package/read-file-list/etc/cli_opts.json +21 -0
  76. package/read-file-list/lib/async.js +202 -0
  77. package/read-file-list/lib/clear_pending.js +47 -0
  78. package/read-file-list/lib/clear_timeout.js +23 -0
  79. package/read-file-list/lib/config.json +4 -0
  80. package/read-file-list/lib/delay.js +47 -0
  81. package/read-file-list/lib/index.js +66 -0
  82. package/read-file-list/lib/sync.js +94 -0
  83. package/read-file-list/package.json +72 -0
  84. package/read-json/README.md +176 -0
  85. package/read-json/docs/repl.txt +72 -0
  86. package/read-json/docs/types/index.d.ts +170 -0
  87. package/read-json/docs/types/test.ts +175 -0
  88. package/read-json/lib/async.js +114 -0
  89. package/read-json/lib/index.js +65 -0
  90. package/read-json/lib/sync.js +89 -0
  91. package/read-json/package.json +66 -0
  92. package/read-ndjson/README.md +166 -0
  93. package/read-ndjson/docs/repl.txt +72 -0
  94. package/read-ndjson/docs/types/index.d.ts +170 -0
  95. package/read-ndjson/docs/types/test.ts +179 -0
  96. package/read-ndjson/lib/async.js +114 -0
  97. package/read-ndjson/lib/index.js +65 -0
  98. package/read-ndjson/lib/sync.js +89 -0
  99. package/read-ndjson/package.json +68 -0
  100. package/read-wasm/README.md +158 -0
  101. package/read-wasm/docs/repl.txt +58 -0
  102. package/read-wasm/docs/types/index.d.ts +153 -0
  103. package/read-wasm/docs/types/test.ts +127 -0
  104. package/read-wasm/lib/async.js +106 -0
  105. package/read-wasm/lib/index.js +67 -0
  106. package/read-wasm/lib/sync.js +86 -0
  107. package/read-wasm/package.json +67 -0
  108. package/rename/README.md +259 -0
  109. package/rename/bin/cli +89 -0
  110. package/rename/docs/repl.txt +80 -0
  111. package/rename/docs/types/index.d.ts +109 -0
  112. package/rename/docs/types/test.ts +111 -0
  113. package/rename/docs/usage.txt +7 -0
  114. package/rename/etc/cli_opts.json +15 -0
  115. package/rename/lib/index.js +60 -0
  116. package/rename/lib/main.js +51 -0
  117. package/rename/lib/sync.js +53 -0
  118. package/rename/package.json +73 -0
  119. package/resolve-parent-path/README.md +204 -0
  120. package/resolve-parent-path/bin/cli +85 -0
  121. package/resolve-parent-path/docs/repl.txt +58 -0
  122. package/resolve-parent-path/docs/types/index.d.ts +128 -0
  123. package/resolve-parent-path/docs/types/test.ts +117 -0
  124. package/resolve-parent-path/docs/usage.txt +9 -0
  125. package/resolve-parent-path/etc/cli_opts.json +17 -0
  126. package/resolve-parent-path/lib/async.js +116 -0
  127. package/resolve-parent-path/lib/index.js +58 -0
  128. package/resolve-parent-path/lib/sync.js +83 -0
  129. package/resolve-parent-path/lib/validate.js +67 -0
  130. package/resolve-parent-path/package.json +78 -0
  131. package/resolve-parent-path-by/README.md +220 -0
  132. package/resolve-parent-path-by/docs/repl.txt +105 -0
  133. package/resolve-parent-path-by/docs/types/index.d.ts +220 -0
  134. package/resolve-parent-path-by/docs/types/test.ts +225 -0
  135. package/resolve-parent-path-by/lib/async.js +156 -0
  136. package/resolve-parent-path-by/lib/index.js +67 -0
  137. package/resolve-parent-path-by/lib/sync.js +98 -0
  138. package/resolve-parent-path-by/lib/validate.js +67 -0
  139. package/resolve-parent-path-by/package.json +76 -0
  140. package/resolve-parent-paths/README.md +228 -0
  141. package/resolve-parent-paths/bin/cli +93 -0
  142. package/resolve-parent-paths/docs/repl.txt +84 -0
  143. package/resolve-parent-paths/docs/types/index.d.ts +145 -0
  144. package/resolve-parent-paths/docs/types/test.ts +196 -0
  145. package/resolve-parent-paths/docs/usage.txt +10 -0
  146. package/resolve-parent-paths/etc/cli_opts.json +18 -0
  147. package/resolve-parent-paths/lib/index.js +60 -0
  148. package/resolve-parent-paths/lib/main.js +415 -0
  149. package/resolve-parent-paths/lib/sync.js +231 -0
  150. package/resolve-parent-paths/lib/validate.js +81 -0
  151. package/resolve-parent-paths/package.json +78 -0
  152. package/unlink/README.md +233 -0
  153. package/unlink/bin/cli +81 -0
  154. package/unlink/docs/repl.txt +62 -0
  155. package/unlink/docs/types/index.d.ts +105 -0
  156. package/unlink/docs/types/test.ts +85 -0
  157. package/unlink/docs/usage.txt +7 -0
  158. package/unlink/etc/cli_opts.json +15 -0
  159. package/unlink/lib/index.js +60 -0
  160. package/unlink/lib/main.js +50 -0
  161. package/unlink/lib/sync.js +52 -0
  162. package/unlink/package.json +70 -0
  163. package/write-file/README.md +250 -0
  164. package/write-file/bin/cli +110 -0
  165. package/write-file/docs/repl.txt +74 -0
  166. package/write-file/docs/types/index.d.ts +133 -0
  167. package/write-file/docs/types/test.ts +193 -0
  168. package/write-file/docs/usage.txt +10 -0
  169. package/write-file/etc/cli_opts.json +22 -0
  170. package/write-file/lib/index.js +60 -0
  171. package/write-file/lib/main.js +58 -0
  172. package/write-file/lib/sync.js +58 -0
  173. package/write-file/package.json +68 -0
@@ -0,0 +1,58 @@
1
+
2
+ {{alias}}( file[, options], clbk )
3
+ Asynchronously reads a file as WebAssembly.
4
+
5
+ The function returns file contents as a Uint8Array.
6
+
7
+ Parameters
8
+ ----------
9
+ file: string|Buffer|integer
10
+ Filename or file descriptor.
11
+
12
+ options: Object (optional)
13
+ Options.
14
+
15
+ options.flag: string (optional)
16
+ Flag. Default: 'r'.
17
+
18
+ clbk: Function
19
+ Callback to invoke upon reading file contents.
20
+
21
+ Examples
22
+ --------
23
+ > function onRead( error, data ) {
24
+ ... if ( error ) {
25
+ ... console.error( error.message );
26
+ ... } else {
27
+ ... console.log( data );
28
+ ... }
29
+ ... };
30
+ > {{alias}}( './beep/boop.wasm', onRead );
31
+
32
+
33
+ {{alias}}.sync( file[, options] )
34
+ Synchronously reads a file as WebAssembly.
35
+
36
+ Parameters
37
+ ----------
38
+ file: string|Buffer|integer
39
+ Filename or file descriptor.
40
+
41
+ options: Object (optional)
42
+ Options.
43
+
44
+ options.flag: string (optional)
45
+ Flag. Default: 'r'.
46
+
47
+ Returns
48
+ -------
49
+ out: Error|Uint8Array
50
+ File contents.
51
+
52
+ Examples
53
+ --------
54
+ > var out = {{alias}}.sync( './beep/boop.wasm' );
55
+
56
+ See Also
57
+ --------
58
+
@@ -0,0 +1,153 @@
1
+ /*
2
+ * @license Apache-2.0
3
+ *
4
+ * Copyright (c) 2020 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
+ * Interface defining function options.
27
+ */
28
+ interface Options {
29
+ /**
30
+ * Flag (default: 'r').
31
+ */
32
+ flag?: string;
33
+ }
34
+
35
+ /**
36
+ * Callback invoked upon reading a file.
37
+ *
38
+ * @param err - error object
39
+ * @param file - file contents
40
+ */
41
+ type Callback = ( err: Error | null, file: Uint8Array ) => void;
42
+
43
+ /**
44
+ * Interface for reading a file as WebAssembly.
45
+ */
46
+ interface ReadWASM {
47
+ /**
48
+ * Asynchronously reads the entire contents of a WebAssembly file.
49
+ *
50
+ * @param file - file path or file descriptor
51
+ * @param options - options
52
+ * @param options.flag - file status flag
53
+ * @param clbk - callback to invoke after reading a file
54
+ *
55
+ * @example
56
+ * var join = require( 'path' ).join;
57
+ * var instanceOf = require( '@stdlib/assert/instance-of' );
58
+ *
59
+ * var fpath = join( __dirname, 'foo.wasm' );
60
+ * readWASM( fpath, onRead );
61
+ *
62
+ * function onRead( error, buf ) {
63
+ * if ( error ) {
64
+ * throw error;
65
+ * }
66
+ * console.log( buf );
67
+ * }
68
+ */
69
+ ( file: string | Buffer | number, options: Options, clbk: Callback ): void;
70
+
71
+ /**
72
+ * Asynchronously reads the entire contents of a WebAssembly file.
73
+ *
74
+ * @param file - file path or file descriptor
75
+ * @param clbk - callback to invoke after reading a file
76
+ *
77
+ * @example
78
+ * var join = require( 'path' ).join;
79
+ * var instanceOf = require( '@stdlib/assert/instance-of' );
80
+ *
81
+ * var fpath = join( __dirname, 'foo.wasm' );
82
+ * readWASM( fpath, onRead );
83
+ *
84
+ * function onRead( error, buf ) {
85
+ * if ( error ) {
86
+ * throw error;
87
+ * }
88
+ * console.log( buf );
89
+ * }
90
+ */
91
+ ( file: string | Buffer | number, clbk: Callback ): void;
92
+
93
+ /**
94
+ * Synchronously reads the entire contents of a WebAssembly file.
95
+ *
96
+ * @param file - file path or file descriptor
97
+ * @param options - options
98
+ * @param options.flag - file status flag
99
+ * @returns file contents or an error
100
+ *
101
+ * @example
102
+ * var join = require( 'path' ).join;
103
+ * var instanceOf = require( '@stdlib/assert/instance-of' );
104
+ *
105
+ * var fpath = join( __dirname, 'foo.wasm' );
106
+ * var out = readWASM.sync( fpath );
107
+ * if ( instanceOf( out, Error ) ) {
108
+ * throw out;
109
+ * }
110
+ * console.log( out );
111
+ */
112
+ sync( file: string | Buffer | number, options?: Options ): Uint8Array | Error;
113
+ }
114
+
115
+ /**
116
+ * Asynchronously reads the entire contents of a WebAssembly file.
117
+ *
118
+ * @param file - file path or file descriptor
119
+ * @param options - options
120
+ * @param options.flag - file status flag
121
+ * @param clbk - callback to invoke after reading a file
122
+ *
123
+ * @example
124
+ * var join = require( 'path' ).join;
125
+ * var instanceOf = require( '@stdlib/assert/instance-of' );
126
+ *
127
+ * var fpath = join( __dirname, 'foo.wasm' );
128
+ * readWASM( fpath, onRead );
129
+ *
130
+ * function onRead( error, buf ) {
131
+ * if ( error ) {
132
+ * throw error;
133
+ * }
134
+ * console.log( buf );
135
+ * }
136
+ *
137
+ * @example
138
+ * var join = require( 'path' ).join;
139
+ * var instanceOf = require( '@stdlib/assert/instance-of' );
140
+ *
141
+ * var fpath = join( __dirname, 'foo.wasm' );
142
+ * var out = readWASM.sync( fpath );
143
+ * if ( instanceOf( out, Error ) ) {
144
+ * throw out;
145
+ * }
146
+ * console.log( out );
147
+ */
148
+ declare var readWASM: ReadWASM;
149
+
150
+
151
+ // EXPORTS //
152
+
153
+ export = readWASM;
@@ -0,0 +1,127 @@
1
+ /*
2
+ * @license Apache-2.0
3
+ *
4
+ * Copyright (c) 2020 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 readWASM = require( './index' );
20
+
21
+ const onLoad = ( error: Error | null, file: Uint8Array ) => {
22
+ if ( error || !file ) {
23
+ throw error;
24
+ }
25
+ };
26
+
27
+
28
+ // TESTS //
29
+
30
+ // The function does not have a return value...
31
+ {
32
+ readWASM( 'foo.wasm', onLoad ); // $ExpectType void
33
+ }
34
+
35
+ // The compiler throws an error if the function is provided a first argument which is not a string, buffer, or file descriptor...
36
+ {
37
+ readWASM( false, onLoad ); // $ExpectError
38
+ readWASM( true, onLoad ); // $ExpectError
39
+ readWASM( null, onLoad ); // $ExpectError
40
+ readWASM( undefined, onLoad ); // $ExpectError
41
+ readWASM( [], onLoad ); // $ExpectError
42
+ readWASM( {}, onLoad ); // $ExpectError
43
+ readWASM( ( x: number ): number => x, onLoad ); // $ExpectError
44
+ }
45
+
46
+ // The compiler throws an error if the function is provided a callback argument which is not a function with the expected signature...
47
+ {
48
+ readWASM( '/path/to/foo.wasm', 'abc' ); // $ExpectError
49
+ readWASM( '/path/to/foo.wasm', 1 ); // $ExpectError
50
+ readWASM( '/path/to/foo.wasm', false ); // $ExpectError
51
+ readWASM( '/path/to/foo.wasm', true ); // $ExpectError
52
+ readWASM( '/path/to/foo.wasm', null ); // $ExpectError
53
+ readWASM( '/path/to/foo.wasm', undefined ); // $ExpectError
54
+ readWASM( '/path/to/foo.wasm', [] ); // $ExpectError
55
+ readWASM( '/path/to/foo.wasm', {} ); // $ExpectError
56
+ readWASM( '/path/to/foo.wasm', ( x: number ): number => x ); // $ExpectError
57
+ }
58
+
59
+ // The compiler throws an error if the function is provided an options argument which is not an object...
60
+ {
61
+ readWASM( 'foo.wasm', false, onLoad ); // $ExpectError
62
+ readWASM( 'foo.wasm', true, onLoad ); // $ExpectError
63
+ readWASM( 'foo.wasm', null, onLoad ); // $ExpectError
64
+ readWASM( 'foo.wasm', undefined, onLoad ); // $ExpectError
65
+ readWASM( 'foo.wasm', 123, onLoad ); // $ExpectError
66
+ readWASM( 'foo.wasm', [], onLoad ); // $ExpectError
67
+ readWASM( 'foo.wasm', ( x: number ): number => x, onLoad ); // $ExpectError
68
+ }
69
+
70
+ // The compiler throws an error if the function is provided a `flag` option which is not a string...
71
+ {
72
+ readWASM( 'foo.wasm', { 'flag': 123 }, onLoad ); // $ExpectError
73
+ readWASM( 'foo.wasm', { 'flag': true }, onLoad ); // $ExpectError
74
+ readWASM( 'foo.wasm', { 'flag': false }, onLoad ); // $ExpectError
75
+ readWASM( 'foo.wasm', { 'flag': null }, onLoad ); // $ExpectError
76
+ readWASM( 'foo.wasm', { 'flag': [] }, onLoad ); // $ExpectError
77
+ readWASM( 'foo.wasm', { 'flag': {} }, onLoad ); // $ExpectError
78
+ readWASM( 'foo.wasm', { 'flag': ( x: number ): number => x }, onLoad ); // $ExpectError
79
+ }
80
+
81
+ // The compiler throws an error if the function is provided an unsupported number of arguments...
82
+ {
83
+ readWASM(); // $ExpectError
84
+ readWASM( 'C:\\foo\\bar\\baz\\foo.wasm' ); // $ExpectError
85
+ }
86
+
87
+ // Attached to main export is a `sync` method which returns file contents as a Uint8Array or an error...
88
+ {
89
+ readWASM.sync( 'foo.wasm' ); // $ExpectType Error | Uint8Array
90
+ }
91
+
92
+ // The compiler throws an error if the `sync` method is provided a first argument which is not a string, buffer, or file descriptor...
93
+ {
94
+ readWASM.sync( false ); // $ExpectError
95
+ readWASM.sync( true ); // $ExpectError
96
+ readWASM.sync( null ); // $ExpectError
97
+ readWASM.sync( undefined ); // $ExpectError
98
+ readWASM.sync( [] ); // $ExpectError
99
+ readWASM.sync( {} ); // $ExpectError
100
+ readWASM.sync( ( x: number ): number => x ); // $ExpectError
101
+ }
102
+
103
+ // The compiler throws an error if the `sync` method is provided an options argument which is not an object...
104
+ {
105
+ readWASM.sync( 'foo.wasm', null ); // $ExpectError
106
+ readWASM.sync( 'foo.wasm', true ); // $ExpectError
107
+ readWASM.sync( 'foo.wasm', false ); // $ExpectError
108
+ readWASM.sync( 'foo.wasm', 123 ); // $ExpectError
109
+ readWASM.sync( 'foo.wasm', [] ); // $ExpectError
110
+ readWASM.sync( 'foo.wasm', ( x: number ): number => x ); // $ExpectError
111
+ }
112
+
113
+ // The compiler throws an error if the `sync` method is provided a `flag` option which is not a string...
114
+ {
115
+ readWASM.sync( 'foo.wasm', { 'flag': 123 } ); // $ExpectError
116
+ readWASM.sync( 'foo.wasm', { 'flag': true } ); // $ExpectError
117
+ readWASM.sync( 'foo.wasm', { 'flag': false } ); // $ExpectError
118
+ readWASM.sync( 'foo.wasm', { 'flag': null } ); // $ExpectError
119
+ readWASM.sync( 'foo.wasm', { 'flag': [] } ); // $ExpectError
120
+ readWASM.sync( 'foo.wasm', { 'flag': {} } ); // $ExpectError
121
+ readWASM.sync( 'foo.wasm', { 'flag': ( x: number ): number => x } ); // $ExpectError
122
+ }
123
+
124
+ // The compiler throws an error if the `sync` method is provided an unsupported number of arguments...
125
+ {
126
+ readWASM.sync(); // $ExpectError
127
+ }
@@ -0,0 +1,106 @@
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 isObject = require( '@stdlib/assert/is-plain-object' );
24
+ var isFunction = require( '@stdlib/assert/is-function' );
25
+ var isUint8Array = require( '@stdlib/assert/is-uint8array' );
26
+ var readFile = require( './../../read-file' );
27
+ var Uint8Array = require( '@stdlib/array/uint8' );
28
+ var format = require( '@stdlib/string/format' );
29
+
30
+
31
+ // MAIN //
32
+
33
+ /**
34
+ * Asynchronously reads the entire contents of a WebAssembly file.
35
+ *
36
+ * @param {(string|Buffer|integer)} file - file path or file descriptor
37
+ * @param {Options} [options] - options
38
+ * @param {string} [options.flag] - file status flag
39
+ * @param {Callback} clbk - callback to invoke after reading a file
40
+ * @throws {TypeError} options argument must be an object
41
+ * @throws {TypeError} callback argument must be a function
42
+ *
43
+ * @example
44
+ * var join = require( 'path' ).join;
45
+ * var instanceOf = require( '@stdlib/assert/instance-of' );
46
+ *
47
+ * var fpath = join( __dirname, 'foo.wasm' );
48
+ * readWASM( fpath, onRead );
49
+ *
50
+ * function onRead( error, buf ) {
51
+ * if ( error ) {
52
+ * throw error;
53
+ * }
54
+ * console.log( buf );
55
+ * }
56
+ */
57
+ function readWASM( file, options, clbk ) {
58
+ var opts;
59
+ var done;
60
+ if ( arguments.length < 3 ) {
61
+ opts = {};
62
+ done = options;
63
+ } else {
64
+ if ( !isObject( options ) ) {
65
+ throw new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) );
66
+ }
67
+ opts = options;
68
+ done = clbk;
69
+ }
70
+ if ( !isFunction( done ) ) {
71
+ throw new TypeError( format( 'invalid argument. Callback argument must be a function. Value: `%s`.', done ) );
72
+ }
73
+ // Always override setting the encoding option, as wasm is a binary file format:
74
+ opts.encoding = null;
75
+ readFile( file, opts, onRead );
76
+
77
+ /**
78
+ * Callback invoked upon reading a file.
79
+ *
80
+ * @private
81
+ * @param {(Error|null)} error - error object
82
+ * @param {(Buffer|string)} file - file contents
83
+ * @returns {void}
84
+ */
85
+ function onRead( error, file ) {
86
+ var out;
87
+ var i;
88
+ if ( error ) {
89
+ return done( error );
90
+ }
91
+ if ( isUint8Array( file ) ) {
92
+ return done( null, file );
93
+ }
94
+ // Handle older Node.js environments where Buffer objects are not Uint8Arrays...
95
+ out = new Uint8Array( file.length );
96
+ for ( i = 0; i < file.length; i++ ) {
97
+ out[ i ] = file[ i ];
98
+ }
99
+ done( null, out );
100
+ }
101
+ }
102
+
103
+
104
+ // EXPORTS //
105
+
106
+ module.exports = readWASM;
@@ -0,0 +1,67 @@
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 the entire contents of a WebAssembly file.
23
+ *
24
+ * @module @stdlib/fs/read-wasm
25
+ *
26
+ * @example
27
+ * var join = require( 'path' ).join;
28
+ * var readWASM = require( '@stdlib/fs/read-wasm' );
29
+ *
30
+ * var fpath = join( __dirname, 'foo.wasm' );
31
+ * readWASM( fpath, onRead );
32
+ *
33
+ * function onRead( error, buf ) {
34
+ * if ( error ) {
35
+ * throw error;
36
+ * }
37
+ * console.log( out );
38
+ * }
39
+ *
40
+ * @example
41
+ * var join = require( 'path' ).join;
42
+ * var instanceOf = require( '@stdlib/assert/instance-of' );
43
+ * var readWASMSync = require( '@stdlib/fs/read-wasm' ).sync;
44
+ *
45
+ * var fpath = join( __dirname, 'foo.wasm' );
46
+ * var out = readWASMSync( fpath );
47
+ * if ( instanceOf( out, Error ) ) {
48
+ * throw out;
49
+ * }
50
+ * console.log( out );
51
+ */
52
+
53
+ // MODULES //
54
+
55
+ var setReadOnly = require( '@ohos-ports/stdlib-utils/define-nonenumerable-read-only-property' );
56
+ var readWASM = require( './async.js' );
57
+ var sync = require( './sync.js' );
58
+
59
+
60
+ // MAIN //
61
+
62
+ setReadOnly( readWASM, 'sync', sync );
63
+
64
+
65
+ // EXPORTS //
66
+
67
+ module.exports = readWASM;
@@ -0,0 +1,86 @@
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 isObject = require( '@stdlib/assert/is-plain-object' );
24
+ var isUint8Array = require( '@stdlib/assert/is-uint8array' );
25
+ var instanceOf = require( '@stdlib/assert/instance-of' );
26
+ var readFileSync = require( './../../read-file' ).sync;
27
+ var Uint8Array = require( '@stdlib/array/uint8' );
28
+ var format = require( '@stdlib/string/format' );
29
+
30
+
31
+ // MAIN //
32
+
33
+ /**
34
+ * Synchronously reads the entire contents of a WebAssembly file.
35
+ *
36
+ * @param {(string|Buffer|integer)} file - file path or file descriptor
37
+ * @param {Options} [options] - options
38
+ * @param {string} [options.flag] - file status flag
39
+ * @throws {TypeError} options argument must be an object
40
+ * @returns {(Uint8Array|Error)} file contents or an error
41
+ *
42
+ * @example
43
+ * var join = require( 'path' ).join;
44
+ * var instanceOf = require( '@stdlib/assert/instance-of' );
45
+ *
46
+ * var fpath = join( __dirname, 'foo.wasm' );
47
+ * var out = readWASMSync( fpath );
48
+ * if ( instanceOf( out, Error ) ) {
49
+ * throw out;
50
+ * }
51
+ * console.log( out );
52
+ */
53
+ function readWASMSync( file, options ) {
54
+ var opts;
55
+ var out;
56
+ var f;
57
+ var i;
58
+ if ( arguments.length > 1 ) {
59
+ if ( !isObject( options ) ) {
60
+ throw new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) );
61
+ }
62
+ opts = options;
63
+ } else {
64
+ opts = {};
65
+ }
66
+ // Always override setting the encoding option, as wasm is a binary file format:
67
+ opts.encoding = null;
68
+ f = readFileSync( file, opts );
69
+ if ( instanceOf( f, Error ) ) {
70
+ return f;
71
+ }
72
+ if ( isUint8Array( f ) ) {
73
+ return f;
74
+ }
75
+ // Handle older Node.js environments where Buffer objects are not Uint8Arrays...
76
+ out = new Uint8Array( f.length );
77
+ for ( i = 0; i < f.length; i++ ) {
78
+ out[ i ] = f[ i ];
79
+ }
80
+ return out;
81
+ }
82
+
83
+
84
+ // EXPORTS //
85
+
86
+ module.exports = readWASMSync;
@@ -0,0 +1,67 @@
1
+ {
2
+ "name": "@stdlib/fs/read-wasm",
3
+ "version": "0.0.0",
4
+ "description": "Read a file as WebAssembly.",
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
+ "directories": {
18
+ "benchmark": "./benchmark",
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
+ ],
51
+ "keywords": [
52
+ "stdlib",
53
+ "stdfs",
54
+ "fs",
55
+ "readfile",
56
+ "readfilesync",
57
+ "path",
58
+ "async",
59
+ "sync",
60
+ "file",
61
+ "read",
62
+ "open",
63
+ "filesystem",
64
+ "wasm",
65
+ "webassembly"
66
+ ]
67
+ }