1
- import { ErrorCode , errors , logger , router , schema } from '@powersync/lib-services-framework' ;
2
- import { RequestParameters } from '@powersync/service-sync-rules' ;
3
- import { Readable } from 'stream' ;
1
+ import { ErrorCode , errors , router , schema } from '@powersync/lib-services-framework' ;
4
2
import Negotiator from 'negotiator' ;
3
+ import { Readable } from 'stream' ;
5
4
6
5
import * as sync from '../../sync/sync-index.js' ;
7
6
import * as util from '../../util/util-index.js' ;
@@ -10,6 +9,7 @@ import { authUser } from '../auth.js';
10
9
import { routeDefinition } from '../router.js' ;
11
10
12
11
import { APIMetric } from '@powersync/service-types' ;
12
+ import { maybeCompressResponseStream } from '../compression.js' ;
13
13
14
14
export enum SyncRoutes {
15
15
STREAM = '/sync/stream'
@@ -31,10 +31,10 @@ export const syncStreamed = routeDefinition({
31
31
const userAgent = headers [ 'x-user-agent' ] ?? headers [ 'user-agent' ] ;
32
32
const clientId = payload . params . client_id ;
33
33
const streamStart = Date . now ( ) ;
34
+ const negotiator = new Negotiator ( payload . request ) ;
34
35
// This falls back to JSON unless there's preference for the bson-stream in the Accept header.
35
36
const useBson =
36
- payload . request . headers . accept &&
37
- new Negotiator ( payload . request ) . mediaType ( supportedContentTypes ) == concatenatedBsonContentType ;
37
+ payload . request . headers . accept && negotiator . mediaType ( supportedContentTypes ) == concatenatedBsonContentType ;
38
38
39
39
logger . defaultMeta = {
40
40
...logger . defaultMeta ,
@@ -80,10 +80,11 @@ export const syncStreamed = routeDefinition({
80
80
} ) ;
81
81
82
82
const byteContents = useBson ? sync . bsonLines ( syncLines ) : sync . ndjson ( syncLines ) ;
83
- const stream = Readable . from ( sync . transformToBytesTracked ( byteContents , tracker ) , {
83
+ const plainStream = Readable . from ( sync . transformToBytesTracked ( byteContents , tracker ) , {
84
84
objectMode : false ,
85
85
highWaterMark : 16 * 1024
86
86
} ) ;
87
+ const { stream, encodingHeaders } = maybeCompressResponseStream ( negotiator , plainStream ) ;
87
88
88
89
// Best effort guess on why the stream was closed.
89
90
// We use the `??=` operator everywhere, so that we catch the first relevant
@@ -118,7 +119,8 @@ export const syncStreamed = routeDefinition({
118
119
return new router . RouterResponse ( {
119
120
status : 200 ,
120
121
headers : {
121
- 'Content-Type' : useBson ? concatenatedBsonContentType : ndJsonContentType
122
+ 'Content-Type' : useBson ? concatenatedBsonContentType : ndJsonContentType ,
123
+ ...encodingHeaders
122
124
} ,
123
125
data : stream ,
124
126
afterSend : async ( details ) => {
0 commit comments