diff --git a/packages/app-console/src/components/FunctionEditor/types/globals.js b/packages/app-console/src/components/FunctionEditor/types/globals.js index 0d4ecd510f..13ab0c48bb 100644 --- a/packages/app-console/src/components/FunctionEditor/types/globals.js +++ b/packages/app-console/src/components/FunctionEditor/types/globals.js @@ -92,6 +92,8 @@ interface IExports { declare const module: IModule declare const exports: IExports declare const console: FunctionConsole +declare const global: typeof globalThis + /** * 主函数,云函数的入口函数 */ diff --git a/packages/app-service/package-lock.json b/packages/app-service/package-lock.json index 9686a77815..e0ec4a8b98 100644 --- a/packages/app-service/package-lock.json +++ b/packages/app-service/package-lock.json @@ -13,6 +13,7 @@ "@aws-sdk/s3-request-presigner": "^3.72.0", "alipay-sdk": "^3.1.7", "axios": "^0.21.1", + "cross-fetch": "^3.1.5", "database-proxy": "^0.8.2", "dayjs": "^1.10.7", "dotenv": "^8.2.0", @@ -2179,6 +2180,14 @@ "sha.js": "^2.4.8" } }, + "node_modules/cross-fetch": { + "version": "3.1.5", + "resolved": "https://registry.npmjs.org/cross-fetch/-/cross-fetch-3.1.5.tgz", + "integrity": "sha512-lvb1SBsI0Z7GDwmuid+mU3kWVBwTVUbe7S0H52yaaAdQOXq2YktTCZdlAcNKFzE6QtRz0snpw9bNiPeOIkkQvw==", + "dependencies": { + "node-fetch": "2.6.7" + } + }, "node_modules/crypto-browserify": { "version": "3.12.0", "resolved": "https://registry.npmmirror.com/crypto-browserify/-/crypto-browserify-3.12.0.tgz", @@ -4066,6 +4075,44 @@ "node": ">= 0.4.0" } }, + "node_modules/node-fetch": { + "version": "2.6.7", + "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.7.tgz", + "integrity": "sha512-ZjMPFEfVx5j+y2yF35Kzx5sF7kDzxuDj6ziH4FFbOp87zKDZNx8yExJIb05OGF4Nlt9IHFIMBkRl41VdvcNdbQ==", + "dependencies": { + "whatwg-url": "^5.0.0" + }, + "engines": { + "node": "4.x || >=6.0.0" + }, + "peerDependencies": { + "encoding": "^0.1.0" + }, + "peerDependenciesMeta": { + "encoding": { + "optional": true + } + } + }, + "node_modules/node-fetch/node_modules/tr46": { + "version": "0.0.3", + "resolved": "https://registry.npmjs.org/tr46/-/tr46-0.0.3.tgz", + "integrity": "sha1-gYT9NH2snNwYWZLzpmIuFLnZq2o=" + }, + "node_modules/node-fetch/node_modules/webidl-conversions": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-3.0.1.tgz", + "integrity": "sha1-JFNCdeKnvGvnvIZhHMFq4KVlSHE=" + }, + "node_modules/node-fetch/node_modules/whatwg-url": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/whatwg-url/-/whatwg-url-5.0.0.tgz", + "integrity": "sha1-lmRU6HZUYuN2RNNib2dCzotwll0=", + "dependencies": { + "tr46": "~0.0.3", + "webidl-conversions": "^3.0.0" + } + }, "node_modules/node-modules-utils": { "version": "0.8.2", "resolved": "https://registry.npmjs.org/node-modules-utils/-/node-modules-utils-0.8.2.tgz", @@ -7239,6 +7286,14 @@ "sha.js": "^2.4.8" } }, + "cross-fetch": { + "version": "3.1.5", + "resolved": "https://registry.npmjs.org/cross-fetch/-/cross-fetch-3.1.5.tgz", + "integrity": "sha512-lvb1SBsI0Z7GDwmuid+mU3kWVBwTVUbe7S0H52yaaAdQOXq2YktTCZdlAcNKFzE6QtRz0snpw9bNiPeOIkkQvw==", + "requires": { + "node-fetch": "2.6.7" + } + }, "crypto-browserify": { "version": "3.12.0", "resolved": "https://registry.npmmirror.com/crypto-browserify/-/crypto-browserify-3.12.0.tgz", @@ -8775,6 +8830,35 @@ "resolved": "https://registry.npmjs.org/netmask/-/netmask-2.0.2.tgz", "integrity": "sha512-dBpDMdxv9Irdq66304OLfEmQ9tbNRFnFTuZiLo+bD+r332bBmMJ8GBLXklIXXgxd3+v9+KUnZaUR5PJMa75Gsg==" }, + "node-fetch": { + "version": "2.6.7", + "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.7.tgz", + "integrity": "sha512-ZjMPFEfVx5j+y2yF35Kzx5sF7kDzxuDj6ziH4FFbOp87zKDZNx8yExJIb05OGF4Nlt9IHFIMBkRl41VdvcNdbQ==", + "requires": { + "whatwg-url": "^5.0.0" + }, + "dependencies": { + "tr46": { + "version": "0.0.3", + "resolved": "https://registry.npmjs.org/tr46/-/tr46-0.0.3.tgz", + "integrity": "sha1-gYT9NH2snNwYWZLzpmIuFLnZq2o=" + }, + "webidl-conversions": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-3.0.1.tgz", + "integrity": "sha1-JFNCdeKnvGvnvIZhHMFq4KVlSHE=" + }, + "whatwg-url": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/whatwg-url/-/whatwg-url-5.0.0.tgz", + "integrity": "sha1-lmRU6HZUYuN2RNNib2dCzotwll0=", + "requires": { + "tr46": "~0.0.3", + "webidl-conversions": "^3.0.0" + } + } + } + }, "node-modules-utils": { "version": "0.8.2", "resolved": "https://registry.npmjs.org/node-modules-utils/-/node-modules-utils-0.8.2.tgz", diff --git a/packages/app-service/package.json b/packages/app-service/package.json index 53f8a333ca..0a8a3c057e 100644 --- a/packages/app-service/package.json +++ b/packages/app-service/package.json @@ -29,6 +29,7 @@ "@aws-sdk/s3-request-presigner": "^3.72.0", "alipay-sdk": "^3.1.7", "axios": "^0.21.1", + "cross-fetch": "^3.1.5", "database-proxy": "^0.8.2", "dayjs": "^1.10.7", "dotenv": "^8.2.0", diff --git a/packages/app-service/src/support/function-engine/engine.ts b/packages/app-service/src/support/function-engine/engine.ts index ad30adc01c..b4988b9288 100644 --- a/packages/app-service/src/support/function-engine/engine.ts +++ b/packages/app-service/src/support/function-engine/engine.ts @@ -1,3 +1,4 @@ +import { URL } from 'node:url' import * as vm from 'vm' import { nanosecond2ms } from '../utils' @@ -75,7 +76,7 @@ export class FunctionEngine { const _module = { exports: {} } - return { + const sandbox = { __context__: functionContext, __filename: functionContext.__function_name, module: _module, @@ -89,8 +90,13 @@ export class FunctionEngine { clearInterval: clearInterval, setTimeout: setTimeout, clearTimeout: clearTimeout, - process: { env: {} } + process: { env: {} }, + URL: URL, + global: null, } + + sandbox.global = sandbox + return sandbox } /** diff --git a/packages/app-service/src/support/function-engine/types.ts b/packages/app-service/src/support/function-engine/types.ts index 2fa51b3895..f15a01a99b 100644 --- a/packages/app-service/src/support/function-engine/types.ts +++ b/packages/app-service/src/support/function-engine/types.ts @@ -26,7 +26,8 @@ export interface RuntimeContext { __filename: string, process: { env: { [key: string]: string }, - } + }, + global: RuntimeContext } /**