typespeed 2.0.13 → 2.0.16

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 (222) hide show
  1. package/dist/core.decorator.js +0 -1
  2. package/dist/database.decorator.js +0 -1
  3. package/dist/default/express-server.class.js +0 -1
  4. package/dist/default/log-default.class.js +0 -1
  5. package/dist/default/node-cache.class.js +0 -1
  6. package/dist/default/rabbitmq.class.js +0 -1
  7. package/dist/default/read-write-db.class.js +0 -1
  8. package/dist/default/redis.class.js +0 -1
  9. package/dist/factory/cache-factory.class.js +0 -1
  10. package/dist/factory/data-source-factory.class.js +0 -1
  11. package/dist/factory/log-factory.class.js +0 -1
  12. package/dist/factory/server-factory.class.js +0 -1
  13. package/dist/route.decorator.js +0 -1
  14. package/dist/scaffold/command.js +0 -1
  15. package/dist/typespeed.d.ts +133 -12
  16. package/dist/typespeed.js +1 -1
  17. package/package.json +2 -2
  18. package/src/typespeed.d.ts +133 -0
  19. package/src/typespeed.ts +2 -1
  20. package/test/node_modules/.package-lock.json +112 -4
  21. package/test/node_modules/@acuminous/bitsyntax/.github/workflows/node-js-ci.yml +17 -0
  22. package/test/node_modules/@acuminous/bitsyntax/.github/workflows/node-js-publish.yml +30 -0
  23. package/test/node_modules/@acuminous/bitsyntax/LICENSE +6 -0
  24. package/test/node_modules/@acuminous/bitsyntax/LICENSE-MIT +21 -0
  25. package/test/node_modules/@acuminous/bitsyntax/Makefile +15 -0
  26. package/test/node_modules/@acuminous/bitsyntax/README.md +308 -0
  27. package/test/node_modules/@acuminous/bitsyntax/index.js +10 -0
  28. package/test/node_modules/@acuminous/bitsyntax/lib/compile.js +303 -0
  29. package/test/node_modules/@acuminous/bitsyntax/lib/constructor.js +142 -0
  30. package/test/node_modules/@acuminous/bitsyntax/lib/grammar.pegjs +67 -0
  31. package/test/node_modules/@acuminous/bitsyntax/lib/interp.js +232 -0
  32. package/test/node_modules/@acuminous/bitsyntax/lib/parse.js +32 -0
  33. package/test/node_modules/@acuminous/bitsyntax/lib/parser.js +1173 -0
  34. package/test/node_modules/@acuminous/bitsyntax/lib/pattern.js +119 -0
  35. package/test/node_modules/@acuminous/bitsyntax/package.json +36 -0
  36. package/test/node_modules/@acuminous/bitsyntax/test/consing.test.js +50 -0
  37. package/test/node_modules/@acuminous/bitsyntax/test/matching.test.js +193 -0
  38. package/test/node_modules/@types/node/README.md +1 -1
  39. package/test/node_modules/@types/node/buffer.d.ts +5 -2
  40. package/test/node_modules/@types/node/child_process.d.ts +27 -0
  41. package/test/node_modules/@types/node/globals.d.ts +1 -0
  42. package/test/node_modules/@types/node/package.json +2 -2
  43. package/test/node_modules/@types/node/test.d.ts +43 -20
  44. package/test/node_modules/@types/node/ts4.8/buffer.d.ts +5 -2
  45. package/test/node_modules/@types/node/ts4.8/child_process.d.ts +27 -0
  46. package/test/node_modules/@types/node/ts4.8/globals.d.ts +1 -0
  47. package/test/node_modules/@types/node/ts4.8/test.d.ts +43 -19
  48. package/test/node_modules/@types/node/ts4.8/url.d.ts +3 -3
  49. package/test/node_modules/@types/node/ts4.8/util.d.ts +1 -1
  50. package/test/node_modules/@types/node/url.d.ts +3 -3
  51. package/test/node_modules/@types/node/util.d.ts +1 -1
  52. package/test/node_modules/amqplib/.github/ISSUE_TEMPLATE/bug_report.md +38 -0
  53. package/test/node_modules/amqplib/.github/workflows/test.yml +38 -0
  54. package/test/node_modules/amqplib/CHANGELOG.md +430 -0
  55. package/test/node_modules/amqplib/LICENSE +6 -0
  56. package/test/node_modules/amqplib/LICENSE-MIT +21 -0
  57. package/test/node_modules/amqplib/Makefile +47 -0
  58. package/test/node_modules/amqplib/README.md +157 -0
  59. package/test/node_modules/amqplib/bin/generate-defs.js +722 -0
  60. package/test/node_modules/amqplib/callback_api.js +22 -0
  61. package/test/node_modules/amqplib/channel_api.js +16 -0
  62. package/test/node_modules/amqplib/examples/headers.js +42 -0
  63. package/test/node_modules/amqplib/examples/receive_generator.js +39 -0
  64. package/test/node_modules/amqplib/examples/send_generators.js +42 -0
  65. package/test/node_modules/amqplib/examples/ssl.js +64 -0
  66. package/test/node_modules/amqplib/examples/tutorials/README.md +93 -0
  67. package/test/node_modules/amqplib/examples/tutorials/callback_api/emit_log.js +28 -0
  68. package/test/node_modules/amqplib/examples/tutorials/callback_api/emit_log_direct.js +30 -0
  69. package/test/node_modules/amqplib/examples/tutorials/callback_api/emit_log_topic.js +27 -0
  70. package/test/node_modules/amqplib/examples/tutorials/callback_api/new_task.js +27 -0
  71. package/test/node_modules/amqplib/examples/tutorials/callback_api/receive.js +30 -0
  72. package/test/node_modules/amqplib/examples/tutorials/callback_api/receive_logs.js +36 -0
  73. package/test/node_modules/amqplib/examples/tutorials/callback_api/receive_logs_direct.js +56 -0
  74. package/test/node_modules/amqplib/examples/tutorials/callback_api/receive_logs_topic.js +55 -0
  75. package/test/node_modules/amqplib/examples/tutorials/callback_api/rpc_client.js +49 -0
  76. package/test/node_modules/amqplib/examples/tutorials/callback_api/rpc_server.js +45 -0
  77. package/test/node_modules/amqplib/examples/tutorials/callback_api/send.js +29 -0
  78. package/test/node_modules/amqplib/examples/tutorials/callback_api/worker.js +35 -0
  79. package/test/node_modules/amqplib/examples/tutorials/emit_log.js +19 -0
  80. package/test/node_modules/amqplib/examples/tutorials/emit_log_direct.js +20 -0
  81. package/test/node_modules/amqplib/examples/tutorials/emit_log_topic.js +19 -0
  82. package/test/node_modules/amqplib/examples/tutorials/new_task.js +18 -0
  83. package/test/node_modules/amqplib/examples/tutorials/package.json +16 -0
  84. package/test/node_modules/amqplib/examples/tutorials/receive.js +21 -0
  85. package/test/node_modules/amqplib/examples/tutorials/receive_logs.js +28 -0
  86. package/test/node_modules/amqplib/examples/tutorials/receive_logs_direct.js +44 -0
  87. package/test/node_modules/amqplib/examples/tutorials/receive_logs_topic.js +43 -0
  88. package/test/node_modules/amqplib/examples/tutorials/rpc_client.js +52 -0
  89. package/test/node_modules/amqplib/examples/tutorials/rpc_server.js +39 -0
  90. package/test/node_modules/amqplib/examples/tutorials/send.js +23 -0
  91. package/test/node_modules/amqplib/examples/tutorials/worker.js +28 -0
  92. package/test/node_modules/amqplib/examples/waitForConfirms.js +22 -0
  93. package/test/node_modules/amqplib/lib/api_args.js +314 -0
  94. package/test/node_modules/amqplib/lib/bitset.js +130 -0
  95. package/test/node_modules/amqplib/lib/callback_model.js +327 -0
  96. package/test/node_modules/amqplib/lib/channel.js +499 -0
  97. package/test/node_modules/amqplib/lib/channel_model.js +302 -0
  98. package/test/node_modules/amqplib/lib/codec.js +321 -0
  99. package/test/node_modules/amqplib/lib/connect.js +189 -0
  100. package/test/node_modules/amqplib/lib/connection.js +663 -0
  101. package/test/node_modules/amqplib/lib/credentials.js +42 -0
  102. package/test/node_modules/amqplib/lib/defs.js +4966 -0
  103. package/test/node_modules/amqplib/lib/error.js +24 -0
  104. package/test/node_modules/amqplib/lib/format.js +40 -0
  105. package/test/node_modules/amqplib/lib/frame.js +113 -0
  106. package/test/node_modules/amqplib/lib/heartbeat.js +89 -0
  107. package/test/node_modules/amqplib/lib/mux.js +129 -0
  108. package/test/node_modules/amqplib/package.json +37 -0
  109. package/test/node_modules/amqplib/test/bitset.js +69 -0
  110. package/test/node_modules/amqplib/test/callback_api.js +312 -0
  111. package/test/node_modules/amqplib/test/channel.js +621 -0
  112. package/test/node_modules/amqplib/test/channel_api.js +596 -0
  113. package/test/node_modules/amqplib/test/codec.js +237 -0
  114. package/test/node_modules/amqplib/test/connect.js +197 -0
  115. package/test/node_modules/amqplib/test/connection.js +390 -0
  116. package/test/node_modules/amqplib/test/data.js +254 -0
  117. package/test/node_modules/amqplib/test/frame.js +198 -0
  118. package/test/node_modules/amqplib/test/mux.js +204 -0
  119. package/test/node_modules/amqplib/test/util.js +219 -0
  120. package/test/node_modules/buffer-more-ints/.travis.yml +6 -0
  121. package/test/node_modules/buffer-more-ints/LICENSE +19 -0
  122. package/test/node_modules/buffer-more-ints/README.md +69 -0
  123. package/test/node_modules/buffer-more-ints/buffer-more-ints-tests.js +183 -0
  124. package/test/node_modules/buffer-more-ints/buffer-more-ints.js +432 -0
  125. package/test/node_modules/buffer-more-ints/package.json +19 -0
  126. package/test/node_modules/buffer-more-ints/polyfill.js +71 -0
  127. package/test/node_modules/core-util-is/LICENSE +19 -0
  128. package/test/node_modules/core-util-is/README.md +3 -0
  129. package/test/node_modules/core-util-is/lib/util.js +107 -0
  130. package/test/node_modules/core-util-is/package.json +38 -0
  131. package/test/node_modules/debug/LICENSE +20 -0
  132. package/test/node_modules/debug/README.md +481 -0
  133. package/test/node_modules/debug/package.json +59 -0
  134. package/test/node_modules/debug/src/browser.js +269 -0
  135. package/test/node_modules/debug/src/common.js +274 -0
  136. package/test/node_modules/debug/src/index.js +10 -0
  137. package/test/node_modules/debug/src/node.js +263 -0
  138. package/test/node_modules/inherits/LICENSE +16 -0
  139. package/test/node_modules/inherits/README.md +42 -0
  140. package/test/node_modules/inherits/inherits.js +9 -0
  141. package/test/node_modules/inherits/inherits_browser.js +27 -0
  142. package/test/node_modules/inherits/package.json +29 -0
  143. package/test/node_modules/isarray/README.md +54 -0
  144. package/test/node_modules/isarray/build/build.js +209 -0
  145. package/test/node_modules/isarray/component.json +19 -0
  146. package/test/node_modules/isarray/index.js +3 -0
  147. package/test/node_modules/isarray/package.json +25 -0
  148. package/test/node_modules/ms/index.js +162 -0
  149. package/test/node_modules/ms/license.md +21 -0
  150. package/test/node_modules/ms/package.json +37 -0
  151. package/test/node_modules/ms/readme.md +60 -0
  152. package/test/node_modules/querystringify/LICENSE +22 -0
  153. package/test/node_modules/querystringify/README.md +61 -0
  154. package/test/node_modules/querystringify/index.js +118 -0
  155. package/test/node_modules/querystringify/package.json +38 -0
  156. package/test/node_modules/readable-stream/LICENSE +18 -0
  157. package/test/node_modules/readable-stream/README.md +15 -0
  158. package/test/node_modules/readable-stream/duplex.js +1 -0
  159. package/test/node_modules/readable-stream/float.patch +923 -0
  160. package/test/node_modules/readable-stream/lib/_stream_duplex.js +89 -0
  161. package/test/node_modules/readable-stream/lib/_stream_passthrough.js +46 -0
  162. package/test/node_modules/readable-stream/lib/_stream_readable.js +951 -0
  163. package/test/node_modules/readable-stream/lib/_stream_transform.js +209 -0
  164. package/test/node_modules/readable-stream/lib/_stream_writable.js +477 -0
  165. package/test/node_modules/readable-stream/package.json +32 -0
  166. package/test/node_modules/readable-stream/passthrough.js +1 -0
  167. package/test/node_modules/readable-stream/readable.js +10 -0
  168. package/test/node_modules/readable-stream/transform.js +1 -0
  169. package/test/node_modules/readable-stream/writable.js +1 -0
  170. package/test/node_modules/requires-port/.travis.yml +19 -0
  171. package/test/node_modules/requires-port/LICENSE +22 -0
  172. package/test/node_modules/requires-port/README.md +47 -0
  173. package/test/node_modules/requires-port/index.js +38 -0
  174. package/test/node_modules/requires-port/package.json +47 -0
  175. package/test/node_modules/requires-port/test.js +98 -0
  176. package/test/node_modules/safe-buffer/LICENSE +21 -0
  177. package/test/node_modules/safe-buffer/README.md +584 -0
  178. package/test/node_modules/safe-buffer/index.d.ts +187 -0
  179. package/test/node_modules/safe-buffer/index.js +62 -0
  180. package/test/node_modules/safe-buffer/package.json +37 -0
  181. package/test/node_modules/string_decoder/LICENSE +20 -0
  182. package/test/node_modules/string_decoder/README.md +7 -0
  183. package/test/node_modules/string_decoder/index.js +221 -0
  184. package/test/node_modules/string_decoder/package.json +25 -0
  185. package/test/node_modules/url-parse/LICENSE +22 -0
  186. package/test/node_modules/url-parse/README.md +153 -0
  187. package/test/node_modules/url-parse/dist/url-parse.js +755 -0
  188. package/test/node_modules/url-parse/dist/url-parse.min.js +1 -0
  189. package/test/node_modules/url-parse/dist/url-parse.min.js.map +1 -0
  190. package/test/node_modules/url-parse/index.js +589 -0
  191. package/test/node_modules/url-parse/package.json +49 -0
  192. package/test/src/test-mq.class.ts +1 -2
  193. package/tsconfig.json +4 -3
  194. package/dist/core.decorator.d.ts +0 -18
  195. package/dist/core.decorator.js.map +0 -1
  196. package/dist/database.decorator.d.ts +0 -24
  197. package/dist/database.decorator.js.map +0 -1
  198. package/dist/default/express-server.class.d.ts +0 -15
  199. package/dist/default/express-server.class.js.map +0 -1
  200. package/dist/default/log-default.class.d.ts +0 -6
  201. package/dist/default/log-default.class.js.map +0 -1
  202. package/dist/default/node-cache.class.d.ts +0 -13
  203. package/dist/default/node-cache.class.js.map +0 -1
  204. package/dist/default/rabbitmq.class.d.ts +0 -9
  205. package/dist/default/rabbitmq.class.js.map +0 -1
  206. package/dist/default/read-write-db.class.d.ts +0 -10
  207. package/dist/default/read-write-db.class.js.map +0 -1
  208. package/dist/default/redis.class.d.ts +0 -5
  209. package/dist/default/redis.class.js.map +0 -1
  210. package/dist/factory/cache-factory.class.d.ts +0 -7
  211. package/dist/factory/cache-factory.class.js.map +0 -1
  212. package/dist/factory/data-source-factory.class.d.ts +0 -4
  213. package/dist/factory/data-source-factory.class.js.map +0 -1
  214. package/dist/factory/log-factory.class.d.ts +0 -4
  215. package/dist/factory/log-factory.class.js.map +0 -1
  216. package/dist/factory/server-factory.class.d.ts +0 -6
  217. package/dist/factory/server-factory.class.js.map +0 -1
  218. package/dist/route.decorator.d.ts +0 -8
  219. package/dist/route.decorator.js.map +0 -1
  220. package/dist/scaffold/command.d.ts +0 -2
  221. package/dist/scaffold/command.js.map +0 -1
  222. package/dist/typespeed.js.map +0 -1
@@ -0,0 +1,198 @@
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
+ require('readable-stream/passthrough');
12
+
13
+ var defs = require('../lib/defs');
14
+
15
+ // We'll need to supply a stream which we manipulate ourselves
16
+
17
+ function inputs() {
18
+ // don't coalesce buffers, since that could mess up properties
19
+ // (e.g., encoded frame size)
20
+ return new PassThrough({objectMode: true});
21
+ }
22
+
23
+ var HB = Buffer.from([defs.constants.FRAME_HEARTBEAT,
24
+ 0, 0, // channel 0
25
+ 0, 0, 0, 0, // zero size
26
+ defs.constants.FRAME_END]);
27
+
28
+ suite("Explicit parsing", function() {
29
+
30
+ test('Parse heartbeat', function() {
31
+ var input = inputs();
32
+ var frames = new Frames(input);
33
+ input.write(HB);
34
+ assert(frames.recvFrame() === HEARTBEAT);
35
+ assert(!frames.recvFrame());
36
+ });
37
+
38
+ test('Parse partitioned', function() {
39
+ var input = inputs();
40
+ var frames = new Frames(input);
41
+ input.write(HB.slice(0, 3));
42
+ assert(!frames.recvFrame());
43
+ input.write(HB.slice(3));
44
+ assert(frames.recvFrame() === HEARTBEAT);
45
+ assert(!frames.recvFrame());
46
+ });
47
+
48
+ function testBogusFrame(name, bytes) {
49
+ test(name, function(done) {
50
+ var input = inputs();
51
+ var frames = new Frames(input);
52
+ frames.frameMax = 5; //for the max frame test
53
+ input.write(Buffer.from(bytes));
54
+ frames.step(function(err, frame) {
55
+ if (err != null) done();
56
+ else done(new Error('Was a bogus frame!'));
57
+ });
58
+ });
59
+ }
60
+
61
+ testBogusFrame('Wrong sized frame',
62
+ [defs.constants.FRAME_BODY,
63
+ 0,0, 0,0,0,0, // zero length
64
+ 65, // but a byte!
65
+ defs.constants.FRAME_END]);
66
+
67
+ testBogusFrame('Unknown method frame',
68
+ [defs.constants.FRAME_METHOD,
69
+ 0,0, 0,0,0,4,
70
+ 0,0,0,0, // garbage ID
71
+ defs.constants.FRAME_END]);
72
+
73
+ testBogusFrame('> max frame',
74
+ [defs.constants.FRAME_BODY,
75
+ 0,0, 0,0,0,6, // too big!
76
+ 65,66,67,68,69,70,
77
+ defs.constants.FRAME_END]);
78
+
79
+ });
80
+
81
+ // Now for a bit more fun.
82
+
83
+ var amqp = require('./data');
84
+ var claire = require('claire');
85
+ var choice = claire.choice;
86
+ var forAll = claire.forAll;
87
+ var repeat = claire.repeat;
88
+ var label = claire.label;
89
+ var sequence = claire.sequence;
90
+ var transform = claire.transform;
91
+ var sized = claire.sized;
92
+
93
+ var assertEqualModuloDefaults =
94
+ require('./codec').assertEqualModuloDefaults;
95
+
96
+ var Trace = label('frame trace',
97
+ repeat(choice.apply(choice, amqp.methods)));
98
+
99
+ suite("Parsing", function() {
100
+
101
+ function testPartitioning(partition) {
102
+ return forAll(Trace).satisfy(function(t) {
103
+ var bufs = [];
104
+ var input = inputs();
105
+ var frames = new Frames(input);
106
+ var i = 0, ex;
107
+ frames.accept = function(f) {
108
+ // A minor hack to make sure we get the assertion exception;
109
+ // otherwise, it's just a test that we reached the line
110
+ // incrementing `i` for each frame.
111
+ try {
112
+ assertEqualModuloDefaults(t[i], f.fields);
113
+ }
114
+ catch (e) {
115
+ ex = e;
116
+ }
117
+ i++;
118
+ };
119
+
120
+ t.forEach(function(f) {
121
+ f.channel = 0;
122
+ bufs.push(defs.encodeMethod(f.id, 0, f.fields));
123
+ });
124
+
125
+ partition(bufs).forEach(function (chunk) { input.write(chunk); });
126
+ frames.acceptLoop();
127
+ if (ex) throw ex;
128
+ return i === t.length;
129
+ }).asTest({times: 20})
130
+ };
131
+
132
+ test("Parse trace of methods",
133
+ testPartitioning(function(bufs) { return bufs; }));
134
+
135
+ test("Parse concat'd methods",
136
+ testPartitioning(function(bufs) {
137
+ return [Buffer.concat(bufs)];
138
+ }));
139
+
140
+ test("Parse partitioned methods",
141
+ testPartitioning(function(bufs) {
142
+ var full = Buffer.concat(bufs);
143
+ var onethird = Math.floor(full.length / 3);
144
+ var twothirds = 2 * onethird;
145
+ return [
146
+ full.slice(0, onethird),
147
+ full.slice(onethird, twothirds),
148
+ full.slice(twothirds)
149
+ ];
150
+ }));
151
+ });
152
+
153
+ var FRAME_MAX_MAX = 4096 * 4;
154
+ var FRAME_MAX_MIN = 4096;
155
+
156
+ var FrameMax = amqp.rangeInt('frame max',
157
+ FRAME_MAX_MIN,
158
+ FRAME_MAX_MAX);
159
+
160
+ var Body = sized(function(_n) {
161
+ return Math.floor(Math.random() * FRAME_MAX_MAX);
162
+ }, repeat(amqp.Octet));
163
+
164
+ var Content = transform(function(args) {
165
+ return {
166
+ method: args[0].fields,
167
+ header: args[1].fields,
168
+ body: Buffer.from(args[2])
169
+ }
170
+ }, sequence(amqp.methods['BasicDeliver'],
171
+ amqp.properties['BasicProperties'], Body));
172
+
173
+ suite("Content framing", function() {
174
+ test("Adhere to frame max",
175
+ forAll(Content, FrameMax).satisfy(function(content, max) {
176
+ var input = inputs();
177
+ var frames = new Frames(input);
178
+ frames.frameMax = max;
179
+ frames.sendMessage(
180
+ 0,
181
+ defs.BasicDeliver, content.method,
182
+ defs.BasicProperties, content.header,
183
+ content.body);
184
+ var f, i = 0, largest = 0;
185
+ while (f = input.read()) {
186
+ i++;
187
+ if (f.length > largest) largest = f.length;
188
+ if (f.length > max) {
189
+ return false;
190
+ }
191
+ }
192
+ // The ratio of frames to 'contents' should always be >= 2
193
+ // (one properties frame and at least one content frame); > 2
194
+ // indicates fragmentation. The largest is always, of course <= frame max
195
+ //console.log('Frames: %d; frames per message: %d; largest frame %d', i, i / t.length, largest);
196
+ return true;
197
+ }).asTest());
198
+ });
@@ -0,0 +1,204 @@
1
+ 'use strict';
2
+
3
+ var assert = require('assert');
4
+ var Mux = require('../lib/mux').Mux;
5
+ var PassThrough = require('stream').PassThrough ||
6
+ require('readable-stream/passthrough');
7
+
8
+ var latch = require('./util').latch;
9
+ var schedule = require('./util').schedule;
10
+
11
+ function stream() {
12
+ return new PassThrough({objectMode: true});
13
+ }
14
+
15
+ function readAllObjects(s, cb) {
16
+ var objs = [];
17
+
18
+ function read() {
19
+ var v = s.read();
20
+ while (v !== null) {
21
+ objs.push(v);
22
+ v = s.read();
23
+ }
24
+ }
25
+
26
+ s.on('end', function() { cb(objs); });
27
+ s.on('readable', read);
28
+
29
+ read();
30
+ }
31
+
32
+ test("single input", function(done) {
33
+ var input = stream();
34
+ var output = stream();
35
+ input.on('end', function() { output.end() });
36
+
37
+ var mux = new Mux(output);
38
+ mux.pipeFrom(input);
39
+
40
+ var data = [1,2,3,4,5,6,7,8,9];
41
+ // not 0, it's treated specially by PassThrough for some reason. By
42
+ // 'specially' I mean it breaks the stream. See e.g.,
43
+ // https://github.com/isaacs/readable-stream/pull/55
44
+ data.forEach(function (chunk) { input.write(chunk); });
45
+
46
+ readAllObjects(output, function(vals) {
47
+ assert.deepEqual(data, vals);
48
+ done();
49
+ });
50
+
51
+ input.end();
52
+ });
53
+
54
+ test("single input, resuming stream", function(done) {
55
+ var input = stream();
56
+ var output = stream();
57
+ input.on('end', function() { output.end() });
58
+
59
+ var mux = new Mux(output);
60
+ mux.pipeFrom(input);
61
+
62
+ // Streams might be blocked and become readable again, simulate this
63
+ // using a special read function and a marker
64
+ var data = [1,2,3,4,'skip',6,7,8,9];
65
+
66
+ var oldRead = input.read;
67
+ input.read = function(size) {
68
+ var val = oldRead.call(input, size)
69
+
70
+ if (val === 'skip') {
71
+ input.emit('readable');
72
+ return null
73
+ }
74
+
75
+ return val;
76
+ }
77
+
78
+ data.forEach(function (chunk) { input.write(chunk); });
79
+
80
+ readAllObjects(output, function(vals) {
81
+ assert.deepEqual([1,2,3,4,6,7,8,9], vals);
82
+ done();
83
+ });
84
+
85
+ input.end();
86
+ });
87
+
88
+ test("two sequential inputs", function(done) {
89
+ var input1 = stream();
90
+ var input2 = stream();
91
+ var output = stream();
92
+ var mux = new Mux(output);
93
+ mux.pipeFrom(input1);
94
+ mux.pipeFrom(input2);
95
+
96
+ var data = [1,2,3,4,5,6,7,8,9];
97
+ data.forEach(function(v) { input1.write(v); });
98
+
99
+ input1.on('end', function() {
100
+ data.forEach(function (v) { input2.write(v); });
101
+ input2.end();
102
+ });
103
+ input2.on('end', function() { output.end(); });
104
+
105
+ input1.end();
106
+ readAllObjects(output, function(vs) {
107
+ assert.equal(2 * data.length, vs.length);
108
+ done();
109
+ });
110
+ });
111
+
112
+ test("two interleaved inputs", function(done) {
113
+ var input1 = stream();
114
+ var input2 = stream();
115
+ var output = stream();
116
+ var mux = new Mux(output);
117
+ mux.pipeFrom(input1);
118
+ mux.pipeFrom(input2);
119
+
120
+ var endLatch = latch(2, function() { output.end(); });
121
+ input1.on('end', endLatch);
122
+ input2.on('end', endLatch);
123
+
124
+ var data = [1,2,3,4,5,6,7,8,9];
125
+ data.forEach(function(v) { input1.write(v); });
126
+ input1.end();
127
+
128
+ data.forEach(function(v) { input2.write(v); });
129
+ input2.end();
130
+
131
+ readAllObjects(output, function(vs) {
132
+ assert.equal(2 * data.length, vs.length);
133
+ done();
134
+ });
135
+ });
136
+
137
+ test("unpipe", function(done) {
138
+ var input = stream();
139
+ var output = stream();
140
+ var mux = new Mux(output);
141
+
142
+ var pipedData = [1,2,3,4,5];
143
+ var unpipedData = [6,7,8,9];
144
+
145
+ mux.pipeFrom(input);
146
+
147
+ schedule(function() {
148
+ pipedData.forEach(function (chunk) { input.write(chunk); });
149
+
150
+ schedule(function() {
151
+ mux.unpipeFrom(input);
152
+
153
+ schedule(function() {
154
+ unpipedData.forEach(function(chunk) { input.write(chunk); });
155
+ input.end();
156
+ schedule(function() {
157
+ // exhaust so that 'end' fires
158
+ var v; while (v = input.read());
159
+ });
160
+ });
161
+ });
162
+
163
+ });
164
+
165
+ input.on('end', function() {
166
+ output.end();
167
+ });
168
+
169
+ readAllObjects(output, function(vals) {
170
+ try {
171
+ assert.deepEqual(pipedData, vals);
172
+ done();
173
+ }
174
+ catch (e) { done(e); }
175
+ });
176
+ });
177
+
178
+ test("roundrobin", function(done) {
179
+ var input1 = stream();
180
+ var input2 = stream();
181
+ var output = stream();
182
+ var mux = new Mux(output);
183
+
184
+ mux.pipeFrom(input1);
185
+ mux.pipeFrom(input2);
186
+
187
+ var endLatch = latch(2, function() { output.end(); });
188
+ input1.on('end', endLatch);
189
+ input2.on('end', endLatch);
190
+
191
+ var ones = [1,1,1,1,1];
192
+ ones.forEach(function(v) { input1.write(v); });
193
+ input1.end();
194
+
195
+ var twos = [2,2,2,2,2];
196
+ twos.forEach(function(v) { input2.write(v); });
197
+ input2.end();
198
+
199
+ readAllObjects(output, function(vs) {
200
+ assert.deepEqual([1,2,1,2,1,2,1,2,1,2], vs);
201
+ done();
202
+ });
203
+
204
+ });
@@ -0,0 +1,219 @@
1
+ 'use strict';
2
+
3
+ var crypto = require('crypto');
4
+ var Connection = require('../lib/connection').Connection;
5
+ var PassThrough =
6
+ require('stream').PassThrough ||
7
+ require('readable-stream/passthrough');
8
+ var defs = require('../lib/defs');
9
+ var assert = require('assert');
10
+
11
+ var schedule = (typeof setImmediate === 'function') ?
12
+ setImmediate : process.nextTick;
13
+
14
+ function randomString() {
15
+ var hash = crypto.createHash('sha1');
16
+ hash.update(crypto.randomBytes(64));
17
+ return hash.digest('base64');
18
+ }
19
+
20
+
21
+ // Set up a socket pair {client, server}, such that writes to the
22
+ // client are readable from the server, and writes to the server are
23
+ // readable at the client.
24
+ //
25
+ // +---+ +---+
26
+ // | C | | S |
27
+ // --write->| l |----->| e |--read-->
28
+ // | i | | r |
29
+ // <--read--| e |<-----| v |<-write--
30
+ // | n | | e |
31
+ // | t | | r |
32
+ // +---+ +---+
33
+ //
34
+ // I also need to make sure that end called on either socket affects
35
+ // the other.
36
+
37
+ function socketPair() {
38
+ var server = new PassThrough();
39
+ var client = new PassThrough();
40
+ server.write = client.push.bind(client);
41
+ client.write = server.push.bind(server);
42
+ function end(chunk, encoding) {
43
+ if (chunk) this.push(chunk, encoding);
44
+ this.push(null);
45
+ }
46
+ server.end = end.bind(client);
47
+ client.end = end.bind(server);
48
+
49
+ return {client: client, server: server};
50
+ }
51
+
52
+ function runServer(socket, run) {
53
+ var frames = new Connection(socket);
54
+ // We will be closing the socket without doing a closing handshake,
55
+ // so cheat
56
+ frames.expectSocketClose = true;
57
+ // We also need to create some channel buffers, again a cheat
58
+ frames.freshChannel(null);
59
+ frames.freshChannel(null);
60
+ frames.freshChannel(null);
61
+
62
+ function send(id, fields, channel, content) {
63
+ channel = channel || 0;
64
+ if (content) {
65
+ schedule(function() {
66
+ frames.sendMessage(channel, id, fields,
67
+ defs.BasicProperties, fields,
68
+ content);
69
+ });
70
+ }
71
+ else {
72
+ schedule(function() {
73
+ frames.sendMethod(channel, id, fields);
74
+ });
75
+ }
76
+ }
77
+
78
+ function wait(method) {
79
+ return function() {
80
+ return new Promise(function(resolve, reject) {
81
+ if (method) {
82
+ frames.step(function(e, f) {
83
+ if (e !== null) return reject(e);
84
+ if (f.id === method)
85
+ resolve(f);
86
+ else
87
+ reject(new Error("Expected method: " + method +
88
+ ", got " + f.id));
89
+ });
90
+ }
91
+ else {
92
+ frames.step(function(e, f) {
93
+ if (e !== null) return reject(e);
94
+ else resolve(f);
95
+ });
96
+ }
97
+ });
98
+ };
99
+ }
100
+ run(send, wait);
101
+ return frames;
102
+ }
103
+
104
+ // Produce a callback that will complete the test successfully
105
+ function succeed(done) {
106
+ return function() { done(); }
107
+ }
108
+
109
+ // Produce a callback that will complete the test successfully
110
+ // only if the value is an object, it has the specified
111
+ // attribute, and its value is equals to the expected value
112
+ function succeedIfAttributeEquals(attribute, value, done) {
113
+ return function(object) {
114
+ if (object && !(object instanceof Error) && value === object[attribute]) {
115
+ return done();
116
+ }
117
+
118
+ done(new Error(attribute + " is not equal to " + value));
119
+ };
120
+ }
121
+
122
+ // Produce a callback that will fail the test, given either an error
123
+ // (to be used as a failure continuation) or any other value (to be
124
+ // used as a success continuation when failure is expected)
125
+ function fail(done) {
126
+ return function(err) {
127
+ if (err instanceof Error) done(err);
128
+ else done(new Error("Expected to fail, instead got " + err.toString()));
129
+ }
130
+ }
131
+
132
+ // Create a function that will call done once it's been called itself
133
+ // `count` times. If it's called with an error value, it will
134
+ // immediately call done with that error value.
135
+ function latch(count, done) {
136
+ var awaiting = count;
137
+ var alive = true;
138
+ return function(err) {
139
+ if (err instanceof Error && alive) {
140
+ alive = false;
141
+ done(err);
142
+ }
143
+ else {
144
+ awaiting--;
145
+ if (awaiting === 0 && alive) {
146
+ alive = false;
147
+ done();
148
+ }
149
+ }
150
+ };
151
+ }
152
+
153
+ // Call a thunk with a continuation that will be called with an error
154
+ // if the thunk throws one, or nothing if it runs to completion.
155
+ function completes(thunk, done) {
156
+ try {
157
+ thunk();
158
+ done();
159
+ }
160
+ catch (e) { done(e); }
161
+ }
162
+
163
+ // Construct a Node.JS-style callback from a success continuation and
164
+ // an error continuation
165
+ function kCallback(k, ek) {
166
+ return function(err, val) {
167
+ if (err === null) k && k(val);
168
+ else ek && ek(err);
169
+ };
170
+ }
171
+
172
+ // A noddy way to make tests depend on the node version.
173
+ function versionGreaterThan(actual, spec) {
174
+
175
+ function int(e) { return parseInt(e); }
176
+
177
+ var version = actual.split('.').map(int);
178
+ var desired = spec.split('.').map(int);
179
+ for (var i=0; i < desired.length; i++) {
180
+ var a = version[i], b = desired[i];
181
+ if (a != b) return a > b;
182
+ }
183
+ return false;
184
+ }
185
+
186
+ suite('versionGreaterThan', function() {
187
+
188
+ test('full spec', function() {
189
+ assert(versionGreaterThan('0.8.26', '0.6.12'));
190
+ assert(versionGreaterThan('0.8.26', '0.8.21'));
191
+ });
192
+
193
+ test('partial spec', function() {
194
+ assert(versionGreaterThan('0.9.12', '0.8'));
195
+ });
196
+
197
+ test('not greater', function() {
198
+ assert(!versionGreaterThan('0.8.12', '0.8.26'));
199
+ assert(!versionGreaterThan('0.6.2', '0.6.12'));
200
+ assert(!versionGreaterThan('0.8.29', '0.8'));
201
+ });
202
+
203
+ test
204
+
205
+ });
206
+
207
+ module.exports = {
208
+ socketPair: socketPair,
209
+ runServer: runServer,
210
+ succeed: succeed,
211
+ succeedIfAttributeEquals: succeedIfAttributeEquals,
212
+ fail: fail,
213
+ latch: latch,
214
+ completes: completes,
215
+ kCallback: kCallback,
216
+ schedule: schedule,
217
+ randomString: randomString,
218
+ versionGreaterThan: versionGreaterThan
219
+ };
@@ -0,0 +1,6 @@
1
+ language: node_js
2
+ node_js:
3
+ - "0.11"
4
+ - "0.10"
5
+ - "0.8"
6
+ - "0.6"
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2012 David Wragg. All rights reserved.
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to
5
+ deal in the Software without restriction, including without limitation the
6
+ rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7
+ sell copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
19
+ IN THE SOFTWARE.