Security News
Node.js EOL Versions CVE Dubbed the "Worst CVE of the Year" by Security Experts
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
js-kubo-rpc-client
Advanced tools
A client library for the IPFS HTTP API
$ npm i js-kubo-rpc-client
We've come a long way, but this project is still in Alpha, lots of development is happening, APIs might change, beware of 🐉..
npm install --save js-kubo-rpc-client
Both the Current and Active LTS versions of Node.js are supported. Please see nodejs.org for what these currently are.
create([options])
create an instance of the HTTP API client
None
options
can be a String
, a URL
or a Multiaddr
which will be interpreted as the address of the IPFS node we wish to use the API of.
Alternatively it can be an object which may have the following keys:
Name | Type | Default | Description |
---|---|---|---|
url | String or URL or Multiaddr | 'http://localhost:5001/api/v0' | A URL that resolves to a running instance of the IPFS HTTP RPC API |
protocol | String | 'http' | The protocol to used (ignored if url is specified) |
host | String | 'localhost' | The host to used (ignored if url is specified) |
port | number | 5001 | The port to used (ignored if url is specified) |
path | String | 'api/v0' | The path to used (ignored if url is specified) |
agent | http.Agent | http.Agent({ keepAlive: true, maxSockets: 6 }) | An http.Agent used to control client behaviour (node.js only) |
Type | Description |
---|---|
Object | An object that conforms to the IPFS Core API |
import { create } from 'js-kubo-rpc-client'
// connect to the default API address http://localhost:5001
const client = create()
// connect to a different API
const client = create({ url: "http://127.0.0.1:5002/api/v0" });
// connect using a URL
const client = create(new URL('http://127.0.0.1:5002'))
// call Core API methods
const { cid } = await client.add('Hello world!')
js-kubo-rpc-client
will not implement the IPFS Core API. Please see https://github.com/ipfs/kubo/issues/9125 for more information.
All core API methods take additional options
specific to the HTTP API:
headers
- An object or Headers instance that can be used to set custom HTTP headers. Note that this option can also be configured globally via the constructor options.searchParams
- An object or URLSearchParams
instance that can be used to add additional query parameters to the query string sent with each request.ipfs.getEndpointConfig()
Call this on your client instance to return an object containing the host
, port
, protocol
and api-path
.
Aside from the default export, js-kubo-rpc-client
exports various types and utilities that are included in the bundle:
multiaddr
multibase
multicodec
multihash
CID
globSource
(not available in the browser)urlSource
These can be accessed like this, for example:
const { CID } = require('js-kubo-rpc-client')
// ...or from an es-module:
import { CID } from 'js-kubo-rpc-client'
A utility to allow files on the file system to be easily added to IPFS.
globSource(path, pattern, [options])
path
: A path to a single file or directory to glob frompattern
: A pattern to match files under path
options
: Optional optionsoptions.hidden
: Hidden/dot files (files or folders starting with a .
, for example, .git/
) are not included by default. To add them, use the option { hidden: true }
.Returns an async iterable that yields { path, content }
objects suitable for passing to ipfs.add
.
import { create, globSource } from 'ipfs'
const ipfs = await create()
for await (const file of ipfs.addAll(globSource('./docs', '**/*'))) {
console.log(file)
}
/*
{
path: 'docs/assets/anchor.js',
cid: CID('QmVHxRocoWgUChLEvfEyDuuD6qJ4PhdDL2dTLcpUy3dSC2'),
size: 15347
}
{
path: 'docs/assets/bass-addons.css',
cid: CID('QmPiLWKd6yseMWDTgHegb8T7wVS7zWGYgyvfj7dGNt2viQ'),
size: 232
}
...
*/
A utility to allow content from the internet to be easily added to IPFS.
urlSource(url)
url
: A string URL or URL
instance to send HTTP GET request toReturns an async iterable that yields { path, content }
objects suitable for passing to ipfs.add
.
import { create, urlSource } from 'js-kubo-rpc-client'
const ipfs = create()
const file = await ipfs.add(urlSource('https://ipfs.io/images/ipfs-logo.svg'))
console.log(file)
/*
{
path: 'ipfs-logo.svg',
cid: CID('QmTqZhR6f7jzdhLgPArDPnsbZpvvgxzCZycXK7ywkLxSyU'),
size: 3243
}
*/
To interact with the API, you need to have a local daemon running. It needs to be open on the right port. 5001
is the default, and is used in the examples below, but it can be set to whatever you need.
# Show the ipfs config API port to check it is correct
> ipfs config Addresses.API
/ip4/127.0.0.1/tcp/5001
# Set it if it does not match the above output
> ipfs config Addresses.API /ip4/127.0.0.1/tcp/5001
# Restart the daemon after changing the config
# Run the daemon
> ipfs daemon
import { create } from 'js-kubo-rpc-client'
// connect to ipfs daemon API server
const ipfs = create('http://localhost:5001') // (the default in Node.js)
// or connect with multiaddr
const ipfs = create('/ip4/127.0.0.1/tcp/5001')
// or using options
const ipfs = create({ host: 'localhost', port: '5001', protocol: 'http' })
// or specifying a specific API path
const ipfs = create({ host: '1.1.1.1', port: '80', apiPath: '/ipfs/api/v0' })
through Browserify
Same as in Node.js, you just have to browserify the code before serving it. See the browserify repo for how to do that.
See the example in the examples folder to get a boilerplate.
through webpack
See the example in the examples folder to get an idea on how to use js-kubo-rpc-client
with webpack.
from CDN
Instead of a local installation (and browserification) you may request a remote copy of IPFS API from jsDelivr.
To always request the latest version, use one of the following examples:
<!-- loading the minified version using jsDelivr -->
<script src="https://cdn.jsdelivr.net/npm/js-kubo-rpc-client/dist/index.min.js"></script>
For maximum security you may also decide to:
Example:
<script
src="https://www.jsdelivr.com/package/npm/js-kubo-rpc-client"
integrity="sha384-5bXRcW9kyxxnSMbOoHzraqa7Z0PQWIao+cgeg327zit1hz5LZCEbIMx/LWKPReuB"
crossorigin="anonymous"
></script>
CDN-based IPFS API provides the IpfsHttpClient
constructor as a method of the global window
object. Example:
const ipfs = window.IpfsHttpClient({ host: 'localhost', port: 5001 })
If you omit the host and port, the client will parse window.host
, and use this information. This also works, and can be useful if you want to write apps that can be run from multiple different gateways:
const ipfs = window.IpfsHttpClient()
If you wish to send custom headers with each request made by this library, for example, the Authorization header. You can use the config to do so:
const ipfs = create({
host: 'localhost',
port: 5001,
protocol: 'http',
headers: {
authorization: 'Bearer ' + TOKEN
}
})
To set a global timeout for all requests pass a value for the timeout
option:
// Timeout after 10 seconds
const ipfs = create({ timeout: 10000 })
// Timeout after 2 minutes
const ipfs = create({ timeout: '2m' })
// see https://www.npmjs.com/package/parse-duration for valid string values
We run tests by executing npm test
in a terminal window. This will run both Node.js and Browser tests, both in Chrome and PhantomJS. To ensure that the module conforms with the interface-ipfs-core
spec, we run the batch of tests provided by the interface module, which can be found here.
This module started as a direct mapping from the go-ipfs cli to a JavaScript implementation, although this was useful and familiar to a lot of developers that were coming to IPFS for the first time, it also created some confusion on how to operate the core of IPFS and have access to the full capacity of the protocol. After much consideration, we decided to create interface-ipfs-core
with the goal of standardizing the interface of a core implementation of IPFS, and keep the utility functions the IPFS community learned to use and love, such as reading files from disk and storing them directly to IPFS.
Licensed under either of
Contributions welcome! Please check out the issues.
Also see our contributing document 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.
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.
1.0.0 (2022-09-06)
pubsub
commands sent over HTTP RPC to fix data corruption caused by topic names and payload bytes that included \n
. More details in https://github.com/ipfs/go-ipfs/issues/7939 and https://github.com/ipfs/go-ipfs/pull/8183undefined
values will be coerced to null
ipfs.dag.put
no longer accepts a format
arg, it is now storeCodec
and inputCodec
. 'json'
has become 'dag-json'
, 'cbor'
has become 'dag-cbor'
and so onipfs.add
or single items to ipfs.addAll
(n.b. you can still pass a list of a single item to ipfs.addAll
)globSource(dir, opts)
to globSource(dir, pattern, opts)
ipfs.get
has changed and the recursive
option has been removed from ipfs.ls
since it was not supported everywhereCo-authored-by: Rod Vagg rod@vagg.org Co-authored-by: achingbrain alex@achingbrain.net
ipfs.files.ls
are now strings, in line with the docs but different to previous behaviourCo-authored-by: Geoffrey Cohler g.cohler@computer.org
remove support for key.export over the http api
Where we used to accept all and any HTTP methods, now only POST is accepted. The API client will now only send POST requests too.
test: add tests to make sure we are post-only
chore: upgrade ipfs-utils
fix: return 405 instead of 404 for bad methods
fix: reject browsers that do not send an origin
Also fixes running interface tests over http in browsers against js-ipfs
ipfs.files.stat(path)
was a hamt sharded dir, the resovled
value returned by js-ipfs previously had a type
property of with a value of
'hamt-sharded-directory'
. To bring it in line with go-ipfs this value is now
'directory'
.--cid-version=1
or --raw-leaves=true
previously returned a CID that resolved to
a raw node (e.g. a buffer). Returned CIDs now resolve to a dag-pb
node that contains a UnixFS entry. This is to allow setting metadata
on small files with CIDv1.aegir check-project
updates (39f89c6)FAQs
A client library for the IPFS HTTP API
The npm package js-kubo-rpc-client receives a total of 3 weekly downloads. As such, js-kubo-rpc-client popularity was classified as not popular.
We found that js-kubo-rpc-client demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
Security News
cURL and Go security teams are publicly rejecting CVSS as flawed for assessing vulnerabilities and are calling for more accurate, context-aware approaches.
Security News
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.