Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: create @helia/block-brokers package (#341) #342

Merged
merged 11 commits into from
Jan 8, 2024
2 changes: 1 addition & 1 deletion .release-please-manifest.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"packages/helia":"3.0.0","packages/interface":"3.0.0","packages/interop":"2.0.0"}
{"packages/block-brokers":"0.0.0", "packages/helia":"3.0.0","packages/interface":"3.0.0","packages/interop":"2.0.0"}
2 changes: 2 additions & 0 deletions .release-please.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{
"plugins": ["node-workspace"],
"last-release-sha": "456f6841aa4f6f27cb9b8636c90a1765a3018878",
"packages": {
"packages/block-brokers": {},
"packages/helia": {},
"packages/interface": {},
"packages/interop": {}
Expand Down
4 changes: 4 additions & 0 deletions packages/block-brokers/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
This project is dual licensed under MIT and Apache-2.0.

MIT: https://www.opensource.org/licenses/mit
Apache-2.0: https://www.apache.org/licenses/license-2.0
5 changes: 5 additions & 0 deletions packages/block-brokers/LICENSE-APACHE
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
19 changes: 19 additions & 0 deletions packages/block-brokers/LICENSE-MIT
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
The MIT License (MIT)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
45 changes: 45 additions & 0 deletions packages/block-brokers/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<p align="center">
<a href="https://github.com/ipfs/helia" title="Helia">
<img src="https://raw.githubusercontent.com/ipfs/helia/main/assets/helia.png" alt="Helia logo" width="300" />
</a>
</p>

[![ipfs.tech](https://img.shields.io/badge/project-IPFS-blue.svg?style=flat-square)](https://ipfs.tech)
[![Discuss](https://img.shields.io/discourse/https/discuss.ipfs.tech/posts.svg?style=flat-square)](https://discuss.ipfs.tech)
[![codecov](https://img.shields.io/codecov/c/github/ipfs/helia.svg?style=flat-square)](https://codecov.io/gh/ipfs/helia)
[![CI](https://img.shields.io/github/actions/workflow/status/ipfs/helia/main.yml?branch=main\&style=flat-square)](https://github.com/ipfs/helia/actions/workflows/main.yml?query=branch%3Amain)

> Block brokers for Helia

## Install

```console
$ npm i @helia/block-brokers
```

<!-- ### Browser `<script>` tag

Loading this module through a script tag will make it's exports available as `bitswap` and `trustlessGateway` and `NetworkedStorage` in the global namespace.

```html
<script src="https://unpkg.com/@helia/block-brokers/dist/index.min.js"></script>
``` -->

## License

Licensed under either of

- Apache 2.0, ([LICENSE-APACHE](LICENSE-APACHE) / <http://www.apache.org/licenses/LICENSE-2.0>)
- MIT ([LICENSE-MIT](LICENSE-MIT) / <http://opensource.org/licenses/MIT>)

## Contribute

Contributions welcome! Please check out [the issues](https://github.com/ipfs/helia/issues).

Also see our [contributing document](https://github.com/ipfs/community/blob/master/CONTRIBUTING_JS.md) for more information on how we work, and about contributing in general.

Please be aware that all interactions related to this repo are subject to the IPFS [Code of Conduct](https://github.com/ipfs/community/blob/master/code-of-conduct.md).

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

[![](https://cdn.rawgit.com/jbenet/contribute-ipfs-gif/master/img/contribute.gif)](https://github.com/ipfs/community/blob/master/CONTRIBUTING.md)
95 changes: 95 additions & 0 deletions packages/block-brokers/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
{
"name": "@helia/block-brokers",
"version": "0.0.0",
"description": "Block brokers for Helia",
"license": "Apache-2.0 OR MIT",
"homepage": "https://github.com/ipfs/helia/tree/master/packages/block-brokers#readme",
"repository": {
"type": "git",
"url": "git+https://github.com/ipfs/helia.git"
},
"bugs": {
"url": "https://github.com/ipfs/helia/issues"
},
"publishConfig": {
"access": "public",
"provenance": true
},
"keywords": [
"IPFS"
],
"type": "module",
"types": "./dist/src/index.d.ts",
"typesVersions": {
"*": {
"*": [
"*",
"dist/*",
"dist/src/*",
"dist/src/*/index"
],
"src/*": [
"*",
"dist/*",
"dist/src/*",
"dist/src/*/index"
]
}
},
"files": [
"src",
"dist",
"!dist/test",
"!**/*.tsbuildinfo"
],
"exports": {
".": {
"types": "./dist/src/index.d.ts",
"import": "./dist/src/index.js"
}
},
"eslintConfig": {
"extends": "ipfs",
"parserOptions": {
"project": true,
"sourceType": "module"
}
},
"scripts": {
"clean": "aegir clean",
"lint": "aegir lint",
"dep-check": "aegir dep-check",
"build": "aegir build",
"test": "aegir test",
"test:chrome": "aegir test -t browser --cov",
"test:chrome-webworker": "aegir test -t webworker",
"test:firefox": "aegir test -t browser -- --browser firefox",
"test:firefox-webworker": "aegir test -t webworker -- --browser firefox",
"test:node": "aegir test -t node --cov",
"test:electron-main": "aegir test -t electron-main"
},
"dependencies": {
"@helia/interface": "^3.0.0",
"@libp2p/interface": "^1.1.1",
"any-signal": "^4.1.1",
"interface-blockstore": "^5.2.7",
"interface-store": "^5.1.5",
"ipfs-bitswap": "^20.0.0",
"it-filter": "^3.0.4",
"it-foreach": "^2.0.6",
"multiformats": "^13.0.0",
"progress-events": "^1.0.0",
"uint8arrays": "^5.0.0"
},
"devDependencies": {
"@libp2p/logger": "^4.0.1",
"@types/sinon": "^17.0.2",
"aegir": "^42.0.0",
"blockstore-core": "^4.3.8",
"delay": "^6.0.0",
"it-all": "^3.0.4",
"it-drain": "^3.0.5",
"sinon": "^17.0.1",
"sinon-ts": "^2.0.0"
}
}
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { bitswap } from './bitswap.js'
export { trustlessGateway } from './trustless-gateway/index.js'
export { NetworkedStorage } from './utils/networked-storage.js'
12 changes: 12 additions & 0 deletions packages/block-brokers/src/utils/default-hashers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { identity } from 'multiformats/hashes/identity'
import { sha256, sha512 } from 'multiformats/hashes/sha2'
import type { MultihashHasher } from 'multiformats/hashes/interface'

export function defaultHashers (hashers: MultihashHasher[] = []): MultihashHasher[] {
return [
sha256,
sha512,
identity,
...hashers
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ export class NetworkedStorage implements Blocks, Startable {
return this.child.has(cid, options)
}

async * getAll (options: AbortOptions & ProgressOptions<GetAllBlocksProgressEvents> = {}): AsyncIterable<Pair> {
async * getAll (options: AbortOptions & ProgressOptions<GetAllBlocksProgressEvents> = {}): AwaitIterable<Pair> {
options.onProgress?.(new CustomProgressEvent('blocks:get-all:blockstore:get-many'))
yield * this.child.getAll(options)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import all from 'it-all'
import * as raw from 'multiformats/codecs/raw'
import Sinon from 'sinon'
import { type StubbedInstance, stubInterface } from 'sinon-ts'
import { defaultHashers } from '../../src/utils/default-hashers.js'
import { NetworkedStorage } from '../../src/utils/networked-storage.js'
import { createBlock } from '../fixtures/create-block.js'
import { defaultHashers } from '../src/utils/default-hashers.js'
import { NetworkedStorage } from '../src/utils/networked-storage.js'
import { createBlock } from './fixtures/create-block.js'
import type { BlockBroker, BlockRetriever } from '@helia/interface/blocks'
import type { Blockstore } from 'interface-blockstore'
import type { CID } from 'multiformats/cid'
Expand Down
18 changes: 18 additions & 0 deletions packages/block-brokers/test/fixtures/create-block.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { CID } from 'multiformats/cid'
SgtPooki marked this conversation as resolved.
Show resolved Hide resolved
import { sha256 } from 'multiformats/hashes/sha2'
import type { Blockstore } from 'interface-blockstore'

export async function createBlock <Codec extends number> (codec: Codec, block: Uint8Array): Promise<{ cid: CID<unknown, Codec, 18>, block: Uint8Array }> {
const mh = await sha256.digest(block)
const cid = CID.createV1(codec, mh)

return { cid, block }
}

export async function createAndPutBlock <Codec extends number> (codec: Codec, block: Uint8Array, blockstore: Blockstore): Promise<CID<unknown, Codec, 18>> {
const result = await createBlock(codec, block)

await blockstore.put(result.cid, block)

return result.cid
}

Check warning on line 18 in packages/block-brokers/test/fixtures/create-block.ts

View check run for this annotation

Codecov / codecov/patch

packages/block-brokers/test/fixtures/create-block.ts#L13-L18

Added lines #L13 - L18 were not covered by tests
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import { expect } from 'aegir/chai'
import * as raw from 'multiformats/codecs/raw'
import Sinon from 'sinon'
import { type StubbedInstance, stubConstructor } from 'sinon-ts'
import { TrustlessGatewayBlockBroker } from '../../src/block-brokers/trustless-gateway/broker.js'
import { TrustlessGateway } from '../../src/block-brokers/trustless-gateway/trustless-gateway.js'
import { createBlock } from '../fixtures/create-block.js'
import { TrustlessGatewayBlockBroker } from '../src/trustless-gateway/broker.js'
import { TrustlessGateway } from '../src/trustless-gateway/trustless-gateway.js'
import { createBlock } from './fixtures/create-block.js'
import type { BlockRetriever } from '@helia/interface/blocks'
import type { CID } from 'multiformats/cid'

Expand Down
15 changes: 15 additions & 0 deletions packages/block-brokers/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"extends": "aegir/src/config/tsconfig.aegir.json",
"compilerOptions": {
"outDir": "dist"
},
"include": [
"src",
"test"
],
"references": [
{
"path": "../interface"
}
]
}
7 changes: 7 additions & 0 deletions packages/block-brokers/typedoc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"entryPoints": [
"./src/index.ts",
"./src/utils/default-hashers.ts"
],
"includeVersion": true
}
2 changes: 1 addition & 1 deletion packages/helia/.aegir.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const options = {
before: async () => {
// use dynamic import otherwise the source may not have been built yet
const { createHelia } = await import('./dist/src/index.js')
const { bitswap } = await import('./dist/src/block-brokers/index.js')
const { bitswap } = await import('@helia/block-brokers')

const helia = await createHelia({
blockBrokers: [
Expand Down
15 changes: 3 additions & 12 deletions packages/helia/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,6 @@
"types": "./dist/src/index.d.ts",
"import": "./dist/src/index.js"
},
"./block-brokers": {
"types": "./dist/src/block-brokers/index.d.ts",
"import": "./dist/src/block-brokers/index.js"
},
"./hashers": {
"types": "./dist/src/utils/default-hashers.d.ts",
"import": "./dist/src/utils/default-hashers.js"
Expand Down Expand Up @@ -81,6 +77,7 @@
"@chainsafe/libp2p-gossipsub": "^11.0.0",
"@chainsafe/libp2p-noise": "^14.0.0",
"@chainsafe/libp2p-yamux": "^6.0.1",
"@helia/block-brokers": "^0.0.0",
"@helia/delegated-routing-v1-http-api-client": "^1.1.0",
"@helia/interface": "^3.0.0",
"@ipld/dag-cbor": "^9.0.0",
Expand All @@ -91,7 +88,7 @@
"@libp2p/circuit-relay-v2": "^1.0.2",
"@libp2p/dcutr": "^1.0.1",
"@libp2p/identify": "^1.0.1",
"@libp2p/interface": "^1.0.1",
"@libp2p/interface": "^1.1.1",
"@libp2p/kad-dht": "^12.0.1",
"@libp2p/keychain": "^4.0.2",
"@libp2p/logger": "^4.0.1",
Expand All @@ -104,18 +101,14 @@
"@libp2p/webrtc": "^4.0.3",
"@libp2p/websockets": "^8.0.2",
"@libp2p/webtransport": "^4.0.3",
"any-signal": "^4.1.1",
"blockstore-core": "^4.0.0",
"cborg": "^4.0.1",
"datastore-core": "^9.0.0",
"interface-blockstore": "^5.0.0",
"interface-datastore": "^8.0.0",
"interface-store": "^5.0.1",
"ipfs-bitswap": "^20.0.0",
"ipns": "^8.0.0",
"it-drain": "^3.0.1",
"it-filter": "^3.0.1",
"it-foreach": "^2.0.2",
"libp2p": "^1.0.3",
"mortice": "^3.0.1",
"multiformats": "^13.0.0",
Expand All @@ -128,9 +121,7 @@
"@types/sinon": "^17.0.2",
"aegir": "^42.0.0",
"delay": "^6.0.0",
"it-all": "^3.0.4",
"sinon": "^17.0.0",
"sinon-ts": "^2.0.0"
"it-all": "^3.0.4"
},
"browser": {
"./dist/src/utils/libp2p-defaults.js": "./dist/src/utils/libp2p-defaults.browser.js"
Expand Down
3 changes: 1 addition & 2 deletions packages/helia/src/helia.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { bitswap, trustlessGateway, NetworkedStorage } from '@helia/block-brokers'
import { start, stop } from '@libp2p/interface'
import drain from 'it-drain'
import { CustomProgressEvent } from 'progress-events'
import { bitswap, trustlessGateway } from './block-brokers/index.js'
import { PinsImpl } from './pins.js'
import { BlockStorage } from './storage.js'
import { assertDatastoreVersionIsCurrent } from './utils/datastore-version.js'
import { defaultHashers } from './utils/default-hashers.js'
import { NetworkedStorage } from './utils/networked-storage.js'
import type { HeliaInit } from '.'
import type { GCOptions, Helia } from '@helia/interface'
import type { Pins } from '@helia/interface/pins'
Expand Down
2 changes: 1 addition & 1 deletion packages/helia/test/fixtures/create-helia.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { bitswap } from '@helia/block-brokers'
import { circuitRelayTransport } from '@libp2p/circuit-relay-v2'
import { identify } from '@libp2p/identify'
import { webSockets } from '@libp2p/websockets'
import * as Filters from '@libp2p/websockets/filters'
import { bitswap } from '../../src/block-brokers/index.js'
import { createHelia as createNode } from '../../src/index.js'
import type { Helia } from '@helia/interface'

Expand Down
2 changes: 1 addition & 1 deletion packages/interface/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
"build": "aegir build"
},
"dependencies": {
"@libp2p/interface": "^1.0.1",
"@libp2p/interface": "^1.1.1",
"interface-blockstore": "^5.0.0",
"interface-datastore": "^8.0.0",
"interface-store": "^5.0.1",
Expand Down
1 change: 1 addition & 0 deletions packages/interop/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
"devDependencies": {
"@chainsafe/libp2p-noise": "^14.0.0",
"@chainsafe/libp2p-yamux": "^6.0.1",
"@helia/block-brokers": "^0.0.0",
"@helia/interface": "^3.0.0",
"@libp2p/identify": "^1.0.1",
"@libp2p/peer-id": "^4.0.3",
Expand Down
Loading