js-tariffs
v1.0.0
Published
[](#)
Readme
npm-tariff
A “trade-war simulator” for Node.js.
Inject delays, quotas, and comedic messages when importing packages – as if they’re being taxed at customs.
~~> WARNING: This is purely satire and not recommended for production. Unless you enjoy forging comedic chaos in your CI pipeline!~~
Features
- Tariff Delays
Slow down imports of specific packages with an artificial “tariff” percentage. - Import Quotas
Enforce a max number of times you can require a certain package. Exceed it, face an “import privilege revoked” message, and an exit(1). - Single Tweet
At the end of the script, print one random ASCII tweet about tariffs, instead of spamming logs for each import. - Trade‑Deficit Dashboard
On exit, show a summary of:- Which packages you “taxed” or “capped”
- Total base load time vs. total artificially delayed time
- A final comedic remark
Installation
npm install npm-tariffOr if you’d rather link it during local dev:
# In your clone of npm-tariff
npm link
# In your test project
npm link npm-tariffUsage
Opt-In via
--require
Run your code like so, ensuring it’s required before your code:node --require npm-tariff yourScript.jsThis preloads
npm-tariff, letting it monkey‑patch the loader.Programmatic API
In your script, you can do:const tariff = require('npm-tariff'); // Tariffs tariff.setTariffs({ lodash: 100, // 100% => artificially adds 100 ms (by default) axios: 200 // 200% => 200 ms }); // Quotas tariff.setQuota({ lodash: 2 // only allow requiring 'lodash' 2 times }); // If you haven't already preloaded it via --require, // enable the monkey-patch manually: tariff.enable(); console.log('--- Importing lodash first time ---'); require('lodash'); console.log('--- Importing axios ---'); require('axios'); console.log('--- Importing lodash second time ---'); require('lodash'); console.log('--- Importing lodash third time (should fail) ---'); require('lodash');Auto‑enable
By default, ifnpm-tariffis loaded before your main code (via--require), it automatically callsenable()so you don’t have to.
Demo
node --require npm-tariff demo.jsPossible Output:
--- Importing lodash first time ---
🛃 [npm‑tariff] Applying 100% tariff on "lodash" (100ms)
🕒 Base: 12ms → After tariff: 112ms (delay = 100ms)
--- Importing axios ---
🛃 [npm‑tariff] Applying 200% tariff on "axios" (200ms)
🕒 Base: 46ms → After tariff: 246ms (delay = 200ms)
--- Importing lodash second time ---
🛃 [npm‑tariff] Applying 100% tariff on "lodash" (100ms)
🕒 Base: 1ms → After tariff: 101ms (delay = 100ms)
--- Importing lodash third time (should fail) ---
❌ [npm‑tariff] Import quota exceeded for "lodash"
Allowed: 2, Attempted: 3
Your import privileges have been revoked. 🇺🇸💼
================ Trade‑Deficit Dashboard ================
lodash x2 | base: 13ms | delay: 200ms
axios x1 | base: 46ms | delay: 200ms
--------------------------------------------------------
Grand Base Time : 59 ms
Grand Delay : 400 ms
========================================================
🗣️ Final Word On Tariffs: (ง'̀-'́)ง WE'RE GONNA MAKE IMPORTS GREAT AGAIN!API
interface Tariff {
setTariffs(table: Record<string, number>): void;
setQuota(table: Record<string, number>): void;
enable(): void;
disable(): void;
}| Method | Description |
|-------------:|:-----------------------------------------------------------------|
| setTariffs | Provide a mapping: { pkgName: percentage }. 100 means +100ms. |
| setQuota | Provide a mapping: { pkgName: maxImports }. Exceed = error. |
| enable | Monkey‑patch Node’s loader, applying tariffs & quotas. |
| disable | Revert to Node’s standard loader. No more import overhead. |
FAQ
Why block the event loop with synchronous sleeps?
If you actually wanted to do something useful, an async approach might be better.Why do I see random internal Node modules in the dashboard?
We intentionally filter them out in the newest version, but if you do see them, it’s because Node is requesting internal modules. To hide them, only track stats for the modules you explicitly tariff/quota.Safe for production?
Absolutely not recommended. Use at your own risk.What’s with the “import privileges have been revoked” message?
That’s the point its called “quota limit.”
Contributing
Feel free to open issues or PRs to:
- Add more comedic ASCII art
- Adjust the base latency or randomize it
- Add more “trade policy” toggles (like weekend surcharges, random “trade embargo” errors, etc.)
License
MIT – Because everyone wants stuff to be free. Enjoy your tariff wars!
