{"size": 1645, "ext": "js", "max_stars_repo_path": "dev/buffer.js", "max_stars_repo_name": "Ashikpaul/moleculer", "max_stars_repo_stars_event_min_datetime": "2018-02-28T08:37:39.000Z", "max_stars_repo_stars_event_max_datetime": "2022-03-31T23:22:36.000Z", "max_issues_repo_path": "dev/buffer.js", "max_issues_repo_name": "Ashikpaul/moleculer", "max_issues_repo_issues_event_min_datetime": "2018-02-28T10:17:43.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-31T09:04:36.000Z", "max_forks_repo_path": "dev/buffer.js", "max_forks_repo_name": "Ashikpaul/moleculer", "max_forks_repo_forks_event_min_datetime": "2018-02-28T08:57:09.000Z", "max_forks_repo_forks_event_max_datetime": "2022-03-27T10:57:26.000Z"}
{"max_stars_count": 4522, "max_issues_count": 809, "max_forks_count": 554, "avg_line_length": 19.3529411765, "max_line_length": 73, "alphanum_fraction": 0.6389057751}
"use strict";

const ServiceBroker = require("../src/service-broker");

let serializer = null;

// serializer = require('./serializer'); // NOPE
// serializer = 'Avro'; // NOPE
// serializer = "MsgPack"; // YES
serializer = "ProtoBuf"; // NOPE
// serializer = 'Thrift'; // NOPE

// Create server broker
const server = new ServiceBroker({
	namespace: "buffer",
	nodeID: "server",
	logger: true,
	logLevel: "info",
	transporter: {
		type: "TCP",
		maxPacketSize: 1 * 1024 * 1024
	},
	requestTimeout: 5 * 1000,
	serializer,
	circuitBreaker: {
		enabled: false
	},
	metrics: true,
	statistics: true,
	maxCallLevel: 10,
	heartbeatInterval: 2,
	heartbeatTimeout: 8
});

server.createService({
	name: "server",
	actions: {
		getABigBuffer(ctx) {
			const { size = 128 * 1024 } = ctx.params;
			return Buffer.allocUnsafe(size);
		}
	}
});

// Create client
const client = new ServiceBroker({
	namespace: "buffer",
	nodeID: "client",
	logger: true,
	logLevel: "info",
	transporter: {
		type: "TCP",
		maxPacketSize: 1 * 1024 * 1024
	},
	requestTimeout: 5 * 1000,
	serializer,
	circuitBreaker: {
		enabled: false
	},
	metrics: true,
	statistics: true,
	maxCallLevel: 10,
	heartbeatInterval: 2,
	heartbeatTimeout: 8
});

Promise.all([server.start(), client.start()])
	.then(() => client.waitForServices("server"))
	.then(() => {
		client.call("server.getABigBuffer", { size: 128 * 1024 }).then(res => {
			client.logger.info("got", inspect(res));
		});
	});

function inspect(x) {
	let rv = "";
	if (x.constructor) {
		rv = x.constructor.name;
	}
	if (typeof x === "object") {
		rv += ". Keys: " + Object.keys(x).join(", ").substr(0, 80);
	}
	return rv;
}
