@juspay/neurolink 9.1.1 → 9.2.0

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.
Files changed (237) hide show
  1. package/CHANGELOG.md +6 -0
  2. package/README.md +54 -7
  3. package/dist/agent/directTools.d.ts +3 -3
  4. package/dist/cli/commands/config.d.ts +6 -6
  5. package/dist/image-gen/ImageGenService.d.ts +143 -0
  6. package/dist/image-gen/ImageGenService.js +345 -0
  7. package/dist/image-gen/imageGenTools.d.ts +126 -0
  8. package/dist/image-gen/imageGenTools.js +304 -0
  9. package/dist/image-gen/index.d.ts +46 -0
  10. package/dist/image-gen/index.js +48 -0
  11. package/dist/image-gen/types.d.ts +237 -0
  12. package/dist/image-gen/types.js +24 -0
  13. package/dist/lib/agent/directTools.d.ts +3 -3
  14. package/dist/lib/image-gen/ImageGenService.d.ts +143 -0
  15. package/dist/lib/image-gen/ImageGenService.js +346 -0
  16. package/dist/lib/image-gen/imageGenTools.d.ts +126 -0
  17. package/dist/lib/image-gen/imageGenTools.js +305 -0
  18. package/dist/lib/image-gen/index.d.ts +46 -0
  19. package/dist/lib/image-gen/index.js +49 -0
  20. package/dist/lib/image-gen/types.d.ts +237 -0
  21. package/dist/lib/image-gen/types.js +25 -0
  22. package/dist/lib/processors/base/BaseFileProcessor.d.ts +273 -0
  23. package/dist/lib/processors/base/BaseFileProcessor.js +614 -0
  24. package/dist/lib/processors/base/index.d.ts +14 -0
  25. package/dist/lib/processors/base/index.js +20 -0
  26. package/dist/lib/processors/base/types.d.ts +593 -0
  27. package/dist/lib/processors/base/types.js +77 -0
  28. package/dist/lib/processors/cli/fileProcessorCli.d.ts +163 -0
  29. package/dist/lib/processors/cli/fileProcessorCli.js +389 -0
  30. package/dist/lib/processors/cli/index.d.ts +37 -0
  31. package/dist/lib/processors/cli/index.js +50 -0
  32. package/dist/lib/processors/code/ConfigProcessor.d.ts +171 -0
  33. package/dist/lib/processors/code/ConfigProcessor.js +401 -0
  34. package/dist/lib/processors/code/SourceCodeProcessor.d.ts +174 -0
  35. package/dist/lib/processors/code/SourceCodeProcessor.js +305 -0
  36. package/dist/lib/processors/code/index.d.ts +44 -0
  37. package/dist/lib/processors/code/index.js +61 -0
  38. package/dist/lib/processors/config/fileTypes.d.ts +283 -0
  39. package/dist/lib/processors/config/fileTypes.js +521 -0
  40. package/dist/lib/processors/config/index.d.ts +32 -0
  41. package/dist/lib/processors/config/index.js +93 -0
  42. package/dist/lib/processors/config/languageMap.d.ts +66 -0
  43. package/dist/lib/processors/config/languageMap.js +411 -0
  44. package/dist/lib/processors/config/mimeTypes.d.ts +376 -0
  45. package/dist/lib/processors/config/mimeTypes.js +339 -0
  46. package/dist/lib/processors/config/sizeLimits.d.ts +194 -0
  47. package/dist/lib/processors/config/sizeLimits.js +247 -0
  48. package/dist/lib/processors/data/JsonProcessor.d.ts +122 -0
  49. package/dist/lib/processors/data/JsonProcessor.js +204 -0
  50. package/dist/lib/processors/data/XmlProcessor.d.ts +160 -0
  51. package/dist/lib/processors/data/XmlProcessor.js +284 -0
  52. package/dist/lib/processors/data/YamlProcessor.d.ts +163 -0
  53. package/dist/lib/processors/data/YamlProcessor.js +295 -0
  54. package/dist/lib/processors/data/index.d.ts +49 -0
  55. package/dist/lib/processors/data/index.js +77 -0
  56. package/dist/lib/processors/document/ExcelProcessor.d.ts +238 -0
  57. package/dist/lib/processors/document/ExcelProcessor.js +520 -0
  58. package/dist/lib/processors/document/OpenDocumentProcessor.d.ts +69 -0
  59. package/dist/lib/processors/document/OpenDocumentProcessor.js +211 -0
  60. package/dist/lib/processors/document/RtfProcessor.d.ts +152 -0
  61. package/dist/lib/processors/document/RtfProcessor.js +362 -0
  62. package/dist/lib/processors/document/WordProcessor.d.ts +168 -0
  63. package/dist/lib/processors/document/WordProcessor.js +354 -0
  64. package/dist/lib/processors/document/index.d.ts +54 -0
  65. package/dist/lib/processors/document/index.js +91 -0
  66. package/dist/lib/processors/errors/FileErrorCode.d.ts +98 -0
  67. package/dist/lib/processors/errors/FileErrorCode.js +256 -0
  68. package/dist/lib/processors/errors/errorHelpers.d.ts +151 -0
  69. package/dist/lib/processors/errors/errorHelpers.js +379 -0
  70. package/dist/lib/processors/errors/errorSerializer.d.ts +139 -0
  71. package/dist/lib/processors/errors/errorSerializer.js +508 -0
  72. package/dist/lib/processors/errors/index.d.ts +46 -0
  73. package/dist/lib/processors/errors/index.js +50 -0
  74. package/dist/lib/processors/index.d.ts +76 -0
  75. package/dist/lib/processors/index.js +113 -0
  76. package/dist/lib/processors/integration/FileProcessorIntegration.d.ts +244 -0
  77. package/dist/lib/processors/integration/FileProcessorIntegration.js +273 -0
  78. package/dist/lib/processors/integration/index.d.ts +42 -0
  79. package/dist/lib/processors/integration/index.js +45 -0
  80. package/dist/lib/processors/markup/HtmlProcessor.d.ts +169 -0
  81. package/dist/lib/processors/markup/HtmlProcessor.js +250 -0
  82. package/dist/lib/processors/markup/MarkdownProcessor.d.ts +165 -0
  83. package/dist/lib/processors/markup/MarkdownProcessor.js +245 -0
  84. package/dist/lib/processors/markup/SvgProcessor.d.ts +156 -0
  85. package/dist/lib/processors/markup/SvgProcessor.js +241 -0
  86. package/dist/lib/processors/markup/TextProcessor.d.ts +135 -0
  87. package/dist/lib/processors/markup/TextProcessor.js +189 -0
  88. package/dist/lib/processors/markup/index.d.ts +66 -0
  89. package/dist/lib/processors/markup/index.js +103 -0
  90. package/dist/lib/processors/registry/ProcessorRegistry.d.ts +334 -0
  91. package/dist/lib/processors/registry/ProcessorRegistry.js +609 -0
  92. package/dist/lib/processors/registry/index.d.ts +12 -0
  93. package/dist/lib/processors/registry/index.js +17 -0
  94. package/dist/lib/processors/registry/types.d.ts +53 -0
  95. package/dist/lib/processors/registry/types.js +11 -0
  96. package/dist/lib/providers/sagemaker/language-model.d.ts +2 -2
  97. package/dist/lib/server/utils/validation.d.ts +6 -6
  98. package/dist/lib/types/fileTypes.d.ts +1 -1
  99. package/dist/lib/types/index.d.ts +25 -24
  100. package/dist/lib/types/index.js +21 -20
  101. package/dist/lib/types/modelTypes.d.ts +18 -18
  102. package/dist/lib/types/pptTypes.d.ts +14 -2
  103. package/dist/lib/types/pptTypes.js +16 -0
  104. package/dist/lib/utils/async/delay.d.ts +40 -0
  105. package/dist/lib/utils/async/delay.js +43 -0
  106. package/dist/lib/utils/async/index.d.ts +23 -0
  107. package/dist/lib/utils/async/index.js +24 -0
  108. package/dist/lib/utils/async/retry.d.ts +141 -0
  109. package/dist/lib/utils/async/retry.js +172 -0
  110. package/dist/lib/utils/async/withTimeout.d.ts +73 -0
  111. package/dist/lib/utils/async/withTimeout.js +97 -0
  112. package/dist/lib/utils/fileDetector.d.ts +7 -1
  113. package/dist/lib/utils/fileDetector.js +91 -18
  114. package/dist/lib/utils/json/extract.d.ts +103 -0
  115. package/dist/lib/utils/json/extract.js +249 -0
  116. package/dist/lib/utils/json/index.d.ts +36 -0
  117. package/dist/lib/utils/json/index.js +37 -0
  118. package/dist/lib/utils/json/safeParse.d.ts +137 -0
  119. package/dist/lib/utils/json/safeParse.js +191 -0
  120. package/dist/lib/utils/messageBuilder.d.ts +2 -2
  121. package/dist/lib/utils/messageBuilder.js +15 -7
  122. package/dist/lib/utils/sanitizers/filename.d.ts +137 -0
  123. package/dist/lib/utils/sanitizers/filename.js +366 -0
  124. package/dist/lib/utils/sanitizers/html.d.ts +170 -0
  125. package/dist/lib/utils/sanitizers/html.js +326 -0
  126. package/dist/lib/utils/sanitizers/index.d.ts +26 -0
  127. package/dist/lib/utils/sanitizers/index.js +30 -0
  128. package/dist/lib/utils/sanitizers/svg.d.ts +81 -0
  129. package/dist/lib/utils/sanitizers/svg.js +483 -0
  130. package/dist/processors/base/BaseFileProcessor.d.ts +273 -0
  131. package/dist/processors/base/BaseFileProcessor.js +613 -0
  132. package/dist/processors/base/index.d.ts +14 -0
  133. package/dist/processors/base/index.js +19 -0
  134. package/dist/processors/base/types.d.ts +593 -0
  135. package/dist/processors/base/types.js +76 -0
  136. package/dist/processors/cli/fileProcessorCli.d.ts +163 -0
  137. package/dist/processors/cli/fileProcessorCli.js +388 -0
  138. package/dist/processors/cli/index.d.ts +37 -0
  139. package/dist/processors/cli/index.js +49 -0
  140. package/dist/processors/code/ConfigProcessor.d.ts +171 -0
  141. package/dist/processors/code/ConfigProcessor.js +400 -0
  142. package/dist/processors/code/SourceCodeProcessor.d.ts +174 -0
  143. package/dist/processors/code/SourceCodeProcessor.js +304 -0
  144. package/dist/processors/code/index.d.ts +44 -0
  145. package/dist/processors/code/index.js +60 -0
  146. package/dist/processors/config/fileTypes.d.ts +283 -0
  147. package/dist/processors/config/fileTypes.js +520 -0
  148. package/dist/processors/config/index.d.ts +32 -0
  149. package/dist/processors/config/index.js +92 -0
  150. package/dist/processors/config/languageMap.d.ts +66 -0
  151. package/dist/processors/config/languageMap.js +410 -0
  152. package/dist/processors/config/mimeTypes.d.ts +376 -0
  153. package/dist/processors/config/mimeTypes.js +338 -0
  154. package/dist/processors/config/sizeLimits.d.ts +194 -0
  155. package/dist/processors/config/sizeLimits.js +246 -0
  156. package/dist/processors/data/JsonProcessor.d.ts +122 -0
  157. package/dist/processors/data/JsonProcessor.js +203 -0
  158. package/dist/processors/data/XmlProcessor.d.ts +160 -0
  159. package/dist/processors/data/XmlProcessor.js +283 -0
  160. package/dist/processors/data/YamlProcessor.d.ts +163 -0
  161. package/dist/processors/data/YamlProcessor.js +294 -0
  162. package/dist/processors/data/index.d.ts +49 -0
  163. package/dist/processors/data/index.js +76 -0
  164. package/dist/processors/document/ExcelProcessor.d.ts +238 -0
  165. package/dist/processors/document/ExcelProcessor.js +519 -0
  166. package/dist/processors/document/OpenDocumentProcessor.d.ts +69 -0
  167. package/dist/processors/document/OpenDocumentProcessor.js +210 -0
  168. package/dist/processors/document/RtfProcessor.d.ts +152 -0
  169. package/dist/processors/document/RtfProcessor.js +361 -0
  170. package/dist/processors/document/WordProcessor.d.ts +168 -0
  171. package/dist/processors/document/WordProcessor.js +353 -0
  172. package/dist/processors/document/index.d.ts +54 -0
  173. package/dist/processors/document/index.js +90 -0
  174. package/dist/processors/errors/FileErrorCode.d.ts +98 -0
  175. package/dist/processors/errors/FileErrorCode.js +255 -0
  176. package/dist/processors/errors/errorHelpers.d.ts +151 -0
  177. package/dist/processors/errors/errorHelpers.js +378 -0
  178. package/dist/processors/errors/errorSerializer.d.ts +139 -0
  179. package/dist/processors/errors/errorSerializer.js +507 -0
  180. package/dist/processors/errors/index.d.ts +46 -0
  181. package/dist/processors/errors/index.js +49 -0
  182. package/dist/processors/index.d.ts +76 -0
  183. package/dist/processors/index.js +112 -0
  184. package/dist/processors/integration/FileProcessorIntegration.d.ts +244 -0
  185. package/dist/processors/integration/FileProcessorIntegration.js +272 -0
  186. package/dist/processors/integration/index.d.ts +42 -0
  187. package/dist/processors/integration/index.js +44 -0
  188. package/dist/processors/markup/HtmlProcessor.d.ts +169 -0
  189. package/dist/processors/markup/HtmlProcessor.js +249 -0
  190. package/dist/processors/markup/MarkdownProcessor.d.ts +165 -0
  191. package/dist/processors/markup/MarkdownProcessor.js +244 -0
  192. package/dist/processors/markup/SvgProcessor.d.ts +156 -0
  193. package/dist/processors/markup/SvgProcessor.js +240 -0
  194. package/dist/processors/markup/TextProcessor.d.ts +135 -0
  195. package/dist/processors/markup/TextProcessor.js +188 -0
  196. package/dist/processors/markup/index.d.ts +66 -0
  197. package/dist/processors/markup/index.js +102 -0
  198. package/dist/processors/registry/ProcessorRegistry.d.ts +334 -0
  199. package/dist/processors/registry/ProcessorRegistry.js +608 -0
  200. package/dist/processors/registry/index.d.ts +12 -0
  201. package/dist/processors/registry/index.js +16 -0
  202. package/dist/processors/registry/types.d.ts +53 -0
  203. package/dist/processors/registry/types.js +10 -0
  204. package/dist/server/utils/validation.d.ts +6 -6
  205. package/dist/types/fileTypes.d.ts +1 -1
  206. package/dist/types/index.d.ts +25 -24
  207. package/dist/types/index.js +21 -20
  208. package/dist/types/modelTypes.d.ts +10 -10
  209. package/dist/types/pptTypes.d.ts +14 -2
  210. package/dist/types/pptTypes.js +16 -0
  211. package/dist/utils/async/delay.d.ts +40 -0
  212. package/dist/utils/async/delay.js +42 -0
  213. package/dist/utils/async/index.d.ts +23 -0
  214. package/dist/utils/async/index.js +23 -0
  215. package/dist/utils/async/retry.d.ts +141 -0
  216. package/dist/utils/async/retry.js +171 -0
  217. package/dist/utils/async/withTimeout.d.ts +73 -0
  218. package/dist/utils/async/withTimeout.js +96 -0
  219. package/dist/utils/fileDetector.d.ts +7 -1
  220. package/dist/utils/fileDetector.js +91 -18
  221. package/dist/utils/json/extract.d.ts +103 -0
  222. package/dist/utils/json/extract.js +248 -0
  223. package/dist/utils/json/index.d.ts +36 -0
  224. package/dist/utils/json/index.js +36 -0
  225. package/dist/utils/json/safeParse.d.ts +137 -0
  226. package/dist/utils/json/safeParse.js +190 -0
  227. package/dist/utils/messageBuilder.d.ts +2 -2
  228. package/dist/utils/messageBuilder.js +15 -7
  229. package/dist/utils/sanitizers/filename.d.ts +137 -0
  230. package/dist/utils/sanitizers/filename.js +365 -0
  231. package/dist/utils/sanitizers/html.d.ts +170 -0
  232. package/dist/utils/sanitizers/html.js +325 -0
  233. package/dist/utils/sanitizers/index.d.ts +26 -0
  234. package/dist/utils/sanitizers/index.js +29 -0
  235. package/dist/utils/sanitizers/svg.d.ts +81 -0
  236. package/dist/utils/sanitizers/svg.js +482 -0
  237. package/package.json +2 -2
@@ -0,0 +1,507 @@
1
+ /**
2
+ * Error Serializer Utility
3
+ *
4
+ * Safe error serialization with full context preservation.
5
+ * Features:
6
+ * - Handles circular references
7
+ * - Filters sensitive data (PII, credentials)
8
+ * - Generates error fingerprints for deduplication
9
+ * - Preserves custom error properties
10
+ * - Filters stack traces in production
11
+ *
12
+ * @module processors/errors
13
+ */
14
+ import { createHash } from "crypto";
15
+ /**
16
+ * Fields that should be redacted for security/privacy.
17
+ */
18
+ const SENSITIVE_FIELDS = [
19
+ "password",
20
+ "token",
21
+ "secret",
22
+ "apiKey",
23
+ "api_key",
24
+ "authorization",
25
+ "cookie",
26
+ "session",
27
+ "credentials",
28
+ "privateKey",
29
+ "private_key",
30
+ "accessToken",
31
+ "access_token",
32
+ "refreshToken",
33
+ "refresh_token",
34
+ "apiSecret",
35
+ "api_secret",
36
+ "clientSecret",
37
+ "client_secret",
38
+ "bearer",
39
+ "auth",
40
+ "ssn",
41
+ "socialSecurity",
42
+ "creditCard",
43
+ "credit_card",
44
+ "cvv",
45
+ "pin",
46
+ "jwt",
47
+ "x-api-key",
48
+ "passphrase",
49
+ "connectionString",
50
+ "connection_string",
51
+ ];
52
+ /**
53
+ * Maximum size limits for serialization.
54
+ */
55
+ const MAX_METADATA_SIZE = 2000;
56
+ const MAX_STACK_FRAMES = 20;
57
+ const MAX_DEPTH = 5;
58
+ /**
59
+ * Safely serialize an error with full context preservation.
60
+ * Handles circular references, redacts sensitive data, and preserves error metadata.
61
+ *
62
+ * @param error - Error instance or unknown value to serialize
63
+ * @param options - Serialization options
64
+ * @returns Serialized error with full context
65
+ *
66
+ * @example
67
+ * ```typescript
68
+ * try {
69
+ * await riskyOperation();
70
+ * } catch (error) {
71
+ * const serialized = serializeError(error);
72
+ * logger.error("Operation failed", serialized);
73
+ * }
74
+ * ```
75
+ */
76
+ export function serializeError(error, options) {
77
+ const { includeStack = true, maxDepth = MAX_DEPTH, filterStacks = process.env.NODE_ENV === "production", } = options || {};
78
+ const timestamp = new Date().toISOString();
79
+ // Handle non-Error objects
80
+ if (!(error instanceof Error)) {
81
+ return {
82
+ errorId: generateErrorId(),
83
+ errorFingerprint: generateFingerprintFromString(String(error)),
84
+ errorType: "UnknownError",
85
+ message: String(error),
86
+ metadata: { originalValue: safeStringify(error, maxDepth) },
87
+ timestamp,
88
+ };
89
+ }
90
+ // Extract basic error info
91
+ const serialized = {
92
+ errorId: generateErrorId(),
93
+ errorType: error.name,
94
+ message: error.message,
95
+ errorFingerprint: generateErrorFingerprint(error),
96
+ timestamp,
97
+ };
98
+ // Add stack trace
99
+ if (includeStack && error.stack) {
100
+ const frames = error.stack.split("\n");
101
+ if (filterStacks) {
102
+ serialized.stackFrames = filterStackFrames(frames, MAX_STACK_FRAMES);
103
+ serialized.stack = serialized.stackFrames.join("\n");
104
+ }
105
+ else {
106
+ serialized.stack = error.stack;
107
+ serialized.stackFrames = frames.slice(0, MAX_STACK_FRAMES);
108
+ }
109
+ }
110
+ // Add custom properties from error objects
111
+ if ("statusCode" in error) {
112
+ serialized.statusCode = error.statusCode;
113
+ }
114
+ if ("isOperational" in error) {
115
+ serialized.isOperational = error.isOperational;
116
+ }
117
+ if ("isRetryable" in error) {
118
+ serialized.isRetryable = error.isRetryable;
119
+ }
120
+ if ("retryable" in error) {
121
+ serialized.isRetryable = error.retryable;
122
+ }
123
+ if ("retriable" in error) {
124
+ serialized.isRetryable = error.retriable;
125
+ }
126
+ if ("code" in error) {
127
+ serialized.code = String(error.code);
128
+ }
129
+ // Extract additional metadata
130
+ const metadata = {};
131
+ const errorKeys = Object.keys(error);
132
+ const excludedKeys = [
133
+ "name",
134
+ "message",
135
+ "stack",
136
+ "statusCode",
137
+ "isOperational",
138
+ "isRetryable",
139
+ "retryable",
140
+ "retriable",
141
+ "code",
142
+ "cause",
143
+ ];
144
+ for (const key of errorKeys) {
145
+ if (!excludedKeys.includes(key)) {
146
+ const value = error[key];
147
+ metadata[key] = sanitizeAndTruncate(key, value, MAX_METADATA_SIZE, maxDepth);
148
+ }
149
+ }
150
+ // Add context if provided
151
+ if (options?.context) {
152
+ for (const [key, value] of Object.entries(options.context)) {
153
+ metadata[`context.${key}`] = sanitizeAndTruncate(key, value, MAX_METADATA_SIZE, maxDepth);
154
+ }
155
+ }
156
+ if (Object.keys(metadata).length > 0) {
157
+ serialized.metadata = metadata;
158
+ }
159
+ // Handle error chaining (cause) - use type assertion for ES2022 cause property
160
+ const errorWithCause = error;
161
+ if (errorWithCause.cause instanceof Error) {
162
+ serialized.cause = serializeError(errorWithCause.cause, {
163
+ ...options,
164
+ maxDepth: Math.max(1, maxDepth - 1),
165
+ });
166
+ }
167
+ return serialized;
168
+ }
169
+ /**
170
+ * Generate a unique error instance ID.
171
+ *
172
+ * @returns Unique error ID in format "err_<timestamp>_<random>"
173
+ */
174
+ function generateErrorId() {
175
+ const timestamp = Date.now().toString(36);
176
+ const random = Math.random().toString(36).substring(2, 11);
177
+ return `err_${timestamp}_${random}`;
178
+ }
179
+ /**
180
+ * Generate a deterministic fingerprint for error aggregation.
181
+ * Normalizes dynamic values (IDs, timestamps, paths) to group similar errors together.
182
+ *
183
+ * @param error - Error to fingerprint
184
+ * @param context - Optional context with operation name
185
+ * @returns 16-character hex fingerprint hash
186
+ *
187
+ * @example
188
+ * ```typescript
189
+ * const fp1 = generateErrorFingerprint(new Error("User 123 not found"));
190
+ * const fp2 = generateErrorFingerprint(new Error("User 456 not found"));
191
+ * // fp1 === fp2 (same error pattern, different IDs)
192
+ * ```
193
+ */
194
+ export function generateErrorFingerprint(error, context) {
195
+ // Normalize message by replacing dynamic values
196
+ const normalizedMessage = normalizeErrorMessage(error.message);
197
+ // Get first relevant stack frame (most relevant location)
198
+ const firstFrame = extractFirstRelevantFrame(error.stack);
199
+ const components = [
200
+ error.name,
201
+ normalizedMessage,
202
+ firstFrame,
203
+ context?.operation || "",
204
+ ];
205
+ return createHash("sha256")
206
+ .update(components.join("|"))
207
+ .digest("hex")
208
+ .substring(0, 16);
209
+ }
210
+ /**
211
+ * Generate a fingerprint from a plain string (for non-Error values).
212
+ *
213
+ * @param value - String value to fingerprint
214
+ * @returns 16-character hex fingerprint hash
215
+ */
216
+ function generateFingerprintFromString(value) {
217
+ const normalized = normalizeErrorMessage(value);
218
+ return createHash("sha256").update(normalized).digest("hex").substring(0, 16);
219
+ }
220
+ /**
221
+ * Normalize an error message by replacing dynamic values with placeholders.
222
+ * This allows grouping similar errors together regardless of specific IDs, paths, etc.
223
+ *
224
+ * @param message - Original error message
225
+ * @returns Normalized message with placeholders
226
+ */
227
+ function normalizeErrorMessage(message) {
228
+ return (message
229
+ // Numbers (IDs, counts, etc.)
230
+ .replace(/\d+/g, "N")
231
+ // UUIDs (v4)
232
+ .replace(/[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}/gi, "UUID")
233
+ // MongoDB ObjectIDs (24 hex chars)
234
+ .replace(/[a-f0-9]{24}/gi, "OBJID")
235
+ // File paths
236
+ .replace(/\/[\w/.~-]+/g, "PATH")
237
+ // Windows paths
238
+ .replace(/[A-Z]:\\[\w\\.~-]+/gi, "PATH")
239
+ // URLs
240
+ .replace(/https?:\/\/[^\s]+/g, "URL")
241
+ // Single-quoted strings
242
+ .replace(/'[^']*'/g, "STR")
243
+ // Double-quoted strings
244
+ .replace(/"[^"]*"/g, "STR")
245
+ // Timestamps (ISO format)
246
+ .replace(/\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/g, "TIMESTAMP")
247
+ // Email addresses
248
+ .replace(/[\w.-]+@[\w.-]+\.\w+/g, "EMAIL")
249
+ // IP addresses
250
+ .replace(/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/g, "IP"));
251
+ }
252
+ /**
253
+ * Extract the first relevant stack frame (application code, not node_modules).
254
+ *
255
+ * @param stack - Full stack trace
256
+ * @returns First relevant frame or empty string
257
+ */
258
+ function extractFirstRelevantFrame(stack) {
259
+ if (!stack) {
260
+ return "";
261
+ }
262
+ const frames = stack.split("\n");
263
+ for (const frame of frames) {
264
+ const trimmed = frame.trim();
265
+ if (trimmed.startsWith("at ") &&
266
+ !trimmed.includes("node_modules") &&
267
+ !trimmed.includes("node:internal")) {
268
+ return trimmed;
269
+ }
270
+ }
271
+ // Fall back to first "at" frame
272
+ const firstAtFrame = frames.find((f) => f.trim().startsWith("at "));
273
+ return firstAtFrame?.trim() || "";
274
+ }
275
+ /**
276
+ * Filter stack frames to application code only.
277
+ * Removes node_modules and internal Node.js frames.
278
+ *
279
+ * @param frames - Array of stack frame strings
280
+ * @param maxFrames - Maximum number of frames to include
281
+ * @returns Filtered stack frames
282
+ */
283
+ function filterStackFrames(frames, maxFrames) {
284
+ const filtered = frames.filter((frame) => {
285
+ // Keep error message line (first line)
286
+ if (!frame.trim().startsWith("at ")) {
287
+ return true;
288
+ }
289
+ // Filter out node internals and dependencies
290
+ return (!frame.includes("node_modules") &&
291
+ !frame.includes("node:internal") &&
292
+ !frame.includes("node:async_hooks") &&
293
+ !frame.includes("node:events"));
294
+ });
295
+ return filtered.slice(0, maxFrames);
296
+ }
297
+ /**
298
+ * Safely stringify an object with circular reference handling.
299
+ *
300
+ * @param obj - Object to stringify
301
+ * @param maxDepth - Maximum depth for nested objects
302
+ * @returns JSON string representation
303
+ */
304
+ export function safeStringify(obj, maxDepth = MAX_DEPTH) {
305
+ const seen = new WeakSet();
306
+ const replacer = (key, value, depth = 0) => {
307
+ // Depth limit
308
+ if (depth > maxDepth) {
309
+ return "[max depth reached]";
310
+ }
311
+ // Check for sensitive fields
312
+ if (key && isSensitiveField(key)) {
313
+ return "[REDACTED]";
314
+ }
315
+ // Handle null/undefined
316
+ if (value === null) {
317
+ return null;
318
+ }
319
+ if (value === undefined) {
320
+ return "[undefined]";
321
+ }
322
+ // Handle circular references
323
+ if (typeof value === "object" && value !== null) {
324
+ if (seen.has(value)) {
325
+ return "[Circular]";
326
+ }
327
+ seen.add(value);
328
+ }
329
+ // Handle special types
330
+ if (typeof value === "bigint") {
331
+ return value.toString() + "n";
332
+ }
333
+ if (value instanceof Error) {
334
+ return {
335
+ name: value.name,
336
+ message: value.message,
337
+ stack: value.stack?.split("\n").slice(0, 5).join("\n"),
338
+ };
339
+ }
340
+ if (value instanceof Date) {
341
+ return value.toISOString();
342
+ }
343
+ if (value instanceof RegExp) {
344
+ return value.toString();
345
+ }
346
+ if (typeof value === "function") {
347
+ return `[Function: ${value.name || "anonymous"}]`;
348
+ }
349
+ if (typeof value === "symbol") {
350
+ return value.toString();
351
+ }
352
+ if (ArrayBuffer.isView(value) || value instanceof ArrayBuffer) {
353
+ const length = value.byteLength;
354
+ return `[Buffer: ${length} bytes]`;
355
+ }
356
+ if (typeof Buffer !== "undefined" && Buffer.isBuffer(value)) {
357
+ return `[Buffer: ${value.length} bytes]`;
358
+ }
359
+ if (value instanceof Map) {
360
+ return {
361
+ __type: "Map",
362
+ entries: Array.from(value.entries()).slice(0, 100),
363
+ };
364
+ }
365
+ if (value instanceof Set) {
366
+ return { __type: "Set", values: Array.from(value).slice(0, 100) };
367
+ }
368
+ return value;
369
+ };
370
+ try {
371
+ // Custom JSON.stringify with depth tracking
372
+ const stringifyWithDepth = (val, currentDepth) => {
373
+ if (currentDepth > maxDepth) {
374
+ return '"[max depth reached]"';
375
+ }
376
+ if (val === null) {
377
+ return "null";
378
+ }
379
+ if (val === undefined) {
380
+ return '"[undefined]"';
381
+ }
382
+ const processed = replacer("", val, currentDepth);
383
+ if (processed === null) {
384
+ return "null";
385
+ }
386
+ if (typeof processed === "string") {
387
+ return JSON.stringify(processed);
388
+ }
389
+ if (typeof processed === "number" || typeof processed === "boolean") {
390
+ return String(processed);
391
+ }
392
+ if (Array.isArray(processed)) {
393
+ const items = processed.map((item) => stringifyWithDepth(item, currentDepth + 1));
394
+ return `[${items.join(",")}]`;
395
+ }
396
+ if (typeof processed === "object") {
397
+ const entries = Object.entries(processed).map(([k, v]) => {
398
+ const stringifiedValue = stringifyWithDepth(v, currentDepth + 1);
399
+ return `${JSON.stringify(k)}:${stringifiedValue}`;
400
+ });
401
+ return `{${entries.join(",")}}`;
402
+ }
403
+ return JSON.stringify(processed);
404
+ };
405
+ return stringifyWithDepth(obj, 0);
406
+ }
407
+ catch {
408
+ return String(obj);
409
+ }
410
+ }
411
+ /**
412
+ * Check if a field name is sensitive and should be redacted.
413
+ *
414
+ * @param fieldName - Field name to check
415
+ * @returns true if the field is sensitive
416
+ */
417
+ function isSensitiveField(fieldName) {
418
+ const lowerName = fieldName.toLowerCase();
419
+ return SENSITIVE_FIELDS.some((field) => lowerName.includes(field.toLowerCase()));
420
+ }
421
+ /**
422
+ * Sanitize and truncate metadata values.
423
+ * Redacts sensitive fields and limits size.
424
+ *
425
+ * @param key - Field key
426
+ * @param value - Field value
427
+ * @param maxSize - Maximum size in characters
428
+ * @param maxDepth - Maximum depth for nested objects
429
+ * @returns Sanitized and truncated value
430
+ */
431
+ function sanitizeAndTruncate(key, value, maxSize, maxDepth) {
432
+ // Check for sensitive fields
433
+ if (isSensitiveField(key)) {
434
+ return "[REDACTED]";
435
+ }
436
+ // Serialize value
437
+ const serialized = typeof value === "string" ? value : safeStringify(value, maxDepth);
438
+ // Truncate if too large
439
+ if (serialized.length > maxSize) {
440
+ return `${serialized.substring(0, maxSize)}... [truncated, total: ${serialized.length} chars]`;
441
+ }
442
+ // Return original value if it's a primitive, for cleaner logs
443
+ if (typeof value === "string" ||
444
+ typeof value === "number" ||
445
+ typeof value === "boolean") {
446
+ return value;
447
+ }
448
+ // For objects, try to return parsed version for cleaner logs
449
+ try {
450
+ return JSON.parse(serialized);
451
+ }
452
+ catch {
453
+ return serialized;
454
+ }
455
+ }
456
+ /**
457
+ * Extract safe metadata from an object.
458
+ * Sanitizes sensitive fields and handles truncation.
459
+ *
460
+ * @param obj - Object to extract metadata from
461
+ * @param options - Extraction options
462
+ * @returns Sanitized metadata record
463
+ *
464
+ * @example
465
+ * ```typescript
466
+ * const metadata = extractSafeMetadata({
467
+ * userId: "123",
468
+ * password: "secret",
469
+ * data: largeObject,
470
+ * });
471
+ * // Result: { userId: "123", password: "[REDACTED]", data: truncated }
472
+ * ```
473
+ */
474
+ export function extractSafeMetadata(obj, options) {
475
+ const { maxSize = MAX_METADATA_SIZE, maxDepth = MAX_DEPTH } = options || {};
476
+ const metadata = {};
477
+ if (!obj || typeof obj !== "object") {
478
+ return { value: String(obj) };
479
+ }
480
+ for (const [key, value] of Object.entries(obj)) {
481
+ metadata[key] = sanitizeAndTruncate(key, value, maxSize, maxDepth);
482
+ }
483
+ return metadata;
484
+ }
485
+ /**
486
+ * Create a minimal error representation for logging.
487
+ * Useful when you need just the essentials without full serialization.
488
+ *
489
+ * @param error - Error to summarize
490
+ * @returns Minimal error representation
491
+ */
492
+ export function summarizeError(error) {
493
+ if (error instanceof Error) {
494
+ return {
495
+ type: error.name,
496
+ message: error.message.substring(0, 200),
497
+ code: "code" in error ? String(error.code) : undefined,
498
+ fingerprint: generateErrorFingerprint(error),
499
+ };
500
+ }
501
+ const message = String(error).substring(0, 200);
502
+ return {
503
+ type: "UnknownError",
504
+ message,
505
+ fingerprint: generateFingerprintFromString(message),
506
+ };
507
+ }
@@ -0,0 +1,46 @@
1
+ /**
2
+ * File Processing Error Infrastructure
3
+ *
4
+ * Provides comprehensive error handling for file processing operations:
5
+ * - Error codes for all file processing scenarios
6
+ * - Factory functions for creating structured errors
7
+ * - Error serialization with PII redaction
8
+ * - Fingerprinting for error deduplication
9
+ * - Retry determination logic
10
+ *
11
+ * @module processors/errors
12
+ *
13
+ * @example
14
+ * ```typescript
15
+ * import {
16
+ * FileErrorCode,
17
+ * createFileError,
18
+ * isRetryableError,
19
+ * serializeError,
20
+ * generateErrorFingerprint,
21
+ * } from "./processors/errors/index.js";
22
+ *
23
+ * // Create a structured error
24
+ * const error = createFileError(FileErrorCode.FILE_TOO_LARGE, {
25
+ * sizeMB: "15.5",
26
+ * maxMB: "10",
27
+ * filename: "large-document.pdf",
28
+ * });
29
+ *
30
+ * // Check if error is retryable
31
+ * if (isRetryableError(error) && retryCount < maxRetries) {
32
+ * await delay(getRetryDelay(error, retryCount));
33
+ * return retry();
34
+ * }
35
+ *
36
+ * // Serialize for logging (with PII redaction)
37
+ * const serialized = serializeError(originalError);
38
+ * logger.error("File processing failed", serialized);
39
+ *
40
+ * // Group similar errors by fingerprint
41
+ * const fingerprint = generateErrorFingerprint(error);
42
+ * ```
43
+ */
44
+ export { combineSummaries, createCustomFileError, createFileError, createProcessingSummary, extractHttpStatus, type FileProcessingError, type FileProcessingSummary, formatFileError, getRetryDelay, isFileProcessingError, isRetryableError, mapErrorToCode, } from "./errorHelpers.js";
45
+ export { extractSafeMetadata, generateErrorFingerprint, type SerializedError, type SerializeOptions, safeStringify, serializeError, summarizeError, } from "./errorSerializer.js";
46
+ export { ERROR_MESSAGES, type ErrorMessageTemplate, FileErrorCode, getErrorTemplate, isRetryableErrorCode, } from "./FileErrorCode.js";
@@ -0,0 +1,49 @@
1
+ /**
2
+ * File Processing Error Infrastructure
3
+ *
4
+ * Provides comprehensive error handling for file processing operations:
5
+ * - Error codes for all file processing scenarios
6
+ * - Factory functions for creating structured errors
7
+ * - Error serialization with PII redaction
8
+ * - Fingerprinting for error deduplication
9
+ * - Retry determination logic
10
+ *
11
+ * @module processors/errors
12
+ *
13
+ * @example
14
+ * ```typescript
15
+ * import {
16
+ * FileErrorCode,
17
+ * createFileError,
18
+ * isRetryableError,
19
+ * serializeError,
20
+ * generateErrorFingerprint,
21
+ * } from "./processors/errors/index.js";
22
+ *
23
+ * // Create a structured error
24
+ * const error = createFileError(FileErrorCode.FILE_TOO_LARGE, {
25
+ * sizeMB: "15.5",
26
+ * maxMB: "10",
27
+ * filename: "large-document.pdf",
28
+ * });
29
+ *
30
+ * // Check if error is retryable
31
+ * if (isRetryableError(error) && retryCount < maxRetries) {
32
+ * await delay(getRetryDelay(error, retryCount));
33
+ * return retry();
34
+ * }
35
+ *
36
+ * // Serialize for logging (with PII redaction)
37
+ * const serialized = serializeError(originalError);
38
+ * logger.error("File processing failed", serialized);
39
+ *
40
+ * // Group similar errors by fingerprint
41
+ * const fingerprint = generateErrorFingerprint(error);
42
+ * ```
43
+ */
44
+ // Error creation helpers
45
+ export { combineSummaries, createCustomFileError, createFileError, createProcessingSummary, extractHttpStatus, formatFileError, getRetryDelay, isFileProcessingError, isRetryableError, mapErrorToCode, } from "./errorHelpers.js";
46
+ // Error serialization
47
+ export { extractSafeMetadata, generateErrorFingerprint, safeStringify, serializeError, summarizeError, } from "./errorSerializer.js";
48
+ // Error codes and message templates
49
+ export { ERROR_MESSAGES, FileErrorCode, getErrorTemplate, isRetryableErrorCode, } from "./FileErrorCode.js";
@@ -0,0 +1,76 @@
1
+ /**
2
+ * File Processors Module
3
+ *
4
+ * Comprehensive file processing infrastructure for NeuroLink.
5
+ * Provides base classes, configuration, error handling, and registry
6
+ * for building and managing file processors.
7
+ *
8
+ * @module processors
9
+ *
10
+ * @example
11
+ * ```typescript
12
+ * import {
13
+ * // Base processor infrastructure
14
+ * BaseFileProcessor,
15
+ * type FileInfo,
16
+ * type FileProcessingResult,
17
+ * type ProcessedFileBase,
18
+ *
19
+ * // Configuration
20
+ * MIME_TYPES,
21
+ * FILE_EXTENSIONS,
22
+ * SIZE_LIMITS,
23
+ *
24
+ * // Error handling
25
+ * FileErrorCode,
26
+ * createFileError,
27
+ *
28
+ * // Registry
29
+ * ProcessorRegistry,
30
+ * getProcessorRegistry,
31
+ * PROCESSOR_PRIORITIES,
32
+ * } from "./processors/index.js";
33
+ *
34
+ * // Create a custom processor
35
+ * class MyProcessor extends BaseFileProcessor<MyProcessedFile> {
36
+ * constructor() {
37
+ * super({
38
+ * maxSizeMB: 10,
39
+ * timeoutMs: 30000,
40
+ * supportedMimeTypes: ["application/x-custom"],
41
+ * supportedExtensions: [".custom"],
42
+ * fileTypeName: "custom",
43
+ * defaultFilename: "file.custom",
44
+ * });
45
+ * }
46
+ *
47
+ * protected buildProcessedResult(buffer: Buffer, fileInfo: FileInfo): MyProcessedFile {
48
+ * return {
49
+ * buffer,
50
+ * mimetype: fileInfo.mimetype,
51
+ * size: buffer.length,
52
+ * filename: this.getFilename(fileInfo),
53
+ * };
54
+ * }
55
+ * }
56
+ *
57
+ * // Register with the registry
58
+ * const registry = getProcessorRegistry();
59
+ * registry.register({
60
+ * name: "custom",
61
+ * priority: 100,
62
+ * processor: new MyProcessor(),
63
+ * isSupported: (mimetype) => mimetype === "application/x-custom",
64
+ * });
65
+ * ```
66
+ */
67
+ export { BaseFileProcessor, type BatchProcessingSummary, DEFAULT_IMAGE_MAX_SIZE_MB, DEFAULT_IMAGE_TIMEOUT_MS, DEFAULT_RETRY_CONFIG, DEFAULT_TEXT_MAX_SIZE_MB, DEFAULT_TEXT_TIMEOUT_MS, type ExcelWorksheet, type FailedFileInfo, type FileInfo, type FileProcessingError, type FileProcessingResult, type FileProcessorConfig, type FileWarning, getDefaultImageMaxSizeMB, getDefaultImageTimeout, getDefaultTextMaxSizeMB, getDefaultTextTimeout, type JsonTypeGuard, type OperationResult, PROCESSOR_PRIORITIES, type ProcessedConfig, type ProcessedExcel, type ProcessedFileBase, type ProcessedFileInfo, type ProcessedHtml, type ProcessedJson, type ProcessedMarkdown, type ProcessedOpenDocument, type ProcessedRtf, type ProcessedSourceCode, type ProcessedSvg, type ProcessedText, type ProcessedWord, type ProcessedYaml, type ProcessOptions, type ProcessorInfo, type ProcessorMatch, type ProcessorPriorityKey, type ProcessorPriorityValue, type RegistryOptions, type RegistryProcessResult, type RetryConfig, type SkippedFileInfo, type UnsupportedFileError, } from "./base/index.js";
68
+ export { AI_VISION_EXTENSIONS, ARCHIVE_EXTENSIONS, ARCHIVE_LIMITS, ARCHIVE_MIME_TYPES, type ArchiveExtension, type ArchiveMimeType, AUDIO_EXTENSIONS, AUDIO_MIME_TYPES, type AudioExtension, type AudioMimeType, bytesToMB, CONFIG_EXTENSIONS, type ConfigExtension, CSV_EXTENSIONS, DATA_EXTENSIONS, DATA_MIME_TYPES, DATABASE_EXTENSIONS, type DataExtension, type DataMimeType, DESIGN_EXTENSIONS, DOCUMENT_EXTENSIONS, DOCUMENT_MIME_TYPES, type DocumentExtension, type DocumentMimeType, detectLanguageFromFilename, EXACT_FILENAME_MAP, EXCEL_EXTENSIONS, EXECUTABLE_EXTENSIONS, type ExactFilenameMap, FILE_EXTENSIONS, type FileExtensions, formatBytes, getLanguageIdentifier, getSizeLimitForType, getSupportedExtensions, getSupportedFilenames, HTML_EXTENSIONS, IMAGE_EXTENSIONS, IMAGE_MIME_TYPES, type ImageExtension, type ImageMimeType, isSourceCodeFile, isWithinSizeLimit, JSON_EXTENSIONS, LANGUAGE_MAP, type LanguageMap, MARKDOWN_EXTENSIONS, MIME_TYPES, type MimeType, mbToBytes, OPENDOCUMENT_EXTENSIONS, PDF_EXTENSIONS, POWERPOINT_EXTENSIONS, PROCESSING_LIMITS, type ProcessingLimitKey, RTF_EXTENSIONS, SIZE_LIMITS, SIZE_LIMITS_BYTES, SIZE_LIMITS_MB, type SizeLimitBytesKey, type SizeLimitMBKey, type SizeLimits, SOURCE_CODE_EXTENSIONS, SOURCE_CODE_MIME_TYPES, type SourceCodeExtension, type SourceCodeMimeType, TEXT_EXTENSIONS, TEXT_MIME_TYPES, type TextExtension, type TextMimeType, VIDEO_EXTENSIONS, VIDEO_MIME_TYPES, type VideoExtension, type VideoMimeType, validateFileSize, WORD_EXTENSIONS, XML_EXTENSIONS, YAML_EXTENSIONS, YAML_LIMITS, } from "./config/index.js";
69
+ export { combineSummaries, createCustomFileError, createFileError, createProcessingSummary, ERROR_MESSAGES, type ErrorMessageTemplate, extractHttpStatus, extractSafeMetadata, FileErrorCode, type FileProcessingError as ProcessingError, type FileProcessingSummary, formatFileError, generateErrorFingerprint, getErrorTemplate, getRetryDelay, isFileProcessingError, isRetryableError, isRetryableErrorCode, mapErrorToCode, type SerializedError, type SerializeOptions, safeStringify, serializeError, summarizeError, } from "./errors/index.js";
70
+ export { getProcessorRegistry, type ProcessorRegistration, ProcessorRegistry, } from "./registry/index.js";
71
+ export { HtmlProcessor, htmlProcessor, isHtmlFile, isMarkdownFile, isSvgFile, isTextFile, MarkdownProcessor, markdownProcessor, processHtml, processMarkdown, processSvg, processText, SvgProcessor, svgProcessor, TextProcessor, textProcessor, validateHtmlSize, validateMarkdownSize, validateSvgSize, } from "./markup/index.js";
72
+ export { ConfigProcessor, configProcessor, detectLanguage, isConfigFile, processConfig, processSourceCode, SourceCodeProcessor, sourceCodeProcessor, validateSourceCodeSize, } from "./code/index.js";
73
+ export { isJsonFile, isXmlFile, isYamlFile, JsonProcessor, jsonProcessor, processJson, processXml, processYaml, validateJsonSize, validateXmlSize, validateYamlSize, XmlProcessor, xmlProcessor, YamlProcessor, yamlProcessor, } from "./data/index.js";
74
+ export { ExcelProcessor, excelProcessor, getExcelMaxRows, getExcelMaxSheets, getExcelMaxSizeMB, getOpenDocumentMaxSizeMB, isExcelFile, isOpenDocumentFile, isRtfFile, isWordFile, OpenDocumentProcessor, openDocumentProcessor, processExcel, processOpenDocument, processRtf, processWord, RtfProcessor, rtfProcessor, validateExcelSize, validateOpenDocumentSize, validateRtfSize, validateWordSize, WordProcessor, wordProcessor, } from "./document/index.js";
75
+ export { type BatchFileProcessingResult, type FileProcessingOptions, getProcessorForFile, getSupportedFileTypes, isFileTypeSupported, processBatchWithRegistry, processFileWithRegistry, } from "./integration/index.js";
76
+ export { type CliFileProcessingOptions, type CliProcessingResult, detectMimeType, fileExists, getCliUsage, getFileExtension, getSupportedFileTypes as getCliSupportedFileTypes, listSupportedFileTypes, loadFileFromPath, processFileFromPath, type SupportedFileTypeInfo, } from "./cli/index.js";