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

glob-github

v1.2.0

Published

Run glob expressions against the Github Repo Contents API and return the matching files and metadata; with caching.

Downloads

8

Readme

glob-github

Run glob expressions against the Github Repo Contents API and return the matching files and metadata; with caching.

Features

  • you can avoid performing a full git clone if you use the Github Contents API to fetch files, but using it to traverse individual file paths is tedious; why not just run a glob expression against a specific Github repository and get the resulting API results?
  • you can use any glob expression like **/*.md to match files
  • supports in-memory caching so that the same file paths are never requested multiple times; you can easily write the cache to disk as a JSON file if you want long term caching as it is a simple hash
  • (since v1.1.0) deduplicates ongoing requests, so that even when executing multiple globs at the same time only one HTTP request is made for each resource

Installation

npm install --save glob-github

Usage

Here's a quick example:

var glob = require('glob-github');

glob({
  user: 'mixu',
  repo: 'singlepageappbook',
  glob: '**/*.md',
  authenticate: {
    type: 'oauth',
    token: '<AUTH TOKEN HERE>'
  }
}, function(err, results, meta) {
  console.log(err, results, meta);
  // results is an array of Github Get Contents API call results:
  // [ { type: 'file', name: 'index.md', path: 'input/index.md', ... }]
  // meta:
  // { limit: 4774, cacheHits: 0, apiCalls: 16 }
});

glob(opts, onDone), where opts is an options hash containing:

  • opts.user: Github username
  • opts.repo: Github repo
  • opts.branch: Github branch (since v1.2.0); defaults to master
  • opts.glob: glob expression to match against
  • opts.authenticate: a hash passed to node-github
  • opts.cache: (Optional). A hash that can be reused across calls, pass in a {} on the first usage and keep passing the same hash in every time to keep using the cache. Note that the results are automatically cached so you only need to pass this if you want to, say, write the cache to disk or something.
  • opts.github: (Optional). An instance of node-github that you have configured to your liking.

The onDone(err, results, meta) callback receives three arguments:

  • err: an error, if any
  • results: an array of results which match the given glob expression as returned from the Github Get Contents API; see the link for examples.
  • meta: an object containing metadata:
    • meta.limit: the smallest Github API x-rate-limit header value that has been received
    • meta.cacheHits: the number of Github API calls that were fulfilled from the cache to resolve the glob expression
    • meta.githubAPI: the number of Github API calls that were made to resolve the glob expression

Caching

If you make the same call (or another glob expression resolution) against the same repo, glob-github will attempt to fulfill matches from the cache:

var glob = require('glob-github');
var config = {
  user: 'mixu',
  repo: 'singlepageappbook',
  glob: '**/*.md',
  authenticate: {
    type: 'oauth',
    token: '<AUTH TOKEN HERE>'
  }
};

glob(config, function(err, results, meta) {
  // should show some number of meta.githubAPI
  console.log(meta);
  glob(config, function(err, results, meta) {
    // should be fulfilled completely from the cache!
    console.log(meta);
  });
});

Deduplication

Concurrent calls are deduplicated: if a request for a specific Github endpoint (e.g. username + repo + path combination) is already pending, then any concurrent requests simply wait for that request to complete rather than making a new request.

var glob = require('glob-github');
var config = { ... };

glob(config, function(err, results, meta) {
  console.log(err, results.length, meta);
});

glob(config, function(err, results, meta) {
  console.log(err, results.length, meta);
});

You should see something like:

null 16 { limit: 10, cacheHits: 10, apiCalls: 6 }
null 16 { limit: 10, cacheHits: 6, apiCalls: 10 }

Note how the 16 API calls needed to resolve this **/*.md glob are divided between the two concurrent glob() calls so that only 16 requests are made and the duplicate requests are served from the cache.