Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 0d94a24

Browse filesBrowse files
authored
fix: remove eval (#30)
1 parent aa21cfb commit 0d94a24
Copy full SHA for 0d94a24

File tree

Expand file treeCollapse file tree

2 files changed

+54
-33
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+54
-33
lines changed

‎index.js

Copy file name to clipboardExpand all lines: index.js
+30-33Lines changed: 30 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -7,33 +7,34 @@ module.exports = thenify
77
/**
88
* Turn async functions into promises
99
*
10-
* @param {Function} $$__fn__$$
10+
* @param {Function} fn
1111
* @return {Function}
1212
* @api public
1313
*/
1414

15-
function thenify($$__fn__$$, options) {
16-
assert(typeof $$__fn__$$ === 'function')
17-
return eval(createWrapper($$__fn__$$.name, options))
15+
function thenify(fn, options) {
16+
assert(typeof fn === 'function')
17+
return createWrapper(fn, options)
1818
}
1919

2020
/**
2121
* Turn async functions into promises and backward compatible with callback
2222
*
23-
* @param {Function} $$__fn__$$
23+
* @param {Function} fn
2424
* @return {Function}
2525
* @api public
2626
*/
2727

28-
thenify.withCallback = function ($$__fn__$$, options) {
29-
assert(typeof $$__fn__$$ === 'function')
28+
thenify.withCallback = function (fn, options) {
29+
assert(typeof fn === 'function')
3030
options = options || {}
3131
options.withCallback = true
32-
if (options.multiArgs === undefined) options.multiArgs = true
33-
return eval(createWrapper($$__fn__$$.name, options))
32+
return createWrapper(fn, options)
3433
}
3534

3635
function createCallback(resolve, reject, multiArgs) {
36+
// default to true
37+
if (multiArgs === undefined) multiArgs = true
3738
return function(err, value) {
3839
if (err) return reject(err)
3940
var length = arguments.length
@@ -52,29 +53,25 @@ function createCallback(resolve, reject, multiArgs) {
5253
}
5354
}
5455

55-
function createWrapper(name, options) {
56-
name = (name || '').replace(/\s|bound(?!$)/g, '')
56+
function createWrapper(fn, options) {
5757
options = options || {}
58-
// default to true
59-
var multiArgs = options.multiArgs !== undefined ? options.multiArgs : true
60-
multiArgs = 'var multiArgs = ' + JSON.stringify(multiArgs) + '\n'
61-
62-
var withCallback = options.withCallback ?
63-
'var lastType = typeof arguments[len - 1]\n'
64-
+ 'if (lastType === "function") return $$__fn__$$.apply(self, arguments)\n'
65-
: ''
66-
67-
return '(function ' + name + '() {\n'
68-
+ 'var self = this\n'
69-
+ 'var len = arguments.length\n'
70-
+ multiArgs
71-
+ withCallback
72-
+ 'var args = new Array(len + 1)\n'
73-
+ 'for (var i = 0; i < len; ++i) args[i] = arguments[i]\n'
74-
+ 'var lastIndex = i\n'
75-
+ 'return new Promise(function (resolve, reject) {\n'
76-
+ 'args[lastIndex] = createCallback(resolve, reject, multiArgs)\n'
77-
+ '$$__fn__$$.apply(self, args)\n'
78-
+ '})\n'
79-
+ '})'
58+
var name = fn.name;
59+
name = (name || '').replace(/\s|bound(?!$)/g, '')
60+
var newFn = function () {
61+
var self = this
62+
var len = arguments.length
63+
if (options.withCallback) {
64+
var lastType = typeof arguments[len - 1]
65+
if (lastType === 'function') return fn.apply(self, arguments)
66+
}
67+
var args = new Array(len + 1)
68+
for (var i = 0; i < len; ++i) args[i] = arguments[i]
69+
var lastIndex = i
70+
return new Promise(function (resolve, reject) {
71+
args[lastIndex] = createCallback(resolve, reject, options.multiArgs)
72+
fn.apply(self, args)
73+
})
74+
}
75+
Object.defineProperty(newFn, 'name', { value: name })
76+
return newFn
8077
}

‎test/test.js

Copy file name to clipboardExpand all lines: test/test.js
+24Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,27 @@ it('fn(..args, callback())', function () {
6969
assert.deepEqual(values, [1, 2, 3])
7070
})
7171
})
72+
73+
it('unicode function name', function () {
74+
function 你好$hello_123(a, b, c, cb) {
75+
cb(null, a, b, c)
76+
}
77+
var wrapper = thenify(你好$hello_123)
78+
assert.equal(wrapper.name, '你好$hello_123')
79+
wrapper(1, 2, 3).then(function (values) {
80+
assert.deepEqual(values, [1, 2, 3])
81+
})
82+
})
83+
84+
it('invalid function name', function () {
85+
function fn(a, b, c, cb) {
86+
cb(null, a, b, c)
87+
}
88+
89+
Object.defineProperty(fn, 'name', { value: 'fake(){a.b;})();(function(){//' })
90+
var wrapper = thenify(fn)
91+
assert.equal(wrapper.name, fn.name)
92+
wrapper(1, 2, 3).then(function (values) {
93+
assert.deepEqual(values, [1, 2, 3])
94+
})
95+
})

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.