@juspay/neurolink 9.1.1 → 9.3.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.
- package/CHANGELOG.md +27 -0
- package/README.md +106 -37
- package/dist/agent/directTools.d.ts +11 -11
- package/dist/cli/commands/config.d.ts +6 -6
- package/dist/cli/commands/rag.d.ts +19 -0
- package/dist/cli/commands/rag.js +756 -0
- package/dist/cli/factories/commandFactory.js +146 -83
- package/dist/cli/parser.js +4 -1
- package/dist/core/baseProvider.d.ts +43 -30
- package/dist/core/baseProvider.js +98 -138
- package/dist/core/conversationMemoryFactory.d.ts +2 -2
- package/dist/core/conversationMemoryFactory.js +2 -2
- package/dist/core/conversationMemoryInitializer.d.ts +1 -2
- package/dist/core/conversationMemoryInitializer.js +2 -2
- package/dist/core/infrastructure/baseError.d.ts +21 -0
- package/dist/core/infrastructure/baseError.js +22 -0
- package/dist/core/infrastructure/baseFactory.d.ts +21 -0
- package/dist/core/infrastructure/baseFactory.js +54 -0
- package/dist/core/infrastructure/baseRegistry.d.ts +21 -0
- package/dist/core/infrastructure/baseRegistry.js +49 -0
- package/dist/core/infrastructure/index.d.ts +5 -0
- package/dist/core/infrastructure/index.js +5 -0
- package/dist/core/infrastructure/retry.d.ts +7 -0
- package/dist/core/infrastructure/retry.js +20 -0
- package/dist/core/infrastructure/typedEventEmitter.d.ts +8 -0
- package/dist/core/infrastructure/typedEventEmitter.js +23 -0
- package/dist/core/redisConversationMemoryManager.d.ts +1 -6
- package/dist/core/redisConversationMemoryManager.js +7 -19
- package/dist/factories/providerFactory.d.ts +5 -3
- package/dist/factories/providerFactory.js +31 -24
- package/dist/image-gen/ImageGenService.d.ts +143 -0
- package/dist/image-gen/ImageGenService.js +345 -0
- package/dist/image-gen/imageGenTools.d.ts +126 -0
- package/dist/image-gen/imageGenTools.js +304 -0
- package/dist/image-gen/index.d.ts +46 -0
- package/dist/image-gen/index.js +48 -0
- package/dist/image-gen/types.d.ts +237 -0
- package/dist/image-gen/types.js +24 -0
- package/dist/index.d.ts +46 -12
- package/dist/index.js +88 -36
- package/dist/lib/agent/directTools.d.ts +8 -8
- package/dist/lib/core/baseProvider.d.ts +43 -30
- package/dist/lib/core/baseProvider.js +98 -138
- package/dist/lib/core/conversationMemoryFactory.d.ts +2 -2
- package/dist/lib/core/conversationMemoryFactory.js +2 -2
- package/dist/lib/core/conversationMemoryInitializer.d.ts +1 -2
- package/dist/lib/core/conversationMemoryInitializer.js +2 -2
- package/dist/lib/core/infrastructure/baseError.d.ts +21 -0
- package/dist/lib/core/infrastructure/baseError.js +23 -0
- package/dist/lib/core/infrastructure/baseFactory.d.ts +21 -0
- package/dist/lib/core/infrastructure/baseFactory.js +55 -0
- package/dist/lib/core/infrastructure/baseRegistry.d.ts +21 -0
- package/dist/lib/core/infrastructure/baseRegistry.js +50 -0
- package/dist/lib/core/infrastructure/index.d.ts +5 -0
- package/dist/lib/core/infrastructure/index.js +6 -0
- package/dist/lib/core/infrastructure/retry.d.ts +7 -0
- package/dist/lib/core/infrastructure/retry.js +21 -0
- package/dist/lib/core/infrastructure/typedEventEmitter.d.ts +8 -0
- package/dist/lib/core/infrastructure/typedEventEmitter.js +24 -0
- package/dist/lib/core/redisConversationMemoryManager.d.ts +1 -6
- package/dist/lib/core/redisConversationMemoryManager.js +7 -19
- package/dist/lib/factories/providerFactory.d.ts +5 -3
- package/dist/lib/factories/providerFactory.js +31 -24
- package/dist/lib/image-gen/ImageGenService.d.ts +143 -0
- package/dist/lib/image-gen/ImageGenService.js +346 -0
- package/dist/lib/image-gen/imageGenTools.d.ts +126 -0
- package/dist/lib/image-gen/imageGenTools.js +305 -0
- package/dist/lib/image-gen/index.d.ts +46 -0
- package/dist/lib/image-gen/index.js +49 -0
- package/dist/lib/image-gen/types.d.ts +237 -0
- package/dist/lib/image-gen/types.js +25 -0
- package/dist/lib/index.d.ts +46 -12
- package/dist/lib/index.js +88 -36
- package/dist/lib/mcp/index.d.ts +6 -5
- package/dist/lib/mcp/index.js +7 -5
- package/dist/lib/neurolink.d.ts +11 -13
- package/dist/lib/neurolink.js +95 -29
- package/dist/lib/processors/base/BaseFileProcessor.d.ts +273 -0
- package/dist/lib/processors/base/BaseFileProcessor.js +614 -0
- package/dist/lib/processors/base/index.d.ts +14 -0
- package/dist/lib/processors/base/index.js +20 -0
- package/dist/lib/processors/base/types.d.ts +593 -0
- package/dist/lib/processors/base/types.js +77 -0
- package/dist/lib/processors/cli/fileProcessorCli.d.ts +163 -0
- package/dist/lib/processors/cli/fileProcessorCli.js +389 -0
- package/dist/lib/processors/cli/index.d.ts +37 -0
- package/dist/lib/processors/cli/index.js +50 -0
- package/dist/lib/processors/code/ConfigProcessor.d.ts +171 -0
- package/dist/lib/processors/code/ConfigProcessor.js +401 -0
- package/dist/lib/processors/code/SourceCodeProcessor.d.ts +174 -0
- package/dist/lib/processors/code/SourceCodeProcessor.js +305 -0
- package/dist/lib/processors/code/index.d.ts +44 -0
- package/dist/lib/processors/code/index.js +61 -0
- package/dist/lib/processors/config/fileTypes.d.ts +283 -0
- package/dist/lib/processors/config/fileTypes.js +521 -0
- package/dist/lib/processors/config/index.d.ts +32 -0
- package/dist/lib/processors/config/index.js +93 -0
- package/dist/lib/processors/config/languageMap.d.ts +66 -0
- package/dist/lib/processors/config/languageMap.js +411 -0
- package/dist/lib/processors/config/mimeTypes.d.ts +376 -0
- package/dist/lib/processors/config/mimeTypes.js +339 -0
- package/dist/lib/processors/config/sizeLimits.d.ts +194 -0
- package/dist/lib/processors/config/sizeLimits.js +247 -0
- package/dist/lib/processors/data/JsonProcessor.d.ts +122 -0
- package/dist/lib/processors/data/JsonProcessor.js +204 -0
- package/dist/lib/processors/data/XmlProcessor.d.ts +160 -0
- package/dist/lib/processors/data/XmlProcessor.js +284 -0
- package/dist/lib/processors/data/YamlProcessor.d.ts +163 -0
- package/dist/lib/processors/data/YamlProcessor.js +295 -0
- package/dist/lib/processors/data/index.d.ts +49 -0
- package/dist/lib/processors/data/index.js +77 -0
- package/dist/lib/processors/document/ExcelProcessor.d.ts +238 -0
- package/dist/lib/processors/document/ExcelProcessor.js +520 -0
- package/dist/lib/processors/document/OpenDocumentProcessor.d.ts +69 -0
- package/dist/lib/processors/document/OpenDocumentProcessor.js +211 -0
- package/dist/lib/processors/document/RtfProcessor.d.ts +152 -0
- package/dist/lib/processors/document/RtfProcessor.js +362 -0
- package/dist/lib/processors/document/WordProcessor.d.ts +168 -0
- package/dist/lib/processors/document/WordProcessor.js +354 -0
- package/dist/lib/processors/document/index.d.ts +54 -0
- package/dist/lib/processors/document/index.js +91 -0
- package/dist/lib/processors/errors/FileErrorCode.d.ts +98 -0
- package/dist/lib/processors/errors/FileErrorCode.js +256 -0
- package/dist/lib/processors/errors/errorHelpers.d.ts +151 -0
- package/dist/lib/processors/errors/errorHelpers.js +379 -0
- package/dist/lib/processors/errors/errorSerializer.d.ts +139 -0
- package/dist/lib/processors/errors/errorSerializer.js +508 -0
- package/dist/lib/processors/errors/index.d.ts +46 -0
- package/dist/lib/processors/errors/index.js +50 -0
- package/dist/lib/processors/index.d.ts +76 -0
- package/dist/lib/processors/index.js +113 -0
- package/dist/lib/processors/integration/FileProcessorIntegration.d.ts +244 -0
- package/dist/lib/processors/integration/FileProcessorIntegration.js +273 -0
- package/dist/lib/processors/integration/index.d.ts +42 -0
- package/dist/lib/processors/integration/index.js +45 -0
- package/dist/lib/processors/markup/HtmlProcessor.d.ts +169 -0
- package/dist/lib/processors/markup/HtmlProcessor.js +250 -0
- package/dist/lib/processors/markup/MarkdownProcessor.d.ts +165 -0
- package/dist/lib/processors/markup/MarkdownProcessor.js +245 -0
- package/dist/lib/processors/markup/SvgProcessor.d.ts +156 -0
- package/dist/lib/processors/markup/SvgProcessor.js +241 -0
- package/dist/lib/processors/markup/TextProcessor.d.ts +135 -0
- package/dist/lib/processors/markup/TextProcessor.js +189 -0
- package/dist/lib/processors/markup/index.d.ts +66 -0
- package/dist/lib/processors/markup/index.js +103 -0
- package/dist/lib/processors/registry/ProcessorRegistry.d.ts +334 -0
- package/dist/lib/processors/registry/ProcessorRegistry.js +609 -0
- package/dist/lib/processors/registry/index.d.ts +12 -0
- package/dist/lib/processors/registry/index.js +17 -0
- package/dist/lib/processors/registry/types.d.ts +53 -0
- package/dist/lib/processors/registry/types.js +11 -0
- package/dist/lib/providers/amazonBedrock.d.ts +15 -2
- package/dist/lib/providers/amazonBedrock.js +65 -8
- package/dist/lib/providers/anthropic.d.ts +3 -3
- package/dist/lib/providers/anthropic.js +10 -7
- package/dist/lib/providers/googleAiStudio.d.ts +5 -5
- package/dist/lib/providers/googleAiStudio.js +10 -7
- package/dist/lib/providers/googleVertex.d.ts +16 -4
- package/dist/lib/providers/googleVertex.js +72 -16
- package/dist/lib/providers/litellm.d.ts +3 -3
- package/dist/lib/providers/litellm.js +10 -10
- package/dist/lib/providers/mistral.d.ts +3 -3
- package/dist/lib/providers/mistral.js +7 -6
- package/dist/lib/providers/ollama.d.ts +3 -4
- package/dist/lib/providers/ollama.js +7 -8
- package/dist/lib/providers/openAI.d.ts +14 -2
- package/dist/lib/providers/openAI.js +60 -6
- package/dist/lib/providers/openRouter.d.ts +2 -2
- package/dist/lib/providers/openRouter.js +10 -6
- package/dist/lib/providers/sagemaker/language-model.d.ts +2 -2
- package/dist/lib/rag/ChunkerFactory.d.ts +91 -0
- package/dist/lib/rag/ChunkerFactory.js +321 -0
- package/dist/lib/rag/ChunkerRegistry.d.ts +91 -0
- package/dist/lib/rag/ChunkerRegistry.js +422 -0
- package/dist/lib/rag/chunkers/BaseChunker.d.ts +53 -0
- package/dist/lib/rag/chunkers/BaseChunker.js +144 -0
- package/dist/lib/rag/chunkers/CharacterChunker.d.ts +18 -0
- package/dist/lib/rag/chunkers/CharacterChunker.js +29 -0
- package/dist/lib/rag/chunkers/HTMLChunker.d.ts +19 -0
- package/dist/lib/rag/chunkers/HTMLChunker.js +39 -0
- package/dist/lib/rag/chunkers/JSONChunker.d.ts +19 -0
- package/dist/lib/rag/chunkers/JSONChunker.js +69 -0
- package/dist/lib/rag/chunkers/LaTeXChunker.d.ts +15 -0
- package/dist/lib/rag/chunkers/LaTeXChunker.js +64 -0
- package/dist/lib/rag/chunkers/MarkdownChunker.d.ts +15 -0
- package/dist/lib/rag/chunkers/MarkdownChunker.js +103 -0
- package/dist/lib/rag/chunkers/RecursiveChunker.d.ts +27 -0
- package/dist/lib/rag/chunkers/RecursiveChunker.js +140 -0
- package/dist/lib/rag/chunkers/SemanticMarkdownChunker.d.ts +22 -0
- package/dist/lib/rag/chunkers/SemanticMarkdownChunker.js +139 -0
- package/dist/lib/rag/chunkers/SentenceChunker.d.ts +19 -0
- package/dist/lib/rag/chunkers/SentenceChunker.js +67 -0
- package/dist/lib/rag/chunkers/TokenChunker.d.ts +19 -0
- package/dist/lib/rag/chunkers/TokenChunker.js +62 -0
- package/dist/lib/rag/chunkers/index.d.ts +15 -0
- package/dist/lib/rag/chunkers/index.js +16 -0
- package/dist/lib/rag/chunking/characterChunker.d.ts +16 -0
- package/dist/lib/rag/chunking/characterChunker.js +143 -0
- package/dist/lib/rag/chunking/chunkerRegistry.d.ts +67 -0
- package/dist/lib/rag/chunking/chunkerRegistry.js +195 -0
- package/dist/lib/rag/chunking/htmlChunker.d.ts +34 -0
- package/dist/lib/rag/chunking/htmlChunker.js +248 -0
- package/dist/lib/rag/chunking/index.d.ts +15 -0
- package/dist/lib/rag/chunking/index.js +18 -0
- package/dist/lib/rag/chunking/jsonChunker.d.ts +20 -0
- package/dist/lib/rag/chunking/jsonChunker.js +282 -0
- package/dist/lib/rag/chunking/latexChunker.d.ts +26 -0
- package/dist/lib/rag/chunking/latexChunker.js +252 -0
- package/dist/lib/rag/chunking/markdownChunker.d.ts +19 -0
- package/dist/lib/rag/chunking/markdownChunker.js +202 -0
- package/dist/lib/rag/chunking/recursiveChunker.d.ts +19 -0
- package/dist/lib/rag/chunking/recursiveChunker.js +149 -0
- package/dist/lib/rag/chunking/semanticChunker.d.ts +41 -0
- package/dist/lib/rag/chunking/semanticChunker.js +307 -0
- package/dist/lib/rag/chunking/sentenceChunker.d.ts +25 -0
- package/dist/lib/rag/chunking/sentenceChunker.js +231 -0
- package/dist/lib/rag/chunking/tokenChunker.d.ts +36 -0
- package/dist/lib/rag/chunking/tokenChunker.js +184 -0
- package/dist/lib/rag/document/MDocument.d.ts +198 -0
- package/dist/lib/rag/document/MDocument.js +393 -0
- package/dist/lib/rag/document/index.d.ts +5 -0
- package/dist/lib/rag/document/index.js +6 -0
- package/dist/lib/rag/document/loaders.d.ts +201 -0
- package/dist/lib/rag/document/loaders.js +501 -0
- package/dist/lib/rag/errors/RAGError.d.ts +244 -0
- package/dist/lib/rag/errors/RAGError.js +275 -0
- package/dist/lib/rag/errors/index.d.ts +6 -0
- package/dist/lib/rag/errors/index.js +7 -0
- package/dist/lib/rag/graphRag/graphRAG.d.ts +115 -0
- package/dist/lib/rag/graphRag/graphRAG.js +385 -0
- package/dist/lib/rag/graphRag/index.d.ts +4 -0
- package/dist/lib/rag/graphRag/index.js +5 -0
- package/dist/lib/rag/index.d.ts +103 -0
- package/dist/lib/rag/index.js +142 -0
- package/dist/lib/rag/metadata/MetadataExtractorFactory.d.ts +157 -0
- package/dist/lib/rag/metadata/MetadataExtractorFactory.js +419 -0
- package/dist/lib/rag/metadata/MetadataExtractorRegistry.d.ts +99 -0
- package/dist/lib/rag/metadata/MetadataExtractorRegistry.js +363 -0
- package/dist/lib/rag/metadata/index.d.ts +6 -0
- package/dist/lib/rag/metadata/index.js +10 -0
- package/dist/lib/rag/metadata/metadataExtractor.d.ts +69 -0
- package/dist/lib/rag/metadata/metadataExtractor.js +278 -0
- package/dist/lib/rag/pipeline/RAGPipeline.d.ts +235 -0
- package/dist/lib/rag/pipeline/RAGPipeline.js +402 -0
- package/dist/lib/rag/pipeline/contextAssembly.d.ts +126 -0
- package/dist/lib/rag/pipeline/contextAssembly.js +338 -0
- package/dist/lib/rag/pipeline/index.d.ts +5 -0
- package/dist/lib/rag/pipeline/index.js +6 -0
- package/dist/lib/rag/ragIntegration.d.ts +38 -0
- package/dist/lib/rag/ragIntegration.js +212 -0
- package/dist/lib/rag/reranker/RerankerFactory.d.ts +184 -0
- package/dist/lib/rag/reranker/RerankerFactory.js +431 -0
- package/dist/lib/rag/reranker/RerankerRegistry.d.ts +119 -0
- package/dist/lib/rag/reranker/RerankerRegistry.js +403 -0
- package/dist/lib/rag/reranker/index.d.ts +6 -0
- package/dist/lib/rag/reranker/index.js +10 -0
- package/dist/lib/rag/reranker/reranker.d.ts +71 -0
- package/dist/lib/rag/reranker/reranker.js +278 -0
- package/dist/lib/rag/resilience/CircuitBreaker.d.ts +215 -0
- package/dist/lib/rag/resilience/CircuitBreaker.js +432 -0
- package/dist/lib/rag/resilience/RetryHandler.d.ts +115 -0
- package/dist/lib/rag/resilience/RetryHandler.js +301 -0
- package/dist/lib/rag/resilience/index.d.ts +7 -0
- package/dist/lib/rag/resilience/index.js +8 -0
- package/dist/lib/rag/retrieval/hybridSearch.d.ts +94 -0
- package/dist/lib/rag/retrieval/hybridSearch.js +314 -0
- package/dist/lib/rag/retrieval/index.d.ts +5 -0
- package/dist/lib/rag/retrieval/index.js +6 -0
- package/dist/lib/rag/retrieval/vectorQueryTool.d.ts +93 -0
- package/dist/lib/rag/retrieval/vectorQueryTool.js +290 -0
- package/dist/lib/rag/types.d.ts +768 -0
- package/dist/lib/rag/types.js +9 -0
- package/dist/lib/server/index.d.ts +15 -11
- package/dist/lib/server/index.js +55 -51
- package/dist/lib/server/utils/validation.d.ts +2 -2
- package/dist/lib/types/common.d.ts +0 -1
- package/dist/lib/types/fileTypes.d.ts +1 -1
- package/dist/lib/types/generateTypes.d.ts +42 -8
- package/dist/lib/types/generateTypes.js +1 -1
- package/dist/lib/types/index.d.ts +25 -24
- package/dist/lib/types/index.js +21 -20
- package/dist/lib/types/modelTypes.d.ts +16 -16
- package/dist/lib/types/pptTypes.d.ts +14 -2
- package/dist/lib/types/pptTypes.js +16 -0
- package/dist/lib/types/streamTypes.d.ts +28 -8
- package/dist/lib/types/streamTypes.js +1 -1
- package/dist/lib/utils/async/delay.d.ts +40 -0
- package/dist/lib/utils/async/delay.js +43 -0
- package/dist/lib/utils/async/index.d.ts +23 -0
- package/dist/lib/utils/async/index.js +24 -0
- package/dist/lib/utils/async/retry.d.ts +141 -0
- package/dist/lib/utils/async/retry.js +172 -0
- package/dist/lib/utils/async/withTimeout.d.ts +73 -0
- package/dist/lib/utils/async/withTimeout.js +97 -0
- package/dist/lib/utils/fileDetector.d.ts +7 -1
- package/dist/lib/utils/fileDetector.js +91 -18
- package/dist/lib/utils/json/extract.d.ts +103 -0
- package/dist/lib/utils/json/extract.js +249 -0
- package/dist/lib/utils/json/index.d.ts +36 -0
- package/dist/lib/utils/json/index.js +37 -0
- package/dist/lib/utils/json/safeParse.d.ts +137 -0
- package/dist/lib/utils/json/safeParse.js +191 -0
- package/dist/lib/utils/messageBuilder.d.ts +2 -2
- package/dist/lib/utils/messageBuilder.js +15 -7
- package/dist/lib/utils/modelRouter.d.ts +4 -4
- package/dist/lib/utils/modelRouter.js +4 -4
- package/dist/lib/utils/sanitizers/filename.d.ts +137 -0
- package/dist/lib/utils/sanitizers/filename.js +366 -0
- package/dist/lib/utils/sanitizers/html.d.ts +170 -0
- package/dist/lib/utils/sanitizers/html.js +326 -0
- package/dist/lib/utils/sanitizers/index.d.ts +26 -0
- package/dist/lib/utils/sanitizers/index.js +30 -0
- package/dist/lib/utils/sanitizers/svg.d.ts +81 -0
- package/dist/lib/utils/sanitizers/svg.js +483 -0
- package/dist/mcp/index.d.ts +6 -5
- package/dist/mcp/index.js +7 -5
- package/dist/neurolink.d.ts +11 -13
- package/dist/neurolink.js +95 -29
- package/dist/processors/base/BaseFileProcessor.d.ts +273 -0
- package/dist/processors/base/BaseFileProcessor.js +613 -0
- package/dist/processors/base/index.d.ts +14 -0
- package/dist/processors/base/index.js +19 -0
- package/dist/processors/base/types.d.ts +593 -0
- package/dist/processors/base/types.js +76 -0
- package/dist/processors/cli/fileProcessorCli.d.ts +163 -0
- package/dist/processors/cli/fileProcessorCli.js +388 -0
- package/dist/processors/cli/index.d.ts +37 -0
- package/dist/processors/cli/index.js +49 -0
- package/dist/processors/code/ConfigProcessor.d.ts +171 -0
- package/dist/processors/code/ConfigProcessor.js +400 -0
- package/dist/processors/code/SourceCodeProcessor.d.ts +174 -0
- package/dist/processors/code/SourceCodeProcessor.js +304 -0
- package/dist/processors/code/index.d.ts +44 -0
- package/dist/processors/code/index.js +60 -0
- package/dist/processors/config/fileTypes.d.ts +283 -0
- package/dist/processors/config/fileTypes.js +520 -0
- package/dist/processors/config/index.d.ts +32 -0
- package/dist/processors/config/index.js +92 -0
- package/dist/processors/config/languageMap.d.ts +66 -0
- package/dist/processors/config/languageMap.js +410 -0
- package/dist/processors/config/mimeTypes.d.ts +376 -0
- package/dist/processors/config/mimeTypes.js +338 -0
- package/dist/processors/config/sizeLimits.d.ts +194 -0
- package/dist/processors/config/sizeLimits.js +246 -0
- package/dist/processors/data/JsonProcessor.d.ts +122 -0
- package/dist/processors/data/JsonProcessor.js +203 -0
- package/dist/processors/data/XmlProcessor.d.ts +160 -0
- package/dist/processors/data/XmlProcessor.js +283 -0
- package/dist/processors/data/YamlProcessor.d.ts +163 -0
- package/dist/processors/data/YamlProcessor.js +294 -0
- package/dist/processors/data/index.d.ts +49 -0
- package/dist/processors/data/index.js +76 -0
- package/dist/processors/document/ExcelProcessor.d.ts +238 -0
- package/dist/processors/document/ExcelProcessor.js +519 -0
- package/dist/processors/document/OpenDocumentProcessor.d.ts +69 -0
- package/dist/processors/document/OpenDocumentProcessor.js +210 -0
- package/dist/processors/document/RtfProcessor.d.ts +152 -0
- package/dist/processors/document/RtfProcessor.js +361 -0
- package/dist/processors/document/WordProcessor.d.ts +168 -0
- package/dist/processors/document/WordProcessor.js +353 -0
- package/dist/processors/document/index.d.ts +54 -0
- package/dist/processors/document/index.js +90 -0
- package/dist/processors/errors/FileErrorCode.d.ts +98 -0
- package/dist/processors/errors/FileErrorCode.js +255 -0
- package/dist/processors/errors/errorHelpers.d.ts +151 -0
- package/dist/processors/errors/errorHelpers.js +378 -0
- package/dist/processors/errors/errorSerializer.d.ts +139 -0
- package/dist/processors/errors/errorSerializer.js +507 -0
- package/dist/processors/errors/index.d.ts +46 -0
- package/dist/processors/errors/index.js +49 -0
- package/dist/processors/index.d.ts +76 -0
- package/dist/processors/index.js +112 -0
- package/dist/processors/integration/FileProcessorIntegration.d.ts +244 -0
- package/dist/processors/integration/FileProcessorIntegration.js +272 -0
- package/dist/processors/integration/index.d.ts +42 -0
- package/dist/processors/integration/index.js +44 -0
- package/dist/processors/markup/HtmlProcessor.d.ts +169 -0
- package/dist/processors/markup/HtmlProcessor.js +249 -0
- package/dist/processors/markup/MarkdownProcessor.d.ts +165 -0
- package/dist/processors/markup/MarkdownProcessor.js +244 -0
- package/dist/processors/markup/SvgProcessor.d.ts +156 -0
- package/dist/processors/markup/SvgProcessor.js +240 -0
- package/dist/processors/markup/TextProcessor.d.ts +135 -0
- package/dist/processors/markup/TextProcessor.js +188 -0
- package/dist/processors/markup/index.d.ts +66 -0
- package/dist/processors/markup/index.js +102 -0
- package/dist/processors/registry/ProcessorRegistry.d.ts +334 -0
- package/dist/processors/registry/ProcessorRegistry.js +608 -0
- package/dist/processors/registry/index.d.ts +12 -0
- package/dist/processors/registry/index.js +16 -0
- package/dist/processors/registry/types.d.ts +53 -0
- package/dist/processors/registry/types.js +10 -0
- package/dist/providers/amazonBedrock.d.ts +15 -2
- package/dist/providers/amazonBedrock.js +65 -8
- package/dist/providers/anthropic.d.ts +3 -3
- package/dist/providers/anthropic.js +10 -7
- package/dist/providers/googleAiStudio.d.ts +5 -5
- package/dist/providers/googleAiStudio.js +10 -7
- package/dist/providers/googleVertex.d.ts +16 -4
- package/dist/providers/googleVertex.js +72 -16
- package/dist/providers/litellm.d.ts +3 -3
- package/dist/providers/litellm.js +10 -10
- package/dist/providers/mistral.d.ts +3 -3
- package/dist/providers/mistral.js +7 -6
- package/dist/providers/ollama.d.ts +3 -4
- package/dist/providers/ollama.js +7 -8
- package/dist/providers/openAI.d.ts +14 -2
- package/dist/providers/openAI.js +60 -6
- package/dist/providers/openRouter.d.ts +2 -2
- package/dist/providers/openRouter.js +10 -6
- package/dist/rag/ChunkerFactory.d.ts +91 -0
- package/dist/rag/ChunkerFactory.js +320 -0
- package/dist/rag/ChunkerRegistry.d.ts +91 -0
- package/dist/rag/ChunkerRegistry.js +421 -0
- package/dist/rag/chunkers/BaseChunker.d.ts +53 -0
- package/dist/rag/chunkers/BaseChunker.js +143 -0
- package/dist/rag/chunkers/CharacterChunker.d.ts +18 -0
- package/dist/rag/chunkers/CharacterChunker.js +28 -0
- package/dist/rag/chunkers/HTMLChunker.d.ts +19 -0
- package/dist/rag/chunkers/HTMLChunker.js +38 -0
- package/dist/rag/chunkers/JSONChunker.d.ts +19 -0
- package/dist/rag/chunkers/JSONChunker.js +68 -0
- package/dist/rag/chunkers/LaTeXChunker.d.ts +15 -0
- package/dist/rag/chunkers/LaTeXChunker.js +63 -0
- package/dist/rag/chunkers/MarkdownChunker.d.ts +15 -0
- package/dist/rag/chunkers/MarkdownChunker.js +102 -0
- package/dist/rag/chunkers/RecursiveChunker.d.ts +27 -0
- package/dist/rag/chunkers/RecursiveChunker.js +139 -0
- package/dist/rag/chunkers/SemanticMarkdownChunker.d.ts +22 -0
- package/dist/rag/chunkers/SemanticMarkdownChunker.js +138 -0
- package/dist/rag/chunkers/SentenceChunker.d.ts +19 -0
- package/dist/rag/chunkers/SentenceChunker.js +66 -0
- package/dist/rag/chunkers/TokenChunker.d.ts +19 -0
- package/dist/rag/chunkers/TokenChunker.js +61 -0
- package/dist/rag/chunkers/index.d.ts +15 -0
- package/dist/rag/chunkers/index.js +15 -0
- package/dist/rag/chunking/characterChunker.d.ts +16 -0
- package/dist/rag/chunking/characterChunker.js +142 -0
- package/dist/rag/chunking/chunkerRegistry.d.ts +67 -0
- package/dist/rag/chunking/chunkerRegistry.js +194 -0
- package/dist/rag/chunking/htmlChunker.d.ts +34 -0
- package/dist/rag/chunking/htmlChunker.js +247 -0
- package/dist/rag/chunking/index.d.ts +15 -0
- package/dist/rag/chunking/index.js +17 -0
- package/dist/rag/chunking/jsonChunker.d.ts +20 -0
- package/dist/rag/chunking/jsonChunker.js +281 -0
- package/dist/rag/chunking/latexChunker.d.ts +26 -0
- package/dist/rag/chunking/latexChunker.js +251 -0
- package/dist/rag/chunking/markdownChunker.d.ts +19 -0
- package/dist/rag/chunking/markdownChunker.js +201 -0
- package/dist/rag/chunking/recursiveChunker.d.ts +19 -0
- package/dist/rag/chunking/recursiveChunker.js +148 -0
- package/dist/rag/chunking/semanticChunker.d.ts +41 -0
- package/dist/rag/chunking/semanticChunker.js +306 -0
- package/dist/rag/chunking/sentenceChunker.d.ts +25 -0
- package/dist/rag/chunking/sentenceChunker.js +230 -0
- package/dist/rag/chunking/tokenChunker.d.ts +36 -0
- package/dist/rag/chunking/tokenChunker.js +183 -0
- package/dist/rag/document/MDocument.d.ts +198 -0
- package/dist/rag/document/MDocument.js +392 -0
- package/dist/rag/document/index.d.ts +5 -0
- package/dist/rag/document/index.js +5 -0
- package/dist/rag/document/loaders.d.ts +201 -0
- package/dist/rag/document/loaders.js +500 -0
- package/dist/rag/errors/RAGError.d.ts +244 -0
- package/dist/rag/errors/RAGError.js +274 -0
- package/dist/rag/errors/index.d.ts +6 -0
- package/dist/rag/errors/index.js +6 -0
- package/dist/rag/graphRag/graphRAG.d.ts +115 -0
- package/dist/rag/graphRag/graphRAG.js +384 -0
- package/dist/rag/graphRag/index.d.ts +4 -0
- package/dist/rag/graphRag/index.js +4 -0
- package/dist/rag/index.d.ts +103 -0
- package/dist/rag/index.js +141 -0
- package/dist/rag/metadata/MetadataExtractorFactory.d.ts +157 -0
- package/dist/rag/metadata/MetadataExtractorFactory.js +418 -0
- package/dist/rag/metadata/MetadataExtractorRegistry.d.ts +99 -0
- package/dist/rag/metadata/MetadataExtractorRegistry.js +362 -0
- package/dist/rag/metadata/index.d.ts +6 -0
- package/dist/rag/metadata/index.js +9 -0
- package/dist/rag/metadata/metadataExtractor.d.ts +69 -0
- package/dist/rag/metadata/metadataExtractor.js +277 -0
- package/dist/rag/pipeline/RAGPipeline.d.ts +235 -0
- package/dist/rag/pipeline/RAGPipeline.js +401 -0
- package/dist/rag/pipeline/contextAssembly.d.ts +126 -0
- package/dist/rag/pipeline/contextAssembly.js +337 -0
- package/dist/rag/pipeline/index.d.ts +5 -0
- package/dist/rag/pipeline/index.js +5 -0
- package/dist/rag/ragIntegration.d.ts +38 -0
- package/dist/rag/ragIntegration.js +211 -0
- package/dist/rag/reranker/RerankerFactory.d.ts +184 -0
- package/dist/rag/reranker/RerankerFactory.js +430 -0
- package/dist/rag/reranker/RerankerRegistry.d.ts +119 -0
- package/dist/rag/reranker/RerankerRegistry.js +402 -0
- package/dist/rag/reranker/index.d.ts +6 -0
- package/dist/rag/reranker/index.js +9 -0
- package/dist/rag/reranker/reranker.d.ts +71 -0
- package/dist/rag/reranker/reranker.js +277 -0
- package/dist/rag/resilience/CircuitBreaker.d.ts +215 -0
- package/dist/rag/resilience/CircuitBreaker.js +431 -0
- package/dist/rag/resilience/RetryHandler.d.ts +115 -0
- package/dist/rag/resilience/RetryHandler.js +300 -0
- package/dist/rag/resilience/index.d.ts +7 -0
- package/dist/rag/resilience/index.js +7 -0
- package/dist/rag/retrieval/hybridSearch.d.ts +94 -0
- package/dist/rag/retrieval/hybridSearch.js +313 -0
- package/dist/rag/retrieval/index.d.ts +5 -0
- package/dist/rag/retrieval/index.js +5 -0
- package/dist/rag/retrieval/vectorQueryTool.d.ts +93 -0
- package/dist/rag/retrieval/vectorQueryTool.js +289 -0
- package/dist/rag/types.d.ts +768 -0
- package/dist/rag/types.js +8 -0
- package/dist/server/index.d.ts +15 -11
- package/dist/server/index.js +55 -51
- package/dist/server/utils/validation.d.ts +8 -8
- package/dist/types/common.d.ts +0 -1
- package/dist/types/fileTypes.d.ts +1 -1
- package/dist/types/generateTypes.d.ts +42 -8
- package/dist/types/generateTypes.js +1 -1
- package/dist/types/index.d.ts +25 -24
- package/dist/types/index.js +21 -20
- package/dist/types/modelTypes.d.ts +10 -10
- package/dist/types/pptTypes.d.ts +14 -2
- package/dist/types/pptTypes.js +16 -0
- package/dist/types/streamTypes.d.ts +28 -8
- package/dist/types/streamTypes.js +1 -1
- package/dist/utils/async/delay.d.ts +40 -0
- package/dist/utils/async/delay.js +42 -0
- package/dist/utils/async/index.d.ts +23 -0
- package/dist/utils/async/index.js +23 -0
- package/dist/utils/async/retry.d.ts +141 -0
- package/dist/utils/async/retry.js +171 -0
- package/dist/utils/async/withTimeout.d.ts +73 -0
- package/dist/utils/async/withTimeout.js +96 -0
- package/dist/utils/fileDetector.d.ts +7 -1
- package/dist/utils/fileDetector.js +91 -18
- package/dist/utils/json/extract.d.ts +103 -0
- package/dist/utils/json/extract.js +248 -0
- package/dist/utils/json/index.d.ts +36 -0
- package/dist/utils/json/index.js +36 -0
- package/dist/utils/json/safeParse.d.ts +137 -0
- package/dist/utils/json/safeParse.js +190 -0
- package/dist/utils/messageBuilder.d.ts +2 -2
- package/dist/utils/messageBuilder.js +15 -7
- package/dist/utils/modelRouter.d.ts +4 -4
- package/dist/utils/modelRouter.js +4 -4
- package/dist/utils/sanitizers/filename.d.ts +137 -0
- package/dist/utils/sanitizers/filename.js +365 -0
- package/dist/utils/sanitizers/html.d.ts +170 -0
- package/dist/utils/sanitizers/html.js +325 -0
- package/dist/utils/sanitizers/index.d.ts +26 -0
- package/dist/utils/sanitizers/index.js +29 -0
- package/dist/utils/sanitizers/svg.d.ts +81 -0
- package/dist/utils/sanitizers/svg.js +482 -0
- package/package.json +2 -2
|
@@ -0,0 +1,366 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Filename and Display Name Sanitization Utilities
|
|
3
|
+
* Prevents path traversal attacks and filesystem issues
|
|
4
|
+
*
|
|
5
|
+
* This module provides:
|
|
6
|
+
* - Filename sanitization for safe filesystem storage
|
|
7
|
+
* - Display name sanitization for user-facing content
|
|
8
|
+
* - Path traversal prevention
|
|
9
|
+
*
|
|
10
|
+
* @see https://cheatsheetseries.owasp.org/cheatsheets/Input_Validation_Cheat_Sheet.html
|
|
11
|
+
*/
|
|
12
|
+
/**
|
|
13
|
+
* Characters that are invalid in filenames on various operating systems.
|
|
14
|
+
* Windows is the most restrictive, so we use its rules as the baseline.
|
|
15
|
+
* Matches: < > : " / \ | ? * and control characters (ASCII 0-31)
|
|
16
|
+
*/
|
|
17
|
+
const INVALID_FILENAME_PATTERN = '[<>:"/\\\\|?*]';
|
|
18
|
+
/**
|
|
19
|
+
* Check if a character code is a control character (0-31)
|
|
20
|
+
*/
|
|
21
|
+
function isControlChar(charCode) {
|
|
22
|
+
return charCode >= 0 && charCode <= 31;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Remove control characters from a string
|
|
26
|
+
*/
|
|
27
|
+
function removeControlChars(str) {
|
|
28
|
+
let result = "";
|
|
29
|
+
for (let i = 0; i < str.length; i++) {
|
|
30
|
+
const charCode = str.charCodeAt(i);
|
|
31
|
+
if (!isControlChar(charCode) && charCode !== 127) {
|
|
32
|
+
result += str[i];
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return result;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Reserved filenames on Windows that cannot be used.
|
|
39
|
+
*/
|
|
40
|
+
const WINDOWS_RESERVED_NAMES = new Set([
|
|
41
|
+
"CON",
|
|
42
|
+
"PRN",
|
|
43
|
+
"AUX",
|
|
44
|
+
"NUL",
|
|
45
|
+
"COM1",
|
|
46
|
+
"COM2",
|
|
47
|
+
"COM3",
|
|
48
|
+
"COM4",
|
|
49
|
+
"COM5",
|
|
50
|
+
"COM6",
|
|
51
|
+
"COM7",
|
|
52
|
+
"COM8",
|
|
53
|
+
"COM9",
|
|
54
|
+
"LPT1",
|
|
55
|
+
"LPT2",
|
|
56
|
+
"LPT3",
|
|
57
|
+
"LPT4",
|
|
58
|
+
"LPT5",
|
|
59
|
+
"LPT6",
|
|
60
|
+
"LPT7",
|
|
61
|
+
"LPT8",
|
|
62
|
+
"LPT9",
|
|
63
|
+
]);
|
|
64
|
+
/**
|
|
65
|
+
* Dangerous file extensions that should be blocked.
|
|
66
|
+
*/
|
|
67
|
+
const DANGEROUS_EXTENSIONS = new Set([
|
|
68
|
+
".exe",
|
|
69
|
+
".dll",
|
|
70
|
+
".bat",
|
|
71
|
+
".cmd",
|
|
72
|
+
".sh",
|
|
73
|
+
".ps1",
|
|
74
|
+
".vbs",
|
|
75
|
+
".vbe",
|
|
76
|
+
".js",
|
|
77
|
+
".jse",
|
|
78
|
+
".ws",
|
|
79
|
+
".wsf",
|
|
80
|
+
".wsc",
|
|
81
|
+
".wsh",
|
|
82
|
+
".msc",
|
|
83
|
+
".scr",
|
|
84
|
+
".pif",
|
|
85
|
+
".com",
|
|
86
|
+
".hta",
|
|
87
|
+
".cpl",
|
|
88
|
+
".msi",
|
|
89
|
+
".msp",
|
|
90
|
+
".jar",
|
|
91
|
+
]);
|
|
92
|
+
/**
|
|
93
|
+
* Sanitize a filename for safe filesystem storage.
|
|
94
|
+
* Removes characters that are invalid on various operating systems.
|
|
95
|
+
*
|
|
96
|
+
* @param filename - Raw filename to sanitize
|
|
97
|
+
* @param options - Sanitization options
|
|
98
|
+
* @returns Safe filename
|
|
99
|
+
* @throws Error if filename is empty after sanitization
|
|
100
|
+
*
|
|
101
|
+
* @example
|
|
102
|
+
* sanitizeFileName('my:file<name>.txt');
|
|
103
|
+
* // Returns: 'my_file_name_.txt'
|
|
104
|
+
*
|
|
105
|
+
* @example
|
|
106
|
+
* sanitizeFileName('../../../etc/passwd');
|
|
107
|
+
* // Returns: '______etc_passwd'
|
|
108
|
+
*
|
|
109
|
+
* @example
|
|
110
|
+
* sanitizeFileName('malware.exe', { blockDangerousExtensions: true });
|
|
111
|
+
* // Throws: Error - dangerous extension
|
|
112
|
+
*/
|
|
113
|
+
export function sanitizeFileName(filename, options = {}) {
|
|
114
|
+
const { maxLength = 255, replacement = "_", blockDangerousExtensions = true, allowHiddenFiles = false, } = options;
|
|
115
|
+
if (!filename || typeof filename !== "string") {
|
|
116
|
+
throw new Error("Filename is required and must be a string");
|
|
117
|
+
}
|
|
118
|
+
let sanitized = filename.trim();
|
|
119
|
+
// Block path traversal attempts
|
|
120
|
+
if (sanitized.includes("..")) {
|
|
121
|
+
sanitized = sanitized.replace(/\.\./g, replacement + replacement);
|
|
122
|
+
}
|
|
123
|
+
// Remove path separators
|
|
124
|
+
sanitized = sanitized.replace(/[/\\]/g, replacement);
|
|
125
|
+
// Replace invalid characters and remove control characters
|
|
126
|
+
sanitized = sanitized.replace(new RegExp(INVALID_FILENAME_PATTERN, "g"), replacement);
|
|
127
|
+
sanitized = removeControlChars(sanitized);
|
|
128
|
+
// Collapse multiple replacement characters
|
|
129
|
+
const escapedReplacement = replacement.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
130
|
+
sanitized = sanitized.replace(new RegExp(`${escapedReplacement}+`, "g"), replacement);
|
|
131
|
+
// Collapse multiple dots
|
|
132
|
+
sanitized = sanitized.replace(/\.{2,}/g, ".");
|
|
133
|
+
// Handle hidden files (files starting with dot)
|
|
134
|
+
if (!allowHiddenFiles && sanitized.startsWith(".")) {
|
|
135
|
+
sanitized = replacement + sanitized.substring(1);
|
|
136
|
+
}
|
|
137
|
+
// Don't end with a dot or space (Windows limitation)
|
|
138
|
+
sanitized = sanitized.replace(/[. ]+$/, "");
|
|
139
|
+
// Check for Windows reserved names
|
|
140
|
+
const nameWithoutExt = sanitized.split(".")[0].toUpperCase();
|
|
141
|
+
if (WINDOWS_RESERVED_NAMES.has(nameWithoutExt)) {
|
|
142
|
+
sanitized = replacement + sanitized;
|
|
143
|
+
}
|
|
144
|
+
// Check for dangerous extensions
|
|
145
|
+
if (blockDangerousExtensions) {
|
|
146
|
+
const lowerFilename = sanitized.toLowerCase();
|
|
147
|
+
const dangerousExtArray = Array.from(DANGEROUS_EXTENSIONS);
|
|
148
|
+
for (let i = 0; i < dangerousExtArray.length; i++) {
|
|
149
|
+
const ext = dangerousExtArray[i];
|
|
150
|
+
if (lowerFilename.endsWith(ext)) {
|
|
151
|
+
throw new Error(`Filename has dangerous extension: ${ext}`);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
// Limit length
|
|
156
|
+
if (sanitized.length > maxLength) {
|
|
157
|
+
// Try to preserve extension
|
|
158
|
+
const lastDot = sanitized.lastIndexOf(".");
|
|
159
|
+
if (lastDot > 0 && lastDot > sanitized.length - 10) {
|
|
160
|
+
const ext = sanitized.substring(lastDot);
|
|
161
|
+
const name = sanitized.substring(0, maxLength - ext.length);
|
|
162
|
+
sanitized = name + ext;
|
|
163
|
+
}
|
|
164
|
+
else {
|
|
165
|
+
sanitized = sanitized.substring(0, maxLength);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
// Ensure we have a valid filename
|
|
169
|
+
if (!sanitized || sanitized === replacement) {
|
|
170
|
+
throw new Error("Filename is empty after sanitization");
|
|
171
|
+
}
|
|
172
|
+
return sanitized;
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Sanitize a display name for safe user-facing display.
|
|
176
|
+
* Removes control characters and limits length.
|
|
177
|
+
*
|
|
178
|
+
* @param name - Raw display name to sanitize
|
|
179
|
+
* @param options - Sanitization options
|
|
180
|
+
* @returns Safe display name
|
|
181
|
+
*
|
|
182
|
+
* @example
|
|
183
|
+
* sanitizeDisplayName(' John\x00Doe ');
|
|
184
|
+
* // Returns: 'John Doe'
|
|
185
|
+
*
|
|
186
|
+
* @example
|
|
187
|
+
* sanitizeDisplayName('User<script>alert(1)</script>');
|
|
188
|
+
* // Returns: 'User'
|
|
189
|
+
*/
|
|
190
|
+
export function sanitizeDisplayName(name, options = {}) {
|
|
191
|
+
const { maxLength = 100, allowUnicode = true } = options;
|
|
192
|
+
if (!name || typeof name !== "string") {
|
|
193
|
+
return "";
|
|
194
|
+
}
|
|
195
|
+
let sanitized = name;
|
|
196
|
+
// Remove control characters (ASCII 0-31 and 127)
|
|
197
|
+
sanitized = removeControlChars(sanitized);
|
|
198
|
+
// Remove HTML tags iteratively to prevent nested tag bypass
|
|
199
|
+
// e.g., "<scr<script>ipt>" after one pass becomes "<script>"
|
|
200
|
+
let previousSanitized;
|
|
201
|
+
do {
|
|
202
|
+
previousSanitized = sanitized;
|
|
203
|
+
sanitized = sanitized.replace(/<[^>]*>/g, "");
|
|
204
|
+
} while (sanitized !== previousSanitized);
|
|
205
|
+
// If not allowing unicode, remove non-ASCII characters
|
|
206
|
+
if (!allowUnicode) {
|
|
207
|
+
// Keep only printable ASCII (space through tilde)
|
|
208
|
+
sanitized = sanitized
|
|
209
|
+
.split("")
|
|
210
|
+
.filter((char) => {
|
|
211
|
+
const code = char.charCodeAt(0);
|
|
212
|
+
return code >= 32 && code <= 126;
|
|
213
|
+
})
|
|
214
|
+
.join("");
|
|
215
|
+
}
|
|
216
|
+
// Normalize whitespace
|
|
217
|
+
sanitized = sanitized.replace(/\s+/g, " ").trim();
|
|
218
|
+
// Limit length
|
|
219
|
+
if (sanitized.length > maxLength) {
|
|
220
|
+
sanitized = sanitized.substring(0, maxLength).trim();
|
|
221
|
+
}
|
|
222
|
+
return sanitized;
|
|
223
|
+
}
|
|
224
|
+
/**
|
|
225
|
+
* Validate a display name strictly.
|
|
226
|
+
* Only allows alphanumeric, spaces, and basic punctuation.
|
|
227
|
+
*
|
|
228
|
+
* @param name - Display name to validate
|
|
229
|
+
* @returns true if valid, false otherwise
|
|
230
|
+
*
|
|
231
|
+
* @example
|
|
232
|
+
* isValidDisplayName('John Doe'); // true
|
|
233
|
+
* isValidDisplayName('John<Doe'); // false
|
|
234
|
+
*/
|
|
235
|
+
export function isValidDisplayName(name) {
|
|
236
|
+
if (!name || typeof name !== "string") {
|
|
237
|
+
return false;
|
|
238
|
+
}
|
|
239
|
+
const trimmed = name.trim();
|
|
240
|
+
// Allow: letters, numbers, spaces, periods, hyphens, underscores, apostrophes
|
|
241
|
+
return /^[a-zA-Z0-9 ._'-]{1,100}$/.test(trimmed);
|
|
242
|
+
}
|
|
243
|
+
/**
|
|
244
|
+
* Validate a filename strictly.
|
|
245
|
+
* Only allows alphanumeric, dash, underscore, and period.
|
|
246
|
+
*
|
|
247
|
+
* @param filename - Filename to validate
|
|
248
|
+
* @returns true if valid, false otherwise
|
|
249
|
+
*
|
|
250
|
+
* @example
|
|
251
|
+
* isValidFileName('my-file.txt'); // true
|
|
252
|
+
* isValidFileName('../passwd'); // false
|
|
253
|
+
*/
|
|
254
|
+
export function isValidFileName(filename) {
|
|
255
|
+
if (!filename || typeof filename !== "string") {
|
|
256
|
+
return false;
|
|
257
|
+
}
|
|
258
|
+
const trimmed = filename.trim();
|
|
259
|
+
// Block path traversal
|
|
260
|
+
if (trimmed.includes("..") ||
|
|
261
|
+
trimmed.includes("/") ||
|
|
262
|
+
trimmed.includes("\\")) {
|
|
263
|
+
return false;
|
|
264
|
+
}
|
|
265
|
+
// Allow only safe characters
|
|
266
|
+
if (!/^[a-zA-Z0-9._-]{1,255}$/.test(trimmed)) {
|
|
267
|
+
return false;
|
|
268
|
+
}
|
|
269
|
+
// Block dangerous extensions
|
|
270
|
+
const lowerFilename = trimmed.toLowerCase();
|
|
271
|
+
const dangerousExtArray = Array.from(DANGEROUS_EXTENSIONS);
|
|
272
|
+
for (let i = 0; i < dangerousExtArray.length; i++) {
|
|
273
|
+
if (lowerFilename.endsWith(dangerousExtArray[i])) {
|
|
274
|
+
return false;
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
return true;
|
|
278
|
+
}
|
|
279
|
+
/**
|
|
280
|
+
* Extract and sanitize the extension from a filename.
|
|
281
|
+
*
|
|
282
|
+
* @param filename - Filename to extract extension from
|
|
283
|
+
* @returns Lowercase extension including the dot, or empty string
|
|
284
|
+
*
|
|
285
|
+
* @example
|
|
286
|
+
* getFileExtension('document.PDF'); // '.pdf'
|
|
287
|
+
* getFileExtension('noextension'); // ''
|
|
288
|
+
*/
|
|
289
|
+
export function getFileExtension(filename) {
|
|
290
|
+
if (!filename || typeof filename !== "string") {
|
|
291
|
+
return "";
|
|
292
|
+
}
|
|
293
|
+
const lastDot = filename.lastIndexOf(".");
|
|
294
|
+
if (lastDot < 1 || lastDot === filename.length - 1) {
|
|
295
|
+
return "";
|
|
296
|
+
}
|
|
297
|
+
const ext = filename.substring(lastDot).toLowerCase();
|
|
298
|
+
// Validate extension contains only alphanumeric
|
|
299
|
+
if (!/^\.[a-z0-9]+$/.test(ext)) {
|
|
300
|
+
return "";
|
|
301
|
+
}
|
|
302
|
+
return ext;
|
|
303
|
+
}
|
|
304
|
+
/**
|
|
305
|
+
* Check if a file extension is considered dangerous.
|
|
306
|
+
*
|
|
307
|
+
* @param extension - File extension to check (with or without leading dot)
|
|
308
|
+
* @returns true if extension is dangerous
|
|
309
|
+
*
|
|
310
|
+
* @example
|
|
311
|
+
* isDangerousExtension('.exe'); // true
|
|
312
|
+
* isDangerousExtension('pdf'); // false
|
|
313
|
+
*/
|
|
314
|
+
export function isDangerousExtension(extension) {
|
|
315
|
+
if (!extension || typeof extension !== "string") {
|
|
316
|
+
return false;
|
|
317
|
+
}
|
|
318
|
+
const normalized = extension.startsWith(".")
|
|
319
|
+
? extension.toLowerCase()
|
|
320
|
+
: `.${extension.toLowerCase()}`;
|
|
321
|
+
return DANGEROUS_EXTENSIONS.has(normalized);
|
|
322
|
+
}
|
|
323
|
+
/**
|
|
324
|
+
* Generate a safe filename from arbitrary input.
|
|
325
|
+
* Creates a valid filename even from completely invalid input.
|
|
326
|
+
*
|
|
327
|
+
* @param input - Any string input
|
|
328
|
+
* @param defaultName - Default name if input sanitizes to empty (default: 'file')
|
|
329
|
+
* @param extension - Optional extension to append
|
|
330
|
+
* @returns Safe filename
|
|
331
|
+
*
|
|
332
|
+
* @example
|
|
333
|
+
* generateSafeFileName('My Document!@#$'); // 'My_Document_'
|
|
334
|
+
* generateSafeFileName('', 'untitled', '.txt'); // 'untitled.txt'
|
|
335
|
+
*/
|
|
336
|
+
export function generateSafeFileName(input, defaultName = "file", extension) {
|
|
337
|
+
let sanitized;
|
|
338
|
+
try {
|
|
339
|
+
sanitized = sanitizeFileName(input || defaultName, {
|
|
340
|
+
blockDangerousExtensions: false,
|
|
341
|
+
});
|
|
342
|
+
}
|
|
343
|
+
catch {
|
|
344
|
+
sanitized = defaultName;
|
|
345
|
+
}
|
|
346
|
+
if (extension) {
|
|
347
|
+
const normalizedExt = extension.startsWith(".")
|
|
348
|
+
? extension.toLowerCase()
|
|
349
|
+
: `.${extension.toLowerCase()}`;
|
|
350
|
+
// Remove existing extension if present
|
|
351
|
+
const lastDot = sanitized.lastIndexOf(".");
|
|
352
|
+
if (lastDot > 0) {
|
|
353
|
+
sanitized = sanitized.substring(0, lastDot);
|
|
354
|
+
}
|
|
355
|
+
sanitized += normalizedExt;
|
|
356
|
+
}
|
|
357
|
+
return sanitized;
|
|
358
|
+
}
|
|
359
|
+
/**
|
|
360
|
+
* Get the list of dangerous file extensions.
|
|
361
|
+
* Useful for validation UI or documentation.
|
|
362
|
+
*/
|
|
363
|
+
export function getDangerousExtensions() {
|
|
364
|
+
return Array.from(DANGEROUS_EXTENSIONS);
|
|
365
|
+
}
|
|
366
|
+
//# sourceMappingURL=filename.js.map
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* HTML/XSS Sanitization Utilities
|
|
3
|
+
* Context-aware output escaping following OWASP guidelines
|
|
4
|
+
*
|
|
5
|
+
* This module provides:
|
|
6
|
+
* - HTML entity escaping for safe display
|
|
7
|
+
* - JavaScript string escaping for embedding in scripts
|
|
8
|
+
* - URL escaping for query parameters
|
|
9
|
+
* - JSON string sanitization
|
|
10
|
+
*
|
|
11
|
+
* Pure TypeScript implementation with no external dependencies.
|
|
12
|
+
*
|
|
13
|
+
* @see https://cheatsheetseries.owasp.org/cheatsheets/Cross_Site_Scripting_Prevention_Cheat_Sheet.html
|
|
14
|
+
*/
|
|
15
|
+
/**
|
|
16
|
+
* Escape HTML special characters for safe insertion into HTML context.
|
|
17
|
+
* Use this when you need to display user text as plain text (not HTML).
|
|
18
|
+
*
|
|
19
|
+
* OWASP Rule 1: HTML Encode Before Inserting Untrusted Data into HTML Element Content
|
|
20
|
+
*
|
|
21
|
+
* @param text - Raw text to escape
|
|
22
|
+
* @returns HTML-escaped text safe for insertion into HTML
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* const userName = '<script>alert(1)</script>';
|
|
26
|
+
* const safe = escapeHtml(userName);
|
|
27
|
+
* // Returns: '<script>alert(1)</script>'
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* // Safe to use in HTML
|
|
31
|
+
* const html = `<div>${escapeHtml(userInput)}</div>`;
|
|
32
|
+
*/
|
|
33
|
+
export declare function escapeHtml(text: string): string;
|
|
34
|
+
/**
|
|
35
|
+
* Unescape HTML entities back to their original characters.
|
|
36
|
+
* Use with caution - only on trusted content.
|
|
37
|
+
*
|
|
38
|
+
* @param text - HTML-escaped text
|
|
39
|
+
* @returns Unescaped text
|
|
40
|
+
*
|
|
41
|
+
* @example
|
|
42
|
+
* const escaped = '<div>Hello</div>';
|
|
43
|
+
* const original = unescapeHtml(escaped);
|
|
44
|
+
* // Returns: '<div>Hello</div>'
|
|
45
|
+
*/
|
|
46
|
+
export declare function unescapeHtml(text: string): string;
|
|
47
|
+
/**
|
|
48
|
+
* Escape text for safe insertion into JavaScript string literals.
|
|
49
|
+
* Use when embedding user data in inline JavaScript.
|
|
50
|
+
*
|
|
51
|
+
* OWASP Rule 3: JavaScript Encode Before Inserting Untrusted Data into JavaScript Data Values
|
|
52
|
+
*
|
|
53
|
+
* @param text - Raw text to escape
|
|
54
|
+
* @returns JavaScript-escaped text safe for string literals
|
|
55
|
+
*
|
|
56
|
+
* @example
|
|
57
|
+
* const userInput = "Hello\nWorld";
|
|
58
|
+
* const safe = escapeJavaScript(userInput);
|
|
59
|
+
* // Returns: 'Hello\\nWorld'
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
62
|
+
* // Safe to use in inline script
|
|
63
|
+
* const script = `const name = '${escapeJavaScript(userName)}';`;
|
|
64
|
+
*/
|
|
65
|
+
export declare function escapeJavaScript(text: string): string;
|
|
66
|
+
/**
|
|
67
|
+
* Escape text for safe insertion into URLs.
|
|
68
|
+
* Use for query parameter values.
|
|
69
|
+
*
|
|
70
|
+
* OWASP Rule 5: URL Encode Before Inserting Untrusted Data into URL Parameter Values
|
|
71
|
+
*
|
|
72
|
+
* @param text - Raw text to escape
|
|
73
|
+
* @returns URL-encoded text safe for query parameters
|
|
74
|
+
*
|
|
75
|
+
* @example
|
|
76
|
+
* const query = 'hello world&foo=bar';
|
|
77
|
+
* const safe = escapeUrl(query);
|
|
78
|
+
* // Returns: 'hello%20world%26foo%3Dbar'
|
|
79
|
+
*
|
|
80
|
+
* @example
|
|
81
|
+
* // Safe to use in URL
|
|
82
|
+
* const url = `https://example.com/search?q=${escapeUrl(userQuery)}`;
|
|
83
|
+
*/
|
|
84
|
+
export declare function escapeUrl(text: string): string;
|
|
85
|
+
/**
|
|
86
|
+
* Decode URL-encoded text.
|
|
87
|
+
*
|
|
88
|
+
* @param text - URL-encoded text
|
|
89
|
+
* @returns Decoded text
|
|
90
|
+
*
|
|
91
|
+
* @example
|
|
92
|
+
* const encoded = 'hello%20world';
|
|
93
|
+
* const decoded = decodeUrl(encoded);
|
|
94
|
+
* // Returns: 'hello world'
|
|
95
|
+
*/
|
|
96
|
+
export declare function decodeUrl(text: string): string;
|
|
97
|
+
/**
|
|
98
|
+
* Sanitize JSON string value to prevent injection attacks.
|
|
99
|
+
* Ensures string can be safely used in JSON without breaking structure.
|
|
100
|
+
*
|
|
101
|
+
* @param value - Raw string value
|
|
102
|
+
* @returns Escaped string safe for JSON values
|
|
103
|
+
*
|
|
104
|
+
* @example
|
|
105
|
+
* const userInput = 'Hello\n"World"';
|
|
106
|
+
* const safe = sanitizeJsonString(userInput);
|
|
107
|
+
* // Returns: 'Hello\\n\\"World\\"'
|
|
108
|
+
*/
|
|
109
|
+
export declare function sanitizeJsonString(value: string): string;
|
|
110
|
+
/**
|
|
111
|
+
* Escape text for safe insertion into CSS context.
|
|
112
|
+
* Use when embedding user data in style attributes or stylesheets.
|
|
113
|
+
*
|
|
114
|
+
* OWASP Rule 4: CSS Encode And Strictly Validate Before Inserting Untrusted Data into HTML Style Property Values
|
|
115
|
+
*
|
|
116
|
+
* @param text - Raw text to escape
|
|
117
|
+
* @returns CSS-escaped text
|
|
118
|
+
*
|
|
119
|
+
* @example
|
|
120
|
+
* const userColor = 'red; background: url(evil.com)';
|
|
121
|
+
* const safe = escapeCss(userColor);
|
|
122
|
+
* // Escapes dangerous characters
|
|
123
|
+
*/
|
|
124
|
+
export declare function escapeCss(text: string): string;
|
|
125
|
+
/**
|
|
126
|
+
* Strip all HTML tags from content, leaving only text.
|
|
127
|
+
* Useful for extracting plain text from HTML.
|
|
128
|
+
*
|
|
129
|
+
* @param html - HTML content
|
|
130
|
+
* @returns Plain text with all tags removed
|
|
131
|
+
*
|
|
132
|
+
* @example
|
|
133
|
+
* const html = '<p>Hello <b>World</b></p>';
|
|
134
|
+
* const text = stripHtmlTags(html);
|
|
135
|
+
* // Returns: 'Hello World'
|
|
136
|
+
*/
|
|
137
|
+
export declare function stripHtmlTags(html: string): string;
|
|
138
|
+
/**
|
|
139
|
+
* Escape text for safe use in XML/XHTML context.
|
|
140
|
+
* Similar to HTML escaping but uses XML numeric entities.
|
|
141
|
+
*
|
|
142
|
+
* @param text - Raw text to escape
|
|
143
|
+
* @returns XML-escaped text
|
|
144
|
+
*/
|
|
145
|
+
export declare function escapeXml(text: string): string;
|
|
146
|
+
/**
|
|
147
|
+
* Sanitize content for safe inclusion in HTML attributes.
|
|
148
|
+
* More aggressive than escapeHtml - also handles newlines and tabs.
|
|
149
|
+
*
|
|
150
|
+
* @param value - Attribute value to sanitize
|
|
151
|
+
* @returns Sanitized attribute value
|
|
152
|
+
*
|
|
153
|
+
* @example
|
|
154
|
+
* const attr = 'value" onclick="alert(1)';
|
|
155
|
+
* const safe = sanitizeHtmlAttribute(attr);
|
|
156
|
+
* // Returns: 'value" onclick="alert(1)'
|
|
157
|
+
*/
|
|
158
|
+
export declare function sanitizeHtmlAttribute(value: string): string;
|
|
159
|
+
/**
|
|
160
|
+
* Check if a string contains potentially dangerous HTML content.
|
|
161
|
+
* Does NOT sanitize - use other functions for that.
|
|
162
|
+
*
|
|
163
|
+
* @param text - Text to check
|
|
164
|
+
* @returns true if text contains dangerous patterns
|
|
165
|
+
*
|
|
166
|
+
* @example
|
|
167
|
+
* containsDangerousHtml('<script>alert(1)</script>'); // true
|
|
168
|
+
* containsDangerousHtml('Hello World'); // false
|
|
169
|
+
*/
|
|
170
|
+
export declare function containsDangerousHtml(text: string): boolean;
|