Skip to content
This repository was archived by the owner on Aug 11, 2021. It is now read-only.

Commit 52f2439

Browse files
authored
feat: add types (#136)
Adds types, fixes all tsc errors, updates all deps.
1 parent d58eacc commit 52f2439

7 files changed

+108
-45
lines changed

Diff for: .travis.yml

+15-4
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,51 @@
11
language: node_js
2+
dist: bionic
23
cache: npm
34
stages:
45
- check
56
- test
67
- cov
78

9+
branches:
10+
only:
11+
- master
12+
- /^release\/.*$/
13+
814
node_js:
9-
- '10'
15+
- 'lts/*'
16+
- 'node'
1017

1118
os:
1219
- linux
1320
- osx
1421
- windows
1522

23+
before_install:
24+
# modules with pre-built binaries may not have deployed versions for bleeding-edge node so this lets us fall back to building from source
25+
- npm install -g @mapbox/node-pre-gyp
26+
1627
script: npx nyc -s npm run test:node -- --bail
1728
after_success: npx nyc report --reporter=text-lcov > coverage.lcov && npx codecov
1829

1930
jobs:
2031
include:
2132
- stage: check
2233
script:
23-
- npx aegir commitlint --travis
2434
- npx aegir dep-check
2535
- npm run lint
2636

2737
- stage: test
2838
name: chrome
2939
addons:
3040
chrome: stable
31-
script: npx aegir test -t browser -t webworker
41+
script:
42+
- npx aegir test -t browser -t webworker
3243

3344
- stage: test
3445
name: firefox
3546
addons:
3647
firefox: latest
37-
script: npx aegir test -t browser -t webworker -- --browsers FirefoxHeadless
48+
script: npx aegir test -t browser -t webworker -- --browser firefox
3849

3950
notifications:
4051
email: false

Diff for: package.json

+18-10
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,18 @@
44
"description": "JavaScript Implementation of BlockService",
55
"leadMaintainer": "Volker Mische <[email protected]>",
66
"main": "src/index.js",
7+
"types": "dist/src/index.d.ts",
78
"scripts": {
9+
"prepare": "aegir build --no-bundle",
810
"lint": "aegir lint",
9-
"build": "aegir build",
11+
"prepublishOnly": "aegir build",
1012
"test": "aegir test",
1113
"test:node": "aegir test --target node",
1214
"test:browser": "aegir test --target browser",
1315
"release": "aegir release --docs",
1416
"release-minor": "aegir release --type minor --docs",
1517
"release-major": "aegir release --type major --docs",
16-
"coverage": "aegir coverage",
17-
"coverage-publish": "aegir coverage --provider coveralls",
18+
"coverage": "aegir test -t node --cov && nyc report --reporter=html",
1819
"docs": "aegir docs"
1920
},
2021
"repository": {
@@ -30,24 +31,31 @@
3031
},
3132
"homepage": "https://github.com/ipfs/js-ipfs-block-service#readme",
3233
"devDependencies": {
34+
"@types/fs-extra": "^9.0.8",
35+
"@types/lodash.range": "^3.2.6",
3336
"abort-controller": "^3.0.0",
34-
"aegir": "^22.0.0",
37+
"aegir": "^31.0.4",
38+
"assert": "^2.0.0",
3539
"cids": "^1.0.0",
40+
"events": "^3.3.0",
3641
"fs-extra": "^9.0.0",
37-
"ipfs-repo": "^6.0.0",
38-
"ipld-block": "^0.10.0",
42+
"ipfs-repo": "^9.0.0",
43+
"ipld-block": "^0.11.1",
44+
"it-all": "^1.0.5",
3945
"it-drain": "^1.0.1",
40-
"lodash": "^4.17.11",
46+
"lodash.range": "^3.2.0",
4147
"multihashing-async": "^2.0.1",
42-
"uint8arrays": "^2.1.2"
48+
"native-abort-controller": "^1.0.3",
49+
"uint8arrays": "^2.1.2",
50+
"util": "^0.12.3"
4351
},
4452
"engines": {
4553
"node": ">=12.0.0",
4654
"npm": ">=3.0.0"
4755
},
4856
"dependencies": {
49-
"err-code": "^2.0.0",
50-
"streaming-iterables": "^5.0.2"
57+
"err-code": "^3.0.1",
58+
"it-map": "^1.0.5"
5159
},
5260
"contributors": [
5361
"David Dias <[email protected]>",

Diff for: src/index.js

+21-23
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
'use strict'
22

3-
const { map } = require('streaming-iterables')
3+
const map = require('it-map')
44
const errcode = require('err-code')
55

6+
/**
7+
* @typedef {import('ipfs-repo')} IPFSRepo
8+
* @typedef {import('ipld-block')} Block
9+
* @typedef {import('cids')} CID
10+
*/
11+
612
/**
713
* BlockService is a hybrid block datastore. It stores data in a local
814
* datastore and may retrieve data from a remote Exchange.
@@ -26,26 +32,21 @@ class BlockService {
2632
* If the node is online all requests for blocks first
2733
* check locally and afterwards ask the network for the blocks.
2834
*
29-
* @param {Bitswap} bitswap
30-
* @returns {void}
35+
* @param {any} bitswap
3136
*/
3237
setExchange (bitswap) {
3338
this._bitswap = bitswap
3439
}
3540

3641
/**
3742
* Go offline, i.e. drop the reference to bitswap.
38-
*
39-
* @returns {void}
4043
*/
4144
unsetExchange () {
4245
this._bitswap = null
4346
}
4447

4548
/**
4649
* Is the blockservice online, i.e. is bitswap present.
47-
*
48-
* @returns {bool}
4950
*/
5051
hasExchange () {
5152
return this._bitswap != null
@@ -55,9 +56,9 @@ class BlockService {
5556
* Put a block to the underlying datastore.
5657
*
5758
* @param {Block} block
58-
* @param {Object} [options] - Options is an object with the following properties
59+
* @param {object} [options] - Options is an object with the following properties
5960
* @param {AbortSignal} [options.signal] - A signal that can be used to abort any long-lived operations that are started as a result of this operation
60-
* @returns {Promise}
61+
* @returns {Promise<Block>}
6162
*/
6263
put (block, options) {
6364
if (this.hasExchange()) {
@@ -70,10 +71,10 @@ class BlockService {
7071
/**
7172
* Put a multiple blocks to the underlying datastore.
7273
*
73-
* @param {AsyncIterator<Block>} blocks
74-
* @param {Object} [options] - Options is an object with the following properties
74+
* @param {AsyncIterable<Block> | Iterable<Block>} blocks
75+
* @param {object} [options] - Options is an object with the following properties
7576
* @param {AbortSignal} [options.signal] - A signal that can be used to abort any long-lived operations that are started as a result of this operation
76-
* @returns {Promise}
77+
* @returns {AsyncIterable<Block>}
7778
*/
7879
putMany (blocks, options) {
7980
if (this.hasExchange()) {
@@ -87,7 +88,7 @@ class BlockService {
8788
* Get a block by cid.
8889
*
8990
* @param {CID} cid
90-
* @param {Object} [options] - Options is an object with the following properties
91+
* @param {object} [options] - Options is an object with the following properties
9192
* @param {AbortSignal} [options.signal] - A signal that can be used to abort any long-lived operations that are started as a result of this operation
9293
* @returns {Promise<Block>}
9394
*/
@@ -102,10 +103,10 @@ class BlockService {
102103
/**
103104
* Get multiple blocks back from an array of cids.
104105
*
105-
* @param {AsyncIterator<CID>} cids
106-
* @param {Object} [options] - Options is an object with the following properties
106+
* @param {AsyncIterable<CID> | Iterable<CID>} cids
107+
* @param {object} [options] - Options is an object with the following properties
107108
* @param {AbortSignal} [options.signal] - A signal that can be used to abort any long-lived operations that are started as a result of this operation
108-
* @returns {AsyncIterator<Block>}
109+
* @returns {AsyncIterable<Block>}
109110
*/
110111
getMany (cids, options) {
111112
if (!Array.isArray(cids)) {
@@ -115,18 +116,16 @@ class BlockService {
115116
if (this.hasExchange()) {
116117
return this._bitswap.getMany(cids, options)
117118
} else {
118-
const getRepoBlocks = map((cid) => this._repo.blocks.get(cid, options))
119-
return getRepoBlocks(cids)
119+
return map(cids, (cid) => this._repo.blocks.get(cid, options))
120120
}
121121
}
122122

123123
/**
124124
* Delete a block from the blockstore.
125125
*
126126
* @param {CID} cid
127-
* @param {Object} [options] - Options is an object with the following properties
127+
* @param {object} [options] - Options is an object with the following properties
128128
* @param {AbortSignal} [options.signal] - A signal that can be used to abort any long-lived operations that are started as a result of this operation
129-
* @returns {Promise}
130129
*/
131130
async delete (cid, options) {
132131
if (!await this._repo.blocks.has(cid)) {
@@ -139,10 +138,9 @@ class BlockService {
139138
/**
140139
* Delete multiple blocks from the blockstore.
141140
*
142-
* @param {AsyncIterator<CID>} cids
143-
* @param {Object} [options] - Options is an object with the following properties
141+
* @param {AsyncIterable<CID> | Iterable<CID>} cids
142+
* @param {object} [options] - Options is an object with the following properties
144143
* @param {AbortSignal} [options.signal] - A signal that can be used to abort any long-lived operations that are started as a result of this operation
145-
* @returns {Promise}
146144
*/
147145
deleteMany (cids, options) {
148146
const repo = this._repo

Diff for: test/aborting-requests.spec.js

+21-4
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,26 @@
33

44
const { expect } = require('aegir/utils/chai')
55

6-
const { collect } = require('streaming-iterables')
7-
const AbortController = require('abort-controller')
6+
const all = require('it-all')
7+
const { AbortController } = require('native-abort-controller')
88

99
const BlockService = require('../src')
1010

11+
/**
12+
* @typedef {import('ipfs-repo')} IPFSRepo
13+
*/
14+
1115
describe('aborting requests', () => {
16+
/** @type {Error} */
1217
let abortedErr
18+
/** @type {BlockService} */
1319
let r
1420

1521
beforeEach(() => {
1622
abortedErr = new Error('Aborted!')
23+
/**
24+
* @param {...any} args
25+
*/
1726
const abortOnSignal = (...args) => {
1827
const { signal } = args[args.length - 1]
1928

@@ -24,14 +33,17 @@ describe('aborting requests', () => {
2433
})
2534
}
2635

36+
/** @type {IPFSRepo} */
2737
const repo = {
2838
blocks: {
2939
put: abortOnSignal,
40+
// @ts-ignore should return async iterable
3041
putMany: abortOnSignal,
3142
get: abortOnSignal,
3243
delete: abortOnSignal,
44+
// @ts-ignore should return async iterable
3345
deleteMany: abortOnSignal,
34-
has: () => true
46+
has: () => Promise.resolve(true)
3547
}
3648
}
3749
r = new BlockService(repo)
@@ -41,6 +53,7 @@ describe('aborting requests', () => {
4153
const controller = new AbortController()
4254
setTimeout(() => controller.abort(), 1)
4355

56+
// @ts-expect-error does not take string
4457
await expect(r.put('block', {
4558
signal: controller.signal
4659
})).to.eventually.rejectedWith(abortedErr)
@@ -50,6 +63,7 @@ describe('aborting requests', () => {
5063
const controller = new AbortController()
5164
setTimeout(() => controller.abort(), 1)
5265

66+
// @ts-expect-error does not take string array
5367
await expect(r.putMany(['block'], {
5468
signal: controller.signal
5569
})).to.eventually.rejectedWith(abortedErr)
@@ -59,6 +73,7 @@ describe('aborting requests', () => {
5973
const controller = new AbortController()
6074
setTimeout(() => controller.abort(), 1)
6175

76+
// @ts-expect-error does not take string
6277
await expect(r.get('cid', {
6378
signal: controller.signal
6479
})).to.eventually.rejectedWith(abortedErr)
@@ -68,7 +83,8 @@ describe('aborting requests', () => {
6883
const controller = new AbortController()
6984
setTimeout(() => controller.abort(), 1)
7085

71-
await expect(collect(r.getMany(['cid'], {
86+
// @ts-expect-error does not take string array
87+
await expect(all(r.getMany(['cid'], {
7288
signal: controller.signal
7389
}))).to.eventually.rejectedWith(abortedErr)
7490
})
@@ -77,6 +93,7 @@ describe('aborting requests', () => {
7793
const controller = new AbortController()
7894
setTimeout(() => controller.abort(), 1)
7995

96+
// @ts-expect-error does not take string
8097
await expect(r.delete('cid', {
8198
signal: controller.signal
8299
})).to.eventually.rejectedWith(abortedErr)

0 commit comments

Comments
 (0)