Skip to content

Commit

Permalink
feat: add file validation configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
@jotadeveloper authored and griffithtp committed Aug 3, 2019
1 parent da6f19e commit bcd46c5
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 14 deletions.
3 changes: 3 additions & 0 deletions plugins/verdaccio-google-cloud/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ store:
## google cloud recomend this file only for development
## this field is not mandatory
keyFilename: /path/project-01.json || env (GOOGLE_CLOUD_VERDACCIO_KEY)
## default validation is, it can be overrided by
## https://cloud.google.com/nodejs/docs/reference/storage/1.6.x/File.html#createWriteStream
# validation: crc32c
```
Define `env` whether you want load the value from environment variables.

Expand Down
37 changes: 24 additions & 13 deletions plugins/verdaccio-google-cloud/src/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import type { ConfigGoogleStorage } from '../types';
export const noSuchFile: string = 'ENOENT';
export const fileExist: string = 'EEXISTS';
export const pkgFileName = 'package.json';
export const defaultValidation = 'crc32c';

declare type StorageType = Package | void;

Expand Down Expand Up @@ -91,7 +92,9 @@ class GoogleCloudStorageHandler implements ILocalPackageManager {
this.logger.debug({ name: file.name }, 'gcloud: deleting @{name} from storage');
try {
file
.delete()
.delete({
validation: this.config.validation || defaultValidation
})
.then(data => {
const apiResponse = data[0];
this.logger.debug({ name: file.name }, 'gcloud: @{name} was deleted successfully from storage');
Expand All @@ -111,16 +114,20 @@ class GoogleCloudStorageHandler implements ILocalPackageManager {
// remove all files from storage
const file = this._getBucket().file(`${this.name}`);
this.logger.debug({ name: file.name }, 'gcloud: removing the package @{name} from storage');
file.delete().then(
() => {
this.logger.debug({ name: file.name }, 'gcloud: package @{name} was deleted successfully from storage');
callback(null);
},
err => {
this.logger.error({ name: file.name, err: err.message }, 'gcloud: delete @{name} package has failed err: @{err}');
callback(fSError(err.message, 500));
}
);
file
.delete({
validation: this.config.validation || defaultValidation
})
.then(
() => {
this.logger.debug({ name: file.name }, 'gcloud: package @{name} was deleted successfully from storage');
callback(null);
},
err => {
this.logger.error({ name: file.name, err: err.message }, 'gcloud: delete @{name} package has failed err: @{err}');
callback(fSError(err.message, 500));
}
);
}

createPackage(name: string, metadata: Object, cb: Function): void {
Expand Down Expand Up @@ -159,7 +166,9 @@ class GoogleCloudStorageHandler implements ILocalPackageManager {
return new Promise(async (resolve, reject) => {
const file = this._buildFilePath(name, pkgFileName);
try {
await file.save(this._convertToString(metadata));
await file.save(this._convertToString(metadata), {
validation: this.config.validation || defaultValidation
});
resolve(null);
} catch (err) {
reject(fSError(err.message, 500));
Expand Down Expand Up @@ -224,7 +233,9 @@ class GoogleCloudStorageHandler implements ILocalPackageManager {
} else {
const file = this._getBucket().file(`${this.name}/${name}`);
this.logger.info({ url: file.name }, 'gcloud: the @{url} is being uploaded to the storage');
const fileStream = file.createWriteStream();
const fileStream = file.createWriteStream({
validation: this.config.validation || defaultValidation
});
uploadStream.done = () => {
uploadStream.on('end', () => {
fileStream.on('response', () => {
Expand Down
4 changes: 3 additions & 1 deletion plugins/verdaccio-google-cloud/types/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ export type ConfigGoogleStorage = {
// https://cloud.google.com/datastore/docs/reference/data/rest/v1/Key
kind: string,
// for local development
keyFilename?: string
keyFilename?: string,
// disable bucket validation
validation?: boolean | string
};

export type GoogleCloudOptions = {
Expand Down

0 comments on commit bcd46c5

Please sign in to comment.