amqplib 0.10.8 → 1.0.0
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.
- package/.claude/commands/lint-disable.md +9 -0
- package/.claude/commands/lint-enable.md +12 -0
- package/.claude/settings.local.json +16 -0
- package/.github/workflows/publish.yml +1 -1
- package/.github/workflows/test.yml +1 -1
- package/CHANGELOG.md +34 -0
- package/CLAUDE.md +12 -0
- package/Makefile +5 -12
- package/bin/generate-defs.js +382 -369
- package/biome.json +51 -0
- package/callback_api.js +12 -8
- package/channel_api.js +5 -10
- package/examples/direct_reply_to_client.js +9 -5
- package/examples/direct_reply_to_server.js +8 -6
- package/examples/headers.js +13 -10
- package/examples/receive_generator.js +13 -20
- package/examples/send_generators.js +2 -9
- package/examples/ssl.js +2 -3
- package/examples/stream_queues/receive_stream.js +39 -36
- package/examples/stream_queues/send_stream.js +26 -29
- package/examples/tutorials/callback_api/emit_log.js +4 -3
- package/examples/tutorials/callback_api/emit_log_direct.js +5 -4
- package/examples/tutorials/callback_api/emit_log_topic.js +5 -4
- package/examples/tutorials/callback_api/new_task.js +5 -4
- package/examples/tutorials/callback_api/receive.js +16 -10
- package/examples/tutorials/callback_api/receive_logs.js +17 -11
- package/examples/tutorials/callback_api/receive_logs_direct.js +19 -13
- package/examples/tutorials/callback_api/receive_logs_topic.js +19 -14
- package/examples/tutorials/callback_api/rpc_client.js +20 -16
- package/examples/tutorials/callback_api/rpc_server.js +27 -21
- package/examples/tutorials/callback_api/send.js +4 -3
- package/examples/tutorials/callback_api/worker.js +18 -13
- package/examples/tutorials/emit_log.js +4 -6
- package/examples/tutorials/emit_log_direct.js +5 -7
- package/examples/tutorials/emit_log_topic.js +5 -7
- package/examples/tutorials/new_task.js +4 -6
- package/examples/tutorials/receive.js +8 -4
- package/examples/tutorials/receive_logs.js +10 -6
- package/examples/tutorials/receive_logs_direct.js +16 -10
- package/examples/tutorials/receive_logs_topic.js +16 -11
- package/examples/tutorials/rpc_client.js +17 -15
- package/examples/tutorials/rpc_server.js +9 -8
- package/examples/tutorials/send.js +4 -6
- package/examples/tutorials/worker.js +17 -14
- package/examples/waitForConfirms.js +2 -2
- package/lefthook.yml +14 -0
- package/lib/api_args.js +124 -149
- package/lib/bitset.js +28 -18
- package/lib/callback_model.js +134 -190
- package/lib/channel.js +175 -213
- package/lib/channel_model.js +41 -85
- package/lib/codec.js +257 -228
- package/lib/connect.js +74 -82
- package/lib/connection.js +204 -240
- package/lib/credentials.js +21 -29
- package/lib/defs.js +2559 -1927
- package/lib/error.js +5 -6
- package/lib/format.js +15 -33
- package/lib/frame.js +66 -66
- package/lib/heartbeat.js +17 -24
- package/lib/mux.js +32 -41
- package/package.json +8 -5
- package/test/bitset.test.js +65 -0
- package/test/callback_api.test.js +438 -0
- package/test/channel.test.js +592 -0
- package/test/channel_api.test.js +537 -0
- package/test/codec.test.js +172 -0
- package/test/connect.test.js +196 -0
- package/test/connection.test.js +330 -0
- package/test/data.test.js +33 -0
- package/test/frame.test.js +180 -0
- package/test/lib/data.js +249 -0
- package/test/lib/util.js +223 -0
- package/test/mux.test.js +215 -0
- package/test/bitset.js +0 -69
- package/test/callback_api.js +0 -379
- package/test/channel.js +0 -621
- package/test/channel_api.js +0 -606
- package/test/codec.js +0 -237
- package/test/connect.js +0 -197
- package/test/connection.js +0 -390
- package/test/data.js +0 -269
- package/test/frame.js +0 -191
- package/test/mux.js +0 -203
- package/test/util.js +0 -217
package/test/frame.js
DELETED
|
@@ -1,191 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var assert = require('assert');
|
|
4
|
-
var succeed = require('./util').succeed;
|
|
5
|
-
var fail = require('./util').fail;
|
|
6
|
-
var connection = require('../lib/connection');
|
|
7
|
-
var Frames = connection.Connection;
|
|
8
|
-
var HEARTBEAT = require('../lib/frame').HEARTBEAT;
|
|
9
|
-
var Stream = require('stream');
|
|
10
|
-
var PassThrough = Stream.PassThrough;
|
|
11
|
-
|
|
12
|
-
var defs = require('../lib/defs');
|
|
13
|
-
|
|
14
|
-
// We'll need to supply a stream which we manipulate ourselves
|
|
15
|
-
|
|
16
|
-
function inputs() {
|
|
17
|
-
// don't coalesce buffers, since that could mess up properties
|
|
18
|
-
// (e.g., encoded frame size)
|
|
19
|
-
return new PassThrough({objectMode: true});
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
var HB = Buffer.from([defs.constants.FRAME_HEARTBEAT,
|
|
23
|
-
0, 0, // channel 0
|
|
24
|
-
0, 0, 0, 0, // zero size
|
|
25
|
-
defs.constants.FRAME_END]);
|
|
26
|
-
|
|
27
|
-
suite("Explicit parsing", function() {
|
|
28
|
-
|
|
29
|
-
test('Parse heartbeat', function() {
|
|
30
|
-
var input = inputs();
|
|
31
|
-
var frames = new Frames(input);
|
|
32
|
-
input.write(HB);
|
|
33
|
-
assert(frames.recvFrame() === HEARTBEAT);
|
|
34
|
-
assert(!frames.recvFrame());
|
|
35
|
-
});
|
|
36
|
-
|
|
37
|
-
test('Parse partitioned', function() {
|
|
38
|
-
var input = inputs();
|
|
39
|
-
var frames = new Frames(input);
|
|
40
|
-
input.write(HB.subarray(0, 3));
|
|
41
|
-
assert(!frames.recvFrame());
|
|
42
|
-
input.write(HB.subarray(3));
|
|
43
|
-
assert(frames.recvFrame() === HEARTBEAT);
|
|
44
|
-
assert(!frames.recvFrame());
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
function testBogusFrame(name, bytes) {
|
|
48
|
-
test(name, function(done) {
|
|
49
|
-
var input = inputs();
|
|
50
|
-
var frames = new Frames(input);
|
|
51
|
-
frames.frameMax = 5; //for the max frame test
|
|
52
|
-
input.write(Buffer.from(bytes));
|
|
53
|
-
frames.step(function(err, frame) {
|
|
54
|
-
if (err != null) done();
|
|
55
|
-
else done(new Error('Was a bogus frame!'));
|
|
56
|
-
});
|
|
57
|
-
});
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
testBogusFrame('Wrong sized frame',
|
|
61
|
-
[defs.constants.FRAME_BODY,
|
|
62
|
-
0,0, 0,0,0,0, // zero length
|
|
63
|
-
65, // but a byte!
|
|
64
|
-
defs.constants.FRAME_END]);
|
|
65
|
-
|
|
66
|
-
testBogusFrame('Unknown method frame',
|
|
67
|
-
[defs.constants.FRAME_METHOD,
|
|
68
|
-
0,0, 0,0,0,4,
|
|
69
|
-
0,0,0,0, // garbage ID
|
|
70
|
-
defs.constants.FRAME_END]);
|
|
71
|
-
|
|
72
|
-
});
|
|
73
|
-
|
|
74
|
-
// Now for a bit more fun.
|
|
75
|
-
|
|
76
|
-
var amqp = require('./data');
|
|
77
|
-
var claire = require('claire');
|
|
78
|
-
var choice = claire.choice;
|
|
79
|
-
var forAll = claire.forAll;
|
|
80
|
-
var repeat = claire.repeat;
|
|
81
|
-
var label = claire.label;
|
|
82
|
-
var sequence = claire.sequence;
|
|
83
|
-
var transform = claire.transform;
|
|
84
|
-
var sized = claire.sized;
|
|
85
|
-
|
|
86
|
-
var assertEqualModuloDefaults =
|
|
87
|
-
require('./codec').assertEqualModuloDefaults;
|
|
88
|
-
|
|
89
|
-
var Trace = label('frame trace',
|
|
90
|
-
repeat(choice.apply(choice, amqp.methods)));
|
|
91
|
-
|
|
92
|
-
suite("Parsing", function() {
|
|
93
|
-
|
|
94
|
-
function testPartitioning(partition) {
|
|
95
|
-
return forAll(Trace).satisfy(function(t) {
|
|
96
|
-
var bufs = [];
|
|
97
|
-
var input = inputs();
|
|
98
|
-
var frames = new Frames(input);
|
|
99
|
-
var i = 0, ex;
|
|
100
|
-
frames.accept = function(f) {
|
|
101
|
-
// A minor hack to make sure we get the assertion exception;
|
|
102
|
-
// otherwise, it's just a test that we reached the line
|
|
103
|
-
// incrementing `i` for each frame.
|
|
104
|
-
try {
|
|
105
|
-
assertEqualModuloDefaults(t[i], f.fields);
|
|
106
|
-
}
|
|
107
|
-
catch (e) {
|
|
108
|
-
ex = e;
|
|
109
|
-
}
|
|
110
|
-
i++;
|
|
111
|
-
};
|
|
112
|
-
|
|
113
|
-
t.forEach(function(f) {
|
|
114
|
-
f.channel = 0;
|
|
115
|
-
bufs.push(defs.encodeMethod(f.id, 0, f.fields));
|
|
116
|
-
});
|
|
117
|
-
|
|
118
|
-
partition(bufs).forEach(function (chunk) { input.write(chunk); });
|
|
119
|
-
frames.acceptLoop();
|
|
120
|
-
if (ex) throw ex;
|
|
121
|
-
return i === t.length;
|
|
122
|
-
}).asTest({times: 20})
|
|
123
|
-
};
|
|
124
|
-
|
|
125
|
-
test("Parse trace of methods",
|
|
126
|
-
testPartitioning(function(bufs) { return bufs; }));
|
|
127
|
-
|
|
128
|
-
test("Parse concat'd methods",
|
|
129
|
-
testPartitioning(function(bufs) {
|
|
130
|
-
return [Buffer.concat(bufs)];
|
|
131
|
-
}));
|
|
132
|
-
|
|
133
|
-
test("Parse partitioned methods",
|
|
134
|
-
testPartitioning(function(bufs) {
|
|
135
|
-
var full = Buffer.concat(bufs);
|
|
136
|
-
var onethird = Math.floor(full.length / 3);
|
|
137
|
-
var twothirds = 2 * onethird;
|
|
138
|
-
return [
|
|
139
|
-
full.subarray(0, onethird),
|
|
140
|
-
full.subarray(onethird, twothirds),
|
|
141
|
-
full.subarray(twothirds)
|
|
142
|
-
];
|
|
143
|
-
}));
|
|
144
|
-
});
|
|
145
|
-
|
|
146
|
-
var FRAME_MAX_MAX = 4096 * 4;
|
|
147
|
-
var FRAME_MAX_MIN = 4096;
|
|
148
|
-
|
|
149
|
-
var FrameMax = amqp.rangeInt('frame max',
|
|
150
|
-
FRAME_MAX_MIN,
|
|
151
|
-
FRAME_MAX_MAX);
|
|
152
|
-
|
|
153
|
-
var Body = sized(function(_n) {
|
|
154
|
-
return Math.floor(Math.random() * FRAME_MAX_MAX);
|
|
155
|
-
}, repeat(amqp.Octet));
|
|
156
|
-
|
|
157
|
-
var Content = transform(function(args) {
|
|
158
|
-
return {
|
|
159
|
-
method: args[0].fields,
|
|
160
|
-
header: args[1].fields,
|
|
161
|
-
body: Buffer.from(args[2])
|
|
162
|
-
}
|
|
163
|
-
}, sequence(amqp.methods['BasicDeliver'],
|
|
164
|
-
amqp.properties['BasicProperties'], Body));
|
|
165
|
-
|
|
166
|
-
suite("Content framing", function() {
|
|
167
|
-
test("Adhere to frame max",
|
|
168
|
-
forAll(Content, FrameMax).satisfy(function(content, max) {
|
|
169
|
-
var input = inputs();
|
|
170
|
-
var frames = new Frames(input);
|
|
171
|
-
frames.frameMax = max;
|
|
172
|
-
frames.sendMessage(
|
|
173
|
-
0,
|
|
174
|
-
defs.BasicDeliver, content.method,
|
|
175
|
-
defs.BasicProperties, content.header,
|
|
176
|
-
content.body);
|
|
177
|
-
var f, i = 0, largest = 0;
|
|
178
|
-
while (f = input.read()) {
|
|
179
|
-
i++;
|
|
180
|
-
if (f.length > largest) largest = f.length;
|
|
181
|
-
if (f.length > max) {
|
|
182
|
-
return false;
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
// The ratio of frames to 'contents' should always be >= 2
|
|
186
|
-
// (one properties frame and at least one content frame); > 2
|
|
187
|
-
// indicates fragmentation. The largest is always, of course <= frame max
|
|
188
|
-
//console.log('Frames: %d; frames per message: %d; largest frame %d', i, i / t.length, largest);
|
|
189
|
-
return true;
|
|
190
|
-
}).asTest());
|
|
191
|
-
});
|
package/test/mux.js
DELETED
|
@@ -1,203 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var assert = require('assert');
|
|
4
|
-
var Mux = require('../lib/mux').Mux;
|
|
5
|
-
var PassThrough = require('stream').PassThrough;
|
|
6
|
-
|
|
7
|
-
var latch = require('./util').latch;
|
|
8
|
-
var schedule = require('./util').schedule;
|
|
9
|
-
|
|
10
|
-
function stream() {
|
|
11
|
-
return new PassThrough({objectMode: true});
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
function readAllObjects(s, cb) {
|
|
15
|
-
var objs = [];
|
|
16
|
-
|
|
17
|
-
function read() {
|
|
18
|
-
var v = s.read();
|
|
19
|
-
while (v !== null) {
|
|
20
|
-
objs.push(v);
|
|
21
|
-
v = s.read();
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
s.on('end', function() { cb(objs); });
|
|
26
|
-
s.on('readable', read);
|
|
27
|
-
|
|
28
|
-
read();
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
test("single input", function(done) {
|
|
32
|
-
var input = stream();
|
|
33
|
-
var output = stream();
|
|
34
|
-
input.on('end', function() { output.end() });
|
|
35
|
-
|
|
36
|
-
var mux = new Mux(output);
|
|
37
|
-
mux.pipeFrom(input);
|
|
38
|
-
|
|
39
|
-
var data = [1,2,3,4,5,6,7,8,9];
|
|
40
|
-
// not 0, it's treated specially by PassThrough for some reason. By
|
|
41
|
-
// 'specially' I mean it breaks the stream. See e.g.,
|
|
42
|
-
// https://github.com/isaacs/readable-stream/pull/55
|
|
43
|
-
data.forEach(function (chunk) { input.write(chunk); });
|
|
44
|
-
|
|
45
|
-
readAllObjects(output, function(vals) {
|
|
46
|
-
assert.deepEqual(data, vals);
|
|
47
|
-
done();
|
|
48
|
-
});
|
|
49
|
-
|
|
50
|
-
input.end();
|
|
51
|
-
});
|
|
52
|
-
|
|
53
|
-
test("single input, resuming stream", function(done) {
|
|
54
|
-
var input = stream();
|
|
55
|
-
var output = stream();
|
|
56
|
-
input.on('end', function() { output.end() });
|
|
57
|
-
|
|
58
|
-
var mux = new Mux(output);
|
|
59
|
-
mux.pipeFrom(input);
|
|
60
|
-
|
|
61
|
-
// Streams might be blocked and become readable again, simulate this
|
|
62
|
-
// using a special read function and a marker
|
|
63
|
-
var data = [1,2,3,4,'skip',6,7,8,9];
|
|
64
|
-
|
|
65
|
-
var oldRead = input.read;
|
|
66
|
-
input.read = function(size) {
|
|
67
|
-
var val = oldRead.call(input, size)
|
|
68
|
-
|
|
69
|
-
if (val === 'skip') {
|
|
70
|
-
input.emit('readable');
|
|
71
|
-
return null
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
return val;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
data.forEach(function (chunk) { input.write(chunk); });
|
|
78
|
-
|
|
79
|
-
readAllObjects(output, function(vals) {
|
|
80
|
-
assert.deepEqual([1,2,3,4,6,7,8,9], vals);
|
|
81
|
-
done();
|
|
82
|
-
});
|
|
83
|
-
|
|
84
|
-
input.end();
|
|
85
|
-
});
|
|
86
|
-
|
|
87
|
-
test("two sequential inputs", function(done) {
|
|
88
|
-
var input1 = stream();
|
|
89
|
-
var input2 = stream();
|
|
90
|
-
var output = stream();
|
|
91
|
-
var mux = new Mux(output);
|
|
92
|
-
mux.pipeFrom(input1);
|
|
93
|
-
mux.pipeFrom(input2);
|
|
94
|
-
|
|
95
|
-
var data = [1,2,3,4,5,6,7,8,9];
|
|
96
|
-
data.forEach(function(v) { input1.write(v); });
|
|
97
|
-
|
|
98
|
-
input1.on('end', function() {
|
|
99
|
-
data.forEach(function (v) { input2.write(v); });
|
|
100
|
-
input2.end();
|
|
101
|
-
});
|
|
102
|
-
input2.on('end', function() { output.end(); });
|
|
103
|
-
|
|
104
|
-
input1.end();
|
|
105
|
-
readAllObjects(output, function(vs) {
|
|
106
|
-
assert.equal(2 * data.length, vs.length);
|
|
107
|
-
done();
|
|
108
|
-
});
|
|
109
|
-
});
|
|
110
|
-
|
|
111
|
-
test("two interleaved inputs", function(done) {
|
|
112
|
-
var input1 = stream();
|
|
113
|
-
var input2 = stream();
|
|
114
|
-
var output = stream();
|
|
115
|
-
var mux = new Mux(output);
|
|
116
|
-
mux.pipeFrom(input1);
|
|
117
|
-
mux.pipeFrom(input2);
|
|
118
|
-
|
|
119
|
-
var endLatch = latch(2, function() { output.end(); });
|
|
120
|
-
input1.on('end', endLatch);
|
|
121
|
-
input2.on('end', endLatch);
|
|
122
|
-
|
|
123
|
-
var data = [1,2,3,4,5,6,7,8,9];
|
|
124
|
-
data.forEach(function(v) { input1.write(v); });
|
|
125
|
-
input1.end();
|
|
126
|
-
|
|
127
|
-
data.forEach(function(v) { input2.write(v); });
|
|
128
|
-
input2.end();
|
|
129
|
-
|
|
130
|
-
readAllObjects(output, function(vs) {
|
|
131
|
-
assert.equal(2 * data.length, vs.length);
|
|
132
|
-
done();
|
|
133
|
-
});
|
|
134
|
-
});
|
|
135
|
-
|
|
136
|
-
test("unpipe", function(done) {
|
|
137
|
-
var input = stream();
|
|
138
|
-
var output = stream();
|
|
139
|
-
var mux = new Mux(output);
|
|
140
|
-
|
|
141
|
-
var pipedData = [1,2,3,4,5];
|
|
142
|
-
var unpipedData = [6,7,8,9];
|
|
143
|
-
|
|
144
|
-
mux.pipeFrom(input);
|
|
145
|
-
|
|
146
|
-
schedule(function() {
|
|
147
|
-
pipedData.forEach(function (chunk) { input.write(chunk); });
|
|
148
|
-
|
|
149
|
-
schedule(function() {
|
|
150
|
-
mux.unpipeFrom(input);
|
|
151
|
-
|
|
152
|
-
schedule(function() {
|
|
153
|
-
unpipedData.forEach(function(chunk) { input.write(chunk); });
|
|
154
|
-
input.end();
|
|
155
|
-
schedule(function() {
|
|
156
|
-
// exhaust so that 'end' fires
|
|
157
|
-
var v; while (v = input.read());
|
|
158
|
-
});
|
|
159
|
-
});
|
|
160
|
-
});
|
|
161
|
-
|
|
162
|
-
});
|
|
163
|
-
|
|
164
|
-
input.on('end', function() {
|
|
165
|
-
output.end();
|
|
166
|
-
});
|
|
167
|
-
|
|
168
|
-
readAllObjects(output, function(vals) {
|
|
169
|
-
try {
|
|
170
|
-
assert.deepEqual(pipedData, vals);
|
|
171
|
-
done();
|
|
172
|
-
}
|
|
173
|
-
catch (e) { done(e); }
|
|
174
|
-
});
|
|
175
|
-
});
|
|
176
|
-
|
|
177
|
-
test("roundrobin", function(done) {
|
|
178
|
-
var input1 = stream();
|
|
179
|
-
var input2 = stream();
|
|
180
|
-
var output = stream();
|
|
181
|
-
var mux = new Mux(output);
|
|
182
|
-
|
|
183
|
-
mux.pipeFrom(input1);
|
|
184
|
-
mux.pipeFrom(input2);
|
|
185
|
-
|
|
186
|
-
var endLatch = latch(2, function() { output.end(); });
|
|
187
|
-
input1.on('end', endLatch);
|
|
188
|
-
input2.on('end', endLatch);
|
|
189
|
-
|
|
190
|
-
var ones = [1,1,1,1,1];
|
|
191
|
-
ones.forEach(function(v) { input1.write(v); });
|
|
192
|
-
input1.end();
|
|
193
|
-
|
|
194
|
-
var twos = [2,2,2,2,2];
|
|
195
|
-
twos.forEach(function(v) { input2.write(v); });
|
|
196
|
-
input2.end();
|
|
197
|
-
|
|
198
|
-
readAllObjects(output, function(vs) {
|
|
199
|
-
assert.deepEqual([1,2,1,2,1,2,1,2,1,2], vs);
|
|
200
|
-
done();
|
|
201
|
-
});
|
|
202
|
-
|
|
203
|
-
});
|
package/test/util.js
DELETED
|
@@ -1,217 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var crypto = require('crypto');
|
|
4
|
-
var Connection = require('../lib/connection').Connection;
|
|
5
|
-
var PassThrough = require('stream').PassThrough;
|
|
6
|
-
var defs = require('../lib/defs');
|
|
7
|
-
var assert = require('assert');
|
|
8
|
-
|
|
9
|
-
var schedule = (typeof setImmediate === 'function') ?
|
|
10
|
-
setImmediate : process.nextTick;
|
|
11
|
-
|
|
12
|
-
function randomString() {
|
|
13
|
-
var hash = crypto.createHash('sha1');
|
|
14
|
-
hash.update(crypto.randomBytes(64));
|
|
15
|
-
return hash.digest('base64');
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
// Set up a socket pair {client, server}, such that writes to the
|
|
20
|
-
// client are readable from the server, and writes to the server are
|
|
21
|
-
// readable at the client.
|
|
22
|
-
//
|
|
23
|
-
// +---+ +---+
|
|
24
|
-
// | C | | S |
|
|
25
|
-
// --write->| l |----->| e |--read-->
|
|
26
|
-
// | i | | r |
|
|
27
|
-
// <--read--| e |<-----| v |<-write--
|
|
28
|
-
// | n | | e |
|
|
29
|
-
// | t | | r |
|
|
30
|
-
// +---+ +---+
|
|
31
|
-
//
|
|
32
|
-
// I also need to make sure that end called on either socket affects
|
|
33
|
-
// the other.
|
|
34
|
-
|
|
35
|
-
function socketPair() {
|
|
36
|
-
var server = new PassThrough();
|
|
37
|
-
var client = new PassThrough();
|
|
38
|
-
server.write = client.push.bind(client);
|
|
39
|
-
client.write = server.push.bind(server);
|
|
40
|
-
function end(chunk, encoding) {
|
|
41
|
-
if (chunk) this.push(chunk, encoding);
|
|
42
|
-
this.push(null);
|
|
43
|
-
}
|
|
44
|
-
server.end = end.bind(client);
|
|
45
|
-
client.end = end.bind(server);
|
|
46
|
-
|
|
47
|
-
return {client: client, server: server};
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
function runServer(socket, run) {
|
|
51
|
-
var frames = new Connection(socket);
|
|
52
|
-
// We will be closing the socket without doing a closing handshake,
|
|
53
|
-
// so cheat
|
|
54
|
-
frames.expectSocketClose = true;
|
|
55
|
-
// We also need to create some channel buffers, again a cheat
|
|
56
|
-
frames.freshChannel(null);
|
|
57
|
-
frames.freshChannel(null);
|
|
58
|
-
frames.freshChannel(null);
|
|
59
|
-
|
|
60
|
-
function send(id, fields, channel, content) {
|
|
61
|
-
channel = channel || 0;
|
|
62
|
-
if (content) {
|
|
63
|
-
schedule(function() {
|
|
64
|
-
frames.sendMessage(channel, id, fields,
|
|
65
|
-
defs.BasicProperties, fields,
|
|
66
|
-
content);
|
|
67
|
-
});
|
|
68
|
-
}
|
|
69
|
-
else {
|
|
70
|
-
schedule(function() {
|
|
71
|
-
frames.sendMethod(channel, id, fields);
|
|
72
|
-
});
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
function wait(method) {
|
|
77
|
-
return function() {
|
|
78
|
-
return new Promise(function(resolve, reject) {
|
|
79
|
-
if (method) {
|
|
80
|
-
frames.step(function(e, f) {
|
|
81
|
-
if (e !== null) return reject(e);
|
|
82
|
-
if (f.id === method)
|
|
83
|
-
resolve(f);
|
|
84
|
-
else
|
|
85
|
-
reject(new Error("Expected method: " + method +
|
|
86
|
-
", got " + f.id));
|
|
87
|
-
});
|
|
88
|
-
}
|
|
89
|
-
else {
|
|
90
|
-
frames.step(function(e, f) {
|
|
91
|
-
if (e !== null) return reject(e);
|
|
92
|
-
else resolve(f);
|
|
93
|
-
});
|
|
94
|
-
}
|
|
95
|
-
});
|
|
96
|
-
};
|
|
97
|
-
}
|
|
98
|
-
run(send, wait);
|
|
99
|
-
return frames;
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
// Produce a callback that will complete the test successfully
|
|
103
|
-
function succeed(done) {
|
|
104
|
-
return function() { done(); }
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
// Produce a callback that will complete the test successfully
|
|
108
|
-
// only if the value is an object, it has the specified
|
|
109
|
-
// attribute, and its value is equals to the expected value
|
|
110
|
-
function succeedIfAttributeEquals(attribute, value, done) {
|
|
111
|
-
return function(object) {
|
|
112
|
-
if (object && !(object instanceof Error) && value === object[attribute]) {
|
|
113
|
-
return done();
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
done(new Error(attribute + " is not equal to " + value));
|
|
117
|
-
};
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
// Produce a callback that will fail the test, given either an error
|
|
121
|
-
// (to be used as a failure continuation) or any other value (to be
|
|
122
|
-
// used as a success continuation when failure is expected)
|
|
123
|
-
function fail(done) {
|
|
124
|
-
return function(err) {
|
|
125
|
-
if (err instanceof Error) done(err);
|
|
126
|
-
else done(new Error("Expected to fail, instead got " + err.toString()));
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
// Create a function that will call done once it's been called itself
|
|
131
|
-
// `count` times. If it's called with an error value, it will
|
|
132
|
-
// immediately call done with that error value.
|
|
133
|
-
function latch(count, done) {
|
|
134
|
-
var awaiting = count;
|
|
135
|
-
var alive = true;
|
|
136
|
-
return function(err) {
|
|
137
|
-
if (err instanceof Error && alive) {
|
|
138
|
-
alive = false;
|
|
139
|
-
done(err);
|
|
140
|
-
}
|
|
141
|
-
else {
|
|
142
|
-
awaiting--;
|
|
143
|
-
if (awaiting === 0 && alive) {
|
|
144
|
-
alive = false;
|
|
145
|
-
done();
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
};
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
// Call a thunk with a continuation that will be called with an error
|
|
152
|
-
// if the thunk throws one, or nothing if it runs to completion.
|
|
153
|
-
function completes(thunk, done) {
|
|
154
|
-
try {
|
|
155
|
-
thunk();
|
|
156
|
-
done();
|
|
157
|
-
}
|
|
158
|
-
catch (e) { done(e); }
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
// Construct a Node.JS-style callback from a success continuation and
|
|
162
|
-
// an error continuation
|
|
163
|
-
function kCallback(k, ek) {
|
|
164
|
-
return function(err, val) {
|
|
165
|
-
if (err === null) k && k(val);
|
|
166
|
-
else ek && ek(err);
|
|
167
|
-
};
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
// A noddy way to make tests depend on the node version.
|
|
171
|
-
function versionGreaterThan(actual, spec) {
|
|
172
|
-
|
|
173
|
-
function int(e) { return parseInt(e); }
|
|
174
|
-
|
|
175
|
-
var version = actual.split('.').map(int);
|
|
176
|
-
var desired = spec.split('.').map(int);
|
|
177
|
-
for (var i=0; i < desired.length; i++) {
|
|
178
|
-
var a = version[i], b = desired[i];
|
|
179
|
-
if (a != b) return a > b;
|
|
180
|
-
}
|
|
181
|
-
return false;
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
suite('versionGreaterThan', function() {
|
|
185
|
-
|
|
186
|
-
test('full spec', function() {
|
|
187
|
-
assert(versionGreaterThan('0.8.26', '0.6.12'));
|
|
188
|
-
assert(versionGreaterThan('0.8.26', '0.8.21'));
|
|
189
|
-
});
|
|
190
|
-
|
|
191
|
-
test('partial spec', function() {
|
|
192
|
-
assert(versionGreaterThan('0.9.12', '0.8'));
|
|
193
|
-
});
|
|
194
|
-
|
|
195
|
-
test('not greater', function() {
|
|
196
|
-
assert(!versionGreaterThan('0.8.12', '0.8.26'));
|
|
197
|
-
assert(!versionGreaterThan('0.6.2', '0.6.12'));
|
|
198
|
-
assert(!versionGreaterThan('0.8.29', '0.8'));
|
|
199
|
-
});
|
|
200
|
-
|
|
201
|
-
test
|
|
202
|
-
|
|
203
|
-
});
|
|
204
|
-
|
|
205
|
-
module.exports = {
|
|
206
|
-
socketPair: socketPair,
|
|
207
|
-
runServer: runServer,
|
|
208
|
-
succeed: succeed,
|
|
209
|
-
succeedIfAttributeEquals: succeedIfAttributeEquals,
|
|
210
|
-
fail: fail,
|
|
211
|
-
latch: latch,
|
|
212
|
-
completes: completes,
|
|
213
|
-
kCallback: kCallback,
|
|
214
|
-
schedule: schedule,
|
|
215
|
-
randomString: randomString,
|
|
216
|
-
versionGreaterThan: versionGreaterThan
|
|
217
|
-
};
|