@@ -10,6 +10,12 @@ export type S2RealtimeStreamsOptions = {
1010 accessToken : string ; // "Bearer" token issued in S2 console
1111 streamPrefix ?: string ; // defaults to ""
1212
13+ // Custom endpoint for s2-lite (self-hosted)
14+ endpoint ?: string ; // e.g., "http://localhost:4566/v1"
15+
16+ // Skip access token issuance (s2-lite doesn't support /access-tokens)
17+ skipAccessTokens ?: boolean ;
18+
1319 // Read behavior
1420 s2WaitSeconds ?: number ;
1521
@@ -37,8 +43,11 @@ type S2AppendAck = {
3743export class S2RealtimeStreams implements StreamResponder , StreamIngestor {
3844 private readonly basin : string ;
3945 private readonly baseUrl : string ;
46+ private readonly accountUrl : string ;
47+ private readonly endpoint ?: string ;
4048 private readonly token : string ;
4149 private readonly streamPrefix : string ;
50+ private readonly skipAccessTokens : boolean ;
4251
4352 private readonly s2WaitSeconds : number ;
4453
@@ -56,9 +65,12 @@ export class S2RealtimeStreams implements StreamResponder, StreamIngestor {
5665
5766 constructor ( opts : S2RealtimeStreamsOptions ) {
5867 this . basin = opts . basin ;
59- this . baseUrl = `https://${ this . basin } .b.aws.s2.dev/v1` ;
68+ this . baseUrl = opts . endpoint ?? `https://${ this . basin } .b.aws.s2.dev/v1` ;
69+ this . accountUrl = opts . endpoint ?? `https://aws.s2.dev/v1` ;
70+ this . endpoint = opts . endpoint ;
6071 this . token = opts . accessToken ;
6172 this . streamPrefix = opts . streamPrefix ?? "" ;
73+ this . skipAccessTokens = opts . skipAccessTokens ?? false ;
6274
6375 this . s2WaitSeconds = opts . s2WaitSeconds ?? 60 ;
6476
@@ -80,17 +92,20 @@ export class S2RealtimeStreams implements StreamResponder, StreamIngestor {
8092 runId : string ,
8193 streamId : string
8294 ) : Promise < { responseHeaders ?: Record < string , string > } > {
83- const id = randomUUID ( ) ;
84-
85- const accessToken = await this . getS2AccessToken ( id ) ;
95+ const accessToken = this . skipAccessTokens
96+ ? this . token
97+ : await this . getS2AccessToken ( randomUUID ( ) ) ;
8698
8799 return {
88100 responseHeaders : {
89101 "X-S2-Access-Token" : accessToken ,
90- "X-S2-Stream-Name" : `/runs/${ runId } /${ streamId } ` ,
102+ "X-S2-Stream-Name" : this . skipAccessTokens
103+ ? this . toStreamName ( runId , streamId )
104+ : `/runs/${ runId } /${ streamId } ` ,
91105 "X-S2-Basin" : this . basin ,
92106 "X-S2-Flush-Interval-Ms" : this . flushIntervalMs . toString ( ) ,
93107 "X-S2-Max-Retries" : this . maxRetries . toString ( ) ,
108+ ...( this . endpoint ? { "X-S2-Endpoint" : this . endpoint } : { } ) ,
94109 } ,
95110 } ;
96111 }
@@ -142,6 +157,7 @@ export class S2RealtimeStreams implements StreamResponder, StreamIngestor {
142157 Authorization : `Bearer ${ this . token } ` ,
143158 Accept : "text/event-stream" ,
144159 "S2-Format" : "raw" ,
160+ "S2-Basin" : this . basin ,
145161 } ,
146162 }
147163 ) ;
@@ -236,7 +252,8 @@ export class S2RealtimeStreams implements StreamResponder, StreamIngestor {
236252 headers : {
237253 Authorization : `Bearer ${ this . token } ` ,
238254 "Content-Type" : "application/json" ,
239- "S2-Format" : "raw" , // UTF-8 JSON encoding (no base64 overhead) when your data is text. :contentReference[oaicite:8]{index=8}
255+ "S2-Format" : "raw" ,
256+ "S2-Basin" : this . basin ,
240257 } ,
241258 body : JSON . stringify ( body ) ,
242259 } ) ;
@@ -265,7 +282,7 @@ export class S2RealtimeStreams implements StreamResponder, StreamIngestor {
265282
266283 private async s2IssueAccessToken ( id : string ) : Promise < string > {
267284 // POST /v1/access-tokens
268- const res = await fetch ( `https://aws.s2.dev/v1 /access-tokens` , {
285+ const res = await fetch ( `${ this . accountUrl } /access-tokens` , {
269286 method : "POST" ,
270287 headers : {
271288 Authorization : `Bearer ${ this . token } ` ,
@@ -316,6 +333,7 @@ export class S2RealtimeStreams implements StreamResponder, StreamIngestor {
316333 Authorization : `Bearer ${ this . token } ` ,
317334 Accept : "text/event-stream" ,
318335 "S2-Format" : "raw" ,
336+ "S2-Basin" : this . basin ,
319337 } ,
320338 signal : opts . signal ,
321339 } ) ;
0 commit comments