Skip to content

Commit b73ebd1

Browse files
committed
Add Typescript definitions
1 parent d95e986 commit b73ebd1

File tree

2 files changed

+280
-0
lines changed

2 files changed

+280
-0
lines changed
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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: 2.0
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+
* 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+
* 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; // tslint-disable-line max-line-length
113+
}
114+
115+
/**
116+
* 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;
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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 commit comments

Comments
 (0)