From 5de92434d4601a6a17f09b03f636699f0b103595 Mon Sep 17 00:00:00 2001 From: Keerthi Niranjan Date: Thu, 16 Aug 2018 11:25:07 +0530 Subject: [PATCH 1/5] Add ffi-napi module --- types/ffi-napi/ffi-napi-tests.ts | 106 +++++++++++++ types/ffi-napi/index.d.ts | 186 +++++++++++++++++++++++ types/ffi-napi/node-ffi-napi-buffer.d.ts | 50 ++++++ types/ffi-napi/tsconfig.json | 24 +++ types/ffi-napi/tslint.json | 8 + 5 files changed, 374 insertions(+) create mode 100644 types/ffi-napi/ffi-napi-tests.ts create mode 100644 types/ffi-napi/index.d.ts create mode 100644 types/ffi-napi/node-ffi-napi-buffer.d.ts create mode 100644 types/ffi-napi/tsconfig.json create mode 100644 types/ffi-napi/tslint.json diff --git a/types/ffi-napi/ffi-napi-tests.ts b/types/ffi-napi/ffi-napi-tests.ts new file mode 100644 index 0000000000..5965e23e12 --- /dev/null +++ b/types/ffi-napi/ffi-napi-tests.ts @@ -0,0 +1,106 @@ +import ffi = require('ffi-napi'); +import ref = require('ref-napi'); +import Struct = require('ref-struct-di'); +import Union = require('ref-union-di'); +import TArray = require('ref-array-di'); + +{ + const sqlite3 = ref.types.void; + const sqlite3Ptr = ref.refType(sqlite3); + const sqlite3PtrPtr = ref.refType(sqlite3Ptr); + const stringPtr = ref.refType(ref.types.CString); + + const libsqlite3 = ffi.Library('libsqlite3', { + sqlite3_open: [ 'int', [ 'string', sqlite3PtrPtr ] ], + sqlite3_close: [ 'int', [ sqlite3PtrPtr ] ], + sqlite3_exec: [ 'int', [ sqlite3PtrPtr, 'string', 'pointer', 'pointer', stringPtr ] ], + sqlite3_changes: [ 'int', [ sqlite3PtrPtr ]] + }); + + const dbPtrPtr = ref.alloc(sqlite3PtrPtr); + libsqlite3.sqlite3_open("test.sqlite3", dbPtrPtr); +} +{ + const func = ffi.ForeignFunction(new Buffer(10), 'int', [ 'int' ]); + func(-5); + func.async(-5, (err: any, res: any) => {}); +} +{ + const funcPtr = ffi.Callback('int', [ 'int' ], Math.abs); + const func = ffi.ForeignFunction(funcPtr, 'int', [ 'int' ]); +} +{ + const printfPointer = ffi.DynamicLibrary().get('printf'); + const printfGen = ffi.VariadicForeignFunction(printfPointer, 'void', [ 'string' ]); + printfGen()('Hello World!\n'); + printfGen('int')('This is an int: %d\n', 10); + printfGen('string')('This is a string: %s\n', 'hello'); +} +{ + ref.address(new Buffer(1)); + const intBuf = ref.alloc(ref.types.int); + const intWith4 = ref.alloc(ref.types.int, 4); + const buf0 = ref.allocCString('hello world'); + const type = ref.coerceType('int **'); + const val = ref.deref(intBuf); +} +{ + ref.isNull(new Buffer(1)); +} +{ + const str = ref.readCString(new Buffer('hello\0world\0'), 0); + const buf = ref.alloc('int64'); + ref.writeInt64BE(buf, 0, '9223372036854775807'); + const val = ref.readInt64BE(buf, 0); +} +{ + const voidPtrType = ref.refType(ref.types.void); + const buf = ref.alloc('int64'); + ref.writeInt64LE(buf, 0, '9223372036854775807'); +} +{ + const S1 = Struct({ a: ref.types.int }); + const S2 = new Struct({ a: 'int' }); +} +{ + const P = new Struct(); + P.defineProperty('a', ref.types.int); + P.defineProperty('d', 'long'); +} +{ + const SimpleStruct = Struct({ + first : ref.types.byte, + last : ref.types.byte + }); + + const ss = new SimpleStruct({ first: 50, last: 100 }); + ss.first += 200; +} +{ + const ST = Struct(); + const test: ref.Type = ST.fields['t'].type; +} +{ + const CharArray = TArray('char'); + const b = new Buffer('hello', 'ascii'); + const a = new CharArray(b); +} +{ + const Int32Array = TArray(ref.types.int32); + const input = [1, 4, 91, 123123, 5123512, 0, -1]; + const a = new Int32Array(input); +} +{ + const int = ref.types.int; + const IntArray = TArray(int); + + const buf = new Buffer(int.size * 3); + int.set(buf, int.size * 0, 5); + int.set(buf, int.size * 1, 8); + int.set(buf, int.size * 2, 0); + + const array = IntArray.untilZeros(buf); +} +{ + const refCharArr = TArray('char')([1, 3, 5], 2).ref(); +} diff --git a/types/ffi-napi/index.d.ts b/types/ffi-napi/index.d.ts new file mode 100644 index 0000000000..6cfd439e13 --- /dev/null +++ b/types/ffi-napi/index.d.ts @@ -0,0 +1,186 @@ +// Type definitions for node-ffi-napi 2.4 +// Project: https://github.com/node-ffi/node-ffi +// Definitions by: Keerthi Niranjan , Kiran Niranjan +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.2 + +/// + + +import ref = require('ref-napi'); +import StructType = require('ref-struct-di'); + +/** Provides a friendly API on-top of `DynamicLibrary` and `ForeignFunction`. */ +export interface Library { + /** The extension to use on libraries. */ + EXT: string; + + /** + * @param libFile name of library + * @param funcs hash of [retType, [...argType], opts?: {abi?, async?, varargs?}] + * @param lib hash that will be extended + */ + new (libFile: string, funcs?: {[key: string]: any[]}, lib?: object): any; + + /** + * @param libFile name of library + * @param funcs hash of [retType, [...argType], opts?: {abi?, async?, varargs?}] + * @param lib hash that will be extended + */ + (libFile: string, funcs?: {[key: string]: any[]}, lib?: object): any; +} +export const Library: Library; + +/** Get value of errno. */ +export function errno(): number; + +export interface Function extends ref.Type { + /** The type of return value. */ + retType: ref.Type; + /** The type of arguments. */ + argTypes: ref.Type[]; + /** Is set for node-ffi functions. */ + ffi_type: Buffer; + abi: number; + + /** Get a `Callback` pointer of this function type. */ + toPointer(fn: (...args: any[]) => any): Buffer; + /** Get a `ForeignFunction` of this function type. */ + toFunction(buf: Buffer): ForeignFunction; +} + +/** Creates and returns a type for a C function pointer. */ +export const Function: { + new (retType: ref.Type | string, argTypes: any[], abi?: number): Function; + (retType: ref.Type | string, argTypes: any[], abi?: number): Function; +}; + +export interface ForeignFunction { + (...args: any[]): any; + async(...args: any[]): void; +} + +/** + * Represents a foreign function in another library. Manages all of the aspects + * of function execution, including marshalling the data parameters for the + * function into native types and also unmarshalling the return from function + * execution. + */ +export const ForeignFunction: { + new (ptr: Buffer, retType: ref.Type | string, argTypes: any[], abi?: number): ForeignFunction; + (ptr: Buffer, retType: ref.Type | string, argTypes: any[], abi?: number): ForeignFunction; +}; + +export interface VariadicForeignFunction { + /** + * What gets returned is another function that needs to be invoked with the rest + * of the variadic types that are being invoked from the function. + */ + (...args: any[]): ForeignFunction; + + /** + * Return type as a property of the function generator to + * allow for monkey patching the return value in the very rare case where the + * return type is variadic as well + */ + returnType: any; +} + +/** + * For when you want to call to a C function with variable amount of arguments. + * i.e. `printf`. + * + * This function takes care of caching and reusing `ForeignFunction` instances that + * contain the same ffi_type argument signature. + */ +export const VariadicForeignFunction: { + new (ptr: Buffer, ret: ref.Type | string, fixedArgs: any[], abi?: number): VariadicForeignFunction; + (ptr: Buffer, ret: ref.Type | string, fixedArgs: any[], abi?: number): VariadicForeignFunction; +}; + +export interface DynamicLibrary { + /** Close library, returns the result of the `dlclose` system function. */ + close(): number; + /** Get a symbol from this library. */ + get(symbol: string): Buffer; + /** Get the result of the `dlerror` system function. */ + error(): string; +} + +/** + * This class loads and fetches function pointers for dynamic libraries + * (.so, .dylib, etc). After the libray's function pointer is acquired, then you + * call `get(symbol)` to retreive a pointer to an exported symbol. You need to + * call `get___` on the pointer to dereference it into its actual value, or + * turn the pointer into a callable function with `ForeignFunction`. + */ +export const DynamicLibrary: { + FLAGS: { + RTLD_LAZY: number; + RTLD_NOW: number; + RTLD_LOCAL: number; + RTLD_GLOBAL: number; + RTLD_NOLOAD: number; + RTLD_NODELETE: number; + RTLD_NEXT: Buffer; + RTLD_DEFAUL: Buffer; + } + + new (path?: string, mode?: number): DynamicLibrary; + (path?: string, mode?: number): DynamicLibrary; +}; + +/** + * Turns a JavaScript function into a C function pointer. + * The function pointer may be used in other C functions that + * accept C callback functions. + */ +export interface Callback { + new (retType: any, argTypes: any[], abi: number, fn: any): Buffer; + new (retType: any, argTypes: any[], fn: any): Buffer; + (retType: any, argTypes: any[], abi: number, fn: any): Buffer; + (retType: any, argTypes: any[], fn: any): Buffer; +} +export const Callback: Callback; + +export const ffiType: { + /** Get a `ffi_type *` Buffer appropriate for the given type. */ + (type: ref.Type | string): Buffer + FFI_TYPE: StructType; +}; + +export function CIF(retType: any, types: any[], abi?: any): Buffer; +export function CIF_var(retType: any, types: any[], numFixedArgs: number, abi?: any): Buffer; +export const HAS_OBJC: boolean; +export const FFI_TYPES: {[key: string]: Buffer}; +export const FFI_OK: number; +export const FFI_BAD_TYPEDEF: number; +export const FFI_BAD_ABI: number; +export const FFI_DEFAULT_ABI: number; +export const FFI_FIRST_ABI: number; +export const FFI_LAST_ABI: number; +export const FFI_SYSV: number; +export const FFI_UNIX64: number; +export const RTLD_LAZY: number; +export const RTLD_NOW: number; +export const RTLD_LOCAL: number; +export const RTLD_GLOBAL: number; +export const RTLD_NOLOAD: number; +export const RTLD_NODELETE: number; +export const RTLD_NEXT: Buffer; +export const RTLD_DEFAULT: Buffer; +export const LIB_EXT: string; +export const FFI_TYPE: StructType; + +/** Default types. */ +export const types: { + void: ref.Type; int64: ref.Type; ushort: ref.Type; + int: ref.Type; uint64: ref.Type; float: ref.Type; + uint: ref.Type; long: ref.Type; double: ref.Type; + int8: ref.Type; ulong: ref.Type; Object: ref.Type; + uint8: ref.Type; longlong: ref.Type; CString: ref.Type; + int16: ref.Type; ulonglong: ref.Type; bool: ref.Type; + uint16: ref.Type; char: ref.Type; byte: ref.Type; + int32: ref.Type; uchar: ref.Type; size_t: ref.Type; + uint32: ref.Type; short: ref.Type; +}; diff --git a/types/ffi-napi/node-ffi-napi-buffer.d.ts b/types/ffi-napi/node-ffi-napi-buffer.d.ts new file mode 100644 index 0000000000..fa817e1662 --- /dev/null +++ b/types/ffi-napi/node-ffi-napi-buffer.d.ts @@ -0,0 +1,50 @@ +// DefinitelyTyped: partial + +interface Buffer { + /** Shorthand for `ref.address`. */ + address(): number; + /** Shorthand for `ref.deref`. */ + deref(): any; + /** Shorthand for `ref.isNull`. */ + isNull(): boolean; + /** Shorthand for `ref.readCString`. */ + readCString(offset?: number): string; + /** Shorthand for `ref.readInt64BE`. */ + readInt64BE(offset?: number): string; + /** Shorthand for `ref.readInt64LE`. */ + readInt64LE(offset?: number): string; + /** Shorthand for `ref.readObject`. */ + readObject(offset?: number): string; + /** Shorthand for `ref.readPointer`. */ + readPointer(offset?: number): string; + /** Shorthand for `ref.readUInt64BE`. */ + readUInt64BE(offset?: number): string; + /** Shorthand for `ref.readUInt64LE`. */ + readUInt64LE(offset?: number): string; + /** Shorthand for `ref.ref`. */ + ref(): Buffer; + /** Shorthand for `ref.reinterpret`. */ + reinterpret(size: number, offset?: number): Buffer; + /** Shorthand for `ref.reinterpretUntilZeros`. */ + reinterpretUntilZeros(size: number, offset?: number): Buffer; + /** Shorthand for `ref.writeCString`. */ + writeCString(offset: number, input: string, encoding?: string): void; + /** Shorthand for `ref.writeInt64BE`. */ + writeInt64BE(offset: number, input: number | string): any; + /** Shorthand for `ref.writeInt64LE`. */ + writeInt64LE(offset: number, input: number | string): any; + /** Shorthand for `ref.writeObject`. */ + writeObject(offset: number, object: object): void; + /** Shorthand for `ref.writePointer`. */ + writePointer(offset: number, pointer: Buffer): void; + /** Shorthand for `ref.writeUInt64BE`. */ + writeUInt64BE(offset: number, input: number | string): any; + /** Shorthand for `ref.writeUInt64LE`. */ + writeUInt64LE(offset: number, input: number | string): any; + + /** + * Generate string for inspecting. + * String includes the hex-encoded memory address of the Buffer instance. + */ + inspect(): string; +} diff --git a/types/ffi-napi/tsconfig.json b/types/ffi-napi/tsconfig.json new file mode 100644 index 0000000000..c914c55679 --- /dev/null +++ b/types/ffi-napi/tsconfig.json @@ -0,0 +1,24 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "node-ffi-napi-buffer.d.ts", + "ffi-napi-tests.ts" + ] +} diff --git a/types/ffi-napi/tslint.json b/types/ffi-napi/tslint.json new file mode 100644 index 0000000000..c5fa24ca82 --- /dev/null +++ b/types/ffi-napi/tslint.json @@ -0,0 +1,8 @@ +{ + "extends": "dtslint/dt.json", + + "rules": { + "no-consecutive-blank-lines": [true, 3], + "ban-types": [false] + } +} From daf1f6c5a7535e6dfe71a5222a9f0399cb386b3e Mon Sep 17 00:00:00 2001 From: Keerthi Niranjan Date: Thu, 16 Aug 2018 11:28:01 +0530 Subject: [PATCH 2/5] Add ref-array-di module --- types/ref-array-di/index.d.ts | 62 +++++++++++++++++++++++++ types/ref-array-di/tsconfig.json | 22 +++++++++ types/ref-array-di/tslint.json | 79 ++++++++++++++++++++++++++++++++ 3 files changed, 163 insertions(+) create mode 100644 types/ref-array-di/index.d.ts create mode 100644 types/ref-array-di/tsconfig.json create mode 100644 types/ref-array-di/tslint.json diff --git a/types/ref-array-di/index.d.ts b/types/ref-array-di/index.d.ts new file mode 100644 index 0000000000..04a69fd1ef --- /dev/null +++ b/types/ref-array-di/index.d.ts @@ -0,0 +1,62 @@ +// Type definitions for ref-array-di 1.2 +// Project: https://github.com/node-ffi-napi/ref-array-di +// Definitions by: Keerthi Niranjan , Kiran Niranjan +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +import ref = require('ref-napi'); + +interface ArrayType extends ref.Type { + BYTES_PER_ELEMENT: number; + fixedLength: number; + /** The reference to the base type. */ + type: ref.Type; + + /** + * Accepts a Buffer instance that should be an already-populated with data + * for the ArrayType. The "length" of the Array is determined by searching + * through the buffer's contents until an aligned NULL pointer is encountered. + */ + untilZeros(buffer: Buffer): { + [i: number]: T; length: number; toArray(): T[]; + toJSON(): T[]; inspect(): string; buffer: Buffer; ref(): Buffer; + }; + + new (length?: number): { + [i: number]: T; length: number; toArray(): T[]; + toJSON(): T[]; inspect(): string; buffer: Buffer; ref(): Buffer; + }; + new (data: number[], length?: number): { + [i: number]: T; length: number; toArray(): T[]; + toJSON(): T[]; inspect(): string; buffer: Buffer; ref(): Buffer; + }; + new (data: Buffer, length?: number): { + [i: number]: T; length: number; toArray(): T[]; + toJSON(): T[]; inspect(): string; buffer: Buffer; ref(): Buffer; + }; + (length?: number): { + [i: number]: T; length: number; toArray(): T[]; + toJSON(): T[]; inspect(): string; buffer: Buffer; ref(): Buffer; + }; + (data: number[], length?: number): { + [i: number]: T; length: number; toArray(): T[]; + toJSON(): T[]; inspect(): string; buffer: Buffer; ref(): Buffer; + }; + (data: Buffer, length?: number): { + [i: number]: T; length: number; toArray(): T[]; + toJSON(): T[]; inspect(): string; buffer: Buffer; ref(): Buffer; + }; +} + +/** + * The array type meta-constructor. + * The returned constructor's API is highly influenced by the WebGL + * TypedArray API. + */ +declare var ArrayType: { + new (type: ref.Type, length?: number): ArrayType; + new (type: string, length?: number): ArrayType; + (type: ref.Type, length?: number): ArrayType; + (type: string, length?: number): ArrayType; +}; + +export = ArrayType; diff --git a/types/ref-array-di/tsconfig.json b/types/ref-array-di/tsconfig.json new file mode 100644 index 0000000000..02963e3afd --- /dev/null +++ b/types/ref-array-di/tsconfig.json @@ -0,0 +1,22 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": false, + "strictFunctionTypes": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts" + ] +} \ No newline at end of file diff --git a/types/ref-array-di/tslint.json b/types/ref-array-di/tslint.json new file mode 100644 index 0000000000..a41bf5d19a --- /dev/null +++ b/types/ref-array-di/tslint.json @@ -0,0 +1,79 @@ +{ + "extends": "dtslint/dt.json", + "rules": { + "adjacent-overload-signatures": false, + "array-type": false, + "arrow-return-shorthand": false, + "ban-types": false, + "callable-types": false, + "comment-format": false, + "dt-header": false, + "eofline": false, + "export-just-namespace": false, + "import-spacing": false, + "interface-name": false, + "interface-over-type-literal": false, + "jsdoc-format": false, + "max-line-length": false, + "member-access": false, + "new-parens": false, + "no-any-union": false, + "no-boolean-literal-compare": false, + "no-conditional-assignment": false, + "no-consecutive-blank-lines": false, + "no-construct": false, + "no-declare-current-package": false, + "no-duplicate-imports": false, + "no-duplicate-variable": false, + "no-empty-interface": false, + "no-for-in-array": false, + "no-inferrable-types": false, + "no-internal-module": false, + "no-irregular-whitespace": false, + "no-mergeable-namespace": false, + "no-misused-new": false, + "no-namespace": false, + "no-object-literal-type-assertion": false, + "no-padding": false, + "no-redundant-jsdoc": false, + "no-redundant-jsdoc-2": false, + "no-redundant-undefined": false, + "no-reference-import": false, + "no-relative-import-in-test": false, + "no-self-import": false, + "no-single-declare-module": false, + "no-string-throw": false, + "no-unnecessary-callback-wrapper": false, + "no-unnecessary-class": false, + "no-unnecessary-generics": false, + "no-unnecessary-qualifier": false, + "no-unnecessary-type-assertion": false, + "no-useless-files": false, + "no-var-keyword": false, + "no-var-requires": false, + "no-void-expression": false, + "no-trailing-whitespace": false, + "object-literal-key-quotes": false, + "object-literal-shorthand": false, + "one-line": false, + "one-variable-per-declaration": false, + "only-arrow-functions": false, + "prefer-conditional-expression": false, + "prefer-const": false, + "prefer-declare-function": false, + "prefer-for-of": false, + "prefer-method-signature": false, + "prefer-template": false, + "radix": false, + "semicolon": false, + "space-before-function-paren": false, + "space-within-parens": false, + "strict-export-declare-modifiers": false, + "trim-file": false, + "triple-equals": false, + "typedef-whitespace": false, + "unified-signatures": false, + "void-return": false, + "whitespace": false + } +} From 07a3c48dc407ec2d2e0d780886a4024d9906142a Mon Sep 17 00:00:00 2001 From: Keerthi Niranjan Date: Thu, 16 Aug 2018 11:28:44 +0530 Subject: [PATCH 3/5] Add ref-napi module --- types/ref-napi/index.d.ts | 188 +++++++++++++++++++++++++++++++++++ types/ref-napi/tsconfig.json | 22 ++++ types/ref-napi/tslint.json | 79 +++++++++++++++ 3 files changed, 289 insertions(+) create mode 100644 types/ref-napi/index.d.ts create mode 100644 types/ref-napi/tsconfig.json create mode 100644 types/ref-napi/tslint.json diff --git a/types/ref-napi/index.d.ts b/types/ref-napi/index.d.ts new file mode 100644 index 0000000000..ab3b38d953 --- /dev/null +++ b/types/ref-napi/index.d.ts @@ -0,0 +1,188 @@ +// Type definitions for ref-napi 1.4 +// Project: https://github.com/node-ffi-napi/ref-napi +// Definitions by: Keerthi Niranjan , Kiran Niranjan +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +/// + +export interface Type { + /** The size in bytes required to hold this datatype. */ + size: number; + /** The current level of indirection of the buffer. */ + indirection: number; + /** To invoke when `ref.get` is invoked on a buffer of this type. */ + get(buffer: Buffer, offset: number): any; + /** To invoke when `ref.set` is invoked on a buffer of this type. */ + set(buffer: Buffer, offset: number, value: any): void; + /** The name to use during debugging for this datatype. */ + name?: string; + /** The alignment of this datatype when placed inside a struct. */ + alignment?: number; +} + +/** A Buffer that references the C NULL pointer. */ +export declare var NULL: Buffer; +/** A pointer-sized buffer pointing to NULL. */ +export declare var NULL_POINTER: Buffer; +/** Get the memory address of buffer. */ +export declare function address(buffer: Buffer): number; +/** Allocate the memory with the given value written to it. */ +export declare function alloc(type: Type, value?: any): Buffer; +/** Allocate the memory with the given value written to it. */ +export declare function alloc(type: string, value?: any): Buffer; + +/** + * Allocate the memory with the given string written to it with the given + * encoding (defaults to utf8). The buffer is 1 byte longer than the + * string itself, and is NULL terminated. + */ +export declare function allocCString(string: string, encoding?: string): Buffer; + +/** Coerce a type.*/ +export declare function coerceType(type: Type): Type; +/** Coerce a type. String are looked up from the ref.types object. */ +export declare function coerceType(type: string): Type; + +/** + * Get value after dereferencing buffer. + * That is, first it checks the indirection count of buffer's type, and + * if it's greater than 1 then it merely returns another Buffer, but with + * one level less indirection. + */ +export declare function deref(buffer: Buffer): any; + +/** Create clone of the type, with decremented indirection level by 1. */ +export declare function derefType(type: Type): Type; +/** Create clone of the type, with decremented indirection level by 1. */ +export declare function derefType(type: string): Type; +/** Represents the native endianness of the processor ("LE" or "BE"). */ +export declare var endianness: string; +/** Check the indirection level and return a dereferenced when necessary. */ +export declare function get(buffer: Buffer, offset?: number, type?: Type): any; +/** Check the indirection level and return a dereferenced when necessary. */ +export declare function get(buffer: Buffer, offset?: number, type?: string): any; +/** Get type of the buffer. Create a default type when none exists. */ +export declare function getType(buffer: Buffer): Type; +/** Check the NULL. */ +export declare function isNull(buffer: Buffer): boolean; +/** Read C string until the first NULL. */ +export declare function readCString(buffer: Buffer, offset?: number): string; + +/** + * Read a big-endian signed 64-bit int. + * If there is losing precision, then return a string, otherwise a number. + * @return {number|string} + */ +export declare function readInt64BE(buffer: Buffer, offset?: number): any; + +/** + * Read a little-endian signed 64-bit int. + * If there is losing precision, then return a string, otherwise a number. + * @return {number|string} + */ +export declare function readInt64LE(buffer: Buffer, offset?: number): any; + +/** Read a JS Object that has previously been written. */ +export declare function readObject(buffer: Buffer, offset?: number): Object; +/** Read data from the pointer. */ +export declare function readPointer(buffer: Buffer, offset?: number, + length?: number): Buffer; +/** + * Read a big-endian unsigned 64-bit int. + * If there is losing precision, then return a string, otherwise a number. + * @return {number|string} + */ +export declare function readUInt64BE(buffer: Buffer, offset?: number): any; + +/** + * Read a little-endian unsigned 64-bit int. + * If there is losing precision, then return a string, otherwise a number. + * @return {number|string} + */ +export declare function readUInt64LE(buffer: Buffer, offset?: number): any; + +/** Create pointer to buffer. */ +export declare function ref(buffer: Buffer): Buffer; +/** Create clone of the type, with incremented indirection level by 1. */ +export declare function refType(type: Type): Type; +/** Create clone of the type, with incremented indirection level by 1. */ +export declare function refType(type: string): Type; + +/** + * Create buffer with the specified size, with the same address as source. + * This function "attaches" source to the returned buffer to prevent it from + * being garbage collected. + */ +export declare function reinterpret(buffer: Buffer, size: number, + offset?: number): Buffer; +/** + * Scan past the boundary of the buffer's length until it finds size number + * of aligned NULL bytes. + */ +export declare function reinterpretUntilZeros(buffer: Buffer, size: number, + offset?: number): Buffer; + +/** Write pointer if the indirection is 1, otherwise write value. */ +export declare function set(buffer: Buffer, offset: number, value: any, type?: Type): void; +/** Write pointer if the indirection is 1, otherwise write value. */ +export declare function set(buffer: Buffer, offset: number, value: any, type?: string): void; +/** Write the string as a NULL terminated. Default encoding is utf8. */ +export declare function writeCString(buffer: Buffer, offset: number, + string: string, encoding?: string): void; +/** Write a big-endian signed 64-bit int. */ +export declare function writeInt64BE(buffer: Buffer, offset: number, input: number): void; +/** Write a big-endian signed 64-bit int. */ +export declare function writeInt64BE(buffer: Buffer, offset: number, input: string): void; +/** Write a little-endian signed 64-bit int. */ +export declare function writeInt64LE(buffer: Buffer, offset: number, input: number): void; +/** Write a little-endian signed 64-bit int. */ +export declare function writeInt64LE(buffer: Buffer, offset: number, input: string): void; + +/** + * Write the JS Object. This function "attaches" object to buffer to prevent + * it from being garbage collected. + */ +export declare function writeObject(buffer: Buffer, offset: number, object: Object): void; + +/** + * Write the memory address of pointer to buffer at the specified offset. This + * function "attaches" object to buffer to prevent it from being garbage collected. + */ +export declare function writePointer(buffer: Buffer, offset: number, + pointer: Buffer): void; + +/** Write a little-endian unsigned 64-bit int. */ +export declare function writeUInt64BE(buffer: Buffer, offset: number, input: number): void; +/** Write a little-endian unsigned 64-bit int. */ +export declare function writeUInt64BE(buffer: Buffer, offset: number, input: string): void; + +/** + * Attach object to buffer such. + * It prevents object from being garbage collected until buffer does. + */ +export declare function _attach(buffer: Buffer, object: Object): void; + +/** Same as ref.reinterpret, except that this version does not attach buffer. */ +export declare function _reinterpret(buffer: Buffer, size: number, + offset?: number): Buffer; +/** Same as ref.reinterpretUntilZeros, except that this version does not attach buffer. */ +export declare function _reinterpretUntilZeros(buffer: Buffer, size: number, + offset?: number): Buffer; +/** Same as ref.writePointer, except that this version does not attach pointer. */ +export declare function _writePointer(buffer: Buffer, offset: number, + pointer: Buffer): void; +/** Same as ref.writeObject, except that this version does not attach object. */ +export declare function _writeObject(buffer: Buffer, offset: number, object: Object): void; + +/** Default types. */ +export declare var types: { + void: Type; int64: Type; ushort: Type; + int: Type; uint64: Type; float: Type; + uint: Type; long: Type; double: Type; + int8: Type; ulong: Type; Object: Type; + uint8: Type; longlong: Type; CString: Type; + int16: Type; ulonglong: Type; bool: Type; + uint16: Type; char: Type; byte: Type; + int32: Type; uchar: Type; size_t: Type; + uint32: Type; short: Type; +}; diff --git a/types/ref-napi/tsconfig.json b/types/ref-napi/tsconfig.json new file mode 100644 index 0000000000..02963e3afd --- /dev/null +++ b/types/ref-napi/tsconfig.json @@ -0,0 +1,22 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": false, + "strictFunctionTypes": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts" + ] +} \ No newline at end of file diff --git a/types/ref-napi/tslint.json b/types/ref-napi/tslint.json new file mode 100644 index 0000000000..a41bf5d19a --- /dev/null +++ b/types/ref-napi/tslint.json @@ -0,0 +1,79 @@ +{ + "extends": "dtslint/dt.json", + "rules": { + "adjacent-overload-signatures": false, + "array-type": false, + "arrow-return-shorthand": false, + "ban-types": false, + "callable-types": false, + "comment-format": false, + "dt-header": false, + "eofline": false, + "export-just-namespace": false, + "import-spacing": false, + "interface-name": false, + "interface-over-type-literal": false, + "jsdoc-format": false, + "max-line-length": false, + "member-access": false, + "new-parens": false, + "no-any-union": false, + "no-boolean-literal-compare": false, + "no-conditional-assignment": false, + "no-consecutive-blank-lines": false, + "no-construct": false, + "no-declare-current-package": false, + "no-duplicate-imports": false, + "no-duplicate-variable": false, + "no-empty-interface": false, + "no-for-in-array": false, + "no-inferrable-types": false, + "no-internal-module": false, + "no-irregular-whitespace": false, + "no-mergeable-namespace": false, + "no-misused-new": false, + "no-namespace": false, + "no-object-literal-type-assertion": false, + "no-padding": false, + "no-redundant-jsdoc": false, + "no-redundant-jsdoc-2": false, + "no-redundant-undefined": false, + "no-reference-import": false, + "no-relative-import-in-test": false, + "no-self-import": false, + "no-single-declare-module": false, + "no-string-throw": false, + "no-unnecessary-callback-wrapper": false, + "no-unnecessary-class": false, + "no-unnecessary-generics": false, + "no-unnecessary-qualifier": false, + "no-unnecessary-type-assertion": false, + "no-useless-files": false, + "no-var-keyword": false, + "no-var-requires": false, + "no-void-expression": false, + "no-trailing-whitespace": false, + "object-literal-key-quotes": false, + "object-literal-shorthand": false, + "one-line": false, + "one-variable-per-declaration": false, + "only-arrow-functions": false, + "prefer-conditional-expression": false, + "prefer-const": false, + "prefer-declare-function": false, + "prefer-for-of": false, + "prefer-method-signature": false, + "prefer-template": false, + "radix": false, + "semicolon": false, + "space-before-function-paren": false, + "space-within-parens": false, + "strict-export-declare-modifiers": false, + "trim-file": false, + "triple-equals": false, + "typedef-whitespace": false, + "unified-signatures": false, + "void-return": false, + "whitespace": false + } +} From a36c55be2ced48e3d3ddba15e543ad2e2308ab63 Mon Sep 17 00:00:00 2001 From: Keerthi Niranjan Date: Thu, 16 Aug 2018 11:29:23 +0530 Subject: [PATCH 4/5] Add ref-struct-di module --- types/ref-struct-di/index.d.ts | 60 ++++++++++++++++ types/ref-struct-di/ref-struct-di-tests.ts | 12 ++++ types/ref-struct-di/tsconfig.json | 23 +++++++ types/ref-struct-di/tslint.json | 79 ++++++++++++++++++++++ 4 files changed, 174 insertions(+) create mode 100644 types/ref-struct-di/index.d.ts create mode 100644 types/ref-struct-di/ref-struct-di-tests.ts create mode 100644 types/ref-struct-di/tsconfig.json create mode 100644 types/ref-struct-di/tslint.json diff --git a/types/ref-struct-di/index.d.ts b/types/ref-struct-di/index.d.ts new file mode 100644 index 0000000000..dd3d878fc1 --- /dev/null +++ b/types/ref-struct-di/index.d.ts @@ -0,0 +1,60 @@ +// Type definitions for ref-struct-di 1.1 +// Project: https://github.com/node-ffi-napi/ref-struct-di +// Definitions by: Keerthi Niranjan , Kiran Niranjan +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.2 + +import ref = require('ref-napi'); + +/** + * This is the `constructor` of the Struct type that gets returned. + * + * Invoke it with `new` to create a new Buffer instance backing the struct. + * Pass it an existing Buffer instance to use that as the backing buffer. + * Pass in an Object containing the struct fields to auto-populate the + * struct with the data. + * + * @constructor + */ +interface StructType extends ref.Type { + /** Pass it an existing Buffer instance to use that as the backing buffer. */ + new (arg: Buffer, data?: {}): any; + new (data?: {}): any; + /** Pass it an existing Buffer instance to use that as the backing buffer. */ + (arg: Buffer, data?: {}): any; + (data?: {}): any; + + fields: { [key: string]: { type: ref.Type } }; + + /** + * Adds a new field to the struct instance with the given name and type. + * Note that this function will throw an Error if any instances of the struct + * type have already been created, therefore this function must be called at the + * beginning, before any instances are created. + */ + defineProperty(name: string, type: ref.Type): void; + + /** + * Adds a new field to the struct instance with the given name and type. + * Note that this function will throw an Error if any instances of the struct + * type have already been created, therefore this function must be called at the + * beginning, before any instances are created. + */ + defineProperty(name: string, type: string): void; + + /** + * Custom for struct type instances. + * @override + */ + toString(): string; +} + +/** The struct type meta-constructor. */ +declare var StructType: { + new (fields?: object, opt?: object): StructType; + new (fields?: any[]): StructType; + (fields?: object, opt?: object): StructType; + (fields?: any[]): StructType; +} + +export = StructType; diff --git a/types/ref-struct-di/ref-struct-di-tests.ts b/types/ref-struct-di/ref-struct-di-tests.ts new file mode 100644 index 0000000000..60b03e0cd5 --- /dev/null +++ b/types/ref-struct-di/ref-struct-di-tests.ts @@ -0,0 +1,12 @@ +import ref = require("ref-napi"); +import StructType = require("ref-struct-di"); + +const normalStruct = StructType({ + t: ref.types.uint8, + v: ref.types.long, +}); + +const packedStruct = StructType({ + t: ref.types.uint8, + v: ref.types.long, +}, {packed: true}); diff --git a/types/ref-struct-di/tsconfig.json b/types/ref-struct-di/tsconfig.json new file mode 100644 index 0000000000..fef426ba7a --- /dev/null +++ b/types/ref-struct-di/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": false, + "strictFunctionTypes": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "ref-struct-di-tests.ts" + ] +} diff --git a/types/ref-struct-di/tslint.json b/types/ref-struct-di/tslint.json new file mode 100644 index 0000000000..a41bf5d19a --- /dev/null +++ b/types/ref-struct-di/tslint.json @@ -0,0 +1,79 @@ +{ + "extends": "dtslint/dt.json", + "rules": { + "adjacent-overload-signatures": false, + "array-type": false, + "arrow-return-shorthand": false, + "ban-types": false, + "callable-types": false, + "comment-format": false, + "dt-header": false, + "eofline": false, + "export-just-namespace": false, + "import-spacing": false, + "interface-name": false, + "interface-over-type-literal": false, + "jsdoc-format": false, + "max-line-length": false, + "member-access": false, + "new-parens": false, + "no-any-union": false, + "no-boolean-literal-compare": false, + "no-conditional-assignment": false, + "no-consecutive-blank-lines": false, + "no-construct": false, + "no-declare-current-package": false, + "no-duplicate-imports": false, + "no-duplicate-variable": false, + "no-empty-interface": false, + "no-for-in-array": false, + "no-inferrable-types": false, + "no-internal-module": false, + "no-irregular-whitespace": false, + "no-mergeable-namespace": false, + "no-misused-new": false, + "no-namespace": false, + "no-object-literal-type-assertion": false, + "no-padding": false, + "no-redundant-jsdoc": false, + "no-redundant-jsdoc-2": false, + "no-redundant-undefined": false, + "no-reference-import": false, + "no-relative-import-in-test": false, + "no-self-import": false, + "no-single-declare-module": false, + "no-string-throw": false, + "no-unnecessary-callback-wrapper": false, + "no-unnecessary-class": false, + "no-unnecessary-generics": false, + "no-unnecessary-qualifier": false, + "no-unnecessary-type-assertion": false, + "no-useless-files": false, + "no-var-keyword": false, + "no-var-requires": false, + "no-void-expression": false, + "no-trailing-whitespace": false, + "object-literal-key-quotes": false, + "object-literal-shorthand": false, + "one-line": false, + "one-variable-per-declaration": false, + "only-arrow-functions": false, + "prefer-conditional-expression": false, + "prefer-const": false, + "prefer-declare-function": false, + "prefer-for-of": false, + "prefer-method-signature": false, + "prefer-template": false, + "radix": false, + "semicolon": false, + "space-before-function-paren": false, + "space-within-parens": false, + "strict-export-declare-modifiers": false, + "trim-file": false, + "triple-equals": false, + "typedef-whitespace": false, + "unified-signatures": false, + "void-return": false, + "whitespace": false + } +} From 15a706e124baacfb796ada019dd5a5413355bf42 Mon Sep 17 00:00:00 2001 From: Keerthi Niranjan Date: Thu, 16 Aug 2018 11:30:00 +0530 Subject: [PATCH 5/5] Add ref-union-di module --- types/ref-union-di/index.d.ts | 59 ++++++++++++++++++++++++ types/ref-union-di/tsconfig.json | 22 +++++++++ types/ref-union-di/tslint.json | 79 ++++++++++++++++++++++++++++++++ 3 files changed, 160 insertions(+) create mode 100644 types/ref-union-di/index.d.ts create mode 100644 types/ref-union-di/tsconfig.json create mode 100644 types/ref-union-di/tslint.json diff --git a/types/ref-union-di/index.d.ts b/types/ref-union-di/index.d.ts new file mode 100644 index 0000000000..29c7401df9 --- /dev/null +++ b/types/ref-union-di/index.d.ts @@ -0,0 +1,59 @@ +// Type definitions for ref-union-di 1.0 +// Project: https://github.com/node-ffi-napi/ref-union-di +// Definitions by: Keerthi Niranjan , Kiran Niranjan +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +import ref = require('ref-napi'); + +/** + * This is the `constructor` of the Struct type that gets returned. + * + * Invoke it with `new` to create a new Buffer instance backing the union. + * Pass it an existing Buffer instance to use that as the backing buffer. + * Pass in an Object containing the union fields to auto-populate the + * union with the data. + * + * @constructor + */ +interface UnionType extends ref.Type { + /** Pass it an existing Buffer instance to use that as the backing buffer. */ + new (arg: Buffer, data?: {}): any; + new (data?: {}): any; + /** Pass it an existing Buffer instance to use that as the backing buffer. */ + (arg: Buffer, data?: {}): any; + (data?: {}): any; + + fields: { [key: string]: { type: ref.Type } }; + + /** + * Adds a new field to the union instance with the given name and type. + * Note that this function will throw an Error if any instances of the union + * type have already been created, therefore this function must be called at the + * beginning, before any instances are created. + */ + defineProperty(name: string, type: ref.Type): void; + + /** + * Adds a new field to the union instance with the given name and type. + * Note that this function will throw an Error if any instances of the union + * type have already been created, therefore this function must be called at the + * beginning, before any instances are created. + */ + defineProperty(name: string, type: string): void; + + /** + * Custom for union type instances. + * @override + */ + toString(): string; +} + +/** The union type meta-constructor. */ +declare var UnionType: { + new (fields?: {}): UnionType; + new (fields?: any[]): UnionType; + (fields?: {}): UnionType; + (fields?: any[]): UnionType; +} + +export = UnionType; diff --git a/types/ref-union-di/tsconfig.json b/types/ref-union-di/tsconfig.json new file mode 100644 index 0000000000..02963e3afd --- /dev/null +++ b/types/ref-union-di/tsconfig.json @@ -0,0 +1,22 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": false, + "strictFunctionTypes": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts" + ] +} \ No newline at end of file diff --git a/types/ref-union-di/tslint.json b/types/ref-union-di/tslint.json new file mode 100644 index 0000000000..a41bf5d19a --- /dev/null +++ b/types/ref-union-di/tslint.json @@ -0,0 +1,79 @@ +{ + "extends": "dtslint/dt.json", + "rules": { + "adjacent-overload-signatures": false, + "array-type": false, + "arrow-return-shorthand": false, + "ban-types": false, + "callable-types": false, + "comment-format": false, + "dt-header": false, + "eofline": false, + "export-just-namespace": false, + "import-spacing": false, + "interface-name": false, + "interface-over-type-literal": false, + "jsdoc-format": false, + "max-line-length": false, + "member-access": false, + "new-parens": false, + "no-any-union": false, + "no-boolean-literal-compare": false, + "no-conditional-assignment": false, + "no-consecutive-blank-lines": false, + "no-construct": false, + "no-declare-current-package": false, + "no-duplicate-imports": false, + "no-duplicate-variable": false, + "no-empty-interface": false, + "no-for-in-array": false, + "no-inferrable-types": false, + "no-internal-module": false, + "no-irregular-whitespace": false, + "no-mergeable-namespace": false, + "no-misused-new": false, + "no-namespace": false, + "no-object-literal-type-assertion": false, + "no-padding": false, + "no-redundant-jsdoc": false, + "no-redundant-jsdoc-2": false, + "no-redundant-undefined": false, + "no-reference-import": false, + "no-relative-import-in-test": false, + "no-self-import": false, + "no-single-declare-module": false, + "no-string-throw": false, + "no-unnecessary-callback-wrapper": false, + "no-unnecessary-class": false, + "no-unnecessary-generics": false, + "no-unnecessary-qualifier": false, + "no-unnecessary-type-assertion": false, + "no-useless-files": false, + "no-var-keyword": false, + "no-var-requires": false, + "no-void-expression": false, + "no-trailing-whitespace": false, + "object-literal-key-quotes": false, + "object-literal-shorthand": false, + "one-line": false, + "one-variable-per-declaration": false, + "only-arrow-functions": false, + "prefer-conditional-expression": false, + "prefer-const": false, + "prefer-declare-function": false, + "prefer-for-of": false, + "prefer-method-signature": false, + "prefer-template": false, + "radix": false, + "semicolon": false, + "space-before-function-paren": false, + "space-within-parens": false, + "strict-export-declare-modifiers": false, + "trim-file": false, + "triple-equals": false, + "typedef-whitespace": false, + "unified-signatures": false, + "void-return": false, + "whitespace": false + } +}