-
Notifications
You must be signed in to change notification settings - Fork 5
fix: layer publish support code is http url,model remove #144
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,10 +4,13 @@ import Table from 'tty-table'; | |
| import * as crc64 from 'crc64-ecma182.js'; | ||
| import { promisify } from 'util'; | ||
| import * as fs from 'fs'; | ||
| import os from 'os'; | ||
| import logger from '../logger'; | ||
| import { execSync } from 'child_process'; | ||
| import axios from 'axios'; | ||
| import { FC_API_ERROR_CODE, isInvalidArgument } from '../resources/fc/error-code'; | ||
| import path from 'path'; | ||
| import downloads from '@serverless-devs/downloads'; | ||
|
|
||
| export { default as verify } from './verify'; | ||
| export { default as runCommand } from './run-command'; | ||
|
|
@@ -261,8 +264,8 @@ export function getUserAgent(userAgent: string, command: string) { | |
| /** | ||
| * 验证并规范化路径 | ||
| */ | ||
| export function checkFcDir(path: string, paramName = 'path'): string { | ||
| const normalizedPath = path.trim(); | ||
| export function checkFcDir(inputPath: string, paramName = 'path'): string { | ||
| const normalizedPath = inputPath.trim(); | ||
|
|
||
| if (!normalizedPath.startsWith('/')) { | ||
| throw new Error(`${paramName} does not start with '/'`); | ||
|
|
@@ -308,3 +311,48 @@ export function checkFcDir(path: string, paramName = 'path'): string { | |
|
|
||
| return normalizedPath; | ||
| } | ||
|
|
||
| /** | ||
| * 从URL下载文件到本地临时目录 | ||
| */ | ||
| export async function _downloadFromUrl(url: string): Promise<string> { | ||
| // 创建临时目录 | ||
| const tempDir = path.join(os.tmpdir(), 'fc_code_download'); | ||
| let downloadPath: string; | ||
|
|
||
| try { | ||
| // 确保临时目录存在 | ||
| if (!fs.existsSync(tempDir)) { | ||
| fs.mkdirSync(tempDir, { recursive: true }); | ||
| } | ||
|
|
||
| // 从URL获取文件名 | ||
| const urlPath = new URL(url).pathname; | ||
| const parsedPathName = path.parse(urlPath).name; | ||
| const filename = path.basename(urlPath) || `downloaded_code_${Date.now()}`; | ||
| downloadPath = path.join(tempDir, filename); | ||
|
|
||
| await downloads(url, { | ||
| dest: tempDir, | ||
| filename: parsedPathName, | ||
| extract: false, | ||
| }); | ||
|
|
||
| logger.debug(`Downloaded file to: ${downloadPath}`); | ||
|
|
||
| // 返回下载文件路径,由主流程决定是否需要压缩 | ||
| return downloadPath; | ||
| } catch (error) { | ||
| // 如果下载失败,清理临时目录 | ||
| try { | ||
| if (fs.existsSync(tempDir)) { | ||
| fs.rmSync(tempDir, { recursive: true, force: true }); | ||
| logger.debug(`Cleaned up temporary directory after error: ${tempDir}`); | ||
| } | ||
| } catch (cleanupError) { | ||
| logger.debug(`Failed to clean up temporary directory: ${cleanupError.message}`); | ||
| } | ||
|
|
||
| throw new Error(`Failed to download code from URL: ${error.message}`); | ||
| } | ||
|
Comment on lines
+345
to
+357
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Error cleanup removes entire temp directory, potentially affecting concurrent operations. On error, the function removes the entire Consider cleaning up only the specific downloaded file rather than the entire directory: 🛡️ Proposed fix } catch (error) {
- // 如果下载失败,清理临时目录
+ // 如果下载失败,清理下载的文件
try {
- if (fs.existsSync(tempDir)) {
- fs.rmSync(tempDir, { recursive: true, force: true });
- logger.debug(`Cleaned up temporary directory after error: ${tempDir}`);
+ if (downloadPath && fs.existsSync(downloadPath)) {
+ fs.rmSync(downloadPath, { force: true });
+ logger.debug(`Cleaned up downloaded file after error: ${downloadPath}`);
}
} catch (cleanupError) {
- logger.debug(`Failed to clean up temporary directory: ${cleanupError.message}`);
+ logger.debug(`Failed to clean up downloaded file: ${cleanupError.message}`);
}
throw new Error(`Failed to download code from URL: ${error.message}`);
}🤖 Prompt for AI Agents |
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Critical: Mismatched filename causes returned path to reference a non-existent file.
The function computes two different filenames:
filename = path.basename(urlPath)→ e.g.,"code.zip"parsedPathName = path.parse(urlPath).name→ e.g.,"code"(without extension)The
downloadPathis computed usingfilename, butdownloads()saves the file usingparsedPathName. This causes the returned path to point to a file that doesn't exist.For example, with URL
https://example.com/code.zip:/tmp/fc_code_download/code.zip/tmp/fc_code_download/code🐛 Proposed fix
// 从URL获取文件名 const urlPath = new URL(url).pathname; - const parsedPathName = path.parse(urlPath).name; const filename = path.basename(urlPath) || `downloaded_code_${Date.now()}`; downloadPath = path.join(tempDir, filename); await downloads(url, { dest: tempDir, - filename: parsedPathName, + filename: filename, extract: false, });📝 Committable suggestion
🤖 Prompt for AI Agents