Compatilibilty for web and react-native

- Implemented own util.inherits function
- Moved functions which need the `fs` module to separate file
This commit is contained in:
max 2020-03-28 15:37:07 +01:00
parent 365cffdad1
commit 84f87899b4
7 changed files with 55 additions and 50 deletions

View File

@ -1,4 +1,4 @@
var util = require('util'), var util = require('../util'),
Match = require ('../match'); Match = require ('../match');

View File

@ -1,4 +1,4 @@
var util = require('util'), var util = require('../util'),
Match = require ('../match'); Match = require ('../match');
/** /**

View File

@ -1,4 +1,4 @@
var util = require('util'), var util = require('../util'),
Match = require ('../match'); Match = require ('../match');
/** /**

View File

@ -1,5 +1,5 @@
'use strict'; 'use strict';
var util = require('util'), var util = require('../util'),
Match = require ('../match'); Match = require ('../match');
/** /**

46
fs.js Normal file
View File

@ -0,0 +1,46 @@
var fs = require('fs');
var self = require('./index');
module.exports.detectFile = function(filepath, opts, cb) {
if (typeof opts === 'function') {
cb = opts;
opts = undefined;
}
var fd;
var handler = function(err, buffer) {
if (fd) {
fs.closeSync(fd);
}
if (err) return cb(err, null);
cb(null, self.detect(buffer, opts));
};
if (opts && opts.sampleSize) {
fd = fs.openSync(filepath, 'r'),
sample = Buffer.allocUnsafe(opts.sampleSize);
fs.read(fd, sample, 0, opts.sampleSize, null, function(err) {
handler(err, sample);
});
return;
}
fs.readFile(filepath, handler);
};
module.exports.detectFileSync = function(filepath, opts) {
if (opts && opts.sampleSize) {
var fd = fs.openSync(filepath, 'r'),
sample = Buffer.allocUnsafe(opts.sampleSize);
fs.readSync(fd, sample, 0, opts.sampleSize);
fs.closeSync(fd);
return self.detect(sample, opts);
}
return self.detect(fs.readFileSync(filepath), opts);
};

View File

@ -1,6 +1,3 @@
var fs = require('fs');
var utf8 = require('./encoding/utf8'), var utf8 = require('./encoding/utf8'),
unicode = require('./encoding/unicode'), unicode = require('./encoding/unicode'),
mbcs = require('./encoding/mbcs'), mbcs = require('./encoding/mbcs'),
@ -78,49 +75,6 @@ module.exports.detect = function(buffer, opts) {
} }
}; };
module.exports.detectFile = function(filepath, opts, cb) {
if (typeof opts === 'function') {
cb = opts;
opts = undefined;
}
var fd;
var handler = function(err, buffer) {
if (fd) {
fs.closeSync(fd);
}
if (err) return cb(err, null);
cb(null, self.detect(buffer, opts));
};
if (opts && opts.sampleSize) {
fd = fs.openSync(filepath, 'r'),
sample = Buffer.allocUnsafe(opts.sampleSize);
fs.read(fd, sample, 0, opts.sampleSize, null, function(err) {
handler(err, sample);
});
return;
}
fs.readFile(filepath, handler);
};
module.exports.detectFileSync = function(filepath, opts) {
if (opts && opts.sampleSize) {
var fd = fs.openSync(filepath, 'r'),
sample = Buffer.allocUnsafe(opts.sampleSize);
fs.readSync(fd, sample, 0, opts.sampleSize);
fs.closeSync(fd);
return self.detect(sample, opts);
}
return self.detect(fs.readFileSync(filepath), opts);
};
// Wrappers for the previous functions to return all encodings // Wrappers for the previous functions to return all encodings
module.exports.detectAll = function(buffer, opts) { module.exports.detectAll = function(buffer, opts) {
if (typeof opts !== 'object') { if (typeof opts !== 'object') {

5
util.js Normal file
View File

@ -0,0 +1,5 @@
module.exports.inherits = function(a, b) {
for (var key of Object.keys(b.prototype)) {
a.prototype[key] = b.prototype[key];
}
}