Permalink
Browse files

move lib to local-tld-lib

  • Loading branch information...
1 parent 2975bd1 commit e3b7fd339fd00dff5fec3ae4c8c2e120d8c676ad @janl janl committed Jan 30, 2013
Showing with 3 additions and 164 deletions.
  1. +1 −5 README.md
  2. +0 −63 lib/local-tld.js
  3. +2 −6 package.json
  4. +0 −90 test/test.js
View
6 README.md
@@ -40,11 +40,7 @@ Dat it. `/.localtld.json` maps the a subdomain to a TCP port. So if you have a h
## I want my app to register itself with local-tld!
- var ltld = require("local-tld");
- ltld.add("yourfancyproject", 12345);
-
- // ok cool, how can I deregister?
- ltld.remove("yourfancyproject");
+See https://github.com/hoodiehq/local-tld-service
## Ok cool, how does it work?
View
63 lib/local-tld.js
@@ -1,63 +0,0 @@
-// An app’s API to ~/.local-tld.json
-var fs = require("fs");
-
-module.exports.tld_file = process.env.HOME + "/.local-tld.json";
-module.exports.base_port = 6000;
-
-module.exports.getPort = function getPort(name) {
-
- var map = read_json(module.exports.tld_file);
- var max_port = module.exports.base_port;
-
- for(var port_m in map) {
- port_m = parseInt(port_m, 10);
-
- if(port_m > max_port) {
- max_port = port_m;
- }
-
- var name_m = map[port_m].name;
- if(name_m == name) {
- return port_m;
- }
- }
-
- // if we got here, max_port is the highest registered port
- var new_port = max_port + 1;
- map[new_port] = {
- name: name
- };
-
- write_json(module.exports.tld_file, map);
- return new_port;
-};
-
-module.exports.setAlias = function setAlias(name, alias) {
- var map = read_json(module.exports.tld_file);
- for(var port_m in map) {
- var name_m = map[port_m].name;
- if(name_m == name) {
- // found it
- if(!map[port_m].aliases) {
- map[port_m].aliases = {};
- }
- map[port_m].aliases[alias] = true;
- write_json(module.exports.tld_file, map);
- return true;
- }
- }
- return false;
-};
-
-var read_json = function read_json(filename, default_value) {
- try {
- return JSON.parse(fs.readFileSync(filename));
- } catch(e) {
- return default_value || {};
- }
-};
-
-var write_json = function write_json(filename, value) {
- fs.writeFileSync(filename + ".tmp", JSON.stringify(value));
- fs.renameSync(filename + ".tmp", filename);
-};
View
8 package.json
@@ -1,19 +1,15 @@
{
"name": "local-tld",
- "version": "2.4.0",
+ "version": "3.0.0",
"description": "Maintain a TLD on localhost for all your projects.",
- "directories": {
- "test": "test"
- },
- "main":"lib/local-tld.js",
+ "main":"bin/local-tld-service",
"dependencies": {
"dnsserver": "https://github.com/sstephenson/dnsserver.js/archive/library.tar.gz",
"watchfd": "~0.0.9",
"http-proxy": "~0.8.7"
},
"devDependencies": {},
"scripts": {
- "test": "node test/test.js",
"postinstall": "./bin/local-tld-setup",
"preuninstall": "./bin/local-tld-uninstall"
},
View
90 test/test.js
@@ -1,90 +0,0 @@
-var assert = require("assert");
-var ltld = require("../lib/local-tld");
-var fs = require("fs");
-
-ltld.tld_file = "/tmp/tld-test.json";
-ltld.base_port = 6000;
-
-var tests = {}
-
-tests.test_getPort = function() {
- ltld.getPort("foo");
- var expect = {
- "6001": {
- "name": "foo"
- }
- };
- var result = read_json(ltld.tld_file);
- assert.deepEqual(result, expect);
-}
-
-tests.test_getPortAgain = function() {
- ltld.getPort("foo");
- ltld.getPort("bar");
- var expect = {
- "6001": {
- "name": "foo"
- },
- "6002": {
- "name": "bar"
- }
- };
- var result = read_json(ltld.tld_file);
- assert.deepEqual(result, expect);
-}
-
-tests.test_setAlias = function() {
- var result = ltld.setAlias("foo", "foo1");
- assert.equal(result, true, "should return true");
-
- var result = ltld.setAlias("foo", "foo1");
- var result = ltld.setAlias("wibble", "foo1");
- assert.equal(result, false, "should return false");
-
- var expect = {
- "6001": {
- "name": "foo",
- "aliases": {
- "foo1": true
- }
- },
- "6002": {
- "name": "bar"
- }
- };
- var result = read_json(ltld.tld_file);
- assert.deepEqual(result, expect);
-}
-
-try {
- run_test("test_getPort");
- run_test("test_getPortAgain");
- run_test("test_setAlias");
- console.log("All tests OK.")
-} catch(e) {
- throw e;
-} finally {
- cleanup();
-}
-
-function cleanup() {
- try {
- fs.unlinkSync(ltld.tld_file);
- } catch (e) {
- // yup
- }
-}
-
-function run_test(name) {
- try {
- tests[name]();
- console.log("OK: %s.", name);
- } catch(e) {
- console.log("Fail: %j.", name, e);
- throw e;
- }
-}
-
-function read_json(filename) {
- return JSON.parse(fs.readFileSync(filename));
-}

0 comments on commit e3b7fd3

Please sign in to comment.