@usebruno/js 0.46.1 → 0.47.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 +7 -6
- package/src/bru.js +173 -75
- package/src/bruno-request.js +2 -0
- package/src/cookie-list.js +272 -0
- package/src/index.js +27 -2
- package/src/property-list.js +184 -0
- package/src/readonly-property-list.js +227 -0
- package/src/runtime/assert-runtime.js +8 -10
- package/src/runtime/script-runtime.js +55 -6
- package/src/runtime/test-runtime.js +19 -1
- package/src/runtime/vars-runtime.js +20 -3
- package/src/sandbox/bundle-libraries.js +1 -1
- package/src/sandbox/quickjs/index.js +2 -34
- package/src/sandbox/quickjs/shims/bru.js +31 -1
- package/src/sandbox/quickjs/shims/bruno-response.js +29 -2
- package/src/sandbox/quickjs/shims/bruno-response.spec.js +91 -0
- package/src/sandbox/quickjs/shims/lib/uuid.spec.js +166 -0
- package/src/sandbox/quickjs/shims/require.js +56 -0
- package/src/sandbox/quickjs/shims/require.spec.js +154 -0
- package/src/sandbox/quickjs/shims/test.js +2 -2
- package/src/sandbox/quickjs/utils/property-list-bridge.js +165 -0
- package/src/sandbox/quickjs/utils/test-helpers.js +31 -0
- package/src/utils/error-formatter.js +345 -20
- package/src/utils/error-formatter.spec.js +683 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@usebruno/js",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.47.0",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"main": "src/index.js",
|
|
6
6
|
"files": [
|
|
@@ -13,8 +13,8 @@
|
|
|
13
13
|
"prepack": "npm run test"
|
|
14
14
|
},
|
|
15
15
|
"dependencies": {
|
|
16
|
-
"@usebruno/common": "0.
|
|
17
|
-
"@usebruno/query": "0.2.
|
|
16
|
+
"@usebruno/common": "0.21.0",
|
|
17
|
+
"@usebruno/query": "0.2.2",
|
|
18
18
|
"ajv": "^8.12.0",
|
|
19
19
|
"ajv-formats": "^2.1.1",
|
|
20
20
|
"atob": "^2.1.2",
|
|
@@ -24,6 +24,7 @@
|
|
|
24
24
|
"chai-string": "^1.5.0",
|
|
25
25
|
"cheerio": "^1.0.0",
|
|
26
26
|
"crypto-js": "^4.2.0",
|
|
27
|
+
"handlebars": "^4.7.9",
|
|
27
28
|
"json-query": "^2.2.2",
|
|
28
29
|
"jsonwebtoken": "^9.0.3",
|
|
29
30
|
"lodash": "^4.17.21",
|
|
@@ -33,7 +34,7 @@
|
|
|
33
34
|
"path": "^0.12.7",
|
|
34
35
|
"quickjs-emscripten": "^0.29.2",
|
|
35
36
|
"tv4": "^1.3.0",
|
|
36
|
-
"uuid": "^
|
|
37
|
+
"uuid": "^10.0.0",
|
|
37
38
|
"xml-formatter": "^3.5.0",
|
|
38
39
|
"xml2js": "^0.6.2",
|
|
39
40
|
"yaml": "^2.3.4"
|
|
@@ -41,7 +42,7 @@
|
|
|
41
42
|
"devDependencies": {
|
|
42
43
|
"@rollup/plugin-commonjs": "^23.0.2",
|
|
43
44
|
"@rollup/plugin-node-resolve": "^15.0.1",
|
|
44
|
-
"rollup": "
|
|
45
|
-
"rollup
|
|
45
|
+
"@rollup/plugin-terser": "^1.0.0",
|
|
46
|
+
"rollup": "3.30.0"
|
|
46
47
|
}
|
|
47
48
|
}
|
package/src/bru.js
CHANGED
|
@@ -3,31 +3,90 @@ const { uuid } = require('./utils');
|
|
|
3
3
|
const xmlFormat = require('xml-formatter');
|
|
4
4
|
const { interpolate: _interpolate } = require('@usebruno/common');
|
|
5
5
|
const { sendRequest, createSendRequest } = require('@usebruno/requests').scripting;
|
|
6
|
-
const { jar: createCookieJar } = require('@usebruno/requests').cookies;
|
|
7
|
-
|
|
6
|
+
const { jar: createCookieJar, getCookiesForUrl } = require('@usebruno/requests').cookies;
|
|
7
|
+
const CookieList = require('./cookie-list');
|
|
8
|
+
const Handlebars = require('handlebars');
|
|
9
|
+
|
|
10
|
+
const VISUALIZATION_CLEAR = '__clear__';
|
|
11
|
+
const MAX_TEMPLATE_LENGTH = 1_000_000; // 1 million characters
|
|
12
|
+
/*
|
|
13
|
+
* All Handlebars.compile() options (v4.x):
|
|
14
|
+
* data - set false to disable @data tracking
|
|
15
|
+
* compat - enable recursive field lookup
|
|
16
|
+
* knownHelpers - hash of helpers known at compile time (optimization)
|
|
17
|
+
* knownHelpersOnly - only allow known helpers (optimization) [SAFE]
|
|
18
|
+
* noEscape - skip HTML escaping of content [SAFE]
|
|
19
|
+
* strict - throw on missing fields instead of ignoring [SAFE]
|
|
20
|
+
* assumeObjects - skip object existence checks on paths [SAFE]
|
|
21
|
+
* preventIndent - disable auto-indent for partials [SAFE]
|
|
22
|
+
* ignoreStandalone - disable standalone tag removal [SAFE]
|
|
23
|
+
* explicitPartialContext - disable implicit context for partials [SAFE]
|
|
24
|
+
*
|
|
25
|
+
* Excluded from allowlist:
|
|
26
|
+
* data, compat, knownHelpers - can alter template resolution in unexpected ways
|
|
27
|
+
* allowProtoPropertiesByDefault, allowProtoMethodsByDefault - defense-in-depth against
|
|
28
|
+
* prototype traversal. Handlebars >=4.3.0 fixed the original CVE-2019-19919 and >=4.7.0
|
|
29
|
+
* hardcodes blocks on __proto__, constructor, etc. even when these flags are true, but
|
|
30
|
+
* we still exclude them to avoid relaxing the security posture.
|
|
31
|
+
* https://nvd.nist.gov/vuln/detail/CVE-2019-19919
|
|
32
|
+
*
|
|
33
|
+
* Use `dangerouslyAllowAllOptions: true` in data.options to bypass the allowlist.
|
|
34
|
+
*/
|
|
35
|
+
const SAFE_HBS_OPTIONS = new Set([
|
|
36
|
+
'noEscape', 'strict', 'assumeObjects', 'preventIndent',
|
|
37
|
+
'ignoreStandalone', 'explicitPartialContext', 'knownHelpersOnly'
|
|
38
|
+
]);
|
|
8
39
|
const variableNameRegex = /^[\w-.]*$/;
|
|
9
40
|
|
|
10
41
|
class Bru {
|
|
11
42
|
/**
|
|
12
|
-
* @param {
|
|
13
|
-
* @
|
|
14
|
-
* @
|
|
15
|
-
* @
|
|
16
|
-
* @
|
|
17
|
-
* @
|
|
18
|
-
* @
|
|
19
|
-
* @
|
|
20
|
-
* @
|
|
21
|
-
* @
|
|
22
|
-
* @
|
|
23
|
-
* @
|
|
24
|
-
* @
|
|
25
|
-
* @
|
|
26
|
-
* @
|
|
27
|
-
* @
|
|
28
|
-
* @
|
|
43
|
+
* @param {object} options - Single options object (destructured)
|
|
44
|
+
* @property {string} options.runtime - The runtime environment ('quickjs' or 'nodevm')
|
|
45
|
+
* @property {object} [options.envVariables={}] - Environment variables
|
|
46
|
+
* @property {object} [options.runtimeVariables={}] - Runtime variables
|
|
47
|
+
* @property {object} [options.processEnvVars={}] - Process environment variables (deep cloned)
|
|
48
|
+
* @property {string} [options.collectionPath] - Path to the collection
|
|
49
|
+
* @property {function} [options.historyLogger] - History logger function
|
|
50
|
+
* @property {function} [options.setVisualizations] - Visualizations setter function
|
|
51
|
+
* @property {object} [options.secretVariables={}] - Secret variables (deep cloned)
|
|
52
|
+
* @property {object} [options.collectionVariables={}] - Collection-level variables
|
|
53
|
+
* @property {object} [options.folderVariables={}] - Folder-level variables
|
|
54
|
+
* @property {object} [options.requestVariables={}] - Request-level variables
|
|
55
|
+
* @property {object} [options.globalEnvironmentVariables={}] - Global environment variables
|
|
56
|
+
* @property {object} [options.oauth2CredentialVariables={}] - OAuth2 credential variables
|
|
57
|
+
* @property {object} [options.iterationDetails] - Iteration details for runner
|
|
58
|
+
* @property {string} [options.collectionName] - Name of the collection
|
|
59
|
+
* @property {object} [options.promptVariables={}] - Prompt variables
|
|
60
|
+
* @property {object} [options.certsAndProxyConfig] - Configuration for bru.sendRequest (proxy, certs, TLS)
|
|
61
|
+
* @property {string} [options.certsAndProxyConfig.collectionPath] - Path to the collection
|
|
62
|
+
* @property {object} [options.certsAndProxyConfig.options] - TLS and proxy options
|
|
63
|
+
* @property {object} [options.certsAndProxyConfig.clientCertificates] - Client certificate configuration
|
|
64
|
+
* @property {object} [options.certsAndProxyConfig.collectionLevelProxy] - Collection-level proxy settings
|
|
65
|
+
* @property {object} [options.certsAndProxyConfig.systemProxyConfig] - System proxy configuration
|
|
66
|
+
* @property {string} [options.requestUrl] - The URL of the current request (used for cookie access)
|
|
67
|
+
* @property {function} [options.onConsoleLog] - Console callback `(type, args) => void` to route warnings to the UI
|
|
29
68
|
*/
|
|
30
|
-
constructor(
|
|
69
|
+
constructor({
|
|
70
|
+
runtime,
|
|
71
|
+
envVariables,
|
|
72
|
+
runtimeVariables,
|
|
73
|
+
processEnvVars,
|
|
74
|
+
collectionPath,
|
|
75
|
+
historyLogger,
|
|
76
|
+
setVisualizations,
|
|
77
|
+
secretVariables,
|
|
78
|
+
collectionVariables,
|
|
79
|
+
folderVariables,
|
|
80
|
+
requestVariables,
|
|
81
|
+
globalEnvironmentVariables,
|
|
82
|
+
oauth2CredentialVariables,
|
|
83
|
+
iterationDetails,
|
|
84
|
+
collectionName,
|
|
85
|
+
promptVariables,
|
|
86
|
+
certsAndProxyConfig,
|
|
87
|
+
requestUrl,
|
|
88
|
+
onConsoleLog
|
|
89
|
+
}) {
|
|
31
90
|
this.envVariables = envVariables || {};
|
|
32
91
|
this.runtimeVariables = runtimeVariables || {};
|
|
33
92
|
this.promptVariables = promptVariables || {};
|
|
@@ -41,58 +100,18 @@ class Bru {
|
|
|
41
100
|
this.collectionPath = collectionPath;
|
|
42
101
|
this.historyLogger = historyLogger;
|
|
43
102
|
this.setVisualizations = setVisualizations;
|
|
103
|
+
this.onConsoleLog = onConsoleLog || null;
|
|
44
104
|
this.collectionName = collectionName;
|
|
45
105
|
// Use createSendRequest with config if provided, otherwise use default sendRequest
|
|
46
106
|
this.sendRequest = certsAndProxyConfig ? createSendRequest(certsAndProxyConfig) : sendRequest;
|
|
47
107
|
this.runtime = runtime;
|
|
48
|
-
this.
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
return cookieJar.getCookie(interpolatedUrl, cookieName, callback);
|
|
56
|
-
},
|
|
57
|
-
|
|
58
|
-
getCookies: (url, callback) => {
|
|
59
|
-
const interpolatedUrl = this.interpolate(url);
|
|
60
|
-
return cookieJar.getCookies(interpolatedUrl, callback);
|
|
61
|
-
},
|
|
62
|
-
|
|
63
|
-
setCookie: (url, nameOrCookieObj, valueOrCallback, maybeCallback) => {
|
|
64
|
-
const interpolatedUrl = this.interpolate(url);
|
|
65
|
-
return cookieJar.setCookie(interpolatedUrl, nameOrCookieObj, valueOrCallback, maybeCallback);
|
|
66
|
-
},
|
|
67
|
-
|
|
68
|
-
setCookies: (url, cookiesArray, callback) => {
|
|
69
|
-
const interpolatedUrl = this.interpolate(url);
|
|
70
|
-
return cookieJar.setCookies(interpolatedUrl, cookiesArray, callback);
|
|
71
|
-
},
|
|
72
|
-
|
|
73
|
-
// Clear entire cookie jar
|
|
74
|
-
clear: (callback) => {
|
|
75
|
-
return cookieJar.clear(callback);
|
|
76
|
-
},
|
|
77
|
-
|
|
78
|
-
// Delete cookies for a specific URL/domain
|
|
79
|
-
deleteCookies: (url, callback) => {
|
|
80
|
-
const interpolatedUrl = this.interpolate(url);
|
|
81
|
-
return cookieJar.deleteCookies(interpolatedUrl, callback);
|
|
82
|
-
},
|
|
83
|
-
|
|
84
|
-
deleteCookie: (url, cookieName, callback) => {
|
|
85
|
-
const interpolatedUrl = this.interpolate(url);
|
|
86
|
-
return cookieJar.deleteCookie(interpolatedUrl, cookieName, callback);
|
|
87
|
-
},
|
|
88
|
-
|
|
89
|
-
hasCookie: (url, cookieName, callback) => {
|
|
90
|
-
const interpolatedUrl = this.interpolate(url);
|
|
91
|
-
return cookieJar.hasCookie(interpolatedUrl, cookieName, callback);
|
|
92
|
-
}
|
|
93
|
-
};
|
|
94
|
-
}
|
|
95
|
-
};
|
|
108
|
+
this.requestUrl = requestUrl;
|
|
109
|
+
this.cookies = new CookieList({
|
|
110
|
+
getUrl: () => this.interpolate(this.requestUrl),
|
|
111
|
+
interpolate: (str) => this.interpolate(str),
|
|
112
|
+
createCookieJar,
|
|
113
|
+
getCookiesForUrl
|
|
114
|
+
});
|
|
96
115
|
// Holds variables that are marked as persistent by scripts
|
|
97
116
|
this.persistentEnvVariables = {};
|
|
98
117
|
// Holds credential IDs to be reset after script execution
|
|
@@ -414,9 +433,41 @@ class Bru {
|
|
|
414
433
|
return this.secretVariables?.[`$secrets.${key}`];
|
|
415
434
|
}
|
|
416
435
|
|
|
436
|
+
/**
|
|
437
|
+
* Render a visualization in the response pane.
|
|
438
|
+
*
|
|
439
|
+
* @param {'table' | 'html'} type - The visualization type.
|
|
440
|
+
* @param {Object} data - Shape depends on `type`:
|
|
441
|
+
*
|
|
442
|
+
* **When `type` is `'table'`:**
|
|
443
|
+
* @param {string} data.provider - `'ag-grid'` or `'react-table'`.
|
|
444
|
+
* @param {Object} data.props - Props forwarded to the grid component.
|
|
445
|
+
* @param {Array} data.props.columnDefinitions - (ag-grid) Column definitions.
|
|
446
|
+
* @param {Array} data.props.rowData - (ag-grid) Row data.
|
|
447
|
+
*
|
|
448
|
+
* **When `type` is `'html'`** (raw mode):
|
|
449
|
+
* @param {string} data.content - Pre-rendered HTML string.
|
|
450
|
+
*
|
|
451
|
+
* **When `type` is `'html'`** (Handlebars template mode):
|
|
452
|
+
* @param {string} data.template - Handlebars template string (required).
|
|
453
|
+
* @param {Object} [data.data={}] - Context object passed to the compiled template.
|
|
454
|
+
* @param {Object} [data.options] - Handlebars compile options.
|
|
455
|
+
* @param {boolean} [data.options.noEscape] - Disable HTML escaping.
|
|
456
|
+
* @param {boolean} [data.options.strict] - Throw on missing variables.
|
|
457
|
+
* @param {boolean} [data.options.assumeObjects] - Skip null checks on nested paths.
|
|
458
|
+
* @param {boolean} [data.options.dangerouslyAllowAllOptions] - Bypass the safe-options
|
|
459
|
+
* allowlist (forwards all remaining options directly to `Handlebars.compile`).
|
|
460
|
+
*
|
|
461
|
+
* @throws {Error} If `type` is falsy.
|
|
462
|
+
* @throws {Error} If `type` is `'html'` and neither `data.content` nor `data.template` is provided.
|
|
463
|
+
* @throws {Error} If template exceeds `MAX_TEMPLATE_LENGTH`.
|
|
464
|
+
*/
|
|
417
465
|
visualize(type, data) {
|
|
418
|
-
if (type
|
|
419
|
-
|
|
466
|
+
if (!type) {
|
|
467
|
+
throw new Error('visualization type is required');
|
|
468
|
+
}
|
|
469
|
+
if (type === 'table') {
|
|
470
|
+
if (data?.provider === 'ag-grid') {
|
|
420
471
|
if (!data?.props?.columnDefinitions) {
|
|
421
472
|
throw new Error(`columns definitions are required`);
|
|
422
473
|
}
|
|
@@ -424,17 +475,63 @@ class Bru {
|
|
|
424
475
|
throw new Error(`row data is required`);
|
|
425
476
|
}
|
|
426
477
|
this.setVisualizations({ uid: uuid(), type, data });
|
|
427
|
-
} else if (data?.provider
|
|
478
|
+
} else if (data?.provider === 'react-table') {
|
|
428
479
|
this.setVisualizations({ uid: uuid(), type, data });
|
|
429
480
|
}
|
|
430
|
-
} else if (type
|
|
431
|
-
if (
|
|
432
|
-
|
|
481
|
+
} else if (type === 'html') {
|
|
482
|
+
if (typeof data?.template === 'string') {
|
|
483
|
+
// Handlebars template mode (translated from pm.visualizer.set)
|
|
484
|
+
if (data.template.length > MAX_TEMPLATE_LENGTH) {
|
|
485
|
+
throw new Error(`template exceeds maximum length of ${MAX_TEMPLATE_LENGTH} characters`);
|
|
486
|
+
}
|
|
487
|
+
// Isolated instance so globally-registered helpers can't be exploited (SSTI prevention)
|
|
488
|
+
const hbs = Handlebars.create();
|
|
489
|
+
// Allowlist safe compile options to prevent re-enabling prototype traversal (CVE-2019-19919)
|
|
490
|
+
let compileOptions;
|
|
491
|
+
if (data.options?.dangerouslyAllowAllOptions) {
|
|
492
|
+
const { dangerouslyAllowAllOptions, ...rest } = data.options;
|
|
493
|
+
compileOptions = rest;
|
|
494
|
+
} else {
|
|
495
|
+
compileOptions = {};
|
|
496
|
+
if (data.options) {
|
|
497
|
+
const blocked = [];
|
|
498
|
+
for (const key of Object.keys(data.options)) {
|
|
499
|
+
if (SAFE_HBS_OPTIONS.has(key)) {
|
|
500
|
+
compileOptions[key] = data.options[key];
|
|
501
|
+
} else {
|
|
502
|
+
blocked.push(key);
|
|
503
|
+
}
|
|
504
|
+
}
|
|
505
|
+
if (blocked.length > 0) {
|
|
506
|
+
const msg = `bru.visualize: the following compile options were blocked and ignored: ${blocked.map((k) => `"${k}"`).join(', ')}. `
|
|
507
|
+
+ 'Use { dangerouslyAllowAllOptions: true } in options to bypass the allowlist.';
|
|
508
|
+
if (this.onConsoleLog) {
|
|
509
|
+
this.onConsoleLog('warn', [msg]);
|
|
510
|
+
} else {
|
|
511
|
+
console.warn(msg);
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
const compiled = hbs.compile(data.template, compileOptions);
|
|
517
|
+
const renderedHtml = compiled(data.data || {});
|
|
518
|
+
this.setVisualizations({
|
|
519
|
+
uid: uuid(), type,
|
|
520
|
+
data: { content: renderedHtml, rawData: data.data || {} }
|
|
521
|
+
});
|
|
522
|
+
} else if (data?.content) {
|
|
523
|
+
// Existing raw HTML mode
|
|
524
|
+
this.setVisualizations({ uid: uuid(), type, data });
|
|
525
|
+
} else {
|
|
526
|
+
throw new Error('html content or template is required');
|
|
433
527
|
}
|
|
434
|
-
this.setVisualizations({ uid: uuid(), type, data });
|
|
435
528
|
}
|
|
436
529
|
}
|
|
437
530
|
|
|
531
|
+
clearVisualizations() {
|
|
532
|
+
this.setVisualizations({ type: VISUALIZATION_CLEAR });
|
|
533
|
+
}
|
|
534
|
+
|
|
438
535
|
sleep(ms) {
|
|
439
536
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
440
537
|
}
|
|
@@ -488,3 +585,4 @@ class IterationDataManager {
|
|
|
488
585
|
}
|
|
489
586
|
|
|
490
587
|
module.exports = Bru;
|
|
588
|
+
module.exports.VISUALIZATION_CLEAR = VISUALIZATION_CLEAR;
|
package/src/bruno-request.js
CHANGED
|
@@ -98,6 +98,8 @@ class BrunoRequest {
|
|
|
98
98
|
getAuthMode() {
|
|
99
99
|
if (this.req?.oauth2) {
|
|
100
100
|
return 'oauth2';
|
|
101
|
+
} else if (this.req?.oauth1config) {
|
|
102
|
+
return 'oauth1';
|
|
101
103
|
} else if (this.headers?.['Authorization']?.startsWith('Bearer')) {
|
|
102
104
|
return 'bearer';
|
|
103
105
|
} else if (this.headers?.['Authorization']?.startsWith('Basic') || this.req?.auth?.username) {
|
|
@@ -0,0 +1,272 @@
|
|
|
1
|
+
const PropertyList = require('./property-list');
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* CookieList — the `bru.cookies` API for reading and writing cookies in scripts.
|
|
5
|
+
*
|
|
6
|
+
* Extends PropertyList in dynamic mode: the cookie list is freshly read from the
|
|
7
|
+
* cookie jar on every access, and write operations delegate to the jar rather
|
|
8
|
+
* than mutating an in-memory array.
|
|
9
|
+
*
|
|
10
|
+
* ---
|
|
11
|
+
*
|
|
12
|
+
* ## Cookie object shape
|
|
13
|
+
*
|
|
14
|
+
* Every cookie surfaced by this list is a plain object:
|
|
15
|
+
*
|
|
16
|
+
* ```js
|
|
17
|
+
* { key, value, domain, path, secure, httpOnly, expires }
|
|
18
|
+
* ```
|
|
19
|
+
*
|
|
20
|
+
* ---
|
|
21
|
+
*
|
|
22
|
+
* ## Read methods (inherited from ReadOnlyPropertyList)
|
|
23
|
+
*
|
|
24
|
+
* | Method | Description | Example return value |
|
|
25
|
+
* |--------------------|--------------------------------------------------|---------------------------------------|
|
|
26
|
+
* | `get(name)` | Value of the first cookie with `key === name` | `'abc123'` |
|
|
27
|
+
* | `one(name)` | Full cookie object for `key === name` | `{ key: 'sid', value: 'abc123', … }` |
|
|
28
|
+
* | `all()` | Cloned array of all cookie objects | `[{ key: 'sid', … }, …]` |
|
|
29
|
+
* | `idx(index)` | Cookie at positional index | `{ key: 'sid', … }` |
|
|
30
|
+
* | `count()` | Number of cookies for the current request URL | `3` |
|
|
31
|
+
*
|
|
32
|
+
* ## Search methods (inherited)
|
|
33
|
+
*
|
|
34
|
+
* | Method | Description | Example return value |
|
|
35
|
+
* |--------------------|--------------------------------------------------|----------------------|
|
|
36
|
+
* | `has(name)` | `true` if a cookie with that key exists | `true` |
|
|
37
|
+
* | `has(name, value)` | `true` if key exists **and** value matches | `false` |
|
|
38
|
+
* | `find(predicate)` | First cookie matching the predicate function | `{ key: 'sid', … }` |
|
|
39
|
+
* | `filter(predicate)`| Array of cookies matching the predicate | `[{ key: … }, …]` |
|
|
40
|
+
* | `indexOf(item)` | Index of a structurally-equal cookie, or `-1` | `0` |
|
|
41
|
+
*
|
|
42
|
+
* ## Iteration methods (inherited)
|
|
43
|
+
*
|
|
44
|
+
* | Method | Description |
|
|
45
|
+
* |-------------------------|----------------------------------------------|
|
|
46
|
+
* | `each(fn)` | Calls `fn(cookie, index)` for every cookie |
|
|
47
|
+
* | `map(fn)` | Returns a new array of mapped values |
|
|
48
|
+
* | `reduce(fn, initial?)` | Reduces cookies to a single value |
|
|
49
|
+
*
|
|
50
|
+
* ## Transform methods (inherited)
|
|
51
|
+
*
|
|
52
|
+
* | Method | Description | Example return value |
|
|
53
|
+
* |---------------|-----------------------------------------------------|-------------------------------------|
|
|
54
|
+
* | `toObject()` | `{ key: value }` map of all cookies | `{ sid: 'abc123', lang: 'en' }` |
|
|
55
|
+
* | `toString()` | Semicolon-separated `key=value` string | `'sid=abc123; lang=en'` |
|
|
56
|
+
* | `toJSON()` | Same as `all()` — suitable for `JSON.stringify()` | `[{ key: 'sid', … }]` |
|
|
57
|
+
*
|
|
58
|
+
* ## Write methods (CookieList overrides)
|
|
59
|
+
*
|
|
60
|
+
* | Method | Description |
|
|
61
|
+
* |--------------------------|------------------------------------------------------|
|
|
62
|
+
* | `add(cookieObj, cb?)` | Alias for `upsert()` — sets a cookie in the jar |
|
|
63
|
+
* | `upsert(cookieObj, cb?)` | Sets (or replaces) a cookie in the jar |
|
|
64
|
+
* | `remove(name, cb?)` | Deletes a single cookie by name (no-op if missing) |
|
|
65
|
+
* | `delete(name, cb?)` | Alias for `remove()` |
|
|
66
|
+
* | `clear(cb?)` | Removes **all** cookies for the current request URL |
|
|
67
|
+
*
|
|
68
|
+
* ## Jar access
|
|
69
|
+
*
|
|
70
|
+
* | Method | Description |
|
|
71
|
+
* |---------|--------------------------------------------------------------------------|
|
|
72
|
+
* | `jar()` | Returns a jar handle with URL interpolation for cross-URL cookie access |
|
|
73
|
+
*
|
|
74
|
+
* The jar handle exposes: `getCookie`, `getCookies`, `setCookie`, `setCookies`,
|
|
75
|
+
* `deleteCookie`, `deleteCookies`, `hasCookie`, and `clear`.
|
|
76
|
+
*/
|
|
77
|
+
class CookieList extends PropertyList {
|
|
78
|
+
/**
|
|
79
|
+
* @param {object} options
|
|
80
|
+
* @param {Function} options.getUrl - Returns the interpolated request URL (or falsy if unavailable)
|
|
81
|
+
* @param {Function} options.interpolate - Interpolates variables in a string
|
|
82
|
+
* @param {Function} options.createCookieJar - Factory that returns a cookie jar instance
|
|
83
|
+
* @param {Function} options.getCookiesForUrl - Returns cookies array for a given URL
|
|
84
|
+
*/
|
|
85
|
+
constructor({ getUrl, interpolate, createCookieJar, getCookiesForUrl }) {
|
|
86
|
+
super({
|
|
87
|
+
keyProperty: 'key',
|
|
88
|
+
dataSource: () => {
|
|
89
|
+
const url = getUrl();
|
|
90
|
+
if (!url) return [];
|
|
91
|
+
// Normalize tough-cookie Cookie instances to plain objects to avoid
|
|
92
|
+
// circular references and exposing internal library structures.
|
|
93
|
+
return getCookiesForUrl(url).map(({ key, value, domain, path, secure, httpOnly, expires }) =>
|
|
94
|
+
({ key, value, domain, path, secure, httpOnly, expires })
|
|
95
|
+
);
|
|
96
|
+
}
|
|
97
|
+
});
|
|
98
|
+
this._getUrl = getUrl;
|
|
99
|
+
this._interpolateFn = interpolate;
|
|
100
|
+
// Factory function — returns a wrapper around the module-level cookie jar singleton
|
|
101
|
+
this._createCookieJar = createCookieJar;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// ── Write methods (cookie jar delegation) ─────────────────────────────
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Add a cookie to the jar (alias for {@link CookieList#upsert}).
|
|
108
|
+
*
|
|
109
|
+
* @param {object} cookieObj - Cookie object with at least `key` and `value`.
|
|
110
|
+
* @param {Function} [callback] - Optional `(error) => void` callback. If omitted, returns a Promise.
|
|
111
|
+
* @returns {Promise<void>|void} A Promise when no callback is given.
|
|
112
|
+
* @example
|
|
113
|
+
* // Promise usage
|
|
114
|
+
* await bru.cookies.add({ key: 'lang', value: 'en' });
|
|
115
|
+
*
|
|
116
|
+
* // Callback usage
|
|
117
|
+
* bru.cookies.add({ key: 'lang', value: 'en' }, (err) => { if (err) throw err; });
|
|
118
|
+
*/
|
|
119
|
+
add(cookieObj, callback) {
|
|
120
|
+
return this.upsert(cookieObj, callback);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Set (or replace) a cookie in the jar for the current request URL.
|
|
125
|
+
*
|
|
126
|
+
* If a cookie with the same key already exists for this URL, it is overwritten.
|
|
127
|
+
* Rejects with an error if `cookieObj` is not a non-null object.
|
|
128
|
+
*
|
|
129
|
+
* @param {object} cookieObj - Cookie object with at least `key` and `value`.
|
|
130
|
+
* @param {Function} [callback] - Optional `(error) => void` callback. If omitted, returns a Promise.
|
|
131
|
+
* @returns {Promise<void>|void} A Promise when no callback is given.
|
|
132
|
+
* @example
|
|
133
|
+
* await bru.cookies.upsert({ key: 'sid', value: 'abc123', secure: true });
|
|
134
|
+
*/
|
|
135
|
+
upsert(cookieObj, callback) {
|
|
136
|
+
if (!cookieObj || typeof cookieObj !== 'object') {
|
|
137
|
+
const error = new Error('cookieObj must be a non-null object');
|
|
138
|
+
if (callback) return callback(error);
|
|
139
|
+
return Promise.reject(error);
|
|
140
|
+
}
|
|
141
|
+
const url = this._getUrl();
|
|
142
|
+
if (!url) {
|
|
143
|
+
if (callback) return callback(undefined);
|
|
144
|
+
return Promise.resolve();
|
|
145
|
+
}
|
|
146
|
+
const jar = this._createCookieJar();
|
|
147
|
+
return jar.setCookie(url, cookieObj, callback);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Remove a single cookie by name from the current request URL.
|
|
152
|
+
*
|
|
153
|
+
* A no-op if `name` is falsy or if no cookie with that name exists
|
|
154
|
+
* (analogous to `Map.prototype.delete`).
|
|
155
|
+
*
|
|
156
|
+
* @param {string} name - The cookie key to remove.
|
|
157
|
+
* @param {Function} [callback] - Optional `(error) => void` callback. If omitted, returns a Promise.
|
|
158
|
+
* @returns {Promise<void>|void} A Promise when no callback is given.
|
|
159
|
+
* @example
|
|
160
|
+
* await bru.cookies.remove('sid');
|
|
161
|
+
*/
|
|
162
|
+
remove(name, callback) {
|
|
163
|
+
const url = this._getUrl();
|
|
164
|
+
if (!url || !name) {
|
|
165
|
+
if (callback) return callback(undefined);
|
|
166
|
+
return Promise.resolve();
|
|
167
|
+
}
|
|
168
|
+
const jar = this._createCookieJar();
|
|
169
|
+
return jar.deleteCookie(url, name, callback);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* Remove cookies scoped to the current request URL only.
|
|
174
|
+
* Unlike jar().clear() which removes ALL cookies globally, this only
|
|
175
|
+
* removes cookies matching the current request's domain and path.
|
|
176
|
+
*/
|
|
177
|
+
clear(callback) {
|
|
178
|
+
const url = this._getUrl();
|
|
179
|
+
if (!url) {
|
|
180
|
+
if (callback) return callback(undefined);
|
|
181
|
+
return Promise.resolve();
|
|
182
|
+
}
|
|
183
|
+
const jar = this._createCookieJar();
|
|
184
|
+
return jar.deleteCookies(url, callback);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* Delete a cookie by name (alias for {@link CookieList#remove}).
|
|
189
|
+
*
|
|
190
|
+
* @param {string} name - The cookie key to delete.
|
|
191
|
+
* @param {Function} [callback] - Optional `(error) => void` callback. If omitted, returns a Promise.
|
|
192
|
+
* @returns {Promise<void>|void} A Promise when no callback is given.
|
|
193
|
+
* @example
|
|
194
|
+
* await bru.cookies.delete('sid');
|
|
195
|
+
*/
|
|
196
|
+
delete(name, callback) {
|
|
197
|
+
return this.remove(name, callback);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
// ── Cookie-specific method ────────────────────────────────────────────
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* Returns a jar handle for cross-URL cookie operations.
|
|
204
|
+
*
|
|
205
|
+
* Unlike the CookieList methods (which are scoped to the current request URL),
|
|
206
|
+
* the jar handle lets you read/write cookies for **any** URL. All URL arguments
|
|
207
|
+
* are automatically interpolated with environment/collection variables.
|
|
208
|
+
*
|
|
209
|
+
* @returns {{ getCookie, getCookies, setCookie, setCookies, deleteCookie, deleteCookies, hasCookie, clear }}
|
|
210
|
+
* @example
|
|
211
|
+
* const jar = bru.cookies.jar();
|
|
212
|
+
*
|
|
213
|
+
* // Read a cookie from a different URL
|
|
214
|
+
* const token = await jar.getCookie('{{authBaseUrl}}/login', 'access_token');
|
|
215
|
+
*
|
|
216
|
+
* // Set a cookie on a specific URL
|
|
217
|
+
* await jar.setCookie('{{apiBaseUrl}}', { key: 'sid', value: 'abc' });
|
|
218
|
+
* await jar.setCookie('{{apiBaseUrl}}', 'theme', 'dark');
|
|
219
|
+
*
|
|
220
|
+
* // Check if a cookie exists
|
|
221
|
+
* const exists = await jar.hasCookie('{{apiBaseUrl}}', 'sid');
|
|
222
|
+
*
|
|
223
|
+
* // Clear ALL cookies globally
|
|
224
|
+
* await jar.clear();
|
|
225
|
+
*/
|
|
226
|
+
jar() {
|
|
227
|
+
const cookieJar = this._createCookieJar();
|
|
228
|
+
|
|
229
|
+
return {
|
|
230
|
+
getCookie: (url, cookieName, callback) => {
|
|
231
|
+
const interpolatedUrl = this._interpolateFn(url);
|
|
232
|
+
return cookieJar.getCookie(interpolatedUrl, cookieName, callback);
|
|
233
|
+
},
|
|
234
|
+
|
|
235
|
+
getCookies: (url, callback) => {
|
|
236
|
+
const interpolatedUrl = this._interpolateFn(url);
|
|
237
|
+
return cookieJar.getCookies(interpolatedUrl, callback);
|
|
238
|
+
},
|
|
239
|
+
|
|
240
|
+
setCookie: (url, nameOrCookieObj, valueOrCallback, maybeCallback) => {
|
|
241
|
+
const interpolatedUrl = this._interpolateFn(url);
|
|
242
|
+
return cookieJar.setCookie(interpolatedUrl, nameOrCookieObj, valueOrCallback, maybeCallback);
|
|
243
|
+
},
|
|
244
|
+
|
|
245
|
+
setCookies: (url, cookiesArray, callback) => {
|
|
246
|
+
const interpolatedUrl = this._interpolateFn(url);
|
|
247
|
+
return cookieJar.setCookies(interpolatedUrl, cookiesArray, callback);
|
|
248
|
+
},
|
|
249
|
+
|
|
250
|
+
clear: (callback) => {
|
|
251
|
+
return cookieJar.clear(callback);
|
|
252
|
+
},
|
|
253
|
+
|
|
254
|
+
deleteCookies: (url, callback) => {
|
|
255
|
+
const interpolatedUrl = this._interpolateFn(url);
|
|
256
|
+
return cookieJar.deleteCookies(interpolatedUrl, callback);
|
|
257
|
+
},
|
|
258
|
+
|
|
259
|
+
deleteCookie: (url, cookieName, callback) => {
|
|
260
|
+
const interpolatedUrl = this._interpolateFn(url);
|
|
261
|
+
return cookieJar.deleteCookie(interpolatedUrl, cookieName, callback);
|
|
262
|
+
},
|
|
263
|
+
|
|
264
|
+
hasCookie: (url, cookieName, callback) => {
|
|
265
|
+
const interpolatedUrl = this._interpolateFn(url);
|
|
266
|
+
return cookieJar.hasCookie(interpolatedUrl, cookieName, callback);
|
|
267
|
+
}
|
|
268
|
+
};
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
module.exports = CookieList;
|
package/src/index.js
CHANGED
|
@@ -3,7 +3,21 @@ const TestRuntime = require('./runtime/test-runtime');
|
|
|
3
3
|
const VarsRuntime = require('./runtime/vars-runtime');
|
|
4
4
|
const AssertRuntime = require('./runtime/assert-runtime');
|
|
5
5
|
const { runScriptInNodeVm } = require('./sandbox/node-vm');
|
|
6
|
-
const {
|
|
6
|
+
const {
|
|
7
|
+
formatErrorWithContext,
|
|
8
|
+
formatErrorWithContextV2,
|
|
9
|
+
SCRIPT_TYPES,
|
|
10
|
+
parseErrorLocation,
|
|
11
|
+
adjustLineNumber,
|
|
12
|
+
resolveSegmentError,
|
|
13
|
+
getSourceContext,
|
|
14
|
+
adjustStackTrace,
|
|
15
|
+
getErrorTypeName,
|
|
16
|
+
findScriptBlockStartLine,
|
|
17
|
+
findScriptBlockEndLine,
|
|
18
|
+
findYmlScriptBlockStartLine,
|
|
19
|
+
findYmlScriptBlockEndLine
|
|
20
|
+
} = require('./utils/error-formatter');
|
|
7
21
|
|
|
8
22
|
module.exports = {
|
|
9
23
|
ScriptRuntime,
|
|
@@ -12,5 +26,16 @@ module.exports = {
|
|
|
12
26
|
AssertRuntime,
|
|
13
27
|
runScriptInNodeVm,
|
|
14
28
|
formatErrorWithContext,
|
|
15
|
-
|
|
29
|
+
formatErrorWithContextV2,
|
|
30
|
+
SCRIPT_TYPES,
|
|
31
|
+
parseErrorLocation,
|
|
32
|
+
adjustLineNumber,
|
|
33
|
+
resolveSegmentError,
|
|
34
|
+
getSourceContext,
|
|
35
|
+
adjustStackTrace,
|
|
36
|
+
getErrorTypeName,
|
|
37
|
+
findScriptBlockStartLine,
|
|
38
|
+
findScriptBlockEndLine,
|
|
39
|
+
findYmlScriptBlockStartLine,
|
|
40
|
+
findYmlScriptBlockEndLine
|
|
16
41
|
};
|