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

Commit

Permalink
fix: use os specific path separator
Browse files Browse the repository at this point in the history
  • Loading branch information
dignifiedquire committed May 28, 2017
1 parent 02d60e5 commit d7ec65a
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 65 deletions.
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"aegir": "^11.0.2",
"chai": "^3.5.0",
"dirty-chai": "^1.2.2",
"flow-bin": "^0.46.0"
"flow-bin": "^0.47.0"
},
"dependencies": {
"async": "^2.4.1",
Expand All @@ -55,5 +55,6 @@
"Erin Dachtler <download333@gmail.com>",
"Friedel Ziegelmayer <dignifiedquire@gmail.com>",
"Juan Batiz-Benet <juan@benet.ai>"
]
}
],
"bundleDependencies": []
}
33 changes: 18 additions & 15 deletions src/key.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
const path = require('path')
const uuid = require('uuid/v4')

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

/**
* A Key represents the unique identifier of an object.
* Our Key scheme is inspired by file systems and Google App Engine key model.
Expand Down Expand Up @@ -38,7 +41,7 @@ class Key {
this.clean()
}

if (this._buf.length === 0 || this._buf[0] !== 47) {
if (this._buf.length === 0 || this._buf[0] !== pathSep) {
throw new Error(`Invalid key: ${this.toString()}`)
}
}
Expand Down Expand Up @@ -80,7 +83,7 @@ class Key {
*
*/
static withNamespaces (list /* : Array<string> */) /* : Key */ {
return new Key(list.join('/'))
return new Key(list.join(pathSepS))
}

/**
Expand All @@ -104,18 +107,18 @@ class Key {
*/
clean () {
if (!this._buf || this._buf.length === 0) {
this._buf = new Buffer('/')
}

if (this._buf[0] !== 47 /* / is 47 */) {
this._buf = Buffer.concat([new Buffer('/'), this._buf])
this._buf = new Buffer(pathSepS, 'utf8')
}

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

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

// normalize does not remove trailing slashes
if (this.toString().length > 1) {
this._buf = new Buffer(this.toString().replace(/\/$/, ''))
if (this.toString().length > 1 && this._buf[this._buf.length - 1] === pathSep) {
this._buf = this._buf.slice(0, -1)
}
}

Expand Down Expand Up @@ -194,7 +197,7 @@ class Key {
*
*/
list () /* : Array<string> */ {
return this.toString().split('/').slice(1)
return this.toString().split(pathSepS).slice(1)
}

/**
Expand Down Expand Up @@ -249,7 +252,7 @@ class Key {
*
*/
path () /* : Key */ {
return new Key(this.parent().toString() + '/' + this.type())
return new Key(this.parent().toString() + pathSepS + this.type())
}

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

return new Key(list.slice(0, -1).join('/'))
return new Key(list.slice(0, -1).join(pathSepS))
}

/**
Expand All @@ -283,9 +286,9 @@ class Key {
*
*/
child (key /* : Key */) /* : Key */ {
if (this.toString() === '/') {
if (this.toString() === pathSepS) {
return key
} else if (key.toString() === '/') {
} else if (key.toString() === pathSepS) {
return this
}

Expand Down
22 changes: 12 additions & 10 deletions src/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ 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 @@ -44,7 +46,7 @@ module.exports = (test/* : Test */) => {
beforeEach((done) => {
test.setup((err, s) => {
if (err) {
return done(err)
throw err
}
store = s
done()
Expand Down Expand Up @@ -89,7 +91,7 @@ module.exports = (test/* : Test */) => {
beforeEach((done) => {
test.setup((err, s) => {
if (err) {
return done(err)
throw err
}
store = s
done()
Expand Down Expand Up @@ -119,7 +121,7 @@ module.exports = (test/* : Test */) => {
beforeEach((done) => {
test.setup((err, s) => {
if (err) {
return done(err)
throw err
}
store = s
done()
Expand Down Expand Up @@ -189,7 +191,7 @@ module.exports = (test/* : Test */) => {
beforeEach((done) => {
test.setup((err, s) => {
if (err) {
return done(err)
throw err
}
store = s
done()
Expand Down Expand Up @@ -236,9 +238,9 @@ module.exports = (test/* : Test */) => {
series([
(cb) => b.commit(cb),
(cb) => parallel([
(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))
(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))
], (err, res) => {
expect(err).to.not.exist()
expect(res[0]).to.have.length(count)
Expand Down Expand Up @@ -288,7 +290,7 @@ module.exports = (test/* : Test */) => {

const tests = [
['empty', {}, [hello, world, hello2]],
['prefix', {prefix: '/z'}, [world, hello2]],
['prefix', {prefix: n('/z')}, [world, hello2]],
['1 filter', {filters: [filter1]}, [world, hello2]],
['2 filters', {filters: [filter1, filter2]}, [hello2]],
['limit', {limit: 1}, 1],
Expand All @@ -301,7 +303,7 @@ module.exports = (test/* : Test */) => {
before((done) => {
test.setup((err, s) => {
if (err) {
return done(err)
throw err
}
store = s

Expand Down Expand Up @@ -363,7 +365,7 @@ module.exports = (test/* : Test */) => {
before((done) => {
test.setup((err, s) => {
if (err) {
return done(err)
throw err
}
store = s
done()
Expand Down
82 changes: 45 additions & 37 deletions test/key.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,36 @@ const path = require('path')

const Key = require('../src').Key

const clean = (s) => {
let fixed = path.normalize(s)
if (fixed.length > 1) {
return fixed.replace(/\/$/, '')
}
return fixed
}
const pathSep = path.sep
const n = (p) => path.normalize(p)

describe('Key', () => {
const clean = (s) => {
let fixed = n(s)
if (fixed.startsWith(pathSep + pathSep)) {
fixed = fixed.slice(1)
}
if (fixed.length > 1 && fixed.endsWith(pathSep)) {
fixed = fixed.slice(0, -1)
}

return fixed
}

describe('basic', () => {
const validKey = (s) => it(s, () => {
const fixed = clean('/' + s)
const namespaces = fixed.split('/').slice(1)
const fixed = clean(pathSep + s)
const namespaces = fixed.split(pathSep).slice(1)
const lastNamespace = namespaces[namespaces.length - 1]
const lnparts = lastNamespace.split(':')
let ktype = ''
if (lnparts.length > 1) {
ktype = lnparts.slice(0, -1).join(':')
}
const kname = lnparts[lnparts.length - 1]
const kchild = clean(fixed + '/cchildd')
const kparent = '/' + namespaces.slice(0, -1).join('/')
const kpath = clean(kparent + '/' + ktype)
const kchild = clean(fixed + n('/cchildd'))
const kparent = pathSep + namespaces.slice(0, -1).join(pathSep)
const kpath = clean(kparent + pathSep + ktype)
const kinstance = fixed + ':inst'

const k = new Key(s)
Expand All @@ -55,34 +62,35 @@ describe('Key', () => {
validKey('')
validKey('abcde')
validKey('disahfidsalfhduisaufidsail')
validKey('/fdisahfodisa/fdsa/fdsafdsafdsafdsa/fdsafdsa/')
validKey(n('/fdisahfodisa/fdsa/fdsafdsafdsafdsa/fdsafdsa/'))
validKey('4215432143214321432143214321')
validKey('/fdisaha////fdsa////fdsafdsafdsafdsa/fdsafdsa/')
validKey(n('a\\b\\c//d\\'))
validKey(n('/fdisaha////fdsa////fdsafdsafdsafdsa/fdsafdsa/'))
validKey('abcde:fdsfd')
validKey('disahfidsalfhduisaufidsail:fdsa')
validKey('/fdisahfodisa/fdsa/fdsafdsafdsafdsa/fdsafdsa/:')
validKey(n('/fdisahfodisa/fdsa/fdsafdsafdsafdsa/fdsafdsa/:'))
validKey('4215432143214321432143214321:')
validKey('fdisaha////fdsa////fdsafdsafdsafdsa/fdsafdsa/f:fdaf')
validKey(n('fdisaha////fdsa////fdsafdsafdsafdsa/fdsafdsa/f:fdaf'))
})

it('ancestry', () => {
const k1 = new Key('/A/B/C')
const k2 = new Key('/A/B/C/D')
const k1 = new Key(n('/A/B/C'))
const k2 = new Key(n('/A/B/C/D'))

expect(k1.toString()).to.be.eql('/A/B/C')
expect(k2.toString()).to.be.eql('/A/B/C/D')
expect(k1.toString()).to.be.eql(n('/A/B/C'))
expect(k2.toString()).to.be.eql(n('/A/B/C/D'))

const checks = [
k1.isAncestorOf(k2),
k2.isDecendantOf(k1),
new Key('/A').isAncestorOf(k2),
new Key('/A').isAncestorOf(k1),
!new Key('/A').isDecendantOf(k2),
!new Key('/A').isDecendantOf(k1),
k2.isDecendantOf(new Key('/A')),
k1.isDecendantOf(new Key('/A')),
!k2.isAncestorOf(new Key('/A')),
!k1.isAncestorOf(new Key('/A')),
new Key(n('/A')).isAncestorOf(k2),
new Key(n('/A')).isAncestorOf(k1),
!new Key(n('/A')).isDecendantOf(k2),
!new Key(n('/A')).isDecendantOf(k1),
k2.isDecendantOf(new Key(n('/A'))),
k1.isDecendantOf(new Key(n('/A'))),
!k2.isAncestorOf(new Key(n('/A'))),
!k1.isAncestorOf(new Key(n('/A'))),
!k2.isAncestorOf(k2),
!k1.isAncestorOf(k1)
]
Expand All @@ -95,8 +103,8 @@ describe('Key', () => {
})

it('type', () => {
const k1 = new Key('/A/B/C:c')
const k2 = new Key('/A/B/C:c/D:d')
const k1 = new Key(n('/A/B/C:c'))
const k2 = new Key(n('/A/B/C:c/D:d'))

expect(k1.isAncestorOf(k2)).to.eql(true)
expect(k2.isDecendantOf(k1)).to.eql(true)
Expand Down Expand Up @@ -126,12 +134,12 @@ describe('Key', () => {
expect(bk.less(ak)).to.eql(false)
}

checkLess('/a/b/c', '/a/b/c/d')
checkLess('/a/b', '/a/b/c/d')
checkLess('/a', '/a/b/c/d')
checkLess('/a/a/c', '/a/b/c')
checkLess('/a/a/d', '/a/b/c')
checkLess('/a/b/c/d/e/f/g/h', '/b')
checkLess('/', '/a')
checkLess(n('/a/b/c'), n('/a/b/c/d'))
checkLess(n('/a/b'), n('/a/b/c/d'))
checkLess(n('/a'), n('/a/b/c/d'))
checkLess(n('/a/a/c'), n('/a/b/c'))
checkLess(n('/a/a/d'), n('/a/b/c'))
checkLess(n('/a/b/c/d/e/f/g/h'), n('/b'))
checkLess(pathSep, n('/a'))
})
})

0 comments on commit d7ec65a

Please sign in to comment.