Repository: mafintosh/airswarm
Branch: master
Commit: 3a9acb5017f9
Files: 8
Total size: 5.7 KB
Directory structure:
gitextract_6zr_6dzb/
├── .gitignore
├── .travis.yml
├── LICENSE
├── README.md
├── example.js
├── index.js
├── package.json
└── test.js
================================================
FILE CONTENTS
================================================
================================================
FILE: .gitignore
================================================
node_modules
================================================
FILE: .travis.yml
================================================
language: node_js
node_js:
- "0.10"
- '0.12'
- 'iojs'
================================================
FILE: LICENSE
================================================
The MIT License (MIT)
Copyright (c) 2015 Mathias Buus
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
================================================
FILE: README.md
================================================
# airswarm
Network swarm that automagically discovers other peers on the network using multicast dns
```
npm install airswarm
```
[](http://travis-ci.org/mafintosh/airswarm)
## Usage
``` js
var airswarm = require('airswarm')
airswarm('testing', function (sock) {
sock.write('hello world (' + process.pid + ')\n')
sock.pipe(process.stdout)
})
```
If you run the above program in a couple of processes on the same local network
the swarms should start connecting to each other and write hello world
## API
#### `swarm = airswarm(name, [options], [onpeer])`
Create a new swarm. The `swarm` will emit `peer` everytime a new peer
is connected. Optionally you can pass a `peer` listener as the second argument.
The `peer` will be a tcp stream to another swarm.
Options include
``` js
{
limit: maxPeersToConnectTo // defaults to Infinity
}
```
#### `swarm.peers`
An array containing all the currently connected peers
## License
MIT
================================================
FILE: example.js
================================================
var airswarm = require('airswarm')
airswarm('testing', function (sock) {
sock.write('hello world (' + process.pid + ')\n')
sock.pipe(process.stdout)
})
================================================
FILE: index.js
================================================
var multicastdns = require('multicast-dns')
var net = require('net')
var addr = require('network-address')
module.exports = function airswarm (name, opts, fn) {
if (typeof opts === 'function') return airswarm(name, null, opts)
if (!opts) opts = {}
var limit = opts.limit || Infinity
var mdns = multicastdns()
var connections = {}
var server = net.createServer(function (sock) {
sock.on('error', function (err) {
sock.destroy(err)
})
track(sock)
})
server.peers = []
function track (sock) {
if (server.peers.length >= limit) return sock.destroy()
server.peers.push(sock)
sock.on('close', function () {
server.peers.splice(server.peers.indexOf(sock), 1)
})
server.emit('peer', sock)
}
server.on('listening', function () {
var host = addr()
var port = server.address().port
var id = host + ':' + port
mdns.on('query', function (q) {
for (var i = 0; i < q.questions.length; i++) {
var qs = q.questions[i]
if (qs.name === name && qs.type === 'SRV') return respond()
}
})
mdns.on('response', function (r) {
for (var i = 0; i < r.answers.length; i++) {
var a = r.answers[i]
if (a.name === name && a.type === 'SRV') connect(a.data.target, a.data.port)
}
})
update()
var interval = setInterval(update, 3000)
server.on('close', function () {
clearInterval(interval)
})
function respond () {
mdns.response([{
name: name,
type: 'SRV',
data: {
port: port,
weigth: 0,
priority: 10,
target: host
}
}])
}
function update () {
if (server.peers.length < limit) mdns.query([{name: name, type: 'SRV'}])
}
function connect (host, port) {
var remoteId = host + ':' + port
if (remoteId === id) return
if (connections[remoteId]) return
if (remoteId < id) return respond()
var sock = connections[remoteId] = net.connect(port, host)
sock.on('error', function () {
sock.destroy()
})
sock.on('close', function () {
delete connections[remoteId]
})
track(sock)
}
})
if (fn) server.on('peer', fn)
server.listen(0)
return server
}
================================================
FILE: package.json
================================================
{
"name": "airswarm",
"version": "1.1.0",
"description": "Network swarm that automagically discovers other peers on the network using multicast dns",
"main": "index.js",
"dependencies": {
"multicast-dns": "^3.0.0",
"network-address": "^1.0.0"
},
"devDependencies": {
"standard": "^5.2.1",
"tape": "^4.2.0"
},
"scripts": {
"test": "standard && tape test.js"
},
"repository": {
"type": "git",
"url": "https://github.com/mafintosh/airswarm.git"
},
"author": "Mathias Buus (@mafintosh)",
"license": "MIT",
"bugs": {
"url": "https://github.com/mafintosh/airswarm/issues"
},
"homepage": "https://github.com/mafintosh/airswarm"
}
================================================
FILE: test.js
================================================
var tape = require('tape')
var airswarm = require('./')
tape('connects', function (t) {
t.plan(3)
var once1 = true
var once2 = true
airswarm(process.pid + '-testing--', function (sock) {
t.ok(once1, 'got socket')
once1 = false
sock.on('data', function (data) {
t.same(data.toString(), '+')
})
})
airswarm(process.pid + '-testing--', function (sock) {
t.ok(once2, 'got socket')
once2 = false
sock.write('+')
})
})
tape('exits', function (t) {
t.end()
process.exit(0)
})
gitextract_6zr_6dzb/ ├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── example.js ├── index.js ├── package.json └── test.js
SYMBOL INDEX (4 symbols across 1 files)
FILE: index.js
function track (line 22) | function track (sock) {
function respond (line 57) | function respond () {
function update (line 70) | function update () {
function connect (line 74) | function connect (host, port) {
Condensed preview — 8 files, each showing path, character count, and a content snippet. Download the .json file or copy for the full structured content (6K chars).
[
{
"path": ".gitignore",
"chars": 13,
"preview": "node_modules\n"
},
{
"path": ".travis.yml",
"chars": 60,
"preview": "language: node_js\nnode_js:\n - \"0.10\"\n - '0.12'\n - 'iojs'\n"
},
{
"path": "LICENSE",
"chars": 1079,
"preview": "The MIT License (MIT)\n\nCopyright (c) 2015 Mathias Buus\n\nPermission is hereby granted, free of charge, to any person obta"
},
{
"path": "README.md",
"chars": 1027,
"preview": "# airswarm\n\nNetwork swarm that automagically discovers other peers on the network using multicast dns\n\n```\nnpm install a"
},
{
"path": "example.js",
"chars": 157,
"preview": "var airswarm = require('airswarm')\n\nairswarm('testing', function (sock) {\n sock.write('hello world (' + process.pid + '"
},
{
"path": "index.js",
"chars": 2288,
"preview": "var multicastdns = require('multicast-dns')\nvar net = require('net')\nvar addr = require('network-address')\n\nmodule.expor"
},
{
"path": "package.json",
"chars": 689,
"preview": "{\n \"name\": \"airswarm\",\n \"version\": \"1.1.0\",\n \"description\": \"Network swarm that automagically discovers other peers o"
},
{
"path": "test.js",
"chars": 526,
"preview": "var tape = require('tape')\nvar airswarm = require('./')\n\ntape('connects', function (t) {\n t.plan(3)\n\n var once1 = true"
}
]
About this extraction
This page contains the full source code of the mafintosh/airswarm GitHub repository, extracted and formatted as plain text for AI agents and large language models (LLMs). The extraction includes 8 files (5.7 KB), approximately 1.7k tokens, and a symbol index with 4 extracted functions, classes, methods, constants, and types. Use this with OpenClaw, Claude, ChatGPT, Cursor, Windsurf, or any other AI tool that accepts text input. You can copy the full output to your clipboard or download it as a .txt file.
Extracted by GitExtract — free GitHub repo to text converter for AI. Built by Nikandr Surkov.