modelence 0.7.0 → 0.7.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/telemetry.js CHANGED
@@ -1,2 +1,2 @@
1
- export{l as captureError,h as logDebug,j as logError,i as logInfo,k as startTransaction}from'./chunk-DVECB2TP.js';//# sourceMappingURL=telemetry.js.map
1
+ export{l as captureError,h as logDebug,j as logError,i as logInfo,k as startTransaction}from'./chunk-PB6WQQ4L.js';//# sourceMappingURL=telemetry.js.map
2
2
  //# sourceMappingURL=telemetry.js.map
@@ -1,7 +1,7 @@
1
1
  import './index-CwdohC5n.js';
2
2
  import { P as Permission, d as Session, e as UserInfo } from './types-Ds1ESQSs.js';
3
3
  import * as mongodb from 'mongodb';
4
- import { WithId, IndexDescription, SearchIndexDescription, MongoClient, Collection, Filter, FindOptions, ObjectId, Document, OptionalUnlessRequiredId, InsertOneResult, InsertManyResult, UpdateFilter, UpdateResult, ClientSession, DeleteResult, AggregateOptions, AggregationCursor, AnyBulkWriteOperation, BulkWriteResult } from 'mongodb';
4
+ import { WithId, IndexDescription, SearchIndexDescription, MongoClient, Collection, FilterOperators, Document, FindOptions, ObjectId, OptionalUnlessRequiredId, InsertOneResult, InsertManyResult, UpdateFilter, UpdateResult, ClientSession, DeleteResult, AggregateOptions, AggregationCursor, AnyBulkWriteOperation, BulkWriteResult } from 'mongodb';
5
5
  import { z, ZodNumber, ZodArray } from 'zod';
6
6
  import { Request, Response, NextFunction } from 'express';
7
7
 
@@ -137,6 +137,72 @@ type SerializedSchema = BaseSerializedSchema | (BaseSerializedSchema & {
137
137
  optional: true;
138
138
  });
139
139
 
140
+ /**
141
+ * Top-level query operators (logical and evaluation) - custom version without Document index signature
142
+ * Based on MongoDB's RootFilterOperators but without the [key: string]: any from Document
143
+ * @internal
144
+ */
145
+ type StrictRootFilterOperators<TSchema> = {
146
+ $and?: TypedFilter<TSchema>[];
147
+ $or?: TypedFilter<TSchema>[];
148
+ $nor?: TypedFilter<TSchema>[];
149
+ $not?: TypedFilter<TSchema>;
150
+ $text?: {
151
+ $search: string;
152
+ $language?: string;
153
+ $caseSensitive?: boolean;
154
+ $diacriticSensitive?: boolean;
155
+ };
156
+ $where?: string | ((this: TSchema) => boolean);
157
+ $comment?: string | Document;
158
+ $expr?: any;
159
+ $jsonSchema?: any;
160
+ };
161
+ /**
162
+ * Type-safe MongoDB filter that ensures only schema fields can be queried
163
+ * while supporting all MongoDB query operators and dot notation for nested fields.
164
+ *
165
+ * This type combines:
166
+ * - MongoDB's native `FilterOperators<T>` for field-level operators (comprehensive operator support)
167
+ * - Custom `StrictRootFilterOperators<T>` for top-level operators without index signature
168
+ * - Custom restriction: only strings containing dots are allowed for nested field queries
169
+ *
170
+ * @example
171
+ * ```ts
172
+ * const dbUsers = new Store('users', {
173
+ * schema: {
174
+ * name: schema.string(),
175
+ * age: schema.number(),
176
+ * tags: schema.array(schema.string()),
177
+ * address: schema.object({
178
+ * street: schema.string(),
179
+ * city: schema.string(),
180
+ * }),
181
+ * },
182
+ * indexes: []
183
+ * });
184
+ *
185
+ * // ✅ Valid - field exists in schema
186
+ * await dbUsers.findOne({ name: 'John' });
187
+ *
188
+ * // ✅ Valid - using MongoDB operators (from FilterOperators)
189
+ * await dbUsers.findOne({ age: { $gt: 18 } });
190
+ * await dbUsers.findOne({ tags: { $in: ['typescript', 'mongodb'] } });
191
+ * await dbUsers.findOne({ $or: [{ name: 'John' }, { name: 'Jane' }] });
192
+ *
193
+ * // ✅ Valid - dot notation for nested fields (must contain a dot)
194
+ * await dbUsers.findOne({ 'address.city': 'New York' });
195
+ * await dbUsers.findOne({ 'emails.0.address': 'test@example.com' });
196
+ *
197
+ * // ❌ TypeScript error - 'id' is not in schema and doesn't contain a dot
198
+ * await dbUsers.findOne({ id: '123' });
199
+ * ```
200
+ */
201
+ type TypedFilter<T> = {
202
+ [K in keyof WithId<T>]?: WithId<T>[K] | FilterOperators<WithId<T>[K]>;
203
+ } & StrictRootFilterOperators<T> & {
204
+ [K: DottedString]: any;
205
+ };
140
206
  /**
141
207
  * Helper type to preserve method types when extending a store.
142
208
  * Maps each method to work with the extended schema while preserving signatures.
@@ -257,8 +323,29 @@ declare class Store<TSchema extends ModelSchema, TMethods extends Record<string,
257
323
  requireCollection(): Collection<this["_type"]>;
258
324
  /** @internal */
259
325
  requireClient(): MongoClient;
260
- findOne(query: Filter<this['_type']>, options?: FindOptions): Promise<this["_doc"] | null>;
261
- requireOne(query: Filter<this['_type']>, options?: FindOptions, errorHandler?: () => Error): Promise<this['_doc']>;
326
+ /**
327
+ * Finds a single document matching the query
328
+ *
329
+ * @param query - Type-safe query filter. Only schema fields, MongoDB operators, and dot notation are allowed.
330
+ * @param options - Find options
331
+ * @returns The document, or null if not found
332
+ *
333
+ * @example
334
+ * ```ts
335
+ * // ✅ Valid queries:
336
+ * await store.findOne({ name: 'John' })
337
+ * await store.findOne({ age: { $gt: 18 } })
338
+ * await store.findOne({ _id: new ObjectId('...') })
339
+ * await store.findOne({ tags: { $in: ['typescript', 'mongodb'] } })
340
+ * await store.findOne({ $or: [{ name: 'John' }, { name: 'Jane' }] })
341
+ * await store.findOne({ 'emails.address': 'test@example.com' }) // dot notation
342
+ *
343
+ * // ❌ TypeScript error - 'id' is not in schema:
344
+ * await store.findOne({ id: '123' })
345
+ * ```
346
+ */
347
+ findOne(query: TypedFilter<this['_type']>, options?: FindOptions): Promise<this["_doc"] | null>;
348
+ requireOne(query: TypedFilter<this['_type']>, options?: FindOptions, errorHandler?: () => Error): Promise<this['_doc']>;
262
349
  private find;
263
350
  /**
264
351
  * Fetches a single document by its ID
@@ -281,7 +368,7 @@ declare class Store<TSchema extends ModelSchema, TMethods extends Record<string,
281
368
  * @param query - The query to filter documents
282
369
  * @returns The number of documents that match the query
283
370
  */
284
- countDocuments(query: Filter<this['_type']>): Promise<number>;
371
+ countDocuments(query: TypedFilter<this['_type']>): Promise<number>;
285
372
  /**
286
373
  * Fetches multiple documents, equivalent to Node.js MongoDB driver's `find` and `toArray` methods combined.
287
374
  *
@@ -289,7 +376,7 @@ declare class Store<TSchema extends ModelSchema, TMethods extends Record<string,
289
376
  * @param options - Options
290
377
  * @returns The documents
291
378
  */
292
- fetch(query: Filter<this['_type']>, options?: {
379
+ fetch(query: TypedFilter<this['_type']>, options?: {
293
380
  sort?: Document;
294
381
  limit?: number;
295
382
  skip?: number;
@@ -315,7 +402,7 @@ declare class Store<TSchema extends ModelSchema, TMethods extends Record<string,
315
402
  * @param update - The update to apply to the document
316
403
  * @returns The result of the update operation
317
404
  */
318
- updateOne(selector: Filter<this['_type']> | string | ObjectId, update: UpdateFilter<this['_type']>): Promise<UpdateResult>;
405
+ updateOne(selector: TypedFilter<this['_type']> | string | ObjectId, update: UpdateFilter<this['_type']>): Promise<UpdateResult>;
319
406
  /**
320
407
  * Updates a single document, or inserts it if it doesn't exist
321
408
  *
@@ -323,7 +410,7 @@ declare class Store<TSchema extends ModelSchema, TMethods extends Record<string,
323
410
  * @param update - The MongoDB modifier to apply to the document
324
411
  * @returns The result of the update operation
325
412
  */
326
- upsertOne(selector: Filter<this['_type']> | string | ObjectId, update: UpdateFilter<this['_type']>): Promise<UpdateResult>;
413
+ upsertOne(selector: TypedFilter<this['_type']> | string | ObjectId, update: UpdateFilter<this['_type']>): Promise<UpdateResult>;
327
414
  /**
328
415
  * Updates multiple documents
329
416
  *
@@ -331,7 +418,7 @@ declare class Store<TSchema extends ModelSchema, TMethods extends Record<string,
331
418
  * @param update - The MongoDB modifier to apply to the documents
332
419
  * @returns The result of the update operation
333
420
  */
334
- updateMany(selector: Filter<this['_type']>, update: UpdateFilter<this['_type']>, options?: {
421
+ updateMany(selector: TypedFilter<this['_type']>, update: UpdateFilter<this['_type']>, options?: {
335
422
  session?: ClientSession;
336
423
  }): Promise<UpdateResult>;
337
424
  /**
@@ -341,21 +428,21 @@ declare class Store<TSchema extends ModelSchema, TMethods extends Record<string,
341
428
  * @param update - The MongoDB modifier to apply to the documents
342
429
  * @returns The result of the update operation
343
430
  */
344
- upsertMany(selector: Filter<this['_type']>, update: UpdateFilter<this['_type']>): Promise<UpdateResult>;
431
+ upsertMany(selector: TypedFilter<this['_type']>, update: UpdateFilter<this['_type']>): Promise<UpdateResult>;
345
432
  /**
346
433
  * Deletes a single document
347
434
  *
348
435
  * @param selector - The selector to find the document to delete
349
436
  * @returns The result of the delete operation
350
437
  */
351
- deleteOne(selector: Filter<this['_type']>): Promise<DeleteResult>;
438
+ deleteOne(selector: TypedFilter<this['_type']>): Promise<DeleteResult>;
352
439
  /**
353
440
  * Deletes multiple documents
354
441
  *
355
442
  * @param selector - The selector to find the documents to delete
356
443
  * @returns The result of the delete operation
357
444
  */
358
- deleteMany(selector: Filter<this['_type']>): Promise<DeleteResult>;
445
+ deleteMany(selector: TypedFilter<this['_type']>): Promise<DeleteResult>;
359
446
  /**
360
447
  * Aggregates documents using MongoDB's aggregation framework
361
448
  *
package/dist/types.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  export { A as AppServer, E as ExpressMiddleware, M as ModelenceConfig } from './index-CwdohC5n.js';
2
- export { A as Args, B as BodyConfig, i as ClientInfo, b as ConnectionInfo, j as Context, n as CronJob, C as CronJobInputParams, o as CronJobMetadata, h as EmailAttachment, d as EmailPayload, E as EmailProvider, r as ExpressHandler, k as Handler, H as HttpMethod, I as InferDocumentType, m as Method, M as MethodDefinition, l as MethodType, p as ModelSchema, a as RateLimitRule, c as RateLimitType, R as RouteDefinition, e as RouteHandler, q as RouteHandlers, f as RouteParams, g as RouteResponse, s as schema } from './types-Ch2QSF7X.js';
2
+ export { A as Args, B as BodyConfig, i as ClientInfo, b as ConnectionInfo, j as Context, n as CronJob, C as CronJobInputParams, o as CronJobMetadata, h as EmailAttachment, d as EmailPayload, E as EmailProvider, r as ExpressHandler, k as Handler, H as HttpMethod, I as InferDocumentType, m as Method, M as MethodDefinition, l as MethodType, p as ModelSchema, a as RateLimitRule, c as RateLimitType, R as RouteDefinition, e as RouteHandler, q as RouteHandlers, f as RouteParams, g as RouteResponse, s as schema } from './types-WgRbQ-tj.js';
3
3
  export { A as AppConfig, b as ConfigKey, C as ConfigSchema, g as ConfigType, h as Configs, D as DefaultRoles, P as Permission, f as Role, R as RoleDefinition, d as Session, U as User, e as UserInfo, a as WebsocketClientProvider, W as WebsocketServerProvider } from './types-Ds1ESQSs.js';
4
4
  import 'express';
5
5
  import 'mongodb';
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "type": "module",
3
3
  "name": "modelence",
4
- "version": "0.7.0",
4
+ "version": "0.7.2",
5
5
  "description": "The Node.js Framework for Real-Time MongoDB Apps",
6
6
  "main": "dist/index.js",
7
7
  "types": "dist/global.d.ts",
@@ -1,3 +0,0 @@
1
- import B from'elastic-apm-node';import v from'winston';import {ElasticsearchTransport}from'winston-elasticsearch';import c from'process';var m={},E={},w=false;function f(t){return E[t]?.value}function U(){if(!w)throw new Error("Config is not initialized: an attempt was made to access configs before they were loaded");return Object.fromEntries(Object.entries(m).filter(([t,e])=>e.isPublic).map(([t,e])=>[t,{key:t,type:e.type,value:E[t]?.value??e.default}]))}function G(t){t.forEach(({key:e,type:o,value:r})=>{!e.toLowerCase().startsWith("_system.")&&!m[e]||(E[e]={key:e,type:o,value:r});}),w=true;}function V(t){Object.entries(t).forEach(([e,o])=>{let{type:r,isPublic:n}=o;if(r==="secret"&&n)throw new Error(`Config ${e} with type "secret" cannot be public`)}),m=t;}var i={stdout:[{log:"",timestamp:null}],stderr:[{log:"",timestamp:null}]},z=1;function h({elasticCloudId:t,elasticApiKey:e}){let o=c.stdout.write,r=c.stderr.write;c.stdout.write=function(n,...s){return x(n.toString(),i.stdout),o.call(c.stdout,n,...s)},c.stderr.write=function(n,...s){return x(n.toString(),i.stderr),r.call(c.stderr,n,...s)},I();}function x(t,e){if(t.length===0)return;let o=new Date;for(let r=0;r<t.length;r++){let n=e[e.length-1];n.timestamp||(n.timestamp=o,n.sequenceId=z++),t[r]===`
2
- `?e.push({log:"",timestamp:null}):n.log+=t[r];}}async function O(){let t=i.stdout.slice(0,-1);i.stdout=[i.stdout[i.stdout.length-1]];let e=i.stderr.slice(0,-1);i.stderr=[i.stderr[i.stderr.length-1]],t.forEach(({log:o,timestamp:r,sequenceId:n})=>{y(o,{timestamp:r,source:"console",sequenceId:n});}),e.forEach(({log:o,timestamp:r,sequenceId:n})=>{L(o,{timestamp:r,source:"console",sequenceId:n});});}function I(){setTimeout(()=>{O(),I();},1e3);}var l=null;function Q(){}function X(t){l=Object.assign({},l,t);}function S(){return l?.environmentId}function T(){return l?.appAlias}function K(){return l?.environmentAlias}function M(){return l?.telemetry?.serviceName}function a(){return !!l?.telemetry?.isEnabled}var j=false,p=null,b=null,ot=async()=>{if(j)throw new Error('Metrics are already initialized, duplicate "initMetrics" call received');j=true,a()&&await P();};async function P(){let t=f("_system.elastic.apmEndpoint"),e=f("_system.elastic.cloudId"),o=f("_system.elastic.apiKey"),r=T()??"unknown",n=K()??"unknown",s=S()??"unknown",g=M();p=B.start({serviceName:g,apiKey:o,serverUrl:t,transactionSampleRate:1,centralConfig:false,globalLabels:{modelenceEnv:"dev",appEnv:"dev",environmentId:s,appAlias:r,environmentAlias:n}});let u=new ElasticsearchTransport({apm:p,level:"debug",clientOpts:{cloud:{id:e},auth:{apiKey:o},requestTimeout:1e4,tls:{rejectUnauthorized:false}},bufferLimit:1e3,silent:false});u.on("error",_=>{console.error("Elasticsearch Transport Error:",_);}),b=v.createLogger({level:"debug",defaultMeta:{serviceName:g},format:v.format.combine(v.format.json()),transports:[u]}),h({elasticCloudId:e,elasticApiKey:o});}function A(){if(!p)throw new Error("APM is not initialized");return p}function d(){if(!b)throw new Error("Logger is not initialized");return b}function C(){let t="";return a()||(t="info"),process.env.MODELENCE_LOG_LEVEL||t}function D(t,e){a()&&d().debug(t,e),C()==="debug"&&console.debug(t,e);}function y(t,e){a()&&d().info(t,e),["debug","info"].includes(C())&&console.info(t,e);}function L(t,e){a()&&d().error(t,e),["debug","info","error"].includes(C())&&console.error(t,e);}function W(t,e,o){if(!a())return {end:()=>{},setContext:()=>{}};let r=A(),n=r.startTransaction(e,t);return o&&r.setCustomContext(o),{end:(s,{endTime:g,context:u}={})=>{u&&r.setCustomContext(u),n.end(s,g);},setContext:s=>{r.setCustomContext(s);}}}function q(t){if(!a()){console.error(t);return}A().captureError(t);}export{f as a,U as b,G as c,V as d,Q as e,X as f,ot as g,D as h,y as i,L as j,W as k,q as l};//# sourceMappingURL=chunk-DVECB2TP.js.map
3
- //# sourceMappingURL=chunk-DVECB2TP.js.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/config/server.ts","../src/app/loggerProcess.ts","../src/app/state.ts","../src/app/metrics.ts","../src/telemetry/index.ts"],"names":["configSchema","config","isInitialized","getConfig","key","getPublicConfigs","_","schema","loadConfigs","configs","type","value","setSchema","isPublic","buffer","sequenceId","startLoggerProcess","_elasticCloudId","_elasticApiKey","originalStdoutWrite","process","originalStderrWrite","chunk","args","addToBuffer","loopSendLogs","timestamp","i","current","sendLogs","stdoutLogs","stderrLogs","log","logInfo","logError","metadata","markAppStarted","setMetadata","_metadata","getEnvironmentId","getAppAlias","getEnvironmentAlias","getTelemetryServiceName","isTelemetryEnabled","apm","logger","initMetrics","initElasticApm","elasticApmEndpoint","elasticCloudId","elasticApiKey","appAlias","environmentAlias","environmentId","serviceName","elasticApm","esTransport","ElasticsearchTransport","error","winston","getApm","getLogger","getLogLevel","defaultLoglevel","logDebug","message","startTransaction","name","context","transaction","result","endTime","captureError"],"mappings":"yIAEA,IAAIA,CAAAA,CAA6B,EAAC,CAC9BC,EAAuC,EAAC,CACxCC,CAAAA,CAAgB,KAAA,CAsCb,SAASC,CAAAA,CAAUC,CAAAA,CAAgB,CACxC,OAAOH,CAAAA,CAAOG,CAAG,CAAA,EAAG,KACtB,CAEO,SAASC,CAAAA,EAAmB,CACjC,GAAI,CAACH,CAAAA,CACH,MAAM,IAAI,KAAA,CACR,0FACF,CAAA,CAGF,OAAO,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A,UAAA,CAAY,IAAM,EAGpB,CAAA,CAGF,IAAMC,CAAAA,CAAMgB,CAAAA,EAAO,CACbS,CAAAA,CAAczB,CAAAA,CAAI,gBAAA,CAAiBuB,CAAAA,CAAMzD,CAAI,CAAA,CACnD,OAAI0D,CAAAA,EACFxB,CAAAA,CAAI,gBAAA,CAAiBwB,CAAO,CAAA,CAGvB,CACL,GAAA,CAAK,CACHE,CAAAA,CACA,CAAE,OAAA,CAAAC,CAAAA,CAAS,QAAAH,CAAQ,CAAA,CAA6D,EAAC,GAC9E,CACCA,CAAAA,EACFxB,CAAAA,CAAI,gBAAA,CAAiBwB,CAAO,CAAA,CAE9BC,CAAAA,CAAY,GAAA,CAAIC,CAAAA,CAAQC,CAAO,EACjC,CAAA,CACA,UAAA,CAAaH,GAAqC,CAChDxB,CAAAA,CAAI,gBAAA,CAAiBwB,CAAO,EAC9B,CACF,CACF,CAEO,SAASI,CAAAA,CAAad,CAAAA,CAAc,CACzC,GAAI,CAACf,CAAAA,EAAmB,CAAG,CACzB,QAAQ,KAAA,CAAMe,CAAK,CAAA,CACnB,MACF,CAEAE,CAAAA,EAAO,CAAE,YAAA,CAAaF,CAAK,EAC7B","file":"chunk-DVECB2TP.js","sourcesContent":["import { AppConfig, ConfigKey, ConfigSchema } from './types';\n\nlet configSchema: ConfigSchema = {};\nlet config: Record<ConfigKey, AppConfig> = {};\nlet isInitialized = false;\n\n/**\n * @sidebarTitle getConfig (server)\n *\n * @param key - The configuration key to retrieve\n * @returns The configuration value (string, number, or boolean)\n *\n * @example\n * ```ts\n * import { getConfig } from 'modelence/server';\n *\n * // Get the site URL\n * const siteUrl = getConfig('_system.site.url');\n * ```\n *\n * Set via environment variable:\n * ```bash\n * MODELENCE_SITE_URL=https://myapp.com\n * ```\n *\n * @example\n * ```ts\n * import { getConfig } from 'modelence/server';\n *\n * // Get the current environment (e.g., 'development', 'staging', 'production')\n * const env = getConfig('_system.env');\n *\n * if (env === 'production') {\n * // Enable production features\n * }\n * ```\n *\n * Set via environment variable:\n * ```bash\n * MODELENCE_SITE_ENV=production\n * ```\n */\nexport function getConfig(key: ConfigKey) {\n return config[key]?.value;\n}\n\nexport function getPublicConfigs() {\n if (!isInitialized) {\n throw new Error(\n 'Config is not initialized: an attempt was made to access configs before they were loaded'\n );\n }\n\n return Object.fromEntries(\n Object.entries(configSchema)\n .filter(([_, schema]) => schema.isPublic)\n .map(([key, schema]) => {\n return [\n key,\n {\n key,\n type: schema.type,\n value: config[key]?.value ?? schema.default,\n },\n ];\n })\n );\n}\n\nexport function loadConfigs(configs: AppConfig[]) {\n configs.forEach(({ key, type, value }) => {\n const isSystemConfig = key.toLowerCase().startsWith('_system.');\n\n if (!isSystemConfig && !configSchema[key]) {\n // Ignore unknown configs\n return;\n }\n\n config[key] = {\n key,\n type,\n value,\n };\n });\n\n isInitialized = true;\n}\n\nexport function setSchema(schema: ConfigSchema) {\n // TODO: more validation on the schema structure\n Object.entries(schema).forEach(([key, value]) => {\n const { type, isPublic } = value;\n\n if (type === 'secret' && isPublic) {\n throw new Error(`Config ${key} with type \"secret\" cannot be public`);\n }\n });\n\n configSchema = schema;\n}\n","// import { spawn } from 'child_process';\n// import { fileURLToPath } from 'url';\n// import { dirname, join } from 'path';\nimport { logInfo, logError } from '@/telemetry';\nimport process from 'process';\n\ntype LogEntry = { log: string; timestamp: Date | null; sequenceId?: number };\ntype LogBuffer = LogEntry[];\n\nconst buffer: { stdout: LogBuffer; stderr: LogBuffer } = {\n stdout: [{ log: '', timestamp: null }],\n stderr: [{ log: '', timestamp: null }],\n};\n\nlet sequenceId = 1;\n\nexport function startLoggerProcess({\n elasticCloudId: _elasticCloudId,\n elasticApiKey: _elasticApiKey,\n}: {\n elasticCloudId: string;\n elasticApiKey: string;\n}) {\n const originalStdoutWrite = process.stdout.write;\n const originalStderrWrite = process.stderr.write;\n\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n process.stdout.write = function (chunk: string | Uint8Array, ...args: any[]) {\n addToBuffer(chunk.toString(), buffer.stdout);\n return originalStdoutWrite.call(process.stdout, chunk, ...args);\n };\n\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n process.stderr.write = function (chunk: string | Uint8Array, ...args: any[]) {\n addToBuffer(chunk.toString(), buffer.stderr);\n return originalStderrWrite.call(process.stderr, chunk, ...args);\n };\n\n loopSendLogs();\n\n // const currentFilePath = fileURLToPath(import.meta.url);\n // const projectRoot = dirname(dirname(currentFilePath));\n // const loggerPath = join(projectRoot, 'bin', 'modelence-logger', 'index.js');\n // const logger = spawn(process.execPath, [loggerPath], {\n // env: {\n // NODE_ENV: process.env.NODE_ENV,\n // ELASTIC_CLOUD_ID: elasticCloudId,\n // ELASTIC_API_KEY: elasticApiKey\n // },\n // stdio: ['pipe', 'inherit', 'inherit'],\n // detached: true\n // });\n\n // const originalStdoutWrite = process.stdout.write;\n // const originalStderrWrite = process.stderr.write;\n\n // process.stdout.write = function(chunk: any, ...args: any[]) {\n // logger.stdin.write(chunk);\n // return originalStdoutWrite.apply(process.stdout, [chunk, ...args]);\n // };\n\n // process.stderr.write = function(chunk: any, ...args: any[]) {\n // logger.stdin.write(chunk);\n // return originalStderrWrite.apply(process.stderr, [chunk, ...args]);\n // };\n\n // process.on('exit', () => {\n // process.stdout.write = originalStdoutWrite;\n // process.stderr.write = originalStderrWrite;\n // });\n\n // logger.unref();\n}\n\nfunction addToBuffer(chunk: string, buffer: LogBuffer) {\n if (chunk.length === 0) {\n return;\n }\n\n const timestamp = new Date();\n\n for (let i = 0; i < chunk.length; i++) {\n const current = buffer[buffer.length - 1];\n if (!current.timestamp) {\n current.timestamp = timestamp;\n current.sequenceId = sequenceId++;\n }\n\n if (chunk[i] === '\\n') {\n buffer.push({ log: '', timestamp: null });\n } else {\n current.log += chunk[i];\n }\n }\n}\n\nasync function sendLogs() {\n const stdoutLogs = buffer.stdout.slice(0, -1);\n buffer.stdout = [buffer.stdout[buffer.stdout.length - 1]];\n\n const stderrLogs = buffer.stderr.slice(0, -1);\n buffer.stderr = [buffer.stderr[buffer.stderr.length - 1]];\n\n stdoutLogs.forEach(({ log, timestamp, sequenceId }: LogEntry) => {\n logInfo(log, { timestamp, source: 'console', sequenceId });\n });\n stderrLogs.forEach(({ log, timestamp, sequenceId }: LogEntry) => {\n logError(log, { timestamp, source: 'console', sequenceId });\n });\n}\n\nfunction loopSendLogs() {\n setTimeout(() => {\n sendLogs();\n loopSendLogs();\n }, 1000);\n}\n","type AppMetadata = {\n environmentId: string;\n appAlias: string;\n environmentAlias: string;\n telemetry: {\n isEnabled: boolean;\n serviceName: string;\n };\n};\n\nlet appStarted = false;\nlet metadata: AppMetadata | null = null;\n\nexport function markAppStarted() {\n appStarted = true;\n}\n\nexport function isAppStarted() {\n return appStarted;\n}\n\nexport function setMetadata(_metadata: AppMetadata) {\n metadata = Object.assign({}, metadata, _metadata);\n}\n\nexport function getEnvironmentId() {\n return metadata?.environmentId;\n}\n\nexport function getAppAlias() {\n return metadata?.appAlias;\n}\n\nexport function getEnvironmentAlias() {\n return metadata?.environmentAlias;\n}\n\nexport function getTelemetryServiceName() {\n return metadata?.telemetry?.serviceName;\n}\n\nexport function isTelemetryEnabled() {\n return Boolean(metadata?.telemetry?.isEnabled);\n}\n","import elasticApm from 'elastic-apm-node';\nimport winston from 'winston';\nimport { ElasticsearchTransport } from 'winston-elasticsearch';\n\nimport { getConfig } from '../config/server';\nimport { startLoggerProcess } from './loggerProcess';\nimport {\n getAppAlias,\n getEnvironmentAlias,\n getEnvironmentId,\n getTelemetryServiceName,\n isTelemetryEnabled,\n} from './state';\n\nlet isInitialized = false;\nlet apm: typeof elasticApm | null = null;\nlet logger: winston.Logger | null = null;\n\nexport const initMetrics = async () => {\n if (isInitialized) {\n throw new Error('Metrics are already initialized, duplicate \"initMetrics\" call received');\n }\n\n isInitialized = true;\n\n if (isTelemetryEnabled()) {\n await initElasticApm();\n }\n};\n\nasync function initElasticApm() {\n const elasticApmEndpoint = getConfig('_system.elastic.apmEndpoint') as string;\n const elasticCloudId = getConfig('_system.elastic.cloudId') as string;\n const elasticApiKey = getConfig('_system.elastic.apiKey') as string;\n\n const appAlias = getAppAlias() ?? 'unknown';\n const environmentAlias = getEnvironmentAlias() ?? 'unknown';\n const environmentId = getEnvironmentId() ?? 'unknown';\n const serviceName = getTelemetryServiceName();\n\n apm = elasticApm.start({\n serviceName,\n apiKey: elasticApiKey,\n serverUrl: elasticApmEndpoint,\n // environment: 'dev',\n transactionSampleRate: 1.0,\n centralConfig: false,\n globalLabels: {\n modelenceEnv: 'dev',\n appEnv: 'dev',\n environmentId,\n appAlias,\n environmentAlias,\n },\n // logLevel: 'debug'\n });\n\n const esTransport = new ElasticsearchTransport({\n apm,\n level: 'debug',\n clientOpts: {\n cloud: {\n id: elasticCloudId,\n },\n auth: {\n apiKey: elasticApiKey,\n },\n requestTimeout: 10000,\n tls: {\n rejectUnauthorized: false,\n },\n },\n bufferLimit: 1000,\n silent: false,\n });\n\n esTransport.on('error', (error) => {\n console.error('Elasticsearch Transport Error:', error);\n });\n\n logger = winston.createLogger({\n level: 'debug',\n defaultMeta: {\n serviceName,\n },\n format: winston.format.combine(winston.format.json()),\n transports: [\n // new winston.transports.Console(), // TODO: remove, just for debugging\n esTransport,\n ],\n });\n\n startLoggerProcess({\n elasticCloudId,\n elasticApiKey,\n });\n}\n\nexport function getApm() {\n if (!apm) {\n throw new Error('APM is not initialized');\n }\n return apm;\n}\n\nexport function getLogger() {\n if (!logger) {\n throw new Error('Logger is not initialized');\n }\n return logger;\n}\n","import { getLogger, getApm } from '@/app/metrics';\nimport { isTelemetryEnabled } from '@/app/state';\n\ntype LogLevel = 'error' | 'info' | 'debug' | '';\n\n/**\n * Gets the logging level for console logs based on the MODELENCE_LOG_LEVEL environment variable.\n *\n * @returns The log level ('error' | 'info' | 'debug' | '')\n *\n * Behavior:\n * - If MODELENCE_LOG_LEVEL is set, returns that value\n * - If telemetry is disabled and MODELENCE_LOG_LEVEL is not set, defaults to 'info'\n * - If telemetry is enabled and MODELENCE_LOG_LEVEL is not set, returns '' (no console logging)\n */\nfunction getLogLevel(): LogLevel {\n let defaultLoglevel: LogLevel = '';\n if (!isTelemetryEnabled()) {\n defaultLoglevel = 'info';\n }\n\n return (process.env.MODELENCE_LOG_LEVEL as LogLevel) || defaultLoglevel;\n}\n\nexport function logDebug(message: string, args: object) {\n if (isTelemetryEnabled()) {\n getLogger().debug(message, args);\n }\n if (getLogLevel() === 'debug') {\n console.debug(message, args);\n }\n}\n\nexport function logInfo(message: string, args: object) {\n if (isTelemetryEnabled()) {\n getLogger().info(message, args);\n }\n if (['debug', 'info'].includes(getLogLevel())) {\n console.info(message, args);\n }\n}\n\nexport function logError(message: string, args: object) {\n if (isTelemetryEnabled()) {\n getLogger().error(message, args);\n }\n if (['debug', 'info', 'error'].includes(getLogLevel())) {\n console.error(message, args);\n }\n}\n\ninterface WrappedTransaction {\n end(result?: string, context?: Record<string, unknown>): void;\n setContext(context: Record<string, unknown>): void;\n}\n\nexport function startTransaction(\n type: 'method' | 'cron' | 'ai' | 'custom' | 'route',\n name: string,\n context?: Record<string, unknown>\n): WrappedTransaction {\n if (!isTelemetryEnabled()) {\n return {\n end: () => {\n // do nothing\n },\n setContext: () => {\n // do nothing\n },\n };\n }\n\n const apm = getApm();\n const transaction = apm.startTransaction(name, type);\n if (context) {\n apm.setCustomContext(context);\n }\n\n return {\n end: (\n result?: string,\n { endTime, context }: { endTime?: number; context?: Record<string, unknown> } = {}\n ) => {\n if (context) {\n apm.setCustomContext(context);\n }\n transaction.end(result, endTime);\n },\n setContext: (context: Record<string, unknown>) => {\n apm.setCustomContext(context);\n },\n };\n}\n\nexport function captureError(error: Error) {\n if (!isTelemetryEnabled()) {\n console.error(error);\n return;\n }\n\n getApm().captureError(error);\n}\n"]}