Skip to content

Commit

Permalink
enforce package bulk limits
Browse files Browse the repository at this point in the history
  • Loading branch information
tlhunter committed Oct 3, 2023
1 parent 0dcfb40 commit f2ced43
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 4 deletions.
9 changes: 9 additions & 0 deletions .github/workflows/pull-request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,12 @@ jobs:
node-version: ${{ matrix.node-version }}
- run: npm install
- run: npm test

limitations:
name: Package Bulk Limitations
runs-on: ubuntu-latest
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: 20.6.1
- run: npm run limits
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
test/
test.sh
.github/
limitation.js
13 changes: 11 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
# Diagnostics Channel Polyfill

This package provides a polyfill for the `diagnostics_channel` core Node.js module. It aims to be compatible with every single version of Node.js. It also aims to remain simple, ideally one or two files, and with zero dependencies.
This package provides a polyfill for the `diagnostics_channel` core Node.js module for use with older versions of Node.js. It aims to remain simple, with zero dependencies, and only taking up a few kilobytes of space.

The package attempts to backport support for every single feature that is added at later dates in Node.js core, at least assuming these are backwards compatible.
This package attempts to backport every feature and bugfix that is added to Node.js core. If a feature hasn't been backported then please open a Pull Request or create an issue.

Whenever the currently running version of Node.js ships with `diagnostics_channel`, **dc-polyfill** will make sure to use the global registry of channels provided by the core module. However, for older versions of Node.js, **dc-polyfill** will instead use a global symbol to track the channels. This symbol will remain the same for all versions of **dc-polyfill** to avoid the issue where multiple versions of an npm library installed in a dependency hierarchy usually provide different singletons.

Ideally, this package will forever remain backwards compatible, and there will never be a v2.x release.

## Usage

```sh
npm install dc-polyfill
```

```javascript
const diagnostics_channel = require('dc-polyfill');
```

## Contributing

When a Pull Request is created the code runs against many different versions of Node.js. Notably, versions right before a change and versions right after a change, the first version of a release line, and the last version of a release line.

Currently this module tests Node.js >= v12. If you would like to use `dc-polyfill` for versions of Node.js older than this then feel free to submit a Pull Request or open an issue.
57 changes: 57 additions & 0 deletions limitation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#!/usr/bin/env node

const { execSync } = require('child_process');

const pkg = require('./package.json');
pkg.dependencies = pkg.dependencies || {};
pkg.devDependencies = pkg.devDependencies || {};

let violations = 0;

const limits = pkg.limits;

if (!limits) {
process.exit(0);
}

if (('packedSize' in limits) || ('unpackedSize' in limits)) {
const { size, unpackedSize } = JSON.parse(execSync(`npm pack --dry-run --json`).toString())[0];

console.log(` Packed Size: ${size}\n Unpacked Size: ${unpackedSize}`);

if ('packedSize' in limits && size > limits.packedSize) {
violations++;
console.error(`The packed size of ${size} exceeds the limit of ${limits.packedSize}!`);
}

if ('unpackedSize' in limits && unpackedSize > limits.unpackedSize) {
violations++;
console.error(`The unpacked size of ${unpackedSize} exceeds the limit of ${limits.unpackedSize}!`);
}
}

const depCount = Object.keys(pkg.dependencies).length;
console.log(` Dependencies: ${depCount}`);

if ('maxDependencies' in limits) {
if (depCount > limits.maxDependencies) {
violations++;
console.error(`The dependency count of ${depCount} exceeds the limit of ${limits.maxDependencies}!`);
}
}

const devDepCount = Object.keys(pkg.devDependencies).length;
console.log(`Dev Dependencies: ${devDepCount}`);

if ('maxDevDependencies' in limits) {
if (devDepCount > limits.maxDevDependencies) {
violations++;
console.error(`The dev dependency count of ${devDepCount} exceeds the limit of ${limits.maxDevDependencies}!`);
}
}

if (violations > 0) {
process.exit(1);
}

console.log('OK');
11 changes: 10 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"description": "A polyfill for the internal diagnostics_channel module",
"main": "dc-polyfill.js",
"scripts": {
"test": "./test.sh"
"test": "./test.sh",
"limits": "./limitation.js"
},
"keywords": [
"diagnostics",
Expand All @@ -17,5 +18,13 @@
"license": "MIT",
"devDependencies": {
"tape": "^5.6.6"
},
"limits": {
"packedSize": 10000,
"unpackedSize": 35000,
"maxDependencies": 0
},
"engines": {
"node": ">=12"
}
}
1 change: 1 addition & 0 deletions primordials.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const makeCall = (fn) => (...args) => fn.call(...args);

// https://github.com/tc39/proposal-relative-indexing-method#polyfill
// v16.6.0+, v17+
function arrayAtPolyfill(n) {
// ToInteger() abstract op
n = Math.trunc(n) || 0;
Expand Down
2 changes: 1 addition & 1 deletion test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ echo "Node.js version: $(node --version)"

errors=0

for i in test/*.js; do
for i in test/*.spec.js; do
node "$i" || ((errors++))
done

Expand Down

0 comments on commit f2ced43

Please sign in to comment.