p-hatan/ mirror

pub repository

mirror.p-hatan.ir

node_interop

0.1.0-beta.2

Interop library for writing NodeJS apps and modules in Dart.

Download archive

Readme

NodeJS interop library

Build Status Pub Gitter

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. This is one of the main goals for node_interop.

Examples

Checkout example/ folder of this repository for some example apps using different APIs.

Status

This package is under active development which means things are likely to change.

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:

  1. 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
  1. 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!
  1. Create node/main.dart and 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
}
  1. Compile. Tell Pub to build node/ folder:
$ pub build node/
  1. 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.

Features and bugs

Please file feature requests and bugs at the issue tracker

Changelog

Changelog

0.1.0-beta.2

  • jsObjectToMap deprecated. There is new helper function dartify. See documentation for more details.
  • New jsify helper function.
  • Clarified type of HTTP server requestListener.
  • New createJSFile test util in test.dart.

0.1.0-beta.1

  • Breaking changes:
    • NodePlatform is no longer exported from node_interop.dart library.
    • Library-level exports getter was removed. Now exports is a direct reference to native JS object. Replace any calls to exports.setProperty(name, value) with new API: node.export(name, value).
    • "http" module: Agent, Server, AgentOptions renamed to HttpAgent, HttpServer, HttpAgentOptions respectively.
    • "http" module: createAgent renamed to createHttpAgent.
    • node_interop/bindings.dart was removed. All bindings are available through main node_interop/node_interop.dart import.
  • New:
    • Many updates to documentation.
    • Main package's library now exposes all (implemented) Node API bindings.
    • New node library object with centralized access to the Node platform and runtime information, as well as module globals like require and exports.
    • Exposed parts of "https", "tls", "dns" and "net" module bindings.
    • Added HTTPS support to NodeClient from node_interop/http.dart.
    • Updated examples.
    • Gitter channel is now up: https://gitter.im/pulyaevskiy/node-interop.

0.0.7

  • Added node_interop/test.dart library with installNodeModules() 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.dart to get access.
  • Added bindings for 'http' module (work in progress).

0.0.4

  • Upgraded to test package with support for running tests in Node
  • Implemented NodeFileSystem.file() and File.writeAsStringSync().

0.0.3

  • Added bindings for Console.

0.0.2

  • Switched to use official node_preamble package

0.0.1

  • Initial version

Installing

export PUB_HOSTED_URL=https://pub.mirror.p-hatan.ir
flutter pub add node_interop
pubspec.yaml
name: node_interop
description: Interop library for writing NodeJS apps and modules in Dart.
version: 0.1.0-beta.2
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

VersionPublishedArchiveSHA-256
0.0.12017-06-18T02:33:05.244951ZDownload336de1a74afa547eaa5816e1b3b31f9b560c1b41951ba2196ad2d7969e618ad1
0.0.1+12017-06-18T02:41:47.575446ZDownloade3ef94857cb6e2b18e2f37424d636ea990ba908fe6ce974cad95d61d687f1b63
0.0.1+22017-06-18T04:11:11.779047ZDownloadb8f8eaf37ca6aecfa778b4bb612cb231d1815ec3a96f7e08d7ad3e5c0d3101ec
0.0.22017-06-30T19:22:38.705721ZDownloadf145eda9653cb0a4095ec8340f30c1c4b08ae57dd230eaf58e2911dbbb743cad
0.0.32017-07-02T10:19:06.570225ZDownloadaaeedf537f9810dc69c352d499ceabfd84c6e27ffeb93fd1ff4a4c533e8fe973
0.0.42017-07-08T13:44:00.731377ZDownload8ae1bb66d477729afb07c08a6098ced3d7f7198c47df1c9ec3c51b1d4d79b428
0.0.52017-10-01T04:00:03.454972ZDownload20b745bce32738f003035df1346cf25dc55cc8bf780a03f771e9ef354623981a
0.0.62017-10-03T06:55:49.794947ZDownload139767856d483cefc251255442f3843e3e2a7921d2a8837490b9e4e1049b8173
0.0.72017-10-04T06:11:56.392132ZDownload4791b2812336a427e054b9ec75746d550981ac552561fda45ae1f00b71d8ec1b
0.1.0-beta.12017-10-09T21:55:01.766303ZDownload6475ae8c33f940756974a0a1cade3d6ad59ce54ce3451a1c7e5ab5318d7d6969
0.1.0-beta.22017-10-11T04:35:39.636934ZDownload97ea197c194d22469736678cc01ed98c672d3132a75b30b5b38fe848ff93afea
0.1.0-beta.32017-10-15T00:23:23.332684ZDownloadf3d464bd26d2b3f051b5e8ffca0f9ea6fb37e3f8da0b182d5d1709cbc0e38e0d
0.1.0-beta.42017-10-17T02:48:51.904318ZDownloada1bc84026ea6094862e2244e049eeddfecfac6f610925b43b505cdcba4831f22
0.1.0-beta.52017-10-21T04:37:23.782539ZDownloada97a2e4712b6f1dec3ca14309cabd3ce0161b57336a5d8d4cd450ca5e25214de
0.1.0-beta.62017-10-22T04:14:50.120800ZDownloadaab23d3b8572a900cf9d301287582cb5e1f281da8a120eb99bae53070102c13f
0.1.0-beta.72017-12-19T17:44:39.520109ZDownload1223c15e4b80200b8f9f6d47f7e8adfbe47d4d914db38685bb0acf0d4b4e25dc
0.1.0-beta.82018-01-22T19:41:04.275553ZDownload939c15fc8fd26ffb91be9018ae8d606494aca7351609e7d097a4381fd8f3881f
0.1.0-beta.8+12018-01-22T19:50:19.739446ZDownloadaeb083e7982b967a899ef86d12ebb68444b92854809b32e692d874ac45488555
0.1.0-beta.92018-01-23T01:03:12.333448ZDownload77ccd91e12e79a93ef1f3e742bc1d95eeaeb80de29efe9db245f7f811d9bb822
1.0.0-dev.1.02018-02-09T20:23:55.364972ZDownloadaacdfe3247480fbef7b159b858145acb1ad454d8764a438db94fde1e956450a3
1.0.0-dev.1.0+12018-02-13T22:26:37.236508ZDownload0abefa798ababb5fe924dc080cadd6cd74d525db2b26b179bc5a1e88b7b436d3
1.0.0-dev.2.02018-02-14T02:43:31.560601ZDownload4889b915466d507dbd876bac57fdde919441323e93a0828ff9a89a46bba26071
1.0.0-dev.3.02018-02-20T18:12:36.925048ZDownload430bf55088510c3d32280475c4ae14e797ea651bf1b4c524d17314b00adecf66
1.0.0-dev.4.02018-03-01T20:18:26.184572ZDownload3cc53062b2d5aa62d8aba090cbe626eee27c45d77f99957af3ff5c4355748b41
1.0.0-dev.5.02018-03-07T22:11:59.712424ZDownload5c405964e6aedf433df7a2d518dbdeaec4276359770d06fed12987b1e38dd2df
1.0.0-dev.6.02018-03-08T18:50:21.105677ZDownload2f2b596967863eb723eb0887d7bdb6fcfe2a7afa18cc637873c61f49048f3796
1.0.0-dev.7.02018-06-06T19:30:37.382264ZDownloadfb3cfba49f71e1ba6d4e46eb7cdac0fa19098a98a5cb053b5aad9382f768d979
1.0.0-dev.8.02018-06-22T17:53:42.821221ZDownloadb85e4932825cc739a2d6f3673a774a4fed8dbd0b0bef103f2e33f6ea6a17b651
1.0.0-dev.9.02018-07-24T05:11:48.462189ZDownloadf630c5c3da805ed41bd3cb9e072b83ba75526025555338e4197b3702c5902c8c
1.0.0-dev.10.02018-08-09T21:11:20.384429ZDownloadac8c583aab4be3ce95624f6efc85a0f522f0259356c53191b772aaf076a8dd79
1.0.0-dev.11.02018-10-05T01:09:00.483879ZDownloada56a9c331e72adc9df0909e39f93b2c8230d26160b23b9c733a78cbaf0f3cac9
1.0.0-dev.12.02018-10-23T19:32:52.497454ZDownload936a81132d71168b4148fa1985be205f922c375ef61167f75abe564afc4f43fb
1.0.0-dev.13.02018-11-01T18:05:14.575185ZDownload4fa6e4646380caaea9f4f7c3053f4a757c62840a78c723708b9e01b1cca6c011
1.0.02018-11-20T20:33:32.317900ZDownloadb952e4801f162a9307dbd57b1ae32e214cecce96644c7d98b8d7a4b0a7270643
1.0.12019-05-09T04:04:06.549894ZDownload15e61de891f168207cb80ed0f4e23fdcae62acdef217cd4ab97e93907b16f626
1.0.22019-05-17T14:42:42.817322ZDownload0e6144bea19eea6ef0f43091806d28bbb4246face9d0a996bc5aee23860f46d6
1.0.32019-05-22T03:24:11.669434ZDownloadf4ba035f48017416979b62991fc9f4217260be5e7445d5c386bc42024783808a
1.1.02020-05-12T04:45:35.339441ZDownload9db54bead497ae71a467908267f817715fb5ea258c8ce1e91cb7799614026366
1.1.12020-05-13T04:42:26.056752ZDownload3afce5317f1071a97df98f6744be23cf67153bc91c91825fbefa3d553bdcb2e6
1.2.02020-10-30T04:39:38.681577ZDownload92499677ff249f005beacedffc1b520f7e9e3df7f8053f34c7b39d265a9788d8
1.2.12020-11-21T17:17:40.886446ZDownload910d75f8589630cd197b8b7ec70e648bc380950b489b07bda1da0c5b20c4b8c1
2.0.0-nullsafety.02021-04-12T21:30:16.447810ZDownloadfca6028f77864c843f23e9a27f3f52e670e688720bef0b597311c820e855d9f3
2.0.02021-04-12T22:27:39.013700ZDownload3b88c3931da3ea1c0fface1b4574b3a5680f689123a3350f2ec57a9a8b2224dd
2.0.12021-04-20T23:20:36.773969ZDownloadb4f93987faa4b63ac83477e4087e1923a69e0250e6437b67295bd2e1e8e5816c
2.0.22021-05-24T22:09:50.962364ZDownload0fb79f5357160e0cf28843dfcd2b34824fde9276cd810fd74862f93523337623
2.1.02021-09-22T20:09:01.294896ZDownload3af2420c728173806f4378cf89c53ba9f27f7f67792b898561bff9d390deb98e
2.2.02025-03-28T00:16:39.447905ZDownload4848ac408c0cdd0f70136b755df816a8e4c96c244e5377a3fb3b8f8950666150

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"