From 33db0d7b4a540c1ecd6cb30d6d318c65aa719ced Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=AB=98=E9=AD=8F=E6=B4=AA?= Date: Fri, 20 Mar 2026 14:54:06 +0800 Subject: [PATCH] fix: layer code support http bug --- __tests__/e2e/ci-mac-linux.sh | 4 ++++ publish.yaml | 2 +- src/utils/index.ts | 40 +++++++++++++++++++++++++++++++++-- 3 files changed, 43 insertions(+), 3 deletions(-) diff --git a/__tests__/e2e/ci-mac-linux.sh b/__tests__/e2e/ci-mac-linux.sh index 1037456..9525504 100755 --- a/__tests__/e2e/ci-mac-linux.sh +++ b/__tests__/e2e/ci-mac-linux.sh @@ -156,6 +156,10 @@ s deploy --trigger -t s2.yaml s deploy --async-invoke-config -t s2.yaml s info -t s2.yaml s remove -y -t s2.yaml + +echo "test layer code support http" +s layer publish --layer-name test --code https://images.devsapp.cn/zip/devs_fc3_layer_test_url_test.zip --compatible-runtime custom.debian12 +s layer remove --layer-name test -y cd .. echo " ********* test scaling config *********" diff --git a/publish.yaml b/publish.yaml index 2713075..01b753f 100644 --- a/publish.yaml +++ b/publish.yaml @@ -3,7 +3,7 @@ Type: Component Name: fc3 Provider: - 阿里云 -Version: 0.1.17 +Version: 0.1.18 Description: 阿里云函数计算全生命周期管理 HomePage: https://github.com/devsapp/fc3 Organization: 阿里云函数计算(FC) diff --git a/src/utils/index.ts b/src/utils/index.ts index 19bde4e..8566069 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -340,8 +340,12 @@ export async function _downloadFromUrl(url: string): Promise { logger.debug(`Downloaded file to: ${downloadPath}`); - // 返回下载文件路径,由主流程决定是否需要压缩 - return downloadPath; + const isDownloadedFileZip = await isZipFile(downloadPath); + if (!isDownloadedFileZip) { + throw new Error(`Downloaded file is not a valid zip file: ${downloadPath}`); + } + + return downloadPath.endsWith('.zip') ? downloadPath : `${downloadPath}.zip`; } catch (error) { // 如果下载失败,清理临时目录 try { @@ -356,3 +360,35 @@ export async function _downloadFromUrl(url: string): Promise { throw new Error(`Failed to download code from URL: ${error.message}`); } } + +/** + * 判断文件是否为ZIP文件 - 通过魔数检查 + */ +async function isZipFile(filePath: string): Promise { + try { + if (!fs.existsSync(filePath)) { + return false; + } + + // 检查文件头部的魔数(而非扩展名) + const buffer = Buffer.alloc(4); + const fd = fs.openSync(filePath, 'r'); + try { + fs.readSync(fd, buffer, 0, 4, 0); + } finally { + fs.closeSync(fd); + } + + // ZIP文件的魔数是 50 4B 03 04 (十六进制) + const isZipMagicNumber = + buffer[0] === 0x50 && + buffer[1] === 0x4b && + (buffer[2] === 0x03 || buffer[2] === 0x05 || buffer[2] === 0x07) && + (buffer[3] === 0x04 || buffer[3] === 0x06 || buffer[3] === 0x08); + + return isZipMagicNumber; + } catch (error) { + logger.debug(`Error checking if file is zip: ${error.message}`); + return false; + } +}