edge-ts
v1.1.1
Published
Strongly typed .NET interop with Node.js
Downloads
4
Readme
edge-ts
Create strongly typed .NET bindings from Node.js.
In-process interop powered by the awesome edge.js.
Usage
1. Get the package
npm install --save edge-ts
2. Import it
import { sync, async, proxy } from 'edge-ts';
Types are bundled with the published package and will be automatically imported.
3. Create your bindings
interface InputArgs {
life: string;
universe: boolean;
everything?: SomeOtherType;
}
const meaning = async<InputArgs, number>({
assemblyFile: 'DeepThought.dll',
typeName: 'FooBar.MyType',
methodName: 'MyMethod' // This must be Func<object, Task<object>>
});
4. Call them as though they were Typscript functions
meaning({life: 'test', universe: true, everything: someOtherValue})
.then(answer => console.log);
// some time passes
=> 42
API
Binding Targets
Bindings can be created to precompiled libraries (*.dll's) or source to be compiled at runtime by edge. Regardless of compile time, the targeted method must be of type Func<object, Task<object>>
.
Source
C# source can be written in-line (as a string) as lambda expressions:
const test = async<string, string>(`
async (input) => {
return ".NET Welcomes " + input.ToString();
}
`);
As full classes:
const test = async<string, string>(`
using System.Data;
using System.Threading.Tasks;
public class Startup
{
public async Task<object> Invoke(object input)
{
// ...
}
}
`);
Or referenced as an external file:
const test = async<string, string>({
source: 'Foo.cs',
typeName: 'MyType', // optional, defaults to 'StartUp'
methodName: 'MyMethod', // optional, defaults to 'Invoke'
references: ['MyOtherLib.dll'], // optional (any external assemblies required)
});
Precompiled
Precompiled targets take a similar form:
const test = async<string, string>({
assemblyFile: 'MyAssembly.dll',
typeName: 'MyType', // optional, defaults to 'StartUp'
methodName: 'MyMethod', // optional, defaults to 'Invoke'
references: ['MyOtherLib.dll'], // optional (any external assemblies required)
});
Or, if you're happy with the defaults, just the path to the assembly:
const test = async<string, string>('MyAssembly.dll');
async<I, O>(«binding target»)
Create an asynchronous function, bound to a CLR/.NET Core/Mono method.
The returned function accepts an input (of type I
) and returns a Promise (of type O
).
Alternatively a Node style callback may be passed as a second argument.
const myAsyncBinding = async<number, string>('FooBar.dll');
myAsyncBinding(123)
.then(doSomething);
.catch(e => console.log('Oh noes!!!'));
// or
myAsyncBinding(123, (err, result) => {
if (err) {
console.log('Oh noes!!!');
} else {
doSomething(result);
}
});
sync<I, O>(«binding target»)
Creates a synchronous function, bound to a CLR/.NET Core/Mono method.
The returned function takes a single input argument (or type I
) and returns an output of type O
. If the linked method does not return a synchronous result, an Error will be raised.
const mySyncBinding = sync<number, string>('FooBar.dll');
doSomthing(mySyncBinding(123));
proxy<I, O>(«func»)
Functions may also be passed to the CLR to allow it to call back into Node. These must be of a specific form expected by edge. proxy(..)
may be used to transform a function of type I => O
into this form.
For further information on marshaling data between the environments, refer to the edge.js guide