pixelpinch-proxy / src /utils /bun-utils.js
3v324v23's picture
refactor logging levels to debug for improved verbosity and add fast hash function for cache key generation
4e57a73
Raw History Blame Contribute Delete
1.44 kB
// Bun utility functions
export function isBunRuntime() {
return typeof global.Bun !== 'undefined';
}
export function getOptimizedHeaders() {
if (isBunRuntime()) {
return {
'X-Powered-By': 'PixelPinch/Bun',
'X-Runtime': 'Bun',
'X-Bun-Version': Bun.version
};
}
return {
'X-Powered-By': 'PixelPinch/Node'
};
}
export function optimizeImageBuffer(buffer) {
if (isBunRuntime()) {
// Use Bun's native buffer operations
return Bun.gzip(buffer);
}
return buffer;
}
/**
* Fast hash function for cache keys
* Uses Bun's built-in hash when available, otherwise uses a simple fast hash
* @param {string} input - String to hash
* @returns {string} Hexadecimal hash string
*/
export function fastHash(input) {
if (!input || typeof input !== 'string') {
input = String(input || '');
}
if (isBunRuntime() && typeof Bun.hash === 'function') {
// Use Bun's native hash function (very fast)
// Bun.hash returns a number, convert to hex string
const hash = Bun.hash(input);
return hash.toString(16);
}
// Fast hash function for Node.js (djb2 variant)
// This is a simple, fast hash that works well for cache keys
let hash = 5381;
for (let i = 0; i < input.length; i++) {
hash = ((hash << 5) + hash) + input.charCodeAt(i);
hash = hash | 0; // Convert to 32-bit integer
}
// Convert to positive hex string
return (hash >>> 0).toString(16);
}