From 73f423db68ab8a9b9b319b43cabdcf33d6de94b8 Mon Sep 17 00:00:00 2001 From: Sam Becker Date: Thu, 29 Feb 2024 11:51:48 -0600 Subject: [PATCH 1/4] Make blog listing more resilient --- src/services/storage/index.ts | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/services/storage/index.ts b/src/services/storage/index.ts index b0428d69..0f3e008e 100644 --- a/src/services/storage/index.ts +++ b/src/services/storage/index.ts @@ -191,13 +191,16 @@ const getStorageUrlsForPrefix = async (prefix = '') => { const urls: StorageListResponse = []; if (HAS_VERCEL_BLOB_STORAGE) { - urls.push(...await vercelBlobList(prefix)); + urls.push(...await vercelBlobList(prefix) + .catch(() => [])); } if (HAS_AWS_S3_STORAGE) { - urls.push(...await awsS3List(prefix)); + urls.push(...await awsS3List(prefix) + .catch(() => [])); } if (HAS_CLOUDFLARE_R2_STORAGE) { - urls.push(...await cloudflareR2List(prefix)); + urls.push(...await cloudflareR2List(prefix) + .catch(() => [])); } return urls From 20fbcca862c4ead0ee1ed778bc4f1de873298712 Mon Sep 17 00:00:00 2001 From: Sam Becker Date: Thu, 29 Feb 2024 11:57:04 -0600 Subject: [PATCH 2/4] Make datetime parsing resilient to empty strings --- .vscode/settings.json | 3 ++- src/utility/date.ts | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 637cea4a..03182fcf 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -41,7 +41,8 @@ "WRHGZC", "wxyz", "zadd", - "zrange" + "zrange", + "datetime" ], "files.associations": { "*.css": "tailwindcss" diff --git a/src/utility/date.ts b/src/utility/date.ts index 04124327..9029c7b3 100644 --- a/src/utility/date.ts +++ b/src/utility/date.ts @@ -21,7 +21,7 @@ export const formatDateForPostgres = (date: Date) => const dateFromTimestamp = (timestamp?: AmbiguousTimestamp): Date => typeof timestamp === 'number' ? new Date(timestamp * 1000) - : typeof timestamp === 'string' + : typeof timestamp === 'string' && timestamp.trim().length > 0 ? /.+Z/i.test(timestamp) ? new Date(timestamp) : new Date(`${timestamp}Z`) From fef580e8fa0cf080bddc9097e70aedd449cb6ffe Mon Sep 17 00:00:00 2001 From: Sam Becker Date: Thu, 29 Feb 2024 14:58:10 -0600 Subject: [PATCH 3/4] Refine date parsing, update tests --- __tests__/date.test.ts | 12 ++++++++++++ __tests__/string.test.ts | 2 ++ src/utility/date.ts | 10 ++++++---- src/utility/string.ts | 2 ++ 4 files changed, 22 insertions(+), 4 deletions(-) diff --git a/__tests__/date.test.ts b/__tests__/date.test.ts index 4e29b7da..f113c9b9 100644 --- a/__tests__/date.test.ts +++ b/__tests__/date.test.ts @@ -30,4 +30,16 @@ describe('Date utility', () => { .toBe('2023-12-02 16:38:36'); }); }); + it('Empty string', () => { + const timestamp = ' '; + const offset = undefined; + expect(convertTimestampWithOffsetToPostgresString( + timestamp, + offset + )) + .toBe(convertTimestampWithOffsetToPostgresString( + new Date().toISOString(), + offset + )); + }); }); diff --git a/__tests__/string.test.ts b/__tests__/string.test.ts index e56dfc4e..bbd33603 100644 --- a/__tests__/string.test.ts +++ b/__tests__/string.test.ts @@ -2,10 +2,12 @@ import { parameterize } from '@/utility/string'; describe('String', () => { it('parameterizes', () => { + expect(parameterize('my-tag')).toBe('my-tag'); expect(parameterize('my tag')).toBe('my-tag'); expect(parameterize('My Tag')).toBe('my-tag'); expect(parameterize('my_tag')).toBe('my-tag'); expect(parameterize('person\'s tag')).toBe('persons-tag'); expect(parameterize('"person\'s tag"')).toBe('persons-tag'); + expect(parameterize('宿宿宿宿')).toBe('宿宿宿宿'); }); }); diff --git a/src/utility/date.ts b/src/utility/date.ts index 9029c7b3..7e6159be 100644 --- a/src/utility/date.ts +++ b/src/utility/date.ts @@ -18,14 +18,16 @@ export const formatDateForPostgres = (date: Date) => '$1-$2-$3 $4', ); -const dateFromTimestamp = (timestamp?: AmbiguousTimestamp): Date => - typeof timestamp === 'number' +const dateFromTimestamp = (timestamp?: AmbiguousTimestamp): Date => { + const date = typeof timestamp === 'number' ? new Date(timestamp * 1000) - : typeof timestamp === 'string' && timestamp.trim().length > 0 + : typeof timestamp === 'string' ? /.+Z/i.test(timestamp) ? new Date(timestamp) : new Date(`${timestamp}Z`) - : new Date(); + : undefined; + return date && !isNaN(date.getTime()) ? date : new Date(); +}; const createNaiveDateWithOffset = ( timestamp?: AmbiguousTimestamp, diff --git a/src/utility/string.ts b/src/utility/string.ts index 5c597cde..8bad0cd2 100644 --- a/src/utility/string.ts +++ b/src/utility/string.ts @@ -24,6 +24,8 @@ export const parameterize = ( .trim() // Replaces spaces, underscores, and dashes with dashes .replaceAll(/[\s_–—]/gi, '-') + // Removes punctuation + .replaceAll(/['"!@#$%^&*()_+=[\]{};:/?,.<>\\|`~]/gi, '') // Removes all non-alphanumeric characters .replaceAll( shouldRemoveNonAlphanumeric From 5bd160249848903e5c75435240d16e8d5d8856ad Mon Sep 17 00:00:00 2001 From: Sam Becker Date: Thu, 29 Feb 2024 16:54:15 -0600 Subject: [PATCH 4/4] Refine date tests --- __tests__/date.test.ts | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/__tests__/date.test.ts b/__tests__/date.test.ts index f113c9b9..6257779e 100644 --- a/__tests__/date.test.ts +++ b/__tests__/date.test.ts @@ -30,16 +30,18 @@ describe('Date utility', () => { .toBe('2023-12-02 16:38:36'); }); }); - it('Empty string', () => { - const timestamp = ' '; - const offset = undefined; - expect(convertTimestampWithOffsetToPostgresString( - timestamp, - offset - )) + it('Malformed date string', () => { + const timestamp = '2024/01a/01 Z'; + expect(convertTimestampWithOffsetToPostgresString(timestamp)) + .toBe(convertTimestampWithOffsetToPostgresString( + new Date().toISOString(), + )); + }); + it('Empty string', () => { + const timestamp = ' '; + expect(convertTimestampWithOffsetToPostgresString(timestamp)) .toBe(convertTimestampWithOffsetToPostgresString( new Date().toISOString(), - offset )); }); });