From 0225340ccbba2781e49689640ded606a8379dd45 Mon Sep 17 00:00:00 2001 From: dblythy Date: Tue, 14 Sep 2021 22:10:37 +1000 Subject: [PATCH] refactor: allow ES import for cloud string if package type is module (#7560) * allow module import for Parse Cloud * Update .babelrc * catch esm error * Update ParseServer.js * add tests * Update CHANGELOG.md * Update CloudCode.spec.js Co-authored-by: Manuel <5673677+mtrezza@users.noreply.github.com> --- .babelrc | 3 ++- CHANGELOG.md | 9 +++++---- spec/CloudCode.spec.js | 8 ++++++++ spec/cloud/cloudCodeModuleFile.js | 3 +++ src/ParseServer.js | 6 +++++- 5 files changed, 23 insertions(+), 6 deletions(-) create mode 100644 spec/cloud/cloudCodeModuleFile.js diff --git a/.babelrc b/.babelrc index 9151969bde..a199154b82 100644 --- a/.babelrc +++ b/.babelrc @@ -7,7 +7,8 @@ ["@babel/preset-env", { "targets": { "node": "12" - } + }, + "exclude": ["proposal-dynamic-import"] }] ], "sourceMaps": "inline" diff --git a/CHANGELOG.md b/CHANGELOG.md index b329847255..70ff5620f9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -151,6 +151,7 @@ ___ - Refactor: uniform issue templates across repos (Manuel Trezza) [#7528](https://github.com/parse-community/parse-server/pull/7528) - ci: bump ci environment (Manuel Trezza) [#7539](https://github.com/parse-community/parse-server/pull/7539) - CI now pushes docker images to Docker Hub (Corey Baker) [#7548](https://github.com/parse-community/parse-server/pull/7548) +- Allow cloud string for ES modules (Daniel Blyth) [#7560](https://github.com/parse-community/parse-server/pull/7560) - docs: Introduce deprecation ID for reference in comments and online search (Manuel Trezza) [#7562](https://github.com/parse-community/parse-server/pull/7562) ## 4.10.3 @@ -178,15 +179,15 @@ ___ *Versions >4.5.2 and <4.10.0 are skipped.* -> ⚠️ A security incident caused a number of incorrect version tags to be pushed to the Parse Server repository. These version tags linked to a personal fork of a contributor who had write access to the repository. The code to which these tags linked has not been reviewed or approved by Parse Platform. Even though no releases were published with these incorrect versions, it was possible to define a Parse Server dependency that pointed to these version tags, for example if you defined this dependency: +> ⚠️ A security incident caused a number of incorrect version tags to be pushed to the Parse Server repository. These version tags linked to a personal fork of a contributor who had write access to the repository. The code to which these tags linked has not been reviewed or approved by Parse Platform. Even though no releases were published with these incorrect versions, it was possible to define a Parse Server dependency that pointed to these version tags, for example if you defined this dependency: > ```js > "parse-server": "git@github.com:parse-community/parse-server.git#4.9.3" > ``` -> +> > We have since deleted the incorrect version tags, but they may still show up if your personal fork on GitHub or locally. We do not know when these tags have been pushed to the Parse Server repository, but we first became aware of this issue on July 21, 2021. We are not aware of any malicious code or concerns related to privacy, security or legality (e.g. proprietary code). However, it has been reported that some functionality does not work as expected and the introduction of security vulnerabilities cannot be ruled out. > -> You may be also affected if you used the Bitnami image for Parse Server. Bitnami picked up the incorrect version tag `4.9.3` and published a new Bitnami image for Parse Server. -> +> You may be also affected if you used the Bitnami image for Parse Server. Bitnami picked up the incorrect version tag `4.9.3` and published a new Bitnami image for Parse Server. +> >**If you are using any of the affected versions, we urgently recommend to upgrade to version `4.10.0`.** ## 4.5.2 diff --git a/spec/CloudCode.spec.js b/spec/CloudCode.spec.js index 86a7627427..07d94a366f 100644 --- a/spec/CloudCode.spec.js +++ b/spec/CloudCode.spec.js @@ -39,6 +39,14 @@ describe('Cloud Code', () => { }); }); + it('can load cloud code as a module', async () => { + process.env.npm_package_type = 'module'; + await reconfigureServer({ cloud: './spec/cloud/cloudCodeModuleFile.js' }); + const result = await Parse.Cloud.run('cloudCodeInFile'); + expect(result).toEqual('It is possible to define cloud code in a file.'); + delete process.env.npm_package_type; + }); + it('can create functions', done => { Parse.Cloud.define('hello', () => { return 'Hello world!'; diff --git a/spec/cloud/cloudCodeModuleFile.js b/spec/cloud/cloudCodeModuleFile.js new file mode 100644 index 0000000000..a62b4fcc24 --- /dev/null +++ b/spec/cloud/cloudCodeModuleFile.js @@ -0,0 +1,3 @@ +Parse.Cloud.define('cloudCodeInFile', () => { + return 'It is possible to define cloud code in a file.'; +}); diff --git a/src/ParseServer.js b/src/ParseServer.js index 43996ac751..a8b0ad38cf 100644 --- a/src/ParseServer.js +++ b/src/ParseServer.js @@ -103,7 +103,11 @@ class ParseServer { if (typeof cloud === 'function') { cloud(Parse); } else if (typeof cloud === 'string') { - require(path.resolve(process.cwd(), cloud)); + if (process.env.npm_package_type === 'module') { + import(path.resolve(process.cwd(), cloud)); + } else { + require(path.resolve(process.cwd(), cloud)); + } } else { throw "argument 'cloud' must either be a string or a function"; }