Browse files

Merge branch 'unit-test'

  • Loading branch information...
2 parents 21f7b53 + f39eb88 commit 22b098f734478f2c92325c1736d77b686f4a9ef9 Greg Guthe committed Apr 15, 2012
Showing with 117 additions and 71 deletions.
  1. +4 −0 .npmignore
  2. +4 −0 .travis.yml
  3. +5 −0 Makefile
  4. +2 −0 README.md
  5. +1 −1 bin/npm-proxy
  6. +25 −0 lib/cli.coffee
  7. +6 −0 lib/index.coffee
  8. +46 −70 lib/server.coffee
  9. +6 −0 lib/targetParser.coffee
  10. +4 −0 package.json
  11. +14 −0 test/unit.coffee
View
4 .npmignore
@@ -0,0 +1,4 @@
+test
+node_modules
+npmjs.org
+.travis.yml
View
4 .travis.yml
@@ -0,0 +1,4 @@
+language: node_js
+node_js:
+ - 0.6
+before_script: 'npm install mocha@latest expect.js@latest'
View
5 Makefile
@@ -14,3 +14,8 @@ setup-npmjs:
acceptance-test: # clean setup-couch start-couch setup-npmjs
REGISTRY='http://localhost:8080/registry/_design/app/_rewrite' ./test/acceptance.sh
+
+unit-test:
+ ./node_modules/mocha/bin/mocha -u bdd --compilers coffee:coffee-script --bail --growl test/*.coffee
+
+test: unit-test acceptance-test
View
2 README.md
@@ -4,6 +4,8 @@ npm to publish packages to a child/private registry and install
packages from a child or parent/public registry (if the child doesn't
have the package).
+[![Build Status](https://secure.travis-ci.org/g-k/npm-proxy.png)](http://travis-ci.org/g-k/npm-proxy)
+
## Install
Requires node version 0.6.6 or newer.
View
2 bin/npm-proxy
@@ -1,3 +1,3 @@
#!/usr/bin/env node
require('coffee-script')
-require('../lib/server')
+require('../lib/cli')
View
25 lib/cli.coffee
@@ -0,0 +1,25 @@
+#!/usr/bin/env coffee
+
+fs = require 'fs'
+
+server = require './server'
+program = require 'commander'
+{ targetParser } = require './targetParser'
+
+
+npmPackage = JSON.parse fs.readFileSync require.resolve('../package.json'), 'utf8'
+
+program
+ .version(npmPackage.version)
+ .option('-t, --target [localhost:8080]',
+ 'hostname and port to listen on', targetParser,
+ { host: 'localhost', port:8080 })
+ .option('-c, --child-registry-target [localhost:5984]',
+ 'NPM registry to push packages and pull from first', targetParser,
+ { host: 'localhost', port:5984 })
+ .option('-p, --parent-registry-target [registry.npmjs.org:80]',
+ "NPM registry to pull packages from if the child doesn't have it", targetParser,
+ { host: 'registry.npmjs.org', port: 80 })
+ .parse(process.argv)
+
+server.createNpmProxyServer program
View
6 lib/index.coffee
@@ -0,0 +1,6 @@
+server = require './server'
+{ targetParser } = require './targetParser'
+
+module.exports =
+ targetParser: targetParser
+ server: server
View
116 lib/server.coffee
@@ -1,94 +1,70 @@
-#!/usr/bin/env node
-
-fs = require 'fs'
http = require 'http'
-program = require 'commander'
httpProxy = require 'http-proxy'
rest = require 'restler'
-targetParser = (target) ->
- # Splits a target string into an object with host and port
- [host, port] = target.split ':'
- host: host
- port: parseInt(port, 10) or 80
-
-npmPackage = JSON.parse fs.readFileSync require.resolve('../package.json'), 'utf8'
-
-program
- .version(npmPackage.version)
- .option('-t, --target [localhost:8080]',
- 'hostname and port to listen on', targetParser,
- { host: 'localhost', port:8080 })
- .option('-c, --child-registry-target [localhost:5984]',
- 'NPM registry to push packages and pull from first', targetParser,
- { host: 'localhost', port:5984 })
- .option('-p, --parent-registry-target [registry.npmjs.org:80]',
- "NPM registry to pull packages from if the child doesn't have it", targetParser,
- { host: 'registry.npmjs.org', port: 80 })
- .parse(process.argv)
-
-
-proxy = program.target
-parent_npm = program.parentRegistryTarget
-child_npm = program.childRegistryTarget
-
+createNpmProxyServer = (options) ->
+ proxy = options.target
+ parent_npm = options.parentRegistryTarget
+ child_npm = options.childRegistryTarget
-child_npm.check = (req) ->
- url = "http://#{@host}:#{@port}" + req.url
- console.log "#{req.method}:", url
- req.headers['host'] = "#{@host}:#{@port}"
- rest.get url, headers: req.headers
+ child_npm.check = (req) ->
+ url = "http://#{@host}:#{@port}" + req.url
+ console.log "#{req.method}:", url
+ req.headers['host'] = "#{@host}:#{@port}"
+ rest.get url, headers: req.headers
+ strip_url_for_npm_vhost = (url) ->
+ # Strips the couchdb npmjs.org app from the URL
+ # for NPM registries with the registry vhost set
+ url.replace 'registry/_design/app/_rewrite/', ''
-strip_url_for_npm_vhost = (url) ->
- # Strips the couchdb npmjs.org app from the URL
- # for NPM registries with the registry vhost set
- url.replace 'registry/_design/app/_rewrite/', ''
+ server = httpProxy.createServer (req, res, proxy) ->
+ buffer = httpProxy.buffer req
+ if req.method is 'GET'
+ # Check if the child npm has it
+ check = child_npm.check req
-server = httpProxy.createServer (req, res, proxy) ->
- buffer = httpProxy.buffer req
+ # Found it in our repo
+ check.on 'success', (data) ->
+ console.log "INFO: Found #{req.url} in child npm!"
- if req.method is 'GET'
- # Check if the child npm has it
- check = child_npm.check req
+ req.headers['host'] = child_npm.host
+ proxyOpts = child_npm
+ proxyOpts['buffer'] = buffer
- # Found it in our repo
- check.on 'success', (data) ->
- console.log "INFO: Found #{req.url} in child npm!"
+ proxy.proxyRequest req, res, proxyOpts
- req.headers['host'] = child_npm.host
- proxyOpts = child_npm
- proxyOpts['buffer'] = buffer
+ check.on 'error', (err) ->
+ console.error "ERROR: Can't find #{req.url} in child npm"
- proxy.proxyRequest req, res, proxyOpts
+ req.url = strip_url_for_npm_vhost req.url
- check.on 'error', (err) ->
- console.error "ERROR: Can't find #{req.url} in child npm"
+ console.info "INFO: proxying to parent npm #{req.url}"
- req.url = strip_url_for_npm_vhost req.url
+ req.headers['host'] = parent_npm.host
+ proxyOpts = parent_npm
+ proxyOpts['buffer'] = buffer
- console.info "INFO: proxying to parent npm #{req.url}"
+ proxy.proxyRequest req, res, proxyOpts
+ else
+ # Update our repo
+ console.info "INFO: Updating #{req.url}"
- req.headers['host'] = parent_npm.host
- proxyOpts = parent_npm
- proxyOpts['buffer'] = buffer
+ req.headers['host'] = child_npm.host
+ proxyOpts = child_npm
+ proxyOpts['buffer'] = buffer
- proxy.proxyRequest req, res, proxyOpts
- else
- # Update our repo
- console.info "INFO: Updating #{req.url}"
+ proxy.proxyRequest req, res, proxyOpts
- req.headers['host'] = child_npm.host
- proxyOpts = child_npm
- proxyOpts['buffer'] = buffer
+ server.on 'error', (err) ->
+ console.error 'ERROR: Server Error:', err
- proxy.proxyRequest req, res, proxyOpts
+ server.listen proxy.port, proxy.host, () ->
+ console.info "Proxying #{proxy.host}:#{proxy.port}"
-server.on 'error', (err) ->
- console.error 'ERROR: Server Error:', err
+ return server
-server.listen proxy.port, proxy.host, () ->
- console.info "Proxying #{proxy.host}:#{proxy.port}"
+module.exports = createNpmProxyServer: createNpmProxyServer
View
6 lib/targetParser.coffee
@@ -0,0 +1,6 @@
+module.exports =
+ targetParser: targetParser = (target) ->
+ # Splits a target string into an object with host and port
+ [host, port] = target.split ':'
+ host: host
+ port: parseInt(port, 10) or 80
View
4 package.json
@@ -20,4 +20,8 @@
, "http-proxy": ">= 0.8.0"
, "restler": "0.2.x"
}
+, "devDependencies": {
+ "mocha": "latest"
+ , "expect.js": "latest"
+ }
}
View
14 test/unit.coffee
@@ -0,0 +1,14 @@
+expect = require 'expect.js'
+{ targetParser, server } = require '../lib'
+
+
+describe 'targetParser', ->
+
+ it 'should return host and port', ->
+ expect(targetParser 'localhost:8080').to.eql {
+ host: 'localhost'
+ port: 8080
+ }
+
+ it 'should default to port 80', ->
+ expect(targetParser('').port).to.equal 80

0 comments on commit 22b098f

Please sign in to comment.