Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
public async onDeploy (data: DeployData, next: Next) {
data.logger!.debug('[CloudFunction] 组装云函数配置');
data.logger!.debug('%o', data);
const config = deepMerge(data.config!.plugins![this.name || this.type], { config: this.config });
data.logger!.debug('[CloudFunction] 组装完成 %o', config);
// 引用服务商部署插件
// eslint-disable-next-line security/detect-non-literal-require, @typescript-eslint/no-var-requires
const Provider = require(config.provider.type);
const provider = new Provider(config.provider.config);
data.dependencies![config.provider.type as string] = loadNpmVersion(config.provider.type);
// 部署云函数
await provider.deploy(this.type, data, config);
await next();
}
public async deploy () {
const data = this.deployData;
const loadResult = await loadTs(data.filename, { tmp: true });
const func = loadResult.module;
if (!func) {
throw Error(`Func load failed: ${data.filename}`);
}
if (func.config) {
data.config = deepMerge(data.config, func.config);
}
data.dependencies = deepMerge(loadResult.dependencies, func.dependencies);
// 按类型分类插件
const includedCloudFunction = [];
for (let i = 0; i < func.plugins.length; i++) {
const plugin = func.plugins[i as number];
// 删除 require 缓存
if (!this.opts.cache && require.cache[cache.file as string]) {
delete require.cache[cache.file as string];
}
// 直接 require ts 文件
// eslint-disable-next-line security/detect-non-literal-require
func = require(cache.file).default;
} catch (error) {
this.logger.error(error);
// 删除 require 缓存
if (require.cache[cache.file + '.tmp.js' as string]) {
delete require.cache[cache.file + '.tmp.js' as string];
}
// 载入 ts 文件
try {
const ts = await loadTs(cache.file, { tmp: true });
func = ts.module;
} catch (error) {
this.logger.error(error);
res.statusCode = 500;
res.write(error.message);
res.end();
return reject(error);
}
}
}
try {
if (!cache.handler) {
// 读取云函数配置并写入缓存
func.config = loadConfig(this.root, path).development;
// eslint-disable-next-line require-atomic-updates
cache.handler = func.export().handler;
env: data.env,
dependencies: data.dependencies,
tmp: data.tmp,
// cos 参数
Bucket: `scf-${config.provider.config.appId}`,
FilePath: `${data.tmp}deploy.zip`,
CosObjectName: config.config.FunctionName + '/' + data.version + '.zip'
});
this.logger.debug('完成参数处理 %o', config);
this.logger.info('开始构建代码包');
this.logger.debug('生成 index.js');
await loadTs(config.config.filename, {
output: {
file: config.config.tmp + '/index.js',
format: 'cjs',
name: 'index',
banner: `/**
* @name ${config.config.name}
* @author ${process.env.LOGNAME}
* @build ${config.config.version}
* @staging ${config.config.env}
* @dependencies ${JSON.stringify(config.config.dependencies)}
*/`,
footer: `
const main = module.exports;
main.config = ${JSON.stringify(data.config, null, 2)};
module.exports = main.export();`
}
try {
const ts = await loadTs(cache.file, { tmp: true });
func = ts.module;
} catch (error) {
this.logger.error(error);
res.statusCode = 500;
res.write(error.message);
res.end();
return reject(error);
}
}
}
try {
if (!cache.handler) {
// 读取云函数配置并写入缓存
func.config = loadConfig(this.root, path).development;
// eslint-disable-next-line require-atomic-updates
cache.handler = func.export().handler;
}
let body = '';
req.on('readable', function () {
body += req.read() || '';
});
req.on('end', async () => {
const uri = URL.parse(req.url!);
let data;
try {
data = await cache.handler({
headers: req.headers,
constructor (data: DeployData) {
data.name = data.filename.replace(data.root, '').replace('.func.ts', '').replace(/^\/?[^/]+\//, '').replace(/\/$/, '');
data.version = new Date().toLocaleString('zh-CN', {
hour12: false,
timeZone: 'Asia/Shanghai',
}).replace(/(\/|:|\s)/g, '_');
data.logger = new Logger('Deployer');
const Config = loadConfig(data.root, data.filename);
if (!data.env) {
data.env = process.env.FaasEnv || Config.defaults.deploy.env;
}
data.config = Config[data.env!];
if (!data.config) {
throw Error(`Config load failed: ${data.env}`);
}
data.tmp = `${data.root}/tmp/${data.env}/${data.name}/${data.version}/`;
data.tmp.split('/').reduce(function (acc: string, cur: string) {
acc += '/' + cur;
if (!existsSync(acc)) {
public async onDeploy (data: DeployData, next: Next) {
switch (this.adapterType || data.config!.plugins[this.name || this.type].adapter) {
case 'sqlite':
data.dependencies!['sqlite3'] = loadNpmVersion('sqlite3')!;
break;
case 'postgresql':
data.dependencies!['pg'] = loadNpmVersion('pg')!;
break;
case 'mysql':
data.dependencies!['mysql'] = loadNpmVersion('mysql')!;
break;
default:
throw Error(`[Sql] Unsupport type: ${this.adapterType || data.config!.plugins[this.name || this.type].type}`);
}
await next();
}
public async onDeploy (data: DeployData, next: Next) {
switch (this.adapterType || data.config!.plugins[this.name || this.type].adapter) {
case 'sqlite':
data.dependencies!['sqlite3'] = loadNpmVersion('sqlite3')!;
break;
case 'postgresql':
data.dependencies!['pg'] = loadNpmVersion('pg')!;
break;
case 'mysql':
data.dependencies!['mysql'] = loadNpmVersion('mysql')!;
break;
default:
throw Error(`[Sql] Unsupport type: ${this.adapterType || data.config!.plugins[this.name || this.type].type}`);
}
await next();
}
public async onDeploy (data: DeployData, next: Next) {
switch (this.adapterType || data.config!.plugins[this.name || this.type].adapter) {
case 'sqlite':
data.dependencies!['sqlite3'] = loadNpmVersion('sqlite3')!;
break;
case 'postgresql':
data.dependencies!['pg'] = loadNpmVersion('pg')!;
break;
case 'mysql':
data.dependencies!['mysql'] = loadNpmVersion('mysql')!;
break;
default:
throw Error(`[Sql] Unsupport type: ${this.adapterType || data.config!.plugins[this.name || this.type].type}`);
}
await next();
}