@davedoesdev/dnode

中文文档
3.2.0发布于 2023/11/16MITCommonJS近 30 天下载 3549

freestyle rpc

原始使用文档
文档目录13 节
  1. listen and connect
  2. streaming
  3. dnode in the browser
  4. var d = dnode(cons, opts={})
  5. d.connect(...)
  6. dnode.connect(...)
  7. d.listen(...)
  8. dnode.listen(...)
  9. d.on('remote', cb)
  10. d.on('local', cb)
  11. d.on('fail', cb)
  12. d.on('error', cb)
  13. d.on('end', cb)

dnode

dnode is an asynchronous rpc system for node.js that lets you call remote functions.

You can pass callbacks to remote functions, and the remote end can call the functions you passed in with callbacks of its own and so on. It's callbacks all the way down!

browser support

build status

dnode: freestyle rpc

example

listen and connect

server:

var dnode = require('dnode');
var server = dnode({
    transform : function (s, cb) {
        cb(s.replace(/[aeiou]{2,}/, 'oo').toUpperCase())
    }
});
server.listen(5004);

client:

var dnode = require('dnode');

var d = dnode.connect(5004);
d.on('remote', function (remote) {
    remote.transform('beep', function (s) {
        console.log('beep => ' + s);
        d.end();
    });
});

output:

$ node server.js &
[1] 27574
$ node client.js
beep => BOOP

streaming

The .connect() and .listen() calls in the previous example are just convenience methods for piping to and from readable/writable streams. Here's the previous example with the streams set up explicitly:

server:

var dnode = require('dnode');
var net = require('net');

var server = net.createServer(function (c) {
    var d = dnode({
        transform : function (s, cb) {
            cb(s.replace(/[aeiou]{2,}/, 'oo').toUpperCase())
        }
    });
    c.pipe(d).pipe(c);
});

server.listen(5004);

client:

var dnode = require('dnode');
var net = require('net');

var d = dnode();
d.on('remote', function (remote) {
    remote.transform('beep', function (s) {
        console.log('beep => ' + s);
        d.end();
    });
});

var c = net.connect(5004);
c.pipe(d).pipe(c);

output:

$ node server.js &
[1] 27586
$ node client.js 
beep => BOOP

dnode in the browser

Since dnode instances are just readable/writable streams, you can use them with any streaming transport, including in the browser!

This example uses the streaming interface provided by shoe, which is just a thin wrapper on top of sockjs that provides websockets with fallbacks.

First whip up a server:

var http = require('http');
var shoe = require('shoe');
var ecstatic = require('ecstatic')(__dirname + '/static');
var dnode = require('dnode');

var server = http.createServer(ecstatic);
server.listen(9999);

var sock = shoe(function (stream) {
    var d = dnode({
        transform : function (s, cb) {
            var res = s.replace(/[aeiou]{2,}/, 'oo').toUpperCase();
            cb(res);
        }
    });
    d.pipe(stream).pipe(d);
});
sock.install(server, '/dnode');

Then write some browser code:

var domready = require('domready');
var shoe = require('shoe');
var dnode = require('dnode');

domready(function () {
    var result = document.getElementById('result');
    var stream = shoe('/dnode');
    
    var d = dnode();
    d.on('remote', function (remote) {
        remote.transform('beep', function (s) {
            result.textContent = 'beep => ' + s;
        });
    });
    d.pipe(stream).pipe(d);
});

Install the dependencies for this example then compile the browser code with browserify:

$ npm install dnode shoe domready ecstatic
$ npm install -g browserify
$ browserify client.js -o static/bundle.js

Now drop a script tag into static/index.html:

<script src="/bundle.js"></script>
<div id="result"></div>

and navigate to http://localhost:9999. You should see beep => BOOP on the page!

Check out the complete shoe example.

methods

var dnode = require('dnode')

var d = dnode(cons, opts={})

Create a new readable/writable dnode stream object d. All the usual stream methods are at your disposal: pipe(), write(), end().

If cons is a function, it will be called new cons(remote, d) to create a new instance object. Otherwise its value will be used directly. When cons is called as a function, the remote ref will be an empty unpopulated object.

By default, dnode uses weakmaps to garbage collect unused callbacks automatically. This behavior prevents memory leaks in long-running connections.

You can turn weakmaps off by setting opts.weak = false.

d.connect(...)

This method is a shortcut for setting up a pipe between d and a new net.connect() stream.

The host, port, and callback arguments supplied will be inferred by their types.

If you pass a callback in as an argument, it will be added as a listener to the 'remote' event.

Returns the d object.

dnode.connect(...)

Shortcut to create a connection without a constructor.

d.listen(...)

This method is a shortcut for setting up a net.createServer() and piping network streams to and from new dnode streams.

The host, port, and callback parameters will be inferred from the types of the arguments.

Returns a net server object that will also emit 'local' and 'remote' events from the underlying dnode streams..

dnode.listen(...)

Shortcut to create a listener without a constructor.

events

d.on('remote', cb)

This event fires with cb(remote, d) when the remote side of the connection has constructed its instance.

d.on('local', cb)

This event fires right after the constructed instance has been created locally but before it gets sent to the remote end so you can modify the ref object.

This event fires with cb(ref, d) where ref is the local instance object.

d.on('fail', cb)

This event fires when the remote end causes errors in the protocol layer.

These are non-fatal and can probably be ignored but you could also terminate the connection here.

d.on('error', cb)

This event fires when local code causes errors in its callbacks. Not all errors can be caught here since some might be in async functions.

d.on('end', cb)

This event fires when the input stream finishes.

install

With npm do:

npm install dnode

protocol

dnode uses a newline-terminated JSON protocol documented in the dnode-protocol project.

dnode in other languages

These libraries implement the dnode protocol too so you can make RPC calls between scripts written in different languages.

shameless plug

Want to make sure your crazy javascript-heavy app still works in other browsers? Give browserling a spin! Browsers in your browser. Powered by dnode.

版本与发布记录

@davedoesdev/dnode 共发布 10 个版本,首次发布于 2019/09/12;过去 12 个月发布了 0 个版本。

版本发布日期距今
3.2.0latest2023/11/162 年前
3.1.02023/11/162 年前
3.0.02023/04/163 年前
2.0.22022/06/034 年前
2.0.12021/12/074 年前
2.0.02021/12/054 年前

关于 @davedoesdev/dnode 的常见问题

@davedoesdev/dnode 是什么?

freestyle rpc

如何安装 @davedoesdev/dnode?

在项目目录运行 npm install @davedoesdev/dnode,或使用 pnpm add / yarn add / bun add。

@davedoesdev/dnode 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/davedoesdev__dnode 是否存在。

@davedoesdev/dnode 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@davedoesdev/dnode 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=16。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@davedoesdev/dnode 可以商用吗?

@davedoesdev/dnode 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@davedoesdev/dnode 的下载量有多少?

根据 npm 官方下载统计,@davedoesdev/dnode 在 2026/10/12 – 2026/11/10 这 30 天内被下载 3,549 次,日均约 118 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@davedoesdev/dnode 还在维护吗?

最新版本 3.2.0 发布于 2023/11/16(2 年前);至今共发布 10 个版本,其中过去 12 个月发布了 0 个;首次发布于 2019/09/12。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。