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

fix: remove node globals #35

Merged
merged 2 commits into from
Apr 1, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
"coverage-publish": "aegir coverage --provider codecov",
"docs": "aegir docs"
},
"browser": {
"./src/utils.js": "./src/utils.browser.js"
},
"repository": {
"type": "git",
"url": "git+https://github.com/ipfs/interface-datastore.git"
Expand All @@ -38,9 +41,11 @@
"dirty-chai": "^2.0.1"
},
"dependencies": {
"buffer": "^5.5.0",
"class-is": "^1.1.0",
"err-code": "^2.0.0",
"uuid": "^3.2.2"
"iso-random-stream": "^1.1.1",
"nanoid": "^2.1.11"
},
"engines": {
"node": ">=8.0.0",
Expand Down
3 changes: 2 additions & 1 deletion src/key.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict'

const uuid = require('uuid/v4')
const { Buffer } = require('buffer')
const uuid = require('nanoid')
const withIs = require('class-is')

const pathSepS = '/'
Expand Down
9 changes: 5 additions & 4 deletions src/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
/* eslint max-nested-callbacks: ["error", 8] */
'use strict'

const { Buffer } = require('buffer')
const randomBytes = require('iso-random-stream/src/random')
const chai = require('chai')
chai.use(require('dirty-chai'))
const expect = chai.expect
const crypto = require('crypto')

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

Expand Down Expand Up @@ -145,9 +146,9 @@ module.exports = (test) => {
const b = store.batch()
const count = 400
for (let i = 0; i < count; i++) {
b.put(new Key(`/a/hello${i}`), crypto.randomBytes(32))
b.put(new Key(`/q/hello${i}`), crypto.randomBytes(64))
b.put(new Key(`/z/hello${i}`), crypto.randomBytes(128))
b.put(new Key(`/a/hello${i}`), randomBytes(32))
b.put(new Key(`/q/hello${i}`), randomBytes(64))
b.put(new Key(`/z/hello${i}`), randomBytes(128))
}

await b.commit()
Expand Down
53 changes: 53 additions & 0 deletions src/utils.browser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
'use strict'

const uuid = require('nanoid')

exports.filter = (iterable, filterer) => {
return (async function * () {
for await (const value of iterable) {
const keep = await filterer(value)
if (!keep) continue
yield value
}
})()
}

// Not just sort, because the sorter is given all the values and should return
// them all sorted
exports.sortAll = (iterable, sorter) => {
return (async function * () {
let values = []
for await (const value of iterable) values.push(value)
values = await sorter(values)
for (const value of values) yield value
})()
}

exports.take = (iterable, n) => {
return (async function * () {
if (n <= 0) return
let i = 0
for await (const value of iterable) {
yield value
i++
if (i >= n) return
}
})()
}

exports.map = (iterable, mapper) => {
return (async function * () {
for await (const value of iterable) {
yield mapper(value)
}
})()
}

exports.replaceStartWith = function (s, r) {
const matcher = new RegExp('^' + r)
return s.replace(matcher, '')
}

exports.tmpdir = () => {
return `datastore_${uuid()}`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really need to duplicate this whole file just to override the tmpdir function?

}
2 changes: 1 addition & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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

exports.filter = (iterable, filterer) => {
return (async function * () {
Expand Down