import type { Express, RequestHandler } from 'express'; import type { Readable, Writable } from 'node:stream'; import type { AllowAccess, Callback, Config, Logger, Manifest, PackageAccess, RemoteUser, Token, TokenFilter } from '@verdaccio/types'; import type { VerdaccioError, searchUtils } from '.'; export interface PluginOptions { config: Config; logger: Logger; } /** * Base Plugin Class * * Set of utilities for developing plugins. */ export declare class Plugin { static version: number; readonly version: number; readonly config: PluginConfig | unknown; readonly options: PluginOptions; constructor(config: PluginConfig, options: PluginOptions); getVersion(): number; } /** * Storage Handler * * Used in storage plugin for managing packages and tarballs. */ export interface StorageHandler { logger: Logger; deletePackage(fileName: string): Promise; removePackage(packageName: string): Promise; updatePackage(packageName: string, handleUpdate: (manifest: Manifest) => Promise): Promise; readPackage(packageName: string): Promise; savePackage(packageName: string, manifest: Manifest): Promise; readTarball(fileName: string, { signal }: { signal: AbortSignal; }): Promise; createPackage(packageName: string, manifest: Manifest): Promise; writeTarball(fileName: string, { signal }: { signal: AbortSignal; }): Promise; hasTarball(fileName: string): Promise; hasPackage(packageName: string): Promise; } /** * Storage Plugin interface * * https://verdaccio.org/docs/plugin-storage */ export interface Storage extends Plugin { add(packageName: string): Promise; remove(packageName: string): Promise; get(): Promise; init(): Promise; getSecret(): Promise; setSecret(secret: string): Promise; getPackageStorage(packageName: string): StorageHandler; search(query: searchUtils.SearchQuery): Promise; saveToken(token: Token): Promise; deleteToken(user: string, tokenKey: string): Promise; readTokens(filter: TokenFilter): Promise; } /** * Middleware Plugin Interface * * https://verdaccio.org/docs/plugin-middleware * * This function allow add middleware to the application. * * ```ts * import express, { Request, Response } from 'express'; * * class Middleware extends Plugin { * // instances of auth and storage are injected * register_middlewares(app, auth, storage) { * const router = express.Router(); * router.post('/my-endpoint', (req: Request, res: Response): void => { res.status(200).end(); }); * } * } * * const [plugin] = await asyncLoadPlugin(...); * plugin.register_middlewares(app, auth, storage); * ``` */ export interface ExpressMiddleware extends Plugin { register_middlewares(app: Express, auth: Auth, storage: Storage): void; } export type AuthCallback = (error: VerdaccioError | null, groups?: string[] | false) => void; export type AuthAccessCallback = (error: VerdaccioError | null, access?: boolean) => void; export type AuthUserCallback = (error: VerdaccioError | null, access?: boolean | string) => void; export type AuthChangePasswordCallback = (error: VerdaccioError | null, access?: boolean) => void; export type AccessCallback = (error: VerdaccioError | null, ok?: boolean) => void; export interface AuthPluginPackage { packageName: string; packageVersion?: string; tag?: string; } /** * Authentication Plugin Interface * * https://verdaccio.org/docs/plugin-auth */ export interface Auth extends Plugin { /** * Handles the authenticated method. * ```ts * class Auth { public authenticate(user: string, password: string, done: AuthCallback): void { if (!password) { return done(errorUtils.getUnauthorized(API_ERROR.BAD_USERNAME_PASSWORD)); } // always return an array of users return done(null, [user]); * } * ``` */ authenticate(user: string, password: string, cb: AuthCallback): void; /** * Handles the authenticated method. * ```ts * class Auth { public adduser(user: string, password: string, done: AuthCallback): void { if (!password) { return done(errorUtils.getUnauthorized(API_ERROR.BAD_USERNAME_PASSWORD)); } // return boolean return done(null, true); * } * ``` */ adduser?(user: string, password: string, cb: AuthUserCallback): void; changePassword?(user: string, password: string, newPassword: string, cb: AuthChangePasswordCallback): void; allow_publish?(user: RemoteUser, pkg: T & PackageAccess, cb: AuthAccessCallback): void; allow_publish?(user: RemoteUser, pkg: AllowAccess & PackageAccess, cb: AuthAccessCallback): void; allow_access?(user: RemoteUser, pkg: T & PackageAccess, cb: AccessCallback): void; allow_access?(user: RemoteUser, pkg: AllowAccess & PackageAccess, cb: AccessCallback): void; allow_unpublish?(user: RemoteUser, pkg: T & PackageAccess, cb: AuthAccessCallback): void; allow_unpublish?(user: RemoteUser, pkg: AllowAccess & PackageAccess, cb: AuthAccessCallback): void; apiJWTmiddleware?(helpers: any): RequestHandler; } export interface IBasicAuth { authenticate(user: string, password: string, cb: Callback): void; invalidateToken?(token: string): Promise; changePassword(user: string, password: string, newPassword: string, cb: Callback): void; allow_access(pkg: AuthPluginPackage, user: RemoteUser, callback: Callback): void; add_user(user: string, password: string, cb: Callback): any; } /** * Filter Plugin Interface * * https://verdaccio.org/docs/plugin-filter */ export interface ManifestFilter extends Plugin { filter_metadata(manifest: Manifest): Promise; }