Add getMilliseconds util for safer value interpretation (#17498)

* Add `getMilliseconds` util for safer value interpretation

* Test more data types

* Remove remnant

* Customizable fallback with default of undefined

* Clean-up

* Transform getMilliseconds to named export

---------

Co-authored-by: ian <licitdev@gmail.com>
This commit is contained in:
Pascal Jufer
2023-02-17 17:06:26 +01:00
committed by GitHub
parent 2ff9a64c87
commit e3c755dcf0
17 changed files with 133 additions and 87 deletions

View File

@@ -56,12 +56,11 @@ const scenarios = [
// Test the ttl value
{
name: 'when ttl is null',
name: 'when ttl is undefined',
input: {
env: {},
headers: {},
accountability: null,
ttl: null,
globalCacheSettings: false,
personalized: false,
},

View File

@@ -11,7 +11,7 @@ import { Request } from 'express';
*/
export function getCacheControlHeader(
req: Request,
ttl: number | null,
ttl: number | undefined,
globalCacheSettings: boolean,
personalized: boolean
): string {
@@ -22,7 +22,7 @@ export function getCacheControlHeader(
if (noCacheRequested) return 'no-store';
// When the resource / current request shouldn't be cached
if (ttl === null || ttl < 0) return 'no-cache';
if (ttl === undefined || ttl < 0) return 'no-cache';
// When the API cache can invalidate at any moment
if (globalCacheSettings && env.CACHE_AUTO_PURGE === true) return 'no-cache';

View File

@@ -0,0 +1,34 @@
import { expect, test } from 'vitest';
import { getMilliseconds } from './get-milliseconds';
test.each([
// accept human readable time format and plain number
['1d', 86400000],
['1000', 1000],
[1000, 1000],
// accept negative values
['-1 minutes', -60000],
[-1, -1],
[0, 0],
// fallback to undefined
[null, undefined],
[undefined, undefined],
['', undefined],
['invalid string', undefined],
[false, undefined],
[[], undefined],
[{}, undefined],
[Symbol(123), undefined],
[
() => {
return 456;
},
undefined,
],
])('should result into %s for input "%s"', (input, expected) => {
expect(getMilliseconds(input)).toBe(expected);
});
test('should return custom fallback on invalid value', () => {
expect(getMilliseconds(undefined, 0)).toBe(0);
});

View File

@@ -0,0 +1,12 @@
import ms from 'ms';
/**
* Safely parse human readable time format into milliseconds
*/
export function getMilliseconds<T>(value: unknown, fallback?: T): number | T;
export function getMilliseconds(value: unknown, fallback = undefined): number | undefined {
if ((typeof value !== 'string' && typeof value !== 'number') || value === '') {
return fallback;
}
return ms(String(value)) ?? fallback;
}

View File

@@ -1,11 +1,11 @@
import ms from 'ms';
import { machineId } from 'node-machine-id';
import os from 'os';
// @ts-ignore
import { toArray } from '@directus/shared/utils';
import { version } from '../../package.json';
import env from '../env';
import logger from '../logger';
import { toArray } from '@directus/shared/utils';
import { getMilliseconds } from './get-milliseconds';
export async function track(event: string): Promise<void> {
const axios = (await import('axios')).default;
@@ -44,7 +44,7 @@ async function getEnvInfo(event: string) {
},
cache: {
enabled: env.CACHE_ENABLED,
ttl: ms(env.CACHE_TTL),
ttl: getMilliseconds(env.CACHE_TTL),
store: env.CACHE_STORE,
},
storage: {