@helia/http
Version:
A lightweight implementation of IPFS over HTTP in JavaScript
71 lines • 2.25 kB
JavaScript
/**
* @packageDocumentation
*
* Exports a `createHeliaHTTP` function that returns an object that implements a lightweight version of the {@link Helia} API that functions only over HTTP.
*
* Pass it to other modules like {@link https://www.npmjs.com/package/@helia/unixfs | @helia/unixfs} to fetch files from the distributed web.
*
* @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'))
* ```
*/
import { trustlessGateway } from '@helia/block-brokers';
import { Helia as HeliaClass } from '@helia/core';
import { MemoryBlockstore } from 'blockstore-core';
import { MemoryDatastore } from 'datastore-core';
import { createLibp2p } from './utils/libp2p.js';
// re-export interface types so people don't have to depend on @helia/interface
// if they don't want to
export * from '@helia/interface';
export * from '@helia/interface/blocks';
export * from '@helia/interface/pins';
/**
* Create and return a Helia node
*/
export async function createHeliaHTTP(init = {}) {
const datastore = init.datastore ?? new MemoryDatastore();
const blockstore = init.blockstore ?? new MemoryBlockstore();
let libp2p;
if (isLibp2p(init.libp2p)) {
libp2p = init.libp2p;
}
else {
libp2p = await createLibp2p({
...init,
libp2p: init.libp2p,
datastore
});
}
const helia = new HeliaClass({
...init,
libp2p,
datastore,
blockstore,
blockBrokers: init.blockBrokers ?? [
trustlessGateway()
]
});
if (init.start !== false) {
await helia.start();
}
return helia;
}
function isLibp2p(obj) {
if (obj == null) {
return false;
}
// a non-exhaustive list of methods found on the libp2p object
const funcs = ['dial', 'dialProtocol', 'hangUp', 'handle', 'unhandle', 'getMultiaddrs', 'getProtocols'];
// if these are all functions it's probably a libp2p object
return funcs.every(m => typeof obj[m] === 'function');
}
//# sourceMappingURL=index.js.map