@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.
- package/dist/DBSQLClient.d.ts +3 -1
- package/dist/DBSQLClient.js +11 -8
- package/dist/DBSQLClient.js.map +1 -1
- package/dist/DBSQLOperation/SchemaHelper.d.ts +1 -1
- package/dist/DBSQLOperation/SchemaHelper.js +4 -2
- package/dist/DBSQLOperation/SchemaHelper.js.map +1 -1
- package/dist/DBSQLSession.js +20 -1
- package/dist/DBSQLSession.js.map +1 -1
- package/dist/connection/auth/PlainHttpAuthentication.d.ts +1 -1
- package/dist/connection/contracts/IConnectionOptions.d.ts +1 -1
- package/dist/connection/contracts/ITransport.d.ts +1 -1
- package/dist/connection/types/AuthOptions.d.ts +1 -1
- package/dist/contracts/IDBSQLSession.d.ts +10 -10
- package/dist/contracts/IOperation.d.ts +1 -1
- package/dist/dto/InfoValue.d.ts +1 -1
- package/dist/dto/Status.d.ts +1 -1
- package/dist/globalConfig.d.ts +5 -0
- package/dist/globalConfig.js +7 -0
- package/dist/globalConfig.js.map +1 -0
- package/dist/hive/Types/index.d.ts +1 -1
- package/dist/result/ArrowResult.d.ts +14 -0
- package/dist/result/ArrowResult.js +105 -0
- package/dist/result/ArrowResult.js.map +1 -0
- package/dist/result/JsonResult.d.ts +1 -5
- package/dist/result/JsonResult.js +5 -59
- package/dist/result/JsonResult.js.map +1 -1
- package/dist/result/utils.d.ts +3 -0
- package/dist/result/utils.js +77 -0
- package/dist/result/utils.js.map +1 -0
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/node_modules/async-limiter/.eslintignore +2 -0
- package/node_modules/async-limiter/.nycrc +10 -0
- package/node_modules/async-limiter/.travis.yml +9 -0
- package/node_modules/async-limiter/LICENSE +8 -0
- package/node_modules/async-limiter/index.js +67 -0
- package/node_modules/async-limiter/package.json +35 -0
- package/node_modules/async-limiter/readme.md +132 -0
- package/node_modules/browser-or-node/LICENSE +21 -0
- package/node_modules/browser-or-node/README.md +63 -0
- package/node_modules/browser-or-node/lib/index.js +31 -0
- package/node_modules/browser-or-node/package.json +60 -0
- package/node_modules/browser-or-node/src/index.js +26 -0
- package/node_modules/isomorphic-ws/LICENSE +21 -0
- package/node_modules/isomorphic-ws/README.md +58 -0
- package/node_modules/isomorphic-ws/browser.js +17 -0
- package/node_modules/isomorphic-ws/index.d.ts +8 -0
- package/node_modules/isomorphic-ws/node.js +3 -0
- package/node_modules/isomorphic-ws/package.json +34 -0
- package/node_modules/node-int64/.npmignore +3 -0
- package/node_modules/node-int64/Int64.js +268 -0
- package/node_modules/node-int64/LICENSE +19 -0
- package/node_modules/node-int64/README.md +78 -0
- package/node_modules/node-int64/package.json +27 -0
- package/node_modules/node-int64/test.js +120 -0
- package/node_modules/q/CHANGES.md +800 -0
- package/node_modules/q/LICENSE +18 -0
- package/node_modules/q/README.md +874 -0
- package/node_modules/q/package.json +76 -0
- package/node_modules/q/q.js +2076 -0
- package/node_modules/q/queue.js +35 -0
- package/node_modules/thrift/LICENSE +306 -0
- package/node_modules/thrift/README.md +210 -0
- package/node_modules/thrift/lib/nodejs/README.md +142 -0
- package/node_modules/thrift/lib/nodejs/lib/thrift/binary.js +168 -0
- package/node_modules/thrift/lib/nodejs/lib/thrift/binary_protocol.js +367 -0
- package/node_modules/thrift/lib/nodejs/lib/thrift/browser.js +49 -0
- package/node_modules/thrift/lib/nodejs/lib/thrift/buffered_transport.js +186 -0
- package/node_modules/thrift/lib/nodejs/lib/thrift/compact_protocol.js +915 -0
- package/node_modules/thrift/lib/nodejs/lib/thrift/connection.js +396 -0
- package/node_modules/thrift/lib/nodejs/lib/thrift/create_client.js +54 -0
- package/node_modules/thrift/lib/nodejs/lib/thrift/framed_transport.js +185 -0
- package/node_modules/thrift/lib/nodejs/lib/thrift/header_protocol.js +258 -0
- package/node_modules/thrift/lib/nodejs/lib/thrift/header_transport.js +341 -0
- package/node_modules/thrift/lib/nodejs/lib/thrift/http_connection.js +282 -0
- package/node_modules/thrift/lib/nodejs/lib/thrift/index.js +76 -0
- package/node_modules/thrift/lib/nodejs/lib/thrift/input_buffer_underrun_error.js +32 -0
- package/node_modules/thrift/lib/nodejs/lib/thrift/int64_util.js +91 -0
- package/node_modules/thrift/lib/nodejs/lib/thrift/json_parse.js +299 -0
- package/node_modules/thrift/lib/nodejs/lib/thrift/json_protocol.js +799 -0
- package/node_modules/thrift/lib/nodejs/lib/thrift/log.js +87 -0
- package/node_modules/thrift/lib/nodejs/lib/thrift/multiplexed_processor.js +63 -0
- package/node_modules/thrift/lib/nodejs/lib/thrift/multiplexed_protocol.js +73 -0
- package/node_modules/thrift/lib/nodejs/lib/thrift/protocol.js +22 -0
- package/node_modules/thrift/lib/nodejs/lib/thrift/server.js +123 -0
- package/node_modules/thrift/lib/nodejs/lib/thrift/thrift.js +241 -0
- package/node_modules/thrift/lib/nodejs/lib/thrift/transport.js +22 -0
- package/node_modules/thrift/lib/nodejs/lib/thrift/web_server.js +567 -0
- package/node_modules/thrift/lib/nodejs/lib/thrift/ws_connection.js +286 -0
- package/node_modules/thrift/lib/nodejs/lib/thrift/ws_transport.js +206 -0
- package/node_modules/thrift/lib/nodejs/lib/thrift/xhr_connection.js +280 -0
- package/node_modules/thrift/package.json +72 -0
- package/node_modules/ws/LICENSE +21 -0
- package/node_modules/ws/README.md +417 -0
- package/node_modules/ws/index.js +9 -0
- package/node_modules/ws/lib/buffer-util.js +72 -0
- package/node_modules/ws/lib/constants.js +10 -0
- package/node_modules/ws/lib/event-target.js +170 -0
- package/node_modules/ws/lib/extension.js +211 -0
- package/node_modules/ws/lib/permessage-deflate.js +516 -0
- package/node_modules/ws/lib/receiver.js +513 -0
- package/node_modules/ws/lib/sender.js +401 -0
- package/node_modules/ws/lib/validation.js +29 -0
- package/node_modules/ws/lib/websocket-server.js +369 -0
- package/node_modules/ws/lib/websocket.js +828 -0
- package/node_modules/ws/package.json +44 -0
- package/package.json +12 -8
- package/CHANGELOG.md +0 -56
|
@@ -0,0 +1,286 @@
|
|
|
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 WebSocket = require('isomorphic-ws');
|
|
21
|
+
var EventEmitter = require("events").EventEmitter;
|
|
22
|
+
var thrift = require('./thrift');
|
|
23
|
+
|
|
24
|
+
var TBufferedTransport = require('./buffered_transport');
|
|
25
|
+
var TJSONProtocol = require('./json_protocol');
|
|
26
|
+
var InputBufferUnderrunError = require('./input_buffer_underrun_error');
|
|
27
|
+
|
|
28
|
+
var createClient = require('./create_client');
|
|
29
|
+
var jsEnv = require('browser-or-node');
|
|
30
|
+
exports.WSConnection = WSConnection;
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* @class
|
|
34
|
+
* @name WSConnectOptions
|
|
35
|
+
* @property {string} transport - The Thrift layered transport to use (TBufferedTransport, etc).
|
|
36
|
+
* @property {string} protocol - The Thrift serialization protocol to use (TJSONProtocol, etc.).
|
|
37
|
+
* @property {string} path - The URL path to connect to (e.g. "/", "/mySvc", "/thrift/quoteSvc", etc.).
|
|
38
|
+
* @property {object} headers - A standard Node.js header hash, an object hash containing key/value
|
|
39
|
+
* pairs where the key is the header name string and the value is the header value string.
|
|
40
|
+
* @property {boolean} secure - True causes the connection to use wss, otherwise ws is used.
|
|
41
|
+
* @property {object} wsOptions - Options passed on to WebSocket.
|
|
42
|
+
* @example
|
|
43
|
+
* //Use a secured websocket connection
|
|
44
|
+
* // uses the buffered transport layer, uses the JSON protocol and directs RPC traffic
|
|
45
|
+
* // to wss://thrift.example.com:9090/hello
|
|
46
|
+
* var thrift = require('thrift');
|
|
47
|
+
* var options = {
|
|
48
|
+
* transport: thrift.TBufferedTransport,
|
|
49
|
+
* protocol: thrift.TJSONProtocol,
|
|
50
|
+
* path: "/hello",
|
|
51
|
+
* secure: true
|
|
52
|
+
* };
|
|
53
|
+
* var con = thrift.createWSConnection("thrift.example.com", 9090, options);
|
|
54
|
+
* con.open()
|
|
55
|
+
* var client = thrift.createWSClient(myService, connection);
|
|
56
|
+
* client.myServiceFunction();
|
|
57
|
+
* con.close()
|
|
58
|
+
*/
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Initializes a Thrift WSConnection instance (use createWSConnection() rather than
|
|
62
|
+
* instantiating directly).
|
|
63
|
+
* @constructor
|
|
64
|
+
* @param {string} host - The host name or IP to connect to.
|
|
65
|
+
* @param {number} port - The TCP port to connect to.
|
|
66
|
+
* @param {WSConnectOptions} options - The configuration options to use.
|
|
67
|
+
* @throws {error} Exceptions other than ttransport.InputBufferUnderrunError are rethrown
|
|
68
|
+
* @event {error} The "error" event is fired when a Node.js error event occurs during
|
|
69
|
+
* request or response processing, in which case the node error is passed on. An "error"
|
|
70
|
+
* event may also be fired when the connection can not map a response back to the
|
|
71
|
+
* appropriate client (an internal error), generating a TApplicationException.
|
|
72
|
+
* @classdesc WSConnection objects provide Thrift end point transport
|
|
73
|
+
* semantics implemented using Websockets.
|
|
74
|
+
* @see {@link createWSConnection}
|
|
75
|
+
*/
|
|
76
|
+
function WSConnection(host, port, options) {
|
|
77
|
+
//Initialize the emitter base object
|
|
78
|
+
EventEmitter.call(this);
|
|
79
|
+
|
|
80
|
+
//Set configuration
|
|
81
|
+
this.options = options || {};
|
|
82
|
+
this.host = host;
|
|
83
|
+
this.port = port;
|
|
84
|
+
this.secure = this.options.secure || false;
|
|
85
|
+
this.transport = this.options.transport || TBufferedTransport;
|
|
86
|
+
this.protocol = this.options.protocol || TJSONProtocol;
|
|
87
|
+
this.path = this.options.path;
|
|
88
|
+
this.send_pending = [];
|
|
89
|
+
|
|
90
|
+
//The sequence map is used to map seqIDs back to the
|
|
91
|
+
// calling client in multiplexed scenarios
|
|
92
|
+
this.seqId2Service = {};
|
|
93
|
+
|
|
94
|
+
//Prepare WebSocket options
|
|
95
|
+
this.wsOptions = {
|
|
96
|
+
host: this.host,
|
|
97
|
+
port: this.port || 80,
|
|
98
|
+
path: this.options.path || '/',
|
|
99
|
+
headers: this.options.headers || {}
|
|
100
|
+
};
|
|
101
|
+
for (var attrname in this.options.wsOptions) {
|
|
102
|
+
this.wsOptions[attrname] = this.options.wsOptions[attrname];
|
|
103
|
+
}
|
|
104
|
+
};
|
|
105
|
+
util.inherits(WSConnection, EventEmitter);
|
|
106
|
+
|
|
107
|
+
WSConnection.prototype.__reset = function() {
|
|
108
|
+
this.socket = null; //The web socket
|
|
109
|
+
this.send_pending = []; //Buffers/Callback pairs waiting to be sent
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
WSConnection.prototype.__onOpen = function() {
|
|
113
|
+
this.emit("open");
|
|
114
|
+
if (this.send_pending.length > 0) {
|
|
115
|
+
//If the user made calls before the connection was fully
|
|
116
|
+
//open, send them now
|
|
117
|
+
this.send_pending.forEach(function(data) {
|
|
118
|
+
this.socket.send(data);
|
|
119
|
+
}, this);
|
|
120
|
+
this.send_pending = [];
|
|
121
|
+
}
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
WSConnection.prototype.__onClose = function(evt) {
|
|
125
|
+
this.emit("close");
|
|
126
|
+
this.__reset();
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
WSConnection.prototype.__decodeCallback = function(transport_with_data) {
|
|
130
|
+
var proto = new this.protocol(transport_with_data);
|
|
131
|
+
try {
|
|
132
|
+
while (true) {
|
|
133
|
+
var header = proto.readMessageBegin();
|
|
134
|
+
var dummy_seqid = header.rseqid * -1;
|
|
135
|
+
var client = this.client;
|
|
136
|
+
//The Multiplexed Protocol stores a hash of seqid to service names
|
|
137
|
+
// in seqId2Service. If the SeqId is found in the hash we need to
|
|
138
|
+
// lookup the appropriate client for this call.
|
|
139
|
+
// The client var is a single client object when not multiplexing,
|
|
140
|
+
// when using multiplexing it is a service name keyed hash of client
|
|
141
|
+
// objects.
|
|
142
|
+
//NOTE: The 2 way interdependencies between protocols, transports,
|
|
143
|
+
// connections and clients in the Node.js implementation are irregular
|
|
144
|
+
// and make the implementation difficult to extend and maintain. We
|
|
145
|
+
// should bring this stuff inline with typical thrift I/O stack
|
|
146
|
+
// operation soon.
|
|
147
|
+
// --ra
|
|
148
|
+
var service_name = this.seqId2Service[header.rseqid];
|
|
149
|
+
if (service_name) {
|
|
150
|
+
client = this.client[service_name];
|
|
151
|
+
delete this.seqId2Service[header.rseqid];
|
|
152
|
+
}
|
|
153
|
+
/*jshint -W083 */
|
|
154
|
+
client._reqs[dummy_seqid] = function(err, success) {
|
|
155
|
+
transport_with_data.commitPosition();
|
|
156
|
+
var clientCallback = client._reqs[header.rseqid];
|
|
157
|
+
delete client._reqs[header.rseqid];
|
|
158
|
+
if (clientCallback) {
|
|
159
|
+
clientCallback(err, success);
|
|
160
|
+
}
|
|
161
|
+
};
|
|
162
|
+
/*jshint +W083 */
|
|
163
|
+
if (client['recv_' + header.fname]) {
|
|
164
|
+
client['recv_' + header.fname](proto, header.mtype, dummy_seqid);
|
|
165
|
+
} else {
|
|
166
|
+
delete client._reqs[dummy_seqid];
|
|
167
|
+
this.emit("error",
|
|
168
|
+
new thrift.TApplicationException(
|
|
169
|
+
thrift.TApplicationExceptionType.WRONG_METHOD_NAME,
|
|
170
|
+
"Received a response to an unknown RPC function"));
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
} catch (e) {
|
|
174
|
+
if (e instanceof InputBufferUnderrunError) {
|
|
175
|
+
transport_with_data.rollbackPosition();
|
|
176
|
+
} else {
|
|
177
|
+
throw e;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
};
|
|
181
|
+
|
|
182
|
+
WSConnection.prototype.__onData = function(data) {
|
|
183
|
+
if (Object.prototype.toString.call(data) === "[object ArrayBuffer]") {
|
|
184
|
+
data = new Uint8Array(data);
|
|
185
|
+
}
|
|
186
|
+
var buf = new Buffer(data);
|
|
187
|
+
this.transport.receiver(this.__decodeCallback.bind(this))(buf);
|
|
188
|
+
|
|
189
|
+
};
|
|
190
|
+
|
|
191
|
+
WSConnection.prototype.__onMessage = function(evt) {
|
|
192
|
+
this.__onData(evt.data);
|
|
193
|
+
};
|
|
194
|
+
|
|
195
|
+
WSConnection.prototype.__onError = function(evt) {
|
|
196
|
+
this.emit("error", evt);
|
|
197
|
+
this.socket.close();
|
|
198
|
+
};
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* Returns true if the transport is open
|
|
202
|
+
* @readonly
|
|
203
|
+
* @returns {boolean}
|
|
204
|
+
*/
|
|
205
|
+
WSConnection.prototype.isOpen = function() {
|
|
206
|
+
return this.socket && this.socket.readyState === this.socket.OPEN;
|
|
207
|
+
};
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* Opens the transport connection
|
|
211
|
+
*/
|
|
212
|
+
WSConnection.prototype.open = function() {
|
|
213
|
+
//If OPEN/CONNECTING/CLOSING ignore additional opens
|
|
214
|
+
if (this.socket && this.socket.readyState !== this.socket.CLOSED) {
|
|
215
|
+
return;
|
|
216
|
+
}
|
|
217
|
+
//If there is no socket or the socket is closed:
|
|
218
|
+
if (jsEnv.isBrowser) {
|
|
219
|
+
this.socket = new WebSocket(this.uri());
|
|
220
|
+
} else {
|
|
221
|
+
this.socket = new WebSocket(this.uri(), "", this.wsOptions);
|
|
222
|
+
}
|
|
223
|
+
this.socket.binaryType = 'arraybuffer';
|
|
224
|
+
this.socket.onopen = this.__onOpen.bind(this);
|
|
225
|
+
this.socket.onmessage = this.__onMessage.bind(this);
|
|
226
|
+
this.socket.onerror = this.__onError.bind(this);
|
|
227
|
+
this.socket.onclose = this.__onClose.bind(this);
|
|
228
|
+
};
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* Closes the transport connection
|
|
232
|
+
*/
|
|
233
|
+
WSConnection.prototype.close = function() {
|
|
234
|
+
this.socket.close();
|
|
235
|
+
};
|
|
236
|
+
|
|
237
|
+
/**
|
|
238
|
+
* Return URI for the connection
|
|
239
|
+
* @returns {string} URI
|
|
240
|
+
*/
|
|
241
|
+
WSConnection.prototype.uri = function() {
|
|
242
|
+
var schema = this.secure ? 'wss' : 'ws';
|
|
243
|
+
var port = '';
|
|
244
|
+
var path = this.path || '/';
|
|
245
|
+
var host = this.host;
|
|
246
|
+
|
|
247
|
+
// avoid port if default for schema
|
|
248
|
+
if (this.port && (('wss' === schema && this.port !== 443) ||
|
|
249
|
+
('ws' === schema && this.port !== 80))) {
|
|
250
|
+
port = ':' + this.port;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
return schema + '://' + host + port + path;
|
|
254
|
+
};
|
|
255
|
+
|
|
256
|
+
/**
|
|
257
|
+
* Writes Thrift message data to the connection
|
|
258
|
+
* @param {Buffer} data - A Node.js Buffer containing the data to write
|
|
259
|
+
* @returns {void} No return value.
|
|
260
|
+
* @event {error} the "error" event is raised upon request failure passing the
|
|
261
|
+
* Node.js error object to the listener.
|
|
262
|
+
*/
|
|
263
|
+
WSConnection.prototype.write = function(data) {
|
|
264
|
+
if (this.isOpen()) {
|
|
265
|
+
//Send data and register a callback to invoke the client callback
|
|
266
|
+
this.socket.send(data);
|
|
267
|
+
} else {
|
|
268
|
+
//Queue the send to go out __onOpen
|
|
269
|
+
this.send_pending.push(data);
|
|
270
|
+
}
|
|
271
|
+
};
|
|
272
|
+
|
|
273
|
+
/**
|
|
274
|
+
* Creates a new WSConnection object, used by Thrift clients to connect
|
|
275
|
+
* to Thrift HTTP based servers.
|
|
276
|
+
* @param {string} host - The host name or IP to connect to.
|
|
277
|
+
* @param {number} port - The TCP port to connect to.
|
|
278
|
+
* @param {WSConnectOptions} options - The configuration options to use.
|
|
279
|
+
* @returns {WSConnection} The connection object.
|
|
280
|
+
* @see {@link WSConnectOptions}
|
|
281
|
+
*/
|
|
282
|
+
exports.createWSConnection = function(host, port, options) {
|
|
283
|
+
return new WSConnection(host, port, options);
|
|
284
|
+
};
|
|
285
|
+
|
|
286
|
+
exports.createWSClient = createClient;
|
|
@@ -0,0 +1,206 @@
|
|
|
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 log = require('./log');
|
|
21
|
+
|
|
22
|
+
module.exports = TWebSocketTransport;
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Constructor Function for the WebSocket transport.
|
|
26
|
+
* @constructor
|
|
27
|
+
* @param {string} [url] - The URL to connect to.
|
|
28
|
+
* @classdesc The Apache Thrift Transport layer performs byte level I/O
|
|
29
|
+
* between RPC clients and servers. The JavaScript TWebSocketTransport object
|
|
30
|
+
* uses the WebSocket protocol. Target servers must implement WebSocket.
|
|
31
|
+
* (see: node.js example server_http.js).
|
|
32
|
+
* @example
|
|
33
|
+
* var transport = new Thrift.TWebSocketTransport("http://localhost:8585");
|
|
34
|
+
*/
|
|
35
|
+
function TWebSocketTransport(url) {
|
|
36
|
+
this.__reset(url);
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
TWebSocketTransport.prototype.__reset = function(url) {
|
|
41
|
+
this.url = url; //Where to connect
|
|
42
|
+
this.socket = null; //The web socket
|
|
43
|
+
this.callbacks = []; //Pending callbacks
|
|
44
|
+
this.send_pending = []; //Buffers/Callback pairs waiting to be sent
|
|
45
|
+
this.send_buf = ''; //Outbound data, immutable until sent
|
|
46
|
+
this.recv_buf = ''; //Inbound data
|
|
47
|
+
this.rb_wpos = 0; //Network write position in receive buffer
|
|
48
|
+
this.rb_rpos = 0; //Client read position in receive buffer
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Sends the current WS request and registers callback. The async
|
|
53
|
+
* parameter is ignored (WS flush is always async) and the callback
|
|
54
|
+
* function parameter is required.
|
|
55
|
+
* @param {object} async - Ignored.
|
|
56
|
+
* @param {object} callback - The client completion callback.
|
|
57
|
+
* @returns {undefined|string} Nothing (undefined)
|
|
58
|
+
*/
|
|
59
|
+
TWebSocketTransport.prototype.flush = function(async, callback) {
|
|
60
|
+
var self = this;
|
|
61
|
+
if (this.isOpen()) {
|
|
62
|
+
//Send data and register a callback to invoke the client callback
|
|
63
|
+
this.socket.send(this.send_buf);
|
|
64
|
+
this.callbacks.push((function() {
|
|
65
|
+
var clientCallback = callback;
|
|
66
|
+
return function(msg) {
|
|
67
|
+
self.setRecvBuffer(msg);
|
|
68
|
+
clientCallback();
|
|
69
|
+
};
|
|
70
|
+
}()));
|
|
71
|
+
} else {
|
|
72
|
+
//Queue the send to go out __onOpen
|
|
73
|
+
this.send_pending.push({
|
|
74
|
+
buf: this.send_buf,
|
|
75
|
+
cb: callback
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
TWebSocketTransport.prototype.__onOpen = function() {
|
|
81
|
+
var self = this;
|
|
82
|
+
if (this.send_pending.length > 0) {
|
|
83
|
+
//If the user made calls before the connection was fully
|
|
84
|
+
//open, send them now
|
|
85
|
+
this.send_pending.forEach(function(elem) {
|
|
86
|
+
self.socket.send(elem.buf);
|
|
87
|
+
self.callbacks.push((function() {
|
|
88
|
+
var clientCallback = elem.cb;
|
|
89
|
+
return function(msg) {
|
|
90
|
+
self.setRecvBuffer(msg);
|
|
91
|
+
clientCallback();
|
|
92
|
+
};
|
|
93
|
+
}()));
|
|
94
|
+
});
|
|
95
|
+
this.send_pending = [];
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
TWebSocketTransport.prototype.__onClose = function(evt) {
|
|
100
|
+
this.__reset(this.url);
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
TWebSocketTransport.prototype.__onMessage = function(evt) {
|
|
104
|
+
if (this.callbacks.length) {
|
|
105
|
+
this.callbacks.shift()(evt.data);
|
|
106
|
+
}
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
TWebSocketTransport.prototype.__onError = function(evt) {
|
|
110
|
+
log.error('websocket: ' + evt.toString());
|
|
111
|
+
this.socket.close();
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Sets the buffer to use when receiving server responses.
|
|
116
|
+
* @param {string} buf - The buffer to receive server responses.
|
|
117
|
+
*/
|
|
118
|
+
TWebSocketTransport.prototype.setRecvBuffer = function(buf) {
|
|
119
|
+
this.recv_buf = buf;
|
|
120
|
+
this.recv_buf_sz = this.recv_buf.length;
|
|
121
|
+
this.wpos = this.recv_buf.length;
|
|
122
|
+
this.rpos = 0;
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Returns true if the transport is open
|
|
127
|
+
* @readonly
|
|
128
|
+
* @returns {boolean}
|
|
129
|
+
*/
|
|
130
|
+
TWebSocketTransport.prototype.isOpen = function() {
|
|
131
|
+
return this.socket && this.socket.readyState == this.socket.OPEN;
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Opens the transport connection
|
|
136
|
+
*/
|
|
137
|
+
TWebSocketTransport.prototype.open = function() {
|
|
138
|
+
//If OPEN/CONNECTING/CLOSING ignore additional opens
|
|
139
|
+
if (this.socket && this.socket.readyState != this.socket.CLOSED) {
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
//If there is no socket or the socket is closed:
|
|
143
|
+
this.socket = new WebSocket(this.url);
|
|
144
|
+
this.socket.onopen = this.__onOpen.bind(this);
|
|
145
|
+
this.socket.onmessage = this.__onMessage.bind(this);
|
|
146
|
+
this.socket.onerror = this.__onError.bind(this);
|
|
147
|
+
this.socket.onclose = this.__onClose.bind(this);
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Closes the transport connection
|
|
152
|
+
*/
|
|
153
|
+
TWebSocketTransport.prototype.close = function() {
|
|
154
|
+
this.socket.close();
|
|
155
|
+
};
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Returns the specified number of characters from the response
|
|
159
|
+
* buffer.
|
|
160
|
+
* @param {number} len - The number of characters to return.
|
|
161
|
+
* @returns {string} Characters sent by the server.
|
|
162
|
+
*/
|
|
163
|
+
TWebSocketTransport.prototype.read = function(len) {
|
|
164
|
+
var avail = this.wpos - this.rpos;
|
|
165
|
+
|
|
166
|
+
if (avail === 0) {
|
|
167
|
+
return '';
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
var give = len;
|
|
171
|
+
|
|
172
|
+
if (avail < len) {
|
|
173
|
+
give = avail;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
var ret = this.read_buf.substr(this.rpos, give);
|
|
177
|
+
this.rpos += give;
|
|
178
|
+
|
|
179
|
+
//clear buf when complete?
|
|
180
|
+
return ret;
|
|
181
|
+
};
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* Returns the entire response buffer.
|
|
185
|
+
* @returns {string} Characters sent by the server.
|
|
186
|
+
*/
|
|
187
|
+
TWebSocketTransport.prototype.readAll = function() {
|
|
188
|
+
return this.recv_buf;
|
|
189
|
+
};
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* Sets the send buffer to buf.
|
|
193
|
+
* @param {string} buf - The buffer to send.
|
|
194
|
+
*/
|
|
195
|
+
TWebSocketTransport.prototype.write = function(buf) {
|
|
196
|
+
this.send_buf = buf;
|
|
197
|
+
};
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* Returns the send buffer.
|
|
201
|
+
* @readonly
|
|
202
|
+
* @returns {string} The send buffer.
|
|
203
|
+
*/
|
|
204
|
+
TWebSocketTransport.prototype.getSendBuffer = function() {
|
|
205
|
+
return this.send_buf;
|
|
206
|
+
};
|