gcs-resumable-upload
Advanced tools
Comparing version 0.1.0 to 0.2.0
50
index.js
@@ -44,4 +44,13 @@ 'use strict' | ||
this.once('writing', function () { | ||
if (self.uri) self.continueUploading() | ||
else self.createResumableUpload() | ||
if (self.uri) { | ||
self.continueUploading() | ||
} else { | ||
self.createURI(function (err, uri) { | ||
if (err) return self.destroy(err) | ||
self.uri = uri | ||
self.set({ uri: uri }) | ||
self.offset = 0 | ||
self.startUploading() | ||
}) | ||
} | ||
}) | ||
@@ -52,4 +61,8 @@ } | ||
Upload.prototype.createResumableUpload = function () { | ||
var self = this | ||
Upload.createURI = function (cfg, callback) { | ||
var up = new Upload(cfg) | ||
up.createURI(callback) | ||
} | ||
Upload.prototype.createURI = function (callback) { | ||
var metadata = this.metadata | ||
@@ -78,9 +91,4 @@ | ||
this.makeRequest(reqOpts, function (err, resp) { | ||
if (err) return | ||
self.uri = resp.headers.location | ||
self.set({ uri: self.uri }) | ||
self.offset = 0 | ||
self.startUploading() | ||
if (err) return callback(err) | ||
callback(null, resp.headers.location) | ||
}) | ||
@@ -117,5 +125,3 @@ } | ||
try { | ||
body = JSON.parse(body) | ||
} catch (e) {} | ||
try { body = JSON.parse(body) } catch (e) {} | ||
@@ -188,3 +194,3 @@ self.emit('response', resp, body) | ||
}, function (err, resp) { | ||
if (err) return | ||
if (err) return self.destroy(err) | ||
@@ -205,12 +211,12 @@ if (resp.statusCode === RESUMABLE_INCOMPLETE_STATUS_CODE) { | ||
Upload.prototype.makeRequest = function (reqOpts, callback) { | ||
var self = this | ||
this.authClient.authorizeRequest(reqOpts, function (err, authorizedReqOpts) { | ||
if (err) return self.destroy(err) | ||
if (err) return callback(err) | ||
request(authorizedReqOpts, function (err, resp, body) { | ||
if (err) return self.destroy(err) | ||
var shouldContinue = self.onResponse(resp) | ||
if (shouldContinue) callback(err, resp, body) | ||
var error = err | ||
try { body = JSON.parse(body) } catch (e) {} | ||
if (resp.statusCode < 200 || resp.statusCode > 299) error = new Error(resp.body) | ||
if (body && body.error) error = body.error | ||
if (error) return callback(error) | ||
callback(null, resp, body) | ||
}) | ||
@@ -217,0 +223,0 @@ }) |
{ | ||
"name": "gcs-resumable-upload", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"description": "Upload a file to Google Cloud Storage with built-in resumable behavior", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -45,2 +45,4 @@ # gcs-resumable-upload | ||
--- | ||
#### upload(config) | ||
@@ -50,4 +52,11 @@ | ||
##### config.authClient | ||
<a name="config"></a> | ||
##### config | ||
- Type: `Object` | ||
Configuration object. | ||
###### config.authClient | ||
- Type: [`GoogleAutoAuth`](http://gitnpm.com/google-auto-auth) | ||
@@ -58,3 +67,3 @@ - *Optional* | ||
##### config.authConfig | ||
###### config.authConfig | ||
@@ -66,3 +75,3 @@ - Type: `object` | ||
##### config.bucket | ||
###### config.bucket | ||
@@ -74,3 +83,3 @@ - Type: `string` | ||
##### config.file | ||
###### config.file | ||
@@ -82,3 +91,3 @@ - Type: `string` | ||
##### config.generation | ||
###### config.generation | ||
@@ -90,3 +99,3 @@ - Type: `number` | ||
##### config.metadata | ||
###### config.metadata | ||
@@ -98,7 +107,7 @@ - Type: `object` | ||
###### config.metadata.contentType | ||
###### *config.metadata.contentType* | ||
Set the content type of the incoming data. | ||
##### config.uri | ||
###### config.uri | ||
@@ -110,8 +119,10 @@ - Type: `String` | ||
## Events | ||
-- | ||
#### .on('error', function (err) {}) | ||
#### Events | ||
##### err | ||
##### .on('error', function (err) {}) | ||
###### err | ||
- Type: `Error` | ||
@@ -121,5 +132,5 @@ | ||
#### .on('response', function (resp, metadata) {}) | ||
##### .on('response', function (resp, metadata) {}) | ||
##### resp | ||
###### resp | ||
@@ -130,3 +141,3 @@ - Type: `Object` | ||
##### metadata | ||
###### metadata | ||
@@ -137,4 +148,22 @@ - Type: `Object` | ||
#### .on('finish', function () {}) | ||
##### .on('finish', function () {}) | ||
The file was uploaded successfully. | ||
--- | ||
#### upload.createURI([config](#config), callback) | ||
##### callback(err, resumableURI) | ||
###### callback.err | ||
- Type: `Error` | ||
Invoked if the authorization failed or the request to start a resumable session failed. | ||
###### callback.resumableURI | ||
- Type: `String` | ||
The resumable upload session URI. |
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
12765
242
158