MCPcopy Create free account
hub / github.com/NodeRT/NodeRT

github.com/NodeRT/NodeRT @3.0.0

Chat with this repo
repository ↗ · DeepWiki ↗ · release 3.0.0 ↗ · + Follow
334 symbols 664 edges 80 files 25 documented · 7% updated 2y ago3.0.0 · 2018-09-12★ 66983 open issues

Browse by type

Functions 301 Types & classes 33
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

NodeRT

Consume WinRT modules from Node.js, Electron, and NW.js.

:package: Installation of WinRT Modules

If you just want to consume modules, you can install them directly from npm.

:computer: Example

npm install --save @nodert-win10-rs4/windows.ui.notifications
SDK Known As Windows Version npm Scope
Windows 10, Build 17134 Redstone 4 1803 npmjs.com/org/nodert-win10-rs4
Windows 10, Build 15063 Creators Update 1703 npmjs.com/org/nodert-win10-cu
Windows 10, Build 14393 Anniversary Update 1607 npmjs.com/org/nodert-win10-au
Windows 10, Build 10586 Threshold 2 1511 npmjs.com/~nodert-win10

[New!] You can now learn about NodeRT by watching the MS Build 2017 talk: "NodeRT: Using native Windows features from Node.js and Electron".

:wrench: Creation of WinRT Modules

NodeRT is a tool that automatically generates node.js Native add-on wrappers for UWP/WinRT APIs.

NodeRT automatically exposes Microsoft’s UWP/WinRT APIs to the Node.js environment by generating Node modules. This enables Node.js developers to write code that consumes native Windows capabilities. The generated modules' APIs are (almost) the same as the UWP/WinRT APIs listed in MSDN. NodeRT can be used to generate Node modules both from command line (NodeRTCmd) and from its UI tool (NodeRTUI). NodeRT is developed and released by a group of Node.js enthusiasts at Microsoft.

Here is an example of using NodeRT windows.devices.geolocation module to retrieve the current location:

var geolocation = require('windows.devices.geolocation');
var locator = new geolocation.Geolocator();

locator.getGeopositionAsync( function(err, res) {
  if (err) {
    console.error(err);
    return;
  }

  console.info('(', res.coordinate.longitude, ',',  res.coordinate.latitude, ')');
});

In general, any WinRT/UWP API that can be called by a desktop app can by called by node.js/Electron using NodeRT. For more details about of desktop supported WinRT/UWP APIs, please visit this page.

For more examples of what NodeRT can do, check out our samples section.

:books: Documentation

NodeRT Prerequisites

Generating a NodeRT module using the UI

Generating a NodeRT module using the cmd line interface

Building for Electron

Consuming a NodeRT module in node.js/Electron

License

Attributions

Contribute


NodeRT Prerequisites

First, in order to use WinRT you must be running on a Windows environment that supports WinRT- meaning Windows 10, Windows 8.1, Windows 8, or Windows Server 2012.

In order to use NodeRT, make sure you have the following installed:

  • Visual Studio 2017 or 2015 for generating Windows 10 compatible modules

  • Visual Studio 2013 or 2012 for generating Windows 8.1/8 compatible modules repsectively.

  • Windows SDK for the version of Windows your are using:

  • node.js (version > 10.*) - from nodejs.org

  • node-gyp - make sure to get the latest version from npm by running:

npm install -g node-gyp

Next, download the latest NodeRT release from here, or clone this repository to your machine and build the NodeRT solution using Visual Studio.


Generating a NodeRT module using the UI

First, launch the UI tool by running NodeRTUI.exe:

Alt Windows.Devices.Geolocation NodeRT module contents

Then, follow this short list of steps in order to create a NodeRT module:

  • Choose a WinMD file:

    • For Windows 10 SDK:

    c:\Program Files (x86)\Windows Kits\10\UnionMetadata\Windows.winmd - For Windows 8.1 SDK:

    c:\Program Files (x86)\Windows Kits\8.1\References\CommonConfiguration\Neutral\Windows.winmd - For Windows 8.0 SDK:

    c:\Program Files (x86)\Windows Kits\8.0\References\CommonConfiguration\Neutral\Windows.winmd * Choose a namespace to generate from the list of namespaces.

  • Select whether you are generating a Windows 10 compatible module using VS 015, Windows 8.1 compatible module using VS2013 or a Windows 8.0 compatible module using VS2012.

  • Choose the output directory in which the module will be created, or just stick with the default ones.

  • You're good to go, hit the Generate & Build button! A message box with (hopefully) a success message should appear shortly.

Generating a NodeRT module using the cmd line interface

NodeRT modules generation is available via a cmd-line interface using the NodeRTCmd tool.

An example of generating the Windows.Devices.Geolocation namespace from the Windows 10 Windows.winmd:

NodeRTCmd.exe --winmd "c:\Program Files (x86)\Windows Kits\10\UnionMetadata\Windows.winmd" --codegendir c:\NodeRT\codegen --outdir c:\NodeRT\output --namespace Windows.Devices.Geolocation

Note that omitting the --namespace option will generate all of the namespaces in the Winmd file.

The following is the list of options that the tool supports:

 --winmd [path]              File path to winmd file from which the module
                             will be generated

 --namespaces                Lists all of the namespaces in the winmd file
                             (only needs --winmd)

 --namespace [namespace]     The namespace to generate from the winmd when
                             not specified , all namespaces will be generated

 --outdir [path]             The output dir in which the compiled NodeRT module
                             will be created in

 --vs [Vs2017|Vs2015|Vs2013|Vs2012] Optional, VS version to use, default is Vs2017

 --winver [10|8.1|8]         Optional, Windows SDK version to use, default is 10

 --npmscope                  Optional, the scope that will be specified for the generated
                             npm package

 --npmversion                Optional, the version that will be specified for the generated
                             npm package

 --nodefgen                  Optional, specifying this option will reult in
                             skipping the generation of TypeScript and
                             JavaScript definition files

 --nobuild                   Optional, specifying this option will result in
                             skipping the build process for the NodeRT module   

 --verbose                   Optional, specifying this option will result in
                             verbose output for the module build operation

 --help                      Print this help screen


Building for Electron

In order to build the generated NodeRT module for Electron, you can follow the instructions in here.

The easiest way to build the module for Electron, is probably to run node-gyp directly from the module folder with the appropriate cmd-line arguments.

For example, opening the cmd-line, cd-ing to the generated module directory and running:

node-gyp rebuild --target=1.3.1 --arch=x64 --dist-url=https://atom.io/download/atom-shell

Just make sure to use the correct Electron version for the "target" argument (here we used 1.3.1).

After rebuilding the module - you can copy it to your Electron app node_modules directory (in case you havn't done that already) and use it like every other node.js/Electron module.


Consuming a NodeRT module in node.js/Electron

Requiring a generated NodeRT module is just like requiring any other node.js module - if for example, you've just generated Windows.Devices.Geolocation, copy the generated windows.devices.geolocation directory from the output folder to a node_modules folder near you (or use a full path), and run:

var geolocation = require('windows.devices.geolocation');

If you are working in the node console (AKA REPL), then entering geolocation will result in printing the contents of the namespace:

Alt Windows.Devices.Geolocation NodeRT module contents

Creating a new WinRT object is done with the new operator. In order to inspect the method and properties of the object, you can print its prototype: For example, creating a new Geolocator object in REPL:

var locator = new geolocation.Geolocator();
//print the prototype
locator.__proto__

And the output will be:

Alt Geolocator prototype contents

(Note that property values are fetched on the fly, and hence have undefined values when printing the prototype)

Classes and fields naming

We use the same convention used for WinRT javascript applications:

  • Class/Enum names have the first letter in upper-case

  • Class/Enum fields, that is properties, methods, and events, both member and static have the first letter in lower-case, the rest of the name is according to MSDN.

  • Enums are just javascript objects with keys corresponding to the enum fields and values to the enum fields numeric values.

Properties

Using Properties of an object is just straight-forward javascript, for example:

locator.reportInterval = 2000;
console.info(locator.reportInterval);

Synchronous methods

Again, straight-forward javascript, just make the call with the appropriate arguments. If there are several WinRT overloads for the method, make the call with the right set of arguments and the correct overload of the method will be called:

var xml = require('windows.data.xml.dom');
var xmlDoc = new xml.XmlDocument();
toastXml.loadXml('<node>some text here</node>');

Asynchronous methods

Each async method accepts the same variables as are listed in the MSDN, with the addition of a completion callback as the last argument.

This callback will be called when the function has finished, and will receive an error as the first argument, and the result as the second argument:

locator.getGeopositionAsync( function(err, res) {
  // result is of type geoposition
  if (err) {
    console.error(err);
    return;
  }

  console.info('(',res.coordinate.longitude, res.coordinate.latitude, ')');
});

Events

Registering to events is done using the class' on method (which is equivalent to addListener), which receives the event name (case insensitive) and the event handler function. For example:

var handler = function handler(sender, eventArgs) {
  console.info('status is:', eventArgs.status); 
};
locator.on('statusChanged', handler);

Unregistering from an event is done the same way, using the class's off or removeListener methods. Just make sure to store the event handler in order to be able to use it.

// using same event handler as in the example above
locator.off('statusChanged', handler);

Separation into namespaces and cross namespace usage

Each NodeRT module represents a single namespace.

For instance, windows.storage will have a NodeRT module, and windows.storage.streams will have another NodeRT module.

The reason for this separation is strictly due to performance considerations.

(We didn't want to have a huge NodeRT module that will cause the memory of node.js to blow up while most of the namespaces probably won't be used in each script).

This architecture means that in case you are using a NodeRT module which contains a function, property, or event which returns an object from another namespace, then you will need to require that namespace before calling that function/property/event.

For example:

var capture = require('windows.media.capture');
// we also require this module in order to be able to access device controller properties
var devices = require('windows.media.devices');

var capture = new capture.MediaCapture();
capture.initializeAsync(function (err, res) {
  if (err) {
    return console.info(err);
  }

  // get the device controller, its type (VideoDeviceController) is defined in the 
  // windows.media.devices namespace -  so, we had to require that namespace as well
  var deviceController = capture.videoDeviceController;

  // we can now use the VideoDeviceController regularly
  deviceController.brightness.trySetValue(-1);
});

Class inheritance and objects casting

Since some WinRT classes inherit from other classes, you might need to cast an object of a certain type to another type.

In order to do so, each NodeRT object has a static method named ca

Core symbols most depended-on inside this repo

Shape

Method 208
Function 93
Class 31
Enum 2

Languages

C++59%
C#34%
TypeScript7%

Modules by API surface

src/NodeRTLib/ProjectFiles/CollectionsWrap.h92 symbols
src/NodeRTLib/Reflector.cs36 symbols
src/NodeRTLib/ProjectFiles/NodeRtUtils.cpp34 symbols
src/NodeRTLib/ProjectFiles/node-async.h25 symbols
src/NodeRTLib/Converter.cs17 symbols
src/NodeRTLib/NodeRTProjectGenerator.cs16 symbols
src/NodeRTUI/MainForm.cs15 symbols
src/NodeRTLib/ProjectFiles/CollectionsConverter.h12 symbols
src/NodeRTCmd/Program.cs9 symbols
src/NodeRTLib/ExternalTypesHelper.cs7 symbols
src/NodeRTLib/ProjectFiles/OpaqueWrapper.h5 symbols
src/NodeRTLib/NodeRTProjectBuildUtils.cs5 symbols

For agents

$ claude mcp add NodeRT \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact

Ask about this repo answers extend the page