Skip to content

Commit cec253f

Browse files
committed
New: Copy-pasted typescript definitions to micro modules, see #599
1 parent 8493dbd commit cec253f

File tree

11 files changed

+233
-2
lines changed

11 files changed

+233
-2
lines changed

examples/reader-writer.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ var protobuf = require("..");
22

33
var writer = protobuf.Writer.create();
44
var buffer = writer
5-
.int32(1 << 3 | 2) // id 1, wireType 2
5+
.uint32((1 << 3 | 2) >>> 0) // id 1, wireType 2
66
.string("hello world!")
77
.finish();
88

99
var reader = protobuf.Reader.create(buffer);
1010
while (reader.pos < reader.len) {
11-
var tag = reader.int32();
11+
var tag = reader.uint32();
1212
switch (tag>>>3) {
1313
case 1:
1414
console.log(reader.string());

src/util/aspromise/index.d.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/**
2+
* Returns a promise from a node-style callback function.
3+
* @memberof util
4+
* @param {function(?Error, ...*)} fn Function to call
5+
* @param {*} ctx Function context
6+
* @param {...*} params Function arguments
7+
* @returns {Promise<*>} Promisified function
8+
*/
9+
declare function asPromise(fn: () => any, ctx: any, params: any): Promise<any>;

src/util/base64/index.d.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* A minimal base64 implementation for number arrays.
3+
* @memberof util
4+
* @namespace
5+
*/
6+
declare module base64 {
7+
8+
/**
9+
* Calculates the byte length of a base64 encoded string.
10+
* @param {string} string Base64 encoded string
11+
* @returns {number} Byte length
12+
*/
13+
function length(string: string): number;
14+
15+
/**
16+
* Encodes a buffer to a base64 encoded string.
17+
* @param {Uint8Array} buffer Source buffer
18+
* @param {number} start Source start
19+
* @param {number} end Source end
20+
* @returns {string} Base64 encoded string
21+
*/
22+
function encode(buffer: Uint8Array, start: number, end: number): string;
23+
24+
/**
25+
* Decodes a base64 encoded string to a buffer.
26+
* @param {string} string Source string
27+
* @param {Uint8Array} buffer Destination buffer
28+
* @param {number} offset Destination offset
29+
* @returns {number} Number of bytes written
30+
* @throws {Error} If encoding is invalid
31+
*/
32+
function decode(string: string, buffer: Uint8Array, offset: number): number;
33+
}

src/util/codegen/index.d.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* A codegen instance as returned by {@link codegen}, that also is a sprintf-like appender function.
3+
* @typedef Codegen
4+
* @type {function}
5+
* @param {string} format Format string
6+
* @param {...*} args Replacements
7+
* @returns {Codegen} Itself
8+
* @property {function(string=):string} str Stringifies the so far generated function source.
9+
* @property {function(string=, Object=):function} eof Ends generation and builds the function whilst applying a scope.
10+
*/
11+
type Codegen = (format: string, args: any) => Codegen;
12+
13+
/**
14+
* A closure for generating functions programmatically.
15+
* @memberof util
16+
* @namespace
17+
* @function
18+
* @param {...string} params Function parameter names
19+
* @returns {Codegen} Codegen instance
20+
* @property {boolean} supported Whether code generation is supported by the environment.
21+
* @property {boolean} verbose=false When set to true, codegen will log generated code to console. Useful for debugging.
22+
*/
23+
declare function codegen(params: string): Codegen;

src/util/eventemitter/index.d.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* Constructs a new event emitter instance.
3+
* @classdesc A minimal event emitter.
4+
* @memberof util
5+
* @constructor
6+
*/
7+
declare class EventEmitter {
8+
9+
/**
10+
* Constructs a new event emitter instance.
11+
* @classdesc A minimal event emitter.
12+
* @memberof util
13+
* @constructor
14+
*/
15+
constructor();
16+
17+
/**
18+
* Registers an event listener.
19+
* @param {string} evt Event name
20+
* @param {function} fn Listener
21+
* @param {*} [ctx] Listener context
22+
* @returns {util.EventEmitter} `this`
23+
*/
24+
on(evt: string, fn: () => any, ctx?: any): EventEmitter;
25+
26+
/**
27+
* Removes an event listener or any matching listeners if arguments are omitted.
28+
* @param {string} [evt] Event name. Removes all listeners if omitted.
29+
* @param {function} [fn] Listener to remove. Removes all listeners of `evt` if omitted.
30+
* @returns {util.EventEmitter} `this`
31+
*/
32+
off(evt?: string, fn?: () => any): EventEmitter;
33+
34+
/**
35+
* Emits an event by calling its listeners with the specified arguments.
36+
* @param {string} evt Event name
37+
* @param {...*} args Arguments
38+
* @returns {util.EventEmitter} `this`
39+
*/
40+
emit(evt: string, args: any): EventEmitter;
41+
}

src/util/extend/index.d.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* Lets the specified constructor extend `this` class.
3+
* @memberof util
4+
* @param {*} ctor Extending constructor
5+
* @returns {Object.<string,*>} Constructor prototype
6+
* @this Function
7+
*/
8+
declare function extend(this: Function, ctor: any): { [k: string]: any };

src/util/fetch/index.d.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* Node-style callback as used by {@link util.fetch}.
3+
* @typedef FetchCallback
4+
* @type {function}
5+
* @param {?Error} error Error, if any, otherwise `null`
6+
* @param {string} [contents] File contents, if there hasn't been an error
7+
* @returns {undefined}
8+
*/
9+
type FetchCallback = (error: Error, contents?: string) => void;
10+
11+
/**
12+
* Fetches the contents of a file.
13+
* @memberof util
14+
* @param {string} path File path or url
15+
* @param {FetchCallback} [callback] Callback function
16+
* @returns {Promise<string>|undefined} A Promise if `callback` has been omitted
17+
*/
18+
declare function fetch(path: string, callback?: FetchCallback): (Promise<string>|undefined);

src/util/inquire/index.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/**
2+
* Requires a module only if available.
3+
* @memberof util
4+
* @param {string} moduleName Module to require
5+
* @returns {?Object} Required module if available and not empty, otherwise `null`
6+
*/
7+
declare function inquire(moduleName: string): Object;

src/util/path/index.d.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* A minimal path module to resolve Unix, Windows and URL paths alike.
3+
* @memberof util
4+
* @namespace
5+
*/
6+
declare module path {
7+
8+
/**
9+
* Tests if the specified path is absolute.
10+
* @param {string} path Path to test
11+
* @returns {boolean} `true` if path is absolute
12+
*/
13+
function isAbsolute(path: string): boolean;
14+
15+
/**
16+
* Normalizes the specified path.
17+
* @param {string} path Path to normalize
18+
* @returns {string} Normalized path
19+
*/
20+
function normalize(path: string): string;
21+
22+
/**
23+
* Resolves the specified include path against the specified origin path.
24+
* @param {string} originPath Path to the origin file
25+
* @param {string} includePath Include path relative to origin path
26+
* @param {boolean} [alreadyNormalized=false] `true` if both paths are already known to be normalized
27+
* @returns {string} Path to the include file
28+
*/
29+
function resolve(originPath: string, includePath: string, alreadyNormalized?: boolean): string;
30+
}

src/util/pool/index.d.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* An allocator as used by {@link util.pool}.
3+
* @typedef PoolAllocator
4+
* @type {function}
5+
* @param {number} size Buffer size
6+
* @returns {Uint8Array} Buffer
7+
*/
8+
type PoolAllocator = (size: number) => Uint8Array;
9+
10+
/**
11+
* A slicer as used by {@link util.pool}.
12+
* @typedef PoolSlicer
13+
* @type {function}
14+
* @param {number} start Start offset
15+
* @param {number} end End offset
16+
* @returns {Uint8Array} Buffer slice
17+
* @this {Uint8Array}
18+
*/
19+
type PoolSlicer = (this: Uint8Array, start: number, end: number) => Uint8Array;
20+
21+
/**
22+
* A general purpose buffer pool.
23+
* @memberof util
24+
* @function
25+
* @param {PoolAllocator} alloc Allocator
26+
* @param {PoolSlicer} slice Slicer
27+
* @param {number} [size=8192] Slab size
28+
* @returns {PoolAllocator} Pooled allocator
29+
*/
30+
declare function pool(alloc: PoolAllocator, slice: PoolSlicer, size?: number): PoolAllocator;

0 commit comments

Comments
 (0)