Skip to content

Commit

Permalink
Updating to swarm 1.6.7 api
Browse files Browse the repository at this point in the history
  • Loading branch information
evertonfraga committed Jul 13, 2017
1 parent 74d7dfb commit c386a55
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 187 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ const config = {
dataDir: process.env.HOME + "/Library/Ethereum/swarm-js",

// Path where geth.ipc is (that file is created when you start geth)
ethApi: process.env.HOME + "/Library/Ethereum/geth.ipc",
ensApi: process.env.HOME + "/Library/Ethereum/geth.ipc",

// Path where the swarm binary is (if there is nothing on this path, swarm-js
// will safely download and place it there). Add `.exe` to the end if you're
Expand Down
159 changes: 0 additions & 159 deletions examples/example_dapp_uploader/swarm.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion examples/run_node.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ fs.writeFileSync(privateKeyPath, "0123456789abcdef0123456789abcdef0123456789abcd
const config = {
privateKey: privateKeyPath,
dataDir: process.env.HOME + "/Library/Ethereum/testnet",
ethApi: process.env.HOME + "/Library/Ethereum/testnet/geth.ipc",
ensApi: process.env.HOME + "/Library/Ethereum/testnet/geth.ipc",
binPath: process.env.HOME + "/.swarm/swarm"
};

Expand Down
33 changes: 19 additions & 14 deletions lib/swarm.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ var mimetype = require('mimetype');
var pick = require("./pick.js");
var request = require("xhr-request-promise");
var downloadUrl = "http://ethereum-mist.s3.amazonaws.com/swarm/";
var archives = require("./../archives/archives.json");
var defaultArchives = require("./../archives/archives.json");

// ∀ a . String -> JSON -> Map String a -o Map String a
// Inserts a key/val pair in an object impurely.
Expand Down Expand Up @@ -46,7 +46,7 @@ var rawUrl = function rawUrl(swarmUrl) {
};

// String -> String -> Promise Buffer
// Gets the raw contents of a Swarm hash address.
// 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) {
Expand All @@ -73,7 +73,7 @@ var downloadEntries = function downloadEntries(swarmUrl) {
hash: entry.hash };
};

// To download a single entry:
// 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) {
Expand Down Expand Up @@ -139,7 +139,7 @@ var downloadDirectory = function downloadDirectory(swarmUrl) {
};

// String -> String -> String -> Promise String
// Gets the raw contents of a Swarm hash address.
// 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) {
Expand Down Expand Up @@ -172,7 +172,7 @@ var downloadDirectoryToDisk = function downloadDirectoryToDisk(swarmUrl) {
};

// String -> Buffer -> Promise String
// Uploads raw data to Swarm.
// Uploads raw data to Swarm.
// Returns a promise with the uploaded hash.
var uploadData = function uploadData(swarmUrl) {
return function (data) {
Expand Down Expand Up @@ -337,10 +337,10 @@ var _download = function _download(swarmUrl) {
// 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) {
var downloadBinary = function downloadBinary(path, archives) {
var os = require("o" + "s");
var system = os.platform().replace("win32", "windows") + "-" + (os.arch() === "x64" ? "amd64" : "386");
var archive = archives[system];
var archive = (archives || defaultArchives)[system];
var archiveUrl = downloadUrl + archive.archive + ".tar.gz";
var archiveMD5 = archive.archiveMD5;
var binaryMD5 = archive.binaryMD5;
Expand All @@ -352,8 +352,13 @@ var downloadBinary = function downloadBinary(path) {
// password : String,
// dataDir : String,
// binPath : String,
// ethApi : String,
// onDownloadProgress : Number ~> ()
// ensApi : String,
// onDownloadProgress : Number ~> (),
// archives : [{
// archive: String,
// binaryMD5: String,
// archiveMD5: String
// }]
// }

// SwarmSetup ~> Promise Process
Expand All @@ -371,7 +376,7 @@ var startProcess = function startProcess(swarmSetup) {
var account = swarmSetup.account,
password = swarmSetup.password,
dataDir = swarmSetup.dataDir,
ethApi = swarmSetup.ethApi,
ensApi = swarmSetup.ensApi,
privateKey = swarmSetup.privateKey;


Expand All @@ -384,7 +389,7 @@ var startProcess = function startProcess(swarmSetup) {

var state = WAITING_PASSWORD;

var swarmProcess = spawn(swarmSetup.binPath, ['--bzzaccount', account || privateKey, '--datadir', dataDir, '--ethapi', ethApi]);
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)) {
Expand All @@ -400,7 +405,7 @@ var startProcess = function startProcess(swarmSetup) {
};

swarmProcess.stdout.on('data', handleProcessOutput);
swarmProcess.stderr.on('data', (data) => reject(new Error(data.toString())));
swarmProcess.stderr.on('data', handleProcessOutput);
//swarmProcess.on('close', () => setTimeout(restart, 2000));

var restart = function restart() {
Expand Down Expand Up @@ -448,7 +453,7 @@ var stopProcess = function stopProcess(process) {
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).onData(function (data) {
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);
Expand Down Expand Up @@ -572,4 +577,4 @@ if (typeof window !== "undefined") {
};
loadLibs();
setTimeout(loadLibs, 0);
};
};
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
},
"author": "MaiaVictor",
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/maiavictor/swarm-js"
},
"dependencies": {
"buffer": "^5.0.5",
"decompress": "^4.0.0",
Expand Down
24 changes: 12 additions & 12 deletions src/swarm.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const impureInsert = 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 = {};
let map = {};
for (let i = 0, l = keys.length; i < l; ++i)
map[keys[i]] = vals[i];
return map;
Expand All @@ -34,7 +34,7 @@ const rawUrl = swarmUrl => hash =>
`${swarmUrl}/bzzr:/${hash}`

// String -> String -> Promise Buffer
// Gets the raw contents of a Swarm hash address.
// Gets the raw contents of a Swarm hash address.
const downloadData = swarmUrl => hash =>
request(rawUrl(swarmUrl)(hash), {responseType: "arraybuffer"})
.then(arrayBuffer => new Buffer(arrayBuffer));
Expand All @@ -52,7 +52,7 @@ const downloadEntries = swarmUrl => hash => {
type: entry.contentType,
hash: entry.hash});

// To download a single entry:
// To download a single entry:
// if type is bzz-manifest, go deeper
// if not, add it to the routing table
const downloadEntry = entry => {
Expand Down Expand Up @@ -86,7 +86,7 @@ const downloadRoutes = swarmUrl => 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 =>
const downloadDirectory = swarmUrl => hash =>
downloadEntries (swarmUrl) (hash)
.then(entries => {
const paths = Object.keys(entries);
Expand All @@ -98,7 +98,7 @@ const downloadDirectory = swarmUrl => hash =>
});

// String -> String -> String -> Promise String
// Gets the raw contents of a Swarm hash address.
// Gets the raw contents of a Swarm hash address.
// Returns a promise with the downloaded file path.
const downloadDataToDisk = swarmUrl => hash => filePath =>
require("."+"/files.js").download (rawUrl(swarmUrl)(hash)) (filePath);
Expand All @@ -120,7 +120,7 @@ const downloadDirectoryToDisk = swarmUrl => hash => dirPath =>
});

// String -> Buffer -> Promise String
// Uploads raw data to Swarm.
// Uploads raw data to Swarm.
// Returns a promise with the uploaded hash.
const uploadData = swarmUrl => data =>
request(`${swarmUrl}/bzzr:/`, {body: data, method: "POST"});
Expand Down Expand Up @@ -166,7 +166,7 @@ const uploadDirectory = swarmUrl => directory =>
});

// String -> Promise String
const uploadDataFromDisk = swarmUrl => filePath =>
const uploadDataFromDisk = swarmUrl => filePath =>
require("f"+"s-promise").readFile(filePath)
.then(uploadData(swarmUrl));

Expand Down Expand Up @@ -253,7 +253,7 @@ const downloadBinary = (path, archives) => {
// password : String,
// dataDir : String,
// binPath : String,
// ethApi : String,
// ensApi : String,
// onDownloadProgress : Number ~> (),
// archives : [{
// archive: String,
Expand All @@ -268,21 +268,21 @@ const startProcess = swarmSetup => new Promise((resolve, reject) => {
const {spawn} = require("c"+"hild_process");

const hasString = str => buffer => ('' + buffer).indexOf(str) !== -1;
const {account, password, dataDir, ethApi, privateKey} = swarmSetup;
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,
'--ethapi', ethApi]);
'--ens-api', ensApi]);

const handleProcessOutput = data => {
if (state === WAITING_PASSWORD && hasString (PASSWORD_PROMPT_HOOK) (data)) {
Expand Down Expand Up @@ -345,7 +345,7 @@ const local = swarmSetup => useAPI =>
.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.
Expand Down

0 comments on commit c386a55

Please sign in to comment.