Skip to content

Commit 75ae177

Browse files
authored
chore: add exports for files (#343)
No more deep imports, instead export files. This means browsers don't accidentally import node's `fs` module via `FSLock`, etc.
1 parent ec8239b commit 75ae177

23 files changed

+110
-126
lines changed

packages/ipfs-repo/README.md

+9-27
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,10 @@ This is the implementation of the [IPFS repo spec](https://github.com/ipfs/specs
2121
- [Background](#background)
2222
- [Install](#install)
2323
- [npm](#npm)
24-
- [Use in Node.js](#use-in-nodejs)
25-
- [Use in a browser with browserify, webpack or any other bundler](#use-in-a-browser-with-browserify-webpack-or-any-other-bundler)
26-
- [Use in a browser Using a script tag](#use-in-a-browser-using-a-script-tag)
2724
- [Usage](#usage)
2825
- [API](#api)
2926
- [Setup](#setup)
30-
- [`new Repo(path[, options])`](#new-repopath-options)
27+
- [`createRepo(path[, options])`](#createrepopath-options)
3128
- [`Promise repo.init()`](#promise-repoinit)
3229
- [`Promise repo.open()`](#promise-repoopen)
3330
- [`Promise repo.close()`](#promise-repoclose)
@@ -117,33 +114,18 @@ This provides a well defined interface for creating and interacting with an IPFS
117114
> npm install ipfs-repo
118115
```
119116

120-
### Use in Node.js
121-
122-
```js
123-
var IPFSRepo from 'ipfs-repo')
124-
```
125-
126-
### Use in a browser with browserify, webpack or any other bundler
127-
128117
```js
129-
var IPFSRepo from 'ipfs-repo')
130-
```
131-
132-
### Use in a browser Using a script tag
133-
134-
Loading this module through a script tag will make the `IpfsRepo` obj available in the global namespace.
135-
136-
```html
137-
<script src="https://unpkg.com/ipfs-repo/dist/index.min.js"></script>
118+
import { createRepo } from 'ipfs-repo'
138119
```
139120

140121
## Usage
141122

142123
Example:
143124

144125
```js
145-
const Repo from 'ipfs-repo')
146-
const repo = new Repo('/tmp/ipfs-repo')
126+
import { createRepo } from 'ipfs-repo'
127+
128+
const repo = createRepo('/tmp/ipfs-repo')
147129

148130
await repo.init({ cool: 'config' })
149131
await repo.open()
@@ -166,7 +148,7 @@ This now has created the following structure, either on disk or as an in memory
166148

167149
### Setup
168150

169-
#### `new Repo(path[, options])`
151+
#### `createRepo(path[, options])`
170152

171153
Creates an IPFS Repo.
172154

@@ -184,7 +166,7 @@ Arguments:
184166
* `datastore` (defaults to [`datastore-level`](https://github.com/ipfs/js-datastore-level#readme)). Defines the back-end type used as the key-value store used for gets and puts of values at `repo.datastore`.
185167

186168
```js
187-
const repo = new Repo('path/to/repo')
169+
const repo = createRepo('path/to/repo')
188170
```
189171

190172
#### `Promise repo.init()`
@@ -367,8 +349,8 @@ Returned promise resolves to an `Object` with the following keys:
367349
IPFS Repo comes with two built in locks: memory and fs. These can be imported via the following:
368350

369351
```js
370-
const fsLock from 'ipfs-repo/src/lock') // Default in Node.js
371-
const memoryLock from 'ipfs-repo/src/lock-memory') // Default in browser
352+
import { FSLock } from 'ipfs-repo/locks/fs' // Default in Node.js
353+
import { MemoryLock } from 'ipfs-repo/locks/memory' // Default in browser
372354
```
373355

374356
You can also provide your own custom Lock. It must be an object with the following interface:

packages/ipfs-repo/package.json

+41-5
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,21 @@
33
"version": "13.0.2",
44
"description": "IPFS Repo implementation",
55
"leadMaintainer": "Alex Potsides <[email protected]>",
6+
"type": "module",
67
"main": "src/index.js",
78
"types": "types/src/index.d.ts",
8-
"type": "module",
9+
"typesVersions": {
10+
"*": {
11+
"*": [
12+
"types/*",
13+
"types/src/*"
14+
],
15+
"types/*": [
16+
"types/*",
17+
"types/src/*"
18+
]
19+
}
20+
},
921
"files": [
1022
"*",
1123
"!**/*.tsbuildinfo"
@@ -19,11 +31,35 @@
1931
"publishConfig": {
2032
"directory": "dist"
2133
},
34+
"exports": {
35+
".": {
36+
"import": "./src/index.js"
37+
},
38+
"./constants": {
39+
"import": "./src/constants.js"
40+
},
41+
"./errors": {
42+
"import": "./src/errors.js"
43+
},
44+
"./pin-types": {
45+
"import": "./src/pin-types.js"
46+
},
47+
"./locks/memory": {
48+
"import": "./src/locks/memory.js"
49+
},
50+
"./locks/fs": {
51+
"browser": "./src/locks/memory.js",
52+
"import": "./src/locks/fs.js"
53+
},
54+
"./utils/blockstore": {
55+
"import": "./src/utils/blockstore.js"
56+
},
57+
"./utils/level": {
58+
"import": "./src/utils/level.js"
59+
}
60+
},
2261
"browser": {
23-
"rimraf": false,
24-
"datastore-fs": "datastore-level",
25-
"./src/locks/fs.js": "./src/locks/memory.js",
26-
"./src/default-options.js": "./src/default-options.browser.js"
62+
"rimraf": false
2763
},
2864
"scripts": {
2965
"clean": "rimraf dist types",

packages/ipfs-repo/src/config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import PQueue from 'p-queue'
44
import _get from 'just-safe-get'
55
import _set from 'just-safe-set'
66
import errCode from 'err-code'
7-
import { NotFoundError } from './errors/index.js'
7+
import { NotFoundError } from './errors.js'
88
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
99
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
1010
import { getWithFallback, hasWithFallback } from './utils/level.js'

packages/ipfs-repo/src/default-options.browser.js

-9
This file was deleted.
+2-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import * as FsLock from './locks/fs.js'
2-
3-
// Default configuration for a repo in node.js
1+
import { MemoryLock } from './locks/memory.js'
42

53
/**
64
* @type {Partial<import('./types').Options>}
@@ -9,5 +7,5 @@ export default {
97
autoMigrate: true,
108
onMigrationProgress: () => {},
119
repoOwner: true,
12-
repoLock: FsLock
10+
repoLock: MemoryLock
1311
}

packages/ipfs-repo/src/gc.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ const MFS_ROOT_KEY = new Key('/local/filesroot')
3131
*
3232
* @param {object} config
3333
* @param {import('./types').GCLock} config.gcLock
34-
* @param {import('./pins').Pins} config.pins
34+
* @param {import('./types').Pins} config.pins
3535
* @param {Blockstore} config.blockstore
3636
* @param {import('interface-datastore').Datastore} config.root
3737
* @param {loadCodec} config.loadCodec
@@ -69,7 +69,7 @@ export function gc ({ gcLock, pins, blockstore, root, loadCodec }) {
6969
* Get Set of CIDs of blocks to keep
7070
*
7171
* @param {object} config
72-
* @param {import('./pins').Pins} config.pins
72+
* @param {import('./pin-manager').Pins} config.pins
7373
* @param {import('interface-blockstore').Blockstore} config.blockstore
7474
* @param {import('interface-datastore').Datastore} config.root
7575
* @param {loadCodec} config.loadCodec

packages/ipfs-repo/src/index.js

+2-20
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,12 @@ import { apiAddr } from './api-addr.js'
1212
import { createIdStore } from './idstore.js'
1313
import defaultOptions from './default-options.js'
1414
import defaultDatastore from './default-datastore.js'
15-
import * as ERRORS from './errors/index.js'
16-
import { PinManager, PinTypes as PinTypesImport } from './pins.js'
15+
import * as ERRORS from './errors.js'
16+
import { PinManager } from './pin-manager.js'
1717
import { createPinnedBlockstore } from './pinned-blockstore.js'
1818
// @ts-ignore - no types
1919
import mortice from 'mortice'
2020
import { gc } from './gc.js'
21-
import * as MemoryLock from './locks/memory.js'
22-
import * as FSLock from './locks/fs.js'
23-
import * as BlockstoreUtils from './utils/blockstore.js'
2421

2522
const log = debug('ipfs:repo')
2623

@@ -439,21 +436,6 @@ export function createRepo (path, loadCodec, backends, options) {
439436
return new Repo(path, loadCodec, backends, options)
440437
}
441438

442-
export const repoVersion = CONSTANTS.repoVersion
443-
444-
export const errors = ERRORS
445-
446-
export const utils = {
447-
blockstore: BlockstoreUtils
448-
}
449-
450-
export const locks = {
451-
memory: MemoryLock,
452-
fs: FSLock
453-
}
454-
455-
export const PinTypes = PinTypesImport
456-
457439
/**
458440
* @param {import('./types').Config} _config
459441
*/

packages/ipfs-repo/src/locks/fs.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { LockExistsError } from '../errors/index.js'
1+
import { LockExistsError } from '../errors.js'
22
import path from 'path'
33
import debug from 'debug'
44
import { lock as properLock, check } from 'proper-lockfile'
@@ -28,7 +28,7 @@ const STALE_TIME = 20000
2828
* @param {string} dir
2929
* @returns {Promise<LockCloser>}
3030
*/
31-
export const lock = async (dir) => {
31+
const lock = async (dir) => {
3232
const file = path.join(dir, lockFile)
3333
log('locking %s', file)
3434
let release
@@ -52,8 +52,13 @@ export const lock = async (dir) => {
5252
* @param {string} dir
5353
* @returns {Promise<boolean>}
5454
*/
55-
export const locked = (dir) => {
55+
const locked = (dir) => {
5656
const file = path.join(dir, lockFile)
5757

5858
return check(dir, { lockfilePath: file, stale: STALE_TIME })
5959
}
60+
61+
export const FSLock = {
62+
lock,
63+
locked
64+
}

packages/ipfs-repo/src/locks/memory.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
import { LockExistsError } from '../errors/index.js'
2+
import { LockExistsError } from '../errors.js'
33
import debug from 'debug'
44

55
const log = debug('ipfs:repo:lock:memory')
@@ -18,7 +18,7 @@ const LOCKS = {}
1818
* @param {string} dir
1919
* @returns {Promise<LockCloser>}
2020
*/
21-
export async function lock (dir) {
21+
async function lock (dir) {
2222
const file = dir + '/' + lockFile
2323
log('locking %s', file)
2424

@@ -43,9 +43,14 @@ export async function lock (dir) {
4343
* @param {string} dir
4444
* @returns {Promise<boolean>}
4545
*/
46-
export async function locked (dir) {
46+
async function locked (dir) {
4747
const file = dir + '/' + lockFile
4848
log(`checking lock: ${file}`)
4949

5050
return Boolean(LOCKS[file])
5151
}
52+
53+
export const MemoryLock = {
54+
lock,
55+
locked
56+
}

packages/ipfs-repo/src/pins.js renamed to packages/ipfs-repo/src/pin-manager.js

+1-11
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
keyToMultihash
1313
} from './utils/blockstore.js'
1414
import { walkDag } from './utils/walk-dag.js'
15+
import { PinTypes } from './pin-types.js'
1516

1617
/**
1718
* @typedef {object} PinInternal
@@ -38,17 +39,6 @@ function invalidPinTypeErr (type) {
3839
return errCode(new Error(errMsg), 'ERR_INVALID_PIN_TYPE')
3940
}
4041

41-
export const PinTypes = {
42-
/** @type {'direct'} */
43-
direct: ('direct'),
44-
/** @type {'recursive'} */
45-
recursive: ('recursive'),
46-
/** @type {'indirect'} */
47-
indirect: ('indirect'),
48-
/** @type {'all'} */
49-
all: ('all')
50-
}
51-
5242
/**
5343
* @implements {Pins}
5444
*/

packages/ipfs-repo/src/pin-types.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
export const PinTypes = {
3+
/** @type {'direct'} */
4+
direct: 'direct',
5+
/** @type {'recursive'} */
6+
recursive: 'recursive',
7+
/** @type {'indirect'} */
8+
indirect: 'indirect',
9+
/** @type {'all'} */
10+
all: 'all'
11+
}

packages/ipfs-repo/src/pinned-blockstore.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11

22
import map from 'it-map'
33
import errCode from 'err-code'
4-
import { PinTypes } from './pins.js'
4+
import { PinTypes } from './pin-types.js'
55

66
/**
77
* @typedef {import('interface-datastore').Query} Query
88
* @typedef {import('interface-datastore').Datastore} Datastore
99
* @typedef {import('interface-datastore').Options} DatastoreOptions
1010
* @typedef {import('interface-blockstore').Blockstore} Blockstore
1111
* @typedef {import('multiformats/cid').CID} CID
12-
* @typedef {import('./pins').Pins} Pins
12+
* @typedef {import('./pin-manager').Pins} Pins
1313
*/
1414

1515
/**

packages/ipfs-repo/src/utils/level.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
import { NotFoundError } from '../errors/index.js'
2+
import { NotFoundError } from '../errors.js'
33

44
/**
55
* @typedef {import('interface-datastore').Datastore} Datastore

packages/ipfs-repo/test/blockstore-test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import * as dagCbor from '@ipld/dag-cbor'
2020
import * as dagPb from '@ipld/dag-pb'
2121
import { loadCodec } from './fixtures/load-codec.js'
2222
import { createBackend } from './fixtures/create-backend.js'
23-
import * as MemoryLock from '../src/locks/memory.js'
23+
import { MemoryLock } from '../src/locks/memory.js'
2424

2525
async function makePair () {
2626
const data = new TextEncoder().encode(`hello-${Math.random()}`)

0 commit comments

Comments
 (0)