@langchain/core 1.1.20 → 1.1.22

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.
@@ -1 +1 @@
1
- {"version":3,"file":"openai.js","names":["message: AIMessage","blocks: Array<ContentBlock.Standard>","blocks: Array<ContentBlock>","convertedBlocks: Array<ContentBlock.Standard>","annotation: ContentBlock","message: AIMessageChunk","ChatOpenAITranslator: StandardContentBlockTranslator"],"sources":["../../../src/messages/block_translators/openai.ts"],"sourcesContent":["import type { ContentBlock } from \"../content/index.js\";\nimport type { AIMessageChunk, AIMessage } from \"../ai.js\";\nimport type { StandardContentBlockTranslator } from \"./index.js\";\nimport { convertToV1FromOpenAIDataBlock, isOpenAIDataBlock } from \"./data.js\";\nimport {\n _isArray,\n _isContentBlock,\n _isObject,\n _isString,\n iife,\n} from \"./utils.js\";\n\n/**\n * Converts a ChatOpenAICompletions message to an array of v1 standard content blocks.\n *\n * This function processes an AI message from ChatOpenAICompletions API format\n * and converts it to the standardized v1 content block format. It handles both\n * string content and structured content blocks, as well as tool calls.\n *\n * @param message - The AI message containing ChatOpenAICompletions formatted content\n * @returns Array of content blocks in v1 standard format\n *\n * @example\n * ```typescript\n * const message = new AIMessage(\"Hello world\");\n * const standardBlocks = convertToV1FromChatCompletions(message);\n * // Returns: [{ type: \"text\", text: \"Hello world\" }]\n * ```\n *\n * @example\n * ```typescript\n * const message = new AIMessage([\n * { type: \"text\", text: \"Hello\" },\n * { type: \"image_url\", image_url: { url: \"https://example.com/image.png\" } }\n * ]);\n * message.tool_calls = [\n * { id: \"call_123\", name: \"calculator\", args: { a: 1, b: 2 } }\n * ];\n *\n * const standardBlocks = convertToV1FromChatCompletions(message);\n * // Returns:\n * // [\n * // { type: \"text\", text: \"Hello\" },\n * // { type: \"image\", url: \"https://example.com/image.png\" },\n * // { type: \"tool_call\", id: \"call_123\", name: \"calculator\", args: { a: 1, b: 2 } }\n * // ]\n * ```\n */\nexport function convertToV1FromChatCompletions(\n message: AIMessage\n): Array<ContentBlock.Standard> {\n const blocks: Array<ContentBlock.Standard> = [];\n if (typeof message.content === \"string\") {\n blocks.push({\n type: \"text\",\n text: message.content,\n });\n } else {\n blocks.push(...convertToV1FromChatCompletionsInput(message.content));\n }\n for (const toolCall of message.tool_calls ?? []) {\n blocks.push({\n type: \"tool_call\",\n id: toolCall.id,\n name: toolCall.name,\n args: toolCall.args,\n });\n }\n return blocks;\n}\n\n/**\n * Converts a ChatOpenAICompletions message chunk to an array of v1 standard content blocks.\n *\n * This function processes an AI message chunk from OpenAI's chat completions API and converts\n * it to the standardized v1 content block format. It handles both string and array content,\n * as well as tool calls that may be present in the chunk.\n *\n * @param message - The AI message chunk containing OpenAI-formatted content blocks\n * @returns Array of content blocks in v1 standard format\n *\n * @example\n * ```typescript\n * const chunk = new AIMessage(\"Hello\");\n * const standardBlocks = convertToV1FromChatCompletionsChunk(chunk);\n * // Returns: [{ type: \"text\", text: \"Hello\" }]\n * ```\n *\n * @example\n * ```typescript\n * const chunk = new AIMessage([\n * { type: \"text\", text: \"Processing...\" }\n * ]);\n * chunk.tool_calls = [\n * { id: \"call_456\", name: \"search\", args: { query: \"test\" } }\n * ];\n *\n * const standardBlocks = convertToV1FromChatCompletionsChunk(chunk);\n * // Returns:\n * // [\n * // { type: \"text\", text: \"Processing...\" },\n * // { type: \"tool_call\", id: \"call_456\", name: \"search\", args: { query: \"test\" } }\n * // ]\n * ```\n */\nexport function convertToV1FromChatCompletionsChunk(\n message: AIMessage\n): Array<ContentBlock.Standard> {\n const blocks: Array<ContentBlock.Standard> = [];\n if (typeof message.content === \"string\") {\n blocks.push({\n type: \"text\",\n text: message.content,\n });\n } else {\n blocks.push(...convertToV1FromChatCompletionsInput(message.content));\n }\n\n // TODO: parse chunk position information\n for (const toolCall of message.tool_calls ?? []) {\n blocks.push({\n type: \"tool_call\",\n id: toolCall.id,\n name: toolCall.name,\n args: toolCall.args,\n });\n }\n return blocks;\n}\n\n/**\n * Converts an array of ChatOpenAICompletions content blocks to v1 standard content blocks.\n *\n * This function processes content blocks from OpenAI's Chat Completions API format\n * and converts them to the standardized v1 content block format. It handles both\n * OpenAI-specific data blocks (which require conversion) and standard blocks\n * (which are passed through with type assertion).\n *\n * @param blocks - Array of content blocks in ChatOpenAICompletions format\n * @returns Array of content blocks in v1 standard format\n *\n * @example\n * ```typescript\n * const openaiBlocks = [\n * { type: \"text\", text: \"Hello world\" },\n * { type: \"image_url\", image_url: { url: \"https://example.com/image.png\" } }\n * ];\n *\n * const standardBlocks = convertToV1FromChatCompletionsInput(openaiBlocks);\n * // Returns:\n * // [\n * // { type: \"text\", text: \"Hello world\" },\n * // { type: \"image\", url: \"https://example.com/image.png\" }\n * // ]\n * ```\n */\nexport function convertToV1FromChatCompletionsInput(\n blocks: Array<ContentBlock>\n): Array<ContentBlock.Standard> {\n const convertedBlocks: Array<ContentBlock.Standard> = [];\n for (const block of blocks) {\n if (isOpenAIDataBlock(block)) {\n convertedBlocks.push(convertToV1FromOpenAIDataBlock(block));\n } else {\n convertedBlocks.push(block as ContentBlock.Standard);\n }\n }\n return convertedBlocks;\n}\n\nfunction convertResponsesAnnotation(\n annotation: ContentBlock\n): ContentBlock | ContentBlock.Citation {\n if (annotation.type === \"url_citation\") {\n const { url, title, start_index, end_index } = annotation;\n return {\n type: \"citation\",\n url,\n title,\n startIndex: start_index,\n endIndex: end_index,\n };\n }\n if (annotation.type === \"file_citation\") {\n const { file_id, filename, index } = annotation;\n return {\n type: \"citation\",\n title: filename,\n startIndex: index,\n endIndex: index,\n fileId: file_id,\n };\n }\n return annotation;\n}\n\n/**\n * Converts a ChatOpenAIResponses message to an array of v1 standard content blocks.\n *\n * This function processes an AI message containing OpenAI Responses-specific content blocks\n * and converts them to the standardized v1 content block format. It handles reasoning summaries,\n * text content with annotations, tool calls, and various tool outputs including code interpreter,\n * web search, file search, computer calls, and MCP-related blocks.\n *\n * @param message - The AI message containing OpenAI Responses-formatted content blocks\n * @returns Array of content blocks in v1 standard format\n *\n * @example\n * ```typescript\n * const message = new AIMessage({\n * content: [{ type: \"text\", text: \"Hello world\", annotations: [] }],\n * tool_calls: [{ id: \"123\", name: \"calculator\", args: { a: 1, b: 2 } }],\n * additional_kwargs: {\n * reasoning: { summary: [{ text: \"Let me calculate this...\" }] },\n * tool_outputs: [\n * {\n * type: \"code_interpreter_call\",\n * code: \"print('hello')\",\n * outputs: [{ type: \"logs\", logs: \"hello\" }]\n * }\n * ]\n * }\n * });\n *\n * const standardBlocks = convertToV1FromResponses(message);\n * // Returns:\n * // [\n * // { type: \"reasoning\", reasoning: \"Let me calculate this...\" },\n * // { type: \"text\", text: \"Hello world\", annotations: [] },\n * // { type: \"tool_call\", id: \"123\", name: \"calculator\", args: { a: 1, b: 2 } },\n * // { type: \"code_interpreter_call\", code: \"print('hello')\" },\n * // { type: \"code_interpreter_result\", output: [{ type: \"code_interpreter_output\", returnCode: 0, stdout: \"hello\" }] }\n * // ]\n * ```\n */\nexport function convertToV1FromResponses(\n message: AIMessage\n): Array<ContentBlock.Standard> {\n function* iterateContent(): Iterable<ContentBlock.Standard> {\n if (\n _isObject(message.additional_kwargs?.reasoning) &&\n _isArray(message.additional_kwargs.reasoning.summary)\n ) {\n const summary =\n message.additional_kwargs.reasoning.summary.reduce<string>(\n (acc, item) => {\n if (_isObject(item) && _isString(item.text)) {\n return `${acc}${item.text}`;\n }\n return acc;\n },\n \"\"\n );\n yield {\n type: \"reasoning\",\n reasoning: summary,\n };\n }\n const content =\n typeof message.content === \"string\"\n ? [{ type: \"text\", text: message.content }]\n : message.content;\n for (const block of content) {\n if (_isContentBlock(block, \"text\")) {\n const { text, annotations, ...rest } = block;\n if (Array.isArray(annotations)) {\n yield {\n ...rest,\n type: \"text\",\n text: String(text),\n annotations: annotations.map(convertResponsesAnnotation),\n };\n } else {\n yield {\n ...rest,\n type: \"text\",\n text: String(text),\n };\n }\n }\n }\n for (const toolCall of message.tool_calls ?? []) {\n yield {\n type: \"tool_call\",\n id: toolCall.id,\n name: toolCall.name,\n args: toolCall.args,\n };\n }\n if (\n _isObject(message.additional_kwargs) &&\n _isArray(message.additional_kwargs.tool_outputs)\n ) {\n for (const toolOutput of message.additional_kwargs.tool_outputs) {\n if (_isContentBlock(toolOutput, \"web_search_call\")) {\n yield {\n id: toolOutput.id,\n type: \"server_tool_call\",\n name: \"web_search\",\n args: { query: toolOutput.query },\n };\n continue;\n } else if (_isContentBlock(toolOutput, \"file_search_call\")) {\n yield {\n id: toolOutput.id,\n type: \"server_tool_call\",\n name: \"file_search\",\n args: { query: toolOutput.query },\n };\n continue;\n } else if (_isContentBlock(toolOutput, \"computer_call\")) {\n yield { type: \"non_standard\", value: toolOutput };\n continue;\n } else if (_isContentBlock(toolOutput, \"code_interpreter_call\")) {\n if (_isString(toolOutput.code)) {\n yield {\n id: toolOutput.id,\n type: \"server_tool_call\",\n name: \"code_interpreter\",\n args: { code: toolOutput.code },\n };\n }\n if (_isArray(toolOutput.outputs)) {\n const returnCode = iife(() => {\n if (toolOutput.status === \"in_progress\") return undefined;\n if (toolOutput.status === \"completed\") return 0;\n if (toolOutput.status === \"incomplete\") return 127;\n if (toolOutput.status === \"interpreting\") return undefined;\n if (toolOutput.status === \"failed\") return 1;\n return undefined;\n });\n for (const output of toolOutput.outputs) {\n if (_isContentBlock(output, \"logs\")) {\n yield {\n type: \"server_tool_call_result\",\n toolCallId: toolOutput.id ?? \"\",\n status: \"success\",\n output: {\n type: \"code_interpreter_output\",\n returnCode: returnCode ?? 0,\n stderr: [0, undefined].includes(returnCode)\n ? undefined\n : String(output.logs),\n stdout: [0, undefined].includes(returnCode)\n ? String(output.logs)\n : undefined,\n },\n };\n continue;\n }\n }\n }\n continue;\n } else if (_isContentBlock(toolOutput, \"mcp_call\")) {\n yield {\n id: toolOutput.id,\n type: \"server_tool_call\",\n name: \"mcp_call\",\n args: toolOutput.input,\n };\n continue;\n } else if (_isContentBlock(toolOutput, \"mcp_list_tools\")) {\n yield {\n id: toolOutput.id,\n type: \"server_tool_call\",\n name: \"mcp_list_tools\",\n args: toolOutput.input,\n };\n continue;\n } else if (_isContentBlock(toolOutput, \"mcp_approval_request\")) {\n yield { type: \"non_standard\", value: toolOutput };\n continue;\n } else if (_isContentBlock(toolOutput, \"image_generation_call\")) {\n // Convert image_generation_call to proper image content block if result is available\n if (_isString(toolOutput.result)) {\n yield {\n type: \"image\",\n mimeType: \"image/png\",\n data: toolOutput.result,\n id: _isString(toolOutput.id) ? toolOutput.id : undefined,\n metadata: {\n status: _isString(toolOutput.status)\n ? toolOutput.status\n : undefined,\n },\n };\n }\n // Also yield as non_standard for backwards compatibility\n yield { type: \"non_standard\", value: toolOutput };\n continue;\n }\n if (_isObject(toolOutput)) {\n yield { type: \"non_standard\", value: toolOutput };\n }\n }\n }\n }\n return Array.from(iterateContent());\n}\n\n/**\n * Converts a ChatOpenAIResponses message chunk to an array of v1 standard content blocks.\n *\n * This function processes an AI message chunk containing OpenAI-specific content blocks\n * and converts them to the standardized v1 content block format. It handles both the\n * regular message content and tool call chunks that are specific to streaming responses.\n *\n * @param message - The AI message chunk containing OpenAI-formatted content blocks\n * @returns Array of content blocks in v1 standard format\n *\n * @example\n * ```typescript\n * const messageChunk = new AIMessageChunk({\n * content: [{ type: \"text\", text: \"Hello\" }],\n * tool_call_chunks: [\n * { id: \"call_123\", name: \"calculator\", args: '{\"a\": 1' }\n * ]\n * });\n *\n * const standardBlocks = convertToV1FromResponsesChunk(messageChunk);\n * // Returns:\n * // [\n * // { type: \"text\", text: \"Hello\" },\n * // { type: \"tool_call_chunk\", id: \"call_123\", name: \"calculator\", args: '{\"a\": 1' }\n * // ]\n * ```\n */\nexport function convertToV1FromResponsesChunk(\n message: AIMessageChunk\n): Array<ContentBlock.Standard> {\n function* iterateContent(): Iterable<ContentBlock.Standard> {\n yield* convertToV1FromResponses(message);\n for (const toolCallChunk of message.tool_call_chunks ?? []) {\n yield {\n type: \"tool_call_chunk\",\n id: toolCallChunk.id,\n name: toolCallChunk.name,\n args: toolCallChunk.args,\n };\n }\n }\n return Array.from(iterateContent());\n}\n\nexport const ChatOpenAITranslator: StandardContentBlockTranslator = {\n translateContent: (message) => {\n if (typeof message.content === \"string\") {\n return convertToV1FromChatCompletions(message);\n }\n return convertToV1FromResponses(message);\n },\n translateContentChunk: (message) => {\n if (typeof message.content === \"string\") {\n return convertToV1FromChatCompletionsChunk(message);\n }\n return convertToV1FromResponsesChunk(message);\n },\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAgDA,SAAgB,+BACdA,SAC8B;CAC9B,MAAMC,SAAuC,CAAE;AAC/C,KAAI,OAAO,QAAQ,YAAY,UAC7B,OAAO,KAAK;EACV,MAAM;EACN,MAAM,QAAQ;CACf,EAAC;MAEF,OAAO,KAAK,GAAG,oCAAoC,QAAQ,QAAQ,CAAC;AAEtE,MAAK,MAAM,YAAY,QAAQ,cAAc,CAAE,GAC7C,OAAO,KAAK;EACV,MAAM;EACN,IAAI,SAAS;EACb,MAAM,SAAS;EACf,MAAM,SAAS;CAChB,EAAC;AAEJ,QAAO;AACR;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAoCD,SAAgB,oCACdD,SAC8B;CAC9B,MAAMC,SAAuC,CAAE;AAC/C,KAAI,OAAO,QAAQ,YAAY,UAC7B,OAAO,KAAK;EACV,MAAM;EACN,MAAM,QAAQ;CACf,EAAC;MAEF,OAAO,KAAK,GAAG,oCAAoC,QAAQ,QAAQ,CAAC;AAItE,MAAK,MAAM,YAAY,QAAQ,cAAc,CAAE,GAC7C,OAAO,KAAK;EACV,MAAM;EACN,IAAI,SAAS;EACb,MAAM,SAAS;EACf,MAAM,SAAS;CAChB,EAAC;AAEJ,QAAO;AACR;;;;;;;;;;;;;;;;;;;;;;;;;;;AA4BD,SAAgB,oCACdC,QAC8B;CAC9B,MAAMC,kBAAgD,CAAE;AACxD,MAAK,MAAM,SAAS,OAClB,KAAI,kBAAkB,MAAM,EAC1B,gBAA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AaC,uBAAuD;CAClE,kBAAkB,CAAC,YAAY;AAC7B,MAAI,OAAO,QAAQ,YAAY,SAC7B,QAAO,+BAA+B,QAAQ;AAEhD,SAAO,yBAAyB,QAAQ;CACzC;CACD,uBAAuB,CAAC,YAAY;AAClC,MAAI,OAAO,QAAQ,YAAY,SAC7B,QAAO,oCAAoC,QAAQ;AAErD,SAAO,8BAA8B,QAAQ;CAC9C;AACF"}
1
+ {"version":3,"file":"openai.js","names":["message: AIMessage","blocks: Array<ContentBlock.Standard>","blocks: Array<ContentBlock>","convertedBlocks: Array<ContentBlock.Standard>","annotation: ContentBlock","message: AIMessageChunk","ChatOpenAITranslator: StandardContentBlockTranslator"],"sources":["../../../src/messages/block_translators/openai.ts"],"sourcesContent":["import type { ContentBlock } from \"../content/index.js\";\nimport type { AIMessageChunk, AIMessage } from \"../ai.js\";\nimport type { StandardContentBlockTranslator } from \"./index.js\";\nimport { convertToV1FromOpenAIDataBlock, isOpenAIDataBlock } from \"./data.js\";\nimport {\n _isArray,\n _isContentBlock,\n _isObject,\n _isString,\n iife,\n} from \"./utils.js\";\n\n/**\n * Converts a ChatOpenAICompletions message to an array of v1 standard content blocks.\n *\n * This function processes an AI message from ChatOpenAICompletions API format\n * and converts it to the standardized v1 content block format. It handles both\n * string content and structured content blocks, as well as tool calls.\n *\n * @param message - The AI message containing ChatOpenAICompletions formatted content\n * @returns Array of content blocks in v1 standard format\n *\n * @example\n * ```typescript\n * const message = new AIMessage(\"Hello world\");\n * const standardBlocks = convertToV1FromChatCompletions(message);\n * // Returns: [{ type: \"text\", text: \"Hello world\" }]\n * ```\n *\n * @example\n * ```typescript\n * const message = new AIMessage([\n * { type: \"text\", text: \"Hello\" },\n * { type: \"image_url\", image_url: { url: \"https://example.com/image.png\" } }\n * ]);\n * message.tool_calls = [\n * { id: \"call_123\", name: \"calculator\", args: { a: 1, b: 2 } }\n * ];\n *\n * const standardBlocks = convertToV1FromChatCompletions(message);\n * // Returns:\n * // [\n * // { type: \"text\", text: \"Hello\" },\n * // { type: \"image\", url: \"https://example.com/image.png\" },\n * // { type: \"tool_call\", id: \"call_123\", name: \"calculator\", args: { a: 1, b: 2 } }\n * // ]\n * ```\n */\nexport function convertToV1FromChatCompletions(\n message: AIMessage\n): Array<ContentBlock.Standard> {\n const blocks: Array<ContentBlock.Standard> = [];\n if (typeof message.content === \"string\") {\n // Only add text block if content is non-empty\n if (message.content.length > 0) {\n blocks.push({\n type: \"text\",\n text: message.content,\n });\n }\n } else {\n blocks.push(...convertToV1FromChatCompletionsInput(message.content));\n }\n for (const toolCall of message.tool_calls ?? []) {\n blocks.push({\n type: \"tool_call\",\n id: toolCall.id,\n name: toolCall.name,\n args: toolCall.args,\n });\n }\n return blocks;\n}\n\n/**\n * Converts a ChatOpenAICompletions message chunk to an array of v1 standard content blocks.\n *\n * This function processes an AI message chunk from OpenAI's chat completions API and converts\n * it to the standardized v1 content block format. It handles both string and array content,\n * as well as tool calls that may be present in the chunk.\n *\n * @param message - The AI message chunk containing OpenAI-formatted content blocks\n * @returns Array of content blocks in v1 standard format\n *\n * @example\n * ```typescript\n * const chunk = new AIMessage(\"Hello\");\n * const standardBlocks = convertToV1FromChatCompletionsChunk(chunk);\n * // Returns: [{ type: \"text\", text: \"Hello\" }]\n * ```\n *\n * @example\n * ```typescript\n * const chunk = new AIMessage([\n * { type: \"text\", text: \"Processing...\" }\n * ]);\n * chunk.tool_calls = [\n * { id: \"call_456\", name: \"search\", args: { query: \"test\" } }\n * ];\n *\n * const standardBlocks = convertToV1FromChatCompletionsChunk(chunk);\n * // Returns:\n * // [\n * // { type: \"text\", text: \"Processing...\" },\n * // { type: \"tool_call\", id: \"call_456\", name: \"search\", args: { query: \"test\" } }\n * // ]\n * ```\n */\nexport function convertToV1FromChatCompletionsChunk(\n message: AIMessage\n): Array<ContentBlock.Standard> {\n const blocks: Array<ContentBlock.Standard> = [];\n if (typeof message.content === \"string\") {\n // Only add text block if content is non-empty\n if (message.content.length > 0) {\n blocks.push({\n type: \"text\",\n text: message.content,\n });\n }\n } else {\n blocks.push(...convertToV1FromChatCompletionsInput(message.content));\n }\n\n // TODO: parse chunk position information\n for (const toolCall of message.tool_calls ?? []) {\n blocks.push({\n type: \"tool_call\",\n id: toolCall.id,\n name: toolCall.name,\n args: toolCall.args,\n });\n }\n return blocks;\n}\n\n/**\n * Converts an array of ChatOpenAICompletions content blocks to v1 standard content blocks.\n *\n * This function processes content blocks from OpenAI's Chat Completions API format\n * and converts them to the standardized v1 content block format. It handles both\n * OpenAI-specific data blocks (which require conversion) and standard blocks\n * (which are passed through with type assertion).\n *\n * @param blocks - Array of content blocks in ChatOpenAICompletions format\n * @returns Array of content blocks in v1 standard format\n *\n * @example\n * ```typescript\n * const openaiBlocks = [\n * { type: \"text\", text: \"Hello world\" },\n * { type: \"image_url\", image_url: { url: \"https://example.com/image.png\" } }\n * ];\n *\n * const standardBlocks = convertToV1FromChatCompletionsInput(openaiBlocks);\n * // Returns:\n * // [\n * // { type: \"text\", text: \"Hello world\" },\n * // { type: \"image\", url: \"https://example.com/image.png\" }\n * // ]\n * ```\n */\nexport function convertToV1FromChatCompletionsInput(\n blocks: Array<ContentBlock>\n): Array<ContentBlock.Standard> {\n const convertedBlocks: Array<ContentBlock.Standard> = [];\n for (const block of blocks) {\n if (isOpenAIDataBlock(block)) {\n convertedBlocks.push(convertToV1FromOpenAIDataBlock(block));\n } else {\n convertedBlocks.push(block as ContentBlock.Standard);\n }\n }\n return convertedBlocks;\n}\n\nfunction convertResponsesAnnotation(\n annotation: ContentBlock\n): ContentBlock | ContentBlock.Citation {\n if (annotation.type === \"url_citation\") {\n const { url, title, start_index, end_index } = annotation;\n return {\n type: \"citation\",\n url,\n title,\n startIndex: start_index,\n endIndex: end_index,\n };\n }\n if (annotation.type === \"file_citation\") {\n const { file_id, filename, index } = annotation;\n return {\n type: \"citation\",\n title: filename,\n startIndex: index,\n endIndex: index,\n fileId: file_id,\n };\n }\n return annotation;\n}\n\n/**\n * Converts a ChatOpenAIResponses message to an array of v1 standard content blocks.\n *\n * This function processes an AI message containing OpenAI Responses-specific content blocks\n * and converts them to the standardized v1 content block format. It handles reasoning summaries,\n * text content with annotations, tool calls, and various tool outputs including code interpreter,\n * web search, file search, computer calls, and MCP-related blocks.\n *\n * @param message - The AI message containing OpenAI Responses-formatted content blocks\n * @returns Array of content blocks in v1 standard format\n *\n * @example\n * ```typescript\n * const message = new AIMessage({\n * content: [{ type: \"text\", text: \"Hello world\", annotations: [] }],\n * tool_calls: [{ id: \"123\", name: \"calculator\", args: { a: 1, b: 2 } }],\n * additional_kwargs: {\n * reasoning: { summary: [{ text: \"Let me calculate this...\" }] },\n * tool_outputs: [\n * {\n * type: \"code_interpreter_call\",\n * code: \"print('hello')\",\n * outputs: [{ type: \"logs\", logs: \"hello\" }]\n * }\n * ]\n * }\n * });\n *\n * const standardBlocks = convertToV1FromResponses(message);\n * // Returns:\n * // [\n * // { type: \"reasoning\", reasoning: \"Let me calculate this...\" },\n * // { type: \"text\", text: \"Hello world\", annotations: [] },\n * // { type: \"tool_call\", id: \"123\", name: \"calculator\", args: { a: 1, b: 2 } },\n * // { type: \"code_interpreter_call\", code: \"print('hello')\" },\n * // { type: \"code_interpreter_result\", output: [{ type: \"code_interpreter_output\", returnCode: 0, stdout: \"hello\" }] }\n * // ]\n * ```\n */\nexport function convertToV1FromResponses(\n message: AIMessage\n): Array<ContentBlock.Standard> {\n function* iterateContent(): Iterable<ContentBlock.Standard> {\n if (\n _isObject(message.additional_kwargs?.reasoning) &&\n _isArray(message.additional_kwargs.reasoning.summary)\n ) {\n const summary =\n message.additional_kwargs.reasoning.summary.reduce<string>(\n (acc, item) => {\n if (_isObject(item) && _isString(item.text)) {\n return `${acc}${item.text}`;\n }\n return acc;\n },\n \"\"\n );\n yield {\n type: \"reasoning\",\n reasoning: summary,\n };\n }\n const content =\n typeof message.content === \"string\"\n ? [{ type: \"text\", text: message.content }]\n : message.content;\n for (const block of content) {\n if (_isContentBlock(block, \"text\")) {\n const { text, annotations, ...rest } = block;\n if (Array.isArray(annotations)) {\n yield {\n ...rest,\n type: \"text\",\n text: String(text),\n annotations: annotations.map(convertResponsesAnnotation),\n };\n } else {\n yield {\n ...rest,\n type: \"text\",\n text: String(text),\n };\n }\n }\n }\n for (const toolCall of message.tool_calls ?? []) {\n yield {\n type: \"tool_call\",\n id: toolCall.id,\n name: toolCall.name,\n args: toolCall.args,\n };\n }\n if (\n _isObject(message.additional_kwargs) &&\n _isArray(message.additional_kwargs.tool_outputs)\n ) {\n for (const toolOutput of message.additional_kwargs.tool_outputs) {\n if (_isContentBlock(toolOutput, \"web_search_call\")) {\n yield {\n id: toolOutput.id,\n type: \"server_tool_call\",\n name: \"web_search\",\n args: { query: toolOutput.query },\n };\n continue;\n } else if (_isContentBlock(toolOutput, \"file_search_call\")) {\n yield {\n id: toolOutput.id,\n type: \"server_tool_call\",\n name: \"file_search\",\n args: { query: toolOutput.query },\n };\n continue;\n } else if (_isContentBlock(toolOutput, \"computer_call\")) {\n yield { type: \"non_standard\", value: toolOutput };\n continue;\n } else if (_isContentBlock(toolOutput, \"code_interpreter_call\")) {\n if (_isString(toolOutput.code)) {\n yield {\n id: toolOutput.id,\n type: \"server_tool_call\",\n name: \"code_interpreter\",\n args: { code: toolOutput.code },\n };\n }\n if (_isArray(toolOutput.outputs)) {\n const returnCode = iife(() => {\n if (toolOutput.status === \"in_progress\") return undefined;\n if (toolOutput.status === \"completed\") return 0;\n if (toolOutput.status === \"incomplete\") return 127;\n if (toolOutput.status === \"interpreting\") return undefined;\n if (toolOutput.status === \"failed\") return 1;\n return undefined;\n });\n for (const output of toolOutput.outputs) {\n if (_isContentBlock(output, \"logs\")) {\n yield {\n type: \"server_tool_call_result\",\n toolCallId: toolOutput.id ?? \"\",\n status: \"success\",\n output: {\n type: \"code_interpreter_output\",\n returnCode: returnCode ?? 0,\n stderr: [0, undefined].includes(returnCode)\n ? undefined\n : String(output.logs),\n stdout: [0, undefined].includes(returnCode)\n ? String(output.logs)\n : undefined,\n },\n };\n continue;\n }\n }\n }\n continue;\n } else if (_isContentBlock(toolOutput, \"mcp_call\")) {\n yield {\n id: toolOutput.id,\n type: \"server_tool_call\",\n name: \"mcp_call\",\n args: toolOutput.input,\n };\n continue;\n } else if (_isContentBlock(toolOutput, \"mcp_list_tools\")) {\n yield {\n id: toolOutput.id,\n type: \"server_tool_call\",\n name: \"mcp_list_tools\",\n args: toolOutput.input,\n };\n continue;\n } else if (_isContentBlock(toolOutput, \"mcp_approval_request\")) {\n yield { type: \"non_standard\", value: toolOutput };\n continue;\n } else if (_isContentBlock(toolOutput, \"image_generation_call\")) {\n // Convert image_generation_call to proper image content block if result is available\n if (_isString(toolOutput.result)) {\n yield {\n type: \"image\",\n mimeType: \"image/png\",\n data: toolOutput.result,\n id: _isString(toolOutput.id) ? toolOutput.id : undefined,\n metadata: {\n status: _isString(toolOutput.status)\n ? toolOutput.status\n : undefined,\n },\n };\n }\n // Also yield as non_standard for backwards compatibility\n yield { type: \"non_standard\", value: toolOutput };\n continue;\n }\n if (_isObject(toolOutput)) {\n yield { type: \"non_standard\", value: toolOutput };\n }\n }\n }\n }\n return Array.from(iterateContent());\n}\n\n/**\n * Converts a ChatOpenAIResponses message chunk to an array of v1 standard content blocks.\n *\n * This function processes an AI message chunk containing OpenAI-specific content blocks\n * and converts them to the standardized v1 content block format. It handles both the\n * regular message content and tool call chunks that are specific to streaming responses.\n *\n * @param message - The AI message chunk containing OpenAI-formatted content blocks\n * @returns Array of content blocks in v1 standard format\n *\n * @example\n * ```typescript\n * const messageChunk = new AIMessageChunk({\n * content: [{ type: \"text\", text: \"Hello\" }],\n * tool_call_chunks: [\n * { id: \"call_123\", name: \"calculator\", args: '{\"a\": 1' }\n * ]\n * });\n *\n * const standardBlocks = convertToV1FromResponsesChunk(messageChunk);\n * // Returns:\n * // [\n * // { type: \"text\", text: \"Hello\" },\n * // { type: \"tool_call_chunk\", id: \"call_123\", name: \"calculator\", args: '{\"a\": 1' }\n * // ]\n * ```\n */\nexport function convertToV1FromResponsesChunk(\n message: AIMessageChunk\n): Array<ContentBlock.Standard> {\n function* iterateContent(): Iterable<ContentBlock.Standard> {\n yield* convertToV1FromResponses(message);\n for (const toolCallChunk of message.tool_call_chunks ?? []) {\n yield {\n type: \"tool_call_chunk\",\n id: toolCallChunk.id,\n name: toolCallChunk.name,\n args: toolCallChunk.args,\n };\n }\n }\n return Array.from(iterateContent());\n}\n\nexport const ChatOpenAITranslator: StandardContentBlockTranslator = {\n translateContent: (message) => {\n if (typeof message.content === \"string\") {\n return convertToV1FromChatCompletions(message);\n }\n return convertToV1FromResponses(message);\n },\n translateContentChunk: (message) => {\n if (typeof message.content === \"string\") {\n return convertToV1FromChatCompletionsChunk(message);\n }\n return convertToV1FromResponsesChunk(message);\n },\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAgDA,SAAgB,+BACdA,SAC8B;CAC9B,MAAMC,SAAuC,CAAE;AAC/C,KAAI,OAAO,QAAQ,YAAY,UAE7B;MAAI,QAAQ,QAAQ,SAAS,GAC3B,OAAO,KAAK;GACV,MAAM;GACN,MAAM,QAAQ;EACf,EAAC;CACH,OAED,OAAO,KAAK,GAAG,oCAAoC,QAAQ,QAAQ,CAAC;AAEtE,MAAK,MAAM,YAAY,QAAQ,cAAc,CAAE,GAC7C,OAAO,KAAK;EACV,MAAM;EACN,IAAI,SAAS;EACb,MAAM,SAAS;EACf,MAAM,SAAS;CAChB,EAAC;AAEJ,QAAO;AACR;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAoCD,SAAgB,oCACdD,SAC8B;CAC9B,MAAMC,SAAuC,CAAE;AAC/C,KAAI,OAAO,QAAQ,YAAY,UAE7B;MAAI,QAAQ,QAAQ,SAAS,GAC3B,OAAO,KAAK;GACV,MAAM;GACN,MAAM,QAAQ;EACf,EAAC;CACH,OAED,OAAO,KAAK,GAAG,oCAAoC,QAAQ,QAAQ,CAAC;AAItE,MAAK,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;;;;;;;;;;;;;;;;;;;;;AA6BD,SAAgB,8BACdK,SAC8B;CAC9B,UAAU,iBAAkD;EAC1D,OAAO,yBAAyB,QAAQ;AACxC,OAAK,MAAM,iBAAiB,QAAQ,oBAAoB,CAAE,GACxD,MAAM;GACJ,MAAM;GACN,IAAI,cAAc;GAClB,MAAM,cAAc;GACpB,MAAM,cAAc;EACrB;CAEJ;AACD,QAAO,MAAM,KAAK,gBAAgB,CAAC;AACpC;AAED,MAAaC,uBAAuD;CAClE,kBAAkB,CAAC,YAAY;AAC7B,MAAI,OAAO,QAAQ,YAAY,SAC7B,QAAO,+BAA+B,QAAQ;AAEhD,SAAO,yBAAyB,QAAQ;CACzC;CACD,uBAAuB,CAAC,YAAY;AAClC,MAAI,OAAO,QAAQ,YAAY,SAC7B,QAAO,oCAAoC,QAAQ;AAErD,SAAO,8BAA8B,QAAQ;CAC9C;AACF"}
@@ -1 +1 @@
1
- {"version":3,"file":"stores.d.ts","names":["Serializable","BaseStore","K","V","Promise","AsyncGenerator","InMemoryStore","T","Record"],"sources":["../src/stores.d.ts"],"sourcesContent":["import { Serializable } from \"./load/serializable.js\";\n/**\n * Abstract interface for a key-value store.\n */\nexport declare abstract class BaseStore<K, V> extends Serializable {\n /**\n * Abstract method to get multiple values for a set of keys.\n * @param {K[]} keys - An array of keys.\n * @returns {Promise<(V | undefined)[]>} - A Promise that resolves with array of values or undefined if key not found.\n */\n abstract mget(keys: K[]): Promise<(V | undefined)[]>;\n /**\n * Abstract method to set a value for multiple keys.\n * @param {[K, V][]} keyValuePairs - An array of key-value pairs.\n * @returns {Promise<void>} - A Promise that resolves when the operation is complete.\n */\n abstract mset(keyValuePairs: [K, V][]): Promise<void>;\n /**\n * Abstract method to delete multiple keys.\n * @param {K[]} keys - An array of keys to delete.\n * @returns {Promise<void>} - A Promise that resolves when the operation is complete.\n */\n abstract mdelete(keys: K[]): Promise<void>;\n /**\n * Abstract method to yield keys optionally based on a prefix.\n * @param {string} prefix - Optional prefix to filter keys.\n * @returns {AsyncGenerator<K | string>} - An asynchronous generator that yields keys on iteration.\n */\n abstract yieldKeys(prefix?: string): AsyncGenerator<K | string>;\n}\n/**\n * In-memory implementation of the BaseStore using a dictionary. Used for\n * storing key-value pairs in memory.\n * @example\n * ```typescript\n * const store = new InMemoryStore<BaseMessage>();\n * await store.mset(\n * Array.from({ length: 5 }).map((_, index) => [\n * `message:id:${index}`,\n * index % 2 === 0\n * ? new AIMessage(\"ai stuff...\")\n * : new HumanMessage(\"human stuff...\"),\n * ]),\n * );\n *\n * const retrievedMessages = await store.mget([\"message:id:0\", \"message:id:1\"]);\n * await store.mdelete(await store.yieldKeys(\"message:id:\").toArray());\n * ```\n */\nexport declare class InMemoryStore<T = any> extends BaseStore<string, T> {\n lc_namespace: string[];\n protected store: Record<string, T>;\n /**\n * Retrieves the values associated with the given keys from the store.\n * @param keys Keys to retrieve values for.\n * @returns Array of values associated with the given keys.\n */\n mget(keys: string[]): Promise<T[]>;\n /**\n * Sets the values for the given keys in the store.\n * @param keyValuePairs Array of key-value pairs to set in the store.\n * @returns Promise that resolves when all key-value pairs have been set.\n */\n mset(keyValuePairs: [string, T][]): Promise<void>;\n /**\n * Deletes the given keys and their associated values from the store.\n * @param keys Keys to delete from the store.\n * @returns Promise that resolves when all keys have been deleted.\n */\n mdelete(keys: string[]): Promise<void>;\n /**\n * Asynchronous generator that yields keys from the store. If a prefix is\n * provided, it only yields keys that start with the prefix.\n * @param prefix Optional prefix to filter keys.\n * @returns AsyncGenerator that yields keys from the store.\n */\n yieldKeys(prefix?: string | undefined): AsyncGenerator<string>;\n}\n//# sourceMappingURL=stores.d.ts.map"],"mappings":";;;;;;AAIA;AAMwBE,uBANMD,SAMNC,CAAAA,CAAAA,EAAAA,CAAAA,CAAAA,SAN8BF,YAAAA,CAM9BE;EAAeC;;;;;EAYZD,SAAAA,IAAAA,CAAAA,IAAAA,EAZHA,CAYGA,EAAAA,CAAAA,EAZGE,OAYHF,CAAAA,CAZYC,CAYZD,GAAAA,SAAAA,CAAAA,EAAAA,CAAAA;EAAME;;;;AAlBiC;EA6C7CE,SAAAA,IAAAA,CAAAA,aAAa,EAAA,CAjCAJ,CAiCA,EAjCGC,CAiCH,CAAA,EAAA,CAAA,EAjCUC,OAiCV,CAAA,IAAA,CAAA;EAAoCG;;;;;EAcrCA,SAAAA,OAAAA,CAAAA,IAAAA,EAzCNL,CAyCMK,EAAAA,CAAAA,EAzCAH,OAyCAG,CAAAA,IAAAA,CAAAA;EAAOH;;;;AAdqB;uCArBpBC,eAAeH;;;;;;;;;;;;;;;;;;;;;cAqBnCI,+BAA+BL,kBAAkBM;;mBAEjDC,eAAeD;;;;;;wBAMVH,QAAQG;;;;;;+BAMDA,OAAOH;;;;;;2BAMXA;;;;;;;0CAOeC"}
1
+ {"version":3,"file":"stores.d.ts","names":["Serializable","BaseStore","K","V","Promise","AsyncGenerator","InMemoryStore","T","Record"],"sources":["../src/stores.d.ts"],"sourcesContent":["import { Serializable } from \"./load/serializable.js\";\n/**\n * Abstract interface for a key-value store.\n */\nexport declare abstract class BaseStore<K, V> extends Serializable {\n /**\n * Abstract method to get multiple values for a set of keys.\n * @param {K[]} keys - An array of keys.\n * @returns {Promise<(V | undefined)[]>} - A Promise that resolves with array of values or undefined if key not found.\n */\n abstract mget(keys: K[]): Promise<(V | undefined)[]>;\n /**\n * Abstract method to set a value for multiple keys.\n * @param {[K, V][]} keyValuePairs - An array of key-value pairs.\n * @returns {Promise<void>} - A Promise that resolves when the operation is complete.\n */\n abstract mset(keyValuePairs: [K, V][]): Promise<void>;\n /**\n * Abstract method to delete multiple keys.\n * @param {K[]} keys - An array of keys to delete.\n * @returns {Promise<void>} - A Promise that resolves when the operation is complete.\n */\n abstract mdelete(keys: K[]): Promise<void>;\n /**\n * Abstract method to yield keys optionally based on a prefix.\n * @param {string} prefix - Optional prefix to filter keys.\n * @returns {AsyncGenerator<K | string>} - An asynchronous generator that yields keys on iteration.\n */\n abstract yieldKeys(prefix?: string): AsyncGenerator<K | string>;\n}\n/**\n * In-memory implementation of the BaseStore using a dictionary. Used for\n * storing key-value pairs in memory.\n * @example\n * ```typescript\n * const store = new InMemoryStore<BaseMessage>();\n * await store.mset(\n * Array.from({ length: 5 }).map((_, index) => [\n * `message:id:${index}`,\n * index % 2 === 0\n * ? new AIMessage(\"ai stuff...\")\n * : new HumanMessage(\"human stuff...\"),\n * ]),\n * );\n *\n * const retrievedMessages = await store.mget([\"message:id:0\", \"message:id:1\"]);\n * await store.mdelete(await store.yieldKeys(\"message:id:\").toArray());\n * ```\n */\nexport declare class InMemoryStore<T = any> extends BaseStore<string, T> {\n lc_namespace: string[];\n protected store: Record<string, T>;\n /**\n * Retrieves the values associated with the given keys from the store.\n * @param keys Keys to retrieve values for.\n * @returns Array of values associated with the given keys.\n */\n mget(keys: string[]): Promise<T[]>;\n /**\n * Sets the values for the given keys in the store.\n * @param keyValuePairs Array of key-value pairs to set in the store.\n * @returns Promise that resolves when all key-value pairs have been set.\n */\n mset(keyValuePairs: [string, T][]): Promise<void>;\n /**\n * Deletes the given keys and their associated values from the store.\n * @param keys Keys to delete from the store.\n * @returns Promise that resolves when all keys have been deleted.\n */\n mdelete(keys: string[]): Promise<void>;\n /**\n * Asynchronous generator that yields keys from the store. If a prefix is\n * provided, it only yields keys that start with the prefix.\n * @param prefix Optional prefix to filter keys.\n * @returns AsyncGenerator that yields keys from the store.\n */\n yieldKeys(prefix?: string | undefined): AsyncGenerator<string>;\n}\n//# sourceMappingURL=stores.d.ts.map"],"mappings":";;;;;;AAIA;AAMwBE,uBANMD,SAMNC,CAAAA,CAAAA,EAAAA,CAAAA,CAAAA,SAN8BF,YAAAA,CAM9BE;EAAeC;;;;;EAYZD,SAAAA,IAAAA,CAAAA,IAAAA,EAZHA,CAYGA,EAAAA,CAAAA,EAZGE,OAYHF,CAAAA,CAZYC,CAYZD,GAAAA,SAAAA,CAAAA,EAAAA,CAAAA;EAAME;;;;AAlBiC;EA6C7CE,SAAAA,IAAAA,CAAAA,aAAaC,EAAA,CAjCAL,CAiCA,EAjCGC,CAiCH,CAAA,EAAA,CAAA,EAjCUC,OAiCV,CAAA,IAAA,CAAA;EAAoCG;;;;;EAcrCA,SAAAA,OAAAA,CAAAA,IAAAA,EAzCNL,CAyCMK,EAAAA,CAAAA,EAzCAH,OAyCAG,CAAAA,IAAAA,CAAAA;EAAOH;;;;AAdqB;uCArBpBC,eAAeH;;;;;;;;;;;;;;;;;;;;;cAqBnCI,+BAA+BL,kBAAkBM;;mBAEjDC,eAAeD;;;;;;wBAMVH,QAAQG;;;;;;+BAMDA,OAAOH;;;;;;2BAMXA;;;;;;;0CAOeC"}
@@ -0,0 +1,271 @@
1
+ const require_rolldown_runtime = require('../_virtual/rolldown_runtime.cjs');
2
+
3
+ //#region src/utils/ssrf.ts
4
+ var ssrf_exports = {};
5
+ require_rolldown_runtime.__export(ssrf_exports, {
6
+ isCloudMetadata: () => isCloudMetadata,
7
+ isLocalhost: () => isLocalhost,
8
+ isPrivateIp: () => isPrivateIp,
9
+ isSafeUrl: () => isSafeUrl,
10
+ isSameOrigin: () => isSameOrigin,
11
+ validateSafeUrl: () => validateSafeUrl
12
+ });
13
+ const PRIVATE_IP_RANGES = [
14
+ "10.0.0.0/8",
15
+ "172.16.0.0/12",
16
+ "192.168.0.0/16",
17
+ "127.0.0.0/8",
18
+ "169.254.0.0/16",
19
+ "0.0.0.0/8",
20
+ "::1/128",
21
+ "fc00::/7",
22
+ "fe80::/10",
23
+ "ff00::/8"
24
+ ];
25
+ const CLOUD_METADATA_IPS = [
26
+ "169.254.169.254",
27
+ "169.254.170.2",
28
+ "100.100.100.200"
29
+ ];
30
+ const CLOUD_METADATA_HOSTNAMES = [
31
+ "metadata.google.internal",
32
+ "metadata",
33
+ "instance-data"
34
+ ];
35
+ const LOCALHOST_NAMES = ["localhost", "localhost.localdomain"];
36
+ /**
37
+ * IPv4 regex: four octets 0-255
38
+ */
39
+ const IPV4_REGEX = /^(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\.){3}(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)$/;
40
+ /**
41
+ * Check if a string is a valid IPv4 address.
42
+ */
43
+ function isIPv4(ip) {
44
+ return IPV4_REGEX.test(ip);
45
+ }
46
+ /**
47
+ * Check if a string is a valid IPv6 address.
48
+ * Uses expandIpv6 for validation.
49
+ */
50
+ function isIPv6(ip) {
51
+ return expandIpv6(ip) !== null;
52
+ }
53
+ /**
54
+ * Check if a string is a valid IP address (IPv4 or IPv6).
55
+ */
56
+ function isIP(ip) {
57
+ return isIPv4(ip) || isIPv6(ip);
58
+ }
59
+ /**
60
+ * Parse an IP address string to an array of integers (for IPv4) or an array of 16-bit values (for IPv6)
61
+ * Returns null if the IP is invalid.
62
+ */
63
+ function parseIp(ip) {
64
+ if (isIPv4(ip)) return ip.split(".").map((octet) => parseInt(octet, 10));
65
+ else if (isIPv6(ip)) {
66
+ const expanded = expandIpv6(ip);
67
+ if (!expanded) return null;
68
+ const parts = expanded.split(":");
69
+ const result = [];
70
+ for (const part of parts) result.push(parseInt(part, 16));
71
+ return result;
72
+ }
73
+ return null;
74
+ }
75
+ /**
76
+ * Expand compressed IPv6 address to full form.
77
+ */
78
+ function expandIpv6(ip) {
79
+ if (!ip || typeof ip !== "string") return null;
80
+ if (!ip.includes(":")) return null;
81
+ if (!/^[0-9a-fA-F:]+$/.test(ip)) return null;
82
+ let normalized = ip;
83
+ if (normalized.includes("::")) {
84
+ const parts$1 = normalized.split("::");
85
+ if (parts$1.length > 2) return null;
86
+ const [left, right] = parts$1;
87
+ const leftParts = left ? left.split(":") : [];
88
+ const rightParts = right ? right.split(":") : [];
89
+ const missing = 8 - (leftParts.length + rightParts.length);
90
+ if (missing < 0) return null;
91
+ const zeros = Array(missing).fill("0");
92
+ normalized = [
93
+ ...leftParts,
94
+ ...zeros,
95
+ ...rightParts
96
+ ].filter((p) => p !== "").join(":");
97
+ }
98
+ const parts = normalized.split(":");
99
+ if (parts.length !== 8) return null;
100
+ for (const part of parts) {
101
+ if (part.length === 0 || part.length > 4) return null;
102
+ if (!/^[0-9a-fA-F]+$/.test(part)) return null;
103
+ }
104
+ return parts.map((p) => p.padStart(4, "0").toLowerCase()).join(":");
105
+ }
106
+ /**
107
+ * Parse CIDR notation (e.g., "192.168.0.0/24") into network address and prefix length.
108
+ */
109
+ function parseCidr(cidr) {
110
+ const [addrStr, prefixStr] = cidr.split("/");
111
+ if (!addrStr || !prefixStr) return null;
112
+ const addr = parseIp(addrStr);
113
+ if (!addr) return null;
114
+ const prefixLen = parseInt(prefixStr, 10);
115
+ if (isNaN(prefixLen)) return null;
116
+ const isIpv6 = isIPv6(addrStr);
117
+ if (isIpv6 && prefixLen > 128) return null;
118
+ if (!isIpv6 && prefixLen > 32) return null;
119
+ return {
120
+ addr,
121
+ prefixLen,
122
+ isIpv6
123
+ };
124
+ }
125
+ /**
126
+ * Check if an IP address is in a given CIDR range.
127
+ */
128
+ function isIpInCidr(ip, cidr) {
129
+ const ipParsed = parseIp(ip);
130
+ if (!ipParsed) return false;
131
+ const cidrParsed = parseCidr(cidr);
132
+ if (!cidrParsed) return false;
133
+ const isIpv6 = isIPv6(ip);
134
+ if (isIpv6 !== cidrParsed.isIpv6) return false;
135
+ const { addr: cidrAddr, prefixLen } = cidrParsed;
136
+ if (isIpv6) for (let i = 0; i < Math.ceil(prefixLen / 16); i++) {
137
+ const bitsToCheck = Math.min(16, prefixLen - i * 16);
138
+ const mask = 65535 << 16 - bitsToCheck & 65535;
139
+ if ((ipParsed[i] & mask) !== (cidrAddr[i] & mask)) return false;
140
+ }
141
+ else for (let i = 0; i < Math.ceil(prefixLen / 8); i++) {
142
+ const bitsToCheck = Math.min(8, prefixLen - i * 8);
143
+ const mask = 255 << 8 - bitsToCheck & 255;
144
+ if ((ipParsed[i] & mask) !== (cidrAddr[i] & mask)) return false;
145
+ }
146
+ return true;
147
+ }
148
+ /**
149
+ * Check if an IP address is private (RFC 1918, loopback, link-local, etc.)
150
+ */
151
+ function isPrivateIp(ip) {
152
+ if (!isIP(ip)) return false;
153
+ for (const range of PRIVATE_IP_RANGES) if (isIpInCidr(ip, range)) return true;
154
+ return false;
155
+ }
156
+ /**
157
+ * Check if a hostname or IP is a known cloud metadata endpoint.
158
+ */
159
+ function isCloudMetadata(hostname, ip) {
160
+ if (CLOUD_METADATA_IPS.includes(ip || "")) return true;
161
+ const lowerHostname = hostname.toLowerCase();
162
+ if (CLOUD_METADATA_HOSTNAMES.includes(lowerHostname)) return true;
163
+ return false;
164
+ }
165
+ /**
166
+ * Check if a hostname or IP is localhost.
167
+ */
168
+ function isLocalhost(hostname, ip) {
169
+ if (ip) {
170
+ if (ip === "127.0.0.1" || ip === "::1" || ip === "0.0.0.0") return true;
171
+ if (ip.startsWith("127.")) return true;
172
+ }
173
+ const lowerHostname = hostname.toLowerCase();
174
+ if (LOCALHOST_NAMES.includes(lowerHostname)) return true;
175
+ return false;
176
+ }
177
+ /**
178
+ * Validate that a URL is safe to connect to.
179
+ * Performs static validation checks against hostnames and direct IP addresses.
180
+ * Does not perform DNS resolution.
181
+ *
182
+ * @param url URL to validate
183
+ * @param options.allowPrivate Allow private IPs (default: false)
184
+ * @param options.allowHttp Allow http:// scheme (default: false)
185
+ * @returns The validated URL
186
+ * @throws Error if URL is not safe
187
+ */
188
+ function validateSafeUrl(url, options) {
189
+ const allowPrivate = options?.allowPrivate ?? false;
190
+ const allowHttp = options?.allowHttp ?? false;
191
+ try {
192
+ let parsedUrl;
193
+ try {
194
+ parsedUrl = new URL(url);
195
+ } catch {
196
+ throw new Error(`Invalid URL: ${url}`);
197
+ }
198
+ const hostname = parsedUrl.hostname;
199
+ if (!hostname) throw new Error("URL missing hostname.");
200
+ if (isCloudMetadata(hostname)) throw new Error(`URL points to cloud metadata endpoint: ${hostname}`);
201
+ if (isLocalhost(hostname)) {
202
+ if (!allowPrivate) throw new Error(`URL points to localhost: ${hostname}`);
203
+ return url;
204
+ }
205
+ const scheme = parsedUrl.protocol;
206
+ if (scheme !== "http:" && scheme !== "https:") throw new Error(`Invalid URL scheme: ${scheme}. Only http and https are allowed.`);
207
+ if (scheme === "http:" && !allowHttp) throw new Error("HTTP scheme not allowed. Use HTTPS or set allowHttp: true.");
208
+ if (isIP(hostname)) {
209
+ const ip = hostname;
210
+ if (isLocalhost(hostname, ip)) {
211
+ if (!allowPrivate) throw new Error(`URL points to localhost: ${hostname}`);
212
+ return url;
213
+ }
214
+ if (isCloudMetadata(hostname, ip)) throw new Error(`URL resolves to cloud metadata IP: ${ip} (${hostname})`);
215
+ if (isPrivateIp(ip)) {
216
+ if (!allowPrivate) throw new Error(`URL resolves to private IP: ${ip} (${hostname}). Set allowPrivate: true to allow.`);
217
+ }
218
+ return url;
219
+ }
220
+ return url;
221
+ } catch (error) {
222
+ if (error && typeof error === "object" && "message" in error) throw error;
223
+ throw new Error(`URL validation failed: ${error}`);
224
+ }
225
+ }
226
+ /**
227
+ * Check if a URL is safe to connect to (non-throwing version).
228
+ *
229
+ * @param url URL to check
230
+ * @param options.allowPrivate Allow private IPs (default: false)
231
+ * @param options.allowHttp Allow http:// scheme (default: false)
232
+ * @returns true if URL is safe, false otherwise
233
+ */
234
+ function isSafeUrl(url, options) {
235
+ try {
236
+ validateSafeUrl(url, options);
237
+ return true;
238
+ } catch {
239
+ return false;
240
+ }
241
+ }
242
+ /**
243
+ * Check if two URLs have the same origin (scheme, host, port).
244
+ * Uses semantic URL parsing to prevent SSRF bypasses via URL variations.
245
+ *
246
+ * @param url1 First URL
247
+ * @param url2 Second URL
248
+ * @returns true if both URLs have the same origin, false otherwise
249
+ */
250
+ function isSameOrigin(url1, url2) {
251
+ try {
252
+ return new URL(url1).origin === new URL(url2).origin;
253
+ } catch {
254
+ return false;
255
+ }
256
+ }
257
+
258
+ //#endregion
259
+ exports.isCloudMetadata = isCloudMetadata;
260
+ exports.isLocalhost = isLocalhost;
261
+ exports.isPrivateIp = isPrivateIp;
262
+ exports.isSafeUrl = isSafeUrl;
263
+ exports.isSameOrigin = isSameOrigin;
264
+ Object.defineProperty(exports, 'ssrf_exports', {
265
+ enumerable: true,
266
+ get: function () {
267
+ return ssrf_exports;
268
+ }
269
+ });
270
+ exports.validateSafeUrl = validateSafeUrl;
271
+ //# sourceMappingURL=ssrf.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ssrf.cjs","names":["ip: string","result: number[]","parts","cidr: string","hostname: string","ip?: string","url: string","options?: { allowPrivate?: boolean; allowHttp?: boolean }","parsedUrl: URL","url1: string","url2: string"],"sources":["../../src/utils/ssrf.ts"],"sourcesContent":["// Private IP ranges (RFC 1918, loopback, link-local, etc.)\nconst PRIVATE_IP_RANGES = [\n \"10.0.0.0/8\",\n \"172.16.0.0/12\",\n \"192.168.0.0/16\",\n \"127.0.0.0/8\",\n \"169.254.0.0/16\",\n \"0.0.0.0/8\",\n \"::1/128\",\n \"fc00::/7\",\n \"fe80::/10\",\n \"ff00::/8\",\n];\n\n// Cloud metadata IPs\nconst CLOUD_METADATA_IPS = [\n \"169.254.169.254\",\n \"169.254.170.2\",\n \"100.100.100.200\",\n];\n\n// Cloud metadata hostnames (case-insensitive)\nconst CLOUD_METADATA_HOSTNAMES = [\n \"metadata.google.internal\",\n \"metadata\",\n \"instance-data\",\n];\n\n// Localhost variations\nconst LOCALHOST_NAMES = [\"localhost\", \"localhost.localdomain\"];\n\n/**\n * IPv4 regex: four octets 0-255\n */\nconst IPV4_REGEX =\n /^(?:(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)\\.){3}(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)$/;\n\n/**\n * Check if a string is a valid IPv4 address.\n */\nfunction isIPv4(ip: string): boolean {\n return IPV4_REGEX.test(ip);\n}\n\n/**\n * Check if a string is a valid IPv6 address.\n * Uses expandIpv6 for validation.\n */\nfunction isIPv6(ip: string): boolean {\n return expandIpv6(ip) !== null;\n}\n\n/**\n * Check if a string is a valid IP address (IPv4 or IPv6).\n */\nfunction isIP(ip: string): boolean {\n return isIPv4(ip) || isIPv6(ip);\n}\n\n/**\n * Parse an IP address string to an array of integers (for IPv4) or an array of 16-bit values (for IPv6)\n * Returns null if the IP is invalid.\n */\nfunction parseIp(ip: string): number[] | null {\n if (isIPv4(ip)) {\n return ip.split(\".\").map((octet) => parseInt(octet, 10));\n } else if (isIPv6(ip)) {\n // Normalize IPv6\n const expanded = expandIpv6(ip);\n if (!expanded) return null;\n const parts = expanded.split(\":\");\n const result: number[] = [];\n for (const part of parts) {\n result.push(parseInt(part, 16));\n }\n return result;\n }\n return null;\n}\n\n/**\n * Expand compressed IPv6 address to full form.\n */\nfunction expandIpv6(ip: string): string | null {\n // Basic structural validation\n if (!ip || typeof ip !== \"string\") return null;\n\n // Must contain at least one colon\n if (!ip.includes(\":\")) return null;\n\n // Check for invalid characters\n if (!/^[0-9a-fA-F:]+$/.test(ip)) return null;\n\n let normalized = ip;\n\n // Handle :: compression\n if (normalized.includes(\"::\")) {\n const parts = normalized.split(\"::\");\n if (parts.length > 2) return null; // Multiple :: is invalid\n\n const [left, right] = parts;\n const leftParts = left ? left.split(\":\") : [];\n const rightParts = right ? right.split(\":\") : [];\n const missing = 8 - (leftParts.length + rightParts.length);\n\n if (missing < 0) return null;\n\n const zeros = Array(missing).fill(\"0\");\n normalized = [...leftParts, ...zeros, ...rightParts]\n .filter((p) => p !== \"\")\n .join(\":\");\n }\n\n const parts = normalized.split(\":\");\n if (parts.length !== 8) return null;\n\n // Validate each part is a valid hex group (1-4 chars)\n for (const part of parts) {\n if (part.length === 0 || part.length > 4) return null;\n if (!/^[0-9a-fA-F]+$/.test(part)) return null;\n }\n\n return parts.map((p) => p.padStart(4, \"0\").toLowerCase()).join(\":\");\n}\n\n/**\n * Parse CIDR notation (e.g., \"192.168.0.0/24\") into network address and prefix length.\n */\nfunction parseCidr(\n cidr: string\n): { addr: number[]; prefixLen: number; isIpv6: boolean } | null {\n const [addrStr, prefixStr] = cidr.split(\"/\");\n if (!addrStr || !prefixStr) {\n return null;\n }\n\n const addr = parseIp(addrStr);\n if (!addr) {\n return null;\n }\n\n const prefixLen = parseInt(prefixStr, 10);\n if (isNaN(prefixLen)) {\n return null;\n }\n\n const isIpv6 = isIPv6(addrStr);\n\n if (isIpv6 && prefixLen > 128) {\n return null;\n }\n if (!isIpv6 && prefixLen > 32) {\n return null;\n }\n\n return { addr, prefixLen, isIpv6 };\n}\n\n/**\n * Check if an IP address is in a given CIDR range.\n */\nfunction isIpInCidr(ip: string, cidr: string): boolean {\n const ipParsed = parseIp(ip);\n if (!ipParsed) {\n return false;\n }\n\n const cidrParsed = parseCidr(cidr);\n if (!cidrParsed) {\n return false;\n }\n\n // Check IPv4 vs IPv6 mismatch\n const isIpv6 = isIPv6(ip);\n if (isIpv6 !== cidrParsed.isIpv6) {\n return false;\n }\n\n const { addr: cidrAddr, prefixLen } = cidrParsed;\n\n // Convert to bits and compare\n if (isIpv6) {\n // IPv6: each element is 16 bits\n for (let i = 0; i < Math.ceil(prefixLen / 16); i++) {\n const bitsToCheck = Math.min(16, prefixLen - i * 16);\n const mask = (0xffff << (16 - bitsToCheck)) & 0xffff;\n if ((ipParsed[i] & mask) !== (cidrAddr[i] & mask)) {\n return false;\n }\n }\n } else {\n // IPv4: each element is 8 bits\n for (let i = 0; i < Math.ceil(prefixLen / 8); i++) {\n const bitsToCheck = Math.min(8, prefixLen - i * 8);\n const mask = (0xff << (8 - bitsToCheck)) & 0xff;\n if ((ipParsed[i] & mask) !== (cidrAddr[i] & mask)) {\n return false;\n }\n }\n }\n\n return true;\n}\n\n/**\n * Check if an IP address is private (RFC 1918, loopback, link-local, etc.)\n */\nexport function isPrivateIp(ip: string): boolean {\n // Validate it's a proper IP\n if (!isIP(ip)) {\n return false;\n }\n\n for (const range of PRIVATE_IP_RANGES) {\n if (isIpInCidr(ip, range)) {\n return true;\n }\n }\n\n return false;\n}\n\n/**\n * Check if a hostname or IP is a known cloud metadata endpoint.\n */\nexport function isCloudMetadata(hostname: string, ip?: string): boolean {\n // Check if it's a known metadata IP\n if (CLOUD_METADATA_IPS.includes(ip || \"\")) {\n return true;\n }\n\n // Check if hostname matches (case-insensitive)\n const lowerHostname = hostname.toLowerCase();\n if (CLOUD_METADATA_HOSTNAMES.includes(lowerHostname)) {\n return true;\n }\n\n return false;\n}\n\n/**\n * Check if a hostname or IP is localhost.\n */\nexport function isLocalhost(hostname: string, ip?: string): boolean {\n // Check if it's a localhost IP\n if (ip) {\n // Check for typical localhost IPs (loopback range)\n if (ip === \"127.0.0.1\" || ip === \"::1\" || ip === \"0.0.0.0\") {\n return true;\n }\n // Check if IP starts with 127. (entire loopback range)\n if (ip.startsWith(\"127.\")) {\n return true;\n }\n }\n\n // Check if hostname is localhost\n const lowerHostname = hostname.toLowerCase();\n if (LOCALHOST_NAMES.includes(lowerHostname)) {\n return true;\n }\n\n return false;\n}\n\n/**\n * Validate that a URL is safe to connect to.\n * Performs static validation checks against hostnames and direct IP addresses.\n * Does not perform DNS resolution.\n *\n * @param url URL to validate\n * @param options.allowPrivate Allow private IPs (default: false)\n * @param options.allowHttp Allow http:// scheme (default: false)\n * @returns The validated URL\n * @throws Error if URL is not safe\n */\nexport function validateSafeUrl(\n url: string,\n options?: { allowPrivate?: boolean; allowHttp?: boolean }\n): string {\n const allowPrivate = options?.allowPrivate ?? false;\n const allowHttp = options?.allowHttp ?? false;\n\n try {\n let parsedUrl: URL;\n try {\n parsedUrl = new URL(url);\n } catch {\n throw new Error(`Invalid URL: ${url}`);\n }\n\n const hostname = parsedUrl.hostname;\n if (!hostname) {\n throw new Error(\"URL missing hostname.\");\n }\n\n // Check if it's a cloud metadata endpoint (always blocked)\n if (isCloudMetadata(hostname)) {\n throw new Error(`URL points to cloud metadata endpoint: ${hostname}`);\n }\n\n // Check if it's localhost (blocked unless allowPrivate is true)\n if (isLocalhost(hostname)) {\n if (!allowPrivate) {\n throw new Error(`URL points to localhost: ${hostname}`);\n }\n return url;\n }\n\n // Check scheme (after localhost checks to give better error messages)\n const scheme = parsedUrl.protocol;\n if (scheme !== \"http:\" && scheme !== \"https:\") {\n throw new Error(\n `Invalid URL scheme: ${scheme}. Only http and https are allowed.`\n );\n }\n\n if (scheme === \"http:\" && !allowHttp) {\n throw new Error(\n \"HTTP scheme not allowed. Use HTTPS or set allowHttp: true.\"\n );\n }\n\n // If hostname is already an IP, validate it directly\n if (isIP(hostname)) {\n const ip = hostname;\n\n // Check if it's localhost first (before private IP check)\n if (isLocalhost(hostname, ip)) {\n if (!allowPrivate) {\n throw new Error(`URL points to localhost: ${hostname}`);\n }\n return url;\n }\n\n // Cloud metadata is always blocked\n if (isCloudMetadata(hostname, ip)) {\n throw new Error(\n `URL resolves to cloud metadata IP: ${ip} (${hostname})`\n );\n }\n\n // Check private IPs\n if (isPrivateIp(ip)) {\n if (!allowPrivate) {\n throw new Error(\n `URL resolves to private IP: ${ip} (${hostname}). Set allowPrivate: true to allow.`\n );\n }\n }\n\n return url;\n }\n\n // For regular hostnames, we've already done all hostname-based checks above\n // (cloud metadata, localhost). If those passed, the URL is safe.\n // We don't perform DNS resolution in this environment-agnostic function.\n return url;\n } catch (error) {\n if (error && typeof error === \"object\" && \"message\" in error) {\n throw error;\n }\n throw new Error(`URL validation failed: ${error}`);\n }\n}\n\n/**\n * Check if a URL is safe to connect to (non-throwing version).\n *\n * @param url URL to check\n * @param options.allowPrivate Allow private IPs (default: false)\n * @param options.allowHttp Allow http:// scheme (default: false)\n * @returns true if URL is safe, false otherwise\n */\nexport function isSafeUrl(\n url: string,\n options?: { allowPrivate?: boolean; allowHttp?: boolean }\n): boolean {\n try {\n validateSafeUrl(url, options);\n return true;\n } catch {\n return false;\n }\n}\n\n/**\n * Check if two URLs have the same origin (scheme, host, port).\n * Uses semantic URL parsing to prevent SSRF bypasses via URL variations.\n *\n * @param url1 First URL\n * @param url2 Second URL\n * @returns true if both URLs have the same origin, false otherwise\n */\nexport function isSameOrigin(url1: string, url2: string): boolean {\n try {\n return new URL(url1).origin === new URL(url2).orig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S;AACT,KAAI;EACF,gBAAgB,KAAK,QAAQ;AAC7B,SAAO;CACR,QAAO;AACN,SAAO;CACR;AACF;;;;;;;;;AAUD,SAAgB,aAAaE,MAAcC,MAAuB;AAChE,KAAI;AACF,SAAO,IAAI,IAAI,MAAM,WAAW,IAAI,IAAI,MAAM;CAC/C,QAAO;AACN,SAAO;CACR;AACF"}
@@ -0,0 +1,52 @@
1
+ //#region src/utils/ssrf.d.ts
2
+ /**
3
+ * Check if an IP address is private (RFC 1918, loopback, link-local, etc.)
4
+ */
5
+ declare function isPrivateIp(ip: string): boolean;
6
+ /**
7
+ * Check if a hostname or IP is a known cloud metadata endpoint.
8
+ */
9
+ declare function isCloudMetadata(hostname: string, ip?: string): boolean;
10
+ /**
11
+ * Check if a hostname or IP is localhost.
12
+ */
13
+ declare function isLocalhost(hostname: string, ip?: string): boolean;
14
+ /**
15
+ * Validate that a URL is safe to connect to.
16
+ * Performs static validation checks against hostnames and direct IP addresses.
17
+ * Does not perform DNS resolution.
18
+ *
19
+ * @param url URL to validate
20
+ * @param options.allowPrivate Allow private IPs (default: false)
21
+ * @param options.allowHttp Allow http:// scheme (default: false)
22
+ * @returns The validated URL
23
+ * @throws Error if URL is not safe
24
+ */
25
+ declare function validateSafeUrl(url: string, options?: {
26
+ allowPrivate?: boolean;
27
+ allowHttp?: boolean;
28
+ }): string;
29
+ /**
30
+ * Check if a URL is safe to connect to (non-throwing version).
31
+ *
32
+ * @param url URL to check
33
+ * @param options.allowPrivate Allow private IPs (default: false)
34
+ * @param options.allowHttp Allow http:// scheme (default: false)
35
+ * @returns true if URL is safe, false otherwise
36
+ */
37
+ declare function isSafeUrl(url: string, options?: {
38
+ allowPrivate?: boolean;
39
+ allowHttp?: boolean;
40
+ }): boolean;
41
+ /**
42
+ * Check if two URLs have the same origin (scheme, host, port).
43
+ * Uses semantic URL parsing to prevent SSRF bypasses via URL variations.
44
+ *
45
+ * @param url1 First URL
46
+ * @param url2 Second URL
47
+ * @returns true if both URLs have the same origin, false otherwise
48
+ */
49
+ declare function isSameOrigin(url1: string, url2: string): boolean;
50
+ //#endregion
51
+ export { isCloudMetadata, isLocalhost, isPrivateIp, isSafeUrl, isSameOrigin, validateSafeUrl };
52
+ //# sourceMappingURL=ssrf.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ssrf.d.cts","names":["isPrivateIp","isCloudMetadata","isLocalhost","validateSafeUrl","isSafeUrl","isSameOrigin"],"sources":["../../src/utils/ssrf.d.ts"],"sourcesContent":["/**\n * Check if an IP address is private (RFC 1918, loopback, link-local, etc.)\n */\nexport declare function isPrivateIp(ip: string): boolean;\n/**\n * Check if a hostname or IP is a known cloud metadata endpoint.\n */\nexport declare function isCloudMetadata(hostname: string, ip?: string): boolean;\n/**\n * Check if a hostname or IP is localhost.\n */\nexport declare function isLocalhost(hostname: string, ip?: string): boolean;\n/**\n * Validate that a URL is safe to connect to.\n * Performs static validation checks against hostnames and direct IP addresses.\n * Does not perform DNS resolution.\n *\n * @param url URL to validate\n * @param options.allowPrivate Allow private IPs (default: false)\n * @param options.allowHttp Allow http:// scheme (default: false)\n * @returns The validated URL\n * @throws Error if URL is not safe\n */\nexport declare function validateSafeUrl(url: string, options?: {\n allowPrivate?: boolean;\n allowHttp?: boolean;\n}): string;\n/**\n * Check if a URL is safe to connect to (non-throwing version).\n *\n * @param url URL to check\n * @param options.allowPrivate Allow private IPs (default: false)\n * @param options.allowHttp Allow http:// scheme (default: false)\n * @returns true if URL is safe, false otherwise\n */\nexport declare function isSafeUrl(url: string, options?: {\n allowPrivate?: boolean;\n allowHttp?: boolean;\n}): boolean;\n/**\n * Check if two URLs have the same origin (scheme, host, port).\n * Uses semantic URL parsing to prevent SSRF bypasses via URL variations.\n *\n * @param url1 First URL\n * @param url2 Second URL\n * @returns true if both URLs have the same origin, false otherwise\n */\nexport declare function isSameOrigin(url1: string, url2: string): boolean;\n//# sourceMappingURL=ssrf.d.ts.map"],"mappings":";;AAGA;AAIA;AAIwBE,iBARAF,WAAAA,CAQW,EAAA,EAAA,MAAA,CAAA,EAAA,OAAA;AAYnC;AAYA;AAYA;iBAxCwBC,eAAAA;;;;iBAIAC,WAAAA;;;;;;;;;;;;iBAYAC,eAAAA;;;;;;;;;;;;iBAYAC,SAAAA;;;;;;;;;;;;iBAYAC,YAAAA"}
@@ -0,0 +1,52 @@
1
+ //#region src/utils/ssrf.d.ts
2
+ /**
3
+ * Check if an IP address is private (RFC 1918, loopback, link-local, etc.)
4
+ */
5
+ declare function isPrivateIp(ip: string): boolean;
6
+ /**
7
+ * Check if a hostname or IP is a known cloud metadata endpoint.
8
+ */
9
+ declare function isCloudMetadata(hostname: string, ip?: string): boolean;
10
+ /**
11
+ * Check if a hostname or IP is localhost.
12
+ */
13
+ declare function isLocalhost(hostname: string, ip?: string): boolean;
14
+ /**
15
+ * Validate that a URL is safe to connect to.
16
+ * Performs static validation checks against hostnames and direct IP addresses.
17
+ * Does not perform DNS resolution.
18
+ *
19
+ * @param url URL to validate
20
+ * @param options.allowPrivate Allow private IPs (default: false)
21
+ * @param options.allowHttp Allow http:// scheme (default: false)
22
+ * @returns The validated URL
23
+ * @throws Error if URL is not safe
24
+ */
25
+ declare function validateSafeUrl(url: string, options?: {
26
+ allowPrivate?: boolean;
27
+ allowHttp?: boolean;
28
+ }): string;
29
+ /**
30
+ * Check if a URL is safe to connect to (non-throwing version).
31
+ *
32
+ * @param url URL to check
33
+ * @param options.allowPrivate Allow private IPs (default: false)
34
+ * @param options.allowHttp Allow http:// scheme (default: false)
35
+ * @returns true if URL is safe, false otherwise
36
+ */
37
+ declare function isSafeUrl(url: string, options?: {
38
+ allowPrivate?: boolean;
39
+ allowHttp?: boolean;
40
+ }): boolean;
41
+ /**
42
+ * Check if two URLs have the same origin (scheme, host, port).
43
+ * Uses semantic URL parsing to prevent SSRF bypasses via URL variations.
44
+ *
45
+ * @param url1 First URL
46
+ * @param url2 Second URL
47
+ * @returns true if both URLs have the same origin, false otherwise
48
+ */
49
+ declare function isSameOrigin(url1: string, url2: string): boolean;
50
+ //#endregion
51
+ export { isCloudMetadata, isLocalhost, isPrivateIp, isSafeUrl, isSameOrigin, validateSafeUrl };
52
+ //# sourceMappingURL=ssrf.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ssrf.d.ts","names":["isPrivateIp","isCloudMetadata","isLocalhost","validateSafeUrl","isSafeUrl","isSameOrigin"],"sources":["../../src/utils/ssrf.d.ts"],"sourcesContent":["/**\n * Check if an IP address is private (RFC 1918, loopback, link-local, etc.)\n */\nexport declare function isPrivateIp(ip: string): boolean;\n/**\n * Check if a hostname or IP is a known cloud metadata endpoint.\n */\nexport declare function isCloudMetadata(hostname: string, ip?: string): boolean;\n/**\n * Check if a hostname or IP is localhost.\n */\nexport declare function isLocalhost(hostname: string, ip?: string): boolean;\n/**\n * Validate that a URL is safe to connect to.\n * Performs static validation checks against hostnames and direct IP addresses.\n * Does not perform DNS resolution.\n *\n * @param url URL to validate\n * @param options.allowPrivate Allow private IPs (default: false)\n * @param options.allowHttp Allow http:// scheme (default: false)\n * @returns The validated URL\n * @throws Error if URL is not safe\n */\nexport declare function validateSafeUrl(url: string, options?: {\n allowPrivate?: boolean;\n allowHttp?: boolean;\n}): string;\n/**\n * Check if a URL is safe to connect to (non-throwing version).\n *\n * @param url URL to check\n * @param options.allowPrivate Allow private IPs (default: false)\n * @param options.allowHttp Allow http:// scheme (default: false)\n * @returns true if URL is safe, false otherwise\n */\nexport declare function isSafeUrl(url: string, options?: {\n allowPrivate?: boolean;\n allowHttp?: boolean;\n}): boolean;\n/**\n * Check if two URLs have the same origin (scheme, host, port).\n * Uses semantic URL parsing to prevent SSRF bypasses via URL variations.\n *\n * @param url1 First URL\n * @param url2 Second URL\n * @returns true if both URLs have the same origin, false otherwise\n */\nexport declare function isSameOrigin(url1: string, url2: string): boolean;\n//# sourceMappingURL=ssrf.d.ts.map"],"mappings":";;AAGA;AAIA;AAIwBE,iBARAF,WAAAA,CAQW,EAAA,EAAA,MAAA,CAAA,EAAA,OAAA;AAYnC;AAYA;AAYA;iBAxCwBC,eAAAA;;;;iBAIAC,WAAAA;;;;;;;;;;;;iBAYAC,eAAAA;;;;;;;;;;;;iBAYAC,SAAAA;;;;;;;;;;;;iBAYAC,YAAAA"}