Unverified Commit e32086b0 authored by vercel-fleet-prod[bot]'s avatar vercel-fleet-prod[bot] Committed by GitHub

Add bundle source CLI queries

Co-authored-by: 's avatarWill Binns-Smith <755844+wbinnssmith@users.noreply.github.com>
parent 39d4f6e6
...@@ -5,11 +5,27 @@ import z from 'next/dist/compiled/zod' ...@@ -5,11 +5,27 @@ import z from 'next/dist/compiled/zod'
const DEFAULT_LIMIT = 50 const DEFAULT_LIMIT = 50
const MAX_LIMIT = 100 const MAX_LIMIT = 100
const MAX_CHUNKS = 100
const COMPRESSED_CAVEAT = const COMPRESSED_CAVEAT =
'Compressed source sizes are estimates because attributed parts are compressed independently.' 'Compressed source sizes are estimates because attributed parts are compressed independently.'
type Environment = 'total' | 'client' type Environment = 'total' | 'client' | 'server'
type Metric = 'raw' | 'compressed' type Metric = 'raw' | 'compressed'
type FileType = 'js' | 'css' | 'json' | 'asset'
interface SourceRow {
key: string
sourcePath?: string
packageName?: string
rawSize: number
compressedSize: number
client: boolean
server: boolean
traced: boolean
chunks: string[]
chunkCount: number
chunksTruncated: boolean
}
interface OverviewArgs { interface OverviewArgs {
snapshot?: string snapshot?: string
...@@ -20,6 +36,18 @@ interface OverviewArgs { ...@@ -20,6 +36,18 @@ interface OverviewArgs {
limit?: number limit?: number
} }
interface SourcesArgs {
route: string
snapshot?: string
search?: string
environment?: Environment
fileTypes?: FileType[]
groupBy?: 'source' | 'package'
metric?: Metric
offset?: number
limit?: number
}
export class AnalyzeQueryError extends Error { export class AnalyzeQueryError extends Error {
constructor(readonly output: Record<string, unknown>) { constructor(readonly output: Record<string, unknown>) {
super( super(
...@@ -137,6 +165,100 @@ function routeSizes(data: AnalyzeData, environment: Environment) { ...@@ -137,6 +165,100 @@ function routeSizes(data: AnalyzeData, environment: Environment) {
return { rawSize, compressedSize } return { rawSize, compressedSize }
} }
function packageNameFromPath(sourcePath: string): string | undefined {
const marker = 'node_modules/'
const start = sourcePath.lastIndexOf(marker)
if (start === -1) return undefined
const parts = sourcePath.slice(start + marker.length).split('/')
if (!parts[0] || parts[0] === '.pnpm') return undefined
return parts[0].startsWith('@') && parts[1]
? `${parts[0]}/${parts[1]}`
: parts[0]
}
function hasFileType(
flags: ReturnType<AnalyzeData['getSourceFlags']>,
types: FileType[] | undefined
): boolean {
return !types?.length || types.some((type) => flags[type])
}
function collectSources(
data: AnalyzeData,
options: {
search?: string
environment: Environment
fileTypes?: FileType[]
groupBy: 'source' | 'package'
}
): SourceRow[] {
const search = options.search?.toLocaleLowerCase()
const rows = new Map<string, SourceRow>()
for (let index = 0; index < data.sourceCount(); index++) {
if (data.sourceChunkParts(index).length === 0) continue
const sourcePath = data.getFullSourcePath(index)
if (
!sourcePath ||
(search && !sourcePath.toLocaleLowerCase().includes(search))
) {
continue
}
const flags = data.getSourceFlags(index)
if (
(options.environment === 'client' && !flags.client) ||
(options.environment === 'server' && !flags.server) ||
!hasFileType(flags, options.fileTypes)
) {
continue
}
const sizes = sourceSizes(data, index, options.environment)
if (sizes.rawSize === 0 && sizes.compressedSize === 0) continue
const packageName = packageNameFromPath(sourcePath)
const key =
options.groupBy === 'package' && packageName ? packageName : sourcePath
const existing = rows.get(key)
if (existing) {
existing.rawSize += sizes.rawSize
existing.compressedSize += sizes.compressedSize
existing.client ||= flags.client
existing.server ||= flags.server
existing.traced ||= flags.traced
existing.chunks.push(...data.sourceChunks(index))
} else {
rows.set(key, {
key,
...(options.groupBy === 'package' && packageName
? { packageName }
: { sourcePath }),
...sizes,
client: flags.client,
server: flags.server,
traced: flags.traced,
chunks: data.sourceChunks(index),
chunkCount: 0,
chunksTruncated: false,
})
}
}
return [...rows.values()].map((row) => {
const chunks = [...new Set(row.chunks)].sort(compareText)
return {
...row,
chunks: chunks.slice(0, MAX_CHUNKS),
chunkCount: chunks.length,
chunksTruncated: chunks.length > MAX_CHUNKS,
}
})
}
function sortBySize(rows: SourceRow[], metric: Metric): SourceRow[] {
return rows.sort(
(a, b) =>
metricValue(b, metric) - metricValue(a, metric) ||
compareText(a.key, b.key)
)
}
function provenance(metadata: SnapshotMetadata) { function provenance(metadata: SnapshotMetadata) {
return { return {
id: metadata.id, id: metadata.id,
...@@ -156,6 +278,7 @@ const pagingSchema = { ...@@ -156,6 +278,7 @@ const pagingSchema = {
} }
const snapshotSchema = z.string().max(100).optional() const snapshotSchema = z.string().max(100).optional()
const metricSchema = z.enum(['raw', 'compressed']).optional() const metricSchema = z.enum(['raw', 'compressed']).optional()
const environmentSchema = z.enum(['total', 'client', 'server']).optional()
export function createAnalyzeQueryRegistry(repository: AnalyzeRepository) { export function createAnalyzeQueryRegistry(repository: AnalyzeRepository) {
const queries: StoredQuery[] = [] const queries: StoredQuery[] = []
...@@ -217,5 +340,48 @@ export function createAnalyzeQueryRegistry(repository: AnalyzeRepository) { ...@@ -217,5 +340,48 @@ export function createAnalyzeQueryRegistry(repository: AnalyzeRepository) {
}) })
) )
registerQuery(
'query_bundle_sources',
{
route: z.string().max(4096),
snapshot: snapshotSchema,
search: z.string().max(1000).optional(),
environment: environmentSchema,
fileTypes: z
.array(z.enum(['js', 'css', 'json', 'asset']))
.max(4)
.optional(),
groupBy: z.enum(['source', 'package']).optional(),
metric: metricSchema,
...pagingSchema,
},
safeQuery(async (args: SourcesArgs) => {
const environment = args.environment ?? 'total'
const metric = args.metric ?? 'raw'
const rows = sortBySize(
collectSources(await repository.loadRoute(args.snapshot, args.route), {
search: args.search,
environment,
fileTypes: args.fileTypes,
groupBy: args.groupBy ?? 'source',
}),
metric
)
const paged = page(rows, args.offset ?? 0, args.limit ?? DEFAULT_LIMIT)
return {
route: args.route,
snapshot: provenance(
(await repository.getSnapshot(args.snapshot)).metadata
),
environment,
metric,
groupBy: args.groupBy ?? 'source',
sources: paged.values,
pagination: paged.pagination,
caveats: [COMPRESSED_CAVEAT],
}
})
)
return new AnalyzeQueryRegistry(queries) return new AnalyzeQueryRegistry(queries)
} }
import { mkdtemp, mkdir, rm, writeFile } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import path from 'node:path'
import { AnalyzeRepository } from './repository'
const directories: string[] = []
afterEach(async () => {
await Promise.all(
directories.splice(0).map((directory) => rm(directory, { recursive: true }))
)
})
async function fixture(): Promise<AnalyzeRepository> {
const directory = await mkdtemp(
path.join(tmpdir(), 'next-analyze-repository-')
)
directories.push(directory)
await mkdir(path.join(directory, 'data'), { recursive: true })
await mkdir(path.join(directory, 'history'), { recursive: true })
const metadata = {
id: '20250101-000000-local',
createdAt: '2025-01-01T00:00:00.000Z',
routeCount: 1,
}
await writeFile(
path.join(directory, 'data', 'metadata.json'),
JSON.stringify(metadata)
)
await writeFile(
path.join(directory, 'data', 'routes.json'),
JSON.stringify(['/'])
)
await writeFile(
path.join(directory, 'history', 'history.json'),
JSON.stringify({ snapshots: [] })
)
return new AnalyzeRepository(directory)
}
describe('AnalyzeRepository path validation', () => {
it.each(['/../secret', '/%2e%2e/secret', '/foo\\..\\secret', 'relative'])(
'rejects unsafe route %s',
async (route) => {
const repository = await fixture()
await expect(repository.loadRoute('current', route)).rejects.toThrow(
'Invalid route'
)
}
)
it('rejects unknown routes before reading a dataset', async () => {
const repository = await fixture()
await expect(repository.loadRoute('current', '/unknown')).rejects.toThrow(
'Unknown route'
)
})
it('rejects malformed and unknown snapshot identifiers', async () => {
const repository = await fixture()
await expect(repository.getSnapshot('../data')).rejects.toThrow(
'Invalid snapshot identifier'
)
await expect(
repository.getSnapshot('20250101-000001-local')
).rejects.toThrow('Unknown snapshot')
})
})
...@@ -112,6 +112,19 @@ describe('next experimental-analyze', () => { ...@@ -112,6 +112,19 @@ describe('next experimental-analyze', () => {
pagination: { limit: 1, returned: 1 }, pagination: { limit: 1, returned: 1 },
routes: [{ route: expect.any(String), rawSize: expect.any(Number) }], routes: [{ route: expect.any(String), rawSize: expect.any(Number) }],
}) })
const sources = await next.runCommand([
'experimental-analyze',
'--query',
'query_bundle_sources',
'--input',
'{"route":"/","limit":1}',
])
expect(sources.exitCode).toBe(0)
expect(JSON.parse(sources.stdout)).toMatchObject({
route: '/',
sources: [{ sourcePath: expect.any(String) }],
})
}) })
}) })
}) })
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment