Skip to content
This repository has been archived by the owner on Jun 16, 2024. It is now read-only.

fix: refactor storage template #18

Merged
merged 5 commits into from
Dec 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions generators/app/templates/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"rules": {
"@typescript-eslint/no-unused-vars": 0
}
}
5 changes: 5 additions & 0 deletions generators/app/templates/javascript/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"rules": {
"no-unused-vars": 0
}
}
1 change: 0 additions & 1 deletion generators/app/templates/javascript/storage/_package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
"devDependencies": {
"@verdaccio/babel-preset": "^8.5.0",
"@verdaccio/eslint-config": "^8.5.0",
"@verdaccio/types": "^8.5.0",
"eslint": "^6.6.0",
"jest": "^24.9.0",
"prettier": "^1.19.1",
Expand Down
153 changes: 153 additions & 0 deletions generators/app/templates/javascript/storage/src/PackageStorage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
import { UploadTarball, ReadTarball } from "@verdaccio/streams";
import { getNotFound, getConflict, getInternalError } from '@verdaccio/commons-api';


export default class StoragePluginManage {
constructor(packageName, helper, config, logger) {
this.logger = logger;
this.packageName = packageName;
this.config = config;
}

/**
* Handle a metadata update and
* @param name
* @param updateHandler
* @param onWrite
* @param transformPackage
* @param onEnd
*/
updatePackage(name, updateHandler, onWrite, transformPackage, onEnd) {
/**
* Example of implementation:
this.customStore.get().then((pkg: Package) => {
updateHandler(pkg, function onUpdateFinish(err) {
if (err) {
onEnd(err);
} else {
onWrite(name, pkg, onEnd);
}
})
});
*/
}

/**
* Delete a specific file (tarball or package.json)
* @param fileName
* @param callback
*/
deletePackage(fileName, callback) {
/**
* Example of implementation:
this.customStore.delete(fileName, (err) => {
if (err) {
callback(err);
} else {
callback(null);
}
})
*/
}

/**
* Delete a package (folder, path)
* This happens after all versions ar tarballs have been removed.
* @param callback
*/
removePackage(callback) {
/**
* Example of implementation:
this.customStore.removePackage((err) => {
if (err) {
callback(err);
} else {
callback(null);
}
})
*/
}

/**
* Publish a new package (version).
* @param name
* @param data
* @param callback
*/
createPackage(name, metadata, cb) {
/**
* Example of implementation:
* this.customStore.create(name, data).then(err => {
if (err.notFound) {
callback(getNotFound());
} else if (err.alreadyExist) {
callback(getConflict());
} else {
callback(null);
}
})
*/
}

/**
* Perform write anobject to the storage.
* Similar to updatePackage but without middleware handlers
* @param pkgName package name
* @param pkg package metadata
* @param callback
*/
savePackage(name, value, cb) {
/*
Example of implementation:
this.cumstomStore.write(pkgName, pkgName).then(data => {
callback(null);
}).catch(err => {
callback(getInternalError(err.message));
})
*/
}

/**
* Read a package from storage
* @param pkgName package name
* @param callback
*/
readPackage(name, cb) {
/**
* Example of implementation:
* this.customStorage.read(name, (err, pkg: Package) => {
if (err.fooError) {
callback(getInternalError(err))
} else if (err.barError) {
callback(getNotFound());
} else {
callback(null, pkg)
}
});
*/
}

/**
* Create writtable stream (write a tarball)
* @param name
*/
writeTarball(name) {
/**
* Example of implementation:
* const stream = new UploadTarball({});
return stream;
*/
}

/**
* Create a readable stream (read a from a tarball)
* @param name
*/
readTarball(name) {
/**
* Example of implementation:
* const stream = new ReadTarball({});
return stream;
*/
}
};
36 changes: 1 addition & 35 deletions generators/app/templates/javascript/storage/src/index.js
Original file line number Diff line number Diff line change
@@ -1,35 +1 @@
class VerdaccioStoragePlugin {
constructor(config, options) {}

getSecret() {}

setSecret(secret) {}

/**
* Add a new element.
* @param {*} name
* @return {Error|*}
*/
add(name, cb) {}

search(onPackage, onEnd, validateName) {}

/**
* Remove an element from the database.
* @param {*} name
* @return {Error|*}
*/
remove(name, cb) {}

/**
* Return all database elements.
* @return {Array}
*/
get(cb) {}

getPackageStorage(packageName) {}
}

module.exports = (config, options) => {
return new VerdaccioStoragePlugin(config, options);
};
export { default } from './plugin';
106 changes: 106 additions & 0 deletions generators/app/templates/javascript/storage/src/plugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import PackageStorage from './PackageStorage';

class VerdaccioStoragePlugin {
constructor(config, options) {
this.config = config;
this.logger = options.logger;
}

async getSecret() {
/**
* return await resolveSecret();
*/
}

async setSecret(secret) {
/**
* return await getYourSecret();
*/
}

/**
* Add a new element.
* @param {*} name
* @return {Error|*}
*/
add(name, cb) {}

search(onPackage, onEnd, validateName) {
/**
* Example of implementation:
* try {
* someApi.getPackages((items) => {
* items.map(() => {
* if (validateName(item.name)) {
* onPackage(item);
* }
* });
* onEnd();
* } catch(err) {
* onEnd(err);
* }
* });
*/
}

/**
* Remove an element from the database.
* @param {*} name
* @return {Error|*}
*/
remove(name, callback) {
/**
* Example of implementation
database.getPackage(name, (item, err) => {
if (err) {
callback(getInternalError('your own message here'));
}

// if all goes well we return nothing
callback(null);
}
*/
}

/**
* Return all database elements.
* @return {Array}
*/
get(callback) {
/*
Example of implementation
database.getAll((allItems, err) => {
callback(err, allItems);
})
*/
}

/**
* Create an instance of the `PackageStorage`
* @param packageInfo
*/
getPackageStorage(packageInfo) {
return new PackageStorage(this.config, packageInfo, this.logger);
}

/**
* All methods for npm token support
* more info here https://github.com/verdaccio/verdaccio/pull/1427
*/

saveToken(token) {
throw new Error('Method not implemented.');
}

deleteToken(user, tokenKey) {
throw new Error('Method not implemented.');
}

readTokens(filter) {
throw new Error('Method not implemented.');
}
}

export default (config, options) => {
return new VerdaccioStoragePlugin(config, options);
};
19 changes: 0 additions & 19 deletions generators/app/templates/javascript/storage/src/storageManager.js

This file was deleted.

3 changes: 2 additions & 1 deletion generators/app/templates/typescript/auth/_package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"@typescript-eslint/eslint-plugin": "^2.12.0",
"@verdaccio/babel-preset": "^8.5.0",
"@verdaccio/eslint-config": "^8.5.0",
"@verdaccio/types": "^8.5.0",
"@verdaccio/types": "^8.5.2",
"eslint": "^6.6.0",
"jest": "^24.9.0",
"prettier": "^1.19.1",
Expand All @@ -30,6 +30,7 @@
"repository": "<%= repository %>",
"author": "<%= authorName %> <<%= authorEmail %>>",
"scripts": {
"release": "standard-version -a -s",
"build": "npm run build:types && npm run build:js",
"build:js": "babel src/ --out-dir lib --extensions \".ts,.tsx\"",
"build:types": "tsc --emitDeclarationOnly",
Expand Down
3 changes: 2 additions & 1 deletion generators/app/templates/typescript/middleware/_package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"@typescript-eslint/eslint-plugin": "^2.12.0",
"@verdaccio/babel-preset": "^8.5.0",
"@verdaccio/eslint-config": "^8.5.0",
"@verdaccio/types": "^8.5.0",
"@verdaccio/types": "^8.5.2",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was quick 🚀

"eslint": "^6.6.0",
"jest": "^24.9.0",
"prettier": "^1.19.1",
Expand All @@ -31,6 +31,7 @@
"repository": "<%= repository %>",
"author": "<%= authorName %> <<%= authorEmail %>>",
"scripts": {
"release": "standard-version -a -s",
"build": "npm run build:types && npm run build:js",
"build:js": "babel src/ --out-dir lib --extensions \".ts,.tsx\"",
"build:types": "tsc --emitDeclarationOnly",
Expand Down
Loading