Initial import.

This commit is contained in:
Dan 2017-12-05 22:09:58 -08:00
commit 013f198b74
8 changed files with 187 additions and 0 deletions

5
.babelrc Normal file
View File

@ -0,0 +1,5 @@
{
"presets": [
"stage-0"
]
}

7
.eslintrc Normal file
View File

@ -0,0 +1,7 @@
{
"extends": "standard",
"rules": {
},
"plugins": [
]
}

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
node_modules
npm-debug.log
dist

31
index.html Normal file
View File

@ -0,0 +1,31 @@
<html>
<head>
<title>Sample App</title>
</head>
<body>
<script>
// From https://github.com/LZMA-JS/LZMA-JS (lzma-d.js)
/*var e=function(){"use ...*/
// Data portion made with: webpack && cat dist/bundle.js | lzma | base64 -w 0
// IDEA: Load this from a CDN (unpkg.com?), and only resort to the local copy if the CDN copy doesn't load.
var compressed_javascript = "data:application/octet-stream;base64,XQAAgAD..."
document.body.insertAdjacentHTML('beforeend', new Date() + 'start<br />');
fetch(compressed_javascript).then(response => {
document.body.insertAdjacentHTML('beforeend', new Date() + 'decompressed<br />');
compressed_javascript = null
return response.arrayBuffer()
}).then(buffer => {
LZMA.decompress(new Uint8Array(buffer), function(result, error) {
document.body.insertAdjacentHTML('beforeend', new Date() + 'starting eval<br />');
buffer = null
eval(result)
})
})
// For conversion of this file to data URI, run: cat index.html | python -c "import urllib, sys; print urllib.quote(sys.stdin.read())"
// Then prepend with: "data:text/html;charset=utf-8,"
</script>
</body>
</html>

22
package.json Normal file
View File

@ -0,0 +1,22 @@
{
"name": "bundle-webpack",
"version": "1.0.0",
"description": "Bundle js-ipfs with WebPack",
"scripts": {
"start": "node server.js"
},
"license": "MIT",
"keywords": [],
"devDependencies": {
"babel-core": "^6.24.1",
"babel-loader": "^7.0.0",
"babel-preset-stage-0": "^6.24.1",
"json-loader": "^0.5.4",
"uglifyjs-webpack-plugin": "^1.1.2",
"webpack": "^2.5.1",
"webpack-dev-server": "^2.4.5"
},
"dependencies": {
"ipfs": "file:../../"
}
}

19
server.js Normal file
View File

@ -0,0 +1,19 @@
'use strict'
const webpack = require('webpack')
const WebpackDevServer = require('webpack-dev-server')
const config = require('./webpack.config')
const wds = new WebpackDevServer(webpack(config), {
publicPath: config.output.publicPath,
hot: true,
historyApiFallback: true
})
wds.listen(3000, 'localhost', (err, result) => {
if (err) {
throw err
}
console.log('Listening at localhost:3000')
})

68
src/index.js Normal file
View File

@ -0,0 +1,68 @@
'use strict'
const IPFS = require('ipfs')
const Repo = require('ipfs-repo')
const LevelStore = require('datastore-level')
function MemoryLevelStore() {
return new LevelStore('/memory', {db: require('memdown')})
}
MemoryLevelStore.prototype = new LevelStore('/memory', {db: require('memdown')})
MemoryLevelStore.prototype.constructor = MemoryLevelStore
const stringToUse = 'Hello world from self-contained, self-hosted js-ipfs!!!'
function App() {
let node
create()
function create () {
// Create the IPFS node instance
var repo_name = String(Math.random() + Date.now())
var storage_backends = {
root: MemoryLevelStore,
blocks: MemoryLevelStore,
datastore: MemoryLevelStore,
}
node = new IPFS({ repo: new Repo(repo_name, {storageBackends: storage_backends}) })
node.once('ready', () => {
console.log('IPFS node is ready')
ops()
})
}
function ops () {
node.id((err, res) => {
if (err) {
throw err
}
document.body.insertAdjacentHTML('beforeend',
"<h1>Everything is working!</h1>" +
"<p>Your ID is <strong>" + res.id + "</strong></p>" +
"<p>Your IPFS version is <strong>" + res.agentVersion + "</strong></p>" +
"<p>Your IPFS protocol version is <strong>" + res.protocolVersion + "</strong></p>"
)
})
node.files.add([Buffer.from(stringToUse)], (err, filesAdded) => {
if (err) { console.log("ERROR"); throw err }
const hash = filesAdded[0].hash
document.body.insertAdjacentHTML('beforeend', "<hr /><div>Added a file!<br /><a href=\"https://gateway.ipfs.io/ipfs/" + hash + "\">" + hash + "</a></div>")
node.files.cat(hash, (err, data) => {
if (err) { console.log("ERROR 2"); throw err }
document.body.insertAdjacentHTML('beforeend', "<p>Contents of this file: <br />" + data + "</p>")
})
})
}
}
module.exports = App
App()

32
webpack.config.js Normal file
View File

@ -0,0 +1,32 @@
'use strict'
var path = require('path')
var webpack = require('webpack')
const UglifyJSPlugin = require('uglifyjs-webpack-plugin');
module.exports = {
devtool: 'eval',
entry: [
'./src/index'
],
output: {
path: path.join(__dirname, 'dist'),
filename: 'bundle.js',
publicPath: '/static/'
},
plugins: [
new UglifyJSPlugin()
],
module: {
loaders: [{
test: /\.js$/,
// loaders: ['babel-loader'],
include: path.join(__dirname, 'src')
}, /*{ test: /\.json$/, loader: 'json-loader' }*/]
},
node: {
fs: 'empty',
net: 'empty',
tls: 'empty'
}
}