1 | import { lstatSync, realpathSync } from "node:fs";
|
2 | import path from "node:path";
|
3 | import { fileURLToPath } from "node:url";
|
4 | import { setFlagsFromString } from "node:v8";
|
5 | import { runInNewContext } from "node:vm";
|
6 | import dotenv from "dotenv";
|
7 | import { environment, isProduction, refreshEnvironmentCache } from "./env.js";
|
8 | import { AppError } from "./error.js";
|
9 | import { isNil } from "./lodash.js";
|
10 | import {
|
11 | loggerDetermineDefaultDestination,
|
12 | loggerExtendGlobalContext,
|
13 | newLogger,
|
14 | } from "./logger.js";
|
15 | import { _compasSentryExport } from "./sentry.js";
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 | export function getSecondsSinceEpoch() {
|
25 | return Math.floor(Date.now() / 1000);
|
26 | }
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 | export function noop() {}
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 | let internalGc = global.gc;
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 | export function gc() {
|
51 | if (isNil(internalGc)) {
|
52 | setFlagsFromString("--expose_gc");
|
53 | internalGc = runInNewContext("gc");
|
54 | }
|
55 |
|
56 |
|
57 | internalGc();
|
58 | }
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 | export function mainFn(meta, cb) {
|
73 | const { isMainFn, name } = isMainFnAndReturnName(meta);
|
74 | if (!isMainFn) {
|
75 | return;
|
76 | }
|
77 |
|
78 |
|
79 | dotenv.config({ path: path.resolve(process.cwd(), ".env.local") });
|
80 | dotenv.config();
|
81 |
|
82 | refreshEnvironmentCache();
|
83 |
|
84 | if (isProduction() && environment.APP_NAME) {
|
85 | loggerExtendGlobalContext({
|
86 | application: environment.APP_NAME,
|
87 | });
|
88 | }
|
89 |
|
90 | loggerDetermineDefaultDestination();
|
91 |
|
92 | const logger = newLogger({
|
93 | ctx: { type: name },
|
94 | });
|
95 |
|
96 | const unhandled = (error) => {
|
97 | logger.error(error);
|
98 | process.exit(1);
|
99 | };
|
100 |
|
101 | process.on("unhandledRejection", (reason, promise) => {
|
102 | if (_compasSentryExport) {
|
103 | _compasSentryExport.captureException(reason);
|
104 | }
|
105 |
|
106 | unhandled({
|
107 | type: "unhandledRejection",
|
108 | reason: AppError.format(reason),
|
109 | promise,
|
110 | });
|
111 | });
|
112 |
|
113 | process.on("uncaughtException", (error, origin) => {
|
114 | if (_compasSentryExport) {
|
115 | _compasSentryExport.captureException(error);
|
116 | }
|
117 |
|
118 | unhandled({
|
119 | type: "uncaughtException",
|
120 | error: AppError.format(error),
|
121 | origin,
|
122 | });
|
123 | });
|
124 |
|
125 | process.on("warning", (warn) => {
|
126 | logger.error({
|
127 | type: "warning",
|
128 | warning: AppError.format(warn),
|
129 | });
|
130 | });
|
131 |
|
132 | Promise.resolve(cb(logger)).catch((e) => {
|
133 | if (_compasSentryExport) {
|
134 | _compasSentryExport.captureException(e);
|
135 | }
|
136 |
|
137 | unhandled({
|
138 | type: "error",
|
139 | message: "Error caught from callback passed in `mainFn`",
|
140 | error: AppError.format(e),
|
141 | });
|
142 | });
|
143 | }
|
144 |
|
145 |
|
146 |
|
147 |
|
148 |
|
149 |
|
150 |
|
151 |
|
152 |
|
153 | export function filenameForModule(meta) {
|
154 | return fileURLToPath(meta.url);
|
155 | }
|
156 |
|
157 |
|
158 |
|
159 |
|
160 |
|
161 |
|
162 |
|
163 |
|
164 |
|
165 | export function dirnameForModule(meta) {
|
166 | return path.dirname(filenameForModule(meta));
|
167 | }
|
168 |
|
169 |
|
170 |
|
171 |
|
172 |
|
173 |
|
174 |
|
175 |
|
176 | export function isMainFnAndReturnName(meta) {
|
177 | const modulePath = fileURLToPath(meta.url);
|
178 |
|
179 | let scriptPath = process.argv[1];
|
180 |
|
181 |
|
182 | const scriptStat = lstatSync(scriptPath);
|
183 | if (scriptStat.isSymbolicLink()) {
|
184 | scriptPath = realpathSync(scriptPath);
|
185 | }
|
186 | const scriptPathExt = path.extname(scriptPath);
|
187 | if (scriptPathExt) {
|
188 | return {
|
189 | isMainFn: modulePath === scriptPath,
|
190 | name: scriptPath
|
191 | .substring(0, scriptPath.length - scriptPathExt.length)
|
192 | .split(path.sep)
|
193 | .pop(),
|
194 | };
|
195 | }
|
196 |
|
197 | let modulePathWithoutExt = modulePath;
|
198 | const modulePathExt = path.extname(modulePath);
|
199 | if (modulePathExt) {
|
200 | modulePathWithoutExt = modulePathWithoutExt.slice(0, -modulePathExt.length);
|
201 | }
|
202 |
|
203 | return {
|
204 | isMainFn: modulePathWithoutExt === scriptPath,
|
205 | name: modulePathWithoutExt.split(path.sep).pop(),
|
206 | };
|
207 | }
|