chore: eslint upgrade (#325)

This commit is contained in:
Norman Breau
2022-06-14 13:41:28 -03:00
committed by GitHub
parent 96aad0e22e
commit 6153f6d7bf
9 changed files with 1224 additions and 1619 deletions

2013
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -38,6 +38,6 @@
}
},
"devDependencies": {
"@cordova/eslint-config": "^3.0.0"
"@cordova/eslint-config": "^4.0.0"
}
}

View File

@@ -21,22 +21,22 @@
/* global Windows, WinJS */
var FTErr = require('./FileTransferError');
var ProgressEvent = require('cordova-plugin-file.ProgressEvent');
var FileUploadResult = require('cordova-plugin-file.FileUploadResult');
var FileProxy = require('cordova-plugin-file.FileProxy');
const FTErr = require('./FileTransferError');
const ProgressEvent = require('cordova-plugin-file.ProgressEvent');
const FileUploadResult = require('cordova-plugin-file.FileUploadResult');
const FileProxy = require('cordova-plugin-file.FileProxy');
var appData = Windows.Storage.ApplicationData.current;
const appData = Windows.Storage.ApplicationData.current;
var LINE_START = '--';
var LINE_END = '\r\n';
var BOUNDARY = '+++++';
const LINE_START = '--';
const LINE_END = '\r\n';
const BOUNDARY = '+++++';
var fileTransferOps = [];
const fileTransferOps = [];
// Some private helper functions, hidden by the module
function cordovaPathToNative (path) {
var cleanPath = String(path);
let cleanPath = String(path);
// turn / into \\
cleanPath = cleanPath.replace(/\//g, '\\');
// turn \\ into \
@@ -51,7 +51,7 @@ function nativePathToCordova (path) {
}
function alreadyCancelled (opId) {
var op = fileTransferOps[opId];
const op = fileTransferOps[opId];
return op && op.state === FileTransferOperation.CANCELLED;
}
@@ -62,21 +62,21 @@ function doUpload (upload, uploadId, filePath, server, successCallback, errorCal
}
// update internal TransferOperation object with newly created promise
var uploadOperation = upload.startAsync();
const uploadOperation = upload.startAsync();
fileTransferOps[uploadId].promise = uploadOperation;
uploadOperation.then(
function (result) {
// Update TransferOperation object with new state, delete promise property
// since it is not actual anymore
var currentUploadOp = fileTransferOps[uploadId];
const currentUploadOp = fileTransferOps[uploadId];
if (currentUploadOp) {
currentUploadOp.state = FileTransferOperation.DONE;
currentUploadOp.promise = null;
}
var response = result.getResponseInformation();
var ftResult = new FileUploadResult(result.progress.bytesSent, response.statusCode, '');
const response = result.getResponseInformation();
const ftResult = new FileUploadResult(result.progress.bytesSent, response.statusCode, '');
// if server's response doesn't contain any data, then resolve operation now
if (result.progress.bytesReceived === 0) {
@@ -85,7 +85,7 @@ function doUpload (upload, uploadId, filePath, server, successCallback, errorCal
}
// otherwise create a data reader, attached to response stream to get server's response
var reader = new Windows.Storage.Streams.DataReader(result.getResultStreamAt(0));
const reader = new Windows.Storage.Streams.DataReader(result.getResultStreamAt(0));
reader.loadAsync(result.progress.bytesReceived).then(function (size) {
ftResult.response = reader.readString(size);
successCallback(ftResult);
@@ -93,23 +93,23 @@ function doUpload (upload, uploadId, filePath, server, successCallback, errorCal
});
},
function (error) {
var source = nativePathToCordova(filePath);
const source = nativePathToCordova(filePath);
// Handle download error here.
// Wrap this routines into promise due to some async methods
var getTransferError = new WinJS.Promise(function (resolve) {
const getTransferError = new WinJS.Promise(function (resolve) {
if (error.message === 'Canceled') {
// If download was cancelled, message property will be specified
resolve(new FTErr(FTErr.ABORT_ERR, source, server, null, null, error));
} else {
// in the other way, try to get response property
var response = upload.getResponseInformation();
const response = upload.getResponseInformation();
if (!response) {
resolve(new FTErr(FTErr.CONNECTION_ERR, source, server));
} else {
var reader = new Windows.Storage.Streams.DataReader(upload.getResultStreamAt(0));
const reader = new Windows.Storage.Streams.DataReader(upload.getResultStreamAt(0));
reader.loadAsync(upload.progress.bytesReceived).then(function (size) {
var responseText = reader.readString(size);
const responseText = reader.readString(size);
resolve(new FTErr(FTErr.FILE_NOT_FOUND_ERR, source, server, response.statusCode, responseText, error));
reader.close();
});
@@ -119,7 +119,7 @@ function doUpload (upload, uploadId, filePath, server, successCallback, errorCal
// Update TransferOperation object with new state, delete promise property
// since it is not actual anymore
var currentUploadOp = fileTransferOps[uploadId];
const currentUploadOp = fileTransferOps[uploadId];
if (currentUploadOp) {
currentUploadOp.state = FileTransferOperation.CANCELLED;
currentUploadOp.promise = null;
@@ -131,7 +131,7 @@ function doUpload (upload, uploadId, filePath, server, successCallback, errorCal
});
},
function (evt) {
var progressEvent = new ProgressEvent('progress', {
const progressEvent = new ProgressEvent('progress', {
loaded: evt.progress.bytesSent,
total: evt.progress.totalBytesToSend,
target: evt.resultFile
@@ -151,7 +151,7 @@ FileTransferOperation.PENDING = 0;
FileTransferOperation.DONE = 1;
FileTransferOperation.CANCELLED = 2;
var HTTP_E_STATUS_NOT_MODIFIED = -2145844944;
const HTTP_E_STATUS_NOT_MODIFIED = -2145844944;
module.exports = {
/*
@@ -159,24 +159,24 @@ exec(win, fail, 'FileTransfer', 'upload',
[filePath, server, fileKey, fileName, mimeType, params, trustAllHosts, chunkedMode, headers, this._id, httpMethod]);
*/
upload: function (successCallback, errorCallback, options) {
var filePath = options[0];
var server = options[1];
var fileKey = options[2] || 'source';
var fileName = options[3];
var mimeType = options[4];
var params = options[5];
let filePath = options[0];
const server = options[1];
const fileKey = options[2] || 'source';
let fileName = options[3];
let mimeType = options[4];
const params = options[5];
// var trustAllHosts = options[6]; // todo
// var chunkedMode = options[7]; // todo
var headers = options[8] || {};
var uploadId = options[9];
var httpMethod = options[10];
const headers = options[8] || {};
const uploadId = options[9];
const httpMethod = options[10];
var isMultipart = typeof headers['Content-Type'] === 'undefined';
const isMultipart = typeof headers['Content-Type'] === 'undefined';
function stringToByteArray (str) {
var byteCharacters = atob(str);
var byteNumbers = new Array(byteCharacters.length);
for (var i = 0; i < byteCharacters.length; i++) {
const byteCharacters = atob(str);
const byteNumbers = new Array(byteCharacters.length);
for (let i = 0; i < byteCharacters.length; i++) {
byteNumbers[i] = byteCharacters.charCodeAt(i);
}
return new Uint8Array(byteNumbers);
@@ -190,11 +190,11 @@ exec(win, fail, 'FileTransfer', 'upload',
if (filePath.indexOf('data:') === 0 && filePath.indexOf('base64') !== -1) {
// First a DataWriter object is created, backed by an in-memory stream where
// the data will be stored.
var writer = Windows.Storage.Streams.DataWriter(new Windows.Storage.Streams.InMemoryRandomAccessStream());
const writer = Windows.Storage.Streams.DataWriter(new Windows.Storage.Streams.InMemoryRandomAccessStream());
writer.unicodeEncoding = Windows.Storage.Streams.UnicodeEncoding.utf8;
writer.byteOrder = Windows.Storage.Streams.ByteOrder.littleEndian;
var commaIndex = filePath.indexOf(',');
const commaIndex = filePath.indexOf(',');
if (commaIndex === -1) {
errorCallback(new FTErr(FTErr.INVALID_URL_ERR, fileName, server, null, null, 'No comma in data: URI'));
return;
@@ -203,12 +203,12 @@ exec(win, fail, 'FileTransfer', 'upload',
// Create internal download operation object
fileTransferOps[uploadId] = new FileTransferOperation(FileTransferOperation.PENDING, null);
var fileDataString = filePath.substr(commaIndex + 1);
const fileDataString = filePath.substr(commaIndex + 1);
// setting request headers for uploader
var uploader = new Windows.Networking.BackgroundTransfer.BackgroundUploader();
const uploader = new Windows.Networking.BackgroundTransfer.BackgroundUploader();
uploader.method = httpMethod;
for (var header in headers) {
for (const header in headers) {
if (Object.prototype.hasOwnProperty.call(headers, header)) {
uploader.setRequestHeader(header, headers[header]);
}
@@ -216,8 +216,8 @@ exec(win, fail, 'FileTransfer', 'upload',
if (isMultipart) {
// adding params supplied to request payload
var multipartParams = '';
for (var key in params) {
let multipartParams = '';
for (const key in params) {
if (Object.prototype.hasOwnProperty.call(params, key)) {
multipartParams += LINE_START + BOUNDARY + LINE_END;
multipartParams += 'Content-Disposition: form-data; name="' + key + '"';
@@ -227,12 +227,12 @@ exec(win, fail, 'FileTransfer', 'upload',
}
}
var multipartFile = LINE_START + BOUNDARY + LINE_END;
let multipartFile = LINE_START + BOUNDARY + LINE_END;
multipartFile += 'Content-Disposition: form-data; name="file";';
multipartFile += ' filename="' + fileName + '"' + LINE_END;
multipartFile += 'Content-Type: ' + mimeType + LINE_END + LINE_END;
var bound = LINE_END + LINE_START + BOUNDARY + LINE_START + LINE_END;
const bound = LINE_END + LINE_START + BOUNDARY + LINE_START + LINE_END;
uploader.setRequestHeader('Content-Type', 'multipart/form-data; boundary=' + BOUNDARY);
writer.writeString(multipartParams);
@@ -243,7 +243,7 @@ exec(win, fail, 'FileTransfer', 'upload',
writer.writeBytes(stringToByteArray(fileDataString));
}
var stream;
let stream;
// The call to store async sends the actual contents of the writer
// to the backing stream.
@@ -279,9 +279,9 @@ exec(win, fail, 'FileTransfer', 'upload',
}
// create download object. This will throw an exception if URL is malformed
var uri = new Windows.Foundation.Uri(server);
const uri = new Windows.Foundation.Uri(server);
var createUploadOperation;
let createUploadOperation;
try {
createUploadOperation = uploader.createUploadFromStreamAsync(uri, stream);
} catch (e) {
@@ -294,7 +294,7 @@ exec(win, fail, 'FileTransfer', 'upload',
doUpload(upload, uploadId, filePath, server, successCallback, errorCallback);
},
function (err) {
var errorObj = new FTErr(FTErr.INVALID_URL_ERR);
const errorObj = new FTErr(FTErr.INVALID_URL_ERR);
errorObj.exception = err;
errorCallback(errorObj);
}
@@ -340,23 +340,23 @@ exec(win, fail, 'FileTransfer', 'upload',
}
// setting request headers for uploader
var uploader = new Windows.Networking.BackgroundTransfer.BackgroundUploader();
const uploader = new Windows.Networking.BackgroundTransfer.BackgroundUploader();
uploader.method = httpMethod;
for (var header in headers) {
for (const header in headers) {
if (Object.prototype.hasOwnProperty.call(headers, header)) {
uploader.setRequestHeader(header, headers[header]);
}
}
// create download object. This will throw an exception if URL is malformed
var uri = new Windows.Foundation.Uri(server);
const uri = new Windows.Foundation.Uri(server);
var createUploadOperation;
let createUploadOperation;
try {
if (isMultipart) {
// adding params supplied to request payload
var transferParts = [];
for (var key in params) {
const transferParts = [];
for (const key in params) {
// Create content part for params only if value is specified because CreateUploadAsync fails otherwise
if (
Object.prototype.hasOwnProperty.call(params, key) &&
@@ -364,7 +364,7 @@ exec(win, fail, 'FileTransfer', 'upload',
params[key] !== undefined &&
params[key].toString() !== ''
) {
var contentPart = new Windows.Networking.BackgroundTransfer.BackgroundTransferContentPart();
const contentPart = new Windows.Networking.BackgroundTransfer.BackgroundTransferContentPart();
contentPart.setHeader('Content-Disposition', 'form-data; name="' + key + '"');
contentPart.setText(params[key]);
transferParts.push(contentPart);
@@ -372,7 +372,7 @@ exec(win, fail, 'FileTransfer', 'upload',
}
// Adding file to upload to request payload
var fileToUploadPart = new Windows.Networking.BackgroundTransfer.BackgroundTransferContentPart(fileKey, fileName);
const fileToUploadPart = new Windows.Networking.BackgroundTransfer.BackgroundTransferContentPart(fileKey, fileName);
fileToUploadPart.setHeader('Content-Type', mimeType);
fileToUploadPart.setFile(storageFile);
transferParts.push(fileToUploadPart);
@@ -391,7 +391,7 @@ exec(win, fail, 'FileTransfer', 'upload',
doUpload(upload, uploadId, filePath, server, successCallback, errorCallback);
},
function (err) {
var errorObj = new FTErr(FTErr.INVALID_URL_ERR);
const errorObj = new FTErr(FTErr.INVALID_URL_ERR);
errorObj.exception = err;
errorCallback(errorObj);
}
@@ -405,10 +405,10 @@ exec(win, fail, 'FileTransfer', 'upload',
// [source, target, trustAllHosts, id, headers]
download: function (successCallback, errorCallback, options) {
var source = options[0];
var target = options[1];
var downloadId = options[3];
var headers = options[4] || {};
const source = options[0];
let target = options[1];
const downloadId = options[3];
const headers = options[4] || {};
if (!target) {
errorCallback(new FTErr(FTErr.FILE_NOT_FOUND_ERR));
@@ -428,22 +428,22 @@ exec(win, fail, 'FileTransfer', 'upload',
}
target = cordovaPathToNative(target);
var path = target.substr(0, target.lastIndexOf('\\'));
var fileName = target.substr(target.lastIndexOf('\\') + 1);
const path = target.substr(0, target.lastIndexOf('\\'));
const fileName = target.substr(target.lastIndexOf('\\') + 1);
if (path === null || fileName === null) {
errorCallback(new FTErr(FTErr.FILE_NOT_FOUND_ERR));
return;
}
// Download to a temp file to avoid the file deletion on 304
// CB-7006 Empty file is created on file transfer if server response is 304
var tempFileName = '~' + fileName;
const tempFileName = '~' + fileName;
var download = null;
let download = null;
// Create internal download operation object
fileTransferOps[downloadId] = new FileTransferOperation(FileTransferOperation.PENDING, null);
var downloadCallback = function (storageFolder) {
const downloadCallback = function (storageFolder) {
storageFolder.createFileAsync(tempFileName, Windows.Storage.CreationCollisionOption.replaceExisting).then(
function (storageFile) {
if (alreadyCancelled(downloadId)) {
@@ -452,8 +452,8 @@ exec(win, fail, 'FileTransfer', 'upload',
}
// if download isn't cancelled, contunue with creating and preparing download operation
var downloader = new Windows.Networking.BackgroundTransfer.BackgroundDownloader();
for (var header in headers) {
const downloader = new Windows.Networking.BackgroundTransfer.BackgroundDownloader();
for (const header in headers) {
if (Object.prototype.hasOwnProperty.call(headers, header)) {
downloader.setRequestHeader(header, headers[header]);
}
@@ -461,7 +461,7 @@ exec(win, fail, 'FileTransfer', 'upload',
// create download object. This will throw an exception if URL is malformed
try {
var uri = Windows.Foundation.Uri(source);
const uri = Windows.Foundation.Uri(source);
download = downloader.createDownload(uri, storageFile);
} catch (e) {
// so we handle this and call errorCallback
@@ -469,7 +469,7 @@ exec(win, fail, 'FileTransfer', 'upload',
return;
}
var downloadOperation = download.startAsync();
const downloadOperation = download.startAsync();
// update internal TransferOperation object with newly created promise
fileTransferOps[downloadId].promise = downloadOperation;
@@ -477,7 +477,7 @@ exec(win, fail, 'FileTransfer', 'upload',
function () {
// Update TransferOperation object with new state, delete promise property
// since it is not actual anymore
var currentDownloadOp = fileTransferOps[downloadId];
const currentDownloadOp = fileTransferOps[downloadId];
if (currentDownloadOp) {
currentDownloadOp.state = FileTransferOperation.DONE;
currentDownloadOp.promise = null;
@@ -485,7 +485,7 @@ exec(win, fail, 'FileTransfer', 'upload',
storageFile.renameAsync(fileName, Windows.Storage.CreationCollisionOption.replaceExisting).done(
function () {
var nativeURI = storageFile.path
const nativeURI = storageFile.path
.replace(appData.localFolder.path, 'ms-appdata:///local')
.replace(appData.temporaryFolder.path, 'ms-appdata:///temp')
.replace(/\\/g, '/');
@@ -500,7 +500,7 @@ exec(win, fail, 'FileTransfer', 'upload',
);
},
function (error) {
var getTransferError = new WinJS.Promise(function (resolve) {
const getTransferError = new WinJS.Promise(function (resolve) {
// Handle download error here. If download was cancelled,
// message property will be specified
if (error.message === 'Canceled') {
@@ -509,7 +509,7 @@ exec(win, fail, 'FileTransfer', 'upload',
resolve(new FTErr(FTErr.NOT_MODIFIED_ERR, source, target, 304, null, error));
} else {
// in the other way, try to get response property
var response = download.getResponseInformation();
const response = download.getResponseInformation();
if (!response) {
resolve(new FTErr(FTErr.CONNECTION_ERR, source, target));
} else {
@@ -517,9 +517,9 @@ exec(win, fail, 'FileTransfer', 'upload',
resolve(new FTErr(FTErr.FILE_NOT_FOUND_ERR, source, target, response.statusCode, null, error));
return;
}
var reader = new Windows.Storage.Streams.DataReader(download.getResultStreamAt(0));
const reader = new Windows.Storage.Streams.DataReader(download.getResultStreamAt(0));
reader.loadAsync(download.progress.bytesReceived).then(function (bytesLoaded) {
var payload = reader.readString(bytesLoaded);
const payload = reader.readString(bytesLoaded);
resolve(
new FTErr(FTErr.FILE_NOT_FOUND_ERR, source, target, response.statusCode, payload, error)
);
@@ -530,7 +530,7 @@ exec(win, fail, 'FileTransfer', 'upload',
getTransferError.then(function (fileTransferError) {
// Update TransferOperation object with new state, delete promise property
// since it is not actual anymore
var currentDownloadOp = fileTransferOps[downloadId];
const currentDownloadOp = fileTransferOps[downloadId];
if (currentDownloadOp) {
currentDownloadOp.state = FileTransferOperation.CANCELLED;
currentDownloadOp.promise = null;
@@ -543,7 +543,7 @@ exec(win, fail, 'FileTransfer', 'upload',
});
},
function (evt) {
var progressEvent = new ProgressEvent('progress', {
const progressEvent = new ProgressEvent('progress', {
loaded: evt.progress.bytesReceived,
total: evt.progress.totalBytesToReceive,
target: evt.resultFile
@@ -562,15 +562,15 @@ exec(win, fail, 'FileTransfer', 'upload',
);
};
var fileNotFoundErrorCallback = function (error) {
const fileNotFoundErrorCallback = function (error) {
errorCallback(new FTErr(FTErr.FILE_NOT_FOUND_ERR, source, target, null, null, error));
};
Windows.Storage.StorageFolder.getFolderFromPathAsync(path).then(downloadCallback, function (error) {
// Handle non-existent directory
if (error.number === -2147024894) {
var parent = path.substr(0, path.lastIndexOf('\\'));
var folderNameToCreate = path.substr(path.lastIndexOf('\\') + 1);
const parent = path.substr(0, path.lastIndexOf('\\'));
const folderNameToCreate = path.substr(path.lastIndexOf('\\') + 1);
Windows.Storage.StorageFolder.getFolderFromPathAsync(parent).then(function (parentFolder) {
parentFolder.createFolderAsync(folderNameToCreate).then(downloadCallback, fileNotFoundErrorCallback);
@@ -582,10 +582,10 @@ exec(win, fail, 'FileTransfer', 'upload',
},
abort: function (successCallback, error, options) {
var fileTransferOpId = options[0];
const fileTransferOpId = options[0];
// Try to find transferOperation with id specified, and cancel its' promise
var currentOp = fileTransferOps[fileTransferOpId];
const currentOp = fileTransferOps[fileTransferOpId];
if (currentOp) {
currentOp.state = FileTransferOperation.CANCELLED;
currentOp.promise && currentOp.promise.cancel();

View File

@@ -21,13 +21,13 @@
*
*/
var path = require('path');
var fs = require('fs');
const path = require('path');
const fs = require('fs');
module.exports = function (context) {
function main () {
// get the file transfer server address from the specified variables
var fileTransferServerAddress = getFileTransferServerAddress(context) || getDefaultFileTransferServerAddress(context);
const fileTransferServerAddress = getFileTransferServerAddress(context) || getDefaultFileTransferServerAddress(context);
console.log('Tests will use the following file transfer server address: ' + fileTransferServerAddress);
console.log(
'If you\'re using cordova@6.3.1 and the above address is wrong at "platform add", don\'t worry, it\'ll fix itself on "cordova run" or "cordova prepare".'
@@ -38,10 +38,10 @@ module.exports = function (context) {
}
function getDefaultFileTransferServerAddress (context) {
var address = null;
var configNodes = context.opts.plugin.pluginInfo._et._root._children;
let address = null;
const configNodes = context.opts.plugin.pluginInfo._et._root._children;
for (var node in configNodes) {
for (const node in configNodes) {
if (configNodes[node].attrib.name === 'FILETRANSFER_SERVER_ADDRESS') {
address = configNodes[node].attrib.default;
}
@@ -51,13 +51,13 @@ module.exports = function (context) {
}
function getFileTransferServerAddress (context) {
var platformJsonFile = path.join(
const platformJsonFile = path.join(
context.opts.projectRoot,
'platforms',
context.opts.platforms[0],
context.opts.platforms[0] + '.json'
);
var platformJson = JSON.parse(fs.readFileSync(platformJsonFile, 'utf8'));
const platformJson = JSON.parse(fs.readFileSync(platformJsonFile, 'utf8'));
if (
platformJson &&
@@ -72,12 +72,12 @@ module.exports = function (context) {
}
function writeFileTransferOptions (address, context) {
for (var p in context.opts.paths) {
var ftOpts = {
for (const p in context.opts.paths) {
const ftOpts = {
serverAddress: address
};
var ftOptsString = JSON.stringify(ftOpts);
var ftOptsFile = path.join(context.opts.paths[p], 'fileTransferOpts.json');
const ftOptsString = JSON.stringify(ftOpts);
const ftOptsFile = path.join(context.opts.paths[p], 'fileTransferOpts.json');
fs.writeFileSync(ftOptsFile, ftOptsString, 'utf8');
}
}

View File

@@ -1,7 +1,7 @@
const http = require('http');
const stringify = require('json-stringify-safe');
const Busboy = require('busboy');
var { Iconv } = require('iconv');
const { Iconv } = require('iconv');
const port = process.env.PORT || 5000;
const DIRECT_UPLOAD_LIMIT = 85; // bytes
@@ -18,7 +18,7 @@ function parseMultipartForm (req, res, finishCb) {
const busboy = new Busboy({ headers: req.headers });
busboy.on('file', function (fieldname, file, filename, encoding, mimetype) {
var currentFile = { size: 0 };
const currentFile = { size: 0 };
file.on('data', function (data) {
currentFile.name = filename;
@@ -62,7 +62,7 @@ function respondWithParsedForm (req, res, parseResultObj) {
function respondWithParsedFormNonUTF (req, res, parseResultObj) {
parseResultObj.latin1Symbols = LATIN1_SYMBOLS;
var buffer = iconv.convert(stringify(parseResultObj));
const buffer = iconv.convert(stringify(parseResultObj));
res.writeHead(200, { 'Content-Type': 'application/json' });
res.write(buffer);
res.end('\n');
@@ -111,7 +111,7 @@ http.createServer(function (req, res) {
parseMultipartForm(req, res, respondWithParsedForm);
} else {
console.log('direct upload');
var body = '';
let body = '';
req.on('data', function (chunk) {
body += chunk;
if (body.length > DIRECT_UPLOAD_LIMIT) {

File diff suppressed because it is too large Load Diff

View File

@@ -21,13 +21,13 @@
/* global cordova, FileSystem */
var argscheck = require('cordova/argscheck');
var exec = require('cordova/exec');
var FileTransferError = require('./FileTransferError');
var ProgressEvent = require('cordova-plugin-file.ProgressEvent');
const argscheck = require('cordova/argscheck');
const exec = require('cordova/exec');
const FileTransferError = require('./FileTransferError');
const ProgressEvent = require('cordova-plugin-file.ProgressEvent');
function newProgressEvent (result) {
var pe = new ProgressEvent();
const pe = new ProgressEvent();
pe.lengthComputable = result.lengthComputable;
pe.loaded = result.loaded;
pe.total = result.total;
@@ -35,24 +35,24 @@ function newProgressEvent (result) {
}
function getUrlCredentials (urlString) {
var credentialsPattern = /^https?:\/\/(?:(?:(([^:@/]*)(?::([^@/]*))?)?@)?([^:/?#]*)(?::(\d*))?).*$/;
var credentials = credentialsPattern.exec(urlString);
const credentialsPattern = /^https?:\/\/(?:(?:(([^:@/]*)(?::([^@/]*))?)?@)?([^:/?#]*)(?::(\d*))?).*$/;
const credentials = credentialsPattern.exec(urlString);
return credentials && credentials[1];
}
function getBasicAuthHeader (urlString) {
var header = null;
let header = null;
// This is changed due to MS Windows doesn't support credentials in http uris
// so we detect them by regexp and strip off from result url
// Proof: http://social.msdn.microsoft.com/Forums/windowsapps/en-US/a327cf3c-f033-4a54-8b7f-03c56ba3203f/windows-foundation-uri-security-problem
if (window.btoa) {
var credentials = getUrlCredentials(urlString);
const credentials = getUrlCredentials(urlString);
if (credentials) {
var authHeader = 'Authorization';
var authHeaderValue = 'Basic ' + window.btoa(credentials);
const authHeader = 'Authorization';
const authHeaderValue = 'Basic ' + window.btoa(credentials);
header = {
name: authHeader,
@@ -65,10 +65,10 @@ function getBasicAuthHeader (urlString) {
}
function convertHeadersToArray (headers) {
var result = [];
for (var header in headers) {
const result = [];
for (const header in headers) {
if (Object.prototype.hasOwnProperty.call(headers, header)) {
var headerValue = headers[header];
const headerValue = headers[header];
result.push({
name: header,
value: headerValue.toString()
@@ -78,13 +78,13 @@ function convertHeadersToArray (headers) {
return result;
}
var idCounter = 0;
let idCounter = 0;
/**
* FileTransfer uploads a file to a remote server.
* @constructor
*/
var FileTransfer = function () {
const FileTransfer = function () {
this._id = ++idCounter;
this.onprogress = null; // optional callback
};
@@ -102,14 +102,14 @@ var FileTransfer = function () {
FileTransfer.prototype.upload = function (filePath, server, successCallback, errorCallback, options, trustAllHosts) {
argscheck.checkArgs('ssFFO*', 'FileTransfer.upload', arguments);
// check for options
var fileKey = null;
var fileName = null;
var mimeType = null;
var params = null;
var chunkedMode = true;
var headers = null;
var httpMethod = null;
var basicAuthHeader = getBasicAuthHeader(server);
let fileKey = null;
let fileName = null;
let mimeType = null;
let params = null;
let chunkedMode = true;
let headers = null;
let httpMethod = null;
const basicAuthHeader = getBasicAuthHeader(server);
if (basicAuthHeader) {
server = server.replace(getUrlCredentials(server) + '@', '');
@@ -144,15 +144,15 @@ FileTransfer.prototype.upload = function (filePath, server, successCallback, err
params = params && convertHeadersToArray(params);
}
var fail =
const fail =
errorCallback &&
function (e) {
var error = new FileTransferError(e.code, e.source, e.target, e.http_status, e.body, e.exception);
const error = new FileTransferError(e.code, e.source, e.target, e.http_status, e.body, e.exception);
errorCallback(error);
};
var self = this;
var win = function (result) {
const self = this;
const win = function (result) {
if (typeof result.lengthComputable !== 'undefined') {
if (self.onprogress) {
self.onprogress(newProgressEvent(result));
@@ -189,9 +189,9 @@ FileTransfer.prototype.upload = function (filePath, server, successCallback, err
*/
FileTransfer.prototype.download = function (source, target, successCallback, errorCallback, trustAllHosts, options) {
argscheck.checkArgs('ssFF*', 'FileTransfer.download', arguments);
var self = this;
const self = this;
var basicAuthHeader = getBasicAuthHeader(source);
const basicAuthHeader = getBasicAuthHeader(source);
if (basicAuthHeader) {
source = source.replace(getUrlCredentials(source) + '@', '');
@@ -200,7 +200,7 @@ FileTransfer.prototype.download = function (source, target, successCallback, err
options.headers[basicAuthHeader.name] = basicAuthHeader.value;
}
var headers = null;
let headers = null;
if (options) {
headers = options.headers || null;
}
@@ -209,13 +209,13 @@ FileTransfer.prototype.download = function (source, target, successCallback, err
headers = convertHeadersToArray(headers);
}
var win = function (result) {
const win = function (result) {
if (typeof result.lengthComputable !== 'undefined') {
if (self.onprogress) {
return self.onprogress(newProgressEvent(result));
}
} else if (successCallback) {
var entry = null;
let entry = null;
if (result.isDirectory) {
entry = new (require('cordova-plugin-file.DirectoryEntry'))();
} else if (result.isFile) {
@@ -233,10 +233,10 @@ FileTransfer.prototype.download = function (source, target, successCallback, err
}
};
var fail =
const fail =
errorCallback &&
function (e) {
var error = new FileTransferError(e.code, e.source, e.target, e.http_status, e.body, e.exception);
const error = new FileTransferError(e.code, e.source, e.target, e.http_status, e.body, e.exception);
errorCallback(error);
};

View File

@@ -23,7 +23,7 @@
* FileTransferError
* @constructor
*/
var FileTransferError = function (code, source, target, status, body, exception) {
const FileTransferError = function (code, source, target, status, body, exception) {
this.code = code || null;
this.source = source || null;
this.target = target || null;

View File

@@ -21,38 +21,38 @@
/* global FileUploadResult */
var argscheck = require('cordova/argscheck');
var FileTransferError = require('./FileTransferError');
const argscheck = require('cordova/argscheck');
const FileTransferError = require('./FileTransferError');
function getParentPath (filePath) {
var pos = filePath.lastIndexOf('/');
const pos = filePath.lastIndexOf('/');
return filePath.substring(0, pos + 1);
}
function getFileName (filePath) {
var pos = filePath.lastIndexOf('/');
const pos = filePath.lastIndexOf('/');
return filePath.substring(pos + 1);
}
function getUrlCredentials (urlString) {
var credentialsPattern = /^https?:\/\/(?:(?:(([^:@/]*)(?::([^@/]*))?)?@)?([^:/?#]*)(?::(\d*))?).*$/;
var credentials = credentialsPattern.exec(urlString);
const credentialsPattern = /^https?:\/\/(?:(?:(([^:@/]*)(?::([^@/]*))?)?@)?([^:/?#]*)(?::(\d*))?).*$/;
const credentials = credentialsPattern.exec(urlString);
return credentials && credentials[1];
}
function getBasicAuthHeader (urlString) {
var header = null;
let header = null;
// This is changed due to MS Windows doesn't support credentials in http uris
// so we detect them by regexp and strip off from result url
// Proof: http://social.msdn.microsoft.com/Forums/windowsapps/en-US/a327cf3c-f033-4a54-8b7f-03c56ba3203f/windows-foundation-uri-security-problem
if (window.btoa) {
var credentials = getUrlCredentials(urlString);
const credentials = getUrlCredentials(urlString);
if (credentials) {
var authHeader = 'Authorization';
var authHeaderValue = 'Basic ' + window.btoa(credentials);
const authHeader = 'Authorization';
const authHeaderValue = 'Basic ' + window.btoa(credentials);
header = {
name: authHeader,
@@ -68,15 +68,15 @@ function checkURL (url) {
return url.indexOf(' ') === -1;
}
var idCounter = 0;
let idCounter = 0;
var transfers = {};
const transfers = {};
/**
* FileTransfer uploads a file to a remote server.
* @constructor
*/
var FileTransfer = function () {
const FileTransfer = function () {
this._id = ++idCounter;
this.onprogress = null; // optional callback
};
@@ -106,32 +106,32 @@ FileTransfer.prototype.upload = function (filePath, server, successCallback, err
options = options || {};
var fileKey = options.fileKey || 'file';
var fileName = options.fileName || 'image.jpg';
var mimeType = options.mimeType || 'image/jpeg';
var params = options.params || {};
var withCredentials = options.withCredentials || false;
const fileKey = options.fileKey || 'file';
const fileName = options.fileName || 'image.jpg';
const mimeType = options.mimeType || 'image/jpeg';
const params = options.params || {};
const withCredentials = options.withCredentials || false;
// var chunkedMode = !!options.chunkedMode; // Not supported
var headers = options.headers || {};
var httpMethod = options.httpMethod && options.httpMethod.toUpperCase() === 'PUT' ? 'PUT' : 'POST';
const headers = options.headers || {};
const httpMethod = options.httpMethod && options.httpMethod.toUpperCase() === 'PUT' ? 'PUT' : 'POST';
var basicAuthHeader = getBasicAuthHeader(server);
const basicAuthHeader = getBasicAuthHeader(server);
if (basicAuthHeader) {
server = server.replace(getUrlCredentials(server) + '@', '');
headers[basicAuthHeader.name] = basicAuthHeader.value;
}
var that = this;
var xhr = (transfers[this._id] = new XMLHttpRequest());
const that = this;
const xhr = (transfers[this._id] = new XMLHttpRequest());
xhr.withCredentials = withCredentials;
var fail =
const fail =
errorCallback &&
function (code, status, response) {
if (transfers[this._id]) {
delete transfers[this._id];
}
var error = new FileTransferError(code, filePath, server, status, response);
const error = new FileTransferError(code, filePath, server, status, response);
if (errorCallback) {
errorCallback(error);
}
@@ -142,14 +142,14 @@ FileTransfer.prototype.upload = function (filePath, server, successCallback, err
function (entry) {
entry.file(
function (file) {
var reader = new FileReader();
const reader = new FileReader();
reader.onloadend = function () {
var blob = new Blob([this.result], { type: mimeType });
const blob = new Blob([this.result], { type: mimeType });
// Prepare form data to send to server
var fd = new FormData();
const fd = new FormData();
fd.append(fileKey, blob, fileName);
for (var prop in params) {
for (const prop in params) {
if (Object.prototype.hasOwnProperty.call(params, prop)) {
fd.append(prop, params[prop]);
}
@@ -158,7 +158,7 @@ FileTransfer.prototype.upload = function (filePath, server, successCallback, err
xhr.open(httpMethod, server);
// Fill XHR headers
for (var header in headers) {
for (const header in headers) {
if (Object.prototype.hasOwnProperty.call(headers, header)) {
xhr.setRequestHeader(header, headers[header]);
}
@@ -167,7 +167,7 @@ FileTransfer.prototype.upload = function (filePath, server, successCallback, err
xhr.onload = function () {
// 2xx codes are valid
if (this.status >= 200 && this.status < 300) {
var result = new FileUploadResult();
const result = new FileUploadResult();
result.bytesSent = blob.size;
result.responseCode = this.status;
result.response = this.response;
@@ -242,19 +242,19 @@ FileTransfer.prototype.download = function (source, target, successCallback, err
options = options || {};
var headers = options.headers || {};
var withCredentials = options.withCredentials || false;
const headers = options.headers || {};
const withCredentials = options.withCredentials || false;
var basicAuthHeader = getBasicAuthHeader(source);
const basicAuthHeader = getBasicAuthHeader(source);
if (basicAuthHeader) {
source = source.replace(getUrlCredentials(source) + '@', '');
headers[basicAuthHeader.name] = basicAuthHeader.value;
}
var that = this;
var xhr = (transfers[this._id] = new XMLHttpRequest());
const that = this;
const xhr = (transfers[this._id] = new XMLHttpRequest());
xhr.withCredentials = withCredentials;
var fail =
const fail =
errorCallback &&
function (code, status, response) {
if (transfers[that._id]) {
@@ -263,24 +263,24 @@ FileTransfer.prototype.download = function (source, target, successCallback, err
// In XHR GET reqests we're setting response type to Blob
// but in case of error we need to raise event with plain text response
if (response instanceof Blob) {
var reader = new FileReader();
const reader = new FileReader();
reader.readAsText(response);
reader.onloadend = function (e) {
var error = new FileTransferError(code, source, target, status, e.target.result);
const error = new FileTransferError(code, source, target, status, e.target.result);
errorCallback(error);
};
} else {
var error = new FileTransferError(code, source, target, status, response);
const error = new FileTransferError(code, source, target, status, response);
errorCallback(error);
}
};
xhr.onload = function (e) {
var fileNotFound = function () {
const fileNotFound = function () {
fail(FileTransferError.FILE_NOT_FOUND_ERR);
};
var req = e.target;
const req = e.target;
// req.status === 0 is special case for local files with file:// URI scheme
if ((req.status === 200 || req.status === 0) && req.response) {
window.resolveLocalFileSystemURL(
@@ -336,7 +336,7 @@ FileTransfer.prototype.download = function (source, target, successCallback, err
xhr.open('GET', source, true);
for (var header in headers) {
for (const header in headers) {
if (Object.prototype.hasOwnProperty.call(headers, header)) {
xhr.setRequestHeader(header, headers[header]);
}