Skip to content
This repository was archived by the owner on Sep 9, 2021. It is now read-only.

fix: make datastore OS agnostic (path things) #13

Merged
merged 6 commits into from
Nov 3, 2017
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions .appveyor.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
environment:
matrix:
- nodejs_version: "6"
- nodejs_version: "8"

# cache:
# - node_modules

platform:
- x64

install:
- ps: Install-Product node $env:nodejs_version $env:platform
- npm install

test_script:
- node --version
- npm --version
- npm test

build: off

version: "{build}"
18 changes: 10 additions & 8 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,26 @@ language: node_js

matrix:
include:
- node_js: 4
env: CXX=g++-4.8
- node_js: 6
env:
- SAUCE=true
- CXX=g++-4.8
- node_js: stable
env: CXX=g++-4.8
- node_js: 8
env: CXX=g++-4.8
# - node_js: stable
# env: CXX=g++-4.8

script:
- npm run lint
- npm run flow
- npm test
- npm run test
- npm run coverage
- make test

before_script:
- export DISPLAY=:99.0
- sh -e /etc/init.d/xvfb start

after_success:
- npm run coverage-publish

addons:
firefox: 'latest'
apt:
Expand Down
12 changes: 9 additions & 3 deletions circle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@ machine:
node:
version: stable

test:
post:
- npm run coverage -- --upload

dependencies:
pre:
- google-chrome --version
- wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | sudo apt-key add -
- sudo sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list'
- curl -L -o google-chrome.deb https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
- sudo dpkg -i google-chrome.deb || true
- sudo apt-get update
- sudo apt-get --only-upgrade install google-chrome-stable
- sudo apt-get install -f
- sudo apt-get install --only-upgrade lsb-base
- sudo dpkg -i google-chrome.deb
- google-chrome --version
33 changes: 17 additions & 16 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@
"description": "datastore interface",
"main": "src/index.js",
"scripts": {
"lint": "aegir-lint",
"build": "aegir-build",
"test": "aegir-test",
"lint": "aegir lint",
"build": "aegir build",
"test": "aegir test",
"flow": "flow",
"test:node": "aegir-test --env node",
"test:browser": "aegir-test --env browser",
"release": "aegir-release --docs",
"release-minor": "aegir-release --type minor --docs",
"release-major": "aegir-release --type major --docs",
"coverage": "aegir-coverage",
"coverage-publish": "aegir-coverage publish",
"docs": "aegir-docs"
"test:node": "aegir test --target node",
"test:browser": "aegir test --target browser",
"release": "aegir release --docs",
"release-minor": "aegir release --type minor --docs",
"release-major": "aegir release --type major --docs",
"coverage": "aegir coverage",
"coverage-publish": "aegir coverage --provider codecov",
"docs": "aegir docs"
},
"repository": {
"type": "git",
Expand All @@ -34,16 +34,16 @@
},
"homepage": "https://github.com/ipfs/interface-datastore#readme",
"devDependencies": {
"aegir": "^11.0.2",
"aegir": "^12.1.3",
"chai": "^4.1.2",
"dirty-chai": "^2.0.1",
"flow-bin": "^0.54.1"
"flow-bin": "^0.58.0"
},
"dependencies": {
"async": "^2.5.0",
"libp2p-crypto": "^0.10.3",
"pull-defer": "^0.2.2",
"pull-stream": "^3.6.0",
"pull-stream": "^3.6.1",
"uuid": "^3.1.0"
},
"engines": {
Expand All @@ -54,7 +54,8 @@
"David Dias <[email protected]>",
"Erin Dachtler <[email protected]>",
"Juan Batiz-Benet <[email protected]>",
"dignifiedquire <[email protected]>"
"dignifiedquire <[email protected]>",
"Richard Schneider <[email protected]>"
],
"bundleDependencies": []
}
}
25 changes: 14 additions & 11 deletions src/key.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/* @flow */
'use strict'

const path = require('path')
const uuid = require('uuid/v4')

const pathSepS = path.sep
const pathSep = new Buffer(pathSepS, 'utf8')[0]
const pathSepS = '/'
const pathSepB = Buffer.from(pathSepS)
const pathSep = pathSepB[0]

/**
* A Key represents the unique identifier of an object.
Expand All @@ -28,7 +28,7 @@ class Key {

constructor (s /* : string|Buffer */, clean /* : ?bool */) {
if (typeof s === 'string') {
this._buf = new Buffer(s)
this._buf = Buffer.from(s)
} else if (Buffer.isBuffer(s)) {
this._buf = s
}
Expand Down Expand Up @@ -107,17 +107,15 @@ class Key {
*/
clean () {
if (!this._buf || this._buf.length === 0) {
this._buf = new Buffer(pathSepS, 'utf8')
this._buf = Buffer.from(pathSepS)
}

this._buf = new Buffer(path.normalize(this.toString()))

if (this._buf[0] !== pathSep) {
this._buf = Buffer.concat([new Buffer(pathSepS, 'utf8'), this._buf])
this._buf = Buffer.concat([pathSepB, this._buf])
}

// normalize does not remove trailing slashes
if (this.toString().length > 1 && this._buf[this._buf.length - 1] === pathSep) {
while (this._buf.length > 1 && this._buf[this._buf.length - 1] === pathSep) {
this._buf = this._buf.slice(0, -1)
}
}
Expand Down Expand Up @@ -252,7 +250,12 @@ class Key {
*
*/
path () /* : Key */ {
return new Key(this.parent().toString() + pathSepS + this.type())
let p = this.parent().toString()
if (!p.endsWith(pathSepS)) {
p += pathSepS
}
p += this.type()
return new Key(p)
}

/**
Expand All @@ -268,7 +271,7 @@ class Key {
parent () /* : Key */ {
const list = this.list()
if (list.length === 1) {
return new Key(pathSepS, false)
return new Key(pathSepS)
}

return new Key(list.slice(0, -1).join(pathSepS))
Expand Down
39 changes: 19 additions & 20 deletions src/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@ const parallel = require('async/parallel')
const map = require('async/map')
const each = require('async/each')
const crypto = require('libp2p-crypto')
const path = require('path')

const Key = require('../src').Key
const n = (p) => path.normalize(p)

/* ::
import type {Datastore, Callback} from '../src'
Expand Down Expand Up @@ -59,13 +57,13 @@ module.exports = (test/* : Test */) => {

it('simple', (done) => {
const k = new Key('/z/one')
check(store).put(k, new Buffer('one'), done)
check(store).put(k, Buffer.from('one'), done)
})

it('parallel', (done) => {
const data = []
for (let i = 0; i < 100; i++) {
data.push([new Key(`/z/key${i}`), new Buffer(`data${i}`)])
data.push([new Key(`/z/key${i}`), Buffer.from(`data${i}`)])
}

each(data, (d, cb) => {
Expand Down Expand Up @@ -105,10 +103,10 @@ module.exports = (test/* : Test */) => {
it('simple', (done) => {
const k = new Key('/z/one')
series([
(cb) => check(store).put(k, new Buffer('hello'), cb),
(cb) => check(store).put(k, Buffer.from('hello'), cb),
(cb) => check(store).get(k, (err, res) => {
expect(err).to.not.exist()
expect(res).to.be.eql(new Buffer('hello'))
expect(res).to.be.eql(Buffer.from('hello'))
cb()
})
], done)
Expand All @@ -135,10 +133,10 @@ module.exports = (test/* : Test */) => {
it('simple', (done) => {
const k = new Key('/z/one')
series([
(cb) => check(store).put(k, new Buffer('hello'), cb),
(cb) => check(store).put(k, Buffer.from('hello'), cb),
(cb) => check(store).get(k, (err, res) => {
expect(err).to.not.exist()
expect(res).to.be.eql(new Buffer('hello'))
expect(res).to.be.eql(Buffer.from('hello'))
cb()
}),
(cb) => check(store).delete(k, cb),
Expand All @@ -153,7 +151,7 @@ module.exports = (test/* : Test */) => {
it('parallel', (done) => {
const data = []
for (let i = 0; i < 100; i++) {
data.push([new Key(`/a/key${i}`), new Buffer(`data${i}`)])
data.push([new Key(`/a/key${i}`), Buffer.from(`data${i}`)])
}

series([
Expand Down Expand Up @@ -206,11 +204,11 @@ module.exports = (test/* : Test */) => {
const b = check(store).batch()

series([
(cb) => check(store).put(new Key('/z/old'), new Buffer('old'), cb),
(cb) => check(store).put(new Key('/z/old'), Buffer.from('old'), cb),
(cb) => {
b.put(new Key('/a/one'), new Buffer('1'))
b.put(new Key('/q/two'), new Buffer('2'))
b.put(new Key('/q/three'), new Buffer('3'))
b.put(new Key('/a/one'), Buffer.from('1'))
b.put(new Key('/q/two'), Buffer.from('2'))
b.put(new Key('/q/three'), Buffer.from('3'))
b.delete(new Key('/z/old'))
b.commit(cb)
},
Expand Down Expand Up @@ -238,9 +236,9 @@ module.exports = (test/* : Test */) => {
series([
(cb) => b.commit(cb),
(cb) => parallel([
(cb) => pull(check(store).query({prefix: n('/a')}), pull.collect(cb)),
(cb) => pull(check(store).query({prefix: n('/z')}), pull.collect(cb)),
(cb) => pull(check(store).query({prefix: n('/q')}), pull.collect(cb))
(cb) => pull(check(store).query({prefix: '/a'}), pull.collect(cb)),
(cb) => pull(check(store).query({prefix: '/z'}), pull.collect(cb)),
(cb) => pull(check(store).query({prefix: '/q'}), pull.collect(cb))
], (err, res) => {
expect(err).to.not.exist()
expect(res[0]).to.have.length(count)
Expand All @@ -254,9 +252,9 @@ module.exports = (test/* : Test */) => {

describe('query', () => {
let store
const hello = {key: new Key('/q/1hello'), value: new Buffer('1')}
const world = {key: new Key('/z/2world'), value: new Buffer('2')}
const hello2 = {key: new Key('/z/3hello2'), value: new Buffer('3')}
const hello = {key: new Key('/q/1hello'), value: Buffer.from('1')}
const world = {key: new Key('/z/2world'), value: Buffer.from('2')}
const hello2 = {key: new Key('/z/3hello2'), value: Buffer.from('3')}
const filter1 = (entry, cb) => {
cb(null, !entry.key.toString().endsWith('hello'))
}
Expand Down Expand Up @@ -290,7 +288,7 @@ module.exports = (test/* : Test */) => {

const tests = [
['empty', {}, [hello, world, hello2]],
['prefix', {prefix: n('/z')}, [world, hello2]],
['prefix', {prefix: '/z'}, [world, hello2]],
['1 filter', {filters: [filter1]}, [world, hello2]],
['2 filters', {filters: [filter1, filter2]}, [hello2]],
['limit', {limit: 1}, 1],
Expand Down Expand Up @@ -329,6 +327,7 @@ module.exports = (test/* : Test */) => {
const expected = t[2]
if (Array.isArray(expected)) {
if (t[1].orders == null) {
expect(res).to.have.length(expected.length)
const s = (a, b) => {
if (a.key.toString() < b.key.toString()) {
return 1
Expand Down
Loading