@databricks/sql 1.1.0 → 1.2.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 (108) hide show
  1. package/dist/DBSQLClient.d.ts +3 -1
  2. package/dist/DBSQLClient.js +11 -8
  3. package/dist/DBSQLClient.js.map +1 -1
  4. package/dist/DBSQLOperation/SchemaHelper.d.ts +1 -1
  5. package/dist/DBSQLOperation/SchemaHelper.js +4 -2
  6. package/dist/DBSQLOperation/SchemaHelper.js.map +1 -1
  7. package/dist/DBSQLSession.js +20 -1
  8. package/dist/DBSQLSession.js.map +1 -1
  9. package/dist/connection/auth/PlainHttpAuthentication.d.ts +1 -1
  10. package/dist/connection/contracts/IConnectionOptions.d.ts +1 -1
  11. package/dist/connection/contracts/ITransport.d.ts +1 -1
  12. package/dist/connection/types/AuthOptions.d.ts +1 -1
  13. package/dist/contracts/IDBSQLSession.d.ts +10 -10
  14. package/dist/contracts/IOperation.d.ts +1 -1
  15. package/dist/dto/InfoValue.d.ts +1 -1
  16. package/dist/dto/Status.d.ts +1 -1
  17. package/dist/globalConfig.d.ts +5 -0
  18. package/dist/globalConfig.js +7 -0
  19. package/dist/globalConfig.js.map +1 -0
  20. package/dist/hive/Types/index.d.ts +1 -1
  21. package/dist/result/ArrowResult.d.ts +14 -0
  22. package/dist/result/ArrowResult.js +105 -0
  23. package/dist/result/ArrowResult.js.map +1 -0
  24. package/dist/result/JsonResult.d.ts +1 -5
  25. package/dist/result/JsonResult.js +5 -59
  26. package/dist/result/JsonResult.js.map +1 -1
  27. package/dist/result/utils.d.ts +3 -0
  28. package/dist/result/utils.js +77 -0
  29. package/dist/result/utils.js.map +1 -0
  30. package/dist/version.d.ts +1 -1
  31. package/dist/version.js +1 -1
  32. package/node_modules/async-limiter/.eslintignore +2 -0
  33. package/node_modules/async-limiter/.nycrc +10 -0
  34. package/node_modules/async-limiter/.travis.yml +9 -0
  35. package/node_modules/async-limiter/LICENSE +8 -0
  36. package/node_modules/async-limiter/index.js +67 -0
  37. package/node_modules/async-limiter/package.json +35 -0
  38. package/node_modules/async-limiter/readme.md +132 -0
  39. package/node_modules/browser-or-node/LICENSE +21 -0
  40. package/node_modules/browser-or-node/README.md +63 -0
  41. package/node_modules/browser-or-node/lib/index.js +31 -0
  42. package/node_modules/browser-or-node/package.json +60 -0
  43. package/node_modules/browser-or-node/src/index.js +26 -0
  44. package/node_modules/isomorphic-ws/LICENSE +21 -0
  45. package/node_modules/isomorphic-ws/README.md +58 -0
  46. package/node_modules/isomorphic-ws/browser.js +17 -0
  47. package/node_modules/isomorphic-ws/index.d.ts +8 -0
  48. package/node_modules/isomorphic-ws/node.js +3 -0
  49. package/node_modules/isomorphic-ws/package.json +34 -0
  50. package/node_modules/node-int64/.npmignore +3 -0
  51. package/node_modules/node-int64/Int64.js +268 -0
  52. package/node_modules/node-int64/LICENSE +19 -0
  53. package/node_modules/node-int64/README.md +78 -0
  54. package/node_modules/node-int64/package.json +27 -0
  55. package/node_modules/node-int64/test.js +120 -0
  56. package/node_modules/q/CHANGES.md +800 -0
  57. package/node_modules/q/LICENSE +18 -0
  58. package/node_modules/q/README.md +874 -0
  59. package/node_modules/q/package.json +76 -0
  60. package/node_modules/q/q.js +2076 -0
  61. package/node_modules/q/queue.js +35 -0
  62. package/node_modules/thrift/LICENSE +306 -0
  63. package/node_modules/thrift/README.md +210 -0
  64. package/node_modules/thrift/lib/nodejs/README.md +142 -0
  65. package/node_modules/thrift/lib/nodejs/lib/thrift/binary.js +168 -0
  66. package/node_modules/thrift/lib/nodejs/lib/thrift/binary_protocol.js +367 -0
  67. package/node_modules/thrift/lib/nodejs/lib/thrift/browser.js +49 -0
  68. package/node_modules/thrift/lib/nodejs/lib/thrift/buffered_transport.js +186 -0
  69. package/node_modules/thrift/lib/nodejs/lib/thrift/compact_protocol.js +915 -0
  70. package/node_modules/thrift/lib/nodejs/lib/thrift/connection.js +396 -0
  71. package/node_modules/thrift/lib/nodejs/lib/thrift/create_client.js +54 -0
  72. package/node_modules/thrift/lib/nodejs/lib/thrift/framed_transport.js +185 -0
  73. package/node_modules/thrift/lib/nodejs/lib/thrift/header_protocol.js +258 -0
  74. package/node_modules/thrift/lib/nodejs/lib/thrift/header_transport.js +341 -0
  75. package/node_modules/thrift/lib/nodejs/lib/thrift/http_connection.js +282 -0
  76. package/node_modules/thrift/lib/nodejs/lib/thrift/index.js +76 -0
  77. package/node_modules/thrift/lib/nodejs/lib/thrift/input_buffer_underrun_error.js +32 -0
  78. package/node_modules/thrift/lib/nodejs/lib/thrift/int64_util.js +91 -0
  79. package/node_modules/thrift/lib/nodejs/lib/thrift/json_parse.js +299 -0
  80. package/node_modules/thrift/lib/nodejs/lib/thrift/json_protocol.js +799 -0
  81. package/node_modules/thrift/lib/nodejs/lib/thrift/log.js +87 -0
  82. package/node_modules/thrift/lib/nodejs/lib/thrift/multiplexed_processor.js +63 -0
  83. package/node_modules/thrift/lib/nodejs/lib/thrift/multiplexed_protocol.js +73 -0
  84. package/node_modules/thrift/lib/nodejs/lib/thrift/protocol.js +22 -0
  85. package/node_modules/thrift/lib/nodejs/lib/thrift/server.js +123 -0
  86. package/node_modules/thrift/lib/nodejs/lib/thrift/thrift.js +241 -0
  87. package/node_modules/thrift/lib/nodejs/lib/thrift/transport.js +22 -0
  88. package/node_modules/thrift/lib/nodejs/lib/thrift/web_server.js +567 -0
  89. package/node_modules/thrift/lib/nodejs/lib/thrift/ws_connection.js +286 -0
  90. package/node_modules/thrift/lib/nodejs/lib/thrift/ws_transport.js +206 -0
  91. package/node_modules/thrift/lib/nodejs/lib/thrift/xhr_connection.js +280 -0
  92. package/node_modules/thrift/package.json +72 -0
  93. package/node_modules/ws/LICENSE +21 -0
  94. package/node_modules/ws/README.md +417 -0
  95. package/node_modules/ws/index.js +9 -0
  96. package/node_modules/ws/lib/buffer-util.js +72 -0
  97. package/node_modules/ws/lib/constants.js +10 -0
  98. package/node_modules/ws/lib/event-target.js +170 -0
  99. package/node_modules/ws/lib/extension.js +211 -0
  100. package/node_modules/ws/lib/permessage-deflate.js +516 -0
  101. package/node_modules/ws/lib/receiver.js +513 -0
  102. package/node_modules/ws/lib/sender.js +401 -0
  103. package/node_modules/ws/lib/validation.js +29 -0
  104. package/node_modules/ws/lib/websocket-server.js +369 -0
  105. package/node_modules/ws/lib/websocket.js +828 -0
  106. package/node_modules/ws/package.json +44 -0
  107. package/package.json +12 -8
  108. package/CHANGELOG.md +0 -56
@@ -0,0 +1,396 @@
1
+ /*
2
+ * Licensed to the Apache Software Foundation (ASF) under one
3
+ * or more contributor license agreements. See the NOTICE file
4
+ * distributed with this work for additional information
5
+ * regarding copyright ownership. The ASF licenses this file
6
+ * to you under the Apache License, Version 2.0 (the
7
+ * "License"); you may not use this file except in compliance
8
+ * with the License. You may obtain a copy of the License at
9
+ *
10
+ * http://www.apache.org/licenses/LICENSE-2.0
11
+ *
12
+ * Unless required by applicable law or agreed to in writing,
13
+ * software distributed under the License is distributed on an
14
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15
+ * KIND, either express or implied. See the License for the
16
+ * specific language governing permissions and limitations
17
+ * under the License.
18
+ */
19
+ var util = require('util');
20
+ var EventEmitter = require('events').EventEmitter;
21
+ var constants = require('constants');
22
+ var net = require('net');
23
+ var tls = require('tls');
24
+ var thrift = require('./thrift');
25
+ var log = require('./log');
26
+
27
+ var TBufferedTransport = require('./buffered_transport');
28
+ var TBinaryProtocol = require('./binary_protocol');
29
+ var InputBufferUnderrunError = require('./input_buffer_underrun_error');
30
+
31
+ var createClient = require('./create_client');
32
+
33
+ var binary = require('./binary');
34
+
35
+ var Connection = exports.Connection = function(stream, options) {
36
+ var self = this;
37
+ EventEmitter.call(this);
38
+
39
+ this.seqId2Service = {};
40
+ this.connection = stream;
41
+ this.ssl = (stream.encrypted);
42
+ this.options = options || {};
43
+ this.transport = this.options.transport || TBufferedTransport;
44
+ this.protocol = this.options.protocol || TBinaryProtocol;
45
+ this.offline_queue = [];
46
+ this.connected = false;
47
+ this.initialize_retry_vars();
48
+
49
+ this._debug = this.options.debug || false;
50
+ if (this.options.max_attempts &&
51
+ !isNaN(this.options.max_attempts) &&
52
+ this.options.max_attempts > 0) {
53
+ this.max_attempts = +this.options.max_attempts;
54
+ }
55
+ this.retry_max_delay = null;
56
+ if (this.options.retry_max_delay !== undefined &&
57
+ !isNaN(this.options.retry_max_delay) &&
58
+ this.options.retry_max_delay > 0) {
59
+ this.retry_max_delay = this.options.retry_max_delay;
60
+ }
61
+ this.connect_timeout = false;
62
+ if (this.options.connect_timeout &&
63
+ !isNaN(this.options.connect_timeout) &&
64
+ this.options.connect_timeout > 0) {
65
+ this.connect_timeout = +this.options.connect_timeout;
66
+ }
67
+
68
+ this.connection.addListener(this.ssl ? "secureConnect" : "connect", function() {
69
+ self.connected = true;
70
+
71
+ this.setTimeout(self.options.timeout || 0);
72
+ this.setNoDelay();
73
+ this.frameLeft = 0;
74
+ this.framePos = 0;
75
+ this.frame = null;
76
+ self.initialize_retry_vars();
77
+ self.flush_offline_queue();
78
+
79
+ self.emit("connect");
80
+ });
81
+
82
+ this.connection.addListener("error", function(err) {
83
+ // Only emit the error if no-one else is listening on the connection
84
+ // or if someone is listening on us, because Node turns unhandled
85
+ // 'error' events into exceptions.
86
+ if (self.connection.listeners('error').length === 1 ||
87
+ self.listeners('error').length > 0) {
88
+ self.emit("error", err);
89
+ }
90
+ });
91
+
92
+ // Add a close listener
93
+ this.connection.addListener("close", function() {
94
+ self.connection_gone(); // handle close event. try to reconnect
95
+ });
96
+
97
+ this.connection.addListener("timeout", function() {
98
+ self.emit("timeout");
99
+ });
100
+
101
+ this.connection.addListener("data", self.transport.receiver(function(transport_with_data) {
102
+ var message = new self.protocol(transport_with_data);
103
+ try {
104
+ while (true) {
105
+ var header = message.readMessageBegin();
106
+ var dummy_seqid = header.rseqid * -1;
107
+ var client = self.client;
108
+ //The Multiplexed Protocol stores a hash of seqid to service names
109
+ // in seqId2Service. If the SeqId is found in the hash we need to
110
+ // lookup the appropriate client for this call.
111
+ // The connection.client object is a single client object when not
112
+ // multiplexing, when using multiplexing it is a service name keyed
113
+ // hash of client objects.
114
+ //NOTE: The 2 way interdependencies between protocols, transports,
115
+ // connections and clients in the Node.js implementation are irregular
116
+ // and make the implementation difficult to extend and maintain. We
117
+ // should bring this stuff inline with typical thrift I/O stack
118
+ // operation soon.
119
+ // --ra
120
+ var service_name = self.seqId2Service[header.rseqid];
121
+ if (service_name) {
122
+ client = self.client[service_name];
123
+ }
124
+ /*jshint -W083 */
125
+ client._reqs[dummy_seqid] = function(err, success){
126
+ transport_with_data.commitPosition();
127
+
128
+ var callback = client._reqs[header.rseqid];
129
+ delete client._reqs[header.rseqid];
130
+ if (service_name) {
131
+ delete self.seqId2Service[header.rseqid];
132
+ }
133
+ if (callback) {
134
+ callback(err, success);
135
+ }
136
+ };
137
+ /*jshint +W083 */
138
+
139
+ if(client['recv_' + header.fname]) {
140
+ client['recv_' + header.fname](message, header.mtype, dummy_seqid);
141
+ } else {
142
+ delete client._reqs[dummy_seqid];
143
+ self.emit("error",
144
+ new thrift.TApplicationException(thrift.TApplicationExceptionType.WRONG_METHOD_NAME,
145
+ "Received a response to an unknown RPC function"));
146
+ }
147
+ }
148
+ }
149
+ catch (e) {
150
+ if (e instanceof InputBufferUnderrunError) {
151
+ transport_with_data.rollbackPosition();
152
+ }
153
+ else {
154
+ self.emit('error', e);
155
+ }
156
+ }
157
+ }));
158
+ };
159
+ util.inherits(Connection, EventEmitter);
160
+
161
+ Connection.prototype.end = function() {
162
+ this.connection.end();
163
+ };
164
+
165
+ Connection.prototype.destroy = function() {
166
+ this.connection.destroy();
167
+ };
168
+
169
+ Connection.prototype.initialize_retry_vars = function () {
170
+ this.retry_timer = null;
171
+ this.retry_totaltime = 0;
172
+ this.retry_delay = 150;
173
+ this.retry_backoff = 1.7;
174
+ this.attempts = 0;
175
+ };
176
+
177
+ Connection.prototype.flush_offline_queue = function () {
178
+ var self = this;
179
+ var offline_queue = this.offline_queue;
180
+
181
+ // Reset offline queue
182
+ this.offline_queue = [];
183
+ // Attempt to write queued items
184
+ offline_queue.forEach(function(data) {
185
+ self.write(data);
186
+ });
187
+ };
188
+
189
+ Connection.prototype.write = function(data) {
190
+ if (!this.connected) {
191
+ this.offline_queue.push(data);
192
+ return;
193
+ }
194
+ this.connection.write(data);
195
+ };
196
+
197
+ Connection.prototype.connection_gone = function () {
198
+ var self = this;
199
+ this.connected = false;
200
+
201
+ // If a retry is already in progress, just let that happen
202
+ if (this.retry_timer) {
203
+ return;
204
+ }
205
+ // We cannot reconnect a secure socket.
206
+ if (!this.max_attempts || this.ssl) {
207
+ self.emit("close");
208
+ return;
209
+ }
210
+
211
+ if (this.retry_max_delay !== null && this.retry_delay >= this.retry_max_delay) {
212
+ this.retry_delay = this.retry_max_delay;
213
+ } else {
214
+ this.retry_delay = Math.floor(this.retry_delay * this.retry_backoff);
215
+ }
216
+
217
+ log.debug("Retry connection in " + this.retry_delay + " ms");
218
+
219
+ if (this.max_attempts && this.attempts >= this.max_attempts) {
220
+ this.retry_timer = null;
221
+ console.error("thrift: Couldn't get thrift connection after " + this.max_attempts + " attempts.");
222
+ self.emit("close");
223
+ return;
224
+ }
225
+
226
+ this.attempts += 1;
227
+ this.emit("reconnecting", {
228
+ delay: self.retry_delay,
229
+ attempt: self.attempts
230
+ });
231
+
232
+ this.retry_timer = setTimeout(function () {
233
+ if (self.connection.destroyed) {
234
+ self.retry_timer = null;
235
+ return;
236
+ }
237
+
238
+ log.debug("Retrying connection...");
239
+
240
+ self.retry_totaltime += self.retry_delay;
241
+
242
+ if (self.connect_timeout && self.retry_totaltime >= self.connect_timeout) {
243
+ self.retry_timer = null;
244
+ console.error("thrift: Couldn't get thrift connection after " + self.retry_totaltime + "ms.");
245
+ self.emit("close");
246
+ return;
247
+ }
248
+
249
+ if (self.path !== undefined) {
250
+ self.connection.connect(self.path);
251
+ } else {
252
+ self.connection.connect(self.port, self.host);
253
+ }
254
+ self.retry_timer = null;
255
+ }, this.retry_delay);
256
+ };
257
+
258
+ exports.createConnection = function(host, port, options) {
259
+ var stream = net.createConnection( {
260
+ port: port,
261
+ host: host,
262
+ timeout: options.connect_timeout || options.timeout || 0
263
+ });
264
+ var connection = new Connection(stream, options);
265
+ connection.host = host;
266
+ connection.port = port;
267
+
268
+ return connection;
269
+ };
270
+
271
+ exports.createUDSConnection = function(path, options) {
272
+ var stream = net.createConnection(path);
273
+ var connection = new Connection(stream, options);
274
+ connection.path = path;
275
+
276
+ return connection;
277
+ };
278
+
279
+ exports.createSSLConnection = function(host, port, options) {
280
+ if (!('secureProtocol' in options) && !('secureOptions' in options)) {
281
+ options.secureProtocol = "SSLv23_method";
282
+ options.secureOptions = constants.SSL_OP_NO_SSLv2 | constants.SSL_OP_NO_SSLv3;
283
+ }
284
+
285
+ var stream = tls.connect(port, host, options);
286
+ var connection = new Connection(stream, options);
287
+ connection.host = host;
288
+ connection.port = port;
289
+
290
+ return connection;
291
+ };
292
+
293
+
294
+ exports.createClient = createClient;
295
+
296
+ var child_process = require('child_process');
297
+ var StdIOConnection = exports.StdIOConnection = function(command, options) {
298
+ var command_parts = command.split(' ');
299
+ command = command_parts[0];
300
+ var args = command_parts.splice(1,command_parts.length -1);
301
+ var child = this.child = child_process.spawn(command,args);
302
+
303
+ var self = this;
304
+ EventEmitter.call(this);
305
+
306
+ this.connection = child.stdin;
307
+ this.options = options || {};
308
+ this.transport = this.options.transport || TBufferedTransport;
309
+ this.protocol = this.options.protocol || TBinaryProtocol;
310
+ this.offline_queue = [];
311
+
312
+ if (log.getLogLevel() === 'debug') {
313
+ this.child.stderr.on('data', function (err) {
314
+ log.debug(err.toString(), 'CHILD ERROR');
315
+ });
316
+
317
+ this.child.on('exit', function (code,signal) {
318
+ log.debug(code + ':' + signal, 'CHILD EXITED');
319
+ });
320
+ }
321
+
322
+ this.frameLeft = 0;
323
+ this.framePos = 0;
324
+ this.frame = null;
325
+ this.connected = true;
326
+
327
+ self.flush_offline_queue();
328
+
329
+ this.connection.addListener("error", function(err) {
330
+ self.emit("error", err);
331
+ });
332
+
333
+ // Add a close listener
334
+ this.connection.addListener("close", function() {
335
+ self.emit("close");
336
+ });
337
+
338
+ child.stdout.addListener("data", self.transport.receiver(function(transport_with_data) {
339
+ var message = new self.protocol(transport_with_data);
340
+ try {
341
+ var header = message.readMessageBegin();
342
+ var dummy_seqid = header.rseqid * -1;
343
+ var client = self.client;
344
+ client._reqs[dummy_seqid] = function(err, success){
345
+ transport_with_data.commitPosition();
346
+
347
+ var callback = client._reqs[header.rseqid];
348
+ delete client._reqs[header.rseqid];
349
+ if (callback) {
350
+ callback(err, success);
351
+ }
352
+ };
353
+ client['recv_' + header.fname](message, header.mtype, dummy_seqid);
354
+ }
355
+ catch (e) {
356
+ if (e instanceof InputBufferUnderrunError) {
357
+ transport_with_data.rollbackPosition();
358
+ }
359
+ else {
360
+ throw e;
361
+ }
362
+ }
363
+ }));
364
+ };
365
+
366
+ util.inherits(StdIOConnection, EventEmitter);
367
+
368
+ StdIOConnection.prototype.end = function() {
369
+ this.connection.end();
370
+ };
371
+
372
+ StdIOConnection.prototype.flush_offline_queue = function () {
373
+ var self = this;
374
+ var offline_queue = this.offline_queue;
375
+
376
+ // Reset offline queue
377
+ this.offline_queue = [];
378
+ // Attempt to write queued items
379
+ offline_queue.forEach(function(data) {
380
+ self.write(data);
381
+ });
382
+ };
383
+
384
+ StdIOConnection.prototype.write = function(data) {
385
+ if (!this.connected) {
386
+ this.offline_queue.push(data);
387
+ return;
388
+ }
389
+ this.connection.write(data);
390
+ };
391
+
392
+ exports.createStdIOConnection = function(command,options){
393
+ return new StdIOConnection(command,options);
394
+ };
395
+
396
+ exports.createStdIOClient = createClient;
@@ -0,0 +1,54 @@
1
+ /*
2
+ * Licensed to the Apache Software Foundation (ASF) under one
3
+ * or more contributor license agreements. See the NOTICE file
4
+ * distributed with this work for additional information
5
+ * regarding copyright ownership. The ASF licenses this file
6
+ * to you under the Apache License, Version 2.0 (the
7
+ * "License"); you may not use this file except in compliance
8
+ * with the License. You may obtain a copy of the License at
9
+ *
10
+ * http://www.apache.org/licenses/LICENSE-2.0
11
+ *
12
+ * Unless required by applicable law or agreed to in writing,
13
+ * software distributed under the License is distributed on an
14
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15
+ * KIND, either express or implied. See the License for the
16
+ * specific language governing permissions and limitations
17
+ * under the License.
18
+ */
19
+
20
+ module.exports = createClient;
21
+
22
+ /**
23
+ * Creates a new client object for the specified Thrift service.
24
+ * @param {object} ServiceClient - The module containing the generated service client
25
+ * @param {Connection} Connection - The connection to use.
26
+ * @returns {object} The client object.
27
+ */
28
+ function createClient(ServiceClient, connection) {
29
+ // TODO validate required options and throw otherwise
30
+ if (ServiceClient.Client) {
31
+ ServiceClient = ServiceClient.Client;
32
+ }
33
+ // TODO detangle these initialization calls
34
+ // creating "client" requires
35
+ // - new service client instance
36
+ //
37
+ // New service client instance requires
38
+ // - new transport instance
39
+ // - protocol class reference
40
+ //
41
+ // New transport instance requires
42
+ // - Buffer to use (or none)
43
+ // - Callback to call on flush
44
+
45
+ // Wrap the write method
46
+ var writeCb = function(buf, seqid) {
47
+ connection.write(buf, seqid);
48
+ };
49
+ var transport = new connection.transport(undefined, writeCb);
50
+ var client = new ServiceClient(transport, connection.protocol);
51
+ transport.client = client;
52
+ connection.client = client;
53
+ return client;
54
+ };
@@ -0,0 +1,185 @@
1
+ /*
2
+ * Licensed to the Apache Software Foundation (ASF) under one
3
+ * or more contributor license agreements. See the NOTICE file
4
+ * distributed with this work for additional information
5
+ * regarding copyright ownership. The ASF licenses this file
6
+ * to you under the Apache License, Version 2.0 (the
7
+ * "License"); you may not use this file except in compliance
8
+ * with the License. You may obtain a copy of the License at
9
+ *
10
+ * http://www.apache.org/licenses/LICENSE-2.0
11
+ *
12
+ * Unless required by applicable law or agreed to in writing,
13
+ * software distributed under the License is distributed on an
14
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15
+ * KIND, either express or implied. See the License for the
16
+ * specific language governing permissions and limitations
17
+ * under the License.
18
+ */
19
+
20
+ var binary = require('./binary');
21
+ var InputBufferUnderrunError = require('./input_buffer_underrun_error');
22
+ var THeaderTransport = require('./header_transport');
23
+
24
+ module.exports = TFramedTransport;
25
+
26
+ function TFramedTransport(buffer, callback) {
27
+ this.inBuf = buffer || new Buffer(0);
28
+ this.outBuffers = [];
29
+ this.outCount = 0;
30
+ this.readPos = 0;
31
+ this.onFlush = callback;
32
+ };
33
+
34
+ TFramedTransport.prototype = new THeaderTransport();
35
+
36
+ TFramedTransport.receiver = function(callback, seqid) {
37
+ var residual = null;
38
+
39
+ return function(data) {
40
+ // Prepend any residual data from our previous read
41
+ if (residual) {
42
+ data = Buffer.concat([residual, data]);
43
+ residual = null;
44
+ }
45
+
46
+ // framed transport
47
+ while (data.length) {
48
+ if (data.length < 4) {
49
+ // Not enough bytes to continue, save and resume on next packet
50
+ residual = data;
51
+ return;
52
+ }
53
+ var frameSize = binary.readI32(data, 0);
54
+ if (data.length < 4 + frameSize) {
55
+ // Not enough bytes to continue, save and resume on next packet
56
+ residual = data;
57
+ return;
58
+ }
59
+
60
+ var frame = data.slice(4, 4 + frameSize);
61
+ residual = data.slice(4 + frameSize);
62
+
63
+ callback(new TFramedTransport(frame), seqid);
64
+
65
+ data = residual;
66
+ residual = null;
67
+ }
68
+ };
69
+ };
70
+
71
+ TFramedTransport.prototype.commitPosition = function(){},
72
+ TFramedTransport.prototype.rollbackPosition = function(){},
73
+
74
+ // TODO: Implement open/close support
75
+ TFramedTransport.prototype.isOpen = function() {
76
+ return true;
77
+ };
78
+ TFramedTransport.prototype.open = function() {};
79
+ TFramedTransport.prototype.close = function() {};
80
+
81
+ // Set the seqid of the message in the client
82
+ // So that callbacks can be found
83
+ TFramedTransport.prototype.setCurrSeqId = function(seqid) {
84
+ this._seqid = seqid;
85
+ };
86
+
87
+ TFramedTransport.prototype.ensureAvailable = function(len) {
88
+ if (this.readPos + len > this.inBuf.length) {
89
+ throw new InputBufferUnderrunError();
90
+ }
91
+ };
92
+
93
+ TFramedTransport.prototype.read = function(len) { // this function will be used for each frames.
94
+ this.ensureAvailable(len);
95
+ var end = this.readPos + len;
96
+
97
+ if (this.inBuf.length < end) {
98
+ throw new Error('read(' + len + ') failed - not enough data');
99
+ }
100
+
101
+ var buf = this.inBuf.slice(this.readPos, end);
102
+ this.readPos = end;
103
+ return buf;
104
+ };
105
+
106
+ TFramedTransport.prototype.readByte = function() {
107
+ this.ensureAvailable(1);
108
+ return binary.readByte(this.inBuf[this.readPos++]);
109
+ };
110
+
111
+ TFramedTransport.prototype.readI16 = function() {
112
+ this.ensureAvailable(2);
113
+ var i16 = binary.readI16(this.inBuf, this.readPos);
114
+ this.readPos += 2;
115
+ return i16;
116
+ };
117
+
118
+ TFramedTransport.prototype.readI32 = function() {
119
+ this.ensureAvailable(4);
120
+ var i32 = binary.readI32(this.inBuf, this.readPos);
121
+ this.readPos += 4;
122
+ return i32;
123
+ };
124
+
125
+ TFramedTransport.prototype.readDouble = function() {
126
+ this.ensureAvailable(8);
127
+ var d = binary.readDouble(this.inBuf, this.readPos);
128
+ this.readPos += 8;
129
+ return d;
130
+ };
131
+
132
+ TFramedTransport.prototype.readString = function(len) {
133
+ this.ensureAvailable(len);
134
+ var str = this.inBuf.toString('utf8', this.readPos, this.readPos + len);
135
+ this.readPos += len;
136
+ return str;
137
+ };
138
+
139
+ TFramedTransport.prototype.borrow = function() {
140
+ return {
141
+ buf: this.inBuf,
142
+ readIndex: this.readPos,
143
+ writeIndex: this.inBuf.length
144
+ };
145
+ };
146
+
147
+ TFramedTransport.prototype.consume = function(bytesConsumed) {
148
+ this.readPos += bytesConsumed;
149
+ };
150
+
151
+ TFramedTransport.prototype.write = function(buf, encoding) {
152
+ if (typeof(buf) === "string") {
153
+ buf = new Buffer(buf, encoding || 'utf8');
154
+ }
155
+ this.outBuffers.push(buf);
156
+ this.outCount += buf.length;
157
+ };
158
+
159
+ TFramedTransport.prototype.flush = function() {
160
+ // If the seqid of the callback is available pass it to the onFlush
161
+ // Then remove the current seqid
162
+ var seqid = this._seqid;
163
+ this._seqid = null;
164
+
165
+ var out = new Buffer(this.outCount),
166
+ pos = 0;
167
+ this.outBuffers.forEach(function(buf) {
168
+ buf.copy(out, pos, 0);
169
+ pos += buf.length;
170
+ });
171
+
172
+ if (this.onFlush) {
173
+ // TODO: optimize this better, allocate one buffer instead of both:
174
+ var msg = new Buffer(out.length + 4);
175
+ binary.writeI32(msg, out.length);
176
+ out.copy(msg, 4, 0, out.length);
177
+ if (this.onFlush) {
178
+ // Passing seqid through this call to get it to the connection
179
+ this.onFlush(msg, seqid);
180
+ }
181
+ }
182
+
183
+ this.outBuffers = [];
184
+ this.outCount = 0;
185
+ };