google-cloud-bucket
Advanced tools
Comparing version 0.9.2 to 0.10.0
@@ -5,2 +5,12 @@ # Change Log | ||
<a name="0.10.0"></a> | ||
# [0.10.0](https://github.com/nicolasdao/google-cloud-bucket/compare/v0.9.2...v0.10.0) (2019-02-08) | ||
### Features | ||
* Add support for event handlers during the zipping process ([6872e77](https://github.com/nicolasdao/google-cloud-bucket/commit/6872e77)) | ||
<a name="0.9.2"></a> | ||
@@ -7,0 +17,0 @@ ## [0.9.2](https://github.com/nicolasdao/google-cloud-bucket/compare/v0.9.1...v0.9.2) (2019-01-28) |
50
index.js
@@ -134,9 +134,14 @@ /** | ||
* [description] | ||
* @param {[type]} bucket Source Bucket | ||
* @param {[type]} filePath Source path where the files are located | ||
* @param {String} options.to.local Destination in the local machine where the zip file will be stored | ||
* @param {String} options.to.bucket.name Destination bucket in the Google Cloud Storage machine where the zip file will be stored | ||
* @param {String} options.to.bucket.path Destination path in the destination bucket where the zip file will be stored | ||
* @param {String} options.ignore Array if strings or regex , or string or regex that will be ignored | ||
* @return {[type]} [description] | ||
* @param {[type]} bucket Source Bucket | ||
* @param {[type]} filePath Source path where the files are located | ||
* @param {String} options.to.local Destination in the local machine where the zip file will be stored | ||
* @param {String} options.to.bucket.name Destination bucket in the Google Cloud Storage machine where the zip file will be stored | ||
* @param {String} options.to.bucket.path Destination path in the destination bucket where the zip file will be stored | ||
* @param {String} options.ignore Array if strings or regex , or string or regex that will be ignored | ||
* @param {String} options.eventHandlers['files-listed'] | ||
* @param {String} options.eventHandlers['file-received'] | ||
* @param {String} options.eventHandlers['finished'] | ||
* @param {String} options.eventHandlers['saved'] | ||
* @param {String} options.eventHandlers['error'] | ||
* @return {[type]} [description] | ||
*/ | ||
@@ -146,2 +151,16 @@ const zipFiles = (bucket, filePath, options) => listObjects(bucket, filePath, options) | ||
objects = objects || [] | ||
options = options || {} | ||
const totalSize = objects.reduce((s,{size}) => { | ||
const fileSize = size*1 | ||
return !isNaN(fileSize) ? (s+fileSize) : s | ||
}, 0) | ||
const eventHandlers = options.eventHandlers || {} | ||
const onFilesListed = eventHandlers['files-listed'] || (() => null) | ||
const onFileReceived = eventHandlers['file-received'] || (() => null) | ||
const onFilesZipped = eventHandlers['finished'] || (() => null) | ||
const onFilesSaved = eventHandlers['saved'] || (() => null) | ||
onFilesListed({ count: objects.length, size: totalSize, data:objects }) | ||
if (options.ignore) { | ||
@@ -161,3 +180,2 @@ if (typeof(options.ignore) == 'string') | ||
} | ||
options = options || {} | ||
@@ -182,3 +200,5 @@ if (options.to && options.to.bucket && options.to.bucket.path && !/\.zip$/.test(options.to.bucket.path)) | ||
return retryGetObject(objName, { streamReader, timeout: 55 * 60 * 1000 }).then(() => { | ||
archive.append(Buffer.concat(chunks), { name: obj.name }) | ||
const b = Buffer.concat(chunks) | ||
onFileReceived({ file: obj.name, size: b.length }) | ||
archive.append(b, { name: obj.name }) | ||
chunks = null | ||
@@ -191,2 +211,3 @@ }) | ||
.then(b => { | ||
onFilesZipped({ size:b.length }) | ||
const to = options.to | ||
@@ -208,3 +229,14 @@ if (to) { | ||
}) | ||
.then(res => { | ||
onFilesSaved(res) | ||
return res | ||
}) | ||
}) | ||
.catch(err => { | ||
const onError = ((options || {}).eventHandlers || {})['error'] | ||
if (onError) | ||
onError(err) | ||
else | ||
throw err | ||
}) | ||
@@ -211,0 +243,0 @@ return { |
{ | ||
"name": "google-cloud-bucket", | ||
"version": "0.9.2", | ||
"version": "0.10.0", | ||
"description": "Nodejs package to add objects to a Google Cloud Bucket.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -285,4 +285,4 @@ # Google Cloud Bucket · [![NPM](https://img.shields.io/npm/v/google-cloud-bucket.svg?style=flat)](https://www.npmjs.com/package/google-cloud-bucket) [![Tests](https://travis-ci.org/nicolasdao/google-cloud-bucket.svg?branch=master)](https://travis-ci.org/nicolasdao/google-cloud-bucket) [![License](https://img.shields.io/badge/License-BSD%203--Clause-blue.svg)](https://opensource.org/licenses/BSD-3-Clause) [![Neap](https://neap.co/img/made_by_neap.svg)](#this-is-what-we-re-up-to) | ||
bucket: { | ||
name: 'another-existing-bucket-name', // Optional (default: Source bucket. In our example, that source bucket is 'your-bucket-name') | ||
path: 'some-folder-path.zip' // Optional (default: 'archive.zip'). If specified, must have the '.zip' extension. | ||
name: 'another-existing-bucket-name', // Optional (default: Source bucket. In our example, that source bucket is 'your-bucket-name') | ||
path: 'some-folder-path.zip' // Optional (default: 'archive.zip'). If specified, must have the '.zip' extension. | ||
} | ||
@@ -300,2 +300,45 @@ }, | ||
__*Extra Options*__ | ||
You can also track the various steps of the zipping process with the optional `eventHandlers` object: | ||
```js | ||
const bucket = storage.bucket('your-bucket-name') | ||
bucket.object('some-folder-path').zip({ | ||
to: { | ||
local: 'some-path-on-your-local-machine', | ||
bucket: { | ||
name: 'another-existing-bucket-name', // Optional (default: Source bucket. In our example, that source bucket is 'your-bucket-name') | ||
path: 'some-folder-path.zip' // Optional (default: 'archive.zip'). If specified, must have the '.zip' extension. | ||
} | ||
}, | ||
eventHandlers:{ | ||
'files-listed': (files) => { | ||
console.log(`Total number of files to be zipped: ${files.count}`) | ||
console.log(`Raw size: ${(files.size/1024/1024).toFixed(1)} MB`) | ||
// 'files.data' is an array of all the files' details | ||
}, | ||
'file-received': ({ file, size }) => { | ||
console.log(`File ${file} (byte size: ${size}) is being zipped`) | ||
}, | ||
'finished': ({ size }) => { | ||
console.log(`Zip process completed. The zip file's size is ${size} bytes`) | ||
}, | ||
'saved': () => { | ||
console.log('The zipped file has been saved') | ||
}, | ||
'error': err => { | ||
console.log(`${err.message}\n${err.stack}`) | ||
} | ||
} | ||
}) | ||
.then(({ count, data }) => { | ||
console.log(`${count} files have been zipped`) | ||
if (data) | ||
// 'data' is null if the 'options.to' is defined | ||
console.log(`The zip file's size is: ${data.length/1024} KB`) | ||
}) | ||
``` | ||
## Extra Precautions To Make Robust Queries | ||
@@ -302,0 +345,0 @@ ### Avoiding Network Errors |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
87642
1406
448