Skip to content

Commit 3485157

Browse files
committed
chore: add support to read text files larger than 16MB
1 parent 35b53fd commit 3485157

File tree

4 files changed

+32
-15
lines changed

4 files changed

+32
-15
lines changed

src/extensibility/ExtensionManager.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ define(function (require, exports, module) {
7070
// never rejects
7171
return new Promise((resolve) => {
7272
const registryFile = FileSystem.getFileForPath(REGISTRY_CACHE_PATH);
73-
FileUtils.readAsText(registryFile)
73+
FileUtils.readAsText(registryFile, true, {ignoreFileSizeLimits: true})
7474
.done(resolve)
7575
.fail(function (err) {
7676
console.error(`Registry cache not found ${REGISTRY_CACHE_PATH}`, err);

src/file/FileUtils.js

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -70,22 +70,32 @@ define(function (require, exports, module) {
7070
* Asynchronously reads a file as UTF-8 encoded text.
7171
* @param {!File} file File to read
7272
* @param {boolean?} bypassCache - an optional argument, if specified will read from disc instead of using cache.
73+
* @param {object} [options]
74+
* @param {boolean} [options.ignoreFileSizeLimits]. Will read larger files than 16MB limit. will bypassCache +
75+
* won't cache if enabled.
76+
* @param {boolean} [options.doNotCache] will not cache if enabled. Auto-enabled if ignoreFileSizeLimits = true
7377
* @return {$.Promise} a jQuery promise that will be resolved with the
7478
* file's text content plus its timestamp, or rejected with a FileSystemError string
7579
* constant if the file can not be read.
7680
*/
77-
function readAsText(file, bypassCache) {
81+
function readAsText(file, bypassCache, options = {}) {
7882
const result = new $.Deferred();
83+
let doNotCache = options.doNotCache;
84+
if(options.ignoreFileSizeLimits) {
85+
bypassCache = true;
86+
doNotCache = true;
87+
}
7988

80-
file.read({bypassCache: bypassCache}, function (err, data, _encoding, stat) {
81-
if(!err && typeof data !== "string"){
82-
result.reject(FileSystemError.UNSUPPORTED_ENCODING);
83-
} else if (!err) {
84-
result.resolve(data, stat.mtime);
85-
} else {
86-
result.reject(err);
87-
}
88-
});
89+
file.read({ bypassCache: bypassCache, ignoreFileSizeLimits: options.ignoreFileSizeLimits, doNotCache},
90+
function (err, data, _encoding, stat) {
91+
if(!err && typeof data !== "string"){
92+
result.reject(FileSystemError.UNSUPPORTED_ENCODING);
93+
} else if (!err) {
94+
result.resolve(data, stat.mtime);
95+
} else {
96+
result.reject(err);
97+
}
98+
});
8999

90100
return result.promise();
91101
}

src/filesystem/File.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,12 @@ define(function (require, exports, module) {
9494
/**
9595
* Read a file.
9696
*
97-
* @param {Object=} options properties \{encoding: 'one of format supported here:
98-
* https://developer.mozilla.org/en-US/docs/Web/API/TextDecoder/encoding'}
97+
* @param {Object} options
98+
* @param {string} [options.encoding] 'one of format supported here:
99+
* https://developer.mozilla.org/en-US/docs/Web/API/TextDecoder/encoding'
100+
* @param {boolean} [options.ignoreFileSizeLimits] by default max file size that can be read is 16MB.
101+
* @param {boolean} [options.doNotCache] will not cache if enabled. Auto-enabled if ignoreFileSizeLimits = true
102+
*
99103
* @param {function (?string, string=, FileSystemStats=)} callback Callback that is passed the
100104
* FileSystemError string or the file's contents and its stats.
101105
*/
@@ -106,6 +110,9 @@ define(function (require, exports, module) {
106110
options.encoding = this._encoding;
107111
}
108112
options.encoding = options.encoding || this._encoding || "utf8";
113+
if(options.ignoreFileSizeLimits) {
114+
options.doNotCache = true;
115+
}
109116

110117
// We don't need to check isWatched() here because contents are only saved
111118
// for watched files. Note that we need to explicitly test this._contents

src/filesystem/impls/appshell/AppshellFileSystem.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,7 @@ define(function (require, exports, module) {
551551
* If both calls fail, the error from the read call is passed back.
552552
*
553553
* @param {string} path
554-
* @param {{encoding: string=, stat: FileSystemStats=}} options
554+
* @param {{encoding: string=, stat: FileSystemStats=, ignoreFileSizeLimits: boolean=}} options
555555
* @param {function(?string, string=, FileSystemStats=)} callback
556556
*/
557557
function readFile(path, options, callback) {
@@ -562,7 +562,7 @@ define(function (require, exports, module) {
562562
// callback to be executed when the call to stat completes
563563
// or immediately if a stat object was passed as an argument
564564
function doReadFile(stat) {
565-
if (stat.size > (FileUtils.MAX_FILE_SIZE)) {
565+
if (!options.ignoreFileSizeLimits && stat.size > (FileUtils.MAX_FILE_SIZE)) {
566566
callback(FileSystemError.EXCEEDS_MAX_FILE_SIZE);
567567
} else {
568568
appshell.fs.readFile(path, encoding, function (_err, _data, encoding, preserveBOM) {

0 commit comments

Comments
 (0)