@usebruno/js 0.13.0 → 0.15.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 +23 -5
- package/src/bru.js +62 -18
- package/src/interpolate-string.js +8 -36
- package/src/runtime/assert-runtime.js +61 -25
- package/src/runtime/script-runtime.js +69 -26
- package/src/runtime/test-runtime.js +67 -49
- package/src/runtime/vars-runtime.js +65 -28
- package/src/sandbox/bundle-browser-rollup.js +502 -0
- package/src/sandbox/bundle-libraries.js +88 -0
- package/src/sandbox/quickjs/index.js +193 -0
- package/src/sandbox/quickjs/shims/bru.js +87 -0
- package/src/sandbox/quickjs/shims/bruno-request.js +112 -0
- package/src/sandbox/quickjs/shims/bruno-response.js +55 -0
- package/src/sandbox/quickjs/shims/console.js +46 -0
- package/src/sandbox/quickjs/shims/lib/axios.js +72 -0
- package/src/sandbox/quickjs/shims/lib/index.js +13 -0
- package/src/sandbox/quickjs/shims/lib/nanoid.js +24 -0
- package/src/sandbox/quickjs/shims/lib/path.js +28 -0
- package/src/sandbox/quickjs/shims/lib/uuid.js +30 -0
- package/src/sandbox/quickjs/shims/local-module.js +35 -0
- package/src/sandbox/quickjs/shims/test.js +63 -0
- package/src/sandbox/quickjs/utils/index.js +33 -0
- package/src/utils.js +6 -1
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
const path = require('path');
|
|
2
|
+
const { marshallToVm } = require('../../utils');
|
|
3
|
+
|
|
4
|
+
const fns = ['resolve'];
|
|
5
|
+
|
|
6
|
+
const addPathShimToContext = async (vm) => {
|
|
7
|
+
fns.forEach((fn) => {
|
|
8
|
+
let fnHandle = vm.newFunction(fn, function (...args) {
|
|
9
|
+
const nativeArgs = args.map(vm.dump);
|
|
10
|
+
return marshallToVm(path[fn](...nativeArgs), vm);
|
|
11
|
+
});
|
|
12
|
+
vm.setProp(vm.global, `__bruno__path__${fn}`, fnHandle);
|
|
13
|
+
fnHandle.dispose();
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
vm.evalCode(
|
|
17
|
+
`
|
|
18
|
+
globalThis.path = {};
|
|
19
|
+
${fns?.map((fn, idx) => `globalThis.path.${fn} = __bruno__path__${fn}`).join('\n')}
|
|
20
|
+
globalThis.requireObject = {
|
|
21
|
+
...(globalThis.requireObject || {}),
|
|
22
|
+
path: globalThis.path,
|
|
23
|
+
}
|
|
24
|
+
`
|
|
25
|
+
);
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
module.exports = addPathShimToContext;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
const uuid = require('uuid');
|
|
2
|
+
const { marshallToVm } = require('../../utils');
|
|
3
|
+
|
|
4
|
+
const fns = ['version', 'parse', 'stringify', 'v1', 'v1ToV6', 'v3', 'v4', 'v5', 'v6', 'v6ToV1', 'v7', 'validate'];
|
|
5
|
+
|
|
6
|
+
const addUuidShimToContext = async (vm) => {
|
|
7
|
+
fns.forEach((fn) => {
|
|
8
|
+
let fnHandle = vm.newFunction(fn, function (...args) {
|
|
9
|
+
const nativeArgs = args.map(vm.dump);
|
|
10
|
+
return marshallToVm(uuid[fn](...nativeArgs), vm);
|
|
11
|
+
});
|
|
12
|
+
vm.setProp(vm.global, `__bruno__uuid__${fn}`, fnHandle);
|
|
13
|
+
fnHandle.dispose();
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
vm.evalCode(
|
|
17
|
+
`
|
|
18
|
+
globalThis.uuid = {};
|
|
19
|
+
${['version', 'parse', 'stringify', 'v1', 'v1ToV6', 'v3', 'v4', 'v5', 'v6', 'v6ToV1', 'v7', 'validate']
|
|
20
|
+
?.map((fn, idx) => `globalThis.uuid.${fn} = __bruno__uuid__${fn}`)
|
|
21
|
+
.join('\n')}
|
|
22
|
+
globalThis.requireObject = {
|
|
23
|
+
...globalThis.requireObject,
|
|
24
|
+
uuid: globalThis.uuid,
|
|
25
|
+
}
|
|
26
|
+
`
|
|
27
|
+
);
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
module.exports = addUuidShimToContext;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
const path = require('path');
|
|
2
|
+
const fs = require('fs');
|
|
3
|
+
const { marshallToVm } = require('../utils');
|
|
4
|
+
|
|
5
|
+
const addLocalModuleLoaderShimToContext = (vm, collectionPath) => {
|
|
6
|
+
let loadLocalModuleHandle = vm.newFunction('loadLocalModule', function (module) {
|
|
7
|
+
const filename = vm.dump(module);
|
|
8
|
+
|
|
9
|
+
// Check if the filename has an extension
|
|
10
|
+
const hasExtension = path.extname(filename) !== '';
|
|
11
|
+
const resolvedFilename = hasExtension ? filename : `${filename}.js`;
|
|
12
|
+
|
|
13
|
+
// Resolve the file path and check if it's within the collectionPath
|
|
14
|
+
const filePath = path.resolve(collectionPath, resolvedFilename);
|
|
15
|
+
const relativePath = path.relative(collectionPath, filePath);
|
|
16
|
+
|
|
17
|
+
// Ensure the resolved file path is inside the collectionPath
|
|
18
|
+
if (relativePath.startsWith('..') || path.isAbsolute(relativePath)) {
|
|
19
|
+
throw new Error('Access to files outside of the collectionPath is not allowed.');
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
if (!fs.existsSync(filePath)) {
|
|
23
|
+
throw new Error(`Cannot find module ${filename}`);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
let code = fs.readFileSync(filePath).toString();
|
|
27
|
+
|
|
28
|
+
return marshallToVm(code, vm);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
vm.setProp(vm.global, '__brunoLoadLocalModule', loadLocalModuleHandle);
|
|
32
|
+
loadLocalModuleHandle.dispose();
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
module.exports = addLocalModuleLoaderShimToContext;
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
const { marshallToVm } = require('../utils');
|
|
2
|
+
|
|
3
|
+
const addBruShimToContext = (vm, __brunoTestResults) => {
|
|
4
|
+
let addResult = vm.newFunction('addResult', function (v) {
|
|
5
|
+
__brunoTestResults.addResult(vm.dump(v));
|
|
6
|
+
});
|
|
7
|
+
vm.setProp(vm.global, '__bruno__addResult', addResult);
|
|
8
|
+
addResult.dispose();
|
|
9
|
+
|
|
10
|
+
let getResults = vm.newFunction('getResults', function () {
|
|
11
|
+
return marshallToVm(__brunoTestResults.getResults(), vm);
|
|
12
|
+
});
|
|
13
|
+
vm.setProp(vm.global, '__bruno__getResults', getResults);
|
|
14
|
+
getResults.dispose();
|
|
15
|
+
|
|
16
|
+
vm.evalCode(
|
|
17
|
+
`
|
|
18
|
+
globalThis.expect = require('chai').expect;
|
|
19
|
+
globalThis.assert = require('chai').assert;
|
|
20
|
+
|
|
21
|
+
globalThis.__brunoTestResults = {
|
|
22
|
+
addResult: globalThis.__bruno__addResult,
|
|
23
|
+
getResults: globalThis.__bruno__getResults,
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
globalThis.DummyChaiAssertionError = class DummyChaiAssertionError extends Error {
|
|
27
|
+
constructor(message, props, ssf) {
|
|
28
|
+
super(message);
|
|
29
|
+
this.name = "AssertionError";
|
|
30
|
+
Object.assign(this, props);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
globalThis.Test = (__brunoTestResults) => async (description, callback) => {
|
|
35
|
+
try {
|
|
36
|
+
await callback();
|
|
37
|
+
__brunoTestResults.addResult({ description, status: "pass" });
|
|
38
|
+
} catch (error) {
|
|
39
|
+
if (error instanceof DummyChaiAssertionError) {
|
|
40
|
+
const { message, actual, expected } = error;
|
|
41
|
+
__brunoTestResults.addResult({
|
|
42
|
+
description,
|
|
43
|
+
status: "fail",
|
|
44
|
+
error: message,
|
|
45
|
+
actual,
|
|
46
|
+
expected,
|
|
47
|
+
});
|
|
48
|
+
} else {
|
|
49
|
+
globalThis.__bruno__addResult({
|
|
50
|
+
description,
|
|
51
|
+
status: "fail",
|
|
52
|
+
error: error.message || "An unexpected error occurred.",
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
globalThis.test = Test(__brunoTestResults);
|
|
59
|
+
`
|
|
60
|
+
);
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
module.exports = addBruShimToContext;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
const marshallToVm = (value, vm) => {
|
|
2
|
+
if (value === undefined) {
|
|
3
|
+
return vm.undefined;
|
|
4
|
+
}
|
|
5
|
+
if (value === null) {
|
|
6
|
+
return vm.null;
|
|
7
|
+
}
|
|
8
|
+
if (typeof value === 'string') {
|
|
9
|
+
return vm.newString(value);
|
|
10
|
+
} else if (typeof value === 'number') {
|
|
11
|
+
return vm.newNumber(value);
|
|
12
|
+
} else if (typeof value === 'boolean') {
|
|
13
|
+
return value ? vm.true : vm.false;
|
|
14
|
+
} else if (typeof value === 'object') {
|
|
15
|
+
if (Array.isArray(value)) {
|
|
16
|
+
const arr = vm.newArray();
|
|
17
|
+
for (let i = 0; i < value.length; i++) {
|
|
18
|
+
vm.setProp(arr, i, marshallToVm(value[i], vm));
|
|
19
|
+
}
|
|
20
|
+
return arr;
|
|
21
|
+
} else {
|
|
22
|
+
const obj = vm.newObject();
|
|
23
|
+
for (const key in value) {
|
|
24
|
+
vm.setProp(obj, key, marshallToVm(value[key], vm));
|
|
25
|
+
}
|
|
26
|
+
return obj;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
module.exports = {
|
|
32
|
+
marshallToVm
|
|
33
|
+
};
|
package/src/utils.js
CHANGED
|
@@ -150,6 +150,10 @@ const uuid = () => {
|
|
|
150
150
|
const customNanoId = customAlphabet(urlAlphabet, 21);
|
|
151
151
|
|
|
152
152
|
return customNanoId();
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
const appendAwaitToTestFunc = (str) => {
|
|
156
|
+
return str.replace(/(?<!\.\s*)(?<!await\s)(test\()/g, 'await $1');
|
|
153
157
|
};
|
|
154
158
|
|
|
155
159
|
module.exports = {
|
|
@@ -158,5 +162,6 @@ module.exports = {
|
|
|
158
162
|
createResponseParser,
|
|
159
163
|
internalExpressionCache,
|
|
160
164
|
cleanJson,
|
|
161
|
-
uuid
|
|
165
|
+
uuid,
|
|
166
|
+
appendAwaitToTestFunc
|
|
162
167
|
};
|