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

feat: enable allowH2 by default and require Node.js >= 18.20.0 #734

Merged
merged 7 commits into from
Nov 30, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
74 changes: 73 additions & 1 deletion app.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import path from 'path';
import { readFile } from 'fs/promises';
import { Application } from 'egg';
import { Application, Context } from 'egg';
import {
HttpClient as RawHttpClient,
RequestURL as HttpClientRequestURL,
RequestOptions,
} from 'urllib';
import ms from 'ms';
import { ChangesStreamService } from './app/core/service/ChangesStreamService';

declare module 'egg' {
Expand All @@ -9,12 +15,78 @@
}
}

interface HttpClientRequestOptions extends RequestOptions {
ctx?: Context;
tracer?: unknown;
}

const SSRF_HTTPCLIENT = Symbol('SSRF_HTTPCLIENT');

class HttpClient extends RawHttpClient {
readonly #app: Application & { tracer?: unknown };

constructor(app: Application, options?: any) {
normalizeConfig(app);
options = {
...app.config.httpclient,
...options,
};
super({
app,
defaultArgs: options.request,
allowH2: options.allowH2,
// use on egg-security ssrf
// https://github.com/eggjs/egg-security/blob/master/lib/extend/safe_curl.js#L11
checkAddress: options.checkAddress,
} as any);
this.#app = app;
}

async request<T = any>(url: HttpClientRequestURL, options?: HttpClientRequestOptions) {
options = options ?? {};
if (options.ctx?.tracer) {
options.tracer = options.ctx.tracer;
} else {
options.tracer = options.tracer ?? this.#app.tracer;
}
return await super.request<T>(url, options);
}

async curl<T = any>(url: HttpClientRequestURL, options?: HttpClientRequestOptions) {
return await this.request<T>(url, options);
}

Check warning on line 57 in app.ts

View check run for this annotation

Codecov / codecov/patch

app.ts#L56-L57

Added lines #L56 - L57 were not covered by tests

async safeCurl<T = any>(url: HttpClientRequestURL, options: any = {}) {
if (!this[SSRF_HTTPCLIENT]) {
const ssrfConfig = this.#app.config.security.ssrf;
if (ssrfConfig?.checkAddress) {
options.checkAddress = ssrfConfig.checkAddress;
} else {
this.#app.logger.warn('[egg-security] please configure `config.security.ssrf` first');
}
this[SSRF_HTTPCLIENT] = new HttpClient(this.#app, {
checkAddress: ssrfConfig.checkAddress,
});
}
return await this[SSRF_HTTPCLIENT].request<T>(url, options);
}

Check warning on line 72 in app.ts

View check run for this annotation

Codecov / codecov/patch

app.ts#L60-L72

Added lines #L60 - L72 were not covered by tests
}

function normalizeConfig(app: Application) {
const config = app.config.httpclient;
if (typeof config.request?.timeout === 'string') {
config.request.timeout = ms(config.request.timeout as string);
}

Check warning on line 79 in app.ts

View check run for this annotation

Codecov / codecov/patch

app.ts#L78-L79

Added lines #L78 - L79 were not covered by tests
}

export default class CnpmcoreAppHook {
private readonly app: Application;

constructor(app: Application) {
this.app = app;
this.app.binaryHTML = '';
Reflect.set(app, 'HttpClient', HttpClient);
Reflect.set(app, 'HttpClientNext', HttpClient);
}

async configWillLoad() {
Expand Down
2 changes: 1 addition & 1 deletion app/common/adapter/binary/AbstractBinary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export abstract class AbstractBinary {
for (const version of versions) {
if (!version.modules) continue;
const modulesVersion = parseInt(version.modules);
// node v6.0.0 moduels 48 min
// node v6.0.0 modules 48 min
if (modulesVersion >= 48 && !nodeABIVersions.includes(modulesVersion)) {
nodeABIVersions.push(modulesVersion);
}
Expand Down
2 changes: 1 addition & 1 deletion app/common/adapter/changesStream/NpmChangesStream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export class NpmChangesStream extends AbstractChangeStream {
const db = this.getChangesStreamUrl(registry, since);
const { res } = await this.httpclient.request(db, {
streaming: true,
timeout: 10000,
timeout: 60000,
});

let buf = '';
Expand Down
4 changes: 3 additions & 1 deletion app/core/service/ChangesStreamService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,9 @@
this.logger.warn('[ChangesStreamService.executeTask:error] %s, exit now', err.message);
if (err.name === 'HttpClientRequestTimeoutError'
|| err.name === 'ConnectTimeoutError'
|| err.name === 'BodyTimeoutError') {
|| err.name === 'BodyTimeoutError'
|| err.message.includes('timeout')) {
// InformationalError: HTTP/2: "stream timeout after 60000"

Check warning on line 109 in app/core/service/ChangesStreamService.ts

View check run for this annotation

Codecov / codecov/patch

app/core/service/ChangesStreamService.ts#L109

Added line #L109 was not covered by tests
fengmk2 marked this conversation as resolved.
Show resolved Hide resolved
this.logger.warn(err);
} else {
this.logger.error(err);
Expand Down
1 change: 1 addition & 0 deletions config/config.default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ export default (appInfo: EggAppConfig) => {

config.httpclient = {
useHttpClientNext: true,
allowH2: true,
};

config.view = {
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@
"leoric": "^2.12.3",
"lodash": "^4.17.21",
"mime-types": "^2.1.35",
"ms": "^2.1.3",
"mysql2": "^3.9.4",
"node-rsa": "^1.1.1",
"npm-package-arg": "^10.1.0",
Expand All @@ -112,6 +113,7 @@
"ssri": "^8.0.1",
"type-fest": "^2.5.3",
"ua-parser-js": "^1.0.34",
"urllib": "4.5.0-beta.3",
"validate-npm-package-name": "^3.0.0"
},
"optionalDependencies": {
Expand Down
18 changes: 16 additions & 2 deletions test/common/adapter/changesStream/NpmChangesStream.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ describe('test/common/adapter/changesStream/NpmChangesStream.test.ts', () => {
};
});
const res: ChangesStreamChange[] = [];
const stream = await npmChangesStream.fetchChanges(registry, '9517');
const stream = npmChangesStream.fetchChanges(registry, '9517');
for await (const change of stream) {
res.push(change);
}
Expand All @@ -74,7 +74,7 @@ describe('test/common/adapter/changesStream/NpmChangesStream.test.ts', () => {
};
});
const res: ChangesStreamChange[] = [];
const stream = await npmChangesStream.fetchChanges(registry, '9517');
const stream = npmChangesStream.fetchChanges(registry, '9517');
assert(stream);
rStream.push('{"seq":2');
rStream.push(',"id":"bac');
Expand All @@ -84,5 +84,19 @@ describe('test/common/adapter/changesStream/NpmChangesStream.test.ts', () => {
}
assert(res.length === 1);
});

it.skip('should read changes work', async () => {
for (let i = 0; i < 10000; i++) {
const stream = npmChangesStream.fetchChanges(registry, '36904024');
assert(stream);
try {
for await (const change of stream) {
console.log(change);
}
} catch (err) {
console.error(err);
}
}
});
fengmk2 marked this conversation as resolved.
Show resolved Hide resolved
});
});
Loading