🌲 Timber - Javascript tools
This library provides helper tools used by the Javascript logger.
Tools
Queue<T>
Generic FIFO queue. Used by makeThrottle
to store pipeline functions to be executed as concurrent 'slots' become available. Provides fast retrieval for any primitive or object that needs ordered, first-in, first-out retrieval.
Usage example
import { Queue } from "@timberio/tools";
interface IPerson {
name: string;
age: number;
}
const q = new Queue<IPerson>();
q.push({ name: "Jeff", age: 50 });
q.push({ name: "Sally", age: 39 });
while (q.length) {
console.log(q.shift().name);
}
makeThrottle<T>(max: number)
Returns a throttle
higher-order function, which wraps an async
function, and limits the number of active Promises to max: number
The throttle
function has this signature:
throttle(fn: T): (...args: InferArgs<T>[]) => Promise<InferArgs<T>>
Usage example
import Timber from "@timberio/logger";
import { makeThrottle } from "@timberio/tools";
const timber = new Timber("apiKey");
const throttle = makeThrottle(2);
const pipeline = async log =>
new Promise(resolve => {
setTimeout(() => resolve(log), 2000);
});
timber.addPipeline(throttle(pipeline));
const promises = [];
for (let i = 0; i < 10; i++) {
promises.push(timber.log({ message: `Hello ${i}` }));
}
void (async () => {
void (await promises);
})();
base64Encode(str: string): string
Node.js only
Converts a plain-text string to a Base64 encoded string. Similar to window.btoa() in the browser.
Used by the logger to convert an API key to Timber's user:password
basic auth.
Usage example:
import { atob } from "@timberio/tools";
console.log(atob("hello world"));