mirror of
https://github.com/orbitdb/orbitdb.git
synced 2025-06-07 06:36:38 +00:00
Merge pull request #57 from saul-jb/feat/path-refactor
feat: Remove dependence on path.
This commit is contained in:
commit
a027525e5c
@ -1,10 +1,7 @@
|
|||||||
import path from 'path'
|
import path from 'path'
|
||||||
import webpack from 'webpack'
|
|
||||||
import { fileURLToPath } from 'url'
|
import { fileURLToPath } from 'url'
|
||||||
import { createRequire } from 'module'
|
|
||||||
|
|
||||||
export default (env, argv) => {
|
export default (env, argv) => {
|
||||||
const require = createRequire(import.meta.url)
|
|
||||||
const __filename = fileURLToPath(import.meta.url)
|
const __filename = fileURLToPath(import.meta.url)
|
||||||
const __dirname = path.dirname(__filename)
|
const __dirname = path.dirname(__filename)
|
||||||
|
|
||||||
@ -24,24 +21,11 @@ export default (env, argv) => {
|
|||||||
fs: '{ existsSync: () => true }',
|
fs: '{ existsSync: () => true }',
|
||||||
mkdirp: '{}'
|
mkdirp: '{}'
|
||||||
},
|
},
|
||||||
plugins: [
|
|
||||||
new webpack.ProvidePlugin({
|
|
||||||
process: 'process/browser',
|
|
||||||
Buffer: ['buffer', 'Buffer']
|
|
||||||
})
|
|
||||||
],
|
|
||||||
resolve: {
|
resolve: {
|
||||||
modules: [
|
modules: [
|
||||||
'node_modules',
|
'node_modules',
|
||||||
path.resolve(__dirname, '../node_modules')
|
path.resolve(__dirname, '../node_modules')
|
||||||
],
|
]
|
||||||
fallback: {
|
|
||||||
path: require.resolve('path-browserify'),
|
|
||||||
os: false,
|
|
||||||
fs: false,
|
|
||||||
constants: false,
|
|
||||||
stream: false
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
resolveLoader: {
|
resolveLoader: {
|
||||||
modules: [
|
modules: [
|
||||||
|
@ -1,10 +1,7 @@
|
|||||||
import path from 'path'
|
import path from 'path'
|
||||||
import webpack from 'webpack'
|
|
||||||
import { fileURLToPath } from 'url'
|
import { fileURLToPath } from 'url'
|
||||||
import { createRequire } from 'module'
|
|
||||||
|
|
||||||
export default (env, argv) => {
|
export default (env, argv) => {
|
||||||
const require = createRequire(import.meta.url)
|
|
||||||
const __filename = fileURLToPath(import.meta.url)
|
const __filename = fileURLToPath(import.meta.url)
|
||||||
const __dirname = path.dirname(__filename)
|
const __dirname = path.dirname(__filename)
|
||||||
|
|
||||||
@ -25,24 +22,11 @@ export default (env, argv) => {
|
|||||||
fs: '{ existsSync: () => true }',
|
fs: '{ existsSync: () => true }',
|
||||||
mkdirp: '{}'
|
mkdirp: '{}'
|
||||||
},
|
},
|
||||||
plugins: [
|
|
||||||
new webpack.ProvidePlugin({
|
|
||||||
process: 'process/browser',
|
|
||||||
Buffer: ['buffer', 'Buffer']
|
|
||||||
})
|
|
||||||
],
|
|
||||||
resolve: {
|
resolve: {
|
||||||
modules: [
|
modules: [
|
||||||
'node_modules',
|
'node_modules',
|
||||||
path.resolve(__dirname, '../node_modules')
|
path.resolve(__dirname, '../node_modules')
|
||||||
],
|
]
|
||||||
fallback: {
|
|
||||||
path: require.resolve('path-browserify'),
|
|
||||||
os: false,
|
|
||||||
fs: false,
|
|
||||||
constants: false,
|
|
||||||
stream: false
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
resolveLoader: {
|
resolveLoader: {
|
||||||
modules: [
|
modules: [
|
||||||
|
@ -35,10 +35,7 @@ export default (env, argv) => {
|
|||||||
],
|
],
|
||||||
fallback: {
|
fallback: {
|
||||||
path: require.resolve('path-browserify'),
|
path: require.resolve('path-browserify'),
|
||||||
os: false,
|
process: false
|
||||||
fs: false,
|
|
||||||
constants: false,
|
|
||||||
stream: false
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
resolveLoader: {
|
resolveLoader: {
|
||||||
|
@ -9,7 +9,7 @@ import OrbitDBAddress, { isValidAddress } from './address.js'
|
|||||||
import DBManifest from './manifest.js'
|
import DBManifest from './manifest.js'
|
||||||
import { createId } from './utils/index.js'
|
import { createId } from './utils/index.js'
|
||||||
// import Logger from 'logplease'
|
// import Logger from 'logplease'
|
||||||
import path from 'path'
|
import pathJoin from './utils/path-join.js'
|
||||||
import * as Block from 'multiformats/block'
|
import * as Block from 'multiformats/block'
|
||||||
import * as dagCbor from '@ipld/dag-cbor'
|
import * as dagCbor from '@ipld/dag-cbor'
|
||||||
import { sha256 } from 'multiformats/hashes/sha2'
|
import { sha256 } from 'multiformats/hashes/sha2'
|
||||||
@ -46,7 +46,7 @@ const OrbitDB = async ({ ipfs, id, identity, keystore, directory } = {}) => {
|
|||||||
id = id || await createId()
|
id = id || await createId()
|
||||||
const { id: peerId } = await ipfs.id()
|
const { id: peerId } = await ipfs.id()
|
||||||
directory = directory || './orbitdb'
|
directory = directory || './orbitdb'
|
||||||
keystore = keystore || await KeyStore({ path: path.join(directory, './keystore') })
|
keystore = keystore || await KeyStore({ path: pathJoin(directory, './keystore') })
|
||||||
const identities = await Identities({ ipfs, keystore })
|
const identities = await Identities({ ipfs, keystore })
|
||||||
identity = identity || await identities.createIdentity({ id })
|
identity = identity || await identities.createIdentity({ id })
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import * as Path from 'path'
|
|
||||||
import { CID } from 'multiformats/cid'
|
import { CID } from 'multiformats/cid'
|
||||||
import { base58btc } from 'multiformats/bases/base58'
|
import { base58btc } from 'multiformats/bases/base58'
|
||||||
|
import { posixJoin } from './utils/path-join.js'
|
||||||
|
|
||||||
const isValidAddress = (address) => {
|
const isValidAddress = (address) => {
|
||||||
address = address.toString()
|
address = address.toString()
|
||||||
@ -45,7 +45,7 @@ const OrbitDBAddress = (address) => {
|
|||||||
const path = address.replace('/orbitdb/', '').replace('\\orbitdb\\', '')
|
const path = address.replace('/orbitdb/', '').replace('\\orbitdb\\', '')
|
||||||
|
|
||||||
const toString = () => {
|
const toString = () => {
|
||||||
return (Path.posix || Path).join('/', protocol, '/', path)
|
return posixJoin('/', protocol, path)
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
import { EventEmitter } from 'events'
|
import { EventEmitter } from 'events'
|
||||||
import PQueue from 'p-queue'
|
import PQueue from 'p-queue'
|
||||||
import Path from 'path'
|
|
||||||
import Sync from './sync.js'
|
import Sync from './sync.js'
|
||||||
import { ComposedStorage, LRUStorage, IPFSBlockStorage, LevelStorage } from './storage/index.js'
|
import { ComposedStorage, LRUStorage, IPFSBlockStorage, LevelStorage } from './storage/index.js'
|
||||||
|
import pathJoin from './utils/path-join.js'
|
||||||
|
|
||||||
const defaultReferencesCount = 16
|
const defaultReferencesCount = 16
|
||||||
const defaultCacheSize = 1000
|
const defaultCacheSize = 1000
|
||||||
@ -10,7 +10,7 @@ const defaultCacheSize = 1000
|
|||||||
const Database = async ({ OpLog, ipfs, identity, address, name, accessController, directory, meta, headsStorage, entryStorage, indexStorage, referencesCount, syncAutomatically }) => {
|
const Database = async ({ OpLog, ipfs, identity, address, name, accessController, directory, meta, headsStorage, entryStorage, indexStorage, referencesCount, syncAutomatically }) => {
|
||||||
const { Log, Entry } = OpLog
|
const { Log, Entry } = OpLog
|
||||||
|
|
||||||
directory = Path.join(directory || './orbitdb', `./${address}/`)
|
directory = pathJoin(directory || './orbitdb', `./${address}/`)
|
||||||
meta = meta || {}
|
meta = meta || {}
|
||||||
referencesCount = referencesCount || defaultReferencesCount
|
referencesCount = referencesCount || defaultReferencesCount
|
||||||
|
|
||||||
@ -21,12 +21,12 @@ const Database = async ({ OpLog, ipfs, identity, address, name, accessController
|
|||||||
|
|
||||||
headsStorage = headsStorage || await ComposedStorage(
|
headsStorage = headsStorage || await ComposedStorage(
|
||||||
await LRUStorage({ size: defaultCacheSize }),
|
await LRUStorage({ size: defaultCacheSize }),
|
||||||
await LevelStorage({ path: Path.join(directory, '/log/_heads/') })
|
await LevelStorage({ path: pathJoin(directory, '/log/_heads/') })
|
||||||
)
|
)
|
||||||
|
|
||||||
indexStorage = indexStorage || await ComposedStorage(
|
indexStorage = indexStorage || await ComposedStorage(
|
||||||
await LRUStorage({ size: defaultCacheSize }),
|
await LRUStorage({ size: defaultCacheSize }),
|
||||||
await LevelStorage({ path: Path.join(directory, '/log/_index/') })
|
await LevelStorage({ path: pathJoin(directory, '/log/_index/') })
|
||||||
)
|
)
|
||||||
|
|
||||||
const log = await Log(identity, { logId: address, access: accessController, entryStorage, headsStorage, indexStorage })
|
const log = await Log(identity, { logId: address, access: accessController, entryStorage, headsStorage, indexStorage })
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
import LevelStorage from '../storage/level.js'
|
import LevelStorage from '../storage/level.js'
|
||||||
import { KeyValue } from './index.js'
|
import { KeyValue } from './index.js'
|
||||||
|
import pathJoin from '../utils/path-join.js'
|
||||||
import PQueue from 'p-queue'
|
import PQueue from 'p-queue'
|
||||||
import path from 'path'
|
|
||||||
|
|
||||||
const valueEncoding = 'json'
|
const valueEncoding = 'json'
|
||||||
|
|
||||||
@ -11,7 +11,7 @@ const KeyValuePersisted = async ({ OpLog, Database, ipfs, identity, address, nam
|
|||||||
|
|
||||||
const queue = new PQueue({ concurrency: 1 })
|
const queue = new PQueue({ concurrency: 1 })
|
||||||
|
|
||||||
directory = path.join(directory || './orbitdb', `./${address}/_index/`)
|
directory = pathJoin(directory || './orbitdb', `./${address}/_index/`)
|
||||||
const index = await LevelStorage({ path: directory, valueEncoding })
|
const index = await LevelStorage({ path: directory, valueEncoding })
|
||||||
|
|
||||||
let latestOplogHash
|
let latestOplogHash
|
||||||
|
@ -4,10 +4,10 @@ import OrbitDBIdentityProvider from './providers/orbitdb.js'
|
|||||||
// import EthIdentityProvider from './identity-providers/ethereum.js'
|
// import EthIdentityProvider from './identity-providers/ethereum.js'
|
||||||
import KeyStore, { signMessage, verifyMessage } from '../key-store.js'
|
import KeyStore, { signMessage, verifyMessage } from '../key-store.js'
|
||||||
import { LRUStorage, IPFSBlockStorage, MemoryStorage, ComposedStorage } from '../storage/index.js'
|
import { LRUStorage, IPFSBlockStorage, MemoryStorage, ComposedStorage } from '../storage/index.js'
|
||||||
import Path from 'path'
|
import pathJoin from '../utils/path-join.js'
|
||||||
|
|
||||||
const DefaultProviderType = 'orbitdb'
|
const DefaultProviderType = 'orbitdb'
|
||||||
const DefaultIdentityKeysPath = Path.join('./orbitdb', 'identities')
|
const DefaultIdentityKeysPath = pathJoin('./orbitdb', 'identities')
|
||||||
|
|
||||||
const supportedTypes = {
|
const supportedTypes = {
|
||||||
orbitdb: OrbitDBIdentityProvider
|
orbitdb: OrbitDBIdentityProvider
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
import path from 'path'
|
import pathJoin from './utils/path-join.js'
|
||||||
|
|
||||||
import * as Block from 'multiformats/block'
|
import * as Block from 'multiformats/block'
|
||||||
import * as dagCbor from '@ipld/dag-cbor'
|
import * as dagCbor from '@ipld/dag-cbor'
|
||||||
import { sha256 } from 'multiformats/hashes/sha2'
|
import { sha256 } from 'multiformats/hashes/sha2'
|
||||||
@ -20,7 +19,7 @@ export default async (storage, name, type, accessControllerAddress, { meta } = {
|
|||||||
{
|
{
|
||||||
name,
|
name,
|
||||||
type,
|
type,
|
||||||
accessController: (path.posix || path).join('/ipfs', accessControllerAddress)
|
accessController: pathJoin('/ipfs', accessControllerAddress)
|
||||||
},
|
},
|
||||||
// meta field is only added to manifest if meta parameter is defined
|
// meta field is only added to manifest if meta parameter is defined
|
||||||
meta !== undefined ? { meta } : {}
|
meta !== undefined ? { meta } : {}
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
import { pipe } from 'it-pipe'
|
import { pipe } from 'it-pipe'
|
||||||
import PQueue from 'p-queue'
|
import PQueue from 'p-queue'
|
||||||
import Path from 'path'
|
|
||||||
import { EventEmitter } from 'events'
|
import { EventEmitter } from 'events'
|
||||||
import { TimeoutController } from 'timeout-abort-controller'
|
import { TimeoutController } from 'timeout-abort-controller'
|
||||||
|
import pathJoin from './utils/path-join.js'
|
||||||
|
|
||||||
const DefaultTimeout = 30000 // 30 seconds
|
const DefaultTimeout = 30000 // 30 seconds
|
||||||
|
|
||||||
@ -46,7 +46,7 @@ const Sync = async ({ ipfs, log, events, onSynced, start, timeout }) => {
|
|||||||
if (!log) throw new Error('An instance of log is required.')
|
if (!log) throw new Error('An instance of log is required.')
|
||||||
|
|
||||||
const address = log.id
|
const address = log.id
|
||||||
const headsSyncAddress = Path.join('/orbitdb/heads/', address)
|
const headsSyncAddress = pathJoin('/orbitdb/heads/', address)
|
||||||
|
|
||||||
const queue = new PQueue({ concurrency: 1 })
|
const queue = new PQueue({ concurrency: 1 })
|
||||||
const peers = new Set()
|
const peers = new Set()
|
||||||
|
12
src/utils/path-join.js
Normal file
12
src/utils/path-join.js
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
export const posixJoin = (...paths) => paths
|
||||||
|
.join('/')
|
||||||
|
.replace(/((?<=\/)\/+)|(^\.\/)|((?<=\/)\.\/)/g, '') || '.'
|
||||||
|
|
||||||
|
export const win32Join = (...paths) => paths
|
||||||
|
.join('\\')
|
||||||
|
.replace(/\//g, '\\')
|
||||||
|
.replace(/((?<=\\)\\+)|(^\.\\)|((?<=\\)\.\\)/g, '') || '.'
|
||||||
|
|
||||||
|
export const join = posixJoin
|
||||||
|
|
||||||
|
export default posixJoin
|
67
test/path-join.spec.js
Normal file
67
test/path-join.spec.js
Normal file
@ -0,0 +1,67 @@
|
|||||||
|
import { equal } from 'assert'
|
||||||
|
import Path from 'path'
|
||||||
|
import { join, posixJoin, win32Join } from '../src/utils/path-join.js'
|
||||||
|
|
||||||
|
const createTestData = s => [
|
||||||
|
[],
|
||||||
|
[''],
|
||||||
|
[s],
|
||||||
|
[s, 'test'],
|
||||||
|
['test'],
|
||||||
|
['test', s],
|
||||||
|
[`test${s}`, s],
|
||||||
|
[s, `${s}test`],
|
||||||
|
[`test${s}`, s, `${s}${s}`, `${s}a`],
|
||||||
|
[`test${s}`, '.', `${s}a`],
|
||||||
|
[`test${s}.${s}a`],
|
||||||
|
['test', s, '.', s],
|
||||||
|
['/test', '\\', 'mixed'],
|
||||||
|
['\\test', '/', 'mixed'],
|
||||||
|
['test', '/', 'mixed', '\\', 'data'],
|
||||||
|
['test', '\\', 'mixed', '/', 'data']
|
||||||
|
]
|
||||||
|
|
||||||
|
const posixTestData = createTestData('/')
|
||||||
|
const win32TestData = createTestData('\\')
|
||||||
|
|
||||||
|
;(Path.posix != null ? describe : describe.skip)('Path posix join', () => {
|
||||||
|
it('gives the same results as \'path\' using posix join on posix paths', () => {
|
||||||
|
for (const data of posixTestData) {
|
||||||
|
equal(Path.posix.join(...data), posixJoin(...data))
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
it('gives the same results as \'path\' using posix join on win32 paths', () => {
|
||||||
|
for (const data of win32TestData) {
|
||||||
|
equal(Path.posix.join(...data), posixJoin(...data))
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
;(Path.win32 != null ? describe : describe.skip)('Path win32 join', () => {
|
||||||
|
it('gives the same results as \'path\' using win32 join on posix paths', () => {
|
||||||
|
for (const data of posixTestData) {
|
||||||
|
equal(Path.win32.join(...data), win32Join(...data))
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
it('gives the same results as \'path\' using win32 join on win32 paths', () => {
|
||||||
|
for (const data of win32TestData) {
|
||||||
|
equal(Path.win32.join(...data), win32Join(...data))
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('Path join', () => {
|
||||||
|
it('gives the same results as \'path\' using join on posix paths', () => {
|
||||||
|
for (const data of posixTestData) {
|
||||||
|
equal(Path.join(...data), join(...data))
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
it('gives the same results as \'path\' using join on win32 paths', () => {
|
||||||
|
for (const data of win32TestData) {
|
||||||
|
equal(Path.join(...data), join(...data))
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
Loading…
x
Reference in New Issue
Block a user