Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

move lib to local-tld-lib

  • Loading branch information...
commit e3b7fd339fd00dff5fec3ae4c8c2e120d8c676ad 1 parent 2975bd1
Jan Lehnardt authored
6  README.md
Source Rendered
@@ -40,11 +40,7 @@ Dat it. `/.localtld.json` maps the a subdomain to a TCP port. So if you have a h
40 40
 
41 41
 ## I want my app to register itself with local-tld!
42 42
 
43  
-  var ltld = require("local-tld");
44  
-  ltld.add("yourfancyproject", 12345);
45  
-
46  
-  // ok cool, how can I deregister?
47  
-  ltld.remove("yourfancyproject");
  43
+See https://github.com/hoodiehq/local-tld-service
48 44
 
49 45
 
50 46
 ## Ok cool, how does it work?
63  lib/local-tld.js
... ...
@@ -1,63 +0,0 @@
1  
-// An app’s API to ~/.local-tld.json
2  
-var fs = require("fs");
3  
-
4  
-module.exports.tld_file = process.env.HOME + "/.local-tld.json";
5  
-module.exports.base_port = 6000;
6  
-
7  
-module.exports.getPort = function getPort(name) {
8  
-
9  
-  var map = read_json(module.exports.tld_file);
10  
-  var max_port = module.exports.base_port;
11  
-
12  
-  for(var port_m in map) {
13  
-    port_m = parseInt(port_m, 10);
14  
-
15  
-    if(port_m > max_port) {
16  
-      max_port = port_m;
17  
-    }
18  
-
19  
-    var name_m = map[port_m].name;
20  
-    if(name_m == name) {
21  
-      return port_m;
22  
-    }
23  
-  }
24  
-
25  
-  // if we got here, max_port is the highest registered port
26  
-  var new_port = max_port + 1;
27  
-  map[new_port] = {
28  
-    name: name
29  
-  };
30  
-
31  
-  write_json(module.exports.tld_file, map);
32  
-  return new_port;
33  
-};
34  
-
35  
-module.exports.setAlias = function setAlias(name, alias) {
36  
-  var map = read_json(module.exports.tld_file);
37  
-  for(var port_m in map) {
38  
-    var name_m = map[port_m].name;
39  
-    if(name_m == name) {
40  
-      // found it
41  
-      if(!map[port_m].aliases) {
42  
-        map[port_m].aliases = {};
43  
-      }
44  
-      map[port_m].aliases[alias] = true;
45  
-      write_json(module.exports.tld_file, map);
46  
-      return true;
47  
-    }
48  
-  }
49  
-  return false;
50  
-};
51  
-
52  
-var read_json = function read_json(filename, default_value) {
53  
-  try {
54  
-    return JSON.parse(fs.readFileSync(filename));
55  
-  } catch(e) {
56  
-    return default_value || {};
57  
-  }
58  
-};
59  
-
60  
-var write_json = function write_json(filename, value) {
61  
-  fs.writeFileSync(filename + ".tmp", JSON.stringify(value));
62  
-  fs.renameSync(filename + ".tmp", filename);
63  
-};
8  package.json
... ...
@@ -1,11 +1,8 @@
1 1
 {
2 2
   "name": "local-tld",
3  
-  "version": "2.4.0",
  3
+  "version": "3.0.0",
4 4
   "description": "Maintain a TLD on localhost for all your projects.",
5  
-  "directories": {
6  
-    "test": "test"
7  
-  },
8  
-  "main":"lib/local-tld.js",
  5
+  "main":"bin/local-tld-service",
9 6
   "dependencies": {
10 7
     "dnsserver": "https://github.com/sstephenson/dnsserver.js/archive/library.tar.gz",
11 8
     "watchfd": "~0.0.9",
@@ -13,7 +10,6 @@
13 10
   },
14 11
   "devDependencies": {},
15 12
   "scripts": {
16  
-    "test": "node test/test.js",
17 13
     "postinstall": "./bin/local-tld-setup",
18 14
     "preuninstall": "./bin/local-tld-uninstall"
19 15
   },
90  test/test.js
... ...
@@ -1,90 +0,0 @@
1  
-var assert = require("assert");
2  
-var ltld = require("../lib/local-tld");
3  
-var fs = require("fs");
4  
-
5  
-ltld.tld_file = "/tmp/tld-test.json";
6  
-ltld.base_port = 6000;
7  
-
8  
-var tests = {}
9  
-
10  
-tests.test_getPort = function() {
11  
-  ltld.getPort("foo");
12  
-  var expect = {
13  
-    "6001": {
14  
-      "name": "foo"
15  
-    }
16  
-  };
17  
-  var result = read_json(ltld.tld_file);
18  
-  assert.deepEqual(result, expect);
19  
-}
20  
-
21  
-tests.test_getPortAgain = function() {
22  
-  ltld.getPort("foo");
23  
-  ltld.getPort("bar");
24  
-  var expect = {
25  
-    "6001": {
26  
-      "name": "foo"
27  
-    },
28  
-    "6002": {
29  
-      "name": "bar"
30  
-    }
31  
-  };
32  
-  var result = read_json(ltld.tld_file);
33  
-  assert.deepEqual(result, expect);
34  
-}
35  
-
36  
-tests.test_setAlias = function() {
37  
-  var result = ltld.setAlias("foo", "foo1");
38  
-  assert.equal(result, true, "should return true");
39  
-
40  
-  var result = ltld.setAlias("foo", "foo1");
41  
-  var result = ltld.setAlias("wibble", "foo1");
42  
-  assert.equal(result, false, "should return false");
43  
-
44  
-  var expect = {
45  
-    "6001": {
46  
-      "name": "foo",
47  
-      "aliases": {
48  
-        "foo1": true
49  
-      }
50  
-    },
51  
-    "6002": {
52  
-      "name": "bar"
53  
-    }
54  
-  };
55  
-  var result = read_json(ltld.tld_file);
56  
-  assert.deepEqual(result, expect);
57  
-}
58  
-
59  
-try {
60  
-  run_test("test_getPort");
61  
-  run_test("test_getPortAgain");
62  
-  run_test("test_setAlias");
63  
-  console.log("All tests OK.")
64  
-} catch(e) {
65  
-  throw e;
66  
-} finally {
67  
-  cleanup();
68  
-}
69  
-
70  
-function cleanup() {
71  
-  try {
72  
-    fs.unlinkSync(ltld.tld_file);
73  
-  } catch (e) {
74  
-    // yup
75  
-  }
76  
-}
77  
-
78  
-function run_test(name) {
79  
-  try {
80  
-    tests[name]();
81  
-    console.log("OK: %s.", name);
82  
-  } catch(e) {
83  
-    console.log("Fail: %j.", name, e);
84  
-    throw e;
85  
-  }
86  
-}
87  
-
88  
-function read_json(filename) {
89  
-  return JSON.parse(fs.readFileSync(filename));
90  
-}

0 notes on commit e3b7fd3

Please sign in to comment.
Something went wrong with that request. Please try again.