Comparing version 0.1.6 to 0.1.7
@@ -8,4 +8,6 @@ // Uploads a DApp from a local directory to the Swarm network. | ||
swarm.upload(path.join(__dirname,"example_dapp_uploader"), "/index.html") | ||
swarm.upload({ | ||
path: path.join(__dirname,"example_dapp_uploader"), | ||
kind: "directory"}) // could be "file", "data" or "directory" | ||
.then(console.log) | ||
.catch(console.log); |
{ | ||
"name": "swarm-js", | ||
"version": "0.1.6", | ||
"version": "0.1.7", | ||
"description": "Swarm tools for JavaScript.", | ||
@@ -5,0 +5,0 @@ "main": "src/swarm.js", |
@@ -45,3 +45,3 @@ ## Swarm.js | ||
#### Downlod raw data | ||
#### Download raw data | ||
@@ -85,15 +85,17 @@ To download raw data, just call `swarm.download(hash)`. It returns a promise with the data buffer. | ||
#### Download a file/directory to disk (on Node.js) | ||
```javascript | ||
swarm.download(dappHash, "/target/dir") | ||
.then(dirPath => console.log(`Downloaded DApp to ${dirPath}.`)) | ||
swarm.download("DAPP_HASH", "/target/dir") | ||
.then(path => console.log(`Downloaded DApp to ${path}.`)) | ||
.catch(console.log); | ||
``` | ||
#### Upload a file/directory from disk (on Node.js) | ||
#### Upload raw data, a file or a directory from disk (on Node.js) | ||
```javascript | ||
swarm.upload("/path/to/file/or/dir", "/optional_default_file.xyz") | ||
swarm.upload({ | ||
path: "/path/to/thing", // path to data / file / directory | ||
kind: "directory", // could also be "file" or "data" | ||
defaultFile: "/index.html"}) // optional, and only for kind === "directory" | ||
.then(console.log) | ||
@@ -103,6 +105,6 @@ .catch(console.log); | ||
#### Upload a file/directory from disk (on Browser) | ||
#### Upload raw data, a file or a directory from disk (on Browser) | ||
```javascript | ||
swarm.upload(isDapp) // uploads file if isDapp === false, otherwise uploads directory | ||
swarm.upload({pick: "file"}) // could also be "directory" or "data" | ||
``` | ||
@@ -109,0 +111,0 @@ |
@@ -1,2 +0,2 @@ | ||
picker = pickDirectory => () => new Promise((resolve, reject) => { | ||
picker = type => () => new Promise((resolve, reject) => { | ||
const fileLoader = e => { | ||
@@ -10,3 +10,3 @@ const directory = {}; | ||
const data = new Buffer(e.target.result); | ||
if (pickDirectory) { | ||
if (type === "directory") { | ||
const path = file.webkitRelativePath; | ||
@@ -19,2 +19,5 @@ directory[path.slice(path.indexOf("/")+1)] = { | ||
resolve(directory); | ||
} else if (type === "file") { | ||
const path = file.webkitRelativePath; | ||
resolve({"type": mimetype.lookup(path), "data": data}); | ||
} else { | ||
@@ -29,3 +32,3 @@ resolve(data); | ||
let fileInput; | ||
if (pickDirectory) { | ||
if (type === "directory") { | ||
fileInput = document.createElement("input"); | ||
@@ -51,4 +54,5 @@ fileInput.addEventListener("change", fileLoader); | ||
module.exports = { | ||
file: picker(false), | ||
directory: picker(true) | ||
data: picker("data"), | ||
file: picker("file"), | ||
directory: picker("directory"), | ||
} |
@@ -147,2 +147,11 @@ const Q = require("bluebird"); | ||
// String -> {type: String, data: Buffer} -> Promise String | ||
const uploadFile = swarmUrl => file => | ||
uploadDirectory(swarmUrl)({"": file}); | ||
// String -> String -> Promise String | ||
const uploadFileFromDisk = swarmUrl => filePath => | ||
fsp.readFile(filePath) | ||
.then(data => uploadFile(swarmUrl)({type: mimetype.lookup(filePath), data: data})); | ||
// String -> Map String File -> Promise String | ||
@@ -176,30 +185,33 @@ // Uploads a directory to Swarm. The directory is | ||
// String -> Buffer | Bool | Map String Buffer | String -> Nullable String -> Promise String | ||
// String -> UploadInfo -> Promise String | ||
// Simplified multi-type upload which calls the correct | ||
// one based on the type of the argument given. | ||
const upload = swarmUrl => pathOrContents => defaultFile => { | ||
const upload = swarmUrl => arg => { | ||
// Upload raw data from browser | ||
if (arg.pick === "data") { | ||
return pick.data().then(uploadData(swarmUrl)); | ||
// Upload a file from browser | ||
if (!pathOrContents) { | ||
return pick.file().then(uploadData(swarmUrl)); | ||
} else if (arg.pick === "file") { | ||
return pick.file().then(uploadFile(swarmUrl)); | ||
// Upload a directory from browser | ||
} else if (pathOrContents === true) { | ||
return pick.directory().then(uploadDirectory(swarmUrl)); | ||
} 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 raw data (buffer) | ||
} else if (pathOrContents.length && typeof pathOrContents !== "string") { | ||
return uploadData(swarmUrl)(pathOrContents); | ||
} else if (arg.length) { | ||
return uploadData(swarmUrl)(arg); | ||
// Upload directory with JSON | ||
} else if (pathOrContents instanceof Object) { | ||
return uploadDirectory(swarmUrl)(pathOrContents); | ||
// Upload directory/file from disk | ||
} else if (typeof pathOrContents === "string") { | ||
const path = pathOrContents; | ||
return fsp.lstat(path).then(stat => { | ||
return stat.isDirectory() | ||
? uploadDirectoryFromDisk(swarmUrl)(defaultFile)(path) | ||
: uploadDataFromDisk(swarmUrl)(path); | ||
}); | ||
} else if (arg instanceof Object) { | ||
return uploadDirectory(swarmUrl)(arg); | ||
} | ||
@@ -373,4 +385,6 @@ | ||
isAvailable: () => isAvailable(swarmUrl), | ||
upload: (pathOrContents,defaultFile) => upload(swarmUrl)(pathOrContents)(defaultFile), | ||
upload: (arg) => upload(swarmUrl)(arg), | ||
uploadData: uncurry(uploadData(swarmUrl)), | ||
uploadFile: uncurry(uploadFile(swarmUrl)), | ||
uploadFileFromDisk: uncurry(uploadFile(swarmUrl)), | ||
uploadDataFromDisk: uncurry(uploadDataFromDisk(swarmUrl)), | ||
@@ -400,2 +414,4 @@ uploadDirectory: uncurry(uploadDirectory(swarmUrl)), | ||
uploadDataFromDisk, | ||
uploadFile, | ||
uploadFileFromDisk, | ||
uploadDirectory, | ||
@@ -402,0 +418,0 @@ uploadDirectoryFromDisk, |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
847431
4262
182