@usebruno/js 0.25.0 → 0.26.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.25.0",
3
+ "version": "0.26.0",
4
4
  "license": "MIT",
5
5
  "main": "src/index.js",
6
6
  "files": [
@@ -21,7 +21,7 @@
21
21
  "ajv": "^8.12.0",
22
22
  "ajv-formats": "^2.1.1",
23
23
  "atob": "^2.1.2",
24
- "axios": "1.7.7",
24
+ "axios": "^1.8.3",
25
25
  "btoa": "^1.2.1",
26
26
  "chai": "^4.3.7",
27
27
  "chai-string": "^1.5.0",
@@ -21,6 +21,10 @@ class BrunoResponse {
21
21
  return this.res ? this.res.status : null;
22
22
  }
23
23
 
24
+ getStatusText() {
25
+ return this.res ? this.res.statusText : null;
26
+ }
27
+
24
28
  getHeader(name) {
25
29
  return this.res && this.res.headers ? this.res.headers[name] : null;
26
30
  }
@@ -22,6 +22,12 @@ const toNumber = (value) => {
22
22
  return Number.isInteger(num) ? parseInt(value, 10) : parseFloat(value);
23
23
  };
24
24
 
25
+ const removeQuotes = (str) => {
26
+ if ((str.startsWith('"') && str.endsWith('"')) || (str.startsWith("'") && str.endsWith("'"))) {
27
+ return str.slice(1, -1);
28
+ }
29
+ return str;
30
+ };
25
31
 
26
32
  const executeQuickJsVm = ({ script: externalScript, context: externalContext, scriptType = 'template-literal' }) => {
27
33
  if (!externalScript?.length || typeof externalScript !== 'string') {
@@ -29,15 +35,25 @@ const executeQuickJsVm = ({ script: externalScript, context: externalContext, sc
29
35
  }
30
36
  externalScript = externalScript?.trim();
31
37
 
32
- if (!isNaN(Number(externalScript))) {
33
- return Number(externalScript);
34
- }
38
+ if(scriptType === 'template-literal') {
39
+ if (!isNaN(Number(externalScript))) {
40
+ const number = Number(externalScript);
35
41
 
36
- if (externalScript === 'true') return true;
37
- if (externalScript === 'false') return false;
38
- if (externalScript === 'null') return null;
39
- if (externalScript === 'undefined') return undefined;
42
+ // Check if the number is too high. Too high number might get altered, see #1000
43
+ if (number > Number.MAX_SAFE_INTEGER) {
44
+ return externalScript;
45
+ }
40
46
 
47
+ return toNumber(externalScript);
48
+ }
49
+
50
+ if (externalScript === 'true') return true;
51
+ if (externalScript === 'false') return false;
52
+ if (externalScript === 'null') return null;
53
+ if (externalScript === 'undefined') return undefined;
54
+
55
+ externalScript = removeQuotes(externalScript);
56
+ }
41
57
 
42
58
  const vm = QuickJSSyncContext;
43
59
 
@@ -78,16 +94,6 @@ const executeQuickJsVmAsync = async ({ script: externalScript, context: external
78
94
  }
79
95
  externalScript = externalScript?.trim();
80
96
 
81
- if (!isNaN(Number(externalScript))) {
82
- return toNumber(externalScript);
83
- }
84
-
85
- if (externalScript === 'true') return true;
86
- if (externalScript === 'false') return false;
87
- if (externalScript === 'null') return null;
88
- if (externalScript === 'undefined') return undefined;
89
-
90
-
91
97
  try {
92
98
  const module = await newQuickJSWASMModule();
93
99
  const vm = module.newContext();
@@ -189,8 +189,8 @@ const addBruShimToContext = (vm, bru) => {
189
189
  const promise = vm.newPromise();
190
190
  bru.runRequest(vm.dump(args))
191
191
  .then((response) => {
192
- const { status, headers, data, dataBuffer, size } = response || {};
193
- promise.resolve(marshallToVm(cleanJson({ status, headers, data, dataBuffer, size }), vm));
192
+ const { status, headers, data, dataBuffer, size, statusText } = response || {};
193
+ promise.resolve(marshallToVm(cleanJson({ status, statusText, headers, data, dataBuffer, size }), vm));
194
194
  })
195
195
  .catch((err) => {
196
196
  promise.resolve(
@@ -6,11 +6,13 @@ const addBrunoResponseShimToContext = (vm, res) => {
6
6
  });
7
7
 
8
8
  const status = marshallToVm(res?.status, vm);
9
+ const statusText = marshallToVm(res?.statusText, vm);
9
10
  const headers = marshallToVm(res?.headers, vm);
10
11
  const body = marshallToVm(res?.body, vm);
11
12
  const responseTime = marshallToVm(res?.responseTime, vm);
12
13
 
13
14
  vm.setProp(resFn, 'status', status);
15
+ vm.setProp(resFn, 'statusText', statusText);
14
16
  vm.setProp(resFn, 'headers', headers);
15
17
  vm.setProp(resFn, 'body', body);
16
18
  vm.setProp(resFn, 'responseTime', responseTime);
@@ -19,6 +21,13 @@ const addBrunoResponseShimToContext = (vm, res) => {
19
21
  headers.dispose();
20
22
  body.dispose();
21
23
  responseTime.dispose();
24
+ statusText.dispose();
25
+
26
+ let getStatusText = vm.newFunction('getStatusText', function () {
27
+ return marshallToVm(res.getStatusText(), vm);
28
+ });
29
+ vm.setProp(resFn, 'getStatusText', getStatusText);
30
+ getStatusText.dispose();
22
31
 
23
32
  let getStatus = vm.newFunction('getStatus', function () {
24
33
  return marshallToVm(res.getStatus(), vm);
package/src/utils.js CHANGED
@@ -86,6 +86,14 @@ const evaluateJsTemplateLiteral = (templateLiteral, context) => {
86
86
  return undefined;
87
87
  }
88
88
 
89
+ if (templateLiteral.startsWith('"') && templateLiteral.endsWith('"')) {
90
+ return templateLiteral.slice(1, -1);
91
+ }
92
+
93
+ if (templateLiteral.startsWith("'") && templateLiteral.endsWith("'")) {
94
+ return templateLiteral.slice(1, -1);
95
+ }
96
+
89
97
  if (!isNaN(templateLiteral)) {
90
98
  const number = Number(templateLiteral);
91
99
  // Check if the number is too high. Too high number might get altered, see #1000