Browse Source

Initial import.

master
Dan 1 year ago
commit
013f198b74
8 changed files with 187 additions and 0 deletions
  1. 5
    0
      .babelrc
  2. 7
    0
      .eslintrc
  3. 3
    0
      .gitignore
  4. 31
    0
      index.html
  5. 22
    0
      package.json
  6. 19
    0
      server.js
  7. 68
    0
      src/index.js
  8. 32
    0
      webpack.config.js

+ 5
- 0
.babelrc View File

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

+ 7
- 0
.eslintrc View File

@@ -0,0 +1,7 @@
1
+{
2
+  "extends": "standard",
3
+  "rules": {
4
+  },
5
+  "plugins": [
6
+  ]
7
+}

+ 3
- 0
.gitignore View File

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

+ 31
- 0
index.html View File

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

+ 22
- 0
package.json View File

@@ -0,0 +1,22 @@
1
+{
2
+  "name": "bundle-webpack",
3
+  "version": "1.0.0",
4
+  "description": "Bundle js-ipfs with WebPack",
5
+  "scripts": {
6
+    "start": "node server.js"
7
+  },
8
+  "license": "MIT",
9
+  "keywords": [],
10
+  "devDependencies": {
11
+    "babel-core": "^6.24.1",
12
+    "babel-loader": "^7.0.0",
13
+    "babel-preset-stage-0": "^6.24.1",
14
+    "json-loader": "^0.5.4",
15
+    "uglifyjs-webpack-plugin": "^1.1.2",
16
+    "webpack": "^2.5.1",
17
+    "webpack-dev-server": "^2.4.5"
18
+  },
19
+  "dependencies": {
20
+    "ipfs": "file:../../"
21
+  }
22
+}

+ 19
- 0
server.js View File

@@ -0,0 +1,19 @@
1
+'use strict'
2
+
3
+const webpack = require('webpack')
4
+const WebpackDevServer = require('webpack-dev-server')
5
+const config = require('./webpack.config')
6
+
7
+const wds = new WebpackDevServer(webpack(config), {
8
+  publicPath: config.output.publicPath,
9
+  hot: true,
10
+  historyApiFallback: true
11
+})
12
+
13
+wds.listen(3000, 'localhost', (err, result) => {
14
+  if (err) {
15
+    throw err
16
+  }
17
+
18
+  console.log('Listening at localhost:3000')
19
+})

+ 68
- 0
src/index.js View File

@@ -0,0 +1,68 @@
1
+'use strict'
2
+
3
+const IPFS = require('ipfs')
4
+const Repo = require('ipfs-repo')
5
+const LevelStore = require('datastore-level')
6
+
7
+function MemoryLevelStore() {
8
+    return new LevelStore('/memory', {db: require('memdown')})
9
+}
10
+MemoryLevelStore.prototype = new LevelStore('/memory', {db: require('memdown')})
11
+MemoryLevelStore.prototype.constructor = MemoryLevelStore
12
+
13
+const stringToUse = 'Hello world from self-contained, self-hosted js-ipfs!!!'
14
+
15
+function App() {
16
+    let node
17
+
18
+    create()
19
+
20
+    function create () {
21
+      // Create the IPFS node instance
22
+
23
+      var repo_name = String(Math.random() + Date.now())
24
+      var storage_backends = {
25
+        root: MemoryLevelStore,
26
+        blocks: MemoryLevelStore,
27
+        datastore: MemoryLevelStore,
28
+      }
29
+
30
+      node = new IPFS({ repo: new Repo(repo_name, {storageBackends: storage_backends}) })
31
+
32
+      node.once('ready', () => {
33
+        console.log('IPFS node is ready')
34
+        ops()
35
+      })
36
+    }
37
+
38
+    function ops () {
39
+      node.id((err, res) => {
40
+          if (err) {
41
+              throw err
42
+          }
43
+
44
+          document.body.insertAdjacentHTML('beforeend',
45
+              "<h1>Everything is working!</h1>" +
46
+              "<p>Your ID is <strong>" + res.id + "</strong></p>" +
47
+              "<p>Your IPFS version is <strong>" + res.agentVersion + "</strong></p>" +
48
+              "<p>Your IPFS protocol version is <strong>" + res.protocolVersion + "</strong></p>"
49
+          )
50
+      })
51
+
52
+      node.files.add([Buffer.from(stringToUse)], (err, filesAdded) => {
53
+        if (err) { console.log("ERROR"); throw err }
54
+
55
+        const hash = filesAdded[0].hash
56
+        document.body.insertAdjacentHTML('beforeend', "<hr /><div>Added a file!<br /><a href=\"https://gateway.ipfs.io/ipfs/" + hash + "\">" + hash + "</a></div>")
57
+
58
+        node.files.cat(hash, (err, data) => {
59
+          if (err) { console.log("ERROR 2"); throw err }
60
+          document.body.insertAdjacentHTML('beforeend', "<p>Contents of this file: <br />" + data + "</p>")
61
+        })
62
+      })
63
+    }
64
+}
65
+
66
+module.exports = App
67
+
68
+App()

+ 32
- 0
webpack.config.js View File

@@ -0,0 +1,32 @@
1
+'use strict'
2
+
3
+var path = require('path')
4
+var webpack = require('webpack')
5
+const UglifyJSPlugin = require('uglifyjs-webpack-plugin');
6
+
7
+module.exports = {
8
+  devtool: 'eval',
9
+  entry: [
10
+    './src/index'
11
+  ],
12
+  output: {
13
+    path: path.join(__dirname, 'dist'),
14
+    filename: 'bundle.js',
15
+    publicPath: '/static/'
16
+  },
17
+  plugins: [
18
+    new UglifyJSPlugin()
19
+  ],
20
+  module: {
21
+    loaders: [{
22
+      test: /\.js$/,
23
+//      loaders: ['babel-loader'],
24
+      include: path.join(__dirname, 'src')
25
+    }, /*{ test: /\.json$/, loader: 'json-loader' }*/]
26
+  },
27
+  node: {
28
+    fs: 'empty',
29
+    net: 'empty',
30
+    tls: 'empty'
31
+  }
32
+}

Loading…
Cancel
Save