Skip to content

Commit c25c344

Browse files
committed
Implemented Prettier
1 parent c798116 commit c25c344

10 files changed

+1252
-85
lines changed

changelog.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
# Changelog
2+
## 2.1.3
3+
- Implemented Prettier.
4+
- Formated files with Prettier.
5+
26
## 2.1.2
37
- Documentation Markdown fix.
48

examples/progressTracking.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,18 @@ const files = [
77
{
88
file: fs.createReadStream("./datafile.txt"),
99
saveLocation: "/datafile1.txt",
10-
id: "1" // required for progress
10+
id: "1", // required for progress
1111
},
1212
{
1313
file: fs.createReadStream("./datafile.txt"),
1414
saveLocation: "/datafile1.txt",
15-
id: "2" // required for progress
15+
id: "2", // required for progress
1616
},
1717
{
1818
file: fs.createReadStream("./datafile.txt"),
1919
saveLocation: "/datafile1.txt",
20-
id: "3" // required for progress
21-
}
20+
id: "3", // required for progress
21+
},
2222
]
2323

2424
// upload the files
@@ -27,4 +27,4 @@ upload(files, process.env.DROPBOXTOKEN)
2727
.then(() => console.log("Files Done!"))
2828

2929
// listen for updates to the progress
30-
progress(data => console.log(data))
30+
progress(data => console.log(data))

examples/simple.js

+11-7
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,28 @@
11
// import modules
22
const fs = require("fs")
3-
const {upload} = require("..")
3+
const { upload } = require("..")
44

55
// setup files to upload
66
const files = [
77
{
88
file: fs.createReadStream("./datafile.txt"),
9-
saveLocation: "/datafile1.txt"
9+
saveLocation: "/datafile1.txt",
1010
},
1111
{
1212
file: fs.createReadStream("./datafile.txt"),
13-
saveLocation: "/datafile2.txt"
13+
saveLocation: "/datafile2.txt",
1414
},
1515
{
1616
file: fs.createReadStream("./datafile.txt"),
17-
saveLocation: "/datafile3.txt"
18-
}
17+
saveLocation: "/datafile3.txt",
18+
},
1919
]
2020

2121
// upload the files
22-
upload(files, process.env.DROPBOXTOKEN, true /* debug mode, defaults to false */)
22+
upload(
23+
files,
24+
process.env.DROPBOXTOKEN,
25+
true /* debug mode, defaults to false */
26+
)
2327
.catch(error => console.log(error))
24-
.then(() => console.log("Done Uploading!"))
28+
.then(() => console.log("Done Uploading!"))

lib/dropbox.js

+60-54
Original file line numberDiff line numberDiff line change
@@ -42,31 +42,34 @@ class DropboxUploadStream extends Transform {
4242

4343
sessionStart(chunk, next) {
4444
// start the session on dropbox, sending the first chunk
45-
this.dropbox.filesUploadSessionStart({
46-
close: false,
47-
contents: chunk
48-
}).then(response => {
49-
// set the session id
50-
this.sessionId = response.session_id
51-
52-
// update the offset
53-
this.offset += chunk.byteLength
54-
55-
// return next
56-
return next()
57-
}, next /* call next if there is an error*/)
45+
this.dropbox
46+
.filesUploadSessionStart({
47+
close: false,
48+
contents: chunk,
49+
})
50+
.then(response => {
51+
// set the session id
52+
this.sessionId = response.session_id
53+
54+
// update the offset
55+
this.offset += chunk.byteLength
56+
57+
// return next
58+
return next()
59+
}, next /* call next if there is an error*/)
5860
}
5961

6062
sessionAppend(chunk, next) {
6163
// append chunk to a session that has already been started
62-
this.dropbox.filesUploadSessionAppendV2({
63-
cursor: {
64-
session_id: this.sessionId, // id for this upload session
65-
offset: this.offset // offset, current location of chunk
66-
},
67-
close: false,
68-
contents: chunk // current chunk being uploaded
69-
})
64+
this.dropbox
65+
.filesUploadSessionAppendV2({
66+
cursor: {
67+
session_id: this.sessionId, // id for this upload session
68+
offset: this.offset, // offset, current location of chunk
69+
},
70+
close: false,
71+
contents: chunk, // current chunk being uploaded
72+
})
7073
.then(() => {
7174
// set the new offset
7275
this.offset += chunk.byteLength
@@ -79,31 +82,35 @@ class DropboxUploadStream extends Transform {
7982

8083
sessionFinish(next) {
8184
// let dropbox know we are finished uploading
82-
this.dropbox.filesUploadSessionFinish({
83-
"cursor": {
84-
"session_id": this.sessionId, // id for this upload session
85-
"offset": this.offset // offset, current location of chunk
86-
},
87-
"commit": {
88-
"path": this.options.saveLocation, // path to save thew new file
89-
"mode": "add", // add mode, this will NOT overwrite an existing file
90-
"autorename": true, // if there is duplicates rename ex. file(2).txt
91-
"mute": false // show notifications to db users that files were changed
92-
}
93-
}).then(() => {
94-
// we are finished uploading the file
95-
if (this.options.debugMode) {
96-
console.log(`Filed Uploaded for save location ${this.options.saveLocation}`)
97-
}
98-
99-
// reset variables
100-
this.sessionId = null
101-
this.offset = 0
102-
this.totalSize = null
103-
104-
// trigger next because we are done
105-
next()
106-
}, next /* call next if there is an error*/)
85+
this.dropbox
86+
.filesUploadSessionFinish({
87+
cursor: {
88+
session_id: this.sessionId, // id for this upload session
89+
offset: this.offset, // offset, current location of chunk
90+
},
91+
commit: {
92+
path: this.options.saveLocation, // path to save thew new file
93+
mode: "add", // add mode, this will NOT overwrite an existing file
94+
autorename: true, // if there is duplicates rename ex. file(2).txt
95+
mute: false, // show notifications to db users that files were changed
96+
},
97+
})
98+
.then(() => {
99+
// we are finished uploading the file
100+
if (this.options.debugMode) {
101+
console.log(
102+
`Filed Uploaded for save location ${this.options.saveLocation}`
103+
)
104+
}
105+
106+
// reset variables
107+
this.sessionId = null
108+
this.offset = 0
109+
this.totalSize = null
110+
111+
// trigger next because we are done
112+
next()
113+
}, next /* call next if there is an error*/)
107114
}
108115

109116
caluclateProgress() {
@@ -112,20 +119,19 @@ class DropboxUploadStream extends Transform {
112119
// size is set, calculate percentage and emit and event
113120
this.options.progressEvent.emit("change", {
114121
id: this.options.id,
115-
percentage: Math.floor((this.offset / this.totalSize) * 100)
122+
percentage: Math.floor(this.offset / this.totalSize * 100),
116123
})
117124
}
118125
}
119126

120127
createTotalSize() {
121-
streamLength(this.options.file)
122-
.then(size => {
123-
// set the total size
124-
this.totalSize = size
128+
streamLength(this.options.file).then(size => {
129+
// set the total size
130+
this.totalSize = size
125131

126-
// execute calculate size for initial calculation
127-
this.caluclateProgress()
128-
})
132+
// execute calculate size for initial calculation
133+
this.caluclateProgress()
134+
})
129135
}
130136
}
131137

lib/index.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ const progressEvent = new EventEmitter()
77

88
// returns a Promise.all(uploads) where
99
// the uploads is an array of promises
10-
const upload = (files, apiKey, debugMode=false) =>
10+
const upload = (files, apiKey, debugMode = false) =>
1111
// wait for all files to be uploaded
12-
Promise.all(files.map(f =>
13-
uploader({ ...f, apiKey, debugMode, progressEvent})))
12+
Promise.all(
13+
files.map(f => uploader({ ...f, apiKey, debugMode, progressEvent }))
14+
)
1415

1516
// progress, uses a callback to send a status of progress
16-
const progress = callback =>
17-
progressEvent.on("change", data => callback(data))
17+
const progress = callback => progressEvent.on("change", data => callback(data))
1818

1919
// export the functions
20-
module.exports = { upload, progress }
20+
module.exports = { upload, progress }

lib/transform.js

+10-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,11 @@ class TransformStream extends Transform {
3434
// chunk is big enough to save
3535
// saving message
3636
if (this.options.debugMode) {
37-
console.log(`Passing buffer (size: ${Math.round(this.buffer.byteLength / 1024)} KB) for save location ${this.options.saveLocation}`)
37+
console.log(
38+
`Passing buffer (size: ${Math.round(
39+
this.buffer.byteLength / 1024
40+
)} KB) for save location ${this.options.saveLocation}`
41+
)
3842
}
3943

4044
// pass the buffer to be saved
@@ -51,7 +55,11 @@ class TransformStream extends Transform {
5155
_flush(next) {
5256
// save message
5357
if (this.options.debugMode) {
54-
console.log(`Passing buffer (size: ${Math.round(this.buffer.byteLength / 1024)} KB) for save location ${this.options.saveLocation}`)
58+
console.log(
59+
`Passing buffer (size: ${Math.round(
60+
this.buffer.byteLength / 1024
61+
)} KB) for save location ${this.options.saveLocation}`
62+
)
5563
}
5664

5765
// pass the buffer to be saved

lib/uploader.js

+12-8
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@ const DropboxUploadStream = require("./dropbox")
55
// return a promise that is resolved when the file
66
// is successfully uploaded or rejected when
77
// there is an error
8-
const uploader = data =>
8+
const uploader = data =>
99
new Promise((resolve, reject) => {
1010
// setup transform stream with the upload data and a 8mb chunk size
11-
const transformStream =
12-
new TransformStream({ ...data, chunkSize: 8000 * 1024 })
11+
const transformStream = new TransformStream({
12+
...data,
13+
chunkSize: 8000 * 1024,
14+
})
1315

1416
// setup dropbox stream with the upload data
1517
const dropboxUpload = new DropboxUploadStream(data)
@@ -19,15 +21,17 @@ const uploader = data =>
1921
// pipe in transform and dropbox upload
2022
.pipe(transformStream)
2123
.pipe(dropboxUpload)
22-
2324
// event listeners
24-
.on("error", err =>
25+
.on("error", err =>
2526
// error, reject and return the error
26-
reject(err))
27+
reject(err)
28+
)
2729
.on("data", d => console.log("DATA ", d))
28-
.on("finish", () =>
30+
.on("finish", () =>
2931
// no error, resolve
30-
resolve())})
32+
resolve()
33+
)
34+
})
3135

3236
// export upload module
3337
module.exports = uploader

0 commit comments

Comments
 (0)