@usebruno/js 0.33.0 → 0.35.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.33.0",
3
+ "version": "0.35.0",
4
4
  "license": "MIT",
5
5
  "main": "src/index.js",
6
6
  "files": [
@@ -16,7 +16,7 @@
16
16
  "prepack": "npm run test"
17
17
  },
18
18
  "dependencies": {
19
- "@usebruno/common": "0.7.0",
19
+ "@usebruno/common": "0.9.0",
20
20
  "@usebruno/crypto-js": "^3.1.9",
21
21
  "@usebruno/query": "0.1.0",
22
22
  "ajv": "^8.12.0",
@@ -169,6 +169,14 @@ class BrunoRequest {
169
169
  this.timeout = timeout;
170
170
  this.req.timeout = timeout;
171
171
  }
172
+
173
+ onFail(callback) {
174
+ if (typeof callback === 'function') {
175
+ this.req.onFailHandler = callback;
176
+ } else if (callback) {
177
+ throw new Error(`${callback} is not a function`);
178
+ }
179
+ }
172
180
 
173
181
  __safeParseJSON(str) {
174
182
  try {
@@ -51,6 +51,50 @@ class BrunoResponse {
51
51
  this.res.data = clonedData;
52
52
  this.body = clonedData;
53
53
  }
54
+
55
+ // TODO: Refactor: dataBuffer size calculation should be handled in a shared utility so it can be passed and reused across the application
56
+ getSize() {
57
+ if (!this.res) {
58
+ return { header: 0, body: 0, total: 0 };
59
+ }
60
+
61
+ const { data, dataBuffer, headers } = this.res;
62
+ let bodySize = 0;
63
+
64
+ // Use raw received bytes
65
+ if (Buffer.isBuffer(dataBuffer)) {
66
+ bodySize = dataBuffer.length;
67
+ } else {
68
+ // Use server-reported Content-Length
69
+ const contentLength = headers && (headers['content-length'] || headers['Content-Length']);
70
+ if (contentLength && !isNaN(contentLength)) {
71
+ bodySize = parseInt(contentLength, 10);
72
+ } else if (data != null) {
73
+ // Manual calculation
74
+ const raw = typeof data === 'string' ? data : JSON.stringify(data);
75
+ bodySize = Buffer.byteLength(raw);
76
+ }
77
+ }
78
+
79
+ const headerLines = [
80
+ `HTTP/1.1 ${this.res.status} ${this.res.statusText}`,
81
+ ...Object.entries(this.res.headers || {}).flatMap(([key, value]) =>
82
+ Array.isArray(value)
83
+ ? value.map((v) => `${key}: ${v}`)
84
+ : [`${key}: ${value}`]
85
+ ),
86
+ '',
87
+ ''
88
+ ];
89
+ const headerSize = Buffer.byteLength(headerLines.join('\r\n'));
90
+
91
+ return { header: headerSize, body: bodySize, total: headerSize + bodySize };
92
+
93
+ }
94
+
95
+ getDataBuffer() {
96
+ return this.res ? this.res.dataBuffer : null;
97
+ }
54
98
  }
55
99
 
56
100
  module.exports = BrunoResponse;
@@ -12,8 +12,6 @@ const { get } = require('lodash');
12
12
  const Bru = require('../bru');
13
13
  const BrunoRequest = require('../bruno-request');
14
14
  const BrunoResponse = require('../bruno-response');
15
- const Test = require('../test');
16
- const TestResults = require('../test-results');
17
15
  const { cleanJson } = require('../utils');
18
16
  const { createBruTestResultMethods } = require('../utils/results');
19
17
 
@@ -181,7 +181,6 @@ class TestRuntime {
181
181
  }
182
182
  } catch (error) {
183
183
  scriptError = error;
184
- console.error('Test script execution error:', error);
185
184
  }
186
185
 
187
186
  if (historyLogger) {
@@ -65,6 +65,12 @@ const addBrunoResponseShimToContext = (vm, res) => {
65
65
  vm.setProp(resFn, 'setBody', setBody);
66
66
  setBody.dispose();
67
67
 
68
+ let getSize = vm.newFunction('getSize', function () {
69
+ return marshallToVm(res.getSize(), vm);
70
+ });
71
+ vm.setProp(resFn, 'getSize', getSize);
72
+ getSize.dispose();
73
+
68
74
  vm.setProp(vm.global, 'res', resFn);
69
75
  resFn.dispose();
70
76
  };