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.
Files changed (85) hide show
  1. package/.claude/commands/lint-disable.md +9 -0
  2. package/.claude/commands/lint-enable.md +12 -0
  3. package/.claude/settings.local.json +16 -0
  4. package/.github/workflows/publish.yml +1 -1
  5. package/.github/workflows/test.yml +1 -1
  6. package/CHANGELOG.md +34 -0
  7. package/CLAUDE.md +12 -0
  8. package/Makefile +5 -12
  9. package/bin/generate-defs.js +382 -369
  10. package/biome.json +51 -0
  11. package/callback_api.js +12 -8
  12. package/channel_api.js +5 -10
  13. package/examples/direct_reply_to_client.js +9 -5
  14. package/examples/direct_reply_to_server.js +8 -6
  15. package/examples/headers.js +13 -10
  16. package/examples/receive_generator.js +13 -20
  17. package/examples/send_generators.js +2 -9
  18. package/examples/ssl.js +2 -3
  19. package/examples/stream_queues/receive_stream.js +39 -36
  20. package/examples/stream_queues/send_stream.js +26 -29
  21. package/examples/tutorials/callback_api/emit_log.js +4 -3
  22. package/examples/tutorials/callback_api/emit_log_direct.js +5 -4
  23. package/examples/tutorials/callback_api/emit_log_topic.js +5 -4
  24. package/examples/tutorials/callback_api/new_task.js +5 -4
  25. package/examples/tutorials/callback_api/receive.js +16 -10
  26. package/examples/tutorials/callback_api/receive_logs.js +17 -11
  27. package/examples/tutorials/callback_api/receive_logs_direct.js +19 -13
  28. package/examples/tutorials/callback_api/receive_logs_topic.js +19 -14
  29. package/examples/tutorials/callback_api/rpc_client.js +20 -16
  30. package/examples/tutorials/callback_api/rpc_server.js +27 -21
  31. package/examples/tutorials/callback_api/send.js +4 -3
  32. package/examples/tutorials/callback_api/worker.js +18 -13
  33. package/examples/tutorials/emit_log.js +4 -6
  34. package/examples/tutorials/emit_log_direct.js +5 -7
  35. package/examples/tutorials/emit_log_topic.js +5 -7
  36. package/examples/tutorials/new_task.js +4 -6
  37. package/examples/tutorials/receive.js +8 -4
  38. package/examples/tutorials/receive_logs.js +10 -6
  39. package/examples/tutorials/receive_logs_direct.js +16 -10
  40. package/examples/tutorials/receive_logs_topic.js +16 -11
  41. package/examples/tutorials/rpc_client.js +17 -15
  42. package/examples/tutorials/rpc_server.js +9 -8
  43. package/examples/tutorials/send.js +4 -6
  44. package/examples/tutorials/worker.js +17 -14
  45. package/examples/waitForConfirms.js +2 -2
  46. package/lefthook.yml +14 -0
  47. package/lib/api_args.js +124 -149
  48. package/lib/bitset.js +28 -18
  49. package/lib/callback_model.js +134 -190
  50. package/lib/channel.js +175 -213
  51. package/lib/channel_model.js +41 -85
  52. package/lib/codec.js +257 -228
  53. package/lib/connect.js +74 -82
  54. package/lib/connection.js +204 -240
  55. package/lib/credentials.js +21 -29
  56. package/lib/defs.js +2559 -1927
  57. package/lib/error.js +5 -6
  58. package/lib/format.js +15 -33
  59. package/lib/frame.js +66 -66
  60. package/lib/heartbeat.js +17 -24
  61. package/lib/mux.js +32 -41
  62. package/package.json +8 -5
  63. package/test/bitset.test.js +65 -0
  64. package/test/callback_api.test.js +438 -0
  65. package/test/channel.test.js +592 -0
  66. package/test/channel_api.test.js +537 -0
  67. package/test/codec.test.js +172 -0
  68. package/test/connect.test.js +196 -0
  69. package/test/connection.test.js +330 -0
  70. package/test/data.test.js +33 -0
  71. package/test/frame.test.js +180 -0
  72. package/test/lib/data.js +249 -0
  73. package/test/lib/util.js +223 -0
  74. package/test/mux.test.js +215 -0
  75. package/test/bitset.js +0 -69
  76. package/test/callback_api.js +0 -379
  77. package/test/channel.js +0 -621
  78. package/test/channel_api.js +0 -606
  79. package/test/codec.js +0 -237
  80. package/test/connect.js +0 -197
  81. package/test/connection.js +0 -390
  82. package/test/data.js +0 -269
  83. package/test/frame.js +0 -191
  84. package/test/mux.js +0 -203
  85. package/test/util.js +0 -217
@@ -1,390 +0,0 @@
1
- 'use strict';
2
-
3
- var assert = require('assert');
4
- var defs = require('../lib/defs');
5
- var Connection = require('../lib/connection').Connection;
6
- var HEARTBEAT = require('../lib/frame').HEARTBEAT;
7
- var HB_BUF = require('../lib/frame').HEARTBEAT_BUF;
8
- var util = require('./util');
9
- var succeed = util.succeed, fail = util.fail, latch = util.latch;
10
- var completes = util.completes;
11
- var kCallback = util.kCallback;
12
-
13
- var LOG_ERRORS = process.env.LOG_ERRORS;
14
-
15
- var OPEN_OPTS = {
16
- // start-ok
17
- 'clientProperties': {},
18
- 'mechanism': 'PLAIN',
19
- 'response': Buffer.from(['', 'guest', 'guest'].join(String.fromCharCode(0))),
20
- 'locale': 'en_US',
21
-
22
- // tune-ok
23
- 'channelMax': 0,
24
- 'frameMax': 0,
25
- 'heartbeat': 0,
26
-
27
- // open
28
- 'virtualHost': '/',
29
- 'capabilities': '',
30
- 'insist': 0
31
- };
32
- module.exports.OPEN_OPTS = OPEN_OPTS;
33
-
34
- function happy_open(send, wait) {
35
- // kick it off
36
- send(defs.ConnectionStart,
37
- {versionMajor: 0,
38
- versionMinor: 9,
39
- serverProperties: {},
40
- mechanisms: Buffer.from('PLAIN'),
41
- locales: Buffer.from('en_US')});
42
- return wait(defs.ConnectionStartOk)()
43
- .then(function(f) {
44
- send(defs.ConnectionTune,
45
- {channelMax: 0,
46
- heartbeat: 0,
47
- frameMax: 0});
48
- })
49
- .then(wait(defs.ConnectionTuneOk))
50
- .then(wait(defs.ConnectionOpen))
51
- .then(function(f) {
52
- send(defs.ConnectionOpenOk,
53
- {knownHosts: ''});
54
- });
55
- }
56
- module.exports.connection_handshake = happy_open;
57
-
58
- function connectionTest(client, server) {
59
- return function(done) {
60
- var bothDone = latch(2, done);
61
- var pair = util.socketPair();
62
- var c = new Connection(pair.client);
63
- if (LOG_ERRORS) c.on('error', console.warn);
64
- client(c, bothDone);
65
-
66
- // NB only not a race here because the writes are synchronous
67
- var protocolHeader = pair.server.read(8);
68
- assert.deepEqual(Buffer.from("AMQP" + String.fromCharCode(0,0,9,1)),
69
- protocolHeader);
70
-
71
- var s = util.runServer(pair.server, function(send, wait) {
72
- server(send, wait, bothDone, pair.server);
73
- });
74
- };
75
- }
76
-
77
- suite("Connection errors", function() {
78
-
79
- test("socket close during open", function(done) {
80
- // RabbitMQ itself will take at least 3 seconds to close the socket
81
- // in the event of a handshake problem. Instead of using a live
82
- // connection, I'm just going to pretend.
83
- var pair = util.socketPair();
84
- var conn = new Connection(pair.client);
85
- pair.server.on('readable', function() {
86
- pair.server.end();
87
- });
88
- conn.open({}, kCallback(fail(done), succeed(done)));
89
- });
90
-
91
- test("bad frame during open", function(done) {
92
- var ss = util.socketPair();
93
- var conn = new (require('../lib/connection').Connection)(ss.client);
94
- ss.server.on('readable', function() {
95
- ss.server.write(Buffer.from([0, 0, 0, 0, 0, 0, 0, 0, 0, 0]));
96
- });
97
- conn.open({}, kCallback(fail(done), succeed(done)));
98
- });
99
-
100
- });
101
-
102
- suite("Connection open", function() {
103
-
104
- test("happy", connectionTest(
105
- function(c, done) {
106
- c.open(OPEN_OPTS, kCallback(succeed(done), fail(done)));
107
- },
108
- function(send, wait, done) {
109
- happy_open(send, wait).then(succeed(done), fail(done));
110
- }));
111
-
112
- test("wrong first frame", connectionTest(
113
- function(c, done) {
114
- c.open(OPEN_OPTS, kCallback(fail(done), succeed(done)));
115
- },
116
- function(send, wait, done) {
117
- // bad server! bad! whatever were you thinking?
118
- completes(function() {
119
- send(defs.ConnectionTune,
120
- {channelMax: 0,
121
- heartbeat: 0,
122
- frameMax: 0});
123
- }, done);
124
- }));
125
-
126
- test("unexpected socket close", connectionTest(
127
- function(c, done) {
128
- c.open(OPEN_OPTS, kCallback(fail(done), succeed(done)));
129
- },
130
- function(send, wait, done, socket) {
131
- send(defs.ConnectionStart,
132
- {versionMajor: 0,
133
- versionMinor: 9,
134
- serverProperties: {},
135
- mechanisms: Buffer.from('PLAIN'),
136
- locales: Buffer.from('en_US')});
137
- return wait(defs.ConnectionStartOk)()
138
- .then(function() {
139
- socket.end();
140
- })
141
- .then(succeed(done), fail(done));
142
- }));
143
-
144
- });
145
-
146
- suite("Connection running", function() {
147
-
148
- test("wrong frame on channel 0", connectionTest(
149
- function(c, done) {
150
- c.on('error', succeed(done));
151
- c.open(OPEN_OPTS);
152
- },
153
- function(send, wait, done) {
154
- happy_open(send, wait)
155
- .then(function() {
156
- // there's actually nothing that would plausibly be sent to a
157
- // just opened connection, so this is violating more than one
158
- // rule. Nonetheless.
159
- send(defs.ChannelOpenOk, {channelId: Buffer.from('')}, 0);
160
- })
161
- .then(wait(defs.ConnectionClose))
162
- .then(function(close) {
163
- send(defs.ConnectionCloseOk, {}, 0);
164
- }).then(succeed(done), fail(done));
165
- }));
166
-
167
- test("unopened channel", connectionTest(
168
- function(c, done) {
169
- c.on('error', succeed(done));
170
- c.open(OPEN_OPTS);
171
- },
172
- function(send, wait, done) {
173
- happy_open(send, wait)
174
- .then(function() {
175
- // there's actually nothing that would plausibly be sent to a
176
- // just opened connection, so this is violating more than one
177
- // rule. Nonetheless.
178
- send(defs.ChannelOpenOk, {channelId: Buffer.from('')}, 3);
179
- })
180
- .then(wait(defs.ConnectionClose))
181
- .then(function(close) {
182
- send(defs.ConnectionCloseOk, {}, 0);
183
- }).then(succeed(done), fail(done));
184
- }));
185
-
186
- test("unexpected socket close", connectionTest(
187
- function(c, done) {
188
- var errorAndClosed = latch(2, done);
189
- c.on('error', succeed(errorAndClosed));
190
- c.on('close', succeed(errorAndClosed));
191
- c.open(OPEN_OPTS, kCallback(function() {
192
- c.sendHeartbeat();
193
- }, fail(errorAndClosed)));
194
- },
195
- function(send, wait, done, socket) {
196
- happy_open(send, wait)
197
- .then(wait())
198
- .then(function() {
199
- socket.end();
200
- }).then(succeed(done));
201
- }));
202
-
203
- test("connection.blocked", connectionTest(
204
- function(c, done) {
205
- c.on('blocked', succeed(done));
206
- c.open(OPEN_OPTS);
207
- },
208
- function(send, wait, done, socket) {
209
- happy_open(send, wait)
210
- .then(function() {
211
- send(defs.ConnectionBlocked, {reason: 'felt like it'}, 0);
212
- })
213
- .then(succeed(done));
214
- }));
215
-
216
- test("connection.unblocked", connectionTest(
217
- function(c, done) {
218
- c.on('unblocked', succeed(done));
219
- c.open(OPEN_OPTS);
220
- },
221
- function(send, wait, done, socket) {
222
- happy_open(send, wait)
223
- .then(function() {
224
- send(defs.ConnectionUnblocked, {}, 0);
225
- })
226
- .then(succeed(done));
227
- }));
228
-
229
-
230
- });
231
-
232
- suite("Connection close", function() {
233
-
234
- test("happy", connectionTest(
235
- function(c, done0) {
236
- var done = latch(2, done0);
237
- c.on('close', done);
238
- c.open(OPEN_OPTS, kCallback(function(_ok) {
239
- c.close(kCallback(succeed(done), fail(done)));
240
- }, function() {}));
241
- },
242
- function(send, wait, done) {
243
- happy_open(send, wait)
244
- .then(wait(defs.ConnectionClose))
245
- .then(function(close) {
246
- send(defs.ConnectionCloseOk, {});
247
- })
248
- .then(succeed(done), fail(done));
249
- }));
250
-
251
- test("interleaved close frames", connectionTest(
252
- function(c, done0) {
253
- var done = latch(2, done0);
254
- c.on('close', done);
255
- c.open(OPEN_OPTS, kCallback(function(_ok) {
256
- c.close(kCallback(succeed(done), fail(done)));
257
- }, done));
258
- },
259
- function(send, wait, done) {
260
- happy_open(send, wait)
261
- .then(wait(defs.ConnectionClose))
262
- .then(function(f) {
263
- send(defs.ConnectionClose, {
264
- replyText: "Ha!",
265
- replyCode: defs.constants.REPLY_SUCCESS,
266
- methodId: 0, classId: 0
267
- });
268
- })
269
- .then(wait(defs.ConnectionCloseOk))
270
- .then(function(f) {
271
- send(defs.ConnectionCloseOk, {});
272
- })
273
- .then(succeed(done), fail(done));
274
- }));
275
-
276
- test("server error close", connectionTest(
277
- function(c, done0) {
278
- var done = latch(2, done0);
279
- c.on('close', succeed(done));
280
- c.on('error', succeed(done));
281
- c.open(OPEN_OPTS);
282
- },
283
- function(send, wait, done) {
284
- happy_open(send, wait)
285
- .then(function(f) {
286
- send(defs.ConnectionClose, {
287
- replyText: "Begone",
288
- replyCode: defs.constants.INTERNAL_ERROR,
289
- methodId: 0, classId: 0
290
- });
291
- })
292
- .then(wait(defs.ConnectionCloseOk))
293
- .then(succeed(done), fail(done));
294
- }));
295
-
296
- test("operator-intiated close", connectionTest(
297
- function(c, done) {
298
- c.on('close', succeed(done));
299
- c.on('error', fail(done));
300
- c.open(OPEN_OPTS);
301
- },
302
- function(send, wait, done) {
303
- happy_open(send, wait)
304
- .then(function(f) {
305
- send(defs.ConnectionClose, {
306
- replyText: "Begone",
307
- replyCode: defs.constants.CONNECTION_FORCED,
308
- methodId: 0, classId: 0
309
- });
310
- })
311
- .then(wait(defs.ConnectionCloseOk))
312
- .then(succeed(done), fail(done));
313
- }));
314
-
315
-
316
- test("double close", connectionTest(
317
- function(c, done) {
318
- c.open(OPEN_OPTS, kCallback(function() {
319
- c.close();
320
- // NB no synchronisation, we do this straight away
321
- assert.throws(function() {
322
- c.close();
323
- });
324
- done();
325
- }, done));
326
- },
327
- function(send, wait, done) {
328
- happy_open(send, wait)
329
- .then(wait(defs.ConnectionClose))
330
- .then(function() {
331
- send(defs.ConnectionCloseOk, {});
332
- })
333
- .then(succeed(done), fail(done));
334
- }));
335
-
336
- });
337
-
338
- suite("heartbeats", function() {
339
-
340
- var heartbeat = require('../lib/heartbeat');
341
-
342
- setup(function() {
343
- heartbeat.UNITS_TO_MS = 20;
344
- });
345
-
346
- teardown(function() {
347
- heartbeat.UNITS_TO_MS = 1000;
348
- });
349
-
350
- test("send heartbeat after open", connectionTest(
351
- function(c, done) {
352
- completes(function() {
353
- var opts = Object.create(OPEN_OPTS);
354
- opts.heartbeat = 1;
355
- // Don't leave the error waiting to happen for the next test, this
356
- // confuses mocha awfully
357
- c.on('error', function() {});
358
- c.open(opts);
359
- }, done);
360
- },
361
- function(send, wait, done, socket) {
362
- var timer;
363
- happy_open(send, wait)
364
- .then(function() {
365
- timer = setInterval(function() {
366
- socket.write(HB_BUF);
367
- }, heartbeat.UNITS_TO_MS);
368
- })
369
- .then(wait())
370
- .then(function(hb) {
371
- if (hb === HEARTBEAT) done();
372
- else done("Next frame after silence not a heartbeat");
373
- clearInterval(timer);
374
- });
375
- }));
376
-
377
- test("detect lack of heartbeats", connectionTest(
378
- function(c, done) {
379
- var opts = Object.create(OPEN_OPTS);
380
- opts.heartbeat = 1;
381
- c.on('error', succeed(done));
382
- c.open(opts);
383
- },
384
- function(send, wait, done, socket) {
385
- happy_open(send, wait)
386
- .then(succeed(done), fail(done));
387
- // conspicuously not sending anything ...
388
- }));
389
-
390
- });
package/test/data.js DELETED
@@ -1,269 +0,0 @@
1
- // Property-based testing representations of various things in AMQP
2
-
3
- 'use strict';
4
-
5
- var C = require('claire');
6
- var forAll = C.forAll;
7
- var arb = C.data;
8
- var transform = C.transform;
9
- var repeat = C.repeat;
10
- var label = C.label;
11
- var sequence = C.sequence;
12
- var asGenerator = C.asGenerator;
13
- var sized = C.sized;
14
- var recursive = C.recursive;
15
- var choice = C.choice;
16
- var Undefined = C.Undefined;
17
-
18
- // Stub these out so we can use outside tests
19
- // if (!suite) var suite = function() {}
20
- // if (!test) var test = function() {}
21
-
22
- // These aren't exported in claire/index. so I could have to reproduce
23
- // them I guess.
24
- function choose(a, b) {
25
- return Math.random() * (b - a) + a;
26
- }
27
-
28
- function chooseInt(a, b) {
29
- return Math.floor(choose(a, b));
30
- }
31
-
32
- function rangeInt(name, a, b) {
33
- return label(name,
34
- asGenerator(function(_) { return chooseInt(a, b); }));
35
- }
36
-
37
- function toFloat32(i) {
38
- var b = Buffer.alloc(4);
39
- b.writeFloatBE(i, 0);
40
- return b.readFloatBE(0);
41
- }
42
-
43
- function floatChooser(maxExp) {
44
- return function() {
45
- var n = Number.NaN;
46
- while (isNaN(n)) {
47
- var mantissa = Math.random() * 2 - 1;
48
- var exponent = chooseInt(0, maxExp);
49
- n = Math.pow(mantissa, exponent);
50
- }
51
- return n;
52
- }
53
- }
54
-
55
- function explicitType(t, underlying) {
56
- return label(t, transform(function(n) {
57
- return {'!': t, value: n};
58
- }, underlying));
59
- }
60
-
61
- // FIXME null, byte array, others?
62
-
63
- var Octet = rangeInt('octet', 0, 255);
64
- var ShortStr = label('shortstr',
65
- transform(function(s) {
66
- return s.substr(0, 255);
67
- }, arb.Str));
68
-
69
- var LongStr = label('longstr',
70
- transform(
71
- function(bytes) { return Buffer.from(bytes); },
72
- repeat(Octet)));
73
-
74
- var UShort = rangeInt('short-uint', 0, 0xffff);
75
- var ULong = rangeInt('long-uint', 0, 0xffffffff);
76
- var ULongLong = rangeInt('longlong-uint', 0, 0xffffffffffffffff);
77
- var Short = rangeInt('short-int', -0x8000, 0x7fff);
78
- var Long = rangeInt('long-int', -0x80000000, 0x7fffffff);
79
- var LongLong = rangeInt('longlong-int', -0x8000000000000000,
80
- 0x7fffffffffffffff);
81
- var Bit = label('bit', arb.Bool);
82
- var Double = label('double', asGenerator(floatChooser(308)));
83
- var Float = label('float', transform(toFloat32, floatChooser(38)));
84
- var Timestamp = label('timestamp', transform(
85
- function(n) {
86
- return {'!': 'timestamp', value: n};
87
- }, ULongLong));
88
- var Decimal = label('decimal', transform(
89
- function(args) {
90
- return {'!': 'decimal', value: {places: args[1], digits: args[0]}};
91
- }, sequence(arb.UInt, Octet)));
92
- var UnsignedByte = label('unsignedbyte', transform(
93
- function(n) {
94
- return {'!': 'unsignedbyte', value: n};
95
- }, Octet));
96
- var UnsignedShort = label('unsignedshort', transform(
97
- function(n) {
98
- return {'!': 'unsignedshort', value: n};
99
- }, UShort));
100
- var UnsignedInt = label('unsignedint', transform(
101
- function(n) {
102
- return {'!': 'unsignedint', value: n};
103
- }, ULong));
104
-
105
- // Signed 8 bit int
106
- var Byte = rangeInt('byte', -128, 127);
107
-
108
- // Explicitly typed values
109
- var ExByte = explicitType('byte', Byte);
110
- var ExInt8 = explicitType('int8', Byte);
111
- var ExShort = explicitType('short', Short);
112
- var ExInt16 = explicitType('int16', Short);
113
- var ExInt = explicitType('int', Long);
114
- var ExInt32 = explicitType('int32', Long);
115
- var ExLong = explicitType('long', LongLong);
116
- var ExInt64 = explicitType('int64', LongLong);
117
-
118
- var FieldArray = label('field-array', recursive(function() {
119
- return arb.Array(
120
- arb.Null,
121
- LongStr, ShortStr,
122
- Octet, UShort, ULong, ULongLong,
123
- Byte, Short, Long, LongLong,
124
- ExByte, ExInt8, ExShort, ExInt16,
125
- ExInt, ExInt32, ExLong, ExInt64,
126
- Bit, Float, Double, FieldTable, FieldArray)
127
- }));
128
-
129
- var FieldTable = label('table', recursive(function() {
130
- return sized(function() { return 5; },
131
- arb.Object(
132
- arb.Null,
133
- LongStr, ShortStr, Octet,
134
- UShort, ULong, ULongLong,
135
- Byte, Short, Long, LongLong,
136
- ExByte, ExInt8, ExShort, ExInt16,
137
- ExInt, ExInt32, ExLong, ExInt64,
138
- Bit, Float, Double, FieldArray, FieldTable))
139
- }));
140
-
141
- // Internal tests of our properties
142
- var domainProps = [
143
- [Octet, function(n) { return n >= 0 && n < 256; }],
144
- [ShortStr, function(s) { return typeof s === 'string' && s.length < 256; }],
145
- [LongStr, function(s) { return Buffer.isBuffer(s); }],
146
- [UShort, function(n) { return n >= 0 && n <= 0xffff; }],
147
- [ULong, function(n) { return n >= 0 && n <= 0xffffffff; }],
148
- [ULongLong, function(n) {
149
- return n >= 0 && n <= 0xffffffffffffffff; }],
150
- [Short, function(n) { return n >= -0x8000 && n <= 0x8000; }],
151
- [Long, function(n) { return n >= -0x80000000 && n < 0x80000000; }],
152
- [LongLong, function(n) { return n >= -0x8000000000000000 && n < 0x8000000000000000; }],
153
- [Bit, function(b) { return typeof b === 'boolean'; }],
154
- [Double, function(f) { return !isNaN(f) && isFinite(f); }],
155
- [Float, function(f) { return !isNaN(f) && isFinite(f) && (Math.log(Math.abs(f)) * Math.LOG10E) < 309; }],
156
- [Decimal, function(d) {
157
- return d['!'] === 'decimal' &&
158
- d.value['places'] <= 255 &&
159
- d.value['digits'] <= 0xffffffff;
160
- }],
161
- [Timestamp, function(t) { return t['!'] === 'timestamp'; }],
162
- [FieldTable, function(t) { return typeof t === 'object'; }],
163
- [FieldArray, function(a) { return Array.isArray(a); }]
164
- ];
165
-
166
- suite("Domains", function() {
167
- domainProps.forEach(function(p) {
168
- test(p[0] + ' domain',
169
- forAll(p[0]).satisfy(p[1]).asTest({times: 500}));
170
- });
171
- });
172
-
173
- // For methods and properties (as opposed to field table values) it's
174
- // easier just to accept and produce numbers for timestamps.
175
- var ArgTimestamp = label('timestamp', ULongLong);
176
-
177
- // These are the domains used in method arguments
178
- var ARG_TYPES = {
179
- 'octet': Octet,
180
- 'shortstr': ShortStr,
181
- 'longstr': LongStr,
182
- 'short': UShort,
183
- 'long': ULong,
184
- 'longlong': ULongLong,
185
- 'bit': Bit,
186
- 'table': FieldTable,
187
- 'timestamp': ArgTimestamp
188
- };
189
-
190
- function argtype(thing) {
191
- if (thing.default === undefined) {
192
- return ARG_TYPES[thing.type];
193
- }
194
- else {
195
- return choice(ARG_TYPES[thing.type], Undefined);
196
- }
197
- }
198
-
199
- function zipObject(vals, names) {
200
- var obj = {};
201
- vals.forEach(function(v, i) { obj[names[i]] = v; });
202
- return obj;
203
- }
204
-
205
- function name(arg) { return arg.name; }
206
-
207
- var defs = require('../lib/defs');
208
-
209
- function method(info) {
210
- var domain = sequence.apply(null, info.args.map(argtype));
211
- var names = info.args.map(name);
212
- return label(info.name, transform(function(fieldVals) {
213
- return {id: info.id,
214
- fields: zipObject(fieldVals, names)};
215
- }, domain));
216
- }
217
-
218
- function properties(info) {
219
- var types = info.args.map(argtype);
220
- types.unshift(ULongLong); // size
221
- var domain = sequence.apply(null, types);
222
- var names = info.args.map(name);
223
- return label(info.name, transform(function(fieldVals) {
224
- return {id: info.id,
225
- size: fieldVals[0],
226
- fields: zipObject(fieldVals.slice(1), names)};
227
- }, domain));
228
- }
229
-
230
- var methods = [];
231
- var propertieses = [];
232
-
233
- for (var k in defs) {
234
- if (k.substr(0, 10) === 'methodInfo') {
235
- methods.push(method(defs[k]));
236
- methods[defs[k].name] = method(defs[k]);
237
- }
238
- else if (k.substr(0, 14) === 'propertiesInfo') {
239
- propertieses.push(properties(defs[k]));
240
- propertieses[defs[k].name] = properties(defs[k]);
241
- }
242
- };
243
-
244
- module.exports = {
245
- Octet: Octet,
246
- ShortStr: ShortStr,
247
- LongStr: LongStr,
248
- UShort: UShort,
249
- ULong: ULong,
250
- ULongLong: ULongLong,
251
- Short: Short,
252
- Long: Long,
253
- LongLong: LongLong,
254
- Bit: Bit,
255
- Double: Double,
256
- Float: Float,
257
- Timestamp: Timestamp,
258
- Decimal: Decimal,
259
- UnsignedByte: UnsignedByte,
260
- UnsignedShort: UnsignedShort,
261
- UnsignedInt: UnsignedInt,
262
- FieldArray: FieldArray,
263
- FieldTable: FieldTable,
264
-
265
- methods: methods,
266
- properties: propertieses
267
- };
268
-
269
- module.exports.rangeInt = rangeInt;