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,663 @@
1
+ //
2
+ //
3
+ //
4
+
5
+ 'use strict';
6
+
7
+ var defs = require('./defs');
8
+ var constants = defs.constants;
9
+ var frame = require('./frame');
10
+ var HEARTBEAT = frame.HEARTBEAT;
11
+ var Mux = require('./mux').Mux;
12
+
13
+ var Duplex =
14
+ require('stream').Duplex ||
15
+ require('readable-stream/duplex');
16
+ var EventEmitter = require('events').EventEmitter;
17
+ var Heart = require('./heartbeat').Heart;
18
+
19
+ var methodName = require('./format').methodName;
20
+ var closeMsg = require('./format').closeMessage;
21
+ var inspect = require('./format').inspect;
22
+
23
+ var BitSet = require('./bitset').BitSet;
24
+ var inherits = require('util').inherits;
25
+ var fmt = require('util').format;
26
+ var PassThrough = require('stream').PassThrough ||
27
+ require('readable-stream/passthrough');
28
+ var IllegalOperationError = require('./error').IllegalOperationError;
29
+ var stackCapture = require('./error').stackCapture;
30
+
31
+ // High-water mark for channel write buffers, in 'objects' (which are
32
+ // encoded frames as buffers).
33
+ var DEFAULT_WRITE_HWM = 1024;
34
+ // If all the frames of a message (method, properties, content) total
35
+ // to less than this, copy them into a single buffer and write it all
36
+ // at once. Note that this is less than the minimum frame size: if it
37
+ // was greater, we might have to fragment the content.
38
+ var SINGLE_CHUNK_THRESHOLD = 2048;
39
+
40
+ function Connection(underlying) {
41
+ EventEmitter.call( this );
42
+ var stream = this.stream = wrapStream(underlying);
43
+ this.muxer = new Mux(stream);
44
+
45
+ // frames
46
+ this.rest = Buffer.alloc(0);
47
+ this.frameMax = constants.FRAME_MIN_SIZE;
48
+ this.sentSinceLastCheck = false;
49
+ this.recvSinceLastCheck = false;
50
+
51
+ this.expectSocketClose = false;
52
+ this.freeChannels = new BitSet();
53
+ this.channels = [{channel: {accept: channel0(this)},
54
+ buffer: underlying}];
55
+ }
56
+ inherits(Connection, EventEmitter);
57
+
58
+ var C = Connection.prototype;
59
+
60
+ // Usual frame accept mode
61
+ function mainAccept(frame) {
62
+ var rec = this.channels[frame.channel];
63
+ if (rec) { return rec.channel.accept(frame); }
64
+ // NB CHANNEL_ERROR may not be right, but I don't know what is ..
65
+ else
66
+ this.closeWithError(
67
+ fmt('Frame on unknown channel %d', frame.channel),
68
+ constants.CHANNEL_ERROR,
69
+ new Error(fmt("Frame on unknown channel: %s",
70
+ inspect(frame, false))));
71
+ }
72
+
73
+ // Handle anything that comes through on channel 0, that's the
74
+ // connection control channel. This is only used once mainAccept is
75
+ // installed as the frame handler, after the opening handshake.
76
+ function channel0(connection) {
77
+ return function(f) {
78
+ // Once we get a 'close', we know 1. we'll get no more frames, and
79
+ // 2. anything we send except close, or close-ok, will be
80
+ // ignored. If we already sent 'close', this won't be invoked since
81
+ // we're already in closing mode; if we didn't well we're not going
82
+ // to send it now are we.
83
+ if (f === HEARTBEAT); // ignore; it's already counted as activity
84
+ // on the socket, which is its purpose
85
+ else if (f.id === defs.ConnectionClose) {
86
+ // Oh. OK. I guess we're done here then.
87
+ connection.sendMethod(0, defs.ConnectionCloseOk, {});
88
+ var emsg = fmt('Connection closed: %s', closeMsg(f));
89
+ var s = stackCapture(emsg);
90
+ var e = new Error(emsg);
91
+ e.code = f.fields.replyCode;
92
+ if (isFatalError(e)) {
93
+ connection.emit('error', e);
94
+ }
95
+ connection.toClosed(s, e);
96
+ }
97
+ else if (f.id === defs.ConnectionBlocked) {
98
+ connection.emit('blocked', f.fields.reason);
99
+ }
100
+ else if (f.id === defs.ConnectionUnblocked) {
101
+ connection.emit('unblocked');
102
+ }
103
+ else {
104
+ connection.closeWithError(
105
+ fmt("Unexpected frame on channel 0"),
106
+ constants.UNEXPECTED_FRAME,
107
+ new Error(fmt("Unexpected frame on channel 0: %s",
108
+ inspect(f, false))));
109
+ }
110
+ };
111
+ }
112
+
113
+ // This changed between versions, as did the codec, methods, etc. AMQP
114
+ // 0-9-1 is fairly similar to 0.8, but better, and nothing implements
115
+ // 0.8 that doesn't implement 0-9-1. In other words, it doesn't make
116
+ // much sense to generalise here.
117
+ C.sendProtocolHeader = function() {
118
+ this.sendBytes(frame.PROTOCOL_HEADER);
119
+ };
120
+
121
+ /*
122
+ The frighteningly complicated opening protocol (spec section 2.2.4):
123
+
124
+ Client -> Server
125
+
126
+ protocol header ->
127
+ <- start
128
+ start-ok ->
129
+ .. next two zero or more times ..
130
+ <- secure
131
+ secure-ok ->
132
+ <- tune
133
+ tune-ok ->
134
+ open ->
135
+ <- open-ok
136
+
137
+ If I'm only supporting SASL's PLAIN mechanism (which I am for the time
138
+ being), it gets a bit easier since the server won't in general send
139
+ back a `secure`, it'll just send `tune` after the `start-ok`.
140
+ (SASL PLAIN: http://tools.ietf.org/html/rfc4616)
141
+
142
+ */
143
+
144
+ C.open = function(allFields, openCallback0) {
145
+ var self = this;
146
+ var openCallback = openCallback0 || function() {};
147
+
148
+ // This is where we'll put our negotiated values
149
+ var tunedOptions = Object.create(allFields);
150
+
151
+ function wait(k) {
152
+ self.step(function(err, frame) {
153
+ if (err !== null) bail(err);
154
+ else if (frame.channel !== 0) {
155
+ bail(new Error(
156
+ fmt("Frame on channel != 0 during handshake: %s",
157
+ inspect(frame, false))));
158
+ }
159
+ else k(frame);
160
+ });
161
+ }
162
+
163
+ function expect(Method, k) {
164
+ wait(function(frame) {
165
+ if (frame.id === Method) k(frame);
166
+ else {
167
+ bail(new Error(
168
+ fmt("Expected %s; got %s",
169
+ methodName(Method), inspect(frame, false))));
170
+ }
171
+ });
172
+ }
173
+
174
+ function bail(err) {
175
+ openCallback(err);
176
+ }
177
+
178
+ function send(Method) {
179
+ // This can throw an exception if there's some problem with the
180
+ // options; e.g., something is a string instead of a number.
181
+ self.sendMethod(0, Method, tunedOptions);
182
+ }
183
+
184
+ function negotiate(server, desired) {
185
+ // We get sent values for channelMax, frameMax and heartbeat,
186
+ // which we may accept or lower (subject to a minimum for
187
+ // frameMax, but we'll leave that to the server to enforce). In
188
+ // all cases, `0` really means "no limit", or rather the highest
189
+ // value in the encoding, e.g., unsigned short for channelMax.
190
+ if (server === 0 || desired === 0) {
191
+ // i.e., whichever places a limit, if either
192
+ return Math.max(server, desired);
193
+ }
194
+ else {
195
+ return Math.min(server, desired);
196
+ }
197
+ }
198
+
199
+ function onStart(start) {
200
+ var mechanisms = start.fields.mechanisms.toString().split(' ');
201
+ if (mechanisms.indexOf(allFields.mechanism) < 0) {
202
+ bail(new Error(fmt('SASL mechanism %s is not provided by the server',
203
+ allFields.mechanism)));
204
+ return;
205
+ }
206
+ self.serverProperties = start.fields.serverProperties;
207
+ try {
208
+ send(defs.ConnectionStartOk);
209
+ } catch (err) {
210
+ bail(err);
211
+ return;
212
+ }
213
+ wait(afterStartOk);
214
+ }
215
+
216
+ function afterStartOk(reply) {
217
+ switch (reply.id) {
218
+ case defs.ConnectionSecure:
219
+ bail(new Error(
220
+ "Wasn't expecting to have to go through secure"));
221
+ break;
222
+ case defs.ConnectionClose:
223
+ bail(new Error(fmt("Handshake terminated by server: %s",
224
+ closeMsg(reply))));
225
+ break;
226
+ case defs.ConnectionTune:
227
+ var fields = reply.fields;
228
+ tunedOptions.frameMax =
229
+ negotiate(fields.frameMax, allFields.frameMax);
230
+ tunedOptions.channelMax =
231
+ negotiate(fields.channelMax, allFields.channelMax);
232
+ tunedOptions.heartbeat =
233
+ negotiate(fields.heartbeat, allFields.heartbeat);
234
+ try {
235
+ send(defs.ConnectionTuneOk);
236
+ send(defs.ConnectionOpen);
237
+ } catch (err) {
238
+ bail(err);
239
+ return;
240
+ }
241
+ expect(defs.ConnectionOpenOk, onOpenOk);
242
+ break;
243
+ default:
244
+ bail(new Error(
245
+ fmt("Expected connection.secure, connection.close, " +
246
+ "or connection.tune during handshake; got %s",
247
+ inspect(reply, false))));
248
+ break;
249
+ }
250
+ }
251
+
252
+ function onOpenOk(openOk) {
253
+ // Impose the maximum of the encoded value, if the negotiated
254
+ // value is zero, meaning "no, no limits"
255
+ self.channelMax = tunedOptions.channelMax || 0xffff;
256
+ self.frameMax = tunedOptions.frameMax || 0xffffffff;
257
+ // 0 means "no heartbeat", rather than "maximum period of
258
+ // heartbeating"
259
+ self.heartbeat = tunedOptions.heartbeat;
260
+ self.heartbeater = self.startHeartbeater();
261
+ self.accept = mainAccept;
262
+ succeed(openOk);
263
+ }
264
+
265
+ // If the server closes the connection, it's probably because of
266
+ // something we did
267
+ function endWhileOpening(err) {
268
+ bail(err || new Error('Socket closed abruptly ' +
269
+ 'during opening handshake'));
270
+ }
271
+
272
+ this.stream.on('end', endWhileOpening);
273
+ this.stream.on('error', endWhileOpening);
274
+
275
+ function succeed(ok) {
276
+ self.stream.removeListener('end', endWhileOpening);
277
+ self.stream.removeListener('error', endWhileOpening);
278
+ self.stream.on('error', self.onSocketError.bind(self));
279
+ self.stream.on('end', self.onSocketError.bind(
280
+ self, new Error('Unexpected close')));
281
+ self.on('frameError', self.onSocketError.bind(self));
282
+ self.acceptLoop();
283
+ openCallback(null, ok);
284
+ }
285
+
286
+ // Now kick off the handshake by prompting the server
287
+ this.sendProtocolHeader();
288
+ expect(defs.ConnectionStart, onStart);
289
+ };
290
+
291
+ // Closing things: AMQP has a closing handshake that applies to
292
+ // closing both connects and channels. As the initiating party, I send
293
+ // Close, then ignore all frames until I see either CloseOK --
294
+ // which signifies that the other party has seen the Close and shut
295
+ // the connection or channel down, so it's fine to free resources; or
296
+ // Close, which means the other party also wanted to close the
297
+ // whatever, and I should send CloseOk so it can free resources,
298
+ // then go back to waiting for the CloseOk. If I receive a Close
299
+ // out of the blue, I should throw away any unsent frames (they will
300
+ // be ignored anyway) and send CloseOk, then clean up resources. In
301
+ // general, Close out of the blue signals an error (or a forced
302
+ // closure, which may as well be an error).
303
+ //
304
+ // RUNNING [1] --- send Close ---> Closing [2] ---> recv Close --+
305
+ // | | [3]
306
+ // | +------ send CloseOk ------+
307
+ // recv Close recv CloseOk
308
+ // | |
309
+ // V V
310
+ // Ended [4] ---- send CloseOk ---> Closed [5]
311
+ //
312
+ // [1] All frames accepted; getting a Close frame from the server
313
+ // moves to Ended; client may initiate a close by sending Close
314
+ // itself.
315
+ // [2] Client has initiated a close; only CloseOk or (simulataneously
316
+ // sent) Close is accepted.
317
+ // [3] Simultaneous close
318
+ // [4] Server won't send any more frames; accept no more frames, send
319
+ // CloseOk.
320
+ // [5] Fully closed, client will send no more, server will send no
321
+ // more. Signal 'close' or 'error'.
322
+ //
323
+ // There are two signalling mechanisms used in the API. The first is
324
+ // that calling `close` will return a promise, that will either
325
+ // resolve once the connection or channel is cleanly shut down, or
326
+ // will reject if the shutdown times out.
327
+ //
328
+ // The second is the 'close' and 'error' events. These are
329
+ // emitted as above. The events will fire *before* promises are
330
+ // resolved.
331
+
332
+ // Close the connection without even giving a reason. Typical.
333
+ C.close = function(closeCallback) {
334
+ var k = closeCallback && function() { closeCallback(null); };
335
+ this.closeBecause("Cheers, thanks", constants.REPLY_SUCCESS, k);
336
+ };
337
+
338
+ // Close with a reason and a 'code'. I'm pretty sure RabbitMQ totally
339
+ // ignores these; maybe it logs them. The continuation will be invoked
340
+ // when the CloseOk has been received, and before the 'close' event.
341
+ C.closeBecause = function(reason, code, k) {
342
+ this.sendMethod(0, defs.ConnectionClose, {
343
+ replyText: reason,
344
+ replyCode: code,
345
+ methodId: 0, classId: 0
346
+ });
347
+ var s = stackCapture('closeBecause called: ' + reason);
348
+ this.toClosing(s, k);
349
+ };
350
+
351
+ C.closeWithError = function(reason, code, error) {
352
+ this.emit('error', error);
353
+ this.closeBecause(reason, code);
354
+ };
355
+
356
+ C.onSocketError = function(err) {
357
+ if (!this.expectSocketClose) {
358
+ // forestall any more calls to onSocketError, since we're signed
359
+ // up for `'error'` *and* `'end'`
360
+ this.expectSocketClose = true;
361
+ this.emit('error', err);
362
+ var s = stackCapture('Socket error');
363
+ this.toClosed(s, err);
364
+ }
365
+ };
366
+
367
+ function invalidOp(msg, stack) {
368
+ return function() {
369
+ throw new IllegalOperationError(msg, stack);
370
+ };
371
+ }
372
+
373
+ function invalidateSend(conn, msg, stack) {
374
+ conn.sendMethod = conn.sendContent = conn.sendMessage =
375
+ invalidOp(msg, stack);
376
+ }
377
+
378
+ // A close has been initiated. Repeat: a close has been initiated.
379
+ // This means we should not send more frames, anyway they will be
380
+ // ignored. We also have to shut down all the channels.
381
+ C.toClosing = function(capturedStack, k) {
382
+ var send = this.sendMethod.bind(this);
383
+
384
+ this.accept = function(f) {
385
+ if (f.id === defs.ConnectionCloseOk) {
386
+ if (k) k();
387
+ var s = stackCapture('ConnectionCloseOk received');
388
+ this.toClosed(s, undefined);
389
+ }
390
+ else if (f.id === defs.ConnectionClose) {
391
+ send(0, defs.ConnectionCloseOk, {});
392
+ }
393
+ // else ignore frame
394
+ };
395
+ invalidateSend(this, 'Connection closing', capturedStack);
396
+ };
397
+
398
+ C._closeChannels = function(capturedStack) {
399
+ for (var i = 1; i < this.channels.length; i++) {
400
+ var ch = this.channels[i];
401
+ if (ch !== null) {
402
+ ch.channel.toClosed(capturedStack); // %%% or with an error? not clear
403
+ }
404
+ }
405
+ };
406
+
407
+ // A close has been confirmed. Cease all communication.
408
+ C.toClosed = function(capturedStack, maybeErr) {
409
+ this._closeChannels(capturedStack);
410
+ var info = fmt('Connection closed (%s)',
411
+ (maybeErr) ? maybeErr.toString() : 'by client');
412
+ // Tidy up, invalidate enverything, dynamite the bridges.
413
+ invalidateSend(this, info, capturedStack);
414
+ this.accept = invalidOp(info, capturedStack);
415
+ this.close = function(cb) {
416
+ cb && cb(new IllegalOperationError(info, capturedStack));
417
+ };
418
+ if (this.heartbeater) this.heartbeater.clear();
419
+ // This is certainly true now, if it wasn't before
420
+ this.expectSocketClose = true;
421
+ this.stream.end();
422
+ this.emit('close', maybeErr);
423
+ };
424
+
425
+ // ===
426
+
427
+ C.startHeartbeater = function() {
428
+ if (this.heartbeat === 0) return null;
429
+ else {
430
+ var self = this;
431
+ var hb = new Heart(this.heartbeat,
432
+ this.checkSend.bind(this),
433
+ this.checkRecv.bind(this));
434
+ hb.on('timeout', function() {
435
+ var hberr = new Error("Heartbeat timeout");
436
+ self.emit('error', hberr);
437
+ var s = stackCapture('Heartbeat timeout');
438
+ self.toClosed(s, hberr);
439
+ });
440
+ hb.on('beat', function() {
441
+ self.sendHeartbeat();
442
+ });
443
+ return hb;
444
+ }
445
+ };
446
+
447
+ // I use an array to keep track of the channels, rather than an
448
+ // object. The channel identifiers are numbers, and allocated by the
449
+ // connection. If I try to allocate low numbers when they are
450
+ // available (which I do, by looking from the start of the bitset),
451
+ // this ought to keep the array small, and out of 'sparse array
452
+ // storage'. I also set entries to null, rather than deleting them, in
453
+ // the expectation that the next channel allocation will fill the slot
454
+ // again rather than growing the array. See
455
+ // http://www.html5rocks.com/en/tutorials/speed/v8/
456
+ C.freshChannel = function(channel, options) {
457
+ var next = this.freeChannels.nextClearBit(1);
458
+ if (next < 0 || next > this.channelMax)
459
+ throw new Error("No channels left to allocate");
460
+ this.freeChannels.set(next);
461
+
462
+ var hwm = (options && options.highWaterMark) || DEFAULT_WRITE_HWM;
463
+ var writeBuffer = new PassThrough({
464
+ objectMode: true, highWaterMark: hwm
465
+ });
466
+ this.channels[next] = {channel: channel, buffer: writeBuffer};
467
+ writeBuffer.on('drain', function() {
468
+ channel.onBufferDrain();
469
+ });
470
+ this.muxer.pipeFrom(writeBuffer);
471
+ return next;
472
+ };
473
+
474
+ C.releaseChannel = function(channel) {
475
+ this.freeChannels.clear(channel);
476
+ var buffer = this.channels[channel].buffer;
477
+ buffer.end(); // will also cause it to be unpiped
478
+ this.channels[channel] = null;
479
+ };
480
+
481
+ C.acceptLoop = function() {
482
+ var self = this;
483
+
484
+ function go() {
485
+ try {
486
+ var f; while (f = self.recvFrame()) self.accept(f);
487
+ }
488
+ catch (e) {
489
+ self.emit('frameError', e);
490
+ }
491
+ }
492
+ self.stream.on('readable', go);
493
+ go();
494
+ };
495
+
496
+ C.step = function(cb) {
497
+ var self = this;
498
+ function recv() {
499
+ var f;
500
+ try {
501
+ f = self.recvFrame();
502
+ }
503
+ catch (e) {
504
+ cb(e, null);
505
+ return;
506
+ }
507
+ if (f) cb(null, f);
508
+ else self.stream.once('readable', recv);
509
+ }
510
+ recv();
511
+ };
512
+
513
+ C.checkSend = function() {
514
+ var check = this.sentSinceLastCheck;
515
+ this.sentSinceLastCheck = false;
516
+ return check;
517
+ }
518
+
519
+ C.checkRecv = function() {
520
+ var check = this.recvSinceLastCheck;
521
+ this.recvSinceLastCheck = false;
522
+ return check;
523
+ }
524
+
525
+ C.sendBytes = function(bytes) {
526
+ this.sentSinceLastCheck = true;
527
+ this.stream.write(bytes);
528
+ };
529
+
530
+ C.sendHeartbeat = function() {
531
+ return this.sendBytes(frame.HEARTBEAT_BUF);
532
+ };
533
+
534
+ var encodeMethod = defs.encodeMethod;
535
+ var encodeProperties = defs.encodeProperties;
536
+
537
+ C.sendMethod = function(channel, Method, fields) {
538
+ var frame = encodeMethod(Method, channel, fields);
539
+ this.sentSinceLastCheck = true;
540
+ var buffer = this.channels[channel].buffer;
541
+ return buffer.write(frame);
542
+ };
543
+
544
+ C.sendMessage = function(channel,
545
+ Method, fields,
546
+ Properties, props,
547
+ content) {
548
+ if (!Buffer.isBuffer(content))
549
+ throw new TypeError('content is not a buffer');
550
+
551
+ var mframe = encodeMethod(Method, channel, fields);
552
+ var pframe = encodeProperties(Properties, channel,
553
+ content.length, props);
554
+ var buffer = this.channels[channel].buffer;
555
+ this.sentSinceLastCheck = true;
556
+
557
+ var methodHeaderLen = mframe.length + pframe.length;
558
+ var bodyLen = (content.length > 0) ?
559
+ content.length + FRAME_OVERHEAD : 0;
560
+ var allLen = methodHeaderLen + bodyLen;
561
+
562
+ if (allLen < SINGLE_CHUNK_THRESHOLD) {
563
+ // Use `allocUnsafe` to avoid excessive allocations and CPU usage
564
+ // from zeroing. The returned Buffer is not zeroed and so must be
565
+ // completely filled to be used safely.
566
+ // See https://github.com/amqp-node/amqplib/pull/695
567
+ var all = Buffer.allocUnsafe(allLen);
568
+ var offset = mframe.copy(all, 0);
569
+ offset += pframe.copy(all, offset);
570
+
571
+ if (bodyLen > 0)
572
+ makeBodyFrame(channel, content).copy(all, offset);
573
+ return buffer.write(all);
574
+ }
575
+ else {
576
+ if (methodHeaderLen < SINGLE_CHUNK_THRESHOLD) {
577
+ // Use `allocUnsafe` to avoid excessive allocations and CPU usage
578
+ // from zeroing. The returned Buffer is not zeroed and so must be
579
+ // completely filled to be used safely.
580
+ // See https://github.com/amqp-node/amqplib/pull/695
581
+ var both = Buffer.allocUnsafe(methodHeaderLen);
582
+ var offset = mframe.copy(both, 0);
583
+ pframe.copy(both, offset);
584
+ buffer.write(both);
585
+ }
586
+ else {
587
+ buffer.write(mframe);
588
+ buffer.write(pframe);
589
+ }
590
+ return this.sendContent(channel, content);
591
+ }
592
+ };
593
+
594
+ var FRAME_OVERHEAD = defs.FRAME_OVERHEAD;
595
+ var makeBodyFrame = frame.makeBodyFrame;
596
+
597
+ C.sendContent = function(channel, body) {
598
+ if (!Buffer.isBuffer(body)) {
599
+ throw new TypeError(fmt("Expected buffer; got %s", body));
600
+ }
601
+ var writeResult = true;
602
+ var buffer = this.channels[channel].buffer;
603
+
604
+ var maxBody = this.frameMax - FRAME_OVERHEAD;
605
+
606
+ for (var offset = 0; offset < body.length; offset += maxBody) {
607
+ var end = offset + maxBody;
608
+ var slice = (end > body.length) ? body.slice(offset) : body.slice(offset, end);
609
+ var bodyFrame = makeBodyFrame(channel, slice);
610
+ writeResult = buffer.write(bodyFrame);
611
+ }
612
+ this.sentSinceLastCheck = true;
613
+ return writeResult;
614
+ };
615
+
616
+ var parseFrame = frame.parseFrame;
617
+ var decodeFrame = frame.decodeFrame;
618
+
619
+ C.recvFrame = function() {
620
+ // %%% identifying invariants might help here?
621
+ var frame = parseFrame(this.rest, this.frameMax);
622
+
623
+ if (!frame) {
624
+ var incoming = this.stream.read();
625
+ if (incoming === null) {
626
+ return false;
627
+ }
628
+ else {
629
+ this.recvSinceLastCheck = true;
630
+ this.rest = Buffer.concat([this.rest, incoming]);
631
+ return this.recvFrame();
632
+ }
633
+ }
634
+ else {
635
+ this.rest = frame.rest;
636
+ return decodeFrame(frame);
637
+ }
638
+ };
639
+
640
+ function wrapStream(s) {
641
+ if (s instanceof Duplex) return s;
642
+ else {
643
+ var ws = new Duplex();
644
+ ws.wrap(s); //wraps the readable side of things
645
+ ws._write = function(chunk, encoding, callback) {
646
+ return s.write(chunk, encoding, callback);
647
+ };
648
+ return ws;
649
+ }
650
+ }
651
+
652
+ function isFatalError(error) {
653
+ switch (error && error.code) {
654
+ case defs.constants.CONNECTION_FORCED:
655
+ case defs.constants.REPLY_SUCCESS:
656
+ return false;
657
+ default:
658
+ return true;
659
+ }
660
+ }
661
+
662
+ module.exports.Connection = Connection;
663
+ module.exports.isFatalError = isFatalError;
@@ -0,0 +1,42 @@
1
+ //
2
+ //
3
+ //
4
+
5
+ // Different kind of credentials that can be supplied when opening a
6
+ // connection, corresponding to SASL mechanisms There's only two
7
+ // useful mechanisms that RabbitMQ implements:
8
+ // * PLAIN (send username and password in the plain)
9
+ // * EXTERNAL (assume the server will figure out who you are from
10
+ // context, i.e., your SSL certificate)
11
+ var codec = require('./codec')
12
+
13
+ module.exports.plain = function(user, passwd) {
14
+ return {
15
+ mechanism: 'PLAIN',
16
+ response: function() {
17
+ return Buffer.from(['', user, passwd].join(String.fromCharCode(0)))
18
+ },
19
+ username: user,
20
+ password: passwd
21
+ }
22
+ }
23
+
24
+ module.exports.amqplain = function(user, passwd) {
25
+ return {
26
+ mechanism: 'AMQPLAIN',
27
+ response: function() {
28
+ const buffer = Buffer.alloc(16384);
29
+ const size = codec.encodeTable(buffer, { LOGIN: user, PASSWORD: passwd}, 0);
30
+ return buffer.slice(4, size);
31
+ },
32
+ username: user,
33
+ password: passwd
34
+ }
35
+ }
36
+
37
+ module.exports.external = function() {
38
+ return {
39
+ mechanism: 'EXTERNAL',
40
+ response: function() { return Buffer.from(''); }
41
+ }
42
+ }