Skip to content

Commit

Permalink
Merge pull request #1 from pelle/bye-bye-streams
Browse files Browse the repository at this point in the history
Remove dependency on ipfs-api
  • Loading branch information
Nick Dodson authored Feb 2, 2017
2 parents 362c5ac + b4dc7e3 commit 0176fb8
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 139 deletions.
113 changes: 42 additions & 71 deletions dist/ipfs-mini.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/ipfs-mini.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/ipfs-mini.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 2 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -177,12 +177,10 @@
"webpack": "2.1.0-beta.15"
},
"browser": {
"./lib/lib/add.js": "./lib/lib/add.browser.js",
"./src/lib/add.js": "./src/lib/add.browser.js"
"xmlhttprequest": false
},
"dependencies": {
"w3c-xmlhttprequest": "2.1.2",
"ipfs-api": "12.1.0"
"xmlhttprequest": "^1.8.0"
},
"lint-staged": {
"lint:eslint": "*.js"
Expand Down
50 changes: 39 additions & 11 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
const XMLHttpRequest = require('w3c-xmlhttprequest').XMLHttpRequest;
const add = require('./lib/add.js');
let XMLHttpRequest; // eslint-disable-line

// browser
if (typeof window !== 'undefined' && window.XMLHttpRequest) {
XMLHttpRequest = window.XMLHttpRequest; // eslint-disable-line

// node
} else {
XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest; // eslint-disable-line
}

module.exports = IPFS;

Expand Down Expand Up @@ -44,12 +52,6 @@ IPFS.prototype.sendAsync = function sendAsync(opts, cb) {
const options = opts || {};
const callback = cb || function emptyCallback() {};

request.open('POST', `${self.requestBase}${opts.uri}`);

if (options.accept) {
request.setRequestHeader('accept', options.accept);
}

request.onreadystatechange = () => {
if (request.readyState === 4 && request.timeout !== 1) {
if (request.status !== 200) {
Expand All @@ -65,22 +67,48 @@ IPFS.prototype.sendAsync = function sendAsync(opts, cb) {
};

if (options.payload) {
request.enctype = 'multipart/form-data';
request.open('POST', `${self.requestBase}${opts.uri}`);
} else {
request.open('GET', `${self.requestBase}${opts.uri}`);
}

if (options.accept) {
request.setRequestHeader('accept', options.accept);
}

if (options.payload && options.boundary) {
request.setRequestHeader('Content-Type', `multipart/form-data; boundary=${options.boundary}`);
request.send(options.payload);
} else {
request.send();
}
};

/**
* creates a boundary that isn't part of the payload
*/
function createBoundary(data) {
while (true) {
const boundary = `----IPFSMini${Math.random() * 100000}.${Math.random() * 100000}`;
if (data.indexOf(boundary) === -1) {
return boundary;
}
}
}

/**
* Add an string or buffer to IPFS
* @param {String|Buffer} `input` a single string or buffer
* @param {Function} `callback` a callback, with (error, ipfsHash String)
* @callback {String} `ipfsHash` returns an IPFS hash string
*/
IPFS.prototype.add = function addData(input, callback) {
const self = this;
add(input, self, callback);
const data = ((typeof input === 'object' && input.isBuffer) ? input.toString('binary') : input);
const boundary = createBoundary(data);
const payload = `--${boundary}\r\nContent-Disposition: form-data; name="path"\r\nContent-Type: application/octet-stream\r\n\r\n${data}\r\n--${boundary}--`;

const addCallback = (err, result) => callback(err, (!err ? result.Hash : null));
this.sendAsync({ jsonParse: true, accept: 'application/json', uri: '/add', payload, boundary }, addCallback);
};

/**
Expand Down
15 changes: 0 additions & 15 deletions src/lib/add.browser.js

This file was deleted.

16 changes: 0 additions & 16 deletions src/lib/add.js

This file was deleted.

Loading

0 comments on commit 0176fb8

Please sign in to comment.