import { memoize } from 'lodash'; import path from 'path'; import request from '../../../utils/request'; export function guid() { return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) { const r = (Math.random() * 16) | 0, v = c == 'x' ? r : (r & 0x3) | 0x8; return v.toString(16); }); } /* eslint-disable no-irregular-whitespace */ interface SpaceConfig { privateKey: string; } async function requestServerSecret(appId: string, spaceId: string): Promise { const { privateKey } = await request({ method: 'GET', host: 'ide', path: '/cli/cloud/space/config.json', needSign: true, data: { appId, spaceId, }, }).catch((e) => { // 接口出错,删除对应缓存 getServerSecret.cache.delete(appId + spaceId); return Promise.reject(e); }); const list = privateKey.split('\\n'); return `${list[0]} ${list[1]} ${list[2]}`; } // 第二个参数设置缓存key export const getServerSecret = memoize(requestServerSecret, function (...args) { return args.join(''); }); export async function requireLocalFunction(functionRootPath, name) { const entry = path.join(functionRootPath, name, 'index.js'); // 用户的require模块会缓存 // eslint-disable-next-line const fn = require(entry); if (!fn || (typeof fn !== 'function' && typeof fn.default !== 'function')) { throw new Error('入口函数必须为 module.exports'); } return typeof fn === 'function' ? fn : fn.default; }