Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -384,4 +384,29 @@ describe('ms(invalid inputs)', () => {
ms(-Infinity);
}).toThrow();
});

it('should throw quickly with a bounded message for large sparse arrays and bigints', () => {
const sparse: unknown[] = [];
sparse[20_000_000] = 0;

const start = performance.now();
let sparseError: Error | undefined;
try {
// @ts-expect-error - We expect this to throw.
ms(sparse);
} catch (error) {
sparseError = error as Error;
}
const elapsed = performance.now() - start;

expect(sparseError).toBeInstanceOf(Error);
expect(sparseError?.message).toContain('[Array(20000001)]');
expect(sparseError?.message.length).toBeLessThan(200);
expect(elapsed).toBeLessThan(50);

expect(() => {
// @ts-expect-error - We expect this to throw.
ms(10n);
}).toThrow('Value provided to ms() must be a string or number. value=10n');
});
});
31 changes: 28 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,31 @@ interface Options {
* @param options - Options for the conversion
* @throws Error if `value` is not a non-empty string or a number
*/
const MAX_ERROR_VALUE_LENGTH = 100;

function formatErrorValue(value: unknown): string {
if (typeof value === 'string') {
const truncated =
value.length > MAX_ERROR_VALUE_LENGTH
? `${value.slice(0, MAX_ERROR_VALUE_LENGTH)}...`
: value;
return JSON.stringify(truncated);
}
if (Array.isArray(value)) {
return `[Array(${value.length})]`;
}
if (typeof value === 'bigint') {
return `${value}n`;
}
if (typeof value === 'object' && value !== null) {
return `[object ${Object.prototype.toString.call(value).slice(8, -1)}]`;
}
if (typeof value === 'number' || typeof value === 'boolean') {
return JSON.stringify(value);
}
return String(value);
}

export function ms(value: StringValue, options?: Options): number;
export function ms(value: number, options?: Options): string;
export function ms(
Expand All @@ -57,7 +82,7 @@ export function ms(
return format(value, options);
}
throw new Error(
`Value provided to ms() must be a string or number. value=${JSON.stringify(value)}`,
`Value provided to ms() must be a string or number. value=${formatErrorValue(value)}`,
);
}

Expand All @@ -71,7 +96,7 @@ export function ms(
export function parse(str: string): number {
if (typeof str !== 'string' || str.length === 0 || str.length > 100) {
throw new Error(
`Value provided to ms.parse() must be a string with length between 1 and 99. value=${JSON.stringify(str)}`,
`Value provided to ms.parse() must be a string with length between 1 and 99. value=${formatErrorValue(str)}`,
);
}
const match =
Expand Down Expand Up @@ -141,7 +166,7 @@ export function parse(str: string): number {
default:
matchUnit satisfies never;
throw new Error(
`Unknown unit "${matchUnit}" provided to ms.parse(). value=${JSON.stringify(str)}`,
`Unknown unit "${matchUnit}" provided to ms.parse(). value=${formatErrorValue(str)}`,
);
}
}
Expand Down