Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Thrift is a software framework for scalable cross-language services development.
The thrift npm package is a JavaScript implementation of the Apache Thrift framework, which is used for scalable cross-language services development. Thrift allows you to define data types and service interfaces in a simple definition file, and it generates code to be used across multiple languages. This makes it easier to build and maintain services that communicate across different programming languages.
Defining a Thrift Service
This code demonstrates how to define a Thrift service in Node.js. The service is defined in a Thrift IDL file, which is then compiled to generate the necessary JavaScript files. The server listens on port 9090 and implements a method called `myMethod`.
const thrift = require('thrift');
const MyService = require('./gen-nodejs/MyService');
const ttypes = require('./gen-nodejs/my_types');
const server = thrift.createServer(MyService, {
myMethod: function(arg, result) {
console.log('myMethod called with:', arg);
result(null, 'response');
}
});
server.listen(9090);
Creating a Thrift Client
This code demonstrates how to create a Thrift client in Node.js. The client connects to the Thrift server running on localhost at port 9090 and calls the `myMethod` function, passing an argument and handling the response.
const thrift = require('thrift');
const MyService = require('./gen-nodejs/MyService');
const connection = thrift.createConnection('localhost', 9090);
const client = thrift.createClient(MyService, connection);
client.myMethod('argument', (err, response) => {
if (err) {
console.error(err);
} else {
console.log('Response:', response);
}
connection.end();
});
Defining Thrift Data Types
This Thrift IDL file defines a data structure `MyStruct` and a service `MyService` with a method `myMethod` that takes `MyStruct` as an argument. This file is used to generate the necessary code for both the client and server.
namespace js MyService
struct MyStruct {
1: i32 id,
2: string name
}
service MyService {
string myMethod(1: MyStruct myStruct)
}
gRPC is a high-performance, open-source universal RPC framework originally developed by Google. It uses HTTP/2 for transport, Protocol Buffers as the interface description language, and provides features such as authentication, load balancing, and more. Compared to Thrift, gRPC is more modern and has better support for streaming and bi-directional communication.
protobufjs is a pure JavaScript implementation of Protocol Buffers, Google's data interchange format. While it does not provide RPC capabilities out of the box like Thrift, it is often used in conjunction with gRPC to define and serialize structured data. It is more lightweight and focused solely on data serialization.
avro-js is a JavaScript library for working with Apache Avro, a data serialization system. Avro is similar to Thrift in that it provides a compact, fast, binary data format. However, Avro is more focused on data serialization and schema evolution, and does not provide RPC capabilities.
This browser based Apache Thrift implementation supports RPC clients using the JSON protocol over Http[s] with XHR and WebSocket.
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
This is the base directory for the Apache Thrift JavaScript library. This directory contains a Gruntfile.js and a package.json. Many of the build and test tools used here require a recent version of Node.js to be installed. To install the support files for the Grunt build tool execute the command:
npm install
This reads the package.json and pulls in the appropriate sources from the internet. To build the JavaScript branch of Apache Thrift execute the command:
npx grunt
This runs the grunt build tool (from within ./node_modules/.bin/
),
linting all of the source files, setting up and running the
tests, concatenating and minifying the main libraries and
generating the html documentation.
The following directories are present (some only after the grunt build): /src - The JavaScript Apache Thrift source /doc - HTML documentation /dist - Distribution files (thrift.js and thrift.min.js) /test - Various tests, this is a good place to look for example code /node_modules - Build support files installed by npm
The listing below demonstrates a simple browser based JavaScript Thrift client and Node.js JavaScript server for the hello_svc service.
service hello_svc {
string get_message(1: string name)
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Hello Thrift</title>
</head>
<body>
Name: <input type="text" id="name_in">
<input type="button" id="get_msg" value="Get Message" >
<div id="output"></div>
<script src="thrift.js"></script>
<script src="gen-js/hello_svc.js"></script>
<script>
(function() {
var transport = new Thrift.TXHRTransport("/hello");
var protocol = new Thrift.TJSONProtocol(transport);
var client = new hello_svcClient(protocol);
var nameElement = document.getElementById("name_in");
var outputElement = document.getElementById("output");
document.getElementById("get_msg")
.addEventListener("click", function(){
client.get_message(nameElement.value, function(result) {
outputElement.innerHTML = result;
});
});
})();
</script>
</body>
</html>
var thrift = require('thrift');
var hello_svc = require('./gen-nodejs/hello_svc.js');
var hello_handler = {
get_message: function(name, result) {
var msg = "Hello " + name + "!";
result(null, msg);
}
}
var hello_svc_opt = {
transport: thrift.TBufferedTransport,
protocol: thrift.TJSONProtocol,
processor: hello_svc,
handler: hello_handler
};
var server_opt = {
staticFilePath: ".",
services: {
"/hello": hello_svc_opt
}
}
var server = Thrift.createWebServer(server_opt);
var port = 9099;
server.listen(port);
console.log("Http/Thrift Server running on port: " + port);
TypeScript definition files can also be generated by running:
thrift --gen js:ts file.thrift
0.13.0
FAQs
node.js bindings for the Apache Thrift RPC system
The npm package thrift receives a total of 222,945 weekly downloads. As such, thrift popularity was classified as popular.
We found that thrift demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 5 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.