From 84bfb4f8cb7d35abcd24d3de58972e4bceeac75c Mon Sep 17 00:00:00 2001 From: Boyma Fahnbulleh Date: Mon, 20 Mar 2017 08:03:52 -0700 Subject: [PATCH 01/10] lib: fix webpack import issues --- lib/env-browser.js | 311 +++++++++++++++++++++++++++++++++++++++++++++ lib/env.js | 2 +- package.json | 7 +- 3 files changed, 317 insertions(+), 3 deletions(-) create mode 100644 lib/env-browser.js diff --git a/lib/env-browser.js b/lib/env-browser.js new file mode 100644 index 00000000..8cef81cc --- /dev/null +++ b/lib/env-browser.js @@ -0,0 +1,311 @@ +/*! + * env.js - environment for bcoin + * Copyright (c) 2014-2015, Fedor Indutny (MIT License). + * Copyright (c) 2014-2017, Christopher Jeffrey (MIT License). + * https://github.com/bcoin-org/bcoin + */ + +'use strict'; + +/** + * A bcoin "environment" which exposes all + * constructors for primitives, the blockchain, + * mempool, wallet, etc. It also exposes a + * global worker pool. + * @exports Environment + * @constructor + * @property {Function} env - See {@link Environment}. + * + * @property {Function} bn - See {@url https://github.com/indutny/bn.js}. + * @property {Object} elliptic - See {@url https://github.com/indutny/elliptic}. + * + * @property {Object} bip70 - See {@link module:bip70}. + * + * @property {Object} blockchain - See {@link module:blockchain}. + * @property {Function} chain - See {@link module:blockchain.Chain}. + * @property {Function} chaindb - See {@link module:blockchain.ChainDB}. + * @property {Function} chainentry - See {@link module:blockchain.ChainEntry}. + * + * @property {Object} btc + * @property {Function} amount + * @property {Function} uri + * + * @property {Object} coins + * @property {Function} coinview + * + * @property {Object} crypto + * @property {Object} ec + * @property {Object} pk + * @property {Object} schnorr + * + * @property {Object} db + * @property {Object} ldb + * + * @property {Object} hd + * + * @property {Object} http + * @property {Object} rpc + * + * @property {Object} txmempool + * @property {Object} fees + * @property {Object} mempool + * @property {Object} mempoolentry + * + * @property {Object} mining + * @property {Object} miner + * @property {Object} minerblock + * + * @property {Object} net + * @property {Object} bip150 + * @property {Object} bip151 + * @property {Object} bip152 + * @property {Object} dns + * @property {Object} packets + * @property {Object} peer + * @property {Object} pool + * @property {Object} tcp + * + * @property {Object} node + * @property {Object} config + * @property {Object} fullnode + * @property {Object} logger + * @property {Object} spvnode + * + * @property {Object} primitives + * @property {Object} address + * @property {Object} block + * @property {Object} coin + * @property {Object} headers + * @property {Object} input + * @property {Object} invitem + * @property {Object} keyring + * @property {Object} merkleblock + * @property {Object} mtx + * @property {Object} netaddress + * @property {Object} outpoint + * @property {Object} output + * @property {Object} tx + * + * @property {Object} protocol + * @property {Object} consensus + * @property {Object} errors + * @property {Object} network + * @property {Object} networks + * @property {Object} policy + * @property {Object} timedata + * + * @property {Object} txscript + * @property {Object} opcodes + * @property {Object} program + * @property {Object} script + * @property {Object} sigcache + * @property {Object} stack + * @property {Object} witness + * + * @property {Object} utils + * @property {Object} base32 + * @property {Object} base58 + * @property {Object} bloom + * @property {Object} co + * @property {Object} encoding + * @property {Object} lock + * @property {Object} reader + * @property {Object} staticwriter + * @property {Object} util + * @property {Object} writer + * + * @property {Object} wallet + * @property {Object} path + * @property {Object} walletkey + * @property {Object} walletdb + * + * @property {Object} workers + * @property {Object} workerpool + */ + +function Environment() { + this.env = Environment; + + // BN + this.bn = require('bn.js'); + this.elliptic = require('elliptic'); + + // Horrible BIP + this.bip70 = require('./bip70'); + + // Blockchain + this.blockchain = require('./blockchain'); + this.chain = require('./blockchain/chain'); + this.chaindb = require('./blockchain/chaindb'); + this.chainentry = require('./blockchain/chainentry'); + + // BTC + this.btc = require('./btc'); + this.amount = require('./btc/amount'); + this.uri = require('./btc/uri'); + + // Coins + this.coins = require('./coins'); + this.coinview = require('./coins/coinview'); + + // Crypto + this.crypto = require('./crypto'); + this.ec = require('./crypto/ec'); + this.pk = require('./crypto/pk'); + this.schnorr = require('./crypto/schnorr'); + + // DB + this.db = require('./db'); + this.ldb = require('./db/ldb'); + + // HD + this.hd = require('./hd/hd'); + + // HTTP + this.http = require('./http'); + this.rpc = require('./http/rpc'); + + // Mempool + this.txmempool = require('./mempool'); + this.fees = require('./mempool/fees'); + this.mempool = require('./mempool/mempool'); + this.mempoolentry = require('./mempool/mempoolentry'); + + // Miner + this.mining = require('./mining'); + this.miner = require('./mining/miner'); + this.template = require('./mining/template'); + + // Net + this.net = require('./net'); + this.bip150 = require('./net/bip150'); + this.bip151 = require('./net/bip151'); + this.bip152 = require('./net/bip152'); + this.dns = require('./net/dns'); + this.packets = require('./net/packets'); + this.peer = require('./net/peer'); + this.pool = require('./net/pool'); + this.tcp = require('./net/tcp'); + + // Node + this.node = require('./node'); + this.config = require('./node/config'); + this.fullnode = require('./node/fullnode'); + this.logger = require('./node/logger'); + this.spvnode = require('./node/spvnode'); + + // Primitives + this.primitives = require('./primitives'); + this.address = require('./primitives/address'); + this.block = require('./primitives/block'); + this.coin = require('./primitives/coin'); + this.headers = require('./primitives/headers'); + this.input = require('./primitives/input'); + this.invitem = require('./primitives/invitem'); + this.keyring = require('./primitives/keyring'); + this.merkleblock = require('./primitives/merkleblock'); + this.mtx = require('./primitives/mtx'); + this.netaddress = require('./primitives/netaddress'); + this.outpoint = require('./primitives/outpoint'); + this.output = require('./primitives/output'); + this.tx = require('./primitives/tx'); + + // Protocol + this.protocol = require('./protocol'); + this.consensus = require('./protocol/consensus'); + this.errors = require('./protocol/errors'); + this.network = require('./protocol/network'); + this.networks = require('./protocol/networks'); + this.policy = require('./protocol/policy'); + this.timedata = require('./protocol/timedata'); + + // Script + this.txscript = require('./script'); + this.opcode = require('./script/opcode'); + this.program = require('./script/program'); + this.script = require('./script/script'); + this.sigcache = require('./script/sigcache'); + this.stack = require('./script/stack'); + this.witness = require('./script/witness'); + + // Utils + this.utils = require('./utils'); + this.base32 = require('./utils/base32'); + this.base58 = require('./utils/base58'); + this.bloom = require('./utils/bloom'); + this.co = require('./utils/co'); + this.encoding = require('./utils/encoding'); + this.lock = require('./utils/lock'); + this.reader = require('./utils/reader'); + this.staticwriter = require('./utils/staticwriter'); + this.util = require('./utils/util'); + this.writer = require('./utils/writer'); + + // Wallet + this.wallet = require('./wallet'); + this.path = require('./wallet/path'); + this.walletkey = require('./wallet/walletkey'); + this.walletdb = require('./wallet/walletdb'); + this.walletplugin = require('./wallet/plugin'); + + // Workers + this.workers = require('./workers'); + this.workerpool = require('./workers/workerpool'); +} + +/** + * Set the default network. + * @param {String} options + */ + +Environment.prototype.set = function set(options) { + if (typeof options === 'string') + options = { network: options }; + + if (!options) + options = {}; + + if (options.network) + this.network.set(options.network); + + this.workerpool.set(options); + + if (options.sigcacheSize != null) + this.sigcache.resize(options.sigcacheSize); + + return this; +}; + +/** + * Get the adjusted time of + * the default network. + * @returns {Number} Adjusted time. + */ + +Environment.prototype.now = function now() { + return this.network.primary.now(); +}; + +/** + * Cache all necessary modules. + */ + +Environment.prototype.cache = function cache() { + this.bip70; + this.common; + this.crypto; + this.fullnode; + this.http; + this.spvnode; +}; + +/* + * Expose by converting `exports` to an + * Environment. + */ + +exports.cache = Environment.prototype.cache; +exports.set = Environment.prototype.set; +exports.now = Environment.prototype.now; + +Environment.call(exports); diff --git a/lib/env.js b/lib/env.js index dc9d5a11..daf82494 100644 --- a/lib/env.js +++ b/lib/env.js @@ -160,7 +160,7 @@ function Environment() { this.require('ldb', './db/ldb'); // HD - this.require('hd', './hd'); + this.require('hd', './hd/hd'); // HTTP this.require('http', './http'); diff --git a/package.json b/package.json index 26c4b5da..20a6b5a5 100644 --- a/package.json +++ b/package.json @@ -30,6 +30,7 @@ "optionalDependencies": { "bcoin-native": "0.0.19", "leveldown": "1.7.0-0", + "level-js": "2.2.4", "secp256k1": "3.2.5", "socket.io": "2.0.1", "socket.io-client": "2.0.1" @@ -45,7 +46,6 @@ "eslint": "^4.1.0", "hash.js": "^1.0.3", "jsdoc": "^3.4.3", - "level-js": "^2.2.4", "mocha": "^3.4.1", "uglify-js": "^3.0.5" }, @@ -71,7 +71,8 @@ "./lib/crypto/backend": "./lib/crypto/backend-browser.js", "./lib/crypto/ec": "./lib/crypto/ec-elliptic.js", "./lib/crypto/pk": "./lib/crypto/pk-browser.js", - "./lib/db/backends": "./lib/db/backends-browser.js", + "./lib/db/backends.js": "./lib/db/backends-browser.js", + "./lib/env.js": "./lib/env-browser.js", "./lib/hd/wordlist": "./lib/hd/wordlist-browser.js", "./lib/http/base": "./browser/empty.js", "./lib/http/client": "./browser/empty.js", @@ -95,7 +96,9 @@ "bcoin-native": "./browser/empty.js", "child_process": "./browser/empty.js", "crypto": "./browser/empty.js", + "ec":"./lib/crypto/ec-elliptic.js", "dgram": "./browser/empty.js", + "dns": "./lib/net/dns-browser.js", "fs": "./browser/empty.js", "net": "./browser/empty.js", "os": "./browser/empty.js", From 64f9b6b0a8754f877016bd6c79bd6f658ba43c1b Mon Sep 17 00:00:00 2001 From: Boyma Fahnbulleh Date: Sat, 25 Mar 2017 17:00:28 -0700 Subject: [PATCH 02/10] update dns browser import --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 20a6b5a5..664fcfe4 100644 --- a/package.json +++ b/package.json @@ -98,7 +98,7 @@ "crypto": "./browser/empty.js", "ec":"./lib/crypto/ec-elliptic.js", "dgram": "./browser/empty.js", - "dns": "./lib/net/dns-browser.js", + "dns": "./browser/empty.js", "fs": "./browser/empty.js", "net": "./browser/empty.js", "os": "./browser/empty.js", From 9113f031076d3c1f4321f83f8a5a053b344f7c48 Mon Sep 17 00:00:00 2001 From: Boyma Fahnbulleh Date: Sat, 25 Mar 2017 17:19:30 -0700 Subject: [PATCH 03/10] remove transform --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 664fcfe4..283146b1 100644 --- a/package.json +++ b/package.json @@ -105,6 +105,6 @@ "secp256k1": "./browser/empty.js" }, "browserify": { - "transform": ["./browser/transform.js", "babelify"] + "transform": ["babelify"] } } From 48d60eeaab363ad8c3ee95ac54d139899234cf27 Mon Sep 17 00:00:00 2001 From: Boyma Fahnbulleh Date: Sun, 26 Mar 2017 15:04:31 -0700 Subject: [PATCH 04/10] make package.json browser field compatible with webpack --- package.json | 24 +++++++++++++++--------- webpack.config.js | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 9 deletions(-) create mode 100644 webpack.config.js diff --git a/package.json b/package.json index 283146b1..aa893e15 100644 --- a/package.json +++ b/package.json @@ -37,6 +37,7 @@ }, "devDependencies": { "babelify": "^7.3.0", + "babel-loader": "^6.4.1", "babel-preset-es2015": "^6.24.1", "babel-polyfill": "^6.23.0", "babel-plugin-transform-runtime": "^6.23.0", @@ -47,7 +48,8 @@ "hash.js": "^1.0.3", "jsdoc": "^3.4.3", "mocha": "^3.4.1", - "uglify-js": "^3.0.5" + "uglify-js": "^3.0.5", + "webpack": "^1.13.3" }, "main": "./lib/bcoin.js", "bin": { @@ -64,13 +66,14 @@ "uglify": "uglifyjs -m -o browser/bcoin.min.js browser/bcoin.js && uglifyjs -m -o browser/bcoin-master.min.js browser/bcoin-master.js", "clean": "rm browser/bcoin.js browser/bcoin.min.js browser/bcoin-master.js browser/bcoin-master.min.js", "lint": "eslint lib/ test/ migrate/ examples/ bench/ bin/cli bin/node bin/spvnode || exit 0", - "docs": "jsdoc -c jsdoc.json" + "docs": "jsdoc -c jsdoc.json", + "webpack": "./node_modules/webpack/bin/webpack.js" }, "browser": { - "./lib/blockchain/layout": "./lib/blockchain/layout-browser.js", - "./lib/crypto/backend": "./lib/crypto/backend-browser.js", - "./lib/crypto/ec": "./lib/crypto/ec-elliptic.js", - "./lib/crypto/pk": "./lib/crypto/pk-browser.js", + "./lib/blockchain/layout.js": "./lib/blockchain/layout-browser.js", + "./lib/crypto/backend.js": "./lib/crypto/backend-browser.js", + "./lib/crypto/ec.js": "./lib/crypto/ec-elliptic.js", + "./lib/crypto/pk.js": "./lib/crypto/pk-browser.js", "./lib/db/backends.js": "./lib/db/backends-browser.js", "./lib/env.js": "./lib/env-browser.js", "./lib/hd/wordlist": "./lib/hd/wordlist-browser.js", @@ -96,15 +99,18 @@ "bcoin-native": "./browser/empty.js", "child_process": "./browser/empty.js", "crypto": "./browser/empty.js", - "ec":"./lib/crypto/ec-elliptic.js", "dgram": "./browser/empty.js", "dns": "./browser/empty.js", + "ec": "./lib/crypto/ec-elliptic.js", "fs": "./browser/empty.js", "net": "./browser/empty.js", "os": "./browser/empty.js", - "secp256k1": "./browser/empty.js" + "secp256k1": "./browser/empty.js", + "socket.io": "./browser/empty.js" }, "browserify": { - "transform": ["babelify"] + "transform": [ + "babelify" + ] } } diff --git a/webpack.config.js b/webpack.config.js new file mode 100644 index 00000000..5b936508 --- /dev/null +++ b/webpack.config.js @@ -0,0 +1,39 @@ +const webpack = require('webpack') +const PATHS = { + bcoin: './lib/bcoin', + master: './lib/workers/master' +} +module.exports = { + entry: { + 'bcoin': PATHS.bcoin, + 'bcoin.min': PATHS.bcoin, + 'bcoin-master': PATHS.master, + 'bcoin-master.min': PATHS.master + }, + output: { + path: './browser', + filename: '[name].js' + }, + resolve: { + extensions: ['', '.js', '.json'], + packageAlias: 'browser' + }, + module: { + loaders: [ + { test: /\.js$/, loader: 'babel', exclude: /node_modules/ }, + { test: /\.json$/, loader: 'json' } + ] + }, + node: { + fs: 'empty' + }, + plugins: [ + new webpack.optimize.UglifyJsPlugin({ + compress: { + warnings: false + }, + include: /\.min\.js$/, + minimize: true + }) + ] +} From 096aaaebb2ed8a3439b05e9d01593cf744fe201b Mon Sep 17 00:00:00 2001 From: Boyma Fahnbulleh Date: Sun, 26 Mar 2017 16:21:09 -0700 Subject: [PATCH 05/10] s/PATHS/paths --- webpack.config.js | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/webpack.config.js b/webpack.config.js index 5b936508..cbcec185 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -1,14 +1,15 @@ const webpack = require('webpack') -const PATHS = { +const paths = { bcoin: './lib/bcoin', master: './lib/workers/master' } + module.exports = { entry: { - 'bcoin': PATHS.bcoin, - 'bcoin.min': PATHS.bcoin, - 'bcoin-master': PATHS.master, - 'bcoin-master.min': PATHS.master + 'bcoin': paths.bcoin, + 'bcoin.min': paths.bcoin, + 'bcoin-master': paths.master, + 'bcoin-master.min': paths.master }, output: { path: './browser', From 6048581f056e2d2e71d47935137bdf665c064ed0 Mon Sep 17 00:00:00 2001 From: Boyma Fahnbulleh Date: Sun, 26 Mar 2017 18:34:27 -0700 Subject: [PATCH 06/10] remove browserify --- Makefile | 3 +-- package.json | 3 --- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/Makefile b/Makefile index 6327dd04..79ec5c58 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,5 @@ all: - @npm run browserify - @npm run uglify + @npm run webpack @cp -f lib/workers/worker-browser.js browser/bcoin-worker.js clean: diff --git a/package.json b/package.json index aa893e15..321dec6d 100644 --- a/package.json +++ b/package.json @@ -43,7 +43,6 @@ "babel-plugin-transform-runtime": "^6.23.0", "babel-plugin-transform-regenerator": "^6.24.1", "babel-plugin-transform-async-to-generator": "^6.24.1", - "browserify": "^14.3.0", "eslint": "^4.1.0", "hash.js": "^1.0.3", "jsdoc": "^3.4.3", @@ -62,8 +61,6 @@ "test": "mocha --reporter spec test/*-test.js", "test-file": "mocha --reporter spec ", "test-browser": "BCOIN_NO_NATIVE=1 BCOIN_USE_ELLIPTIC=1 mocha --reporter spec test/*-test.js", - "browserify": "browserify --im -o browser/bcoin.js lib/bcoin.js && browserify --im -o browser/bcoin-master.js lib/workers/master.js", - "uglify": "uglifyjs -m -o browser/bcoin.min.js browser/bcoin.js && uglifyjs -m -o browser/bcoin-master.min.js browser/bcoin-master.js", "clean": "rm browser/bcoin.js browser/bcoin.min.js browser/bcoin-master.js browser/bcoin-master.min.js", "lint": "eslint lib/ test/ migrate/ examples/ bench/ bin/cli bin/node bin/spvnode || exit 0", "docs": "jsdoc -c jsdoc.json", From 0d831b0c57e9cb808317c0b02d414e13cddf7d13 Mon Sep 17 00:00:00 2001 From: Boyma Fahnbulleh Date: Sun, 26 Mar 2017 19:10:49 -0700 Subject: [PATCH 07/10] only output minimized files --- browser/index.html | 2 +- browser/server.js | 8 ++++---- lib/workers/worker-browser.js | 2 +- webpack.config.js | 18 +++++------------- 4 files changed, 11 insertions(+), 19 deletions(-) diff --git a/browser/index.html b/browser/index.html index b3d72ef5..d05b7613 100644 --- a/browser/index.html +++ b/browser/index.html @@ -74,7 +74,7 @@ overflow-y: scroll; } - +

Bcoin, the browser full node

diff --git a/browser/server.js b/browser/server.js index c98ed884..a75d16d4 100644 --- a/browser/server.js +++ b/browser/server.js @@ -7,8 +7,8 @@ var server, proxy; var index = fs.readFileSync(__dirname + '/index.html'); var indexjs = fs.readFileSync(__dirname + '/index.js'); -var bcoin = fs.readFileSync(__dirname + '/bcoin.js'); -var master = fs.readFileSync(__dirname + '/bcoin-master.js'); +var bcoin = fs.readFileSync(__dirname + '/bcoin.min.js'); +var master = fs.readFileSync(__dirname + '/bcoin-master.min.js'); var worker = fs.readFileSync(__dirname + '/bcoin-worker.js'); proxy = new WSProxy({ @@ -37,11 +37,11 @@ server.get('/index.js', function(req, res) { res.send(200, indexjs, 'js'); }); -server.get('/bcoin.js', function(req, res) { +server.get('/bcoin.min.js', function(req, res) { res.send(200, bcoin, 'js'); }); -server.get('/bcoin-master.js', function(req, res) { +server.get('/bcoin-master.min.js', function(req, res) { res.send(200, master, 'js'); }); diff --git a/lib/workers/worker-browser.js b/lib/workers/worker-browser.js index a1fa25d9..dfc17a6e 100644 --- a/lib/workers/worker-browser.js +++ b/lib/workers/worker-browser.js @@ -9,7 +9,7 @@ /* jshint worker: true */ -self.importScripts('/bcoin-master.js'); +self.importScripts('/bcoin-master.min.js'); self.onmessage = function onmessage(event) { var env; diff --git a/webpack.config.js b/webpack.config.js index cbcec185..6de4195c 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -1,15 +1,9 @@ const webpack = require('webpack') -const paths = { - bcoin: './lib/bcoin', - master: './lib/workers/master' -} module.exports = { entry: { - 'bcoin': paths.bcoin, - 'bcoin.min': paths.bcoin, - 'bcoin-master': paths.master, - 'bcoin-master.min': paths.master + 'bcoin.min': './lib/bcoin', + 'bcoin-master.min': './lib/workers/master' }, output: { path: './browser', @@ -30,11 +24,9 @@ module.exports = { }, plugins: [ new webpack.optimize.UglifyJsPlugin({ - compress: { - warnings: false - }, - include: /\.min\.js$/, - minimize: true + compress: { + warnings: false + } }) ] } From c8cdb8c3e213ae192954b324e9f0aa33375fe120 Mon Sep 17 00:00:00 2001 From: Boyma Fahnbulleh Date: Sun, 26 Mar 2017 19:44:11 -0700 Subject: [PATCH 08/10] remove transform.js --- browser/transform.js | 57 -------------------------------------------- 1 file changed, 57 deletions(-) delete mode 100644 browser/transform.js diff --git a/browser/transform.js b/browser/transform.js deleted file mode 100644 index fef5f49e..00000000 --- a/browser/transform.js +++ /dev/null @@ -1,57 +0,0 @@ -var assert = require('assert'); -var Transform = require('stream').Transform; -var path = require('path'); -var StringDecoder = require('string_decoder').StringDecoder; - -function nil() { - var stream = new Transform(); - - stream._transform = function(chunk, encoding, callback) { - callback(null, chunk); - }; - - stream._flush = function(callback) { - callback(); - }; - - return stream; -} - -function processEnv(str) { - return str.replace( - /^( *)this\.require\('(\w+)', '([^']+)'\)/gm, - '$1this.$2 = require(\'$3\')'); -} - -function transformer(file, process) { - var stream = new Transform(); - var decoder = new StringDecoder('utf8'); - var str = ''; - - stream._transform = function(chunk, encoding, callback) { - assert(Buffer.isBuffer(chunk)); - str += decoder.write(chunk); - callback(null, Buffer.allocUnsafe(0)); - }; - - stream._flush = function(callback) { - str = process(str); - - stream.push(Buffer.from(str, 'utf8')); - - callback(); - }; - - return stream; -} - -function end(file, offset) { - return path.normalize(file).split(path.sep).slice(-offset).join('/'); -} - -module.exports = function(file) { - if (end(file, 2) === 'lib/env.js') - return transformer(file, processEnv); - - return nil(); -}; From fb2a5c853481a065287389fded169be40411650b Mon Sep 17 00:00:00 2001 From: Boyma Fahnbulleh Date: Sun, 26 Mar 2017 20:20:28 -0700 Subject: [PATCH 09/10] edit make clean --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 321dec6d..20da46a1 100644 --- a/package.json +++ b/package.json @@ -61,7 +61,7 @@ "test": "mocha --reporter spec test/*-test.js", "test-file": "mocha --reporter spec ", "test-browser": "BCOIN_NO_NATIVE=1 BCOIN_USE_ELLIPTIC=1 mocha --reporter spec test/*-test.js", - "clean": "rm browser/bcoin.js browser/bcoin.min.js browser/bcoin-master.js browser/bcoin-master.min.js", + "clean": "rm browser/bcoin.min.js browser/bcoin-master.min.js", "lint": "eslint lib/ test/ migrate/ examples/ bench/ bin/cli bin/node bin/spvnode || exit 0", "docs": "jsdoc -c jsdoc.json", "webpack": "./node_modules/webpack/bin/webpack.js" From bf5ea728635f31014ed2b29fa62b83c9f5dd2a61 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Mon, 26 Jun 2017 17:41:11 -0700 Subject: [PATCH 10/10] webpack: fix babelification. upgrade to webpack3. --- .babelrc | 2 +- Makefile | 5 ++- browser/index.html | 12 ++++---- browser/server.js | 8 ++--- lib/env-browser.js | 14 +++------ lib/env.js | 17 +++++++++-- lib/utils/index.js | 1 - lib/utils/lazy-browser.js | 11 ------- lib/utils/lazy.js | 25 --------------- lib/utils/nfkd.js | 10 +----- lib/workers/worker-browser.js | 2 +- package.json | 57 +++++++++++++++-------------------- webpack.config.js | 32 +++++++++++--------- 13 files changed, 78 insertions(+), 118 deletions(-) delete mode 100644 lib/utils/lazy-browser.js delete mode 100644 lib/utils/lazy.js diff --git a/.babelrc b/.babelrc index 1142f253..17d01063 100644 --- a/.babelrc +++ b/.babelrc @@ -1,7 +1,7 @@ { "presets": ["es2015"], "plugins": [ - "transform-async-to-generator", + ["transform-async-to-generator"], ["transform-runtime", { "polyfill": true, "regenerator": true diff --git a/Makefile b/Makefile index 79ec5c58..71225bcf 100644 --- a/Makefile +++ b/Makefile @@ -8,7 +8,10 @@ clean: docs: @npm run docs +lint: + @npm run lint + test: @npm test -.PHONY: all clean docs test +.PHONY: all clean docs lint test diff --git a/browser/index.html b/browser/index.html index d05b7613..3e147a00 100644 --- a/browser/index.html +++ b/browser/index.html @@ -3,12 +3,12 @@ bcoin - +

Bcoin, the browser full node

diff --git a/browser/server.js b/browser/server.js index a75d16d4..c98ed884 100644 --- a/browser/server.js +++ b/browser/server.js @@ -7,8 +7,8 @@ var server, proxy; var index = fs.readFileSync(__dirname + '/index.html'); var indexjs = fs.readFileSync(__dirname + '/index.js'); -var bcoin = fs.readFileSync(__dirname + '/bcoin.min.js'); -var master = fs.readFileSync(__dirname + '/bcoin-master.min.js'); +var bcoin = fs.readFileSync(__dirname + '/bcoin.js'); +var master = fs.readFileSync(__dirname + '/bcoin-master.js'); var worker = fs.readFileSync(__dirname + '/bcoin-worker.js'); proxy = new WSProxy({ @@ -37,11 +37,11 @@ server.get('/index.js', function(req, res) { res.send(200, indexjs, 'js'); }); -server.get('/bcoin.min.js', function(req, res) { +server.get('/bcoin.js', function(req, res) { res.send(200, bcoin, 'js'); }); -server.get('/bcoin-master.min.js', function(req, res) { +server.get('/bcoin-master.js', function(req, res) { res.send(200, master, 'js'); }); diff --git a/lib/env-browser.js b/lib/env-browser.js index 8cef81cc..0c740bfe 100644 --- a/lib/env-browser.js +++ b/lib/env-browser.js @@ -15,6 +15,7 @@ * @exports Environment * @constructor * @property {Function} env - See {@link Environment}. + * @property {Function} require - See {@link module:utils/lazy}. * * @property {Function} bn - See {@url https://github.com/indutny/bn.js}. * @property {Object} elliptic - See {@url https://github.com/indutny/elliptic}. @@ -126,10 +127,6 @@ function Environment() { this.env = Environment; - // BN - this.bn = require('bn.js'); - this.elliptic = require('elliptic'); - // Horrible BIP this.bip70 = require('./bip70'); @@ -150,6 +147,7 @@ function Environment() { // Crypto this.crypto = require('./crypto'); + this.bn = require('./crypto/bn'); this.ec = require('./crypto/ec'); this.pk = require('./crypto/pk'); this.schnorr = require('./crypto/schnorr'); @@ -235,6 +233,7 @@ function Environment() { this.bloom = require('./utils/bloom'); this.co = require('./utils/co'); this.encoding = require('./utils/encoding'); + this.int64 = require('./utils/int64'); this.lock = require('./utils/lock'); this.reader = require('./utils/reader'); this.staticwriter = require('./utils/staticwriter'); @@ -291,12 +290,7 @@ Environment.prototype.now = function now() { */ Environment.prototype.cache = function cache() { - this.bip70; - this.common; - this.crypto; - this.fullnode; - this.http; - this.spvnode; + ; }; /* diff --git a/lib/env.js b/lib/env.js index daf82494..60996c66 100644 --- a/lib/env.js +++ b/lib/env.js @@ -7,8 +7,6 @@ 'use strict'; -var lazy = require('./utils/lazy'); - /** * A bcoin "environment" which exposes all * constructors for primitives, the blockchain, @@ -128,7 +126,6 @@ var lazy = require('./utils/lazy'); function Environment() { this.env = Environment; - this.require = lazy(require, this); // Horrible BIP this.require('bip70', './bip70'); @@ -301,12 +298,26 @@ Environment.prototype.cache = function cache() { this.spvnode; }; +/** + * Cache all necessary modules. + */ + +Evironment.prototype.require = function _require(name, path) { + var cache; + this.__defineGetter__(name, function() { + if (!cache) + cache = require(path); + return cache; + }); +}; + /* * Expose by converting `exports` to an * Environment. */ exports.cache = Environment.prototype.cache; +exports.require = Environment.prototype.require; exports.set = Environment.prototype.set; exports.now = Environment.prototype.now; diff --git a/lib/utils/index.js b/lib/utils/index.js index ba3bac2a..a9dc8bc8 100644 --- a/lib/utils/index.js +++ b/lib/utils/index.js @@ -17,7 +17,6 @@ exports.fs = require('./fs'); exports.Heap = require('./heap'); exports.Int64 = require('./int64'); exports.IP = require('./ip'); -exports.lazy = require('./lazy'); exports.Lock = require('./lock'); exports.MappedLock = exports.Lock.Mapped; exports.LRU = require('./lru'); diff --git a/lib/utils/lazy-browser.js b/lib/utils/lazy-browser.js deleted file mode 100644 index 92b907a2..00000000 --- a/lib/utils/lazy-browser.js +++ /dev/null @@ -1,11 +0,0 @@ -/*! - * lazy-browser.js - lazy loading for bcoin - * Copyright (c) 2016-2017, Christopher Jeffrey (MIT License). - * https://github.com/bcoin-org/bcoin - */ - -'use strict'; - -module.exports = function lazy(require, exports) { - return function() {}; -}; diff --git a/lib/utils/lazy.js b/lib/utils/lazy.js deleted file mode 100644 index 7627315a..00000000 --- a/lib/utils/lazy.js +++ /dev/null @@ -1,25 +0,0 @@ -/*! - * lazy.js - lazy loading for bcoin - * Copyright (c) 2016-2017, Christopher Jeffrey (MIT License). - * https://github.com/bcoin-org/bcoin - */ - -'use strict'; - -/** - * Setup a lazy loader. - * @alias module:utils.lazy - * @param {Function} require - * @param {Object} exports - */ - -module.exports = function lazy(require, exports) { - return function _require(name, path) { - var cache; - exports.__defineGetter__(name, function() { - if (!cache) - cache = require(path); - return cache; - }); - }; -}; diff --git a/lib/utils/nfkd.js b/lib/utils/nfkd.js index 8ab57ba0..35335502 100644 --- a/lib/utils/nfkd.js +++ b/lib/utils/nfkd.js @@ -6,8 +6,6 @@ 'use strict'; -var unorm; - /** * Normalize unicode string. * @alias module:utils.nfkd @@ -16,13 +14,7 @@ var unorm; */ function nfkd(str) { - if (str.normalize) - return str.normalize('NFKD'); - - if (!unorm) - unorm = require('../../vendor/unorm'); - - return unorm.nfkd(str); + return str.normalize('NFKD'); } /* diff --git a/lib/workers/worker-browser.js b/lib/workers/worker-browser.js index dfc17a6e..a1fa25d9 100644 --- a/lib/workers/worker-browser.js +++ b/lib/workers/worker-browser.js @@ -9,7 +9,7 @@ /* jshint worker: true */ -self.importScripts('/bcoin-master.min.js'); +self.importScripts('/bcoin-master.js'); self.onmessage = function onmessage(event) { var env; diff --git a/package.json b/package.json index 20da46a1..b9305a15 100644 --- a/package.json +++ b/package.json @@ -30,25 +30,24 @@ "optionalDependencies": { "bcoin-native": "0.0.19", "leveldown": "1.7.0-0", - "level-js": "2.2.4", "secp256k1": "3.2.5", "socket.io": "2.0.1", "socket.io-client": "2.0.1" }, "devDependencies": { - "babelify": "^7.3.0", - "babel-loader": "^6.4.1", - "babel-preset-es2015": "^6.24.1", - "babel-polyfill": "^6.23.0", + "babel-core": "^6.25.0", + "babel-loader": "^7.1.0", + "babel-plugin-transform-async-to-generator": "^6.24.1", "babel-plugin-transform-runtime": "^6.23.0", "babel-plugin-transform-regenerator": "^6.24.1", - "babel-plugin-transform-async-to-generator": "^6.24.1", + "babel-polyfill": "^6.23.0", + "babel-preset-es2015": "^6.24.1", "eslint": "^4.1.0", "hash.js": "^1.0.3", "jsdoc": "^3.4.3", + "level-js": "2.2.4", "mocha": "^3.4.1", - "uglify-js": "^3.0.5", - "webpack": "^1.13.3" + "webpack": "^3.0.0" }, "main": "./lib/bcoin.js", "bin": { @@ -58,15 +57,26 @@ "bcoin": "./bin/bcoin" }, "scripts": { - "test": "mocha --reporter spec test/*-test.js", - "test-file": "mocha --reporter spec ", - "test-browser": "BCOIN_NO_NATIVE=1 BCOIN_USE_ELLIPTIC=1 mocha --reporter spec test/*-test.js", - "clean": "rm browser/bcoin.min.js browser/bcoin-master.min.js", - "lint": "eslint lib/ test/ migrate/ examples/ bench/ bin/cli bin/node bin/spvnode || exit 0", + "clean": "rm browser/bcoin.js browser/bcoin-master.js browser/bcoin-worker.js", "docs": "jsdoc -c jsdoc.json", - "webpack": "./node_modules/webpack/bin/webpack.js" + "lint": "eslint lib/ test/ migrate/ examples/ bench/ bin/cli bin/node bin/spvnode || exit 0", + "lint-file": "eslint", + "test": "mocha --reporter spec test/*-test.js", + "test-browser": "BCOIN_NO_NATIVE=1 BCOIN_USE_ELLIPTIC=1 mocha --reporter spec test/*-test.js", + "test-file": "mocha --reporter spec", + "webpack": "webpack" }, "browser": { + "bcoin-native": "./browser/empty.js", + "child_process": "./browser/empty.js", + "crypto": "./browser/empty.js", + "dgram": "./browser/empty.js", + "dns": "./browser/empty.js", + "fs": "./browser/empty.js", + "net": "./browser/empty.js", + "os": "./browser/empty.js", + "secp256k1": "./browser/empty.js", + "socket.io": "./browser/empty.js", "./lib/blockchain/layout.js": "./lib/blockchain/layout-browser.js", "./lib/crypto/backend.js": "./lib/crypto/backend-browser.js", "./lib/crypto/ec.js": "./lib/crypto/ec-elliptic.js", @@ -89,25 +99,8 @@ "./lib/utils/native": "./browser/empty.js", "./lib/utils/nfkd": "./lib/utils/nfkd-browser.js", "./lib/utils/nexttick": "./lib/utils/nexttick-browser.js", - "./lib/utils/lazy": "./lib/utils/lazy-browser.js", "./lib/wallet/http": "./browser/empty.js", "./lib/wallet/layout": "./lib/wallet/layout-browser.js", - "./lib/wallet/server": "./browser/empty.js", - "bcoin-native": "./browser/empty.js", - "child_process": "./browser/empty.js", - "crypto": "./browser/empty.js", - "dgram": "./browser/empty.js", - "dns": "./browser/empty.js", - "ec": "./lib/crypto/ec-elliptic.js", - "fs": "./browser/empty.js", - "net": "./browser/empty.js", - "os": "./browser/empty.js", - "secp256k1": "./browser/empty.js", - "socket.io": "./browser/empty.js" - }, - "browserify": { - "transform": [ - "babelify" - ] + "./lib/wallet/server": "./browser/empty.js" } } diff --git a/webpack.config.js b/webpack.config.js index 6de4195c..77c89ddf 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -1,26 +1,30 @@ -const webpack = require('webpack') +'use strict'; + +var webpack = require('webpack') +var path = require('path'); module.exports = { + target: 'web', entry: { - 'bcoin.min': './lib/bcoin', - 'bcoin-master.min': './lib/workers/master' + 'bcoin': './lib/bcoin', + 'bcoin-master': './lib/workers/master' }, output: { - path: './browser', + path: path.resolve(__dirname, 'browser'), filename: '[name].js' }, resolve: { - extensions: ['', '.js', '.json'], - packageAlias: 'browser' + descriptionFiles: ['package.json'], + modules: ['node_modules'], + extensions: ['.js', '.json'], + aliasFields: ['browser'] }, module: { - loaders: [ - { test: /\.js$/, loader: 'babel', exclude: /node_modules/ }, - { test: /\.json$/, loader: 'json' } - ] - }, - node: { - fs: 'empty' + rules: [{ + test: /\.js$/, + exclude: path.resolve(__dirname, 'node_modules'), + loader: 'babel-loader' + }] }, plugins: [ new webpack.optimize.UglifyJsPlugin({ @@ -29,4 +33,4 @@ module.exports = { } }) ] -} +};