@usebruno/js 0.6.0 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@usebruno/js",
3
- "version": "0.6.0",
3
+ "version": "0.7.0",
4
4
  "license": "MIT",
5
5
  "main": "src/index.js",
6
6
  "files": [
package/src/bru.js CHANGED
@@ -2,10 +2,11 @@ const Handlebars = require('handlebars');
2
2
  const { cloneDeep } = require('lodash');
3
3
 
4
4
  class Bru {
5
- constructor(envVariables, collectionVariables, processEnvVars) {
5
+ constructor(envVariables, collectionVariables, processEnvVars, collectionPath) {
6
6
  this.envVariables = envVariables;
7
7
  this.collectionVariables = collectionVariables;
8
8
  this.processEnvVars = cloneDeep(processEnvVars || {});
9
+ this.collectionPath = collectionPath;
9
10
  }
10
11
 
11
12
  _interpolateEnvVar = (str) => {
@@ -24,6 +25,10 @@ class Bru {
24
25
  });
25
26
  };
26
27
 
28
+ cwd() {
29
+ return this.collectionPath;
30
+ }
31
+
27
32
  getEnvName() {
28
33
  return this.envVariables.__name__;
29
34
  }
@@ -7,6 +7,7 @@ const util = require('util');
7
7
  const zlib = require('zlib');
8
8
  const url = require('url');
9
9
  const punycode = require('punycode');
10
+ const fs = require('fs');
10
11
  const Bru = require('../bru');
11
12
  const BrunoRequest = require('../bruno-request');
12
13
  const BrunoResponse = require('../bruno-response');
@@ -27,6 +28,8 @@ const CryptoJS = require('crypto-js');
27
28
  class ScriptRuntime {
28
29
  constructor() {}
29
30
 
31
+ // This approach is getting out of hand
32
+ // Need to refactor this to use a single arg (object) instead of 7
30
33
  async runRequestScript(
31
34
  script,
32
35
  request,
@@ -34,9 +37,10 @@ class ScriptRuntime {
34
37
  collectionVariables,
35
38
  collectionPath,
36
39
  onConsoleLog,
37
- processEnvVars
40
+ processEnvVars,
41
+ allowScriptFilesystemAccess
38
42
  ) {
39
- const bru = new Bru(envVariables, collectionVariables, processEnvVars);
43
+ const bru = new Bru(envVariables, collectionVariables, processEnvVars, collectionPath);
40
44
  const req = new BrunoRequest(request);
41
45
 
42
46
  const context = {
@@ -84,7 +88,8 @@ class ScriptRuntime {
84
88
  axios,
85
89
  chai,
86
90
  'node-fetch': fetch,
87
- 'crypto-js': CryptoJS
91
+ 'crypto-js': CryptoJS,
92
+ fs: allowScriptFilesystemAccess ? fs : undefined
88
93
  }
89
94
  }
90
95
  });
@@ -105,9 +110,10 @@ class ScriptRuntime {
105
110
  collectionVariables,
106
111
  collectionPath,
107
112
  onConsoleLog,
108
- processEnvVars
113
+ processEnvVars,
114
+ allowScriptFilesystemAccess
109
115
  ) {
110
- const bru = new Bru(envVariables, collectionVariables, processEnvVars);
116
+ const bru = new Bru(envVariables, collectionVariables, processEnvVars, collectionPath);
111
117
  const req = new BrunoRequest(request);
112
118
  const res = new BrunoResponse(response);
113
119
 
@@ -138,6 +144,16 @@ class ScriptRuntime {
138
144
  external: true,
139
145
  root: [collectionPath],
140
146
  mock: {
147
+ // node libs
148
+ path,
149
+ stream,
150
+ util,
151
+ url,
152
+ http,
153
+ https,
154
+ punycode,
155
+ zlib,
156
+ // 3rd party libs
141
157
  atob,
142
158
  btoa,
143
159
  lodash,
@@ -146,7 +162,8 @@ class ScriptRuntime {
146
162
  nanoid,
147
163
  axios,
148
164
  'node-fetch': fetch,
149
- 'crypto-js': CryptoJS
165
+ 'crypto-js': CryptoJS,
166
+ fs: allowScriptFilesystemAccess ? fs : undefined
150
167
  }
151
168
  }
152
169
  });
@@ -1,6 +1,14 @@
1
1
  const { NodeVM } = require('vm2');
2
2
  const chai = require('chai');
3
3
  const path = require('path');
4
+ const http = require('http');
5
+ const https = require('https');
6
+ const stream = require('stream');
7
+ const util = require('util');
8
+ const zlib = require('zlib');
9
+ const url = require('url');
10
+ const punycode = require('punycode');
11
+ const fs = require('fs');
4
12
  const Bru = require('../bru');
5
13
  const BrunoRequest = require('../bruno-request');
6
14
  const BrunoResponse = require('../bruno-response');
@@ -10,6 +18,7 @@ const { cleanJson } = require('../utils');
10
18
 
11
19
  // Inbuilt Library Support
12
20
  const atob = require('atob');
21
+ const axios = require('axios');
13
22
  const btoa = require('btoa');
14
23
  const lodash = require('lodash');
15
24
  const moment = require('moment');
@@ -20,7 +29,7 @@ const CryptoJS = require('crypto-js');
20
29
  class TestRuntime {
21
30
  constructor() {}
22
31
 
23
- runTests(
32
+ async runTests(
24
33
  testsFile,
25
34
  request,
26
35
  response,
@@ -28,9 +37,10 @@ class TestRuntime {
28
37
  collectionVariables,
29
38
  collectionPath,
30
39
  onConsoleLog,
31
- processEnvVars
40
+ processEnvVars,
41
+ allowScriptFilesystemAccess
32
42
  ) {
33
- const bru = new Bru(envVariables, collectionVariables, processEnvVars);
43
+ const bru = new Bru(envVariables, collectionVariables, processEnvVars, collectionPath);
34
44
  const req = new BrunoRequest(request);
35
45
  const res = new BrunoResponse(response);
36
46
 
@@ -77,19 +87,32 @@ class TestRuntime {
77
87
  external: true,
78
88
  root: [collectionPath],
79
89
  mock: {
90
+ // node libs
91
+ path,
92
+ stream,
93
+ util,
94
+ url,
95
+ http,
96
+ https,
97
+ punycode,
98
+ zlib,
99
+ // 3rd party libs
80
100
  atob,
101
+ axios,
81
102
  btoa,
82
103
  lodash,
83
104
  moment,
84
105
  uuid,
85
106
  nanoid,
86
107
  chai,
87
- 'crypto-js': CryptoJS
108
+ 'crypto-js': CryptoJS,
109
+ fs: allowScriptFilesystemAccess ? fs : undefined
88
110
  }
89
111
  }
90
112
  });
91
113
 
92
- vm.run(testsFile, path.join(collectionPath, 'vm.js'));
114
+ const asyncVM = vm.run(`module.exports = async () => { ${testsFile}}`, path.join(collectionPath, 'vm.js'));
115
+ await asyncVM();
93
116
 
94
117
  return {
95
118
  request,
package/src/test.js CHANGED
@@ -1,6 +1,6 @@
1
- const Test = (__brunoTestResults, chai) => (description, callback) => {
1
+ const Test = (__brunoTestResults, chai) => async (description, callback) => {
2
2
  try {
3
- callback();
3
+ await callback();
4
4
  __brunoTestResults.addResult({ description, status: 'pass' });
5
5
  } catch (error) {
6
6
  console.log(chai.AssertionError);
package/src/utils.js CHANGED
@@ -11,7 +11,7 @@ const JS_KEYWORDS = `
11
11
  .filter((word) => word.length > 0);
12
12
 
13
13
  /**
14
- * Creates a function from a Javascript expression
14
+ * Creates a function from a JavaScript expression
15
15
  *
16
16
  * When the function is called, the variables used in this expression are picked up from the context
17
17
  *
@@ -119,7 +119,7 @@ const createResponseParser = (response = {}) => {
119
119
  };
120
120
 
121
121
  /**
122
- * Objects that are created inside vm2 execution context result in an serilaization error when sent to the renderer process
122
+ * Objects that are created inside vm2 execution context result in an serialization error when sent to the renderer process
123
123
  * Error sending from webFrameMain: Error: Failed to serialize arguments
124
124
  * at s.send (node:electron/js2c/browser_init:169:631)
125
125
  * at g.send (node:electron/js2c/browser_init:165:2156)