Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring the Code #30

Merged
merged 5 commits into from
Jan 31, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
The diff you're trying to view is too large. We only load the first 3000 changed files.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
tests/t-run*

# Logs
logs
*.log
Expand Down
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
tests/ipfs-repo-tests
13 changes: 8 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
registry-mirror
===============

[![](https://img.shields.io/badge/made%20by-Protocol%20Labs-blue.svg?style=flat-square)](http://ipn.io) [![](https://img.shields.io/badge/freenode-%23ipfs-blue.svg?style=flat-square)](http://webchat.freenode.net/?channels=%23ipfs) [![Dependency Status](https://david-dm.org/diasdavid/registry-mirror.svg?style=flat-square)](https://david-dm.org/diasdavid/registry-mirror) [![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square)](https://github.com/feross/standard) [![Build Status](https://img.shields.io/travis/diasdavid/registry-mirror/master.svg?style=flat-square)](https://travis-ci.org/diasdavid/registry-mirror)
[![](https://img.shields.io/badge/made%20by-Protocol%20Labs-blue.svg?style=flat-square)](http://ipn.io)
[![](https://img.shields.io/badge/freenode-%23ipfs-blue.svg?style=flat-square)](http://webchat.freenode.net/?channels=%23ipfs)
[![Dependency Status](https://david-dm.org/diasdavid/registry-mirror.svg?style=flat-square)](https://david-dm.org/diasdavid/registry-mirror)
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square)](https://github.com/feross/standard)
[![Build Status](https://img.shields.io/travis/diasdavid/registry-mirror/master.svg?style=flat-square)](https://travis-ci.org/diasdavid/registry-mirror)

> Set up a NPM registry mirror, using your favourite storage, including IPFS! :D
> registry-mirror sets a mirror the the whole NPM registry, using IPFS for the discovery and transport of modules.

# Quick setup (probably all that you need)

## Install IPFS dev0.4.0

To install IPFS dev0.4.0, you will need go installed, to install go in your machine, go to https://golang.org/dl and then run:

```bash
$ go get -u github.com/ipfs/go-ipfs
$ cd $GOPATH/src/github.com/ipfs/go-ipfs
$ git checkout dev0.4.0
$ go install ./cmd/ipfs
$ ipfs version
ipfs version 0.4.0-dev
```
Expand Down
20 changes: 11 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"name": "registry-mirror",
"version": "0.3.3",
"description": "Set up a NPM registry mirror, using your favourite storage, including IPFS! :D",
"description": "Set up a NPM registry mirror, using your favourite storage, IPFS! :D",
"main": "src/index.js",
"bin": {
"registry-mirror": "src/cli/bin.js"
},
"scripts": {
"test": "mocha",
"test": "mocha tests/test-*.js",
"lint": "standard"
},
"pre-commit": [
Expand All @@ -28,25 +28,27 @@
},
"homepage": "https://github.com/diasdavid/registry-mirror#readme",
"dependencies": {
"davlog": "0.0.2",
"async": "^1.5.2",
"debug": "^2.2.0",
"express": "^4.12.3",
"ipfs-api": "github:ipfs/js-ipfs-api#ba85b3b",
"ipfs-blob-store": "^0.2.2",
"ipfs-api": "github:ipfs/js-ipfs-api#2938470",
"ipfs-blob-store": "^0.3.0",
"lru-cache": "^2.5.0",
"registry-static": "github:diasdavid/registry-static",
"ronin": "^0.3.11",
"st": "^1.0.0"
},
"devDependencies": {
"chai": "^3.5.0",
"eslint-config-standard-react": "^1.2.1",
"eslint-plugin-react": "^3.9.0",
"mocha": "^2.2.1",
"mockery": "^1.4.0",
"ipfsd-ctl": "^0.8.1",
"mocha": "^2.4.5",
"ncp": "^2.0.0",
"nock": "^1.2.1",
"pre-commit": "^1.1.2",
"request": "^2.54.0",
"rimraf": "^2.3.2",
"standard": "^5.3.1"
"standard": "^5.3.1",
"wreck": "^7.0.0"
}
}
16 changes: 4 additions & 12 deletions src/cli/commands/daemon.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ module.exports = Command.extend({
type: 'boolean',
default: false
},
ipfs: {
type: 'boolean',
default: true
},
port: {
type: 'number'
},
Expand All @@ -30,17 +26,13 @@ module.exports = Command.extend({
}
},

run: function (folder, blobStore, clone, ipfs, port, host, name) {
if (ipfs) {
console.log('IPFS mode ON')
blobStore = path.resolve(__dirname, '../../ibs.js')
folder = '/npm-registry/'
}
run: function (folder, blobStore, clone, port, host, name) {
blobStore = path.resolve(__dirname, '../../ibs.js')

mirror({
outputDir: folder,
outputDir: '/npm-registry/',
blobStore: blobStore,
clone: clone,
ipfs: ipfs,
port: port,
host: host
})
Expand Down
8 changes: 5 additions & 3 deletions src/cli/commands/ls.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
var Command = require('ronin').Command
var fetchIPNS = require('../../fetch-ipns')
var ipfsAPI = require('ipfs-api')
var logger = require('../../logger')
var debug = require('debug')
var log = debug('registry-mirror')
log.err = debug('registry-mirror:error')

module.exports = Command.extend({
desc: 'Check modules available in the mirror',
Expand All @@ -18,7 +20,7 @@ module.exports = Command.extend({
if (update) {
fetchIPNS({ blobStore: true }, function (err) {
if (err) {
logger.err('Failed to update /npm-registry mDAG node', err)
log.err('Failed to update /npm-registry mDAG node', err)
}
ls()
})
Expand All @@ -32,7 +34,7 @@ function ls () {
var apiCtl = ipfsAPI('/ip4/127.0.0.1/tcp/5001')
apiCtl.files.ls('/npm-registry', function (err, res) {
if (err) {
return logger.err(err)
return log.err(err)
}
res.Entries.forEach(function (module) {
console.log(module.Name, '\t', module.Hash)
Expand Down
18 changes: 10 additions & 8 deletions src/cli/commands/npm/publish.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
var Command = require('ronin').Command
var ipfsAPI = require('ipfs-api')
var logger = require('../../../logger')
var debug = require('debug')
var log = debug('registry-mirror')
log.err = debug('registry-mirror:error')

module.exports = Command.extend({
desc: 'Publish an IPNS record with your current npm list',
Expand All @@ -12,23 +14,23 @@ module.exports = Command.extend({

ctl.files.stat('/npm-registry', function (err, res) {
if (err) {
return logger.error('stat', err)
return log.err('stat', err)
}
ctl.block.get(res.Hash, function (err, stream) {
if (err) {
return logger.error('block get', err)
return log.err('block get', err)
}
ctl.add(stream, function (err, res) {
if (err) {
return logger.error('add', err)
return log.err('add', err)
}
ctl.name.publish('/ipfs/' + res[0].Hash, function (err, res) {
if (err) {
return logger.error('name publish', err)
return log.err('name publish', err)
}
logger.info('Published:')
logger.info('IPNS: ', '/ipns/' + res.Name)
logger.info('IPFS: ', res.Value)
log('Published:')
log('IPNS: ', '/ipns/' + res.Name)
log('IPFS: ', res.Value)
})
})
})
Expand Down
9 changes: 6 additions & 3 deletions src/cli/commands/npm/update.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
var Command = require('ronin').Command
var fetchIPNS = require('../../../fetch-ipns')
var logger = require('../../../logger')
var ipfsAPI = require('ipfs-api')
var debug = require('debug')
var log = debug('registry-mirror')
log.err = debug('registry-mirror:error')

module.exports = Command.extend({
desc: 'Update your npm list of modules from IPNS',
Expand All @@ -23,9 +25,10 @@ module.exports = Command.extend({
}
function result (err, res) {
if (err) {
return logger.error('Failed: ', err)
console.log(err)
return log.err('Failed: ', err)
}
logger.info('Updated local registry list copy:', res)
console.log('updated local registry list copy to:', res)
}
}
})
36 changes: 15 additions & 21 deletions src/clone-npm.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,24 @@
var spawn = require('child_process').spawn
var path = require('path')
var logger = require('./logger')
const spawn = require('child_process').spawn
const path = require('path')
const debug = require('debug')
const log = debug('registry-mirror')
log.err = debug('registry-mirror:error')

exports = module.exports = clone
module.exports = (config) => {
const opts = [
'-o', config.outputDir,
'-d', 'localhost'
]

function clone (config) {
var opts = ['-o', config.outputDir, '-d', 'localhost']
if (config.blobStore) { opts.push('--blobstore=' + config.blobStore) }

// add a custom blob-store
if (config.blobStore) {
opts.push('--blobstore=' + config.blobStore)
}
const rspath = path.resolve(require.resolve('registry-static'), '../../bin/registry-static')
const child = spawn(rspath, opts, { stdio: 'inherit' })

var child = spawn(
path.resolve(
require.resolve('registry-static'), '../../bin/registry-static'),
opts,
{
stdio: 'inherit'
}
)

process.on('SIGINT', function () {
process.on('SIGINT', () => {
child.kill('SIGINT')
process.kill()
})

logger.info('Cloning NPM from https://registry.npmjs.org')
log('cloning NPM from https://registry.npmjs.org')
}
95 changes: 34 additions & 61 deletions src/fetch-ipns.js
Original file line number Diff line number Diff line change
@@ -1,83 +1,56 @@
var logger = require('./logger')
var ipfsAPI = require('ipfs-api')

exports = module.exports = fetchIPNS
const debug = require('debug')
const log = debug('registry-mirror')
const async = require('async')
log.error = debug('registry-mirror:error')
const ipfsAPI = require('ipfs-api')

// TODO update this to enable multiple nodes to be our
var updaterNodes = {
castor: {
addr: '/ip4/37.59.33.238/tcp/4001/ipfs/QmdNc4B89DxVeiuDKRN5bWdKsAPCmekgmJMkRSdUNa7x9z',
ipns: '/ipns/QmdNc4B89DxVeiuDKRN5bWdKsAPCmekgmJMkRSdUNa7x9z'
},
const updaterNodes = {
biham: {
addr: '/ip4/188.40.114.11/tcp/4001/ipfs/QmZY7MtK8ZbG1suwrxc7xEYZ2hQLf1dAWPRHhjxC8rjq8E',
ipns: '/ipns/QmZY7MtK8ZbG1suwrxc7xEYZ2hQLf1dAWPRHhjxC8rjq8E'
}
}

function fetchIPNS (config, callback) {
if (!callback) {
callback = function (err) {
if (err) {
logger.err(err)
}
}
}
exports = module.exports = (config, callback) => {
if (!callback) { callback = (err) => { if (err) { log.err(err) } } }

if (!config.clone && config.blobStore) {
var apiCtl = ipfsAPI('/ip4/127.0.0.1/tcp/5001')
const apiCtl = ipfsAPI('/ip4/127.0.0.1/tcp/5001')

apiCtl.swarm.connect(updaterNodes.biham.addr, function (err) {
if (err) {
return callback(err)
}
apiCtl.swarm.connect(updaterNodes.biham.addr, (err) => {
if (err) { return callback(err) }
copyNpmRegistry(apiCtl, updaterNodes.biham.ipns, callback)
})
}
}

exports.copyNpmRegistry = copyNpmRegistry
function copyNpmRegistry (ctl, ipns, callback) {
mv()

function mv () {
ctl.files.stat('/npm-registry', function (err) {
if (err && err.code === 0) {
return cp()
}

ctl.files.mv(['/npm-registry', '/npm-registry' + Date.now().toString()], function (err) {
if (err) {
return callback(err)
}
cp()
async.waterfall([
(cb) => {
ctl.name.resolve(ipns, (err, res) => {
cb(err, res.Path)
})
})
}

function cp () {
ctl.name.resolve(ipns, function (err, res) {
if (err) {
return callback(err)
}

ctl.cat(res.Path, function (err, stream) {
if (err) {
return callback(err)
}
ctl.block.put(stream, function (err, res) {
if (err) {
return callback(err)
}
ctl.files.cp(['/ipfs/' + res.Key, '/npm-registry'], function (err) {
if (err) {
return callback(err)
}
callback(null, '/ipfs/' + res.Path)
})
})
},
ctl.cat,
(stream, cb) => {
ctl.block.put(stream, (err, res) => {
cb(err, '/ipfs/' + res.Key)
})
})
}
},
(ipfsHash, cb) => {
ctl.files.mv(['/npm-registry', '/npm-registry.bak-' + Date.now().toString()], (err) => {
cb(err, ipfsHash)
})
},
(ipfsHash, cb) => {
ctl.files.cp([ipfsHash, '/npm-registry'], (err) => {
cb(err, ipfsHash)
})
}
], (err, ipfsHash) => {
callback(err, ipfsHash)
})
}

10 changes: 6 additions & 4 deletions src/ibs.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
var ipfsBlobStore = require('ipfs-blob-store')

module.exports = function (baseDir) {
var mfs = require('ipfs-blob-store')()
// console.log('BASEDIR->', baseDir)
mfs.baseDir = baseDir
return mfs
return ipfsBlobStore({
baseDir: baseDir,
flush: true
})
}
Loading