Coder Social home page Coder Social logo

glob-interceptor's Introduction

glob-interceptor

Bring your own file system for globbing by proxying the cache

Usage

This library uses some implementation detail of the glob package by adding Proxies to specific cache options. Since the cache is always checked before executing a certain file system operation, this package completely bypasses the underlying fs module.

By its very nature a cache is synchronous. Therefore all proxied file system operations need to return synchronously. That's why this package only makes sense for glob.sync. It also works for the async counterpart, but since file system access is sync behind the scenes, this is rather useless.

Creating an interceptor

You can create an interceptor by calling createGlobInterceptor(fs) and providing an object that implements the following interface for file system access:

/** A minimal abstraction of FileSystem operations needed to provide a cache proxy for 'glob'. None of the methods are expected to throw. */
export interface GlobFileSystem {
    /** Returns `true` if the specified `path` is a directory, `undefined` if it doesn't exist and `false` otherwise. */
    isDirectory(path: string): boolean | undefined;
    /** Returns `true` if the specified `path` is a symlink, `false` in all other cases. */
    isSymbolicLink(path: string): boolean;
    /** Get the entries of a directory as string array. Will only be called on paths where `isDirectory` returns `true`*/
    readDirectory(dir: string): string[];
    /** Get the realpath of a given `path` by resolving all symlinks in the path. */
    realpath(path: string): string;
}

For convenience there is a utility function fromNodeLikeFileSystem, that returns an instance of GlobFileSystem for a given file system compatible with Node's fs module.

import {Volume} from "memfs";
import {fromNodeLikeFileSystem, createGlobInterceptor} from "glob-interceptor";

const interceptor = createGlobInterceptor(fromNodeLikeFileSystem(Volume.fromJSON({/* your in-memory files go here*/})));

Using the interceptor

With the previously created interceptor you can now invoke glob.sync to intercept all file system interaction:

let result = glob.sync('**' /* any pattern you want */, {nodir: true /* any options you like */, ...interceptor});
// or if you are targeting a runtime without object spread, you can use `Object.assign` instead
result = glob.sync('**' /* any pattern you want */, Object.assign({nodir: true /* any options you like */}, interceptor));

You can reuse an interceptor as often as you want or need.

Note that interceptor contains the following properties: cache, statCache, realpathCache, symlinks. If you add one of these properties to your options object, they will be overridden by the ones from interceptor. If you explicitly override any of these properties with your own, the interceptor will not work as expected.

Caching

Unless your implementation of GlobFileSystem does some caching, it will always execute the underlying binding. There's a utility function memoizeFileSystem to add caching to your GlobFileSystem:

import {fromNodeLikeFileSystem, createGlobInterceptor, memoizeFileSystem} from "glob-interceptor";
import fs from "fs";

const interceptor = createGlobInterceptor(memoizeFileSystem(fromNodeLikeFileSystem(fs)));

License

MIT © Klaus Meinhardt

glob-interceptor's People

Contributors

ajafff avatar renovate-bot avatar renovate[bot] avatar

Watchers

 avatar

glob-interceptor's Issues

Action Required: Fix Renovate Configuration

There is an error with this repository's Renovate configuration that needs to be fixed. As a precaution, Renovate will stop PRs until it is resolved.

Location: renovate.json
Error type: The renovate configuration file contains some invalid settings
Message: Invalid configuration option: node

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Edited/Blocked

These updates have been manually edited so Renovate will no longer make changes. To discard all commits and start over, click on a checkbox.

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

npm
package.json
  • @fimbul/valtyr ^0.21.0
  • @fimbul/wotan ^0.21.0
  • @types/glob ^7.1.0
  • @types/node ^10.9.4
  • ava ^2.0.0
  • codecov ^3.1.0
  • glob 7.1.6
  • memfs ^2.10.1
  • npm-run-all ^4.1.3
  • nyc ^13.0.1
  • tslint ^5.11.0
  • tslint-consistent-codestyle ^1.13.3
  • typescript ~3.9.0
travis
.travis.yml
  • node 8
  • node 10
  • node 12

  • Check this box to trigger a request for Renovate to run again on this repository

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.