How to use istextorbinary - 10 common examples

To help you get started, we’ve selected a few istextorbinary examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github frctl / fractal / src / filesystem / entity.js View on Github external
this.relativeDir        = relativefileInfo.dir;

        this.pathSegments       = _.compact(this.path.split('/'));
        this.depth              = this.pathSegments.length;

        this.modified           = this.stat.mtime;

        this.order              = nameParts ? parseInt(nameParts[1], 10) : 10000; // Can't use Infinity as it converts to NULL on JSON stringification!
        this.hidden             = !! _.find(this.pathSegments, function(segment){
                                    return segment.charAt(0) === '_';
                                });

        if (this.isFile()) {
            this.raw = this.contents;
            this.lang = this.ext.replace(/^\./,'').toLowerCase();
            this.isBinary = tob.isBinarySync(this.relativePath, this.contents);
        }

        return this;
    };
github adamfowleruk / mljs / apps / mljsadmin-workplace / util / mljsadmin.js View on Github external
fs.readFile(settings.folder + "/" + file, function(err,data) {


            if (err) {
              //crapout(err);
              console.log("    - ERROR reading file prior to save: " + settings.folder + "/" + file);
              deferred2.resolve(settings.folder + "/" + file);
            } else {
              itob.isText(file,data,function (err,result) {
                if (result) {
                  data = data.toString(); // convert to string if utf8, otherwise leave as binary buffer
                }

              // actually upload the file once working

              var vf = settings.folder;
              if (settings.stripBaseFolder) {
                vf = settings.folder.substring(base.length + 1);
              }
              /*if (0 == vf.indexOf("/")) {
                vf = vf.substring(1);
              }*/
              if (0 != vf.indexOf("/") && vf.length != 0) {
                vf = "/" + vf;
              }
github codesandbox / codesandbox-importers / packages / import-utils / src / is-text.ts View on Github external
return new Promise((resolve, reject) => {
    _isText(filename, buffer, (err: Error, result: boolean) => {
      if (err) {
        return reject(err);
      }

      // We don't support null bytes in the database with postgres,
      // so we need to mark it as binary if there are null bytes
      const hasNullByte = buffer.toString().includes("\0");

      resolve(
        result &&
          !FILE_LOADER_REGEX.test(filename) &&
          !isTooBig(buffer) &&
          !hasNullByte
      );
    });
  });
github frctl / fractal / src / core / fs.js View on Github external
'use strict';

const Promise = require('bluebird');
const Path = require('path');
const co = require('co');
const _ = require('lodash');
const fs = Promise.promisifyAll(require('fs'));
const readFile = Promise.promisify(fs.readFile);
const isBinary = Promise.promisify(require('istextorbinary').isBinary);
const utils = require('./utils');
const glob = require('globby');

const notBinary = ['.nunj', '.nunjucks', '.hbs', '.handlebars', '.jsx', '.twig']; // TODO: handle this in a scalable, extendable way

module.exports = {

    describe(dir, relDir, filter) {
        filter = filter || (filePath => !(/(^|\/)\.[^\/\.]/g).test(filePath));

        return dirscribe(dir, {
            filter: filter,
            after: files => _.orderBy(files, ['isDirectory', 'order', 'path'], ['desc', 'asc', 'asc']),
            build: build,
        });
github x-orpheus / nei-toolkit / lib / nei / template.js View on Github external
files.forEach(file =>{
      let fileName = path.join(this.output, path.relative(this.path,file));
      const buffer = fs.readFileSync(file);
      let shouldCompile = true;

      if(!isTextOrBinary.isTextSync(fileName, buffer)){//如果测试为binary文件,就不解析content,当然文件名还是得解析的
          logger.log("debug", {message: `文件${fileName}为二进制文件,不解析文件内容,直接复制`});
          shouldCompile = false;
      }
      //解析文件名
      try {
        // 使用strict模式,会提示未定义field错误
        fileName = Handlebars.compile(fileName, {strict: true})(this.data);
      } catch (e) {
        logger.log('error', {message: `生成文件${fileName} 时出现错误: ${e}`});
        fileName = Handlebars.compile(fileName)(this.data); // 非严格模式下重新编译
      }
      let fileContent;
      if(shouldCompile) {
        if (fs.existsSync(fileName) && !this.overwrite) {
          logger.log("debug", {message: `${fileName} 已存在,将保留该文件内容`});
          return;
github bevry-archive / feedr / source / plugins / string.js View on Github external
module.exports.parse = function parseString({ feed, data }, next) {
	// Detect
	const isText = require('istextorbinary').isTextSync(feed.basename, data)
	if (!isText) {
		next()
	} else {
		// Parse
		next(null, data.toString())
	}
}
github eggjs / egg-init / lib / init_command.js View on Github external
files.forEach(file => {
      const from = path.join(src, file);
      const to = path.join(targetDir, this.replaceTemplate(this.fileMapping[file] || file, locals));
      const content = fs.readFileSync(from);
      this.log('write to %s', to);

      // check if content is a text file
      const result = isTextOrBinary.isTextSync(from, content)
        ? this.replaceTemplate(content.toString('utf8'), locals)
        : content;

      mkdirp.sync(path.dirname(to));
      fs.writeFileSync(to, result);
    });
    return files;
github stefanpenner / async-disk-cache / index.js View on Github external
return async (fileStream) => {
    let value = await decompress(fileStream);

    // is Buffer or string? >:D
    if (!this.supportBuffer || require('istextorbinary').isTextSync(false, value)) {
      debug('convert to string');
      value = value.toString();
    } else {
      debug('keep data as Buffer');
    }

    return new CacheEntry(true, filePath, value);
  };
}
github hawkeyesec / scanner-cli / lib / util.js View on Github external
readFileSync: function (absolute) {
    const stat = fs.statSync(absolute)
    if (stat.size > 1000000) {
      console.warn(('[warn] File which exceeds 1MB limited detected: ' + absolute).yellow)
      return ''
    }
    const buffer = fs.readFileSync(absolute)
    if (!istext.isTextSync(absolute, buffer)) {
      console.warn(('[warn] Binary file detected when expected text: ' + absolute).yellow)
      return ''
    }
    const contents = buffer.toString().trim()
    return contents
  },
  readFile: function (absolute, done) {
github frctl / fractal / src / fs.js View on Github external
'use strict';

const Promise   = require('bluebird');
const Path      = require('path');
const co        = require('co');
const _         = require('lodash');
const dirscribe = require('@allmarkedup/dirscribe');
const fs        = require('fs');
const readFile  = Promise.promisify(fs.readFile);
const isBinary  = Promise.promisify(require('istextorbinary').isBinary);
const utils     = require('./utils');
const console       = require('./console');

module.exports = {

    describe(dir, noCache) {

        dir = Path.resolve(dir);

        return dirscribe(dir, {
            filter: filePath => !(/(^|\/)\.[^\/\.]/g).test(filePath),
            after:  files => _.orderBy(files, ['type', 'order', 'path'], ['desc', 'asc', 'asc']),
            build:  build
        });

        function build(filePath, stat) {

istextorbinary

Determine if a filename and/or buffer is text or binary. Smarter detection than the other solutions.

Artistic-2.0
Latest version published 4 months ago

Package Health Score

79 / 100
Full package analysis