-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(datasource): migrate to class based datasource
A small experiment into what OOP/class based datasources might look like. Picked Cdnjs as it's the smallest & simplest. With this approach we can share common logic, like error handling, rate limiting, etc. between different datasources, instead of having to reimplement it each time we write a new datasource. Currently there's nothing shared, as it's only 1 datasource, but the interesting stuff will come with the 2nd datasource
- Loading branch information
1 parent
910e44a
commit 9a09d00
Showing
15 changed files
with
210 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { Release, getPkgReleases } from '..'; | ||
import * as mavenVersioning from '../../versioning/maven'; | ||
import { id as datasource } from '.'; | ||
|
||
const config = { | ||
versioning: mavenVersioning.id, | ||
datasource, | ||
}; | ||
|
||
describe('datasource/clojure', () => { | ||
describe('getReleases', () => { | ||
function generateReleases(versions: string[]): Release[] { | ||
return versions.map((v) => ({ version: v })); | ||
} | ||
|
||
it('should return empty if library is not found', async () => { | ||
const releases = await getPkgReleases({ | ||
...config, | ||
depName: 'unknown:unknown', | ||
registryUrls: [ | ||
's3://somewhere.s3.aws.amazon.com', | ||
'file://lib/datasource/maven/__fixtures__/repo1.maven.org/maven2/', | ||
], | ||
}); | ||
expect(releases).toBeNull(); | ||
}); | ||
|
||
it('should simply return all versions of a specific library', async () => { | ||
const releases = await getPkgReleases({ | ||
...config, | ||
depName: 'org.hamcrest:hamcrest-core', | ||
registryUrls: [ | ||
'file://lib/datasource/maven/__fixtures__/repo1.maven.org/maven2/', | ||
'file://lib/datasource/maven/__fixtures__/custom_maven_repo/maven2/', | ||
's3://somewhere.s3.aws.amazon.com', | ||
], | ||
}); | ||
expect(releases.releases).toEqual( | ||
generateReleases([ | ||
'1.1', | ||
'1.2', | ||
'1.2.1', | ||
'1.3.RC2', | ||
'1.3', | ||
'2.1-rc2', | ||
'2.1-rc3', | ||
]) | ||
); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,26 @@ | ||
import { Datasource } from '../datasource'; | ||
import { getReleases } from '../maven'; | ||
import { MAVEN_REPO } from '../maven/common'; | ||
import { GetReleasesConfig, ReleaseResult } from '../types'; | ||
|
||
export const id = 'clojure'; | ||
export const customRegistrySupport = true; | ||
export const defaultRegistryUrls = ['https://clojars.org/repo', MAVEN_REPO]; | ||
export const registryStrategy = 'merge'; | ||
|
||
export { getReleases } from '../maven'; | ||
export class Clojure extends Datasource { | ||
readonly id = 'clojure'; | ||
|
||
readonly registryStrategy = 'merge'; | ||
|
||
readonly customRegistrySupport = true; | ||
|
||
readonly defaultRegistryUrls = ['https://clojars.org/repo', MAVEN_REPO]; | ||
|
||
getReleases({ | ||
lookupName, | ||
registryUrl, | ||
}: GetReleasesConfig): Promise<ReleaseResult | null> { | ||
return getReleases({ lookupName, registryUrl }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { ExternalHostError } from '../types/errors/external-host-error'; | ||
import { Http, HttpError } from '../util/http'; | ||
import { | ||
DatasourceApi, | ||
DigestConfig, | ||
GetReleasesConfig, | ||
ReleaseResult, | ||
} from './types'; | ||
|
||
export abstract class Datasource implements DatasourceApi { | ||
constructor() { | ||
this.http = new Http(this.getId()); | ||
} | ||
|
||
abstract id: string; | ||
|
||
caching: boolean; | ||
|
||
defaultConfig: Record<string, unknown>; | ||
|
||
customRegistrySupport: boolean; | ||
|
||
defaultRegistryUrls: string[]; | ||
|
||
defaultVersioning: string; | ||
|
||
registryStrategy: 'first' | 'hunt' | 'merge'; | ||
|
||
getId(): string { | ||
return this.id; | ||
} | ||
|
||
protected http: Http; | ||
|
||
abstract getReleases( | ||
getReleasesConfig: GetReleasesConfig | ||
): Promise<ReleaseResult | null>; | ||
|
||
getDigest?(config: DigestConfig, newValue?: string): Promise<string>; | ||
|
||
handleSpecificErrors(err: HttpError): void {} | ||
|
||
protected handleGenericErrors(err: HttpError): never { | ||
this.handleSpecificErrors(err); | ||
if (err.statusCode !== undefined) { | ||
if ( | ||
err.statusCode === 429 || | ||
(err.statusCode >= 500 && err.statusCode < 600) | ||
) { | ||
throw new ExternalHostError(err); | ||
} | ||
} | ||
throw err; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.