@tomei/finance
Version:
NestJS package for finance module
308 lines • 12 kB
JavaScript
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.ChromeSetup = void 0;
const child_process_1 = require("child_process");
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const os = __importStar(require("os"));
class ChromeSetup {
constructor() {
this.chromeExecutable = null;
this.setupAttempted = false;
this.platform = os.platform();
}
static getInstance() {
if (!ChromeSetup.instance) {
ChromeSetup.instance = new ChromeSetup();
}
return ChromeSetup.instance;
}
async getChromeExecutable() {
if (this.chromeExecutable) {
return this.chromeExecutable;
}
this.chromeExecutable = this.findChromeExecutable();
if (!this.chromeExecutable && !this.setupAttempted) {
try {
await this.setupChrome();
this.chromeExecutable = this.findChromeExecutable();
this.setupAttempted = true;
}
catch (error) {
console.warn('Automatic Chrome setup failed:', error);
this.setupAttempted = true;
}
}
return this.chromeExecutable;
}
findChromeExecutable() {
let possiblePaths = [];
switch (this.platform) {
case 'win32':
possiblePaths = [
process.env.PUPPETEER_EXECUTABLE_PATH,
'C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe',
'C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe',
path.join(process.env.USERPROFILE || '', 'AppData', 'Local', 'Google', 'Chrome', 'Application', 'chrome.exe'),
].filter(Boolean);
break;
case 'darwin':
possiblePaths = [
process.env.PUPPETEER_EXECUTABLE_PATH,
'/Applications/Google Chrome.app/Contents/MacOS/Google Chrome',
'/Applications/Chromium.app/Contents/MacOS/Chromium',
].filter(Boolean);
break;
case 'linux':
default:
possiblePaths = [
process.env.PUPPETEER_EXECUTABLE_PATH,
'/usr/bin/google-chrome',
'/usr/bin/google-chrome-stable',
'/usr/bin/chromium-browser',
'/usr/bin/chromium',
'/usr/bin/chrome',
'/opt/google/chrome/chrome',
].filter(Boolean);
break;
}
const cacheDirectories = this.getCacheDirectories();
for (const cacheDir of cacheDirectories) {
try {
let chromePath;
if (this.platform === 'win32') {
try {
const result = (0, child_process_1.execSync)(`dir "${cacheDir}" /s /b 2>nul | findstr chrome.exe`, { encoding: 'utf8' }).trim();
chromePath = result.split('\n')[0];
}
catch (e) {
continue;
}
}
else {
try {
chromePath = (0, child_process_1.execSync)(`find "${cacheDir}" -name "chrome" -type f -executable 2>/dev/null | head -1`, { encoding: 'utf8' }).trim();
}
catch (e) {
continue;
}
}
if (chromePath && fs.existsSync(chromePath)) {
possiblePaths.unshift(chromePath);
}
}
catch (e) {
}
}
for (const chromePath of possiblePaths) {
if (chromePath && fs.existsSync(chromePath)) {
try {
fs.accessSync(chromePath, fs.constants.X_OK);
return chromePath;
}
catch (e) {
if (this.platform === 'win32') {
return chromePath;
}
}
}
}
return null;
}
getCacheDirectories() {
const userId = process.getuid ? process.getuid() : null;
const isRoot = userId === 0;
switch (this.platform) {
case 'win32':
return [
process.env.PUPPETEER_CACHE_DIR,
path.join(process.env.USERPROFILE || process.env.HOME || os.tmpdir(), '.cache', 'puppeteer'),
].filter(Boolean);
case 'darwin':
return [
process.env.PUPPETEER_CACHE_DIR,
path.join(process.env.HOME || os.tmpdir(), '.cache', 'puppeteer'),
].filter(Boolean);
case 'linux':
default:
const paths = [process.env.PUPPETEER_CACHE_DIR];
if (isRoot) {
paths.push('/root/.cache/puppeteer');
}
else {
paths.push(path.join(process.env.HOME || os.tmpdir(), '.cache', 'puppeteer'));
}
return paths.filter(Boolean);
}
}
async setupChrome() {
console.log('Setting up Chrome for PDF generation...');
const cacheDirectories = this.getCacheDirectories();
const cacheDir = cacheDirectories[0] || this.getDefaultCacheDir();
try {
if (this.platform === 'win32') {
(0, child_process_1.execSync)(`if not exist "${cacheDir}" mkdir "${cacheDir}"`, {
stdio: 'pipe',
});
}
else {
(0, child_process_1.execSync)(`mkdir -p "${cacheDir}"`, { stdio: 'pipe' });
}
const installEnv = {
...process.env,
PUPPETEER_CACHE_DIR: cacheDir,
};
if (this.platform === 'linux') {
const userId = process.getuid ? process.getuid() : null;
const isRoot = userId === 0;
installEnv.HOME = isRoot ? '/root' : process.env.HOME || '/tmp';
}
(0, child_process_1.execSync)('npx puppeteer browsers install chrome', {
stdio: 'pipe',
env: installEnv,
timeout: 300000,
});
console.log('Chrome setup completed successfully');
}
catch (error) {
throw new Error(`Chrome setup failed: ${error}`);
}
}
getDefaultCacheDir() {
const userId = process.getuid ? process.getuid() : null;
const isRoot = userId === 0;
switch (this.platform) {
case 'win32':
return path.join(process.env.USERPROFILE || process.env.HOME || os.tmpdir(), '.cache', 'puppeteer');
case 'darwin':
return path.join(process.env.HOME || os.tmpdir(), '.cache', 'puppeteer');
case 'linux':
default:
if (isRoot) {
return '/root/.cache/puppeteer';
}
else {
return path.join(process.env.HOME || os.tmpdir(), '.cache', 'puppeteer');
}
}
}
async getBrowserLaunchOptions() {
const chromeExecutable = await this.getChromeExecutable();
const baseArgs = [
'--no-sandbox',
'--disable-setuid-sandbox',
'--disable-dev-shm-usage',
'--disable-accelerated-2d-canvas',
'--no-first-run',
'--disable-gpu',
'--disable-extensions',
'--disable-default-apps',
'--disable-background-networking',
'--disable-sync',
'--metrics-recording-only',
'--no-default-browser-check',
'--no-first-run',
'--mute-audio',
'--hide-scrollbars',
'--disable-client-side-phishing-detection',
'--disable-component-extensions-with-background-pages',
'--disable-ipc-flooding-protection',
];
let platformArgs = [];
switch (this.platform) {
case 'win32':
platformArgs = [
'--disable-background-timer-throttling',
'--disable-backgrounding-occluded-windows',
'--disable-renderer-backgrounding',
];
break;
case 'linux':
platformArgs = [
'--no-zygote',
'--single-process',
'--disable-web-security',
'--disable-features=VizDisplayCompositor',
'--disable-background-timer-throttling',
'--disable-backgrounding-occluded-windows',
'--disable-renderer-backgrounding',
];
break;
case 'darwin':
platformArgs = [
'--disable-background-timer-throttling',
'--disable-backgrounding-occluded-windows',
];
break;
}
const options = {
headless: true,
args: [...baseArgs, ...platformArgs],
timeout: 0,
dumpio: false,
handleSIGINT: false,
handleSIGTERM: false,
handleSIGHUP: false,
protocolTimeout: 0,
};
if (chromeExecutable) {
options.executablePath = chromeExecutable;
}
return options;
}
async isReady() {
const executable = await this.getChromeExecutable();
return executable !== null;
}
getSetupInstructions() {
const instructions = ['Run: npx puppeteer browsers install chrome'];
switch (this.platform) {
case 'linux':
instructions.push('Install dependencies: sudo apt-get install -y libxfixes3 libnss3 libatk-bridge2.0-0t64');
instructions.push('Set environment: export PUPPETEER_CACHE_DIR=~/.cache/puppeteer');
break;
case 'win32':
instructions.push('Set environment: set PUPPETEER_CACHE_DIR=%USERPROFILE%\\.cache\\puppeteer');
break;
case 'darwin':
instructions.push('Set environment: export PUPPETEER_CACHE_DIR=~/.cache/puppeteer');
break;
}
return instructions;
}
}
exports.ChromeSetup = ChromeSetup;
//# sourceMappingURL=chrome-setup.js.map