@@ -37,18 +37,29 @@ export class RealtimeClient {
37
37
this . #registerCommands( ) ;
38
38
}
39
39
40
- async streamRun ( url : URL | string , environment : RealtimeEnvironment , runId : string ) {
41
- return this . #streamRunsWhere( url , environment , `id='${ runId } '` ) ;
40
+ async streamRun (
41
+ url : URL | string ,
42
+ environment : RealtimeEnvironment ,
43
+ runId : string ,
44
+ clientVersion ?: string
45
+ ) {
46
+ return this . #streamRunsWhere( url , environment , `id='${ runId } '` , clientVersion ) ;
42
47
}
43
48
44
- async streamBatch ( url : URL | string , environment : RealtimeEnvironment , batchId : string ) {
45
- return this . #streamRunsWhere( url , environment , `"batchId"='${ batchId } '` ) ;
49
+ async streamBatch (
50
+ url : URL | string ,
51
+ environment : RealtimeEnvironment ,
52
+ batchId : string ,
53
+ clientVersion ?: string
54
+ ) {
55
+ return this . #streamRunsWhere( url , environment , `"batchId"='${ batchId } '` , clientVersion ) ;
46
56
}
47
57
48
58
async streamRuns (
49
59
url : URL | string ,
50
60
environment : RealtimeEnvironment ,
51
- params : RealtimeRunsParams
61
+ params : RealtimeRunsParams ,
62
+ clientVersion ?: string
52
63
) {
53
64
const whereClauses : string [ ] = [ `"runtimeEnvironmentId"='${ environment . id } '` ] ;
54
65
@@ -58,16 +69,21 @@ export class RealtimeClient {
58
69
59
70
const whereClause = whereClauses . join ( " AND " ) ;
60
71
61
- return this . #streamRunsWhere( url , environment , whereClause ) ;
72
+ return this . #streamRunsWhere( url , environment , whereClause , clientVersion ) ;
62
73
}
63
74
64
- async #streamRunsWhere( url : URL | string , environment : RealtimeEnvironment , whereClause : string ) {
65
- const electricUrl = this . #constructElectricUrl( url , whereClause ) ;
75
+ async #streamRunsWhere(
76
+ url : URL | string ,
77
+ environment : RealtimeEnvironment ,
78
+ whereClause : string ,
79
+ clientVersion ?: string
80
+ ) {
81
+ const electricUrl = this . #constructElectricUrl( url , whereClause , clientVersion ) ;
66
82
67
- return this . #performElectricRequest( electricUrl , environment ) ;
83
+ return this . #performElectricRequest( electricUrl , environment , clientVersion ) ;
68
84
}
69
85
70
- #constructElectricUrl( url : URL | string , whereClause : string ) : URL {
86
+ #constructElectricUrl( url : URL | string , whereClause : string , clientVersion ?: string ) : URL {
71
87
const $url = new URL ( url . toString ( ) ) ;
72
88
73
89
const electricUrl = new URL ( `${ this . options . electricOrigin } /v1/shape` ) ;
@@ -77,36 +93,42 @@ export class RealtimeClient {
77
93
electricUrl . searchParams . set ( key , value ) ;
78
94
} ) ;
79
95
80
- // const electricParams = ["shape_id", "live", "offset", "columns", "cursor"];
81
-
82
- // electricParams.forEach((param) => {
83
- // if ($url.searchParams.has(param) && $url.searchParams.get(param)) {
84
- // electricUrl.searchParams.set(param, $url.searchParams.get(param)!);
85
- // }
86
- // });
87
-
88
96
electricUrl . searchParams . set ( "where" , whereClause ) ;
89
97
electricUrl . searchParams . set ( "table" , 'public."TaskRun"' ) ;
90
98
99
+ if ( ! clientVersion ) {
100
+ // If the client version is not provided, that means we're using an older client
101
+ // This means the client will be sending shape_id instead of handle
102
+ electricUrl . searchParams . set ( "handle" , electricUrl . searchParams . get ( "shape_id" ) ?? "" ) ;
103
+ }
104
+
91
105
return electricUrl ;
92
106
}
93
107
94
- async #performElectricRequest( url : URL , environment : RealtimeEnvironment ) {
108
+ async #performElectricRequest(
109
+ url : URL ,
110
+ environment : RealtimeEnvironment ,
111
+ clientVersion ?: string
112
+ ) {
95
113
const shapeId = extractShapeId ( url ) ;
96
114
97
115
logger . debug ( "[realtimeClient] request" , {
98
116
url : url . toString ( ) ,
99
117
} ) ;
100
118
119
+ const rewriteResponseHeaders : Record < string , string > = clientVersion
120
+ ? { }
121
+ : { "electric-handle" : "electric-shape-id" , "electric-offset" : "electric-chunk-last-offset" } ;
122
+
101
123
if ( ! shapeId ) {
102
124
// If the shapeId is not present, we're just getting the initial value
103
- return longPollingFetch ( url . toString ( ) ) ;
125
+ return longPollingFetch ( url . toString ( ) , { } , rewriteResponseHeaders ) ;
104
126
}
105
127
106
128
const isLive = isLiveRequestUrl ( url ) ;
107
129
108
130
if ( ! isLive ) {
109
- return longPollingFetch ( url . toString ( ) ) ;
131
+ return longPollingFetch ( url . toString ( ) , { } , rewriteResponseHeaders ) ;
110
132
}
111
133
112
134
const requestId = randomUUID ( ) ;
@@ -148,7 +170,7 @@ export class RealtimeClient {
148
170
149
171
try {
150
172
// ... (rest of your existing code for the long polling request)
151
- const response = await longPollingFetch ( url . toString ( ) ) ;
173
+ const response = await longPollingFetch ( url . toString ( ) , { } , rewriteResponseHeaders ) ;
152
174
153
175
// Decrement the counter after the long polling request is complete
154
176
await this . #decrementConcurrency( environment . id , requestId ) ;
0 commit comments