npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@depack/cache

v1.2.1

Published

Computes Necessary Information To Cache A Module, And Allows To Check If It Has Been Updated.

Downloads

460

Readme

@depack/cache

npm version Node.js CI

@depack/cache Computes Necessary Information To Cache A Module, And Allows To Check If It Has Been Updated. Does so with static analysis of source (mtimes), Node.JS built-ins (names) and package dependencies (versions).

yarn add @depack/cache
npm i @depack/cache

Table Of Contents

API

The package is available by importing its default function:

import compare from '@depack/cache'

async compare(  path: string,  cache=: !Cache,  log=: !Function,): !CacheResult

Checks the entry file's mtime, calculates its dependencies and compare against the values stored in the cache object. When the result is negative, the cache object must be updated with the result returned by the function.

  • path* string: The path to the JS file.
  • cache !Cache (optional): Current cache object.
  • log !Function (optional): The function used to display what changes have been made to the dependencies.

!Object<string, CacheEntry> Cache: Interface for the cache object.

CacheEntry: A single entry in the cache.

| Name | Type | Description | | ---------- | ----------------------------- | ----------------------------------------------------------------------------------------------------------- | | mtime* | number | The mtime of the source file. | | hash* | !Array<string> | The analysis array containing strings with internal, external and built-in dependencies and their versions. |

CacheResult: The return type of the program.

| Name | Type | Description | | ------------ | ----------------------------- | ------------------------------------------------------------------------------------------------------ | | result* | boolean | Whether the result of the comparison was successful. | | reason | string | The reason for the failed comparison. Can be either: NO_CACHE, MTIME_CHANGE, HASH_CHANGE. | | mtime | number | The mtime of when the entry file was changed. | | currentMtime | number | The mtime from the cache passed to the function. | | hash | !Array<string> | The analysis array that is used for comparison and user-friendly display of what dependencies changed. | | md5* | string | The md5 of the hash array. |

There are multiple scenarios when using this package. Examples of each are given in the examples below.

No Cache

The first instance is when the cache entry does not exist. The cache can be stored in a json file, and read with the require function (but the delete require.cache[path] must be called first), or using fs.readFileSync or any other read method and then parsing the cache.

For example, given the following dir:

example/source/index.js

import { homedir } from 'os'
import dep from './dep'
import staticAnalysis from 'static-analysis'
import myPackage from 'myPackage'

example/source/dep.js

export default () => {
  console.log('dep')
}

The compare method can be called in the following way:

import compare from '@depack/cache'

// returns empty cache
const readCache = () => ({})
// updates cache
const writeCache = (entry) => {
  const current = readCache()
  const updated = { ...current, ...entry }
  // fs.writeFileSync('cache.json', JSON.stringify(updated, null, 2))
}

;(async () => {
  const cache = readCache()
  const modulePath = 'example/source/index.js'
  const res = await compare(modulePath, cache)
  if (res.reason == 'NO_CACHE') {
    console.log(res)
    // do some logic
    const { mtime, hash } = res
    const cacheToWrite = {
      [modulePath]: {
        mtime, hash,
      },
    }
    writeCache(cacheToWrite)
  }
})()

It will return the result that indicates that the cache does not exist, and provide all information that should be written in cache so that it can be retrieved next time:

{
  result: false,
  reason: 'NO_CACHE',
  mtime: 1554399982000,
  hash: [
    'os',
    'example/source/dep.js 1554389422000',
    'static-analysis 2.1.1',
    'myPackage 1.0.0'
  ],
  md5: '980d26e614a016566682df0ddd47bb6f'
}

Mtime Change

If the module's mtime has changed, the result will be false, with the new mtime returned so that it can be updated. The current implementation is coupled to mtime logic, therefore when transferring onto other machines via git for example, the cache will fail. It might be improved in the future.

let cache = {}
const { mtime, hash } = await compare(path, cache)
cache[path] = { mtime, hash }
await update()
const res = await compare(path, cache)
console.log(res)
{
  result: false,
  reason: 'MTIME_CHANGE',
  mtime: 1582738908000,
  hash: [
    'os',
    'example/source/dep.js 1554389422000',
    'static-analysis 2.1.1',
    'myPackage 1.0.0'
  ],
  currentMtime: 1582738907000,
  md5: '980d26e614a016566682df0ddd47bb6f'
}

Hash Update

The hash is an array with strings that show what version of a dependency/file are used by the entry source file. They are saved in cache in the full array form rather than md5 itself so that it is possible to log about when the changes were made and to which files. The changes will be logged using the function provided (console.log by default).

let cache = {}
const { mtime, hash } = await compare(path, cache)
cache[path] = { mtime, hash }
await update()
const res = await compare(path, cache, console.error)
console.log(res)

stderr

+ example/temp/source/dep.js 2/26/2020, 20:41:50
+ myPackage 1.0.1
+ path 
- example/temp/source/dep.js 2/26/2020, 20:41:49
- myPackage 1.0.0
{
  result: false,
  mtime: 1582738909000,
  hash: [
    'os',
    'example/temp/source/dep.js 1582738910000',
    'static-analysis 2.1.1',
    'myPackage 1.0.1',
    'path'
  ],
  reason: 'HASH_CHANGE',
  md5: '8ee4ba1189bd9cae5132e49a0d48856c'
}

Copyright