Merge branch 'webpack'. Closes #178.

This commit is contained in:
Christopher Jeffrey 2017-06-26 17:43:50 -07:00
commit 19f236f740
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
12 changed files with 399 additions and 148 deletions

View File

@ -1,7 +1,7 @@
{
"presets": ["es2015"],
"plugins": [
"transform-async-to-generator",
["transform-async-to-generator"],
["transform-runtime", {
"polyfill": true,
"regenerator": true

View File

@ -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:
@ -9,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

View File

@ -3,12 +3,12 @@
<head>
<title>bcoin</title>
<style>
html {
height: 100%;
html {
height: 100%;
}
body {
height: 90%;
padding: 20px;
body {
height: 90%;
padding: 20px;
}
h1 {
font: 3em monospace;

View File

@ -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();
};

305
lib/env-browser.js Normal file
View File

@ -0,0 +1,305 @@
/*!
* 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} 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}.
*
* @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;
// 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.bn = require('./crypto/bn');
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.int64 = require('./utils/int64');
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() {
;
};
/*
* 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);

View File

@ -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');
@ -160,7 +157,7 @@ function Environment() {
this.require('ldb', './db/ldb');
// HD
this.require('hd', './hd');
this.require('hd', './hd/hd');
// HTTP
this.require('http', './http');
@ -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;

View File

@ -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');

View File

@ -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() {};
};

View File

@ -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;
});
};
};

View File

@ -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');
}
/*

View File

@ -35,19 +35,19 @@
"socket.io-client": "2.0.1"
},
"devDependencies": {
"babelify": "^7.3.0",
"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",
"browserify": "^14.3.0",
"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",
"level-js": "2.2.4",
"mocha": "^3.4.1",
"uglify-js": "^3.0.5"
"webpack": "^3.0.0"
},
"main": "./lib/bcoin.js",
"bin": {
@ -57,21 +57,32 @@
"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",
"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",
"clean": "rm browser/bcoin.js browser/bcoin-master.js browser/bcoin-worker.js",
"docs": "jsdoc -c jsdoc.json",
"lint": "eslint lib/ test/ migrate/ examples/ bench/ bin/cli bin/node bin/spvnode || exit 0",
"docs": "jsdoc -c jsdoc.json"
"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": {
"./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/db/backends": "./lib/db/backends-browser.js",
"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",
"./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",
"./lib/http/base": "./browser/empty.js",
"./lib/http/client": "./browser/empty.js",
@ -88,20 +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",
"fs": "./browser/empty.js",
"net": "./browser/empty.js",
"os": "./browser/empty.js",
"secp256k1": "./browser/empty.js"
},
"browserify": {
"transform": ["./browser/transform.js", "babelify"]
"./lib/wallet/server": "./browser/empty.js"
}
}

36
webpack.config.js Normal file
View File

@ -0,0 +1,36 @@
'use strict';
var webpack = require('webpack')
var path = require('path');
module.exports = {
target: 'web',
entry: {
'bcoin': './lib/bcoin',
'bcoin-master': './lib/workers/master'
},
output: {
path: path.resolve(__dirname, 'browser'),
filename: '[name].js'
},
resolve: {
descriptionFiles: ['package.json'],
modules: ['node_modules'],
extensions: ['.js', '.json'],
aliasFields: ['browser']
},
module: {
rules: [{
test: /\.js$/,
exclude: path.resolve(__dirname, 'node_modules'),
loader: 'babel-loader'
}]
},
plugins: [
new webpack.optimize.UglifyJsPlugin({
compress: {
warnings: false
}
})
]
};