From 6e316502c6dae8d2933e5d155946e93a90c3d834 Mon Sep 17 00:00:00 2001 From: haad Date: Fri, 25 Nov 2016 16:02:49 +0100 Subject: [PATCH] Fix browser examples --- conf/webpack.example.config.js | 69 +++++++++++++++++----------------- examples/benchmark.js | 16 ++++---- examples/browser/index.js | 4 +- examples/keyvalue.js | 57 +++++++++++++++------------- examples/start-daemon.js | 14 ------- 5 files changed, 75 insertions(+), 85 deletions(-) delete mode 100644 examples/start-daemon.js diff --git a/conf/webpack.example.config.js b/conf/webpack.example.config.js index 48c1032..e279d55 100644 --- a/conf/webpack.example.config.js +++ b/conf/webpack.example.config.js @@ -13,45 +13,44 @@ module.exports = { Buffer: true }, plugins: [ - // new webpack.optimize.UglifyJsPlugin({ - // mangle: false, - // compress: { warnings: false } - // }) + new webpack.optimize.UglifyJsPlugin({ + mangle: false, + compress: { warnings: false } + }) ], resolve: { modules: [ - path.join(__dirname, '../node_modules') - ] + 'node_modules', + path.resolve(__dirname, '../node_modules') + ], + alias: { + // These are needed because node-libs-browser depends on outdated + // versions + // + // Can be dropped once https://github.com/devongovett/browserify-zlib/pull/18 + // is shipped + zlib: 'browserify-zlib', + // Can be dropped once https://github.com/webpack/node-libs-browser/pull/41 + // is shipped + http: 'stream-http' + } + }, + resolveLoader: { + modules: [ + 'node_modules', + path.resolve(__dirname, '../node_modules') + ], + moduleExtensions: ['-loader'] }, module: { - loaders: [ - { - test: /\.js$/, - exclude: /node_modules/, - loader: 'babel-loader', - query: { - presets: require.resolve('babel-preset-es2015'), - plugins: require.resolve('babel-plugin-transform-runtime') - } - }, - { - test: /\.js$/, - include: /node_modules\/(hoek|qs|wreck|boom|ipfs.+|orbit.+|logplease|crdts|promisify-es|whatwg-fetch|node-fetch|isomorphic-fetch|db\.js)/, - loader: 'babel-loader', - query: { - presets: require.resolve('babel-preset-es2015'), - plugins: require.resolve('babel-plugin-transform-runtime') - } - }, - { - test: /\.json$/, - loader: 'json-loader' - } - ] + rules: [{ + test: /\.json$/, + loader: 'json-loader' + }] }, - externals: { - net: '{}', - tls: '{}', - 'require-dir': '{}' - } + node: { + Buffer: true + }, + plugins: [], + target: 'web' } diff --git a/examples/benchmark.js b/examples/benchmark.js index 3453130..2c8a9a0 100644 --- a/examples/benchmark.js +++ b/examples/benchmark.js @@ -11,16 +11,18 @@ let lastTenSeconds = 0 // Main loop const queryLoop = (db) => { - db.add(totalQueries).then(() => { - totalQueries ++ - lastTenSeconds ++ - queriesPerSecond ++ - process.nextTick(() => queryLoop(db)) - }) + db.add(totalQueries) + .then(() => { + totalQueries ++ + lastTenSeconds ++ + queriesPerSecond ++ + process.nextTick(() => queryLoop(db)) + }) + .catch((e) => console.error(e)) } // Start -console.log("Starting...") +console.log("Starting IPFS daemon...") const ipfs = new IpfsDaemon({ IpfsDataDir: '/tmp/orbit-db-benchmark' }) diff --git a/examples/browser/index.js b/examples/browser/index.js index 4c35543..f5fb4a4 100644 --- a/examples/browser/index.js +++ b/examples/browser/index.js @@ -1,6 +1,6 @@ 'use strict' -const IpfsApi = require('exports-loader?HaadIpfsApi!@haad/ipfs-api/dist/index.js') +const IpfsApi = require('@haad/ipfs-api') const OrbitDB = require('../../src/OrbitDB') const username = new Date().getTime() @@ -9,7 +9,7 @@ const key = 'greeting' try { const elm = document.getElementById("result") - const ipfs = IpfsApi('localhost', '5001') + const ipfs = new IpfsApi('localhost', '5001') const orbit = new OrbitDB(ipfs, username) const db = orbit.kvstore(channel) diff --git a/examples/keyvalue.js b/examples/keyvalue.js index 8eccf8b..953980b 100644 --- a/examples/keyvalue.js +++ b/examples/keyvalue.js @@ -4,6 +4,7 @@ const IpfsDaemon = require('ipfs-daemon') const OrbitDB = require('../src/OrbitDB') const userId = Math.floor(Math.random() * 1000) + const conf = { IpfsDataDir: '/tmp/' + userId, Addresses: { @@ -15,33 +16,35 @@ const conf = { console.log("Starting...") -IpfsDaemon(conf) - .then((res) => { - const orbitdb = new OrbitDB(res.ipfs) - const db = orbitdb.kvstore("|orbit-db|examples|kvstore-example") +const ipfs = new IpfsDaemon(conf) - const creatures = ['🐙', '🐬', '🐋', '🐠', '🐡', '🦀', '🐢', '🐟', '🐳'] +ipfs.on('error', (err) => console.error(err)) - const query = () => { - const index = Math.floor(Math.random() * creatures.length) - db.put(userId, { avatar: creatures[index], updated: new Date().getTime() }) - .then(() => { - const user = db.get(userId) - let output = `\n` - output += `----------------------\n` - output += `User\n` - output += `----------------------\n` - output += `Id: ${userId}\n` - output += `Avatar: ${user.avatar}\n` - output += `Updated: ${user.updated}\n` - output += `----------------------` - console.log(output) - }) - .catch((e) => { - console.error(e.stack) - }) - } +ipfs.on('ready', () => { + const orbitdb = new OrbitDB(ipfs, userId) + const db = orbitdb.kvstore("|orbit-db|examples|kvstore-example") - setInterval(query, 1000) - }) - .catch((err) => console.error(err)) + const creatures = ['🐙', '🐬', '🐋', '🐠', '🐡', '🦀', '🐢', '🐟', '🐳'] + + const query = () => { + const index = Math.floor(Math.random() * creatures.length) + db.put(userId, { avatar: creatures[index], updated: new Date().getTime() }) + .then(() => { + const user = db.get(userId) + let output = `\n` + output += `----------------------\n` + output += `User\n` + output += `----------------------\n` + output += `Id: ${userId}\n` + output += `Avatar: ${user.avatar}\n` + output += `Updated: ${user.updated}\n` + output += `----------------------` + console.log(output) + }) + .catch((e) => { + console.error(e.stack) + }) + } + + setInterval(query, 1000) +}) diff --git a/examples/start-daemon.js b/examples/start-daemon.js deleted file mode 100644 index 042a9bf..0000000 --- a/examples/start-daemon.js +++ /dev/null @@ -1,14 +0,0 @@ -const IpfsDaemon = require('ipfs-daemon') - -module.exports = IpfsDaemon({ - IpfsDataDir: '/tmp/orbit-db-examples', - API: { - HTTPHeaders: { - "Access-Control-Allow-Origin": ['*'], - "Access-Control-Allow-Methods": ["PUT", "GET", "POST"], - "Access-Control-Allow-Credentials": ["true"] - } - } -}) -.then((res) => console.log("Started IPFS daemon")) -.catch((err) => console.error(err))