@usebruno/js 0.10.1 → 0.12.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.10.1",
3
+ "version": "0.12.0",
4
4
  "license": "MIT",
5
5
  "main": "src/index.js",
6
6
  "files": [
@@ -16,6 +16,7 @@
16
16
  "dependencies": {
17
17
  "@usebruno/query": "0.1.0",
18
18
  "ajv": "^8.12.0",
19
+ "ajv-formats": "^2.1.1",
19
20
  "atob": "^2.1.2",
20
21
  "axios": "^1.5.1",
21
22
  "btoa": "^1.2.1",
@@ -28,7 +29,7 @@
28
29
  "moment": "^2.29.4",
29
30
  "nanoid": "3.3.4",
30
31
  "node-fetch": "2.*",
31
- "uuid": "^9.0.0",
32
- "node-vault": "^0.10.2"
32
+ "node-vault": "^0.10.2",
33
+ "uuid": "^9.0.0"
33
34
  }
34
35
  }
package/src/bru.js CHANGED
@@ -1,14 +1,24 @@
1
1
  const Handlebars = require('handlebars');
2
2
  const { cloneDeep } = require('lodash');
3
+ const { uuid } = require('./utils');
3
4
 
4
5
  const variableNameRegex = /^[\w-.]*$/;
5
6
 
6
7
  class Bru {
7
- constructor(envVariables, collectionVariables, processEnvVars, collectionPath) {
8
+ constructor(
9
+ envVariables,
10
+ collectionVariables,
11
+ processEnvVars,
12
+ collectionPath,
13
+ historyLogger,
14
+ setHtmlVisualizationString
15
+ ) {
8
16
  this.envVariables = envVariables;
9
17
  this.collectionVariables = collectionVariables;
10
18
  this.processEnvVars = cloneDeep(processEnvVars || {});
11
19
  this.collectionPath = collectionPath;
20
+ this.historyLogger = historyLogger;
21
+ this.setHtmlVisualizationString = setHtmlVisualizationString;
12
22
  }
13
23
 
14
24
  _interpolateEnvVar = (str) => {
@@ -48,6 +58,15 @@ class Bru {
48
58
  throw new Error('Creating a env variable without specifying a name is not allowed.');
49
59
  }
50
60
 
61
+ if (this.historyLogger) {
62
+ this.historyLogger({
63
+ uid: uuid(),
64
+ type: 'setEnvVar()',
65
+ data: { key, value },
66
+ createdAt: new Date().toISOString()
67
+ });
68
+ }
69
+
51
70
  this.envVariables[key] = value;
52
71
  }
53
72
 
@@ -63,6 +82,15 @@ class Bru {
63
82
  );
64
83
  }
65
84
 
85
+ if (this.historyLogger) {
86
+ this.historyLogger({
87
+ uid: uuid(),
88
+ type: 'setVar()',
89
+ data: { key, value },
90
+ createdAt: new Date().toISOString()
91
+ });
92
+ }
93
+
66
94
  this.collectionVariables[key] = value;
67
95
  }
68
96
 
@@ -80,6 +108,10 @@ class Bru {
80
108
  setNextRequest(nextRequest) {
81
109
  this.nextRequest = nextRequest;
82
110
  }
111
+
112
+ visualize(htmlString) {
113
+ this.setHtmlVisualizationString(htmlString);
114
+ }
83
115
  }
84
116
 
85
117
  module.exports = Bru;
@@ -1,11 +1,14 @@
1
+ const { uuid } = require('./utils');
2
+
1
3
  class BrunoRequest {
2
- constructor(req) {
4
+ constructor(req, historyLogger) {
3
5
  this.req = req;
4
6
  this.url = req.url;
5
7
  this.method = req.method;
6
8
  this.headers = req.headers;
7
9
  this.body = req.data;
8
10
  this.timeout = req.timeout;
11
+ this.historyLogger = historyLogger;
9
12
  }
10
13
 
11
14
  getUrl() {
@@ -20,6 +23,22 @@ class BrunoRequest {
20
23
  return this.req.method;
21
24
  }
22
25
 
26
+ getAuthMode() {
27
+ if (this.req?.oauth2) {
28
+ return 'oauth2';
29
+ } else if (this.headers?.['Authorization']?.startsWith('Bearer')) {
30
+ return 'bearer';
31
+ } else if (this.headers?.['Authorization']?.startsWith('Basic') || this.req?.auth?.username) {
32
+ return 'basic';
33
+ } else if (this.req?.awsv4) {
34
+ return 'awsv4';
35
+ } else if (this.req?.digestConfig) {
36
+ return 'digest';
37
+ } else {
38
+ return 'none';
39
+ }
40
+ }
41
+
23
42
  setMethod(method) {
24
43
  this.req.method = method;
25
44
  }
@@ -37,6 +56,14 @@ class BrunoRequest {
37
56
  }
38
57
 
39
58
  setHeader(name, value) {
59
+ if (this.historyLogger) {
60
+ this.historyLogger({
61
+ uid: uuid(),
62
+ type: 'setHeader()',
63
+ data: { name, value },
64
+ createdAt: new Date().toISOString()
65
+ });
66
+ }
40
67
  this.req.headers[name] = value;
41
68
  }
42
69
 
@@ -45,6 +72,14 @@ class BrunoRequest {
45
72
  }
46
73
 
47
74
  setBody(data) {
75
+ if (this.historyLogger) {
76
+ this.historyLogger({
77
+ uid: uuid(),
78
+ type: 'setBody()',
79
+ data: { body: data },
80
+ createdAt: new Date().toISOString()
81
+ });
82
+ }
48
83
  this.req.data = data;
49
84
  }
50
85
 
@@ -3,7 +3,7 @@ const chai = require('chai');
3
3
  const { nanoid } = require('nanoid');
4
4
  const Bru = require('../bru');
5
5
  const BrunoRequest = require('../bruno-request');
6
- const { evaluateJsTemplateLiteral, evaluateJsExpression, createResponseParser } = require('../utils');
6
+ const { evaluateJsTemplateLiteral, evaluateJsExpression, createResponseParser, uuid } = require('../utils');
7
7
  const { interpolateString } = require('../interpolate-string');
8
8
 
9
9
  const { expect } = chai;
@@ -199,14 +199,14 @@ const evaluateRhsOperand = (rhsOperand, operator, context) => {
199
199
  };
200
200
 
201
201
  class AssertRuntime {
202
- runAssertions(assertions, request, response, envVariables, collectionVariables, collectionPath) {
202
+ runAssertions(assertions, request, response, envVariables, collectionVariables, collectionPath, historyLogger) {
203
203
  const enabledAssertions = _.filter(assertions, (a) => a.enabled);
204
204
  if (!enabledAssertions.length) {
205
205
  return [];
206
206
  }
207
207
 
208
208
  const bru = new Bru(envVariables, collectionVariables);
209
- const req = new BrunoRequest(request);
209
+ const req = new BrunoRequest(request, historyLogger);
210
210
  const res = createResponseParser(response);
211
211
 
212
212
  const bruContext = {
@@ -339,6 +339,15 @@ class AssertRuntime {
339
339
  }
340
340
  }
341
341
 
342
+ if (historyLogger) {
343
+ historyLogger({
344
+ uid: uuid(),
345
+ type: 'network',
346
+ assertions: assertionResults,
347
+ createdAt: new Date().toISOString()
348
+ });
349
+ }
350
+
342
351
  return assertionResults;
343
352
  }
344
353
  }
@@ -16,6 +16,7 @@ const { cleanJson } = require('../utils');
16
16
 
17
17
  // Inbuilt Library Support
18
18
  const ajv = require('ajv');
19
+ const addFormats = require('ajv-formats');
19
20
  const atob = require('atob');
20
21
  const btoa = require('btoa');
21
22
  const lodash = require('lodash');
@@ -41,10 +42,22 @@ class ScriptRuntime {
41
42
  collectionPath,
42
43
  onConsoleLog,
43
44
  processEnvVars,
44
- scriptingConfig
45
+ scriptingConfig,
46
+ historyLogger
45
47
  ) {
46
- const bru = new Bru(envVariables, collectionVariables, processEnvVars, collectionPath);
47
- const req = new BrunoRequest(request);
48
+ let htmlVisualizationString;
49
+ let setHtmlVisualizationString = (htmlString) => {
50
+ htmlVisualizationString = htmlString;
51
+ };
52
+ const bru = new Bru(
53
+ envVariables,
54
+ collectionVariables,
55
+ processEnvVars,
56
+ collectionPath,
57
+ historyLogger,
58
+ setHtmlVisualizationString
59
+ );
60
+ const req = new BrunoRequest(request, historyLogger);
48
61
  const allowScriptFilesystemAccess = get(scriptingConfig, 'filesystemAccess.allow', false);
49
62
  const moduleWhitelist = get(scriptingConfig, 'moduleWhitelist', []);
50
63
  const additionalContextRoots = get(scriptingConfig, 'additionalContextRoots', []);
@@ -102,6 +115,7 @@ class ScriptRuntime {
102
115
  zlib,
103
116
  // 3rd party libs
104
117
  ajv,
118
+ 'ajv-formats': addFormats,
105
119
  atob,
106
120
  btoa,
107
121
  lodash,
@@ -124,6 +138,7 @@ class ScriptRuntime {
124
138
  request,
125
139
  envVariables: cleanJson(envVariables),
126
140
  collectionVariables: cleanJson(collectionVariables),
141
+ htmlVisualizationString,
127
142
  nextRequestName: bru.nextRequest
128
143
  };
129
144
  }
@@ -137,10 +152,22 @@ class ScriptRuntime {
137
152
  collectionPath,
138
153
  onConsoleLog,
139
154
  processEnvVars,
140
- scriptingConfig
155
+ scriptingConfig,
156
+ historyLogger
141
157
  ) {
142
- const bru = new Bru(envVariables, collectionVariables, processEnvVars, collectionPath);
143
- const req = new BrunoRequest(request);
158
+ let htmlVisualizationString;
159
+ let setHtmlVisualizationString = (htmlString) => {
160
+ htmlVisualizationString = htmlString;
161
+ };
162
+ const bru = new Bru(
163
+ envVariables,
164
+ collectionVariables,
165
+ processEnvVars,
166
+ collectionPath,
167
+ historyLogger,
168
+ setHtmlVisualizationString
169
+ );
170
+ const req = new BrunoRequest(request, historyLogger);
144
171
  const res = new BrunoResponse(response);
145
172
  const allowScriptFilesystemAccess = get(scriptingConfig, 'filesystemAccess.allow', false);
146
173
  const moduleWhitelist = get(scriptingConfig, 'moduleWhitelist', []);
@@ -194,6 +221,7 @@ class ScriptRuntime {
194
221
  zlib,
195
222
  // 3rd party libs
196
223
  ajv,
224
+ 'ajv-formats': addFormats,
197
225
  atob,
198
226
  btoa,
199
227
  lodash,
@@ -217,6 +245,7 @@ class ScriptRuntime {
217
245
  response,
218
246
  envVariables: cleanJson(envVariables),
219
247
  collectionVariables: cleanJson(collectionVariables),
248
+ htmlVisualizationString,
220
249
  nextRequestName: bru.nextRequest
221
250
  };
222
251
  }
@@ -19,6 +19,7 @@ const { cleanJson } = require('../utils');
19
19
 
20
20
  // Inbuilt Library Support
21
21
  const ajv = require('ajv');
22
+ const addFormats = require('ajv-formats');
22
23
  const atob = require('atob');
23
24
  const btoa = require('btoa');
24
25
  const lodash = require('lodash');
@@ -42,10 +43,11 @@ class TestRuntime {
42
43
  collectionPath,
43
44
  onConsoleLog,
44
45
  processEnvVars,
45
- scriptingConfig
46
+ scriptingConfig,
47
+ historyLogger
46
48
  ) {
47
- const bru = new Bru(envVariables, collectionVariables, processEnvVars, collectionPath);
48
- const req = new BrunoRequest(request);
49
+ const bru = new Bru(envVariables, collectionVariables, processEnvVars, collectionPath, historyLogger);
50
+ const req = new BrunoRequest(request, historyLogger);
49
51
  const res = new BrunoResponse(response);
50
52
  const allowScriptFilesystemAccess = get(scriptingConfig, 'filesystemAccess.allow', false);
51
53
  const moduleWhitelist = get(scriptingConfig, 'moduleWhitelist', []);
@@ -120,6 +122,7 @@ class TestRuntime {
120
122
  zlib,
121
123
  // 3rd party libs
122
124
  ajv,
125
+ 'ajv-formats': addFormats,
123
126
  btoa,
124
127
  atob,
125
128
  lodash,
@@ -140,6 +143,15 @@ class TestRuntime {
140
143
  const asyncVM = vm.run(`module.exports = async () => { ${testsFile}}`, path.join(collectionPath, 'vm.js'));
141
144
  await asyncVM();
142
145
 
146
+ if (historyLogger) {
147
+ historyLogger({
148
+ uid: uuid.v4(),
149
+ type: 'network',
150
+ tests: cleanJson(__brunoTestResults.getResults()),
151
+ createdAt: new Date().toISOString()
152
+ });
153
+ }
154
+
143
155
  return {
144
156
  request,
145
157
  envVariables: cleanJson(envVariables),
@@ -4,14 +4,14 @@ const BrunoRequest = require('../bruno-request');
4
4
  const { evaluateJsTemplateLiteral, evaluateJsExpression, createResponseParser } = require('../utils');
5
5
 
6
6
  class VarsRuntime {
7
- runPreRequestVars(vars, request, envVariables, collectionVariables, collectionPath, processEnvVars) {
7
+ runPreRequestVars(vars, request, envVariables, collectionVariables, collectionPath, processEnvVars, historyLogger) {
8
8
  const enabledVars = _.filter(vars, (v) => v.enabled);
9
9
  if (!enabledVars.length) {
10
10
  return;
11
11
  }
12
12
 
13
- const bru = new Bru(envVariables, collectionVariables, processEnvVars);
14
- const req = new BrunoRequest(request);
13
+ const bru = new Bru(envVariables, collectionVariables, processEnvVars, null, historyLogger);
14
+ const req = new BrunoRequest(request, historyLogger);
15
15
 
16
16
  const bruContext = {
17
17
  bru,
@@ -34,14 +34,23 @@ class VarsRuntime {
34
34
  };
35
35
  }
36
36
 
37
- runPostResponseVars(vars, request, response, envVariables, collectionVariables, collectionPath, processEnvVars) {
37
+ runPostResponseVars(
38
+ vars,
39
+ request,
40
+ response,
41
+ envVariables,
42
+ collectionVariables,
43
+ collectionPath,
44
+ processEnvVars,
45
+ historyLogger
46
+ ) {
38
47
  const enabledVars = _.filter(vars, (v) => v.enabled);
39
48
  if (!enabledVars.length) {
40
49
  return;
41
50
  }
42
51
 
43
- const bru = new Bru(envVariables, collectionVariables, processEnvVars);
44
- const req = new BrunoRequest(request);
52
+ const bru = new Bru(envVariables, collectionVariables, processEnvVars, null, historyLogger);
53
+ const req = new BrunoRequest(request, historyLogger);
45
54
  const res = createResponseParser(response);
46
55
 
47
56
  const bruContext = {
package/src/utils.js CHANGED
@@ -1,5 +1,6 @@
1
1
  const jsonQuery = require('json-query');
2
2
  const { get } = require('@usebruno/query');
3
+ const { customAlphabet } = require('nanoid');
3
4
 
4
5
  const JS_KEYWORDS = `
5
6
  break case catch class const continue debugger default delete do
@@ -142,10 +143,20 @@ const cleanJson = (data) => {
142
143
  }
143
144
  };
144
145
 
146
+ // a customized version of nanoid without using _ and -
147
+ const uuid = () => {
148
+ // https://github.com/ai/nanoid/blob/main/url-alphabet/index.js
149
+ const urlAlphabet = 'useandom26T198340PX75pxJACKVERYMINDBUSHWOLFGQZbfghjklqvwyzrict';
150
+ const customNanoId = customAlphabet(urlAlphabet, 21);
151
+
152
+ return customNanoId();
153
+ };
154
+
145
155
  module.exports = {
146
156
  evaluateJsExpression,
147
157
  evaluateJsTemplateLiteral,
148
158
  createResponseParser,
149
159
  internalExpressionCache,
150
- cleanJson
160
+ cleanJson,
161
+ uuid
151
162
  };