Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[sitecore-jss-dev-tools] Reuse Node.js ClientRequest for package deploy process #2013

Merged
merged 9 commits into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Our versioning strategy is as follows:

### 🛠 Breaking Change

* `[all packages]` `[all samples]` Remove Axios ([#2006](https://github.com/Sitecore/jss/pull/2006)) ([#2008](https://github.com/Sitecore/jss/pull/2008)) ([#2011](https://github.com/Sitecore/jss/pull/2011))
* `[all packages]` `[all samples]` Remove Axios ([#2006](https://github.com/Sitecore/jss/pull/2006)) ([#2008](https://github.com/Sitecore/jss/pull/2008)) ([#2011](https://github.com/Sitecore/jss/pull/2011))([#2013](https://github.com/Sitecore/jss/pull/2013))
* `AxiosDataFetcher` is replaced by the `NativeDataFetcher`.
* `AxiosDataFetcherConfig` is replaced by `NativeDataFetcherConfig`.
* `AxiosResponse` is replaced by `NativeDataFetcherResponse`.
Expand Down
1 change: 0 additions & 1 deletion packages/sitecore-jss-dev-tools/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
"recast": "^0.23.5",
"resolve": "^1.22.1",
"ts-node": "^10.9.1",
"undici": "^7.2.1",
"url-join": "^4.0.1",
"uuid": "^9.0.0",
"yargs": "^17.6.2"
Expand Down
167 changes: 103 additions & 64 deletions packages/sitecore-jss-dev-tools/src/package-deploy.test.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,58 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { expect } from 'chai';
import chalk from 'chalk';
import { spy } from 'sinon';
import { SinonSpy, spy } from 'sinon';
import { ClientRequest } from 'http';
import nock from 'nock';
import {
extractProxy,
getHttpsTransport,
doFingerprintsMatch,
normalizeFingerprint,
applyCertPinning,
finishWatchJobStatusTask,
logJobStatus,
setProxy,
attachFormDataHandlers,
} from './package-deploy';
import { Socket } from 'net';
import FormData from 'form-data';
import { RequestOptions } from 'https';

describe('package-deploy', () => {
beforeEach(() => {
nock.cleanAll();
});

describe('attachFormDataHandlers', () => {
it('should attach event handlers', () => {
const req = ({
destroy: spy(),
} as unknown) as ClientRequest;

const form = ({
events: {},
on: spy((event: string, _cb: () => void) => {
form.events[event] = spy();
}),
once: spy((event: string, _cb: () => void) => {
form.events[event] = spy();
}),
emit(event: string) {
form.events[event]();
},
} as unknown) as FormData & { events: { [key: string]: SinonSpy } };

attachFormDataHandlers(req, form);

form.emit('error');
form.emit('end');
form.emit('close');

expect(form.events.error.called).to.equal(true);
expect(form.events.end.called).to.equal(true);
expect(form.events.close.called).to.equal(true);
});
});

describe('finishWatchJobStatusTask', () => {
it('with warnings', (done) => {
const consoleWarnSpy = spy(console, 'warn');
Expand Down Expand Up @@ -138,31 +171,6 @@ describe('package-deploy', () => {
});
});

describe('extractProxy', () => {
addy-pathania marked this conversation as resolved.
Show resolved Hide resolved
it('should return proxy object', () => {
expect(extractProxy('https://localhost:9999')).to.deep.equal({
protocol: 'https',
port: 9999,
host: 'localhost',
});

expect(extractProxy('http://myhostname:1234')).to.deep.equal({
protocol: 'http',
port: 1234,
host: 'myhostname',
});
});

it('should return undefined if proxy not provided', () => {
expect(extractProxy()).to.equal(undefined);
});

it('should return undefined if proxy is not valid url', () => {
process.exit = () => [] as never;
expect(extractProxy('test')).to.equal(undefined);
});
});

describe('applyCertPinning', () => {
it('should skip certs comparison', (done) => {
const req = ({
Expand Down Expand Up @@ -279,42 +287,6 @@ describe('package-deploy', () => {
});
});

describe('getHttpsTransport', () => {
it('should execute request', (done) => {
nock('https://superhost')
.get('/test')
.reply(200, {
success: true,
text: 'test',
});

const transport = getHttpsTransport({
packagePath: 'xxx',
appName: 'jssapp',
importServiceUrl: 'xxx',
secret: 'yyy',
});

const req = transport.request(
{ method: 'GET', hostname: 'superhost', path: '/test' },
(response) => {
let result = '';

response.on('data', (data) => {
result += data;
});

response.on('end', () => {
expect(result).to.equal('{"success":true,"text":"test"}');
done();
});
}
);

req.end();
});
});

describe('doFingerprintsMatch', () => {
it('should match', () => {
const fp = '5E:D1:5E:D4:D4:42:71:CC:30:A5:B6:A2:DA:A4:79:06:67:CB:F6:36';
Expand All @@ -330,6 +302,73 @@ describe('package-deploy', () => {
});
});

describe('setProxy', () => {
const reqOptions: RequestOptions = {};
it('should set hostname, port, protocol, and path for a valid HTTP proxy', () => {
const proxy = 'http://proxy.example.com:8080';
const targetUrl = 'https://targetsite.com/resource';

setProxy(reqOptions, proxy, targetUrl);

expect(reqOptions).to.deep.include({
hostname: 'proxy.example.com',
port: '8080',
protocol: 'http:',
path: targetUrl,
});
});

it('should set default port 443 for HTTPS proxy when port is not provided', () => {
const proxy = 'https://proxy.example.com';
const targetUrl = 'https://targetsite.com/resource';

setProxy(reqOptions, proxy, targetUrl);

expect(reqOptions).to.deep.include({
hostname: 'proxy.example.com',
port: '443',
protocol: 'https:',
path: targetUrl,
});
});

it('should set default port 80 for HTTP proxy when port is not provided', () => {
const proxy = 'http://proxy.example.com';
const targetUrl = 'http://targetsite.com/resource';

setProxy(reqOptions, proxy, targetUrl);

expect(reqOptions).to.deep.include({
hostname: 'proxy.example.com',
port: '80',
protocol: 'http:',
path: targetUrl,
});
});

it('should handle invalid proxy URL gracefully', () => {
const proxy = 'invalid-proxy-url';
const targetUrl = 'https://targetsite.com/resource';

let exitCode: number | undefined;
const originalExit = process.exit;

process.exit = ((code?: number) => {
exitCode = code;
throw new Error('process.exit called');
}) as typeof process.exit;

try {
setProxy(reqOptions, proxy, targetUrl);
} catch (error) {
expect((error as Error).message).to.equal('process.exit called');
expect(exitCode).to.equal(1);
} finally {
process.exit = originalExit;
}
});
});

it('normalizeFingerprint', () => {
expect(
normalizeFingerprint('5E:D1:5E:D4:D4:42:71:CC:30:A5:B6:A2:DA:A4:79:06:67:CB:F6:36')
Expand Down
Loading