Lightweight platform detection for Git hosting providers. Designed for GitHub Actions and self-hosted Gitea, with easy extensibility for additional platforms.
- Hostname matching followed by API probing for unknown hosts
- Explicit provider override when required
- Clean provider factory with validation and credentials support
- Simple extension model for new platforms
- Node.js 20 compatible
flowchart TD
Start[Start] --> Explicit{ExplicitProvider?}
Explicit -->|Yes| Validate[createByName]
Explicit -->|No| Candidates[CollectCandidateUrls]
Candidates --> HostMatch[createByUrl hostname match]
HostMatch -->|Matched| Done[Result]
HostMatch -->|No match| Probe[API probing]
Probe -->|Matched| Done
Probe -->|No match| Fallback[Generic fallback]
npm install @liquidlogiclabs/git-platform-detectorThe package is published publicly to npmjs and installs with no authentication.
import { detectPlatform } from '@liquidlogiclabs/git-platform-detector';
const result = await detectPlatform({
repositoryUrl: 'https://gitea.example.com/org/repo',
credentials: { token: process.env.GITEA_TOKEN }
});
console.log(result.providerId);
console.log(result.baseUrl);import { detectPlatform } from '@liquidlogiclabs/git-platform-detector';
const result = await detectPlatform({
requestedProvider: 'gitea',
repositoryUrl: 'https://gitea.example.com/org/repo'
});import { createByName, createByUrl } from '@liquidlogiclabs/git-platform-detector';
const explicit = createByName('github');
const auto = await createByUrl('https://github.com/octo/repo', {
credentials: { token: process.env.GITHUB_TOKEN }
});Returns a DetectionResult:
providerId: detected provider idbaseUrl: normalized base URL (if available)owner,repo: parsed owner/repo (when available)confidence: numeric confidence scoreevidence: detection evidence list
Validates and returns a provider by name. No hostname matching or probing is performed.
Provider Aliases: The following aliases are supported for the generic provider:
generic(canonical name)git(alias for generic - local Git CLI operations)local(alias for generic - local Git CLI operations)
All aliases are case-insensitive and map to the generic provider.
Runs hostname matching first, then API probing. Returns a provider match or falls back to generic.
Collects URLs from explicit inputs and environment variables.
Returns every built-in Provider — GitHub, Gitea, Bitbucket and the generic
fallback. Useful for inspecting what will be matched against, or for building a
custom provider list.
import { getBuiltInProviders } from '@liquidlogiclabs/git-platform-detector';
console.log(getBuiltInProviders().map(p => p.id));
// ['gitea', 'github', 'bitbucket', 'generic']Looks up a single provider by id, returning undefined if there is no match.
Pass providers to search a custom list instead of the built-ins.
Parses a string into a URL, returning undefined rather than throwing when the
input is not a valid URL. Use it when handling untrusted or optional input.
Extracts { owner?, repo? } from a repository URL. Both fields are optional —
a URL that does not contain them yields an empty object rather than an error.
import { toUrl, parseOwnerRepo } from '@liquidlogiclabs/git-platform-detector';
const url = toUrl('https://github.com/octocat/Hello-World.git');
if (url) console.log(parseOwnerRepo(url)); // { owner: 'octocat', repo: 'Hello-World' }Returns true when the current environment looks like Gitea Actions rather than
GitHub Actions. Defaults to process.env; pass an object to test explicitly.
Detection is by environment shape, not by hostname — a GITHUB_SERVER_URL that
does not point at github.com indicates Gitea running in GitHub-compatibility
mode.
The library never logs credentials. Supply a Logger to see detection steps:
import { detectPlatform, ConsoleLogger } from '@liquidlogiclabs/git-platform-detector';
await detectPlatform({
repositoryUrl: 'https://gitea.example.com/org/repo',
logger: new ConsoleLogger()
});ConsoleLogger writes to the console; NoopLogger discards everything and is
the default. A Logger is any object with info, warn and debug methods
taking a single string, so your own logger can be passed directly.
Types ship with the package. In addition to the functions above, these types are exported for annotating your own code:
BuiltInProviderId, ProviderId, CredentialSet, ProbeContext,
ProbeResult, DetectionEvidence, DetectionResult, Logger, Provider,
FactoryOptions.
- GitHub
- Gitea
- Bitbucket
- Generic (fallback)
- Implement the
Providerinterface insrc/providers/<provider>.ts. - Register the provider in
src/providers/index.ts. - Add unit tests for hostname matching and probe behavior.
Example provider structure:
import type { Provider } from '@liquidlogiclabs/git-platform-detector';
export const myProvider: Provider = {
id: 'my-platform',
displayName: 'My Platform',
isUrlMatch: (url) => url.hostname.endsWith('example.com'),
probeApi: async () => ({ matched: false }),
determineBaseUrl: (urls) => urls[0]
};- Tokens can be passed via
credentialsfor API probing. - Use environment variables or secrets for tokens in CI.
- Avoid logging tokens; the library never logs credentials.
Node.js 20 or later (engines: node >=20). Ships CommonJS with TypeScript
declarations.
Source lives at LiquidLogicLabs/npm-package-git-platform-detector. Issues and pull requests are welcome there.
npm install
npm run build
npm testMIT