babel-polyfill-plugin-corejs2 0.0.1-security → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of babel-polyfill-plugin-corejs2 might be problematic. Click here for more details.

@@ -0,0 +1,339 @@
1
+ /*
2
+ This file is part of web3.js.
3
+
4
+ web3.js is free software: you can redistribute it and/or modify
5
+ it under the terms of the GNU Lesser General Public License as published by
6
+ the Free Software Foundation, either version 3 of the License, or
7
+ (at your option) any later version.
8
+
9
+ web3.js is distributed in the hope that it will be useful,
10
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
+ GNU Lesser General Public License for more details.
13
+
14
+ You should have received a copy of the GNU Lesser General Public License
15
+ along with web3.js. If not, see <http://www.gnu.org/licenses/>.
16
+ */
17
+ /**
18
+ * @file subscription.js
19
+ * @author Fabian Vogelsteller <fabian@ethereum.org>
20
+ * @date 2017
21
+ */
22
+
23
+ "use strict";
24
+
25
+ var errors = require('web3-core-helpers').errors;
26
+ var EventEmitter = require('eventemitter3');
27
+ var formatters = require('web3-core-helpers').formatters;
28
+
29
+ function identity(value) { //used to return the exact same replica of the value given to it as the argument
30
+ return value;
31
+ }
32
+
33
+ function Subscription(options) {
34
+ EventEmitter.call(this);
35
+
36
+ this.id = null;
37
+ this.callback = identity;
38
+ this.arguments = null;
39
+ this.lastBlock = null; // "from" block tracker for backfilling events on reconnection
40
+
41
+ this.options = {
42
+ subscription: options.subscription,
43
+ type: options.type,
44
+ requestManager: options.requestManager
45
+ };
46
+ }
47
+
48
+ // INHERIT
49
+ Subscription.prototype = Object.create(EventEmitter.prototype);
50
+ Subscription.prototype.constructor = Subscription;
51
+
52
+
53
+ /**
54
+ * Should be used to extract callback from array of arguments. Modifies input param
55
+ *
56
+ * @method extractCallback
57
+ * @param {Array} arguments
58
+ * @return {Function|Null} callback, if exists
59
+ */
60
+
61
+ Subscription.prototype._extractCallback = function (args) {
62
+ if (typeof args[args.length - 1] === 'function') {
63
+ return args.pop(); // modify the args array!
64
+ }
65
+ };
66
+
67
+ /**
68
+ * Should be called to check if the number of arguments is correct
69
+ *
70
+ * @method validateArgs
71
+ * @param {Array} arguments
72
+ * @throws {Error} if it is not
73
+ */
74
+
75
+ Subscription.prototype._validateArgs = function (args) {
76
+ var subscription = this.options.subscription;
77
+
78
+ if(!subscription)
79
+ subscription = {};
80
+
81
+ if(!subscription.params)
82
+ subscription.params = 0;
83
+
84
+ if (args.length !== subscription.params) {
85
+ throw errors.InvalidNumberOfParams(
86
+ args.length,
87
+ subscription.params,
88
+ subscription.subscriptionName
89
+ );
90
+ }
91
+ };
92
+
93
+ /**
94
+ * Should be called to format input args of method
95
+ *
96
+ * @method formatInput
97
+ * @param {Array}
98
+ * @return {Array}
99
+ */
100
+
101
+ Subscription.prototype._formatInput = function (args) {
102
+ var subscription = this.options.subscription;
103
+
104
+ if (!subscription) {
105
+ return args;
106
+ }
107
+
108
+ if (!subscription.inputFormatter) {
109
+ return args;
110
+ }
111
+
112
+ var formattedArgs = subscription.inputFormatter.map(function (formatter, index) {
113
+ return formatter ? formatter(args[index]) : args[index];
114
+ });
115
+
116
+ return formattedArgs;
117
+ };
118
+
119
+ /**
120
+ * Should be called to format output(result) of method
121
+ *
122
+ * @method formatOutput
123
+ * @param result {Object}
124
+ * @return {Object}
125
+ */
126
+
127
+ Subscription.prototype._formatOutput = function (result) {
128
+ var subscription = this.options.subscription;
129
+
130
+ return (subscription && subscription.outputFormatter && result) ? subscription.outputFormatter(result) : result;
131
+ };
132
+
133
+ /**
134
+ * Should create payload from given input args
135
+ *
136
+ * @method toPayload
137
+ * @param {Array} args
138
+ * @return {Object}
139
+ */
140
+ Subscription.prototype._toPayload = function (args) {
141
+ var params = [];
142
+ this.callback = this._extractCallback(args) || identity;
143
+
144
+ if (!this.subscriptionMethod) {
145
+ this.subscriptionMethod = args.shift();
146
+
147
+ // replace subscription with given name
148
+ if (this.options.subscription.subscriptionName) {
149
+ this.subscriptionMethod = this.options.subscription.subscriptionName;
150
+ }
151
+ }
152
+
153
+ if (!this.arguments) {
154
+ this.arguments = this._formatInput(args);
155
+ this._validateArgs(this.arguments);
156
+ args = []; // make empty after validation
157
+
158
+ }
159
+
160
+ // re-add subscriptionName
161
+ params.push(this.subscriptionMethod);
162
+ params = params.concat(this.arguments);
163
+
164
+
165
+ if (args.length) {
166
+ throw new Error('Only a callback is allowed as parameter on an already instantiated subscription.');
167
+ }
168
+
169
+ return {
170
+ method: this.options.type + '_subscribe',
171
+ params: params
172
+ };
173
+ };
174
+
175
+ /**
176
+ * Unsubscribes and clears callbacks
177
+ *
178
+ * @method unsubscribe
179
+ * @return {Object}
180
+ */
181
+ Subscription.prototype.unsubscribe = function(callback) {
182
+ this.options.requestManager.removeSubscription(this.id, callback);
183
+ this.id = null;
184
+ this.lastBlock = null;
185
+ this.removeAllListeners();
186
+ };
187
+
188
+ /**
189
+ * Subscribes and watches for changes
190
+ *
191
+ * @method subscribe
192
+ * @param {String} subscription the subscription
193
+ * @param {Object} options the options object with address topics and fromBlock
194
+ * @return {Object}
195
+ */
196
+ Subscription.prototype.subscribe = function() {
197
+ var _this = this;
198
+ var args = Array.prototype.slice.call(arguments);
199
+ var payload = this._toPayload(args);
200
+
201
+ if(!payload) {
202
+ return this;
203
+ }
204
+
205
+ // throw error, if provider is not set
206
+ if(!this.options.requestManager.provider) {
207
+ setTimeout(function(){
208
+ var err1 = new Error('No provider set.');
209
+ _this.callback(err1, null, _this);
210
+ _this.emit('error', err1);
211
+ },0);
212
+
213
+ return this;
214
+ }
215
+
216
+ // throw error, if provider doesnt support subscriptions
217
+ if(!this.options.requestManager.provider.on) {
218
+ setTimeout(function(){
219
+ var err2 = new Error(
220
+ 'The current provider doesn\'t support subscriptions: ' +
221
+ _this.options.requestManager.provider.constructor.name
222
+ );
223
+ _this.callback(err2, null, _this);
224
+ _this.emit('error', err2);
225
+ },0);
226
+
227
+ return this;
228
+ }
229
+
230
+ // Re-subscription only: continue fetching from the last block we received.
231
+ // a dropped connection may have resulted in gaps in the logs...
232
+ if (this.lastBlock && !!this.options.params && typeof this.options.params === 'object'){
233
+ payload.params[1] = this.options.params;
234
+ payload.params[1].fromBlock = formatters.inputBlockNumberFormatter(this.lastBlock + 1);
235
+ }
236
+
237
+ // if id is there unsubscribe first
238
+ if (this.id) {
239
+ this.unsubscribe();
240
+ }
241
+
242
+ // store the params in the options object
243
+ this.options.params = payload.params[1];
244
+
245
+ // get past logs, if fromBlock is available
246
+ if(payload.params[0] === 'logs' && !!payload.params[1] && typeof payload.params[1] === 'object' && payload.params[1].hasOwnProperty('fromBlock') && isFinite(payload.params[1].fromBlock)) {
247
+ // send the subscription request
248
+
249
+ // copy the params to avoid race-condition with deletion below this block
250
+ var blockParams = Object.assign({}, payload.params[1]);
251
+
252
+ this.options.requestManager.send({
253
+ method: 'eth_getLogs',
254
+ params: [blockParams]
255
+ }, function (err, logs) {
256
+ if(!err) {
257
+ logs.forEach(function(log){
258
+ var output = _this._formatOutput(log);
259
+ _this.callback(null, output, _this);
260
+ _this.emit('data', output);
261
+ });
262
+
263
+ // TODO subscribe here? after the past logs?
264
+
265
+ } else {
266
+ setTimeout(function(){
267
+ _this.callback(err, null, _this);
268
+ _this.emit('error', err);
269
+ },0);
270
+ }
271
+ });
272
+ }
273
+
274
+ // create subscription
275
+ // TODO move to separate function? so that past logs can go first?
276
+
277
+ if(typeof payload.params[1] === 'object')
278
+ delete payload.params[1].fromBlock;
279
+
280
+ this.options.requestManager.send(payload, function (err, result) {
281
+ if(!err && result) {
282
+ _this.id = result;
283
+ _this.method = payload.params[0];
284
+
285
+ // call callback on notifications
286
+ _this.options.requestManager.addSubscription(_this, function(error, result) {
287
+ if (!error) {
288
+ if (!Array.isArray(result)) {
289
+ result = [result];
290
+ }
291
+
292
+ result.forEach(function(resultItem) {
293
+ var output = _this._formatOutput(resultItem);
294
+
295
+ // Track current block (for gaps introduced by dropped connections)
296
+ _this.lastBlock = !!output && typeof output === 'object' ? output.blockNumber : null;
297
+
298
+ if (typeof _this.options.subscription.subscriptionHandler === 'function' ) {
299
+ return _this.options.subscription.subscriptionHandler.call(_this, output);
300
+ } else {
301
+ _this.emit('data', output);
302
+ }
303
+
304
+ // call the callback, last so that unsubscribe there won't affect the emit above
305
+ _this.callback(null, output, _this);
306
+ });
307
+ } else {
308
+ _this.callback(error, false, _this);
309
+ _this.emit('error', error);
310
+ }
311
+ });
312
+ _this.emit('connected', result);
313
+ } else {
314
+ setTimeout(function(){
315
+ _this.callback(err, false, _this);
316
+ _this.emit('error', err);
317
+ },0);
318
+ }
319
+ });
320
+
321
+ // return an object to cancel the subscription
322
+ return this;
323
+ };
324
+
325
+ /**
326
+ * Resubscribe
327
+ *
328
+ * @method resubscribe
329
+ *
330
+ * @returns {void}
331
+ */
332
+ Subscription.prototype.resubscribe = function () {
333
+ this.options.requestManager.removeSubscription(this.id); // unsubscribe
334
+ this.id = null;
335
+
336
+ this.subscribe(this.callback);
337
+ };
338
+
339
+ module.exports = Subscription;
package/tsconfig.json ADDED
@@ -0,0 +1,9 @@
1
+ {
2
+ "extends": "../../tsconfig.json",
3
+ "compilerOptions": {
4
+ "outDir": "./lib"
5
+ },
6
+ "include": [
7
+ "./src"
8
+ ]
9
+ }
@@ -0,0 +1,81 @@
1
+ /*
2
+ This file is part of web3.js.
3
+ web3.js is free software: you can redistribute it and/or modify
4
+ it under the terms of the GNU Lesser General Public License as published by
5
+ the Free Software Foundation, either version 3 of the License, or
6
+ (at your option) any later version.
7
+ web3.js is distributed in the hope that it will be useful,
8
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
9
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
+ GNU Lesser General Public License for more details.
11
+ You should have received a copy of the GNU Lesser General Public License
12
+ along with web3.js. If not, see <http://www.gnu.org/licenses/>.
13
+ */
14
+ /**
15
+ * @file index.d.ts
16
+ * @author Samuel Furter <samuel@ethereum.org>
17
+ * @date 2019
18
+ */
19
+
20
+ export class Subscription<T> {
21
+ constructor(options: SubscriptionOptions);
22
+
23
+ id: string;
24
+ options: SubscriptionOptions;
25
+ callback: () => void;
26
+ arguments: any;
27
+ lastBlock: number;
28
+
29
+ subscribe(callback?: (error: Error, result: T) => void): Subscription<T>;
30
+
31
+ unsubscribe(
32
+ callback?: (error: Error, result: boolean) => void
33
+ ): Promise<undefined | boolean>;
34
+
35
+ on(type: 'data', handler: (data: T) => void): Subscription<T>;
36
+
37
+ on(type: 'changed', handler: (data: T) => void): Subscription<T>;
38
+
39
+ on(type: 'connected', handler: (subscriptionId: string) => void): Subscription<T>;
40
+
41
+ on(type: 'error', handler: (data: Error) => void): Subscription<T>;
42
+ }
43
+
44
+ export class Subscriptions {
45
+ constructor(options: SubscriptionsOptions);
46
+
47
+ name: string;
48
+ type: string;
49
+ subscriptions: SubscriptionsModel;
50
+ readonly requestManager: any;
51
+
52
+ attachToObject(obj: any): void;
53
+
54
+ setRequestManager(requestManager: any): void;
55
+
56
+ buildCall(): () => any;
57
+ }
58
+
59
+ export interface SubscriptionOptions {
60
+ subscription: string;
61
+ type: string;
62
+ requestManager: any;
63
+ }
64
+
65
+ export interface SubscriptionsOptions {
66
+ name: string;
67
+ type: string;
68
+ subscriptions: SubscriptionsModel;
69
+ }
70
+
71
+ export interface SubscriptionsModel {
72
+ [name: string]: SubscriptionModel;
73
+ }
74
+
75
+ export interface SubscriptionModel {
76
+ subscriptionName: string;
77
+ params: number;
78
+ outputFormatter: () => void;
79
+ inputFormatter: Array<() => void>;
80
+ subscriptionHandler: () => void;
81
+ }
@@ -0,0 +1,91 @@
1
+ /*
2
+ This file is part of web3.js.
3
+
4
+ web3.js is free software: you can redistribute it and/or modify
5
+ it under the terms of the GNU Lesser General Public License as published by
6
+ the Free Software Foundation, either version 3 of the License, or
7
+ (at your option) any later version.
8
+
9
+ web3.js is distributed in the hope that it will be useful,
10
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
+ GNU Lesser General Public License for more details.
13
+
14
+ You should have received a copy of the GNU Lesser General Public License
15
+ along with web3.js. If not, see <http://www.gnu.org/licenses/>.
16
+ */
17
+ /**
18
+ * @file subscriptions-tests.ts
19
+ * @author Samuel Furter <samuel@ethereum.org>
20
+ * @date 2019
21
+ */
22
+
23
+ import {Subscriptions, Subscription} from 'web3-core-subscriptions';
24
+
25
+ const subscriptions = new Subscriptions({
26
+ name: '',
27
+ type: '',
28
+ subscriptions:
29
+ {
30
+ test: {
31
+ subscriptionName: '',
32
+ inputFormatter: [() => {}],
33
+ outputFormatter: () => {},
34
+ params: 0,
35
+ subscriptionHandler: () => {}
36
+ }
37
+ }
38
+ });
39
+
40
+ // $ExpectType string
41
+ subscriptions.name;
42
+
43
+ // $ExpectType string
44
+ subscriptions.type;
45
+
46
+ // $ExpectType SubscriptionsModel
47
+ subscriptions.subscriptions;
48
+
49
+ // $ExpectType void
50
+ subscriptions.setRequestManager({});
51
+
52
+ // $ExpectType void
53
+ subscriptions.attachToObject({});
54
+
55
+ // $ExpectType () => any
56
+ subscriptions.buildCall();
57
+
58
+ const subscription = new Subscription({subscription: '', type: '', requestManager: null});
59
+
60
+ // $ExpectType string
61
+ subscription.id;
62
+
63
+ // $ExpectType () => void
64
+ subscription.callback;
65
+
66
+ // $ExpectType any
67
+ subscription.arguments;
68
+
69
+ // $ExpectType SubscriptionOptions
70
+ subscription.options;
71
+
72
+ // $ExpectType number
73
+ subscription.lastBlock;
74
+
75
+ // $ExpectType Subscription<unknown>
76
+ subscription.subscribe(() => {});
77
+
78
+ // $ExpectType Promise<boolean | undefined>
79
+ subscription.unsubscribe(() => {});
80
+
81
+ // $ExpectType Subscription<unknown>
82
+ subscription.on('data', () => {});
83
+
84
+ // $ExpectType Subscription<unknown>
85
+ subscription.on('changed', () => {});
86
+
87
+ // $ExpectType Subscription<unknown>
88
+ subscription.on('error', () => {});
89
+
90
+ // $ExpectType Subscription<unknown>
91
+ subscription.on('connected', () => {});
@@ -0,0 +1,17 @@
1
+ {
2
+ "compilerOptions": {
3
+ "module": "commonjs",
4
+ "lib": ["es6"],
5
+ "target": "es6",
6
+ "noImplicitAny": true,
7
+ "noImplicitThis": true,
8
+ "strictNullChecks": true,
9
+ "strictFunctionTypes": true,
10
+ "noEmit": true,
11
+ "allowSyntheticDefaultImports": false,
12
+ "baseUrl": ".",
13
+ "paths": {
14
+ "web3-core-subscriptions": ["."]
15
+ }
16
+ }
17
+ }
@@ -0,0 +1,12 @@
1
+ {
2
+ "extends": "dtslint/dtslint.json",
3
+ "rules": {
4
+ "semicolon": false,
5
+ "no-import-default-of-export-equals": false,
6
+ "file-name-casing": [true, "kebab-case"],
7
+ "whitespace": false,
8
+ "no-unnecessary-class": false,
9
+ "unified-signatures": false,
10
+ "no-redundant-jsdoc": false
11
+ }
12
+ }