1
0
mirror of https://github.com/verdaccio/verdaccio.git synced 2024-11-17 07:45:52 +01:00
verdaccio/website/translated_docs/vi/dev-plugins.md
verdacciobot 1fc1a33c4c docs: new Crowdin translations (#910)
* New translations auth.md (Vietnamese)
docs(website): new translations

* New translations auth.md (Vietnamese)
docs(website): new translations

* New translations build.md (Vietnamese)
docs(website): new translations

* New translations config.md (Vietnamese)
docs(website): new translations

* New translations ci.md (Vietnamese)
docs(website): new translations

* New translations config.md (Vietnamese)
docs(website): new translations

* New translations config.md (Vietnamese)
docs(website): new translations

* New translations config.md (Vietnamese)
docs(website): new translations

* New translations config.md (Vietnamese)
docs(website): new translations

* New translations config.md (Vietnamese)
docs(website): new translations

* New translations config.md (Vietnamese)
docs(website): new translations

* New translations config.md (Vietnamese)
docs(website): new translations

* New translations config.md (Vietnamese)
docs(website): new translations

* New translations auth.md (Vietnamese)
docs(website): new translations

* New translations config.md (Vietnamese)
docs(website): new translations

* New translations contributing.md (Vietnamese)
docs(website): new translations

* New translations contributing.md (Vietnamese)
docs(website): new translations

* New translations contributing.md (Vietnamese)
docs(website): new translations

* New translations contributing.md (Vietnamese)
docs(website): new translations

* New translations contributing.md (Vietnamese)
docs(website): new translations

* New translations contributing.md (Vietnamese)
docs(website): new translations

* New translations contributing.md (Vietnamese)
docs(website): new translations

* New translations contributing.md (Vietnamese)
docs(website): new translations

* New translations contributing.md (Vietnamese)
docs(website): new translations

* New translations contributing.md (Vietnamese)
docs(website): new translations

* New translations contributing.md (Vietnamese)
docs(website): new translations

* New translations contributing.md (Vietnamese)
docs(website): new translations

* New translations contributing.md (Vietnamese)
docs(website): new translations

* New translations contributing.md (Vietnamese)
docs(website): new translations

* New translations contributing.md (Vietnamese)
docs(website): new translations

* New translations contributing.md (Vietnamese)
docs(website): new translations

* New translations contributing.md (Vietnamese)
docs(website): new translations

* New translations contributing.md (Vietnamese)
docs(website): new translations

* New translations contributing.md (Vietnamese)
docs(website): new translations

* New translations contributing.md (Vietnamese)
docs(website): new translations

* New translations contributing.md (Vietnamese)
docs(website): new translations

* New translations dev-plugins.md (Vietnamese)
docs(website): new translations

* New translations dev-plugins.md (Vietnamese)
docs(website): new translations

* New translations dev-plugins.md (Vietnamese)
docs(website): new translations

* New translations dev-plugins.md (Vietnamese)
docs(website): new translations

* New translations dev-plugins.md (Vietnamese)
docs(website): new translations

* New translations packages.md (Vietnamese)
docs(website): new translations

* New translations packages.md (Italian)
docs(website): new translations

* New translations dev-plugins.md (Vietnamese)
docs(website): new translations

* New translations packages.md (Polish)
docs(website): new translations

* New translations packages.md (Portuguese)
docs(website): new translations

* New translations packages.md (Russian)
docs(website): new translations

* New translations packages.md (Korean)
docs(website): new translations

* New translations packages.md (Arabic)
docs(website): new translations

* New translations packages.md (Chinese Simplified)
docs(website): new translations

* New translations packages.md (Spanish)
docs(website): new translations

* New translations packages.md (Portuguese, Brazilian)
docs(website): new translations

* New translations packages.md (Japanese)
docs(website): new translations

* New translations packages.md (German)
docs(website): new translations

* New translations packages.md (French)
docs(website): new translations

* New translations dev-plugins.md (Vietnamese)
docs(website): new translations

* New translations auth.md (Vietnamese)
docs(website): new translations

* New translations contributing.md (Vietnamese)
docs(website): new translations

* New translations build.md (Vietnamese)
docs(website): new translations

* New translations en.json (Chinese Simplified)
docs(website): new translations

* New translations plugins.md (Vietnamese)
docs(website): new translations

* New translations plugins.md (Italian)
docs(website): new translations

* New translations plugins.md (Polish)
docs(website): new translations

* New translations plugins.md (Russian)
docs(website): new translations

* New translations plugins.md (Portuguese)
docs(website): new translations

* New translations plugins.md (Korean)
docs(website): new translations

* New translations plugins.md (Spanish)
docs(website): new translations

* New translations plugins.md (Arabic)
docs(website): new translations

* New translations plugins.md (Chinese Simplified)
docs(website): new translations

* New translations plugins.md (Portuguese, Brazilian)
docs(website): new translations

* New translations plugins.md (German)
docs(website): new translations

* New translations plugins.md (Japanese)
docs(website): new translations

* New translations plugins.md (French)
docs(website): new translations
2018-08-17 08:03:53 +02:00

6.4 KiB

id title
dev-plugins Phát triển các plugin

Có nhiều cách để mở rộng verdaccio. Các loại plugin được hỗ trợ là:

  • Những plugin xác minh
  • Plugin Middleware (kể từ phiên bản v2.7.0)
  • Plugin lưu trữ từ phiên bản (v3.x)

Chúng tôi khuyên bạn nên phát triển plugin bằng cách sử dụng định nghĩa loại luồng của chúng tôi.

Plugin xác minh

Cơ bản chúng ta phải trả về một đối tượng với phương thức được gọi là authenticate, và sẽ nhận lại 3 tham số (user, password, callback).

API

interface IPluginAuth extends IPlugin {
  login_url?: string;
  authenticate(user: string, password: string, cb: Callback): void;
  adduser(user: string, password: string, cb: Callback): void;
  allow_access(user: RemoteUser, pkg: $Subtype<PackageAccess>, cb: Callback): void;
  allow_publish(user: RemoteUser, pkg: $Subtype<PackageAccess>, cb: Callback): void;
}

Chỉ có các tùy chọn là adduser, allow_accessallow_publish và verdaccio cung cấp chức năng dự phòng trong tất cả các tùy chọn này.

Callback

Khi xác thực được thực hiện, có hai tùy chọn để trả lời verdaccio.

OnError

Hiện lỗi này nghĩa là hoặc xảy ra lỗi hoặc xác thực không thành công.

callback(null, false)
OnSuccess

Xác thực thành công.

groups là một tập hợp các chuỗi người dùng.

 callback(null, groups);

Ví dụ

function Auth(config, stuff) {
  var self = Object.create(Auth.prototype);
  self._users = {};

  // config for this module
  self._config = config;

  // verdaccio logger
  self._logger = stuff.logger;

  // pass verdaccio logger to ldapauth
  self._config.client_options.log = stuff.logger;

  return self;
}

Auth.prototype.authenticate = function (user, password, callback) {
  var LdapClient = new LdapAuth(self._config.client_options);
  ....
  LdapClient.authenticate(user, password, function (err, ldapUser) {
    ...
    var groups;
     ...
    callback(null, groups);
  });
};

module.exports = Auth;

Cấu hình sẽ trông như thế này:

auth:
  htpasswd:
    file: ./htpasswd

Trong đó htpasswd là tên của plugin, ví dụ: hậu tố của verdaccio-htpasswd. Các mã còn lại là các tham số của cấu hình plugin.

Plugin Middleware

Plugin Middleware có khả năng sửa đổi giao diện API để thêm các điểm cuối mới hoặc chặn các yêu cầu.

interface verdaccio$IPluginMiddleware extends verdaccio$IPlugin {
  register_middlewares(app: any, auth: IBasicAuth, storage: IStorageManager): void;
}

register_middlewares

The method provide full access to the authentification and storage via auth and storage. app is the express application that allows you to add new endpoints.

A pretty good example of middleware plugin is the sinopia-github-oauth and verdaccio-audit.

API

function register_middlewares(expressApp, authInstance, storageInstance) {
   /* more stuff */
}

To register a middleware we need an object with a single method called register_middlewares that will recieve 3 arguments (expressApp, auth, storage). Auth is the authentification instance and storage is also the main Storage instance that will give you have access to all to the storage actions.

Storage Plugin

Verdaccio by default uses a file system storage plugin local-storage, but, since verdaccio@3.x you can plug in a custom storage replacing the default behaviour.

API

The storage API is a bit more complex, you will need to create a class that return a IPluginStorage implementation. Please see details bellow.

class LocalDatabase<IPluginStorage>{
  constructor(config: $Subtype<verdaccio$Config>, logger: verdaccio$Logger): ILocalData;
}

interface IPluginStorage {
  logger: verdaccio$Logger;
    config: $Subtype<verdaccio$Config>;
  add(name: string, callback: verdaccio$Callback): void;
  remove(name: string, callback: verdaccio$Callback): void;
  get(callback: verdaccio$Callback): void;
  getSecret(): Promise<string>;
  setSecret(secret: string): Promise<any>;
  getPackageStorage(packageInfo: string): verdaccio$IPackageStorage;
  search(onPackage: verdaccio$Callback, onEnd: verdaccio$Callback, validateName: Function): void;
}

interface IPackageStorageManager {
  path: string;
  logger: verdaccio$Logger;
  writeTarball(name: string): verdaccio$IUploadTarball;
  readTarball(name: string): verdaccio$IReadTarball;
  readPackage(fileName: string, callback: verdaccio$Callback): void;
  createPackage(name: string, value: verdaccio$Package, cb: verdaccio$Callback): void;
  deletePackage(fileName: string, callback: verdaccio$Callback): void;
  removePackage(callback: verdaccio$Callback): void;
  updatePackage(pkgFileName: string,
                updateHandler: verdaccio$Callback,
                onWrite: verdaccio$Callback,
                transformPackage: Function,
                onEnd: verdaccio$Callback): void;
  savePackage(fileName: string, json: verdaccio$Package, callback: verdaccio$Callback): void;
}

class verdaccio$IUploadTarball extends stream$PassThrough {
  abort: Function;
  done: Function;
  _transform: Function;
  abort(): void;
  done(): void;
}

class verdaccio$IReadTarball extends stream$PassThrough {
  abort: Function;
  abort(): void;
}

The Storage API is still experimental and might change in the next minor versions. For further information about Storage API please follow the types definitions in our official repository.

Storage Plugins Examples

The following list of plugins are implementing the Storage API and might be used them as example.

Are you willing to contribute with new Storage Plugins? Click here.