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

esmac

v3.0.0

Published

Control access to ES modules

Downloads

359

Readme

es-module access control

Control access to modules and enforce the style of specifiers that they may use to import each other based on their location.

esmac is a Node.js library that helps keep things under control in large trees by restricting access where needed.

Motivation

Working in a large codebase with thousands of modules, it helps to group them into layers where access between them can be made predictable. Gating access helps you assess the impact of a change you're making; you can, for example, tell who exactly will be affected by a change, which is very desirable.

It is not unlike access control in programming languages. Consider the number of times you wished for private variable visibility in JavaScript and instead had to resort to factory patterns merely for the benefit of access protection?

This is access protection but for files.

Programmers also habitually violate access patterns when they can, because they're practical beings and I find it hard to blame them, especially when the alternative usually requires one to think. /s This is especially true in older systems where nothing stops you from importing any specific module regardless of where it is. This tool helps break that habit.

Authoritarianism is not the goal. The goal is a more pleasant system where one can make deliberate, well-informed decisions on where code should go and have the peace of mind that a change cannot leak.

Example

esmac lets us control exactly who may access modules and how. Consider the following file tree:

.
├── lib
│   ├── bar
│   └── foo
└── packages
    ├── a
    ├── b
    └── c

We will be making lib/ private where package modules aren't able to access it. Files under lib/ import each other using relative specifiers. For packages/, files of a single package also import each other using relative specifiers, while inter-package imports (and lib -> packages) are done using bare specifiers.

Permit files under lib/ to access other files under lib/ and no one else:

[
  {
    source: "lib/**",
    target: "lib/**",
    specifier: require("esmac/specifiers/relative")
  }
]

This also means that lib/ cannot access packages/ anymore. To allow that, we can add another edge, going from lib/ to packages/ this time:

(2)
[
  ...,
  {
    source: "lib/**",
    target: "packages/**",
    specifier: require("esmac/specifiers/package")
  }
]

Third, ensure that modules of a single package import other modules of that same package using only relative specifiers:

(3)
[
  ...,
  {
    source: "packages/*/**",
    target: "packages/*/**",
    boundary: 0,
    specifier: require("esmac/specifiers/relative")
  }
]

The boundary property for the rule qualifies it to apply only when the captures of both the source and the target at the specified index are equal. Effectively, this tells esmac to apply the rule only when both source and target share the same folder directly under packages/:

{ source: 'packages/a/lib/a.js', target: 'packages/a/lib/b.js' } // OK
                    ^                              ^
{ source: 'packages/a/lib/a.js', target: 'packages/b/lib/a.js' } // NOT ok
                    ^                              ^

Finally, to allow access between modules of different packages, we can adopt a rule similar to what we did for lib/:

(4)
[
  ...,
  {
    source: "**",
    target: "packages/**",
    specifier: require("esmac/specifiers/package")
  }
]

This does make rule (2) superfluous and can be dropped. With this, the full list of rules becomes:

[
  {
    source: "lib/**",
    target: "lib/**",
    specifier: require("esmac/specifiers/relative")
  },
  {
    source: "packages/*/**",
    target: "packages/*/**",
    boundary: 0,
    specifier: require("esmac/specifiers/relative")
  },
  {
    source: "**",
    target: "packages/**",
    specifier: require("esmac/specifiers/package")
  }
]

Note that order matters. Rules are considered in the order they are given, so you should order them by specificity.

API

Signature:

(Array.<Rule>): (Dependency): CheckResult?

Creates an instance of the checker which gives you a function to validate a dependency. The function finds the first applicable rule and checks whether the input's specifier matches the one described by the rule.

See ./types.d.ts for an explanation of the relevant types.

API example

const esmac = require('esmac')
const relative = require('esmac/specifiers/relative')

const check = esmac([
  {
    source: 'lib/**',
    target: 'lib/**',
    specifier: relative
  }
])

// rule matches and specifier check passes:
check({
  source: 'lib/a.js',
  target: 'lib/b.js',
  request: './b'
}) // => [true, 0, {}]

// rule matches but specifier check does not pass:
check({
  source: 'lib/a.js',
  target: 'lib/b.js',
  request: 'b'
}) // => [false, 0, {}]

// no applicable rule found:
check({
  source: 'lib/a.js',
  target: 'packages/foo/lib/index.js',
  request: 'foo'
}) // => null

License

MIT