@gera2ld/common 0.1.1 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,6 @@
1
+ export declare function b64encode(bytes: Uint8Array): string;
2
+ export declare function b64decode(base64: string): Uint8Array<ArrayBuffer>;
3
+ export declare function b64urlEncode(bytes: Uint8Array): string;
4
+ export declare function b64urlDecode(base64url: string): Uint8Array<ArrayBuffer>;
5
+ export declare function encodeText(str: string): Uint8Array<ArrayBuffer>;
6
+ export declare function decodeText(bytes: Uint8Array): string;
@@ -0,0 +1,14 @@
1
+ export declare const CrLf: Uint8Array<ArrayBuffer>;
2
+ export declare class BufferReader {
3
+ private buffer;
4
+ private requests;
5
+ isClosed: boolean;
6
+ read(size?: number): Promise<Uint8Array>;
7
+ readExact(size: number): Promise<Uint8Array>;
8
+ readUntil(delimiter?: string | Uint8Array): Promise<Uint8Array>;
9
+ feed(chunk: Uint8Array): void;
10
+ close(): void;
11
+ private processBuffer;
12
+ }
13
+ export declare function concatBuffer(a: Uint8Array, b: Uint8Array): Uint8Array;
14
+ export declare function searchBuffer(buffer: Uint8Array, slice: Uint8Array): number;
@@ -0,0 +1,13 @@
1
+ export declare function limitConcurrency<T extends unknown[], U>(fn: (...args: T) => Promise<U>, concurrency: number): (...args: T) => Promise<U>;
2
+ export declare class Queue<T> {
3
+ maxSize: number;
4
+ private data;
5
+ private getQueue;
6
+ private putQueue;
7
+ constructor(maxSize?: number);
8
+ get size(): number;
9
+ private defer;
10
+ private resolve;
11
+ get(maxWait?: number): Promise<T | undefined>;
12
+ put(item: T, maxWait?: number): Promise<void>;
13
+ }
@@ -0,0 +1,9 @@
1
+ export * from './base64';
2
+ export * from './buffer';
3
+ export * from './concurrency';
4
+ export * from './logger';
5
+ export * from './number';
6
+ export * from './rate-limiter';
7
+ export * from './request';
8
+ export * from './time';
9
+ export * from './util';
@@ -0,0 +1,16 @@
1
+ interface ILoggerOptions {
2
+ prefix?: string | (() => string);
3
+ }
4
+ export declare class Logger {
5
+ static defaultOptions: ILoggerOptions;
6
+ private output?;
7
+ options: ILoggerOptions;
8
+ constructor(opts?: string | Partial<ILoggerOptions>);
9
+ getPrefix(): string;
10
+ collect(): () => string[];
11
+ log(type: 'info' | 'error', pattern: string, ...args: unknown[]): void;
12
+ info(pattern: string, ...args: unknown[]): void;
13
+ error(pattern: string, ...args: unknown[]): void;
14
+ wrap<U extends unknown[], V, T>(fn: (this: T, ...args: U) => V, wrapper: (logger: Logger, fn: (this: T, ...args: U) => V, ...args: U) => V): (this: T, ...args: U) => V;
15
+ }
16
+ export {};
@@ -0,0 +1,41 @@
1
+ export declare function inflateNumber(input: number | string, decimals: number): bigint;
2
+ export declare function deflateNumber(input: number | string | bigint, decimals: number): number;
3
+ export interface INumberOptions {
4
+ /**
5
+ * Maximum digits after the decimal point in fixed-point notation.
6
+ * `1.2345` with `2` becomes `1.23`.
7
+ */
8
+ maximumFractionDigits: number;
9
+ /**
10
+ * Absolute value threshold for switching to exponential notation for large numbers.
11
+ * `12345678`>\`1e6` becomes `1.23456e+7`.
12
+ */
13
+ exponentialThresholdLarge: number;
14
+ /**
15
+ * Absolute value threshold for switching to exponential notation for small non-zero numbers.
16
+ * `0.000000123`<\`1e-6` becomes `1.23e-7`.
17
+ */
18
+ exponentialThresholdSmall: number;
19
+ /**
20
+ * Minimum number of significant digits to display.
21
+ * `12` with `3` becomes `12.00`.
22
+ */
23
+ minimumSignificantDigits: number;
24
+ /**
25
+ * Maximum number of significant digits to display.
26
+ * `123456789` with `5` might become `1.2346e+8`.
27
+ */
28
+ maximumSignificantDigits: number;
29
+ /**
30
+ * Rounds the numerical value to the nearest multiple of this step *before* formatting.
31
+ * `123456` with `100` becomes `123400` then formatted.
32
+ */
33
+ quantizationStep: number;
34
+ /**
35
+ * Whether to keep trailing zeros in the fractional part of a number.
36
+ * e.g. `1.200` becomes `1.2` if false.
37
+ */
38
+ keepTrailingZeros?: boolean;
39
+ }
40
+ export declare function formatNumber(num?: number | string, options?: Partial<INumberOptions>): string;
41
+ export declare function formatPercentage(value: number, options?: Partial<INumberOptions>): string;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,17 @@
1
+ export interface IRetryOptions {
2
+ interval: number;
3
+ maxRetry: number;
4
+ maxInterval: number;
5
+ }
6
+ export interface IRateLimitOptions extends IRetryOptions {
7
+ isTooFrequent?: (error: any) => boolean;
8
+ onScheduleRetry?: (interval: number) => void;
9
+ }
10
+ export declare function rateLimiter(options?: Partial<IRateLimitOptions>): <U>(fn: () => Promise<U>) => Promise<U>;
11
+ export interface IWaitResult<T = unknown> {
12
+ interval?: number;
13
+ error?: unknown;
14
+ ok: boolean;
15
+ data: T;
16
+ }
17
+ export declare function waitUntil<T = unknown>(fn: () => Promise<IWaitResult<T>>, options?: IRetryOptions): Promise<IWaitResult<T>>;
@@ -0,0 +1,25 @@
1
+ export type IRequestOptions = RequestInit & {
2
+ json?: unknown;
3
+ timeout?: number;
4
+ searchParams?: string[][] | Record<string, any> | URLSearchParams;
5
+ };
6
+ export declare class SimpleRequestError extends Error {
7
+ request: {
8
+ url: string;
9
+ method: string;
10
+ options?: IRequestOptions;
11
+ };
12
+ response?: Response | undefined;
13
+ constructor(message: string, request: {
14
+ url: string;
15
+ method: string;
16
+ options?: IRequestOptions;
17
+ }, response?: Response | undefined, cause?: unknown);
18
+ }
19
+ export declare function buildSearchParams(init?: IRequestOptions["searchParams"]): URLSearchParams;
20
+ export declare function simpleRequest<T = unknown>(url: string | URL, options?: IRequestOptions): {
21
+ arrayBuffer(): Promise<ArrayBuffer>;
22
+ blob(): Promise<Blob>;
23
+ json<U = T>(): Promise<U>;
24
+ text(): Promise<string>;
25
+ };
@@ -0,0 +1 @@
1
+ export {};
package/dist/time.d.ts ADDED
@@ -0,0 +1,17 @@
1
+ /** Converts a string like "1h" or a number to milliseconds. */
2
+ export declare function normalizeInterval(interval: string | number): number;
3
+ /** Formats a signed ms duration as a locale-aware string. */
4
+ export declare function formatDuration(ms: number, options?: {
5
+ locale?: string;
6
+ style?: "long" | "short" | "narrow";
7
+ }): string;
8
+ /** Formats a Date as "YYYY-MM-DD". */
9
+ export declare function formatISODate(date: Date): string;
10
+ /** Formats a date using Intl.DateTimeFormat with en-GB short date and medium time. */
11
+ export declare function reprDate(date: number | string | Date): string;
12
+ /** Formats a date as a relative time string like "2 hours ago". */
13
+ export declare function timeAgo(date: number | string | Date, options?: {
14
+ locale?: string;
15
+ numeric?: "auto" | "always";
16
+ style?: "long" | "short" | "narrow";
17
+ }): string;
@@ -0,0 +1 @@
1
+ export {};
package/dist/util.d.ts ADDED
@@ -0,0 +1,21 @@
1
+ export declare function replacerBigInt(_key: string, value: any): any;
2
+ interface ReprJsonOptions {
3
+ truncateThreshold: number;
4
+ headLength: number;
5
+ tailLength: number;
6
+ }
7
+ export declare function truncateString(input: string, options?: Partial<ReprJsonOptions>): string;
8
+ export declare function reprJson(obj: unknown, options?: Partial<ReprJsonOptions>): string;
9
+ export declare function formatString(fmt: string, ...args: unknown[]): string;
10
+ export interface IDeferred<T> {
11
+ promise: Promise<T>;
12
+ resolve: (result: T | Promise<T>) => void;
13
+ reject: (reason?: any) => void;
14
+ status: 'pending' | 'resolved' | 'rejected';
15
+ }
16
+ export declare function defer<T = void>(): IDeferred<T>;
17
+ type IFunction<U extends any[], V, T> = (this: T, ...args: U) => V;
18
+ export declare function wrapFunction<U extends any[], V, T>(fn: IFunction<U, V, T>, wrapper: (this: T, originalFn: IFunction<U, V, T>, ...args: U) => V): (this: T, ...args: U) => V;
19
+ export declare function bytesToHex(bytes: Uint8Array): string;
20
+ export declare function loadJS(url: string): Promise<unknown>;
21
+ export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gera2ld/common",
3
- "version": "0.1.1",
3
+ "version": "0.1.2",
4
4
  "type": "module",
5
5
  "sideEffects": false,
6
6
  "exports": {
@@ -24,6 +24,6 @@
24
24
  "clean": "del-cli dist tsconfig.tsbuildinfo",
25
25
  "build:types": "tsc",
26
26
  "build:js": "vite build",
27
- "build": "pnpm run clean && pnpm run /^build:/"
27
+ "build": "pnpm clean && pnpm build:js && pnpm build:types"
28
28
  }
29
29
  }