Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

helpers.js 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. module.exports = function init(global, jsUtil, cookieHandler, messages, base64, errorCodes, dependencyValidator, ponyfills) {
  2. var validSerializers = ['urlencoded', 'json', 'utf8', 'raw', 'multipart'];
  3. var validCertModes = ['default', 'nocheck', 'pinned', 'legacy'];
  4. var validClientAuthModes = ['none', 'systemstore', 'buffer'];
  5. var validHttpMethods = ['get', 'put', 'post', 'patch', 'head', 'delete', 'options', 'upload', 'download'];
  6. var validResponseTypes = ['text', 'json', 'arraybuffer', 'blob'];
  7. var interface = {
  8. b64EncodeUnicode: b64EncodeUnicode,
  9. checkClientAuthMode: checkClientAuthMode,
  10. checkClientAuthOptions: checkClientAuthOptions,
  11. checkDownloadFilePath: checkDownloadFilePath,
  12. checkFollowRedirectValue: checkFollowRedirectValue,
  13. checkForBlacklistedHeaderKey: checkForBlacklistedHeaderKey,
  14. checkForInvalidHeaderValue: checkForInvalidHeaderValue,
  15. checkSerializer: checkSerializer,
  16. checkSSLCertMode: checkSSLCertMode,
  17. checkTimeoutValue: checkTimeoutValue,
  18. checkUploadFileOptions: checkUploadFileOptions,
  19. getMergedHeaders: getMergedHeaders,
  20. processData: processData,
  21. handleMissingCallbacks: handleMissingCallbacks,
  22. handleMissingOptions: handleMissingOptions,
  23. injectCookieHandler: injectCookieHandler,
  24. injectFileEntryHandler: injectFileEntryHandler,
  25. injectRawResponseHandler: injectRawResponseHandler,
  26. };
  27. // expose all functions for testing purposes
  28. if (init.debug) {
  29. interface.mergeHeaders = mergeHeaders;
  30. interface.checkForValidStringValue = checkForValidStringValue;
  31. interface.checkKeyValuePairObject = checkKeyValuePairObject;
  32. interface.checkHttpMethod = checkHttpMethod;
  33. interface.checkResponseType = checkResponseType;
  34. interface.checkHeadersObject = checkHeadersObject;
  35. interface.checkParamsObject = checkParamsObject;
  36. interface.resolveCookieString = resolveCookieString;
  37. interface.createFileEntry = createFileEntry;
  38. interface.getCookieHeader = getCookieHeader;
  39. interface.getMatchingHostHeaders = getMatchingHostHeaders;
  40. interface.getAllowedDataTypes = getAllowedDataTypes;
  41. }
  42. return interface;
  43. // Thanks Mozilla: https://developer.mozilla.org/en-US/docs/Web/API/WindowBase64/Base64_encoding_and_decoding#The_.22Unicode_Problem.22
  44. function b64EncodeUnicode(str) {
  45. return btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g, function (match, p1) {
  46. return String.fromCharCode('0x' + p1);
  47. }));
  48. }
  49. function mergeHeaders(globalHeaders, localHeaders) {
  50. var globalKeys = Object.keys(globalHeaders);
  51. var key;
  52. for (var i = 0; i < globalKeys.length; i++) {
  53. key = globalKeys[i];
  54. if (!localHeaders.hasOwnProperty(key)) {
  55. localHeaders[key] = globalHeaders[key];
  56. }
  57. }
  58. return localHeaders;
  59. }
  60. function checkForValidStringValue(list, value, onInvalidValueMessage) {
  61. if (jsUtil.getTypeOf(value) !== 'String') {
  62. throw new Error(onInvalidValueMessage + ' ' + list.join(', '));
  63. }
  64. value = value.trim().toLowerCase();
  65. if (list.indexOf(value) === -1) {
  66. throw new Error(onInvalidValueMessage + ' ' + list.join(', '));
  67. }
  68. return value;
  69. }
  70. function checkKeyValuePairObject(obj, allowedChildren, onInvalidValueMessage) {
  71. if (jsUtil.getTypeOf(obj) !== 'Object') {
  72. throw new Error(onInvalidValueMessage);
  73. }
  74. var keys = Object.keys(obj);
  75. for (var i = 0; i < keys.length; i++) {
  76. if (allowedChildren.indexOf(jsUtil.getTypeOf(obj[keys[i]])) === -1) {
  77. throw new Error(onInvalidValueMessage);
  78. }
  79. }
  80. return obj;
  81. }
  82. function checkArray(array, allowedDataTypes, onInvalidValueMessage) {
  83. if (jsUtil.getTypeOf(array) !== 'Array') {
  84. throw new Error(onInvalidValueMessage);
  85. }
  86. for (var i = 0; i < array.length; ++i) {
  87. if (allowedDataTypes.indexOf(jsUtil.getTypeOf(array[i])) === -1) {
  88. throw new Error(onInvalidValueMessage);
  89. }
  90. }
  91. return array;
  92. }
  93. function checkHttpMethod(method) {
  94. return checkForValidStringValue(validHttpMethods, method, messages.INVALID_HTTP_METHOD);
  95. }
  96. function checkResponseType(type) {
  97. return checkForValidStringValue(validResponseTypes, type, messages.INVALID_RESPONSE_TYPE);
  98. }
  99. function checkSerializer(serializer) {
  100. return checkForValidStringValue(validSerializers, serializer, messages.INVALID_DATA_SERIALIZER);
  101. }
  102. function checkSSLCertMode(mode) {
  103. return checkForValidStringValue(validCertModes, mode, messages.INVALID_SSL_CERT_MODE);
  104. }
  105. function checkClientAuthMode(mode) {
  106. return checkForValidStringValue(validClientAuthModes, mode, messages.INVALID_CLIENT_AUTH_MODE);
  107. }
  108. function checkClientAuthOptions(mode, options) {
  109. options = options || {};
  110. // none
  111. if (mode === validClientAuthModes[0]) {
  112. return {
  113. alias: null,
  114. rawPkcs: null,
  115. pkcsPassword: ''
  116. };
  117. }
  118. if (jsUtil.getTypeOf(options) !== 'Object') {
  119. throw new Error(messages.INVALID_CLIENT_AUTH_OPTIONS);
  120. }
  121. // systemstore
  122. if (mode === validClientAuthModes[1]) {
  123. if (jsUtil.getTypeOf(options.alias) !== 'String'
  124. && jsUtil.getTypeOf(options.alias) !== 'Undefined') {
  125. throw new Error(messages.INVALID_CLIENT_AUTH_ALIAS);
  126. }
  127. return {
  128. alias: jsUtil.getTypeOf(options.alias) === 'Undefined' ? null : options.alias,
  129. rawPkcs: null,
  130. pkcsPassword: ''
  131. };
  132. }
  133. // buffer
  134. if (mode === validClientAuthModes[2]) {
  135. if (jsUtil.getTypeOf(options.rawPkcs) !== 'ArrayBuffer') {
  136. throw new Error(messages.INVALID_CLIENT_AUTH_RAW_PKCS);
  137. }
  138. if (jsUtil.getTypeOf(options.pkcsPassword) !== 'String') {
  139. throw new Error(messages.INVALID_CLIENT_AUTH_PKCS_PASSWORD);
  140. }
  141. return {
  142. alias: null,
  143. rawPkcs: options.rawPkcs,
  144. pkcsPassword: options.pkcsPassword
  145. }
  146. }
  147. }
  148. function checkForBlacklistedHeaderKey(key) {
  149. if (key.toLowerCase() === 'cookie') {
  150. throw new Error(messages.ADDING_COOKIES_NOT_SUPPORTED);
  151. }
  152. return key;
  153. }
  154. function checkForInvalidHeaderValue(value) {
  155. var type = jsUtil.getTypeOf(value);
  156. if (type !== 'String' && type !== 'Null') {
  157. throw new Error(messages.INVALID_HEADER_VALUE);
  158. }
  159. return value;
  160. }
  161. function checkTimeoutValue(timeout) {
  162. if (jsUtil.getTypeOf(timeout) !== 'Number' || timeout < 0) {
  163. throw new Error(messages.INVALID_TIMEOUT_VALUE);
  164. }
  165. return timeout;
  166. }
  167. function checkFollowRedirectValue(follow) {
  168. if (jsUtil.getTypeOf(follow) !== 'Boolean') {
  169. throw new Error(messages.INVALID_FOLLOW_REDIRECT_VALUE);
  170. }
  171. return follow;
  172. }
  173. function checkHeadersObject(headers) {
  174. return checkKeyValuePairObject(headers, ['String'], messages.TYPE_MISMATCH_HEADERS);
  175. }
  176. function checkParamsObject(params) {
  177. return checkKeyValuePairObject(params, ['String', 'Array'], messages.TYPE_MISMATCH_PARAMS);
  178. }
  179. function checkDownloadFilePath(filePath) {
  180. if (!filePath || jsUtil.getTypeOf(filePath) !== 'String') {
  181. throw new Error(messages.INVALID_DOWNLOAD_FILE_PATH);
  182. }
  183. return filePath;
  184. }
  185. function checkUploadFileOptions(filePaths, names) {
  186. if (jsUtil.getTypeOf(filePaths) === 'String') {
  187. filePaths = [filePaths];
  188. }
  189. if (jsUtil.getTypeOf(names) === 'String') {
  190. names = [names];
  191. }
  192. var opts = {
  193. filePaths: checkArray(filePaths, ['String'], messages.TYPE_MISMATCH_FILE_PATHS),
  194. names: checkArray(names, ['String'], messages.TYPE_MISMATCH_NAMES)
  195. };
  196. if (!opts.filePaths.length) {
  197. throw new Error(messages.EMPTY_FILE_PATHS);
  198. }
  199. if (!opts.names.length) {
  200. throw new Error(messages.EMPTY_NAMES);
  201. }
  202. return opts;
  203. }
  204. function resolveCookieString(headers) {
  205. var keys = Object.keys(headers || {});
  206. for (var i = 0; i < keys.length; ++i) {
  207. if (keys[i].match(/^set-cookie$/i)) {
  208. return headers[keys[i]];
  209. }
  210. }
  211. return null;
  212. }
  213. function createFileEntry(rawEntry) {
  214. var entry = new (require('cordova-plugin-file.FileEntry'))();
  215. entry.isDirectory = rawEntry.isDirectory;
  216. entry.isFile = rawEntry.isFile;
  217. entry.name = rawEntry.name;
  218. entry.fullPath = rawEntry.fullPath;
  219. entry.filesystem = new FileSystem(rawEntry.filesystemName || (rawEntry.filesystem == global.PERSISTENT ? 'persistent' : 'temporary'));
  220. entry.nativeURL = rawEntry.nativeURL;
  221. return entry;
  222. }
  223. function injectCookieHandler(url, cb) {
  224. return function (response) {
  225. cookieHandler.setCookieFromString(url, resolveCookieString(response.headers));
  226. cb(response);
  227. }
  228. }
  229. function injectRawResponseHandler(responseType, success, failure) {
  230. return function (response) {
  231. var dataType = jsUtil.getTypeOf(response.data);
  232. // don't need post-processing if it's already binary type (on browser platform)
  233. if (dataType === 'ArrayBuffer' || dataType === 'Blob') {
  234. return success(response);
  235. }
  236. try {
  237. // json
  238. if (responseType === validResponseTypes[1]) {
  239. response.data = response.data === ''
  240. ? undefined
  241. : JSON.parse(response.data);
  242. }
  243. // arraybuffer
  244. else if (responseType === validResponseTypes[2]) {
  245. response.data = response.data === ''
  246. ? null
  247. : base64.toArrayBuffer(response.data);
  248. }
  249. // blob
  250. else if (responseType === validResponseTypes[3]) {
  251. if (response.data === '') {
  252. response.data = null;
  253. } else {
  254. var buffer = base64.toArrayBuffer(response.data);
  255. var type = response.headers['content-type'] || '';
  256. var blob = new Blob([buffer], { type: type });
  257. response.data = blob;
  258. }
  259. }
  260. success(response);
  261. } catch (error) {
  262. failure({
  263. status: errorCodes.POST_PROCESSING_FAILED,
  264. error: messages.POST_PROCESSING_FAILED + ' ' + error.message,
  265. url: response.url,
  266. headers: response.headers
  267. });
  268. }
  269. }
  270. }
  271. function injectFileEntryHandler(cb) {
  272. return function (response) {
  273. cb(createFileEntry(response.file));
  274. }
  275. }
  276. function getCookieHeader(url) {
  277. var cookieString = cookieHandler.getCookieString(url);
  278. if (cookieString.length) {
  279. return { Cookie: cookieHandler.getCookieString(url) };
  280. }
  281. return {};
  282. }
  283. function getMatchingHostHeaders(url, headersList) {
  284. var matches = url.match(/^https?\:\/\/([^\/?#]+)(?:[\/?#]|$)/i);
  285. var domain = matches && matches[1];
  286. return headersList[domain] || null;
  287. }
  288. function getMergedHeaders(url, requestHeaders, predefinedHeaders) {
  289. var globalHeaders = predefinedHeaders['*'] || {};
  290. var hostHeaders = getMatchingHostHeaders(url, predefinedHeaders) || {};
  291. var mergedHeaders = mergeHeaders(globalHeaders, hostHeaders);
  292. mergedHeaders = mergeHeaders(mergedHeaders, requestHeaders);
  293. mergedHeaders = mergeHeaders(mergedHeaders, getCookieHeader(url));
  294. return mergedHeaders;
  295. }
  296. function getAllowedDataTypes(dataSerializer) {
  297. switch (dataSerializer) {
  298. case 'utf8':
  299. return ['String'];
  300. case 'urlencoded':
  301. return ['Object'];
  302. case 'json':
  303. return ['Array', 'Object'];
  304. case 'raw':
  305. return ['Uint8Array', 'ArrayBuffer'];
  306. default:
  307. return [];
  308. }
  309. }
  310. function getAllowedInstanceTypes(dataSerializer) {
  311. return dataSerializer === 'multipart' ? ['FormData'] : null;
  312. }
  313. function processData(data, dataSerializer, cb) {
  314. var currentDataType = jsUtil.getTypeOf(data);
  315. var allowedDataTypes = getAllowedDataTypes(dataSerializer);
  316. var allowedInstanceTypes = getAllowedInstanceTypes(dataSerializer);
  317. if (allowedInstanceTypes) {
  318. var isCorrectInstanceType = false;
  319. allowedInstanceTypes.forEach(function (type) {
  320. if ((global[type] && data instanceof global[type]) || (ponyfills[type] && data instanceof ponyfills[type])) {
  321. isCorrectInstanceType = true;
  322. }
  323. });
  324. if (!isCorrectInstanceType) {
  325. throw new Error(messages.INSTANCE_TYPE_MISMATCH_DATA + ' ' + allowedInstanceTypes.join(', '));
  326. }
  327. }
  328. if (!allowedInstanceTypes && allowedDataTypes.indexOf(currentDataType) === -1) {
  329. throw new Error(messages.TYPE_MISMATCH_DATA + ' ' + allowedDataTypes.join(', '));
  330. }
  331. switch (dataSerializer) {
  332. case 'utf8':
  333. return cb({ text: data });
  334. case 'raw':
  335. return cb(currentDataType === 'Uint8Array' ? data.buffer : data);
  336. case 'multipart':
  337. return processFormData(data, cb);
  338. default:
  339. return cb(data);
  340. }
  341. }
  342. function processFormData(data, cb) {
  343. dependencyValidator.checkBlobApi();
  344. dependencyValidator.checkFileReaderApi();
  345. dependencyValidator.checkTextEncoderApi();
  346. dependencyValidator.checkFormDataInstance(data);
  347. var textEncoder = new global.TextEncoder('utf8');
  348. var iterator = data.entries();
  349. var result = {
  350. buffers: [],
  351. names: [],
  352. fileNames: [],
  353. types: []
  354. };
  355. processFormDataIterator(iterator, textEncoder, result, cb);
  356. }
  357. function processFormDataIterator(iterator, textEncoder, result, onFinished) {
  358. var entry = iterator.next();
  359. if (entry.done) {
  360. return onFinished(result);
  361. }
  362. if (entry.value[1] instanceof global.Blob || entry.value[1] instanceof global.File) {
  363. var reader = new global.FileReader();
  364. reader.onload = function () {
  365. result.buffers.push(base64.fromArrayBuffer(reader.result));
  366. result.names.push(entry.value[0]);
  367. result.fileNames.push(entry.value[1].name !== undefined ? entry.value[1].name : 'blob');
  368. result.types.push(entry.value[1].type || '');
  369. processFormDataIterator(iterator, textEncoder, result, onFinished);
  370. };
  371. return reader.readAsArrayBuffer(entry.value[1]);
  372. }
  373. if (jsUtil.getTypeOf(entry.value[1]) === 'String') {
  374. result.buffers.push(base64.fromArrayBuffer(textEncoder.encode(entry.value[1]).buffer));
  375. result.names.push(entry.value[0]);
  376. result.fileNames.push(null);
  377. result.types.push('text/plain');
  378. return processFormDataIterator(iterator, textEncoder, result, onFinished)
  379. }
  380. // skip items which are not supported
  381. processFormDataIterator(iterator, textEncoder, result, onFinished);
  382. }
  383. function handleMissingCallbacks(successFn, failFn) {
  384. if (jsUtil.getTypeOf(successFn) !== 'Function') {
  385. throw new Error(messages.MANDATORY_SUCCESS);
  386. }
  387. if (jsUtil.getTypeOf(failFn) !== 'Function') {
  388. throw new Error(messages.MANDATORY_FAIL);
  389. }
  390. }
  391. function handleMissingOptions(options, globals) {
  392. options = options || {};
  393. return {
  394. data: jsUtil.getTypeOf(options.data) === 'Undefined' ? null : options.data,
  395. filePath: options.filePath,
  396. followRedirect: checkFollowRedirectValue(options.followRedirect || globals.followRedirect),
  397. headers: checkHeadersObject(options.headers || {}),
  398. method: checkHttpMethod(options.method || validHttpMethods[0]),
  399. name: options.name,
  400. params: checkParamsObject(options.params || {}),
  401. responseType: checkResponseType(options.responseType || validResponseTypes[0]),
  402. serializer: checkSerializer(options.serializer || globals.serializer),
  403. timeout: checkTimeoutValue(options.timeout || globals.timeout),
  404. };
  405. }
  406. };