Skip to content

Commit

Permalink
Merge branch 'master' into udpate-index-json
Browse files Browse the repository at this point in the history
  • Loading branch information
elrrrrrrr authored Jan 28, 2023
2 parents 994cc43 + 68edfb5 commit d87f421
Show file tree
Hide file tree
Showing 30 changed files with 643 additions and 565 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changelog

## [3.1.2](https://github.com/cnpm/npmcore/compare/v3.1.1...v3.1.2) (2023-01-28)


### Bug Fixes

* binary path ([#381](https://github.com/cnpm/npmcore/issues/381)) ([790621b](https://github.com/cnpm/npmcore/commit/790621b4b941f06ac075423c139c365bd440fb9e))

## [3.1.1](https://github.com/cnpm/npmcore/compare/v3.1.0...v3.1.1) (2023-01-18)


Expand Down
27 changes: 14 additions & 13 deletions app/common/adapter/binary/AbstractBinary.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { EggContextHttpClient, EggLogger } from 'egg';
import { ImplDecorator, Inject, QualifierImplDecoratorUtil } from '@eggjs/tegg';
import { BinaryType } from 'app/common/enum/Binary';
import { EggHttpClient, EggLogger } from 'egg';
import { BinaryTaskConfig } from '../../../../config/binaries';

export type BinaryItem = {
Expand All @@ -15,20 +17,16 @@ export type FetchResult = {
nextParams?: any;
};

export const BINARY_ADAPTER_ATTRIBUTE = 'BINARY_ADAPTER_ATTRIBUTE';

export abstract class AbstractBinary {
protected httpclient: EggContextHttpClient;
@Inject()
protected logger: EggLogger;
protected binaryConfig: BinaryTaskConfig;
protected binaryName: string;

constructor(httpclient: EggContextHttpClient, logger: EggLogger, binaryConfig: BinaryTaskConfig, binaryName: string) {
this.httpclient = httpclient;
this.logger = logger;
this.binaryConfig = binaryConfig;
this.binaryName = binaryName;
}
@Inject()
protected httpclient: EggHttpClient;

abstract fetch(dir: string, params?: any): Promise<FetchResult | undefined>;
abstract fetch(dir: string, binaryName?: string): Promise<FetchResult | undefined>;

protected async requestXml(url: string) {
const { status, data, headers } = await this.httpclient.request(url, {
Expand Down Expand Up @@ -78,8 +76,8 @@ export abstract class AbstractBinary {
return [ 'darwin', 'linux', 'win32' ];
}

protected listNodeArchs() {
if (this.binaryConfig.options?.nodeArchs) return this.binaryConfig.options.nodeArchs;
protected listNodeArchs(binaryConfig?: BinaryTaskConfig) {
if (binaryConfig?.options?.nodeArchs) return binaryConfig.options.nodeArchs;
// https://nodejs.org/api/os.html#osarch
return {
linux: [ 'arm', 'arm64', 's390x', 'ia32', 'x64' ],
Expand All @@ -97,3 +95,6 @@ export abstract class AbstractBinary {
};
}
}

export const BinaryAdapter: ImplDecorator<AbstractBinary, typeof BinaryType> =
QualifierImplDecoratorUtil.generatorDecorator(AbstractBinary, BINARY_ADAPTER_ATTRIBUTE);
22 changes: 12 additions & 10 deletions app/common/adapter/binary/ApiBinary.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import { EggContextHttpClient, EggLogger } from 'egg';
import { AbstractBinary, FetchResult, BinaryItem } from './AbstractBinary';
import { BinaryTaskConfig } from '../../../../config/binaries';
import { AbstractBinary, FetchResult, BinaryItem, BinaryAdapter } from './AbstractBinary';
import { Inject, SingletonProto } from '@eggjs/tegg';
import { BinaryType } from 'app/common/enum/Binary';
import { EggAppConfig } from 'egg';

@SingletonProto()
@BinaryAdapter(BinaryType.Api)
export class ApiBinary extends AbstractBinary {
private apiUrl: string;
constructor(httpclient: EggContextHttpClient, logger: EggLogger, binaryConfig: BinaryTaskConfig, apiUrl: string, binaryName: string) {
super(httpclient, logger, binaryConfig, binaryName);
this.apiUrl = apiUrl;
}

async fetch(dir: string): Promise<FetchResult | undefined> {
const url = `${this.apiUrl}/${this.binaryName}${dir}`;
@Inject()
private readonly config: EggAppConfig;

async fetch(dir: string, binaryName?: string): Promise<FetchResult | undefined> {
const apiUrl = this.config.syncBinaryFromAPISource || `${this.config.sourceRegistry}/-/binary`;
const url = `${apiUrl}/${binaryName}${dir}`;
const data = await this.requestJSON(url);
if (!Array.isArray(data)) {
this.logger.warn('[ApiBinary.fetch:response-data-not-array] data: %j', data);
Expand Down
20 changes: 13 additions & 7 deletions app/common/adapter/binary/BucketBinary.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
import { SingletonProto } from '@eggjs/tegg';
import { BinaryType } from 'app/common/enum/Binary';
import binaries, { BinaryTaskConfig } from 'config/binaries';
import path from 'path';
import { AbstractBinary, FetchResult, BinaryItem } from './AbstractBinary';
import { AbstractBinary, FetchResult, BinaryItem, BinaryAdapter } from './AbstractBinary';

@SingletonProto()
@BinaryAdapter(BinaryType.Bucket)
export class BucketBinary extends AbstractBinary {
async fetch(dir: string): Promise<FetchResult | undefined> {
async fetch(dir: string, binaryName: string): Promise<FetchResult | undefined> {
// /foo/ => foo/
const binaryConfig = binaries[binaryName];
const subDir = dir.substring(1);
const url = `${this.binaryConfig.distUrl}?delimiter=/&prefix=${encodeURIComponent(subDir)}`;
const url = `${binaryConfig.distUrl}?delimiter=/&prefix=${encodeURIComponent(subDir)}`;
const xml = await this.requestXml(url);
return { items: this.parseItems(xml, dir), nextParams: null };
return { items: this.parseItems(xml, dir, binaryConfig), nextParams: null };
}

protected parseItems(xml: string, dir: string) {
protected parseItems(xml: string, dir: string, binaryConfig: BinaryTaskConfig): BinaryItem[] {
const items: BinaryItem[] = [];
// https://nwjs2.s3.amazonaws.com/?prefix=v0.59.0%2Fx64%2F
// https://chromedriver.storage.googleapis.com/?delimiter=/&prefix=
Expand All @@ -35,7 +41,7 @@ export class BucketBinary extends AbstractBinary {
items.push({
name,
isDir: false,
url: `${this.binaryConfig.distUrl}${fullname}`,
url: `${binaryConfig.distUrl}${fullname}`,
size,
date,
});
Expand All @@ -50,7 +56,7 @@ export class BucketBinary extends AbstractBinary {
const fullname = m[1].trim();
const name = `${path.basename(fullname)}/`;
const fullpath = `${dir}${name}`;
if (this.binaryConfig.ignoreDirs?.includes(fullpath)) continue;
if (binaryConfig.ignoreDirs?.includes(fullpath)) continue;
let date = '-';
// root dir children, should set date to '2022-04-19T01:00:00Z', sync per hour
if (dir === '/') {
Expand Down
6 changes: 5 additions & 1 deletion app/common/adapter/binary/CypressBinary.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { AbstractBinary, FetchResult, BinaryItem } from './AbstractBinary';
import { SingletonProto } from '@eggjs/tegg';
import { BinaryType } from 'app/common/enum/Binary';
import { AbstractBinary, FetchResult, BinaryItem, BinaryAdapter } from './AbstractBinary';

@SingletonProto()
@BinaryAdapter(BinaryType.Cypress)
export class CypressBinary extends AbstractBinary {
private dirItems: {
[key: string]: BinaryItem[];
Expand Down
11 changes: 8 additions & 3 deletions app/common/adapter/binary/ElectronBinary.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import { BinaryItem, FetchResult } from './AbstractBinary';
import { SingletonProto } from '@eggjs/tegg';
import { BinaryType } from 'app/common/enum/Binary';
import binaries from 'config/binaries';
import { BinaryAdapter, BinaryItem, FetchResult } from './AbstractBinary';
import { GithubBinary } from './GithubBinary';

@SingletonProto()
@BinaryAdapter(BinaryType.Electron)
export class ElectronBinary extends GithubBinary {
async fetch(dir: string): Promise<FetchResult | undefined> {
const releases = await this.initReleases();
const releases = await this.initReleases(binaries.electron);
if (!releases) return;

let items: BinaryItem[] = [];
Expand All @@ -30,7 +35,7 @@ export class ElectronBinary extends GithubBinary {
} else {
for (const item of releases) {
if (dir === `/${item.tag_name}/` || dir === `/${item.tag_name.substring(1)}/`) {
items = this.formatItems(item);
items = this.formatItems(item, binaries.electron);
break;
}
}
Expand Down
26 changes: 16 additions & 10 deletions app/common/adapter/binary/GithubBinary.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
import { AbstractBinary, FetchResult, BinaryItem } from './AbstractBinary';
import { SingletonProto } from '@eggjs/tegg';
import { BinaryType } from 'app/common/enum/Binary';
import binaries, { BinaryTaskConfig } from 'config/binaries';
import { AbstractBinary, FetchResult, BinaryItem, BinaryAdapter } from './AbstractBinary';

@SingletonProto()
@BinaryAdapter(BinaryType.GitHub)
export class GithubBinary extends AbstractBinary {
private releases?: any[];

protected async initReleases() {
protected async initReleases(binaryConfig: BinaryTaskConfig) {
if (!this.releases) {
// https://docs.github.com/en/rest/reference/releases get three pages
// https://api.github.com/repos/electron/electron/releases
// https://api.github.com/repos/electron/electron/releases?per_page=100&page=3
let releases: any[] = [];
const maxPage = this.binaryConfig.options?.maxPage || 1;
const maxPage = binaryConfig.options?.maxPage || 1;
for (let i = 0; i < maxPage; i++) {
const url = `https://api.github.com/repos/${this.binaryConfig.repo}/releases?per_page=100&page=${i + 1}`;
const url = `https://api.github.com/repos/${binaryConfig.repo}/releases?per_page=100&page=${i + 1}`;
const data = await this.requestJSON(url);
if (!Array.isArray(data)) {
// {"message":"API rate limit exceeded for 47.57.239.54. (But here's the good news: Authenticated requests get a higher rate limit. Check out the documentation for more details.)","documentation_url":"https://docs.github.com/rest/overview/resources-in-the-rest-api#rate-limiting"}
Expand All @@ -29,7 +34,7 @@ export class GithubBinary extends AbstractBinary {
return this.releases;
}

protected formatItems(releaseItem: any) {
protected formatItems(releaseItem: any, binaryConfig: BinaryTaskConfig) {
const items: BinaryItem[] = [];
// 200MB
const maxFileSize = 1024 * 1024 * 200;
Expand All @@ -50,7 +55,7 @@ export class GithubBinary extends AbstractBinary {
items.push({
name: `${releaseItem.tag_name}.tar.gz`,
isDir: false,
url: `https://github.com/${this.binaryConfig.repo}/archive/${releaseItem.tag_name}.tar.gz`,
url: `https://github.com/${binaryConfig.repo}/archive/${releaseItem.tag_name}.tar.gz`,
size: '-',
date: releaseItem.published_at,
});
Expand All @@ -59,16 +64,17 @@ export class GithubBinary extends AbstractBinary {
items.push({
name: `${releaseItem.tag_name}.zip`,
isDir: false,
url: `https://github.com/${this.binaryConfig.repo}/archive/${releaseItem.tag_name}.zip`,
url: `https://github.com/${binaryConfig.repo}/archive/${releaseItem.tag_name}.zip`,
size: '-',
date: releaseItem.published_at,
});
}
return items;
}

async fetch(dir: string): Promise<FetchResult | undefined> {
const releases = await this.initReleases();
async fetch(dir: string, binaryName: string): Promise<FetchResult | undefined> {
const binaryConfig = binaries[binaryName];
const releases = await this.initReleases(binaryConfig);
if (!releases) return;

let items: BinaryItem[] = [];
Expand All @@ -85,7 +91,7 @@ export class GithubBinary extends AbstractBinary {
} else {
for (const item of releases) {
if (dir === `/${item.tag_name}/`) {
items = this.formatItems(item);
items = this.formatItems(item, binaryConfig);
break;
}
}
Expand Down
Loading

0 comments on commit d87f421

Please sign in to comment.