Skip to content

Commit 6a1c2b8

Browse files
author
高魏洪
committed
fix: layer code support http bug
1 parent 3fe39c2 commit 6a1c2b8

File tree

3 files changed

+42
-4
lines changed

3 files changed

+42
-4
lines changed

__tests__/e2e/ci-mac-linux.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,10 @@ s deploy --trigger -t s2.yaml
156156
s deploy --async-invoke-config -t s2.yaml
157157
s info -t s2.yaml
158158
s remove -y -t s2.yaml
159+
160+
echo "test layer code support http"
161+
s layer publish --layer-name test --code https://test-gwh.oss-cn-hangzhou.aliyuncs.com/test.zip --compatible-runtime custom.debian12
162+
s layer remove --layer-name test -y
159163
cd ..
160164

161165
echo " ********* test scaling config *********"

publish.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Type: Component
33
Name: fc3
44
Provider:
55
- 阿里云
6-
Version: 0.1.17
6+
Version: 0.1.18
77
Description: 阿里云函数计算全生命周期管理
88
HomePage: https://github.com/devsapp/fc3
99
Organization: 阿里云函数计算(FC)

src/utils/index.ts

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -338,10 +338,15 @@ export async function _downloadFromUrl(url: string): Promise<string> {
338338
extract: false,
339339
});
340340

341-
logger.debug(`Downloaded file to: ${downloadPath}`);
341+
const downloadFilePath = downloadPath.endsWith('.zip') ? downloadPath : `${downloadPath}.zip`;
342+
logger.debug(`Downloaded file to: ${downloadFilePath}`);
342343

343-
// 返回下载文件路径,由主流程决定是否需要压缩
344-
return downloadPath;
344+
const isDownloadedFileZip = await isZipFile(downloadFilePath);
345+
if (!isDownloadedFileZip) {
346+
throw new Error(`Downloaded file is not a valid zip file: ${downloadFilePath}`);
347+
}
348+
349+
return downloadFilePath;
345350
} catch (error) {
346351
// 如果下载失败,清理临时目录
347352
try {
@@ -356,3 +361,32 @@ export async function _downloadFromUrl(url: string): Promise<string> {
356361
throw new Error(`Failed to download code from URL: ${error.message}`);
357362
}
358363
}
364+
365+
/**
366+
* 判断文件是否为ZIP文件 - 通过魔数检查
367+
*/
368+
async function isZipFile(filePath: string): Promise<boolean> {
369+
try {
370+
if (!fs.existsSync(filePath)) {
371+
return false;
372+
}
373+
374+
// 检查文件头部的魔数(而非扩展名)
375+
const buffer = Buffer.alloc(4);
376+
const fd = fs.openSync(filePath, 'r');
377+
fs.readSync(fd, buffer, 0, 4, 0);
378+
fs.closeSync(fd);
379+
380+
// ZIP文件的魔数是 50 4B 03 04 (十六进制)
381+
const isZipMagicNumber =
382+
buffer[0] === 0x50 &&
383+
buffer[1] === 0x4b &&
384+
(buffer[2] === 0x03 || buffer[2] === 0x05 || buffer[2] === 0x07) &&
385+
(buffer[3] === 0x04 || buffer[3] === 0x06 || buffer[3] === 0x08);
386+
387+
return isZipMagicNumber;
388+
} catch (error) {
389+
logger.debug(`Error checking if file is zip: ${error.message}`);
390+
return false;
391+
}
392+
}

0 commit comments

Comments
 (0)