pub repository
mirror.p-hatan.irnode_interop
0.1.0-beta.6Interop library for writing NodeJS apps and modules in Dart.
Readme
NodeJS interop library Build Status Pub Gitter
Write applications in Dart, run in NodeJS. This is an early preview, alpha open-source project.
What is this?
node_interop provides JavaScript bindings and enables running Dart
applications in NodeJS.
Some Node modules are also exposed as idiomatic Dart libraries. For instance, in addition to Node's http module bindings this package provides a HTTP client implementation based on Dart's http package.
Here is an example app using this client:
import 'package:node_interop/http.dart';
void main() async {
var http = new NodeClient();
var response = await http.get('https://example.com');
print("Response code: ${response.statusCode}.");
http.close(); // close any open IO connections.
}
There is not much Node or JS specific about this app, it looks very much like a
regular Dart app. If you are familiar with Dart node_interop allows you to
stay in comfortable environment of all the tools provided in Dart SDK and IDEs.
Examples
Checkout example/ folder of this repository for some example apps using
different APIs.
Status
This is a early preview, alpha version which is far from being feature complete. Breaking changes are likely to occur.
Make sure to checkout CHANGELOG.md after every release, all notable changes and upgrade instructions will be described there.
If you found a bug, please don't hesitate to create an issue in the issue tracker.
Usage
How to create a simple Node app written in Dart:
- Create a new Dart project. Stagehand is a great way to scaffold all the boilerplate:
$ pub global activate stagehand
$ mkdir my_node_app
$ cd my_node_app
$ stagehand package-simple # if you have .pub-cache in your PATH, or
$ pub global run stagehand package-simple # if you don't have it in your PATH
- Add dependency and transformers to the generated
pubspec.yaml
dependencies:
node_interop: '>=0.1.0 <0.2.0'
transformers:
- $dart2js
- node_interop # <- must go after dart2js!
- Create
node/main.dartand write some code:
import 'package:node_interop/fs.dart';
void main() async {
var fs = new NodeFileSystem(); // access Node `fs` module
print(fs.currentDirectory); // prints the path from `process.cwd()`
var files = await fs.currentDirectory.list().toList();
print(files); // lists current directory contents
}
- Compile.
Tell Pub to build
node/folder:
$ pub build node/
- Run the app. Compiled app is located in
build/node/main.dart.js:
$ # assuming you have NodeJS installed:
$ node build/bin/main.dart.js
$ # If everything worked well you should see your current
$ # directory contents printed out.
Libraries
node_interop consists of a set of libraries.
Node Interop
import 'package:node_interop/node_interop.dart'.
Main library which provides NodeJS API bindings as-is. Not all of the APIs are implemented at this point, in fact, it's a small percentage. If you didn't find a class or method you were looking for, please create an issue or submit a pull request.
You have to explicitly require a Node module which declares an API you want
to use. Refer to dartdoc for more details. Here is an example if using
bindings directly:
import 'package:node_interop/node_interop.dart';
void main() {
// Require specific modules:
FS nodeFS = require('fs');
OS nodeOS = require('os');
// Using globals:
console.log('message');
}
Note that bindings provide direct access to JavaScript objects and functions and
you need to make sure objects are properly converted when passed
between Dart and JS parts of the app (except primitives), as well as Dart
functions are wrapped with allowInterop when passed to JS.
node object
Special node object provides convenience methods to access Node's runtime and
platform information as well as some global functionality:
import 'package:node_interop/node_interop.dart';
void main() {
// Get platform details like environment variables, OS or current script:
Map<String, String> env = node.platform.environment;
print(env);
print(node.platform.script); // prints absolute path to the executed script.
// Require Node module:
FS fs = node.require('fs');
// Export something:
node.export('simplePi', 3.1415);
}
FS
import 'package:node_interop/fs.dart'.
Exposes Node's file system API as a set of Dart classes compatible with "dart:io" (specifically it implements interfaces from the file package).
Note that not all methods are currently implemented for File and Directory
classes.
HTTP
import 'package:node_interop/http.dart'.
Implements Dart-style HTTP client using http package interface.
There are several benefits to use this client over plain JS bindings:
- Encapsulates HTTP/HTTPS connections, no need to juggle with Node's "http" and "https" modules.
- Replaces callbacks with Dart Futures which enables async/await.
- Encapsulates JS interop logic like
allowInterop. - Provides very Dart-like experience.
Future releases of this library will also provide implementation
of ("dart:io" compatible) HttpServer powered by Node IO system.
Async
Contains Dart wrappers around Node's built-in Readable and
Writable streams. Following classes are provided:
ReadableStream<T> implements Stream<T>.WritableStream<T> implements StreamSink<T>.NodeIOSink extends WritableStream<List<int>> implements IOSink.
Most of the higher level abstractions in FS and HTTP libraries use these classes under the hood.
Features and bugs
Please file feature requests and bugs at the issue tracker
Changelog
Changelog
0.1.0-beta.6
- Breaking:
- renamed
ReadableStream.nativeStreamtoReadableStream.nativeInstance - renamed
WritableStream.nativeStreamtoWritableStream.nativeInstance
- renamed
- New:
- Added
jsonStringifyandjsonParsewhich bind to nativeJSON.stringifyandJSON.parserespectively.
- Added
0.1.0-beta.5
- Fixed:
HttpResponse.close()failed when trying to finalize headers.
0.1.0-beta.4
- Made
Promise<T>a generic type. Also added definition ofThenable. onRejectedinPromise.thenis now optional.- Added explicit type to
nodevariable.
0.1.0-beta.3
- More updates to bindings.
- Added new
async.dartlibrary with basic implementations ofReadableStream<T>,WritableStream<T>andNodeIOSink. - Added implementations of server side
HttpRequestandHttpResponsetohttp.dart, as well some other objects likeHttpHeaders. - Added
dartifyError(JsError error)to the main library which converts from JSErrorinstances in to Dart's equivalent. - Implemented more methods in
File:openRead,openWrite,readAsBytes. - Deprecated
createJSFileintest.dartlibrary. UsecreateFileinstead.
0.1.0-beta.2
jsObjectToMapdeprecated. There is new helper functiondartify. See documentation for more details.- New
jsifyhelper function. - Clarified type of HTTP server
requestListener. - New
createJSFiletest util intest.dart.
0.1.0-beta.1
- Breaking changes:
NodePlatformis no longer exported fromnode_interop.dartlibrary.- Library-level
exportsgetter was removed. Nowexportsis a direct reference to native JS object. Replace any calls toexports.setProperty(name, value)with new API:node.export(name, value). - "http" module:
Agent,Server,AgentOptionsrenamed toHttpAgent,HttpServer,HttpAgentOptionsrespectively. - "http" module:
createAgentrenamed tocreateHttpAgent. node_interop/bindings.dartwas removed. All bindings are available through mainnode_interop/node_interop.dartimport.
- New:
- Many updates to documentation.
- Main package's library now exposes all (implemented) Node API bindings.
- New
nodelibrary object with centralized access to the Node platform and runtime information, as well as module globals likerequireandexports. - Exposed parts of "https", "tls", "dns" and "net" module bindings.
- Added HTTPS support to
NodeClientfromnode_interop/http.dart. - Updated examples.
- Gitter channel is now up: https://gitter.im/pulyaevskiy/node-interop.
0.0.7
- Added
node_interop/test.dartlibrary withinstallNodeModules()helper function. See dartdoc for more details.
0.0.6
jsObjectToMap: added null-check.- Added basic HTTP client implementation for Node, based on an interface
from 'http' package. Use with
import package:node_interop/http.dart.
0.0.5
- Streamlined bindings layer and exposed as it's own library. Use
import package:node_interop/bindings.dartto get access. - Added bindings for 'http' module (work in progress).
0.0.4
- Upgraded to
testpackage with support for running tests in Node - Implemented
NodeFileSystem.file()andFile.writeAsStringSync().
0.0.3
- Added bindings for
Console.
0.0.2
- Switched to use official
node_preamblepackage
0.0.1
- Initial version
Installing
export PUB_HOSTED_URL=https://pub.mirror.p-hatan.ir
flutter pub add node_interoppubspec.yaml
name: node_interop
description: Interop library for writing NodeJS apps and modules in Dart.
version: 0.1.0-beta.6
homepage: https://github.com/pulyaevskiy/node-interop
author: Anatoly Pulyaevskiy <anatoly.pulyaevskiy@gmail.com>
environment:
sdk: '>=1.20.1 <2.0.0'
dependencies:
js: ^0.6.1
file: ^2.3.2
platform: ^2.0.0
glob: ^1.1.3
barback: ^0.15.2
node_preamble: ^1.4.0
http: ^0.11.3
dev_dependencies:
test: ^0.12.24
transformers:
- $dart2js
- node_interop
Version history
| Version | Published | Archive | SHA-256 |
|---|---|---|---|
| 0.0.1 | 2017-06-18T02:33:05.244951Z | Download | 336de1a74afa547eaa5816e1b3b31f9b560c1b41951ba2196ad2d7969e618ad1 |
| 0.0.1+1 | 2017-06-18T02:41:47.575446Z | Download | e3ef94857cb6e2b18e2f37424d636ea990ba908fe6ce974cad95d61d687f1b63 |
| 0.0.1+2 | 2017-06-18T04:11:11.779047Z | Download | b8f8eaf37ca6aecfa778b4bb612cb231d1815ec3a96f7e08d7ad3e5c0d3101ec |
| 0.0.2 | 2017-06-30T19:22:38.705721Z | Download | f145eda9653cb0a4095ec8340f30c1c4b08ae57dd230eaf58e2911dbbb743cad |
| 0.0.3 | 2017-07-02T10:19:06.570225Z | Download | aaeedf537f9810dc69c352d499ceabfd84c6e27ffeb93fd1ff4a4c533e8fe973 |
| 0.0.4 | 2017-07-08T13:44:00.731377Z | Download | 8ae1bb66d477729afb07c08a6098ced3d7f7198c47df1c9ec3c51b1d4d79b428 |
| 0.0.5 | 2017-10-01T04:00:03.454972Z | Download | 20b745bce32738f003035df1346cf25dc55cc8bf780a03f771e9ef354623981a |
| 0.0.6 | 2017-10-03T06:55:49.794947Z | Download | 139767856d483cefc251255442f3843e3e2a7921d2a8837490b9e4e1049b8173 |
| 0.0.7 | 2017-10-04T06:11:56.392132Z | Download | 4791b2812336a427e054b9ec75746d550981ac552561fda45ae1f00b71d8ec1b |
| 0.1.0-beta.1 | 2017-10-09T21:55:01.766303Z | Download | 6475ae8c33f940756974a0a1cade3d6ad59ce54ce3451a1c7e5ab5318d7d6969 |
| 0.1.0-beta.2 | 2017-10-11T04:35:39.636934Z | Download | 97ea197c194d22469736678cc01ed98c672d3132a75b30b5b38fe848ff93afea |
| 0.1.0-beta.3 | 2017-10-15T00:23:23.332684Z | Download | f3d464bd26d2b3f051b5e8ffca0f9ea6fb37e3f8da0b182d5d1709cbc0e38e0d |
| 0.1.0-beta.4 | 2017-10-17T02:48:51.904318Z | Download | a1bc84026ea6094862e2244e049eeddfecfac6f610925b43b505cdcba4831f22 |
| 0.1.0-beta.5 | 2017-10-21T04:37:23.782539Z | Download | a97a2e4712b6f1dec3ca14309cabd3ce0161b57336a5d8d4cd450ca5e25214de |
| 0.1.0-beta.6 | 2017-10-22T04:14:50.120800Z | Download | aab23d3b8572a900cf9d301287582cb5e1f281da8a120eb99bae53070102c13f |
| 0.1.0-beta.7 | 2017-12-19T17:44:39.520109Z | Download | 1223c15e4b80200b8f9f6d47f7e8adfbe47d4d914db38685bb0acf0d4b4e25dc |
| 0.1.0-beta.8 | 2018-01-22T19:41:04.275553Z | Download | 939c15fc8fd26ffb91be9018ae8d606494aca7351609e7d097a4381fd8f3881f |
| 0.1.0-beta.8+1 | 2018-01-22T19:50:19.739446Z | Download | aeb083e7982b967a899ef86d12ebb68444b92854809b32e692d874ac45488555 |
| 0.1.0-beta.9 | 2018-01-23T01:03:12.333448Z | Download | 77ccd91e12e79a93ef1f3e742bc1d95eeaeb80de29efe9db245f7f811d9bb822 |
| 1.0.0-dev.1.0 | 2018-02-09T20:23:55.364972Z | Download | aacdfe3247480fbef7b159b858145acb1ad454d8764a438db94fde1e956450a3 |
| 1.0.0-dev.1.0+1 | 2018-02-13T22:26:37.236508Z | Download | 0abefa798ababb5fe924dc080cadd6cd74d525db2b26b179bc5a1e88b7b436d3 |
| 1.0.0-dev.2.0 | 2018-02-14T02:43:31.560601Z | Download | 4889b915466d507dbd876bac57fdde919441323e93a0828ff9a89a46bba26071 |
| 1.0.0-dev.3.0 | 2018-02-20T18:12:36.925048Z | Download | 430bf55088510c3d32280475c4ae14e797ea651bf1b4c524d17314b00adecf66 |
| 1.0.0-dev.4.0 | 2018-03-01T20:18:26.184572Z | Download | 3cc53062b2d5aa62d8aba090cbe626eee27c45d77f99957af3ff5c4355748b41 |
| 1.0.0-dev.5.0 | 2018-03-07T22:11:59.712424Z | Download | 5c405964e6aedf433df7a2d518dbdeaec4276359770d06fed12987b1e38dd2df |
| 1.0.0-dev.6.0 | 2018-03-08T18:50:21.105677Z | Download | 2f2b596967863eb723eb0887d7bdb6fcfe2a7afa18cc637873c61f49048f3796 |
| 1.0.0-dev.7.0 | 2018-06-06T19:30:37.382264Z | Download | fb3cfba49f71e1ba6d4e46eb7cdac0fa19098a98a5cb053b5aad9382f768d979 |
| 1.0.0-dev.8.0 | 2018-06-22T17:53:42.821221Z | Download | b85e4932825cc739a2d6f3673a774a4fed8dbd0b0bef103f2e33f6ea6a17b651 |
| 1.0.0-dev.9.0 | 2018-07-24T05:11:48.462189Z | Download | f630c5c3da805ed41bd3cb9e072b83ba75526025555338e4197b3702c5902c8c |
| 1.0.0-dev.10.0 | 2018-08-09T21:11:20.384429Z | Download | ac8c583aab4be3ce95624f6efc85a0f522f0259356c53191b772aaf076a8dd79 |
| 1.0.0-dev.11.0 | 2018-10-05T01:09:00.483879Z | Download | a56a9c331e72adc9df0909e39f93b2c8230d26160b23b9c733a78cbaf0f3cac9 |
| 1.0.0-dev.12.0 | 2018-10-23T19:32:52.497454Z | Download | 936a81132d71168b4148fa1985be205f922c375ef61167f75abe564afc4f43fb |
| 1.0.0-dev.13.0 | 2018-11-01T18:05:14.575185Z | Download | 4fa6e4646380caaea9f4f7c3053f4a757c62840a78c723708b9e01b1cca6c011 |
| 1.0.0 | 2018-11-20T20:33:32.317900Z | Download | b952e4801f162a9307dbd57b1ae32e214cecce96644c7d98b8d7a4b0a7270643 |
| 1.0.1 | 2019-05-09T04:04:06.549894Z | Download | 15e61de891f168207cb80ed0f4e23fdcae62acdef217cd4ab97e93907b16f626 |
| 1.0.2 | 2019-05-17T14:42:42.817322Z | Download | 0e6144bea19eea6ef0f43091806d28bbb4246face9d0a996bc5aee23860f46d6 |
| 1.0.3 | 2019-05-22T03:24:11.669434Z | Download | f4ba035f48017416979b62991fc9f4217260be5e7445d5c386bc42024783808a |
| 1.1.0 | 2020-05-12T04:45:35.339441Z | Download | 9db54bead497ae71a467908267f817715fb5ea258c8ce1e91cb7799614026366 |
| 1.1.1 | 2020-05-13T04:42:26.056752Z | Download | 3afce5317f1071a97df98f6744be23cf67153bc91c91825fbefa3d553bdcb2e6 |
| 1.2.0 | 2020-10-30T04:39:38.681577Z | Download | 92499677ff249f005beacedffc1b520f7e9e3df7f8053f34c7b39d265a9788d8 |
| 1.2.1 | 2020-11-21T17:17:40.886446Z | Download | 910d75f8589630cd197b8b7ec70e648bc380950b489b07bda1da0c5b20c4b8c1 |
| 2.0.0-nullsafety.0 | 2021-04-12T21:30:16.447810Z | Download | fca6028f77864c843f23e9a27f3f52e670e688720bef0b597311c820e855d9f3 |
| 2.0.0 | 2021-04-12T22:27:39.013700Z | Download | 3b88c3931da3ea1c0fface1b4574b3a5680f689123a3350f2ec57a9a8b2224dd |
| 2.0.1 | 2021-04-20T23:20:36.773969Z | Download | b4f93987faa4b63ac83477e4087e1923a69e0250e6437b67295bd2e1e8e5816c |
| 2.0.2 | 2021-05-24T22:09:50.962364Z | Download | 0fb79f5357160e0cf28843dfcd2b34824fde9276cd810fd74862f93523337623 |
| 2.1.0 | 2021-09-22T20:09:01.294896Z | Download | 3af2420c728173806f4378cf89c53ba9f27f7f67792b898561bff9d390deb98e |
| 2.2.0 | 2025-03-28T00:16:39.447905Z | Download | 4848ac408c0cdd0f70136b755df816a8e4c96c244e5377a3fb3b8f8950666150 |
Dependencies
barback "^0.15.2"
file "^2.3.2"
glob "^1.1.3"
http "^0.11.3"
js "^0.6.1"
node_preamble "^1.4.0"
platform "^2.0.0"