files.com 1.0.256 → 1.0.257

Sign up to get free protection for your applications and to get access to all the features.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "files.com",
3
- "version": "1.0.256",
3
+ "version": "1.0.257",
4
4
  "description": "Files.com SDK for JavaScript",
5
5
  "keywords": [
6
6
  "files.com",
@@ -21,6 +21,19 @@ const saveUrlToStream = async (url, stream) => new Promise((resolve, reject) =>
21
21
  })
22
22
  })
23
23
 
24
+ const saveUrlToString = async url => new Promise((resolve, reject) => {
25
+ const https = require('https')
26
+
27
+ https.get(url, response => {
28
+ const chunks = []
29
+ response.on('data', chunk => chunks.push(Buffer.from(chunk)))
30
+ response.on('end', () => resolve(Buffer.concat(chunks).toString('utf8')))
31
+ })
32
+ .on('error', error => {
33
+ reject(error)
34
+ })
35
+ })
36
+
24
37
  const saveUrlToFile = async (url, destinationPath) => {
25
38
  const stream = openDiskFileWriteStream(destinationPath)
26
39
  await saveUrlToStream(url, stream)
@@ -32,4 +45,5 @@ export {
32
45
  openDiskFileWriteStream,
33
46
  saveUrlToFile,
34
47
  saveUrlToStream,
48
+ saveUrlToString,
35
49
  }
@@ -199,6 +199,21 @@ class File {
199
199
  return saveUrlToStream(downloadUri, writableStream)
200
200
  }
201
201
 
202
+ downloadToString = async () => {
203
+ if (isBrowser()) {
204
+ throw new errors.NotImplementedError('String downloads are only available in a NodeJS environment')
205
+ }
206
+
207
+ const downloadUri = this.getDownloadUri()
208
+
209
+ if (!downloadUri) {
210
+ throw new errors.EmptyPropertyError('Current object has no download URI')
211
+ }
212
+
213
+ const { saveUrlToString } = require('../isomorphic/File.node.js')
214
+ return saveUrlToString(downloadUri)
215
+ }
216
+
202
217
  downloadToFile = async destinationPath => {
203
218
  if (isBrowser()) {
204
219
  throw new errors.NotImplementedError('Disk file downloads are only available in a NodeJS environment')
package/test/src/index.js CHANGED
@@ -64,7 +64,7 @@ const testSuite = async () => {
64
64
  Logger.info('***** testFolderListAutoPagination() succeeded! *****')
65
65
  }
66
66
 
67
- const testUploadAndDownload = async () => {
67
+ const testUploadAndDownloadToFile = async () => {
68
68
  const sourceFilePath = '../files.com-logo.png'
69
69
 
70
70
  const displayName = `files.com-logo__${nonce}.png`
@@ -99,7 +99,37 @@ const testSuite = async () => {
99
99
 
100
100
  await file.delete()
101
101
 
102
- Logger.info('***** testUploadAndDownload() succeeded! *****')
102
+ Logger.info('***** testUploadAndDownloadToFile() succeeded! *****')
103
+ }
104
+
105
+ const testUploadAndDownloadToString = async () => {
106
+ const displayName = `test-text__${nonce}.txt`
107
+ const destinationPath = `${SDK_TEST_ROOT_FOLDER}/${displayName}`
108
+
109
+ const sourceFileContents = 'The quick brown fox jumped over the lazy dogs.'
110
+ const file = await File.uploadData(destinationPath, sourceFileContents)
111
+
112
+ assert(!!file.path)
113
+ assert(file.display_name === displayName)
114
+
115
+ const foundFile = await File.find(destinationPath)
116
+
117
+ assert(foundFile.path === destinationPath)
118
+ assert(foundFile.display_name === displayName)
119
+ assert(typeof foundFile.getDownloadUri() === 'undefined')
120
+
121
+ if (!isBrowser()) {
122
+ const downloadableFile = await foundFile.download()
123
+ assert(typeof downloadableFile.getDownloadUri() !== 'undefined')
124
+
125
+ const downloadedFileContents = await downloadableFile.downloadToString()
126
+
127
+ assert(sourceFileContents === downloadedFileContents)
128
+ }
129
+
130
+ await file.delete()
131
+
132
+ Logger.info('***** testUploadAndDownloadToString() succeeded! *****')
103
133
  }
104
134
 
105
135
  /* to run this test, put a file (or symlink) at huge-file.ext * /
@@ -200,7 +230,8 @@ const testSuite = async () => {
200
230
  //
201
231
 
202
232
  await testFolderListAutoPagination()
203
- await testUploadAndDownload()
233
+ await testUploadAndDownloadToFile()
234
+ await testUploadAndDownloadToString()
204
235
  // await testUploadHugeFile() // to run this test, put a file (or symlink) at huge-file.ext
205
236
  await testSession()
206
237
  await testFailure()