node_interop 0.0.7

  • README.md
  • CHANGELOG.md
  • Installing
  • Versions
  • 88

NodeJS interop library

Build Status

Provides interface bindings for NodeJS APIs and allows writing Dart applications and libraries which can be compiled and used in NodeJS.

Unlock the full power of Dart static analysis, type system and built-in features like Futures, Streams and async/await for building NodeJS apps.

Philosophy

Provided API interface is Dart-centric which means Node functionality is exposed in idiomatic Dart as much as possible. For instance:

  • File system API follows dart:io interface (thanks to the awesome file package)
  • Platform API also exposed via interface provided by platform package.
  • Dart's Futures and Streams are used whenever possible.

The intention is to provide familiar and productive environment for Dart developers when working with NodeJS.

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.0.1

  transformers:
    - $dart2js
    - node_interop
  1. Create bin/main.dart and write some code:
  import 'package:node_interop/node_interop.dart';
  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. Pub by default assumes you are building for web so it looks for web folder. We need to explicitely tell it to look in bin/ where our main.dart is:
  $ pub build bin
  1. Run the app. Compiled app is located in build/bin/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.dart - globals (require, exports), platform
  • fs.dart - File system abstraction
  • http.dart - HTTP client powered by Node IO and 'http' package.
  • bindings.dart - Bindings only.

Pub transformer

This library provides Pub transformer which by default processes all .dart files in the bin/ folder. The only thing it does is it prepends Node preamble to JS generated by dart2js transformer.

NodeJS modules and exports

It is possible to create a module which exposes some functionality via NodeJS exports.

Here is simple module example exporting a bang function:

// file:bin/bang.dart
import 'package:node_interop/node_interop.dart';

/// A module which exports a bang function.
void main() {
  exports.setProperty('bang', bang);
}

String bang(String value) {
  return value.toUpperCase() + '!';
}

After compiled with pub build it can be required from any JS file:

const bang = require('path/to/build/bin/bang.dart.js');
console.log(bang.bang('Hi')); 
// prints out "HI!"

For such a small function the resulting bang.dart.js would be quite large (~1k lines of code). However creating Node modules like this can be considered an edge use case when you would want to mix in some functionality from Dart into an existing Node app.

Bindings

In case you only need bindings to Node APIs:

@JS()
library myapp;

import 'package:node_interop/bindings.dart';
import 'package:js/js.dart';

void main() {
  // Require specific modules:
  FS nodeFS = require('fs');
  OS nodeOS = require('os');
  // Use globals:
  console.log('message');
}

Features and bugs

Please file feature requests and bugs at the issue tracker

Changelog

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

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  node_interop: "^0.0.7"

2. Install it

You can install packages from the command line:

with pub:


$ pub get

Alternatively, your editor might support pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:node_interop/node_interop.dart';
        
Version Uploaded Documentation Archive
0.0.7 Oct 4, 2017 Go to the documentation of node_interop 0.0.7 Download node_interop 0.0.7 archive
0.0.6 Oct 3, 2017 Go to the documentation of node_interop 0.0.6 Download node_interop 0.0.6 archive
0.0.5 Oct 1, 2017 Go to the documentation of node_interop 0.0.5 Download node_interop 0.0.5 archive
0.0.4 Jul 8, 2017 Go to the documentation of node_interop 0.0.4 Download node_interop 0.0.4 archive
0.0.3 Jul 2, 2017 Go to the documentation of node_interop 0.0.3 Download node_interop 0.0.3 archive
0.0.2 Jun 30, 2017 Go to the documentation of node_interop 0.0.2 Download node_interop 0.0.2 archive
0.0.1+2 Jun 18, 2017 Go to the documentation of node_interop 0.0.1+2 Download node_interop 0.0.1+2 archive
0.0.1+1 Jun 18, 2017 Go to the documentation of node_interop 0.0.1+1 Download node_interop 0.0.1+1 archive
0.0.1 Jun 18, 2017 failed Download node_interop 0.0.1 archive
1.0.0-dev.6.0 Mar 8, 2018 Go to the documentation of node_interop 1.0.0-dev.6.0 Download node_interop 1.0.0-dev.6.0 archive

All 26 versions...

Analysis

We analyzed this package on Apr 9, 2018, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.0.0-dev.46.0
  • pana: 0.10.6

Scores

Popularity:
Describes how popular the package is relative to other packages. [more]
78 / 100
Health:
Code health derived from static analysis. [more]
96 / 100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
99 / 100
Overall score:
Weighted score of the above. [more]
88
Learn more about scoring.

Platforms

Detected platforms: web, other

Primary library: package:node_interop/node_interop.dart with components: js, io.

Suggestions

  • Fix analysis and formatting issues.

    Analysis or formatting checks reported 45 hints.

    Strong-mode analysis of lib/src/bindings/globals.dart gave the following hint:

    line: 6 col: 8
    Unused import: 'dart:js'.

    Strong-mode analysis of lib/src/bindings/http.dart gave the following hint:

    line: 38 col: 16
    The class '_HTTPAgentAccess' isn't used.

    Similar analysis of the following files failed:

    • lib/src/fs/file.dart (hint)
    • lib/test.dart (hint)
    • lib/transformer.dart (hint)
  • Package is pre-v1 release.

    While there is nothing inherently wrong with versions of 0.*.*, it usually means that the author is still experimenting with the general direction API.

  • Maintain an example.

    None of the files in your example/ directory matches a known example patterns. Common file name patterns include: main.dart, example.dart or you could also use node_interop.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=1.20.1 <2.0.0
barback ^0.15.2 0.15.2+15
file ^2.3.2 2.3.7 4.0.0
glob ^1.1.3 1.1.5
http ^0.11.3 0.11.3+16
js ^0.6.1 0.6.1
node_preamble ^1.4.0 1.4.0
platform ^2.0.0 2.1.2
Transitive dependencies
async 2.0.6
charcode 1.1.1
collection 1.14.9
http_parser 3.1.1
intl 0.15.4
meta 1.1.2
path 1.5.1
pool 1.3.4
source_span 1.4.0
stack_trace 1.9.2
string_scanner 1.0.2
typed_data 1.1.5
Dev dependencies
test ^0.12.24