mirror of
https://github.com/verdaccio/verdaccio.git
synced 2024-12-30 22:34:10 -05:00
chore: add flow plugin examples
middleware, authentication and storage
This commit is contained in:
parent
7ce6abf3b2
commit
55c146cb5d
6 changed files with 214 additions and 34 deletions
|
@ -16,7 +16,7 @@
|
|||
},
|
||||
"dependencies": {
|
||||
"@verdaccio/file-locking": "0.0.7",
|
||||
"@verdaccio/local-storage": "1.1.2",
|
||||
"@verdaccio/local-storage": "1.1.3",
|
||||
"@verdaccio/streams": "1.0.0",
|
||||
"JSONStream": "1.3.2",
|
||||
"asciidoctor.js": "1.5.6",
|
||||
|
@ -52,7 +52,7 @@
|
|||
"devDependencies": {
|
||||
"@commitlint/cli": "6.1.3",
|
||||
"@commitlint/config-conventional": "6.1.3",
|
||||
"@verdaccio/types": "3.1.0",
|
||||
"@verdaccio/types": "3.2.0",
|
||||
"babel-cli": "6.26.0",
|
||||
"babel-core": "6.26.0",
|
||||
"babel-eslint": "8.2.2",
|
||||
|
|
|
@ -1,19 +1,18 @@
|
|||
// @flow
|
||||
|
||||
// this file is not aim to be tested, just to check flow definitions
|
||||
|
||||
import Config from '../../../../src/lib/config';
|
||||
import LoggerApi from '../../../../src/lib/logger';
|
||||
|
||||
import type {
|
||||
AuthPlugin,
|
||||
Config as AppConfig,
|
||||
IAuthPlugin,
|
||||
IPluginAuth,
|
||||
Logger,
|
||||
PluginOptions
|
||||
} from '@verdaccio/types';
|
||||
|
||||
|
||||
// this class is not aim to be tested, just to check flow definitions
|
||||
class ExampleAuthPlugin implements IAuthPlugin {
|
||||
class ExampleAuthPlugin implements IPluginAuth {
|
||||
config: AppConfig;
|
||||
logger: Logger;
|
||||
|
||||
|
@ -45,6 +44,8 @@ const options: PluginOptions = {
|
|||
logger: LoggerApi.logger.child()
|
||||
}
|
||||
|
||||
// $FlowFixMe
|
||||
const instance1: AuthPlugin = new ExampleAuthPlugin(config1, options);
|
||||
console.log(instance1);
|
||||
const auth = new ExampleAuthPlugin(config1, options);
|
||||
|
||||
auth.authenticate('user', 'pass', () => {});
|
||||
auth.allow_access('packageName', 'user', () => {});
|
||||
auth.allow_publish('packageName', 'user', () => {});
|
47
test/flow/plugins/middleware/example.middleware.plugin.js
Normal file
47
test/flow/plugins/middleware/example.middleware.plugin.js
Normal file
|
@ -0,0 +1,47 @@
|
|||
// @flow
|
||||
|
||||
// this file is not aim to be tested, just to check flow definitions
|
||||
import Config from '../../../../src/lib/config';
|
||||
import {generatePackageTemplate} from '../../../../src/lib/storage-utils';
|
||||
import {readFile} from '../../../functional/lib/test.utils';
|
||||
|
||||
const readMetadata = (fileName: string = 'metadata') => readFile(`../../unit/partials/${fileName}`);
|
||||
|
||||
import type {
|
||||
Config as AppConfig,
|
||||
IPluginMiddleware,
|
||||
IBasicStorage,
|
||||
IBasicAuth,
|
||||
} from '@verdaccio/types';
|
||||
import type { IUploadTarball, IReadTarball } from '@verdaccio/streams';
|
||||
|
||||
export default class ExampleMiddlewarePlugin implements IPluginMiddleware {
|
||||
register_middlewares(app: any, auth: IBasicAuth, storage: IBasicStorage): void {
|
||||
auth.authenticate('user', 'password', () => {});
|
||||
auth.allow_access('packageName', 'user', () => {});
|
||||
auth.add_user('user', 'password', () => {});
|
||||
auth.aesEncrypt(new Buffer('pass'));
|
||||
// storage
|
||||
storage.addPackage('name', generatePackageTemplate('test'), () => {});
|
||||
storage.addVersion('name', 'version', readMetadata(), 'tag', () => {});
|
||||
storage.mergeTags('name', {'latest': '1.0.0'}, () => {});
|
||||
storage.changePackage('name', readMetadata(), 'revision', () => {});
|
||||
storage.removePackage('name', () => {});
|
||||
storage.updateVersions('name', generatePackageTemplate('test'), () => {});
|
||||
storage.mergeTags('name', {'latest': '1.0.0'}, () => {});
|
||||
storage.removeTarball('name', 'filename', 'revision', () => {});
|
||||
storage.getPackageMetadata('test', () => {});
|
||||
/* eslint no-unused-vars: 0 */
|
||||
const config1: AppConfig = new Config({
|
||||
storage: './storage',
|
||||
self_path: '/home/sotrage'
|
||||
});
|
||||
const add: IUploadTarball = storage.addTarball('name', 'filename');
|
||||
storage.getTarball('name', 'filename');
|
||||
const read: IReadTarball = storage.getTarball('name', 'filename');
|
||||
const search: IUploadTarball = storage.search('test');
|
||||
const secret: Promise<any> = storage.getSecret(config1);
|
||||
/* eslint no-unused-vars: 0 */
|
||||
}
|
||||
}
|
||||
|
139
test/flow/plugins/storage/example.storage.plugin.js
Normal file
139
test/flow/plugins/storage/example.storage.plugin.js
Normal file
|
@ -0,0 +1,139 @@
|
|||
// @flow
|
||||
|
||||
// this file is not aim to be tested, just to check flow definitions
|
||||
|
||||
import Config from '../../../../src/lib/config';
|
||||
import LoggerApi from '../../../../src/lib/logger';
|
||||
import {generatePackageTemplate} from '../../../../src/lib/storage-utils';
|
||||
import { UploadTarball, ReadTarball } from '@verdaccio/streams';
|
||||
|
||||
import type {
|
||||
Callback,
|
||||
Config as AppConfig,
|
||||
Logger,
|
||||
Package,
|
||||
// PluginOptions
|
||||
} from '@verdaccio/types';
|
||||
|
||||
import type {
|
||||
IPluginStorage,
|
||||
IPackageStorageManager,
|
||||
IPackageStorage
|
||||
} from '@verdaccio/local-storage';
|
||||
import type { IUploadTarball, IReadTarball } from '@verdaccio/streams';
|
||||
|
||||
class PackageStorage implements IPackageStorageManager {
|
||||
path: string;
|
||||
logger: Logger;
|
||||
|
||||
constructor(path: string, logger: Logger) {
|
||||
this.path = path;
|
||||
this.logger = logger;
|
||||
}
|
||||
|
||||
updatePackage(name: string, updateHandler: Callback,
|
||||
onWrite: Callback,
|
||||
transformPackage: Function,
|
||||
onEnd: Callback) {
|
||||
onEnd();
|
||||
}
|
||||
|
||||
deletePackage(fileName: string, callback: Callback) {
|
||||
callback();
|
||||
}
|
||||
|
||||
removePackage(callback: Callback): void {
|
||||
callback();
|
||||
}
|
||||
|
||||
createPackage(name: string, value: Package, cb: Callback) {
|
||||
cb();
|
||||
}
|
||||
|
||||
savePackage(name: string, value: Package, cb: Callback) {
|
||||
cb();
|
||||
}
|
||||
|
||||
readPackage(name: string, cb: Callback) {
|
||||
cb();
|
||||
}
|
||||
|
||||
writeTarball(name: string): IUploadTarball {
|
||||
const uploadStream = new UploadTarball();
|
||||
|
||||
return uploadStream;
|
||||
}
|
||||
|
||||
readTarball(name: string): IReadTarball {
|
||||
const readTarballStream: IReadTarball = new ReadTarball();
|
||||
|
||||
return readTarballStream;
|
||||
}
|
||||
}
|
||||
|
||||
class ExampleStoragePlugin implements IPluginStorage {
|
||||
logger: Logger;
|
||||
config: AppConfig;
|
||||
|
||||
constructor(config: AppConfig, logger: Logger) {
|
||||
this.config = config;
|
||||
this.logger = logger;
|
||||
}
|
||||
|
||||
getSecret(): Promise<any> {
|
||||
return Promise.resolve();
|
||||
}
|
||||
|
||||
setSecret(secret: string): Promise<any> {
|
||||
return Promise.resolve();
|
||||
}
|
||||
|
||||
add(name: string, cb: Callback) {
|
||||
cb();
|
||||
}
|
||||
|
||||
remove(name: string, cb: Callback) {
|
||||
cb();
|
||||
}
|
||||
|
||||
get(cb: Callback) {
|
||||
cb();
|
||||
}
|
||||
|
||||
getPackageStorage(packageInfo: string): IPackageStorage {
|
||||
return new PackageStorage(packageInfo, this.logger);
|
||||
}
|
||||
|
||||
search(onPackage: Callback, onEnd: Callback, validateName: any): void {
|
||||
onPackage(onEnd());
|
||||
}
|
||||
}
|
||||
|
||||
export default ExampleStoragePlugin;
|
||||
|
||||
const config1: AppConfig = new Config({
|
||||
storage: './storage',
|
||||
self_path: '/home/sotrage'
|
||||
});
|
||||
|
||||
|
||||
const storage = new ExampleStoragePlugin(config1, LoggerApi.logger.child());
|
||||
|
||||
storage.add('test', () => {});
|
||||
storage.remove('test', () => {});
|
||||
storage.getSecret().then(() => {});
|
||||
storage.setSecret('newSecret').then(() => {});
|
||||
storage.search(() => {}, () => {}, 'validateName');
|
||||
storage.get(() => {});
|
||||
|
||||
const storageManager: IPackageStorage = storage.getPackageStorage('test');
|
||||
|
||||
if (storageManager) {
|
||||
storageManager.createPackage('test', generatePackageTemplate('test'), () => {});
|
||||
storageManager.savePackage('fileName', generatePackageTemplate('test'), () => {});
|
||||
storageManager.updatePackage('pkgFileName', () =>{}, () => {}, () => {}, () => {});
|
||||
storageManager.deletePackage('test', () => {});
|
||||
storageManager.removePackage(() => {});
|
||||
storageManager.readPackage('test', () => {});
|
||||
storageManager.writeTarball('test');
|
||||
}
|
|
@ -1,6 +1,8 @@
|
|||
// @flow
|
||||
|
||||
import type {
|
||||
IBasicAuth,
|
||||
IBasicStorage,
|
||||
UpLinkConf,
|
||||
Callback,
|
||||
Versions,
|
||||
|
@ -9,6 +11,7 @@ import type {
|
|||
Config,
|
||||
Logger,
|
||||
PackageAccess,
|
||||
StringValue as verdaccio$StringValue,
|
||||
Package} from '@verdaccio/types';
|
||||
import type {
|
||||
IUploadTarball,
|
||||
|
@ -17,20 +20,22 @@ import type {
|
|||
import type {ILocalData} from '@verdaccio/local-storage';
|
||||
import type {NextFunction, $Request, $Response} from 'request';
|
||||
|
||||
export type StringValue = string | void | null;
|
||||
export type StringValue = verdaccio$StringValue;
|
||||
|
||||
export interface IAuth {
|
||||
config: Config;
|
||||
logger: Logger;
|
||||
secret: string;
|
||||
plugins: Array<any>;
|
||||
aesEncrypt(buf: Buffer): Buffer;
|
||||
interface IAuthWebUI {
|
||||
issueUIjwt(user: string, time: string): string;
|
||||
}
|
||||
|
||||
interface IAuthMiddleware {
|
||||
apiJWTmiddleware(): $NextFunctionVer;
|
||||
webUIJWTmiddleware(): $NextFunctionVer;
|
||||
authenticate(user: string, password: string, cb: Callback): void;
|
||||
allow_access(packageName: string, user: string, callback: Callback): void;
|
||||
issueUIjwt(user: string, time: string): string;
|
||||
add_user(user: string, password: string, cb: Callback): any;
|
||||
}
|
||||
|
||||
export interface IAuth extends IBasicAuth, IAuthMiddleware, IAuthWebUI {
|
||||
config: verdaccio$Config;
|
||||
logger: verdaccio$Logger;
|
||||
secret: string;
|
||||
plugins: Array<any>;
|
||||
}
|
||||
|
||||
export interface IWebSearch {
|
||||
|
@ -103,22 +108,10 @@ export type StartUpConfig = {
|
|||
|
||||
export type MatchedPackage = PackageAccess | void;
|
||||
|
||||
export interface IStorage {
|
||||
export interface IStorage extends IBasicStorage {
|
||||
config: Config;
|
||||
localData: ILocalData;
|
||||
logger: Logger;
|
||||
addPackage(name: string, info: Package, callback: Callback): void;
|
||||
removePackage(name: string, callback: Callback): void;
|
||||
updateVersions(name: string, packageInfo: Package, callback: Callback): void;
|
||||
addVersion(name: string, version: string, metadata: Version, tag: StringValue, callback: Callback): void;
|
||||
mergeTags(name: string, tags: MergeTags, callback: Callback): void;
|
||||
changePackage(name: string, metadata: Package, revision: string, callback: Callback): void;
|
||||
removeTarball(name: string, filename: string, revision: string, callback: Callback): void;
|
||||
addTarball(name: string, filename: string): IUploadTarball;
|
||||
getTarball(name: string, filename: string): IReadTarball;
|
||||
getPackageMetadata(name: string, callback: Callback): void;
|
||||
search(startKey: string, options: any): IUploadTarball;
|
||||
getSecret(config: Config): Promise<any>;
|
||||
}
|
||||
|
||||
export type JWTPayload = {
|
||||
|
|
BIN
yarn.lock
BIN
yarn.lock
Binary file not shown.
Loading…
Reference in a new issue