Compare commits

...

4 Commits

Author SHA1 Message Date
447b1b406f Update balena-sdk to 21.4.7
Update balena-sdk from 21.3.0 to 21.4.7

Change-type: patch
2025-06-02 15:31:16 +03:00
6688e1b575 Enable native fetch mocking with nock
Change-type: patch
See: https://github.com/nock/nock/blob/v15.0.0-beta.3/lib/intercept.js#L6
2025-06-02 15:31:16 +03:00
2b76a3999f Deduplicate dependencies 2025-06-02 15:31:14 +03:00
ebbdb6f96a Update nock to 15.0.0-beta.3 to work with native fetch
Nock 15 adds support for native fetch.

Change-type: patch
See: https://github.com/nock/nock/blob/beta/migration_guides/migrating_to_15.md
2025-06-02 15:30:34 +03:00
9 changed files with 132 additions and 164 deletions

130
npm-shrinkwrap.json generated
View File

@ -21,7 +21,7 @@
"balena-errors": "^4.7.3",
"balena-image-fs": "^7.5.2",
"balena-preload": "^18.0.4",
"balena-sdk": "^21.3.0",
"balena-sdk": "^21.4.7",
"balena-semver": "^2.3.0",
"balena-settings-client": "^5.0.2",
"balena-settings-storage": "^8.1.0",
@ -151,14 +151,15 @@
"mocha": "^10.6.0",
"mock-fs": "^5.2.0",
"mock-require": "^3.0.3",
"nock": "^14.0.4",
"nock": "^15.0.0-beta.3",
"oclif": "^4.17.0",
"rewire": "^7.0.0",
"simple-git": "^3.14.1",
"sinon": "^19.0.0",
"string-to-stream": "^3.0.1",
"ts-node": "^10.4.0",
"typescript": "^5.8.2"
"typescript": "^5.8.2",
"undici": "^7.10.0"
},
"engines": {
"node": "^20.6.0"
@ -1566,9 +1567,9 @@
}
},
"node_modules/@balena/lint/node_modules/@eslint/js": {
"version": "9.27.0",
"resolved": "https://registry.npmjs.org/@eslint/js/-/js-9.27.0.tgz",
"integrity": "sha512-G5JD9Tu5HJEu4z2Uo4aHY2sLV64B7CDMXxFzqzjl3NKd6RVzSXNoE80jk7Y0lJkTTkjiIhBAqmlYwjuBY3tvpA==",
"version": "9.28.0",
"resolved": "https://registry.npmjs.org/@eslint/js/-/js-9.28.0.tgz",
"integrity": "sha512-fnqSjGWd/CoIp4EXIxWVK/sHA6DOHN4+8Ix2cX5ycOY7LG0UY8nHCU5pIp2eaE1Mc7Qd8kHspYNzYXT2ojPLzg==",
"dev": true,
"license": "MIT",
"engines": {
@ -1642,9 +1643,9 @@
}
},
"node_modules/@balena/lint/node_modules/eslint": {
"version": "9.27.0",
"resolved": "https://registry.npmjs.org/eslint/-/eslint-9.27.0.tgz",
"integrity": "sha512-ixRawFQuMB9DZ7fjU3iGGganFDp3+45bPOdaRurcFHSXO1e/sYwUX/FtQZpLZJR6SjMoJH8hR2pPEAfDyCoU2Q==",
"version": "9.28.0",
"resolved": "https://registry.npmjs.org/eslint/-/eslint-9.28.0.tgz",
"integrity": "sha512-ocgh41VhRlf9+fVpe7QKzwLj9c92fDiqOj8Y3Sd4/ZmVA4Btx4PlUYPq4pp9JDyupkf1upbEXecxL2mwNV7jPQ==",
"dev": true,
"license": "MIT",
"dependencies": {
@ -1654,7 +1655,7 @@
"@eslint/config-helpers": "^0.2.1",
"@eslint/core": "^0.14.0",
"@eslint/eslintrc": "^3.3.1",
"@eslint/js": "9.27.0",
"@eslint/js": "9.28.0",
"@eslint/plugin-kit": "^0.3.1",
"@humanfs/node": "^0.16.6",
"@humanwhocodes/module-importer": "^1.0.1",
@ -1974,15 +1975,6 @@
"node": ">=16"
}
},
"node_modules/@balena/node-web-streams": {
"version": "0.2.4",
"resolved": "https://registry.npmjs.org/@balena/node-web-streams/-/node-web-streams-0.2.4.tgz",
"integrity": "sha512-Q9By3GPzANMZuf1i5i7Agyh6BUe6tTa+VCCZzsFzU32iXMcuDRXYHbNIKESrcjVXxiZScPB4u++WPw4LRyK1Gg==",
"dependencies": {
"is-stream": "^1.1.0",
"web-streams-polyfill": "^3.1.0"
}
},
"node_modules/@balena/odata-parser": {
"version": "3.1.2",
"resolved": "https://registry.npmjs.org/@balena/odata-parser/-/odata-parser-3.1.2.tgz",
@ -2596,9 +2588,9 @@
}
},
"node_modules/@inquirer/core/node_modules/@types/node": {
"version": "22.15.27",
"resolved": "https://registry.npmjs.org/@types/node/-/node-22.15.27.tgz",
"integrity": "sha512-5fF+eu5mwihV2BeVtX5vijhdaZOfkQTATrePEaXTcKqI16LhJ7gi2/Vhd9OZM0UojcdmiOCVg5rrax+i1MdoQQ==",
"version": "22.15.29",
"resolved": "https://registry.npmjs.org/@types/node/-/node-22.15.29.tgz",
"integrity": "sha512-LNdjOkUDlU1RZb8e1kOIUpN1qQUlzGkEtbVNo53vbrwDg5om6oduhm4SiUaPW5ASTXhAiP0jInWG8Qx9fVlOeQ==",
"dev": true,
"license": "MIT",
"dependencies": {
@ -6298,9 +6290,9 @@
}
},
"node_modules/@types/node": {
"version": "20.17.55",
"resolved": "https://registry.npmjs.org/@types/node/-/node-20.17.55.tgz",
"integrity": "sha512-ESpPDUEtW1a9nueMQtcTq/5iY/7osurPpBpFKH2VAyREKdzoFRRod6Oms0SSTfV7u52CcH7b6dFVnjfPD8fxWg==",
"version": "20.17.57",
"resolved": "https://registry.npmjs.org/@types/node/-/node-20.17.57.tgz",
"integrity": "sha512-f3T4y6VU4fVQDKVqJV4Uppy8c1p/sVvS3peyqxyWnzkqXFJLRU7Y1Bl7rMS1Qe9z0v4M6McY0Fp9yBsgHJUsWQ==",
"license": "MIT",
"dependencies": {
"undici-types": "~6.19.2"
@ -6391,9 +6383,10 @@
}
},
"node_modules/@types/semver": {
"version": "7.5.3",
"resolved": "https://registry.npmjs.org/@types/semver/-/semver-7.5.3.tgz",
"integrity": "sha512-OxepLK9EuNEIPxWNME+C6WwbRAOOI2o2BaQEGzz5Lu2e4Z5eDnEo+/aVEDMIXywoJitJ7xWd641wrGLZdtwRyw=="
"version": "7.7.0",
"resolved": "https://registry.npmjs.org/@types/semver/-/semver-7.7.0.tgz",
"integrity": "sha512-k107IF4+Xr7UHjwDc7Cfd6PRQfbdkiRabXGRjo07b4WyPahFBZCZ1sE+BNxYIJPPg73UkfOsVOLwqVc/6ETrIA==",
"license": "MIT"
},
"node_modules/@types/serve-static": {
"version": "1.13.10",
@ -7521,16 +7514,16 @@
}
},
"node_modules/balena-request": {
"version": "14.0.1",
"resolved": "https://registry.npmjs.org/balena-request/-/balena-request-14.0.1.tgz",
"integrity": "sha512-D6sQyeOZNoR736WVBD5F0AL/XsiGTioD9vaDpiqQJZvhShTDVzbNXkW7Sd+QCGrSP3gpdqorGN7hyEZsv5WZlA==",
"version": "14.1.5",
"resolved": "https://registry.npmjs.org/balena-request/-/balena-request-14.1.5.tgz",
"integrity": "sha512-pPcRMK5mnyRKqUBNSQHLqk32iXUQAtutyMDlpyFTp0FjS8kGT6dJNh7TFPN93kJpA3R5Vopfn4lFlt69nZGzFA==",
"license": "Apache-2.0",
"dependencies": {
"@balena/node-web-streams": "^0.2.3",
"balena-errors": "^4.9.0",
"fetch-ponyfill": "^7.1.0",
"fetch-readablestream": "^0.2.0",
"form-data-encoder": "^4.0.2",
"formdata-node": "^6.0.3",
"node-fetch": "^2.7.0",
"progress-stream": "^2.0.0",
"qs": "^6.9.4",
"tslib": "^2.0.0",
@ -7544,9 +7537,9 @@
}
},
"node_modules/balena-sdk": {
"version": "21.3.0",
"resolved": "https://registry.npmjs.org/balena-sdk/-/balena-sdk-21.3.0.tgz",
"integrity": "sha512-3Ub2doNsXwBPoRNr/feoMY18tXCUOTkclq6/Qe3lEiQA40/Q/pBfglveVXuOV6P2ljyC6jayJ8aR/3r5Kov/2w==",
"version": "21.4.7",
"resolved": "https://registry.npmjs.org/balena-sdk/-/balena-sdk-21.4.7.tgz",
"integrity": "sha512-MWR6s+VLjCMUDzud6nQSJCpCsxYecujzhZuxAvvvvGalBcMPOLeniEvbz8Ke5wh8HxsBGgL4F9eDQaFvtw7N4g==",
"license": "Apache-2.0",
"dependencies": {
"@balena/es-version": "^1.0.0",
@ -7557,8 +7550,8 @@
"balena-errors": "^4.9.0",
"balena-hup-action-utils": "~6.2.0",
"balena-register-device": "^9.0.4",
"balena-request": "^14.0.0",
"balena-semver": "^2.3.0",
"balena-request": "^14.0.8",
"balena-semver": "^3.0.0",
"balena-settings-client": "^5.0.0",
"date-fns": "^3.0.5",
"handlebars": "^4.7.7",
@ -7574,6 +7567,21 @@
"node": "^20.12.0 || >= 22.0.0"
}
},
"node_modules/balena-sdk/node_modules/balena-semver": {
"version": "3.0.6",
"resolved": "https://registry.npmjs.org/balena-semver/-/balena-semver-3.0.6.tgz",
"integrity": "sha512-UAoXJ5QrppbbaaWtVBRnQ6rvjuvXmrAV+Uy7aW7Jn6D4Dd0y7Ncz7XV6usWt2Dm0V/kq+5eGkGkNa4Xj/OjlhA==",
"license": "Apache-2.0",
"dependencies": {
"@types/lodash": "^4.14.200",
"@types/semver": "^7.5.4",
"lodash": "^4.17.21",
"semver": "^7.5.4"
},
"engines": {
"node": ">=16"
}
},
"node_modules/balena-sdk/node_modules/date-fns": {
"version": "3.6.0",
"resolved": "https://registry.npmjs.org/date-fns/-/date-fns-3.6.0.tgz",
@ -10876,14 +10884,6 @@
"node": "^12.20 || >= 14.13"
}
},
"node_modules/fetch-ponyfill": {
"version": "7.1.0",
"resolved": "https://registry.npmjs.org/fetch-ponyfill/-/fetch-ponyfill-7.1.0.tgz",
"integrity": "sha512-FhbbL55dj/qdVO3YNK7ZEkshvj3eQ7EuIGV2I6ic/2YiocvyWv+7jg2s4AyS0wdRU75s3tA8ZxI/xPigb0v5Aw==",
"dependencies": {
"node-fetch": "~2.6.1"
}
},
"node_modules/fetch-readablestream": {
"version": "0.2.0",
"resolved": "https://registry.npmjs.org/fetch-readablestream/-/fetch-readablestream-0.2.0.tgz",
@ -14843,15 +14843,14 @@
}
},
"node_modules/nock": {
"version": "14.0.4",
"resolved": "https://registry.npmjs.org/nock/-/nock-14.0.4.tgz",
"integrity": "sha512-86fh+gIKH8H02+y0/HKAOZZXn6OwgzXvl6JYwfjvKkoKxUWz54wIIDU/+w24xzMvk/R8pNVXOrvTubyl+Ml6cg==",
"version": "15.0.0-beta.3",
"resolved": "https://registry.npmjs.org/nock/-/nock-15.0.0-beta.3.tgz",
"integrity": "sha512-4q0Cr/pU11G5f3aM/V7w4u1PEeimng6MhjqGdaUEyMYp32ZbCvTuiLQknoSS9iWTbKvTCpr0Q/jN8NZpHbXfdQ==",
"dev": true,
"license": "MIT",
"dependencies": {
"@mswjs/interceptors": "^0.38.5",
"json-stringify-safe": "^5.0.1",
"propagate": "^2.0.0"
"@mswjs/interceptors": "^0.38.7",
"json-stringify-safe": "^5.0.1"
},
"engines": {
"node": ">=18.20.0 <20 || >=20.12.1"
@ -14903,9 +14902,10 @@
}
},
"node_modules/node-fetch": {
"version": "2.6.13",
"resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.13.tgz",
"integrity": "sha512-StxNAxh15zr77QvvkmveSQ8uCQ4+v5FkvNTj0OESmiHu+VRi/gXArXtkWMElOsOUNLtUEvI4yS+rdtOHZTwlQA==",
"version": "2.7.0",
"resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.7.0.tgz",
"integrity": "sha512-c4FRfUm/dbcWZ7U+1Wq0AwCyFL+3nt2bEw05wfxSz+DWpWsitgmSgYmy2dQdWyKC1694ELPqMs/YzUSNozLt8A==",
"license": "MIT",
"dependencies": {
"whatwg-url": "^5.0.0"
},
@ -16440,15 +16440,6 @@
"react-is": "^16.13.1"
}
},
"node_modules/propagate": {
"version": "2.0.1",
"resolved": "https://registry.npmjs.org/propagate/-/propagate-2.0.1.tgz",
"integrity": "sha512-vGrhOavPSTz4QVNuBNdcNXePNdNMaO1xj9yBeH1ScQPjk/rhg9sSlCXPhMkFuaNNW/syTvYqsnbIJxMBfRbbag==",
"dev": true,
"engines": {
"node": ">= 8"
}
},
"node_modules/proto-list": {
"version": "1.2.4",
"resolved": "https://registry.npmjs.org/proto-list/-/proto-list-1.2.4.tgz",
@ -19163,6 +19154,16 @@
"resolved": "git+ssh://git@github.com/balena-io-modules/unbzip2-stream.git#4a54f56a25b58950f9e4277c56db2912d62242e7",
"integrity": "sha512-hqSQ+EGPNNs80IGpZszUngM7ttKBCgjRtrw+2PTRDUd1UNvPOoSL4M7V1bDBJXktGv8KaVJFRKk2mWi6RmbiEg=="
},
"node_modules/undici": {
"version": "7.10.0",
"resolved": "https://registry.npmjs.org/undici/-/undici-7.10.0.tgz",
"integrity": "sha512-u5otvFBOBZvmdjWLVW+5DAc9Nkq8f24g0O9oY7qw2JVIF1VocIFoyz9JFkuVOS2j41AufeO0xnlweJ2RLT8nGw==",
"dev": true,
"license": "MIT",
"engines": {
"node": ">=20.18.1"
}
},
"node_modules/undici-types": {
"version": "6.19.8",
"resolved": "https://registry.npmjs.org/undici-types/-/undici-types-6.19.8.tgz",
@ -19435,6 +19436,7 @@
"version": "3.3.3",
"resolved": "https://registry.npmjs.org/web-streams-polyfill/-/web-streams-polyfill-3.3.3.tgz",
"integrity": "sha512-d2JWLCivmZYTSIoge9MsgFCZrt571BikcWGYkjC1khllbTeDlGqZ2D8vD8E/lJa8WGWbb7Plm8/XJYV7IJHZZw==",
"dev": true,
"engines": {
"node": ">= 8"
}

View File

@ -153,14 +153,15 @@
"mocha": "^10.6.0",
"mock-fs": "^5.2.0",
"mock-require": "^3.0.3",
"nock": "^14.0.4",
"nock": "^15.0.0-beta.3",
"oclif": "^4.17.0",
"rewire": "^7.0.0",
"simple-git": "^3.14.1",
"sinon": "^19.0.0",
"string-to-stream": "^3.0.1",
"ts-node": "^10.4.0",
"typescript": "^5.8.2"
"typescript": "^5.8.2",
"undici": "^7.10.0"
},
"dependencies": {
"@balena/compose": "^7.0.9",
@ -174,7 +175,7 @@
"balena-errors": "^4.7.3",
"balena-image-fs": "^7.5.2",
"balena-preload": "^18.0.4",
"balena-sdk": "^21.3.0",
"balena-sdk": "^21.4.7",
"balena-semver": "^2.3.0",
"balena-settings-client": "^5.0.2",
"balena-settings-storage": "^8.1.0",

View File

@ -108,7 +108,7 @@ describe('Utils:', async function () {
});
});
return describe('given the token does authenticate with the server', function () {
describe('given the token does authenticate with the server', function () {
beforeEach(function () {
this.balenaAuthIsLoggedInStub = sinon.stub(balena.auth, 'isLoggedIn');
return this.balenaAuthIsLoggedInStub.resolves(true);
@ -118,7 +118,7 @@ describe('Utils:', async function () {
return this.balenaAuthIsLoggedInStub.restore();
});
return it('should eventually be true', function () {
it('should eventually be true', function () {
const promise = utils.loginIfTokenValid(tokens.johndoe.token);
return expect(promise).to.eventually.be.true;
});

View File

@ -20,7 +20,6 @@ import type { Request as ReleaseRequest } from '@balena/compose/dist/release';
import { expect } from 'chai';
import { promises as fs } from 'fs';
import * as _ from 'lodash';
import type * as nock from 'nock';
import * as path from 'path';
import * as sinon from 'sinon';
@ -181,8 +180,8 @@ describe('balena deploy', function () {
];
api.expectPostRelease({
inspectRequest: (_uri: string, requestBody: nock.Body) => {
const body = requestBody.valueOf() as Partial<ReleaseRequest>;
inspectRequest: async (request: Request) => {
const body = (await request.json()) as Partial<ReleaseRequest>;
expect(body.contract).to.deep.equal({
name: 'testContract',
type: 'sw.application',
@ -232,8 +231,8 @@ describe('balena deploy', function () {
];
api.expectPostRelease({
inspectRequest: (_uri: string, requestBody: nock.Body) => {
const body = requestBody.valueOf() as Partial<ReleaseRequest>;
inspectRequest: async (request: Request) => {
const body = (await request.json()) as Partial<ReleaseRequest>;
expect(body.contract).to.deep.equal({
name: 'testContract',
type: 'sw.application',
@ -298,21 +297,21 @@ describe('balena deploy', function () {
statusCode: 500,
// b/c failed requests are retried
times: maxRequestRetries,
inspectRequest: (_uri, requestBody) => {
const imageBody = requestBody as Partial<
inspectRequest: async (request: Request) => {
const body = (await request.json()) as Partial<
import('@balena/compose/dist/release/models').ImageModel
>;
expect(imageBody.status).to.equal('success');
expect(body.status).to.equal('success');
failedImagePatchRequests++;
},
});
// Check that the CLI patches the release with status="failed"
api.expectPatchRelease({
inspectRequest: (_uri, requestBody) => {
const releaseBody = requestBody as Partial<
inspectRequest: async (request: Request) => {
const body = (await request.json()) as Partial<
import('@balena/compose/dist/release/models').ReleaseModel
>;
expect(releaseBody.status).to.equal('failed');
expect(body.status).to.equal('failed');
},
});
api.expectPostImageLabel();
@ -386,8 +385,8 @@ describe('balena deploy', function () {
let succesfullImagePatchRequests = 0;
api
.optPatch(/^\/v7\/image($|[(?])/, { times: maxRequestRetries })
.reply((_uri, requestBody) => {
const imageBody = requestBody as Partial<
.reply(async (request) => {
const imageBody = (await request.json()) as Partial<
import('@balena/compose/dist/release/models').ImageModel
>;
expect(imageBody.status).to.equal('success');

View File

@ -61,12 +61,15 @@ export class BalenaAPIMock extends NockMock {
}
public expectDownloadConfig(opts: ScopeOpts = {}) {
this.optPost('/download-config', opts).reply(200, (_uri, body) => {
let deviceType = 'raspberrypi3';
if (typeof body === 'object' && 'deviceType' in body) {
deviceType = body.deviceType;
}
return JSON.parse(`{
this.optPost('/download-config', opts).reply(
200,
async (request: Request) => {
const body = await request.json();
let deviceType = 'raspberrypi3';
if (typeof body === 'object' && 'deviceType' in body) {
deviceType = body.deviceType;
}
return JSON.parse(`{
"applicationId":1301645,
"deviceType":"${deviceType}",
"userId":43699,
@ -79,7 +82,8 @@ export class BalenaAPIMock extends NockMock {
"deltaEndpoint":"https://delta.balena-cloud.com",
"apiKey":"nothingtoseehere"
}`);
});
},
);
}
public expectApplicationProvisioning(opts: ScopeOpts = {}) {
@ -223,7 +227,7 @@ export class BalenaAPIMock extends NockMock {
this.optGet(
providedUuid.length !== 32
? /^\/v\d+\/device($|\?)/
: /^\/v\d+\/device\(uuid=%27[0-9a-f]{32}%27\)/,
: /^\/v\d+\/device\(uuid='[0-9a-f]{32}'\)/,
opts,
).reply(200, {
d: [
@ -284,8 +288,8 @@ export class BalenaAPIMock extends NockMock {
public expectGetAppServiceVars(opts: ScopeOpts = {}) {
this.optGet(/^\/v\d+\/service_environment_variable($|\?)/, opts).reply(
function (uri, _requestBody) {
const match = uri.match(/service_name%20eq%20%27(.+?)%27/);
function (request) {
const match = request.url.match(/service_name%20eq%20%27(.+?)%27/);
const serviceName = match?.[1] || undefined;
let varArray: any[];
if (serviceName) {
@ -332,8 +336,8 @@ export class BalenaAPIMock extends NockMock {
this.optGet(
/^\/v\d+\/device_service_environment_variable($|\?)/,
opts,
).reply(function (uri, _requestBody) {
const match = uri.match(/service_name%20eq%20%27(.+?)%27/);
).reply(function (request) {
const match = request.url.match(/service_name%20eq%20%27(.+?)%27/);
const serviceName = match?.[1] || undefined;
let varArray: any[];
if (serviceName) {

View File

@ -16,12 +16,7 @@
*/
import * as path from 'path';
import * as zlib from 'zlib';
import { NockMock } from './nock-mock';
import { promisify } from 'util';
const gunzipAsync = promisify(zlib.gunzip);
export const builderResponsePath = path.normalize(
path.join(__dirname, '..', 'test-data', 'builder-response'),
@ -40,25 +35,17 @@ export class BuilderMock extends NockMock {
checkURI: (uri: string) => Promise<void> | void;
checkBuildRequestBody: (requestBody: string | Buffer) => Promise<void>;
}) {
this.optPost(/^\/v3\/build($|[(?])/, opts).reply(
async function (uri, requestBody, callback) {
let error: Error | null = null;
try {
await opts.checkURI(uri);
if (typeof requestBody === 'string') {
const gzipped = Buffer.from(requestBody, 'hex');
const gunzipped = await gunzipAsync(gzipped);
await opts.checkBuildRequestBody(gunzipped);
} else {
throw new Error(
`unexpected requestBody type "${typeof requestBody}"`,
);
}
} catch (err) {
error = err;
}
callback(error, [opts.responseCode, opts.responseBody]);
},
);
this.optPost(/^\/v3\/build($|[(?])/, opts).reply(async function (
request: Request,
) {
await opts.checkURI(request.url);
const requestBody = await request.text();
if (typeof requestBody === 'string') {
await opts.checkBuildRequestBody(requestBody);
} else {
throw new Error(`unexpected requestBody type "${typeof requestBody}"`);
}
return [opts.responseCode, opts.responseBody];
});
}
}

View File

@ -81,21 +81,15 @@ export class DockerMock extends NockMock {
this.optPost(
new RegExp(`^/build\\?(|.+&)${qs.stringify({ t: opts.tag })}&`),
opts,
).reply(async function (uri, requestBody, cb) {
let error: Error | null = null;
try {
await opts.checkURI(uri);
if (typeof requestBody === 'string') {
await opts.checkBuildRequestBody(requestBody);
} else {
throw new Error(
`unexpected requestBody type "${typeof requestBody}"`,
);
}
} catch (err) {
error = err;
).reply(async function (request) {
await opts.checkURI(request.url);
const requestBody = await request.text();
if (typeof requestBody === 'string') {
await opts.checkBuildRequestBody(requestBody);
} else {
throw new Error(`unexpected requestBody type "${typeof requestBody}"`);
}
cb(error, [opts.responseCode, opts.responseBody]);
return [opts.responseCode, opts.responseBody];
});
}

View File

@ -100,47 +100,28 @@ export class NockMock {
return this.optMethod('post', uri, opts);
}
protected inspectNoOp(_uri: string, _requestBody: nock.Body): void {
return undefined;
protected inspectNoOp(_request: Request): void | Promise<void> {
return;
}
protected getInspectedReplyBodyFunction(
inspectRequest: (uri: string, requestBody: nock.Body) => void,
inspectRequest: (request: Request) => void | Promise<void>,
replyBody: nock.ReplyBody,
) {
return function (
this: nock.ReplyFnContext,
uri: string,
requestBody: nock.Body,
cb: (err: NodeJS.ErrnoException | null, result: nock.ReplyBody) => void,
) {
try {
inspectRequest(uri, requestBody);
} catch (err) {
cb(err, '');
}
cb(null, replyBody);
return async function (request: Request): Promise<nock.ReplyBody> {
await inspectRequest(request);
return replyBody;
};
}
protected getInspectedReplyFileFunction(
inspectRequest: (uri: string, requestBody: nock.Body) => void,
inspectRequest: (request: Request) => void | Promise<void>,
replyBodyFile: string,
) {
return function (
this: nock.ReplyFnContext,
uri: string,
requestBody: nock.Body,
cb: (err: NodeJS.ErrnoException | null, result: nock.ReplyBody) => void,
) {
try {
inspectRequest(uri, requestBody);
} catch (err) {
cb(err, '');
}
return async function (request: Request): Promise<nock.ReplyBody> {
await inspectRequest(request);
const replyBody = fs.readFileSync(replyBodyFile);
cb(null, replyBody);
return replyBody;
};
}

View File

@ -48,8 +48,8 @@ export class SupervisorMock extends NockMock {
}, 10);
},
});
this.optGet('/v2/local/logs', opts).reply((_uri, _reqBody, cb) => {
cb(null, [200, chunkedStream]);
this.optGet('/v2/local/logs', opts).reply(() => {
return [200, chunkedStream];
});
}
}