1
0
mirror of https://github.com/verdaccio/verdaccio.git synced 2024-11-08 23:25:51 +01:00

chore: add flow plugin examples

middleware, authentication and storage
This commit is contained in:
Juan Picado @jotadeveloper 2018-07-15 19:26:58 +02:00
parent 7ce6abf3b2
commit 55c146cb5d
No known key found for this signature in database
GPG Key ID: 18AC54485952D158
6 changed files with 214 additions and 34 deletions

@ -16,7 +16,7 @@
}, },
"dependencies": { "dependencies": {
"@verdaccio/file-locking": "0.0.7", "@verdaccio/file-locking": "0.0.7",
"@verdaccio/local-storage": "1.1.2", "@verdaccio/local-storage": "1.1.3",
"@verdaccio/streams": "1.0.0", "@verdaccio/streams": "1.0.0",
"JSONStream": "1.3.2", "JSONStream": "1.3.2",
"asciidoctor.js": "1.5.6", "asciidoctor.js": "1.5.6",
@ -52,7 +52,7 @@
"devDependencies": { "devDependencies": {
"@commitlint/cli": "6.1.3", "@commitlint/cli": "6.1.3",
"@commitlint/config-conventional": "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-cli": "6.26.0",
"babel-core": "6.26.0", "babel-core": "6.26.0",
"babel-eslint": "8.2.2", "babel-eslint": "8.2.2",

@ -1,19 +1,18 @@
// @flow // @flow
// this file is not aim to be tested, just to check flow definitions
import Config from '../../../../src/lib/config'; import Config from '../../../../src/lib/config';
import LoggerApi from '../../../../src/lib/logger'; import LoggerApi from '../../../../src/lib/logger';
import type { import type {
AuthPlugin,
Config as AppConfig, Config as AppConfig,
IAuthPlugin, IPluginAuth,
Logger, Logger,
PluginOptions PluginOptions
} from '@verdaccio/types'; } from '@verdaccio/types';
class ExampleAuthPlugin implements IPluginAuth {
// this class is not aim to be tested, just to check flow definitions
class ExampleAuthPlugin implements IAuthPlugin {
config: AppConfig; config: AppConfig;
logger: Logger; logger: Logger;
@ -45,6 +44,8 @@ const options: PluginOptions = {
logger: LoggerApi.logger.child() logger: LoggerApi.logger.child()
} }
// $FlowFixMe const auth = new ExampleAuthPlugin(config1, options);
const instance1: AuthPlugin = new ExampleAuthPlugin(config1, options);
console.log(instance1); auth.authenticate('user', 'pass', () => {});
auth.allow_access('packageName', 'user', () => {});
auth.allow_publish('packageName', 'user', () => {});

@ -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 */
}
}

@ -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 // @flow
import type { import type {
IBasicAuth,
IBasicStorage,
UpLinkConf, UpLinkConf,
Callback, Callback,
Versions, Versions,
@ -9,6 +11,7 @@ import type {
Config, Config,
Logger, Logger,
PackageAccess, PackageAccess,
StringValue as verdaccio$StringValue,
Package} from '@verdaccio/types'; Package} from '@verdaccio/types';
import type { import type {
IUploadTarball, IUploadTarball,
@ -17,20 +20,22 @@ import type {
import type {ILocalData} from '@verdaccio/local-storage'; import type {ILocalData} from '@verdaccio/local-storage';
import type {NextFunction, $Request, $Response} from 'request'; import type {NextFunction, $Request, $Response} from 'request';
export type StringValue = string | void | null; export type StringValue = verdaccio$StringValue;
export interface IAuth { interface IAuthWebUI {
config: Config; issueUIjwt(user: string, time: string): string;
logger: Logger; }
secret: string;
plugins: Array<any>; interface IAuthMiddleware {
aesEncrypt(buf: Buffer): Buffer;
apiJWTmiddleware(): $NextFunctionVer; apiJWTmiddleware(): $NextFunctionVer;
webUIJWTmiddleware(): $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; export interface IAuth extends IBasicAuth, IAuthMiddleware, IAuthWebUI {
add_user(user: string, password: string, cb: Callback): any; config: verdaccio$Config;
logger: verdaccio$Logger;
secret: string;
plugins: Array<any>;
} }
export interface IWebSearch { export interface IWebSearch {
@ -103,22 +108,10 @@ export type StartUpConfig = {
export type MatchedPackage = PackageAccess | void; export type MatchedPackage = PackageAccess | void;
export interface IStorage { export interface IStorage extends IBasicStorage {
config: Config; config: Config;
localData: ILocalData; localData: ILocalData;
logger: Logger; 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 = { export type JWTPayload = {

BIN
yarn.lock

Binary file not shown.