How to use isbinaryfile - 10 common examples

To help you get started, we’ve selected a few isbinaryfile 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 ember-cli / ember-cli / lib / models / file-info.js View on Github external
let promptOptions = {
      type: 'expand',
      name: 'answer',
      default: false,
      message: `${chalk.red('Overwrite')} ${path}?`,
      choices: [
        { key: 'y', name: 'Yes, overwrite', value: 'overwrite' },
        { key: 'n', name: 'No, skip', value: 'skip' },
      ],
    };

    let outputPathIsFile = false;
    try { outputPathIsFile = fs.statSync(this.outputPath).isFile(); } catch (err) { /* ignore */ }

    let canDiff = (
      !isBinaryFile(this.inputPath) && (
        !outputPathIsFile ||
        !isBinaryFile(this.outputPath)
      )
    );

    if (canDiff) {
      promptOptions.choices.push({ key: 'd', name: 'Diff', value: 'diff' });

      if (canEdit()) {
        promptOptions.choices.push({ key: 'e', name: 'Edit', value: 'edit' });
      }
    }

    return this.ui.prompt(promptOptions)
      .then(response => response.answer);
  }
github FormidableLabs / publish-diff / lib / diff.js View on Github external
var isBinary = function (buffer) {
  return (
    // First, check magic numbers to see if we are a possible text file.
    //
    // _Note_: While a `sync`-named method, there's no actual sync I/O when
    // size parameter is provided.
    isBinaryFile.sync(buffer, buffer.length) ||

    // Then check if we have known non-text file types.
    !!fileType(buffer)
  );
};
github todogroup / repolinter / lib / file_system.js View on Github external
isBinaryFile (relativeFile) {
    const file = path.resolve(this.targetDir, relativeFile)
    try {
      return isBinaryFile.sync(file)
    } catch (e) {
      // File doesn't exist or is a directory, so it isn't a binary file
      if (e.message.includes('ENOENT')) {
        return false
      }
      throw e
    }
  }
github KuangPF / vue-cli-analysis / packages / @vue / cli / lib / GeneratorAPI.js View on Github external
function renderFile (name, data, ejsOptions) {
  if (isBinary.sync(name)) { // 检测是否为二进制文件
    return fs.readFileSync(name) // return buffer
  }
  const template = fs.readFileSync(name, 'utf-8')

  // custom template inheritance via yaml front matter.
  // ---
  // extend: 'source-file'
  // replace: !!js/regexp /some-regex/
  // OR
  // replace:
  //   - !!js/regexp /foo/
  //   - !!js/regexp /bar/
  // ---
  const yaml = require('yaml-front-matter')
  const parsed = yaml.loadFront(template)
  const content = parsed.__content
github teambit / bit / src / utils / eol.js View on Github external
function converts(text: string | Buffer, to: string) {
  if (Buffer.isBuffer(text)) {
    if (isBinaryFileSync(text)) return text; // don't touch binary files
    newLines.forEach((newLine) => {
      // $FlowFixMe text is Buffer here
      if (newLine !== to) text = replaceBuffer(text, newLine, to);
    });
    return text;
  }
  return text.toString().replace(newline, to);
}
github luoxue-victor / webpack-box / packages / cli / lib / GeneratorAPI.js View on Github external
function renderFile (name, data, ejsOptions) {
  if (isBinaryFileSync(name)) {
    return fs.readFileSync(name) // return buffer
  }
  const template = fs.readFileSync(name, 'utf-8')

  // custom template inheritance via yaml front matter.
  // ---
  // extend: 'source-file'
  // replace: !!js/regexp /some-regex/
  // OR
  // replace:
  //   - !!js/regexp /foo/
  //   - !!js/regexp /bar/
  // ---
  const yaml = require('yaml-front-matter')
  const parsed = yaml.loadFront(template)
  const content = parsed.__content
github SBoudrias / mem-fs-editor / lib / actions / copy-tpl.js View on Github external
function render(contents, filename, context, tplSettings) {
  let result;

  const contentsBuffer = Buffer.from(contents, 'binary');
  if (isBinaryFileSync(contentsBuffer, contentsBuffer.length)) {
    result = contentsBuffer;
  } else {
    result = ejs.render(
      contents.toString(),
      context,
      // Setting filename by default allow including partials.
      extend({filename: filename}, tplSettings)
    );
  }

  return result;
}
github electron-userland / electron-builder / packages / app-builder-lib / src / asar / unpackDetector.ts View on Github external
const pathInArchive = path.relative(rootForAppFilesWithoutAsar, getDestinationPath(file, fileSet))
    if (autoUnpackDirs.has(packageDirPathInArchive)) {
      // if package dir is unpacked, any file also unpacked
      addParents(pathInArchive, packageDirPathInArchive)
      continue
    }

    // https://github.com/electron-userland/electron-builder/issues/2679
    let shouldUnpack = false
    // ffprobe-static and ffmpeg-static are known packages to always unpack
    const moduleName = path.basename(packageDir)
    if (moduleName ===  "ffprobe-static" || moduleName === "ffmpeg-static" || isLibOrExe(file)) {
      shouldUnpack = true
    }
    else if (!file.includes(".", nextSlashIndex) && path.extname(file) === "") {
      shouldUnpack = await isBinaryFile(file)
    }

    if (!shouldUnpack) {
      continue
    }

    if (log.isDebugEnabled) {
      log.debug({file: pathInArchive, reason: "contains executable code"}, "not packed into asar archive")
    }

    addParents(pathInArchive, packageDirPathInArchive)
  }

  if (dirToCreate.size > 0) {
    await ensureDir(unpackedDest + path.sep + "node_modules")
    // child directories should be not created asynchronously - parent directories should be created first
github karma-runner / karma / lib / preprocessor.js View on Github external
return async function preprocess (file) {
    const buffer = await tryToRead(file.originalPath, log)
    const isBinary = await isBinaryFile(buffer, buffer.length)

    const preprocessorNames = Object.keys(config).reduce((ppNames, pattern) => {
      if (mm(file.originalPath, pattern, { dot: true })) {
        ppNames = _.union(ppNames, config[pattern])
      }
      return ppNames
    }, [])

    // Apply preprocessor priority.
    const preprocessors = preprocessorNames
      .map((name) => [name, preprocessorPriority[name] || 0])
      .sort((a, b) => b[1] - a[1])
      .map((duo) => duo[0])
      .reduce((preProcs, name) => {
        const p = instantiatePreprocessor(name)
github open-wc / open-wc / packages / es-dev-server / src / utils / utils.js View on Github external
if (isStream(ctx.body)) {
    // cache request path, see above
    // @ts-ignore
    if (ctx.body.path) {
      // @ts-ignore
      filePathsForRequests.set(ctx.request, ctx.body.path);
    }

    // a stream can only be read once, so after reading it assign
    // the string response to the body so that it can be accessed
    // again later
    try {
      const bodyBuffer = await getStream.buffer(ctx.body);
      const contentLength = Number(ctx.response.get('content-length'));

      if (await isBinaryFile(bodyBuffer, contentLength)) {
        ctx.body = bodyBuffer;
        throw new IsBinaryFileError();
      }

      const bodyString = bodyBuffer.toString();
      ctx.body = bodyString;
      return bodyString;
    } catch (error) {
      if (requestCanceled) {
        throw new RequestCancelledError();
      }
      throw error;
    }
  }

  return ctx.body;

isbinaryfile

Detects if a file is binary in Node.js. Similar to Perl's -B.

MIT
Latest version published 2 months ago

Package Health Score

85 / 100
Full package analysis