Skip to content

Commit 0487662

Browse files
committed
2.0.1
1 parent 9516aec commit 0487662

11 files changed

+1532
-89
lines changed

.eslintrc.js

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
module.exports = {
2+
"env": {
3+
"browser": false,
4+
"node": true,
5+
"commonjs": true,
6+
"es6": true
7+
},
8+
"extends": "eslint:recommended",
9+
"parserOptions": {
10+
"sourceType": "module",
11+
"ecmaVersion": 6,
12+
"ecmaFeatures": {
13+
"experimentalObjectRestSpread": true
14+
}
15+
},
16+
"rules": {
17+
"indent": [
18+
"error",
19+
2
20+
],
21+
"linebreak-style": [
22+
"error",
23+
"unix"
24+
],
25+
"quotes": [
26+
"error",
27+
"double"
28+
],
29+
"semi": [
30+
"error",
31+
"never"
32+
],
33+
"no-console": 0,
34+
"no-process-env": 0,
35+
}
36+
};

changelog.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
11
# Changelog
2+
## 2.0.1
3+
- Added eslint support.
4+
- Updated formating.
5+
- Added detailed comments.
6+
- Added debugMode feature.
7+
28
## 2.0.0
3-
- This is the first offical release. No "changes" for this release
9+
- This is the first offical release. No "changes" for this release.

example/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,6 @@ const files = [
2121
]
2222

2323
// upload the files
24-
dropboxSessionUpload(files, process.env.DROPBOXTOKEN)
24+
dropboxSessionUpload(files, process.env.DROPBOXTOKEN, true /* debug mode, defaults to false */)
2525
.catch(error => console.log(error))
2626
.then(() => console.log("Done Uploading!"))

index.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,2 @@
1-
'use strict'
2-
3-
// export
4-
module.exports = require('./lib/index')
1+
// exposes the entry point for the package
2+
module.exports = require("./lib/index")

lib/dropbox.js

+46-44
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class DropboxUploadStream extends Transform {
88
// super options
99
super(options)
1010

11-
// setup options
11+
// setup options (apiKey, debugMode)
1212
this.options = options
1313

1414
// setup session and offset information
@@ -20,81 +20,83 @@ class DropboxUploadStream extends Transform {
2020
}
2121

2222
_transform(chunk, encoding, next) {
23-
24-
if (!this.sessionId) {
23+
// check if session is set
24+
if (this.sessionId) {
25+
// call sessionAppend if session already exists
26+
this.sessionAppend(chunk, next)
27+
} else {
28+
// return the session start if it does not exists
2529
return this.sessionStart(chunk, next)
2630
}
27-
28-
this.sessionAppend(chunk, next)
2931
}
3032

3133
_flush(next) {
34+
// finish the session on flush
3235
this.sessionFinish(next)
3336
}
3437

35-
/**
36-
* Starts a dropbox session
37-
*
38-
* @param chunk
39-
* @param next
40-
*/
4138
sessionStart(chunk, next) {
39+
// start the session on dropbox, sending the first chunk
4240
this.dropbox.filesUploadSessionStart({
4341
close: false,
4442
contents: chunk
45-
})
46-
.then((response) => {
47-
this.sessionId = response.session_id
48-
this.offset += chunk.byteLength
49-
return next()
50-
}, next)
43+
}).then(response => {
44+
// set the session id
45+
this.sessionId = response.session_id
46+
47+
// update the offset
48+
this.offset += chunk.byteLength
49+
50+
// return next
51+
return next()
52+
}, next /* call next if there is an error*/)
5153
}
5254

53-
/**
54-
* Appends data to an open dropbox session
55-
*
56-
* @param chunk
57-
* @param next
58-
*/
5955
sessionAppend(chunk, next) {
56+
// append chunk to a session that has already been started
6057
this.dropbox.filesUploadSessionAppendV2({
6158
cursor: {
62-
session_id: this.sessionId,
63-
offset: this.offset
59+
session_id: this.sessionId, // id for this upload session
60+
offset: this.offset // offset, current location of chunk
6461
},
6562
close: false,
66-
contents: chunk
63+
contents: chunk // current chunk being uploaded
6764
})
6865
.then(() => {
66+
// set the new offset
6967
this.offset += chunk.byteLength
68+
69+
// were done, next
7070
next()
71-
}, next)
71+
}, next /* call next if there is an error*/)
7272
}
7373

74-
/**
75-
* Closes the session and commits the file(s)
76-
*
77-
* @param next
78-
*/
7974
sessionFinish(next) {
75+
// let dropbox know we are finished uploading
8076
this.dropbox.filesUploadSessionFinish({
8177
"cursor": {
82-
"session_id": this.sessionId,
83-
"offset": this.offset
78+
"session_id": this.sessionId, // id for this upload session
79+
"offset": this.offset // offset, current location of chunk
8480
},
8581
"commit": {
86-
"path": this.options.saveLocation,
87-
"mode": "add",
88-
"autorename": true,
89-
"mute": false
82+
"path": this.options.saveLocation, // path to save thew new file
83+
"mode": "add", // add mode, this will NOT overwrite an existing file
84+
"autorename": true, // if there is duplicates rename ex. file(2).txt
85+
"mute": false // show notifications to db users that files were changed
9086
}
91-
})
92-
.then(() => {
87+
}).then(() => {
88+
// we are finished uploading the file
89+
if (this.options.debugMode) {
9390
console.log(`Filed Uploaded for save location ${this.options.saveLocation}`)
94-
this.sessionId = null
95-
this.offset = 0
96-
next()
97-
}, next)
91+
}
92+
93+
// reset sessionId and offset
94+
this.sessionId = null
95+
this.offset = 0
96+
97+
// trigger next because we are done
98+
next()
99+
}, next /* call next if there is an error*/)
98100
}
99101
}
100102

lib/index.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
"use strict"
2-
3-
// Import Required Functions
1+
// import upload function
42
const upload = require("./upload")
53

6-
// Uploader Function
7-
const dropboxSessionUpload = (files, apiKey) =>
4+
// returns a Promise.all(uploads) where
5+
// the uploads is an array of promises
6+
const dropboxSessionUpload = (files, apiKey, debugMode=false) =>
87
// wait for all files to be uploaded
9-
Promise.all(files.map(f => upload({...f, apiKey})))
8+
Promise.all(files.map(f => upload({ ...f, apiKey, debugMode})))
109

10+
// export the function
1111
module.exports = dropboxSessionUpload

lib/transform.js

+35-15
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,66 @@
1-
const debug = require("debug")("stream-to-dropbox:transform-stream")
1+
// import modules
22
const { Transform } = require("stream")
33

44
class TransformStream extends Transform {
55
constructor(options) {
6+
// super options
67
super(options)
78

8-
// setup options
9+
// setup options (apiKey, debugMode)
910
this.options = options
1011

1112
// setup chunk size of 1mb
1213
this.chunkSize = options.chunkSize || 1 * 1024 * 1024 // 1 MB
1314
}
1415

15-
// Buffer the input to reach minimal size of options.chunkSize
16+
// buffer the input to reach minimal size of options.chunkSize
1617
checkBuffer(chunk) {
17-
if (!this.buffer) {
18-
this.buffer = Buffer.from(chunk)
19-
} else {
18+
// check if buffer is already set
19+
if (this.buffer) {
20+
// conact a chunk to the existing buffer
2021
this.buffer = Buffer.concat([this.buffer, chunk])
22+
} else {
23+
// no buffer exists, set a new one
24+
this.buffer = Buffer.from(chunk)
2125
}
2226

27+
// return true if buffer length is greater or equal to the chunk size
2328
return this.buffer.byteLength >= this.chunkSize
2429
}
2530

2631
_transform(chunk, encoding, next) {
27-
// If buffer is too small, wait for more chunks
28-
if (!this.checkBuffer(chunk)) {
32+
// if buffer is too small, wait for more chunks
33+
if (this.checkBuffer(chunk)) {
34+
// chunk is big enough to save
35+
// saving message
36+
if (this.options.debugMode) {
37+
console.log(`Passing buffer (size: ${Math.round(this.buffer.byteLength / 1024)} KB) for save location ${this.options.saveLocation}`)
38+
}
39+
40+
// pass the buffer to be saved
41+
next(null, this.buffer)
42+
43+
// Clear the buffer
44+
this.buffer = undefined
45+
} else {
46+
// buffer is to small
2947
return next()
3048
}
31-
32-
// Chunk is big enough
33-
console.log(`Passing buffer (size: ${Math.round(this.buffer.byteLength / 1024)} KB) for save location ${this.options.saveLocation}`)
34-
next(null, this.buffer)
35-
// Clear the buffer
36-
this.buffer = undefined
3749
}
3850

3951
_flush(next) {
40-
console.log(`Passing buffer (size: ${Math.round(this.buffer.byteLength / 1024)} KB) for save location ${this.options.saveLocation}`)
52+
// save message
53+
if (this.options.debugMode) {
54+
console.log(`Passing buffer (size: ${Math.round(this.buffer.byteLength / 1024)} KB) for save location ${this.options.saveLocation}`)
55+
}
56+
57+
// pass the buffer to be saved
4158
next(null, this.buffer)
59+
60+
// clear the buffer
4261
this.buffer = undefined
4362
}
4463
}
4564

65+
// export module
4666
module.exports = TransformStream

lib/upload.js

+24-15
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,32 @@
1-
"use strict"
2-
3-
// Import Required Classes
1+
// import required classes
42
const TransformStream = require("./transform")
53
const DropboxUploadStream = require("./dropbox")
64

7-
// Uploader Function
8-
const upload = (data) =>
5+
// return a promise that is resolved when the file
6+
// is successfully uploaded or rejected when
7+
// there is an error
8+
const upload = data =>
99
new Promise((resolve, reject) => {
10-
// Prepare the Streams
11-
const transformStream = new TransformStream({ ...data, chunkSize: 8000 * 1024 })
10+
// setup transform stream with the upload data and a 8mb chunk size
11+
const transformStream =
12+
new TransformStream({ ...data, chunkSize: 8000 * 1024 })
13+
14+
// setup dropbox stream with the upload data
1215
const dropboxUpload = new DropboxUploadStream(data)
1316

14-
// pipe to the file
15-
data.file.pipe(transformStream).pipe(dropboxUpload)
16-
.on("error", (err) => {
17-
reject(err)
18-
})
19-
.on("finish", () => {
20-
resolve()
21-
})})
17+
// pipe our file
18+
data.file
19+
// pipe in transform and dropbox upload
20+
.pipe(transformStream)
21+
.pipe(dropboxUpload)
22+
23+
// event listeners
24+
.on("error", err =>
25+
// error, reject and return the error
26+
reject(err))
27+
.on("finish", () =>
28+
// no error, resolve
29+
resolve())})
2230

31+
// export upload module
2332
module.exports = upload

0 commit comments

Comments
 (0)