@usebruno/js 0.3.0 → 0.4.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 +3 -1
- package/src/bru.js +6 -2
- package/src/bruno-response.js +8 -8
- package/src/runtime/script-runtime.js +29 -7
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@usebruno/js",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"main": "src/index.js",
|
|
5
5
|
"files": [
|
|
6
6
|
"src",
|
|
@@ -16,12 +16,14 @@
|
|
|
16
16
|
"@usebruno/query": "0.1.0",
|
|
17
17
|
"ajv": "^8.12.0",
|
|
18
18
|
"atob": "^2.1.2",
|
|
19
|
+
"axios": "^0.26.0",
|
|
19
20
|
"btoa": "^1.2.1",
|
|
20
21
|
"crypto-js": "^4.1.1",
|
|
21
22
|
"json-query": "^2.2.2",
|
|
22
23
|
"lodash": "^4.17.21",
|
|
23
24
|
"moment": "^2.29.4",
|
|
24
25
|
"nanoid": "3.3.4",
|
|
26
|
+
"node-fetch": "2.*",
|
|
25
27
|
"uuid": "^9.0.0"
|
|
26
28
|
}
|
|
27
29
|
}
|
package/src/bru.js
CHANGED
|
@@ -5,6 +5,10 @@ class Bru {
|
|
|
5
5
|
this._collectionVariables = collectionVariables;
|
|
6
6
|
}
|
|
7
7
|
|
|
8
|
+
getProcessEnv(key) {
|
|
9
|
+
return process.env[key];
|
|
10
|
+
}
|
|
11
|
+
|
|
8
12
|
getEnvVar(key) {
|
|
9
13
|
return this._envVariables[key];
|
|
10
14
|
}
|
|
@@ -16,7 +20,7 @@ class Bru {
|
|
|
16
20
|
|
|
17
21
|
// gracefully ignore if key is not present in environment
|
|
18
22
|
if(!this._envVariables.hasOwnProperty(key)) {
|
|
19
|
-
|
|
23
|
+
return;
|
|
20
24
|
}
|
|
21
25
|
|
|
22
26
|
this._envVariables[key] = value;
|
|
@@ -35,4 +39,4 @@ class Bru {
|
|
|
35
39
|
}
|
|
36
40
|
}
|
|
37
41
|
|
|
38
|
-
module.exports = Bru;
|
|
42
|
+
module.exports = Bru;
|
package/src/bruno-response.js
CHANGED
|
@@ -1,26 +1,26 @@
|
|
|
1
1
|
class BrunoResponse {
|
|
2
2
|
constructor(res) {
|
|
3
3
|
this.res = res;
|
|
4
|
-
this.status = res.status;
|
|
5
|
-
this.statusText = res.statusText;
|
|
6
|
-
this.headers = res.headers;
|
|
7
|
-
this.body = res.data;
|
|
4
|
+
this.status = res ? res.status : null;
|
|
5
|
+
this.statusText = res ? res.statusText : null;
|
|
6
|
+
this.headers = res ? res.headers : null;
|
|
7
|
+
this.body = res ? res.data : null;
|
|
8
8
|
}
|
|
9
9
|
|
|
10
10
|
getStatus() {
|
|
11
|
-
return this.res.status;
|
|
11
|
+
return this.res ? this.res.status : null;
|
|
12
12
|
}
|
|
13
13
|
|
|
14
14
|
getHeader(name) {
|
|
15
|
-
return this.res.
|
|
15
|
+
return (this.res && this.res.headers) ? this.res.headers[name] : null;
|
|
16
16
|
}
|
|
17
17
|
|
|
18
18
|
getHeaders() {
|
|
19
|
-
return this.res.headers;
|
|
19
|
+
return this.res ? this.res.headers : null;
|
|
20
20
|
}
|
|
21
21
|
|
|
22
22
|
getBody() {
|
|
23
|
-
return this.res.data;
|
|
23
|
+
return this.res ? this.res.data : null;
|
|
24
24
|
}
|
|
25
25
|
}
|
|
26
26
|
|
|
@@ -1,5 +1,12 @@
|
|
|
1
1
|
const { NodeVM } = require('vm2');
|
|
2
2
|
const path = require('path');
|
|
3
|
+
const http = require('http');
|
|
4
|
+
const https = require('https');
|
|
5
|
+
const stream = require('stream');
|
|
6
|
+
const util = require('util');
|
|
7
|
+
const zlib = require('zlib');
|
|
8
|
+
const url = require('url');
|
|
9
|
+
const punycode = require('punycode');
|
|
3
10
|
const Bru = require('../bru');
|
|
4
11
|
const BrunoRequest = require('../bruno-request');
|
|
5
12
|
const BrunoResponse = require('../bruno-response');
|
|
@@ -11,16 +18,17 @@ const lodash = require('lodash');
|
|
|
11
18
|
const moment = require('moment');
|
|
12
19
|
const uuid = require('uuid');
|
|
13
20
|
const nanoid = require('nanoid');
|
|
21
|
+
const axios = require('axios');
|
|
22
|
+
const fetch = require('node-fetch');
|
|
14
23
|
const CryptoJS = require('crypto-js');
|
|
15
24
|
|
|
16
25
|
class ScriptRuntime {
|
|
17
26
|
constructor() {
|
|
18
27
|
}
|
|
19
28
|
|
|
20
|
-
runRequestScript(script, request, envVariables, collectionVariables, collectionPath)
|
|
29
|
+
async runRequestScript(script, request, envVariables, collectionVariables, collectionPath){
|
|
21
30
|
const bru = new Bru(envVariables, collectionVariables);
|
|
22
31
|
const req = new BrunoRequest(request);
|
|
23
|
-
|
|
24
32
|
const context = {
|
|
25
33
|
bru,
|
|
26
34
|
req
|
|
@@ -32,19 +40,30 @@ class ScriptRuntime {
|
|
|
32
40
|
external: true,
|
|
33
41
|
root: [collectionPath],
|
|
34
42
|
mock: {
|
|
43
|
+
// node libs
|
|
44
|
+
path,
|
|
45
|
+
stream,
|
|
46
|
+
util,
|
|
47
|
+
url,
|
|
48
|
+
http,
|
|
49
|
+
https,
|
|
50
|
+
punycode,
|
|
51
|
+
zlib,
|
|
52
|
+
// 3rd party libs
|
|
35
53
|
atob,
|
|
36
54
|
btoa,
|
|
37
55
|
lodash,
|
|
38
56
|
moment,
|
|
39
57
|
uuid,
|
|
40
58
|
nanoid,
|
|
59
|
+
axios,
|
|
60
|
+
'node-fetch': fetch,
|
|
41
61
|
'crypto-js': CryptoJS
|
|
42
62
|
}
|
|
43
63
|
}
|
|
44
64
|
});
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
65
|
+
const asyncVM = vm.run(`module.exports = async () => { ${script} }`, path.join(collectionPath, 'vm.js'));
|
|
66
|
+
await asyncVM();
|
|
48
67
|
return {
|
|
49
68
|
request,
|
|
50
69
|
envVariables,
|
|
@@ -52,7 +71,7 @@ class ScriptRuntime {
|
|
|
52
71
|
};
|
|
53
72
|
}
|
|
54
73
|
|
|
55
|
-
runResponseScript(script, request, response, envVariables, collectionVariables, collectionPath) {
|
|
74
|
+
async runResponseScript(script, request, response, envVariables, collectionVariables, collectionPath) {
|
|
56
75
|
const bru = new Bru(envVariables, collectionVariables);
|
|
57
76
|
const req = new BrunoRequest(request);
|
|
58
77
|
const res = new BrunoResponse(response);
|
|
@@ -75,12 +94,15 @@ class ScriptRuntime {
|
|
|
75
94
|
moment,
|
|
76
95
|
uuid,
|
|
77
96
|
nanoid,
|
|
97
|
+
axios,
|
|
98
|
+
'node-fetch': fetch,
|
|
78
99
|
'crypto-js': CryptoJS
|
|
79
100
|
}
|
|
80
101
|
}
|
|
81
102
|
});
|
|
82
103
|
|
|
83
|
-
vm.run(script
|
|
104
|
+
const asyncVM = vm.run(`module.exports = async () => { ${script} }`, path.join(collectionPath, 'vm.js'));
|
|
105
|
+
await asyncVM();
|
|
84
106
|
|
|
85
107
|
return {
|
|
86
108
|
response,
|