Skip to content

Commit d2cf60a

Browse files
committed
fix(api): stop encoding large v2 source answers in the route, which Next strips the Content-Encoding of
Next's sendResponse drops a route handler's Content-Encoding header (FORBIDDEN_HEADERS in send-response.js), so a production server sent brotli bytes as plain JSON and every client failed to parse them (the workflow source e2e: JSON Parse error). The routes answer plain JSON again; the CLI still offers br and gzip for a server or proxy that compresses.
1 parent da088b2 commit d2cf60a

8 files changed

Lines changed: 8 additions & 176 deletions

File tree

‎apps/sim/app/api/v2/workflows/[workflowId]/source/route.ts‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ export const GET = defineV2JsonRoute({
3333
}),
3434
useCase: readWorkflowSource,
3535
present: (source) => ({ data: source }),
36-
encodeLargeBody: true,
3736
})
3837

3938
/**

‎apps/sim/app/api/v2/workspaces/[workspaceId]/source/route.ts‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ export const GET = defineV2JsonRoute({
3636
}),
3737
useCase: withSourceSlot(readWorkspaceSource, 'read'),
3838
present: (source) => ({ data: source }),
39-
encodeLargeBody: true,
4039
})
4140

4241
/**

‎apps/sim/app/api/v2/workspaces/[workspaceId]/workflows/source/catalog/route.ts‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ export const revalidate = 0
99

1010
/**
1111
* GET /api/v2/workspaces/{workspaceId}/workflows/source/catalog — the source catalog. The
12-
* response carries `ETag: "<hash>"`; a matching `If-None-Match` answers `304` with no body. The
13-
* catalog runs to megabytes, so a client accepting brotli or gzip gets it encoded.
12+
* response carries `ETag: "<hash>"`; a matching `If-None-Match` answers `304` with no body.
1413
*/
1514
export const GET = defineV2JsonRoute({
1615
contract: v2GetSourceCatalogContract,
@@ -25,5 +24,4 @@ export const GET = defineV2JsonRoute({
2524
useCase: readSourceCatalog,
2625
entityTag: (catalog) => catalog.hash,
2726
present: (catalog) => ({ data: catalog }),
28-
encodeLargeBody: true,
2927
})

‎apps/sim/app/api/v2/workspaces/[workspaceId]/workflows/source/resources/route.ts‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,4 @@ export const GET = defineV2JsonRoute({
2525
mapInput: ({ params }) => ({ workspaceId: params.workspaceId }),
2626
useCase: readSourceResources,
2727
present: (manifest) => ({ data: manifest }),
28-
encodeLargeBody: true,
2928
})

‎apps/sim/lib/api/server/routes/response-compression.ts‎

Lines changed: 0 additions & 59 deletions
This file was deleted.

‎apps/sim/lib/api/server/routes/v2-json-route.test.ts‎

Lines changed: 0 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { brotliDecompressSync, gunzipSync } from 'node:zlib'
21
import type { PersonalApiKeyPrincipal } from '@sim/auth/principal'
32
import {
43
MockV2ApiKeyUnauthenticatedError,
@@ -232,70 +231,6 @@ describe('defineV2JsonRoute', () => {
232231
expect(unchanged.headers.get('Cache-Control')).toBe('private, no-cache')
233232
})
234233

235-
it.each([
236-
{ acceptEncoding: 'br, gzip', encoding: 'br' },
237-
{ acceptEncoding: 'gzip, deflate', encoding: 'gzip' },
238-
{ acceptEncoding: 'br;q=0, gzip', encoding: 'gzip' },
239-
{ acceptEncoding: 'gzip;q=0.5, *', encoding: 'br' },
240-
{ acceptEncoding: 'identity', encoding: null },
241-
{ acceptEncoding: null, encoding: null },
242-
])(
243-
'encodes a large body as the request accepts ($acceptEncoding → $encoding), keeping the tag weak',
244-
async ({ acceptEncoding, encoding }) => {
245-
const value = 'x'.repeat(64 * 1024)
246-
const handler = defineV2JsonRoute({
247-
contract,
248-
auth: v2ApiKeyAuth,
249-
operation,
250-
rateLimit: v2RateLimits.publicApi,
251-
errorPolicy: v2OrchestrationErrorPolicy,
252-
mapInput: ({ body }) => body,
253-
useCase: { operation, execute: async () => ({ value }) },
254-
entityTag: () => 'v1',
255-
present: (result) => ({ data: result }),
256-
encodeLargeBody: true,
257-
})
258-
const asking = request()
259-
if (acceptEncoding) asking.headers.set('accept-encoding', acceptEncoding)
260-
261-
const served = await handler(asking)
262-
263-
const bytes = Buffer.from(await served.arrayBuffer())
264-
const decoded =
265-
encoding === 'br'
266-
? brotliDecompressSync(bytes)
267-
: encoding === 'gzip'
268-
? gunzipSync(bytes)
269-
: bytes
270-
expect(JSON.parse(decoded.toString('utf8'))).toEqual({ data: { value } })
271-
expect(served.headers.get('Content-Encoding')).toBe(encoding)
272-
expect(served.headers.get('Vary')).toBe('Accept-Encoding')
273-
expect(served.headers.get('ETag')).toBe(encoding ? 'W/"v1"' : '"v1"')
274-
if (encoding) expect(bytes.byteLength).toBeLessThan(value.length / 10)
275-
}
276-
)
277-
278-
it('sends a body under the encoding threshold as it is', async () => {
279-
const handler = defineV2JsonRoute({
280-
contract,
281-
auth: v2ApiKeyAuth,
282-
operation,
283-
rateLimit: v2RateLimits.publicApi,
284-
errorPolicy: v2OrchestrationErrorPolicy,
285-
mapInput: ({ body }) => body,
286-
useCase: { operation, execute: async ({ input }) => input },
287-
present: (result) => ({ data: result }),
288-
encodeLargeBody: true,
289-
})
290-
const asking = request()
291-
asking.headers.set('accept-encoding', 'br, gzip')
292-
293-
const served = await handler(asking)
294-
295-
expect(served.headers.get('Content-Encoding')).toBeNull()
296-
await expect(served.json()).resolves.toEqual({ data: { value: 'ok' } })
297-
})
298-
299234
it('never serves a cached body to a request whose input presents it differently', async () => {
300235
const handler = defineV2JsonRoute({
301236
contract,

‎apps/sim/lib/api/server/routes/v2-json-route.ts‎

Lines changed: 4 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,6 @@ import {
1818
requireHeadAuthorizableUseCase,
1919
requireJsonRouteDefinition,
2020
} from '@/lib/api/server/routes/definition'
21-
import {
22-
encodeBody,
23-
MIN_ENCODED_BODY_BYTES,
24-
preferredEncoding,
25-
} from '@/lib/api/server/routes/response-compression'
2621
import type {
2722
JsonApiRouteContract,
2823
JsonNextRouteHandler,
@@ -602,13 +597,6 @@ interface V2JsonRouteOptions<C extends JsonApiRouteContract, O extends Applicati
602597
* `present` never sees the principal, so the body is a function of exactly that key.
603598
*/
604599
entityTag?(result: NoInfer<R>): string
605-
/**
606-
* Encodes a success body of at least {@link MIN_ENCODED_BODY_BYTES} as brotli or gzip when the
607-
* request's `Accept-Encoding` allows (`response-compression.ts`), for a route whose answers run
608-
* to megabytes. Every such answer says `Vary: Accept-Encoding`; an encoded one with an entity
609-
* tag carries it weak (`W/"…"`), as its bytes differ from the plain body's.
610-
*/
611-
encodeLargeBody?: true
612600
}
613601

614602
/**
@@ -642,11 +630,6 @@ export function defineV2JsonRoute<
642630
const validatedByTag = options.entityTag
643631
? new LRUCache<string, NonNullable<unknown>>({ max: VALIDATED_BODY_CACHE_MAX })
644632
: undefined
645-
/** Encoded bodies by the validated body's cache key and encoding, so a repeat skips encoding. */
646-
const encodedByTag =
647-
options.entityTag && options.encodeLargeBody
648-
? new LRUCache<string, Buffer>({ max: VALIDATED_BODY_CACHE_MAX })
649-
: undefined
650633
const wrapped = withRouteHandler<JsonRouteContext | undefined>(
651634
async (request, context) => {
652635
if (!methodMatchesContract(request.method, options.contract.method)) {
@@ -758,33 +741,11 @@ export function defineV2JsonRoute<
758741
)
759742
}
760743
await options.onSuccess?.({ principal: auth.principal, input, result })
761-
const cacheControl = entityTag ? REVALIDATE_CACHE_CONTROL : 'private, no-store'
762-
if (!options.encodeLargeBody) {
763-
return NextResponse.json(validatedBody, {
764-
status: responseStatus,
765-
headers: { 'Cache-Control': cacheControl, ...(entityTag ? { ETag: entityTag } : {}) },
766-
})
767-
}
768-
const json = Buffer.from(JSON.stringify(validatedBody))
769-
const encoding =
770-
json.byteLength >= MIN_ENCODED_BODY_BYTES
771-
? preferredEncoding(request.headers.get('accept-encoding'))
772-
: null
773-
const encodedKey = cacheKey && encoding ? `${encoding} ${cacheKey}` : undefined
774-
const encoded = encoding
775-
? ((encodedKey ? encodedByTag?.get(encodedKey) : undefined) ??
776-
(await encodeBody(json, encoding)))
777-
: null
778-
if (encodedKey && encoded) encodedByTag?.set(encodedKey, encoded)
779-
return new NextResponse(new Uint8Array(encoded ?? json), {
744+
return NextResponse.json(validatedBody, {
780745
status: responseStatus,
781-
headers: {
782-
'Content-Type': 'application/json',
783-
'Cache-Control': cacheControl,
784-
Vary: 'Accept-Encoding',
785-
...(encoding ? { 'Content-Encoding': encoding } : {}),
786-
...(entityTag ? { ETag: encoding ? `W/${entityTag}` : entityTag } : {}),
787-
},
746+
headers: entityTag
747+
? { 'Cache-Control': REVALIDATE_CACHE_CONTROL, ETag: entityTag }
748+
: { 'Cache-Control': 'private, no-store' },
788749
})
789750
} catch (error) {
790751
const response = options.errorPolicy.render(error)

‎packages/sim-cli/src/http/client.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -734,9 +734,9 @@ export class SimClient {
734734
? { authorization: `Bearer ${credential.oauth.accessToken}` }
735735
: {}),
736736
accept: 'application/json',
737-
// Large answers (the block catalog, a workspace's source) come encoded, a tenth of
738-
// their size; fetch decodes them. An in-process transport reads the handler's
739-
// response as it is, so it never asks for an encoding.
737+
// A server or proxy that compresses answers may send the large ones (the block catalog,
738+
// a workspace's source) as brotli or gzip; fetch decodes them. An in-process transport
739+
// reads the handler's response as it is, so it never asks for an encoding.
740740
...(this.profile.transport ? {} : { 'accept-encoding': ACCEPT_ENCODING }),
741741
...identityHeaders(),
742742
...(hasBody ? { 'content-type': 'application/json' } : {}),

0 commit comments

Comments
 (0)