Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
options,
transport,
dependencies = [],
messagingListener,
} = config;
const serverEventSubject = new Subject();
const transportLayer = provideTransportLayer(transport, options);
const boundTransportLayer = bindTo(TransportLayerToken)(() => transportLayer);
const boundServerEvents = bindTo(ServerEventsToken)(() => serverEventSubject);
const context = registerAll([ boundTransportLayer, boundServerEvents, ...dependencies ])(createContext());
const listenerWithContext = messagingListener(context);
const serverEvent$ = serverEventSubject.asObservable().pipe(takeWhile(e => !isCloseEvent(e)));
const ctx = createEffectContext({ ask: lookup(context), client: undefined });
const combinedEvents = event$ ? combineEffects(statusLogger$, event$) : statusLogger$;
combinedEvents(serverEvent$, ctx).subscribe();
return listenerWithContext.listen;
};
const handleConnection = (
conn: TransportLayerConnection,
serverEventsSubject: Subject,
ask: ContextProvider,
) => {
let effectsSub: Subscription;
const errorSubject = new Subject();
const combinedEffects = combineEffects(...effects);
const combinedMiddlewares = combineMiddlewares(inputLogger$, ...middlewares);
const ctx = createEffectContext({ ask, client: conn });
const toUnhandledErrorEvent = ({ name, message }: Error): Observable =>
of({ type: 'UNHANDLED_ERROR', error: { name, message } });
const decode = (msg: TransportMessage): TransportMessage => ({
...msg,
data: { ...msgTransformer.decode(msg.data), raw: msg },
});
const processMiddlewares: ProcessOperator = flow(
publish(msg$ => zip(
combinedMiddlewares(msg$.pipe(map(m => m.data)), ctx).pipe(
catchError(toUnhandledErrorEvent),
),
const logger = provideLogger(ctx.ask);
const transportLayer = useContext(TransportLayerToken)(ctx.ask);
return event$.pipe(
matchEvent(ServerEvent.error),
map(event => event.payload),
tap(({ error }) => logger({
tag: 'ERROR',
message: `${error.name}, ${error.message}`,
level: LoggerLevel.ERROR,
channel: transportLayer.config.channel,
}))
);
};
export const statusLogger$ = combineEffects(
connect$,
disconnect$,
error$,
);
export const webSocketListener = (config: WebSocketListenerConfig = {}) => {
const {
error$,
effects = [],
middlewares = [],
eventTransformer,
connection$ = (req$: Observable) => req$,
output$ = (out$: Observable) => out$,
} = config;
const combinedMiddlewares = combineMiddlewares(...middlewares);
const combinedEffects = combineEffects(...effects);
const providedError$ = provideErrorEffect(error$, eventTransformer);
const providedTransformer: EventTransformer = eventTransformer || jsonTransformer;
const handleMessage: HandleMessage = (client, ask) => {
const eventSubject$ = new Subject();
const incomingEventSubject$ = new Subject();
const defaultMetadata = createEffectMetadata({ ask });
const decodedEvent$ = incomingEventSubject$.pipe(map(providedTransformer.decode));
const middlewares$ = combinedMiddlewares(decodedEvent$, client, defaultMetadata);
const effects$ = combinedEffects(eventSubject$, client, defaultMetadata);
const effectsOutput$ = output$(effects$, client, defaultMetadata);
const subscribeMiddlewares = (input$: Observable) =>
input$.subscribe(
event => eventSubject$.next(event),
error => handleEffectsError(defaultMetadata, client, providedError$)(error),
);
const event2$: MsgEffect = event$ =>
event$.pipe(
matchEvent(event2.type),
);
const output$: MsgOutputEffect = event$ =>
event$.pipe(
tap(({ event }) => outputSubject.next(event)),
map(({ event }) => event),
);
const options = createOptions({ expectAck: false });
const client = await runClient(Transport.AMQP, options);
const server = await runServer(Transport.AMQP, options)(combineEffects(event1$, event2$), output$);
await client.emitMessage(options.queue, createMessage(event1));
await client.emitMessage(options.queue, createMessage(event2));
});
});