Skip to content

Commit

Permalink
removed SYNC, moved providers in separate packages
Browse files Browse the repository at this point in the history
  • Loading branch information
frozeman committed Feb 28, 2017
1 parent a9cd4b3 commit b1cce88
Show file tree
Hide file tree
Showing 36 changed files with 230 additions and 331 deletions.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "web3",
"namespace": "ethereum",
"version": "1.0.0",
"version": "1.0.0-beta",
"description": "Ethereum Compatible JavaScript API",
"main": [
"./dist/web3.js",
Expand Down
124 changes: 91 additions & 33 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ var gulp = require('gulp');
var browserify = require('browserify');
var jshint = require('gulp-jshint');
var uglify = require('gulp-uglify');
// var closureCompiler = require('google-closure-compiler').gulp();
var rename = require('gulp-rename');
var source = require('vinyl-source-stream');
var exorcist = require('exorcist');
Expand All @@ -18,13 +19,63 @@ var streamify = require('gulp-streamify');
var replace = require('gulp-replace');

var DEST = path.join(__dirname, 'dist/');
var src = 'index';
var dst = 'web3';
var lightDst = 'web3-light';
var packages = [{
fileName: 'web3',
expose: 'Web3',
src: './src/index.js'
},{
fileName: 'web3-utils',
expose: 'Utils',
src: './packages/web3-utils/src/index.js'
},{
fileName: 'web3-core-requestManager',
expose: 'RequestManager',
src: './packages/web3-core-requestManager/src/index.js'
},{
fileName: 'web3-providers-ipc',
expose: 'IpcProvider',
src: './packages/web3-providers-ipc/src/index.js'
},{
fileName: 'web3-providers-http',
expose: 'HttpProvider',
src: './packages/web3-providers-http/src/index.js',
ignore: ['xmlhttprequest']
},{
fileName: 'web3-providers-ws',
expose: 'WsProvider',
src: './packages/web3-providers-ws/src/index.js'
},{
fileName: 'web3-eth',
expose: 'Eth',
src: './packages/web3-eth/src/index.js'
}];
// ,{
// fileName: 'web3-eth',
// expose: 'Eth',
// src: './packages/web3-eth/src/index.js'
// },{
// fileName: 'web3-personal',
// expose: 'Personal',
// src: './packages/web3-personal/src/index.js'
// },{
// fileName: 'web3-shh',
// expose: 'Shh',
// src: './packages/web3-shh/src/index.js'
// },{
// fileName: 'web3-bzz',
// expose: 'Bzz',
// src: './packages/web3-bzz/src/index.js'
// },{
// fileName: 'web3-eth-iban',
// expose: 'Iban',
// src: './packages/web3-eth-iban/src/index.js'
// }];

var browserifyOptions = {
debug: true,
insert_global_vars: false, // jshint ignore:line
// standalone: 'Web3',
derequire: true,
insertGlobalVars: false, // jshint ignore:line
detectGlobals: false,
bundleExternal: true
};
Expand Down Expand Up @@ -58,39 +109,46 @@ gulp.task('clean', ['lint'], function(cb) {
del([ DEST ]).then(cb.bind(null, null));
});

gulp.task('light', ['clean'], function () {
return browserify(browserifyOptions)
.require('./' + src + '.js', {expose: 'web3'})
.ignore('bn.js')
.require('./lib/utils/browser-bn.js', {expose: 'bn.js'}) // fake bn.js
.add('./' + src + '.js')
.bundle()
.pipe(exorcist(path.join( DEST, lightDst + '.js.map')))
.pipe(source(lightDst + '.js'))
.pipe(gulp.dest( DEST ))
.pipe(streamify(uglify()))
.pipe(rename(lightDst + '.min.js'))
.pipe(gulp.dest( DEST ));
});
packages.forEach(function(pckg, i){
var prevPckg = (!i) ? 'clean' : packages[i-1].fileName;

gulp.task(pckg.fileName, [prevPckg], function () {
browserifyOptions.standalone = pckg.expose;

var pipe = browserify(browserifyOptions)
.require(pckg.src, {expose: pckg.expose})
.require('bn.js', {expose: 'BN'}) // expose it to dapp developers
.add(pckg.src);

gulp.task('standalone', ['clean'], function () {
return browserify(browserifyOptions)
.require('./' + src + '.js', {expose: 'web3'})
.require('bn.js') // expose it to dapp users
.add('./' + src + '.js')
.ignore('crypto')
.bundle()
.pipe(exorcist(path.join( DEST, dst + '.js.map')))
.pipe(source(dst + '.js'))
.pipe(gulp.dest( DEST ))
.pipe(streamify(uglify()))
.pipe(rename(dst + '.min.js'))
.pipe(gulp.dest( DEST ));
if(pckg.ignore) {
pckg.ignore.forEach(function (ignore) {
pipe.ignore(ignore);
});
}

return pipe.bundle()
.pipe(exorcist(path.join( DEST, pckg.fileName + '.js.map')))
.pipe(source(pckg.fileName + '.js'))
.pipe(gulp.dest( DEST ))
.pipe(streamify(uglify()))
.pipe(rename(pckg.fileName + '.min.js'))
// .pipe(streamify(closureCompiler({
// compilation_level: 'ADVANCED_OPTIMIZATIONS',
// warning_level: 'VERBOSE',
// jscomp_off: 'checkVars',
// language_in: 'ECMASCRIPT6_STRICT',
// language_out: 'ECMASCRIPT5_STRICT',
// output_wrapper: '(function(){\n%output%\n}).call(this)',
// js_output_file: pckg.fileName + '.min.js'
// })))
.pipe(gulp.dest( DEST ));
});
});


gulp.task('watch', function() {
gulp.watch(['./lib/*.js'], ['lint', 'build']);
gulp.watch(['./src/*.js'], ['lint', 'build']);
});

gulp.task('default', ['version', 'lint', 'clean', 'light', 'standalone']);
gulp.task('default', ['version', 'lint', 'clean', packages[packages.length-1].fileName]);

2 changes: 1 addition & 1 deletion package.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* jshint ignore:start */
Package.describe({
name: 'ethereum:web3',
version: '1.0.0',
version: '1.0.0-beta',
summary: 'Ethereum JavaScript API, middleware to talk to a ethreum node over RPC',
git: 'https://github.com/ethereum/ethereum.js',
// By default, Meteor will default to using README.md for documentation.
Expand Down
16 changes: 8 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,38 +1,38 @@
{
"name": "web3",
"namespace": "ethereum",
"version": "1.0.0",
"version": "1.0.0-beta",
"description": "Ethereum JavaScript API, middleware to talk to a ethereum node over RPC",
"license": "LGPL-3.0",
"main": "./src/index.js",
"directories": {
"src": "./src",
"doc": "./doc"
},
"dependencies": {
},
"dependencies": {},
"devDependencies": {
"bn.js": "^4.11.6",
"bower": ">=1.4.1",
"browserify": ">=10.0",
"chai": "^3.0.0",
"coveralls": "^2.11.2",
"crypto-js": "^3.1.4",
"del": ">=2.0.2",
"exorcist": "^0.4.0",
"google-closure-compiler": "^20170218.0.0",
"gulp": ">=3.9.0",
"gulp-jshint": ">=1.5.0",
"gulp-rename": ">=1.2.0",
"gulp-replace": "^0.5.3",
"gulp-streamify": "0.0.5",
"gulp-uglify": ">=1.2.0",
"gulp-uglify": "^2.0.1",
"istanbul": "^0.4.4",
"jshint": ">=2.5.0",
"lerna": "2.0.0-beta.32",
"mocha": ">=2.3.3",
"sandboxed-module": "^2.0.2",
"vinyl-source-stream": "^1.1.0",
"lerna": "2.0.0-beta.32",
"crypto-js": "^3.1.4",
"underscore": "^1.8.3",
"bn.js": "^4.11.6"
"vinyl-source-stream": "^1.1.0"
},
"scripts": {
"postinstall": "lerna bootstrap",
Expand Down
8 changes: 4 additions & 4 deletions packages/web3-bzz/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ var core = require('web3-core');
var Method = require('web3-core-method');


function Swarm() {
var Bzz = function Bzz() {
var _this = this;

// sets _requestmanager
Expand All @@ -37,9 +37,9 @@ function Swarm() {
method.attachToObject(_this);
method.setRequestManager(_this._requestManager);
});
}
};

core.addProviders(Swarm);
core.addProviders(Bzz);



Expand Down Expand Up @@ -98,5 +98,5 @@ var methods = function () {
};


module.exports = Swarm;
module.exports = Bzz;

2 changes: 1 addition & 1 deletion packages/web3-core-method/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ var promiEvent = require('web3-core-promiEvent');
var TIMEOUTBLOCK = 50;
var CONFIRMATIONBLOCKS = 12;

var Method = function (options) {
var Method = function Method(options) {

if(!options.call || !options.name) {
throw new Error('When creating a method you need to provide at least the "name" and "call" property.');
Expand Down
4 changes: 2 additions & 2 deletions packages/web3-core-promiEvent/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ var Promise = require("bluebird");
*
* @method eventifiedPromise
*/
var promiEvent = function(justPromise) {
var PromiEvent = function PromiEvent(justPromise) {
var resolve, reject,
eventEmitter = new Promise(function() {
resolve = arguments[0];
Expand Down Expand Up @@ -65,4 +65,4 @@ var promiEvent = function(justPromise) {
};
};

module.exports = promiEvent;
module.exports = PromiEvent;
2 changes: 1 addition & 1 deletion packages/web3-core-requestManager/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "web3-requestManager",
"version": "1.0.0",
"description": "Web3 module to handle requests to external providers. Contains also the providers.",
"description": "Web3 module to handle requests to external providers.",
"repository": "https://github.com/ethereum/web3.js/tree/master/packages/web3-requestManager",
"license": "LGPL-3.0",
"main": "src/index.js",
Expand Down
4 changes: 2 additions & 2 deletions packages/web3-core-requestManager/src/batch.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
var Jsonrpc = require('./jsonrpc');
var errors = require('web3-core-helpers').errors;

var Batch = function (web3) {
this.requestManager = web3._requestManager;
var Batch = function (requestManager) {
this.requestManager = requestManager;
this.requests = [];
};

Expand Down
67 changes: 14 additions & 53 deletions packages/web3-core-requestManager/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,33 +28,6 @@ var Jsonrpc = require('./jsonrpc');
var errors = require('web3-core-helpers').errors;
var BatchManager = require('./batch');

var HttpProvider = require('./providers/httpprovider');
var IpcProvider = require('./providers/ipcprovider');
var WebsocketProvider = require('./providers/websocketprovider');

var providers = {
HttpProvider: HttpProvider,
IpcProvider: IpcProvider,
WebsocketProvider: WebsocketProvider,
givenProvider: null
};

// add given provider
/* jshint ignore:start */
if(typeof ethereumProvider !== 'undefined') {
providers.givenProvider = ethereumProvider;

} else if(typeof web3 !== 'undefined' && web3.currentProvider) {
if(web3.currentProvider.sendAsync) {
web3.currentProvider.sendSync = web3.currentProvider.send;
web3.currentProvider.send = web3.currentProvider.sendAsync;
delete web3.currentProvider.sendAsync;
}

providers.givenProvider = web3.currentProvider;
}
/* jshint ignore:end */



/**
Expand All @@ -63,43 +36,31 @@ if(typeof ethereumProvider !== 'undefined') {
* Default poll timeout is 1 second
* Singleton
*/
var RequestManager = function (provider) {
var RequestManager = function RequestManager(provider) {
this.provider = null;
this.providers = providers;

this.setProvider(provider);
this.subscriptions = {};
};

// expose providers
RequestManager.providers = providers;

/**
* Should be used to synchronously send request
*
* @method send
* @param {Object} data
* @return {Object}
*/
RequestManager.prototype.sendSync = function (data) {
if (!this.provider) {
console.error(errors.InvalidProvider());
return null;
}
// ADD GIVEN PROVIDER
/* jshint ignore:start */
var global = Function('return this')();

var payload = Jsonrpc.toPayload(data.method, data.params);
var result = this.provider.sendSync(payload);
if(typeof global.ethereumProvider !== 'undefined') {
RequestManager.givenProvider = global.ethereumProvider;

if (result && result.error) {
throw errors.ErrorResponse(result);
} else if(typeof global.web3 !== 'undefined' && global.web3.currentProvider) {
if(global.web3.currentProvider.sendAsync) {
global.web3.currentProvider.send = global.web3.currentProvider.sendAsync;
delete global.web3.currentProvider.sendAsync;
}

if (!Jsonrpc.isValidResponse(result)) {
throw errors.InvalidResponse(result);
}
RequestManager.givenProvider = global.web3.currentProvider;
}
/* jshint ignore:end */

return result.result;
};

/**
* Should be used to asynchronously send request
Expand All @@ -117,7 +78,7 @@ RequestManager.prototype.send = function (data, callback) {

var payload = Jsonrpc.toPayload(data.method, data.params);
this.provider.send(payload, function (err, result) {
if(payload.id !== result.id) return callback(new Error('Wrong response id "'+ result.id +'" (expected: "'+ payload.id +'") in '+ JSON.stringify(payload)));
if(result.id && payload.id !== result.id) return callback(new Error('Wrong response id "'+ result.id +'" (expected: "'+ payload.id +'") in '+ JSON.stringify(payload)));

if (err) {
return callback(err);
Expand Down
2 changes: 1 addition & 1 deletion packages/web3-core-subscriptions/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
var Subscription = require('./subscription.js');


var Subscriptions = function (options) {
var Subscriptions = function Subscriptions(options) {
this.name = options.name;
this.type = options.type;
this.subscriptions = options.subscriptions || {};
Expand Down
Loading

0 comments on commit b1cce88

Please sign in to comment.