{"_id":"dnode-weak-napi","_rev":"3340210","name":"dnode-weak-napi","description":"freestyle rpc","dist-tags":{"latest":"1.2.2"},"maintainers":[{"name":"christianv","email":"vueringschristian@gmail.com"}],"time":{"modified":"2024-07-10T08:26:44.000Z","created":"2020-04-20T22:00:08.762Z","1.2.2":"2020-04-20T22:00:08.762Z"},"users":{},"author":{"name":"James Halliday","email":"mail@substack.net","url":"http://substack.net"},"repository":{"type":"git","url":"git+ssh://git@github.com/substack/dnode.git"},"versions":{"1.2.2":{"name":"dnode-weak-napi","version":"1.2.2","description":"freestyle rpc","main":"./index.js","keywords":["rpc","callbacks"],"repository":{"type":"git","url":"git+ssh://git@github.com/substack/dnode.git"},"dependencies":{"dnode-protocol":"~0.2.2","jsonify":"~0.0.0","weak-napi":"^1.0.3"},"optionalDependencies":{"weak-napi":"^1.0.3"},"devDependencies":{"tape":"~2.3.2"},"scripts":{"test":"tape test/*.js test/server/*.js"},"testling":{"files":"test/*.js","browsers":["ie/6..latest","chrome/10","chrome/latest","chrome/canary","firefox/10","firefox/latest","firefox/nightly","safari/latest","opera/11.0..latest","iphone/6","ipad/6","android-browser/latest"]},"browserify":"browser.js","engine":{"node":">=0.6.0"},"license":"MIT","author":{"name":"James Halliday","email":"mail@substack.net","url":"http://substack.net"},"gitHead":"075b5f47861ef75d88b234abcd4116ff3137e25e","bugs":{"url":"https://github.com/substack/dnode/issues"},"homepage":"https://github.com/substack/dnode#readme","_id":"dnode-weak-napi@1.2.2","_nodeVersion":"12.14.1","_npmVersion":"6.13.4","dist":{"shasum":"7618c12eae5ef0622ff6b48a924b342e2162f8de","size":11193,"noattachment":false,"key":"/dnode-weak-napi/-/dnode-weak-napi-1.2.2.tgz","tarball":"http://registry.cnpm.dingdandao.com/dnode-weak-napi/download/dnode-weak-napi-1.2.2.tgz"},"maintainers":[{"name":"christianv","email":"vueringschristian@gmail.com"}],"_npmUser":{"name":"christianv","email":"vueringschristian@gmail.com"},"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/dnode-weak-napi_1.2.2_1587420008586_0.8915307617832613"},"_hasShrinkwrap":false,"publish_time":1587420008762,"_cnpm_publish_time":1587420008762,"_cnpmcore_publish_time":"2021-12-17T00:49:52.109Z"}},"readme":"# dnode\n\ndnode is an asynchronous rpc system for node.js that lets you\ncall remote functions.\n\nYou can pass callbacks to remote functions, and the remote end can call\nthe functions you passed in with callbacks of its own and so on.\nIt's callbacks all the way down!\n\n[![browser support](https://ci.testling.com/substack/dnode.png)](http://ci.testling.com/substack/dnode)\n\n[![build status](https://secure.travis-ci.org/substack/dnode.png)](http://travis-ci.org/substack/dnode)\n\n![dnode: freestyle rpc](http://substack.net/images/dnode.png)\n\n# example\n\n## listen and connect\n\nserver:\n\n``` js\nvar dnode = require('dnode');\nvar server = dnode({\n    transform : function (s, cb) {\n        cb(s.replace(/[aeiou]{2,}/, 'oo').toUpperCase())\n    }\n});\nserver.listen(5004);\n```\n\nclient:\n\n``` js\nvar dnode = require('dnode');\n\nvar d = dnode.connect(5004);\nd.on('remote', function (remote) {\n    remote.transform('beep', function (s) {\n        console.log('beep => ' + s);\n        d.end();\n    });\n});\n```\n\noutput:\n\n```\n$ node server.js &\n[1] 27574\n$ node client.js\nbeep => BOOP\n```\n\n## streaming\n\nThe `.connect()` and `.listen()` calls in the previous example are just\nconvenience methods for piping to and from readable/writable streams.\nHere's the previous example with the streams set up explicitly:\n\nserver:\n\n``` js\nvar dnode = require('dnode');\nvar net = require('net');\n\nvar server = net.createServer(function (c) {\n    var d = dnode({\n        transform : function (s, cb) {\n            cb(s.replace(/[aeiou]{2,}/, 'oo').toUpperCase())\n        }\n    });\n    c.pipe(d).pipe(c);\n});\n\nserver.listen(5004);\n```\n\nclient:\n\n``` js\nvar dnode = require('dnode');\nvar net = require('net');\n\nvar d = dnode();\nd.on('remote', function (remote) {\n    remote.transform('beep', function (s) {\n        console.log('beep => ' + s);\n        d.end();\n    });\n});\n\nvar c = net.connect(5004);\nc.pipe(d).pipe(c);\n```\n\noutput:\n\n```\n$ node server.js &\n[1] 27586\n$ node client.js \nbeep => BOOP\n```\n\n## dnode in the browser\n\nSince dnode instances are just readable/writable streams, you can use them with\nany streaming transport, including in the browser!\n\nThis example uses the streaming interface provided by\n[shoe](https://github.com/substack/shoe), which is just a thin wrapper on top of\n[sockjs](http://sockjs.org/) that provides websockets with fallbacks.\n\nFirst whip up a server:\n\n``` js\nvar http = require('http');\nvar shoe = require('shoe');\nvar ecstatic = require('ecstatic')(__dirname + '/static');\nvar dnode = require('dnode');\n\nvar server = http.createServer(ecstatic);\nserver.listen(9999);\n\nvar sock = shoe(function (stream) {\n    var d = dnode({\n        transform : function (s, cb) {\n            var res = s.replace(/[aeiou]{2,}/, 'oo').toUpperCase();\n            cb(res);\n        }\n    });\n    d.pipe(stream).pipe(d);\n});\nsock.install(server, '/dnode');\n```\n\nThen write some browser code:\n\n``` js\nvar domready = require('domready');\nvar shoe = require('shoe');\nvar dnode = require('dnode');\n\ndomready(function () {\n    var result = document.getElementById('result');\n    var stream = shoe('/dnode');\n    \n    var d = dnode();\n    d.on('remote', function (remote) {\n        remote.transform('beep', function (s) {\n            result.textContent = 'beep => ' + s;\n        });\n    });\n    d.pipe(stream).pipe(d);\n});\n```\n\nInstall the dependencies for this example then compile the browser code with\n[browserify](https://github.com/substack/node-browserify):\n\n```\n$ npm install dnode shoe domready ecstatic\n$ npm install -g browserify\n$ browserify client.js -o static/bundle.js\n```\n\nNow drop a script tag into static/index.html:\n\n``` html\n<script src=\"/bundle.js\"></script>\n<div id=\"result\"></div>\n```\n\nand navigate to http://localhost:9999.\nYou should see `beep => BOOP` on the page!\n\nCheck out the\n[complete shoe example](https://github.com/substack/dnode/tree/master/example/shoe).\n\n# methods\n\n``` js\nvar dnode = require('dnode')\n```\n\n## var d = dnode(cons, opts={})\n\nCreate a new readable/writable dnode stream object `d`.\nAll the usual stream methods are at your disposal: pipe(), write(), end().\n\nIf `cons` is a function, it will be called `new cons(remote, d)` to create a new\ninstance object. Otherwise its value will be used directly. When `cons` is\ncalled as a function, the `remote` ref will be an empty unpopulated object.\n\nBy default, dnode uses weakmaps to garbage collect unused callbacks\nautomatically. This behavior prevents memory leaks in long-running connections.\n\nYou can turn weakmaps off by setting `opts.weak = false`.\n\n## d.connect(...)\n\nThis method is a shortcut for setting up a pipe between `d` and a new\n`net.connect()` stream.\n\nThe host, port, and callback arguments supplied will be inferred by their\ntypes.\n\nIf you pass a callback in as an argument, it will be added as a listener to the\n`'remote'` event.\n\nReturns the `d` object.\n\n## dnode.connect(...)\n\nShortcut to create a connection without a constructor.\n\n## d.listen(...)\n\nThis method is a shortcut for setting up a `net.createServer()` and piping\nnetwork streams to and from new dnode streams.\n\nThe host, port, and callback parameters will be inferred from the types of the\narguments.\n\nReturns a net server object that will also emit `'local'` and `'remote'` events\nfrom the underlying dnode streams..\n\n## dnode.listen(...)\n\nShortcut to create a listener without a constructor.\n\n# events\n\n## d.on('remote', cb)\n\nThis event fires with `cb(remote, d)` when the remote side of the connection\nhas constructed its instance.\n\n## d.on('local', cb)\n\nThis event fires right after the constructed instance has been created locally\nbut before it gets sent to the remote end so you can modify the ref object.\n\nThis event fires with `cb(ref, d)` where `ref` is the local instance object.\n\n## d.on('fail', cb)\n\nThis event fires when the remote end causes errors in the protocol layer.\n\nThese are non-fatal and can probably be ignored but you could also terminate the\nconnection here.\n\n## d.on('error', cb)\n\nThis event fires when local code causes errors in its callbacks.\nNot all errors can be caught here since some might be in async functions.\n\n## d.on('end', cb)\n\nThis event fires when the input stream finishes.\n\n# install\n\nWith [npm](http://npmjs.org) do:\n\n```\nnpm install dnode\n```\n\n# protocol\n\ndnode uses a newline-terminated JSON protocol\n[documented in the dnode-protocol project](https://github.com/substack/dnode-protocol/blob/master/doc/protocol.markdown#the-protocol).\n\n# dnode in other languages\n\nThese libraries implement the dnode protocol too so you can make RPC calls\nbetween scripts written in different languages.\n\n* [dnode-perl](http://github.com/substack/dnode-perl)\n* [dnode-ruby](http://github.com/substack/dnode-ruby)\n* [dnode-php](https://github.com/bergie/dnode-php)\n* [dnode-php-sync-client](https://github.com/erasys/dnode-php-sync-client)\n* [dnode-java](https://github.com/aslakhellesoy/dnode-java)\n\n# shameless plug\n\nWant to make sure your crazy javascript-heavy app still works in other\nbrowsers?\nGive [browserling](http://browserling.com) a spin!\nBrowsers in your browser. Powered by dnode.\n\n","_attachments":{},"homepage":"https://github.com/substack/dnode#readme","bugs":{"url":"https://github.com/substack/dnode/issues"},"license":"MIT"}