@opengis/fastify-table
Version:
core-plugins
70 lines (57 loc) • 2.19 kB
JavaScript
/* eslint-disable consistent-return */
import grpc from '@grpc/grpc-js';
import protoLoader from '@grpc/proto-loader';
import config from '../../../config.js';
import logger from '../logger/getLogger.js';
config.ready = config.ready || {};
// external ip not-accessible from internal network
const defaultOfficeConverterServerAddress = config.pg?.host?.startsWith('192.168.3') ? '192.168.1.96:4011' : '193.239.152.181:44011';
const officeConverterServerAddress = config.officeConverterServerAddress || defaultOfficeConverterServerAddress;
console.log('officeConverterServerAddress: ', officeConverterServerAddress, !!config.officeConverterServerAddress);
const relpath = 'server/plugins/grpc/utils/office2pdf.proto';
const protoLocation = process.cwd().includes('fastify-table') ? relpath : `node_modules/@opengis/fastify-table/${relpath}`;
const proto = grpc.loadPackageDefinition(
protoLoader.loadSync(protoLocation, {
keepCase: true,
longs: String,
enums: String,
defaults: true,
oneofs: true,
}),
);
const officeClient = new proto.OfficeConverterService(
officeConverterServerAddress,
grpc.credentials.createInsecure(),
{
'grpc.max_send_message_length': 512 * 1024 * 1024,
'grpc.max_receive_message_length': 512 * 1024 * 1024,
},
);
officeClient.waitForReady(Date.now() + 5000, (err) => {
if (err) {
config.ready.office2pdf = false;
console.error('Client connection timeout or failure:', err);
}
else {
config.ready.office2pdf = true;
console.log('Client connected successfully.');
// You can now make RPC calls safely
}
});
const officeToPdf = async (data) => new Promise((res, rej) => {
officeClient.OfficeToPdf(data, (err, data1) => {
if (err) {
logger.file('grpc/office2pdf', { error: err.toString(), stack: err.stack, ready: config.ready.office2pdf });
if (!config.ready.office2pdf) {
return rej(new Error('no grpc office2pdf connection'));
}
return rej(err);
}
res(data1);
});
});
const getOffice = () => ({
officeToPdf,
officeConverterServerAddress,
});
export default getOffice;