]> git.proxmox.com Git - ceph.git/blob - ceph/src/jaegertracing/thrift/lib/nodejs/README.md
update source to Ceph Pacific 16.2.2
[ceph.git] / ceph / src / jaegertracing / thrift / lib / nodejs / README.md
1 Thrift Node.js Library
2 =========================
3
4 License
5 -------
6 Licensed to the Apache Software Foundation (ASF) under one
7 or more contributor license agreements. See the NOTICE file
8 distributed with this work for additional information
9 regarding copyright ownership. The ASF licenses this file
10 to you under the Apache License, Version 2.0 (the
11 "License"); you may not use this file except in compliance
12 with the License. You may obtain a copy of the License at
13
14 http://www.apache.org/licenses/LICENSE-2.0
15
16 Unless required by applicable law or agreed to in writing,
17 software distributed under the License is distributed on an
18 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
19 KIND, either express or implied. See the License for the
20 specific language governing permissions and limitations
21 under the License.
22
23 ## Compatibility
24
25 node version 6 or later is required
26
27 ## Install
28
29 npm install thrift
30
31 ## Thrift Compiler
32
33 You can compile IDL sources for Node.js with the following command:
34
35 thrift --gen js:node thrift_file
36
37 ## Cassandra Client Example:
38
39 Here is a Cassandra example:
40
41 ```js
42 var thrift = require('thrift'),
43 Cassandra = require('./gen-nodejs/Cassandra')
44 ttypes = require('./gen-nodejs/cassandra_types');
45
46 var connection = thrift.createConnection("localhost", 9160),
47 client = thrift.createClient(Cassandra, connection);
48
49 connection.on('error', function(err) {
50 console.error(err);
51 });
52
53 client.get_slice("Keyspace", "key", new ttypes.ColumnParent({column_family: "ExampleCF"}), new ttypes.SlicePredicate({slice_range: new ttypes.SliceRange({start: '', finish: ''})}), ttypes.ConsistencyLevel.ONE, function(err, data) {
54 if (err) {
55 // handle err
56 } else {
57 // data == [ttypes.ColumnOrSuperColumn, ...]
58 }
59 connection.end();
60 });
61 ```
62
63 <a name="int64"></a>
64 ## Int64
65
66 Since JavaScript represents all numbers as doubles, int64 values cannot be accurately represented naturally. To solve this, int64 values in responses will be wrapped with Thrift.Int64 objects. The Int64 implementation used is [broofa/node-int64](https://github.com/broofa/node-int64).
67
68 ## Client and server examples
69
70 Several example clients and servers are included in the thrift/lib/nodejs/examples folder and the cross language tutorial thrift/tutorial/nodejs folder.
71
72 ## Use on browsers
73
74 You can use code generated with js:node on browsers with Webpack. Here is an example.
75
76 thrift --gen js:node,ts,es6,with_ns
77
78 ```
79 import * as thrift from 'thrift/browser';
80 import { MyServiceClient } from '../gen-nodejs/MyService';
81
82 let host = window.location.hostname;
83 let port = 443;
84 let opts = {
85 transport: thrift.TBufferedTransport,
86 protocol: thrift.TJSONProtocol,
87 headers: {
88 'Content-Type': 'application/vnd.apache.thrift.json',
89 },
90 https: true,
91 path: '/url/path',
92 useCORS: true,
93 };
94
95 let connection = thrift.createXHRConnection(host, port, opts);
96 let thriftClient = thrift.createXHRClient(MyServiceClient, connection);
97
98 connection.on('error', (err) => {
99 console.error(err);
100 });
101
102 thriftClient.myService(param)
103 .then((result) => {
104 console.log(result);
105 })
106 .catch((err) => {
107 ....
108 });
109 ```
110
111 Note that thrift/index.js must be renamed or skipped for browsers.