files.com 1.0.304 → 1.0.306

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.304
1
+ 1.0.306
package/lib/Api.js CHANGED
@@ -17,7 +17,8 @@ var _Files = _interopRequireDefault(require("./Files"));
17
17
  var errors = _interopRequireWildcard(require("./Errors"));
18
18
  var _Logger = _interopRequireDefault(require("./Logger"));
19
19
  var _utils = require("./utils");
20
- var _excluded = ["timeoutSecs"];
20
+ var _excluded = ["timeoutSecs"],
21
+ _excluded2 = ["getAgentForUrl"];
21
22
  function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
22
23
  function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
23
24
  function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
@@ -86,19 +87,20 @@ var Api = /*#__PURE__*/(0, _createClass2.default)(function Api() {
86
87
  (0, _classCallCheck2.default)(this, Api);
87
88
  });
88
89
  (0, _defineProperty2.default)(Api, "_sendVerbatim", /*#__PURE__*/function () {
89
- var _ref3 = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee2(path, verb, options) {
90
- var isExternal, baseUrl, url, agent, response, headers, contentType, data, normalizedResponse;
90
+ var _ref3 = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee2(path, verb, optionsRaw) {
91
+ var _ref4, getAgentForUrl, options, isExternal, baseUrl, url, agent, response, headers, contentType, data, normalizedResponse;
91
92
  return _regenerator.default.wrap(function _callee2$(_context2) {
92
93
  while (1) switch (_context2.prev = _context2.next) {
93
94
  case 0:
95
+ _ref4 = optionsRaw || {}, getAgentForUrl = _ref4.getAgentForUrl, options = (0, _objectWithoutProperties2.default)(_ref4, _excluded2);
94
96
  isExternal = /^[a-zA-Z]+:\/\//.test(path);
95
97
  baseUrl = _Files.default.getBaseUrl();
96
98
  if (!(!isExternal && !baseUrl)) {
97
- _context2.next = 4;
99
+ _context2.next = 5;
98
100
  break;
99
101
  }
100
102
  throw new errors.ConfigurationError('Base URL has not been set - use Files.setBaseUrl() to set it');
101
- case 4:
103
+ case 5:
102
104
  url = isExternal ? path : "".concat(baseUrl).concat(_Files.default.getEndpointPrefix()).concat(path);
103
105
  _Logger.default.debug("Sending request: ".concat(verb, " ").concat(url));
104
106
  _Logger.default.debug('Sending options:', _objectSpread(_objectSpread({
@@ -108,15 +110,15 @@ var Api = /*#__PURE__*/(0, _createClass2.default)(function Api() {
108
110
  'X-FilesAPI-Key': '<redacted>'
109
111
  })
110
112
  }));
111
- _context2.prev = 7;
112
- agent = (options === null || options === void 0 ? void 0 : options.agent) || (options === null || options === void 0 ? void 0 : options.httpsAgent) || (options === null || options === void 0 ? void 0 : options.httpAgent);
113
- _context2.next = 11;
113
+ _context2.prev = 8;
114
+ agent = (getAgentForUrl === null || getAgentForUrl === void 0 ? void 0 : getAgentForUrl(url)) || (options === null || options === void 0 ? void 0 : options.agent) || (options === null || options === void 0 ? void 0 : options.httpsAgent) || (options === null || options === void 0 ? void 0 : options.httpAgent);
115
+ _context2.next = 12;
114
116
  return fetchWithRetry(url, _objectSpread({
115
117
  agent: agent,
116
118
  method: verb,
117
119
  timeoutSecs: _Files.default.getNetworkTimeout()
118
120
  }, options));
119
- case 11:
121
+ case 12:
120
122
  response = _context2.sent;
121
123
  headers = Object.fromEntries(response.headers.entries());
122
124
  _Logger.default.debug("Status: ".concat(response.status, " ").concat(response.statusText));
@@ -126,40 +128,40 @@ var Api = /*#__PURE__*/(0, _createClass2.default)(function Api() {
126
128
  }
127
129
  contentType = headers['content-type'] || '';
128
130
  if (!contentType.includes('application/json')) {
129
- _context2.next = 22;
131
+ _context2.next = 23;
130
132
  break;
131
133
  }
132
- _context2.next = 19;
134
+ _context2.next = 20;
133
135
  return response.json();
134
- case 19:
136
+ case 20:
135
137
  data = _context2.sent;
136
- _context2.next = 35;
138
+ _context2.next = 36;
137
139
  break;
138
- case 22:
140
+ case 23:
139
141
  if (!contentType.includes('text/')) {
140
- _context2.next = 28;
142
+ _context2.next = 29;
141
143
  break;
142
144
  }
143
- _context2.next = 25;
145
+ _context2.next = 26;
144
146
  return response.text();
145
- case 25:
147
+ case 26:
146
148
  data = _context2.sent;
147
- _context2.next = 35;
149
+ _context2.next = 36;
148
150
  break;
149
- case 28:
151
+ case 29:
150
152
  if (!contentType.includes('multipart/form-data')) {
151
- _context2.next = 34;
153
+ _context2.next = 35;
152
154
  break;
153
155
  }
154
- _context2.next = 31;
156
+ _context2.next = 32;
155
157
  return response.formData();
156
- case 31:
158
+ case 32:
157
159
  data = _context2.sent;
158
- _context2.next = 35;
160
+ _context2.next = 36;
159
161
  break;
160
- case 34:
161
- data = response.body;
162
162
  case 35:
163
+ data = response.body;
164
+ case 36:
163
165
  normalizedResponse = {
164
166
  status: response.status,
165
167
  reason: response.statusText,
@@ -167,42 +169,39 @@ var Api = /*#__PURE__*/(0, _createClass2.default)(function Api() {
167
169
  data: data
168
170
  };
169
171
  if (response.ok) {
170
- _context2.next = 38;
172
+ _context2.next = 39;
171
173
  break;
172
174
  }
173
175
  throw {
174
176
  response: normalizedResponse
175
177
  };
176
- case 38:
178
+ case 39:
177
179
  return _context2.abrupt("return", normalizedResponse);
178
- case 41:
179
- _context2.prev = 41;
180
- _context2.t0 = _context2["catch"](7);
180
+ case 42:
181
+ _context2.prev = 42;
182
+ _context2.t0 = _context2["catch"](8);
181
183
  errors.handleErrorResponse(_context2.t0);
182
- case 44:
184
+ case 45:
183
185
  case "end":
184
186
  return _context2.stop();
185
187
  }
186
- }, _callee2, null, [[7, 41]]);
188
+ }, _callee2, null, [[8, 42]]);
187
189
  }));
188
190
  return function (_x3, _x4, _x5) {
189
191
  return _ref3.apply(this, arguments);
190
192
  };
191
193
  }());
192
194
  (0, _defineProperty2.default)(Api, "sendFilePart", function (externalUrl, verb, data) {
193
- var headers = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
194
- var params = {
195
+ var optionsRaw = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
196
+ var options = _objectSpread(_objectSpread({}, optionsRaw), {}, {
195
197
  body: data
196
- };
197
- if (headers) {
198
- params.headers = headers;
199
- }
200
- return Api._sendVerbatim(externalUrl, verb, params);
198
+ });
199
+ return Api._sendVerbatim(externalUrl, verb, options);
201
200
  });
202
201
  (0, _defineProperty2.default)(Api, "_autoPaginate", /*#__PURE__*/function () {
203
- var _ref4 = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee3(path, verb, params, options, response, metadata) {
202
+ var _ref5 = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee3(path, verb, params, options, response, metadata) {
204
203
  var _options$autoPaginate;
205
- var _response$headers, nextCursor, _ref5, autoPaginateCount, previousAutoPaginateData, nextPage, nextParams, nextMetadata;
204
+ var _response$headers, nextCursor, _ref6, autoPaginateCount, previousAutoPaginateData, nextPage, nextParams, nextMetadata;
206
205
  return _regenerator.default.wrap(function _callee3$(_context3) {
207
206
  while (1) switch (_context3.prev = _context3.next) {
208
207
  case 0:
@@ -211,7 +210,7 @@ var Api = /*#__PURE__*/(0, _createClass2.default)(function Api() {
211
210
  break;
212
211
  }
213
212
  nextCursor = response === null || response === void 0 || (_response$headers = response.headers) === null || _response$headers === void 0 ? void 0 : _response$headers['x-files-cursor'];
214
- _ref5 = metadata || {}, autoPaginateCount = _ref5.autoPaginateCount, previousAutoPaginateData = _ref5.previousAutoPaginateData;
213
+ _ref6 = metadata || {}, autoPaginateCount = _ref6.autoPaginateCount, previousAutoPaginateData = _ref6.previousAutoPaginateData;
215
214
  if (!nextCursor) {
216
215
  _context3.next = 10;
217
216
  break;
@@ -244,11 +243,11 @@ var Api = /*#__PURE__*/(0, _createClass2.default)(function Api() {
244
243
  }, _callee3);
245
244
  }));
246
245
  return function (_x6, _x7, _x8, _x9, _x10, _x11) {
247
- return _ref4.apply(this, arguments);
246
+ return _ref5.apply(this, arguments);
248
247
  };
249
248
  }());
250
249
  (0, _defineProperty2.default)(Api, "sendRequest", /*#__PURE__*/function () {
251
- var _ref6 = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee4(path, verb) {
250
+ var _ref7 = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee4(path, verb) {
252
251
  var params,
253
252
  options,
254
253
  metadata,
@@ -309,10 +308,10 @@ var Api = /*#__PURE__*/(0, _createClass2.default)(function Api() {
309
308
  hasParams = (0, _utils.isObject)(params) && !(0, _utils.isEmpty)(params);
310
309
  if (hasParams) {
311
310
  if (verb.toUpperCase() === 'GET') {
312
- pairs = Object.entries(params).map(function (_ref7) {
313
- var _ref8 = (0, _slicedToArray2.default)(_ref7, 2),
314
- key = _ref8[0],
315
- value = _ref8[1];
311
+ pairs = Object.entries(params).map(function (_ref8) {
312
+ var _ref9 = (0, _slicedToArray2.default)(_ref8, 2),
313
+ key = _ref9[0],
314
+ value = _ref9[1];
316
315
  return "".concat(encodeURIComponent(key), "=").concat(encodeURIComponent(value));
317
316
  });
318
317
  requestPath += path.includes('?') ? '&' : '?';
@@ -343,7 +342,7 @@ var Api = /*#__PURE__*/(0, _createClass2.default)(function Api() {
343
342
  }, _callee4);
344
343
  }));
345
344
  return function (_x12, _x13) {
346
- return _ref6.apply(this, arguments);
345
+ return _ref7.apply(this, arguments);
347
346
  };
348
347
  }());
349
348
  var _default = Api;
@@ -939,7 +939,7 @@ var File = /*#__PURE__*/(0, _createClass2.default)(function File() {
939
939
  });
940
940
  readableStream.on('data', /*#__PURE__*/function () {
941
941
  var _ref19 = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee15(chunk) {
942
- var nextLength, excessLength, chunkBuffer, tailLength, lastChunkForPart, firstChunkForNextPart, buffer, nextFileUploadPart, upload_uri, uploadPromise;
942
+ var nextLength, excessLength, chunkBuffer, _options$getAgentForU, tailLength, lastChunkForPart, firstChunkForNextPart, buffer, nextFileUploadPart, upload_uri, agent, uploadPromise;
943
943
  return _regenerator.default.wrap(function _callee15$(_context15) {
944
944
  while (1) switch (_context15.prev = _context15.next) {
945
945
  case 0:
@@ -948,7 +948,7 @@ var File = /*#__PURE__*/(0, _createClass2.default)(function File() {
948
948
  excessLength = nextLength - firstFileUploadPart.partsize;
949
949
  chunkBuffer = _safeBuffer.Buffer.from(chunk);
950
950
  if (!(excessLength > 0)) {
951
- _context15.next = 27;
951
+ _context15.next = 28;
952
952
  break;
953
953
  }
954
954
  readableStream.pause();
@@ -963,52 +963,55 @@ var File = /*#__PURE__*/(0, _createClass2.default)(function File() {
963
963
  return File._continueUpload(destinationPath, ++part, firstFileUploadPart, options);
964
964
  case 13:
965
965
  nextFileUploadPart = _context15.sent;
966
- upload_uri = determinePartUploadUri(nextFileUploadPart);
967
- uploadPromise = _Api.default.sendFilePart(upload_uri, 'PUT', buffer);
966
+ upload_uri = determinePartUploadUri(nextFileUploadPart); // instantiate an httpsAgent dynamically if needed
967
+ agent = ((_options$getAgentForU = options.getAgentForUrl) === null || _options$getAgentForU === void 0 ? void 0 : _options$getAgentForU.call(options, upload_uri)) || (options === null || options === void 0 ? void 0 : options.agent);
968
+ uploadPromise = _Api.default.sendFilePart(upload_uri, 'PUT', buffer, {
969
+ agent: agent
970
+ });
968
971
  if (!firstFileUploadPart.parallel_parts) {
969
- _context15.next = 20;
972
+ _context15.next = 21;
970
973
  break;
971
974
  }
972
975
  concurrentUploads.push(uploadPromise);
973
- _context15.next = 22;
976
+ _context15.next = 23;
974
977
  break;
975
- case 20:
976
- _context15.next = 22;
978
+ case 21:
979
+ _context15.next = 23;
977
980
  return uploadPromise;
978
- case 22:
981
+ case 23:
979
982
  chunks = [firstChunkForNextPart];
980
983
  length = firstChunkForNextPart.length;
981
984
  readableStream.resume();
982
- _context15.next = 29;
985
+ _context15.next = 30;
983
986
  break;
984
- case 27:
987
+ case 28:
985
988
  chunks.push(chunkBuffer);
986
989
  length += chunk.length;
987
- case 29:
988
- _context15.next = 34;
990
+ case 30:
991
+ _context15.next = 35;
989
992
  break;
990
- case 31:
991
- _context15.prev = 31;
993
+ case 32:
994
+ _context15.prev = 32;
992
995
  _context15.t0 = _context15["catch"](0);
993
996
  reject(_context15.t0);
994
- case 34:
997
+ case 35:
995
998
  case "end":
996
999
  return _context15.stop();
997
1000
  }
998
- }, _callee15, null, [[0, 31]]);
1001
+ }, _callee15, null, [[0, 32]]);
999
1002
  }));
1000
1003
  return function (_x20) {
1001
1004
  return _ref19.apply(this, arguments);
1002
1005
  };
1003
1006
  }());
1004
1007
  readableStream.on('end', /*#__PURE__*/(0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee16() {
1005
- var buffer, nextFileUploadPart, upload_uri, response, createdFile;
1008
+ var _options$getAgentForU2, buffer, nextFileUploadPart, upload_uri, agent, response, createdFile;
1006
1009
  return _regenerator.default.wrap(function _callee16$(_context16) {
1007
1010
  while (1) switch (_context16.prev = _context16.next) {
1008
1011
  case 0:
1009
1012
  _context16.prev = 0;
1010
1013
  if (!(chunks.length > 0)) {
1011
- _context16.next = 8;
1014
+ _context16.next = 9;
1012
1015
  break;
1013
1016
  }
1014
1017
  buffer = _safeBuffer.Buffer.concat(chunks);
@@ -1016,29 +1019,32 @@ var File = /*#__PURE__*/(0, _createClass2.default)(function File() {
1016
1019
  return File._continueUpload(destinationPath, ++part, firstFileUploadPart, options);
1017
1020
  case 5:
1018
1021
  nextFileUploadPart = _context16.sent;
1019
- upload_uri = determinePartUploadUri(nextFileUploadPart);
1020
- concurrentUploads.push(_Api.default.sendFilePart(upload_uri, 'PUT', buffer));
1021
- case 8:
1022
- _context16.next = 10;
1022
+ upload_uri = determinePartUploadUri(nextFileUploadPart); // instantiate an httpsAgent dynamically if needed
1023
+ agent = ((_options$getAgentForU2 = options.getAgentForUrl) === null || _options$getAgentForU2 === void 0 ? void 0 : _options$getAgentForU2.call(options, upload_uri)) || (options === null || options === void 0 ? void 0 : options.agent);
1024
+ concurrentUploads.push(_Api.default.sendFilePart(upload_uri, 'PUT', buffer, {
1025
+ agent: agent
1026
+ }));
1027
+ case 9:
1028
+ _context16.next = 11;
1023
1029
  return Promise.all(concurrentUploads);
1024
- case 10:
1025
- _context16.next = 12;
1030
+ case 11:
1031
+ _context16.next = 13;
1026
1032
  return File._completeUpload(firstFileUploadPart, options);
1027
- case 12:
1033
+ case 13:
1028
1034
  response = _context16.sent;
1029
1035
  createdFile = new File(response.data, options);
1030
1036
  resolve(createdFile);
1031
- _context16.next = 20;
1037
+ _context16.next = 21;
1032
1038
  break;
1033
- case 17:
1034
- _context16.prev = 17;
1039
+ case 18:
1040
+ _context16.prev = 18;
1035
1041
  _context16.t0 = _context16["catch"](0);
1036
1042
  reject(_context16.t0);
1037
- case 20:
1043
+ case 21:
1038
1044
  case "end":
1039
1045
  return _context16.stop();
1040
1046
  }
1041
- }, _callee16, null, [[0, 17]]);
1047
+ }, _callee16, null, [[0, 18]]);
1042
1048
  })));
1043
1049
  });
1044
1050
  case 10:
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "files.com",
3
- "version": "1.0.304",
3
+ "version": "1.0.306",
4
4
  "description": "Files.com SDK for JavaScript",
5
5
  "keywords": [
6
6
  "files.com",
package/src/Api.js CHANGED
@@ -40,7 +40,9 @@ const fetchWithRetry = async (url, options, retries = 0) => {
40
40
  }
41
41
 
42
42
  class Api {
43
- static _sendVerbatim = async (path, verb, options) => {
43
+ static _sendVerbatim = async (path, verb, optionsRaw) => {
44
+ const { getAgentForUrl, ...options } = optionsRaw || {}
45
+
44
46
  const isExternal = /^[a-zA-Z]+:\/\//.test(path)
45
47
  const baseUrl = Files.getBaseUrl()
46
48
 
@@ -64,7 +66,7 @@ class Api {
64
66
  })
65
67
 
66
68
  try {
67
- const agent = options?.agent || options?.httpsAgent || options?.httpAgent
69
+ const agent = getAgentForUrl?.(url) || options?.agent || options?.httpsAgent || options?.httpAgent
68
70
 
69
71
  const response = await fetchWithRetry(url, {
70
72
  agent,
@@ -112,14 +114,13 @@ class Api {
112
114
  }
113
115
  }
114
116
 
115
- static sendFilePart = (externalUrl, verb, data, headers = {}) => {
116
- const params = { body: data }
117
-
118
- if (headers) {
119
- params.headers = headers
117
+ static sendFilePart = (externalUrl, verb, data, optionsRaw = {}) => {
118
+ const options = {
119
+ ...optionsRaw,
120
+ body: data,
120
121
  }
121
122
 
122
- return Api._sendVerbatim(externalUrl, verb, params)
123
+ return Api._sendVerbatim(externalUrl, verb, options)
123
124
  }
124
125
 
125
126
  static _autoPaginate = async (path, verb, params, options, response, metadata) => {
@@ -121,7 +121,11 @@ class File {
121
121
  const nextFileUploadPart = await File._continueUpload(destinationPath, ++part, firstFileUploadPart, options)
122
122
 
123
123
  const upload_uri = determinePartUploadUri(nextFileUploadPart)
124
- const uploadPromise = Api.sendFilePart(upload_uri, 'PUT', buffer)
124
+
125
+ // instantiate an httpsAgent dynamically if needed
126
+ const agent = options.getAgentForUrl?.(upload_uri) || options?.agent
127
+
128
+ const uploadPromise = Api.sendFilePart(upload_uri, 'PUT', buffer, { agent })
125
129
 
126
130
  if (firstFileUploadPart.parallel_parts) {
127
131
  concurrentUploads.push(uploadPromise)
@@ -149,7 +153,11 @@ class File {
149
153
  const nextFileUploadPart = await File._continueUpload(destinationPath, ++part, firstFileUploadPart, options)
150
154
 
151
155
  const upload_uri = determinePartUploadUri(nextFileUploadPart)
152
- concurrentUploads.push(Api.sendFilePart(upload_uri, 'PUT', buffer))
156
+
157
+ // instantiate an httpsAgent dynamically if needed
158
+ const agent = options.getAgentForUrl?.(upload_uri) || options?.agent
159
+
160
+ concurrentUploads.push(Api.sendFilePart(upload_uri, 'PUT', buffer, { agent }))
153
161
  }
154
162
 
155
163
  await Promise.all(concurrentUploads)