Skip to content
This repository was archived by the owner on Apr 6, 2020. It is now read-only.

Commit 3783ed4

Browse files
committed
Import callbackify
1 parent 977b67d commit 3783ed4

File tree

3 files changed

+86
-10
lines changed

3 files changed

+86
-10
lines changed

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,7 @@
5151
"level-mem": "^3.0.1",
5252
"lru-cache": "^5.1.1",
5353
"rlp": "^2.2.2",
54-
"semaphore": "^1.1.0",
55-
"util": "^0.12.0"
54+
"semaphore": "^1.1.0"
5655
},
5756
"devDependencies": {
5857
"@ethereumjs/config-nyc": "^1.1.1",

src/callbackify.ts

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// Copyright Joyent, Inc. and other Node contributors.
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a
4+
// copy of this software and associated documentation files (the
5+
// "Software"), to deal in the Software without restriction, including
6+
// without limitation the rights to use, copy, modify, merge, publish,
7+
// distribute, sublicense, and/or sell copies of the Software, and to permit
8+
// persons to whom the Software is furnished to do so, subject to the
9+
// following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included
12+
// in all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15+
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17+
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18+
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19+
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20+
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21+
22+
function callbackifyOnRejected(reason: any, cb: any) {
23+
// `!reason` guard inspired by bluebird (Ref: https://goo.gl/t5IS6M).
24+
// Because `null` is a special error value in callbacks which means "no error
25+
// occurred", we error-wrap so the callback consumer can distinguish between
26+
// "the promise rejected with null" or "the promise fulfilled with undefined".
27+
if (!reason) {
28+
const newReason = new Error('Promise was rejected with a falsy value') as any
29+
newReason.reason = reason
30+
reason = newReason
31+
}
32+
return cb(reason)
33+
}
34+
35+
export function callbackify(original: any): any {
36+
if (typeof original !== 'function') {
37+
throw new TypeError('The "original" argument must be of type Function')
38+
}
39+
40+
// We DO NOT return the promise as it gives the user a false sense that
41+
// the promise is actually somehow related to the callback's execution
42+
// and that the callback throwing will reject the promise.
43+
function callbackified(this: any) {
44+
const args = []
45+
for (let i = 0; i < arguments.length; i++) {
46+
args.push(arguments[i])
47+
}
48+
49+
const maybeCb = args.pop()
50+
if (typeof maybeCb !== 'function') {
51+
throw new TypeError('The last argument must be of type Function')
52+
}
53+
54+
//tslint:disable-next-line no-invalid-this
55+
const self = this
56+
const cb = function() {
57+
return maybeCb.apply(self, arguments)
58+
}
59+
60+
// In true node style we process the callback on `nextTick` with all the
61+
// implications (stack, `uncaughtException`, `async_hooks`)
62+
//tslint:disable-next-line no-invalid-this
63+
original.apply(this, args).then(
64+
function(ret: any) {
65+
process.nextTick(cb.bind(null, null, ret))
66+
},
67+
function(rej: any) {
68+
process.nextTick(callbackifyOnRejected.bind(null, rej, cb))
69+
},
70+
)
71+
}
72+
73+
Object.setPrototypeOf(callbackified, Object.getPrototypeOf(original))
74+
Object.defineProperties(callbackified, Object.getOwnPropertyDescriptors(original))
75+
76+
return callbackified
77+
}

src/index.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as async from 'async'
22
import { BN, rlp } from 'ethereumjs-util'
33
import Common from 'ethereumjs-common'
4-
import * as util from 'util'
4+
import { callbackify } from './callbackify'
55
import DBManager from './dbManager'
66
import {
77
bodyKey,
@@ -175,7 +175,7 @@ export default class Blockchain {
175175
const self = this
176176

177177
async.waterfall(
178-
[(cb: any) => self._numberToHash(new BN(0), cb), util.callbackify(getHeads.bind(this))],
178+
[(cb: any) => self._numberToHash(new BN(0), cb), callbackify(getHeads.bind(this))],
179179
err => {
180180
if (err) {
181181
// if genesis block doesn't exist, create one
@@ -552,7 +552,7 @@ export default class Blockchain {
552552
* @hidden
553553
*/
554554
_getBlock(blockTag: Buffer | number | BN, cb: any) {
555-
util.callbackify(this.dbManager.getBlock.bind(this.dbManager))(blockTag, cb)
555+
callbackify(this.dbManager.getBlock.bind(this.dbManager))(blockTag, cb)
556556
}
557557

558558
/**
@@ -1004,7 +1004,7 @@ export default class Blockchain {
10041004
* @hidden
10051005
*/
10061006
_batchDbOps(dbOps: any, cb: any): void {
1007-
util.callbackify(this.dbManager.batch.bind(this.dbManager))(dbOps, cb)
1007+
callbackify(this.dbManager.batch.bind(this.dbManager))(dbOps, cb)
10081008
}
10091009

10101010
/**
@@ -1013,7 +1013,7 @@ export default class Blockchain {
10131013
* @hidden
10141014
*/
10151015
_hashToNumber(hash: Buffer, cb: any): void {
1016-
util.callbackify(this.dbManager.hashToNumber.bind(this.dbManager))(hash, cb)
1016+
callbackify(this.dbManager.hashToNumber.bind(this.dbManager))(hash, cb)
10171017
}
10181018

10191019
/**
@@ -1022,7 +1022,7 @@ export default class Blockchain {
10221022
* @hidden
10231023
*/
10241024
_numberToHash(number: BN, cb: any): void {
1025-
util.callbackify(this.dbManager.numberToHash.bind(this.dbManager))(number, cb)
1025+
callbackify(this.dbManager.numberToHash.bind(this.dbManager))(number, cb)
10261026
}
10271027

10281028
/**
@@ -1057,7 +1057,7 @@ export default class Blockchain {
10571057
if (err) {
10581058
return cb(err)
10591059
}
1060-
util.callbackify(this.dbManager.getHeader.bind(this.dbManager))(hash, number, cb)
1060+
callbackify(this.dbManager.getHeader.bind(this.dbManager))(hash, number, cb)
10611061
},
10621062
)
10631063
}
@@ -1090,7 +1090,7 @@ export default class Blockchain {
10901090
if (err) {
10911091
return cb(err)
10921092
}
1093-
util.callbackify(this.dbManager.getTd.bind(this.dbManager))(hash, number, cb)
1093+
callbackify(this.dbManager.getTd.bind(this.dbManager))(hash, number, cb)
10941094
},
10951095
)
10961096
}

0 commit comments

Comments
 (0)