blob: 5c7a1c23b846dab609334a7c611b82cef048dfa2 [file] [log] [blame] [view]
T Jake Luciani322caa22010-02-15 03:24:55 +00001Thrift Javascript Library
Henrique Mendonça095ddb72013-09-20 19:38:03 +02002=========================
3This browser based Apache Thrift implementation supports
henrique2a7dccc2014-03-07 22:16:51 +01004RPC clients using the JSON protocol over Http[s] with XHR
5and WebSocket.
T Jake Luciani322caa22010-02-15 03:24:55 +00006
7License
Henrique Mendonça095ddb72013-09-20 19:38:03 +02008-------
T Jake Luciani322caa22010-02-15 03:24:55 +00009Licensed to the Apache Software Foundation (ASF) under one
10or more contributor license agreements. See the NOTICE file
11distributed with this work for additional information
12regarding copyright ownership. The ASF licenses this file
13to you under the Apache License, Version 2.0 (the
14"License"); you may not use this file except in compliance
15with the License. You may obtain a copy of the License at
16
17 http://www.apache.org/licenses/LICENSE-2.0
18
19Unless required by applicable law or agreed to in writing,
20software distributed under the License is distributed on an
21"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
22KIND, either express or implied. See the License for the
23specific language governing permissions and limitations
24under the License.
25
henrique2a7dccc2014-03-07 22:16:51 +010026Grunt Build
27------------
Roger Meier52744ee2014-03-12 09:38:42 +010028This is the base directory for the Apache Thrift JavaScript
henrique2a7dccc2014-03-07 22:16:51 +010029library. This directory contains a Gruntfile.js and a
30package.json. Many of the build and test tools used here
31require a recent version of Node.js to be installed. To
32install the support files for the Grunt build tool execute
33the command:
henriqued0265c72014-05-19 19:11:10 +020034
35 npm install
36
henrique2a7dccc2014-03-07 22:16:51 +010037This reads the package.json and pulls in the appropriate
38sources from the internet. To build the JavaScript branch
39of Apache Thrift execute the command:
henriqued0265c72014-05-19 19:11:10 +020040
Brian Forbisb5d6ea32018-08-25 23:39:29 -040041 npx grunt
henriqued0265c72014-05-19 19:11:10 +020042
Brian Forbisb5d6ea32018-08-25 23:39:29 -040043This runs the grunt build tool (from within `./node_modules/.bin/`),
44linting all of the source files, setting up and running the
45tests, concatenating and minifying the main libraries and
46generating the html documentation.
Randy Abernethyb2652302014-11-15 12:02:57 -080047
henrique2a7dccc2014-03-07 22:16:51 +010048Tree
49----
50The following directories are present (some only after the
51grunt build):
52 /src - The JavaScript Apache Thrift source
Roger Meier52744ee2014-03-12 09:38:42 +010053 /doc - HTML documentation
henrique2a7dccc2014-03-07 22:16:51 +010054 /dist - Distribution files (thrift.js and thrift.min.js)
55 /test - Various tests, this is a good place to look for
56 example code
57 /node_modules - Build support files installed by npm
58
59
60Example JavaScript Client and Server
61------------------------------------
Henrique Mendonça095ddb72013-09-20 19:38:03 +020062The listing below demonstrates a simple browser based JavaScript
henriquea2de4102014-02-07 14:12:56 +010063Thrift client and Node.js JavaScript server for the hello_svc
64service.
T Jake Luciani322caa22010-02-15 03:24:55 +000065
henrique2a7dccc2014-03-07 22:16:51 +010066### hello.thrift - Service IDL
67### build with: $ thrift -gen js -gen js:node hello.thrift
henriquea2de4102014-02-07 14:12:56 +010068 service hello_svc {
69 string get_message(1: string name)
Henrique Mendonça095ddb72013-09-20 19:38:03 +020070 }
T Jake Luciani322caa22010-02-15 03:24:55 +000071
Henrique Mendonça095ddb72013-09-20 19:38:03 +020072### hello.html - Browser Client
henriquea2de4102014-02-07 14:12:56 +010073 <!DOCTYPE html>
Henrique Mendonça095ddb72013-09-20 19:38:03 +020074 <html lang="en">
henriquea2de4102014-02-07 14:12:56 +010075 <head>
76 <meta charset="utf-8">
77 <title>Hello Thrift</title>
78 </head>
79 <body>
80 Name: <input type="text" id="name_in">
81 <input type="button" id="get_msg" value="Get Message" >
82 <div id="output"></div>
83
84 <script src="thrift.js"></script>
85 <script src="gen-js/hello_svc.js"></script>
86 <script>
87 (function() {
Roger Meier52744ee2014-03-12 09:38:42 +010088 var transport = new Thrift.TXHRTransport("/hello");
89 var protocol = new Thrift.TJSONProtocol(transport);
henriquea2de4102014-02-07 14:12:56 +010090 var client = new hello_svcClient(protocol);
91 var nameElement = document.getElementById("name_in");
92 var outputElement = document.getElementById("output");
93 document.getElementById("get_msg")
94 .addEventListener("click", function(){
95 client.get_message(nameElement.value, function(result) {
96 outputElement.innerHTML = result;
97 });
98 });
99 })();
Henrique Mendonça095ddb72013-09-20 19:38:03 +0200100 </script>
henriquea2de4102014-02-07 14:12:56 +0100101 </body>
Henrique Mendonça095ddb72013-09-20 19:38:03 +0200102 </html>
T Jake Luciani322caa22010-02-15 03:24:55 +0000103
Henrique Mendonça095ddb72013-09-20 19:38:03 +0200104### hello.js - Node Server
Roger Meier52744ee2014-03-12 09:38:42 +0100105 var thrift = require('thrift');
henriquea2de4102014-02-07 14:12:56 +0100106 var hello_svc = require('./gen-nodejs/hello_svc.js');
Henrique Mendonça095ddb72013-09-20 19:38:03 +0200107
henriquea2de4102014-02-07 14:12:56 +0100108 var hello_handler = {
109 get_message: function(name, result) {
110 var msg = "Hello " + name + "!";
111 result(null, msg);
Henrique Mendonça095ddb72013-09-20 19:38:03 +0200112 }
henriquea2de4102014-02-07 14:12:56 +0100113 }
Henrique Mendonça095ddb72013-09-20 19:38:03 +0200114
henriquea2de4102014-02-07 14:12:56 +0100115 var hello_svc_opt = {
Roger Meier52744ee2014-03-12 09:38:42 +0100116 transport: thrift.TBufferedTransport,
117 protocol: thrift.TJSONProtocol,
118 processor: hello_svc,
henriquea2de4102014-02-07 14:12:56 +0100119 handler: hello_handler
120 };
121
122 var server_opt = {
123 staticFilePath: ".",
124 services: {
125 "/hello": hello_svc_opt
126 }
127 }
128
Roger Meier52744ee2014-03-12 09:38:42 +0100129 var server = Thrift.createWebServer(server_opt);
henriquea2de4102014-02-07 14:12:56 +0100130 var port = 9099;
131 server.listen(port);
henrique2a7dccc2014-03-07 22:16:51 +0100132 console.log("Http/Thrift Server running on port: " + port);
133
henriqued0265c72014-05-19 19:11:10 +0200134
135TypeScript
136------------------------------------
137TypeScript definition files can also be generated by running:
138
139 thrift --gen js:ts file.thrift
140
Mustafa Senol Cosarf86845e2018-12-05 17:50:18 +0300141# Breaking Changes
142
143## 0.13.0
144
1451. 64-bit integer constants are now generatd using node-int64 e.g.: var x = new Int64("7fffffffffffffff");