11// app/realtime/S2RealtimeStreams.ts
2+ import type { UnkeyCache } from "@internal/cache" ;
23import { StreamIngestor , StreamResponder , StreamResponseOptions } from "./types" ;
34import { Logger , LogLevel } from "@trigger.dev/core/logger" ;
45import { randomUUID } from "node:crypto" ;
@@ -17,6 +18,12 @@ export type S2RealtimeStreamsOptions = {
1718
1819 logger ?: Logger ;
1920 logLevel ?: LogLevel ;
21+
22+ accessTokenExpirationInMs ?: number ;
23+
24+ cache ?: UnkeyCache < {
25+ accessToken : string ;
26+ } > ;
2027} ;
2128
2229type S2IssueAccessTokenResponse = { access_token : string } ;
@@ -41,6 +48,12 @@ export class S2RealtimeStreams implements StreamResponder, StreamIngestor {
4148 private readonly logger : Logger ;
4249 private readonly level : LogLevel ;
4350
51+ private readonly accessTokenExpirationInMs : number ;
52+
53+ private readonly cache ?: UnkeyCache < {
54+ accessToken : string ;
55+ } > ;
56+
4457 constructor ( opts : S2RealtimeStreamsOptions ) {
4558 this . basin = opts . basin ;
4659 this . baseUrl = `https://${ this . basin } .b.aws.s2.dev/v1` ;
@@ -54,14 +67,13 @@ export class S2RealtimeStreams implements StreamResponder, StreamIngestor {
5467
5568 this . logger = opts . logger ?? new Logger ( "S2RealtimeStreams" , opts . logLevel ?? "info" ) ;
5669 this . level = opts . logLevel ?? "info" ;
57- }
5870
59- private toStreamName ( runId : string , streamId : string ) : string {
60- return ` ${ this . toStreamPrefix ( runId ) } ${ streamId } ` ;
71+ this . cache = opts . cache ;
72+ this . accessTokenExpirationInMs = opts . accessTokenExpirationInMs ?? 60_000 * 60 * 24 ; // 1 day
6173 }
6274
63- private toStreamPrefix ( runId : string ) : string {
64- return `${ this . streamPrefix } /runs/${ runId } /` ;
75+ private toStreamName ( runId : string , streamId : string ) : string {
76+ return `${ this . streamPrefix } /runs/${ runId } /${ streamId } ` ;
6577 }
6678
6779 async initializeStream (
@@ -70,11 +82,12 @@ export class S2RealtimeStreams implements StreamResponder, StreamIngestor {
7082 ) : Promise < { responseHeaders ?: Record < string , string > } > {
7183 const id = randomUUID ( ) ;
7284
73- const accessToken = await this . s2IssueAccessToken ( id , runId , streamId ) ;
85+ const accessToken = await this . getS2AccessToken ( id ) ;
7486
7587 return {
7688 responseHeaders : {
7789 "X-S2-Access-Token" : accessToken ,
90+ "X-S2-Stream-Name" : `/runs/${ runId } /${ streamId } ` ,
7891 "X-S2-Basin" : this . basin ,
7992 "X-S2-Flush-Interval-Ms" : this . flushIntervalMs . toString ( ) ,
8093 "X-S2-Max-Retries" : this . maxRetries . toString ( ) ,
@@ -153,7 +166,23 @@ export class S2RealtimeStreams implements StreamResponder, StreamIngestor {
153166 return ( await res . json ( ) ) as S2AppendAck ;
154167 }
155168
156- private async s2IssueAccessToken ( id : string , runId : string , streamId : string ) : Promise < string > {
169+ private async getS2AccessToken ( id : string ) : Promise < string > {
170+ if ( ! this . cache ) {
171+ return this . s2IssueAccessToken ( id ) ;
172+ }
173+
174+ const result = await this . cache . accessToken . swr ( this . streamPrefix , async ( ) => {
175+ return this . s2IssueAccessToken ( id ) ;
176+ } ) ;
177+
178+ if ( ! result . val ) {
179+ throw new Error ( "Failed to get S2 access token" ) ;
180+ }
181+
182+ return result . val ;
183+ }
184+
185+ private async s2IssueAccessToken ( id : string ) : Promise < string > {
157186 // POST /v1/access-tokens
158187 const res = await fetch ( `https://aws.s2.dev/v1/access-tokens` , {
159188 method : "POST" ,
@@ -169,10 +198,10 @@ export class S2RealtimeStreams implements StreamResponder, StreamIngestor {
169198 } ,
170199 ops : [ "append" , "create-stream" ] ,
171200 streams : {
172- prefix : this . toStreamPrefix ( runId ) ,
201+ prefix : this . streamPrefix ,
173202 } ,
174203 } ,
175- expires_at : new Date ( Date . now ( ) + 1000 * 60 * 60 * 24 ) . toISOString ( ) , // 1 day
204+ expires_at : new Date ( Date . now ( ) + this . accessTokenExpirationInMs ) . toISOString ( ) ,
176205 auto_prefix_streams : true ,
177206 } ) ,
178207 } ) ;
0 commit comments