Beta 7 with some repo names changed.

This commit is contained in:
Luis Fernando Molina committed 2020-11-15 13:05:19 +01:00
1 parent 0f7b40cca7
commit 963c19493a
3408 files changed
+462217 -15516

No files matched your search

+67
View File
@@ -0,0 +1,67 @@
{
"windows-amd64": {
"archive": "swarm-windows-amd64-1.6.7.exe",
"binaryMD5": "c2d827dc4553d9b91a7d6c1d5a6140fd",
"archiveMD5": "059196d21548060a18a12e17cc0ee59a"
},
"linux-amd64": {
"archive": "swarm-linux-amd64-1.6.7",
"binaryMD5": "85002d79b8ebc2d2f2f10fb198636a81",
"archiveMD5": "3e8874299ab8c0e3043d70ebb6673879"
},
"linux-386": {
"archive": "swarm-linux-386-1.6.7",
"binaryMD5": "35bc2ab976f60f96a2cede117e0df19d",
"archiveMD5": "7868a86c9cbdf8ac7ac2e5682b4ce40f"
},
"darwin-amd64": {
"archive": "swarm-darwin-amd64-1.6.7",
"binaryMD5": "c499b186645229260dd6ab685dd58f07",
"archiveMD5": "0794d111e5018eac3b657bcb29851121"
},
"linux-arm5": {
"archive": "swarm-linux-arm5-1.6.7",
"binaryMD5": "516fcd85246c905529442cd9b689c12f",
"archiveMD5": "47312708d417cb196b07ba0af1d3abb4"
},
"linux-arm6": {
"archive": "swarm-linux-arm6-1.6.7",
"binaryMD5": "82ff7bdbe388b4a190f4101c5150d3b4",
"archiveMD5": "350276de7bb175a15c314cfc4cb7f8fd"
},
"linux-mips": {
"archive": "swarm-linux-mips-1.6.7",
"binaryMD5": "e1e95280441c0ca35633927792ef5317",
"archiveMD5": "8fb4b64e94cd73aa718db787b9d4c53e"
},
"linux-arm7": {
"archive": "swarm-linux-arm7-1.6.7",
"binaryMD5": "bfc0b4d1c86d8a975af052fc7854bdd3",
"archiveMD5": "4378641d8e1e1fbb947f941c8fca8613"
},
"linux-arm64": {
"archive": "swarm-linux-arm64-1.6.7",
"binaryMD5": "bbac21a6c6fa8208f67ca4123d3f948a",
"archiveMD5": "4e503160327c5fbcca0414f17c54e5ee"
},
"linux-mipsle": {
"archive": "swarm-linux-mipsle-1.6.7",
"binaryMD5": "a82f191b2f9d2c470d0273219c820657",
"archiveMD5": "3016bdb6d237ae654c0cdf36fe85dc7c"
},
"windows-386": {
"archive": "swarm-windows-386-1.6.7.exe",
"binaryMD5": "ce0b34640642e58068ae5a359faef102",
"archiveMD5": "640aede4da08a3a9d8a6ac0434ba7c0f"
},
"linux-mips64": {
"archive": "swarm-linux-mips64-1.6.7",
"binaryMD5": "9da967664f384817adb5083fd1ffe8f1",
"archiveMD5": "357a33be470f8f89ba2619957a08deff"
},
"linux-mips64le": {
"archive": "swarm-linux-mips64le-1.6.7",
"binaryMD5": "ec1abcf7b216e87645ec83954d8344cd",
"archiveMD5": "a81fd0158190d99813c738ffa4f87627"
}
}
+61
View File
@@ -0,0 +1,61 @@
// This uploads a DApp to the Swarm network *without* using the filesystem.
// That allows a web application running on the browser to upload any DApp.
// This will output a hash that can be accessed on Mist & similar.
const swarm = require("./..").at("http://swarm-gateways.net");
// The contents of index.html.
const indexHtml = new Buffer(
`<html>
<body>
<h3><img src="ethereum_icon.png"/> Swarm.js example DApp!</h3>
<p><a href="foo/test_text_1.txt">Test #1</a></p>
<p><a href="foo/test_text_2.txt">Test #2</a></p>
</body>
</html>`);
// For binary data, we can just use a buffer.
// This is a 16x16 Ethereum icon png.
const ethereumIconPng = new Buffer([
0x89,0x50,0x4e,0x47,0x0d,0x0a,0x1a,0x0a,0x00,0x00,0x00,0x0d,0x49,0x48,0x44,0x52,
0x00,0x00,0x00,0x10,0x00,0x00,0x00,0x10,0x08,0x04,0x00,0x00,0x00,0xb5,0xfa,0x37,
0xea,0x00,0x00,0x00,0xf7,0x49,0x44,0x41,0x54,0x28,0x91,0x63,0x60,0x40,0x01,0x0d,
0x09,0x0e,0x8e,0x0c,0xb8,0xc1,0x2c,0xa1,0xce,0x9b,0xc6,0xcf,0xa3,0xf8,0x70,0x2a,
0x98,0x90,0xd7,0xf9,0xdf,0xf0,0xbf,0x55,0x27,0x2e,0xfd,0x62,0x7d,0xff,0x40,0x0a,
0x4c,0xdf,0x3a,0x2a,0x62,0x55,0xd0,0x77,0xac,0xe7,0x3f,0x48,0x81,0xf1,0x7f,0xcb,
0xcd,0x58,0xa4,0x27,0xc6,0xf6,0xfc,0xe9,0xf9,0xdf,0x01,0x56,0x60,0xfc,0xdf,0x25,
0x04,0xdd,0x78,0x9e,0x89,0x97,0xbb,0xfe,0x17,0xff,0x0f,0xf8,0x2f,0xfb,0x5f,0xeb,
0xbf,0xd1,0x7f,0xf3,0xa7,0x81,0x42,0x28,0x0a,0x26,0x95,0x66,0xfd,0x77,0xf9,0x6f,
0xf5,0xdf,0xec,0xbf,0xf0,0x7f,0xd1,0xff,0x12,0xff,0xd5,0xfe,0x5b,0xd4,0xa0,0x28,
0xf0,0x5d,0x61,0xfa,0xd3,0xe2,0xbf,0xc5,0x7f,0x73,0xa0,0x02,0x11,0x10,0xfc,0xa6,
0xba,0x0c,0x45,0xc1,0x7c,0x97,0xdc,0x76,0x8b,0xf5,0x96,0x50,0x05,0x12,0x6b,0x0c,
0x5b,0x7c,0xfc,0x50,0x14,0x4c,0x15,0xea,0x3b,0xd7,0xbb,0xc6,0x2f,0xc4,0xfc,0x93,
0xe8,0x2f,0xdd,0x04,0xcb,0x8d,0xa6,0x0f,0x82,0x84,0xd1,0x9c,0x39,0xc9,0xa3,0xe7,
0x7f,0xff,0xab,0x96,0x74,0xc7,0x68,0xb3,0xe7,0xc6,0xff,0x5d,0x1d,0xb0,0x79,0xb4,
0xbe,0xe7,0x7f,0xf7,0x5f,0x23,0xa0,0x27,0x6d,0x8b,0xb0,0x06,0x14,0x30,0xa8,0xf7,
0x83,0x02,0xca,0x7c,0xaf,0x27,0x17,0x0e,0x05,0xd3,0x54,0x3b,0xdf,0x1a,0x7d,0x73,
0x35,0xc0,0x21,0x0d,0x02,0x4d,0x05,0xb6,0xe1,0xa8,0x22,0x00,0xbf,0x45,0x6f,0x1b,
0x57,0x23,0xc8,0x42,0x00,0x00,0x00,0x00,0x49,0x45,0x4e,0x44,0xae,0x42,0x60,0x82]);
// Some test .txt file.
const testText1 = new Buffer("test text #1");
// Other test .txt file.
const testText2 = new Buffer("test text #2");
// The DApp is just an object mapping routes to contents, which can be either
// strings or buffers. Mime types are inferred from the extension. You can also
// force them by using `{"type": "application/json", "content": "[1,2,3]"}`, for
// example. Notice the empty route also pointing to index.html.
const exampleDApp = {
"" : {type: "text/html", data: indexHtml},
"/index.html" : {type: "text/html", data: indexHtml},
"/ethereum_icon.png" : {type: "image/png", data: ethereumIconPng},
"/foo/test_text_1.txt" : {type: "text/plain", data: testText1},
"/foo/test_text_2.txt" : {type: "text/plain", data: testText2}
}
// Now we just upload it.
swarm.upload(exampleDApp)
.then(console.log)
.catch(console.log);
@@ -0,0 +1,29 @@
var $jscomp=$jscomp||{};$jscomp.scope={};$jscomp.ASSUME_ES5=!1;$jscomp.ASSUME_NO_NATIVE_MAP=!1;$jscomp.ASSUME_NO_NATIVE_SET=!1;$jscomp.defineProperty=$jscomp.ASSUME_ES5||"function"==typeof Object.defineProperties?Object.defineProperty:function(a,e,f){a!=Array.prototype&&a!=Object.prototype&&(a[e]=f.value)};$jscomp.getGlobal=function(a){return"undefined"!=typeof window&&window===a?a:"undefined"!=typeof global&&null!=global?global:a};$jscomp.global=$jscomp.getGlobal(this);$jscomp.SYMBOL_PREFIX="jscomp_symbol_";
$jscomp.initSymbol=function(){$jscomp.initSymbol=function(){};$jscomp.global.Symbol||($jscomp.global.Symbol=$jscomp.Symbol)};$jscomp.Symbol=function(){var a=0;return function(e){return $jscomp.SYMBOL_PREFIX+(e||"")+a++}}();
$jscomp.initSymbolIterator=function(){$jscomp.initSymbol();var a=$jscomp.global.Symbol.iterator;a||(a=$jscomp.global.Symbol.iterator=$jscomp.global.Symbol("iterator"));"function"!=typeof Array.prototype[a]&&$jscomp.defineProperty(Array.prototype,a,{configurable:!0,writable:!0,value:function(){return $jscomp.arrayIterator(this)}});$jscomp.initSymbolIterator=function(){}};$jscomp.arrayIterator=function(a){var e=0;return $jscomp.iteratorPrototype(function(){return e<a.length?{done:!1,value:a[e++]}:{done:!0}})};
$jscomp.iteratorPrototype=function(a){$jscomp.initSymbolIterator();a={next:a};a[$jscomp.global.Symbol.iterator]=function(){return this};return a};$jscomp.makeIterator=function(a){$jscomp.initSymbolIterator();var e=a[Symbol.iterator];return e?e.call(a):$jscomp.arrayIterator(a)};
$jscomp.polyfill=function(a,e,f,h){if(e){f=$jscomp.global;a=a.split(".");for(h=0;h<a.length-1;h++){var c=a[h];c in f||(f[c]={});f=f[c]}a=a[a.length-1];h=f[a];e=e(h);e!=h&&null!=e&&$jscomp.defineProperty(f,a,{configurable:!0,writable:!0,value:e})}};$jscomp.FORCE_POLYFILL_PROMISE=!1;
$jscomp.polyfill("Promise",function(a){function e(){this.batch_=null}function f(b){return b instanceof c?b:new c(function(a,c){a(b)})}if(a&&!$jscomp.FORCE_POLYFILL_PROMISE)return a;e.prototype.asyncExecute=function(b){null==this.batch_&&(this.batch_=[],this.asyncExecuteBatch_());this.batch_.push(b);return this};e.prototype.asyncExecuteBatch_=function(){var b=this;this.asyncExecuteFunction(function(){b.executeBatch_()})};var h=$jscomp.global.setTimeout;e.prototype.asyncExecuteFunction=function(b){h(b,
0)};e.prototype.executeBatch_=function(){for(;this.batch_&&this.batch_.length;){var b=this.batch_;this.batch_=[];for(var a=0;a<b.length;++a){var c=b[a];b[a]=null;try{c()}catch(q){this.asyncThrow_(q)}}}this.batch_=null};e.prototype.asyncThrow_=function(b){this.asyncExecuteFunction(function(){throw b;})};var c=function(b){this.state_=0;this.result_=void 0;this.onSettledCallbacks_=[];var a=this.createResolveAndReject_();try{b(a.resolve,a.reject)}catch(k){a.reject(k)}};c.prototype.createResolveAndReject_=
function(){function b(b){return function(m){c||(c=!0,b.call(a,m))}}var a=this,c=!1;return{resolve:b(this.resolveTo_),reject:b(this.reject_)}};c.prototype.resolveTo_=function(b){if(b===this)this.reject_(new TypeError("A Promise cannot resolve to itself"));else if(b instanceof c)this.settleSameAsPromise_(b);else{a:switch(typeof b){case "object":var a=null!=b;break a;case "function":a=!0;break a;default:a=!1}a?this.resolveToNonPromiseObj_(b):this.fulfill_(b)}};c.prototype.resolveToNonPromiseObj_=function(a){var b=
void 0;try{b=a.then}catch(k){this.reject_(k);return}"function"==typeof b?this.settleSameAsThenable_(b,a):this.fulfill_(a)};c.prototype.reject_=function(a){this.settle_(2,a)};c.prototype.fulfill_=function(a){this.settle_(1,a)};c.prototype.settle_=function(a,c){if(0!=this.state_)throw Error("Cannot settle("+a+", "+c+"): Promise already settled in state"+this.state_);this.state_=a;this.result_=c;this.executeOnSettledCallbacks_()};c.prototype.executeOnSettledCallbacks_=function(){if(null!=this.onSettledCallbacks_){for(var a=
0;a<this.onSettledCallbacks_.length;++a)u.asyncExecute(this.onSettledCallbacks_[a]);this.onSettledCallbacks_=null}};var u=new e;c.prototype.settleSameAsPromise_=function(a){var b=this.createResolveAndReject_();a.callWhenSettled_(b.resolve,b.reject)};c.prototype.settleSameAsThenable_=function(a,c){var b=this.createResolveAndReject_();try{a.call(c,b.resolve,b.reject)}catch(q){b.reject(q)}};c.prototype.then=function(a,e){function b(a,b){return"function"==typeof a?function(b){try{h(a(b))}catch(n){f(n)}}:
b}var h,f,m=new c(function(a,b){h=a;f=b});this.callWhenSettled_(b(a,h),b(e,f));return m};c.prototype.catch=function(a){return this.then(void 0,a)};c.prototype.callWhenSettled_=function(a,c){function b(){switch(e.state_){case 1:a(e.result_);break;case 2:c(e.result_);break;default:throw Error("Unexpected state: "+e.state_);}}var e=this;null==this.onSettledCallbacks_?u.asyncExecute(b):this.onSettledCallbacks_.push(b)};c.resolve=f;c.reject=function(a){return new c(function(b,c){c(a)})};c.race=function(a){return new c(function(b,
c){for(var e=$jscomp.makeIterator(a),h=e.next();!h.done;h=e.next())f(h.value).callWhenSettled_(b,c)})};c.all=function(a){var b=$jscomp.makeIterator(a),e=b.next();return e.done?f([]):new c(function(a,c){function h(b){return function(c){m[b]=c;k--;0==k&&a(m)}}var m=[],k=0;do m.push(void 0),k++,f(e.value).callWhenSettled_(h(m.length-1),c),e=b.next();while(!e.done)})};return c},"es6","es3");
$jscomp.iteratorFromArray=function(a,e){$jscomp.initSymbolIterator();a instanceof String&&(a+="");var f=0,h={next:function(){if(f<a.length){var c=f++;return{value:e(c,a[c]),done:!1}}h.next=function(){return{done:!0,value:void 0}};return h.next()}};h[Symbol.iterator]=function(){return h};return h};$jscomp.polyfill("Array.prototype.entries",function(a){return a?a:function(){return $jscomp.iteratorFromArray(this,function(a,f){return[a,f]})}},"es6","es3");
$jscomp.polyfill("Array.prototype.keys",function(a){return a?a:function(){return $jscomp.iteratorFromArray(this,function(a){return a})}},"es6","es3");
(function(){function a(e,f,h){function c(b,k){if(!f[b]){if(!e[b]){var m="function"==typeof require&&require;if(!k&&m)return m(b,!0);if(u)return u(b,!0);k=Error("Cannot find module '"+b+"'");throw k.code="MODULE_NOT_FOUND",k;}k=f[b]={exports:{}};e[b][0].call(k.exports,function(a){return c(e[b][1][a]||a)},k,k.exports,a,e,f,h)}return f[b].exports}for(var u="function"==typeof require&&require,b=0;b<h.length;b++)c(h[b]);return c}return a})()({1:[function(a,e,f){e.exports=function(a){var c=a.fsp,e=a.files,
b=a.os,f=a.path,h=a.child_process,q=a.mimetype,T=a.defaultArchives,A=a.request,U=a.downloadUrl,w=a.bytes,L=a.hash,n=a.pick,V=function(a){return function(d){return function(g){return g[a]=d,g}}},B=function(a){return function(d){for(var g={},b=0,c=a.length;b<c;++b)g[a[b]]=d[b];return g}},X=function(a){return function(d){var g={},b;for(b in a)g[b]=a[b];for(var c in d)g[c]=d[c];return g}},Y=function(a){return function(d){if(a.length!==d.length)return!1;for(var b=0;b<a;++b)if(a[b]!==d[b])return!1;return!0}},
M=function(a){return function(d){return a+"/bzz-raw:/"+d}},r=function(a){return function(d){return A(M(a)(d),{responseType:"arraybuffer"}).then(function(a){a=new Uint8Array(a);if(Y(a)([52,48,52,32,112,97,103,101,32,110,111,116,32,102,111,117,110,100,10]))throw"Error 404.";return a})}},y=function(a){return function(d){return function W(d){return function(b){return function(g){var c=function(a){return void 0===a.path?Promise.resolve():"application/bzz-manifest+json"===a.contentType?W(a.hash)(b+a.path)(g):
Promise.resolve(V(b+a.path)({type:a.contentType,hash:a.hash})(g))};return r(a)(d).then(function(a){return JSON.parse(x(a)).entries}).then(function(a){return Promise.all(a.map(c))}).then(function(){return g})}}}(d)("")({})}},C=function(a){return function(d){return y(a)(d).then(function(a){return B(Object.keys(a))(Object.keys(a).map(function(d){return a[d].hash}))})}},D=function(a){return function(d){return y(a)(d).then(function(d){var b=Object.keys(d),g=b.map(function(a){return d[a].hash}),c=b.map(function(a){return d[a].type});
g=g.map(r(a));var e=function(a){return a.map(function(a,d){return{type:c[d],data:a}})};return Promise.all(g).then(function(a){return B(b)(e(a))})})}},z=function(a){return function(d){return function(b){return e.download(M(a)(d))(b)}}},E=function(a){return function(d){return function(b){return C(a)(d).then(function(d){var g=[],c;for(c in d)if(0<c.length){var e=f.join(b,c);g.push(z(a)(d[c])(e))}return Promise.all(g).then(function(){return b})})}}},p=function(a){return function(d){return A(a+"/bzz-raw:/",
{body:"string"===typeof d?F(d):d,method:"POST"})}},G=function(a){return function(d){return function(b){return function(g){return function Z(c){return A(a+"/bzz:/"+d+("/"===b[0]?b:"/"+b),{method:"PUT",headers:{"Content-Type":g.type},body:g.data}).then(function(a){if(-1!==a.indexOf("error"))throw a;return a}).catch(function(a){return 0<c&&Z(c-1)})}(3)}}}},v=function(a){return function(d){return t(a)({"":d})}},N=function(a){return function(d){return c.readFile(d).then(function(b){return v(a)({type:q.lookup(d),
data:b})})}},t=function(a){return function(d){return p(a)("{}").then(function(b){var c=function(b){return function(c){return G(a)(c)(b)(d[b])}};return Object.keys(d).reduce(function(a,d){return a.then(c(d))},Promise.resolve(b))})}},H=function(a){return function(d){return c.readFile(d).then(p(a))}},I=function(a){return function(d){return function(b){return e.directoryTree(b).then(function(a){return Promise.all(a.map(function(a){return c.readFile(a)})).then(function(d){var c=a.map(function(a){return a.slice(b.length)}),
g=a.map(function(a){return q.lookup(a)||"text/plain"});return B(c)(d.map(function(a,d){return{type:g[d],data:a}}))})}).then(function(a){return X(d?{"":a[d]}:{})(a)}).then(t(a))}}},O=function(a){return function(d){if("data"===d.pick)return n.data().then(p(a));if("file"===d.pick)return n.file().then(v(a));if("directory"===d.pick)return n.directory().then(t(a));if(d.path)switch(d.kind){case "data":return H(a)(d.path);case "file":return N(a)(d.path);case "directory":return I(a)(d.defaultFile)(d.path)}else{if(d.length||
"string"===typeof d)return p(a)(d);if(d instanceof Object)return t(a)(d)}return Promise.reject(Error("Bad arguments"))}},P=function(a){return function(d){return function(b){return aa(a)(d).then(function(c){return c?b?E(a)(d)(b):D(a)(d):b?z(a)(d)(b):r(a)(d)})}}},Q=function(a,c){var d=b.platform().replace("win32","windows")+"-"+("x64"===b.arch()?"amd64":"386");c=(c||T)[d];d=c.archiveMD5;var g=c.binaryMD5;return e.safeDownloadArchived(U+c.archive+".tar.gz")(d)(g)(a)},R=function(a){return new Promise(function(d,
b){var c=h.spawn,e=function(a){return function(d){return-1!==(""+d).indexOf(a)}},g=a.password,f=a.privateKey,k=0,l=c(a.binPath,["--bzzaccount",a.account||f,"--datadir",a.dataDir,"--ens-api",a.ensApi]);c=function(a){0===k&&e("Passphrase")(a)?setTimeout(function(){k=1;l.stdin.write(g+"\n")},500):e("Swarm http proxy started")(a)&&(k=2,clearTimeout(m),d(l))};l.stdout.on("data",c);l.stderr.on("data",c);var m=setTimeout(function(){return b(Error("Couldn't start swarm process."))},2E4)})},S=function(a){return new Promise(function(d,
b){a.stderr.removeAllListeners("data");a.stdout.removeAllListeners("data");a.stdin.removeAllListeners("error");a.removeAllListeners("error");a.removeAllListeners("exit");a.kill("SIGINT");var c=setTimeout(function(){return a.kill("SIGKILL")},8E3);a.once("close",function(){clearTimeout(c);d()})})},J=function(a){return p(a)("test").then(function(a){return"c9a99c7d326dcc6316f32fe2625b311f6dc49a175e6877681ded93137d3569e7"===a}).catch(function(){return!1})},aa=function(a){return function(b){return r(a)(b).then(function(a){try{return!!JSON.parse(x(a)).entries}catch(ba){return!1}})}},
l=function(a){return function(b,d,c,e,f){var g;"undefined"!==typeof b&&(g=a(b));"undefined"!==typeof d&&(g=a(d));"undefined"!==typeof c&&(g=a(c));"undefined"!==typeof e&&(g=a(e));"undefined"!==typeof f&&(g=a(f));return g}},x=function(a){return w.toString(w.fromUint8Array(a))},F=function(a){return w.toUint8Array(w.fromString(a))},K=function(a){return{download:function(b,c){return P(a)(b)(c)},downloadData:l(r(a)),downloadDataToDisk:l(z(a)),downloadDirectory:l(D(a)),downloadDirectoryToDisk:l(E(a)),downloadEntries:l(y(a)),
downloadRoutes:l(C(a)),isAvailable:function(){return J(a)},upload:function(b){return O(a)(b)},uploadData:l(p(a)),uploadFile:l(v(a)),uploadFileFromDisk:l(v(a)),uploadDataFromDisk:l(H(a)),uploadDirectory:l(t(a)),uploadDirectoryFromDisk:l(I(a)),uploadToManifest:l(G(a)),pick:n,hash:L,fromString:F,toString:x}};return{at:K,local:function(a){return function(b){return J("http://localhost:8500").then(function(c){return c?b(K("http://localhost:8500")).then(function(){}):Q(a.binPath,a.archives).onData(function(b){return(a.onProgress||
function(){})(b.length)}).then(function(){return R(a)}).then(function(a){return b(K("http://localhost:8500")).then(function(){return a})}).then(S)})}},download:P,downloadBinary:Q,downloadData:r,downloadDataToDisk:z,downloadDirectory:D,downloadDirectoryToDisk:E,downloadEntries:y,downloadRoutes:C,isAvailable:J,startProcess:R,stopProcess:S,upload:O,uploadData:p,uploadDataFromDisk:H,uploadFile:v,uploadFileFromDisk:N,uploadDirectory:t,uploadDirectoryFromDisk:I,uploadToManifest:G,pick:n,hash:L,fromString:F,
toString:x}}},{}]},{},[1]);
+36
View File
@@ -0,0 +1,36 @@
// THIS IS OUTDATED
// TODO: update it
const Swarm = require("./..");
const fs = require("fs");
const path = require("path");
const privateKeyPath = path.join(process.cwd(), "swarmPrivateKey");
// Writes a temporary private key file to disk
fs.writeFileSync(privateKeyPath, "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef");
// To run Swarm locally, you need a running Geth
// node and an Ethereum account/password
const swarmSetup = {
privateKey: privateKeyPath,
dataDir: process.env.HOME + "/Library/Ethereum/testnet",
ensApi: process.env.HOME + "/Library/Ethereum/testnet/geth.ipc",
binPath: process.env.HOME + "/.swarm/swarm"
};
// Magically starts a local Swarm node
// Downloads binaries if necessary
Swarm.local(swarmSetup)(swarm => new Promise((resolve, reject) => {
// Removes the temporary private key file
fs.unlinkSync(privateKeyPath);
// Uploads data using the local node
swarm.upload("test").then(hash => {
console.log("Uploaded data. Address:", hash);
// Closes the Swarm process.
resolve();
}).catch(e => console.log(e));
}))
.then(() => console.log("Done!"))
.catch(e => console.log(e));
+31
View File
@@ -0,0 +1,31 @@
const swarm = require("./..").at("http://swarm-gateways.net");
(async () => {
try {
// Uploading raw data
const file = "this is a test";
const fileHash = await swarm.upload(new Buffer(file))
console.log("Uploaded file. SwarmHash:", fileHash);
//// Downloading raw data
const fileBuffer = await swarm.download(fileHash);
console.log("Downloaded file. Contents:", swarm.toString(fileBuffer));
// Uploading directory
const dir = {
"/foo.txt": {type: "text/plain", data: "this is foo.txt"},
"/bar.txt": {type: "text/plain", data: "this is bar.txt"}
};
const dirHash = await swarm.upload(dir);
console.log("Uploaded directory. SwarmHash:", dirHash);
//// Downloaading a directory
const dirObj = await swarm.download(dirHash);
console.log("Downloaded directory. Contents:");
for (let path in dirObj)
console.log("-", path, ":", swarm.toString(dirObj[path].data));
} catch (e) {
console.log(e);
}
})();
+75
View File
@@ -0,0 +1,75 @@
let swarm = Swarm.at("http://swarm-gateways.net");
// Uploads a file or directory to the Swarm network
const upload = dir => {
swarm.upload(dir).then(print).catch(console.log);
};
// Downloads from Swarm and inserts on site
const download = () => {
const hash = prompt("Hash:");
if (hash) swarm.download(hash).then(print).catch(console.log);
};
// Sets the Swarm provider / gateway
const setProvider = e => {
swarm = Swarm.at(document.getElementById("provider").value);
};
// Pretty printers
const print = val => {
let p = document.createElement("p");
let o = document.getElementById("output");
p.appendChild(toHTML(val));
o.insertBefore(p, o.firstChild);
};
// Interprets data as text/png/jpg/raw/directory, builds an HTML
const toHTML = val => {
// Directory
if (val instanceof Object && !val.length) {
let table = document.createElement("table");
for (var key in val) {
let row = document.createElement("tr");
let cell0 = document.createElement("td");
let cell1 = document.createElement("td");
cell0.appendChild(toHTML(key));
cell1.appendChild(toHTML(val[key].data));
row.appendChild(cell0);
row.appendChild(cell1);
table.appendChild(row);
}
return table;
// String
} else if (typeof val === "string") {
let span = document.createElement("span");
span.innerHTML = val;
return span;
// Buffer
} else if (val.length) {
// PNG
if (val[1] === 80 && val[2] === 78 && val[3] === 71) {
let image = document.createElement("img");
image.src = "data:image/png;base64," + btoa(String.fromCharCode.apply(null,val));
return image;
// JPG
} else if (val[0] === 0xFF && val[1] === 0xD8 && val[val.length-2] === 0xFF && val[val.length-1] === 0xD9) {
let image = document.createElement("img");
image.src = "data:image/jpg;base64," + btoa(String.fromCharCode.apply(null,val));
return image;
// Plain text / binary data
} else {
let isText = true;
for (let i = 0; i < val.length; ++i)
if (val[i] < 32 || val[i] > 126)
isText = false;
return toHTML(isText
? [].map.call(val, c => String.fromCharCode(c)).join("")
: [].map.call(val, b => ("00"+b.toString(16)).slice(-2)).join(""));
};
};
};
+654
View File
@@ -0,0 +1,654 @@
(function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){
// TODO: this is a temporary fix to hide those libraries from the browser. A
// slightly better long-term solution would be to split this file into two,
// separating the functions that are used on Node.js from the functions that
// are used only on the browser.
module.exports = function (_ref) {
var fs = _ref.fs,
files = _ref.files,
os = _ref.os,
path = _ref.path,
child_process = _ref.child_process,
mimetype = _ref.mimetype,
defaultArchives = _ref.defaultArchives,
request = _ref.request,
downloadUrl = _ref.downloadUrl,
bytes = _ref.bytes,
hash = _ref.hash,
pick = _ref.pick;
// ∀ a . String -> JSON -> Map String a -o Map String a
// Inserts a key/val pair in an object impurely.
var impureInsert = function impureInsert(key) {
return function (val) {
return function (map) {
return map[key] = val, map;
};
};
}; // String -> JSON -> Map String JSON
// Merges an array of keys and an array of vals into an object.
var toMap = function toMap(keys) {
return function (vals) {
var map = {};
for (var i = 0, l = keys.length; i < l; ++i) {
map[keys[i]] = vals[i];
}
return map;
};
}; // ∀ a . Map String a -> Map String a -> Map String a
// Merges two maps into one.
var merge = function merge(a) {
return function (b) {
var map = {};
for (var key in a) {
map[key] = a[key];
}
for (var _key in b) {
map[_key] = b[_key];
}
return map;
};
}; // ∀ a . [a] -> [a] -> Bool
var equals = function equals(a) {
return function (b) {
if (a.length !== b.length) {
return false;
} else {
for (var i = 0, l = a.length; i < a; ++i) {
if (a[i] !== b[i]) return false;
}
}
return true;
};
}; // String -> String -> String
var rawUrl = function rawUrl(swarmUrl) {
return function (hash) {
return "".concat(swarmUrl, "/bzzr:/").concat(hash);
};
}; // String -> String -> Promise Uint8Array
// Gets the raw contents of a Swarm hash address.
var downloadData = function downloadData(swarmUrl) {
return function (hash) {
return request(rawUrl(swarmUrl)(hash), {
responseType: "arraybuffer"
}).then(function (arrayBuffer) {
var uint8Array = new Uint8Array(arrayBuffer);
var error404 = [52, 48, 52, 32, 112, 97, 103, 101, 32, 110, 111, 116, 32, 102, 111, 117, 110, 100, 10];
if (equals(uint8Array)(error404)) throw "Error 404.";
return uint8Array;
});
};
}; // type Entry = {"type": String, "hash": String}
// type File = {"type": String, "data": Uint8Array}
// String -> String -> Promise (Map String Entry)
// Solves the manifest of a Swarm address recursively.
// Returns a map from full paths to entries.
var downloadEntries = function downloadEntries(swarmUrl) {
return function (hash) {
var search = function search(hash) {
return function (path) {
return function (routes) {
// Formats an entry to the Swarm.js type.
var format = function format(entry) {
return {
type: entry.contentType,
hash: entry.hash
};
}; // To download a single entry:
// if type is bzz-manifest, go deeper
// if not, add it to the routing table
var downloadEntry = function downloadEntry(entry) {
if (entry.path === undefined) {
return Promise.resolve();
} else {
return entry.contentType === "application/bzz-manifest+json" ? search(entry.hash)(path + entry.path)(routes) : Promise.resolve(impureInsert(path + entry.path)(format(entry))(routes));
}
}; // Downloads the initial manifest and then each entry.
return downloadData(swarmUrl)(hash).then(function (text) {
return JSON.parse(toString(text)).entries;
}).then(function (entries) {
return Promise.all(entries.map(downloadEntry));
}).then(function () {
return routes;
});
};
};
};
return search(hash)("")({});
};
}; // String -> String -> Promise (Map String String)
// Same as `downloadEntries`, but returns only hashes (no types).
var downloadRoutes = function downloadRoutes(swarmUrl) {
return function (hash) {
return downloadEntries(swarmUrl)(hash).then(function (entries) {
return toMap(Object.keys(entries))(Object.keys(entries).map(function (route) {
return entries[route].hash;
}));
});
};
}; // String -> String -> Promise (Map String File)
// Gets the entire directory tree in a Swarm address.
// Returns a promise mapping paths to file contents.
var downloadDirectory = function downloadDirectory(swarmUrl) {
return function (hash) {
return downloadEntries(swarmUrl)(hash).then(function (entries) {
var paths = Object.keys(entries);
var hashs = paths.map(function (path) {
return entries[path].hash;
});
var types = paths.map(function (path) {
return entries[path].type;
});
var datas = hashs.map(downloadData(swarmUrl));
var files = function files(datas) {
return datas.map(function (data, i) {
return {
type: types[i],
data: data
};
});
};
return Promise.all(datas).then(function (datas) {
return toMap(paths)(files(datas));
});
});
};
}; // String -> String -> String -> Promise String
// Gets the raw contents of a Swarm hash address.
// Returns a promise with the downloaded file path.
var downloadDataToDisk = function downloadDataToDisk(swarmUrl) {
return function (hash) {
return function (filePath) {
return files.download(rawUrl(swarmUrl)(hash))(filePath);
};
};
}; // String -> String -> String -> Promise (Map String String)
// Gets the entire directory tree in a Swarm address.
// Returns a promise mapping paths to file contents.
var downloadDirectoryToDisk = function downloadDirectoryToDisk(swarmUrl) {
return function (hash) {
return function (dirPath) {
return downloadRoutes(swarmUrl)(hash).then(function (routingTable) {
var downloads = [];
for (var route in routingTable) {
if (route.length > 0) {
var filePath = path.join(dirPath, route);
downloads.push(downloadDataToDisk(swarmUrl)(routingTable[route])(filePath));
}
;
}
;
return Promise.all(downloads).then(function () {
return dirPath;
});
});
};
};
}; // String -> Uint8Array -> Promise String
// Uploads raw data to Swarm.
// Returns a promise with the uploaded hash.
var uploadData = function uploadData(swarmUrl) {
return function (data) {
return request("".concat(swarmUrl, "/bzzr:/"), {
body: typeof data === "string" ? fromString(data) : data,
method: "POST"
});
};
}; // String -> String -> String -> File -> Promise String
// Uploads a file to the Swarm manifest at a given hash, under a specific
// route. Returns a promise containing the uploaded hash.
// FIXME: for some reasons Swarm-Gateways is sometimes returning
// error 404 (bad request), so we retry up to 3 times. Why?
var uploadToManifest = function uploadToManifest(swarmUrl) {
return function (hash) {
return function (route) {
return function (file) {
var attempt = function attempt(n) {
var slashRoute = route[0] === "/" ? route : "/" + route;
var url = "".concat(swarmUrl, "/bzz:/").concat(hash).concat(slashRoute);
var opt = {
method: "PUT",
headers: {
"Content-Type": file.type
},
body: file.data
};
return request(url, opt).then(function (response) {
if (response.indexOf("error") !== -1) {
throw response;
}
return response;
}).catch(function (e) {
return n > 0 && attempt(n - 1);
});
};
return attempt(3);
};
};
};
}; // String -> {type: String, data: Uint8Array} -> Promise String
var uploadFile = function uploadFile(swarmUrl) {
return function (file) {
return uploadDirectory(swarmUrl)({
"": file
});
};
}; // String -> String -> Promise String
var uploadFileFromDisk = function uploadFileFromDisk(swarmUrl) {
return function (filePath) {
return fs.readFile(filePath).then(function (data) {
return uploadFile(swarmUrl)({
type: mimetype.lookup(filePath),
data: data
});
});
};
}; // String -> Map String File -> Promise String
// Uploads a directory to Swarm. The directory is
// represented as a map of routes and files.
// A default path is encoded by having a "" route.
var uploadDirectory = function uploadDirectory(swarmUrl) {
return function (directory) {
return uploadData(swarmUrl)("{}").then(function (hash) {
var uploadRoute = function uploadRoute(route) {
return function (hash) {
return uploadToManifest(swarmUrl)(hash)(route)(directory[route]);
};
};
var uploadToHash = function uploadToHash(hash, route) {
return hash.then(uploadRoute(route));
};
return Object.keys(directory).reduce(uploadToHash, Promise.resolve(hash));
});
};
}; // String -> Promise String
var uploadDataFromDisk = function uploadDataFromDisk(swarmUrl) {
return function (filePath) {
return fs.readFile(filePath).then(uploadData(swarmUrl));
};
}; // String -> Nullable String -> String -> Promise String
var uploadDirectoryFromDisk = function uploadDirectoryFromDisk(swarmUrl) {
return function (defaultPath) {
return function (dirPath) {
return files.directoryTree(dirPath).then(function (fullPaths) {
return Promise.all(fullPaths.map(function (path) {
return fs.readFile(path);
})).then(function (datas) {
var paths = fullPaths.map(function (path) {
return path.slice(dirPath.length);
});
var types = fullPaths.map(function (path) {
return mimetype.lookup(path) || "text/plain";
});
return toMap(paths)(datas.map(function (data, i) {
return {
type: types[i],
data: data
};
}));
});
}).then(function (directory) {
return merge(defaultPath ? {
"": directory[defaultPath]
} : {})(directory);
}).then(uploadDirectory(swarmUrl));
};
};
}; // String -> UploadInfo -> Promise String
// Simplified multi-type upload which calls the correct
// one based on the type of the argument given.
var _upload = function upload(swarmUrl) {
return function (arg) {
// Upload raw data from browser
if (arg.pick === "data") {
return pick.data().then(uploadData(swarmUrl)); // Upload a file from browser
} else if (arg.pick === "file") {
return pick.file().then(uploadFile(swarmUrl)); // Upload a directory from browser
} else if (arg.pick === "directory") {
return pick.directory().then(uploadDirectory(swarmUrl)); // Upload directory/file from disk
} else if (arg.path) {
switch (arg.kind) {
case "data":
return uploadDataFromDisk(swarmUrl)(arg.path);
case "file":
return uploadFileFromDisk(swarmUrl)(arg.path);
case "directory":
return uploadDirectoryFromDisk(swarmUrl)(arg.defaultFile)(arg.path);
}
; // Upload UTF-8 string or raw data (buffer)
} else if (arg.length || typeof arg === "string") {
return uploadData(swarmUrl)(arg); // Upload directory with JSON
} else if (arg instanceof Object) {
return uploadDirectory(swarmUrl)(arg);
}
return Promise.reject(new Error("Bad arguments"));
};
}; // String -> String -> Nullable String -> Promise (String | Uint8Array | Map String Uint8Array)
// Simplified multi-type download which calls the correct function based on
// the type of the argument given, and on whether the Swwarm address has a
// directory or a file.
var _download = function download(swarmUrl) {
return function (hash) {
return function (path) {
return isDirectory(swarmUrl)(hash).then(function (isDir) {
if (isDir) {
return path ? downloadDirectoryToDisk(swarmUrl)(hash)(path) : downloadDirectory(swarmUrl)(hash);
} else {
return path ? downloadDataToDisk(swarmUrl)(hash)(path) : downloadData(swarmUrl)(hash);
}
});
};
};
}; // String -> Promise String
// Downloads the Swarm binaries into a path. Returns a promise that only
// resolves when the exact Swarm file is there, and verified to be correct.
// If it was already there to begin with, skips the download.
var downloadBinary = function downloadBinary(path, archives) {
var system = os.platform().replace("win32", "windows") + "-" + (os.arch() === "x64" ? "amd64" : "386");
var archive = (archives || defaultArchives)[system];
var archiveUrl = downloadUrl + archive.archive + ".tar.gz";
var archiveMD5 = archive.archiveMD5;
var binaryMD5 = archive.binaryMD5;
return files.safeDownloadArchived(archiveUrl)(archiveMD5)(binaryMD5)(path);
}; // type SwarmSetup = {
// account : String,
// password : String,
// dataDir : String,
// binPath : String,
// ensApi : String,
// onDownloadProgress : Number ~> (),
// archives : [{
// archive: String,
// binaryMD5: String,
// archiveMD5: String
// }]
// }
// SwarmSetup ~> Promise Process
// Starts the Swarm process.
var startProcess = function startProcess(swarmSetup) {
return new Promise(function (resolve, reject) {
var spawn = child_process.spawn;
var hasString = function hasString(str) {
return function (buffer) {
return ('' + buffer).indexOf(str) !== -1;
};
};
var account = swarmSetup.account,
password = swarmSetup.password,
dataDir = swarmSetup.dataDir,
ensApi = swarmSetup.ensApi,
privateKey = swarmSetup.privateKey;
var STARTUP_TIMEOUT_SECS = 3;
var WAITING_PASSWORD = 0;
var STARTING = 1;
var LISTENING = 2;
var PASSWORD_PROMPT_HOOK = "Passphrase";
var LISTENING_HOOK = "Swarm http proxy started";
var state = WAITING_PASSWORD;
var swarmProcess = spawn(swarmSetup.binPath, ['--bzzaccount', account || privateKey, '--datadir', dataDir, '--ens-api', ensApi]);
var handleProcessOutput = function handleProcessOutput(data) {
if (state === WAITING_PASSWORD && hasString(PASSWORD_PROMPT_HOOK)(data)) {
setTimeout(function () {
state = STARTING;
swarmProcess.stdin.write(password + '\n');
}, 500);
} else if (hasString(LISTENING_HOOK)(data)) {
state = LISTENING;
clearTimeout(timeout);
resolve(swarmProcess);
}
};
swarmProcess.stdout.on('data', handleProcessOutput);
swarmProcess.stderr.on('data', handleProcessOutput); //swarmProcess.on('close', () => setTimeout(restart, 2000));
var restart = function restart() {
return startProcess(swarmSetup).then(resolve).catch(reject);
};
var error = function error() {
return reject(new Error("Couldn't start swarm process."));
};
var timeout = setTimeout(error, 20000);
});
}; // Process ~> Promise ()
// Stops the Swarm process.
var stopProcess = function stopProcess(process) {
return new Promise(function (resolve, reject) {
process.stderr.removeAllListeners('data');
process.stdout.removeAllListeners('data');
process.stdin.removeAllListeners('error');
process.removeAllListeners('error');
process.removeAllListeners('exit');
process.kill('SIGINT');
var killTimeout = setTimeout(function () {
return process.kill('SIGKILL');
}, 8000);
process.once('close', function () {
clearTimeout(killTimeout);
resolve();
});
});
}; // SwarmSetup -> (SwarmAPI -> Promise ()) -> Promise ()
// Receives a Swarm configuration object and a callback function. It then
// checks if a local Swarm node is running. If no local Swarm is found, it
// downloads the Swarm binaries to the dataDir (if not there), checksums,
// starts the Swarm process and calls the callback function with an API
// object using the local node. That callback must return a promise which
// will resolve when it is done using the API, so that this function can
// close the Swarm process properly. Returns a promise that resolves when the
// user is done with the API and the Swarm process is closed.
// TODO: check if Swarm process is already running (improve `isAvailable`)
var local = function local(swarmSetup) {
return function (useAPI) {
return _isAvailable("http://localhost:8500").then(function (isAvailable) {
return isAvailable ? useAPI(at("http://localhost:8500")).then(function () {}) : downloadBinary(swarmSetup.binPath, swarmSetup.archives).onData(function (data) {
return (swarmSetup.onProgress || function () {})(data.length);
}).then(function () {
return startProcess(swarmSetup);
}).then(function (process) {
return useAPI(at("http://localhost:8500")).then(function () {
return process;
});
}).then(stopProcess);
});
};
}; // String ~> Promise Bool
// Returns true if Swarm is available on `url`.
// Perfoms a test upload to determine that.
// TODO: improve this?
var _isAvailable = function isAvailable(swarmUrl) {
var testFile = "test";
var testHash = "c9a99c7d326dcc6316f32fe2625b311f6dc49a175e6877681ded93137d3569e7";
return uploadData(swarmUrl)(testFile).then(function (hash) {
return hash === testHash;
}).catch(function () {
return false;
});
}; // String -> String ~> Promise Bool
// Returns a Promise which is true if that Swarm address is a directory.
// Determines that by checking that it (i) is a JSON, (ii) has a .entries.
// TODO: improve this?
var isDirectory = function isDirectory(swarmUrl) {
return function (hash) {
return downloadData(swarmUrl)(hash).then(function (data) {
try {
return !!JSON.parse(toString(data)).entries;
} catch (e) {
return false;
}
});
};
}; // Uncurries a function; used to allow the f(x,y,z) style on exports.
var uncurry = function uncurry(f) {
return function (a, b, c, d, e) {
var p; // Hardcoded because efficiency (`arguments` is very slow).
if (typeof a !== "undefined") p = f(a);
if (typeof b !== "undefined") p = f(b);
if (typeof c !== "undefined") p = f(c);
if (typeof d !== "undefined") p = f(d);
if (typeof e !== "undefined") p = f(e);
return p;
};
}; // () -> Promise Bool
// Not sure how to mock Swarm to test it properly. Ideas?
var test = function test() {
return Promise.resolve(true);
}; // Uint8Array -> String
var toString = function toString(uint8Array) {
return bytes.toString(bytes.fromUint8Array(uint8Array));
}; // String -> Uint8Array
var fromString = function fromString(string) {
return bytes.toUint8Array(bytes.fromString(string));
}; // String -> SwarmAPI
// Fixes the `swarmUrl`, returning an API where you don't have to pass it.
var at = function at(swarmUrl) {
return {
download: function download(hash, path) {
return _download(swarmUrl)(hash)(path);
},
downloadData: uncurry(downloadData(swarmUrl)),
downloadDataToDisk: uncurry(downloadDataToDisk(swarmUrl)),
downloadDirectory: uncurry(downloadDirectory(swarmUrl)),
downloadDirectoryToDisk: uncurry(downloadDirectoryToDisk(swarmUrl)),
downloadEntries: uncurry(downloadEntries(swarmUrl)),
downloadRoutes: uncurry(downloadRoutes(swarmUrl)),
isAvailable: function isAvailable() {
return _isAvailable(swarmUrl);
},
upload: function upload(arg) {
return _upload(swarmUrl)(arg);
},
uploadData: uncurry(uploadData(swarmUrl)),
uploadFile: uncurry(uploadFile(swarmUrl)),
uploadFileFromDisk: uncurry(uploadFile(swarmUrl)),
uploadDataFromDisk: uncurry(uploadDataFromDisk(swarmUrl)),
uploadDirectory: uncurry(uploadDirectory(swarmUrl)),
uploadDirectoryFromDisk: uncurry(uploadDirectoryFromDisk(swarmUrl)),
uploadToManifest: uncurry(uploadToManifest(swarmUrl)),
pick: pick,
hash: hash,
fromString: fromString,
toString: toString
};
};
return {
at: at,
local: local,
download: _download,
downloadBinary: downloadBinary,
downloadData: downloadData,
downloadDataToDisk: downloadDataToDisk,
downloadDirectory: downloadDirectory,
downloadDirectoryToDisk: downloadDirectoryToDisk,
downloadEntries: downloadEntries,
downloadRoutes: downloadRoutes,
isAvailable: _isAvailable,
startProcess: startProcess,
stopProcess: stopProcess,
upload: _upload,
uploadData: uploadData,
uploadDataFromDisk: uploadDataFromDisk,
uploadFile: uploadFile,
uploadFileFromDisk: uploadFileFromDisk,
uploadDirectory: uploadDirectory,
uploadDirectoryFromDisk: uploadDirectoryFromDisk,
uploadToManifest: uploadToManifest,
pick: pick,
hash: hash,
fromString: fromString,
toString: toString
};
};
},{}]},{},[1])
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIm5vZGVfbW9kdWxlcy9icm93c2VyLXBhY2svX3ByZWx1ZGUuanMiLCJsaWIvc3dhcm0uanMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUE7QUNBQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0EiLCJmaWxlIjoiZ2VuZXJhdGVkLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXNDb250ZW50IjpbIihmdW5jdGlvbigpe2Z1bmN0aW9uIHIoZSxuLHQpe2Z1bmN0aW9uIG8oaSxmKXtpZighbltpXSl7aWYoIWVbaV0pe3ZhciBjPVwiZnVuY3Rpb25cIj09dHlwZW9mIHJlcXVpcmUmJnJlcXVpcmU7aWYoIWYmJmMpcmV0dXJuIGMoaSwhMCk7aWYodSlyZXR1cm4gdShpLCEwKTt2YXIgYT1uZXcgRXJyb3IoXCJDYW5ub3QgZmluZCBtb2R1bGUgJ1wiK2krXCInXCIpO3Rocm93IGEuY29kZT1cIk1PRFVMRV9OT1RfRk9VTkRcIixhfXZhciBwPW5baV09e2V4cG9ydHM6e319O2VbaV1bMF0uY2FsbChwLmV Line truncated
+53
View File
@@ -0,0 +1,53 @@
var unavailable = function unavailable() {
throw "This swarm.js function isn't available on the browser.";
};
var fs = {
readFile: unavailable
};
var files = {
download: unavailable,
safeDownloadArchived: unavailable,
directoryTree: unavailable
};
var os = {
platform: unavailable,
arch: unavailable
};
var path = {
join: unavailable,
slice: unavailable
};
var child_process = {
spawn: unavailable
};
var mimetype = {
lookup: unavailable
};
var defaultArchives = {};
var downloadUrl = null;
var request = require("xhr-request");
var bytes = require("eth-lib/lib/bytes");
var hash = require("./swarm-hash.js");
var pick = require("./pick.js");
var swarm = require("./swarm");
module.exports = swarm({
fs: fs,
files: files,
os: os,
path: path,
child_process: child_process,
defaultArchives: defaultArchives,
mimetype: mimetype,
request: request,
downloadUrl: downloadUrl,
bytes: bytes,
hash: hash,
pick: pick
});
+55
View File
@@ -0,0 +1,55 @@
var fs = require("fs-extra");
var files = require("./files.js");
var os = require("os");
var path = require("path");
var child_process = require("child_process");
var mimetype = require('mime-types');
var defaultArchives = require("./../archives/archives.json");
var requester = require("xhr-request");
var downloadUrl = "http://ethereum-mist.s3.amazonaws.com/swarm/";
var bytes = require("eth-lib/lib/bytes");
var hash = require("./swarm-hash.js");
var pick = require("./pick.js");
var swarm = require("./swarm"); // Fixes issue that causes xhr-request-promise on Node.js to only accept Buffer
var request = function request(url, params, callback) {
var newParams = {};
for (var key in params) {
newParams[key] = params[key];
}
if (typeof newParams.body !== "undefined") {
newParams.body = newParams.body instanceof Buffer ? newParams.body : new Buffer(newParams.body);
}
return requester(url, newParams, callback);
};
module.exports = swarm({
fs: fs,
files: files,
os: os,
path: path,
child_process: child_process,
defaultArchives: defaultArchives,
mimetype: mimetype,
request: request,
downloadUrl: downloadUrl,
bytes: bytes,
hash: hash,
pick: pick
});
+272
View File
@@ -0,0 +1,272 @@
// This module implements some file download utils. Its most important export
// is `safeDownloadTargzFile`, which, given a file, its md5, a tar.gz url, its
// md5 and a path, returns a Promise that will only resolve once the exact file
// you expect is available on that path.
var Q = require("bluebird");
var assert = require("assert");
var crypto = require("crypto");
var fs = require("fs-extra");
var got = require("got");
var mkdirp = require("mkdirp-promise");
var path = require("path");
var tar = require("tar"); // String -> String ~> Promise String
// Downloads a file from an url to a path.
// Returns a promise containing the path.
var download = function download(url) {
return function (filePath) {
var promise = Q.resolve(mkdirp(path.dirname(filePath))).then(function () {
return new Q(function (resolve, reject) {
var writeStream = fs.createWriteStream(filePath);
var downloadStream = got.stream(url);
downloadStream.on("end", function () {
return resolve(filePath);
});
downloadStream.on("data", function (chunk) {
return promise.onDataCallback(chunk);
});
downloadStream.on("error", reject);
downloadStream.pipe(writeStream);
});
});
promise.onDataCallback = function () {};
promise.onData = function (callback) {
promise.onDataCallback = callback || function () {};
return promise;
};
return promise;
};
}; // String -> String ~> Promise String
// Hashes a file using the given algorithm (ex: "md5").
// Returns a promise containing the hashed string.
var hash = function hash(algorithm) {
return function (path) {
return new Q(function (resolve, reject) {
var readStream = fs.ReadStream(path);
var hash = crypto.createHash(algorithm);
readStream.on("data", function (d) {
return hash.update(d);
});
readStream.on("end", function () {
return resolve(hash.digest("hex"));
});
readStream.on("error", reject);
});
};
}; // String -> String ~> Promise ()
// Asserts a file matches this md5 hash.
// Returns a promise containing its path.
var checksum = function checksum(fileHash) {
return function (path) {
return hash("md5")(path).then(function (actualHash) {
return actualHash === fileHash;
}).then(assert).then(function () {
return path;
});
};
}; // String ~> String ~> String ~> Promise String
// Downloads a file to a directory, check.
// Checks if the md5 hash matches.
// Returns a promise containing the path.
var downloadAndCheck = function downloadAndCheck(url) {
return function (path) {
return function (fileHash) {
return download(url)(path).then(checksum(fileHash));
};
};
}; // String -> String ~> Promise String
// TODO: work for zip and other types
var extract = function extract(fromPath) {
return function (toPath) {
return tar.x(fromPath, toPath).then(function () {
return toPath;
});
};
}; // String ~> Promise String
// Reads a file as an UTF8 string.
// Returns a promise containing that string.
var readUTF8 = function readUTF8(path) {
return fs.readFile(path, {
encoding: "utf8"
});
}; // String ~> Promise Bool
var isDirectory = function isDirectory(path) {
return fs.exists(path).then(assert).then(function () {
return fs.lstat(path);
}).then(function (stats) {
return stats.isDirectory();
})["catch"](function () {
return false;
});
}; // String -> Promise String
var directoryTree = function directoryTree(dirPath) {
var paths = [];
var search = function search(dirPath) {
return isDirectory(dirPath).then(function (isDir) {
if (isDir) {
var searchOnDir = function searchOnDir(dir) {
return search(path.join(dirPath, dir));
};
return Q.all(Q.map(fs.readdir(dirPath), searchOnDir));
} else {
paths.push(dirPath);
}
;
});
};
return Q.all(search(dirPath)).then(function () {
return paths;
});
}; // Regex -> String ~> Promise (Array String)
var search = function search(regex) {
return function (dirPath) {
return directoryTree(dirPath).then(function (tree) {
return tree.filter(function (path) {
return regex.test(path);
});
});
};
}; // String -> String -> String -> String ~> Promise String
// Downloads a file inside a tar.gz and places it at `filePath`.
// Checks the md5 hash of the tar before extracting it.
// Checks the md5 hash of the file after extracting it.
// If all is OK, returns a promise containing the file path.
var safeDownloadArchived = function safeDownloadArchived(url) {
return function (archiveHash) {
return function (fileHash) {
return function (filePath) {
var fileDir = path.dirname(filePath);
var fileName = path.basename(filePath);
var archivePath = path.join(fileDir, ".swarm_downloads/files.tar.gz");
var archiveDir = path.dirname(archivePath);
var promise = Q.resolve(mkdirp(archiveDir)).then(function () {
return checksum(fileHash)(filePath);
}).then(function () {
return filePath;
})["catch"](function () {
return fs.exists(archiveDir).then(function (exists) {
return !exists ? fs.mkdir(archiveDir) : null;
}).then(function () {
return download(url)(archivePath).onData(promise.onDataCallback);
}).then(function () {
return hash("md5")(archivePath);
}).then(function () {
return archiveHash ? checksum(archiveHash)(archivePath) : null;
}).then(function () {
return extract(archivePath)(archiveDir);
}).then(function () {
return search(new RegExp(fileName + "$"))(archiveDir);
}).then(function (fp) {
return fs.rename(fp[0], filePath);
}).then(function () {
return fs.unlink(archivePath);
}).then(function () {
return fileHash ? checksum(fileHash)(filePath) : null;
}).then(function () {
return filePath;
});
});
promise.onDataCallback = function () {};
promise.onData = function (callback) {
promise.onDataCallback = callback || function () {};
return promise;
};
return promise;
};
};
};
}; // String -> String ~> Promise String
// Like `safeDownloadArchivedFile`, but without the checksums.
var downloadArchived = function downloadArchived(url) {
return function (path) {
return safeDownloadArchived(url)(null)(null)(path);
};
}; // () => Promise Bool
// Tests the implementation by downloading a predetermined tar.gz
// from a mocked HTTP-server into a mocked filesystem. Does some
// redundancy tests such as checking the file constents and double
// checking its MD5 hash.
// Returns a promise containing a boolean, true if tests passed.
var test = function test() {
var filePath = "/swarm/foo.txt";
var fileHash = "d3b07384d113edec49eaa6238ad5ff00";
var archiveUrl = "http://localhost:12534";
var archiveHash = "7fa45f946bb2a696bdd9972e0fbceac2";
var archiveData = new Buffer([0x1f, 0x8b, 0x08, 0x00, 0xf1, 0x34, 0xaf, 0x58, 0x00, 0x03, 0xed, 0xcf, 0x3d, 0x0e, 0x83, 0x30, 0x0c, 0x86, 0x61, 0x66, 0x4e, 0xe1, 0x13, 0x54, 0xce, 0x0f, 0xc9, 0x79, 0x58, 0xb2, 0x46, 0x82, 0x14, 0x71, 0x7c, 0xd2, 0x06, 0x31, 0x52, 0x75, 0x40, 0x08, 0xe9, 0x7d, 0x96, 0x4f, 0x96, 0x3d, 0x7c, 0x4e, 0x39, 0xbf, 0xca, 0x5a, 0xba, 0x2b, 0xa9, 0x6a, 0xf0, 0x5e, 0x3e, 0x19, 0xc3, 0xf0, 0x4d, 0xb5, 0x6d, 0xde, 0x79, 0x31, 0x4e, 0x07, 0x17, 0x9c, 0xb5, 0x31, 0x8a, 0x1a, 0xab, 0xc6, 0x77, 0xa2, 0x97, 0xb6, 0xda, 0xbd, 0xe7, 0x32, 0x4e, 0xb5, 0xca, 0xf2, 0xe3, 0xae, 0x9e, 0xa5, 0x74, 0xb2, 0x6f, 0x8f, 0xc8, 0x91, 0x0f, 0x91, 0x72, 0xee, 0xef, 0xee, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf8, 0xdf, 0x06, 0xb3, 0x2a, 0xda, 0xed, 0x00, 0x28, 0x00, 0x00]);
var crypto = require("crypto");
var fsMock = require("mock-fs")({
"/swarm": {}
});
var httpMock = require("http").createServer(function (_, res) {
return res.end(archiveData);
}).listen(12534);
return safeDownloadArchived(archiveUrl)(archiveHash)(fileHash)(filePath).then(checksum(fileHash)).then(readUTF8).then(function (text) {
return text === "foo\n";
}).then(assert).then(function () {
return safeDownloadArchived(archiveUrl)(archiveHash)(fileHash)(filePath);
}).then(function () {
return true;
})["catch"](false)["finally"](function () {
return httpMock.close();
});
};
module.exports = {
download: download,
hash: hash,
checksum: checksum,
downloadAndCheck: downloadAndCheck,
extract: extract,
readUTF8: readUTF8,
safeDownloadArchived: safeDownloadArchived,
directoryTree: directoryTree,
downloadArchived: downloadArchived,
search: search,
test: test
};
+65
View File
@@ -0,0 +1,65 @@
var picker = function picker(type) {
return function () {
return new Promise(function (resolve, reject) {
var fileLoader = function fileLoader(e) {
var directory = {};
var totalFiles = e.target.files.length;
var loadedFiles = 0;
[].map.call(e.target.files, function (file) {
var reader = new FileReader();
reader.onload = function (e) {
var data = new Uint8Array(e.target.result);
if (type === "directory") {
var path = file.webkitRelativePath;
directory[path.slice(path.indexOf("/") + 1)] = {
type: "text/plain",
data: data
};
if (++loadedFiles === totalFiles) resolve(directory);
} else if (type === "file") {
var _path = file.webkitRelativePath;
resolve({
"type": mimetype.lookup(_path),
"data": data
});
} else {
resolve(data);
}
};
reader.readAsArrayBuffer(file);
});
};
var fileInput;
if (type === "directory") {
fileInput = document.createElement("input");
fileInput.addEventListener("change", fileLoader);
fileInput.type = "file";
fileInput.webkitdirectory = true;
fileInput.mozdirectory = true;
fileInput.msdirectory = true;
fileInput.odirectory = true;
fileInput.directory = true;
} else {
fileInput = document.createElement("input");
fileInput.addEventListener("change", fileLoader);
fileInput.type = "file";
}
;
var mouseEvent = document.createEvent("MouseEvents");
mouseEvent.initEvent("click", true, false);
fileInput.dispatchEvent(mouseEvent);
});
};
};
module.exports = {
data: picker("data"),
file: picker("file"),
directory: picker("directory")
};
+42
View File
@@ -0,0 +1,42 @@
// Thanks https://github.com/axic/swarmhash
var keccak = require("eth-lib/lib/hash").keccak256;
var Bytes = require("eth-lib/lib/bytes");
var swarmHashBlock = function swarmHashBlock(length, data) {
var lengthEncoded = Bytes.reverse(Bytes.pad(6, Bytes.fromNumber(length)));
var bytes = Bytes.flatten([lengthEncoded, "0x0000", data]);
return keccak(bytes).slice(2);
}; // (Bytes | Uint8Array | String) -> String
var swarmHash = function swarmHash(data) {
if (typeof data === "string" && data.slice(0, 2) !== "0x") {
data = Bytes.fromString(data);
} else if (typeof data !== "string" && data.length !== undefined) {
data = Bytes.fromUint8Array(data);
}
var length = Bytes.length(data);
if (length <= 4096) {
return swarmHashBlock(length, data);
}
var maxSize = 4096;
while (maxSize * (4096 / 32) < length) {
maxSize *= 4096 / 32;
}
var innerNodes = [];
for (var i = 0; i < length; i += maxSize) {
var size = maxSize < length - i ? maxSize : length - i;
innerNodes.push(swarmHash(Bytes.slice(data, i, i + size)));
}
return swarmHashBlock(length, Bytes.flatten(innerNodes));
};
module.exports = swarmHash;
+673
View File
@@ -0,0 +1,673 @@
// TODO: this is a temporary fix to hide those libraries from the browser. A
// slightly better long-term solution would be to split this file into two,
// separating the functions that are used on Node.js from the functions that
// are used only on the browser.
module.exports = function (_ref) {
var fs = _ref.fs,
files = _ref.files,
os = _ref.os,
path = _ref.path,
child_process = _ref.child_process,
mimetype = _ref.mimetype,
defaultArchives = _ref.defaultArchives,
request = _ref.request,
downloadUrl = _ref.downloadUrl,
bytes = _ref.bytes,
hash = _ref.hash,
pick = _ref.pick;
// ∀ a . String -> JSON -> Map String a -o Map String a
// Inserts a key/val pair in an object impurely.
var impureInsert = function impureInsert(key) {
return function (val) {
return function (map) {
return map[key] = val, map;
};
};
}; // String -> JSON -> Map String JSON
// Merges an array of keys and an array of vals into an object.
var toMap = function toMap(keys) {
return function (vals) {
var map = {};
for (var i = 0, l = keys.length; i < l; ++i) {
map[keys[i]] = vals[i];
}
return map;
};
}; // ∀ a . Map String a -> Map String a -> Map String a
// Merges two maps into one.
var merge = function merge(a) {
return function (b) {
var map = {};
for (var key in a) {
map[key] = a[key];
}
for (var _key in b) {
map[_key] = b[_key];
}
return map;
};
}; // ∀ a . [a] -> [a] -> Bool
var equals = function equals(a) {
return function (b) {
if (a.length !== b.length) {
return false;
} else {
for (var i = 0, l = a.length; i < l; ++i) {
if (a[i] !== b[i]) return false;
}
}
return true;
};
}; // String -> String -> String
var rawUrl = function rawUrl(swarmUrl) {
return function (hash) {
return "".concat(swarmUrl, "/bzz-raw:/").concat(hash);
};
}; // String -> String -> Promise Uint8Array
// Gets the raw contents of a Swarm hash address.
var downloadData = function downloadData(swarmUrl) {
return function (hash) {
return new Promise(function (resolve, reject) {
request(rawUrl(swarmUrl)(hash), {
responseType: "arraybuffer"
}, function (err, arrayBuffer, response) {
if (err) {
return reject(err);
}
if (response.statusCode >= 400) {
return reject(new Error("Error ".concat(response.statusCode, ".")));
}
return resolve(new Uint8Array(arrayBuffer));
});
});
};
}; // type Entry = {"type": String, "hash": String}
// type File = {"type": String, "data": Uint8Array}
// String -> String -> Promise (Map String Entry)
// Solves the manifest of a Swarm address recursively.
// Returns a map from full paths to entries.
var downloadEntries = function downloadEntries(swarmUrl) {
return function (hash) {
var search = function search(hash) {
return function (path) {
return function (routes) {
// Formats an entry to the Swarm.js type.
var format = function format(entry) {
return {
type: entry.contentType,
hash: entry.hash
};
}; // To download a single entry:
// if type is bzz-manifest, go deeper
// if not, add it to the routing table
var downloadEntry = function downloadEntry(entry) {
if (entry.path === undefined) {
return Promise.resolve();
} else {
return entry.contentType === "application/bzz-manifest+json" ? search(entry.hash)(path + entry.path)(routes) : Promise.resolve(impureInsert(path + entry.path)(format(entry))(routes));
}
}; // Downloads the initial manifest and then each entry.
return downloadData(swarmUrl)(hash).then(function (text) {
return JSON.parse(toString(text)).entries;
}).then(function (entries) {
return Promise.all(entries.map(downloadEntry));
}).then(function () {
return routes;
});
};
};
};
return search(hash)("")({});
};
}; // String -> String -> Promise (Map String String)
// Same as `downloadEntries`, but returns only hashes (no types).
var downloadRoutes = function downloadRoutes(swarmUrl) {
return function (hash) {
return downloadEntries(swarmUrl)(hash).then(function (entries) {
return toMap(Object.keys(entries))(Object.keys(entries).map(function (route) {
return entries[route].hash;
}));
});
};
}; // String -> String -> Promise (Map String File)
// Gets the entire directory tree in a Swarm address.
// Returns a promise mapping paths to file contents.
var downloadDirectory = function downloadDirectory(swarmUrl) {
return function (hash) {
return downloadEntries(swarmUrl)(hash).then(function (entries) {
var paths = Object.keys(entries);
var hashs = paths.map(function (path) {
return entries[path].hash;
});
var types = paths.map(function (path) {
return entries[path].type;
});
var datas = hashs.map(downloadData(swarmUrl));
var files = function files(datas) {
return datas.map(function (data, i) {
return {
type: types[i],
data: data
};
});
};
return Promise.all(datas).then(function (datas) {
return toMap(paths)(files(datas));
});
});
};
}; // String -> String -> String -> Promise String
// Gets the raw contents of a Swarm hash address.
// Returns a promise with the downloaded file path.
var downloadDataToDisk = function downloadDataToDisk(swarmUrl) {
return function (hash) {
return function (filePath) {
return files.download(rawUrl(swarmUrl)(hash))(filePath);
};
};
}; // String -> String -> String -> Promise (Map String String)
// Gets the entire directory tree in a Swarm address.
// Returns a promise mapping paths to file contents.
var downloadDirectoryToDisk = function downloadDirectoryToDisk(swarmUrl) {
return function (hash) {
return function (dirPath) {
return downloadRoutes(swarmUrl)(hash).then(function (routingTable) {
var downloads = [];
for (var route in routingTable) {
if (route.length > 0) {
var filePath = path.join(dirPath, route);
downloads.push(downloadDataToDisk(swarmUrl)(routingTable[route])(filePath));
}
;
}
;
return Promise.all(downloads).then(function () {
return dirPath;
});
});
};
};
}; // String -> Uint8Array -> Promise String
// Uploads raw data to Swarm.
// Returns a promise with the uploaded hash.
var uploadData = function uploadData(swarmUrl) {
return function (data) {
return new Promise(function (resolve, reject) {
var params = {
body: typeof data === "string" ? fromString(data) : data,
method: "POST"
};
request("".concat(swarmUrl, "/bzz-raw:/"), params, function (err, data) {
if (err) {
return reject(err);
}
return resolve(data);
});
});
};
}; // String -> String -> String -> File -> Promise String
// Uploads a file to the Swarm manifest at a given hash, under a specific
// route. Returns a promise containing the uploaded hash.
// FIXME: for some reasons Swarm-Gateways is sometimes returning
// error 404 (bad request), so we retry up to 3 times. Why?
var uploadToManifest = function uploadToManifest(swarmUrl) {
return function (hash) {
return function (route) {
return function (file) {
var attempt = function attempt(n) {
var slashRoute = route[0] === "/" ? route : "/" + route;
var url = "".concat(swarmUrl, "/bzz:/").concat(hash).concat(slashRoute);
var opt = {
method: "PUT",
headers: {
"Content-Type": file.type
},
body: file.data
};
return new Promise(function (resolve, reject) {
request(url, opt, function (err, data) {
if (err) {
return reject(err);
}
if (data.indexOf("error") !== -1) {
return reject(data);
}
return resolve(data);
});
})["catch"](function (e) {
return n > 0 && attempt(n - 1);
});
};
return attempt(3);
};
};
};
}; // String -> {type: String, data: Uint8Array} -> Promise String
var uploadFile = function uploadFile(swarmUrl) {
return function (file) {
return uploadDirectory(swarmUrl)({
"": file
});
};
}; // String -> String -> Promise String
var uploadFileFromDisk = function uploadFileFromDisk(swarmUrl) {
return function (filePath) {
return fs.readFile(filePath).then(function (data) {
return uploadFile(swarmUrl)({
type: mimetype.lookup(filePath),
data: data
});
});
};
}; // String -> Map String File -> Promise String
// Uploads a directory to Swarm. The directory is
// represented as a map of routes and files.
// A default path is encoded by having a "" route.
var uploadDirectory = function uploadDirectory(swarmUrl) {
return function (directory) {
return uploadData(swarmUrl)("{}").then(function (hash) {
var uploadRoute = function uploadRoute(route) {
return function (hash) {
return uploadToManifest(swarmUrl)(hash)(route)(directory[route]);
};
};
var uploadToHash = function uploadToHash(hash, route) {
return hash.then(uploadRoute(route));
};
return Object.keys(directory).reduce(uploadToHash, Promise.resolve(hash));
});
};
}; // String -> Promise String
var uploadDataFromDisk = function uploadDataFromDisk(swarmUrl) {
return function (filePath) {
return fs.readFile(filePath).then(uploadData(swarmUrl));
};
}; // String -> Nullable String -> String -> Promise String
var uploadDirectoryFromDisk = function uploadDirectoryFromDisk(swarmUrl) {
return function (defaultPath) {
return function (dirPath) {
return files.directoryTree(dirPath).then(function (fullPaths) {
return Promise.all(fullPaths.map(function (path) {
return fs.readFile(path);
})).then(function (datas) {
var paths = fullPaths.map(function (path) {
return path.slice(dirPath.length);
});
var types = fullPaths.map(function (path) {
return mimetype.lookup(path) || "text/plain";
});
return toMap(paths)(datas.map(function (data, i) {
return {
type: types[i],
data: data
};
}));
});
}).then(function (directory) {
return merge(defaultPath ? {
"": directory[defaultPath]
} : {})(directory);
}).then(uploadDirectory(swarmUrl));
};
};
}; // String -> UploadInfo -> Promise String
// Simplified multi-type upload which calls the correct
// one based on the type of the argument given.
var _upload = function upload(swarmUrl) {
return function (arg) {
// Upload raw data from browser
if (arg.pick === "data") {
return pick.data().then(uploadData(swarmUrl)); // Upload a file from browser
} else if (arg.pick === "file") {
return pick.file().then(uploadFile(swarmUrl)); // Upload a directory from browser
} else if (arg.pick === "directory") {
return pick.directory().then(uploadDirectory(swarmUrl)); // Upload directory/file from disk
} else if (arg.path) {
switch (arg.kind) {
case "data":
return uploadDataFromDisk(swarmUrl)(arg.path);
case "file":
return uploadFileFromDisk(swarmUrl)(arg.path);
case "directory":
return uploadDirectoryFromDisk(swarmUrl)(arg.defaultFile)(arg.path);
}
; // Upload UTF-8 string or raw data (buffer)
} else if (arg.length || typeof arg === "string") {
return uploadData(swarmUrl)(arg); // Upload directory with JSON
} else if (arg instanceof Object) {
return uploadDirectory(swarmUrl)(arg);
}
return Promise.reject(new Error("Bad arguments"));
};
}; // String -> String -> Nullable String -> Promise (String | Uint8Array | Map String Uint8Array)
// Simplified multi-type download which calls the correct function based on
// the type of the argument given, and on whether the Swwarm address has a
// directory or a file.
var _download = function download(swarmUrl) {
return function (hash) {
return function (path) {
return isDirectory(swarmUrl)(hash).then(function (isDir) {
if (isDir) {
return path ? downloadDirectoryToDisk(swarmUrl)(hash)(path) : downloadDirectory(swarmUrl)(hash);
} else {
return path ? downloadDataToDisk(swarmUrl)(hash)(path) : downloadData(swarmUrl)(hash);
}
});
};
};
}; // String -> Promise String
// Downloads the Swarm binaries into a path. Returns a promise that only
// resolves when the exact Swarm file is there, and verified to be correct.
// If it was already there to begin with, skips the download.
var downloadBinary = function downloadBinary(path, archives) {
var system = os.platform().replace("win32", "windows") + "-" + (os.arch() === "x64" ? "amd64" : "386");
var archive = (archives || defaultArchives)[system];
var archiveUrl = downloadUrl + archive.archive + ".tar.gz";
var archiveMD5 = archive.archiveMD5;
var binaryMD5 = archive.binaryMD5;
return files.safeDownloadArchived(archiveUrl)(archiveMD5)(binaryMD5)(path);
}; // type SwarmSetup = {
// account : String,
// password : String,
// dataDir : String,
// binPath : String,
// ensApi : String,
// onDownloadProgress : Number ~> (),
// archives : [{
// archive: String,
// binaryMD5: String,
// archiveMD5: String
// }]
// }
// SwarmSetup ~> Promise Process
// Starts the Swarm process.
var startProcess = function startProcess(swarmSetup) {
return new Promise(function (resolve, reject) {
var spawn = child_process.spawn;
var hasString = function hasString(str) {
return function (buffer) {
return ('' + buffer).indexOf(str) !== -1;
};
};
var account = swarmSetup.account,
password = swarmSetup.password,
dataDir = swarmSetup.dataDir,
ensApi = swarmSetup.ensApi,
privateKey = swarmSetup.privateKey;
var STARTUP_TIMEOUT_SECS = 3;
var WAITING_PASSWORD = 0;
var STARTING = 1;
var LISTENING = 2;
var PASSWORD_PROMPT_HOOK = "Passphrase";
var LISTENING_HOOK = "Swarm http proxy started";
var state = WAITING_PASSWORD;
var swarmProcess = spawn(swarmSetup.binPath, ['--bzzaccount', account || privateKey, '--datadir', dataDir, '--ens-api', ensApi]);
var handleProcessOutput = function handleProcessOutput(data) {
if (state === WAITING_PASSWORD && hasString(PASSWORD_PROMPT_HOOK)(data)) {
setTimeout(function () {
state = STARTING;
swarmProcess.stdin.write(password + '\n');
}, 500);
} else if (hasString(LISTENING_HOOK)(data)) {
state = LISTENING;
clearTimeout(timeout);
resolve(swarmProcess);
}
};
swarmProcess.stdout.on('data', handleProcessOutput);
swarmProcess.stderr.on('data', handleProcessOutput); //swarmProcess.on('close', () => setTimeout(restart, 2000));
var restart = function restart() {
return startProcess(swarmSetup).then(resolve)["catch"](reject);
};
var error = function error() {
return reject(new Error("Couldn't start swarm process."));
};
var timeout = setTimeout(error, 20000);
});
}; // Process ~> Promise ()
// Stops the Swarm process.
var stopProcess = function stopProcess(process) {
return new Promise(function (resolve, reject) {
process.stderr.removeAllListeners('data');
process.stdout.removeAllListeners('data');
process.stdin.removeAllListeners('error');
process.removeAllListeners('error');
process.removeAllListeners('exit');
process.kill('SIGINT');
var killTimeout = setTimeout(function () {
return process.kill('SIGKILL');
}, 8000);
process.once('close', function () {
clearTimeout(killTimeout);
resolve();
});
});
}; // SwarmSetup -> (SwarmAPI -> Promise ()) -> Promise ()
// Receives a Swarm configuration object and a callback function. It then
// checks if a local Swarm node is running. If no local Swarm is found, it
// downloads the Swarm binaries to the dataDir (if not there), checksums,
// starts the Swarm process and calls the callback function with an API
// object using the local node. That callback must return a promise which
// will resolve when it is done using the API, so that this function can
// close the Swarm process properly. Returns a promise that resolves when the
// user is done with the API and the Swarm process is closed.
// TODO: check if Swarm process is already running (improve `isAvailable`)
var local = function local(swarmSetup) {
return function (useAPI) {
return _isAvailable("http://localhost:8500").then(function (isAvailable) {
return isAvailable ? useAPI(at("http://localhost:8500")).then(function () {}) : downloadBinary(swarmSetup.binPath, swarmSetup.archives).onData(function (data) {
return (swarmSetup.onProgress || function () {})(data.length);
}).then(function () {
return startProcess(swarmSetup);
}).then(function (process) {
return useAPI(at("http://localhost:8500")).then(function () {
return process;
});
}).then(stopProcess);
});
};
}; // String ~> Promise Bool
// Returns true if Swarm is available on `url`.
// Perfoms a test upload to determine that.
// TODO: improve this?
var _isAvailable = function isAvailable(swarmUrl) {
var testFile = "test";
var testHash = "c9a99c7d326dcc6316f32fe2625b311f6dc49a175e6877681ded93137d3569e7";
return uploadData(swarmUrl)(testFile).then(function (hash) {
return hash === testHash;
})["catch"](function () {
return false;
});
}; // String -> String ~> Promise Bool
// Returns a Promise which is true if that Swarm address is a directory.
// Determines that by checking that it (i) is a JSON, (ii) has a .entries.
// TODO: improve this?
var isDirectory = function isDirectory(swarmUrl) {
return function (hash) {
return downloadData(swarmUrl)(hash).then(function (data) {
try {
return !!JSON.parse(toString(data)).entries;
} catch (e) {
return false;
}
});
};
}; // Uncurries a function; used to allow the f(x,y,z) style on exports.
var uncurry = function uncurry(f) {
return function (a, b, c, d, e) {
var p; // Hardcoded because efficiency (`arguments` is very slow).
if (typeof a !== "undefined") p = f(a);
if (typeof b !== "undefined") p = f(b);
if (typeof c !== "undefined") p = f(c);
if (typeof d !== "undefined") p = f(d);
if (typeof e !== "undefined") p = f(e);
return p;
};
}; // () -> Promise Bool
// Not sure how to mock Swarm to test it properly. Ideas?
var test = function test() {
return Promise.resolve(true);
}; // Uint8Array -> String
var toString = function toString(uint8Array) {
return bytes.toString(bytes.fromUint8Array(uint8Array));
}; // String -> Uint8Array
var fromString = function fromString(string) {
return bytes.toUint8Array(bytes.fromString(string));
}; // String -> SwarmAPI
// Fixes the `swarmUrl`, returning an API where you don't have to pass it.
var at = function at(swarmUrl) {
return {
download: function download(hash, path) {
return _download(swarmUrl)(hash)(path);
},
downloadData: uncurry(downloadData(swarmUrl)),
downloadDataToDisk: uncurry(downloadDataToDisk(swarmUrl)),
downloadDirectory: uncurry(downloadDirectory(swarmUrl)),
downloadDirectoryToDisk: uncurry(downloadDirectoryToDisk(swarmUrl)),
downloadEntries: uncurry(downloadEntries(swarmUrl)),
downloadRoutes: uncurry(downloadRoutes(swarmUrl)),
isAvailable: function isAvailable() {
return _isAvailable(swarmUrl);
},
upload: function upload(arg) {
return _upload(swarmUrl)(arg);
},
uploadData: uncurry(uploadData(swarmUrl)),
uploadFile: uncurry(uploadFile(swarmUrl)),
uploadFileFromDisk: uncurry(uploadFile(swarmUrl)),
uploadDataFromDisk: uncurry(uploadDataFromDisk(swarmUrl)),
uploadDirectory: uncurry(uploadDirectory(swarmUrl)),
uploadDirectoryFromDisk: uncurry(uploadDirectoryFromDisk(swarmUrl)),
uploadToManifest: uncurry(uploadToManifest(swarmUrl)),
pick: pick,
hash: hash,
fromString: fromString,
toString: toString
};
};
return {
at: at,
local: local,
download: _download,
downloadBinary: downloadBinary,
downloadData: downloadData,
downloadDataToDisk: downloadDataToDisk,
downloadDirectory: downloadDirectory,
downloadDirectoryToDisk: downloadDirectoryToDisk,
downloadEntries: downloadEntries,
downloadRoutes: downloadRoutes,
isAvailable: _isAvailable,
startProcess: startProcess,
stopProcess: stopProcess,
upload: _upload,
uploadData: uploadData,
uploadDataFromDisk: uploadDataFromDisk,
uploadFile: uploadFile,
uploadFileFromDisk: uploadFileFromDisk,
uploadDirectory: uploadDirectory,
uploadDirectoryFromDisk: uploadDirectoryFromDisk,
uploadToManifest: uploadToManifest,
pick: pick,
hash: hash,
fromString: fromString,
toString: toString
};
};
+51
View File
@@ -0,0 +1,51 @@
'use strict';
const PassThrough = require('stream').PassThrough;
module.exports = opts => {
opts = Object.assign({}, opts);
const array = opts.array;
let encoding = opts.encoding;
const buffer = encoding === 'buffer';
let objectMode = false;
if (array) {
objectMode = !(encoding || buffer);
} else {
encoding = encoding || 'utf8';
}
if (buffer) {
encoding = null;
}
let len = 0;
const ret = [];
const stream = new PassThrough({objectMode});
if (encoding) {
stream.setEncoding(encoding);
}
stream.on('data', chunk => {
ret.push(chunk);
if (objectMode) {
len = ret.length;
} else {
len += chunk.length;
}
});
stream.getBufferedValue = () => {
if (array) {
return ret;
}
return buffer ? Buffer.concat(ret, len) : ret.join('');
};
stream.getBufferedLength = () => len;
return stream;
};
+51
View File
@@ -0,0 +1,51 @@
'use strict';
const bufferStream = require('./buffer-stream');
function getStream(inputStream, opts) {
if (!inputStream) {
return Promise.reject(new Error('Expected a stream'));
}
opts = Object.assign({maxBuffer: Infinity}, opts);
const maxBuffer = opts.maxBuffer;
let stream;
let clean;
const p = new Promise((resolve, reject) => {
const error = err => {
if (err) { // null check
err.bufferedData = stream.getBufferedValue();
}
reject(err);
};
stream = bufferStream(opts);
inputStream.once('error', error);
inputStream.pipe(stream);
stream.on('data', () => {
if (stream.getBufferedLength() > maxBuffer) {
reject(new Error('maxBuffer exceeded'));
}
});
stream.once('error', error);
stream.on('end', resolve);
clean = () => {
// some streams doesn't implement the `stream.Readable` interface correctly
if (inputStream.unpipe) {
inputStream.unpipe(stream);
}
};
});
p.then(clean, clean);
return p.then(() => stream.getBufferedValue());
}
module.exports = getStream;
module.exports.buffer = (stream, opts) => getStream(stream, Object.assign({}, opts, {encoding: 'buffer'}));
module.exports.array = (stream, opts) => getStream(stream, Object.assign({}, opts, {array: true}));
+52
View File
@@ -0,0 +1,52 @@
{
"name": "get-stream",
"version": "3.0.0",
"description": "Get a stream as a string, buffer, or array",
"license": "MIT",
"repository": "sindresorhus/get-stream",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
},
"engines": {
"node": ">=4"
},
"scripts": {
"test": "xo && ava"
},
"files": [
"index.js",
"buffer-stream.js"
],
"keywords": [
"get",
"stream",
"promise",
"concat",
"string",
"str",
"text",
"buffer",
"read",
"data",
"consume",
"readable",
"readablestream",
"array",
"object",
"obj"
],
"devDependencies": {
"ava": "*",
"into-stream": "^3.0.0",
"xo": "*"
},
"xo": {
"esnext": true
}
,"_resolved": "https://registry.npmjs.org/get-stream/-/get-stream-3.0.0.tgz"
,"_integrity": "sha1-jpQ9E1jcN1VQVOy+LtsFqhdO3hQ="
,"_from": "get-stream@^3.0.0"
}
+479
View File
@@ -0,0 +1,479 @@
'use strict';
const EventEmitter = require('events');
const http = require('http');
const https = require('https');
const PassThrough = require('stream').PassThrough;
const urlLib = require('url');
const querystring = require('querystring');
const duplexer3 = require('duplexer3');
const isStream = require('is-stream');
const getStream = require('get-stream');
const timedOut = require('timed-out');
const urlParseLax = require('url-parse-lax');
const urlToOptions = require('url-to-options');
const lowercaseKeys = require('lowercase-keys');
const decompressResponse = require('decompress-response');
const isRetryAllowed = require('is-retry-allowed');
const Buffer = require('safe-buffer').Buffer;
const isURL = require('isurl');
const isPlainObj = require('is-plain-obj');
const PCancelable = require('p-cancelable');
const pTimeout = require('p-timeout');
const pkg = require('./package');
const getMethodRedirectCodes = new Set([300, 301, 302, 303, 304, 305, 307, 308]);
const allMethodRedirectCodes = new Set([300, 303, 307, 308]);
function requestAsEventEmitter(opts) {
opts = opts || {};
const ee = new EventEmitter();
const requestUrl = opts.href || urlLib.resolve(urlLib.format(opts), opts.path);
const redirects = [];
let retryCount = 0;
let redirectUrl;
const get = opts => {
if (opts.protocol !== 'http:' && opts.protocol !== 'https:') {
ee.emit('error', new got.UnsupportedProtocolError(opts));
return;
}
let fn = opts.protocol === 'https:' ? https : http;
if (opts.useElectronNet && process.versions.electron) {
const electron = require('electron');
fn = electron.net || electron.remote.net;
}
const req = fn.request(opts, res => {
const statusCode = res.statusCode;
res.url = redirectUrl || requestUrl;
res.requestUrl = requestUrl;
const followRedirect = opts.followRedirect && 'location' in res.headers;
const redirectGet = followRedirect && getMethodRedirectCodes.has(statusCode);
const redirectAll = followRedirect && allMethodRedirectCodes.has(statusCode);
if (redirectAll || (redirectGet && (opts.method === 'GET' || opts.method === 'HEAD'))) {
res.resume();
if (statusCode === 303) {
// Server responded with "see other", indicating that the resource exists at another location,
// and the client should request it from that location via GET or HEAD.
opts.method = 'GET';
}
if (redirects.length >= 10) {
ee.emit('error', new got.MaxRedirectsError(statusCode, redirects, opts), null, res);
return;
}
const bufferString = Buffer.from(res.headers.location, 'binary').toString();
redirectUrl = urlLib.resolve(urlLib.format(opts), bufferString);
redirects.push(redirectUrl);
const redirectOpts = Object.assign({}, opts, urlLib.parse(redirectUrl));
ee.emit('redirect', res, redirectOpts);
get(redirectOpts);
return;
}
setImmediate(() => {
const response = opts.decompress === true &&
typeof decompressResponse === 'function' &&
req.method !== 'HEAD' ? decompressResponse(res) : res;
if (!opts.decompress && ['gzip', 'deflate'].indexOf(res.headers['content-encoding']) !== -1) {
opts.encoding = null;
}
response.redirectUrls = redirects;
ee.emit('response', response);
});
});
req.once('error', err => {
const backoff = opts.retries(++retryCount, err);
if (backoff) {
setTimeout(get, backoff, opts);
return;
}
ee.emit('error', new got.RequestError(err, opts));
});
if (opts.gotTimeout) {
timedOut(req, opts.gotTimeout);
}
setImmediate(() => {
ee.emit('request', req);
});
};
setImmediate(() => {
get(opts);
});
return ee;
}
function asPromise(opts) {
const timeoutFn = requestPromise => opts.gotTimeout && opts.gotTimeout.request ?
pTimeout(requestPromise, opts.gotTimeout.request, new got.RequestError({message: 'Request timed out', code: 'ETIMEDOUT'}, opts)) :
requestPromise;
return timeoutFn(new PCancelable((onCancel, resolve, reject) => {
const ee = requestAsEventEmitter(opts);
let cancelOnRequest = false;
onCancel(() => {
cancelOnRequest = true;
});
ee.on('request', req => {
if (cancelOnRequest) {
req.abort();
}
onCancel(() => {
req.abort();
});
if (isStream(opts.body)) {
opts.body.pipe(req);
opts.body = undefined;
return;
}
req.end(opts.body);
});
ee.on('response', res => {
const stream = opts.encoding === null ? getStream.buffer(res) : getStream(res, opts);
stream
.catch(err => reject(new got.ReadError(err, opts)))
.then(data => {
const statusCode = res.statusCode;
const limitStatusCode = opts.followRedirect ? 299 : 399;
res.body = data;
if (opts.json && res.body) {
try {
res.body = JSON.parse(res.body);
} catch (e) {
if (statusCode >= 200 && statusCode < 300) {
throw new got.ParseError(e, statusCode, opts, data);
}
}
}
if (statusCode !== 304 && (statusCode < 200 || statusCode > limitStatusCode)) {
throw new got.HTTPError(statusCode, res.headers, opts);
}
resolve(res);
})
.catch(err => {
Object.defineProperty(err, 'response', {value: res});
reject(err);
});
});
ee.on('error', reject);
}));
}
function asStream(opts) {
const input = new PassThrough();
const output = new PassThrough();
const proxy = duplexer3(input, output);
let timeout;
if (opts.gotTimeout && opts.gotTimeout.request) {
timeout = setTimeout(() => {
proxy.emit('error', new got.RequestError({message: 'Request timed out', code: 'ETIMEDOUT'}, opts));
}, opts.gotTimeout.request);
}
if (opts.json) {
throw new Error('got can not be used as stream when options.json is used');
}
if (opts.body) {
proxy.write = () => {
throw new Error('got\'s stream is not writable when options.body is used');
};
}
const ee = requestAsEventEmitter(opts);
ee.on('request', req => {
proxy.emit('request', req);
if (isStream(opts.body)) {
opts.body.pipe(req);
return;
}
if (opts.body) {
req.end(opts.body);
return;
}
if (opts.method === 'POST' || opts.method === 'PUT' || opts.method === 'PATCH') {
input.pipe(req);
return;
}
req.end();
});
ee.on('response', res => {
clearTimeout(timeout);
const statusCode = res.statusCode;
res.pipe(output);
if (statusCode !== 304 && (statusCode < 200 || statusCode > 299)) {
proxy.emit('error', new got.HTTPError(statusCode, res.headers, opts), null, res);
return;
}
proxy.emit('response', res);
});
ee.on('redirect', proxy.emit.bind(proxy, 'redirect'));
ee.on('error', proxy.emit.bind(proxy, 'error'));
return proxy;
}
function normalizeArguments(url, opts) {
if (typeof url !== 'string' && typeof url !== 'object') {
throw new TypeError(`Parameter \`url\` must be a string or object, not ${typeof url}`);
} else if (typeof url === 'string') {
url = url.replace(/^unix:/, 'http://$&');
url = urlParseLax(url);
} else if (isURL.lenient(url)) {
url = urlToOptions(url);
}
if (url.auth) {
throw new Error('Basic authentication must be done with auth option');
}
opts = Object.assign(
{
path: '',
retries: 2,
decompress: true,
useElectronNet: true
},
url,
{
protocol: url.protocol || 'http:' // Override both null/undefined with default protocol
},
opts
);
opts.headers = Object.assign({
'user-agent': `${pkg.name}/${pkg.version} (https://github.com/sindresorhus/got)`,
'accept-encoding': 'gzip,deflate'
}, lowercaseKeys(opts.headers));
const query = opts.query;
if (query) {
if (typeof query !== 'string') {
opts.query = querystring.stringify(query);
}
opts.path = `${opts.path.split('?')[0]}?${opts.query}`;
delete opts.query;
}
if (opts.json && opts.headers.accept === undefined) {
opts.headers.accept = 'application/json';
}
const body = opts.body;
if (body !== null && body !== undefined) {
const headers = opts.headers;
if (!isStream(body) && typeof body !== 'string' && !Buffer.isBuffer(body) && !(opts.form || opts.json)) {
throw new TypeError('options.body must be a ReadableStream, string, Buffer or plain Object');
}
const canBodyBeStringified = isPlainObj(body) || Array.isArray(body);
if ((opts.form || opts.json) && !canBodyBeStringified) {
throw new TypeError('options.body must be a plain Object or Array when options.form or options.json is used');
}
if (isStream(body) && typeof body.getBoundary === 'function') {
// Special case for https://github.com/form-data/form-data
headers['content-type'] = headers['content-type'] || `multipart/form-data; boundary=${body.getBoundary()}`;
} else if (opts.form && canBodyBeStringified) {
headers['content-type'] = headers['content-type'] || 'application/x-www-form-urlencoded';
opts.body = querystring.stringify(body);
} else if (opts.json && canBodyBeStringified) {
headers['content-type'] = headers['content-type'] || 'application/json';
opts.body = JSON.stringify(body);
}
if (headers['content-length'] === undefined && headers['transfer-encoding'] === undefined && !isStream(body)) {
const length = typeof opts.body === 'string' ? Buffer.byteLength(opts.body) : opts.body.length;
headers['content-length'] = length;
}
opts.method = (opts.method || 'POST').toUpperCase();
} else {
opts.method = (opts.method || 'GET').toUpperCase();
}
if (opts.hostname === 'unix') {
const matches = /(.+?):(.+)/.exec(opts.path);
if (matches) {
opts.socketPath = matches[1];
opts.path = matches[2];
opts.host = null;
}
}
if (typeof opts.retries !== 'function') {
const retries = opts.retries;
opts.retries = (iter, err) => {
if (iter > retries || !isRetryAllowed(err)) {
return 0;
}
const noise = Math.random() * 100;
return ((1 << iter) * 1000) + noise;
};
}
if (opts.followRedirect === undefined) {
opts.followRedirect = true;
}
if (opts.timeout) {
if (typeof opts.timeout === 'number') {
opts.gotTimeout = {request: opts.timeout};
} else {
opts.gotTimeout = opts.timeout;
}
delete opts.timeout;
}
return opts;
}
function got(url, opts) {
try {
return asPromise(normalizeArguments(url, opts));
} catch (err) {
return Promise.reject(err);
}
}
got.stream = (url, opts) => asStream(normalizeArguments(url, opts));
const methods = [
'get',
'post',
'put',
'patch',
'head',
'delete'
];
for (const method of methods) {
got[method] = (url, opts) => got(url, Object.assign({}, opts, {method}));
got.stream[method] = (url, opts) => got.stream(url, Object.assign({}, opts, {method}));
}
class StdError extends Error {
constructor(message, error, opts) {
super(message);
this.name = 'StdError';
if (error.code !== undefined) {
this.code = error.code;
}
Object.assign(this, {
host: opts.host,
hostname: opts.hostname,
method: opts.method,
path: opts.path,
protocol: opts.protocol,
url: opts.href
});
}
}
got.RequestError = class extends StdError {
constructor(error, opts) {
super(error.message, error, opts);
this.name = 'RequestError';
}
};
got.ReadError = class extends StdError {
constructor(error, opts) {
super(error.message, error, opts);
this.name = 'ReadError';
}
};
got.ParseError = class extends StdError {
constructor(error, statusCode, opts, data) {
super(`${error.message} in "${urlLib.format(opts)}": \n${data.slice(0, 77)}...`, error, opts);
this.name = 'ParseError';
this.statusCode = statusCode;
this.statusMessage = http.STATUS_CODES[this.statusCode];
}
};
got.HTTPError = class extends StdError {
constructor(statusCode, headers, opts) {
const statusMessage = http.STATUS_CODES[statusCode];
super(`Response code ${statusCode} (${statusMessage})`, {}, opts);
this.name = 'HTTPError';
this.statusCode = statusCode;
this.statusMessage = statusMessage;
this.headers = headers;
}
};
got.MaxRedirectsError = class extends StdError {
constructor(statusCode, redirectUrls, opts) {
super('Redirected 10 times. Aborting.', {}, opts);
this.name = 'MaxRedirectsError';
this.statusCode = statusCode;
this.statusMessage = http.STATUS_CODES[this.statusCode];
this.redirectUrls = redirectUrls;
}
};
got.UnsupportedProtocolError = class extends StdError {
constructor(opts) {
super(`Unsupported protocol "${opts.protocol}"`, {}, opts);
this.name = 'UnsupportedProtocolError';
}
};
module.exports = got;
+120
View File
@@ -0,0 +1,120 @@
{
"_from": "got@^7.1.0",
"_id": "got@7.1.0",
"_inBundle": false,
"_integrity": "sha512-Y5WMo7xKKq1muPsxD+KmrR8DH5auG7fBdDVueZwETwV6VytKyU9OX/ddpq2/1hp1vIPvVb4T81dKQz3BivkNLw==",
"_location": "/swarm-js/got",
"_phantomChildren": {},
"_requested": {
"type": "range",
"registry": true,
"raw": "got@^7.1.0",
"name": "got",
"escapedName": "got",
"rawSpec": "^7.1.0",
"saveSpec": null,
"fetchSpec": "^7.1.0"
},
"_requiredBy": [
"/swarm-js"
],
"_resolved": "https://registry.npmjs.org/got/-/got-7.1.0.tgz",
"_shasum": "05450fd84094e6bbea56f451a43a9c289166385a",
"_spec": "got@^7.1.0",
"_where": "C:\\Users\\Luis\\source\\repos\\AAMasters\\BackendServers\\node_modules\\swarm-js",
"ava": {
"concurrency": 4
},
"browser": {
"decompress-response": false
},
"bugs": {
"url": "https://github.com/sindresorhus/got/issues"
},
"bundleDependencies": false,
"dependencies": {
"decompress-response": "^3.2.0",
"duplexer3": "^0.1.4",
"get-stream": "^3.0.0",
"is-plain-obj": "^1.1.0",
"is-retry-allowed": "^1.0.0",
"is-stream": "^1.0.0",
"isurl": "^1.0.0-alpha5",
"lowercase-keys": "^1.0.0",
"p-cancelable": "^0.3.0",
"p-timeout": "^1.1.1",
"safe-buffer": "^5.0.1",
"timed-out": "^4.0.0",
"url-parse-lax": "^1.0.0",
"url-to-options": "^1.0.1"
},
"deprecated": false,
"description": "Simplified HTTP requests",
"devDependencies": {
"ava": "^0.20.0",
"coveralls": "^2.11.4",
"form-data": "^2.1.1",
"get-port": "^3.0.0",
"into-stream": "^3.0.0",
"nyc": "^11.0.2",
"pem": "^1.4.4",
"pify": "^3.0.0",
"tempfile": "^2.0.0",
"tempy": "^0.1.0",
"universal-url": "^1.0.0-alpha",
"xo": "^0.18.0"
},
"engines": {
"node": ">=4"
},
"files": [
"index.js"
],
"homepage": "https://github.com/sindresorhus/got#readme",
"keywords": [
"http",
"https",
"get",
"got",
"url",
"uri",
"request",
"util",
"utility",
"simple",
"curl",
"wget",
"fetch",
"net",
"network",
"electron"
],
"license": "MIT",
"maintainers": [
{
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
},
{
"name": "Vsevolod Strukchinsky",
"email": "floatdrop@gmail.com",
"url": "github.com/floatdrop"
},
{
"name": "Alexander Tesfamichael",
"email": "alex.tesfamichael@gmail.com",
"url": "alextes.me"
}
],
"name": "got",
"repository": {
"type": "git",
"url": "git+https://github.com/sindresorhus/got.git"
},
"scripts": {
"coveralls": "nyc report --reporter=text-lcov | coveralls",
"test": "xo && nyc ava"
},
"version": "7.1.0"
}
+77
View File
@@ -0,0 +1,77 @@
'use strict';
class CancelError extends Error {
constructor() {
super('Promise was canceled');
this.name = 'CancelError';
}
}
class PCancelable {
static fn(fn) {
return function () {
const args = [].slice.apply(arguments);
return new PCancelable((onCancel, resolve, reject) => {
args.unshift(onCancel);
fn.apply(null, args).then(resolve, reject);
});
};
}
constructor(executor) {
this._pending = true;
this._canceled = false;
this._promise = new Promise((resolve, reject) => {
this._reject = reject;
return executor(
fn => {
this._cancel = fn;
},
val => {
this._pending = false;
resolve(val);
},
err => {
this._pending = false;
reject(err);
}
);
});
}
then() {
return this._promise.then.apply(this._promise, arguments);
}
catch() {
return this._promise.catch.apply(this._promise, arguments);
}
cancel() {
if (!this._pending || this._canceled) {
return;
}
if (typeof this._cancel === 'function') {
try {
this._cancel();
} catch (err) {
this._reject(err);
}
}
this._canceled = true;
this._reject(new CancelError());
}
get canceled() {
return this._canceled;
}
}
Object.setPrototypeOf(PCancelable.prototype, Promise.prototype);
module.exports = PCancelable;
module.exports.CancelError = CancelError;
+50
View File
@@ -0,0 +1,50 @@
{
"name": "p-cancelable",
"version": "0.3.0",
"description": "Create a promise that can be canceled",
"license": "MIT",
"repository": "sindresorhus/p-cancelable",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
},
"engines": {
"node": ">=4"
},
"scripts": {
"test": "xo && ava"
},
"files": [
"index.js"
],
"keywords": [
"promise",
"cancelable",
"cancel",
"canceled",
"canceling",
"cancellable",
"cancellation",
"abort",
"abortable",
"aborting",
"cleanup",
"task",
"token",
"async",
"function",
"await",
"promises",
"bluebird"
],
"devDependencies": {
"ava": "*",
"delay": "^2.0.0",
"xo": "*"
}
,"_resolved": "https://registry.npmjs.org/p-cancelable/-/p-cancelable-0.3.0.tgz"
,"_integrity": "sha512-RVbZPLso8+jFeq1MfNvgXtCRED2raz/dKpacfTNxsx6pLEpEomM7gah6VeHSYV3+vo0OAi4MkArtQcWWXuQoyw=="
,"_from": "p-cancelable@^0.3.0"
}
+14
View File
@@ -0,0 +1,14 @@
'use strict';
module.exports = function (url) {
if (typeof url !== 'string') {
throw new TypeError('Expected a string, got ' + typeof url);
}
url = url.trim();
if (/^\.*\/|^(?!localhost)\w+:/.test(url)) {
return url;
}
return url.replace(/^(?!(?:\w+:)?\/\/)/, 'http://');
};
+39
View File
@@ -0,0 +1,39 @@
{
"name": "prepend-http",
"version": "1.0.4",
"description": "Prepend `http://` to humanized URLs like todomvc.com and localhost",
"license": "MIT",
"repository": "sindresorhus/prepend-http",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
},
"engines": {
"node": ">=0.10.0"
},
"scripts": {
"test": "xo && ava"
},
"files": [
"index.js"
],
"keywords": [
"prepend",
"protocol",
"scheme",
"url",
"uri",
"http",
"https",
"humanized"
],
"devDependencies": {
"ava": "*",
"xo": "*"
}
,"_resolved": "https://registry.npmjs.org/prepend-http/-/prepend-http-1.0.4.tgz"
,"_integrity": "sha1-1PRWKwzjaW5BrFLQ4ALlemNdxtw="
,"_from": "prepend-http@^1.0.1"
}
+14
View File
@@ -0,0 +1,14 @@
'use strict';
var url = require('url');
var prependHttp = require('prepend-http');
module.exports = function (x) {
var withProtocol = prependHttp(x);
var parsed = url.parse(withProtocol);
if (withProtocol !== x) {
parsed.protocol = null;
}
return parsed;
};
+45
View File
@@ -0,0 +1,45 @@
{
"name": "url-parse-lax",
"version": "1.0.0",
"description": "url.parse() with support for protocol-less URLs & IPs",
"license": "MIT",
"repository": "sindresorhus/url-parse-lax",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
},
"engines": {
"node": ">=0.10.0"
},
"scripts": {
"test": "node test.js"
},
"files": [
"index.js"
],
"keywords": [
"url",
"uri",
"parse",
"parser",
"loose",
"lax",
"protocol",
"less",
"protocol-less",
"ip",
"ipv4",
"ipv6"
],
"dependencies": {
"prepend-http": "^1.0.1"
},
"devDependencies": {
"ava": "0.0.4"
}
,"_resolved": "https://registry.npmjs.org/url-parse-lax/-/url-parse-lax-1.0.0.tgz"
,"_integrity": "sha1-evjzA2Rem9eaJy56FKxovAYJ2nM="
,"_from": "url-parse-lax@^1.0.0"
}
+81
View File
@@ -0,0 +1,81 @@
{
"_from": "swarm-js@^0.1.40",
"_id": "swarm-js@0.1.40",
"_inBundle": false,
"_integrity": "sha512-yqiOCEoA4/IShXkY3WKwP5PvZhmoOOD8clsKA7EEcRILMkTEYHCQ21HDCAcVpmIxZq4LyZvWeRJ6quIyHk1caA==",
"_location": "/swarm-js",
"_phantomChildren": {
"decompress-response": "3.3.0",
"duplexer3": "0.1.4",
"is-plain-obj": "1.1.0",
"is-retry-allowed": "1.2.0",
"is-stream": "1.1.0",
"isurl": "1.0.0",
"lowercase-keys": "1.0.1",
"p-timeout": "1.2.1",
"safe-buffer": "5.1.2",
"timed-out": "4.0.1",
"url-to-options": "1.0.1"
},
"_requested": {
"type": "range",
"registry": true,
"raw": "swarm-js@^0.1.40",
"name": "swarm-js",
"escapedName": "swarm-js",
"rawSpec": "^0.1.40",
"saveSpec": null,
"fetchSpec": "^0.1.40"
},
"_requiredBy": [
"/web3-bzz"
],
"_resolved": "https://registry.npmjs.org/swarm-js/-/swarm-js-0.1.40.tgz",
"_shasum": "b1bc7b6dcc76061f6c772203e004c11997e06b99",
"_spec": "swarm-js@^0.1.40",
"_where": "C:\\Users\\Luis\\source\\repos\\AAMasters\\BackendServers\\node_modules\\web3-bzz",
"author": {
"name": "MaiaVictor"
},
"browser": "lib/api-browser.js",
"bugs": {
"url": "https://github.com/maiavictor/swarm-js/issues"
},
"bundleDependencies": false,
"dependencies": {
"bluebird": "^3.5.0",
"buffer": "^5.0.5",
"eth-lib": "^0.1.26",
"fs-extra": "^4.0.2",
"got": "^7.1.0",
"mime-types": "^2.1.16",
"mkdirp-promise": "^5.0.1",
"mock-fs": "^4.1.0",
"setimmediate": "^1.0.5",
"tar": "^4.0.2",
"xhr-request": "^1.0.1"
},
"deprecated": false,
"description": "Swarm tools for JavaScript.",
"devDependencies": {
"@babel/cli": "^7.0.0",
"@babel/core": "^7.0.0",
"@babel/preset-env": "^7.0.0",
"decompress": "^4.0.0",
"xml2js": "^0.4.19"
},
"homepage": "https://github.com/maiavictor/swarm-js#readme",
"license": "MIT",
"main": "lib/api-node.js",
"name": "swarm-js",
"repository": {
"type": "git",
"url": "git+https://github.com/maiavictor/swarm-js.git"
},
"scripts": {
"babel": "babel src --out-dir=lib",
"build": "npm run babel",
"test": "echo \"Error: no test specified\" && exit 1"
},
"version": "0.1.40"
}
+5
View File
@@ -0,0 +1,5 @@
{
"darwin-amd64": {
"archive": "swarm-darwin-amd64-0.3.3-89451f7c.tar.gz"
}
}
+96
View File
@@ -0,0 +1,96 @@
const Q = require("bluebird");
const decompress = require("decompress");
const files = require("./../src/files.js");
const fs = require("fs-extra");
const got = require("got");
const path = require("path");
const tar = require("tar");
const xml = require("xml2js");
// Downloads list of Geth releases
console.log("Retrieving list of Geth releases from https://gethstore.blob.core.windows.net.");
got("https://gethstore.blob.core.windows.net/builds?restype=container&comp=list")
// Parses XML
.then((res) => new Q((resolve, reject) => {
console.log("Parsing XML.");
xml.parseString(res.body, (err, res) => {
if (err) {
reject();
}
else {
resolve(res.EnumerationResults.Blobs[0].Blob);
}
});
}))
// Formats data
.then(blobs => {
console.log("Formatting blobs.");
return blobs
.filter((file) => {
const isStable = !/unstable/.test(file.Name[0]);
const hasSwarm = /alltools/.test(file.Name[0]);
const isArchive = !/\.asc$/.test(file.Name[0]);
return isStable && hasSwarm && isArchive;
})
.map((file) => {
const gethArchiveName = file.Name[0];
const parts = gethArchiveName.split("-");
const props = file.Properties[0];
const swarmArchiveExt = parts[2] === "windows" ? ".exe" : "";
const swarmArchiveName = `swarm-${parts[2]}-${parts[3]}-${parts[4]}${swarmArchiveExt}`;
return {
date: new Date(props["Last-Modified"]),
hash: props["Content-MD5"][0],
os: parts[2],
arch: parts[3],
version: parts[4],
gethArchiveName: gethArchiveName,
gethArchivePath: path.join(process.cwd(), "tmp_downloads", gethArchiveName),
gethArchiveUrl: "https://gethstore.blob.core.windows.net/builds/" + gethArchiveName,
gethFilesPath: path.join(process.cwd(), "tmp_downloads", gethArchiveName.replace(/(\.zip|\.tar\.gz)/,"")),
swarmArchiveName: swarmArchiveName,
swarmBinaryDir: path.join(process.cwd(), "tmp_downloads", swarmArchiveName),
swarmBinaryPath: path.join(process.cwd(), "tmp_downloads", swarmArchiveName, "swarm" + swarmArchiveExt),
swarmArchivePath: path.join(process.cwd(), "archives", swarmArchiveName + ".tar.gz"),
}
})
.sort((a, b) => a.date - b.date)
})
// Downloads Geth archives
.then(binaries => {
console.log("Downloading archives.");
if (!fs.existsSync("archives"))
fs.mkdirSync(path.join(process.cwd(), "archives"));
let archives = {};
return Q.all(binaries
.filter(bin => bin.version === binaries[binaries.length - 1].version)
.map(bin => {
const archive = archives[bin.os + "-" + bin.arch] = {
archive: bin.swarmArchiveName
};
console.log("Downloading " + bin.gethArchivePath + ".");
return files.download(bin.gethArchiveUrl)(bin.gethArchivePath)
.then(path => (console.log("Downloaded " + path + "."), path))
.then(path => decompress(path, "tmp_downloads"))
.then(() => !fs.existsSync(bin.swarmBinaryDir) && fs.mkdir(bin.swarmBinaryDir))
.then(() => files.search(/swarm(.exe|)$/)(bin.gethFilesPath))
.then(([swarmPath]) => fs.rename(swarmPath, bin.swarmBinaryPath))
.then(() => files.hash("md5")(bin.swarmBinaryPath))
.then(binaryMD5 => archive.binaryMD5 = binaryMD5)
.then(() => tar.c({gzip: true, file: bin.swarmArchivePath}, bin.swarmBinaryDir))
.then(() => files.hash("md5")(bin.swarmArchivePath))
.then(archiveMD5 => archive.archiveMD5 = archiveMD5)
.catch(e => console.log(e));
}))
.then(() => fs.writeFileSync("archives/archives.json", JSON.stringify(archives, null, 2)));
})
// Finishes
.then(() => {
fs.removeSync(path.join(process.cwd(), "tmp_downloads"));
console.log("Done.")
})
.catch(e => console.log(e));
+30
View File
@@ -0,0 +1,30 @@
const unavailable = () => { throw "This swarm.js function isn't available on the browser."; }
const fs = {readFile: unavailable};
const files = {download: unavailable, safeDownloadArchived: unavailable, directoryTree: unavailable};
const os = {platform: unavailable, arch: unavailable};
const path = {join: unavailable, slice: unavailable};
const child_process = {spawn: unavailable};
const mimetype = {lookup: unavailable};
const defaultArchives = {};
const downloadUrl = null;
const request = require("xhr-request");
const bytes = require("eth-lib/lib/bytes");
const hash = require("./swarm-hash.js");
const pick = require("./pick.js");
const swarm = require("./swarm");
module.exports = swarm({
fs,
files,
os,
path,
child_process,
defaultArchives,
mimetype,
request,
downloadUrl,
bytes,
hash,
pick
});
+42
View File
@@ -0,0 +1,42 @@
const fs = require("fs-extra");
const files = require("./files.js");
const os = require("os");
const path = require("path");
const child_process = require("child_process");
const mimetype = require('mime-types');
const defaultArchives = require("./../archives/archives.json");
const requester = require("xhr-request");
const downloadUrl = "http://ethereum-mist.s3.amazonaws.com/swarm/";
const bytes = require("eth-lib/lib/bytes");
const hash = require("./swarm-hash.js");
const pick = require("./pick.js");
const swarm = require("./swarm");
// Fixes issue that causes xhr-request-promise on Node.js to only accept Buffer
const request = (url, params, callback) => {
let newParams = {};
for (let key in params) {
newParams[key] = params[key];
}
if (typeof newParams.body !== "undefined") {
newParams.body = newParams.body instanceof Buffer
? newParams.body
: new Buffer(newParams.body);
}
return requester(url, newParams, callback);
};
module.exports = swarm({
fs,
files,
os,
path,
child_process,
defaultArchives,
mimetype,
request,
downloadUrl,
bytes,
hash,
pick
});
+193
View File
@@ -0,0 +1,193 @@
// This module implements some file download utils. Its most important export
// is `safeDownloadTargzFile`, which, given a file, its md5, a tar.gz url, its
// md5 and a path, returns a Promise that will only resolve once the exact file
// you expect is available on that path.
const Q = require("bluebird");
const assert = require("assert");
const crypto = require("crypto");
const fs = require("fs-extra");
const got = require("got");
const mkdirp = require("mkdirp-promise");
const path = require("path");
const tar = require("tar");
// String -> String ~> Promise String
// Downloads a file from an url to a path.
// Returns a promise containing the path.
const download = url => filePath => {
const promise = Q.resolve(mkdirp(path.dirname(filePath))).then(() =>
new Q((resolve, reject) => {
const writeStream = fs.createWriteStream(filePath);
const downloadStream = got.stream(url);
downloadStream.on("end", () => resolve(filePath));
downloadStream.on("data", chunk => promise.onDataCallback(chunk));
downloadStream.on("error", reject);
downloadStream.pipe(writeStream);
}));
promise.onDataCallback = () => {};
promise.onData = callback => {
promise.onDataCallback = callback || (() => {});
return promise;
}
return promise;
};
// String -> String ~> Promise String
// Hashes a file using the given algorithm (ex: "md5").
// Returns a promise containing the hashed string.
const hash = algorithm => path =>
new Q((resolve, reject) => {
const readStream = fs.ReadStream(path);
const hash = crypto.createHash(algorithm);
readStream.on("data", d => hash.update(d));
readStream.on("end", () => resolve(hash.digest("hex")));
readStream.on("error", reject);
});
// String -> String ~> Promise ()
// Asserts a file matches this md5 hash.
// Returns a promise containing its path.
const checksum = fileHash => path =>
hash("md5")(path)
.then(actualHash => actualHash === fileHash)
.then(assert)
.then(() => path);
// String ~> String ~> String ~> Promise String
// Downloads a file to a directory, check.
// Checks if the md5 hash matches.
// Returns a promise containing the path.
const downloadAndCheck = url => path => fileHash =>
download(url)(path)
.then(checksum(fileHash));
// String -> String ~> Promise String
// TODO: work for zip and other types
const extract = fromPath => toPath =>
tar.x(fromPath, toPath)
.then(() => toPath);
// String ~> Promise String
// Reads a file as an UTF8 string.
// Returns a promise containing that string.
const readUTF8 = path =>
fs.readFile (path, {encoding: "utf8"});
// String ~> Promise Bool
const isDirectory = path =>
fs.exists(path)
.then(assert)
.then(() => fs.lstat(path))
.then(stats => stats.isDirectory())
.catch(() => false);
// String -> Promise String
const directoryTree = dirPath => {
let paths = [];
const search = dirPath =>
isDirectory(dirPath).then(isDir => {
if (isDir) {
const searchOnDir = dir => search (path.join(dirPath, dir));
return Q.all(Q.map(fs.readdir(dirPath), searchOnDir));
} else {
paths.push(dirPath);
};
});
return Q.all(search (dirPath)).then(() => paths);
}
// Regex -> String ~> Promise (Array String)
const search = regex => dirPath =>
directoryTree(dirPath)
.then(tree => tree.filter(path => regex.test(path)));
// String -> String -> String -> String ~> Promise String
// Downloads a file inside a tar.gz and places it at `filePath`.
// Checks the md5 hash of the tar before extracting it.
// Checks the md5 hash of the file after extracting it.
// If all is OK, returns a promise containing the file path.
const safeDownloadArchived = url => archiveHash => fileHash => filePath => {
const fileDir = path.dirname(filePath);
const fileName = path.basename(filePath);
const archivePath = path.join(fileDir, ".swarm_downloads/files.tar.gz");
const archiveDir = path.dirname(archivePath);
const promise = Q.resolve(mkdirp(archiveDir))
.then(() => checksum (fileHash) (filePath))
.then(() => filePath)
.catch(() => fs.exists(archiveDir)
.then(exists => !exists ? fs.mkdir(archiveDir) : null)
.then(() => download (url)(archivePath).onData(promise.onDataCallback))
.then(() => hash("md5")(archivePath))
.then(() => archiveHash ? checksum(archiveHash)(archivePath) : null)
.then(() => extract (archivePath) (archiveDir))
.then(() => search (new RegExp(fileName+"$")) (archiveDir))
.then(fp => fs.rename (fp[0], filePath))
.then(() => fs.unlink (archivePath))
.then(() => fileHash ? checksum(fileHash)(filePath) : null)
.then(() => filePath));
promise.onDataCallback = () => {};
promise.onData = callback => {
promise.onDataCallback = callback || (() => {});
return promise;
};
return promise;
};
// String -> String ~> Promise String
// Like `safeDownloadArchivedFile`, but without the checksums.
const downloadArchived = url => path =>
safeDownloadArchived (url) (null) (null) (path);
// () => Promise Bool
// Tests the implementation by downloading a predetermined tar.gz
// from a mocked HTTP-server into a mocked filesystem. Does some
// redundancy tests such as checking the file constents and double
// checking its MD5 hash.
// Returns a promise containing a boolean, true if tests passed.
const test = () => {
const filePath = "/swarm/foo.txt";
const fileHash = "d3b07384d113edec49eaa6238ad5ff00";
const archiveUrl = "http://localhost:12534";
const archiveHash = "7fa45f946bb2a696bdd9972e0fbceac2";
const archiveData = new Buffer([
0x1f,0x8b,0x08,0x00,0xf1,0x34,0xaf,0x58,0x00,0x03,0xed,0xcf,0x3d,0x0e,0x83,0x30,
0x0c,0x86,0x61,0x66,0x4e,0xe1,0x13,0x54,0xce,0x0f,0xc9,0x79,0x58,0xb2,0x46,0x82,
0x14,0x71,0x7c,0xd2,0x06,0x31,0x52,0x75,0x40,0x08,0xe9,0x7d,0x96,0x4f,0x96,0x3d,
0x7c,0x4e,0x39,0xbf,0xca,0x5a,0xba,0x2b,0xa9,0x6a,0xf0,0x5e,0x3e,0x19,0xc3,0xf0,
0x4d,0xb5,0x6d,0xde,0x79,0x31,0x4e,0x07,0x17,0x9c,0xb5,0x31,0x8a,0x1a,0xab,0xc6,
0x77,0xa2,0x97,0xb6,0xda,0xbd,0xe7,0x32,0x4e,0xb5,0xca,0xf2,0xe3,0xae,0x9e,0xa5,
0x74,0xb2,0x6f,0x8f,0xc8,0x91,0x0f,0x91,0x72,0xee,0xef,0xee,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xf8,0xdf,0x06,0xb3,0x2a,0xda,
0xed,0x00,0x28,0x00,0x00]);
const crypto = require("crypto");
const fsMock = require("mock-fs")({"/swarm":{}});
const httpMock = require("http")
.createServer((_,res) => res.end(archiveData))
.listen(12534);
return safeDownloadArchived (archiveUrl) (archiveHash) (fileHash) (filePath)
.then(checksum (fileHash))
.then(readUTF8)
.then(text => text === "foo\n")
.then(assert)
.then(() => safeDownloadArchived (archiveUrl) (archiveHash) (fileHash) (filePath))
.then(() => true)
.catch(false)
.finally(() => httpMock.close());
};
module.exports = {
download,
hash,
checksum,
downloadAndCheck,
extract,
readUTF8,
safeDownloadArchived,
directoryTree,
downloadArchived,
search,
test
};
+54
View File
@@ -0,0 +1,54 @@
const picker = type => () => new Promise((resolve, reject) => {
const fileLoader = e => {
const directory = {};
const totalFiles = e.target.files.length;
let loadedFiles = 0;
[].map.call(e.target.files, file => {
const reader = new FileReader();
reader.onload = e => {
const data = new Uint8Array(e.target.result);
if (type === "directory") {
const path = file.webkitRelativePath;
directory[path.slice(path.indexOf("/")+1)] = {
type: "text/plain",
data: data
};
if (++loadedFiles === totalFiles)
resolve(directory);
} else if (type === "file") {
const path = file.webkitRelativePath;
resolve({"type": mimetype.lookup(path), "data": data});
} else {
resolve(data);
}
};
reader.readAsArrayBuffer(file);
});
};
let fileInput;
if (type === "directory") {
fileInput = document.createElement("input");
fileInput.addEventListener("change", fileLoader);
fileInput.type = "file";
fileInput.webkitdirectory = true;
fileInput.mozdirectory = true;
fileInput.msdirectory = true;
fileInput.odirectory = true;
fileInput.directory = true;
} else {
fileInput = document.createElement("input");
fileInput.addEventListener("change", fileLoader);
fileInput.type = "file";
};
const mouseEvent = document.createEvent("MouseEvents");
mouseEvent.initEvent("click", true, false);
fileInput.dispatchEvent(mouseEvent);
});
module.exports = {
data: picker("data"),
file: picker("file"),
directory: picker("directory"),
}
+40
View File
@@ -0,0 +1,40 @@
// Thanks https://github.com/axic/swarmhash
const keccak = require("eth-lib/lib/hash").keccak256;
const Bytes = require("eth-lib/lib/bytes");
const swarmHashBlock = (length, data) => {
const lengthEncoded = Bytes.reverse(Bytes.pad(6, Bytes.fromNumber(length)));
const bytes = Bytes.flatten([lengthEncoded, "0x0000", data]);
return keccak(bytes).slice(2);
};
// (Bytes | Uint8Array | String) -> String
const swarmHash = data => {
if (typeof data === "string" && data.slice(0,2) !== "0x") {
data = Bytes.fromString(data);
} else if (typeof data !== "string" && data.length !== undefined) {
data = Bytes.fromUint8Array(data);
}
const length = Bytes.length(data);
if (length <= 4096) {
return swarmHashBlock(length, data);
}
let maxSize = 4096;
while ((maxSize * (4096 / 32)) < length) {
maxSize *= (4096 / 32);
}
let innerNodes = [];
for (let i = 0; i < length; i += maxSize) {
const size = (maxSize < (length - i)) ? maxSize : (length - i);
innerNodes.push(swarmHash(Bytes.slice(data, i, i + size)));
}
return swarmHashBlock(length, Bytes.flatten(innerNodes));
};
module.exports = swarmHash;
+505
View File
@@ -0,0 +1,505 @@
// TODO: this is a temporary fix to hide those libraries from the browser. A
// slightly better long-term solution would be to split this file into two,
// separating the functions that are used on Node.js from the functions that
// are used only on the browser.
module.exports = ({
fs,
files,
os,
path,
child_process,
mimetype,
defaultArchives,
request,
downloadUrl,
bytes,
hash,
pick
}) => {
// ∀ a . String -> JSON -> Map String a -o Map String a
// Inserts a key/val pair in an object impurely.
const impureInsert = key => val => map =>
(map[key] = val, map);
// String -> JSON -> Map String JSON
// Merges an array of keys and an array of vals into an object.
const toMap = keys => vals => {
let map = {};
for (let i = 0, l = keys.length; i < l; ++i)
map[keys[i]] = vals[i];
return map;
};
// ∀ a . Map String a -> Map String a -> Map String a
// Merges two maps into one.
const merge = a => b => {
let map = {};
for (let key in a)
map[key] = a[key];
for (let key in b)
map[key] = b[key];
return map;
};
// ∀ a . [a] -> [a] -> Bool
const equals = a => b => {
if (a.length !== b.length) {
return false;
} else {
for (let i = 0, l = a.length; i < l; ++i) {
if (a[i] !== b[i]) return false;
}
}
return true;
}
// String -> String -> String
const rawUrl = swarmUrl => hash =>
`${swarmUrl}/bzz-raw:/${hash}`
// String -> String -> Promise Uint8Array
// Gets the raw contents of a Swarm hash address.
const downloadData = swarmUrl => hash =>
new Promise((resolve, reject) => {
request(rawUrl(swarmUrl)(hash), {responseType: "arraybuffer"}, (err, arrayBuffer, response) => {
if (err) {
return reject(err);
}
if (response.statusCode >= 400) {
return reject(new Error(`Error ${response.statusCode}.`));
}
return resolve(new Uint8Array(arrayBuffer));
})
});
// type Entry = {"type": String, "hash": String}
// type File = {"type": String, "data": Uint8Array}
// String -> String -> Promise (Map String Entry)
// Solves the manifest of a Swarm address recursively.
// Returns a map from full paths to entries.
const downloadEntries = swarmUrl => hash => {
const search = hash => path => routes => {
// Formats an entry to the Swarm.js type.
const format = entry => ({
type: entry.contentType,
hash: entry.hash});
// To download a single entry:
// if type is bzz-manifest, go deeper
// if not, add it to the routing table
const downloadEntry = entry => {
if (entry.path === undefined) {
return Promise.resolve();
} else {
return entry.contentType === "application/bzz-manifest+json"
? search (entry.hash) (path + entry.path) (routes)
: Promise.resolve (impureInsert (path + entry.path) (format(entry)) (routes));
}
}
// Downloads the initial manifest and then each entry.
return downloadData(swarmUrl)(hash)
.then(text => JSON.parse(toString(text)).entries)
.then(entries => Promise.all(entries.map(downloadEntry)))
.then(() => routes);
}
return search (hash) ("") ({});
}
// String -> String -> Promise (Map String String)
// Same as `downloadEntries`, but returns only hashes (no types).
const downloadRoutes = swarmUrl => hash =>
downloadEntries(swarmUrl)(hash)
.then(entries => toMap
(Object.keys(entries))
(Object.keys(entries).map(route => entries[route].hash)));
// String -> String -> Promise (Map String File)
// Gets the entire directory tree in a Swarm address.
// Returns a promise mapping paths to file contents.
const downloadDirectory = swarmUrl => hash =>
downloadEntries (swarmUrl) (hash)
.then(entries => {
const paths = Object.keys(entries);
const hashs = paths.map(path => entries[path].hash);
const types = paths.map(path => entries[path].type);
const datas = hashs.map(downloadData(swarmUrl));
const files = datas => datas.map((data, i) => ({type: types[i], data: data}));
return Promise.all(datas).then(datas => toMap(paths)(files(datas)));
});
// String -> String -> String -> Promise String
// Gets the raw contents of a Swarm hash address.
// Returns a promise with the downloaded file path.
const downloadDataToDisk = swarmUrl => hash => filePath =>
files.download (rawUrl(swarmUrl)(hash)) (filePath);
// String -> String -> String -> Promise (Map String String)
// Gets the entire directory tree in a Swarm address.
// Returns a promise mapping paths to file contents.
const downloadDirectoryToDisk = swarmUrl => hash => dirPath =>
downloadRoutes (swarmUrl) (hash)
.then(routingTable => {
let downloads = [];
for (let route in routingTable) {
if (route.length > 0) {
const filePath = path.join(dirPath, route);
downloads.push(downloadDataToDisk(swarmUrl)(routingTable[route])(filePath));
};
};
return Promise.all(downloads).then(() => dirPath);
});
// String -> Uint8Array -> Promise String
// Uploads raw data to Swarm.
// Returns a promise with the uploaded hash.
const uploadData = swarmUrl => data =>
new Promise((resolve, reject) => {
const params = {
body: typeof data === "string" ? fromString(data) : data,
method: "POST"
};
request(`${swarmUrl}/bzz-raw:/`, params, (err, data) => {
if (err) {
return reject(err);
}
return resolve(data);
});
});
// String -> String -> String -> File -> Promise String
// Uploads a file to the Swarm manifest at a given hash, under a specific
// route. Returns a promise containing the uploaded hash.
// FIXME: for some reasons Swarm-Gateways is sometimes returning
// error 404 (bad request), so we retry up to 3 times. Why?
const uploadToManifest = swarmUrl => hash => route => file => {
const attempt = n => {
const slashRoute = route[0] === "/" ? route : "/" + route;
const url = `${swarmUrl}/bzz:/${hash}${slashRoute}`;
const opt = {
method: "PUT",
headers: {"Content-Type": file.type},
body: file.data};
return new Promise((resolve, reject) => {
request(url, opt, (err, data) => {
if (err) {
return reject(err);
}
if (data.indexOf("error") !== -1) {
return reject(data);
}
return resolve(data);
});
}).catch(e => n > 0 && attempt(n-1));
};
return attempt(3);
};
// String -> {type: String, data: Uint8Array} -> Promise String
const uploadFile = swarmUrl => file =>
uploadDirectory(swarmUrl)({"": file});
// String -> String -> Promise String
const uploadFileFromDisk = swarmUrl => filePath =>
fs.readFile(filePath)
.then(data => uploadFile(swarmUrl)({type: mimetype.lookup(filePath), data: data}));
// String -> Map String File -> Promise String
// Uploads a directory to Swarm. The directory is
// represented as a map of routes and files.
// A default path is encoded by having a "" route.
const uploadDirectory = swarmUrl => directory =>
uploadData(swarmUrl)("{}")
.then(hash => {
const uploadRoute = route => hash => uploadToManifest(swarmUrl)(hash)(route)(directory[route]);
const uploadToHash = (hash, route) => hash.then(uploadRoute(route));
return Object.keys(directory).reduce(uploadToHash, Promise.resolve(hash));
});
// String -> Promise String
const uploadDataFromDisk = swarmUrl => filePath =>
fs.readFile(filePath)
.then(uploadData(swarmUrl));
// String -> Nullable String -> String -> Promise String
const uploadDirectoryFromDisk = swarmUrl => defaultPath => dirPath =>
files.directoryTree(dirPath)
.then(fullPaths => Promise.all(fullPaths.map(path => fs.readFile(path))).then(datas => {
const paths = fullPaths.map(path => path.slice(dirPath.length));
const types = fullPaths.map(path => mimetype.lookup(path) || "text/plain");
return toMap (paths) (datas.map((data, i) => ({type: types[i], data: data})));
}))
.then(directory => merge (defaultPath ? {"": directory[defaultPath]} : {}) (directory))
.then(uploadDirectory(swarmUrl));
// String -> UploadInfo -> Promise String
// Simplified multi-type upload which calls the correct
// one based on the type of the argument given.
const upload = swarmUrl => arg => {
// Upload raw data from browser
if (arg.pick === "data") {
return pick.data().then(uploadData(swarmUrl));
// Upload a file from browser
} else if (arg.pick === "file") {
return pick.file().then(uploadFile(swarmUrl));
// Upload a directory from browser
} else if (arg.pick === "directory") {
return pick.directory().then(uploadDirectory(swarmUrl));
// Upload directory/file from disk
} else if (arg.path) {
switch (arg.kind) {
case "data": return uploadDataFromDisk(swarmUrl)(arg.path);
case "file": return uploadFileFromDisk(swarmUrl)(arg.path);
case "directory": return uploadDirectoryFromDisk(swarmUrl)(arg.defaultFile)(arg.path);
};
// Upload UTF-8 string or raw data (buffer)
} else if (arg.length || typeof arg === "string") {
return uploadData(swarmUrl)(arg);
// Upload directory with JSON
} else if (arg instanceof Object) {
return uploadDirectory(swarmUrl)(arg);
}
return Promise.reject(new Error("Bad arguments"));
}
// String -> String -> Nullable String -> Promise (String | Uint8Array | Map String Uint8Array)
// Simplified multi-type download which calls the correct function based on
// the type of the argument given, and on whether the Swwarm address has a
// directory or a file.
const download = swarmUrl => hash => path =>
isDirectory(swarmUrl)(hash).then(isDir => {
if (isDir) {
return path
? downloadDirectoryToDisk(swarmUrl)(hash)(path)
: downloadDirectory(swarmUrl)(hash);
} else {
return path
? downloadDataToDisk(swarmUrl)(hash)(path)
: downloadData(swarmUrl)(hash);
}
});
// String -> Promise String
// Downloads the Swarm binaries into a path. Returns a promise that only
// resolves when the exact Swarm file is there, and verified to be correct.
// If it was already there to begin with, skips the download.
const downloadBinary = (path, archives) => {
const system = os.platform().replace("win32","windows") + "-" + (os.arch() === "x64" ? "amd64" : "386");
const archive = (archives || defaultArchives)[system];
const archiveUrl = downloadUrl + archive.archive + ".tar.gz";
const archiveMD5 = archive.archiveMD5;
const binaryMD5 = archive.binaryMD5;
return files.safeDownloadArchived(archiveUrl)(archiveMD5)(binaryMD5)(path);
};
// type SwarmSetup = {
// account : String,
// password : String,
// dataDir : String,
// binPath : String,
// ensApi : String,
// onDownloadProgress : Number ~> (),
// archives : [{
// archive: String,
// binaryMD5: String,
// archiveMD5: String
// }]
// }
// SwarmSetup ~> Promise Process
// Starts the Swarm process.
const startProcess = swarmSetup => new Promise((resolve, reject) => {
const {spawn} = child_process;
const hasString = str => buffer => ('' + buffer).indexOf(str) !== -1;
const {account, password, dataDir, ensApi, privateKey} = swarmSetup;
const STARTUP_TIMEOUT_SECS = 3;
const WAITING_PASSWORD = 0;
const STARTING = 1;
const LISTENING = 2;
const PASSWORD_PROMPT_HOOK = "Passphrase";
const LISTENING_HOOK = "Swarm http proxy started";
let state = WAITING_PASSWORD;
const swarmProcess = spawn(swarmSetup.binPath, [
'--bzzaccount', account || privateKey,
'--datadir', dataDir,
'--ens-api', ensApi]);
const handleProcessOutput = data => {
if (state === WAITING_PASSWORD && hasString (PASSWORD_PROMPT_HOOK) (data)) {
setTimeout(() => {
state = STARTING;
swarmProcess.stdin.write(password + '\n');
}, 500);
} else if (hasString (LISTENING_HOOK) (data)) {
state = LISTENING;
clearTimeout(timeout);
resolve(swarmProcess);
}
}
swarmProcess.stdout.on('data', handleProcessOutput);
swarmProcess.stderr.on('data', handleProcessOutput);
//swarmProcess.on('close', () => setTimeout(restart, 2000));
let restart = () => startProcess(swarmSetup).then(resolve).catch(reject);
let error = () => reject(new Error("Couldn't start swarm process."));
let timeout = setTimeout(error, 20000);
});
// Process ~> Promise ()
// Stops the Swarm process.
const stopProcess = process => new Promise((resolve, reject) => {
process.stderr.removeAllListeners('data');
process.stdout.removeAllListeners('data');
process.stdin.removeAllListeners('error');
process.removeAllListeners('error');
process.removeAllListeners('exit');
process.kill('SIGINT');
const killTimeout = setTimeout(
() => process.kill('SIGKILL'),
8000);
process.once('close', () => {
clearTimeout(killTimeout);
resolve();
});
});
// SwarmSetup -> (SwarmAPI -> Promise ()) -> Promise ()
// Receives a Swarm configuration object and a callback function. It then
// checks if a local Swarm node is running. If no local Swarm is found, it
// downloads the Swarm binaries to the dataDir (if not there), checksums,
// starts the Swarm process and calls the callback function with an API
// object using the local node. That callback must return a promise which
// will resolve when it is done using the API, so that this function can
// close the Swarm process properly. Returns a promise that resolves when the
// user is done with the API and the Swarm process is closed.
// TODO: check if Swarm process is already running (improve `isAvailable`)
const local = swarmSetup => useAPI =>
isAvailable("http://localhost:8500").then(isAvailable =>
isAvailable
? useAPI(at("http://localhost:8500")).then(() => {})
: downloadBinary(swarmSetup.binPath, swarmSetup.archives)
.onData(data => (swarmSetup.onProgress || (() => {}))(data.length))
.then(() => startProcess(swarmSetup))
.then(process => useAPI(at("http://localhost:8500")).then(() => process))
.then(stopProcess));
// String ~> Promise Bool
// Returns true if Swarm is available on `url`.
// Perfoms a test upload to determine that.
// TODO: improve this?
const isAvailable = swarmUrl => {
const testFile = "test";
const testHash = "c9a99c7d326dcc6316f32fe2625b311f6dc49a175e6877681ded93137d3569e7";
return uploadData(swarmUrl)(testFile)
.then(hash => hash === testHash)
.catch(() => false);
};
// String -> String ~> Promise Bool
// Returns a Promise which is true if that Swarm address is a directory.
// Determines that by checking that it (i) is a JSON, (ii) has a .entries.
// TODO: improve this?
const isDirectory = swarmUrl => hash =>
downloadData(swarmUrl)(hash)
.then(data => {
try {
return !!JSON.parse(toString(data)).entries;
} catch (e) {
return false;
}
});
// Uncurries a function; used to allow the f(x,y,z) style on exports.
const uncurry = f => (a,b,c,d,e) => {
var p;
// Hardcoded because efficiency (`arguments` is very slow).
if (typeof a !== "undefined") p = f(a);
if (typeof b !== "undefined") p = f(b);
if (typeof c !== "undefined") p = f(c);
if (typeof d !== "undefined") p = f(d);
if (typeof e !== "undefined") p = f(e);
return p;
};
// () -> Promise Bool
// Not sure how to mock Swarm to test it properly. Ideas?
const test = () => Promise.resolve(true);
// Uint8Array -> String
const toString = uint8Array =>
bytes.toString(bytes.fromUint8Array(uint8Array));
// String -> Uint8Array
const fromString = string =>
bytes.toUint8Array(bytes.fromString(string));
// String -> SwarmAPI
// Fixes the `swarmUrl`, returning an API where you don't have to pass it.
const at = swarmUrl => ({
download: (hash,path) => download(swarmUrl)(hash)(path),
downloadData: uncurry(downloadData(swarmUrl)),
downloadDataToDisk: uncurry(downloadDataToDisk(swarmUrl)),
downloadDirectory: uncurry(downloadDirectory(swarmUrl)),
downloadDirectoryToDisk: uncurry(downloadDirectoryToDisk(swarmUrl)),
downloadEntries: uncurry(downloadEntries(swarmUrl)),
downloadRoutes: uncurry(downloadRoutes(swarmUrl)),
isAvailable: () => isAvailable(swarmUrl),
upload: (arg) => upload(swarmUrl)(arg),
uploadData: uncurry(uploadData(swarmUrl)),
uploadFile: uncurry(uploadFile(swarmUrl)),
uploadFileFromDisk: uncurry(uploadFile(swarmUrl)),
uploadDataFromDisk: uncurry(uploadDataFromDisk(swarmUrl)),
uploadDirectory: uncurry(uploadDirectory(swarmUrl)),
uploadDirectoryFromDisk: uncurry(uploadDirectoryFromDisk(swarmUrl)),
uploadToManifest: uncurry(uploadToManifest(swarmUrl)),
pick: pick,
hash: hash,
fromString: fromString,
toString: toString
});
return {
at,
local,
download,
downloadBinary,
downloadData,
downloadDataToDisk,
downloadDirectory,
downloadDirectoryToDisk,
downloadEntries,
downloadRoutes,
isAvailable,
startProcess,
stopProcess,
upload,
uploadData,
uploadDataFromDisk,
uploadFile,
uploadFileFromDisk,
uploadDirectory,
uploadDirectoryFromDisk,
uploadToManifest,
pick,
hash,
fromString,
toString
};
};