files.com 1.0.205 → 1.0.208

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/_VERSION CHANGED
@@ -1 +1 @@
1
- 1.0.205
1
+ 1.0.208
@@ -900,15 +900,15 @@ var File = /*#__PURE__*/(0, _createClass2.default)(function File() {
900
900
  this.options = _objectSpread({}, options);
901
901
  });
902
902
  (0, _defineProperty2.default)(File, "_openUpload", /*#__PURE__*/function () {
903
- var _ref13 = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee11(path) {
903
+ var _ref13 = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee11(path, paramsRaw) {
904
904
  var params, response, partData;
905
905
  return _regenerator.default.wrap(function _callee11$(_context11) {
906
906
  while (1) {
907
907
  switch (_context11.prev = _context11.next) {
908
908
  case 0:
909
- params = {
909
+ params = _objectSpread(_objectSpread({}, paramsRaw), {}, {
910
910
  action: 'put'
911
- };
911
+ });
912
912
  _context11.next = 3;
913
913
  return _Api.default.sendRequest("/files/".concat(encodeURIComponent(path)), 'POST', params);
914
914
 
@@ -937,7 +937,7 @@ var File = /*#__PURE__*/(0, _createClass2.default)(function File() {
937
937
  }, _callee11);
938
938
  }));
939
939
 
940
- return function (_x5) {
940
+ return function (_x5, _x6) {
941
941
  return _ref13.apply(this, arguments);
942
942
  };
943
943
  }());
@@ -981,12 +981,12 @@ var File = /*#__PURE__*/(0, _createClass2.default)(function File() {
981
981
  }, _callee12);
982
982
  }));
983
983
 
984
- return function (_x6, _x7, _x8) {
984
+ return function (_x7, _x8, _x9) {
985
985
  return _ref14.apply(this, arguments);
986
986
  };
987
987
  }());
988
988
  (0, _defineProperty2.default)(File, "_completeUpload", /*#__PURE__*/function () {
989
- var _ref15 = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee13(fileUploadPart) {
989
+ var _ref15 = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee13(firstFileUploadPart) {
990
990
  var params;
991
991
  return _regenerator.default.wrap(function _callee13$(_context13) {
992
992
  while (1) {
@@ -994,9 +994,9 @@ var File = /*#__PURE__*/(0, _createClass2.default)(function File() {
994
994
  case 0:
995
995
  params = {
996
996
  action: 'end',
997
- ref: fileUploadPart.ref
997
+ ref: firstFileUploadPart.ref
998
998
  };
999
- return _context13.abrupt("return", _Api.default.sendRequest("/files/".concat(encodeURIComponent(fileUploadPart.path)), 'POST', params));
999
+ return _context13.abrupt("return", _Api.default.sendRequest("/files/".concat(encodeURIComponent(firstFileUploadPart.path)), 'POST', params));
1000
1000
 
1001
1001
  case 2:
1002
1002
  case "end":
@@ -1006,24 +1006,24 @@ var File = /*#__PURE__*/(0, _createClass2.default)(function File() {
1006
1006
  }, _callee13);
1007
1007
  }));
1008
1008
 
1009
- return function (_x9) {
1009
+ return function (_x10) {
1010
1010
  return _ref15.apply(this, arguments);
1011
1011
  };
1012
1012
  }());
1013
1013
  (0, _defineProperty2.default)(File, "uploadStream", /*#__PURE__*/function () {
1014
- var _ref16 = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee16(destinationPath, readableStream) {
1015
- var fileUploadPart, file;
1014
+ var _ref16 = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee16(destinationPath, readableStream, params) {
1015
+ var firstFileUploadPart, file;
1016
1016
  return _regenerator.default.wrap(function _callee16$(_context16) {
1017
1017
  while (1) {
1018
1018
  switch (_context16.prev = _context16.next) {
1019
1019
  case 0:
1020
1020
  _context16.next = 2;
1021
- return File._openUpload(destinationPath);
1021
+ return File._openUpload(destinationPath, params);
1022
1022
 
1023
1023
  case 2:
1024
- fileUploadPart = _context16.sent;
1024
+ firstFileUploadPart = _context16.sent;
1025
1025
 
1026
- if (fileUploadPart) {
1026
+ if (firstFileUploadPart) {
1027
1027
  _context16.next = 5;
1028
1028
  break;
1029
1029
  }
@@ -1037,50 +1037,66 @@ var File = /*#__PURE__*/(0, _createClass2.default)(function File() {
1037
1037
  var part = 0;
1038
1038
  var chunks = [];
1039
1039
  var length = 0;
1040
+ var concurrentUploads = [];
1040
1041
  readableStream.on('error', function (error) {
1041
1042
  reject(error);
1042
1043
  });
1043
1044
  readableStream.on('data', /*#__PURE__*/function () {
1044
1045
  var _ref17 = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee14(chunk) {
1045
- var buffer, nextFileUploadPart;
1046
+ var nextLength, excessLength, chunkBuffer, lastChunkForPart, firstChunkForNextPart, buffer, nextFileUploadPart;
1046
1047
  return _regenerator.default.wrap(function _callee14$(_context14) {
1047
1048
  while (1) {
1048
1049
  switch (_context14.prev = _context14.next) {
1049
1050
  case 0:
1050
- length += chunk.length;
1051
+ _context14.prev = 0;
1052
+ nextLength = length + chunk.length;
1053
+ excessLength = nextLength - firstFileUploadPart.partsize;
1054
+ chunkBuffer = _safeBuffer.Buffer.from(chunk);
1051
1055
 
1052
- if (!(length > fileUploadPart.partsize)) {
1053
- _context14.next = 12;
1056
+ if (!(excessLength > 0)) {
1057
+ _context14.next = 19;
1054
1058
  break;
1055
1059
  }
1056
1060
 
1057
1061
  readableStream.pause();
1062
+ lastChunkForPart = chunkBuffer.subarray(0, excessLength);
1063
+ firstChunkForNextPart = chunkBuffer.subarray(excessLength);
1064
+ chunks.push(lastChunkForPart);
1058
1065
  buffer = _safeBuffer.Buffer.concat(chunks);
1059
- _context14.next = 6;
1060
- return File._continueUpload(destinationPath, ++part, fileUploadPart);
1066
+ _context14.next = 12;
1067
+ return File._continueUpload(destinationPath, ++part, firstFileUploadPart);
1061
1068
 
1062
- case 6:
1069
+ case 12:
1063
1070
  nextFileUploadPart = _context14.sent;
1064
- _context14.next = 9;
1065
- return _Api.default.sendFilePart(nextFileUploadPart.upload_uri, 'PUT', buffer);
1066
-
1067
- case 9:
1068
- chunks = [];
1069
- length = 0;
1071
+ concurrentUploads.push(_Api.default.sendFilePart(nextFileUploadPart.upload_uri, 'PUT', buffer));
1072
+ chunks = [firstChunkForNextPart];
1073
+ length = 1;
1070
1074
  readableStream.resume();
1075
+ _context14.next = 21;
1076
+ break;
1071
1077
 
1072
- case 12:
1073
- chunks.push(_safeBuffer.Buffer.from(chunk));
1078
+ case 19:
1079
+ chunks.push(chunkBuffer);
1080
+ length += chunk.length;
1081
+
1082
+ case 21:
1083
+ _context14.next = 26;
1084
+ break;
1074
1085
 
1075
- case 13:
1086
+ case 23:
1087
+ _context14.prev = 23;
1088
+ _context14.t0 = _context14["catch"](0);
1089
+ reject(_context14.t0);
1090
+
1091
+ case 26:
1076
1092
  case "end":
1077
1093
  return _context14.stop();
1078
1094
  }
1079
1095
  }
1080
- }, _callee14);
1096
+ }, _callee14, null, [[0, 23]]);
1081
1097
  }));
1082
1098
 
1083
- return function (_x12) {
1099
+ return function (_x14) {
1084
1100
  return _ref17.apply(this, arguments);
1085
1101
  };
1086
1102
  }());
@@ -1090,35 +1106,47 @@ var File = /*#__PURE__*/(0, _createClass2.default)(function File() {
1090
1106
  while (1) {
1091
1107
  switch (_context15.prev = _context15.next) {
1092
1108
  case 0:
1109
+ _context15.prev = 0;
1110
+
1093
1111
  if (!(chunks.length > 0)) {
1094
1112
  _context15.next = 7;
1095
1113
  break;
1096
1114
  }
1097
1115
 
1098
1116
  buffer = _safeBuffer.Buffer.concat(chunks);
1099
- _context15.next = 4;
1100
- return File._continueUpload(destinationPath, ++part, fileUploadPart);
1117
+ _context15.next = 5;
1118
+ return File._continueUpload(destinationPath, ++part, firstFileUploadPart);
1101
1119
 
1102
- case 4:
1120
+ case 5:
1103
1121
  nextFileUploadPart = _context15.sent;
1104
- _context15.next = 7;
1105
- return _Api.default.sendFilePart(nextFileUploadPart.upload_uri, 'PUT', buffer);
1122
+ concurrentUploads.push(_Api.default.sendFilePart(nextFileUploadPart.upload_uri, 'PUT', buffer));
1106
1123
 
1107
1124
  case 7:
1108
1125
  _context15.next = 9;
1109
- return File._completeUpload(fileUploadPart);
1126
+ return Promise.all(concurrentUploads);
1110
1127
 
1111
1128
  case 9:
1129
+ _context15.next = 11;
1130
+ return File._completeUpload(firstFileUploadPart);
1131
+
1132
+ case 11:
1112
1133
  response = _context15.sent;
1113
1134
  createdFile = new File(response.data);
1114
1135
  resolve(createdFile);
1136
+ _context15.next = 19;
1137
+ break;
1138
+
1139
+ case 16:
1140
+ _context15.prev = 16;
1141
+ _context15.t0 = _context15["catch"](0);
1142
+ reject(_context15.t0);
1115
1143
 
1116
- case 12:
1144
+ case 19:
1117
1145
  case "end":
1118
1146
  return _context15.stop();
1119
1147
  }
1120
1148
  }
1121
- }, _callee15);
1149
+ }, _callee15, null, [[0, 16]]);
1122
1150
  })));
1123
1151
  });
1124
1152
 
@@ -1139,12 +1167,12 @@ var File = /*#__PURE__*/(0, _createClass2.default)(function File() {
1139
1167
  }, _callee16, null, [[5, 12]]);
1140
1168
  }));
1141
1169
 
1142
- return function (_x10, _x11) {
1170
+ return function (_x11, _x12, _x13) {
1143
1171
  return _ref16.apply(this, arguments);
1144
1172
  };
1145
1173
  }());
1146
1174
  (0, _defineProperty2.default)(File, "uploadData", /*#__PURE__*/function () {
1147
- var _ref19 = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee17(destinationPath, data) {
1175
+ var _ref19 = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee17(destinationPath, data, params) {
1148
1176
  return _regenerator.default.wrap(function _callee17$(_context17) {
1149
1177
  while (1) {
1150
1178
  switch (_context17.prev = _context17.next) {
@@ -1157,7 +1185,7 @@ var File = /*#__PURE__*/(0, _createClass2.default)(function File() {
1157
1185
  throw new errors.MissingParameterError('Upload data was not provided');
1158
1186
 
1159
1187
  case 2:
1160
- return _context17.abrupt("return", File.uploadStream(destinationPath, _readableStream.default.from(data)));
1188
+ return _context17.abrupt("return", File.uploadStream(destinationPath, _readableStream.default.from(data), params));
1161
1189
 
1162
1190
  case 3:
1163
1191
  case "end":
@@ -1167,12 +1195,12 @@ var File = /*#__PURE__*/(0, _createClass2.default)(function File() {
1167
1195
  }, _callee17);
1168
1196
  }));
1169
1197
 
1170
- return function (_x13, _x14) {
1198
+ return function (_x15, _x16, _x17) {
1171
1199
  return _ref19.apply(this, arguments);
1172
1200
  };
1173
1201
  }());
1174
1202
  (0, _defineProperty2.default)(File, "uploadFile", /*#__PURE__*/function () {
1175
- var _ref20 = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee18(destinationPath, sourceFilePath) {
1203
+ var _ref20 = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee18(destinationPath, sourceFilePath, params) {
1176
1204
  var _require3, openDiskFileReadStream, stream;
1177
1205
 
1178
1206
  return _regenerator.default.wrap(function _callee18$(_context18) {
@@ -1189,7 +1217,7 @@ var File = /*#__PURE__*/(0, _createClass2.default)(function File() {
1189
1217
  case 2:
1190
1218
  _require3 = require('../isomorphic/File.node.js'), openDiskFileReadStream = _require3.openDiskFileReadStream;
1191
1219
  stream = openDiskFileReadStream(sourceFilePath);
1192
- return _context18.abrupt("return", File.uploadStream(destinationPath, stream));
1220
+ return _context18.abrupt("return", File.uploadStream(destinationPath, stream, params));
1193
1221
 
1194
1222
  case 5:
1195
1223
  case "end":
@@ -1199,7 +1227,7 @@ var File = /*#__PURE__*/(0, _createClass2.default)(function File() {
1199
1227
  }, _callee18);
1200
1228
  }));
1201
1229
 
1202
- return function (_x15, _x16) {
1230
+ return function (_x18, _x19, _x20) {
1203
1231
  return _ref20.apply(this, arguments);
1204
1232
  };
1205
1233
  }());
@@ -1329,7 +1357,7 @@ var File = /*#__PURE__*/(0, _createClass2.default)(function File() {
1329
1357
  }, _callee19);
1330
1358
  }));
1331
1359
 
1332
- return function (_x17) {
1360
+ return function (_x21) {
1333
1361
  return _ref21.apply(this, arguments);
1334
1362
  };
1335
1363
  }());
@@ -1395,7 +1423,7 @@ var File = /*#__PURE__*/(0, _createClass2.default)(function File() {
1395
1423
  }, _callee20);
1396
1424
  }));
1397
1425
 
1398
- return function (_x18) {
1426
+ return function (_x22) {
1399
1427
  return _ref22.apply(this, arguments);
1400
1428
  };
1401
1429
  }());
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "files.com",
3
- "version": "1.0.205",
3
+ "version": "1.0.208",
4
4
  "description": "Files.com SDK for JavaScript",
5
5
  "keywords": [
6
6
  "files.com",
@@ -28,8 +28,8 @@ class File {
28
28
  }
29
29
 
30
30
  isLoaded = () => !!this.attributes.path
31
- static _openUpload = async path => {
32
- const params = { action: 'put' }
31
+ static _openUpload = async (path, paramsRaw) => {
32
+ const params = { ...paramsRaw, action: 'put' }
33
33
  const response = await Api.sendRequest(`/files/${encodeURIComponent(path)}`, 'POST', params)
34
34
 
35
35
  if (!response) {
@@ -67,19 +67,22 @@ class File {
67
67
  return new FileUploadPart(partData)
68
68
  }
69
69
 
70
- static _completeUpload = async fileUploadPart => {
70
+ static _completeUpload = async firstFileUploadPart => {
71
71
  const params = {
72
72
  action: 'end',
73
- ref: fileUploadPart.ref,
73
+ ref: firstFileUploadPart.ref,
74
74
  }
75
75
 
76
- return Api.sendRequest(`/files/${encodeURIComponent(fileUploadPart.path)}`, 'POST', params)
76
+ return Api.sendRequest(`/files/${encodeURIComponent(firstFileUploadPart.path)}`, 'POST', params)
77
77
  }
78
78
 
79
- static uploadStream = async (destinationPath, readableStream) => {
80
- const fileUploadPart = await File._openUpload(destinationPath)
79
+ /**
80
+ * @note see File.copy() for list of supported params
81
+ */
82
+ static uploadStream = async (destinationPath, readableStream, params) => {
83
+ const firstFileUploadPart = await File._openUpload(destinationPath, params)
81
84
 
82
- if (!fileUploadPart) {
85
+ if (!firstFileUploadPart) {
83
86
  return
84
87
  }
85
88
 
@@ -88,41 +91,61 @@ class File {
88
91
  let part = 0
89
92
  let chunks = []
90
93
  let length = 0
94
+ const concurrentUploads = []
91
95
 
92
96
  readableStream.on('error', error => { reject(error) })
93
97
 
94
98
  readableStream.on('data', async chunk => {
95
- length += chunk.length
99
+ try {
100
+ const nextLength = length + chunk.length
101
+ const excessLength = nextLength - firstFileUploadPart.partsize
96
102
 
97
- if (length > fileUploadPart.partsize) {
98
- readableStream.pause()
103
+ const chunkBuffer = Buffer.from(chunk)
99
104
 
100
- const buffer = Buffer.concat(chunks)
101
- const nextFileUploadPart = await File._continueUpload(destinationPath, ++part, fileUploadPart)
105
+ if (excessLength > 0) {
106
+ readableStream.pause()
102
107
 
103
- await Api.sendFilePart(nextFileUploadPart.upload_uri, 'PUT', buffer)
108
+ const lastChunkForPart = chunkBuffer.subarray(0, excessLength)
109
+ const firstChunkForNextPart = chunkBuffer.subarray(excessLength)
104
110
 
105
- chunks = []
106
- length = 0
111
+ chunks.push(lastChunkForPart)
107
112
 
108
- readableStream.resume()
109
- }
113
+ const buffer = Buffer.concat(chunks)
114
+ const nextFileUploadPart = await File._continueUpload(destinationPath, ++part, firstFileUploadPart)
115
+
116
+ concurrentUploads.push(Api.sendFilePart(nextFileUploadPart.upload_uri, 'PUT', buffer))
117
+
118
+ chunks = [firstChunkForNextPart]
119
+ length = 1
110
120
 
111
- chunks.push(Buffer.from(chunk))
121
+ readableStream.resume()
122
+ } else {
123
+ chunks.push(chunkBuffer)
124
+ length += chunk.length
125
+ }
126
+ } catch (error) {
127
+ reject(error)
128
+ }
112
129
  })
113
130
 
114
131
  readableStream.on('end', async () => {
115
- if (chunks.length > 0) {
116
- const buffer = Buffer.concat(chunks)
117
- const nextFileUploadPart = await File._continueUpload(destinationPath, ++part, fileUploadPart)
132
+ try {
133
+ if (chunks.length > 0) {
134
+ const buffer = Buffer.concat(chunks)
135
+ const nextFileUploadPart = await File._continueUpload(destinationPath, ++part, firstFileUploadPart)
118
136
 
119
- await Api.sendFilePart(nextFileUploadPart.upload_uri, 'PUT', buffer)
120
- }
137
+ concurrentUploads.push(Api.sendFilePart(nextFileUploadPart.upload_uri, 'PUT', buffer))
138
+ }
139
+
140
+ await Promise.all(concurrentUploads)
121
141
 
122
- const response = await File._completeUpload(fileUploadPart)
123
- const createdFile = new File(response.data)
142
+ const response = await File._completeUpload(firstFileUploadPart)
143
+ const createdFile = new File(response.data)
124
144
 
125
- resolve(createdFile)
145
+ resolve(createdFile)
146
+ } catch (error) {
147
+ reject(error)
148
+ }
126
149
  })
127
150
  })
128
151
 
@@ -134,16 +157,20 @@ class File {
134
157
 
135
158
  /**
136
159
  * data - string, Buffer, Stream, any object implementing Symbol.iterator or Symbol.asyncIterator
160
+ * @note see File.copy() for list of supported params
137
161
  */
138
- static uploadData = async (destinationPath, data) => {
162
+ static uploadData = async (destinationPath, data, params) => {
139
163
  if (!data) {
140
164
  throw new errors.MissingParameterError('Upload data was not provided')
141
165
  }
142
166
 
143
- return File.uploadStream(destinationPath, Readable.from(data))
167
+ return File.uploadStream(destinationPath, Readable.from(data), params)
144
168
  }
145
169
 
146
- static uploadFile = async (destinationPath, sourceFilePath) => {
170
+ /**
171
+ * @note see File.copy() for list of supported params
172
+ */
173
+ static uploadFile = async (destinationPath, sourceFilePath, params) => {
147
174
  if (isBrowser()) {
148
175
  throw new errors.NotImplementedError('Disk file uploads are only available in a NodeJS environment')
149
176
  }
@@ -151,7 +178,7 @@ class File {
151
178
  const { openDiskFileReadStream } = require('../isomorphic/File.node.js')
152
179
  const stream = openDiskFileReadStream(sourceFilePath)
153
180
 
154
- return File.uploadStream(destinationPath, stream)
181
+ return File.uploadStream(destinationPath, stream, params)
155
182
  }
156
183
 
157
184
  downloadToStream = async writableStream => {