-
Notifications
You must be signed in to change notification settings - Fork 130
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add status codes and event methods to log requests
This commit moves the logRequestLine function into a separate file and adds status codes and event methods. It grabs both get and post requests
- Loading branch information
1 parent
ef08e31
commit 581abd8
Showing
6 changed files
with
122 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@shopify/cli': patch | ||
--- | ||
|
||
Improve user logging by adding status codes and event methods |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import {shouldLog} from './log-request-line.js' | ||
import {createEvent} from 'h3' | ||
import {describe, test, expect} from 'vitest' | ||
import {IncomingMessage, ServerResponse} from 'node:http' | ||
import {Socket} from 'node:net' | ||
|
||
function createH3Event(method = 'GET', path = '/', headers = {}) { | ||
const req = new IncomingMessage(new Socket()) | ||
const res = new ServerResponse(req) | ||
|
||
req.method = method | ||
req.url = path | ||
req.headers = headers | ||
|
||
return createEvent(req, res) | ||
} | ||
|
||
describe('shouldLog', () => { | ||
test('returns false for paths with ignored prefixes', () => { | ||
const event = createH3Event('GET', '/checkouts/some-path') | ||
expect(shouldLog(event)).toBe(false) | ||
}) | ||
|
||
test('returns false for paths with ignored extensions', () => { | ||
const event = createH3Event('GET', '/assets/styles.css') | ||
expect(shouldLog(event)).toBe(false) | ||
}) | ||
|
||
test('returns true for paths without ignored prefixes or extensions', () => { | ||
const event = createH3Event('GET', '/products/some-product') | ||
expect(shouldLog(event)).toBe(true) | ||
}) | ||
|
||
test('returns false for paths with query parameters and ignored extensions', () => { | ||
const event = createH3Event('GET', '/assets/script.js?version=1.2.3') | ||
expect(shouldLog(event)).toBe(false) | ||
}) | ||
|
||
test('returns true for paths with query parameters and no ignored extensions', () => { | ||
const event = createH3Event('GET', '/products/some-product?variant=123') | ||
expect(shouldLog(event)).toBe(true) | ||
}) | ||
|
||
test('returns false for paths with EXTENSION_CDN_PREFIX', () => { | ||
const event = createH3Event('GET', '/cdn/extension/some-path') | ||
expect(shouldLog(event)).toBe(false) | ||
}) | ||
|
||
test('returns false for paths with VANITY_CDN_PREFIX', () => { | ||
const event = createH3Event('GET', '/cdn/vanity/some-path') | ||
expect(shouldLog(event)).toBe(false) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import {EXTENSION_CDN_PREFIX, VANITY_CDN_PREFIX} from './theme-environment/proxy.js' | ||
import {timestampDateFormat} from '../constants.js' | ||
import {Response as CliKitResponse} from '@shopify/cli-kit/node/http' | ||
import {outputContent, outputInfo, outputToken} from '@shopify/cli-kit/node/output' | ||
import {H3Event} from 'h3' | ||
import {extname} from '@shopify/cli-kit/node/path' | ||
|
||
const CHARACTER_TRUNCATION_LIMIT = 80 | ||
|
||
export function logRequestLine(event: H3Event, response: CliKitResponse | Response) { | ||
if (shouldLog(event) === false) return | ||
|
||
const truncatedPath = | ||
event.path.length > CHARACTER_TRUNCATION_LIMIT | ||
? `${event.path.substring(0, CHARACTER_TRUNCATION_LIMIT)}...` | ||
: event.path | ||
const serverTiming = response.headers.get('server-timing') | ||
const requestDuration = serverTiming?.match(/cfRequestDuration;dur=([\d.]+)/)?.[1] | ||
const durationString = requestDuration ? `${Math.round(Number(requestDuration))}ms` : '' | ||
|
||
const statusColor = getColorizeStatus(response.status) | ||
|
||
const eventMethodAligned = event.method.padStart(6) | ||
|
||
outputInfo( | ||
outputContent`• ${timestampDateFormat.format(new Date())} Request ${outputToken.raw( | ||
'»', | ||
)} ${eventMethodAligned} ${statusColor(String(response.status))} ${truncatedPath} ${outputToken.gray( | ||
`${durationString}`, | ||
)}`, | ||
) | ||
} | ||
|
||
export function shouldLog(event: H3Event) { | ||
const ignoredPathPrefixes = [EXTENSION_CDN_PREFIX, VANITY_CDN_PREFIX, '/checkouts', '/payments'] | ||
const ignoredExtensions = ['.js', '.css', '.json', '.map'] | ||
|
||
if (ignoredPathPrefixes.some((prefix) => event.path.startsWith(prefix))) return false | ||
|
||
const [pathname] = event.path.split('?') as [string] | ||
const extension = extname(pathname) | ||
|
||
if (ignoredExtensions.includes(extension)) return false | ||
|
||
return true | ||
} | ||
|
||
function getColorizeStatus(status: number) { | ||
if (status < 300) { | ||
return outputToken.green | ||
} else if (status < 400) { | ||
return outputToken.yellow | ||
} else { | ||
return outputToken.errorText | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters