diff --git a/package.json b/package.json index e398a1f0..f848217b 100644 --- a/package.json +++ b/package.json @@ -25,6 +25,7 @@ "semver": "^7.1.3" }, "devDependencies": { + "@types/bindings": "^1.5.5", "husky": "^4.3.0", "lint-staged": "^10.5.2" } diff --git a/src/1-getting-started/1_hello_world/nan/hello.js b/src/1-getting-started/1_hello_world/nan/hello.js index 45cff083..4930298e 100644 --- a/src/1-getting-started/1_hello_world/nan/hello.js +++ b/src/1-getting-started/1_hello_world/nan/hello.js @@ -1,3 +1,4 @@ +/** @type {import('../type')} */ var addon = require('bindings')('hello'); console.log(addon.hello()); // 'world' \ No newline at end of file diff --git a/src/1-getting-started/1_hello_world/napi/hello.js b/src/1-getting-started/1_hello_world/napi/hello.js index 55570039..6640f59d 100644 --- a/src/1-getting-started/1_hello_world/napi/hello.js +++ b/src/1-getting-started/1_hello_world/napi/hello.js @@ -1,3 +1,4 @@ +/** @type {import('../type')} */ var addon = require('bindings')('hello'); console.log(addon.hello()); // 'world' diff --git a/src/1-getting-started/1_hello_world/node-addon-api-addon-class/hello.js b/src/1-getting-started/1_hello_world/node-addon-api-addon-class/hello.js index 55570039..6640f59d 100644 --- a/src/1-getting-started/1_hello_world/node-addon-api-addon-class/hello.js +++ b/src/1-getting-started/1_hello_world/node-addon-api-addon-class/hello.js @@ -1,3 +1,4 @@ +/** @type {import('../type')} */ var addon = require('bindings')('hello'); console.log(addon.hello()); // 'world' diff --git a/src/1-getting-started/1_hello_world/node-addon-api/hello.js b/src/1-getting-started/1_hello_world/node-addon-api/hello.js index 55570039..6640f59d 100644 --- a/src/1-getting-started/1_hello_world/node-addon-api/hello.js +++ b/src/1-getting-started/1_hello_world/node-addon-api/hello.js @@ -1,3 +1,4 @@ +/** @type {import('../type')} */ var addon = require('bindings')('hello'); console.log(addon.hello()); // 'world' diff --git a/src/1-getting-started/1_hello_world/type.d.ts b/src/1-getting-started/1_hello_world/type.d.ts new file mode 100644 index 00000000..1efb560f --- /dev/null +++ b/src/1-getting-started/1_hello_world/type.d.ts @@ -0,0 +1,4 @@ +/** + * Returns the string "world". + */ +export function hello(): "world"; diff --git a/src/1-getting-started/2_function_arguments/nan/addon.js b/src/1-getting-started/2_function_arguments/nan/addon.js index 14fc6dd8..8c2e2bae 100644 --- a/src/1-getting-started/2_function_arguments/nan/addon.js +++ b/src/1-getting-started/2_function_arguments/nan/addon.js @@ -1,3 +1,4 @@ +/** @type {import('../type')} */ var addon = require('bindings')('addon.node') console.log('This should be eight:', addon.add(3, 5)) \ No newline at end of file diff --git a/src/1-getting-started/2_function_arguments/napi/addon.js b/src/1-getting-started/2_function_arguments/napi/addon.js index 14fc6dd8..8c2e2bae 100644 --- a/src/1-getting-started/2_function_arguments/napi/addon.js +++ b/src/1-getting-started/2_function_arguments/napi/addon.js @@ -1,3 +1,4 @@ +/** @type {import('../type')} */ var addon = require('bindings')('addon.node') console.log('This should be eight:', addon.add(3, 5)) \ No newline at end of file diff --git a/src/1-getting-started/2_function_arguments/type.d.ts b/src/1-getting-started/2_function_arguments/type.d.ts new file mode 100644 index 00000000..7cac38dc --- /dev/null +++ b/src/1-getting-started/2_function_arguments/type.d.ts @@ -0,0 +1,6 @@ +/** + * add two digits + * @param a first digit + * @param b second digit + */ +export function add(a: number, b: number): number; \ No newline at end of file diff --git a/src/1-getting-started/3_callbacks/nan/addon.js b/src/1-getting-started/3_callbacks/nan/addon.js index 1839c204..dc7e9f91 100644 --- a/src/1-getting-started/3_callbacks/nan/addon.js +++ b/src/1-getting-started/3_callbacks/nan/addon.js @@ -1,3 +1,6 @@ +/** + * @type {import('../type').default} + */ var addon = require('bindings')('addon'); addon(function(msg){ diff --git a/src/1-getting-started/3_callbacks/napi/addon.js b/src/1-getting-started/3_callbacks/napi/addon.js index 4b5299c9..7dc53e09 100644 --- a/src/1-getting-started/3_callbacks/napi/addon.js +++ b/src/1-getting-started/3_callbacks/napi/addon.js @@ -1,3 +1,6 @@ +/** + * @type {import('../type').default} + */ var addon = require('bindings')('addon'); addon(function(msg){ diff --git a/src/1-getting-started/3_callbacks/node-addon-api/addon.js b/src/1-getting-started/3_callbacks/node-addon-api/addon.js index 4b5299c9..7dc53e09 100644 --- a/src/1-getting-started/3_callbacks/node-addon-api/addon.js +++ b/src/1-getting-started/3_callbacks/node-addon-api/addon.js @@ -1,3 +1,6 @@ +/** + * @type {import('../type').default} + */ var addon = require('bindings')('addon'); addon(function(msg){ diff --git a/src/1-getting-started/3_callbacks/type.d.ts b/src/1-getting-started/3_callbacks/type.d.ts new file mode 100644 index 00000000..d38fe507 --- /dev/null +++ b/src/1-getting-started/3_callbacks/type.d.ts @@ -0,0 +1 @@ +export default function addon(callback: (msg: "hello world") => void): void; \ No newline at end of file diff --git a/src/1-getting-started/4_object_factory/nan/addon.js b/src/1-getting-started/4_object_factory/nan/addon.js index b6c4dafc..dfc2c0e9 100644 --- a/src/1-getting-started/4_object_factory/nan/addon.js +++ b/src/1-getting-started/4_object_factory/nan/addon.js @@ -1,3 +1,6 @@ +/** + * @type {import('../type').default} + */ var addon = require('bindings')('addon'); var obj1 = addon('hello'); diff --git a/src/1-getting-started/4_object_factory/napi/addon.js b/src/1-getting-started/4_object_factory/napi/addon.js index eecdb28f..dcecb2df 100644 --- a/src/1-getting-started/4_object_factory/napi/addon.js +++ b/src/1-getting-started/4_object_factory/napi/addon.js @@ -1,3 +1,6 @@ +/** + * @type {import('../type').default} + */ var addon = require('bindings')('addon'); var obj1 = addon('hello'); diff --git a/src/1-getting-started/4_object_factory/node-addon-api/addon.js b/src/1-getting-started/4_object_factory/node-addon-api/addon.js index b6c4dafc..dfc2c0e9 100644 --- a/src/1-getting-started/4_object_factory/node-addon-api/addon.js +++ b/src/1-getting-started/4_object_factory/node-addon-api/addon.js @@ -1,3 +1,6 @@ +/** + * @type {import('../type').default} + */ var addon = require('bindings')('addon'); var obj1 = addon('hello'); diff --git a/src/1-getting-started/4_object_factory/type.d.ts b/src/1-getting-started/4_object_factory/type.d.ts new file mode 100644 index 00000000..67d1d6a1 --- /dev/null +++ b/src/1-getting-started/4_object_factory/type.d.ts @@ -0,0 +1,4 @@ +/** + * Returns the string "world". + */ +export default function hello(msg: string): { msg: string }; diff --git a/src/1-getting-started/5_function_factory/nan/addon.js b/src/1-getting-started/5_function_factory/nan/addon.js index f5a7f96c..4ae14161 100644 --- a/src/1-getting-started/5_function_factory/nan/addon.js +++ b/src/1-getting-started/5_function_factory/nan/addon.js @@ -1,3 +1,6 @@ +/** + * @type {import('../type').default} + */ var addon = require('bindings')('addon'); var fn = addon(); diff --git a/src/1-getting-started/5_function_factory/napi/addon.js b/src/1-getting-started/5_function_factory/napi/addon.js index edf4bf02..f365c05e 100644 --- a/src/1-getting-started/5_function_factory/napi/addon.js +++ b/src/1-getting-started/5_function_factory/napi/addon.js @@ -1,3 +1,6 @@ +/** + * @type {import('../type').default} + */ var addon = require('bindings')('addon'); var fn = addon(); diff --git a/src/1-getting-started/5_function_factory/node-addon-api/addon.js b/src/1-getting-started/5_function_factory/node-addon-api/addon.js index edf4bf02..f365c05e 100644 --- a/src/1-getting-started/5_function_factory/node-addon-api/addon.js +++ b/src/1-getting-started/5_function_factory/node-addon-api/addon.js @@ -1,3 +1,6 @@ +/** + * @type {import('../type').default} + */ var addon = require('bindings')('addon'); var fn = addon(); diff --git a/src/1-getting-started/5_function_factory/type.d.ts b/src/1-getting-started/5_function_factory/type.d.ts new file mode 100644 index 00000000..7600b924 --- /dev/null +++ b/src/1-getting-started/5_function_factory/type.d.ts @@ -0,0 +1,4 @@ +/** + * Returns the string "world". + */ +export default function factory(): ()=> "hello world"; diff --git a/src/1-getting-started/6_object_wrap/nan/addon.js b/src/1-getting-started/6_object_wrap/nan/addon.js index d7dd6e0a..34efaf0d 100644 --- a/src/1-getting-started/6_object_wrap/nan/addon.js +++ b/src/1-getting-started/6_object_wrap/nan/addon.js @@ -1,3 +1,6 @@ +/** + * @type {import('../type')} + */ var addon = require('bindings')('addon'); var obj = new addon.MyObject(10); diff --git a/src/1-getting-started/6_object_wrap/napi/addon.js b/src/1-getting-started/6_object_wrap/napi/addon.js index 1a5aec65..eb7fc3e5 100644 --- a/src/1-getting-started/6_object_wrap/napi/addon.js +++ b/src/1-getting-started/6_object_wrap/napi/addon.js @@ -1,3 +1,6 @@ +/** + * @type {import('./type')} + */ var addon = require('bindings')('addon'); var obj = new addon.MyObject(10); diff --git a/src/1-getting-started/6_object_wrap/napi/type.d.ts b/src/1-getting-started/6_object_wrap/napi/type.d.ts new file mode 100644 index 00000000..de62e98c --- /dev/null +++ b/src/1-getting-started/6_object_wrap/napi/type.d.ts @@ -0,0 +1,7 @@ +export class MyObject{ + public value: number; + + constructor(num: number); + public plusOne(): number; + public multiply(num?: number): MyObject; +} \ No newline at end of file diff --git a/src/1-getting-started/6_object_wrap/node-addon-api/addon.js b/src/1-getting-started/6_object_wrap/node-addon-api/addon.js index d7dd6e0a..34efaf0d 100644 --- a/src/1-getting-started/6_object_wrap/node-addon-api/addon.js +++ b/src/1-getting-started/6_object_wrap/node-addon-api/addon.js @@ -1,3 +1,6 @@ +/** + * @type {import('../type')} + */ var addon = require('bindings')('addon'); var obj = new addon.MyObject(10); diff --git a/src/1-getting-started/6_object_wrap/type.d.ts b/src/1-getting-started/6_object_wrap/type.d.ts new file mode 100644 index 00000000..80eeb77b --- /dev/null +++ b/src/1-getting-started/6_object_wrap/type.d.ts @@ -0,0 +1,6 @@ +export declare class MyObject{ + constructor(num: number); + public value(): number; + public plusOne(): number; + public multiply(num?: number): MyObject; +} \ No newline at end of file diff --git a/src/1-getting-started/7_factory_wrap/nan/addon.js b/src/1-getting-started/7_factory_wrap/nan/addon.js index 13f7c9d5..36e2a598 100644 --- a/src/1-getting-started/7_factory_wrap/nan/addon.js +++ b/src/1-getting-started/7_factory_wrap/nan/addon.js @@ -1,3 +1,6 @@ +/** + * @type {import('../type').default} + */ var createObject = require('bindings')('addon'); var obj = createObject(10); diff --git a/src/1-getting-started/7_factory_wrap/napi/addon.js b/src/1-getting-started/7_factory_wrap/napi/addon.js index 13f7c9d5..36e2a598 100644 --- a/src/1-getting-started/7_factory_wrap/napi/addon.js +++ b/src/1-getting-started/7_factory_wrap/napi/addon.js @@ -1,3 +1,6 @@ +/** + * @type {import('../type').default} + */ var createObject = require('bindings')('addon'); var obj = createObject(10); diff --git a/src/1-getting-started/7_factory_wrap/node-addon-api/addon.js b/src/1-getting-started/7_factory_wrap/node-addon-api/addon.js index 13f7c9d5..36e2a598 100644 --- a/src/1-getting-started/7_factory_wrap/node-addon-api/addon.js +++ b/src/1-getting-started/7_factory_wrap/node-addon-api/addon.js @@ -1,3 +1,6 @@ +/** + * @type {import('../type').default} + */ var createObject = require('bindings')('addon'); var obj = createObject(10); diff --git a/src/1-getting-started/7_factory_wrap/type.d.ts b/src/1-getting-started/7_factory_wrap/type.d.ts new file mode 100644 index 00000000..adf6c615 --- /dev/null +++ b/src/1-getting-started/7_factory_wrap/type.d.ts @@ -0,0 +1,3 @@ +export default function createObject(number: number): { + plusOne(): number; +} \ No newline at end of file diff --git a/src/1-getting-started/a-first-project/node-addon-api/lib/binding.js b/src/1-getting-started/a-first-project/node-addon-api/lib/binding.js index 79521490..0990909f 100644 --- a/src/1-getting-started/a-first-project/node-addon-api/lib/binding.js +++ b/src/1-getting-started/a-first-project/node-addon-api/lib/binding.js @@ -1,3 +1,6 @@ +/** + * @type {import('./type')} + */ const addon = require('../build/Release/hello-world-native'); module.exports = addon.HelloWorld diff --git a/src/1-getting-started/a-first-project/node-addon-api/lib/type.d.ts b/src/1-getting-started/a-first-project/node-addon-api/lib/type.d.ts new file mode 100644 index 00000000..755a39a0 --- /dev/null +++ b/src/1-getting-started/a-first-project/node-addon-api/lib/type.d.ts @@ -0,0 +1,4 @@ +/** + * Returns the string "world". + */ +export function HelloWorld(notused?: unknown): "world"; diff --git a/src/1-getting-started/a-first-project/node-addon-api/test/test_binding.js b/src/1-getting-started/a-first-project/node-addon-api/test/test_binding.js index a59e7cb7..cfd8e964 100644 --- a/src/1-getting-started/a-first-project/node-addon-api/test/test_binding.js +++ b/src/1-getting-started/a-first-project/node-addon-api/test/test_binding.js @@ -5,7 +5,7 @@ assert(HelloWorld, "The expected function is undefined"); function testBasic() { - const result = HelloWorld("hello"); + const result = HelloWorld("hello"); assert.strictEqual(result, "world", "Unexpected value returned"); } diff --git a/src/2-js-to-native-conversion/8_passing_wrapped/nan/addon.js b/src/2-js-to-native-conversion/8_passing_wrapped/nan/addon.js index 7667d1ff..1b52a158 100644 --- a/src/2-js-to-native-conversion/8_passing_wrapped/nan/addon.js +++ b/src/2-js-to-native-conversion/8_passing_wrapped/nan/addon.js @@ -1,3 +1,6 @@ +/** + * @type {import('../type')} + */ var addon = require('bindings')('addon'); var obj1 = addon.createObject(10); diff --git a/src/2-js-to-native-conversion/8_passing_wrapped/napi/addon.js b/src/2-js-to-native-conversion/8_passing_wrapped/napi/addon.js index 7667d1ff..b523efd1 100644 --- a/src/2-js-to-native-conversion/8_passing_wrapped/napi/addon.js +++ b/src/2-js-to-native-conversion/8_passing_wrapped/napi/addon.js @@ -1,5 +1,9 @@ +/** + * @type {import('../type')} + */ var addon = require('bindings')('addon'); + var obj1 = addon.createObject(10); var obj2 = addon.createObject(20); var result = addon.add(obj1, obj2); diff --git a/src/2-js-to-native-conversion/8_passing_wrapped/type.d.ts b/src/2-js-to-native-conversion/8_passing_wrapped/type.d.ts new file mode 100644 index 00000000..48160037 --- /dev/null +++ b/src/2-js-to-native-conversion/8_passing_wrapped/type.d.ts @@ -0,0 +1,9 @@ +export class MyObject { +} + +export function createObject(num: number): MyObject; +export function add(obj1: MyObject, obj2: MyObject): number; +/** + * imported path ending by .node + */ +export const path: string; \ No newline at end of file diff --git a/src/2-js-to-native-conversion/array_buffer_to_native/node-addon-api/index.js b/src/2-js-to-native-conversion/array_buffer_to_native/node-addon-api/index.js index f81f5be5..56c36cc5 100644 --- a/src/2-js-to-native-conversion/array_buffer_to_native/node-addon-api/index.js +++ b/src/2-js-to-native-conversion/array_buffer_to_native/node-addon-api/index.js @@ -1,3 +1,6 @@ +/** + * @type {import('./type')} + */ const binding = require('bindings')('array_buffer_to_native'); const array = new Int32Array(10); diff --git a/src/2-js-to-native-conversion/array_buffer_to_native/node-addon-api/type.d.ts b/src/2-js-to-native-conversion/array_buffer_to_native/node-addon-api/type.d.ts new file mode 100644 index 00000000..70455255 --- /dev/null +++ b/src/2-js-to-native-conversion/array_buffer_to_native/node-addon-api/type.d.ts @@ -0,0 +1,5 @@ +/** + * dump a buffer to the console + * @param buffer buffer to dump + */ +export function AcceptArrayBuffer(buffer: ArrayBufferLike): void; diff --git a/src/2-js-to-native-conversion/object-template-demo/nan/index.js b/src/2-js-to-native-conversion/object-template-demo/nan/index.js index e1f4c1cc..379f9d6b 100644 --- a/src/2-js-to-native-conversion/object-template-demo/nan/index.js +++ b/src/2-js-to-native-conversion/object-template-demo/nan/index.js @@ -1,3 +1,6 @@ +/** + * @type {import('../type')} + */ const addon = require('bindings')('object-template-demo'); const interceptor = addon.create(); diff --git a/src/2-js-to-native-conversion/object-template-demo/napi/index.js b/src/2-js-to-native-conversion/object-template-demo/napi/index.js index bcf2920e..1f8c3e47 100644 --- a/src/2-js-to-native-conversion/object-template-demo/napi/index.js +++ b/src/2-js-to-native-conversion/object-template-demo/napi/index.js @@ -1,3 +1,6 @@ +/** + * @type {import('../type')} + */ const addon = require('bindings')('object_template_demo'); const interceptor = addon.create(); diff --git a/src/2-js-to-native-conversion/object-template-demo/type.d.ts b/src/2-js-to-native-conversion/object-template-demo/type.d.ts new file mode 100644 index 00000000..1a76383f --- /dev/null +++ b/src/2-js-to-native-conversion/object-template-demo/type.d.ts @@ -0,0 +1,6 @@ +/** + * proxy object template + */ +export type ObjectTemplate = any; + +export function create() : ObjectTemplate; \ No newline at end of file diff --git a/src/2-js-to-native-conversion/object-wrap-demo/node-addon-api/lib/binding.js b/src/2-js-to-native-conversion/object-wrap-demo/node-addon-api/lib/binding.js index 1a859b10..26dc5e31 100644 --- a/src/2-js-to-native-conversion/object-wrap-demo/node-addon-api/lib/binding.js +++ b/src/2-js-to-native-conversion/object-wrap-demo/node-addon-api/lib/binding.js @@ -1,5 +1,11 @@ +/** + * @type {import('./type')} + */ const addon = require('../build/Release/object-wrap-demo-native'); - +/** + * + * @param {string} name + */ function ObjectWrapDemo(name) { this.greet = function(str) { return _addonInstance.greet(str); diff --git a/src/2-js-to-native-conversion/object-wrap-demo/node-addon-api/lib/type.d.ts b/src/2-js-to-native-conversion/object-wrap-demo/node-addon-api/lib/type.d.ts new file mode 100644 index 00000000..f206f179 --- /dev/null +++ b/src/2-js-to-native-conversion/object-wrap-demo/node-addon-api/lib/type.d.ts @@ -0,0 +1,8 @@ +export class ObjectWrapDemo { + constructor(name: string); + /** + * return a string like "Hello, ${str}\nI am ${name}\n" + * @param str greeting string + */ + greet(str: string): string; +} \ No newline at end of file diff --git a/src/2-js-to-native-conversion/typed_array_to_native/node-addon-api/index.js b/src/2-js-to-native-conversion/typed_array_to_native/node-addon-api/index.js index 3900f451..87c4837d 100644 --- a/src/2-js-to-native-conversion/typed_array_to_native/node-addon-api/index.js +++ b/src/2-js-to-native-conversion/typed_array_to_native/node-addon-api/index.js @@ -1,3 +1,6 @@ +/** + * @type {import('./type')} + */ const binding = require('bindings')('typed_array_to_native'); { diff --git a/src/2-js-to-native-conversion/typed_array_to_native/node-addon-api/type.d.ts b/src/2-js-to-native-conversion/typed_array_to_native/node-addon-api/type.d.ts new file mode 100644 index 00000000..8a8f79e1 --- /dev/null +++ b/src/2-js-to-native-conversion/typed_array_to_native/node-addon-api/type.d.ts @@ -0,0 +1,2 @@ +export function AcceptByteArray(array: Uint8Array): undefined; +export function CreateByteArray(array: number[]): Uint8Array; diff --git a/src/3-context-awareness/napi/index.js b/src/3-context-awareness/napi/index.js index d956f73f..3db03eb1 100644 --- a/src/3-context-awareness/napi/index.js +++ b/src/3-context-awareness/napi/index.js @@ -9,6 +9,9 @@ const { } = require('worker_threads'); // We load the native addon. +/** + * @type {import('../type')} + */ const addon = require('bindings')('multiple_load'); // The iteration count can be tweaked to ensure that the output from the two @@ -16,8 +19,14 @@ const addon = require('bindings')('multiple_load'); // follows the output of the other, not really illustrating the concurrency. const iterations = 1000; -// This function is an idle loop that performs a random walk from 0 by calling -// into the native addon to either increment or decrement the initial value. +/** + * This function is an idle loop that performs a random walk from 0 by calling + * into the native addon to either increment or decrement the initial value. + * + * @param {import('../type')} addon + * @param {string} prefix + * @param {number} iterations + */ function useAddon(addon, prefix, iterations) { if (iterations >= 0) { if (Math.random() < 0.5) { diff --git a/src/3-context-awareness/node_10/index.js b/src/3-context-awareness/node_10/index.js index d956f73f..3db03eb1 100644 --- a/src/3-context-awareness/node_10/index.js +++ b/src/3-context-awareness/node_10/index.js @@ -9,6 +9,9 @@ const { } = require('worker_threads'); // We load the native addon. +/** + * @type {import('../type')} + */ const addon = require('bindings')('multiple_load'); // The iteration count can be tweaked to ensure that the output from the two @@ -16,8 +19,14 @@ const addon = require('bindings')('multiple_load'); // follows the output of the other, not really illustrating the concurrency. const iterations = 1000; -// This function is an idle loop that performs a random walk from 0 by calling -// into the native addon to either increment or decrement the initial value. +/** + * This function is an idle loop that performs a random walk from 0 by calling + * into the native addon to either increment or decrement the initial value. + * + * @param {import('../type')} addon + * @param {string} prefix + * @param {number} iterations + */ function useAddon(addon, prefix, iterations) { if (iterations >= 0) { if (Math.random() < 0.5) { diff --git a/src/3-context-awareness/type.d.ts b/src/3-context-awareness/type.d.ts new file mode 100644 index 00000000..6ae47cb5 --- /dev/null +++ b/src/3-context-awareness/type.d.ts @@ -0,0 +1,8 @@ +/** + * Decrement the counter with a CPU-intensif calculation. + */ +export function decrement(): number; +/** + * Increment the counter with a CPU-intensif calculation. + */ +export function increment(): number; diff --git a/src/4-references-and-handle-scope/function-reference-demo/node-addon-api/index.js b/src/4-references-and-handle-scope/function-reference-demo/node-addon-api/index.js index cb226c9b..9bc6b384 100644 --- a/src/4-references-and-handle-scope/function-reference-demo/node-addon-api/index.js +++ b/src/4-references-and-handle-scope/function-reference-demo/node-addon-api/index.js @@ -1,5 +1,8 @@ 'use strict' +/** + * @type {import('./type')} + */ const { NativeAddon } = require('bindings')('addon') function JSFnRef() { diff --git a/src/4-references-and-handle-scope/function-reference-demo/node-addon-api/type.d.ts b/src/4-references-and-handle-scope/function-reference-demo/node-addon-api/type.d.ts new file mode 100644 index 00000000..fb735063 --- /dev/null +++ b/src/4-references-and-handle-scope/function-reference-demo/node-addon-api/type.d.ts @@ -0,0 +1,11 @@ +export declare class NativeAddon { + constructor(JSFnRef: () => void, JSFn: () => void); + /** + * call the JSFn by the stored reference + */ + tryCallByStoredReference(): void; + /** + * call the JSFn directly + */ + tryCallByStoredFunction(): void; +} \ No newline at end of file diff --git a/src/5-async-work/async-iterator/node-addon-api/index.js b/src/5-async-work/async-iterator/node-addon-api/index.js index 59680b26..ca75eab8 100644 --- a/src/5-async-work/async-iterator/node-addon-api/index.js +++ b/src/5-async-work/async-iterator/node-addon-api/index.js @@ -1,5 +1,13 @@ +/** + * @type {import('./type')} + */ const { AsyncIteratorExample } = require('bindings')('example'); +/** + * + * @param {number} from + * @param {number} to + */ async function main(from, to) { const iterator = new AsyncIteratorExample(from, to); for await (const value of iterator) { diff --git a/src/5-async-work/async-iterator/node-addon-api/type.d.ts b/src/5-async-work/async-iterator/node-addon-api/type.d.ts new file mode 100644 index 00000000..0b3624ec --- /dev/null +++ b/src/5-async-work/async-iterator/node-addon-api/type.d.ts @@ -0,0 +1,10 @@ +export declare class AsyncIteratorExample { + from: number; + to: number; + constructor(from: number, to: number); + [Symbol.asyncIterator](): { + current: number; + last: number; + next(): Promise; + }; +} diff --git a/src/5-async-work/async_pi_estimate/nan/addon.js b/src/5-async-work/async_pi_estimate/nan/addon.js index 0c84f78e..f4980a5f 100755 --- a/src/5-async-work/async_pi_estimate/nan/addon.js +++ b/src/5-async-work/async_pi_estimate/nan/addon.js @@ -1,3 +1,6 @@ +/** + * @type {import('../type')} + */ var addon = require('./build/Release/addon'); var calculations = process.argv[2] || 100000000; diff --git a/src/5-async-work/async_pi_estimate/node-addon-api/addon.js b/src/5-async-work/async_pi_estimate/node-addon-api/addon.js index d52b3a3d..f0116ede 100755 --- a/src/5-async-work/async_pi_estimate/node-addon-api/addon.js +++ b/src/5-async-work/async_pi_estimate/node-addon-api/addon.js @@ -1,3 +1,6 @@ +/** + * @type {import('../type')} + */ var addon = require('bindings')('addon'); var calculations = process.argv[2] || 100000000; diff --git a/src/5-async-work/async_pi_estimate/type.d.ts b/src/5-async-work/async_pi_estimate/type.d.ts new file mode 100644 index 00000000..c7be2390 --- /dev/null +++ b/src/5-async-work/async_pi_estimate/type.d.ts @@ -0,0 +1,2 @@ +export function calculateSync(num: number): number; +export function calculateAsync(num: number, callback:(err: Error, num: string) => void): void; \ No newline at end of file diff --git a/src/5-async-work/async_work_promise/napi/index.js b/src/5-async-work/async_work_promise/napi/index.js index ae4e3773..9c44533d 100644 --- a/src/5-async-work/async_work_promise/napi/index.js +++ b/src/5-async-work/async_work_promise/napi/index.js @@ -1,4 +1,7 @@ // Use the "bindings" package to locate the native bindings. +/** + * @type {import('../type')} + */ const binding = require('bindings')('binding'); // Call the function "startWork" which the native bindings library exposes. diff --git a/src/5-async-work/async_work_promise/node-addon-api/index.js b/src/5-async-work/async_work_promise/node-addon-api/index.js index bb30d79e..1f8950f3 100644 --- a/src/5-async-work/async_work_promise/node-addon-api/index.js +++ b/src/5-async-work/async_work_promise/node-addon-api/index.js @@ -1,3 +1,6 @@ +/** + * @type {import('../type')} + */ const bindings = require("bindings")("addon"); bindings.doHeavyMath(2, 1).then(console.log); diff --git a/src/5-async-work/async_work_promise/type.d.ts b/src/5-async-work/async_work_promise/type.d.ts new file mode 100644 index 00000000..5909e3fd --- /dev/null +++ b/src/5-async-work/async_work_promise/type.d.ts @@ -0,0 +1 @@ +export function doHeavyMath(num1: number, num2: number): Promise; \ No newline at end of file diff --git a/src/5-async-work/async_work_thread_safe_function/napi/index.js b/src/5-async-work/async_work_thread_safe_function/napi/index.js index 85e9d0dc..c44204ca 100644 --- a/src/5-async-work/async_work_thread_safe_function/napi/index.js +++ b/src/5-async-work/async_work_thread_safe_function/napi/index.js @@ -1,4 +1,7 @@ // Use the "bindings" package to locate the native bindings. +/** + * @type {import('./type')} + */ const binding = require('bindings')('binding'); // Call the function "startThread" which the native bindings library exposes. diff --git a/src/5-async-work/async_work_thread_safe_function/napi/type.d.ts b/src/5-async-work/async_work_thread_safe_function/napi/type.d.ts new file mode 100644 index 00000000..b8754288 --- /dev/null +++ b/src/5-async-work/async_work_thread_safe_function/napi/type.d.ts @@ -0,0 +1 @@ +export function startThread(callback: (thread: number) => void): void; \ No newline at end of file diff --git a/src/5-async-work/call-js-from-async-worker-execute/node-addon-api/index.js b/src/5-async-work/call-js-from-async-worker-execute/node-addon-api/index.js index b9b9642a..ad7ea5f0 100644 --- a/src/5-async-work/call-js-from-async-worker-execute/node-addon-api/index.js +++ b/src/5-async-work/call-js-from-async-worker-execute/node-addon-api/index.js @@ -1,6 +1,9 @@ 'use strict' const { EventEmitter } = require('node:events'); +/** + * @type {import('./type')} + */ const { dispatch } = require('bindings')('dispatcher'); class Socket extends EventEmitter { diff --git a/src/5-async-work/call-js-from-async-worker-execute/node-addon-api/type.d.ts b/src/5-async-work/call-js-from-async-worker-execute/node-addon-api/type.d.ts new file mode 100644 index 00000000..3266d3ef --- /dev/null +++ b/src/5-async-work/call-js-from-async-worker-execute/node-addon-api/type.d.ts @@ -0,0 +1,6 @@ +type Action = +| ((action: "open" | "close") => void) +| ((action: "message", data: string) => void) +| ((action: "error", data: Error) => void); + +export function dispatch(callback: Action): void; diff --git a/src/5-async-work/napi-asyncworker-example/node-addon-api/test/Test.js b/src/5-async-work/napi-asyncworker-example/node-addon-api/test/Test.js index a9ec9c69..34b6514c 100644 --- a/src/5-async-work/napi-asyncworker-example/node-addon-api/test/Test.js +++ b/src/5-async-work/napi-asyncworker-example/node-addon-api/test/Test.js @@ -1,3 +1,6 @@ +/** + * @type {import('../type')} + */ const runWorker = require('../build/Release/napi-asyncworker-example-native'); let result = runWorker.runSimpleAsyncWorker(2, AsyncWorkerCompletion); @@ -9,6 +12,11 @@ console.log("runSimpleAsyncWorker returned '"+result+"'."); result = runWorker.runSimpleAsyncWorker(8, AsyncWorkerCompletion); console.log("runSimpleAsyncWorker returned '"+result+"'."); +/** + * + * @param {Error | null} err + * @param {string | undefined} result + */ function AsyncWorkerCompletion (err, result) { if (err) { console.log("SimpleAsyncWorker returned an error: ", err); diff --git a/src/5-async-work/napi-asyncworker-example/node-addon-api/type.d.ts b/src/5-async-work/napi-asyncworker-example/node-addon-api/type.d.ts new file mode 100644 index 00000000..223ac241 --- /dev/null +++ b/src/5-async-work/napi-asyncworker-example/node-addon-api/type.d.ts @@ -0,0 +1 @@ +export function runSimpleAsyncWorker(runTime: number, callback: (err: Error | null, result?: string) => void): string; diff --git a/src/8-tooling/typescript_with_addon/node-addon-api/index.ts b/src/8-tooling/typescript_with_addon/node-addon-api/index.ts index 14cd72ad..f80e1997 100644 --- a/src/8-tooling/typescript_with_addon/node-addon-api/index.ts +++ b/src/8-tooling/typescript_with_addon/node-addon-api/index.ts @@ -1,7 +1,7 @@ -import {javascriptPart} from './jsPart'; +import { javascriptPart } from './jsPart'; class TypeScriptPart { - constructor(value:number) { + constructor(value: number) { console.log('I\'m a TypeScript class constructor I\'m sending', value, 'to JavaScript and I received', javascriptPart(value)); diff --git a/src/8-tooling/typescript_with_addon/node-addon-api/jsPart.d.ts b/src/8-tooling/typescript_with_addon/node-addon-api/jsPart.d.ts new file mode 100644 index 00000000..702abb98 --- /dev/null +++ b/src/8-tooling/typescript_with_addon/node-addon-api/jsPart.d.ts @@ -0,0 +1,8 @@ +/** + * will forward the call to cppPartExportedByNapi + * + * typing for jsPart.js + * + * @param value will be send to cppPartExportedByNapi + */ +export function javascriptPart(value: number): number; \ No newline at end of file diff --git a/src/8-tooling/typescript_with_addon/node-addon-api/jsPart.js b/src/8-tooling/typescript_with_addon/node-addon-api/jsPart.js index 4db1151e..df6da81c 100644 --- a/src/8-tooling/typescript_with_addon/node-addon-api/jsPart.js +++ b/src/8-tooling/typescript_with_addon/node-addon-api/jsPart.js @@ -1,6 +1,9 @@ +/** + * @type {import('./type')} + */ const addon = require('bindings')('typescript_with_addon'); -function javascriptPart (value) { +function javascriptPart(value) { const result = addon.cppPartExportedByNapi(++value); console.log('I\'m a Javascript function and I\'m sending', diff --git a/src/8-tooling/typescript_with_addon/node-addon-api/type.d.ts b/src/8-tooling/typescript_with_addon/node-addon-api/type.d.ts new file mode 100644 index 00000000..ba03212b --- /dev/null +++ b/src/8-tooling/typescript_with_addon/node-addon-api/type.d.ts @@ -0,0 +1,6 @@ +/** + * Typing for node-addon-api function + * + * @param number will be use as a 32bit integer in C++ then passed to callingCPart + */ +export function cppPartExportedByNapi(number: number): number; \ No newline at end of file