@usebruno/js 0.44.0 → 0.45.1

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.
@@ -0,0 +1,42 @@
1
+ const path = require('node:path');
2
+ const nodeModule = require('node:module');
3
+
4
+ /**
5
+ * Check if a module is a Node.js builtin
6
+ * @param {string} moduleName - Module name to check
7
+ * @returns {boolean} True if module is a builtin
8
+ */
9
+ function isBuiltinModule(moduleName) {
10
+ const normalized = moduleName.startsWith('node:') ? moduleName.slice(5) : moduleName;
11
+ return nodeModule.builtinModules.includes(normalized);
12
+ }
13
+
14
+ /**
15
+ * Validate that a path is within allowed context roots
16
+ * @param {string} normalizedPath - Normalized file path
17
+ * @param {Array<string>} additionalContextRootsAbsolute - Allowed roots
18
+ * @returns {boolean} True if path is within allowed roots
19
+ */
20
+ function isPathWithinAllowedRoots(normalizedPath, additionalContextRootsAbsolute) {
21
+ return additionalContextRootsAbsolute.some((allowedRoot) => {
22
+ const normalizedAllowedRoot = path.normalize(allowedRoot);
23
+ const relativePath = path.relative(normalizedAllowedRoot, normalizedPath);
24
+ return !relativePath.startsWith('..') && !path.isAbsolute(relativePath);
25
+ });
26
+ }
27
+
28
+ class ScriptError extends Error {
29
+ constructor(error, script) {
30
+ super(error.message);
31
+ this.name = 'ScriptError';
32
+ this.originalError = error;
33
+ this.script = script;
34
+ this.stack = error.stack;
35
+ }
36
+ }
37
+
38
+ module.exports = {
39
+ isBuiltinModule,
40
+ isPathWithinAllowedRoots,
41
+ ScriptError
42
+ };
@@ -24,6 +24,12 @@ const addBruShimToContext = (vm, bru) => {
24
24
  vm.setProp(bruObject, 'getCollectionName', getCollectionName);
25
25
  getCollectionName.dispose();
26
26
 
27
+ let isSafeMode = vm.newFunction('isSafeMode', function () {
28
+ return marshallToVm(bru.isSafeMode(), vm);
29
+ });
30
+ vm.setProp(bruObject, 'isSafeMode', isSafeMode);
31
+ isSafeMode.dispose();
32
+
27
33
  let getProcessEnv = vm.newFunction('getProcessEnv', function (key) {
28
34
  return marshallToVm(bru.getProcessEnv(vm.dump(key)), vm);
29
35
  });
@@ -9,6 +9,7 @@ const addBrunoRequestShimToContext = (vm, req) => {
9
9
  const body = marshallToVm(req.getBody(), vm);
10
10
  const timeout = marshallToVm(req.getTimeout(), vm);
11
11
  const name = marshallToVm(req.getName(), vm);
12
+ const pathParams = marshallToVm(req.getPathParams(), vm);
12
13
  const tags = marshallToVm(req.getTags(), vm);
13
14
 
14
15
  vm.setProp(reqObject, 'url', url);
@@ -17,6 +18,7 @@ const addBrunoRequestShimToContext = (vm, req) => {
17
18
  vm.setProp(reqObject, 'body', body);
18
19
  vm.setProp(reqObject, 'timeout', timeout);
19
20
  vm.setProp(reqObject, 'name', name);
21
+ vm.setProp(reqObject, 'pathParams', pathParams);
20
22
  vm.setProp(reqObject, 'tags', tags);
21
23
 
22
24
  url.dispose();
@@ -25,6 +27,7 @@ const addBrunoRequestShimToContext = (vm, req) => {
25
27
  body.dispose();
26
28
  timeout.dispose();
27
29
  name.dispose();
30
+ pathParams.dispose();
28
31
  tags.dispose();
29
32
 
30
33
  let getUrl = vm.newFunction('getUrl', function () {
@@ -39,6 +42,24 @@ const addBrunoRequestShimToContext = (vm, req) => {
39
42
  vm.setProp(reqObject, 'setUrl', setUrl);
40
43
  setUrl.dispose();
41
44
 
45
+ let getHost = vm.newFunction('getHost', function () {
46
+ return marshallToVm(req.getHost(), vm);
47
+ });
48
+ vm.setProp(reqObject, 'getHost', getHost);
49
+ getHost.dispose();
50
+
51
+ let getPath = vm.newFunction('getPath', function () {
52
+ return marshallToVm(req.getPath(), vm);
53
+ });
54
+ vm.setProp(reqObject, 'getPath', getPath);
55
+ getPath.dispose();
56
+
57
+ let getQueryString = vm.newFunction('getQueryString', function () {
58
+ return marshallToVm(req.getQueryString(), vm);
59
+ });
60
+ vm.setProp(reqObject, 'getQueryString', getQueryString);
61
+ getQueryString.dispose();
62
+
42
63
  let getMethod = vm.newFunction('getMethod', function () {
43
64
  return marshallToVm(req.getMethod(), vm);
44
65
  });
@@ -57,6 +78,12 @@ const addBrunoRequestShimToContext = (vm, req) => {
57
78
  vm.setProp(reqObject, 'getName', getName);
58
79
  getName.dispose();
59
80
 
81
+ let getPathParams = vm.newFunction('getPathParams', function () {
82
+ return marshallToVm(req.getPathParams(), vm);
83
+ });
84
+ vm.setProp(reqObject, 'getPathParams', getPathParams);
85
+ getPathParams.dispose();
86
+
60
87
  let setMethod = vm.newFunction('setMethod', function (method) {
61
88
  req.setMethod(vm.dump(method));
62
89
  });
@@ -58,6 +58,27 @@ const addBruShimToContext = (vm, __brunoTestResults) => {
58
58
  globalThis.test = Test(__brunoTestResults);
59
59
  `
60
60
  );
61
+
62
+ // Register custom chai assertion for isJson (expect(...).to.be.json)
63
+ // The bundled chai only exposes { expect, assert } — no Assertion class.
64
+ // Access the prototype through an expect() instance instead.
65
+ vm.evalCode(
66
+ `
67
+ (function() {
68
+ var proto = Object.getPrototypeOf(expect(null));
69
+ Object.defineProperty(proto, 'json', {
70
+ get: function () {
71
+ var obj = this._obj;
72
+ var isJson = typeof obj === 'object' && obj !== null && !Array.isArray(obj) &&
73
+ Object.prototype.toString.call(obj) === '[object Object]';
74
+ this.assert(isJson, 'expected #{this} to be JSON', 'expected #{this} not to be JSON');
75
+ return this;
76
+ },
77
+ configurable: true
78
+ });
79
+ })();
80
+ `
81
+ );
61
82
  };
62
83
 
63
84
  module.exports = addBruShimToContext;