Skip to content
This repository has been archived by the owner on Feb 8, 2023. It is now read-only.

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed Jul 1, 2013
0 parents commit 54fc87d
Show file tree
Hide file tree
Showing 11 changed files with 391 additions and 0 deletions.
15 changes: 15 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
coverage.html
*.seed
*.log
*.csv
*.dat
*.out
*.pid
*.gz

pids
logs
results

node_modules
npm-debug.log
6 changes: 6 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
test/
coverage.html
lib-cov/
Makefile
.travis.yml
logo.png
7 changes: 7 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
language: node_js
node_js:
- '0.11'
- '0.10'
- 0.8
- 0.6
script: make test-coveralls
21 changes: 21 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
This software is licensed under the MIT License.

Copyright (C) 2013 by fengmk2 <fengmk2@gmail.com>

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.
30 changes: 30 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
TESTS = test/*.test.js
REPORTER = spec
TIMEOUT = 2000
MOCHA_OPTS =

install:
@npm install

test: install
@NODE_ENV=test ./node_modules/mocha/bin/mocha \
--reporter $(REPORTER) \
--timeout $(TIMEOUT) \
$(MOCHA_OPTS) \
$(TESTS)

test-cov:
@rm -f coverage.html
@$(MAKE) test MOCHA_OPTS='--require blanket' REPORTER=html-cov > coverage.html
@$(MAKE) test MOCHA_OPTS='--require blanket' REPORTER=travis-cov
@ls -lh coverage.html

test-coveralls:
@$(MAKE) test
@echo TRAVIS_JOB_ID $(TRAVIS_JOB_ID)
@$(MAKE) test MOCHA_OPTS='--require blanket' REPORTER=mocha-lcov-reporter | ./node_modules/coveralls/bin/coveralls.js

test-all: test test-cov

.PHONY: install test test-cov test-all test-coveralls

50 changes: 50 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
cache-dns [![Build Status](https://secure.travis-ci.org/fengmk2/cache-dns.png)](http://travis-ci.org/fengmk2/cache-dns) [![Coverage Status](https://coveralls.io/repos/fengmk2/cache-dns/badge.png)](https://coveralls.io/r/fengmk2/cache-dns)
=======

![logo](https://raw.github.com/fengmk2/cache-dns/master/logo.png)

Like `dns` module, but cache the results.

## Install

```bash
$ npm install cache-dns
```

## Usage

```js
var CacheDNS = require('cache-dns');

var dns = CacheDNS.create({
cacheTime: 10000, // 10 seconds
});
dns.resolve4('www.taobao.com', function (err, addresses) {
console.log(arguments);
});
```

## License

(The MIT License)

Copyright (c) 2013 fengmk2 &lt;fengmk2@gmail.com&gt;

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.
56 changes: 56 additions & 0 deletions benchmark.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*!
* cache-dns - benchmark.js
* Copyright(c) 2013 fengmk2 <fengmk2@gmail.com>
* MIT Licensed
*/

"use strict";

/**
* Module dependencies.
*/

var dns = require('dns');
var CacheDNS = require('./');
var Benchmark = require('benchmark');
// https://github.com/bestiejs/benchmark.js/issues/39
Benchmark.support.timeout = false;
var suite = new Benchmark.Suite();

var cacheDNS = CacheDNS.create();
dns.resolve4('www.taobao.com', function () {
console.log('dns', arguments);
});
cacheDNS.resolve4('www.taobao.com', function () {
console.log('cacheDNS', arguments);

suite
.add("cacheDNS.resolve4('www.taobao.com')", function (deferred) {
cacheDNS.resolve4('www.taobao.com', function (err, addresses) {
deferred.resolve();
});
}, {defer: true})
.add('setImmediate()', function (deferred) {
setImmediate(function () {
deferred.resolve();
});
}, {
defer: true
})
.add("dns.resolve4('www.taobao.com')", function (deferred) {
dns.resolve4('www.taobao.com', function (err, addresses) {
deferred.resolve();
});
}, {defer: true})

// add listeners
.on('cycle', function (event) {
console.log(String(event.target));
})
.on('complete', function () {
console.log('Fastest is ' + this.filter('fastest').pluck('name'));
cacheDNS.close();
})
.run({ async: true });
});

1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('./lib/cache-dns');
70 changes: 70 additions & 0 deletions lib/cache-dns.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*!
* cache-dns - lib/cache-dns.js
* Copyright(c) 2013 fengmk2 <fengmk2@gmail.com>
* MIT Licensed
*/

"use strict";

/**
* Module dependencies.
*/

var dns = require('dns');
var util = require('util');
var EventEmitter = require('events').EventEmitter;

function CacheDNS(options) {
if (!(this instanceof CacheDNS)) {
return new CacheDNS(options);
}
EventEmitter.call(this);

options = options || {};
this.cacheTime = options.cacheTime || 10000;
this.domains = {};
this.timer = setInterval(this._updateCaches.bind(this), this.cacheTime);
}

util.inherits(CacheDNS, EventEmitter);

CacheDNS.prototype.resolve4 = function (domain, callback) {
var addresses = this.domains[domain];
if (!addresses) {
return this._resolve4(domain, callback);
}
callback(null, addresses);
};

CacheDNS.prototype._resolve4 = function (domain, callback) {
var self = this;
dns.resolve4(domain, function (err, addresses) {
self.emit(domain, err, addresses);
if (err) {
return callback(err);
}
addresses = addresses || [];
if (addresses.length > 0) {
self.domains[domain] = addresses;
}
callback(null, addresses);
});
};

function noop() {}

CacheDNS.prototype._updateCaches = function () {
for (var domain in this.domains) {
this._resolve4(domain, noop);
}
};

CacheDNS.prototype.close = function () {
clearInterval(this.timer);
};

CacheDNS.create = function (options) {
return new CacheDNS(options);
};

module.exports = CacheDNS;
44 changes: 44 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"name": "cache-dns",
"version": "0.0.1",
"description": "Like `dns` module, but cache the results.",
"main": "index.js",
"scripts": {
"test": "make test-all",
"blanket": {
"pattern": "cache-dns/lib",
"data-cover-flags": {
"debug": false
}
},
"travis-cov": {
"threshold": 90
}
},
"dependencies": {

},
"devDependencies": {
"should": "*",
"blanket": "*",
"travis-cov": "*",
"coveralls": "*",
"mocha-lcov-reporter": "*",
"pedding": "*",
"mm": "*",
"benchmark": "*",
"mocha": "*"
},
"repository": {
"type": "git",
"url": "git://github.com/fengmk2/cache-dns.git"
},
"keywords": [
"cache-dns"
],
"engines": {
"node": ">= 0.8.0"
},
"author": "fengmk2 <fengmk2@gmail.com>",
"license": "MIT"
}
Loading

0 comments on commit 54fc87d

Please sign in to comment.