@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,282 @@
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 http = require('http');
21
+ var https = require('https');
22
+ var EventEmitter = require('events').EventEmitter;
23
+ var thrift = require('./thrift');
24
+
25
+ var TBufferedTransport = require('./buffered_transport');
26
+ var TBinaryProtocol = require('./binary_protocol');
27
+ var InputBufferUnderrunError = require('./input_buffer_underrun_error');
28
+
29
+ var createClient = require('./create_client');
30
+
31
+ /**
32
+ * @class
33
+ * @name ConnectOptions
34
+ * @property {string} transport - The Thrift layered transport to use (TBufferedTransport, etc).
35
+ * @property {string} protocol - The Thrift serialization protocol to use (TBinaryProtocol, etc.).
36
+ * @property {string} path - The URL path to POST to (e.g. "/", "/mySvc", "/thrift/quoteSvc", etc.).
37
+ * @property {object} headers - A standard Node.js header hash, an object hash containing key/value
38
+ * pairs where the key is the header name string and the value is the header value string.
39
+ * @property {boolean} https - True causes the connection to use https, otherwise http is used.
40
+ * @property {object} nodeOptions - Options passed on to node.
41
+ * @example
42
+ * //Use a connection that requires ssl/tls, closes the connection after each request,
43
+ * // uses the buffered transport layer, uses the JSON protocol and directs RPC traffic
44
+ * // to https://thrift.example.com:9090/hello
45
+ * var thrift = require('thrift');
46
+ * var options = {
47
+ * transport: thrift.TBufferedTransport,
48
+ * protocol: thrift.TJSONProtocol,
49
+ * path: "/hello",
50
+ * headers: {"Connection": "close"},
51
+ * https: true
52
+ * };
53
+ * var con = thrift.createHttpConnection("thrift.example.com", 9090, options);
54
+ * var client = thrift.createHttpClient(myService, connection);
55
+ * client.myServiceFunction();
56
+ */
57
+
58
+ /**
59
+ * Initializes a Thrift HttpConnection instance (use createHttpConnection() rather than
60
+ * instantiating directly).
61
+ * @constructor
62
+ * @param {ConnectOptions} options - The configuration options to use.
63
+ * @throws {error} Exceptions other than InputBufferUnderrunError are rethrown
64
+ * @event {error} The "error" event is fired when a Node.js error event occurs during
65
+ * request or response processing, in which case the node error is passed on. An "error"
66
+ * event may also be fired when the connection can not map a response back to the
67
+ * appropriate client (an internal error), generating a TApplicationException.
68
+ * @classdesc HttpConnection objects provide Thrift end point transport
69
+ * semantics implemented over the Node.js http.request() method.
70
+ * @see {@link createHttpConnection}
71
+ */
72
+ var HttpConnection = exports.HttpConnection = function(options) {
73
+ //Initialize the emitter base object
74
+ EventEmitter.call(this);
75
+
76
+ //Set configuration
77
+ var self = this;
78
+ this.options = options || {};
79
+ this.host = this.options.host;
80
+ this.port = this.options.port;
81
+ this.socketPath = this.options.socketPath;
82
+ this.https = this.options.https || false;
83
+ this.transport = this.options.transport || TBufferedTransport;
84
+ this.protocol = this.options.protocol || TBinaryProtocol;
85
+
86
+ //Prepare Node.js options
87
+ this.nodeOptions = {
88
+ host: this.host,
89
+ port: this.port,
90
+ socketPath: this.socketPath,
91
+ path: this.options.path || '/',
92
+ method: 'POST',
93
+ headers: this.options.headers || {},
94
+ responseType: this.options.responseType || null
95
+ };
96
+ for (var attrname in this.options.nodeOptions) {
97
+ this.nodeOptions[attrname] = this.options.nodeOptions[attrname];
98
+ }
99
+ /*jshint -W069 */
100
+ if (! this.nodeOptions.headers['Connection']) {
101
+ this.nodeOptions.headers['Connection'] = 'keep-alive';
102
+ }
103
+ /*jshint +W069 */
104
+
105
+ //The sequence map is used to map seqIDs back to the
106
+ // calling client in multiplexed scenarios
107
+ this.seqId2Service = {};
108
+
109
+ function decodeCallback(transport_with_data, handleError) {
110
+ var proto = new self.protocol(transport_with_data);
111
+ try {
112
+ while (true) {
113
+ var header = proto.readMessageBegin();
114
+ var dummy_seqid = header.rseqid * -1;
115
+ var client = self.client;
116
+ //The Multiplexed Protocol stores a hash of seqid to service names
117
+ // in seqId2Service. If the SeqId is found in the hash we need to
118
+ // lookup the appropriate client for this call.
119
+ // The client var is a single client object when not multiplexing,
120
+ // when using multiplexing it is a service name keyed hash of client
121
+ // objects.
122
+ //NOTE: The 2 way interdependencies between protocols, transports,
123
+ // connections and clients in the Node.js implementation are irregular
124
+ // and make the implementation difficult to extend and maintain. We
125
+ // should bring this stuff inline with typical thrift I/O stack
126
+ // operation soon.
127
+ // --ra
128
+ var service_name = self.seqId2Service[header.rseqid];
129
+ if (service_name) {
130
+ client = self.client[service_name];
131
+ delete self.seqId2Service[header.rseqid];
132
+ }
133
+ /*jshint -W083 */
134
+ client._reqs[dummy_seqid] = function(err, success){
135
+ transport_with_data.commitPosition();
136
+ var clientCallback = client._reqs[header.rseqid];
137
+ delete client._reqs[header.rseqid];
138
+ if (clientCallback) {
139
+ process.nextTick(function() {
140
+ clientCallback(err, success);
141
+ });
142
+ }
143
+ };
144
+ /*jshint +W083 */
145
+ if(client['recv_' + header.fname]) {
146
+ client['recv_' + header.fname](proto, header.mtype, dummy_seqid);
147
+ } else {
148
+ delete client._reqs[dummy_seqid];
149
+ handleError(
150
+ new thrift.TApplicationException(
151
+ thrift.TApplicationExceptionType.WRONG_METHOD_NAME,
152
+ "Received a response to an unknown RPC function")
153
+ );
154
+ }
155
+ }
156
+ }
157
+ catch (e) {
158
+ if (e instanceof InputBufferUnderrunError) {
159
+ transport_with_data.rollbackPosition();
160
+ } else {
161
+ handleError(e);
162
+ }
163
+ }
164
+ }
165
+
166
+
167
+ //Response handler
168
+ //////////////////////////////////////////////////
169
+ this.responseCallback = function(response, handleError) {
170
+ var data = [];
171
+ var dataLen = 0;
172
+
173
+ if (response.statusCode !== 200) {
174
+ handleError(new THTTPException(response));
175
+ }
176
+
177
+ response.on('error', handleError);
178
+
179
+ // When running directly under node, chunk will be a buffer,
180
+ // however, when running in a Browser (e.g. Browserify), chunk
181
+ // will be a string or an ArrayBuffer.
182
+ response.on('data', function (chunk) {
183
+ if ((typeof chunk == 'string') ||
184
+ (Object.prototype.toString.call(chunk) == '[object Uint8Array]')) {
185
+ // Wrap ArrayBuffer/string in a Buffer so data[i].copy will work
186
+ data.push(new Buffer(chunk));
187
+ } else {
188
+ data.push(chunk);
189
+ }
190
+ dataLen += chunk.length;
191
+ });
192
+
193
+ response.on('end', function(){
194
+ var buf = new Buffer(dataLen);
195
+ for (var i=0, len=data.length, pos=0; i<len; i++) {
196
+ data[i].copy(buf, pos);
197
+ pos += data[i].length;
198
+ }
199
+ //Get the receiver function for the transport and
200
+ // call it with the buffer
201
+ self.transport.receiver(function(transport_with_data) {
202
+ return decodeCallback(transport_with_data, handleError);
203
+ })(buf);
204
+ });
205
+ };
206
+ };
207
+ util.inherits(HttpConnection, EventEmitter);
208
+
209
+ /**
210
+ * Writes Thrift message data to the connection
211
+ * @param {Buffer} data - A Node.js Buffer containing the data to write
212
+ * @returns {void} No return value.
213
+ * @event {error} the "error" event is raised upon request failure passing the
214
+ * Node.js error object to the listener.
215
+ */
216
+ HttpConnection.prototype.write = function(data, seqid) {
217
+ var self = this;
218
+ var opts = self.nodeOptions;
219
+ opts.headers["Content-length"] = data.length;
220
+ if (!opts.headers["Content-Type"])
221
+ opts.headers["Content-Type"] = "application/x-thrift";
222
+
223
+ // The purpose of this handler is to pass an error to corresponding operation's callback if
224
+ // error is associated with any. If error cannot be associated with any operation, it will be
225
+ // emitted as 'error' event
226
+ function handleError(err) {
227
+ var client = self.client;
228
+ var callback = client._reqs[seqid] || function() {
229
+ self.emit("error", err);
230
+ };
231
+ delete client._reqs[seqid];
232
+ callback(err);
233
+ }
234
+
235
+ function responseCallback(response) {
236
+ return self.responseCallback(response, handleError);
237
+ }
238
+
239
+ var req = (self.https) ?
240
+ https.request(opts, responseCallback) :
241
+ http.request(opts, responseCallback);
242
+ req.on('error', handleError);
243
+ req.write(data);
244
+ req.end();
245
+ };
246
+
247
+ /**
248
+ * Creates a new HttpConnection object, used by Thrift clients to connect
249
+ * to Thrift HTTP based servers.
250
+ * @param {string} host - The host name or IP to connect to.
251
+ * @param {number} port - The TCP port to connect to.
252
+ * @param {ConnectOptions} options - The configuration options to use.
253
+ * @returns {HttpConnection} The connection object.
254
+ * @see {@link ConnectOptions}
255
+ */
256
+ exports.createHttpConnection = function(host, port, options) {
257
+ options.host = host;
258
+ options.port = port || 80;
259
+ return new HttpConnection(options);
260
+ };
261
+
262
+ exports.createHttpUDSConnection = function(path, options) {
263
+ options.socketPath = path;
264
+ return new HttpConnection(options);
265
+ };
266
+
267
+ exports.createHttpClient = createClient
268
+
269
+
270
+ function THTTPException(response) {
271
+ thrift.TApplicationException.call(this);
272
+ if (Error.captureStackTrace !== undefined) {
273
+ Error.captureStackTrace(this, this.constructor);
274
+ }
275
+
276
+ this.name = this.constructor.name;
277
+ this.statusCode = response.statusCode;
278
+ this.response = response;
279
+ this.type = thrift.TApplicationExceptionType.PROTOCOL_ERROR;
280
+ this.message = "Received a response with a bad HTTP status code: " + response.statusCode;
281
+ }
282
+ util.inherits(THTTPException, thrift.TApplicationException);
@@ -0,0 +1,76 @@
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
+ exports.Thrift = require('./thrift');
20
+
21
+ var log = require('./log');
22
+ exports.setLogFunc = log.setLogFunc;
23
+ exports.setLogLevel = log.setLogLevel;
24
+ exports.getLogLevel = log.getLogLevel;
25
+
26
+ var connection = require('./connection');
27
+ exports.Connection = connection.Connection;
28
+ exports.createClient = connection.createClient;
29
+ exports.createConnection = connection.createConnection;
30
+ exports.createUDSConnection = connection.createUDSConnection;
31
+ exports.createSSLConnection = connection.createSSLConnection;
32
+ exports.createStdIOClient = connection.createStdIOClient;
33
+ exports.createStdIOConnection = connection.createStdIOConnection;
34
+
35
+ var httpConnection = require('./http_connection');
36
+ exports.HttpConnection = httpConnection.HttpConnection;
37
+ exports.createHttpConnection = httpConnection.createHttpConnection;
38
+ exports.createHttpUDSConnection = httpConnection.createHttpUDSConnection;
39
+ exports.createHttpClient = httpConnection.createHttpClient;
40
+
41
+ var wsConnection = require('./ws_connection');
42
+ exports.WSConnection = wsConnection.WSConnection;
43
+ exports.createWSConnection = wsConnection.createWSConnection;
44
+ exports.createWSClient = wsConnection.createWSClient;
45
+
46
+ var xhrConnection = require('./xhr_connection');
47
+ exports.XHRConnection = xhrConnection.XHRConnection;
48
+ exports.createXHRConnection = xhrConnection.createXHRConnection;
49
+ exports.createXHRClient = xhrConnection.createXHRClient;
50
+
51
+ var server = require('./server');
52
+ exports.createServer = server.createServer;
53
+ exports.createMultiplexServer = server.createMultiplexServer;
54
+
55
+ var web_server = require('./web_server');
56
+ exports.createWebServer = web_server.createWebServer;
57
+
58
+ exports.Int64 = require('node-int64');
59
+ exports.Q = require('q');
60
+
61
+ var mpxProcessor = require('./multiplexed_processor');
62
+ var mpxProtocol = require('./multiplexed_protocol');
63
+ exports.MultiplexedProcessor = mpxProcessor.MultiplexedProcessor;
64
+ exports.Multiplexer = mpxProtocol.Multiplexer;
65
+
66
+ /*
67
+ * Export transport and protocol so they can be used outside of a
68
+ * cassandra/server context
69
+ */
70
+ exports.TBufferedTransport = require('./buffered_transport');
71
+ exports.TFramedTransport = require('./framed_transport');
72
+
73
+ exports.TJSONProtocol = require('./json_protocol');
74
+ exports.TBinaryProtocol = require('./binary_protocol');
75
+ exports.TCompactProtocol = require('./compact_protocol');
76
+ exports.THeaderProtocol = require('./header_protocol');
@@ -0,0 +1,32 @@
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
+
21
+ module.exports = InputBufferUnderrunError;
22
+
23
+ function InputBufferUnderrunError(message) {
24
+ Error.call(this);
25
+ if (Error.captureStackTrace !== undefined) {
26
+ Error.captureStackTrace(this, this.constructor);
27
+ }
28
+ this.name = this.constructor.name;
29
+ this.message = message;
30
+ };
31
+
32
+ util.inherits(InputBufferUnderrunError, Error);
@@ -0,0 +1,91 @@
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 Int64 = require('node-int64');
21
+
22
+ var Int64Util = module.exports = {};
23
+
24
+ var POW2_24 = Math.pow(2, 24);
25
+ var POW2_31 = Math.pow(2, 31);
26
+ var POW2_32 = Math.pow(2, 32);
27
+ var POW10_11 = Math.pow(10, 11);
28
+
29
+ Int64Util.toDecimalString = function(i64) {
30
+ var b = i64.buffer;
31
+ var o = i64.offset;
32
+ if ((!b[o] && !(b[o + 1] & 0xe0)) ||
33
+ (!~b[o] && !~(b[o + 1] & 0xe0))) {
34
+ // The magnitude is small enough.
35
+ return i64.toString();
36
+ } else {
37
+ var negative = b[o] & 0x80;
38
+ if (negative) {
39
+ // 2's complement
40
+ var incremented = false;
41
+ var buffer = new Buffer(8);
42
+ for (var i = 7; i >= 0; --i) {
43
+ buffer[i] = (~b[o + i] + (incremented ? 0 : 1)) & 0xff;
44
+ incremented |= b[o + i];
45
+ }
46
+ b = buffer;
47
+ }
48
+ var high2 = b[o + 1] + (b[o] << 8);
49
+ // Lesser 11 digits with exceeding values but is under 53 bits capacity.
50
+ var low = b[o + 7] + (b[o + 6] << 8) + (b[o + 5] << 16)
51
+ + b[o + 4] * POW2_24 // Bit shift renders 32th bit as sign, so use multiplication
52
+ + (b[o + 3] + (b[o + 2] << 8)) * POW2_32 + high2 * 74976710656; // The literal is 2^48 % 10^11
53
+ // 12th digit and greater.
54
+ var high = Math.floor(low / POW10_11) + high2 * 2814; // The literal is 2^48 / 10^11
55
+ // Make it exactly 11 with leading zeros.
56
+ low = ('00000000000' + String(low % POW10_11)).slice(-11);
57
+ return (negative ? '-' : '') + String(high) + low;
58
+ }
59
+ };
60
+
61
+ Int64Util.fromDecimalString = function(text) {
62
+ var negative = text.charAt(0) === '-';
63
+ if (text.length < (negative ? 17 : 16)) {
64
+ // The magnitude is smaller than 2^53.
65
+ return new Int64(+text);
66
+ } else if (text.length > (negative ? 20 : 19)) {
67
+ throw new RangeError('Too many digits for Int64: ' + text);
68
+ } else {
69
+ // Most significant (up to 5) digits
70
+ var high5 = +text.slice(negative ? 1 : 0, -15);
71
+ var low = +text.slice(-15) + high5 * 2764472320; // The literal is 10^15 % 2^32
72
+ var high = Math.floor(low / POW2_32) + high5 * 232830; // The literal is 10^15 / 2^&32
73
+ low = low % POW2_32;
74
+ if (high >= POW2_31 &&
75
+ !(negative && high == POW2_31 && low == 0) // Allow minimum Int64
76
+ ) {
77
+ throw new RangeError('The magnitude is too large for Int64.');
78
+ }
79
+ if (negative) {
80
+ // 2's complement
81
+ high = ~high;
82
+ if (low === 0) {
83
+ high = (high + 1) & 0xffffffff;
84
+ } else {
85
+ low = ~low + 1;
86
+ }
87
+ high = 0x80000000 | high;
88
+ }
89
+ return new Int64(high, low);
90
+ }
91
+ };