
Product
Socket for Jira Is Now Available
Socket for Jira lets teams turn alerts into Jira tickets with manual creation, automated ticketing rules, and two-way sync.
Modern, lightweight library to convert local/network image files to data URI scheme
Convert local and remote images to data URI scheme for inline embedding.
Requirements: Node.js >= 18.0.0
npm install imguri
import { encodeSingle } from 'imguri';
// Local file
const uri = await encodeSingle('logo.png');
console.log(uri); // data:image/png;base64,...
// Remote URL
const uri = await encodeSingle('https://example.com/image.jpg');
// With options
const uri = await encodeSingle('large.png', {
sizeLimit: 256000, // 250KB
force: true, // override limit
});
import { encode } from 'imguri';
const results = await encode(['icon1.png', 'icon2.png', 'https://example.com/logo.jpg'], {
concurrency: 5,
});
for (const [path, result] of results) {
if (result.error) {
console.error(path, result.error.message);
} else {
console.log(path, result.data.substring(0, 50) + '...');
}
}
const { encodeSingle } = require('imguri');
(async () => {
const uri = await encodeSingle('image.png');
console.log(uri);
})();
Encodes a single file or URL to data URI.
string - File path or HTTP/HTTPS URLobject
sizeLimit number - Max bytes (default: 131072 / 128KB)force boolean - Override size limit (default: false)timeout number - HTTP timeout ms (default: 20000)Returns Promise<string> - Data URI string
Throws if file not found, size exceeded, or network error.
Encodes multiple files/URLs with concurrent processing.
string | string[] - Path(s) to encodeobject - Same as encodeSingle, plus:
concurrency number - Max parallel ops (default: 10)Returns Promise<Map<string, EncodeResult>> where EncodeResult is:
{
data: string | null, // Data URI or null if error
error: Error | null // Error or null if success
}
Callback-based API for v0.x compatibility. Deprecated, will be removed in v2.0.
| Option | Type | Default | Description |
|---|---|---|---|
| sizeLimit | number | 131072 | Max file size in bytes (128KB) |
| timeout | number | 20000 | HTTP timeout in milliseconds |
| concurrency | number | 10 | Max concurrent operations |
| force | boolean | false | Override size limit |
Path validation prevents directory traversal attacks (../ is blocked). Absolute paths are allowed but can access any readable file - validate user input in production.
For HTTP URLs, content-type validation ensures only images are processed. Size limits prevent memory exhaustion.
v1.0 replaces callbacks with promises and changes result format from object to Map. Use encodeLegacy() for backward compatibility or update to promise-based API:
// v0.x
imguri.encode(['file.png'], opts, (err, results) => { ... });
// v1.0
const results = await encode(['file.png'], opts);
for (const [path, result] of results) { ... }
See CHANGELOG.md for complete migration guide.
Two-layer architecture separates business logic from I/O:
Data flows: Adapter → Validation → Encoder → Data URI string
npm install # Install dependencies
npm test # Run tests (54 tests, 89% coverage)
npm run lint # Check code style
npm run build # Build CJS/ESM bundles
MIT License
FAQs
Modern, lightweight library to convert local/network image files to data URI scheme
We found that imguri demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Product
Socket for Jira lets teams turn alerts into Jira tickets with manual creation, automated ticketing rules, and two-way sync.

Company News
Socket won two 2026 Reppy Awards from RepVue, ranking in the top 5% of all sales orgs. AE Alexandra Lister shares what it's like to grow a sales career here.

Security News
NIST will stop enriching most CVEs under a new risk-based model, narrowing the NVD's scope as vulnerability submissions continue to surge.