@usebruno/js 0.29.0 → 0.31.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.29.0",
3
+ "version": "0.31.0",
4
4
  "license": "MIT",
5
5
  "main": "src/index.js",
6
6
  "files": [
@@ -15,7 +15,7 @@
15
15
  "sandbox:bundle-libraries": "node ./src/sandbox/bundle-libraries.js"
16
16
  },
17
17
  "dependencies": {
18
- "@usebruno/common": "0.4.0",
18
+ "@usebruno/common": "0.6.0",
19
19
  "@usebruno/crypto-js": "^3.1.9",
20
20
  "@usebruno/query": "0.1.0",
21
21
  "ajv": "^8.12.0",
@@ -35,6 +35,7 @@
35
35
  "node-vault": "^0.10.2",
36
36
  "path": "^0.12.7",
37
37
  "quickjs-emscripten": "^0.29.2",
38
+ "tv4": "^1.3.0",
38
39
  "uuid": "^9.0.0",
39
40
  "xml2js": "^0.6.2"
40
41
  },
package/src/bru.js CHANGED
@@ -1,12 +1,11 @@
1
1
  const { cloneDeep } = require('lodash');
2
2
  const { uuid } = require('./utils');
3
- const { interpolate } = require('@usebruno/common');
3
+ const { interpolate: _interpolate } = require('@usebruno/common');
4
4
 
5
5
  const variableNameRegex = /^[\w-.]*$/;
6
6
 
7
7
  class Bru {
8
- constructor(envVariables, runtimeVariables, processEnvVars, collectionPath, historyLogger, setVisualizations, secretVariables, collectionVariables, folderVariables, requestVariables, globalEnvironmentVariables, oauth2CredentialVariables, iterationDetails) {
9
-
8
+ constructor(envVariables, runtimeVariables, processEnvVars, collectionPath, historyLogger, setVisualizations, secretVariables, collectionVariables, folderVariables, requestVariables, globalEnvironmentVariables, oauth2CredentialVariables, iterationDetails, collectionName) {
10
9
  this.envVariables = envVariables || {};
11
10
  this.runtimeVariables = runtimeVariables || {};
12
11
  this.processEnvVars = cloneDeep(processEnvVars || {});
@@ -19,7 +18,7 @@ class Bru {
19
18
  this.collectionPath = collectionPath;
20
19
  this.historyLogger = historyLogger;
21
20
  this.setVisualizations = setVisualizations;
22
-
21
+ this.collectionName = collectionName;
23
22
  this.runner = {
24
23
  skipRequest: () => {
25
24
  this.skipRequest = true;
@@ -38,10 +37,10 @@ class Bru {
38
37
  }
39
38
 
40
39
 
41
- _interpolate = (str) => {
42
- if (!str || !str.length || typeof str !== 'string') {
43
- return str;
44
- }
40
+ interpolate = (strOrObj) => {
41
+ if (!strOrObj) return strOrObj;
42
+ const isObj = typeof strOrObj === 'object';
43
+ const strToInterpolate = isObj ? JSON.stringify(strOrObj) : strOrObj;
45
44
 
46
45
  const combinedVars = {
47
46
  ...this.globalEnvironmentVariables,
@@ -58,7 +57,8 @@ class Bru {
58
57
  }
59
58
  };
60
59
 
61
- return interpolate(str, combinedVars);
60
+ const interpolatedStr = _interpolate(strToInterpolate, combinedVars);
61
+ return isObj ? JSON.parse(interpolatedStr) : interpolatedStr;
62
62
  };
63
63
 
64
64
  cwd() {
@@ -78,7 +78,7 @@ class Bru {
78
78
  }
79
79
 
80
80
  getEnvVar(key) {
81
- return this._interpolate(this.envVariables[key]);
81
+ return this.interpolate(this.envVariables[key]);
82
82
  }
83
83
 
84
84
  setEnvVar(key, value) {
@@ -103,7 +103,7 @@ class Bru {
103
103
  }
104
104
 
105
105
  getGlobalEnvVar(key) {
106
- return this._interpolate(this.globalEnvironmentVariables[key]);
106
+ return this.interpolate(this.globalEnvironmentVariables[key]);
107
107
  }
108
108
 
109
109
  setGlobalEnvVar(key, value) {
@@ -115,7 +115,7 @@ class Bru {
115
115
  }
116
116
 
117
117
  getOauth2CredentialVar(key) {
118
- return this._interpolate(this.oauth2CredentialVariables[key]);
118
+ return this.interpolate(this.oauth2CredentialVariables[key]);
119
119
  }
120
120
 
121
121
  hasVar(key) {
@@ -130,7 +130,7 @@ class Bru {
130
130
  if (variableNameRegex.test(key) === false) {
131
131
  throw new Error(
132
132
  `Variable name: "${key}" contains invalid characters!` +
133
- ' Names must only contain alpha-numeric characters, "-", "_", "."'
133
+ ' Names must only contain alpha-numeric characters, "-", "_", "."'
134
134
  );
135
135
  }
136
136
 
@@ -150,11 +150,11 @@ class Bru {
150
150
  if (variableNameRegex.test(key) === false) {
151
151
  throw new Error(
152
152
  `Variable name: "${key}" contains invalid characters!` +
153
- ' Names must only contain alpha-numeric characters, "-", "_", "."'
153
+ ' Names must only contain alpha-numeric characters, "-", "_", "."'
154
154
  );
155
155
  }
156
156
 
157
- return this._interpolate(this.runtimeVariables[key]);
157
+ return this.interpolate(this.runtimeVariables[key]);
158
158
  }
159
159
 
160
160
  deleteVar(key) {
@@ -170,15 +170,15 @@ class Bru {
170
170
  }
171
171
 
172
172
  getCollectionVar(key) {
173
- return this._interpolate(this.collectionVariables[key]);
173
+ return this.interpolate(this.collectionVariables[key]);
174
174
  }
175
175
 
176
176
  getFolderVar(key) {
177
- return this._interpolate(this.folderVariables[key]);
177
+ return this.interpolate(this.folderVariables[key]);
178
178
  }
179
179
 
180
180
  getRequestVar(key) {
181
- return this._interpolate(this.requestVariables[key]);
181
+ return this.interpolate(this.requestVariables[key]);
182
182
  }
183
183
 
184
184
  setNextRequest(nextRequest) {
@@ -215,6 +215,10 @@ class Bru {
215
215
  sleep(ms) {
216
216
  return new Promise((resolve) => setTimeout(resolve, ms));
217
217
  }
218
+
219
+ getCollectionName() {
220
+ return this.collectionName;
221
+ }
218
222
  }
219
223
 
220
224
  class IterationDataManager {
@@ -21,7 +21,7 @@ class BrunoRequest {
21
21
  this.headers = req.headers;
22
22
  this.timeout = req.timeout;
23
23
  this.historyLogger = historyLogger;
24
-
24
+ this.name = req.name;
25
25
  /**
26
26
  * We automatically parse the JSON body if the content type is JSON
27
27
  * This is to make it easier for the user to access the body directly
@@ -198,6 +198,10 @@ class BrunoRequest {
198
198
  getExecutionMode() {
199
199
  return this.req.__bruno__executionMode;
200
200
  }
201
+
202
+ getName() {
203
+ return this.req.name;
204
+ }
201
205
  }
202
206
 
203
207
  module.exports = BrunoRequest;
@@ -30,6 +30,7 @@ const CryptoJS = require('crypto-js');
30
30
  const NodeVault = require('node-vault');
31
31
  const xml2js = require('xml2js');
32
32
  const cheerio = require('cheerio');
33
+ const tv4 = require('tv4');
33
34
  const { executeQuickJsVmAsync } = require('../sandbox/quickjs');
34
35
 
35
36
  class ScriptRuntime {
@@ -50,7 +51,8 @@ class ScriptRuntime {
50
51
  scriptingConfig,
51
52
  historyLogger,
52
53
  secretVariables,
53
- runRequestByItemPathname
54
+ runRequestByItemPathname,
55
+ collectionName
54
56
  ) {
55
57
  let visualizations = [];
56
58
  let setVisualizations = (data) => {
@@ -63,7 +65,7 @@ class ScriptRuntime {
63
65
  const requestVariables = request?.requestVariables || {};
64
66
  const iterationDetails = request?.runnerIterationDetails || {};
65
67
  // TODO please clean this up
66
- const bru = new Bru(envVariables, runtimeVariables, processEnvVars, collectionPath, historyLogger, setVisualizations, secretVariables, collectionVariables, folderVariables, requestVariables, globalEnvironmentVariables, oauth2CredentialVariables, iterationDetails);
68
+ const bru = new Bru(envVariables, runtimeVariables, processEnvVars, collectionPath, historyLogger, setVisualizations, secretVariables, collectionVariables, folderVariables, requestVariables, globalEnvironmentVariables, oauth2CredentialVariables, iterationDetails, collectionName);
67
69
  const req = new BrunoRequest(request);
68
70
  const allowScriptFilesystemAccess = get(scriptingConfig, 'filesystemAccess.allow', false);
69
71
  const moduleWhitelist = get(scriptingConfig, 'moduleWhitelist', []);
@@ -104,7 +106,7 @@ class ScriptRuntime {
104
106
  };
105
107
  }
106
108
 
107
- if(runRequestByItemPathname) {
109
+ if (runRequestByItemPathname) {
108
110
  context.bru.runRequest = runRequestByItemPathname;
109
111
  }
110
112
 
@@ -158,8 +160,9 @@ class ScriptRuntime {
158
160
  chai,
159
161
  'node-fetch': fetch,
160
162
  'crypto-js': CryptoJS,
161
- 'xml2js': xml2js,
163
+ xml2js: xml2js,
162
164
  cheerio,
165
+ tv4,
163
166
  ...whitelistedModules,
164
167
  fs: allowScriptFilesystemAccess ? fs : undefined,
165
168
  'node-vault': NodeVault
@@ -193,7 +196,8 @@ class ScriptRuntime {
193
196
  scriptingConfig,
194
197
  historyLogger,
195
198
  secretVariables,
196
- runRequestByItemPathname
199
+ runRequestByItemPathname,
200
+ collectionName
197
201
  ) {
198
202
  let visualizations = [];
199
203
  let setVisualizations = (data) => {
@@ -205,7 +209,7 @@ class ScriptRuntime {
205
209
  const folderVariables = request?.folderVariables || {};
206
210
  const requestVariables = request?.requestVariables || {};
207
211
  const iterationDetails = request?.runnerIterationDetails || {};
208
- const bru = new Bru(envVariables, runtimeVariables, processEnvVars, collectionPath, historyLogger, setVisualizations, secretVariables, collectionVariables, folderVariables, requestVariables, globalEnvironmentVariables, oauth2CredentialVariables, iterationDetails);
212
+ const bru = new Bru(envVariables, runtimeVariables, processEnvVars, collectionPath, historyLogger, setVisualizations, secretVariables, collectionVariables, folderVariables, requestVariables, globalEnvironmentVariables, oauth2CredentialVariables, iterationDetails, collectionName);
209
213
  const req = new BrunoRequest(request);
210
214
  const res = new BrunoResponse(response);
211
215
  const allowScriptFilesystemAccess = get(scriptingConfig, 'filesystemAccess.allow', false);
@@ -248,7 +252,7 @@ class ScriptRuntime {
248
252
  };
249
253
  }
250
254
 
251
- if(runRequestByItemPathname) {
255
+ if (runRequestByItemPathname) {
252
256
  context.bru.runRequest = runRequestByItemPathname;
253
257
  }
254
258
 
@@ -303,6 +307,7 @@ class ScriptRuntime {
303
307
  'crypto-js': CryptoJS,
304
308
  'xml2js': xml2js,
305
309
  cheerio,
310
+ tv4,
306
311
  ...whitelistedModules,
307
312
  fs: allowScriptFilesystemAccess ? fs : undefined,
308
313
  'node-vault': NodeVault
@@ -32,6 +32,7 @@ const CryptoJS = require('crypto-js');
32
32
  const NodeVault = require('node-vault');
33
33
  const xml2js = require('xml2js');
34
34
  const cheerio = require('cheerio');
35
+ const tv4 = require('tv4');
35
36
  const { executeQuickJsVmAsync } = require('../sandbox/quickjs');
36
37
 
37
38
  const getResultsSummary = (results) => {
@@ -70,7 +71,8 @@ class TestRuntime {
70
71
  scriptingConfig,
71
72
  historyLogger,
72
73
  secretVariables,
73
- runRequestByItemPathname
74
+ runRequestByItemPathname,
75
+ collectionName
74
76
  ) {
75
77
  const globalEnvironmentVariables = request?.globalEnvironmentVariables || {};
76
78
  const oauth2CredentialVariables = request?.oauth2CredentialVariables || {};
@@ -79,7 +81,7 @@ class TestRuntime {
79
81
  const requestVariables = request?.requestVariables || {};
80
82
  const iterationDetails = request?.runnerIterationDetails || {};
81
83
  const assertionResults = request?.assertionResults || [];
82
- const bru = new Bru(envVariables, runtimeVariables, processEnvVars, collectionPath, historyLogger, undefined, secretVariables, collectionVariables, folderVariables, requestVariables, globalEnvironmentVariables, oauth2CredentialVariables, iterationDetails);
84
+ const bru = new Bru(envVariables, runtimeVariables, processEnvVars, collectionPath, historyLogger, undefined, secretVariables, collectionVariables, folderVariables, requestVariables, globalEnvironmentVariables, oauth2CredentialVariables, iterationDetails, collectionName);
83
85
  const req = new BrunoRequest(request, historyLogger);
84
86
  const res = new BrunoResponse(response);
85
87
  const allowScriptFilesystemAccess = get(scriptingConfig, 'filesystemAccess.allow', false);
@@ -213,6 +215,7 @@ class TestRuntime {
213
215
  'crypto-js': CryptoJS,
214
216
  'xml2js': xml2js,
215
217
  cheerio,
218
+ tv4,
216
219
  ...whitelistedModules,
217
220
  fs: allowScriptFilesystemAccess ? fs : undefined,
218
221
  'node-vault': NodeVault