Skip to content

Commit

Permalink
Merge pull request #36 from ipfs/ipld
Browse files Browse the repository at this point in the history
Add configurable extension support
  • Loading branch information
daviddias committed Apr 10, 2016
2 parents 63fd8f8 + 6ec29c0 commit 495a9b9
Show file tree
Hide file tree
Showing 46 changed files with 149 additions and 120 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
test/test-repo-for*

# Logs
logs
*.log
Expand Down Expand Up @@ -26,3 +28,6 @@ build
# Dependency directory
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
node_modules

dist
lib
30 changes: 30 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
test

# Logs
logs
*.log

coverage

# Runtime data
pids
*.pid
*.seed

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# node-waf configuration
.lock-wscript

build

# Dependency directory
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
node_modules
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ before_install:
- npm i -g npm
# Workaround for a permissions issue with Travis virtual machine images
script:
- npm run lint
- npm test

before_script:
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ js-ipfs-repo
[![](https://img.shields.io/badge/freenode-%23ipfs-blue.svg?style=flat-square)](http://webchat.freenode.net/?channels=%23ipfs)
[![Build Status](https://travis-ci.org/ipfs/js-ipfs-repo.svg)](https://travis-ci.org/ipfs/js-ipfs-repo)
![](https://img.shields.io/badge/coverage-90%25-yellow.svg?style=flat-square) [![Dependency Status](https://david-dm.org/diasdavid/js-peer-id.svg?style=flat-square)](https://david-dm.org/ipfs/js-ipfs-repo)
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square)](https://github.com/feross/standard)
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square)](https://github.com/feross/standard) [![dignified.js](https://img.shields.io/badge/uses-dignified.js-blue.svg?style=flat-square)](https://github.com/dignifiedquire/dignified.js)

## Description

Expand Down
7 changes: 7 additions & 0 deletions circle.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
dependencies:
pre:
- npm i -g npm

machine:
node:
version: stable
48 changes: 0 additions & 48 deletions karma.conf.js

This file was deleted.

32 changes: 9 additions & 23 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
"description": "IPFS Repo implementation",
"main": "src/index.js",
"scripts": {
"test": "npm run test:node && npm run test:browser",
"test:node": "mocha tests/node-tests.js",
"test:browser": "karma start karma.conf.js",
"coverage": "istanbul cover --print both -- _mocha tests/node-tests.js",
"lint": "standard"
"test": "dignified-test",
"test:node": "dignified-test node",
"test:browser": "dignified-test browser",
"build": "dignified-build",
"coverage": "dignified-coverage",
"lint": "dignified-lint",
"release": "dignified-release"
},
"repository": {
"type": "git",
Expand All @@ -24,34 +26,18 @@
"homepage": "https://github.com/ipfs/js-ipfs-repo",
"devDependencies": {
"async": "^1.5.2",
"babel-core": "^6.7.2",
"babel-loader": "^6.2.4",
"babel-polyfill": "^6.7.2",
"babel-preset-es2015": "^6.6.0",
"bl": "^1.1.2",
"bs58": "^3.0.0",
"buffer-loader": "^0.0.1",
"chai": "^3.5.0",
"dignified.js": "^1.0.0",
"fs-blob-store": "^5.2.1",
"idb-plus-blob-store": "^1.0.0",
"istanbul": "^0.4.2",
"karma": "^0.13.22",
"karma-chrome-launcher": "^0.2.2",
"karma-cli": "^0.1.2",
"karma-firefox-launcher": "^0.1.7",
"karma-mocha": "^0.2.2",
"karma-phantomjs-launcher": "^1.0.0",
"karma-spec-reporter": "0.0.26",
"karma-webpack": "^1.7.0",
"local-storage-blob-store": "0.0.3",
"lodash": "^4.6.1",
"mocha": "^2.4.5",
"ncp": "^2.0.0",
"phantomjs-prebuilt": "^2.1.6",
"pre-commit": "^1.1.2",
"rimraf": "^2.5.2",
"standard": "^6.0.8",
"webpack": "github:diasdavid/webpack#webpack-1"
"rimraf": "^2.5.2"
},
"dependencies": {
"bl": "^1.1.2",
Expand Down
8 changes: 4 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@

const stores = require('./stores')

exports = module.exports = Repo

function Repo (repoPath, options) {
if (!options) { throw new Error('missing options param') }
if (!options.stores) { throw new Error('missing options.stores param') }

// If options.stores is an abstract-blob-store instead of a map, use it for
// all stores.
if (options.stores.prototype && options.stores.prototype.createWriteSteam) {
var store = options.stores
if (options.stores.prototype && options.stores.prototype.createWriteStream) {
const store = options.stores
options.stores = {
keys: store,
config: store,
Expand Down Expand Up @@ -70,3 +68,5 @@ function Repo (repoPath, options) {
// .logs
// .setUp(repoPath, options.stores.logs, this.locks)
}

exports = module.exports = Repo
48 changes: 32 additions & 16 deletions src/stores/datastore.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,50 @@ const PREFIX_LENGTH = 8

exports = module.exports

function multihashToPath (multihash, extension) {
extension = extension || 'data'
const filename = `${multihash.toString('hex')}.${extension}`
const folder = filename.slice(0, PREFIX_LENGTH)
const path = folder + '/' + filename

return path
}

exports.setUp = (basePath, blobStore, locks) => {
const store = blobStore(basePath + '/blocks')

return {
createReadStream: (multihash) => {
const path = multihashToPath(multihash)
createReadStream: (multihash, extension) => {
const path = multihashToPath(multihash, extension)
return store.createReadStream(path)
},

createWriteStream: (multihash, cb) => {
const path = multihashToPath(multihash)
createWriteStream: (multihash, extension, cb) => {
if (typeof extension === 'function') {
cb = extension
extension = undefined
}

const path = multihashToPath(multihash, extension)
return store.createWriteStream(path, cb)
},
exists: (multihash, cb) => {
const path = multihashToPath(multihash)
exists: (multihash, extension, cb) => {
if (typeof extension === 'function') {
cb = extension
extension = undefined
}

const path = multihashToPath(multihash, extension)
return store.exists(path, cb)
},
remove: (multihash, cb) => {
const path = multihashToPath(multihash)
remove: (multihash, extension, cb) => {
if (typeof extension === 'function') {
cb = extension
extension = undefined
}

const path = multihashToPath(multihash, extension)
return store.remove(path, cb)
}
}
}

function multihashToPath (multihash) {
const filename = multihash.toString('hex') + '.data'
const folder = filename.slice(0, PREFIX_LENGTH)
const path = folder + '/' + filename

return path
}
18 changes: 9 additions & 9 deletions src/stores/locks.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,14 @@ exports.setUp = (basePath, blobStore) => {

return {
lock: function (cb) {
store.exists(lockFile, doesExist)
function createLock () {
store
.createWriteStream(lockFile)
.on('finish', () => {
cb()
})
.end()
}

function doesExist (err, exists) {
if (err) {
Expand All @@ -24,14 +31,7 @@ exports.setUp = (basePath, blobStore) => {
createLock()
}

function createLock () {
store
.createWriteStream(lockFile)
.on('finish', () => {
cb()
})
.end()
}
store.exists(lockFile, doesExist)
},
unlock: (cb) => {
store.remove(lockFile, (err) => {
Expand Down
3 changes: 1 addition & 2 deletions tests/browser-tests.js → test/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

'use strict'

require('babel-polyfill')
const async = require('async')
const store = require('idb-plus-blob-store')
const tests = require('./repo-test')
Expand All @@ -22,7 +21,7 @@ idb.deleteDatabase('ipfs/blocks')
// TODO use arrow funtions again when https://github.com/webpack/webpack/issues/1944 is fixed
describe('IPFS Repo Tests on the Browser', function () {
before(function (done) {
var repoData = []
const repoData = []
repoContext.keys().forEach(function (key) {
repoData.push({
key: key.replace('./', ''),
Expand Down
15 changes: 2 additions & 13 deletions tests/node-tests.js → test/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const IPFSRepo = require('../src')
describe('IPFS Repo Tests on on Node.js', () => {
const testRepoPath = path.join(__dirname, 'test-repo')
const date = Date.now().toString()
const repoPath = testRepoPath + date
const repoPath = testRepoPath + '-for-' + date

before((done) => {
ncp(testRepoPath, repoPath, (err) => {
Expand All @@ -29,17 +29,6 @@ describe('IPFS Repo Tests on on Node.js', () => {
})

const fs = require('fs-blob-store')
const options = {
stores: {
keys: fs,
config: fs,
datastore: fs,
// datastoreLegacy: needs https://github.com/ipfs/js-ipfs-repo/issues/6#issuecomment-164650642
logs: fs,
locks: fs,
version: fs
}
}
const repo = new IPFSRepo(repoPath, options)
const repo = new IPFSRepo(repoPath, {stores: fs})
require('./repo-test')(repo)
})
Loading

0 comments on commit 495a9b9

Please sign in to comment.