diff --git a/lib/shortener/google.ts b/lib/shortener/google.ts index 295c3158a..5d57d0413 100644 --- a/lib/shortener/google.ts +++ b/lib/shortener/google.ts @@ -58,63 +58,74 @@ export class ShortLinkResolver { } async resolve(url: string): Promise<{longUrl: string}> { - // Extract fragment from URL (e.g., "abcd" from "https://goo.gl/abcd") - const fragment = url.split('/').pop()?.split('?')[0]; + const fragment = this.extractFragment(url); - if (this.dynamoDb && this.tableName && fragment) { - try { - const result = await this.dynamoDb.getItem({ - TableName: this.tableName, - Key: { - fragment: {S: fragment}, - }, - }); - - if (result.Item?.expanded_url) { - const expandedUrl = result.Item.expanded_url.S; - if (!expandedUrl) { - throw new Error('404: Not Found'); - } - GoogleLinkDynamoDbHitCounter.inc(); - return { - longUrl: expandedUrl, - }; - } - // DynamoDB is configured but the link wasn't found - GoogleLinkDynamoDbMissCounter.inc(); - logger.warn( - `Google short link '${fragment}' not found in DynamoDB, falling back to Google URL shortener`, - ); - } catch (err) { - // If DynamoDB lookup fails, fall back to Google URL shortener - logger.error('DynamoDB lookup failed:', err); + if (this.hasDynamoDbConfigured() && fragment) { + const dynamoResult = await this.tryDynamoDbLookup(fragment); + if (dynamoResult) { + return dynamoResult; } } // In August 2025, the Google URL shortener will stop working. At that point, use this code: // throw new Error('404: Not Found'); - // Fall back to Google URL shortener - if (this.dynamoDb && this.tableName) { - // If we reach here with DynamoDB configured, it means the link wasn't found + return this.fallbackToGoogleShortener(url); + } + + // Exported for testing + extractFragment(url: string): string | undefined { + return url.split('/').pop()?.split('?')[0]; + } + + // Exported for testing + hasDynamoDbConfigured(): boolean { + return !!(this.dynamoDb && this.tableName); + } + + private async tryDynamoDbLookup(fragment: string): Promise<{longUrl: string} | null> { + try { + const result = await this.dynamoDb!.getItem({ + TableName: this.tableName!, + Key: { + fragment: {S: fragment}, + }, + }); + + const expandedUrl = result.Item?.expanded_url?.S; + if (expandedUrl) { + GoogleLinkDynamoDbHitCounter.inc(); + return {longUrl: expandedUrl}; + } + + GoogleLinkDynamoDbMissCounter.inc(); + logger.warn(`Google short link '${fragment}' not found in DynamoDB, falling back to Google URL shortener`); + return null; + } catch (err) { + logger.error('DynamoDB lookup failed:', err); + return null; + } + } + + private async fallbackToGoogleShortener(url: string): Promise<{longUrl: string}> { + if (this.hasDynamoDbConfigured()) { logger.warn(`Falling back to Google URL shortener for '${url}' - this indicates missing data in DynamoDB`); } - const settings: RequestInit = { + const res = await fetch(url + '?si=1', { method: 'HEAD', redirect: 'manual', - }; + }); - const res = await fetch(url + '?si=1', settings); if (res.status !== 302) { throw new Error(`Got response ${res.status}`); } + const targetLocation = res.headers.get('Location'); if (!targetLocation) { throw new Error('Missing location url'); } - return { - longUrl: targetLocation, - }; + + return {longUrl: targetLocation}; } } diff --git a/test/google-tests.ts b/test/google-tests.ts index 7a570db98..18826607d 100644 --- a/test/google-tests.ts +++ b/test/google-tests.ts @@ -76,3 +76,63 @@ describe('Google short URL resolver tests', () => { }); }); }); + +describe('ShortLinkResolver utility methods', () => { + const resolver = new google.ShortLinkResolver(); + + describe('extractFragment', () => { + it('should extract fragment from goo.gl URL', () => { + expect(resolver.extractFragment('https://goo.gl/abcd1234')).toBe('abcd1234'); + }); + + it('should extract fragment from URL with query parameters', () => { + expect(resolver.extractFragment('https://goo.gl/xyz789?param=value')).toBe('xyz789'); + }); + + it('should handle URL with multiple query parameters', () => { + expect(resolver.extractFragment('https://goo.gl/test123?foo=bar&baz=qux')).toBe('test123'); + }); + + it('should handle URL with fragment and query', () => { + expect(resolver.extractFragment('https://goo.gl/hello?world=123')).toBe('hello'); + }); + + it('should handle simple fragment', () => { + expect(resolver.extractFragment('https://goo.gl/x')).toBe('x'); + }); + + it('should handle URL ending with slash', () => { + expect(resolver.extractFragment('https://goo.gl/fragment/')).toBe(''); + }); + + it('should return empty string for empty URL', () => { + expect(resolver.extractFragment('')).toBe(''); + }); + + it('should return empty string for URL with no path segments', () => { + expect(resolver.extractFragment('https://')).toBe(''); + }); + + it('should handle URL without fragment', () => { + expect(resolver.extractFragment('https://goo.gl/')).toBe(''); + }); + }); + + describe('hasDynamoDbConfigured', () => { + it('should return false for resolver without DynamoDB config', () => { + const basicResolver = new google.ShortLinkResolver(); + expect(basicResolver.hasDynamoDbConfigured()).toBe(false); + }); + + it('should return false for resolver with undefined props', () => { + const basicResolver = new google.ShortLinkResolver(undefined); + expect(basicResolver.hasDynamoDbConfigured()).toBe(false); + }); + + it('should return false for resolver with empty props', () => { + const mockProps = vi.fn().mockReturnValue(undefined); + const basicResolver = new google.ShortLinkResolver(mockProps); + expect(basicResolver.hasDynamoDbConfigured()).toBe(false); + }); + }); +});