Code Monkey home page Code Monkey logo

js-ipfs-http-client's Introduction

🔒 Archived

The contents of this repo have been merged into ipfs/js-ipfs.

Please open issues or submit PRs there.

IPFS http client lib logo

The JavaScript HTTP client library for IPFS implementations.



A client library for the IPFS HTTP API, implemented in JavaScript. This client library implements the interface-ipfs-core enabling applications to change between an embedded js-ipfs node and any remote IPFS node without having to change the code. In addition, this client library implements a set of utility functions.

Lead Maintainer

Alan Shaw.

Table of Contents

Install

This module uses node.js, and can be installed through npm:

npm install --save ipfs-http-client

We support both the Current and Active LTS versions of Node.js. Please see nodejs.org for what these currently are.

Running the daemon with the right port

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

Importing the module and usage

const ipfsClient = require('ipfs-http-client')

// connect to ipfs daemon API server
const ipfs = ipfsClient('http://localhost:5001') // (the default in Node.js)

// or connect with multiaddr
const ipfs = ipfsClient('/ip4/127.0.0.1/tcp/5001')

// or using options
const ipfs = ipfsClient({ host: 'localhost', port: '5001', protocol: 'http' })

// or specifying a specific API path
const ipfs = ipfsClient({ host: '1.1.1.1', port: '80', apiPath: '/ipfs/api/v0' })

Importing a sub-module and usage

const bitswap = require('ipfs-http-client/src/bitswap')('/ip4/127.0.0.1/tcp/5001')

const list = await bitswap.wantlist(key)
// ...

In a web browser

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-ipfs-http-client with webpack.

from CDN

Instead of a local installation (and browserification) you may request a remote copy of IPFS API from unpkg CDN.

To always request the latest version, use the following:

<script src="https://unpkg.com/ipfs-http-client/dist/index.min.js"></script>

Note: remove the .min from the URL to get the human-readable (not minified) version.

For maximum security you may also decide to:

  • reference a specific version of IPFS API (to prevent unexpected breaking changes when a newer latest version is published)
  • generate a SRI hash of that version and use it to ensure integrity
  • set the CORS settings attribute to make anonymous requests to CDN

Example:

<script src="https://unpkg.com/[email protected]/dist/index.js"
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()

CORS

In a web browser IPFS HTTP client (either browserified or CDN-based) might encounter an error saying that the origin is not allowed. This would be a CORS ("Cross Origin Resource Sharing") failure: IPFS servers are designed to reject requests from unknown domains by default. You can whitelist the domain that you are calling from by changing your ipfs config like this:

$ ipfs config --json API.HTTPHeaders.Access-Control-Allow-Origin  '["http://example.com"]'
$ ipfs config --json API.HTTPHeaders.Access-Control-Allow-Methods '["PUT", "POST", "GET"]'

Custom Headers

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 = ipfsClient({
  host: 'localhost',
  port: 5001,
  protocol: 'http',
  headers: {
    authorization: 'Bearer ' + TOKEN
  }
})

Global Timeouts

To set a global timeout for all requests pass a value for the timeout option:

// Timeout after 10 seconds
const ipfs = ipfsClient({ timeout: 10000 })
// Timeout after 2 minutes
const ipfs = ipfsClient({ timeout: '2m' })
// see https://www.npmjs.com/package/parse-duration for valid string values

Usage

API

IPFS Core API Compatible

js-ipfs-http-client follows the spec defined by interface-ipfs-core, which concerns the interface to expect from IPFS implementations. This interface is a currently active endeavor. You can use it today to consult the methods available.

Files

Graph

Network

Node Management

Additional Options

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.
  • signal - An AbortSignal that can be used to abort the request on demand.
  • timeout - A number or string specifying a timeout for the request. If the timeout is reached before data is received a TimeoutError is thrown. If a number is specified it is interpreted as milliseconds, if a string is passed, it is intepreted according to parse-duration. 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.

Instance Utils

  • ipfs.getEndpointConfig()

Call this on your client instance to return an object containing the host, port, protocol and api-path.

Static Types and Utils

Aside from the default export, ipfs-http-client exports various types and utilities that are included in the bundle:

These can be accessed like this, for example:

const { CID } = require('ipfs-http-client')
// ...or from an es-module:
import { CID } from 'ipfs-http-client'
Glob source

A utility to allow files on the file system to be easily added to IPFS.

globSource(path, [options])
  • path: A path to a single file or directory to glob from
  • options: Optional options
  • options.recursive: If path is a directory, use option { recursive: true } to add the directory and all its sub-directories.
  • options.ignore: To exclude file globs from the directory, use option { ignore: ['ignore/this/folder/**', 'and/this/file'] }.
  • options.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.

Example
const IpfsHttpClient = require('ipfs-http-client')
const { globSource } = IpfsHttpClient
const ipfs = IpfsHttpClient()

for await (const file of ipfs.add(globSource('./docs', { recursive: true }))) {
  console.log(file)
}
/*
{
  path: 'docs/assets/anchor.js',
  cid: CID('QmVHxRocoWgUChLEvfEyDuuD6qJ4PhdDL2dTLcpUy3dSC2'),
  size: 15347
}
{
  path: 'docs/assets/bass-addons.css',
  cid: CID('QmPiLWKd6yseMWDTgHegb8T7wVS7zWGYgyvfj7dGNt2viQ'),
  size: 232
}
...
*/
URL source

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 to

Returns an async iterable that yields { path, content } objects suitable for passing to ipfs.add.

Example
const IpfsHttpClient = require('ipfs-http-client')
const { urlSource } = IpfsHttpClient
const ipfs = IpfsHttpClient()

for await (const file of ipfs.add(urlSource('https://ipfs.io/images/ipfs-logo.svg'))) {
  console.log(file)
}
/*
{
  path: 'ipfs-logo.svg',
  cid: CID('QmTqZhR6f7jzdhLgPArDPnsbZpvvgxzCZycXK7ywkLxSyU'),
  size: 3243
}
*/

Development

Testing

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.

Contribute

The js-ipfs-http-client is a work in progress. As such, there's a few things you can do right now to help out:

  • Check out the existing issues!
  • Perform code reviews. More eyes will help a) speed the project along b) ensure quality and c) reduce possible future bugs.
  • Add tests. There can never be enough tests. Note that interface tests exist inside interface-ipfs-core.
  • Contribute to the FAQ repository with any questions you have about IPFS or any of the relevant technology. A good example would be asking, 'What is a merkledag tree?'. If you don't know a term, odds are, someone else doesn't either. Eventually, we should have a good understanding of where we need to improve communications and teaching together to make IPFS and IPNS better.

Want to hack on IPFS?

Historical context

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.

License

MIT

FOSSA Status

js-ipfs-http-client's People

Contributors

achingbrain avatar alanshaw avatar ckeenan avatar daviddias avatar dignifiedquire avatar dirkmc avatar fbaiodias avatar gavinmcdermott avatar greenkeeper[bot] avatar greenkeeperio-bot avatar hacdias avatar hackergrrl avatar harlantwood avatar hugomrdias avatar jacobheun avatar jbenet avatar krl avatar lidel avatar magik6k avatar mappum avatar michaelmure avatar nginnever avatar olizilla avatar pgte avatar richardlitt avatar richardschneider avatar travisperson avatar vasco-santos avatar victorb avatar vmx avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

js-ipfs-http-client's Issues

No timeout on ipfs.ls is called

Is there a reason why no timeout implemented on the http.request? My problem is: If I request an unavailable hash, the request and its socket stays open forever, which causes problems.

Tested on DigitalOcean Ubuntu 14.04 and OS X 10.10 With node 4.0.0 and ipfs version 0.3.8-dev.
In the following code the callback is never called:

var ipfsApi = require('ipfs-api');

var ipfs = ipfsApi('localhost','5001');

var start = (new Date());
console.log( 'started on ' + start );

ipfs.ls("QmPzWqn4ZRhGFJCTnNvaz3Wwe6inKQ4n6nJic4bAw3maz3", function( err, res ) {
  console.log('got error after '+((new Date() - start)), err );
});

403 forbidden when post file from form

Hello to you all.
I am failry new with IPFS, and I am still strugling with things...

Right now, I am trying to make a very simple web service to post any file in ipfs.
Cool.

My apache configuration is the following:

  • I have a domain: http://ipfs.stadja.net
  • this domain is used to serve the html file post form AND the IPFS api.
  • so in my domain I have a proxypass from / to my ipfs listening port
  • and i have a /local alias to serve my form

my form is the following and is accessible at http://ipfs.stadja.net/local

<form action="/api/v0/add" method="post" enctype="multipart/form-data">
    Select image to upload:
    <input type="file" name="fileToUpload" id="fileToUpload">
    <input type="submit" value="Upload Image" name="submit">
</form>

So when I submit my form, the response I have is:

403 forbidden

Why ???????

My configuration works well,
http://ipfs.stadja.net/webui serves me the webui right...
Any one have any idea ? :)

[0.3.7] .ping returns streaming JSON in the browser

Request:

http://127.0.0.1:56191/api/v0/ping?n=1&arg=QmWkMgYviPiHjfCdKkG4mmYotr5zJ

Response:

{
  "Success": false,
  "Time": 0,
  "Text": "PING QmWkMgYviPiHjfCdKkG4mmYotr5zJn3vVpMpYFCaezFQVH."
}{
  "Success": true,
  "Time": 1341453,
  "Text": ""
}{
  "Success": false,
  "Time": 0,
  "Text": "Average latency: 1.34ms"
}

Allow buffers to be given a name

When doing ipfs.add we should allow buffers to be named.

ipfs.add([{name: 'filename.txt', data: new Buffer('file contents')}], function(...) {}

This will bring the supported input to

  • Filename
  • Raw buffer
  • Named buffer

source linting

The webui uses standard for linting.

I would support using this for all of the javascript in the ipfs ecosystem, and will prepare a pull request if noone opposes this.

Configurable http.request protocol option

Running ipfs-api from an on-disk "file:///.../index.html" does not work. Calling http.request without passing a protocol in the opts, it will default to window.location.protocol. This causes the following error:
TypeError: Failed to execute 'fetch' on 'Window': Failed to parse URL from file://127.0.0.1:5001/api/...

see dist/ipfsapi.js:5837

Could we add a way to explicitly set this protocol option? It could be passed as an opt in the requestAPI or included as a param of IpfsAPI. The latter might be preferable for this scenario so not to require a protocol opt for every method.. maybe it could be IpfsAPI(host_or_multiaddr, port, opts)?

circleCI doesn't like es6

snipped taken from circleCI logs. @dignifiedquire @victorbjelkholm thoughts? :)

> gulp test

[16:20:06] Using gulpfile ~/js-ipfs-api/gulpfile.js
[16:20:06] Starting 'test'...
[16:20:06] Starting 'test:node'...
[16:20:06] Starting 'daemons:start'...

/home/ubuntu/js-ipfs-api/node_modules/ipfsd-ctl/node_modules/ipfs-api/src/request-api.js:3
const request = require('request')
^^^^^
[16:20:07] 'daemons:start' errored after 233 ms
[16:20:07] SyntaxError: Use of const in strict mode.
    at Module._compile (module.js:439:25)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)

files api

Add the files API option to node-ipfs-api

» ipfs files --help                                            

    ipfs files - Manipulate unixfs files

SUBCOMMANDS:

    ipfs files cp <source> <dest>  - copy files into mfs
    ipfs files ls <path>           - List directories
    ipfs files mkdir <path>        - make directories
    ipfs files stat <path>         - display file status
    ipfs files rm <path>...        - remove a file
    ipfs files read <path>         - Read a file in a given mfs
    ipfs files write <path> <data> - Write to a mutable file in a given filesystem
    ipfs files mv <source> <dest>  - Move files

    Use 'ipfs files <subcmd> --help' for more information about each command.

DESCRIPTION:

    Files is an API for manipulating ipfs objects as if they were a unix filesystem.

/api/files/{ls,read,write,mkdir,rm...}

note: only available on dev0.4.0 branch for now

Make examples tested

At the moment we have an examples folder but they might be outdated or broken without us knowing, so we should set up our tests to execute them to ensure they do what is expected.

Add URLs directly

ipfs add in the cli can add from the filesystem (makes sense, given the cli context). I think the js api on the browser should be able to add from the web (makes sense, given the web context).

So, i would like this to work:

ipfs.add('http://i.imgur.com/YTSpZdP.gif', function(err, res) { ... })

I think it should work on Node, too, given that node is js and already straddles the boundaries between unix and the web.

There is also an argument to be made re adding this natively to the go-ipfs cli too. (i.e. making the cmds lib accept files on http as just other files)

Convert to use streams

Currently everywhere in this module requires either a buffer or a file on disk to be used. I think we should switch to this take streams and allow the user to decide how to create that stream to pass in. This will greatly help with the ipfs-blob-store module, as well as allow this module to be more versatile.

Streams are easy to make from buffers and strings

var stream = require('stream')
var buffer = new Buffer('foo')
var bufferStream = new stream.PassThrough()
bufferStream.end(buffer)
bufferStream.pipe(process.stdout)

Source: http://stackoverflow.com/a/27964353/1489655

Thoughts?

/cc @krl

Tests failing on CI

On Circle CI the tests are failing to start up, in the before block:

npm test

> [email protected] test /home/ubuntu/node-ipfs-api
> mocha

pfs node api
    1) "before all" hook

  1) ipfs node api "before all" hook:
     Uncaught Error: spawn ENOENT
      at errnoException (child_process.js:1001:11)
      at Process.ChildProcess._handle.onexit (child_process.js:792:34)

Not sure why. Googled some, but still not sure. Anyone feeling more clueful on this? @krl or @travisperson perhaps?

ipfs.add(config.project, { recursive: true }, function (err, res) {}) is having a byzantine behaviour

ipfs.add when in recursive mode fails, sometimes silently, sometimes with a EPIPE error

Example from ipsurge (which does ipfs.add(config.project, { recursive: true }, function (err, res) {}) to the project folder

» ipsurge publish                                                                           
project: src/public
[ { Name: 'bootstrap-3.3.1/css/bootstrap-theme.css',
    Hash: 'QmYxvCd18WxWiKENoVaQWc28GWBZGa2fhTtFSRejhSwFj7' },
  { Name: 'bootstrap-3.3.1/css/bootstrap-theme.css.map',
    Hash: 'QmX6HipPP2vgx49MMkR6d5U3pocEZu6QJ3SpD72cWYaVbT' },
  { Name: 'bootstrap-3.3.1/css/bootstrap-theme.min.css',
    Hash: 'Qme5EFm8fgraoXKTUNDZZPJ6qdrjABxnyPQWUob5MsP1Gu' },
  { Name: 'bootstrap-3.3.1/css/bootstrap.css',
    Hash: 'QmVNfoG6KQ9bfkAi3Hj5vNGr6cB3NXHW3Vi9Wa4hVMCfgL' },
  { Name: 'bootstrap-3.3.1/css/bootstrap.css.map',
    Hash: 'QmRoVxqYhK8jgMedNwG8rCvvx6AbJzhBKx1tsRdv8ohV86' },
  { Name: 'bootstrap-3.3.1/css/bootstrap.min.css',
    Hash: 'QmfQorLd6NvVB53MKfSLGL2ZSgGZMWrF85qyWbB3vScvkX' },
  { Name: 'bootstrap-3.3.1/css',
    Hash: 'QmRFipREcWDPhSpxb5pEAbnxxRLcQgRoyxen5VWGyiZj4q' },
  { Name: 'bootstrap-3.3.1/fonts/glyphicons-halflings-regular.eot',
    Hash: 'Qmbn92gFfZ6Pte6NYgJisE4yBA7Km1zPbkW6xscpFBB9yX' },
  { Name: 'bootstrap-3.3.1/fonts/glyphicons-halflings-regular.svg',
    Hash: 'QmVYcwoiq3JeRHj38JhioijZju4ZJHHQZGbmpPykRtvD13' },
  { Name: 'bootstrap-3.3.1/fonts/glyphicons-halflings-regular.ttf',
    Hash: 'QmPrrwHNiSVDKn7xyLxkoeS9qQh3rkE96PfMGmTUTXQXx2' },
  { Name: 'bootstrap-3.3.1/fonts/glyphicons-halflings-regular.woff',
    Hash: 'Qmac6nKjPgEMDPg8MuEfgJskonyxLX5vR8phJ6cDqE9Zgc' },
  { Name: 'bootstrap-3.3.1/fonts',
    Hash: 'QmfYd1zuEoxjSK4FoH578vDQrfJ3zeM6EN52HWYP2EN7Ue' } ]
# note: there was more files to add, but it just exited
» ipsurge publish                                                                           
project: src/public
[ { Name: 'bootstrap-3.3.1/css/bootstrap-theme.css',
    Hash: 'QmYxvCd18WxWiKENoVaQWc28GWBZGa2fhTtFSRejhSwFj7' },
  { Name: 'bootstrap-3.3.1/css/bootstrap-theme.css.map',
    Hash: 'QmX6HipPP2vgx49MMkR6d5U3pocEZu6QJ3SpD72cWYaVbT' },
  { Name: 'bootstrap-3.3.1/css/bootstrap-theme.min.css',
    Hash: 'Qme5EFm8fgraoXKTUNDZZPJ6qdrjABxnyPQWUob5MsP1Gu' } ]
err { [Error: write EPIPE]
  code: 'EPIPE',
  errno: 'EPIPE',
  syscall: 'write',
  address: undefined }

Only works well with fewer files

ipfs add example failure

for example:

https://github.com/ipfs/node-ipfs-api/blob/master/examples/add.js

i am getting this error trace:

path.js:439
throw new TypeError('Arguments to path.resolve must be strings');
^
TypeError: Arguments to path.resolve must be strings
at Object.posix.resolve (path.js:439:13)
at Object.posix.relative (path.js:505:16)
at addFile (/home/dan/node_modules/ipfs-api/multipartdir.js:56:23)
at MultipartDir (/home/dan/node_modules/ipfs-api/multipartdir.js:14:5)
at getFileStream (/home/dan/node_modules/ipfs-api/index.js:151:12)
at send (/home/dan/node_modules/ipfs-api/index.js:47:16)
at Object.add (/home/dan/node_modules/ipfs-api/index.js:170:14)
at Object. (/home/dan/repos/haskell-ipfs-api/playground/contact_ipfs.js:15:6)
at Module._compile (module.js:460:26)
at Object.Module._extensions..js (module.js:478:10)

Compiling for the browser

When compiling to a browser module, to then be imported to my app
example... import ipfsApi from 'ipfs-api/browser-ipfs';
where I tuck the compiled browser-ipfs file inside the node_modules/ folder and import aka require.

I get nothing. I log ipfsApi I get an empty object.

Can you provide a working example where its not just sitting as script tag in an html file, not sure if it works that way but thats what browserify suggest to do.

Would like to see it actually getting pulled into a proper ES6/Webpack/Gulp etc app that aims to spit out one JS file. Thats how modern development work for browsers these days.

Thanks!
Jeff

Uncaught Error: connect ECONNREFUSED

Happens when trying to use ipfs-api on a stopped daemon. In the browser, with ipfsapi.min.js, it is "Uncaught (in promise) TypeError: Failed to fetch"

Would be nice to trap this so I can prompt user to check their ipfs connection

findprovs returns object or string

using the method ipfs.dht.findprovs in node environment does not work like one would expect. Using the following code:

var ipfsAPI = require('ipfs-api')

var ipfs = ipfsAPI('localhost', '5001')

const hash = "QmXFmCEhamPHvpbzQ3HMFABrpAXgE7D9jzp6QtvyC8Xp55"

ipfs.dht.findprovs(hash, (err, res) => {
    if(err) throw err
    console.log(res)
    console.log(typeof res)
    console.log("Streamable?", !!res.readable)
    console.log('So what to do with `res` here?')
})

I get the following output sometimes (which works correctly):

object
Streamable? false
So what to do with `res` here?

but other times, it returns this (which is neither streamable or valid json)

string
Streamable? false
So what to do with `res` here?

Originally open issue ipfs/kubo#1851 on go-ipfs but seems to be a bug in this library.

Buffers are now implemented in pure JS

Node.js 4.1 came out and:

Buffers are now created in JavaScript, rather than C++. This increases the speed of buffer creation (Trevor Norris) #2866.
https://github.com/nodejs/node/blob/v4.1.0/CHANGELOG.md

This doesn't mean we will have the type Buffer as native in JS, but it does mean that the perf between browser and Node.js will be the same (or at least very close).

Just wanted to make sure that one touches this module, knows it :) //cc @krl

CLI?

What

A CLI for node-ipfs-api

Why

I want to write better docs for this, although that may be being worked on in #58. I was looking through the code line by line, and I think that it would be useful to have the methods docs I am writing in not only README, but in some sort of CLI, like we have with ipfs/go-ipfs.

How

It wouldn't be too hard to get a CLI going for this, and that might be useful for running tests and checking docs before using this in the browser.

Open questions

  • Would this result in confusion, given that go-ipfs already has a CLI?
  • Would this result in code duplication?
  • Is there a better way to display and test methods than in a CLI and a README.md combo?

ipfs-api webcomponent

The way i do ipfs-api webcomponent now is like this:

<script src='ipfsapi.min.js'></script>
<dom-module id="ipfs-api">
  <script>
    (function () {
      Polymer({
        is: 'ipfs-api',
        properties: {
          host: String,
          port: Number
        },
        url: function (path) {
          return 'http://' + this.host + ':' + this.port + path
        },
        ready: function() {
          if (!this.host) {
            var split = window.location.host.split(":")
            this.host = split[0]
            this.port = split[1] || 80
          }
          this.api = ipfsAPI(this.host, this.port)
        }
      });
    })();
  </script>
</dom-module>

This is non-ideal since it also exposes a global ipfsAPI, for self-containing reasons it would therefore be nice to have a build step, that browserifies the code + adds the polymer init stuff. We should probably also add the api commands directly to the webmodule object, instead of in .api

The reason i think it makes sense to actually publish the api as a webcomponent, is that you can use it in your browser directly, without any build steps required.

Any thought on this?
@diasdavid @jbenet

Use of const in strict mode

SyntaxError: /Users/aakilfernandes/projects/safemarket/grunt-ipfs/node_modules/ipfs-api/src/request-api.js:3
const request = require('request')
^^^^^
Use of const in strict mode.

using 0.12.7

Sometimes ECONNRESET Error is thrown

Only sometimes I get this error on ipfs.ls(<hash>, cb ) and sometimes on ipfs.pin. In special when a set of executions is run.

I20151004-17:56:25.538(2)? catched error on ipfs ls { [Error: read ECONNRESET] code: 'ECONNRESET', errno: 'ECONNRESET', syscall: 'read' }
W20151004-17:56:25.539(2)? (STDERR) Possibly unhandled Error: read ECONNRESET
W20151004-17:56:25.539(2)? (STDERR)     at errnoException (net.js:905:11)
W20151004-17:56:25.539(2)? (STDERR)     at TCP.onread (net.js:559:19)

Any Idea what this could be?

tests fail on master

$ npm test

> [email protected] test /Users/david/Documents/Toptal/clients/Eris-Industries/node-ipfs-api
> mocha



  ipfs node api
    1) "before all" hook

  0 passing (4s)
  1 failing

  1) ipfs node api "before all" hook:
     Error: the object {
  "Code": 0
  "Message": "blockservice: key not found"
  "uncaught": true
} was thrown, throw an Error :)




npm ERR! Test failed.  See above for more details.

[suggestion] Usage of ES2015 features available in node@4

As we already restrict the node version to be > 4 it seems very reasonable to me to start using the available ES2015 features shipping with v8 (list can be found here)

This means adding two things to support it:

  1. Use babel-eslint parser for eslint instead of the standard one, as that supports all features needed.
  2. Add babelify to the browserify toolchain to ensure the code still works in all browsers.

window.ipfsApi undefined in browser

Re docs:

Make the ipfsapi.min.js available through your server and load it using a normal <script> tag, this will export the ipfsAPI constructor on the window object, such that...

However, I am seeing console.log(window.ipfsAPI) // undefined

Is documentation up to date for in-browser usage? I am trying to test out #55 (comment)

dist/{ipfsapi.js, ipfsapi.min.js} not actually compiled

The files dist/ipfsapi.min.js and dist/ipfsapi.js are not containing the libraries code anymore, since commit f6a7da6. The only thing I can see in the files is the standard browserify code, without anything about ipfs. Seems like we're missing to source the correct files in the build-step.

Add a readme.md

I'd love to build off of this for a gulp plugin, but I don't see any documentation.

Trouble with base64 encoded buffers via browserified ipfsApi

Looking up a base64 encoded buffer works fine with the node ipfs-api module. In the browser, it appears there's a problem converting it to a string from Uint8Array. Here is some code that encounters this

var gifData = 'R0lGODlhAQABAIAAAAUEBAAAACwAAAAAAQABAAACAkQBADs=';

ipfsApi.add(new Buffer(gifData, 'base64'), function(err, ipfsArr) {
    if (err) throw err;

    ipfsApi.cat(ipfsArr[0].Hash, function(err, blobStr) {
      if (err) throw err;
      console.log('equal:', Buffer(blobStr, 'ascii').toString('base64') === gifData);   // false
    });
});

This change -- [https://github.com/ckeenan/node-ipfs-api/commit/0a9290a17e970b8a447cd0ba4b3a202693a1dcb5] -- resolves it, but unsure if best solution

Implement pin.remove to allow for passing the recursive options.

The pin.remove command needs to be able to send the recursive options, and probably the pin.add command as well.

Something along the lines

remove: function(hash, opts, cb) {
  if(typeof opts === 'function') {
    cb = opts
    opts = null
  }

  send('pin/rm', hash, opts, null, cb)
}

Build folder structures given path with names.

This will be possible given #3 is incorporated. Folder sturctures can be send to IFPS by nesting multipart/form-data structures into a single part.

Example request:

User-Agent: /node-ipfs-webui/0.1.0
Content-Type: multipart/form-data; boundary=knyfc6alrmu9dx6rfis020litgknvcxr

--knyfc6alrmu9dx6rfis020litgknvcxr
Content-Type: multipart/form-data; boundary=lugqob2u0zjb57b9nz5l6ggohxrjxlxr
Content-Disposition: folder; name="folder"; filename="afolder"

--lugqob2u0zjb57b9nz5l6ggohxrjxlxr
Content-Type: application/octet-stream
Content-Disposition: file; name="file"; filename="FileA.txt"

A file inside of a folder!
--lugqob2u0zjb57b9nz5l6ggohxrjxlxr
Content-Type: application/octet-stream
Content-Disposition: file; name="file"; filename="FileB.txt"

Another file inside of a folder!
--lugqob2u0zjb57b9nz5l6ggohxrjxlxr--
--knyfc6alrmu9dx6rfis020litgknvcxr--

Promises API

Most environments now support the more pleasant API of Promise ( https://developer.mozilla.org/en-US/docs/Mozilla/JavaScript_code_modules/Promise.jsm/Promise ), that is easier to handle and chain together. We should think about migrating to start using promises instead of callbacks where applicable.

List of browsers that supports it current: https://developer.mozilla.org/en-US/docs/Mozilla/JavaScript_code_modules/Promise.jsm/Promise
(IE doesn't support it + some olders versions of other browsers, probably have to provide an existing shim as well...)

In node, promises have been natively supported since 0.12 so shouldn't be an issue there.

add (with recursive: true) returns improperly-formatted JSON

The add method (with recursive: true) isn't returning properly-formatted JSON for me. Instead I get a string that almost looks like JSON, but isn't actually, so to parse it I have to do some string mangling. Example:

var ipfs = require("ipfs-api")("localhost", "5001");
ipfs.add("./ipfs", {recursive: true}, function (err, res) {
            if (err || !res) return console.error(err);
            console.log(res);
});

This prints out:

{
  "Name": "funcrusher.json",
  "Hash": "QmUSpeNRKrYHRG55WvYTi5u3VVHaP3uHtH1mDKCB9Qa9AJ"
}{
  "Name": "ipfs",
  "Hash": "QmYuDw2SxfWkFasmmenzsdKSV7QVHAEuevAuCjcDh8kEYt"
}{
  "Name": "",
  "Hash": "QmWike1KhJRzY4EMtreidZDgq1EhYpLY444pfVJxxD3wSp"
}

(Note the lack of commas between the objects, and lack of enclosing array brackets.)

[discussion] library choice changes -- requests, vinyl

As some might have seen or read I'm currently suggesting to move away from using raw http.request and if possible to drop the code in src/get-files-stream.js, which is mostly using vinyl to generate a nested multipart requests of the files to be uploaded to the API. But as @jbenet pointed out on IRC I have not properly explained why I make these suggestions and why I think doing so would be very beneficial for this module.

To understand my reasoning let me formulate the main goals for this project as I understand them:

  1. Provide a small and simple wrapper around the http API of ipfs daemon
  2. 100% compatibility between node and browser
  3. Do not reinvent/reimplement the wheel, unless there is a very good reason to do so

Part 1: Drop direct usage of http.request

A lot of the code in this project is reimplementing things that already have been implemented and battle tested in other modules, when it comes to the usage of http.request. Two main projects come here to mind request and superagent. As explored in #92 the only request module that will do everything we need and has excellent support for node and browser seems to be request. The initial reason for exploring these options for me was #69 which has strong implications for the stability of station.

Part 2: Drop vinyl if possible

First vinyl on its own is a representation of the file system in memory and a nice API around it to access it. This taken on itself is nothing bad in any way, but does not need to be in this module in my opinion as it adds additional bloat which in the browser has very little relevance, as the power of vinyl comes from it being able to actually access the file system in the background to read it in.

we'll still need something like it (a js object to represent a file before reading it all in).
@jbenet (IRC)

From my understanding, we do not need a representation of a file object in this API wrapper. Application code around this API might very well need it, but for the purposes of uploading files via the http API there are more straight forward ways to do it:

  1. [browser][node]: The content of a file and the filename are passed: We can directly generate a multipart request from this.
  2. [node]: The path to a file is passed: Use fs.createReadStream to read the file from the file system and generate the multipart stream
  3. [node]: The path of a directory is passed: Use something like glob to walk the directory and convert into the same structure as 2.
  4. [browser]: The content of a directory of files is passed: Use the conversion from 3. to generate the multipart request.

I hope this explanation makes my thinking and efforts more understandable and sorry again for not writing this up earlier but parts of this were simply only understood by me when doing the research and work.

Problem with [ ] characters in path

Adding a file at a path that contains open and close square brackets doesn't work. No error, but the response name is blank and the hash points to an empty directory.

Steps to reproduce:

ipfs.add('[TEST]/test.txt', function(err, res) {
    console.log(err, res);
})

I'm expecting this response:

[ { Name: 'text.txt',
Hash: 'Qmazm4yVXBrbJfLsscsNdTUwZ6yFMfjNoV26vKHMyjm9qw' } ]

but get this instead:

[ { Name: '',
Hash: 'QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn' } ]

which points to an empty directory.

Can't connect to individual nodes with many instances

So, I'm trying to have a couple of IPFS machines to mirror some content. So I've setup two boxes to test, and assign them to different variables and I expected that I would be able to call them separately but there is apparently some sharing in between them...

Example:

var ipfsAPI = require('ipfs-api')

var d1 = ipfsAPI('/ip4/127.0.0.1/tcp/5001')
var d2 = ipfsAPI('/ip4/127.0.0.2/tcp/5001')

d1.id(function(err, res) {
    console.log(res.ID) // => id for d2
})
d2.id(function(err, res) {
    console.log(res.ID) // => id for d2
})

The last one is the one being used.

ipfs.add should always return same type

ipfs.add have a kind of confused API (the same with some other methods), that it's sometimes returning a stream and sometimes an object. I think this is because of the environment but we should try to make sure that inside the callback, users don't have to try to figure out what type it is, it should always be the same type.

Will also help simplifying the tests for browser/node environments.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.