How to use hasha - 10 common examples

To help you get started, we’ve selected a few hasha 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 dosyago / p2. / index.js View on Github external
app.post('/very-secure-manifest-convert', upload.single('pdf'), async (req, res) => {
  const {file:pdf} = req;

  // logging 
    log(req, {file:pdf.path});

  // hash check for duplicate files
    const hash = await hasha.fromFile(pdf.path);
    const viewUrl = `${req.protocol}://${req.get('host')}/uploads/${pdf.filename}.html`;
    console.log({hash});
    if ( State.Files.has(hash) ) {
      const existingViewUrl = State.Files.get(hash);
      log(req, {note:'File exists', hash, existingViewUrl});
      return res.end(existingViewUrl);
    } else {
      newFiles += 1;
      State.Files.set(hash, viewUrl);
      if ( newFiles % WAIT_NEW_FILES_BEFORE_DISK_SYNC == 0 ) {
        syncHashes(State.Files);
      }
    }

  // job start
  const subshell = spawn(CONVERTER, [pdf.path, uploadPath]);
github alwaysonlinetxm / rollup-plugin-fill-html / src / index.js View on Github external
fileList.forEach(node => {
				let { type, file } = node;
				let hash = '';
				let code = '';

				if (/\[hash\]/.test(file)) {
					if (file === destPath) {
						// data.code will remove the last line of the source code(//# sourceMappingURL=xxx), so it's needed to add this
						code = data.code + `//# sourceMappingURL=${basename(file)}.map`;
					} else {
						code = readFileSync(file).toString();
					}
					hash = hasha(code, { algorithm: 'md5' });
					// remove the file without hash
					unlinkSync(file);
					file = file.replace('[hash]', hash)
					writeFileSync(file, code);
				}

				const src = isURL(file) ? file : relative(firstDir, file);

				if (type === 'js') {
					let attrs = {src: src};
					let mode = node.mode || defaultmode;
					if (mode) attrs.type = mode;
					attrs = Object.entries(([key, val]) => `${key}="${val}"`).join(' ');
					const script = `\n`;
					// node.inject will cover the inject
					if (node.inject === 'head' || inject === 'head') {
github cypress-io / github-action / index.js View on Github external
restoreCache,
  saveCache
} = require('cache/lib/index')
const fs = require('fs')
const os = require('os')
const path = require('path')
const quote = require('quote')
const cliParser = require('argument-vector')()

const homeDirectory = os.homedir()

const useYarn = fs.existsSync('yarn.lock')
const lockFilename = useYarn
  ? 'yarn.lock'
  : 'package-lock.json'
const lockHash = hasha.fromFileSync(lockFilename)
const platformAndArch = `${process.platform}-${process.arch}`

// enforce the same NPM cache folder across different operating systems
const NPM_CACHE_FOLDER = path.join(homeDirectory, '.npm')
const NPM_CACHE = (() => {
  const o = {}
  let key = core.getInput('cache-key')

  if (!key) {
    if (useYarn) {
      key = `yarn-${platformAndArch}-${lockHash}`
    } else {
      key = `npm-${platformAndArch}-${lockHash}`
    }
  } else {
    console.log('using custom cache key "%s"', key)
github AlexxNB / svelte-docs / packages / core / builtins / svelte_preprocess_builtins.js View on Github external
function replaceBuiltins(text,filename){
  const used = new Set();
  let uid = 0;

  //current file identificator
  const fid = hasha(filename,{algorithm: 'md5'});
  
  //remve all examples by file id
  ExamplesStore.delete(new RegExp(`Ex_\\d+_${fid}`));
  
   
  // parse blocks in the file
  getBlocks(text).forEach(block => {
    
    if(block.type === 'example') {
      used.add('Example');

      let name = `Ex_${uid++}_${fid}`;
      text = text.replace(
        block.fragment,
        example_replacer(block.content,block.params,name)
      )
github sveltejs / svelte-hackernews / client / rollup.config.js View on Github external
css: componentStyles => {
				let styles = globalStyles.replace( '__components__', componentStyles );

				try {
					fs.mkdirSync( 'client/dist' );
				} catch ( err ) {
					// noop
				}

				if ( dev ) {
					fs.writeFileSync( `client/dist/main.css`, styles );
				} else {
					styles = new CleanCSS().minify( styles ).styles;

					const hash = hasha( styles, { algorithm: 'md5' });
					fs.writeFileSync( `client/dist/main.${hash}.css`, styles );
					fs.writeFileSync( `server/manifests/css.json`, JSON.stringify({ 'main.css': `client/dist/main.${hash}.css` }) );
				}
			}
		}),
github Yoctol / bottender / packages / bottender / src / cli / providers / messenger / attachment.js View on Github external
success: [],
      error: [],
      unchanged: [],
    };

    print(`Trying to upload ${files.length} files...`);

    for (let i = 0; i < files.length; i++) {
      const _uploadedFiles = jsonfile.readFileSync(pathOfMappingFile);
      const uploadedFiles = _uploadedFiles.messenger || {};

      const name = files[i];
      const basename = path.basename(name);

      const fileMeta = uploadedFiles[basename];
      const checksum = hasha.fromFileSync(name);

      let pageId;
      if (force || !fileMeta || checksum !== fileMeta.checksum) {
        try {
          if (!pageId) {
            // eslint-disable-next-line no-await-in-loop
            const pageInfo = await client.getPageInfo();
            pageId = pageInfo.id;
          }
          // eslint-disable-next-line no-await-in-loop
          const data = await client.uploadAttachment(
            getFileType(name),
            fs.createReadStream(name),
            {
              is_reusable: true,
            }
github Yoctol / bottender / src / cli / actions / uploadImages.js View on Github external
: path.resolve('uploaded-images.json');

  if (!fs.existsSync(pathOfMappingFile)) {
    jsonfile.writeFileSync(pathOfMappingFile, {});

    print(`initialize ${bold('uploaded-images.json')} for you`);
  }

  for (let i = 0; i < filenames.length; i++) {
    const uploadedImages = jsonfile.readFileSync(pathOfMappingFile);

    const name = filenames[i];
    const basename = path.basename(name);
    const imageMeta = uploadedImages[basename];

    const checksum = hasha.fromFileSync(name);
    if (!imageMeta || checksum !== imageMeta.checksum) {
      // overwrite
      const file = fs.readFileSync(name);
      try {
        const data = await manager.pushFile(
          container,
          shortid.generate(),
          file,
          {
            contentType: fileType(file).mime,
          }
        );
        jsonfile.writeFileSync(
          pathOfMappingFile,
          {
            ...uploadedImages,
github paazmaya / image-duplicate-remover / lib / read-image.js View on Github external
}
  catch (error) {
    return false;
  }

  /*
  let meta = identifyImage(filepath);
  if (!meta) {
    // Identifying via GM failed...
    meta = {};
  }
  */

  const meta = {};
  //const color = getPixelColor(filepath);
  const sha256 = hasha.fromFileSync(filepath, {
    algorithm: 'sha256'
  });
  const size = imageSize(filepath);

  const data = Object.assign({
    filepath: filepath,
    hash: sha256,
    filesize: size
  }, meta);

  return data;
};
github bahmutov / npm-install / index.js View on Github external
// @ts-check
const core = require('@actions/core')
const exec = require('@actions/exec')
const io = require('@actions/io')
const hasha = require('hasha')
const { restoreCache, saveCache } = require('cache/lib/index')
const fs = require('fs')
const os = require('os')
const path = require('path')
const quote = require('quote')

const homeDirectory = os.homedir()

const useYarn = fs.existsSync('yarn.lock')
const lockFilename = useYarn ? 'yarn.lock' : 'package-lock.json'
const lockHash = hasha.fromFileSync(lockFilename)
const platformAndArch = `${process.platform}-${process.arch}`

// enforce the same NPM cache folder across different operating systems
const NPM_CACHE_FOLDER = path.join(homeDirectory, '.npm')
const NPM_CACHE = (() => {
  const o = {}
  if (useYarn) {
    o.inputPath = path.join(homeDirectory, '.cache', 'yarn')
    o.restoreKeys = `yarn-${platformAndArch}-`
  } else {
    o.inputPath = NPM_CACHE_FOLDER
    o.restoreKeys = `npm-${platformAndArch}-`
  }
  o.primaryKey = o.restoreKeys + lockHash
  return o
})()
github garden-io / garden / garden-service / src / vcs / git.ts View on Github external
async hashObject(stats: Stats, path: string) {
    const stream = new PassThrough()
    const output = hasha.fromStream(stream, { algorithm: "sha1" })
    stream.push(`blob ${stats.size}\0`)

    if (stats.isSymbolicLink()) {
      // For symlinks, we follow git's behavior, which is to hash the link itself (i.e. the path it contains) as
      // opposed to the file/directory that it points to.
      stream.push(await readlink(path))
      stream.end()
    } else {
      createReadStream(path).pipe(stream)
    }

    return output
  }

hasha

Hashing made simple. Get the hash of a buffer/string/stream/file.

MIT
Latest version published 6 months ago

Package Health Score

76 / 100
Full package analysis