UNPKG

@opengis/fastify-table

Version:

core-plugins

79 lines (78 loc) 3.01 kB
/* eslint-disable no-promise-executor-return */ /* eslint-disable no-console */ import path from "node:path"; import { fileURLToPath } from "url"; import grpc from "@grpc/grpc-js"; import protoLoader from "@grpc/proto-loader"; import config from "../../../config.js"; import logger from "../logger/getLogger.js"; const filename = fileURLToPath(import.meta.url); const dirname = path.dirname(filename); 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 === true ? defaultOfficeConverterServerAddress : config.officeConverterServerAddress; 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(`${dirname}/utils/office2pdf.proto`, { keepCase: true, longs: String, enums: String, defaults: true, oneofs: true, })); const OfficeConverterService = proto.OfficeConverterService; const officeClient = officeConverterServerAddress ? new OfficeConverterService(officeConverterServerAddress, grpc.credentials.createInsecure(), { "grpc.max_send_message_length": 512 * 1024 * 1024, "grpc.max_receive_message_length": 512 * 1024 * 1024, }) : {}; if (officeConverterServerAddress) { 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) => { if (!officeConverterServerAddress) { logger.file("grpc/convert", { method: "officeToPdf", error: "grpc office2pdf not set", stack: new Error().stack, }); return rej(new Error("grpc office2pdf not set")); } 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); } return res(data1); }); return null; }); const getOffice = () => ({ officeToPdf, officeConverterServerAddress, }); export default getOffice;