chore: import upstream snapshot with attribution
CI / Migrate Dev DB (push) Has been skipped
CI / Detect Version (push) Has been cancelled
CI / Migrate DB (push) Has been cancelled
CI / Build Dev ECR (./docker/app.Dockerfile, ECR_APP) (push) Has been cancelled
CI / Build Dev ECR (./docker/db.Dockerfile, ECR_MIGRATIONS) (push) Has been cancelled
CI / Build Dev ECR (./docker/pii.Dockerfile, ECR_PII) (push) Has been cancelled
CI / Build Dev ECR (./docker/realtime.Dockerfile, ECR_REALTIME) (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
CI / Build AMD64 (./docker/app.Dockerfile, ECR_APP, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build AMD64 (./docker/db.Dockerfile, ECR_MIGRATIONS, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (./docker/pii.Dockerfile, ECR_PII, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (./docker/realtime.Dockerfile, ECR_REALTIME, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/app.Dockerfile, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/db.Dockerfile, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/pii.Dockerfile, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/realtime.Dockerfile, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Test and Build (push) Has been cancelled
Publish CLI Package / publish-npm (push) Has been cancelled
Publish Python SDK / publish-pypi (push) Has been cancelled
Publish TypeScript SDK / publish-npm (push) Has been cancelled
CI / Migrate Dev DB (push) Has been skipped
CI / Detect Version (push) Has been cancelled
CI / Migrate DB (push) Has been cancelled
CI / Build Dev ECR (./docker/app.Dockerfile, ECR_APP) (push) Has been cancelled
CI / Build Dev ECR (./docker/db.Dockerfile, ECR_MIGRATIONS) (push) Has been cancelled
CI / Build Dev ECR (./docker/pii.Dockerfile, ECR_PII) (push) Has been cancelled
CI / Build Dev ECR (./docker/realtime.Dockerfile, ECR_REALTIME) (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
CI / Build AMD64 (./docker/app.Dockerfile, ECR_APP, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build AMD64 (./docker/db.Dockerfile, ECR_MIGRATIONS, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (./docker/pii.Dockerfile, ECR_PII, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (./docker/realtime.Dockerfile, ECR_REALTIME, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/app.Dockerfile, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/db.Dockerfile, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/pii.Dockerfile, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/realtime.Dockerfile, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Test and Build (push) Has been cancelled
Publish CLI Package / publish-npm (push) Has been cancelled
Publish Python SDK / publish-pypi (push) Has been cancelled
Publish TypeScript SDK / publish-npm (push) Has been cancelled
This commit is contained in:
@@ -0,0 +1,124 @@
|
||||
/**
|
||||
* @vitest-environment node
|
||||
*/
|
||||
import { hybridAuthMockFns } from '@sim/testing'
|
||||
import { NextRequest } from 'next/server'
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import type { TableDefinition } from '@/lib/table'
|
||||
|
||||
const { mockCheckAccess, mockGetTableJob, mockGeneratePresignedDownloadUrl } = vi.hoisted(() => ({
|
||||
mockCheckAccess: vi.fn(),
|
||||
mockGetTableJob: vi.fn(),
|
||||
mockGeneratePresignedDownloadUrl: vi.fn(),
|
||||
}))
|
||||
|
||||
vi.mock('@/lib/table/jobs/service', () => ({ getTableJob: mockGetTableJob }))
|
||||
vi.mock('@/lib/uploads/core/storage-service', () => ({
|
||||
generatePresignedDownloadUrl: mockGeneratePresignedDownloadUrl,
|
||||
}))
|
||||
vi.mock('@/app/api/table/utils', async () => {
|
||||
const { NextResponse } = await import('next/server')
|
||||
return {
|
||||
checkAccess: mockCheckAccess,
|
||||
accessError: (result: { status: number }) =>
|
||||
NextResponse.json({ error: 'denied' }, { status: result.status }),
|
||||
}
|
||||
})
|
||||
|
||||
import { GET } from '@/app/api/table/[tableId]/export/download/route'
|
||||
|
||||
function buildTable(overrides: Partial<TableDefinition> = {}): TableDefinition {
|
||||
return {
|
||||
id: 'tbl_1',
|
||||
name: 'People',
|
||||
description: null,
|
||||
schema: { columns: [] },
|
||||
metadata: null,
|
||||
rowCount: 0,
|
||||
maxRows: 1_000_000,
|
||||
workspaceId: 'workspace-1',
|
||||
createdBy: 'user-1',
|
||||
archivedAt: null,
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
...overrides,
|
||||
}
|
||||
}
|
||||
|
||||
function makeRequest(query: Record<string, string>, tableId = 'tbl_1') {
|
||||
const qs = new URLSearchParams(query).toString()
|
||||
const req = new NextRequest(`http://localhost:3000/api/table/${tableId}/export/download?${qs}`)
|
||||
return GET(req, { params: Promise.resolve({ tableId }) })
|
||||
}
|
||||
|
||||
const validQuery = { workspaceId: 'workspace-1', jobId: 'job_1' }
|
||||
|
||||
describe('GET /api/table/[tableId]/export/download', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
hybridAuthMockFns.mockCheckSessionOrInternalAuth.mockResolvedValue({
|
||||
success: true,
|
||||
userId: 'user-1',
|
||||
authType: 'session',
|
||||
})
|
||||
mockCheckAccess.mockResolvedValue({ ok: true, table: buildTable() })
|
||||
mockGetTableJob.mockResolvedValue({
|
||||
id: 'job_1',
|
||||
type: 'export',
|
||||
status: 'ready',
|
||||
payload: { format: 'csv', resultKey: 'workspace/workspace-1/exports/tbl_1/job_1/people.csv' },
|
||||
})
|
||||
mockGeneratePresignedDownloadUrl.mockResolvedValue('https://storage.example/signed-url')
|
||||
})
|
||||
|
||||
it('resolves a ready export to a presigned URL', async () => {
|
||||
const response = await makeRequest(validQuery)
|
||||
const data = await response.json()
|
||||
|
||||
expect(response.status).toBe(200)
|
||||
expect(data.data).toEqual({ url: 'https://storage.example/signed-url', fileName: 'people.csv' })
|
||||
expect(mockGeneratePresignedDownloadUrl).toHaveBeenCalledWith(
|
||||
'workspace/workspace-1/exports/tbl_1/job_1/people.csv',
|
||||
'workspace'
|
||||
)
|
||||
})
|
||||
|
||||
it('404s when the job is missing or not an export', async () => {
|
||||
mockGetTableJob.mockResolvedValue({ id: 'job_1', type: 'delete', status: 'ready', payload: {} })
|
||||
const response = await makeRequest(validQuery)
|
||||
expect(response.status).toBe(404)
|
||||
})
|
||||
|
||||
it('409s when the export is not ready yet', async () => {
|
||||
mockGetTableJob.mockResolvedValue({
|
||||
id: 'job_1',
|
||||
type: 'export',
|
||||
status: 'running',
|
||||
payload: { format: 'csv' },
|
||||
})
|
||||
const response = await makeRequest(validQuery)
|
||||
expect(response.status).toBe(409)
|
||||
})
|
||||
|
||||
it('410s when the result file is gone from the payload', async () => {
|
||||
mockGetTableJob.mockResolvedValue({
|
||||
id: 'job_1',
|
||||
type: 'export',
|
||||
status: 'ready',
|
||||
payload: { format: 'csv' },
|
||||
})
|
||||
const response = await makeRequest(validQuery)
|
||||
expect(response.status).toBe(410)
|
||||
})
|
||||
|
||||
it('returns 401 when unauthenticated', async () => {
|
||||
hybridAuthMockFns.mockCheckSessionOrInternalAuth.mockResolvedValue({ success: false })
|
||||
const response = await makeRequest(validQuery)
|
||||
expect(response.status).toBe(401)
|
||||
})
|
||||
|
||||
it('returns 400 on workspace mismatch', async () => {
|
||||
const response = await makeRequest({ ...validQuery, workspaceId: 'other-ws' })
|
||||
expect(response.status).toBe(400)
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,64 @@
|
||||
import { createLogger } from '@sim/logger'
|
||||
import { type NextRequest, NextResponse } from 'next/server'
|
||||
import { exportDownloadContract } from '@/lib/api/contracts/tables'
|
||||
import { parseRequest } from '@/lib/api/server'
|
||||
import { checkSessionOrInternalAuth } from '@/lib/auth/hybrid'
|
||||
import { generateRequestId } from '@/lib/core/utils/request'
|
||||
import { withRouteHandler } from '@/lib/core/utils/with-route-handler'
|
||||
import { getTableJob } from '@/lib/table/jobs/service'
|
||||
import type { TableExportJobPayload } from '@/lib/table/types'
|
||||
import { generatePresignedDownloadUrl } from '@/lib/uploads/core/storage-service'
|
||||
import { accessError, checkAccess } from '@/app/api/table/utils'
|
||||
|
||||
const logger = createLogger('TableExportDownload')
|
||||
|
||||
export const runtime = 'nodejs'
|
||||
export const dynamic = 'force-dynamic'
|
||||
|
||||
interface RouteParams {
|
||||
params: Promise<{ tableId: string }>
|
||||
}
|
||||
|
||||
/**
|
||||
* GET /api/table/[tableId]/export/download?jobId=…
|
||||
*
|
||||
* Resolves a completed export job to a short-lived presigned URL for the generated file. The job
|
||||
* must belong to the table, be an export, and be `ready` — the worker stamps `resultKey` onto the
|
||||
* job payload when the upload lands.
|
||||
*/
|
||||
export const GET = withRouteHandler(async (request: NextRequest, { params }: RouteParams) => {
|
||||
const requestId = generateRequestId()
|
||||
|
||||
const authResult = await checkSessionOrInternalAuth(request, { requireWorkflowId: false })
|
||||
if (!authResult.success || !authResult.userId) {
|
||||
return NextResponse.json({ error: 'Authentication required' }, { status: 401 })
|
||||
}
|
||||
|
||||
const parsed = await parseRequest(exportDownloadContract, request, { params })
|
||||
if (!parsed.success) return parsed.response
|
||||
const { tableId } = parsed.data.params
|
||||
const { workspaceId, jobId } = parsed.data.query
|
||||
|
||||
const access = await checkAccess(tableId, authResult.userId, 'read')
|
||||
if (!access.ok) return accessError(access, requestId, tableId)
|
||||
if (access.table.workspaceId !== workspaceId) {
|
||||
return NextResponse.json({ error: 'Invalid workspace ID' }, { status: 400 })
|
||||
}
|
||||
|
||||
const job = await getTableJob(tableId, jobId)
|
||||
if (!job || job.type !== 'export') {
|
||||
return NextResponse.json({ error: 'Export job not found' }, { status: 404 })
|
||||
}
|
||||
if (job.status !== 'ready') {
|
||||
return NextResponse.json({ error: 'Export is not ready' }, { status: 409 })
|
||||
}
|
||||
const payload = job.payload as TableExportJobPayload | null
|
||||
if (!payload?.resultKey) {
|
||||
return NextResponse.json({ error: 'Export file is no longer available' }, { status: 410 })
|
||||
}
|
||||
|
||||
const url = await generatePresignedDownloadUrl(payload.resultKey, 'workspace')
|
||||
const fileName = payload.resultKey.split('/').pop() ?? `export.${payload.format}`
|
||||
logger.info(`[${requestId}] Export download URL issued`, { tableId, jobId })
|
||||
return NextResponse.json({ success: true, data: { url, fileName } })
|
||||
})
|
||||
@@ -0,0 +1,95 @@
|
||||
/**
|
||||
* @vitest-environment node
|
||||
*/
|
||||
import { hybridAuthMockFns } from '@sim/testing'
|
||||
import { NextRequest } from 'next/server'
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import type { TableDefinition } from '@/lib/table'
|
||||
|
||||
const { mockCheckAccess, mockQueryRows } = vi.hoisted(() => ({
|
||||
mockCheckAccess: vi.fn(),
|
||||
mockQueryRows: vi.fn(),
|
||||
}))
|
||||
|
||||
vi.mock('@/app/api/table/utils', async () => {
|
||||
const { NextResponse } = await import('next/server')
|
||||
return {
|
||||
checkAccess: mockCheckAccess,
|
||||
accessError: (result: { status: number }) =>
|
||||
NextResponse.json({ error: 'Access denied' }, { status: result.status }),
|
||||
}
|
||||
})
|
||||
|
||||
vi.mock('@/lib/table/rows/service', () => ({
|
||||
queryRows: mockQueryRows,
|
||||
}))
|
||||
|
||||
import { GET } from '@/app/api/table/[tableId]/export/route'
|
||||
|
||||
/** Table with an id-native column whose stable id (`col_email`) differs from its display name. */
|
||||
function buildTable(): TableDefinition {
|
||||
return {
|
||||
id: 'tbl_1',
|
||||
name: 'People',
|
||||
description: null,
|
||||
schema: {
|
||||
columns: [
|
||||
{ id: 'col_email', name: 'email', type: 'string' },
|
||||
{ name: 'legacy', type: 'string' }, // legacy: id == name
|
||||
],
|
||||
},
|
||||
metadata: null,
|
||||
rowCount: 1,
|
||||
maxRows: 100,
|
||||
workspaceId: 'workspace-1',
|
||||
createdBy: 'user-1',
|
||||
archivedAt: null,
|
||||
createdAt: new Date('2024-01-01'),
|
||||
updatedAt: new Date('2024-01-01'),
|
||||
}
|
||||
}
|
||||
|
||||
function callGet(format: string) {
|
||||
const req = new NextRequest(`http://localhost:3000/api/table/tbl_1/export?format=${format}`, {
|
||||
method: 'GET',
|
||||
})
|
||||
return GET(req, { params: Promise.resolve({ tableId: 'tbl_1' }) })
|
||||
}
|
||||
|
||||
describe('table export route — id→name translation', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
hybridAuthMockFns.mockCheckSessionOrInternalAuth.mockResolvedValue({
|
||||
success: true,
|
||||
userId: 'user-1',
|
||||
authType: 'session',
|
||||
})
|
||||
mockCheckAccess.mockResolvedValue({ ok: true, table: buildTable() })
|
||||
// Row data is keyed by stable column id (`col_email`), not the display name.
|
||||
mockQueryRows.mockResolvedValue({
|
||||
rows: [{ id: 'r1', data: { col_email: 'a@b.c', legacy: 'x' }, executions: {}, position: 0 }],
|
||||
rowCount: 1,
|
||||
totalCount: 1,
|
||||
limit: 1000,
|
||||
offset: 0,
|
||||
})
|
||||
})
|
||||
|
||||
it('CSV: header uses display names and cell values resolve from id-keyed data', async () => {
|
||||
const res = await callGet('csv')
|
||||
expect(res.status).toBe(200)
|
||||
const body = await res.text()
|
||||
const [header, firstRow] = body.trim().split('\n')
|
||||
expect(header).toBe('email,legacy')
|
||||
// Without id→name resolution the email cell would be blank.
|
||||
expect(firstRow).toBe('a@b.c,x')
|
||||
})
|
||||
|
||||
it('JSON: keys are display names, never the stable column id', async () => {
|
||||
const res = await callGet('json')
|
||||
expect(res.status).toBe(200)
|
||||
const parsed = JSON.parse(await res.text())
|
||||
expect(parsed).toEqual([{ email: 'a@b.c', legacy: 'x' }])
|
||||
expect(JSON.stringify(parsed)).not.toContain('col_email')
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,168 @@
|
||||
import { AuditAction, AuditResourceType, recordAudit } from '@sim/audit'
|
||||
import { createLogger } from '@sim/logger'
|
||||
import { type NextRequest, NextResponse } from 'next/server'
|
||||
import { tableExportFormatSchema, tableIdParamsSchema } from '@/lib/api/contracts/tables'
|
||||
import { getValidationErrorMessage } from '@/lib/api/server'
|
||||
import { checkSessionOrInternalAuth } from '@/lib/auth/hybrid'
|
||||
import { neutralizeCsvFormula } from '@/lib/core/utils/csv'
|
||||
import { generateRequestId } from '@/lib/core/utils/request'
|
||||
import { withRouteHandler } from '@/lib/core/utils/with-route-handler'
|
||||
import { captureServerEvent } from '@/lib/posthog/server'
|
||||
import { buildNameById, getColumnId, rowDataIdToName } from '@/lib/table/column-keys'
|
||||
import { queryRows } from '@/lib/table/rows/service'
|
||||
import { accessError, checkAccess } from '@/app/api/table/utils'
|
||||
|
||||
const logger = createLogger('TableExport')
|
||||
|
||||
const EXPORT_BATCH_SIZE = 1000
|
||||
|
||||
type ExportFormat = 'csv' | 'json'
|
||||
|
||||
interface RouteParams {
|
||||
params: Promise<{ tableId: string }>
|
||||
}
|
||||
|
||||
/** GET /api/table/[tableId]/export - Streams the full table contents as CSV or JSON. */
|
||||
export const GET = withRouteHandler(async (request: NextRequest, { params }: RouteParams) => {
|
||||
const requestId = generateRequestId()
|
||||
const { tableId } = tableIdParamsSchema.parse(await params)
|
||||
|
||||
const auth = await checkSessionOrInternalAuth(request, { requireWorkflowId: false })
|
||||
if (!auth.success || !auth.userId) {
|
||||
return NextResponse.json({ error: 'Authentication required' }, { status: 401 })
|
||||
}
|
||||
const userId = auth.userId
|
||||
|
||||
const { searchParams } = new URL(request.url)
|
||||
const formatValidation = tableExportFormatSchema.safeParse(
|
||||
searchParams.get('format') ?? undefined
|
||||
)
|
||||
if (!formatValidation.success) {
|
||||
return NextResponse.json(
|
||||
{ error: getValidationErrorMessage(formatValidation.error) },
|
||||
{ status: 400 }
|
||||
)
|
||||
}
|
||||
const format: ExportFormat = formatValidation.data
|
||||
|
||||
const access = await checkAccess(tableId, auth.userId, 'read')
|
||||
if (!access.ok) return accessError(access, requestId, tableId)
|
||||
const { table } = access
|
||||
|
||||
const columns = table.schema.columns
|
||||
// Stored row data is id-keyed; CSV headers and JSON keys are display names, so
|
||||
// translate id → name on the way out (export is a name-friendly boundary).
|
||||
const nameById = buildNameById(table.schema)
|
||||
const safeName = sanitizeFilename(table.name)
|
||||
const filename = `${safeName}.${format}`
|
||||
|
||||
// Audit before streaming: rows leave incrementally, so a mid-stream failure still exfiltrates partial data.
|
||||
recordAudit({
|
||||
workspaceId: table.workspaceId ?? null,
|
||||
actorId: userId,
|
||||
action: AuditAction.TABLE_EXPORTED,
|
||||
resourceType: AuditResourceType.TABLE,
|
||||
resourceId: tableId,
|
||||
resourceName: table.name,
|
||||
description: `Exported table "${table.name}" as ${format.toUpperCase()}`,
|
||||
metadata: { format, rowCount: table.rowCount },
|
||||
request,
|
||||
})
|
||||
if (table.workspaceId) {
|
||||
captureServerEvent(
|
||||
userId,
|
||||
'table_exported',
|
||||
{ table_id: tableId, workspace_id: table.workspaceId },
|
||||
{ groups: { workspace: table.workspaceId } }
|
||||
)
|
||||
}
|
||||
|
||||
const stream = new ReadableStream<Uint8Array>({
|
||||
async start(controller) {
|
||||
const encoder = new TextEncoder()
|
||||
try {
|
||||
if (format === 'csv') {
|
||||
controller.enqueue(
|
||||
encoder.encode(`${toCsvRow(columns.map((c) => neutralizeCsvFormula(c.name)))}\n`)
|
||||
)
|
||||
} else {
|
||||
controller.enqueue(encoder.encode('['))
|
||||
}
|
||||
|
||||
let offset = 0
|
||||
let firstJsonRow = true
|
||||
while (true) {
|
||||
const result = await queryRows(
|
||||
table,
|
||||
{ limit: EXPORT_BATCH_SIZE, offset, includeTotal: false },
|
||||
requestId
|
||||
)
|
||||
|
||||
for (const row of result.rows) {
|
||||
if (format === 'csv') {
|
||||
const values = columns.map((c) => formatCsvValue(row.data[getColumnId(c)]))
|
||||
controller.enqueue(encoder.encode(`${toCsvRow(values)}\n`))
|
||||
} else {
|
||||
const prefix = firstJsonRow ? '' : ','
|
||||
firstJsonRow = false
|
||||
controller.enqueue(
|
||||
encoder.encode(prefix + JSON.stringify(rowDataIdToName(row.data, nameById)))
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
if (result.rows.length < EXPORT_BATCH_SIZE) break
|
||||
offset += result.rows.length
|
||||
}
|
||||
|
||||
if (format === 'json') controller.enqueue(encoder.encode(']'))
|
||||
controller.close()
|
||||
|
||||
logger.info(`[${requestId}] Exported table ${tableId}`, {
|
||||
format,
|
||||
rowCount: table.rowCount,
|
||||
})
|
||||
} catch (err) {
|
||||
logger.error(`[${requestId}] Export failed for table ${tableId}`, err)
|
||||
controller.error(err)
|
||||
}
|
||||
},
|
||||
})
|
||||
|
||||
return new NextResponse(stream, {
|
||||
status: 200,
|
||||
headers: {
|
||||
'Content-Type': format === 'csv' ? 'text/csv; charset=utf-8' : 'application/json',
|
||||
'Content-Disposition': `attachment; filename="${filename}"`,
|
||||
'Cache-Control': 'no-store',
|
||||
},
|
||||
})
|
||||
})
|
||||
|
||||
function sanitizeFilename(name: string): string {
|
||||
const cleaned = name.replace(/[^a-zA-Z0-9_-]+/g, '_').replace(/^_+|_+$/g, '')
|
||||
return cleaned || 'table'
|
||||
}
|
||||
|
||||
/**
|
||||
* Serializes a cell for CSV. Only string cells are formula-neutralized; numbers,
|
||||
* booleans, dates, and JSON objects can never form a trigger and pass through verbatim.
|
||||
*/
|
||||
function formatCsvValue(value: unknown): string {
|
||||
if (value === null || value === undefined) return ''
|
||||
if (value instanceof Date) return value.toISOString()
|
||||
if (typeof value === 'object') return JSON.stringify(value)
|
||||
if (typeof value === 'string') return neutralizeCsvFormula(value)
|
||||
return String(value)
|
||||
}
|
||||
|
||||
function toCsvRow(values: string[]): string {
|
||||
return values.map(escapeCsvField).join(',')
|
||||
}
|
||||
|
||||
function escapeCsvField(field: string): string {
|
||||
if (/[",\n\r]/.test(field)) {
|
||||
return `"${field.replace(/"/g, '""')}"`
|
||||
}
|
||||
return field
|
||||
}
|
||||
Reference in New Issue
Block a user