|
1 | 1 | "use strict"; |
| 2 | +var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { |
| 3 | + function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } |
| 4 | + return new (P || (P = Promise))(function (resolve, reject) { |
| 5 | + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } |
| 6 | + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } |
| 7 | + function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } |
| 8 | + step((generator = generator.apply(thisArg, _arguments || [])).next()); |
| 9 | + }); |
| 10 | +}; |
2 | 11 | var __importDefault = (this && this.__importDefault) || function (mod) { |
3 | 12 | return (mod && mod.__esModule) ? mod : { "default": mod }; |
4 | 13 | }; |
5 | 14 | Object.defineProperty(exports, "__esModule", { value: true }); |
6 | 15 | const path_1 = __importDefault(require("path")); |
7 | 16 | const child_process_1 = require("child_process"); |
8 | | -const bootstrapFilePath = path_1.default.join(__dirname, '../../utils/runtime/nodejs/bootstrap.js'); |
9 | | -child_process_1.execFile('node', [bootstrapFilePath], { |
10 | | - cwd: path_1.default.dirname(bootstrapFilePath), |
11 | | - maxBuffer: 1024 * 1024, |
12 | | - timeout: 20, |
13 | | - env: Object.assign(Object.assign({}, process.env), { SCF_FUNCTION_HANDLER: 'index:main', SCF_FUNCTION_NAME: 'main', GLOBAL_USER_FILE_PATH: '/Users/hengechang/Desktop/cloudbase-demo/functions/test/' }) |
14 | | -}, (error, stdout, stderr) => { |
15 | | - if (error) { |
16 | | - console.log(error); |
| 17 | +const error_1 = require("../../error"); |
| 18 | +const utils_1 = require("../../utils"); |
| 19 | +const bootstrapFilePath = path_1.default.join(__dirname, '../../../runtime/nodejs/bootstrap.js'); |
| 20 | +function checkJSON(data) { |
| 21 | + try { |
| 22 | + JSON.parse(data); |
| 23 | + } |
| 24 | + catch (e) { |
| 25 | + throw new error_1.CloudBaseError('非法的 JSON 字符串'); |
17 | 26 | } |
18 | | - console.log(stdout); |
19 | | - console.log(stderr); |
20 | | -}); |
| 27 | +} |
| 28 | +function getDebugArgs(port = 9229) { |
| 29 | + return [ |
| 30 | + `--inspect-brk=0.0.0.0:${port}`, |
| 31 | + '--nolazy', |
| 32 | + '--expose-gc', |
| 33 | + '--max-semi-space-size=150', |
| 34 | + '--max-old-space-size=2707' |
| 35 | + ]; |
| 36 | +} |
| 37 | +function spawnNodeProcess(args, options) { |
| 38 | + const commonOptions = { |
| 39 | + cwd: path_1.default.dirname(bootstrapFilePath), |
| 40 | + windowsHide: true |
| 41 | + }; |
| 42 | + const exec = child_process_1.spawn('node', args, Object.assign(Object.assign({}, commonOptions), options)); |
| 43 | + exec.on('error', e => { |
| 44 | + console.log(`进程执行异常:${e.message}`); |
| 45 | + setTimeout(() => { }, 100); |
| 46 | + }); |
| 47 | + exec.stdout.on('data', data => { |
| 48 | + console.log(`${data}`); |
| 49 | + }); |
| 50 | + exec.stderr.on('data', data => { |
| 51 | + console.log(`${data}`); |
| 52 | + }); |
| 53 | + exec.on('close', code => { |
| 54 | + if (code !== 0) { |
| 55 | + console.log(`\n云函数执行异常退出,错误码:${code}`); |
| 56 | + } |
| 57 | + }); |
| 58 | +} |
| 59 | +function debugFunctionByPath(functionPath, options) { |
| 60 | + return __awaiter(this, void 0, void 0, function* () { |
| 61 | + const { params, debug, port } = options; |
| 62 | + params && checkJSON(params); |
| 63 | + const filePath = path_1.default.resolve(functionPath); |
| 64 | + utils_1.checkPathExist(filePath, true); |
| 65 | + let debugDirname; |
| 66 | + if (utils_1.isDirectory(filePath)) { |
| 67 | + const exists = utils_1.checkPathExist(path_1.default.join(filePath, 'index.js')); |
| 68 | + if (!exists) { |
| 69 | + throw new error_1.CloudBaseError('index.js 文件不存在!'); |
| 70 | + } |
| 71 | + debugDirname = filePath; |
| 72 | + } |
| 73 | + else { |
| 74 | + const { base, dir } = path_1.default.parse(filePath); |
| 75 | + if (base !== 'index.js') { |
| 76 | + throw new error_1.CloudBaseError('index.js 文件不存在!'); |
| 77 | + } |
| 78 | + debugDirname = dir; |
| 79 | + } |
| 80 | + try { |
| 81 | + const fileExports = require(path_1.default.join(debugDirname, 'index.js')); |
| 82 | + if (!fileExports.main) { |
| 83 | + throw new error_1.CloudBaseError('main 方法不存在!'); |
| 84 | + } |
| 85 | + } |
| 86 | + catch (e) { |
| 87 | + throw new error_1.CloudBaseError(`导入云函数异常:${e.message}`); |
| 88 | + } |
| 89 | + const debugArgs = getDebugArgs(port); |
| 90 | + const args = debug ? [...debugArgs, bootstrapFilePath] : [bootstrapFilePath]; |
| 91 | + console.log('> 以默认配置启动 Node 云函数调试'); |
| 92 | + spawnNodeProcess(args, { |
| 93 | + env: Object.assign(Object.assign({}, process.env), { SCF_FUNCTION_HANDLER: 'index.main', SCF_FUNCTION_NAME: 'main', GLOBAL_USER_FILE_PATH: path_1.default.join(debugDirname, path_1.default.sep), SCF_EVENT_BODY: params || '{}' }) |
| 94 | + }); |
| 95 | + }); |
| 96 | +} |
| 97 | +exports.debugFunctionByPath = debugFunctionByPath; |
| 98 | +function debugByConfig(ctx, options) { |
| 99 | + return __awaiter(this, void 0, void 0, function* () { |
| 100 | + const { name, config } = ctx; |
| 101 | + const { params, debug, port } = options; |
| 102 | + params && checkJSON(params); |
| 103 | + let functionPath = path_1.default.resolve(config.functionRoot, name); |
| 104 | + const filePath = path_1.default.resolve(functionPath); |
| 105 | + utils_1.checkPathExist(filePath, true); |
| 106 | + let debugDirname; |
| 107 | + const funcConfig = utils_1.findAndFlattenFunConfig(config, name); |
| 108 | + const handlers = (funcConfig.handler || 'index.js').split('.'); |
| 109 | + const indexFileName = handlers[0]; |
| 110 | + const indexFile = `${indexFileName}.js`; |
| 111 | + const mainFunction = handlers[1]; |
| 112 | + if (utils_1.isDirectory(filePath)) { |
| 113 | + const exists = utils_1.checkPathExist(path_1.default.join(filePath, indexFile)); |
| 114 | + if (!exists) { |
| 115 | + throw new error_1.CloudBaseError(`${indexFile} 文件不存在!`); |
| 116 | + } |
| 117 | + debugDirname = filePath; |
| 118 | + } |
| 119 | + else { |
| 120 | + const { base, dir } = path_1.default.parse(filePath); |
| 121 | + if (base !== indexFile) { |
| 122 | + throw new error_1.CloudBaseError(`${indexFile} 文件不存在!`); |
| 123 | + } |
| 124 | + debugDirname = dir; |
| 125 | + } |
| 126 | + try { |
| 127 | + const fileExports = require(path_1.default.join(debugDirname, indexFile)); |
| 128 | + if (!fileExports[mainFunction]) { |
| 129 | + throw new error_1.CloudBaseError(`handler 中的 ${mainFunction} 方法不存在,请检查你的配置!`); |
| 130 | + } |
| 131 | + } |
| 132 | + catch (e) { |
| 133 | + throw new error_1.CloudBaseError(`导入云函数异常:${e.message}`); |
| 134 | + } |
| 135 | + const debugArgs = getDebugArgs(port); |
| 136 | + const args = debug ? [...debugArgs, bootstrapFilePath] : [bootstrapFilePath]; |
| 137 | + spawnNodeProcess(args, { |
| 138 | + env: Object.assign(Object.assign(Object.assign({}, process.env), { SCF_FUNCTION_HANDLER: funcConfig.handler || 'index.main', SCF_FUNCTION_NAME: funcConfig.name || 'main', GLOBAL_USER_FILE_PATH: path_1.default.join(debugDirname, path_1.default.sep), SCF_EVENT_BODY: params || JSON.stringify(funcConfig.params || {}) }), funcConfig.envVariables) |
| 139 | + }); |
| 140 | + }); |
| 141 | +} |
| 142 | +exports.debugByConfig = debugByConfig; |
0 commit comments