Skip to content

Commit 119493e

Browse files
authored
Dev (#1)
* base soter-mini * btfs-mini * update readme * update .gitignore and remove node_modules
1 parent 6924556 commit 119493e

9 files changed

+343
-3
lines changed

.DS_Store

6 KB
Binary file not shown.

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ bower_components
3838
build/Release
3939

4040
# Dependency directories
41-
node_modules/
41+
/node_module/*
4242
jspm_packages/
4343

4444
# TypeScript v1 declaration files

README.md

+111-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,111 @@
1-
# soter-sdk-mini
2-
soter node sdk
1+
## BTFS-mini
2+
3+
<div>
4+
<!-- Dependency Status -->
5+
<a href="https://david-dm.org/SilentCicero/BTFS-mini">
6+
<img src="https://david-dm.org/SilentCicero/BTFS-mini.svg"
7+
alt="Dependency Status" />
8+
</a>
9+
10+
<!-- devDependency Status -->
11+
<a href="https://david-dm.org/SilentCicero/BTFS-mini#info=devDependencies">
12+
<img src="https://david-dm.org/SilentCicero/BTFS-mini/dev-status.svg" alt="devDependency Status" />
13+
</a>
14+
15+
<!-- Build Status -->
16+
<a href="https://travis-ci.org/SilentCicero/BTFS-mini">
17+
<img src="https://travis-ci.org/SilentCicero/BTFS-mini.svg"
18+
alt="Build Status" />
19+
</a>
20+
21+
<!-- NPM Version -->
22+
<a href="https://www.npmjs.org/package/BTFS-mini">
23+
<img src="http://img.shields.io/npm/v/BTFS-mini.svg"
24+
alt="NPM version" />
25+
</a>
26+
27+
<!-- Test Coverage -->
28+
<a href="https://coveralls.io/r/SilentCicero/BTFS-mini">
29+
<img src="https://coveralls.io/repos/github/SilentCicero/BTFS-mini/badge.svg" alt="Test Coverage" />
30+
</a>
31+
32+
<!-- Javascript Style -->
33+
<a href="http://airbnb.io/javascript/">
34+
<img src="https://img.shields.io/badge/code%20style-airbnb-brightgreen.svg" alt="js-airbnb-style" />
35+
</a>
36+
</div>
37+
38+
<br />
39+
40+
A super tiny module for querying an btfsnode, that works in the browser and in Node. Only **2.76 kB** compressed!
41+
42+
## Install
43+
44+
```
45+
npm install --save btfs-sdk-mini
46+
```
47+
48+
## Usage
49+
50+
```js
51+
const btfs= require('btfs-sdk-mini');
52+
const btfs= new BTFS({ host: 'xxxx', port: 5001, protocol: 'https' });
53+
54+
BTFS.add('hello world!').then(console.log).catch(console.log);
55+
56+
// result null 'QmTp2hEo8eXRp6wg7jXv1BLCMh5a4F3B7buAUZNZUu772j'
57+
58+
// result null 'hello world!'
59+
60+
BTFS.addJSON({ somevalue: 2, name: 'Nick' }, (err, result) => {
61+
console.log(err, result);
62+
});
63+
64+
// result null 'QmTp2hEo8eXRp6wg7jXv1BLCMh5a4F3B7buAUZNZUu772j'
65+
66+
```
67+
68+
69+
## Examples
70+
71+
An example of the module in use for the browser, can be found in [./example](./examples).
72+
73+
Inside is a single, no configuration required, HTML file using the `btfs-sdk-mini` module.
74+
75+
## Browser Usage
76+
77+
`btfs-sdk-mini` is completely browserifiable and webpack ready. The main export found in our distributions [dist](./dist) folder is `BTFS`.
78+
79+
```
80+
<html>
81+
<body>
82+
<script type="text/javascript" src="btfs-mini.min.js">
83+
<script type="text/javascript">
84+
var btfs= new BTFS({ provider: 'xxxx', protocol: 'https' });
85+
86+
// ...
87+
</script>
88+
</body>
89+
</html>
90+
```
91+
92+
## API Design
93+
94+
### add
95+
96+
97+
Result output `BTFSHash` **String**.
98+
99+
```js
100+
const btfs= require('BTFS-mini');
101+
const btfs= new BTFS({ host: 'BTFS.infura.io', port: 5001, protocol: 'https' });
102+
103+
BTFS.add('hello world!', (err, result) => {
104+
console.log(err, result);
105+
});
106+
107+
// result null 'QmTp2hEo8eXRp6wg7jXv1BLCMh5a4F3B7buAUZNZUu772j'
108+
```
109+
110+
111+

examples/index.html

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<html>
2+
<body>
3+
<h2>btfs-mini</h2>
4+
<h3>Browser Example</h3>
5+
6+
7+
<hr />
8+
9+
<h4>Add String Data</h4>
10+
<textarea id="addInputData" placeholder="Hello world!"></textarea>
11+
12+
<br />
13+
14+
<button id="addData">Add Data</button>
15+
16+
<br /><br />
17+
18+
<div id="addResponse"></div>
19+
20+
<script type="text/javascript" src="../dist/btfs-sdk-mini.js"></script>
21+
<script type="text/javascript">
22+
var el = function(id){ return document.querySelector(id); };
23+
var btfs = new BTFS({ host: '18.163.127.219', protocol: 'http' });
24+
25+
26+
el('#addData').addEventListener('click', function(){
27+
var isJSON = false;
28+
var inputData = el('#addInputData').value;
29+
30+
btfs.add(inputData, (err, result) => {
31+
if (err) {
32+
el('#addResponse').innerHTML = 'Hmm.. there was an error: ' + String(err);
33+
} else {
34+
el('#addResponse').innerHTML = result;
35+
}
36+
});
37+
});
38+
</script>
39+
</body>
40+
</html>

package.json

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"name": "btfs-mini",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"build": "webpack --config webpack.config.js",
8+
"test": "echo \"Error: no test specified\" && exit 1"
9+
},
10+
"author": "",
11+
"license": "ISC",
12+
"dependencies": {
13+
"fs": "0.0.1-security",
14+
"uglify-js": "^3.7.7",
15+
"uglifyjs-webpack-plugin": "^2.2.0",
16+
"xmlhttprequest": "^1.8.0"
17+
},
18+
"browser": {
19+
"xmlhttprequest": false,
20+
"./src/lib/XMLHttpRequest.js": "./src/lib/XMLHttpRequest-browser.js",
21+
"./lib/lib/XMLHttpRequest.js": "./lib/lib/XMLHttpRequest-browser.js"
22+
},
23+
"devDependencies": {
24+
"uglify-es": "^3.3.9",
25+
"webpack": "^4.41.5",
26+
"webpack-cli": "^3.3.10"
27+
}
28+
}

src/index.js

+140
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
var XMLHttpRequest = require('./lib/XMLHttpRequest');
2+
3+
module.exports = BTFS;
4+
5+
/**
6+
* The varructor object
7+
* @param {Object} `provider` the provider object
8+
* @return {Object} `BTFS` returns an BTFS instance
9+
* @throws if the `new` flag is not used
10+
*/
11+
function BTFS(provider) {
12+
if (!(this instanceof BTFS)) { throw new Error('[BTFS-mini] BTFS instance must be instantiated with "new" flag (e.g. var BTFS = new BTFS("http://localhost:8545");).'); }
13+
14+
var self = this;
15+
self.setProvider(provider || {});
16+
}
17+
18+
/**
19+
* No operation method
20+
*/
21+
function noop() {}
22+
function newPromise(val) { return new Promise(val); }
23+
function noopPromise(val) { val(noop, noop); }
24+
25+
/**
26+
* Sets the provider of the BTFS instance
27+
* @param {Object} `provider` the provider object
28+
* @throws if the provider object is not an object
29+
*/
30+
BTFS.prototype.setProvider = function setProvider(provider) {
31+
if (typeof provider !== 'object') { throw new Error(`[ifpsjs] provider must be type Object, got '${typeof provider}'.`); }
32+
var self = this;
33+
var data = self.provider = Object.assign({
34+
host: '127.0.0.1',
35+
pinning: true,
36+
port: '5001',
37+
protocol: 'http',
38+
base: '/api/v0' }, provider || {});
39+
self.requestBase = String(`${data.protocol}://${data.host}:${data.port}${data.base}`);
40+
};
41+
42+
/**
43+
* Sends an async data packet to an BTFS node
44+
* @param {Object} `opts` the options object
45+
* @param {Function} `cb` the provider callback
46+
* @callback returns an error if any, or the data from BTFS
47+
*/
48+
BTFS.prototype.sendAsync = function sendAsync(opts, cb) {
49+
var self = this;
50+
var request = new XMLHttpRequest(); // eslint-disable-line
51+
var options = opts || {};
52+
53+
return (cb ? noopPromise : newPromise)(function (resolve, reject) {
54+
function callback(e, r){
55+
(cb || noop)(e, options.takeHash ? r.Hash : r);
56+
if (e) return reject(e);
57+
if (!e && r) return resolve(options.takeHash ? r.Hash : r);
58+
};
59+
60+
request.onreadystatechange = function () {
61+
if (request.readyState === 4 && request.timeout !== 1) {
62+
if (request.status !== 200) {
63+
callback(new Error(`[BTFS-mini] status ${request.status}: ${request.responseText}`), null);
64+
} else {
65+
try {
66+
callback(null, (options.jsonParse ? JSON.parse(request.responseText) : request.responseText));
67+
} catch (jsonError) {
68+
callback(new Error(`[BTFS-mini] while parsing data: '${String(request.responseText)}', error: ${String(jsonError)} with provider: '${self.requestBase}'`, null));
69+
}
70+
}
71+
}
72+
};
73+
74+
try {
75+
var pinningURI = self.provider.pinning && opts.uri == '/add' ? '?pin=true' : '';
76+
77+
if (options.payload) {
78+
request.open('POST', `${self.requestBase}${opts.uri}`);
79+
} else {
80+
request.open('GET', `${self.requestBase}${opts.uri}`);
81+
}
82+
83+
if (options.accept) {
84+
request.setRequestHeader('accept', options.accept);
85+
}
86+
87+
if (options.payload && options.boundary) {
88+
request.setRequestHeader('Content-Type', `multipart/form-data; boundary=${options.boundary}`);
89+
request.send(options.payload);
90+
} else {
91+
request.send();
92+
}
93+
} catch (err) {
94+
callback(err, null);
95+
}
96+
});
97+
};
98+
99+
/**
100+
* creates a boundary that isn't part of the payload
101+
*/
102+
function createBoundary(data) {
103+
while (true) {
104+
var boundary = `----BTFSMini${Math.random() * 100000}.${Math.random() * 100000}`;
105+
if (data.indexOf(boundary) === -1) {
106+
return boundary;
107+
}
108+
}
109+
}
110+
111+
/**
112+
* Add an string or buffer to BTFS
113+
* @param {String|Buffer} `input` a single string or buffer
114+
* @param {Function} `callback` a callback, with (error, BTFSHash String)
115+
* @callback {String} `BTFSHash` returns an BTFS hash string
116+
*/
117+
BTFS.prototype.add = function addData(input, callback) {
118+
var data = ((typeof input === 'object' && input.isBuffer) ? input.toString('binary') : input);
119+
var boundary = createBoundary(data);
120+
var payload = `--${boundary}\r\nContent-Disposition: form-data; name="path"\r\nContent-Type: application/octet-stream\r\n\r\n${data}\r\n--${boundary}--`;
121+
122+
return this.sendAsync({
123+
jsonParse: true,
124+
accept: 'application/json',
125+
uri: '/add',
126+
takeHash: true,
127+
payload, boundary,
128+
}, callback);
129+
};
130+
131+
/**
132+
* Add an JSON object to BTFS
133+
* @param {Object} `jsonData` a single JSON object
134+
* @param {Function} `callback` a callback, with (error, BTFSHash String)
135+
* @callback {String} `BTFSHash` returns an BTFS hash string
136+
*/
137+
BTFS.prototype.addJSON = function addJson(jsonData, callback) {
138+
var self = this;
139+
return self.add(JSON.stringify(jsonData), callback);
140+
};

src/lib/XMLHttpRequest-browser.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const XMLHttpRequest = window.XMLHttpRequest; // eslint-disable-line
2+
3+
module.exports = XMLHttpRequest;

src/lib/XMLHttpRequest.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest;
2+
3+
module.exports = XMLHttpRequest;

webpack.config.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const path = require('path');
2+
3+
module.exports = {
4+
mode: 'production',
5+
entry: './src/index.js',
6+
output: {
7+
path: path.resolve(__dirname, 'dist'),
8+
library: 'BTFS',
9+
libraryTarget: 'umd',
10+
filename: 'btfs-sdk-mini.js'
11+
},
12+
target: "web",
13+
optimization: {
14+
minimize: false
15+
}
16+
17+
};

0 commit comments

Comments
 (0)