@@ -26,7 +26,8 @@ import * as ServerSecretStore from "./auth/ServerSecretStore.ts";
2626import * as ServerConfig from "./config.ts" ;
2727import * as ServerEnvironment from "./environment/ServerEnvironment.ts" ;
2828import * as SqlitePersistence from "./persistence/Sqlite.ts" ;
29- import { subscribeAuthAccess , WS_RPC_SERVER_OPTIONS } from "./ws.ts" ;
29+ import { rpcInitialItems } from "./rpcInitialItems.ts" ;
30+ import { WS_RPC_SERVER_OPTIONS } from "./ws.ts" ;
3031
3132const group = WsRpcGroup . omit (
3233 ...[ ...WsRpcGroup . requests . keys ( ) ] . filter (
@@ -41,7 +42,9 @@ const layerAuth = EnvironmentAuth.layer.pipe(
4142 Layer . provideMerge ( ServerSecretStore . layer ) ,
4243 Layer . provideMerge ( SqlitePersistence . layerMemory ) ,
4344 Layer . provide ( ServerEnvironment . layerIdentity ) ,
44- Layer . provide ( ServerConfig . layerTest ( process . cwd ( ) , { prefix : "t3-auth-access-stream-test-" } ) ) ,
45+ Layer . provideMerge (
46+ ServerConfig . layerTest ( process . cwd ( ) , { prefix : "t3-auth-access-stream-test-" } ) ,
47+ ) ,
4548) ;
4649
4750const makeProtocol = Effect . gen ( function * ( ) {
@@ -115,18 +118,25 @@ it.layer(NodeServices.layer)("subscribeAuthAccess snapshot handoff", (it) => {
115118 const snapshotRead = yield * Deferred . make < void > ( ) ;
116119 const releaseSnapshot = yield * Deferred . make < void > ( ) ;
117120 const { responses, receive, protocol } = yield * makeProtocol ;
118- const snapshotAuth = EnvironmentAuth . EnvironmentAuth . of ( {
119- ...auth ,
120- listClientSessions : ( sessionId ) =>
121- auth . listClientSessions ( sessionId ) . pipe (
122- Effect . tap ( ( ) => Deferred . succeed ( snapshotRead , undefined ) ) ,
123- Effect . flatMap ( ( snapshot ) => Deferred . await ( releaseSnapshot ) . pipe ( Effect . as ( snapshot ) ) ) ,
124- ) ,
125- } ) ;
121+ const snapshotAuth = yield * EnvironmentAuth . make . pipe (
122+ Effect . provideService ( SessionStore . SessionStore , {
123+ ...sessions ,
124+ listActive : ( ) =>
125+ sessions . listActive ( ) . pipe (
126+ Effect . tap ( ( ) => Deferred . succeed ( snapshotRead , undefined ) ) ,
127+ Effect . flatMap ( ( snapshot ) =>
128+ Deferred . await ( releaseSnapshot ) . pipe ( Effect . as ( snapshot ) ) ,
129+ ) ,
130+ ) ,
131+ } ) ,
132+ ) ;
126133 const { write } = yield * startRpc (
127134 Stream . unwrap (
128- subscribeAuthAccess ( current . sessionId ) . pipe (
129- Effect . provideService ( EnvironmentAuth . EnvironmentAuth , snapshotAuth ) ,
135+ snapshotAuth . streamAccess ( current . sessionId ) . pipe (
136+ Effect . map ( ( { snapshot, changes } ) =>
137+ Stream . concat ( rpcInitialItems ( [ snapshot ] ) , changes ) ,
138+ ) ,
139+ Effect . mapError ( ( error ) => new AuthAccessStreamError ( { message : error . message } ) ) ,
130140 ) ,
131141 ) ,
132142 { responses, receive, protocol } ,
@@ -212,48 +222,47 @@ it.layer(NodeServices.layer)("subscribeAuthAccess snapshot handoff", (it) => {
212222 "releases both subscriptions on cancellation or failure during %s" ,
213223 ( milestone ) =>
214224 Effect . gen ( function * ( ) {
215- const auth = yield * EnvironmentAuth . EnvironmentAuth ;
216225 const pairingLinks = yield * PairingGrantStore . PairingGrantStore ;
217226 const sessions = yield * SessionStore . SessionStore ;
218227 const current = yield * sessions . issue ( { subject : "current-client" } ) ;
219228 const acquired = yield * Queue . unbounded < PubSub . Subscription < unknown > > ( ) ;
220229 const snapshotRead = yield * Deferred . make < void > ( ) ;
221230 const releaseSnapshot = yield * Deferred . make < void > ( ) ;
222- const snapshotAuth = EnvironmentAuth . EnvironmentAuth . of ( {
223- ...auth ,
224- listClientSessions : ( sessionId ) =>
225- auth . listClientSessions ( sessionId ) . pipe (
226- Effect . tap ( ( ) => Deferred . succeed ( snapshotRead , undefined ) ) ,
227- Effect . flatMap ( ( snapshot ) =>
228- Deferred . await ( releaseSnapshot ) . pipe (
229- Effect . andThen (
230- milestone === "snapshot failure"
231- ? Effect . fail (
232- new EnvironmentAuth . ServerAuthSessionsListError ( { cause : "test" } ) ,
233- )
234- : Effect . succeed ( snapshot ) ,
231+ const snapshotAuth = yield * EnvironmentAuth . make . pipe (
232+ Effect . provideService ( PairingGrantStore . PairingGrantStore , {
233+ ...pairingLinks ,
234+ subscribeChanges : pairingLinks . subscribeChanges . pipe (
235+ Effect . tap ( ( subscription ) => Queue . offer ( acquired , subscription ) ) ,
236+ ) ,
237+ } ) ,
238+ Effect . provideService ( SessionStore . SessionStore , {
239+ ...sessions ,
240+ subscribeChanges : sessions . subscribeChanges . pipe (
241+ Effect . tap ( ( subscription ) => Queue . offer ( acquired , subscription ) ) ,
242+ ) ,
243+ listActive : ( ) =>
244+ sessions . listActive ( ) . pipe (
245+ Effect . tap ( ( ) => Deferred . succeed ( snapshotRead , undefined ) ) ,
246+ Effect . flatMap ( ( snapshot ) =>
247+ Deferred . await ( releaseSnapshot ) . pipe (
248+ Effect . andThen (
249+ milestone === "snapshot failure"
250+ ? Effect . fail ( new SessionStore . ActiveSessionsListError ( { cause : "test" } ) )
251+ : Effect . succeed ( snapshot ) ,
252+ ) ,
235253 ) ,
236254 ) ,
237255 ) ,
238- ) ,
239- } ) ;
256+ } ) ,
257+ ) ;
240258 const { responses, receive, protocol } = yield * makeProtocol ;
241259 const { write } = yield * startRpc (
242260 Stream . unwrap (
243- subscribeAuthAccess ( current . sessionId ) . pipe (
244- Effect . provideService ( EnvironmentAuth . EnvironmentAuth , snapshotAuth ) ,
245- Effect . provideService ( PairingGrantStore . PairingGrantStore , {
246- ...pairingLinks ,
247- subscribeChanges : pairingLinks . subscribeChanges . pipe (
248- Effect . tap ( ( subscription ) => Queue . offer ( acquired , subscription ) ) ,
249- ) ,
250- } ) ,
251- Effect . provideService ( SessionStore . SessionStore , {
252- ...sessions ,
253- subscribeChanges : sessions . subscribeChanges . pipe (
254- Effect . tap ( ( subscription ) => Queue . offer ( acquired , subscription ) ) ,
255- ) ,
256- } ) ,
261+ snapshotAuth . streamAccess ( current . sessionId ) . pipe (
262+ Effect . map ( ( { snapshot, changes } ) =>
263+ Stream . concat ( rpcInitialItems ( [ snapshot ] ) , changes ) ,
264+ ) ,
265+ Effect . mapError ( ( error ) => new AuthAccessStreamError ( { message : error . message } ) ) ,
257266 ) ,
258267 ) ,
259268 { responses, receive, protocol } ,
0 commit comments