mirror of
https://github.com/embarklabs/swarm-api.git
synced 2026-08-27 15:51:16 +00:00
Split module between nodejs and browser
Somehow when embark webpacks the dapp, it is still seeing the ‘fs’ object, which of course it doesn’t know about, so it is complaining about a missing module. Any help would be greatly appreciated.
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
{
|
||||
"comments": false,
|
||||
"compact": false,
|
||||
"env": {
|
||||
"browser": {
|
||||
"ignore": [
|
||||
"src/swarmjs.js",
|
||||
"src/node/index.js",
|
||||
"src/standalone/index.js",
|
||||
"bin/swarmjs"
|
||||
],
|
||||
"plugins": [
|
||||
["@babel/plugin-transform-runtime", {
|
||||
"corejs": 2
|
||||
}]
|
||||
],
|
||||
"presets": [
|
||||
["@babel/env", {
|
||||
"targets": {"node": "8.11.3"}
|
||||
}]
|
||||
]
|
||||
},
|
||||
"node": {
|
||||
"ignore": [
|
||||
"src/browser.js",
|
||||
"bin/swarmjs"
|
||||
],
|
||||
"plugins": [
|
||||
["@babel/plugin-transform-runtime", {
|
||||
"corejs": 2
|
||||
}]
|
||||
],
|
||||
"presets": [
|
||||
["@babel/env", {
|
||||
"targets": {"node": "8.11.3"}
|
||||
}]
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
+13
@@ -25,3 +25,16 @@ build/Release
|
||||
# Dependency directory
|
||||
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
|
||||
node_modules
|
||||
|
||||
dist
|
||||
package
|
||||
|
||||
.idea
|
||||
.vscode
|
||||
.eslintrc.json
|
||||
|
||||
swarm.min.js
|
||||
swarmjs-*.tgz
|
||||
NOTES
|
||||
npm-debug.log
|
||||
TODO
|
||||
|
||||
+3
-3
@@ -1,6 +1,6 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
const SwarmJS = require('./index.js');
|
||||
const SwarmJS = require('../src/index.js');
|
||||
const swarmhash = require('swarmhash');
|
||||
const fs = require('fs');
|
||||
|
||||
@@ -43,7 +43,7 @@ function abort(msg) {
|
||||
|
||||
switch (command) {
|
||||
case 'get':
|
||||
swarmjs.get(argv.hash, function (err, ret) {
|
||||
swarmjs.downloadRaw(argv.hash, function (err, ret) {
|
||||
if (err) {
|
||||
abort('Failed to download: ' + err)
|
||||
} else {
|
||||
@@ -52,7 +52,7 @@ switch (command) {
|
||||
})
|
||||
break
|
||||
case 'put':
|
||||
swarmjs.putFile(fs.readFileSync(argv.filename), function (err, ret) {
|
||||
swarmjs.uploadRaw(fs.readFileSync(argv.filename), function (err, ret) {
|
||||
if (err) {
|
||||
console.log('Failed to upload: ' + err)
|
||||
} else {
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<title>SwarmJS — standalone</title>
|
||||
<script src="swarmjs.min.js"></script>
|
||||
</head>
|
||||
<body></body>
|
||||
</html>
|
||||
+52
-22
@@ -1,12 +1,50 @@
|
||||
{
|
||||
"name": "swarmjs",
|
||||
"version": "0.0.1",
|
||||
"description": "",
|
||||
"main": "src/index.js",
|
||||
"description": "JavaScript library for easily interacting with Swarm distributed storage.",
|
||||
"main": "dist/node/index.js",
|
||||
"browser": {
|
||||
"request": "xhr",
|
||||
"./dist/node/index.js": "./dist/browser/browser.js"
|
||||
},
|
||||
"bin": {
|
||||
"swarmjs": "./bin/swarmjs"
|
||||
},
|
||||
"browserslist": [
|
||||
"last 1 version",
|
||||
"not dead",
|
||||
"> 0.2%"
|
||||
],
|
||||
"scripts": {
|
||||
"lint": "./node_modules/.bin/eslint src/",
|
||||
"babel:browser": "cross-env BABEL_ENV=browser babel --out-dir dist/browser src",
|
||||
"babel:node": "cross-env BABEL_ENV=node babel --out-dir dist src",
|
||||
"build": "npm run clean && npm run babel && npm run webpack",
|
||||
"clean": "rimraf dist swarmjs-*.tgz package",
|
||||
"http-server": "http-server",
|
||||
"prepare": "npm run build",
|
||||
"test": "echo \"Error: no test specified\" && exit 1",
|
||||
"webpack": "webpack"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/embark-framework/swarmjs.git"
|
||||
},
|
||||
"author": "",
|
||||
"license": "MIT",
|
||||
"keywords": [
|
||||
"ethereum",
|
||||
"dapps",
|
||||
"swarm",
|
||||
"blockchain",
|
||||
"serverless"
|
||||
],
|
||||
"bugs": {
|
||||
"url": "https://github.com/embark-framework/swarmjs/issues"
|
||||
},
|
||||
"homepage": "https://github.com/embark-framework/swarmjs#readme",
|
||||
"dependencies": {
|
||||
"@babel/runtime-corejs2": "7.0.0-rc.1",
|
||||
"klaw": "^3.0.0",
|
||||
"request": "^2.87.0",
|
||||
"swarmhash": "^0.1.0",
|
||||
@@ -15,27 +53,19 @@
|
||||
"yargs": "^12.0.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@babel/cli": "7.0.0-rc.1",
|
||||
"@babel/core": "7.0.0-rc.1",
|
||||
"@babel/plugin-transform-runtime": "7.0.0-rc.1",
|
||||
"@babel/preset-env": "7.0.0-rc.1",
|
||||
"ajv": "6.5.2",
|
||||
"cross-env": "5.2.0",
|
||||
"http-server": "0.11.1",
|
||||
"rimraf": "2.6.2",
|
||||
"webpack": "4.16.1",
|
||||
"webpack-cli": "3.0.8",
|
||||
"eslint": "^4.13.1"
|
||||
},
|
||||
"scripts": {
|
||||
"lint": "./node_modules/.bin/eslint src/",
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/embark-framework/swarmjs.git"
|
||||
},
|
||||
"keywords": [
|
||||
"ethereum",
|
||||
"swarm"
|
||||
],
|
||||
"author": "",
|
||||
"license": "MIT",
|
||||
"bugs": {
|
||||
"url": "https://github.com/embark-framework/swarmjs/issues"
|
||||
},
|
||||
"homepage": "https://github.com/embark-framework/swarmjs#readme",
|
||||
"browser": {
|
||||
"request": "xhr"
|
||||
"engines": {
|
||||
"node": ">=8.11.3"
|
||||
}
|
||||
}
|
||||
|
||||
+126
@@ -0,0 +1,126 @@
|
||||
const request = require('request');
|
||||
|
||||
//class SwarmJS {
|
||||
|
||||
// constructor(opts) {
|
||||
// this.opts = opts || {};
|
||||
// if (this.opts.gateway) {
|
||||
// this.gateway = opts.gateway
|
||||
// } else if (this.opts.mode === 'http') {
|
||||
// this.gateway = 'http://swarm-gateways.net'
|
||||
// } else {
|
||||
// this.gateway = 'https://swarm-gateways.net'
|
||||
// }
|
||||
// }
|
||||
|
||||
function SwarmJS(opts){
|
||||
this.opts = opts || {};
|
||||
if (this.opts.gateway) {
|
||||
this.gateway = opts.gateway
|
||||
} else if (this.opts.mode === 'http') {
|
||||
this.gateway = 'http://swarm-gateways.net'
|
||||
} else {
|
||||
this.gateway = 'https://swarm-gateways.net'
|
||||
}
|
||||
}
|
||||
|
||||
SwarmJS.prototype._isValidHash = (hash) => {
|
||||
return /^[0-9a-f]{64}$/.test(hash)
|
||||
}
|
||||
|
||||
SwarmJS.prototype._hashResponse = (error, response, body, cb) => {
|
||||
if (error) {
|
||||
cb(error)
|
||||
} else if (response.statusCode !== 200) {
|
||||
cb(body)
|
||||
} else if (!this._isValidHash(body)) {
|
||||
cb('Invalid hash')
|
||||
} else {
|
||||
cb(null, body)
|
||||
}
|
||||
}
|
||||
|
||||
SwarmJS.prototype.download = (url, cb) => {
|
||||
request(`${this.gateway}/${url}`, (error, response, body) => {
|
||||
if (error) {
|
||||
cb(error)
|
||||
} else if (response.statusCode !== 200) {
|
||||
cb(body)
|
||||
} else {
|
||||
cb(null, body)
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
SwarmJS.prototype.downloadRaw = (hash, cb) => {
|
||||
this.download(`bzz-raw:/${hash}`, cb);
|
||||
}
|
||||
|
||||
SwarmJS.prototype.upload = (url, content, cb) => {
|
||||
request.post({
|
||||
url: `${this.gateway}/${url}`,
|
||||
body: content
|
||||
}, (error, response, body) => this._hashResponse(error, response, body, cb))
|
||||
}
|
||||
|
||||
SwarmJS.prototype.uploadRaw = (content, cb) => {
|
||||
this.upload('bzz-raw:', content, cb);
|
||||
}
|
||||
|
||||
SwarmJS.prototype.uploadForm = (formData, cb) => {
|
||||
request.post({
|
||||
url: `${this.gateway}/bzz:/`,
|
||||
formData: formData
|
||||
}, (error, response, body) => this._hashResponse(error, response, body, cb));
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
SwarmJS.prototype.isAvailable = (cb) => {
|
||||
const testContent = "test";
|
||||
const testHash = "6de1faa7d29b1931b4ba3d44befcf7a5e43e947cd0bf2db154172bac5ecac3a6";
|
||||
try {
|
||||
this.uploadRaw(testContent, (err, hash) => {
|
||||
if (err) return cb(err);
|
||||
cb(null, hash === testHash);
|
||||
});
|
||||
} catch (e) {
|
||||
cb(e);
|
||||
}
|
||||
}
|
||||
//}
|
||||
|
||||
module.exports = SwarmJS;
|
||||
|
||||
// (opts) => {
|
||||
// opts = opts || {};
|
||||
// if (!opts.gateway) {
|
||||
// if (opts.mode === 'http') {
|
||||
// opts.gateway = 'http://swarm-gateways.net';
|
||||
// } else {
|
||||
// opts.gateway = 'https://swarm-gateways.net';
|
||||
// }
|
||||
// }
|
||||
// //const swarmjs = new SwarmJS(opts);
|
||||
// // return {
|
||||
// // download: swarmjs.download,
|
||||
// // downloadRaw: swarmjs.downloadRaw,
|
||||
// // upload: swarmjs.upload,
|
||||
// // uploadRaw: swarmjs.uploadRaw,
|
||||
// // uploadForm: swarmjs.uploadForm,
|
||||
// // gateway: swarmjs.gateway,
|
||||
// // isAvailable: swarmjs.isAvailable,
|
||||
// // _hashResponse: swarmjs._hashResponse
|
||||
// // }
|
||||
// return {
|
||||
// download: download,
|
||||
// downloadRaw: downloadRaw,
|
||||
// upload: upload,
|
||||
// uploadRaw: uploadRaw,
|
||||
// uploadForm: uploadForm,
|
||||
// gateway: opts.gateway,
|
||||
// isAvailable: isAvailable,
|
||||
// _hashResponse: _hashResponse
|
||||
// }
|
||||
// }
|
||||
@@ -0,0 +1 @@
|
||||
module.exports = require('../swarmjs').default;
|
||||
@@ -0,0 +1 @@
|
||||
module.exports = require('../../smarmjs.min');
|
||||
@@ -0,0 +1 @@
|
||||
export {default} from './index';
|
||||
Vendored
+1
File diff suppressed because one or more lines are too long
@@ -0,0 +1,26 @@
|
||||
const path = require('path');
|
||||
|
||||
const standalone = {
|
||||
entry: path.join(__dirname, 'dist/browser', 'browser.js'),
|
||||
mode: 'production',
|
||||
// optimization: {
|
||||
// minimize: false
|
||||
// },
|
||||
output: {
|
||||
filename: 'swarmjs.min.js',
|
||||
globalObject: 'typeof self !== \'undefined\' ? self : this',
|
||||
library: 'SwarmJS',
|
||||
libraryTarget: 'umd',
|
||||
libraryExport: 'default',
|
||||
path: __dirname,
|
||||
umdNamedDefine: true,
|
||||
},
|
||||
target: 'web',
|
||||
node: {
|
||||
fs: 'empty'
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = [
|
||||
standalone
|
||||
];
|
||||
Reference in New Issue
Block a user