-
-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathcallbackify.js
228 lines (204 loc) · 6.5 KB
/
callbackify.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
'use strict';
// This test checks that the semantics of `util.callbackify` are as described in
// the API docs
var common = require('./common');
var assert = require('assert');
var callbackify = require('../../').callbackify;
var execFile = require('child_process').execFile;
var nodeJSVersion = require('../../support/nodeJSVersion');
var isFunctionLengthConfigurable = require("../../support/isFunctionLengthConfigurable");
(function callbackify_resulting_function_should_have_one_more_argument() {
if (!isFunctionLengthConfigurable()) {
console.log("skipping this test as function.length is not configurable in the current javascript engine");
return;
}
var nodeVersion = nodeJSVersion();
console.log("Testing callbackify resulting function should have one more argument")
var original_callbackify = require('util').callbackify;
// Test that resulting function should have one more argument
[
function () { },
function (a) { },
function (a, b) { }
].forEach(function (fct) {
var node_callbackified = original_callbackify(fct);
var browser_callbackified = callbackify(fct);
if (nodeVersion >= 12 && node_callbackified.length !== fct.length + 1) {
// this behavior is only true with node 12 and above, where the bug was fixed
throw new Error("callbackified function should have one more argument");
}
if (browser_callbackified.length !== node_callbackified.length) {
console.log("browser_callbackified=", browser_callbackified.length, "node_callbackified=", node_callbackified.length)
throw new Error("callbackified function should have one more argument, like in node");
}
});
})();
if (typeof Promise === 'undefined') {
console.log('no global Promise found, skipping callbackify tests');
return;
}
var values = [
'hello world',
null,
undefined,
false,
0,
{},
{ key: 'value' },
function ok() {},
['array', 'with', 4, 'values'],
new Error('boo')
];
if (typeof Symbol !== 'undefined') {
values.push(Symbol('I am a symbol'));
}
{
// Test that the resolution value is passed as second argument to callback
values.forEach(function(value) {
// Test Promise factory
function promiseFn() {
return Promise.resolve(value);
}
var cbPromiseFn = callbackify(promiseFn);
cbPromiseFn(common.mustCall(function(err, ret) {
assert.ifError(err);
assert.strictEqual(ret, value);
}));
// Test Thenable
function thenableFn() {
return {
then: function(onRes, onRej) {
onRes(value);
}
};
}
var cbThenableFn = callbackify(thenableFn);
cbThenableFn(common.mustCall(function(err, ret) {
assert.ifError(err);
assert.strictEqual(ret, value);
}));
});
}
{
// Test that rejection reason is passed as first argument to callback
values.forEach(function(value) {
// test a Promise factory
function promiseFn() {
return Promise.reject(value);
}
var cbPromiseFn = callbackify(promiseFn);
cbPromiseFn(common.mustCall(function(err, ret) {
assert.strictEqual(ret, undefined);
if (err instanceof Error) {
if ('reason' in err) {
assert(!value);
assert.strictEqual(err.message, 'Promise was rejected with a falsy value');
assert.strictEqual(err.reason, value);
} else {
assert.strictEqual(String(value).slice(-err.message.length), err.message);
}
} else {
assert.strictEqual(err, value);
}
}));
// Test Thenable
function thenableFn() {
return {
then: function (onRes, onRej) {
onRej(value);
}
};
}
var cbThenableFn = callbackify(thenableFn);
cbThenableFn(common.mustCall(function(err, ret) {
assert.strictEqual(ret, undefined);
if (err instanceof Error) {
if ('reason' in err) {
assert(!value);
assert.strictEqual(err.message, 'Promise was rejected with a falsy value');
assert.strictEqual(err.reason, value);
} else {
assert.strictEqual(String(value).slice(-err.message.length), err.message);
}
} else {
assert.strictEqual(err, value);
}
}));
});
}
{
// Test that arguments passed to callbackified function are passed to original
values.forEach(function(value) {
function promiseFn(arg) {
assert.strictEqual(arg, value);
return Promise.resolve(arg);
}
var cbPromiseFn = callbackify(promiseFn);
cbPromiseFn(value, common.mustCall(function(err, ret) {
assert.ifError(err);
assert.strictEqual(ret, value);
}));
});
}
{
// Test that `this` binding is the same for callbackified and original
values.forEach(function(value) {
var iAmThis = {
fn: function(arg) {
assert.strictEqual(this, iAmThis);
return Promise.resolve(arg);
},
};
iAmThis.cbFn = callbackify(iAmThis.fn);
iAmThis.cbFn(value, common.mustCall(function(err, ret) {
assert.ifError(err);
assert.strictEqual(ret, value);
assert.strictEqual(this, iAmThis);
}));
});
}
// These tests are not necessary in the browser.
if (false) {
// Test that callback that throws emits an `uncaughtException` event
var fixture = fixtures.path('uncaught-exceptions', 'callbackify1.js');
execFile(
process.execPath,
[fixture],
common.mustCall(function (err, stdout, stderr) {
assert.strictEqual(err.code, 1);
assert.strictEqual(Object.getPrototypeOf(err).name, 'Error');
assert.strictEqual(stdout, '');
var errLines = stderr.trim().split(/[\r\n]+/);
var errLine = errLines.find(function (l) { return /^Error/.exec(l) });
assert.strictEqual(errLine, 'Error: ' + fixture);
})
);
}
if (false) {
// Test that handled `uncaughtException` works and passes rejection reason
var fixture = fixtures.path('uncaught-exceptions', 'callbackify2.js');
execFile(
process.execPath,
[fixture],
common.mustCall(function (err, stdout, stderr) {
assert.ifError(err);
assert.strictEqual(stdout.trim(), fixture);
assert.strictEqual(stderr, '');
})
);
}
{
// Verify that non-function inputs throw.
['foo', null, undefined, false, 0, {}, typeof Symbol !== 'undefined' ? Symbol() : undefined, []].forEach(function(value) {
common.expectsError(function() {
callbackify(value);
}, {
code: 'ERR_INVALID_ARG_TYPE',
type: TypeError,
message: 'The "original" argument must be of type Function'
});
});
}
if (require('is-async-supported')()) {
require('./callbackify-async');
}