Merge pull request #57 from saul-jb/feat/path-refactor

feat: Remove dependence on path.
This commit is contained in:
Haad 2023-03-29 18:20:11 +03:00 committed by GitHub
commit a027525e5c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 98 additions and 55 deletions

View File

@ -1,10 +1,7 @@
import path from 'path'
import webpack from 'webpack'
import { fileURLToPath } from 'url'
import { createRequire } from 'module'
export default (env, argv) => {
const require = createRequire(import.meta.url)
const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)
@ -24,24 +21,11 @@ export default (env, argv) => {
fs: '{ existsSync: () => true }',
mkdirp: '{}'
},
plugins: [
new webpack.ProvidePlugin({
process: 'process/browser',
Buffer: ['buffer', 'Buffer']
})
],
resolve: {
modules: [
'node_modules',
path.resolve(__dirname, '../node_modules')
],
fallback: {
path: require.resolve('path-browserify'),
os: false,
fs: false,
constants: false,
stream: false
}
]
},
resolveLoader: {
modules: [

View File

@ -1,10 +1,7 @@
import path from 'path'
import webpack from 'webpack'
import { fileURLToPath } from 'url'
import { createRequire } from 'module'
export default (env, argv) => {
const require = createRequire(import.meta.url)
const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)
@ -25,24 +22,11 @@ export default (env, argv) => {
fs: '{ existsSync: () => true }',
mkdirp: '{}'
},
plugins: [
new webpack.ProvidePlugin({
process: 'process/browser',
Buffer: ['buffer', 'Buffer']
})
],
resolve: {
modules: [
'node_modules',
path.resolve(__dirname, '../node_modules')
],
fallback: {
path: require.resolve('path-browserify'),
os: false,
fs: false,
constants: false,
stream: false
}
]
},
resolveLoader: {
modules: [

View File

@ -35,10 +35,7 @@ export default (env, argv) => {
],
fallback: {
path: require.resolve('path-browserify'),
os: false,
fs: false,
constants: false,
stream: false
process: false
}
},
resolveLoader: {

View File

@ -9,7 +9,7 @@ import OrbitDBAddress, { isValidAddress } from './address.js'
import DBManifest from './manifest.js'
import { createId } from './utils/index.js'
// import Logger from 'logplease'
import path from 'path'
import pathJoin from './utils/path-join.js'
import * as Block from 'multiformats/block'
import * as dagCbor from '@ipld/dag-cbor'
import { sha256 } from 'multiformats/hashes/sha2'
@ -46,7 +46,7 @@ const OrbitDB = async ({ ipfs, id, identity, keystore, directory } = {}) => {
id = id || await createId()
const { id: peerId } = await ipfs.id()
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 })
identity = identity || await identities.createIdentity({ id })

View File

@ -1,6 +1,6 @@
import * as Path from 'path'
import { CID } from 'multiformats/cid'
import { base58btc } from 'multiformats/bases/base58'
import { posixJoin } from './utils/path-join.js'
const isValidAddress = (address) => {
address = address.toString()
@ -45,7 +45,7 @@ const OrbitDBAddress = (address) => {
const path = address.replace('/orbitdb/', '').replace('\\orbitdb\\', '')
const toString = () => {
return (Path.posix || Path).join('/', protocol, '/', path)
return posixJoin('/', protocol, path)
}
return {

View File

@ -1,8 +1,8 @@
import { EventEmitter } from 'events'
import PQueue from 'p-queue'
import Path from 'path'
import Sync from './sync.js'
import { ComposedStorage, LRUStorage, IPFSBlockStorage, LevelStorage } from './storage/index.js'
import pathJoin from './utils/path-join.js'
const defaultReferencesCount = 16
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 { Log, Entry } = OpLog
directory = Path.join(directory || './orbitdb', `./${address}/`)
directory = pathJoin(directory || './orbitdb', `./${address}/`)
meta = meta || {}
referencesCount = referencesCount || defaultReferencesCount
@ -21,12 +21,12 @@ const Database = async ({ OpLog, ipfs, identity, address, name, accessController
headsStorage = headsStorage || await ComposedStorage(
await LRUStorage({ size: defaultCacheSize }),
await LevelStorage({ path: Path.join(directory, '/log/_heads/') })
await LevelStorage({ path: pathJoin(directory, '/log/_heads/') })
)
indexStorage = indexStorage || await ComposedStorage(
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 })

View File

@ -1,7 +1,7 @@
import LevelStorage from '../storage/level.js'
import { KeyValue } from './index.js'
import pathJoin from '../utils/path-join.js'
import PQueue from 'p-queue'
import path from 'path'
const valueEncoding = 'json'
@ -11,7 +11,7 @@ const KeyValuePersisted = async ({ OpLog, Database, ipfs, identity, address, nam
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 })
let latestOplogHash

View File

@ -4,10 +4,10 @@ import OrbitDBIdentityProvider from './providers/orbitdb.js'
// import EthIdentityProvider from './identity-providers/ethereum.js'
import KeyStore, { signMessage, verifyMessage } from '../key-store.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 DefaultIdentityKeysPath = Path.join('./orbitdb', 'identities')
const DefaultIdentityKeysPath = pathJoin('./orbitdb', 'identities')
const supportedTypes = {
orbitdb: OrbitDBIdentityProvider

View File

@ -1,5 +1,4 @@
import path from 'path'
import pathJoin from './utils/path-join.js'
import * as Block from 'multiformats/block'
import * as dagCbor from '@ipld/dag-cbor'
import { sha256 } from 'multiformats/hashes/sha2'
@ -20,7 +19,7 @@ export default async (storage, name, type, accessControllerAddress, { meta } = {
{
name,
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 !== undefined ? { meta } : {}

View File

@ -1,8 +1,8 @@
import { pipe } from 'it-pipe'
import PQueue from 'p-queue'
import Path from 'path'
import { EventEmitter } from 'events'
import { TimeoutController } from 'timeout-abort-controller'
import pathJoin from './utils/path-join.js'
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.')
const address = log.id
const headsSyncAddress = Path.join('/orbitdb/heads/', address)
const headsSyncAddress = pathJoin('/orbitdb/heads/', address)
const queue = new PQueue({ concurrency: 1 })
const peers = new Set()

12
src/utils/path-join.js Normal file
View 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
View 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))
}
})
})