Skip to content

Commit

Permalink
Merge remote-tracking branch 'http/main' into chore/add-helia-http
Browse files Browse the repository at this point in the history
  • Loading branch information
achingbrain committed Jan 9, 2024
2 parents 3f4c6bf + 1373d7c commit 4ddd3ee
Show file tree
Hide file tree
Showing 26 changed files with 1,969 additions and 0 deletions.
34 changes: 34 additions & 0 deletions packages/http/.aegir.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { CID } from 'multiformats/cid'
import { sha256 } from 'multiformats/hashes/sha2'
import * as raw from 'multiformats/codecs/raw'

/** @type {import('aegir').PartialOptions} */
const options = {
test: {
before: async () => {
// use dynamic import otherwise the source may not have been built yet
const { createHeliaHTTP } = await import('./dist/src/index.js')

const heliaHTTP = await createHeliaHTTP()

const block = Uint8Array.from([0, 1, 2, 3])
const mh = await sha256.digest(block)
const cid = CID.createV1(raw.code, mh)
await heliaHTTP.blockstore.put(cid, block)

return {
env: {
BLOCK_CID: cid.toString()
},
heliaHTTP
}
},
after: async (_, beforeResult) => {
if (beforeResult.heliaHTTP != null) {
await beforeResult.heliaHTTP.stop()
}
}
}
}

export default options
4 changes: 4 additions & 0 deletions packages/http/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/http/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/http/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.
72 changes: 72 additions & 0 deletions packages/http/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<p align="center">
<a href="https://github.com/ipfs/helia-http" title="helia-http">
<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/meandavejustice/helia-http.svg?style=flat-square)](https://codecov.io/gh/meandavejustice/helia-http)
[![CI](https://img.shields.io/github/actions/workflow/status/meandavejustice/helia-http/main.yml?branch=main\&style=flat-square)](https://github.com/meandavejustice/helia-http/actions/workflows/main.yml?query=branch%3Amain)

# Helia-http

> A lightweight implementation of IPFS over HTTP in JavaScript
## About

Exports a `createHeliaHTTP` function that returns an object that implements the lightweight Helia API.

Pass it to other modules like @helia/unixfs to make files available on the distributed web.

This repo solves [this issue](https://github.com/ipfs/helia/issues/289) and aims to eventually merge back into the [Helia repo](https://github.com/ipfs/helia).

### Example

```typescript
import { createHeliaHttp } from 'helia-http'
import { unixfs } from '@helia/unixfs'
import { CID } from 'multiformats/cid'

const helia = await createHeliaHTTP()

const fs = unixfs(helia)
fs.cat(CID.parse('bafyFoo'))
```

<!-- ## Install
```console
$ npm i @helia/http
```
### Browser `<script>` tag
Loading this module through a script tag will make it's exports available as `HeliaHTTP` in the global namespace.
```html
<script src="https://unpkg.com/@helia/http/dist/index.min.js"></script>
```
## API Docs
- <https://ipfs.github.io/helia/modules/http.html> -->

## 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/meandavejustice/helia-http/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)
83 changes: 83 additions & 0 deletions packages/http/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
{
"name": "@helia/http",
"version": "0.9.0",
"description": "A lightweight implementation of IPFS over HTTP in JavaScript",
"license": "Apache-2.0 OR MIT",
"homepage": "https://github.com/meandavejustice/helia-http#readme",
"repository": {
"type": "git",
"url": "git+https://github.com/meandavejustice/helia-http.git"
},
"bugs": {
"url": "https://github.com/meandavejustice/helia-http/issues"
},
"publishConfig": {
"access": "public",
"provenance": true
},
"keywords": [
"IPFS"
],
"type": "module",
"types": "./dist/src/index.d.ts",
"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/block-brokers": "https://gitpkg.now.sh/meandavejustice/helia/packages/block-brokers?341-breakout-block-brokers",
"@helia/interface": "https://gitpkg.now.sh/meandavejustice/helia/packages/interface?352-update-@helia/interface-for-http",
"@ipld/dag-cbor": "^9.0.6",
"@ipld/dag-json": "^10.1.5",
"@ipld/dag-pb": "^4.0.6",
"@libp2p/interface": "^1.0.1",
"@libp2p/logger": "^4.0.2",
"blockstore-core": "^4.3.8",
"cborg": "^4.0.1",
"datastore-core": "^9.2.6",
"interface-blockstore": "^5.2.7",
"interface-datastore": "^8.2.9",
"interface-store": "^5.1.5",
"it-drain": "^3.0.5",
"multiformats": "^12.1.3",
"p-defer": "^4.0.0",
"p-queue": "^8.0.1",
"progress-events": "^1.0.0",
"uint8arrays": "^5.0.0"
},
"devDependencies": {
"@types/sinon": "^17.0.2",
"aegir": "^41.0.0",
"delay": "^6.0.0",
"it-all": "^3.0.4",
"mortice": "^3.0.4"
}
}
102 changes: 102 additions & 0 deletions packages/http/src/helia-http.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import { start, stop } from '@libp2p/interface'
import drain from 'it-drain'
import { CustomProgressEvent } from 'progress-events'
import { trustlessGateway, NetworkedStorage } from '@helia/block-brokers'
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 type { HeliaHTTPInit } from '.'
import type { GCOptions, HeliaHTTP } from '@helia/interface/http'
import type { Pins } from '@helia/interface/pins'
import type { ComponentLogger, Logger } from '@libp2p/interface'
import type { Blockstore } from 'interface-blockstore'
import type { Datastore } from 'interface-datastore'
import type { CID } from 'multiformats/cid'
import { defaultLogger } from '@libp2p/logger'

interface HeliaHTTPImplInit extends HeliaHTTPInit {
blockstore: Blockstore
datastore: Datastore
}

export class HeliaHTTPImpl implements HeliaHTTP {
public blockstore: BlockStorage
public datastore: Datastore
public pins: Pins
public logger: ComponentLogger
private readonly log: Logger

constructor (init: HeliaHTTPImplInit) {
this.logger = init.logger ?? defaultLogger()
this.log = this.logger.forComponent('helia:http')
const hashers = defaultHashers(init.hashers)

const components = {
blockstore: init.blockstore,
datastore: init.datastore,
hashers,
logger: this.logger
}

const blockBrokers = init.blockBrokers?.map((fn) => {
return fn(components)
}) ?? [
trustlessGateway()(components)
]

const networkedStorage = new NetworkedStorage(components, {
blockBrokers,
hashers
})

this.pins = new PinsImpl(init.datastore, networkedStorage, init.dagWalkers ?? [])

this.blockstore = new BlockStorage(networkedStorage, this.pins, {
holdGcLock: init.holdGcLock
})
this.datastore = init.datastore
}

async start (): Promise<void> {
await assertDatastoreVersionIsCurrent(this.datastore)
await start(this.blockstore)
}

async stop (): Promise<void> {
await stop(this.blockstore)
}

async gc (options: GCOptions = {}): Promise<void> {
const releaseLock = await this.blockstore.lock.writeLock()

try {
const heliaHTTP = this
const blockstore = this.blockstore.unwrap()

this.log('gc start')

await drain(blockstore.deleteMany((async function * (): AsyncGenerator<CID> {
for await (const { cid } of blockstore.getAll()) {
try {
if (await heliaHTTP.pins.isPinned(cid, options)) {
continue
}

yield cid

options.onProgress?.(new CustomProgressEvent<CID>('heliaHTTP:gc:deleted', cid))
} catch (err) {
heliaHTTP.log.error('Error during gc', err)
options.onProgress?.(new CustomProgressEvent<Error>('heliaHTTP:gc:error', err))
}
}
}())))
} finally {
releaseLock()
}

this.log('gc finished')
}
}
Loading

0 comments on commit 4ddd3ee

Please sign in to comment.