Ver Fonte

refactor(testing): Remove node-libcurl dependency

Replaced with node-fetch implementation that does not require native modules to be compiled on install.
Michael Bromley há 5 anos atrás
pai
commit
c2ee5814f6
3 ficheiros alterados com 45 adições e 121 exclusões
  1. 1 1
      packages/testing/package.json
  2. 33 47
      packages/testing/src/simple-graphql-client.ts
  3. 11 73
      yarn.lock

+ 1 - 1
packages/testing/package.json

@@ -35,10 +35,10 @@
     "@types/node-fetch": "^2.5.4",
     "@vendure/common": "^0.12.1",
     "faker": "^4.1.0",
+    "form-data": "^3.0.0",
     "graphql": "^14.5.8",
     "graphql-tag": "^2.10.1",
     "node-fetch": "^2.6.0",
-    "node-libcurl": "^2.0.2",
     "sql.js": "1.1.0"
   },
   "devDependencies": {

+ 33 - 47
packages/testing/src/simple-graphql-client.ts

@@ -1,10 +1,11 @@
 import { SUPER_ADMIN_USER_IDENTIFIER, SUPER_ADMIN_USER_PASSWORD } from '@vendure/common/lib/shared-constants';
 import { VendureConfig } from '@vendure/core';
+import FormData from 'form-data';
+import fs from 'fs';
 import { DocumentNode } from 'graphql';
 import gql from 'graphql-tag';
 import { print } from 'graphql/language/printer';
 import fetch, { RequestInit, Response } from 'node-fetch';
-import { Curl } from 'node-libcurl';
 import { stringify } from 'querystring';
 
 import { QueryParams } from './types';
@@ -192,64 +193,49 @@ export class SimpleGraphQLClient {
 
     /**
      * @description
-     * Uses curl to post a multipart/form-data request to the server. Due to differences between the Node and browser
-     * environments, we cannot just use an existing library like apollo-upload-client.
+     * Perform a file upload mutation.
      *
      * Upload spec: https://github.com/jaydenseric/graphql-multipart-request-spec
      * Discussion of issue: https://github.com/jaydenseric/apollo-upload-client/issues/32
      */
-    fileUploadMutation(options: {
+    async fileUploadMutation(options: {
         mutation: DocumentNode;
         filePaths: string[];
         mapVariables: (filePaths: string[]) => any;
     }): Promise<any> {
         const { mutation, filePaths, mapVariables } = options;
-        return new Promise((resolve, reject) => {
-            const curl = new Curl();
 
-            const postData = createUploadPostData(mutation, filePaths, mapVariables);
-            const processedPostData = [
-                {
-                    name: 'operations',
-                    contents: JSON.stringify(postData.operations),
-                },
-                {
-                    name: 'map',
-                    contents:
-                        '{' +
-                        Object.entries(postData.map)
-                            .map(([i, path]) => `"${i}":["${path}"]`)
-                            .join(',') +
-                        '}',
-                },
-                ...postData.filePaths,
-            ];
-            curl.setOpt(Curl.option.URL, this.apiUrl);
-            curl.setOpt(Curl.option.VERBOSE, false);
-            curl.setOpt(Curl.option.TIMEOUT_MS, 30000);
-            curl.setOpt(Curl.option.HTTPPOST, processedPostData);
-            curl.setOpt(Curl.option.HTTPHEADER, [
-                `Authorization: Bearer ${this.authToken}`,
-                `${this.vendureConfig.apiOptions.channelTokenKey}: ${this.channelToken}`,
-            ]);
-            curl.perform();
-            curl.on('end', (statusCode: any, body: any) => {
-                curl.close();
-                const response = JSON.parse(body);
-                if (response.errors && response.errors.length) {
-                    const error = response.errors[0];
-                    console.log(JSON.stringify(error.extensions, null, 2));
-                    throw new Error(error.message);
-                }
-                resolve(response.data);
-            });
+        const postData = createUploadPostData(mutation, filePaths, mapVariables);
+        const body = new FormData();
+        body.append('operations', JSON.stringify(postData.operations));
+        body.append(
+            'map',
+            '{' +
+                Object.entries(postData.map)
+                    .map(([i, path]) => `"${i}":["${path}"]`)
+                    .join(',') +
+                '}',
+        );
+        for (const filePath of postData.filePaths) {
+            const file = fs.readFileSync(filePath.file);
+            body.append(filePath.name, file, { filename: filePath.file });
+        }
 
-            curl.on('error', (err: any) => {
-                curl.close();
-                console.log(err);
-                reject(err);
-            });
+        const result = await fetch(this.apiUrl, {
+            method: 'POST',
+            body,
+            headers: {
+                ...this.headers,
+            },
         });
+
+        const response = await result.json();
+        if (response.errors && response.errors.length) {
+            const error = response.errors[0];
+            console.log(JSON.stringify(error.extensions, null, 2));
+            throw new Error(error.message);
+        }
+        return response.data;
     }
 }
 

+ 11 - 73
yarn.lock

@@ -7814,16 +7814,11 @@ entities@^2.0.0:
   resolved "https://registry.npmjs.org/entities/-/entities-2.0.0.tgz#68d6084cab1b079767540d80e56a39b423e4abf4"
   integrity sha512-D9f7V0JSRwIxlRI2mjMqufDrRDnx8p+eEOz7aUM9SuvF8gsBzra0/6tbjl1m8eQHrZlYj6PxqE00hZ1SAIKPLw==
 
-env-paths@2.2.0, env-paths@^2.2.0:
+env-paths@^2.2.0:
   version "2.2.0"
   resolved "https://registry.npmjs.org/env-paths/-/env-paths-2.2.0.tgz#cdca557dc009152917d6166e2febe1f039685e43"
   integrity sha512-6u0VYSCo/OW6IoD5WCLLy9JUGARbamfSavcNXry/eu8aHVFei6CD3Sw+VGX5alea1i9pgPHW0mbu6Xj0uBh7gA==
 
-env-paths@^1.0.0:
-  version "1.0.0"
-  resolved "https://registry.npmjs.org/env-paths/-/env-paths-1.0.0.tgz#4168133b42bb05c38a35b1ae4397c8298ab369e0"
-  integrity sha1-QWgTO0K7BcOKNbGuQ5fIKYqzaeA=
-
 envinfo@^7.3.1:
   version "7.5.0"
   resolved "https://registry.npmjs.org/envinfo/-/envinfo-7.5.0.tgz#91410bb6db262fb4f1409bd506e9ff57e91023f4"
@@ -13180,7 +13175,7 @@ mz@^2.4.0, mz@^2.5.0:
     object-assign "^4.0.1"
     thenify-all "^1.0.0"
 
-nan@2.14.0, nan@^2.12.1:
+nan@^2.12.1:
   version "2.14.0"
   resolved "https://registry.npmjs.org/nan/-/nan-2.14.0.tgz#7818f722027b2459a86f0295d434d1fc2336c52c"
   integrity sha512-INOFj37C7k3AfaNTtX8RhsTw7qRy7eLET14cROi9+5HAVbbHuIWUHEauBv5qT4Av2tWasiTY1Jw6puUNqRJXQg==
@@ -13352,23 +13347,6 @@ node-forge@0.9.0:
   resolved "https://registry.npmjs.org/node-forge/-/node-forge-0.9.0.tgz#d624050edbb44874adca12bb9a52ec63cb782579"
   integrity sha512-7ASaDa3pD+lJ3WvXFsxekJQelBKRpne+GOVbLbtHYdd7pFspyeuJHnWfLplGf3SwKGbfs/aYl5V/JCIaHVUKKQ==
 
-node-gyp@5.0.3:
-  version "5.0.3"
-  resolved "https://registry.npmjs.org/node-gyp/-/node-gyp-5.0.3.tgz#80d64c23790244991b6d44532f0a351bedd3dd45"
-  integrity sha512-z/JdtkFGUm0QaQUusvloyYuGDub3nUbOo5de1Fz57cM++osBTvQatBUSTlF1k/w8vFHPxxXW6zxGvkxXSpaBkQ==
-  dependencies:
-    env-paths "^1.0.0"
-    glob "^7.0.3"
-    graceful-fs "^4.1.2"
-    mkdirp "^0.5.0"
-    nopt "2 || 3"
-    npmlog "0 || 1 || 2 || 3 || 4"
-    request "^2.87.0"
-    rimraf "2"
-    semver "~5.3.0"
-    tar "^4.4.8"
-    which "1"
-
 node-gyp@^5.0.2:
   version "5.1.0"
   resolved "https://registry.npmjs.org/node-gyp/-/node-gyp-5.1.0.tgz#8e31260a7af4a2e2f994b0673d4e0b3866156332"
@@ -13391,18 +13369,6 @@ node-int64@^0.4.0:
   resolved "https://registry.npmjs.org/node-int64/-/node-int64-0.4.0.tgz#87a9065cdb355d3182d8f94ce11188b825c68a3b"
   integrity sha1-h6kGXNs1XTGC2PlM4RGIuCXGijs=
 
-node-libcurl@^2.0.2:
-  version "2.0.3"
-  resolved "https://registry.npmjs.org/node-libcurl/-/node-libcurl-2.0.3.tgz#f2183e86ab9b33d4ada98d33b1efb277bda9d8da"
-  integrity sha512-0xrrxFZ6FBtbcdWy1mpeAAsvEacrD209gUMZ5cSBdNwVG8bCRwxpBfdVIe2xXHjiMsSm30ZnpfbcFEoRJjFyWQ==
-  dependencies:
-    env-paths "2.2.0"
-    nan "2.14.0"
-    node-gyp "5.0.3"
-    node-pre-gyp "0.13.0"
-    npmlog "4.1.2"
-    tslib "^1.10.0"
-
 node-libs-browser@^2.2.1:
   version "2.2.1"
   resolved "https://registry.npmjs.org/node-libs-browser/-/node-libs-browser-2.2.1.tgz#b64f513d18338625f90346d27b0d235e631f6425"
@@ -13448,22 +13414,6 @@ node-notifier@^6.0.0:
     shellwords "^0.1.1"
     which "^1.3.1"
 
-node-pre-gyp@0.13.0:
-  version "0.13.0"
-  resolved "https://registry.npmjs.org/node-pre-gyp/-/node-pre-gyp-0.13.0.tgz#df9ab7b68dd6498137717838e4f92a33fc9daa42"
-  integrity sha512-Md1D3xnEne8b/HGVQkZZwV27WUi1ZRuZBij24TNaZwUPU3ZAFtvT6xxJGaUVillfmMKnn5oD1HoGsp2Ftik7SQ==
-  dependencies:
-    detect-libc "^1.0.2"
-    mkdirp "^0.5.1"
-    needle "^2.2.1"
-    nopt "^4.0.1"
-    npm-packlist "^1.1.6"
-    npmlog "^4.0.2"
-    rc "^1.2.7"
-    rimraf "^2.6.1"
-    semver "^5.3.0"
-    tar "^4"
-
 node-pre-gyp@0.14.0:
   version "0.14.0"
   resolved "https://registry.npmjs.org/node-pre-gyp/-/node-pre-gyp-0.14.0.tgz#9a0596533b877289bcad4e143982ca3d904ddc83"
@@ -13520,13 +13470,6 @@ noop-logger@^0.1.1:
   resolved "https://registry.npmjs.org/noop-logger/-/noop-logger-0.1.1.tgz#94a2b1633c4f1317553007d8966fd0e841b6a4c2"
   integrity sha1-lKKxYzxPExdVMAfYlm/Q6EG2pMI=
 
-"nopt@2 || 3":
-  version "3.0.6"
-  resolved "https://registry.npmjs.org/nopt/-/nopt-3.0.6.tgz#c6465dbf08abcd4db359317f79ac68a646b28ff9"
-  integrity sha1-xkZdvwirzU2zWTF/eaxopkayj/k=
-  dependencies:
-    abbrev "1"
-
 nopt@^4.0.1, nopt@~4.0.1:
   version "4.0.3"
   resolved "https://registry.npmjs.org/nopt/-/nopt-4.0.3.tgz#a375cad9d02fd921278d954c2254d5aa57e15e48"
@@ -13706,7 +13649,7 @@ npm-run-path@^4.0.0:
   dependencies:
     path-key "^3.0.0"
 
-"npmlog@0 || 1 || 2 || 3 || 4", npmlog@4.1.2, npmlog@^4.0.1, npmlog@^4.0.2, npmlog@^4.1.2:
+npmlog@^4.0.1, npmlog@^4.0.2, npmlog@^4.1.2:
   version "4.1.2"
   resolved "https://registry.npmjs.org/npmlog/-/npmlog-4.1.2.tgz#08a7f2a8bf734604779a9efa4ad5cc717abb954b"
   integrity sha512-2uUqazuKlTaSI/dC8AzicUck7+IrEaOnN/e0jd3Xtt1KcGpwx30v50mL7oPyr/h9bL3E4aZccVwpwP+5W9Vjkg==
@@ -16126,13 +16069,6 @@ rgba-regex@^1.0.0:
   resolved "https://registry.npmjs.org/rgba-regex/-/rgba-regex-1.0.0.tgz#43374e2e2ca0968b0ef1523460b7d730ff22eeb3"
   integrity sha1-QzdOLiyglosO8VI0YLfXMP8i7rM=
 
-rimraf@2, rimraf@^2.2.8, rimraf@^2.5.2, rimraf@^2.5.4, rimraf@^2.6.0, rimraf@^2.6.1, rimraf@^2.6.2, rimraf@^2.6.3, rimraf@^2.7.1:
-  version "2.7.1"
-  resolved "https://registry.npmjs.org/rimraf/-/rimraf-2.7.1.tgz#35797f13a7fdadc566142c29d4f07ccad483e3ec"
-  integrity sha512-uWjbaKIK3T1OSVptzX7Nl6PvQ3qAGtKEtVRjRuazjfL3Bx5eI409VZSqgND+4UNnmzLVdPj9FqFJNPqBZFve4w==
-  dependencies:
-    glob "^7.1.3"
-
 rimraf@3.0.0:
   version "3.0.0"
   resolved "https://registry.npmjs.org/rimraf/-/rimraf-3.0.0.tgz#614176d4b3010b75e5c390eb0ee96f6dc0cebb9b"
@@ -16147,6 +16083,13 @@ rimraf@3.0.2, rimraf@^3.0.0:
   dependencies:
     glob "^7.1.3"
 
+rimraf@^2.2.8, rimraf@^2.5.2, rimraf@^2.5.4, rimraf@^2.6.0, rimraf@^2.6.1, rimraf@^2.6.2, rimraf@^2.6.3, rimraf@^2.7.1:
+  version "2.7.1"
+  resolved "https://registry.npmjs.org/rimraf/-/rimraf-2.7.1.tgz#35797f13a7fdadc566142c29d4f07ccad483e3ec"
+  integrity sha512-uWjbaKIK3T1OSVptzX7Nl6PvQ3qAGtKEtVRjRuazjfL3Bx5eI409VZSqgND+4UNnmzLVdPj9FqFJNPqBZFve4w==
+  dependencies:
+    glob "^7.1.3"
+
 ripemd160@^2.0.0, ripemd160@^2.0.1:
   version "2.0.2"
   resolved "https://registry.npmjs.org/ripemd160/-/ripemd160-2.0.2.tgz#a1c1a6f624751577ba5d07914cbc92850585890c"
@@ -16449,11 +16392,6 @@ semver@7.1.3, semver@^7.0.0, semver@^7.1.1, semver@^7.1.3:
   resolved "https://registry.npmjs.org/semver/-/semver-7.1.3.tgz#e4345ce73071c53f336445cfc19efb1c311df2a6"
   integrity sha512-ekM0zfiA9SCBlsKa2X1hxyxiI4L3B6EbVJkkdgQXnSEEaHlGdvyodMruTiulSRWMMB4NeIuYNMC9rTKTz97GxA==
 
-semver@~5.3.0:
-  version "5.3.0"
-  resolved "https://registry.npmjs.org/semver/-/semver-5.3.0.tgz#9b2ce5d3de02d17c6012ad326aa6b4d0cf54f94f"
-  integrity sha1-myzl094C0XxgEq0yaqa00M9U+U8=
-
 send@0.17.1:
   version "0.17.1"
   resolved "https://registry.npmjs.org/send/-/send-0.17.1.tgz#c1d8b059f7900f7466dd4938bdc44e11ddb376c8"
@@ -18819,7 +18757,7 @@ which-pm-runs@^1.0.0:
   resolved "https://registry.npmjs.org/which-pm-runs/-/which-pm-runs-1.0.0.tgz#670b3afbc552e0b55df6b7780ca74615f23ad1cb"
   integrity sha1-Zws6+8VS4LVd9rd4DKdGFfI60cs=
 
-which@1, which@^1.2.1, which@^1.2.14, which@^1.2.9, which@^1.3.1:
+which@^1.2.1, which@^1.2.14, which@^1.2.9, which@^1.3.1:
   version "1.3.1"
   resolved "https://registry.npmjs.org/which/-/which-1.3.1.tgz#a45043d54f5805316da8d62f9f50918d3da70b0a"
   integrity sha512-HxJdYWq1MTIQbJ3nw0cqssHoTNU267KlrDuGZ1WYlxDStUtKUhOaJmh112/TZmHxxUfuJqPXSOm7tDyas0OSIQ==