From 84f87899b449e6d15c0f4a013a4999b5d2b66266 Mon Sep 17 00:00:00 2001 From: max Date: Sat, 28 Mar 2020 15:37:07 +0100 Subject: [PATCH] Compatilibilty for web and react-native - Implemented own util.inherits function - Moved functions which need the `fs` module to separate file --- encoding/iso2022.js | 2 +- encoding/mbcs.js | 2 +- encoding/sbcs.js | 2 +- encoding/unicode.js | 2 +- fs.js | 46 +++++++++++++++++++++++++++++++++++++++++++++ index.js | 46 --------------------------------------------- util.js | 5 +++++ 7 files changed, 55 insertions(+), 50 deletions(-) create mode 100644 fs.js create mode 100644 util.js diff --git a/encoding/iso2022.js b/encoding/iso2022.js index afff88a..99d4fdb 100644 --- a/encoding/iso2022.js +++ b/encoding/iso2022.js @@ -1,4 +1,4 @@ -var util = require('util'), +var util = require('../util'), Match = require ('../match'); diff --git a/encoding/mbcs.js b/encoding/mbcs.js index 5aa1557..b6da630 100644 --- a/encoding/mbcs.js +++ b/encoding/mbcs.js @@ -1,4 +1,4 @@ -var util = require('util'), +var util = require('../util'), Match = require ('../match'); /** diff --git a/encoding/sbcs.js b/encoding/sbcs.js index 80d525e..e79c738 100644 --- a/encoding/sbcs.js +++ b/encoding/sbcs.js @@ -1,4 +1,4 @@ -var util = require('util'), +var util = require('../util'), Match = require ('../match'); /** diff --git a/encoding/unicode.js b/encoding/unicode.js index 6458d79..6c5a907 100644 --- a/encoding/unicode.js +++ b/encoding/unicode.js @@ -1,5 +1,5 @@ 'use strict'; -var util = require('util'), +var util = require('../util'), Match = require ('../match'); /** diff --git a/fs.js b/fs.js new file mode 100644 index 0000000..36a48a0 --- /dev/null +++ b/fs.js @@ -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); + }; + \ No newline at end of file diff --git a/index.js b/index.js index 91b2bec..26bd737 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,3 @@ - -var fs = require('fs'); - var utf8 = require('./encoding/utf8'), unicode = require('./encoding/unicode'), 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 module.exports.detectAll = function(buffer, opts) { if (typeof opts !== 'object') { diff --git a/util.js b/util.js new file mode 100644 index 0000000..037d986 --- /dev/null +++ b/util.js @@ -0,0 +1,5 @@ +module.exports.inherits = function(a, b) { + for (var key of Object.keys(b.prototype)) { + a.prototype[key] = b.prototype[key]; + } +}