prefix
stringlengths
82
32.6k
middle
stringlengths
5
470
suffix
stringlengths
0
81.2k
file_path
stringlengths
6
168
repo_name
stringlengths
16
77
context
listlengths
5
5
lang
stringclasses
4 values
ground_truth
stringlengths
5
470
import Nostr from "nostr-typedef"; import { BehaviorSubject, Observable, type OperatorFunction } from "rxjs"; import { LazyFilter, ReqPacket } from "./packet.js"; import type { Override } from "./util.js"; /** * The RxReq interface that is provided for RxNostr (**not for users**). */ export interface RxReq<S extends RxReqStrategy = RxReqStrategy> { /** @internal User should not use this directly.The RxReq strategy. It is read-only and must not change. */ get strategy(): S; /** @internal User should not use this directly. Used to construct subId. */ get rxReqId(): string; /** @internal User should not use this directly. Get an Observable of ReqPacket. */ getReqObservable(): Observable<ReqPacket>; } /** * REQ strategy. * * See comments on `createRxForwardReq()`, `createRxBackwardReq()` and `createRxOneshotReq() */ export type RxReqStrategy = "forward" | "backward" | "oneshot"; /** * The RxReq interface that is provided for users (not for RxNostr). */ export interface RxReqController { /** Start new REQ or stop REQ on the RxNostr with witch the RxReq is associated. */ emit(filters: LazyFilter | LazyFilter[] | null): void; /** * Returns itself overriding only `getReqObservable()`. * It is useful for throttling and other control purposes. */ pipe(): RxReq; pipe(op1: OperatorFunction<ReqPacket, ReqPacket>): RxReq; pipe<A>( op1: OperatorFunction<ReqPacket, A>, op2: OperatorFunction<A, ReqPacket> ): RxReq; pipe<A, B>( op1: OperatorFunction<ReqPacket, A>, op2: OperatorFunction<A, B>, op3: OperatorFunction<B, ReqPacket> ): RxReq; pipe<A, B, C>( op1: OperatorFunction<ReqPacket, A>, op2: OperatorFunction<A, B>, op3: OperatorFunction<B, C>, op4: OperatorFunction<C, ReqPacket> ): RxReq; pipe<A, B, C, D>( op1: OperatorFunction<ReqPacket, A>, op2: OperatorFunction<A, B>, op3: OperatorFunction<B, C>, op4: OperatorFunction<C, D>, op5: OperatorFunction<D, ReqPacket> ): RxReq; } abstract class RxReqBase implements RxReq { protected filters$ = new BehaviorSubject<ReqPacket>(null); private _rxReqId: string; abstract get strategy(): RxReqStrategy; get rxReqId() { return this._rxReqId; } constructor(rxReqId?: string) { this._rxReqId = rxReqId ?? getRandomDigitsString(); } getReqObservable(): Observable<ReqPacket> { return this.filters$.asObservable(); } emit(filters: LazyFilter | LazyFilter[] | null) { const normalized = normalizeFilters(filters); if (normalized) { this.filters$.next(normalized); } else { this.filters$.next(null); } } pipe(): RxReq; pipe(op1: OperatorFunction<ReqPacket, ReqPacket>): RxReq; pipe<A>( op1: OperatorFunction<ReqPacket, A>, op2: OperatorFunction<A, ReqPacket> ): RxReq; pipe<A, B>( op1: OperatorFunction<ReqPacket, A>, op2: OperatorFunction<A, B>, op3: OperatorFunction<B, ReqPacket> ): RxReq; pipe<A, B, C>( op1: OperatorFunction<ReqPacket, A>, op2: OperatorFunction<A, B>, op3: OperatorFunction<B, C>, op4: OperatorFunction<C, ReqPacket> ): RxReq; pipe<A, B, C, D>( op1: OperatorFunction<ReqPacket, A>, op2: OperatorFunction<A, B>, op3: OperatorFunction<B, C>, op4: OperatorFunction<C, D>, op5: OperatorFunction<D, ReqPacket> ): RxReq; // eslint-disable-next-line @typescript-eslint/no-explicit-any pipe(...operators: OperatorFunction<any, any>[]): RxReq { const rxReqId = this.rxReqId; const strategy = this.strategy; return { ...this, get rxReqId() { return rxReqId; }, get strategy() { return strategy; }, getReqObservable: () => this.getReqObservable().pipe(...(operators as [])), }; } } /** * Create a RxReq instance based on the backward strategy. * It is useful if you want to retrieve past events that have already been published. * * In backward strategy: * - All REQs have different subIds. * - All REQ-subscriptions keep alive until timeout or getting EOSE. * - In most cases, you should specify `until` or `limit` for filters. * * For more information, see [document](https://penpenpng.github.io/rx-nostr/docs/req-strategy.html#backward-strategy). */ export function createRxBackwardReq( subIdBase?: string ): RxReq<"backward"> & RxReqController { return new RxBackwardReq(subIdBase); } class RxBackwardReq extends RxReqBase implements RxReqController { constructor(rxReqId?: string) { super(rxReqId); } override get strategy(): "backward" { return "backward"; } } /** * Create a RxReq instance based on the forward strategy. * It is useful if you want to listen future events. * * In forward strategy: * - All REQs have the same subId. * - When a new REQ is issued, the old REQ is overwritten and terminated immediately. * The latest REQ keeps alive until it is overwritten or explicitly terminated. * - In most cases, you should not specify `limit` for filters. * * For more information, see [document](https://penpenpng.github.io/rx-nostr/docs/req-strategy.html#forward-strategy). */ export function createRxForwardReq( subId?: string ): RxReq<"forward"> & RxReqController { return new RxForwardReq(subId); } class RxForwardReq extends RxReqBase implements RxReqController { constructor(rxReqId?: string) { super(rxReqId); } override get strategy(): "forward" { return "forward"; } } /** * Create a RxReq instance based on the oneshot strategy. * It is almost the same as backward strategy, however can publish only one REQ * and the Observable completes on EOSE. * * For more information, see [document](https://penpenpng.github.io/rx-nostr/docs/req-strategy.html#oneshot-strategy). */ export function createRxOneshotReq(req: { filters
: LazyFilter | LazyFilter[];
subId?: string; }): RxReq<"oneshot"> { return new RxOneshotReq(req); } class RxOneshotReq extends RxReqBase { constructor(req: { filters: LazyFilter | LazyFilter[]; subId?: string }) { super(req?.subId); this.emit(req.filters); } override get strategy(): "oneshot" { return "oneshot"; } } export interface Mixin<R, T> { (): ThisType<R> & T; } /** NOTE: unstable feature */ export function mixin<R extends object, T extends object>( def: () => ThisType<R> & T ): Mixin<R, T> { return def; } /** NOTE: unstable feature */ export function extend<B extends R, R extends object, T extends object>( base: B, mixin: Mixin<R, T> ): Override<B, T> { return Object.assign(base, mixin()) as Override<B, T>; } function getRandomDigitsString() { return `${Math.floor(Math.random() * 1000000)}`; } function normalizeFilter(filter: LazyFilter): LazyFilter | null { const res: LazyFilter = {}; const isTagName = (s: string): s is Nostr.TagName => /^#[a-zA-Z]$/.test(s); for (const key of Object.keys(filter)) { if (key === "limit" && (filter[key] ?? -1) >= 0) { res[key] = filter[key]; continue; } if (key === "since" || key === "until") { const f = filter[key]; if (typeof f !== "number" || (f ?? -1) >= 0) { res[key] = f; continue; } } if ( (isTagName(key) || key === "ids" || key === "authors") && filter[key] !== undefined && (filter[key]?.length ?? -1) > 0 ) { res[key] = filter[key]; continue; } if ( key === "kinds" && filter[key] !== undefined && (filter[key]?.length ?? -1) > 0 ) { res[key] = filter[key]; continue; } if (key === "search" && filter[key] !== undefined) { res[key] = filter[key]; continue; } } const timeRangeIsValid = typeof res.since !== "number" || typeof res.until !== "number" || res.since <= res.until; if (!timeRangeIsValid) { return null; } if (Object.keys(res).length <= 0) { return null; } return res; } function normalizeFilters( filters: LazyFilter | LazyFilter[] | null ): LazyFilter[] | null { if (!filters) { return null; } const normalized = (Array.isArray(filters) ? filters : [filters]).flatMap( (e) => normalizeFilter(e) ?? [] ); return normalized.length > 0 ? normalized : null; }
src/req.ts
penpenpng-rx-nostr-7c52614
[ { "filename": "src/rx-nostr.ts", "retrieved_chunk": "/** Create a RxNostr object. This is the only way to create that. */\nexport function createRxNostr(options?: Partial<RxNostrOptions>): RxNostr {\n return new RxNostrImpl(options);\n}\nexport interface RxNostrOptions {\n /** Auto reconnection strategy. */\n retry: BackoffConfig;\n /**\n * The time in milliseconds to timeout when following the backward strategy.\n * The observable is terminated when the specified amount of time has elapsed", "score": 34.598425764139286 }, { "filename": "src/rx-nostr.ts", "retrieved_chunk": " const createConnectionStateObservable =\n this.createConnectionStateObservable.bind(this);\n const ensureReq = this.ensureReq.bind(this);\n const finalizeReq = this.finalizeReq.bind(this);\n const subId$ = rxReq.getReqObservable().pipe(\n filter((filters): filters is LazyFilter[] => filters !== null),\n strategy === \"oneshot\" ? first() : identity,\n attachSubId(),\n strategy === \"forward\" ? manageActiveForwardReq() : identity,\n tap((req) => {", "score": 32.6195294618043 }, { "filename": "src/__test__/subscription.test.ts", "retrieved_chunk": " wasClean: true,\n });\n rxNostr.send(faker.event());\n await expect(relay).toReceiveEVENT();\n });\n test(\"[oneshot] Receipt of EOSE terminates the Observable.\", async () => {\n const req = createRxOneshotReq({\n subId: \"sub\",\n filters: faker.filter(),\n });", "score": 29.525666085069354 }, { "filename": "src/__test__/extend.test.ts", "retrieved_chunk": "import { expect, test } from \"vitest\";\nimport { createRxBackwardReq, extend, mixin } from \"../index.js\";\ntest(\"Extend req.\", async () => {\n const addFoo = mixin<{ strategy: \"backward\" }, { foo: () => string }>(() => ({\n foo() {\n return this.strategy;\n },\n }));\n const req = extend(createRxBackwardReq(), addFoo);\n expect(req.strategy).toBe(\"backward\");", "score": 25.565405077238307 }, { "filename": "src/packet.ts", "retrieved_chunk": "import Nostr from \"nostr-typedef\";\n// Packet is data treated by rx-nostr Observables.\n/**\n * Packets flowing through the Observable stream sent from RxReq towards RxNostr.\n * When null is sent, the subscription is suspended.\n */\nexport type ReqPacket = LazyFilter[] | null;\n/**\n * Filter object, but allows parameters since/until to be function.\n * If so, values will be evaluated just before submission.", "score": 25.300385558488873 } ]
typescript
: LazyFilter | LazyFilter[];
import Nostr from "nostr-typedef"; import { catchError, EMPTY, filter, finalize, first, identity, map, merge, mergeAll, mergeMap, type MonoTypeOperatorFunction, Observable, of, type OperatorFunction, ReplaySubject, Subject, Subscription, take, takeUntil, tap, timeout, type Unsubscribable, } from "rxjs"; import { BackoffConfig, Connection } from "./connection.js"; import { getSignedEvent } from "./nostr/event.js"; import { fetchRelayInfo } from "./nostr/nip11.js"; import { completeOnTimeout } from "./operator.js"; import type { ConnectionState, ConnectionStatePacket, ErrorPacket, EventPacket, LazyFilter, LazyREQ, MessagePacket, OkPacket, } from "./packet.js"; import type { RxReq } from "./req.js"; import { defineDefaultOptions, normalizeRelayUrl } from "./util.js"; /** * The core object of rx-nostr, which holds a connection to relays * and manages subscriptions as directed by the RxReq object connected by `use()`. * Use `createRxNostr()` to get the object. */ export interface RxNostr { /** * Return a list of relays used by this object. * The relay URLs are normalised so may not match the URLs set. */ getRelays(): RelayConfig[]; /** * Set the list of relays. * If a REQ subscription already exists, the same REQ is issued for the newly added relay * and CLOSE is sent for the removed relay. */ switchRelays(config: AcceptableRelaysConfig): Promise<void>; /** Utility wrapper for `switchRelays()`. */ addRelay(relay: string | RelayConfig): Promise<void>; /** Utility wrapper for `switchRelays()`. */ removeRelay(url: string): Promise<void>; /** Return true if the given relay is set to rxNostr. */ hasRelay(url: string): boolean; /** Return true if the given relay allows to be written. */ canWriteRelay(url: string): boolean; /** Return true if the given relay allows to be read. */ canReadRelay(url: string): boolean; /** Fetch all relays' info based on [NIP-11](https://github.com/nostr-protocol/nips/blob/master/11.md) */ fetchAllRelaysInfo(): Promise<Record<string, Nostr.Nip11.RelayInfo | null>>; /** * Return a dictionary in which you can look up connection state. * * **NOTE**: Keys are **normalized** URL, so may be different from one you set. */ getAllRelayState(): Record<string, ConnectionState>; /** * Return connection state of the given relay. * Throw if unknown URL is given. */ getRelayState(url: string): ConnectionState; /** * Attempt to reconnect the WebSocket if its state is `error` or `rejected`. * If not, do nothing. */ reconnect(url: string): void; // TODO: document /** * Set or unset a pipe to be applied to all EventPackets. */ setGlobalEventPacketPipe( pipe: MonoTypeOperatorFunction<EventPacket> | null ): void; /** * Associate RxReq with RxNostr. * When the associated RxReq is manipulated, * the RxNostr issues a new REQ to all relays allowed to be read. * The method returns an Observable that issues EventPackets * when an EVENT is received that is subscribed by RxReq. * You can unsubscribe the Observable to CLOSE. */ use( rxReq: RxReq, options?: Partial<RxNostrUseOptions> ): Observable<EventPacket>; /** * Create an Observable that receives all events (EVENT) from all websocket connections. * * Nothing happens when this Observable is unsubscribed. * */ createAllEventObservable(): Observable<EventPacket>; /** * Create an Observable that receives all errors from all websocket connections. * Note that an Observable is terminated when it receives any error, * so this method is the only way to receive errors arising from multiplexed websocket connections * (It means that Observables returned by `use()` never throw error). * * Nothing happens when this Observable is unsubscribed. * */ createAllErrorObservable(): Observable<ErrorPacket>; /** * Create an Observable that receives all messages from all websocket connections. * * Nothing happens when this Observable is unsubscribed. * */ createAllMessageObservable(): Observable<MessagePacket>; /** * Create an Observable that receives changing of WebSocket connection state. * * Nothing happens when this Observable is unsubscribed. */ createConnectionStateObservable(): Observable<ConnectionStatePacket>; /** * Attempt to send events to all relays that are allowed to write. * The `seckey` option accepts both nsec format and hex format, * and if omitted NIP-07 will be automatically used. */ send( params: Nostr.EventParameters, options?: RxNostrSendOptions ): Observable<OkPacket>; /** * Release all resources held by the RxNostr object. * Any Observable resulting from this RxNostr will be in the completed state * and will never receive messages again. * RxReq used by this object is not affected; in other words, if the RxReq is used * by another RxNostr, its use is not prevented. */ dispose(): void; } /** Create a RxNostr object. This is the only way to create that. */ export function createRxNostr(options?: Partial<RxNostrOptions>): RxNostr { return new RxNostrImpl(options); } export interface RxNostrOptions { /** Auto reconnection strategy. */ retry: BackoffConfig; /** * The time in milliseconds to timeout when following the backward strategy. * The observable is terminated when the specified amount of time has elapsed * during which no new events are available. */ timeout: number; globalRelayConfig?: { disableAutoFetchNip11Limitations?: boolean; maxConcurrentReqsFallback?: number; }; } const makeRxNostrOptions = defineDefaultOptions<RxNostrOptions>({ retry: { strategy: "exponential", maxCount: 5, initialDelay: 1000, }, timeout: 10000, globalRelayConfig: undefined, }); export interface RxNostrUseOptions { scope?: string[]; } const makeRxNostrUseOptions = defineDefaultOptions<RxNostrUseOptions>({ scope: undefined, }); export interface RxNostrSendOptions { scope?: string[]; seckey?: string; } const makeRxNostrSendOptions = defineDefaultOptions<RxNostrSendOptions>({ scope: undefined, seckey: undefined, }); /** Config object specifying WebSocket behavior. */ export interface RelayConfig { /** WebSocket endpoint URL. */ url: string; /** If true, rxNostr can publish REQ and subscribe EVENTs. */ read: boolean; /** If true, rxNostr can send EVENTs. */ write: boolean; disableAutoFetchNip11Limitations?: boolean; } /** Parameter of `rxNostr.switchRelays()` */ export type AcceptableRelaysConfig = | (string | RelayConfig)[] | Nostr.Nip07.GetRelayResult; class RxNostrImpl implements RxNostr { private options: RxNostrOptions; private connections: Map<string, Connection> = new Map(); private ongoings: Map<string, OngoingReq> = new Map(); private messageIn$: Subject<MessagePacket> = new Subject(); private error$: Subject<ErrorPacket> = new Subject(); private status$: Subject<ConnectionStatePacket> = new Subject(); private globalEventPacketPipe: MonoTypeOperatorFunction<EventPacket> | null = null; private disposed = false; private get messageOut$() { return this.messageIn$.pipe( mergeMap((packet) => { const pipe = this.globalEventPacketPipe; if (!pipe) { return of(packet); } const message = packet.message; if (message[0] !== "EVENT") { return of(packet); } return of({ from: packet.from, subId: message[1], event: message[2], }).pipe( pipe, map( ({ from, subId, event }): MessagePacket => ({ from, message: ["EVENT", subId, event], }) ) ); }) ); } constructor(options?: Partial<RxNostrOptions>) { const opt = makeRxNostrOptions(options); this.options = { ...opt, }; } getRelays(): RelayConfig[] { return Array.from(this.connections.values()).map( ({ url, read, write }) => ({ url, read, write, }) ); } private createConnection({ url, read, write, disableAutoFetchNip11Limitations, }: RelayConfig): Connection { const connection = new Connection(url, { backoff: this.options.retry, read, write, disableAutoFetchNip11Limitations: disableAutoFetchNip11Limitations ?? this.options.globalRelayConfig?.disableAutoFetchNip11Limitations, maxConcurrentReqsFallback: this.options.globalRelayConfig?.maxConcurrentReqsFallback, }); connection.getConnectionStateObservable().subscribe((state) => { this.status$.next({ from: url, state, }); }); connection.getErrorObservable().
subscribe((reason) => {
this.error$.next({ from: url, reason }); }); connection .getMessageObservable() .pipe( catchError((reason: unknown) => { this.error$.next({ from: url, reason }); return EMPTY; }) ) .subscribe((v) => { this.messageIn$.next(v); }); return connection; } async switchRelays(config: AcceptableRelaysConfig): Promise<void> { const nextConns: Map<string, Connection> = new Map(); for (const { url, read, write } of normalizeRelaysConfig(config)) { // pop a connection if exists const prevConn = this.connections.get(url); this.connections.delete(url); if (prevConn) { prevConn.read = read; prevConn.write = write; nextConns.set(url, prevConn); } else { nextConns.set(url, this.createConnection({ url, read, write })); } } // connections that are no longer used for (const conn of this.connections.values()) { conn.dispose(); } const ensureConns: Promise<unknown>[] = []; for (const conn of nextConns.values()) { if (conn.read) { ensureConns.push(conn.start()); } else { conn.stop(); } } await Promise.all(ensureConns); this.connections = nextConns; // If disposed during switchRelay processing if (this.disposed) { for (const conn of this.connections.values()) { conn.dispose(); } return; } for (const { req, scope } of this.ongoings.values()) { this.ensureReq(req, { scope }); } // --- scoped untility pure functions --- function normalizeRelaysConfig( config: AcceptableRelaysConfig ): RelayConfig[] { if (Array.isArray(config)) { return config.map((urlOrConfig) => { const relay: RelayConfig = typeof urlOrConfig === "string" ? { url: urlOrConfig, read: true, write: true, } : urlOrConfig; relay.url = normalizeRelayUrl(relay.url); return relay; }); } else { return Object.entries(config).map(([url, flags]) => ({ url: normalizeRelayUrl(url), ...flags, })); } } } async addRelay(relay: string | RelayConfig): Promise<void> { await this.switchRelays([...this.getRelays(), relay]); } async removeRelay(url: string): Promise<void> { const u = normalizeRelayUrl(url); const currentRelays = this.getRelays(); const nextRelays = currentRelays.filter((relay) => relay.url !== u); if (currentRelays.length !== nextRelays.length) { await this.switchRelays(nextRelays); } } hasRelay(url: string): boolean { const u = normalizeRelayUrl(url); return this.getRelays().some((relay) => relay.url === u); } canWriteRelay(url: string): boolean { const u = normalizeRelayUrl(url); return this.getRelays().some((relay) => relay.url === u && relay.write); } canReadRelay(url: string): boolean { const u = normalizeRelayUrl(url); return this.getRelays().some((relay) => relay.url === u && relay.read); } async fetchAllRelaysInfo(): Promise< Record<string, Nostr.Nip11.RelayInfo | null> > { const entries = await Promise.all( Array.from(this.connections.keys()).map( async (url): Promise<[string, Nostr.Nip11.RelayInfo | null]> => [ url, await fetchRelayInfo(url).catch(() => null), ] ) ); return Object.fromEntries(entries); } getAllRelayState(): Record<string, ConnectionState> { return Object.fromEntries( Array.from(this.connections.values()).map((e) => [ e.url, this.getRelayState(e.url), ]) ); } getRelayState(url: string): ConnectionState { const conn = this.connections.get(normalizeRelayUrl(url)); if (!conn) { throw new Error("RelayConfig not found"); } // this.relays[url] may be set before this.relays[url].websocket is initialized return conn?.getConnectionState() ?? "not-started"; } reconnect(url: string): void { if (this.canReadRelay(url)) { this.connections.get(normalizeRelayUrl(url))?.start(); } } setGlobalEventPacketPipe(pipe: MonoTypeOperatorFunction<EventPacket> | null) { this.globalEventPacketPipe = pipe; } use( rxReq: RxReq, options?: Partial<RxNostrUseOptions> ): Observable<EventPacket> { const { scope: _scope } = makeRxNostrUseOptions(options); const scope = _scope?.map(normalizeRelayUrl); const TIMEOUT = this.options.timeout; const strategy = rxReq.strategy; const rxReqId = rxReq.rxReqId; const message$ = this.messageOut$; const ongoings = this.ongoings; const getAllRelayState = this.getAllRelayState.bind(this); const createConnectionStateObservable = this.createConnectionStateObservable.bind(this); const ensureReq = this.ensureReq.bind(this); const finalizeReq = this.finalizeReq.bind(this); const subId$ = rxReq.getReqObservable().pipe( filter((filters): filters is LazyFilter[] => filters !== null), strategy === "oneshot" ? first() : identity, attachSubId(), strategy === "forward" ? manageActiveForwardReq() : identity, tap((req) => { ensureReq(req, { overwrite: strategy === "forward", scope }); }), map(([, subId]) => subId) ); if (strategy === "forward") { const subId = makeSubId({ rxReqId, }); const resource: Unsubscribable[] = []; const subject = new Subject<EventPacket>(); resource.push(subject); return subject.pipe( tap({ subscribe: () => { resource.push(subId$.subscribe()); resource.push( message$ .pipe(filterBySubId(subId), pickEvents()) .subscribe((v) => { subject.next(v); }) ); }, finalize: () => { for (const r of resource) { r.unsubscribe(); } finalizeReq({ subId }); }, }) ); } else { return subId$.pipe(map(createEoseManagedEventObservable), mergeAll()); } function attachSubId(): OperatorFunction<LazyFilter[], LazyREQ> { const makeId = (index?: number) => makeSubId({ rxReqId, index }); switch (strategy) { case "backward": return map((filters, index) => ["REQ", makeId(index), ...filters]); case "forward": case "oneshot": return map((filters) => ["REQ", makeId(), ...filters]); } } function manageActiveForwardReq(): MonoTypeOperatorFunction<LazyREQ> { const recordActiveReq = (req: LazyREQ) => { const subId = req[1]; ongoings.set(subId, { req, scope, }); }; const forgetActiveReq = (subId: string) => { ongoings.delete(subId); }; return tap({ next: (req: LazyREQ) => { recordActiveReq(req); }, finalize: () => { forgetActiveReq( makeSubId({ rxReqId, }) ); }, }); } function createEoseManagedEventObservable( subId: string ): Observable<EventPacket> { const eose$ = new Subject<void>(); const complete$ = new Subject<void>(); const eoseRelays = new Set<string>(); const manageCompletion = merge( eose$, createConnectionStateObservable() ).subscribe(() => { const status = getAllRelayState(); const shouldComplete = Object.entries(status).every( ([url, state]) => (scope && !scope.includes(url)) || state === "error" || state === "terminated" || (state === "ongoing" && eoseRelays.has(url)) ); if (shouldComplete) { complete$.next(); } }); return message$.pipe( takeUntil(complete$), completeOnTimeout(TIMEOUT), filterBySubId(subId), filter((e) => !eoseRelays.has(e.from)), tap((e) => { if (e.message[0] === "EOSE") { eoseRelays.add(e.from); finalizeReq({ subId, url: e.from }); eose$.next(); } }), pickEvents(), finalize(() => { finalizeReq({ subId }); complete$.unsubscribe(); eose$.unsubscribe(); manageCompletion.unsubscribe(); }) ); } function filterBySubId( subId: string ): MonoTypeOperatorFunction<MessagePacket> { return filter( (packet) => (packet.message[0] === "EVENT" || packet.message[0] === "EOSE") && packet.message[1] === subId ); } function pickEvents(): OperatorFunction<MessagePacket, EventPacket> { return mergeMap(({ from, message }) => message[0] === "EVENT" ? of({ from, subId: message[1], event: message[2] }) : EMPTY ); } } createAllEventObservable(): Observable<EventPacket> { return this.messageOut$.pipe( mergeMap(({ from, message }) => message[0] === "EVENT" ? of({ from, subId: message[1], event: message[2] }) : EMPTY ) ); } createAllErrorObservable(): Observable<ErrorPacket> { return this.error$.asObservable(); } createAllMessageObservable(): Observable<MessagePacket> { return this.messageOut$; } createConnectionStateObservable(): Observable<ConnectionStatePacket> { return this.status$.asObservable(); } send( params: Nostr.EventParameters, options?: RxNostrSendOptions ): Observable<OkPacket> { const { seckey, scope: _scope } = makeRxNostrSendOptions(options); const scope = _scope?.map(normalizeRelayUrl); const writableConns = Array.from(this.connections.values()).filter( (conn) => (!scope || scope.includes(conn.url)) && conn.write ); const subject = new ReplaySubject<OkPacket>(writableConns.length); let subscription: Subscription | null = null; getSignedEvent(params, seckey).then((event) => { if (!subject.closed) { subscription = this.createAllMessageObservable().subscribe( ({ from, message }) => { if (message[0] !== "OK") { return; } subject.next({ from, id: event.id, ok: message[2], }); } ); } for (const conn of writableConns) { conn.sendEVENT(["EVENT", event]); } }); return subject.pipe( take(writableConns.length), timeout(30 * 1000), finalize(() => { subject.complete(); subject.unsubscribe(); subscription?.unsubscribe(); }) ); } dispose(): void { this.disposed = true; this.messageIn$.complete(); this.error$.complete(); for (const conn of this.connections.values()) { conn.dispose(); } } private ensureReq( req: LazyREQ, options?: { scope?: string[] | null; overwrite?: boolean } ) { const scope = options?.scope; for (const url of this.connections.keys()) { const conn = this.connections.get(url); if (!conn || !conn.read || (scope && !scope.includes(url))) { continue; } conn.ensureReq(req, { overwrite: options?.overwrite }); } } private finalizeReq(params: { subId: string; url?: string }) { const { subId, url } = params; if (subId === undefined && url === undefined) { throw new Error(); } if (url) { const conn = this.connections.get(url); conn?.finalizeReq(subId); } else { for (const conn of this.connections.values()) { conn?.finalizeReq(subId); } } } } interface OngoingReq { req: LazyREQ; scope?: string[]; } function makeSubId(params: { rxReqId: string; index?: number }): string { return `${params.rxReqId}:${params.index ?? 0}`; }
src/rx-nostr.ts
penpenpng-rx-nostr-7c52614
[ { "filename": "src/packet.ts", "retrieved_chunk": "/**\n * Packets emitted when WebSocket connection state is changed.\n */\nexport interface ConnectionStatePacket {\n from: string;\n state: ConnectionState;\n}\n/**\n * WebSocket connection state.\n *", "score": 28.92005812025353 }, { "filename": "src/connection.ts", "retrieved_chunk": " }\n private setConnectionState(state: ConnectionState) {\n if (this.connectionState === \"terminated\") {\n return;\n }\n this.connectionState = state;\n this.connectionState$.next(state);\n }\n private async fetchServerLimitationsIfNeeded() {\n if (", "score": 24.684896900905017 }, { "filename": "src/connection.ts", "retrieved_chunk": " }\n get maxConcurrentReqs(): number | null {\n return (\n this.serverLimitations?.max_subscriptions ??\n this.config.maxConcurrentReqsFallback ??\n null\n );\n }\n constructor(public url: string, private config: ConnectionConfig) {\n this.connectionState$.next(\"not-started\");", "score": 15.895636151510638 }, { "filename": "src/connection.ts", "retrieved_chunk": " }\n getErrorObservable() {\n return this.error$.asObservable();\n }\n ensureReq(req: LazyREQ, options?: { overwrite?: boolean }) {\n const subId = req[1];\n if (this.connectionState === \"terminated\") {\n return;\n }\n if (!this.read) {", "score": 14.651608735641812 }, { "filename": "src/connection.ts", "retrieved_chunk": " };\n const onmessage = ({ data }: MessageEvent) => {\n if (this.connectionState === \"terminated\") {\n return;\n }\n try {\n this.message$.next({ from: this.url, message: JSON.parse(data) });\n } catch (err) {\n this.error$.next(err);\n }", "score": 14.311430222900524 } ]
typescript
subscribe((reason) => {
import Nostr from "nostr-typedef"; import { catchError, EMPTY, filter, finalize, first, identity, map, merge, mergeAll, mergeMap, type MonoTypeOperatorFunction, Observable, of, type OperatorFunction, ReplaySubject, Subject, Subscription, take, takeUntil, tap, timeout, type Unsubscribable, } from "rxjs"; import { BackoffConfig, Connection } from "./connection.js"; import { getSignedEvent } from "./nostr/event.js"; import { fetchRelayInfo } from "./nostr/nip11.js"; import { completeOnTimeout } from "./operator.js"; import type { ConnectionState, ConnectionStatePacket, ErrorPacket, EventPacket, LazyFilter, LazyREQ, MessagePacket, OkPacket, } from "./packet.js"; import type { RxReq } from "./req.js"; import { defineDefaultOptions, normalizeRelayUrl } from "./util.js"; /** * The core object of rx-nostr, which holds a connection to relays * and manages subscriptions as directed by the RxReq object connected by `use()`. * Use `createRxNostr()` to get the object. */ export interface RxNostr { /** * Return a list of relays used by this object. * The relay URLs are normalised so may not match the URLs set. */ getRelays(): RelayConfig[]; /** * Set the list of relays. * If a REQ subscription already exists, the same REQ is issued for the newly added relay * and CLOSE is sent for the removed relay. */ switchRelays(config: AcceptableRelaysConfig): Promise<void>; /** Utility wrapper for `switchRelays()`. */ addRelay(relay: string | RelayConfig): Promise<void>; /** Utility wrapper for `switchRelays()`. */ removeRelay(url: string): Promise<void>; /** Return true if the given relay is set to rxNostr. */ hasRelay(url: string): boolean; /** Return true if the given relay allows to be written. */ canWriteRelay(url: string): boolean; /** Return true if the given relay allows to be read. */ canReadRelay(url: string): boolean; /** Fetch all relays' info based on [NIP-11](https://github.com/nostr-protocol/nips/blob/master/11.md) */ fetchAllRelaysInfo(): Promise<Record<string, Nostr.Nip11.RelayInfo | null>>; /** * Return a dictionary in which you can look up connection state. * * **NOTE**: Keys are **normalized** URL, so may be different from one you set. */ getAllRelayState(): Record<string, ConnectionState>; /** * Return connection state of the given relay. * Throw if unknown URL is given. */ getRelayState(url: string): ConnectionState; /** * Attempt to reconnect the WebSocket if its state is `error` or `rejected`. * If not, do nothing. */ reconnect(url: string): void; // TODO: document /** * Set or unset a pipe to be applied to all EventPackets. */ setGlobalEventPacketPipe( pipe: MonoTypeOperatorFunction<EventPacket> | null ): void; /** * Associate RxReq with RxNostr. * When the associated RxReq is manipulated, * the RxNostr issues a new REQ to all relays allowed to be read. * The method returns an Observable that issues EventPackets * when an EVENT is received that is subscribed by RxReq. * You can unsubscribe the Observable to CLOSE. */ use( rxReq: RxReq, options?: Partial<RxNostrUseOptions> ): Observable<EventPacket>; /** * Create an Observable that receives all events (EVENT) from all websocket connections. * * Nothing happens when this Observable is unsubscribed. * */ createAllEventObservable(): Observable<EventPacket>; /** * Create an Observable that receives all errors from all websocket connections. * Note that an Observable is terminated when it receives any error, * so this method is the only way to receive errors arising from multiplexed websocket connections * (It means that Observables returned by `use()` never throw error). * * Nothing happens when this Observable is unsubscribed. * */ createAllErrorObservable(): Observable<ErrorPacket>; /** * Create an Observable that receives all messages from all websocket connections. * * Nothing happens when this Observable is unsubscribed. * */ createAllMessageObservable(): Observable<MessagePacket>; /** * Create an Observable that receives changing of WebSocket connection state. * * Nothing happens when this Observable is unsubscribed. */ createConnectionStateObservable(): Observable<ConnectionStatePacket>; /** * Attempt to send events to all relays that are allowed to write. * The `seckey` option accepts both nsec format and hex format, * and if omitted NIP-07 will be automatically used. */ send( params: Nostr.EventParameters, options?: RxNostrSendOptions ): Observable<OkPacket>; /** * Release all resources held by the RxNostr object. * Any Observable resulting from this RxNostr will be in the completed state * and will never receive messages again. * RxReq used by this object is not affected; in other words, if the RxReq is used * by another RxNostr, its use is not prevented. */ dispose(): void; } /** Create a RxNostr object. This is the only way to create that. */ export function createRxNostr(options?: Partial<RxNostrOptions>): RxNostr { return new RxNostrImpl(options); } export interface RxNostrOptions { /** Auto reconnection strategy. */
retry: BackoffConfig;
/** * The time in milliseconds to timeout when following the backward strategy. * The observable is terminated when the specified amount of time has elapsed * during which no new events are available. */ timeout: number; globalRelayConfig?: { disableAutoFetchNip11Limitations?: boolean; maxConcurrentReqsFallback?: number; }; } const makeRxNostrOptions = defineDefaultOptions<RxNostrOptions>({ retry: { strategy: "exponential", maxCount: 5, initialDelay: 1000, }, timeout: 10000, globalRelayConfig: undefined, }); export interface RxNostrUseOptions { scope?: string[]; } const makeRxNostrUseOptions = defineDefaultOptions<RxNostrUseOptions>({ scope: undefined, }); export interface RxNostrSendOptions { scope?: string[]; seckey?: string; } const makeRxNostrSendOptions = defineDefaultOptions<RxNostrSendOptions>({ scope: undefined, seckey: undefined, }); /** Config object specifying WebSocket behavior. */ export interface RelayConfig { /** WebSocket endpoint URL. */ url: string; /** If true, rxNostr can publish REQ and subscribe EVENTs. */ read: boolean; /** If true, rxNostr can send EVENTs. */ write: boolean; disableAutoFetchNip11Limitations?: boolean; } /** Parameter of `rxNostr.switchRelays()` */ export type AcceptableRelaysConfig = | (string | RelayConfig)[] | Nostr.Nip07.GetRelayResult; class RxNostrImpl implements RxNostr { private options: RxNostrOptions; private connections: Map<string, Connection> = new Map(); private ongoings: Map<string, OngoingReq> = new Map(); private messageIn$: Subject<MessagePacket> = new Subject(); private error$: Subject<ErrorPacket> = new Subject(); private status$: Subject<ConnectionStatePacket> = new Subject(); private globalEventPacketPipe: MonoTypeOperatorFunction<EventPacket> | null = null; private disposed = false; private get messageOut$() { return this.messageIn$.pipe( mergeMap((packet) => { const pipe = this.globalEventPacketPipe; if (!pipe) { return of(packet); } const message = packet.message; if (message[0] !== "EVENT") { return of(packet); } return of({ from: packet.from, subId: message[1], event: message[2], }).pipe( pipe, map( ({ from, subId, event }): MessagePacket => ({ from, message: ["EVENT", subId, event], }) ) ); }) ); } constructor(options?: Partial<RxNostrOptions>) { const opt = makeRxNostrOptions(options); this.options = { ...opt, }; } getRelays(): RelayConfig[] { return Array.from(this.connections.values()).map( ({ url, read, write }) => ({ url, read, write, }) ); } private createConnection({ url, read, write, disableAutoFetchNip11Limitations, }: RelayConfig): Connection { const connection = new Connection(url, { backoff: this.options.retry, read, write, disableAutoFetchNip11Limitations: disableAutoFetchNip11Limitations ?? this.options.globalRelayConfig?.disableAutoFetchNip11Limitations, maxConcurrentReqsFallback: this.options.globalRelayConfig?.maxConcurrentReqsFallback, }); connection.getConnectionStateObservable().subscribe((state) => { this.status$.next({ from: url, state, }); }); connection.getErrorObservable().subscribe((reason) => { this.error$.next({ from: url, reason }); }); connection .getMessageObservable() .pipe( catchError((reason: unknown) => { this.error$.next({ from: url, reason }); return EMPTY; }) ) .subscribe((v) => { this.messageIn$.next(v); }); return connection; } async switchRelays(config: AcceptableRelaysConfig): Promise<void> { const nextConns: Map<string, Connection> = new Map(); for (const { url, read, write } of normalizeRelaysConfig(config)) { // pop a connection if exists const prevConn = this.connections.get(url); this.connections.delete(url); if (prevConn) { prevConn.read = read; prevConn.write = write; nextConns.set(url, prevConn); } else { nextConns.set(url, this.createConnection({ url, read, write })); } } // connections that are no longer used for (const conn of this.connections.values()) { conn.dispose(); } const ensureConns: Promise<unknown>[] = []; for (const conn of nextConns.values()) { if (conn.read) { ensureConns.push(conn.start()); } else { conn.stop(); } } await Promise.all(ensureConns); this.connections = nextConns; // If disposed during switchRelay processing if (this.disposed) { for (const conn of this.connections.values()) { conn.dispose(); } return; } for (const { req, scope } of this.ongoings.values()) { this.ensureReq(req, { scope }); } // --- scoped untility pure functions --- function normalizeRelaysConfig( config: AcceptableRelaysConfig ): RelayConfig[] { if (Array.isArray(config)) { return config.map((urlOrConfig) => { const relay: RelayConfig = typeof urlOrConfig === "string" ? { url: urlOrConfig, read: true, write: true, } : urlOrConfig; relay.url = normalizeRelayUrl(relay.url); return relay; }); } else { return Object.entries(config).map(([url, flags]) => ({ url: normalizeRelayUrl(url), ...flags, })); } } } async addRelay(relay: string | RelayConfig): Promise<void> { await this.switchRelays([...this.getRelays(), relay]); } async removeRelay(url: string): Promise<void> { const u = normalizeRelayUrl(url); const currentRelays = this.getRelays(); const nextRelays = currentRelays.filter((relay) => relay.url !== u); if (currentRelays.length !== nextRelays.length) { await this.switchRelays(nextRelays); } } hasRelay(url: string): boolean { const u = normalizeRelayUrl(url); return this.getRelays().some((relay) => relay.url === u); } canWriteRelay(url: string): boolean { const u = normalizeRelayUrl(url); return this.getRelays().some((relay) => relay.url === u && relay.write); } canReadRelay(url: string): boolean { const u = normalizeRelayUrl(url); return this.getRelays().some((relay) => relay.url === u && relay.read); } async fetchAllRelaysInfo(): Promise< Record<string, Nostr.Nip11.RelayInfo | null> > { const entries = await Promise.all( Array.from(this.connections.keys()).map( async (url): Promise<[string, Nostr.Nip11.RelayInfo | null]> => [ url, await fetchRelayInfo(url).catch(() => null), ] ) ); return Object.fromEntries(entries); } getAllRelayState(): Record<string, ConnectionState> { return Object.fromEntries( Array.from(this.connections.values()).map((e) => [ e.url, this.getRelayState(e.url), ]) ); } getRelayState(url: string): ConnectionState { const conn = this.connections.get(normalizeRelayUrl(url)); if (!conn) { throw new Error("RelayConfig not found"); } // this.relays[url] may be set before this.relays[url].websocket is initialized return conn?.getConnectionState() ?? "not-started"; } reconnect(url: string): void { if (this.canReadRelay(url)) { this.connections.get(normalizeRelayUrl(url))?.start(); } } setGlobalEventPacketPipe(pipe: MonoTypeOperatorFunction<EventPacket> | null) { this.globalEventPacketPipe = pipe; } use( rxReq: RxReq, options?: Partial<RxNostrUseOptions> ): Observable<EventPacket> { const { scope: _scope } = makeRxNostrUseOptions(options); const scope = _scope?.map(normalizeRelayUrl); const TIMEOUT = this.options.timeout; const strategy = rxReq.strategy; const rxReqId = rxReq.rxReqId; const message$ = this.messageOut$; const ongoings = this.ongoings; const getAllRelayState = this.getAllRelayState.bind(this); const createConnectionStateObservable = this.createConnectionStateObservable.bind(this); const ensureReq = this.ensureReq.bind(this); const finalizeReq = this.finalizeReq.bind(this); const subId$ = rxReq.getReqObservable().pipe( filter((filters): filters is LazyFilter[] => filters !== null), strategy === "oneshot" ? first() : identity, attachSubId(), strategy === "forward" ? manageActiveForwardReq() : identity, tap((req) => { ensureReq(req, { overwrite: strategy === "forward", scope }); }), map(([, subId]) => subId) ); if (strategy === "forward") { const subId = makeSubId({ rxReqId, }); const resource: Unsubscribable[] = []; const subject = new Subject<EventPacket>(); resource.push(subject); return subject.pipe( tap({ subscribe: () => { resource.push(subId$.subscribe()); resource.push( message$ .pipe(filterBySubId(subId), pickEvents()) .subscribe((v) => { subject.next(v); }) ); }, finalize: () => { for (const r of resource) { r.unsubscribe(); } finalizeReq({ subId }); }, }) ); } else { return subId$.pipe(map(createEoseManagedEventObservable), mergeAll()); } function attachSubId(): OperatorFunction<LazyFilter[], LazyREQ> { const makeId = (index?: number) => makeSubId({ rxReqId, index }); switch (strategy) { case "backward": return map((filters, index) => ["REQ", makeId(index), ...filters]); case "forward": case "oneshot": return map((filters) => ["REQ", makeId(), ...filters]); } } function manageActiveForwardReq(): MonoTypeOperatorFunction<LazyREQ> { const recordActiveReq = (req: LazyREQ) => { const subId = req[1]; ongoings.set(subId, { req, scope, }); }; const forgetActiveReq = (subId: string) => { ongoings.delete(subId); }; return tap({ next: (req: LazyREQ) => { recordActiveReq(req); }, finalize: () => { forgetActiveReq( makeSubId({ rxReqId, }) ); }, }); } function createEoseManagedEventObservable( subId: string ): Observable<EventPacket> { const eose$ = new Subject<void>(); const complete$ = new Subject<void>(); const eoseRelays = new Set<string>(); const manageCompletion = merge( eose$, createConnectionStateObservable() ).subscribe(() => { const status = getAllRelayState(); const shouldComplete = Object.entries(status).every( ([url, state]) => (scope && !scope.includes(url)) || state === "error" || state === "terminated" || (state === "ongoing" && eoseRelays.has(url)) ); if (shouldComplete) { complete$.next(); } }); return message$.pipe( takeUntil(complete$), completeOnTimeout(TIMEOUT), filterBySubId(subId), filter((e) => !eoseRelays.has(e.from)), tap((e) => { if (e.message[0] === "EOSE") { eoseRelays.add(e.from); finalizeReq({ subId, url: e.from }); eose$.next(); } }), pickEvents(), finalize(() => { finalizeReq({ subId }); complete$.unsubscribe(); eose$.unsubscribe(); manageCompletion.unsubscribe(); }) ); } function filterBySubId( subId: string ): MonoTypeOperatorFunction<MessagePacket> { return filter( (packet) => (packet.message[0] === "EVENT" || packet.message[0] === "EOSE") && packet.message[1] === subId ); } function pickEvents(): OperatorFunction<MessagePacket, EventPacket> { return mergeMap(({ from, message }) => message[0] === "EVENT" ? of({ from, subId: message[1], event: message[2] }) : EMPTY ); } } createAllEventObservable(): Observable<EventPacket> { return this.messageOut$.pipe( mergeMap(({ from, message }) => message[0] === "EVENT" ? of({ from, subId: message[1], event: message[2] }) : EMPTY ) ); } createAllErrorObservable(): Observable<ErrorPacket> { return this.error$.asObservable(); } createAllMessageObservable(): Observable<MessagePacket> { return this.messageOut$; } createConnectionStateObservable(): Observable<ConnectionStatePacket> { return this.status$.asObservable(); } send( params: Nostr.EventParameters, options?: RxNostrSendOptions ): Observable<OkPacket> { const { seckey, scope: _scope } = makeRxNostrSendOptions(options); const scope = _scope?.map(normalizeRelayUrl); const writableConns = Array.from(this.connections.values()).filter( (conn) => (!scope || scope.includes(conn.url)) && conn.write ); const subject = new ReplaySubject<OkPacket>(writableConns.length); let subscription: Subscription | null = null; getSignedEvent(params, seckey).then((event) => { if (!subject.closed) { subscription = this.createAllMessageObservable().subscribe( ({ from, message }) => { if (message[0] !== "OK") { return; } subject.next({ from, id: event.id, ok: message[2], }); } ); } for (const conn of writableConns) { conn.sendEVENT(["EVENT", event]); } }); return subject.pipe( take(writableConns.length), timeout(30 * 1000), finalize(() => { subject.complete(); subject.unsubscribe(); subscription?.unsubscribe(); }) ); } dispose(): void { this.disposed = true; this.messageIn$.complete(); this.error$.complete(); for (const conn of this.connections.values()) { conn.dispose(); } } private ensureReq( req: LazyREQ, options?: { scope?: string[] | null; overwrite?: boolean } ) { const scope = options?.scope; for (const url of this.connections.keys()) { const conn = this.connections.get(url); if (!conn || !conn.read || (scope && !scope.includes(url))) { continue; } conn.ensureReq(req, { overwrite: options?.overwrite }); } } private finalizeReq(params: { subId: string; url?: string }) { const { subId, url } = params; if (subId === undefined && url === undefined) { throw new Error(); } if (url) { const conn = this.connections.get(url); conn?.finalizeReq(subId); } else { for (const conn of this.connections.values()) { conn?.finalizeReq(subId); } } } } interface OngoingReq { req: LazyREQ; scope?: string[]; } function makeSubId(params: { rxReqId: string; index?: number }): string { return `${params.rxReqId}:${params.index ?? 0}`; }
src/rx-nostr.ts
penpenpng-rx-nostr-7c52614
[ { "filename": "src/req.ts", "retrieved_chunk": "export type RxReqStrategy = \"forward\" | \"backward\" | \"oneshot\";\n/**\n * The RxReq interface that is provided for users (not for RxNostr).\n */\nexport interface RxReqController {\n /** Start new REQ or stop REQ on the RxNostr with witch the RxReq is associated. */\n emit(filters: LazyFilter | LazyFilter[] | null): void;\n /**\n * Returns itself overriding only `getReqObservable()`.\n * It is useful for throttling and other control purposes.", "score": 26.017040586913488 }, { "filename": "src/nostr/filter.ts", "retrieved_chunk": "/**\n * Return true if the given filter matches the given filters.\n */\nexport function isFiltered(\n event: Nostr.Event,\n filters: Nostr.Filter | Nostr.Filter[],\n options?: Partial<MatchFilterOptions>\n): boolean {\n if (Array.isArray(filters)) {\n return filters.some((filter) => _isFiltered(event, filter, options));", "score": 18.335436704746698 }, { "filename": "src/nostr/filter.ts", "retrieved_chunk": " } else {\n return _isFiltered(event, filters, options);\n }\n}\nfunction _isFiltered(\n event: Nostr.Event,\n filter: Nostr.Filter,\n options?: Partial<MatchFilterOptions>\n): boolean {\n const { sinceInclusive, untilInclusive } = makeMatchFilterOptions(options);", "score": 17.60126383712412 }, { "filename": "src/req.ts", "retrieved_chunk": "import Nostr from \"nostr-typedef\";\nimport { BehaviorSubject, Observable, type OperatorFunction } from \"rxjs\";\nimport { LazyFilter, ReqPacket } from \"./packet.js\";\nimport type { Override } from \"./util.js\";\n/**\n * The RxReq interface that is provided for RxNostr (**not for users**).\n */\nexport interface RxReq<S extends RxReqStrategy = RxReqStrategy> {\n /** @internal User should not use this directly.The RxReq strategy. It is read-only and must not change. */\n get strategy(): S;", "score": 17.053331060626782 }, { "filename": "src/req.ts", "retrieved_chunk": " constructor(rxReqId?: string) {\n super(rxReqId);\n }\n override get strategy(): \"forward\" {\n return \"forward\";\n }\n}\n/**\n * Create a RxReq instance based on the oneshot strategy.\n * It is almost the same as backward strategy, however can publish only one REQ", "score": 16.61323664599239 } ]
typescript
retry: BackoffConfig;
import { afterEach, assert, beforeEach, describe, expect, test } from "vitest"; import { createMockRelay, type MockRelay } from "vitest-nostr"; import { WebSocketCloseCode } from "../connection.js"; import { createRxBackwardReq, createRxForwardReq, createRxNostr, createRxOneshotReq, RxNostr, } from "../index.js"; import { faker, spyEvent, spySub } from "./helper.js"; describe("Basic subscription behavior (single relay)", () => { const RELAY_URL = "ws://localhost:1234"; let rxNostr: RxNostr; let relay: MockRelay; beforeEach(async () => { relay = createMockRelay(RELAY_URL); rxNostr = createRxNostr({ retry: { strategy: "immediately", maxCount: 1 }, globalRelayConfig: { disableAutoFetchNip11Limitations: true, }, }); await rxNostr.switchRelays([RELAY_URL]); await relay.connected; }); afterEach(() => { rxNostr.dispose(); relay.close({ code: WebSocketCloseCode.DISPOSED_BY_RX_NOSTR, reason: "Clean up on afterEach()", wasClean: true, }); }); test("[forward] Each REQ is published with the same subId.", async () => { const req = createRxForwardReq("sub"); rxNostr.use(req).subscribe(); req.emit(faker.filter()); await expect(relay).toReceiveREQ("sub:0"); req.emit(faker.filters()); await expect(relay).toReceiveREQ("sub:0"); req.emit(faker.filters()); await expect(relay).toReceiveREQ("sub:0"); }); test("[forward] If connection is abnormally closed, REQ will be retried.", async () => { const req = createRxForwardReq("sub"); const spy =
spyEvent();
rxNostr.use(req).pipe(spy.tap()).subscribe(); req.emit(faker.filters()); await expect(relay).toReceiveREQ("sub:0"); // Emulate an abnormal disconnection of a relay. const socket = await relay.getSocket(0); socket.close({ code: WebSocketCloseCode.ABNORMAL_CLOSURE, reason: "Relay's internal error", wasClean: true, }); await expect(relay).toReceiveREQ("sub:0"); relay.emitEVENT("sub:0"); await expect(spy).toSeeEVENT(); }); test("[forward] Backoff option `maxCount` works.", async () => { const req = createRxForwardReq("sub"); const spy = spyEvent(); rxNostr.use(req).pipe(spy.tap()).subscribe(); req.emit(faker.filters()); await expect(relay).toReceiveREQ("sub:0"); (await relay.getSocket(0)).close({ code: WebSocketCloseCode.ABNORMAL_CLOSURE, reason: "Relay's internal error", wasClean: true, }); await expect(relay).toReceiveREQ("sub:0"); (await relay.getSocket(1)).close({ code: WebSocketCloseCode.ABNORMAL_CLOSURE, reason: "Relay's internal error", wasClean: true, }); // FIXME: dirty return new Promise((resolve) => { setTimeout(resolve, 100); }).then(async () => { await expect(rxNostr.getRelayState(RELAY_URL)).toBe("error"); }); }); test("[forward] If connection is closed with 4000, REQ will not be retried.", async () => { const req = createRxForwardReq("sub"); const spy = spyEvent(); rxNostr.use(req).pipe(spy.tap()).subscribe(); req.emit(faker.filters()); await expect(relay).toReceiveREQ("sub:0"); // Emulate an abnormal disconnection of a relay. const socket = await relay.getSocket(0); socket.close({ code: WebSocketCloseCode.DONT_RETRY, reason: "Relay's internal error, but should not retry.", wasClean: true, }); rxNostr.send(faker.event()); // REQ will not be retried, so next message is EVENT await expect(relay).toReceiveEVENT(); }); test("[forward] since/until is reevaluated when a lazy REQ is resubmitted.", async () => { const req = createRxForwardReq("sub"); rxNostr.use(req).subscribe(); let since = 0; req.emit({ ...faker.filter(), since: () => since++ }); await expect(relay).toReceiveREQ([ "sub:0", { ...faker.filter(), since: 0 }, ]); // Emulate an abnormal disconnection of a relay. const socket = await relay.getSocket(0); socket.close({ code: WebSocketCloseCode.ABNORMAL_CLOSURE, reason: "Relay's internal error", wasClean: true, }); await expect(relay).toReceiveREQ([ "sub:0", { ...faker.filter(), since: 1 }, ]); }); test("[forward] Reject EVENTs that do not match the given filters.", async () => { const req = createRxForwardReq("sub"); const spy = spyEvent(); rxNostr.use(req).pipe(spy.tap()).subscribe(); req.emit({ kinds: [1] }); await expect(relay).toReceiveREQ("sub:0"); relay.emitEVENT("sub:0", faker.event({ kind: 1, content: "pass" })); await expect(spy).toSeeEVENT([ "sub:0", faker.event({ kind: 1, content: "pass" }), ]); relay.emitEVENT("sub:0", faker.event({ kind: 0, content: "rejected" })); relay.emitEVENT("sub:0", faker.event({ kind: 1, content: "pass" })); await expect(spy).toSeeEVENT([ "sub:0", faker.event({ kind: 1, content: "pass" }), ]); }); test("[backward] After receiving EOSE, CLOSE is sent out.", async () => { const req = createRxBackwardReq("sub"); rxNostr.use(req).pipe().subscribe(); req.emit(faker.filters()); await expect(relay).toReceiveREQ("sub:0"); relay.emitEOSE("sub:0"); await expect(relay).toReceiveCLOSE("sub:0"); }); test("[backward] Receipt of EOSE does not terminate the Observable.", async () => { const req = createRxBackwardReq("sub"); const spy = spySub(); rxNostr.use(req).pipe(spy.tap()).subscribe(); req.emit(faker.filters()); await expect(relay).toReceiveREQ(); relay.emitEOSE("sub:0"); assert(!spy.completed()); }); test("[backward] Each EOSE CLOSEs the REQ in the order of arrival.", async () => { const req = createRxBackwardReq("sub"); rxNostr.use(req).subscribe(); req.emit(faker.filters()); await expect(relay).toReceiveREQ("sub:0"); req.emit(faker.filters()); await expect(relay).toReceiveREQ("sub:1"); req.emit(faker.filters()); await expect(relay).toReceiveREQ("sub:2"); relay.emitEOSE("sub:2"); await expect(relay).toReceiveCLOSE("sub:2"); relay.emitEOSE("sub:1"); await expect(relay).toReceiveCLOSE("sub:1"); relay.emitEOSE("sub:0"); await expect(relay).toReceiveCLOSE("sub:0"); }); test("[backward] Even if a newer REQ emits EOSE, EVENTs from older but still active REQ can be received.", async () => { const req = createRxBackwardReq("sub"); const spy = spyEvent(); rxNostr.use(req).pipe(spy.tap()).subscribe(); req.emit(faker.filters()); await expect(relay).toReceiveREQ("sub:0"); req.emit(faker.filters()); await expect(relay).toReceiveREQ("sub:1"); relay.emitEOSE("sub:1"); await expect(relay).toReceiveCLOSE("sub:1"); relay.emitEVENT("sub:0"); await expect(spy).toSeeEVENT("sub:0"); relay.emitEOSE("sub:0"); await expect(relay).toReceiveCLOSE("sub:0"); }); test("[backward] If connection is abnormally closed before receiving EOSE, REQ will be retried.", async () => { const req = createRxBackwardReq("sub"); const spy = spyEvent(); rxNostr.use(req).pipe(spy.tap()).subscribe(); req.emit(faker.filters()); await expect(relay).toReceiveREQ("sub:0"); // Emulate an abnormal disconnection of a relay. const socket = await relay.getSocket(0); socket.close({ code: WebSocketCloseCode.ABNORMAL_CLOSURE, reason: "Relay's internal error", wasClean: true, }); await expect(relay).toReceiveREQ("sub:0"); relay.emitEVENT("sub:0"); await expect(spy).toSeeEVENT(); relay.emitEOSE("sub:0"); await expect(relay).toReceiveCLOSE("sub:0"); }); test("[backward] If connection is abnormally closed after receiving EOSE, REQ will not be retried.", async () => { const req = createRxBackwardReq("sub"); rxNostr.use(req).subscribe(); req.emit(faker.filters()); await expect(relay).toReceiveREQ("sub:0"); relay.emitEOSE("sub:0"); await expect(relay).toReceiveCLOSE("sub:0"); // Emulate an abnormal disconnection of a relay. const socket = await relay.getSocket(0); socket.close({ code: WebSocketCloseCode.ABNORMAL_CLOSURE, reason: "Relay's internal error", wasClean: true, }); rxNostr.send(faker.event()); await expect(relay).toReceiveEVENT(); }); test("[oneshot] Receipt of EOSE terminates the Observable.", async () => { const req = createRxOneshotReq({ subId: "sub", filters: faker.filter(), }); const spy = spySub(); rxNostr.use(req).pipe(spy.tap()).subscribe(); await expect(relay).toReceiveREQ("sub:0"); assert(!spy.completed()); relay.emitEOSE("sub:0"); assert(spy.completed()); }); }); describe("Basic subscription behavior (multiple relays)", () => { const RELAY_URL1 = "ws://localhost:1234"; const RELAY_URL2 = "ws://localhost:1235"; const RELAY_URL3 = "ws://localhost:1236"; let rxNostr: RxNostr; let relay1: MockRelay; let relay2: MockRelay; let relay3: MockRelay; beforeEach(async () => { relay1 = createMockRelay(RELAY_URL1); relay2 = createMockRelay(RELAY_URL2); relay3 = createMockRelay(RELAY_URL3); rxNostr = createRxNostr({ globalRelayConfig: { disableAutoFetchNip11Limitations: true, }, }); await rxNostr.switchRelays([RELAY_URL1, RELAY_URL2]); await relay1.connected; await relay2.connected; }); afterEach(() => { rxNostr.dispose(); relay1.close({ code: WebSocketCloseCode.DISPOSED_BY_RX_NOSTR, reason: "Clean up on afterEach()", wasClean: true, }); relay2.close({ code: WebSocketCloseCode.DISPOSED_BY_RX_NOSTR, reason: "Clean up on afterEach()", wasClean: true, }); relay3.close({ code: WebSocketCloseCode.DISPOSED_BY_RX_NOSTR, reason: "Clean up on afterEach()", wasClean: true, }); }); test("[forward] Adding new relay affects existing REQ.", async () => { const req = createRxForwardReq("sub"); rxNostr.use(req).subscribe(); req.emit(faker.filters()); await expect(relay1).toReceiveREQ("sub:0"); await expect(relay2).toReceiveREQ("sub:0"); await rxNostr.addRelay(RELAY_URL3); await expect(relay3).toReceiveREQ("sub:0"); }); test("[forward] Removing a relay affects existing REQ.", async () => { const req = createRxForwardReq("sub"); rxNostr.use(req).subscribe(); req.emit(faker.filters()); await expect(relay1).toReceiveREQ("sub:0"); await expect(relay2).toReceiveREQ("sub:0"); await rxNostr.removeRelay(RELAY_URL2); await expect(relay2).toReceiveCLOSE("sub:0"); }); test("[forward] Adding new relay doesn't affect existing subset-REQ when the relay is not in subset.", async () => { const req = createRxForwardReq("sub"); rxNostr.use(req, { scope: [RELAY_URL1] }).subscribe(); req.emit(faker.filters()); await expect(relay1).toReceiveREQ("sub:0"); await rxNostr.addRelay(RELAY_URL3); rxNostr.send(faker.event()); await expect(relay1).toReceiveEVENT(); await expect(relay2).toReceiveEVENT(); await expect(relay3).toReceiveEVENT(); expect(relay2.messagesToConsume.pendingItems.length).toBe(0); expect(relay3.messagesToConsume.pendingItems.length).toBe(0); }); test("[forward] Adding new relay affects existing subset-REQ when the relay is in subset.", async () => { const req = createRxForwardReq("sub"); rxNostr.use(req, { scope: [RELAY_URL1, RELAY_URL3] }).subscribe(); req.emit(faker.filters()); await expect(relay1).toReceiveREQ("sub:0"); await rxNostr.addRelay(RELAY_URL3); await expect(relay3).toReceiveREQ("sub:0"); expect(relay2.messagesToConsume.pendingItems.length).toBe(0); }); test("[backward] EOSE on all subset relays triggers CLOSE.", async () => { const req = createRxBackwardReq("sub"); rxNostr.use(req, { scope: [RELAY_URL1] }).subscribe(); req.emit(faker.filters()); await expect(relay1).toReceiveREQ("sub:0"); relay1.emitEOSE("sub:0"); await expect(relay1).toReceiveCLOSE("sub:0"); expect(relay2.messagesToConsume.pendingItems.length).toBe(0); expect(relay3.messagesToConsume.pendingItems.length).toBe(0); }); test("[backward] EOSE on all subset and active relays triggers CLOSE.", async () => { const req = createRxBackwardReq("sub"); rxNostr.use(req, { scope: [RELAY_URL1, RELAY_URL3] }).subscribe(); req.emit(faker.filters()); await expect(relay1).toReceiveREQ("sub:0"); relay1.emitEOSE("sub:0"); await expect(relay1).toReceiveCLOSE("sub:0"); expect(relay2.messagesToConsume.pendingItems.length).toBe(0); }); test("[oneshot] EOSE on all subset and active relays triggers completion.", async () => { const req = createRxOneshotReq({ subId: "sub", filters: faker.filters(), }); const spy = spySub(); rxNostr .use(req, { scope: [RELAY_URL1, RELAY_URL3] }) .pipe(spy.tap()) .subscribe(); await expect(relay1).toReceiveREQ("sub:0"); relay1.emitEOSE("sub:0"); await expect(relay1).toReceiveCLOSE("sub:0"); assert(spy.completed()); }); test("[oneshot] Collect all events under different timing EOSE.", async () => { const req = createRxOneshotReq({ subId: "sub", filters: faker.filters(), }); const spy = spyEvent(); rxNostr.use(req).pipe(spy.tap()).subscribe(); await expect(relay1).toReceiveREQ("sub:0"); await expect(relay2).toReceiveREQ("sub:0"); relay1.emitEVENT("sub:0"); await expect(spy).toSeeEVENT("sub:0"); relay2.emitEVENT("sub:0"); await expect(spy).toSeeEVENT("sub:0"); relay1.emitEOSE("sub:0"); await expect(relay1).toReceiveCLOSE("sub:0"); relay2.emitEVENT("sub:0"); await expect(spy).toSeeEVENT("sub:0"); relay2.emitEOSE("sub:0"); await expect(relay2).toReceiveCLOSE("sub:0"); }); }); describe("Under limited REQ concurency (single relay)", () => { const RELAY_URL = "ws://localhost:1234"; let rxNostr: RxNostr; let relay: MockRelay; beforeEach(async () => { relay = createMockRelay(RELAY_URL); rxNostr = createRxNostr({ retry: { strategy: "immediately", maxCount: 1 }, globalRelayConfig: { disableAutoFetchNip11Limitations: true, maxConcurrentReqsFallback: 1, }, }); await rxNostr.switchRelays([RELAY_URL]); await relay.connected; }); afterEach(() => { rxNostr.dispose(); relay.close({ code: WebSocketCloseCode.DISPOSED_BY_RX_NOSTR, reason: "Clean up on afterEach()", wasClean: true, }); }); test("[backward] Overflowed REQs will be enqueued.", async () => { const req = createRxBackwardReq("sub"); rxNostr.use(req).pipe().subscribe(); req.emit(faker.filters()); req.emit(faker.filters()); req.emit(faker.filters()); await expect(relay).toReceiveREQ("sub:0"); relay.emitEOSE("sub:0"); await expect(relay).toReceiveCLOSE("sub:0"); await expect(relay).toReceiveREQ("sub:1"); relay.emitEOSE("sub:1"); await expect(relay).toReceiveCLOSE("sub:1"); await expect(relay).toReceiveREQ("sub:2"); relay.emitEOSE("sub:2"); await expect(relay).toReceiveCLOSE("sub:2"); }); });
src/__test__/subscription.test.ts
penpenpng-rx-nostr-7c52614
[ { "filename": "src/__test__/sending.test.ts", "retrieved_chunk": " await rxNostr.switchRelays([RELAY_URL1, RELAY_URL2]);\n rxNostr\n .send(faker.event(), { scope: [RELAY_URL2] })\n .pipe(spy.tap())\n .subscribe();\n await expect(relay2).toReceiveEVENT();\n expect(relay1.messagesToConsume.pendingItems.length).toBe(0);\n relay2.emitOK(\"*\", true);\n expect(spy.completed()).toBe(true);\n });", "score": 42.96644968390062 }, { "filename": "src/__test__/sending.test.ts", "retrieved_chunk": " rxNostr.send(faker.event()).pipe(spy.tap()).subscribe();\n rxNostr.addRelay(RELAY_URL3);\n await expect(relay1).toReceiveEVENT();\n expect(relay2.messagesToConsume.pendingItems.length).toBe(0);\n expect(relay3.messagesToConsume.pendingItems.length).toBe(0);\n relay1.emitOK(\"*\", true);\n expect(spy.completed()).toBe(true);\n });\n test(\"send() doesn't wait for OK from out of scope.\", async () => {\n const spy = spySub();", "score": 42.64412429512492 }, { "filename": "src/__test__/sending.test.ts", "retrieved_chunk": " wasClean: true,\n });\n });\n test(\"send() sends only to writable relays.\", async () => {\n rxNostr.send(faker.event());\n await expect(relay1).toReceiveEVENT();\n expect(relay2.messagesToConsume.pendingItems.length).toBe(0);\n });\n test(\"send() doesn't wait for OK from relays added later.\", async () => {\n const spy = spySub();", "score": 41.026924092612816 }, { "filename": "src/__test__/extend.test.ts", "retrieved_chunk": "import { expect, test } from \"vitest\";\nimport { createRxBackwardReq, extend, mixin } from \"../index.js\";\ntest(\"Extend req.\", async () => {\n const addFoo = mixin<{ strategy: \"backward\" }, { foo: () => string }>(() => ({\n foo() {\n return this.strategy;\n },\n }));\n const req = extend(createRxBackwardReq(), addFoo);\n expect(req.strategy).toBe(\"backward\");", "score": 30.8161247141856 }, { "filename": "src/__test__/extend.test.ts", "retrieved_chunk": " expect(req.foo()).toBe(\"backward\");\n});\ntest(\"Override req.\", async () => {\n const original = extend(\n createRxBackwardReq(),\n mixin<object, { foo: () => string }>(() => ({\n foo() {\n return \"foo\";\n },\n }))", "score": 29.528215543564027 } ]
typescript
spyEvent();
import { afterEach, assert, beforeEach, describe, expect, test } from "vitest"; import { createMockRelay, type MockRelay } from "vitest-nostr"; import { WebSocketCloseCode } from "../connection.js"; import { createRxBackwardReq, createRxForwardReq, createRxNostr, createRxOneshotReq, RxNostr, } from "../index.js"; import { faker, spyEvent, spySub } from "./helper.js"; describe("Basic subscription behavior (single relay)", () => { const RELAY_URL = "ws://localhost:1234"; let rxNostr: RxNostr; let relay: MockRelay; beforeEach(async () => { relay = createMockRelay(RELAY_URL); rxNostr = createRxNostr({ retry: { strategy: "immediately", maxCount: 1 }, globalRelayConfig: { disableAutoFetchNip11Limitations: true, }, }); await rxNostr.switchRelays([RELAY_URL]); await relay.connected; }); afterEach(() => { rxNostr.dispose(); relay.close({ code: WebSocketCloseCode.DISPOSED_BY_RX_NOSTR, reason: "Clean up on afterEach()", wasClean: true, }); }); test("[forward] Each REQ is published with the same subId.", async () => { const req = createRxForwardReq("sub"); rxNostr.use(req).subscribe(); req.emit(faker.filter()); await expect(relay).toReceiveREQ("sub:0"); req.emit(faker.filters()); await expect(relay).toReceiveREQ("sub:0"); req.emit(faker.filters()); await expect(relay).toReceiveREQ("sub:0"); }); test("[forward] If connection is abnormally closed, REQ will be retried.", async () => { const req = createRxForwardReq("sub"); const spy = spyEvent(); rxNostr.use(req).pipe(spy.tap()).subscribe(); req.emit(faker.filters()); await expect(relay).toReceiveREQ("sub:0"); // Emulate an abnormal disconnection of a relay. const socket = await relay.getSocket(0); socket.close({ code: WebSocketCloseCode.ABNORMAL_CLOSURE, reason: "Relay's internal error", wasClean: true, }); await expect(relay).toReceiveREQ("sub:0"); relay.emitEVENT("sub:0"); await expect(spy).toSeeEVENT(); }); test("[forward] Backoff option `maxCount` works.", async () => { const req = createRxForwardReq("sub"); const spy = spyEvent(); rxNostr.use(req).pipe(spy.tap()).subscribe(); req.emit(faker.filters()); await expect(relay).toReceiveREQ("sub:0"); (await relay.getSocket(0)).close({ code: WebSocketCloseCode.ABNORMAL_CLOSURE, reason: "Relay's internal error", wasClean: true, }); await expect(relay).toReceiveREQ("sub:0"); (await relay.getSocket(1)).close({ code: WebSocketCloseCode.ABNORMAL_CLOSURE, reason: "Relay's internal error", wasClean: true, }); // FIXME: dirty return new Promise((resolve) => { setTimeout(resolve, 100); }).then(async () => { await expect(rxNostr.getRelayState(RELAY_URL)).toBe("error"); }); }); test("[forward] If connection is closed with 4000, REQ will not be retried.", async () => { const req = createRxForwardReq("sub"); const spy = spyEvent(); rxNostr.use(req).pipe(spy.tap()).subscribe(); req.emit(faker.filters()); await expect(relay).toReceiveREQ("sub:0"); // Emulate an abnormal disconnection of a relay. const socket = await relay.getSocket(0); socket.close({ code
: WebSocketCloseCode.DONT_RETRY, reason: "Relay's internal error, but should not retry.", wasClean: true, });
rxNostr.send(faker.event()); // REQ will not be retried, so next message is EVENT await expect(relay).toReceiveEVENT(); }); test("[forward] since/until is reevaluated when a lazy REQ is resubmitted.", async () => { const req = createRxForwardReq("sub"); rxNostr.use(req).subscribe(); let since = 0; req.emit({ ...faker.filter(), since: () => since++ }); await expect(relay).toReceiveREQ([ "sub:0", { ...faker.filter(), since: 0 }, ]); // Emulate an abnormal disconnection of a relay. const socket = await relay.getSocket(0); socket.close({ code: WebSocketCloseCode.ABNORMAL_CLOSURE, reason: "Relay's internal error", wasClean: true, }); await expect(relay).toReceiveREQ([ "sub:0", { ...faker.filter(), since: 1 }, ]); }); test("[forward] Reject EVENTs that do not match the given filters.", async () => { const req = createRxForwardReq("sub"); const spy = spyEvent(); rxNostr.use(req).pipe(spy.tap()).subscribe(); req.emit({ kinds: [1] }); await expect(relay).toReceiveREQ("sub:0"); relay.emitEVENT("sub:0", faker.event({ kind: 1, content: "pass" })); await expect(spy).toSeeEVENT([ "sub:0", faker.event({ kind: 1, content: "pass" }), ]); relay.emitEVENT("sub:0", faker.event({ kind: 0, content: "rejected" })); relay.emitEVENT("sub:0", faker.event({ kind: 1, content: "pass" })); await expect(spy).toSeeEVENT([ "sub:0", faker.event({ kind: 1, content: "pass" }), ]); }); test("[backward] After receiving EOSE, CLOSE is sent out.", async () => { const req = createRxBackwardReq("sub"); rxNostr.use(req).pipe().subscribe(); req.emit(faker.filters()); await expect(relay).toReceiveREQ("sub:0"); relay.emitEOSE("sub:0"); await expect(relay).toReceiveCLOSE("sub:0"); }); test("[backward] Receipt of EOSE does not terminate the Observable.", async () => { const req = createRxBackwardReq("sub"); const spy = spySub(); rxNostr.use(req).pipe(spy.tap()).subscribe(); req.emit(faker.filters()); await expect(relay).toReceiveREQ(); relay.emitEOSE("sub:0"); assert(!spy.completed()); }); test("[backward] Each EOSE CLOSEs the REQ in the order of arrival.", async () => { const req = createRxBackwardReq("sub"); rxNostr.use(req).subscribe(); req.emit(faker.filters()); await expect(relay).toReceiveREQ("sub:0"); req.emit(faker.filters()); await expect(relay).toReceiveREQ("sub:1"); req.emit(faker.filters()); await expect(relay).toReceiveREQ("sub:2"); relay.emitEOSE("sub:2"); await expect(relay).toReceiveCLOSE("sub:2"); relay.emitEOSE("sub:1"); await expect(relay).toReceiveCLOSE("sub:1"); relay.emitEOSE("sub:0"); await expect(relay).toReceiveCLOSE("sub:0"); }); test("[backward] Even if a newer REQ emits EOSE, EVENTs from older but still active REQ can be received.", async () => { const req = createRxBackwardReq("sub"); const spy = spyEvent(); rxNostr.use(req).pipe(spy.tap()).subscribe(); req.emit(faker.filters()); await expect(relay).toReceiveREQ("sub:0"); req.emit(faker.filters()); await expect(relay).toReceiveREQ("sub:1"); relay.emitEOSE("sub:1"); await expect(relay).toReceiveCLOSE("sub:1"); relay.emitEVENT("sub:0"); await expect(spy).toSeeEVENT("sub:0"); relay.emitEOSE("sub:0"); await expect(relay).toReceiveCLOSE("sub:0"); }); test("[backward] If connection is abnormally closed before receiving EOSE, REQ will be retried.", async () => { const req = createRxBackwardReq("sub"); const spy = spyEvent(); rxNostr.use(req).pipe(spy.tap()).subscribe(); req.emit(faker.filters()); await expect(relay).toReceiveREQ("sub:0"); // Emulate an abnormal disconnection of a relay. const socket = await relay.getSocket(0); socket.close({ code: WebSocketCloseCode.ABNORMAL_CLOSURE, reason: "Relay's internal error", wasClean: true, }); await expect(relay).toReceiveREQ("sub:0"); relay.emitEVENT("sub:0"); await expect(spy).toSeeEVENT(); relay.emitEOSE("sub:0"); await expect(relay).toReceiveCLOSE("sub:0"); }); test("[backward] If connection is abnormally closed after receiving EOSE, REQ will not be retried.", async () => { const req = createRxBackwardReq("sub"); rxNostr.use(req).subscribe(); req.emit(faker.filters()); await expect(relay).toReceiveREQ("sub:0"); relay.emitEOSE("sub:0"); await expect(relay).toReceiveCLOSE("sub:0"); // Emulate an abnormal disconnection of a relay. const socket = await relay.getSocket(0); socket.close({ code: WebSocketCloseCode.ABNORMAL_CLOSURE, reason: "Relay's internal error", wasClean: true, }); rxNostr.send(faker.event()); await expect(relay).toReceiveEVENT(); }); test("[oneshot] Receipt of EOSE terminates the Observable.", async () => { const req = createRxOneshotReq({ subId: "sub", filters: faker.filter(), }); const spy = spySub(); rxNostr.use(req).pipe(spy.tap()).subscribe(); await expect(relay).toReceiveREQ("sub:0"); assert(!spy.completed()); relay.emitEOSE("sub:0"); assert(spy.completed()); }); }); describe("Basic subscription behavior (multiple relays)", () => { const RELAY_URL1 = "ws://localhost:1234"; const RELAY_URL2 = "ws://localhost:1235"; const RELAY_URL3 = "ws://localhost:1236"; let rxNostr: RxNostr; let relay1: MockRelay; let relay2: MockRelay; let relay3: MockRelay; beforeEach(async () => { relay1 = createMockRelay(RELAY_URL1); relay2 = createMockRelay(RELAY_URL2); relay3 = createMockRelay(RELAY_URL3); rxNostr = createRxNostr({ globalRelayConfig: { disableAutoFetchNip11Limitations: true, }, }); await rxNostr.switchRelays([RELAY_URL1, RELAY_URL2]); await relay1.connected; await relay2.connected; }); afterEach(() => { rxNostr.dispose(); relay1.close({ code: WebSocketCloseCode.DISPOSED_BY_RX_NOSTR, reason: "Clean up on afterEach()", wasClean: true, }); relay2.close({ code: WebSocketCloseCode.DISPOSED_BY_RX_NOSTR, reason: "Clean up on afterEach()", wasClean: true, }); relay3.close({ code: WebSocketCloseCode.DISPOSED_BY_RX_NOSTR, reason: "Clean up on afterEach()", wasClean: true, }); }); test("[forward] Adding new relay affects existing REQ.", async () => { const req = createRxForwardReq("sub"); rxNostr.use(req).subscribe(); req.emit(faker.filters()); await expect(relay1).toReceiveREQ("sub:0"); await expect(relay2).toReceiveREQ("sub:0"); await rxNostr.addRelay(RELAY_URL3); await expect(relay3).toReceiveREQ("sub:0"); }); test("[forward] Removing a relay affects existing REQ.", async () => { const req = createRxForwardReq("sub"); rxNostr.use(req).subscribe(); req.emit(faker.filters()); await expect(relay1).toReceiveREQ("sub:0"); await expect(relay2).toReceiveREQ("sub:0"); await rxNostr.removeRelay(RELAY_URL2); await expect(relay2).toReceiveCLOSE("sub:0"); }); test("[forward] Adding new relay doesn't affect existing subset-REQ when the relay is not in subset.", async () => { const req = createRxForwardReq("sub"); rxNostr.use(req, { scope: [RELAY_URL1] }).subscribe(); req.emit(faker.filters()); await expect(relay1).toReceiveREQ("sub:0"); await rxNostr.addRelay(RELAY_URL3); rxNostr.send(faker.event()); await expect(relay1).toReceiveEVENT(); await expect(relay2).toReceiveEVENT(); await expect(relay3).toReceiveEVENT(); expect(relay2.messagesToConsume.pendingItems.length).toBe(0); expect(relay3.messagesToConsume.pendingItems.length).toBe(0); }); test("[forward] Adding new relay affects existing subset-REQ when the relay is in subset.", async () => { const req = createRxForwardReq("sub"); rxNostr.use(req, { scope: [RELAY_URL1, RELAY_URL3] }).subscribe(); req.emit(faker.filters()); await expect(relay1).toReceiveREQ("sub:0"); await rxNostr.addRelay(RELAY_URL3); await expect(relay3).toReceiveREQ("sub:0"); expect(relay2.messagesToConsume.pendingItems.length).toBe(0); }); test("[backward] EOSE on all subset relays triggers CLOSE.", async () => { const req = createRxBackwardReq("sub"); rxNostr.use(req, { scope: [RELAY_URL1] }).subscribe(); req.emit(faker.filters()); await expect(relay1).toReceiveREQ("sub:0"); relay1.emitEOSE("sub:0"); await expect(relay1).toReceiveCLOSE("sub:0"); expect(relay2.messagesToConsume.pendingItems.length).toBe(0); expect(relay3.messagesToConsume.pendingItems.length).toBe(0); }); test("[backward] EOSE on all subset and active relays triggers CLOSE.", async () => { const req = createRxBackwardReq("sub"); rxNostr.use(req, { scope: [RELAY_URL1, RELAY_URL3] }).subscribe(); req.emit(faker.filters()); await expect(relay1).toReceiveREQ("sub:0"); relay1.emitEOSE("sub:0"); await expect(relay1).toReceiveCLOSE("sub:0"); expect(relay2.messagesToConsume.pendingItems.length).toBe(0); }); test("[oneshot] EOSE on all subset and active relays triggers completion.", async () => { const req = createRxOneshotReq({ subId: "sub", filters: faker.filters(), }); const spy = spySub(); rxNostr .use(req, { scope: [RELAY_URL1, RELAY_URL3] }) .pipe(spy.tap()) .subscribe(); await expect(relay1).toReceiveREQ("sub:0"); relay1.emitEOSE("sub:0"); await expect(relay1).toReceiveCLOSE("sub:0"); assert(spy.completed()); }); test("[oneshot] Collect all events under different timing EOSE.", async () => { const req = createRxOneshotReq({ subId: "sub", filters: faker.filters(), }); const spy = spyEvent(); rxNostr.use(req).pipe(spy.tap()).subscribe(); await expect(relay1).toReceiveREQ("sub:0"); await expect(relay2).toReceiveREQ("sub:0"); relay1.emitEVENT("sub:0"); await expect(spy).toSeeEVENT("sub:0"); relay2.emitEVENT("sub:0"); await expect(spy).toSeeEVENT("sub:0"); relay1.emitEOSE("sub:0"); await expect(relay1).toReceiveCLOSE("sub:0"); relay2.emitEVENT("sub:0"); await expect(spy).toSeeEVENT("sub:0"); relay2.emitEOSE("sub:0"); await expect(relay2).toReceiveCLOSE("sub:0"); }); }); describe("Under limited REQ concurency (single relay)", () => { const RELAY_URL = "ws://localhost:1234"; let rxNostr: RxNostr; let relay: MockRelay; beforeEach(async () => { relay = createMockRelay(RELAY_URL); rxNostr = createRxNostr({ retry: { strategy: "immediately", maxCount: 1 }, globalRelayConfig: { disableAutoFetchNip11Limitations: true, maxConcurrentReqsFallback: 1, }, }); await rxNostr.switchRelays([RELAY_URL]); await relay.connected; }); afterEach(() => { rxNostr.dispose(); relay.close({ code: WebSocketCloseCode.DISPOSED_BY_RX_NOSTR, reason: "Clean up on afterEach()", wasClean: true, }); }); test("[backward] Overflowed REQs will be enqueued.", async () => { const req = createRxBackwardReq("sub"); rxNostr.use(req).pipe().subscribe(); req.emit(faker.filters()); req.emit(faker.filters()); req.emit(faker.filters()); await expect(relay).toReceiveREQ("sub:0"); relay.emitEOSE("sub:0"); await expect(relay).toReceiveCLOSE("sub:0"); await expect(relay).toReceiveREQ("sub:1"); relay.emitEOSE("sub:1"); await expect(relay).toReceiveCLOSE("sub:1"); await expect(relay).toReceiveREQ("sub:2"); relay.emitEOSE("sub:2"); await expect(relay).toReceiveCLOSE("sub:2"); }); });
src/__test__/subscription.test.ts
penpenpng-rx-nostr-7c52614
[ { "filename": "src/__test__/sending.test.ts", "retrieved_chunk": " wasClean: true,\n });\n relay2.close({\n code: WebSocketCloseCode.DISPOSED_BY_RX_NOSTR,\n reason: \"Clean up on afterEach()\",\n wasClean: true,\n });\n relay3.close({\n code: WebSocketCloseCode.DISPOSED_BY_RX_NOSTR,\n reason: \"Clean up on afterEach()\",", "score": 30.366344790513864 }, { "filename": "src/__test__/sending.test.ts", "retrieved_chunk": " { url: RELAY_URL2, write: false, read: true },\n ]);\n await relay1.connected;\n await relay2.connected;\n });\n afterEach(() => {\n rxNostr.dispose();\n relay1.close({\n code: WebSocketCloseCode.DISPOSED_BY_RX_NOSTR,\n reason: \"Clean up on afterEach()\",", "score": 24.76975193263973 }, { "filename": "src/__test__/sending.test.ts", "retrieved_chunk": " await rxNostr.switchRelays([RELAY_URL1, RELAY_URL2]);\n rxNostr\n .send(faker.event(), { scope: [RELAY_URL2] })\n .pipe(spy.tap())\n .subscribe();\n await expect(relay2).toReceiveEVENT();\n expect(relay1.messagesToConsume.pendingItems.length).toBe(0);\n relay2.emitOK(\"*\", true);\n expect(spy.completed()).toBe(true);\n });", "score": 22.11473254375433 }, { "filename": "src/__test__/sending.test.ts", "retrieved_chunk": " wasClean: true,\n });\n });\n test(\"send() sends only to writable relays.\", async () => {\n rxNostr.send(faker.event());\n await expect(relay1).toReceiveEVENT();\n expect(relay2.messagesToConsume.pendingItems.length).toBe(0);\n });\n test(\"send() doesn't wait for OK from relays added later.\", async () => {\n const spy = spySub();", "score": 20.88674650686126 }, { "filename": "src/rx-nostr.ts", "retrieved_chunk": " typeof urlOrConfig === \"string\"\n ? {\n url: urlOrConfig,\n read: true,\n write: true,\n }\n : urlOrConfig;\n relay.url = normalizeRelayUrl(relay.url);\n return relay;\n });", "score": 20.036857979094375 } ]
typescript
: WebSocketCloseCode.DONT_RETRY, reason: "Relay's internal error, but should not retry.", wasClean: true, });
import Nostr from "nostr-typedef"; import { catchError, EMPTY, filter, finalize, first, identity, map, merge, mergeAll, mergeMap, type MonoTypeOperatorFunction, Observable, of, type OperatorFunction, ReplaySubject, Subject, Subscription, take, takeUntil, tap, timeout, type Unsubscribable, } from "rxjs"; import { BackoffConfig, Connection } from "./connection.js"; import { getSignedEvent } from "./nostr/event.js"; import { fetchRelayInfo } from "./nostr/nip11.js"; import { completeOnTimeout } from "./operator.js"; import type { ConnectionState, ConnectionStatePacket, ErrorPacket, EventPacket, LazyFilter, LazyREQ, MessagePacket, OkPacket, } from "./packet.js"; import type { RxReq } from "./req.js"; import { defineDefaultOptions, normalizeRelayUrl } from "./util.js"; /** * The core object of rx-nostr, which holds a connection to relays * and manages subscriptions as directed by the RxReq object connected by `use()`. * Use `createRxNostr()` to get the object. */ export interface RxNostr { /** * Return a list of relays used by this object. * The relay URLs are normalised so may not match the URLs set. */ getRelays(): RelayConfig[]; /** * Set the list of relays. * If a REQ subscription already exists, the same REQ is issued for the newly added relay * and CLOSE is sent for the removed relay. */ switchRelays(config: AcceptableRelaysConfig): Promise<void>; /** Utility wrapper for `switchRelays()`. */ addRelay(relay: string | RelayConfig): Promise<void>; /** Utility wrapper for `switchRelays()`. */ removeRelay(url: string): Promise<void>; /** Return true if the given relay is set to rxNostr. */ hasRelay(url: string): boolean; /** Return true if the given relay allows to be written. */ canWriteRelay(url: string): boolean; /** Return true if the given relay allows to be read. */ canReadRelay(url: string): boolean; /** Fetch all relays' info based on [NIP-11](https://github.com/nostr-protocol/nips/blob/master/11.md) */ fetchAllRelaysInfo(): Promise<Record<string, Nostr.Nip11.RelayInfo | null>>; /** * Return a dictionary in which you can look up connection state. * * **NOTE**: Keys are **normalized** URL, so may be different from one you set. */ getAllRelayState(): Record<string, ConnectionState>; /** * Return connection state of the given relay. * Throw if unknown URL is given. */ getRelayState(url: string): ConnectionState; /** * Attempt to reconnect the WebSocket if its state is `error` or `rejected`. * If not, do nothing. */ reconnect(url: string): void; // TODO: document /** * Set or unset a pipe to be applied to all EventPackets. */ setGlobalEventPacketPipe( pipe: MonoTypeOperatorFunction<EventPacket> | null ): void; /** * Associate RxReq with RxNostr. * When the associated RxReq is manipulated, * the RxNostr issues a new REQ to all relays allowed to be read. * The method returns an Observable that issues EventPackets * when an EVENT is received that is subscribed by RxReq. * You can unsubscribe the Observable to CLOSE. */ use( rxReq: RxReq, options?: Partial<RxNostrUseOptions> ): Observable<EventPacket>; /** * Create an Observable that receives all events (EVENT) from all websocket connections. * * Nothing happens when this Observable is unsubscribed. * */ createAllEventObservable(): Observable<EventPacket>; /** * Create an Observable that receives all errors from all websocket connections. * Note that an Observable is terminated when it receives any error, * so this method is the only way to receive errors arising from multiplexed websocket connections * (It means that Observables returned by `use()` never throw error). * * Nothing happens when this Observable is unsubscribed. * */ createAllErrorObservable(): Observable<ErrorPacket>; /** * Create an Observable that receives all messages from all websocket connections. * * Nothing happens when this Observable is unsubscribed. * */ createAllMessageObservable(): Observable<MessagePacket>; /** * Create an Observable that receives changing of WebSocket connection state. * * Nothing happens when this Observable is unsubscribed. */ createConnectionStateObservable(): Observable<ConnectionStatePacket>; /** * Attempt to send events to all relays that are allowed to write. * The `seckey` option accepts both nsec format and hex format, * and if omitted NIP-07 will be automatically used. */ send( params: Nostr.EventParameters, options?: RxNostrSendOptions ): Observable<OkPacket>; /** * Release all resources held by the RxNostr object. * Any Observable resulting from this RxNostr will be in the completed state * and will never receive messages again. * RxReq used by this object is not affected; in other words, if the RxReq is used * by another RxNostr, its use is not prevented. */ dispose(): void; } /** Create a RxNostr object. This is the only way to create that. */ export function createRxNostr(options?: Partial<RxNostrOptions>): RxNostr { return new RxNostrImpl(options); } export interface RxNostrOptions { /** Auto reconnection strategy. */ retry: BackoffConfig; /** * The time in milliseconds to timeout when following the backward strategy. * The observable is terminated when the specified amount of time has elapsed * during which no new events are available. */ timeout: number; globalRelayConfig?: { disableAutoFetchNip11Limitations?: boolean; maxConcurrentReqsFallback?: number; }; } const makeRxNostrOptions = defineDefaultOptions<RxNostrOptions>({ retry: { strategy: "exponential", maxCount: 5, initialDelay: 1000, }, timeout: 10000, globalRelayConfig: undefined, }); export interface RxNostrUseOptions { scope?: string[]; } const makeRxNostrUseOptions = defineDefaultOptions<RxNostrUseOptions>({ scope: undefined, }); export interface RxNostrSendOptions { scope?: string[]; seckey?: string; } const makeRxNostrSendOptions = defineDefaultOptions<RxNostrSendOptions>({ scope: undefined, seckey: undefined, }); /** Config object specifying WebSocket behavior. */ export interface RelayConfig { /** WebSocket endpoint URL. */ url: string; /** If true, rxNostr can publish REQ and subscribe EVENTs. */ read: boolean; /** If true, rxNostr can send EVENTs. */ write: boolean; disableAutoFetchNip11Limitations?: boolean; } /** Parameter of `rxNostr.switchRelays()` */ export type AcceptableRelaysConfig = | (string | RelayConfig)[] | Nostr.Nip07.GetRelayResult; class RxNostrImpl implements RxNostr { private options: RxNostrOptions; private connections: Map<string, Connection> = new Map(); private ongoings: Map<string, OngoingReq> = new Map(); private messageIn$: Subject<MessagePacket> = new Subject(); private error$: Subject<ErrorPacket> = new Subject(); private status$: Subject<ConnectionStatePacket> = new Subject(); private globalEventPacketPipe: MonoTypeOperatorFunction<EventPacket> | null = null; private disposed = false; private get messageOut$() { return this.messageIn$.pipe( mergeMap((packet) => { const pipe = this.globalEventPacketPipe; if (!pipe) { return of(packet); } const message = packet.message; if (message[0] !== "EVENT") { return of(packet); } return of({ from: packet.from, subId: message[1], event: message[2], }).pipe( pipe, map( ({ from, subId, event }): MessagePacket => ({ from, message: ["EVENT", subId, event], }) ) ); }) ); } constructor(options?: Partial<RxNostrOptions>) { const opt = makeRxNostrOptions(options); this.options = { ...opt, }; } getRelays(): RelayConfig[] { return Array.from(this.connections.values()).map( ({ url, read, write }) => ({ url, read, write, }) ); } private createConnection({ url, read, write, disableAutoFetchNip11Limitations, }: RelayConfig): Connection { const connection = new Connection(url, { backoff: this.options.retry, read, write, disableAutoFetchNip11Limitations: disableAutoFetchNip11Limitations ?? this.options.globalRelayConfig?.disableAutoFetchNip11Limitations, maxConcurrentReqsFallback: this.options.globalRelayConfig?.maxConcurrentReqsFallback, });
connection.getConnectionStateObservable().subscribe((state) => {
this.status$.next({ from: url, state, }); }); connection.getErrorObservable().subscribe((reason) => { this.error$.next({ from: url, reason }); }); connection .getMessageObservable() .pipe( catchError((reason: unknown) => { this.error$.next({ from: url, reason }); return EMPTY; }) ) .subscribe((v) => { this.messageIn$.next(v); }); return connection; } async switchRelays(config: AcceptableRelaysConfig): Promise<void> { const nextConns: Map<string, Connection> = new Map(); for (const { url, read, write } of normalizeRelaysConfig(config)) { // pop a connection if exists const prevConn = this.connections.get(url); this.connections.delete(url); if (prevConn) { prevConn.read = read; prevConn.write = write; nextConns.set(url, prevConn); } else { nextConns.set(url, this.createConnection({ url, read, write })); } } // connections that are no longer used for (const conn of this.connections.values()) { conn.dispose(); } const ensureConns: Promise<unknown>[] = []; for (const conn of nextConns.values()) { if (conn.read) { ensureConns.push(conn.start()); } else { conn.stop(); } } await Promise.all(ensureConns); this.connections = nextConns; // If disposed during switchRelay processing if (this.disposed) { for (const conn of this.connections.values()) { conn.dispose(); } return; } for (const { req, scope } of this.ongoings.values()) { this.ensureReq(req, { scope }); } // --- scoped untility pure functions --- function normalizeRelaysConfig( config: AcceptableRelaysConfig ): RelayConfig[] { if (Array.isArray(config)) { return config.map((urlOrConfig) => { const relay: RelayConfig = typeof urlOrConfig === "string" ? { url: urlOrConfig, read: true, write: true, } : urlOrConfig; relay.url = normalizeRelayUrl(relay.url); return relay; }); } else { return Object.entries(config).map(([url, flags]) => ({ url: normalizeRelayUrl(url), ...flags, })); } } } async addRelay(relay: string | RelayConfig): Promise<void> { await this.switchRelays([...this.getRelays(), relay]); } async removeRelay(url: string): Promise<void> { const u = normalizeRelayUrl(url); const currentRelays = this.getRelays(); const nextRelays = currentRelays.filter((relay) => relay.url !== u); if (currentRelays.length !== nextRelays.length) { await this.switchRelays(nextRelays); } } hasRelay(url: string): boolean { const u = normalizeRelayUrl(url); return this.getRelays().some((relay) => relay.url === u); } canWriteRelay(url: string): boolean { const u = normalizeRelayUrl(url); return this.getRelays().some((relay) => relay.url === u && relay.write); } canReadRelay(url: string): boolean { const u = normalizeRelayUrl(url); return this.getRelays().some((relay) => relay.url === u && relay.read); } async fetchAllRelaysInfo(): Promise< Record<string, Nostr.Nip11.RelayInfo | null> > { const entries = await Promise.all( Array.from(this.connections.keys()).map( async (url): Promise<[string, Nostr.Nip11.RelayInfo | null]> => [ url, await fetchRelayInfo(url).catch(() => null), ] ) ); return Object.fromEntries(entries); } getAllRelayState(): Record<string, ConnectionState> { return Object.fromEntries( Array.from(this.connections.values()).map((e) => [ e.url, this.getRelayState(e.url), ]) ); } getRelayState(url: string): ConnectionState { const conn = this.connections.get(normalizeRelayUrl(url)); if (!conn) { throw new Error("RelayConfig not found"); } // this.relays[url] may be set before this.relays[url].websocket is initialized return conn?.getConnectionState() ?? "not-started"; } reconnect(url: string): void { if (this.canReadRelay(url)) { this.connections.get(normalizeRelayUrl(url))?.start(); } } setGlobalEventPacketPipe(pipe: MonoTypeOperatorFunction<EventPacket> | null) { this.globalEventPacketPipe = pipe; } use( rxReq: RxReq, options?: Partial<RxNostrUseOptions> ): Observable<EventPacket> { const { scope: _scope } = makeRxNostrUseOptions(options); const scope = _scope?.map(normalizeRelayUrl); const TIMEOUT = this.options.timeout; const strategy = rxReq.strategy; const rxReqId = rxReq.rxReqId; const message$ = this.messageOut$; const ongoings = this.ongoings; const getAllRelayState = this.getAllRelayState.bind(this); const createConnectionStateObservable = this.createConnectionStateObservable.bind(this); const ensureReq = this.ensureReq.bind(this); const finalizeReq = this.finalizeReq.bind(this); const subId$ = rxReq.getReqObservable().pipe( filter((filters): filters is LazyFilter[] => filters !== null), strategy === "oneshot" ? first() : identity, attachSubId(), strategy === "forward" ? manageActiveForwardReq() : identity, tap((req) => { ensureReq(req, { overwrite: strategy === "forward", scope }); }), map(([, subId]) => subId) ); if (strategy === "forward") { const subId = makeSubId({ rxReqId, }); const resource: Unsubscribable[] = []; const subject = new Subject<EventPacket>(); resource.push(subject); return subject.pipe( tap({ subscribe: () => { resource.push(subId$.subscribe()); resource.push( message$ .pipe(filterBySubId(subId), pickEvents()) .subscribe((v) => { subject.next(v); }) ); }, finalize: () => { for (const r of resource) { r.unsubscribe(); } finalizeReq({ subId }); }, }) ); } else { return subId$.pipe(map(createEoseManagedEventObservable), mergeAll()); } function attachSubId(): OperatorFunction<LazyFilter[], LazyREQ> { const makeId = (index?: number) => makeSubId({ rxReqId, index }); switch (strategy) { case "backward": return map((filters, index) => ["REQ", makeId(index), ...filters]); case "forward": case "oneshot": return map((filters) => ["REQ", makeId(), ...filters]); } } function manageActiveForwardReq(): MonoTypeOperatorFunction<LazyREQ> { const recordActiveReq = (req: LazyREQ) => { const subId = req[1]; ongoings.set(subId, { req, scope, }); }; const forgetActiveReq = (subId: string) => { ongoings.delete(subId); }; return tap({ next: (req: LazyREQ) => { recordActiveReq(req); }, finalize: () => { forgetActiveReq( makeSubId({ rxReqId, }) ); }, }); } function createEoseManagedEventObservable( subId: string ): Observable<EventPacket> { const eose$ = new Subject<void>(); const complete$ = new Subject<void>(); const eoseRelays = new Set<string>(); const manageCompletion = merge( eose$, createConnectionStateObservable() ).subscribe(() => { const status = getAllRelayState(); const shouldComplete = Object.entries(status).every( ([url, state]) => (scope && !scope.includes(url)) || state === "error" || state === "terminated" || (state === "ongoing" && eoseRelays.has(url)) ); if (shouldComplete) { complete$.next(); } }); return message$.pipe( takeUntil(complete$), completeOnTimeout(TIMEOUT), filterBySubId(subId), filter((e) => !eoseRelays.has(e.from)), tap((e) => { if (e.message[0] === "EOSE") { eoseRelays.add(e.from); finalizeReq({ subId, url: e.from }); eose$.next(); } }), pickEvents(), finalize(() => { finalizeReq({ subId }); complete$.unsubscribe(); eose$.unsubscribe(); manageCompletion.unsubscribe(); }) ); } function filterBySubId( subId: string ): MonoTypeOperatorFunction<MessagePacket> { return filter( (packet) => (packet.message[0] === "EVENT" || packet.message[0] === "EOSE") && packet.message[1] === subId ); } function pickEvents(): OperatorFunction<MessagePacket, EventPacket> { return mergeMap(({ from, message }) => message[0] === "EVENT" ? of({ from, subId: message[1], event: message[2] }) : EMPTY ); } } createAllEventObservable(): Observable<EventPacket> { return this.messageOut$.pipe( mergeMap(({ from, message }) => message[0] === "EVENT" ? of({ from, subId: message[1], event: message[2] }) : EMPTY ) ); } createAllErrorObservable(): Observable<ErrorPacket> { return this.error$.asObservable(); } createAllMessageObservable(): Observable<MessagePacket> { return this.messageOut$; } createConnectionStateObservable(): Observable<ConnectionStatePacket> { return this.status$.asObservable(); } send( params: Nostr.EventParameters, options?: RxNostrSendOptions ): Observable<OkPacket> { const { seckey, scope: _scope } = makeRxNostrSendOptions(options); const scope = _scope?.map(normalizeRelayUrl); const writableConns = Array.from(this.connections.values()).filter( (conn) => (!scope || scope.includes(conn.url)) && conn.write ); const subject = new ReplaySubject<OkPacket>(writableConns.length); let subscription: Subscription | null = null; getSignedEvent(params, seckey).then((event) => { if (!subject.closed) { subscription = this.createAllMessageObservable().subscribe( ({ from, message }) => { if (message[0] !== "OK") { return; } subject.next({ from, id: event.id, ok: message[2], }); } ); } for (const conn of writableConns) { conn.sendEVENT(["EVENT", event]); } }); return subject.pipe( take(writableConns.length), timeout(30 * 1000), finalize(() => { subject.complete(); subject.unsubscribe(); subscription?.unsubscribe(); }) ); } dispose(): void { this.disposed = true; this.messageIn$.complete(); this.error$.complete(); for (const conn of this.connections.values()) { conn.dispose(); } } private ensureReq( req: LazyREQ, options?: { scope?: string[] | null; overwrite?: boolean } ) { const scope = options?.scope; for (const url of this.connections.keys()) { const conn = this.connections.get(url); if (!conn || !conn.read || (scope && !scope.includes(url))) { continue; } conn.ensureReq(req, { overwrite: options?.overwrite }); } } private finalizeReq(params: { subId: string; url?: string }) { const { subId, url } = params; if (subId === undefined && url === undefined) { throw new Error(); } if (url) { const conn = this.connections.get(url); conn?.finalizeReq(subId); } else { for (const conn of this.connections.values()) { conn?.finalizeReq(subId); } } } } interface OngoingReq { req: LazyREQ; scope?: string[]; } function makeSubId(params: { rxReqId: string; index?: number }): string { return `${params.rxReqId}:${params.index ?? 0}`; }
src/rx-nostr.ts
penpenpng-rx-nostr-7c52614
[ { "filename": "src/connection.ts", "retrieved_chunk": " read: boolean;\n write: boolean;\n disableAutoFetchNip11Limitations?: boolean;\n maxConcurrentReqsFallback?: number;\n}\nexport type BackoffConfig =\n | {\n // Exponential backoff and jitter strategy\n strategy: \"exponential\";\n maxCount: number;", "score": 35.627749287082 }, { "filename": "src/__test__/subscription.test.ts", "retrieved_chunk": " globalRelayConfig: {\n disableAutoFetchNip11Limitations: true,\n maxConcurrentReqsFallback: 1,\n },\n });\n await rxNostr.switchRelays([RELAY_URL]);\n await relay.connected;\n });\n afterEach(() => {\n rxNostr.dispose();", "score": 35.53960415329612 }, { "filename": "src/__test__/subscription.test.ts", "retrieved_chunk": " rxNostr = createRxNostr({\n globalRelayConfig: {\n disableAutoFetchNip11Limitations: true,\n },\n });\n await rxNostr.switchRelays([RELAY_URL1, RELAY_URL2]);\n await relay1.connected;\n await relay2.connected;\n });\n afterEach(() => {", "score": 23.982505290885094 }, { "filename": "src/connection.ts", "retrieved_chunk": " }\n getErrorObservable() {\n return this.error$.asObservable();\n }\n ensureReq(req: LazyREQ, options?: { overwrite?: boolean }) {\n const subId = req[1];\n if (this.connectionState === \"terminated\") {\n return;\n }\n if (!this.read) {", "score": 22.90950563394145 }, { "filename": "src/connection.ts", "retrieved_chunk": " this.config.disableAutoFetchNip11Limitations ||\n this.serverLimitations\n ) {\n return;\n }\n try {\n const info = await fetchRelayInfo(this.url);\n this.serverLimitations = info.limitation ?? null;\n } catch {\n // do nothing", "score": 21.38024315652448 } ]
typescript
connection.getConnectionStateObservable().subscribe((state) => {
import Nostr from "nostr-typedef"; import { type MonoTypeOperatorFunction, tap } from "rxjs"; import { TestScheduler } from "rxjs/testing"; import { expect } from "vitest"; import { createClientSpy, faker as _faker } from "vitest-nostr"; import { EventPacket, MessagePacket } from "../packet.js"; export function testScheduler() { return new TestScheduler((a, b) => expect(a).toEqual(b)); } export const faker = { ..._faker, eventPacket( packetOrEvent?: Partial< EventPacket["event"] & Omit<EventPacket, "event"> & { event?: Partial<EventPacket["event"]>; } > ): EventPacket { return { from: packetOrEvent?.from ?? "*", subId: packetOrEvent?.subId ?? "*", event: faker.event(packetOrEvent?.event ?? packetOrEvent), }; }, messagePacket(message: Nostr.ToClientMessage.Any): MessagePacket { return { from: "*", message, }; }, }; export function spySub(): { completed: () => boolean; error: () => boolean; count: () => number; subscribed: () => boolean; unsubscribed: () => boolean; tap: <T>() => MonoTypeOperatorFunction<T>; } { let completed = false; let error = false; let count = 0; let subscribed = false; let unsubscribed = false; return { completed: () => completed, error: () => error, count: () => count, subscribed: () => subscribed, unsubscribed: () => unsubscribed, tap: () => tap({ complete: () => void (completed = true), error: () => void (error = true), next: () => void count++, subscribe: () => void (subscribed = true), unsubscribe: () => void (unsubscribed = true), }), }; } export function spyMessage(): { tap: () => MonoTypeOperatorFunction<MessagePacket>; } { let tapNext: (message: Nostr.ToClientMessage.Any) => void; const spy = createClientSpy((listener) => { tapNext = listener; }); return { tap: () => tap((packet) => { tapNext(packet.message); }), ...spy, }; } export function spyEvent(): { tap: () => MonoTypeOperatorFunction<EventPacket>; } { let tapNext: (message: Nostr.ToClientMessage.Any) => void; const spy = createClientSpy((listener) => { tapNext = listener; }); return { tap: () => tap((packet) => {
tapNext(["EVENT", packet.subId, packet.event]);
}), ...spy, }; }
src/__test__/helper.ts
penpenpng-rx-nostr-7c52614
[ { "filename": "src/rx-nostr.ts", "retrieved_chunk": " }\n function filterBySubId(\n subId: string\n ): MonoTypeOperatorFunction<MessagePacket> {\n return filter(\n (packet) =>\n (packet.message[0] === \"EVENT\" || packet.message[0] === \"EOSE\") &&\n packet.message[1] === subId\n );\n }", "score": 26.949558384610793 }, { "filename": "src/rx-nostr.ts", "retrieved_chunk": " return of(packet);\n }\n const message = packet.message;\n if (message[0] !== \"EVENT\") {\n return of(packet);\n }\n return of({\n from: packet.from,\n subId: message[1],\n event: message[2],", "score": 25.0312411728605 }, { "filename": "src/operator.ts", "retrieved_chunk": " type: T\n): OperatorFunction<\n MessagePacket,\n MessagePacket<Nostr.ToClientMessage.Message<T>>\n> {\n return filter(\n (packet): packet is MessagePacket<Nostr.ToClientMessage.Message<T>> =>\n packet.message[0] === type\n );\n}", "score": 24.50566915763818 }, { "filename": "src/connection.ts", "retrieved_chunk": " }),\n rejectFilterUnmatchEvents()\n );\n function rejectFilterUnmatchEvents(): MonoTypeOperatorFunction<MessagePacket> {\n return filter((packet) => {\n const [type, subId, event] = packet.message;\n if (type !== \"EVENT\") {\n return true;\n }\n const req = reqs.get(subId);", "score": 24.46306352913328 }, { "filename": "src/operator.ts", "retrieved_chunk": "}\n/**\n * Only the latest events are allowed to pass.\n */\nexport function latest(): MonoTypeOperatorFunction<EventPacket> {\n return pipe(\n scan<EventPacket>((acc, packet) =>\n compareEvents(acc.event, packet.event) < 0 ? packet : acc\n ),\n distinctUntilChanged(", "score": 20.506627519245654 } ]
typescript
tapNext(["EVENT", packet.subId, packet.event]);
import Nostr from "nostr-typedef"; import { BehaviorSubject, Observable, type OperatorFunction } from "rxjs"; import { LazyFilter, ReqPacket } from "./packet.js"; import type { Override } from "./util.js"; /** * The RxReq interface that is provided for RxNostr (**not for users**). */ export interface RxReq<S extends RxReqStrategy = RxReqStrategy> { /** @internal User should not use this directly.The RxReq strategy. It is read-only and must not change. */ get strategy(): S; /** @internal User should not use this directly. Used to construct subId. */ get rxReqId(): string; /** @internal User should not use this directly. Get an Observable of ReqPacket. */ getReqObservable(): Observable<ReqPacket>; } /** * REQ strategy. * * See comments on `createRxForwardReq()`, `createRxBackwardReq()` and `createRxOneshotReq() */ export type RxReqStrategy = "forward" | "backward" | "oneshot"; /** * The RxReq interface that is provided for users (not for RxNostr). */ export interface RxReqController { /** Start new REQ or stop REQ on the RxNostr with witch the RxReq is associated. */ emit(filters: LazyFilter | LazyFilter[] | null): void; /** * Returns itself overriding only `getReqObservable()`. * It is useful for throttling and other control purposes. */ pipe(): RxReq; pipe(op1: OperatorFunction<ReqPacket, ReqPacket>): RxReq; pipe<A>( op1: OperatorFunction<ReqPacket, A>, op2: OperatorFunction<A, ReqPacket> ): RxReq; pipe<A, B>( op1: OperatorFunction<ReqPacket, A>, op2: OperatorFunction<A, B>, op3: OperatorFunction<B, ReqPacket> ): RxReq; pipe<A, B, C>( op1: OperatorFunction<ReqPacket, A>, op2: OperatorFunction<A, B>, op3: OperatorFunction<B, C>, op4: OperatorFunction<C, ReqPacket> ): RxReq; pipe<A, B, C, D>( op1: OperatorFunction<ReqPacket, A>, op2: OperatorFunction<A, B>, op3: OperatorFunction<B, C>, op4: OperatorFunction<C, D>, op5: OperatorFunction<D, ReqPacket> ): RxReq; } abstract class RxReqBase implements RxReq { protected filters$ = new BehaviorSubject<ReqPacket>(null); private _rxReqId: string; abstract get strategy(): RxReqStrategy; get rxReqId() { return this._rxReqId; } constructor(rxReqId?: string) { this._rxReqId = rxReqId ?? getRandomDigitsString(); } getReqObservable(): Observable<ReqPacket> { return this.filters$.asObservable(); } emit(filters: LazyFilter | LazyFilter[] | null) { const normalized = normalizeFilters(filters); if (normalized) { this.filters$.next(normalized); } else { this.filters$.next(null); } } pipe(): RxReq; pipe(op1: OperatorFunction<ReqPacket, ReqPacket>): RxReq; pipe<A>( op1: OperatorFunction<ReqPacket, A>, op2: OperatorFunction<A, ReqPacket> ): RxReq; pipe<A, B>( op1: OperatorFunction<ReqPacket, A>, op2: OperatorFunction<A, B>, op3: OperatorFunction<B, ReqPacket> ): RxReq; pipe<A, B, C>( op1: OperatorFunction<ReqPacket, A>, op2: OperatorFunction<A, B>, op3: OperatorFunction<B, C>, op4: OperatorFunction<C, ReqPacket> ): RxReq; pipe<A, B, C, D>( op1: OperatorFunction<ReqPacket, A>, op2: OperatorFunction<A, B>, op3: OperatorFunction<B, C>, op4: OperatorFunction<C, D>, op5: OperatorFunction<D, ReqPacket> ): RxReq; // eslint-disable-next-line @typescript-eslint/no-explicit-any pipe(...operators: OperatorFunction<any, any>[]): RxReq { const rxReqId = this.rxReqId; const strategy = this.strategy; return { ...this, get rxReqId() { return rxReqId; }, get strategy() { return strategy; }, getReqObservable: () => this.getReqObservable().pipe(...(operators as [])), }; } } /** * Create a RxReq instance based on the backward strategy. * It is useful if you want to retrieve past events that have already been published. * * In backward strategy: * - All REQs have different subIds. * - All REQ-subscriptions keep alive until timeout or getting EOSE. * - In most cases, you should specify `until` or `limit` for filters. * * For more information, see [document](https://penpenpng.github.io/rx-nostr/docs/req-strategy.html#backward-strategy). */ export function createRxBackwardReq( subIdBase?: string ): RxReq<"backward"> & RxReqController { return new RxBackwardReq(subIdBase); } class RxBackwardReq extends RxReqBase implements RxReqController { constructor(rxReqId?: string) { super(rxReqId); } override get strategy(): "backward" { return "backward"; } } /** * Create a RxReq instance based on the forward strategy. * It is useful if you want to listen future events. * * In forward strategy: * - All REQs have the same subId. * - When a new REQ is issued, the old REQ is overwritten and terminated immediately. * The latest REQ keeps alive until it is overwritten or explicitly terminated. * - In most cases, you should not specify `limit` for filters. * * For more information, see [document](https://penpenpng.github.io/rx-nostr/docs/req-strategy.html#forward-strategy). */ export function createRxForwardReq( subId?: string ): RxReq<"forward"> & RxReqController { return new RxForwardReq(subId); } class RxForwardReq extends RxReqBase implements RxReqController { constructor(rxReqId?: string) { super(rxReqId); } override get strategy(): "forward" { return "forward"; } } /** * Create a RxReq instance based on the oneshot strategy. * It is almost the same as backward strategy, however can publish only one REQ * and the Observable completes on EOSE. * * For more information, see [document](https://penpenpng.github.io/rx-nostr/docs/req-strategy.html#oneshot-strategy). */ export function createRxOneshotReq(req: { filters: LazyFilter | LazyFilter[]; subId?: string; }): RxReq<"oneshot"> { return new RxOneshotReq(req); } class RxOneshotReq extends RxReqBase { constructor(req: { filters: LazyFilter | LazyFilter[]; subId?: string }) { super(req?.subId); this.emit(req.filters); } override get strategy(): "oneshot" { return "oneshot"; } } export interface Mixin<R, T> { (): ThisType<R> & T; } /** NOTE: unstable feature */ export function mixin<R extends object, T extends object>( def: () => ThisType<R> & T ): Mixin<R, T> { return def; } /** NOTE: unstable feature */ export function extend<B extends R, R extends object, T extends object>( base: B, mixin: Mixin<R, T>
): Override<B, T> {
return Object.assign(base, mixin()) as Override<B, T>; } function getRandomDigitsString() { return `${Math.floor(Math.random() * 1000000)}`; } function normalizeFilter(filter: LazyFilter): LazyFilter | null { const res: LazyFilter = {}; const isTagName = (s: string): s is Nostr.TagName => /^#[a-zA-Z]$/.test(s); for (const key of Object.keys(filter)) { if (key === "limit" && (filter[key] ?? -1) >= 0) { res[key] = filter[key]; continue; } if (key === "since" || key === "until") { const f = filter[key]; if (typeof f !== "number" || (f ?? -1) >= 0) { res[key] = f; continue; } } if ( (isTagName(key) || key === "ids" || key === "authors") && filter[key] !== undefined && (filter[key]?.length ?? -1) > 0 ) { res[key] = filter[key]; continue; } if ( key === "kinds" && filter[key] !== undefined && (filter[key]?.length ?? -1) > 0 ) { res[key] = filter[key]; continue; } if (key === "search" && filter[key] !== undefined) { res[key] = filter[key]; continue; } } const timeRangeIsValid = typeof res.since !== "number" || typeof res.until !== "number" || res.since <= res.until; if (!timeRangeIsValid) { return null; } if (Object.keys(res).length <= 0) { return null; } return res; } function normalizeFilters( filters: LazyFilter | LazyFilter[] | null ): LazyFilter[] | null { if (!filters) { return null; } const normalized = (Array.isArray(filters) ? filters : [filters]).flatMap( (e) => normalizeFilter(e) ?? [] ); return normalized.length > 0 ? normalized : null; }
src/req.ts
penpenpng-rx-nostr-7c52614
[ { "filename": "src/util.ts", "retrieved_chunk": " ])\n ) as T;\n}\nexport type Override<T extends object, U extends object> = {\n [K in keyof T | keyof U]: K extends keyof U\n ? U[K]\n : K extends keyof T\n ? T[K]\n : never;\n};", "score": 93.80677242429344 }, { "filename": "src/util.ts", "retrieved_chunk": "import normalizeUrl from \"normalize-url\";\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport function defineDefaultOptions<T extends Record<string, any>>(\n defaultParams: T\n): (givenParams?: Partial<T>) => T {\n return (givenParams) =>\n Object.fromEntries(\n Object.keys(defaultParams).map((key) => [\n key,\n givenParams?.[key] ?? defaultParams[key],", "score": 51.229029599265445 }, { "filename": "src/operator.ts", "retrieved_chunk": "): MonoTypeOperatorFunction<EventPacket> {\n return sort(\n bufferTime,\n compareFn ?? ((a, b) => compareEvents(a.event, b.event))\n );\n}\n// ----------------------- //\n// MessagePacket operators //\n// ----------------------- //\nexport function filterType<T extends Nostr.ToClientMessage.Type>(", "score": 47.43273114198813 }, { "filename": "src/operator.ts", "retrieved_chunk": " */\nexport function sort<T>(\n bufferTime: number,\n compareFn: (a: T, b: T) => number\n): MonoTypeOperatorFunction<T> {\n const buffer: T[] = [];\n return pipe(\n tap((v) => {\n buffer.push(v);\n buffer.sort(compareFn);", "score": 41.68555484523767 }, { "filename": "src/__test__/helper.ts", "retrieved_chunk": " },\n};\nexport function spySub(): {\n completed: () => boolean;\n error: () => boolean;\n count: () => number;\n subscribed: () => boolean;\n unsubscribed: () => boolean;\n tap: <T>() => MonoTypeOperatorFunction<T>;\n} {", "score": 37.01994119751296 } ]
typescript
): Override<B, T> {
import Nostr from "nostr-typedef"; import { catchError, EMPTY, filter, finalize, first, identity, map, merge, mergeAll, mergeMap, type MonoTypeOperatorFunction, Observable, of, type OperatorFunction, ReplaySubject, Subject, Subscription, take, takeUntil, tap, timeout, type Unsubscribable, } from "rxjs"; import { BackoffConfig, Connection } from "./connection.js"; import { getSignedEvent } from "./nostr/event.js"; import { fetchRelayInfo } from "./nostr/nip11.js"; import { completeOnTimeout } from "./operator.js"; import type { ConnectionState, ConnectionStatePacket, ErrorPacket, EventPacket, LazyFilter, LazyREQ, MessagePacket, OkPacket, } from "./packet.js"; import type { RxReq } from "./req.js"; import { defineDefaultOptions, normalizeRelayUrl } from "./util.js"; /** * The core object of rx-nostr, which holds a connection to relays * and manages subscriptions as directed by the RxReq object connected by `use()`. * Use `createRxNostr()` to get the object. */ export interface RxNostr { /** * Return a list of relays used by this object. * The relay URLs are normalised so may not match the URLs set. */ getRelays(): RelayConfig[]; /** * Set the list of relays. * If a REQ subscription already exists, the same REQ is issued for the newly added relay * and CLOSE is sent for the removed relay. */ switchRelays(config: AcceptableRelaysConfig): Promise<void>; /** Utility wrapper for `switchRelays()`. */ addRelay(relay: string | RelayConfig): Promise<void>; /** Utility wrapper for `switchRelays()`. */ removeRelay(url: string): Promise<void>; /** Return true if the given relay is set to rxNostr. */ hasRelay(url: string): boolean; /** Return true if the given relay allows to be written. */ canWriteRelay(url: string): boolean; /** Return true if the given relay allows to be read. */ canReadRelay(url: string): boolean; /** Fetch all relays' info based on [NIP-11](https://github.com/nostr-protocol/nips/blob/master/11.md) */ fetchAllRelaysInfo(): Promise<Record<string, Nostr.Nip11.RelayInfo | null>>; /** * Return a dictionary in which you can look up connection state. * * **NOTE**: Keys are **normalized** URL, so may be different from one you set. */ getAllRelayState(): Record<string, ConnectionState>; /** * Return connection state of the given relay. * Throw if unknown URL is given. */ getRelayState(url: string): ConnectionState; /** * Attempt to reconnect the WebSocket if its state is `error` or `rejected`. * If not, do nothing. */ reconnect(url: string): void; // TODO: document /** * Set or unset a pipe to be applied to all EventPackets. */ setGlobalEventPacketPipe( pipe: MonoTypeOperatorFunction<EventPacket> | null ): void; /** * Associate RxReq with RxNostr. * When the associated RxReq is manipulated, * the RxNostr issues a new REQ to all relays allowed to be read. * The method returns an Observable that issues EventPackets * when an EVENT is received that is subscribed by RxReq. * You can unsubscribe the Observable to CLOSE. */ use( rxReq: RxReq, options?: Partial<RxNostrUseOptions> ): Observable<EventPacket>; /** * Create an Observable that receives all events (EVENT) from all websocket connections. * * Nothing happens when this Observable is unsubscribed. * */ createAllEventObservable(): Observable<EventPacket>; /** * Create an Observable that receives all errors from all websocket connections. * Note that an Observable is terminated when it receives any error, * so this method is the only way to receive errors arising from multiplexed websocket connections * (It means that Observables returned by `use()` never throw error). * * Nothing happens when this Observable is unsubscribed. * */ createAllErrorObservable(): Observable<ErrorPacket>; /** * Create an Observable that receives all messages from all websocket connections. * * Nothing happens when this Observable is unsubscribed. * */ createAllMessageObservable(): Observable<MessagePacket>; /** * Create an Observable that receives changing of WebSocket connection state. * * Nothing happens when this Observable is unsubscribed. */ createConnectionStateObservable(): Observable<ConnectionStatePacket>; /** * Attempt to send events to all relays that are allowed to write. * The `seckey` option accepts both nsec format and hex format, * and if omitted NIP-07 will be automatically used. */ send( params: Nostr.EventParameters, options?: RxNostrSendOptions ): Observable<OkPacket>; /** * Release all resources held by the RxNostr object. * Any Observable resulting from this RxNostr will be in the completed state * and will never receive messages again. * RxReq used by this object is not affected; in other words, if the RxReq is used * by another RxNostr, its use is not prevented. */ dispose(): void; } /** Create a RxNostr object. This is the only way to create that. */ export function createRxNostr(options?: Partial<RxNostrOptions>): RxNostr { return new RxNostrImpl(options); } export interface RxNostrOptions { /** Auto reconnection strategy. */ retry: BackoffConfig; /** * The time in milliseconds to timeout when following the backward strategy. * The observable is terminated when the specified amount of time has elapsed * during which no new events are available. */ timeout: number; globalRelayConfig?: { disableAutoFetchNip11Limitations?: boolean; maxConcurrentReqsFallback?: number; }; } const makeRxNostrOptions = defineDefaultOptions<RxNostrOptions>({ retry: { strategy: "exponential", maxCount: 5, initialDelay: 1000, }, timeout: 10000, globalRelayConfig: undefined, }); export interface RxNostrUseOptions { scope?: string[]; } const makeRxNostrUseOptions = defineDefaultOptions<RxNostrUseOptions>({ scope: undefined, }); export interface RxNostrSendOptions { scope?: string[]; seckey?: string; } const makeRxNostrSendOptions = defineDefaultOptions<RxNostrSendOptions>({ scope: undefined, seckey: undefined, }); /** Config object specifying WebSocket behavior. */ export interface RelayConfig { /** WebSocket endpoint URL. */ url: string; /** If true, rxNostr can publish REQ and subscribe EVENTs. */ read: boolean; /** If true, rxNostr can send EVENTs. */ write: boolean; disableAutoFetchNip11Limitations?: boolean; } /** Parameter of `rxNostr.switchRelays()` */ export type AcceptableRelaysConfig = | (string | RelayConfig)[] | Nostr.Nip07.GetRelayResult; class RxNostrImpl implements RxNostr { private options: RxNostrOptions; private connections: Map<string, Connection> = new Map(); private ongoings: Map<string, OngoingReq> = new Map(); private messageIn$: Subject<MessagePacket> = new Subject(); private error$: Subject<ErrorPacket> = new Subject(); private status$: Subject<ConnectionStatePacket> = new Subject(); private globalEventPacketPipe: MonoTypeOperatorFunction<EventPacket> | null = null; private disposed = false; private get messageOut$() { return this.messageIn$.pipe( mergeMap((packet) => { const pipe = this.globalEventPacketPipe; if (!pipe) { return of(packet); } const message = packet.message; if (message[0] !== "EVENT") { return of(packet); } return of({ from: packet.from, subId: message[1], event: message[2], }).pipe( pipe, map( ({ from, subId, event }): MessagePacket => ({ from, message: ["EVENT", subId, event], }) ) ); }) ); } constructor(options?: Partial<RxNostrOptions>) { const opt = makeRxNostrOptions(options); this.options = { ...opt, }; } getRelays(): RelayConfig[] { return Array.from(this.connections.values()).map( ({ url, read, write }) => ({ url, read, write, }) ); } private createConnection({ url, read, write, disableAutoFetchNip11Limitations, }: RelayConfig): Connection { const connection = new Connection(url, { backoff: this.options.retry, read, write, disableAutoFetchNip11Limitations: disableAutoFetchNip11Limitations ?? this.options.globalRelayConfig?.disableAutoFetchNip11Limitations, maxConcurrentReqsFallback: this.options.globalRelayConfig?.maxConcurrentReqsFallback, }); connection.getConnectionStateObservable().subscribe((state) => { this.status$.next({ from: url, state, }); }); connection.getErrorObservable().subscribe((reason) => { this.error$.next({ from: url, reason }); }); connection .getMessageObservable() .pipe( catchError((reason: unknown) => { this.error$.next({ from: url, reason }); return EMPTY; }) ) .subscribe((v) => { this.messageIn$.next(v); }); return connection; } async switchRelays(config: AcceptableRelaysConfig): Promise<void> { const nextConns: Map<string, Connection> = new Map(); for (const { url, read, write } of normalizeRelaysConfig(config)) { // pop a connection if exists const prevConn = this.connections.get(url); this.connections.delete(url); if (prevConn) { prevConn.read = read; prevConn.write = write; nextConns.set(url, prevConn); } else { nextConns.set(url, this.createConnection({ url, read, write })); } } // connections that are no longer used for (const conn of this.connections.values()) { conn.dispose(); } const ensureConns: Promise<unknown>[] = []; for (const conn of nextConns.values()) { if (conn.read) { ensureConns.push(conn.start()); } else { conn.stop(); } } await Promise.all(ensureConns); this.connections = nextConns; // If disposed during switchRelay processing if (this.disposed) { for (const conn of this.connections.values()) { conn.dispose(); } return; } for (const { req, scope } of this.ongoings.values()) { this.ensureReq(req, { scope }); } // --- scoped untility pure functions --- function normalizeRelaysConfig( config: AcceptableRelaysConfig ): RelayConfig[] { if (Array.isArray(config)) { return config.map((urlOrConfig) => { const relay: RelayConfig = typeof urlOrConfig === "string" ? { url: urlOrConfig, read: true, write: true, } : urlOrConfig; relay.url = normalizeRelayUrl(relay.url); return relay; }); } else { return Object.entries(config).map(([url, flags]) => ({ url: normalizeRelayUrl(url), ...flags, })); } } } async addRelay(relay: string | RelayConfig): Promise<void> { await this.switchRelays([...this.getRelays(), relay]); } async removeRelay(url: string): Promise<void> { const u = normalizeRelayUrl(url); const currentRelays = this.getRelays(); const nextRelays = currentRelays.filter((relay) => relay.url !== u); if (currentRelays.length !== nextRelays.length) { await this.switchRelays(nextRelays); } } hasRelay(url: string): boolean { const u = normalizeRelayUrl(url); return this.getRelays().some((relay) => relay.url === u); } canWriteRelay(url: string): boolean { const u = normalizeRelayUrl(url); return this.getRelays().some((relay) => relay.url === u && relay.write); } canReadRelay(url: string): boolean { const u = normalizeRelayUrl(url); return this.getRelays().some((relay) => relay.url === u && relay.read); } async fetchAllRelaysInfo(): Promise< Record<string, Nostr.Nip11.RelayInfo | null> > { const entries = await Promise.all( Array.from(this.connections.keys()).map( async (url): Promise<[string, Nostr.Nip11.RelayInfo | null]> => [ url, await fetchRelayInfo(url).catch(() => null), ] ) ); return Object.fromEntries(entries); } getAllRelayState(): Record<string, ConnectionState> { return Object.fromEntries( Array.from(this.connections.values()).map((e) => [ e.url, this.getRelayState(e.url), ]) ); } getRelayState(url: string): ConnectionState { const conn = this.connections.get(normalizeRelayUrl(url)); if (!conn) { throw new Error("RelayConfig not found"); } // this.relays[url] may be set before this.relays[url].websocket is initialized return conn?.getConnectionState() ?? "not-started"; } reconnect(url: string): void { if (this.canReadRelay(url)) { this.connections.get(normalizeRelayUrl(url))?.start(); } } setGlobalEventPacketPipe(pipe: MonoTypeOperatorFunction<EventPacket> | null) { this.globalEventPacketPipe = pipe; } use( rxReq: RxReq, options?: Partial<RxNostrUseOptions> ): Observable<EventPacket> { const { scope: _scope } = makeRxNostrUseOptions(options); const scope = _scope?.map(normalizeRelayUrl); const TIMEOUT = this.options.timeout; const strategy = rxReq.strategy; const rxReqId = rxReq.rxReqId; const message$ = this.messageOut$; const ongoings = this.ongoings; const getAllRelayState = this.getAllRelayState.bind(this); const createConnectionStateObservable = this.createConnectionStateObservable.bind(this); const ensureReq = this.ensureReq.bind(this); const finalizeReq = this.finalizeReq.bind(this); const subId$ = rxReq.getReqObservable().pipe( filter((filters): filters is LazyFilter[] => filters !== null), strategy === "oneshot" ? first() : identity, attachSubId(), strategy === "forward" ? manageActiveForwardReq() : identity, tap((req) => {
ensureReq(req, { overwrite: strategy === "forward", scope });
}), map(([, subId]) => subId) ); if (strategy === "forward") { const subId = makeSubId({ rxReqId, }); const resource: Unsubscribable[] = []; const subject = new Subject<EventPacket>(); resource.push(subject); return subject.pipe( tap({ subscribe: () => { resource.push(subId$.subscribe()); resource.push( message$ .pipe(filterBySubId(subId), pickEvents()) .subscribe((v) => { subject.next(v); }) ); }, finalize: () => { for (const r of resource) { r.unsubscribe(); } finalizeReq({ subId }); }, }) ); } else { return subId$.pipe(map(createEoseManagedEventObservable), mergeAll()); } function attachSubId(): OperatorFunction<LazyFilter[], LazyREQ> { const makeId = (index?: number) => makeSubId({ rxReqId, index }); switch (strategy) { case "backward": return map((filters, index) => ["REQ", makeId(index), ...filters]); case "forward": case "oneshot": return map((filters) => ["REQ", makeId(), ...filters]); } } function manageActiveForwardReq(): MonoTypeOperatorFunction<LazyREQ> { const recordActiveReq = (req: LazyREQ) => { const subId = req[1]; ongoings.set(subId, { req, scope, }); }; const forgetActiveReq = (subId: string) => { ongoings.delete(subId); }; return tap({ next: (req: LazyREQ) => { recordActiveReq(req); }, finalize: () => { forgetActiveReq( makeSubId({ rxReqId, }) ); }, }); } function createEoseManagedEventObservable( subId: string ): Observable<EventPacket> { const eose$ = new Subject<void>(); const complete$ = new Subject<void>(); const eoseRelays = new Set<string>(); const manageCompletion = merge( eose$, createConnectionStateObservable() ).subscribe(() => { const status = getAllRelayState(); const shouldComplete = Object.entries(status).every( ([url, state]) => (scope && !scope.includes(url)) || state === "error" || state === "terminated" || (state === "ongoing" && eoseRelays.has(url)) ); if (shouldComplete) { complete$.next(); } }); return message$.pipe( takeUntil(complete$), completeOnTimeout(TIMEOUT), filterBySubId(subId), filter((e) => !eoseRelays.has(e.from)), tap((e) => { if (e.message[0] === "EOSE") { eoseRelays.add(e.from); finalizeReq({ subId, url: e.from }); eose$.next(); } }), pickEvents(), finalize(() => { finalizeReq({ subId }); complete$.unsubscribe(); eose$.unsubscribe(); manageCompletion.unsubscribe(); }) ); } function filterBySubId( subId: string ): MonoTypeOperatorFunction<MessagePacket> { return filter( (packet) => (packet.message[0] === "EVENT" || packet.message[0] === "EOSE") && packet.message[1] === subId ); } function pickEvents(): OperatorFunction<MessagePacket, EventPacket> { return mergeMap(({ from, message }) => message[0] === "EVENT" ? of({ from, subId: message[1], event: message[2] }) : EMPTY ); } } createAllEventObservable(): Observable<EventPacket> { return this.messageOut$.pipe( mergeMap(({ from, message }) => message[0] === "EVENT" ? of({ from, subId: message[1], event: message[2] }) : EMPTY ) ); } createAllErrorObservable(): Observable<ErrorPacket> { return this.error$.asObservable(); } createAllMessageObservable(): Observable<MessagePacket> { return this.messageOut$; } createConnectionStateObservable(): Observable<ConnectionStatePacket> { return this.status$.asObservable(); } send( params: Nostr.EventParameters, options?: RxNostrSendOptions ): Observable<OkPacket> { const { seckey, scope: _scope } = makeRxNostrSendOptions(options); const scope = _scope?.map(normalizeRelayUrl); const writableConns = Array.from(this.connections.values()).filter( (conn) => (!scope || scope.includes(conn.url)) && conn.write ); const subject = new ReplaySubject<OkPacket>(writableConns.length); let subscription: Subscription | null = null; getSignedEvent(params, seckey).then((event) => { if (!subject.closed) { subscription = this.createAllMessageObservable().subscribe( ({ from, message }) => { if (message[0] !== "OK") { return; } subject.next({ from, id: event.id, ok: message[2], }); } ); } for (const conn of writableConns) { conn.sendEVENT(["EVENT", event]); } }); return subject.pipe( take(writableConns.length), timeout(30 * 1000), finalize(() => { subject.complete(); subject.unsubscribe(); subscription?.unsubscribe(); }) ); } dispose(): void { this.disposed = true; this.messageIn$.complete(); this.error$.complete(); for (const conn of this.connections.values()) { conn.dispose(); } } private ensureReq( req: LazyREQ, options?: { scope?: string[] | null; overwrite?: boolean } ) { const scope = options?.scope; for (const url of this.connections.keys()) { const conn = this.connections.get(url); if (!conn || !conn.read || (scope && !scope.includes(url))) { continue; } conn.ensureReq(req, { overwrite: options?.overwrite }); } } private finalizeReq(params: { subId: string; url?: string }) { const { subId, url } = params; if (subId === undefined && url === undefined) { throw new Error(); } if (url) { const conn = this.connections.get(url); conn?.finalizeReq(subId); } else { for (const conn of this.connections.values()) { conn?.finalizeReq(subId); } } } } interface OngoingReq { req: LazyREQ; scope?: string[]; } function makeSubId(params: { rxReqId: string; index?: number }): string { return `${params.rxReqId}:${params.index ?? 0}`; }
src/rx-nostr.ts
penpenpng-rx-nostr-7c52614
[ { "filename": "src/connection.ts", "retrieved_chunk": " }\n getErrorObservable() {\n return this.error$.asObservable();\n }\n ensureReq(req: LazyREQ, options?: { overwrite?: boolean }) {\n const subId = req[1];\n if (this.connectionState === \"terminated\") {\n return;\n }\n if (!this.read) {", "score": 42.91939392050059 }, { "filename": "src/req.ts", "retrieved_chunk": "class RxOneshotReq extends RxReqBase {\n constructor(req: { filters: LazyFilter | LazyFilter[]; subId?: string }) {\n super(req?.subId);\n this.emit(req.filters);\n }\n override get strategy(): \"oneshot\" {\n return \"oneshot\";\n }\n}\nexport interface Mixin<R, T> {", "score": 38.17830286597062 }, { "filename": "src/connection.ts", "retrieved_chunk": " }\n private ensureReqs() {\n const reqs = Array.from(this.reqs.values()).slice(\n 0,\n this.maxConcurrentReqs ?? undefined\n );\n for (const req of reqs) {\n this.ensureReq(req.original);\n }\n }", "score": 36.244021983431374 }, { "filename": "src/connection.ts", "retrieved_chunk": " finalizeReq(subId: string) {\n if (this.connectionState === \"terminated\") {\n return;\n }\n const req = this.reqs.get(subId);\n if (!req) {\n return;\n }\n this.reqs.delete(subId);\n if (req.isOngoing) {", "score": 35.91753164513862 }, { "filename": "src/req.ts", "retrieved_chunk": " get rxReqId() {\n return this._rxReqId;\n }\n constructor(rxReqId?: string) {\n this._rxReqId = rxReqId ?? getRandomDigitsString();\n }\n getReqObservable(): Observable<ReqPacket> {\n return this.filters$.asObservable();\n }\n emit(filters: LazyFilter | LazyFilter[] | null) {", "score": 32.799606436975466 } ]
typescript
ensureReq(req, { overwrite: strategy === "forward", scope });
import { afterEach, assert, beforeEach, describe, expect, test } from "vitest"; import { createMockRelay, type MockRelay } from "vitest-nostr"; import { WebSocketCloseCode } from "../connection.js"; import { createRxBackwardReq, createRxForwardReq, createRxNostr, createRxOneshotReq, RxNostr, } from "../index.js"; import { faker, spyEvent, spySub } from "./helper.js"; describe("Basic subscription behavior (single relay)", () => { const RELAY_URL = "ws://localhost:1234"; let rxNostr: RxNostr; let relay: MockRelay; beforeEach(async () => { relay = createMockRelay(RELAY_URL); rxNostr = createRxNostr({ retry: { strategy: "immediately", maxCount: 1 }, globalRelayConfig: { disableAutoFetchNip11Limitations: true, }, }); await rxNostr.switchRelays([RELAY_URL]); await relay.connected; }); afterEach(() => { rxNostr.dispose(); relay.close({ code: WebSocketCloseCode.DISPOSED_BY_RX_NOSTR, reason: "Clean up on afterEach()", wasClean: true, }); }); test("[forward] Each REQ is published with the same subId.", async () => { const req = createRxForwardReq("sub"); rxNostr.use(req).subscribe(); req.emit(faker.filter()); await expect(relay).toReceiveREQ("sub:0"); req.emit(faker.filters()); await expect(relay).toReceiveREQ("sub:0"); req.emit(faker.filters()); await expect(relay).toReceiveREQ("sub:0"); }); test("[forward] If connection is abnormally closed, REQ will be retried.", async () => { const req = createRxForwardReq("sub"); const spy = spyEvent(); rxNostr.use(req).pipe(spy.tap()).subscribe(); req.emit(faker.filters()); await expect(relay).toReceiveREQ("sub:0"); // Emulate an abnormal disconnection of a relay. const socket = await relay.getSocket(0); socket.close({
code: WebSocketCloseCode.ABNORMAL_CLOSURE, reason: "Relay's internal error", wasClean: true, });
await expect(relay).toReceiveREQ("sub:0"); relay.emitEVENT("sub:0"); await expect(spy).toSeeEVENT(); }); test("[forward] Backoff option `maxCount` works.", async () => { const req = createRxForwardReq("sub"); const spy = spyEvent(); rxNostr.use(req).pipe(spy.tap()).subscribe(); req.emit(faker.filters()); await expect(relay).toReceiveREQ("sub:0"); (await relay.getSocket(0)).close({ code: WebSocketCloseCode.ABNORMAL_CLOSURE, reason: "Relay's internal error", wasClean: true, }); await expect(relay).toReceiveREQ("sub:0"); (await relay.getSocket(1)).close({ code: WebSocketCloseCode.ABNORMAL_CLOSURE, reason: "Relay's internal error", wasClean: true, }); // FIXME: dirty return new Promise((resolve) => { setTimeout(resolve, 100); }).then(async () => { await expect(rxNostr.getRelayState(RELAY_URL)).toBe("error"); }); }); test("[forward] If connection is closed with 4000, REQ will not be retried.", async () => { const req = createRxForwardReq("sub"); const spy = spyEvent(); rxNostr.use(req).pipe(spy.tap()).subscribe(); req.emit(faker.filters()); await expect(relay).toReceiveREQ("sub:0"); // Emulate an abnormal disconnection of a relay. const socket = await relay.getSocket(0); socket.close({ code: WebSocketCloseCode.DONT_RETRY, reason: "Relay's internal error, but should not retry.", wasClean: true, }); rxNostr.send(faker.event()); // REQ will not be retried, so next message is EVENT await expect(relay).toReceiveEVENT(); }); test("[forward] since/until is reevaluated when a lazy REQ is resubmitted.", async () => { const req = createRxForwardReq("sub"); rxNostr.use(req).subscribe(); let since = 0; req.emit({ ...faker.filter(), since: () => since++ }); await expect(relay).toReceiveREQ([ "sub:0", { ...faker.filter(), since: 0 }, ]); // Emulate an abnormal disconnection of a relay. const socket = await relay.getSocket(0); socket.close({ code: WebSocketCloseCode.ABNORMAL_CLOSURE, reason: "Relay's internal error", wasClean: true, }); await expect(relay).toReceiveREQ([ "sub:0", { ...faker.filter(), since: 1 }, ]); }); test("[forward] Reject EVENTs that do not match the given filters.", async () => { const req = createRxForwardReq("sub"); const spy = spyEvent(); rxNostr.use(req).pipe(spy.tap()).subscribe(); req.emit({ kinds: [1] }); await expect(relay).toReceiveREQ("sub:0"); relay.emitEVENT("sub:0", faker.event({ kind: 1, content: "pass" })); await expect(spy).toSeeEVENT([ "sub:0", faker.event({ kind: 1, content: "pass" }), ]); relay.emitEVENT("sub:0", faker.event({ kind: 0, content: "rejected" })); relay.emitEVENT("sub:0", faker.event({ kind: 1, content: "pass" })); await expect(spy).toSeeEVENT([ "sub:0", faker.event({ kind: 1, content: "pass" }), ]); }); test("[backward] After receiving EOSE, CLOSE is sent out.", async () => { const req = createRxBackwardReq("sub"); rxNostr.use(req).pipe().subscribe(); req.emit(faker.filters()); await expect(relay).toReceiveREQ("sub:0"); relay.emitEOSE("sub:0"); await expect(relay).toReceiveCLOSE("sub:0"); }); test("[backward] Receipt of EOSE does not terminate the Observable.", async () => { const req = createRxBackwardReq("sub"); const spy = spySub(); rxNostr.use(req).pipe(spy.tap()).subscribe(); req.emit(faker.filters()); await expect(relay).toReceiveREQ(); relay.emitEOSE("sub:0"); assert(!spy.completed()); }); test("[backward] Each EOSE CLOSEs the REQ in the order of arrival.", async () => { const req = createRxBackwardReq("sub"); rxNostr.use(req).subscribe(); req.emit(faker.filters()); await expect(relay).toReceiveREQ("sub:0"); req.emit(faker.filters()); await expect(relay).toReceiveREQ("sub:1"); req.emit(faker.filters()); await expect(relay).toReceiveREQ("sub:2"); relay.emitEOSE("sub:2"); await expect(relay).toReceiveCLOSE("sub:2"); relay.emitEOSE("sub:1"); await expect(relay).toReceiveCLOSE("sub:1"); relay.emitEOSE("sub:0"); await expect(relay).toReceiveCLOSE("sub:0"); }); test("[backward] Even if a newer REQ emits EOSE, EVENTs from older but still active REQ can be received.", async () => { const req = createRxBackwardReq("sub"); const spy = spyEvent(); rxNostr.use(req).pipe(spy.tap()).subscribe(); req.emit(faker.filters()); await expect(relay).toReceiveREQ("sub:0"); req.emit(faker.filters()); await expect(relay).toReceiveREQ("sub:1"); relay.emitEOSE("sub:1"); await expect(relay).toReceiveCLOSE("sub:1"); relay.emitEVENT("sub:0"); await expect(spy).toSeeEVENT("sub:0"); relay.emitEOSE("sub:0"); await expect(relay).toReceiveCLOSE("sub:0"); }); test("[backward] If connection is abnormally closed before receiving EOSE, REQ will be retried.", async () => { const req = createRxBackwardReq("sub"); const spy = spyEvent(); rxNostr.use(req).pipe(spy.tap()).subscribe(); req.emit(faker.filters()); await expect(relay).toReceiveREQ("sub:0"); // Emulate an abnormal disconnection of a relay. const socket = await relay.getSocket(0); socket.close({ code: WebSocketCloseCode.ABNORMAL_CLOSURE, reason: "Relay's internal error", wasClean: true, }); await expect(relay).toReceiveREQ("sub:0"); relay.emitEVENT("sub:0"); await expect(spy).toSeeEVENT(); relay.emitEOSE("sub:0"); await expect(relay).toReceiveCLOSE("sub:0"); }); test("[backward] If connection is abnormally closed after receiving EOSE, REQ will not be retried.", async () => { const req = createRxBackwardReq("sub"); rxNostr.use(req).subscribe(); req.emit(faker.filters()); await expect(relay).toReceiveREQ("sub:0"); relay.emitEOSE("sub:0"); await expect(relay).toReceiveCLOSE("sub:0"); // Emulate an abnormal disconnection of a relay. const socket = await relay.getSocket(0); socket.close({ code: WebSocketCloseCode.ABNORMAL_CLOSURE, reason: "Relay's internal error", wasClean: true, }); rxNostr.send(faker.event()); await expect(relay).toReceiveEVENT(); }); test("[oneshot] Receipt of EOSE terminates the Observable.", async () => { const req = createRxOneshotReq({ subId: "sub", filters: faker.filter(), }); const spy = spySub(); rxNostr.use(req).pipe(spy.tap()).subscribe(); await expect(relay).toReceiveREQ("sub:0"); assert(!spy.completed()); relay.emitEOSE("sub:0"); assert(spy.completed()); }); }); describe("Basic subscription behavior (multiple relays)", () => { const RELAY_URL1 = "ws://localhost:1234"; const RELAY_URL2 = "ws://localhost:1235"; const RELAY_URL3 = "ws://localhost:1236"; let rxNostr: RxNostr; let relay1: MockRelay; let relay2: MockRelay; let relay3: MockRelay; beforeEach(async () => { relay1 = createMockRelay(RELAY_URL1); relay2 = createMockRelay(RELAY_URL2); relay3 = createMockRelay(RELAY_URL3); rxNostr = createRxNostr({ globalRelayConfig: { disableAutoFetchNip11Limitations: true, }, }); await rxNostr.switchRelays([RELAY_URL1, RELAY_URL2]); await relay1.connected; await relay2.connected; }); afterEach(() => { rxNostr.dispose(); relay1.close({ code: WebSocketCloseCode.DISPOSED_BY_RX_NOSTR, reason: "Clean up on afterEach()", wasClean: true, }); relay2.close({ code: WebSocketCloseCode.DISPOSED_BY_RX_NOSTR, reason: "Clean up on afterEach()", wasClean: true, }); relay3.close({ code: WebSocketCloseCode.DISPOSED_BY_RX_NOSTR, reason: "Clean up on afterEach()", wasClean: true, }); }); test("[forward] Adding new relay affects existing REQ.", async () => { const req = createRxForwardReq("sub"); rxNostr.use(req).subscribe(); req.emit(faker.filters()); await expect(relay1).toReceiveREQ("sub:0"); await expect(relay2).toReceiveREQ("sub:0"); await rxNostr.addRelay(RELAY_URL3); await expect(relay3).toReceiveREQ("sub:0"); }); test("[forward] Removing a relay affects existing REQ.", async () => { const req = createRxForwardReq("sub"); rxNostr.use(req).subscribe(); req.emit(faker.filters()); await expect(relay1).toReceiveREQ("sub:0"); await expect(relay2).toReceiveREQ("sub:0"); await rxNostr.removeRelay(RELAY_URL2); await expect(relay2).toReceiveCLOSE("sub:0"); }); test("[forward] Adding new relay doesn't affect existing subset-REQ when the relay is not in subset.", async () => { const req = createRxForwardReq("sub"); rxNostr.use(req, { scope: [RELAY_URL1] }).subscribe(); req.emit(faker.filters()); await expect(relay1).toReceiveREQ("sub:0"); await rxNostr.addRelay(RELAY_URL3); rxNostr.send(faker.event()); await expect(relay1).toReceiveEVENT(); await expect(relay2).toReceiveEVENT(); await expect(relay3).toReceiveEVENT(); expect(relay2.messagesToConsume.pendingItems.length).toBe(0); expect(relay3.messagesToConsume.pendingItems.length).toBe(0); }); test("[forward] Adding new relay affects existing subset-REQ when the relay is in subset.", async () => { const req = createRxForwardReq("sub"); rxNostr.use(req, { scope: [RELAY_URL1, RELAY_URL3] }).subscribe(); req.emit(faker.filters()); await expect(relay1).toReceiveREQ("sub:0"); await rxNostr.addRelay(RELAY_URL3); await expect(relay3).toReceiveREQ("sub:0"); expect(relay2.messagesToConsume.pendingItems.length).toBe(0); }); test("[backward] EOSE on all subset relays triggers CLOSE.", async () => { const req = createRxBackwardReq("sub"); rxNostr.use(req, { scope: [RELAY_URL1] }).subscribe(); req.emit(faker.filters()); await expect(relay1).toReceiveREQ("sub:0"); relay1.emitEOSE("sub:0"); await expect(relay1).toReceiveCLOSE("sub:0"); expect(relay2.messagesToConsume.pendingItems.length).toBe(0); expect(relay3.messagesToConsume.pendingItems.length).toBe(0); }); test("[backward] EOSE on all subset and active relays triggers CLOSE.", async () => { const req = createRxBackwardReq("sub"); rxNostr.use(req, { scope: [RELAY_URL1, RELAY_URL3] }).subscribe(); req.emit(faker.filters()); await expect(relay1).toReceiveREQ("sub:0"); relay1.emitEOSE("sub:0"); await expect(relay1).toReceiveCLOSE("sub:0"); expect(relay2.messagesToConsume.pendingItems.length).toBe(0); }); test("[oneshot] EOSE on all subset and active relays triggers completion.", async () => { const req = createRxOneshotReq({ subId: "sub", filters: faker.filters(), }); const spy = spySub(); rxNostr .use(req, { scope: [RELAY_URL1, RELAY_URL3] }) .pipe(spy.tap()) .subscribe(); await expect(relay1).toReceiveREQ("sub:0"); relay1.emitEOSE("sub:0"); await expect(relay1).toReceiveCLOSE("sub:0"); assert(spy.completed()); }); test("[oneshot] Collect all events under different timing EOSE.", async () => { const req = createRxOneshotReq({ subId: "sub", filters: faker.filters(), }); const spy = spyEvent(); rxNostr.use(req).pipe(spy.tap()).subscribe(); await expect(relay1).toReceiveREQ("sub:0"); await expect(relay2).toReceiveREQ("sub:0"); relay1.emitEVENT("sub:0"); await expect(spy).toSeeEVENT("sub:0"); relay2.emitEVENT("sub:0"); await expect(spy).toSeeEVENT("sub:0"); relay1.emitEOSE("sub:0"); await expect(relay1).toReceiveCLOSE("sub:0"); relay2.emitEVENT("sub:0"); await expect(spy).toSeeEVENT("sub:0"); relay2.emitEOSE("sub:0"); await expect(relay2).toReceiveCLOSE("sub:0"); }); }); describe("Under limited REQ concurency (single relay)", () => { const RELAY_URL = "ws://localhost:1234"; let rxNostr: RxNostr; let relay: MockRelay; beforeEach(async () => { relay = createMockRelay(RELAY_URL); rxNostr = createRxNostr({ retry: { strategy: "immediately", maxCount: 1 }, globalRelayConfig: { disableAutoFetchNip11Limitations: true, maxConcurrentReqsFallback: 1, }, }); await rxNostr.switchRelays([RELAY_URL]); await relay.connected; }); afterEach(() => { rxNostr.dispose(); relay.close({ code: WebSocketCloseCode.DISPOSED_BY_RX_NOSTR, reason: "Clean up on afterEach()", wasClean: true, }); }); test("[backward] Overflowed REQs will be enqueued.", async () => { const req = createRxBackwardReq("sub"); rxNostr.use(req).pipe().subscribe(); req.emit(faker.filters()); req.emit(faker.filters()); req.emit(faker.filters()); await expect(relay).toReceiveREQ("sub:0"); relay.emitEOSE("sub:0"); await expect(relay).toReceiveCLOSE("sub:0"); await expect(relay).toReceiveREQ("sub:1"); relay.emitEOSE("sub:1"); await expect(relay).toReceiveCLOSE("sub:1"); await expect(relay).toReceiveREQ("sub:2"); relay.emitEOSE("sub:2"); await expect(relay).toReceiveCLOSE("sub:2"); }); });
src/__test__/subscription.test.ts
penpenpng-rx-nostr-7c52614
[ { "filename": "src/__test__/sending.test.ts", "retrieved_chunk": " await rxNostr.switchRelays([RELAY_URL1, RELAY_URL2]);\n rxNostr\n .send(faker.event(), { scope: [RELAY_URL2] })\n .pipe(spy.tap())\n .subscribe();\n await expect(relay2).toReceiveEVENT();\n expect(relay1.messagesToConsume.pendingItems.length).toBe(0);\n relay2.emitOK(\"*\", true);\n expect(spy.completed()).toBe(true);\n });", "score": 38.23520742379848 }, { "filename": "src/__test__/sending.test.ts", "retrieved_chunk": " rxNostr.send(faker.event()).pipe(spy.tap()).subscribe();\n rxNostr.addRelay(RELAY_URL3);\n await expect(relay1).toReceiveEVENT();\n expect(relay2.messagesToConsume.pendingItems.length).toBe(0);\n expect(relay3.messagesToConsume.pendingItems.length).toBe(0);\n relay1.emitOK(\"*\", true);\n expect(spy.completed()).toBe(true);\n });\n test(\"send() doesn't wait for OK from out of scope.\", async () => {\n const spy = spySub();", "score": 33.894361823095956 }, { "filename": "src/__test__/sending.test.ts", "retrieved_chunk": " wasClean: true,\n });\n relay2.close({\n code: WebSocketCloseCode.DISPOSED_BY_RX_NOSTR,\n reason: \"Clean up on afterEach()\",\n wasClean: true,\n });\n relay3.close({\n code: WebSocketCloseCode.DISPOSED_BY_RX_NOSTR,\n reason: \"Clean up on afterEach()\",", "score": 30.366344790513864 }, { "filename": "src/__test__/sending.test.ts", "retrieved_chunk": " { url: RELAY_URL2, write: false, read: true },\n ]);\n await relay1.connected;\n await relay2.connected;\n });\n afterEach(() => {\n rxNostr.dispose();\n relay1.close({\n code: WebSocketCloseCode.DISPOSED_BY_RX_NOSTR,\n reason: \"Clean up on afterEach()\",", "score": 28.167719865159373 }, { "filename": "src/__test__/sending.test.ts", "retrieved_chunk": " wasClean: true,\n });\n });\n test(\"send() sends only to writable relays.\", async () => {\n rxNostr.send(faker.event());\n await expect(relay1).toReceiveEVENT();\n expect(relay2.messagesToConsume.pendingItems.length).toBe(0);\n });\n test(\"send() doesn't wait for OK from relays added later.\", async () => {\n const spy = spySub();", "score": 26.619025592215756 } ]
typescript
code: WebSocketCloseCode.ABNORMAL_CLOSURE, reason: "Relay's internal error", wasClean: true, });
import Nostr from "nostr-typedef"; import { catchError, EMPTY, filter, finalize, first, identity, map, merge, mergeAll, mergeMap, type MonoTypeOperatorFunction, Observable, of, type OperatorFunction, ReplaySubject, Subject, Subscription, take, takeUntil, tap, timeout, type Unsubscribable, } from "rxjs"; import { BackoffConfig, Connection } from "./connection.js"; import { getSignedEvent } from "./nostr/event.js"; import { fetchRelayInfo } from "./nostr/nip11.js"; import { completeOnTimeout } from "./operator.js"; import type { ConnectionState, ConnectionStatePacket, ErrorPacket, EventPacket, LazyFilter, LazyREQ, MessagePacket, OkPacket, } from "./packet.js"; import type { RxReq } from "./req.js"; import { defineDefaultOptions, normalizeRelayUrl } from "./util.js"; /** * The core object of rx-nostr, which holds a connection to relays * and manages subscriptions as directed by the RxReq object connected by `use()`. * Use `createRxNostr()` to get the object. */ export interface RxNostr { /** * Return a list of relays used by this object. * The relay URLs are normalised so may not match the URLs set. */ getRelays(): RelayConfig[]; /** * Set the list of relays. * If a REQ subscription already exists, the same REQ is issued for the newly added relay * and CLOSE is sent for the removed relay. */ switchRelays(config: AcceptableRelaysConfig): Promise<void>; /** Utility wrapper for `switchRelays()`. */ addRelay(relay: string | RelayConfig): Promise<void>; /** Utility wrapper for `switchRelays()`. */ removeRelay(url: string): Promise<void>; /** Return true if the given relay is set to rxNostr. */ hasRelay(url: string): boolean; /** Return true if the given relay allows to be written. */ canWriteRelay(url: string): boolean; /** Return true if the given relay allows to be read. */ canReadRelay(url: string): boolean; /** Fetch all relays' info based on [NIP-11](https://github.com/nostr-protocol/nips/blob/master/11.md) */ fetchAllRelaysInfo(): Promise<Record<string, Nostr.Nip11.RelayInfo | null>>; /** * Return a dictionary in which you can look up connection state. * * **NOTE**: Keys are **normalized** URL, so may be different from one you set. */ getAllRelayState(): Record<string, ConnectionState>; /** * Return connection state of the given relay. * Throw if unknown URL is given. */ getRelayState(url: string): ConnectionState; /** * Attempt to reconnect the WebSocket if its state is `error` or `rejected`. * If not, do nothing. */ reconnect(url: string): void; // TODO: document /** * Set or unset a pipe to be applied to all EventPackets. */ setGlobalEventPacketPipe( pipe: MonoTypeOperatorFunction<EventPacket> | null ): void; /** * Associate RxReq with RxNostr. * When the associated RxReq is manipulated, * the RxNostr issues a new REQ to all relays allowed to be read. * The method returns an Observable that issues EventPackets * when an EVENT is received that is subscribed by RxReq. * You can unsubscribe the Observable to CLOSE. */ use( rxReq: RxReq, options?: Partial<RxNostrUseOptions> ): Observable<EventPacket>; /** * Create an Observable that receives all events (EVENT) from all websocket connections. * * Nothing happens when this Observable is unsubscribed. * */ createAllEventObservable(): Observable<EventPacket>; /** * Create an Observable that receives all errors from all websocket connections. * Note that an Observable is terminated when it receives any error, * so this method is the only way to receive errors arising from multiplexed websocket connections * (It means that Observables returned by `use()` never throw error). * * Nothing happens when this Observable is unsubscribed. * */ createAllErrorObservable(): Observable<ErrorPacket>; /** * Create an Observable that receives all messages from all websocket connections. * * Nothing happens when this Observable is unsubscribed. * */ createAllMessageObservable(): Observable<MessagePacket>; /** * Create an Observable that receives changing of WebSocket connection state. * * Nothing happens when this Observable is unsubscribed. */ createConnectionStateObservable(): Observable<ConnectionStatePacket>; /** * Attempt to send events to all relays that are allowed to write. * The `seckey` option accepts both nsec format and hex format, * and if omitted NIP-07 will be automatically used. */ send( params: Nostr.EventParameters, options?: RxNostrSendOptions ): Observable<OkPacket>; /** * Release all resources held by the RxNostr object. * Any Observable resulting from this RxNostr will be in the completed state * and will never receive messages again. * RxReq used by this object is not affected; in other words, if the RxReq is used * by another RxNostr, its use is not prevented. */ dispose(): void; } /** Create a RxNostr object. This is the only way to create that. */ export function createRxNostr(options?: Partial<RxNostrOptions>): RxNostr { return new RxNostrImpl(options); } export interface RxNostrOptions { /** Auto reconnection strategy. */ retry: BackoffConfig; /** * The time in milliseconds to timeout when following the backward strategy. * The observable is terminated when the specified amount of time has elapsed * during which no new events are available. */ timeout: number; globalRelayConfig?: { disableAutoFetchNip11Limitations?: boolean; maxConcurrentReqsFallback?: number; }; } const makeRxNostrOptions = defineDefaultOptions<RxNostrOptions>({ retry: { strategy: "exponential", maxCount: 5, initialDelay: 1000, }, timeout: 10000, globalRelayConfig: undefined, }); export interface RxNostrUseOptions { scope?: string[]; } const makeRxNostrUseOptions = defineDefaultOptions<RxNostrUseOptions>({ scope: undefined, }); export interface RxNostrSendOptions { scope?: string[]; seckey?: string; } const makeRxNostrSendOptions = defineDefaultOptions<RxNostrSendOptions>({ scope: undefined, seckey: undefined, }); /** Config object specifying WebSocket behavior. */ export interface RelayConfig { /** WebSocket endpoint URL. */ url: string; /** If true, rxNostr can publish REQ and subscribe EVENTs. */ read: boolean; /** If true, rxNostr can send EVENTs. */ write: boolean; disableAutoFetchNip11Limitations?: boolean; } /** Parameter of `rxNostr.switchRelays()` */ export type AcceptableRelaysConfig = | (string | RelayConfig)[] | Nostr.Nip07.GetRelayResult; class RxNostrImpl implements RxNostr { private options: RxNostrOptions; private connections: Map<string, Connection> = new Map(); private ongoings: Map<string, OngoingReq> = new Map(); private messageIn$: Subject<MessagePacket> = new Subject(); private error$: Subject<ErrorPacket> = new Subject(); private status$: Subject<ConnectionStatePacket> = new Subject(); private globalEventPacketPipe: MonoTypeOperatorFunction<EventPacket> | null = null; private disposed = false; private get messageOut$() { return this.messageIn$.pipe( mergeMap((packet) => { const pipe = this.globalEventPacketPipe; if (!pipe) { return of(packet); } const message = packet.message; if (message[0] !== "EVENT") { return of(packet); } return of({ from: packet.from, subId: message[1], event: message[2], }).pipe( pipe, map( ({ from, subId, event }): MessagePacket => ({ from, message: ["EVENT", subId, event], }) ) ); }) ); } constructor(options?: Partial<RxNostrOptions>) { const opt = makeRxNostrOptions(options); this.options = { ...opt, }; } getRelays(): RelayConfig[] { return Array.from(this.connections.values()).map( ({ url, read, write }) => ({ url, read, write, }) ); } private createConnection({ url, read, write, disableAutoFetchNip11Limitations, }: RelayConfig): Connection { const connection = new Connection(url, { backoff: this.options.retry, read, write, disableAutoFetchNip11Limitations: disableAutoFetchNip11Limitations ?? this.options.globalRelayConfig?.disableAutoFetchNip11Limitations, maxConcurrentReqsFallback: this.options.globalRelayConfig?.maxConcurrentReqsFallback, }); connection.getConnectionStateObservable().subscribe((state) => { this.status$.next({ from: url, state, }); }); connection.getErrorObservable().subscribe((reason) => { this.error$.next({ from: url, reason }); }); connection .getMessageObservable() .pipe( catchError((reason: unknown) => { this.error$.next({ from: url, reason }); return EMPTY; }) ) .subscribe((v) => { this.messageIn$.next(v); }); return connection; } async switchRelays(config: AcceptableRelaysConfig): Promise<void> { const nextConns: Map<string, Connection> = new Map(); for (const { url, read, write } of normalizeRelaysConfig(config)) { // pop a connection if exists const prevConn = this.connections.get(url); this.connections.delete(url); if (prevConn) { prevConn.read = read; prevConn.write = write; nextConns.set(url, prevConn); } else { nextConns.set(url, this.createConnection({ url, read, write })); } } // connections that are no longer used for (const conn of this.connections.values()) { conn.dispose(); } const ensureConns: Promise<unknown>[] = []; for (const conn of nextConns.values()) { if (conn.read) { ensureConns.push(conn.start()); } else { conn.stop(); } } await Promise.all(ensureConns); this.connections = nextConns; // If disposed during switchRelay processing if (this.disposed) { for (const conn of this.connections.values()) { conn.dispose(); } return; } for (const { req, scope } of this.ongoings.values()) { this.ensureReq(req, { scope }); } // --- scoped untility pure functions --- function normalizeRelaysConfig( config: AcceptableRelaysConfig ): RelayConfig[] { if (Array.isArray(config)) { return config.map((urlOrConfig) => { const relay: RelayConfig = typeof urlOrConfig === "string" ? { url: urlOrConfig, read: true, write: true, } : urlOrConfig; relay.
url = normalizeRelayUrl(relay.url);
return relay; }); } else { return Object.entries(config).map(([url, flags]) => ({ url: normalizeRelayUrl(url), ...flags, })); } } } async addRelay(relay: string | RelayConfig): Promise<void> { await this.switchRelays([...this.getRelays(), relay]); } async removeRelay(url: string): Promise<void> { const u = normalizeRelayUrl(url); const currentRelays = this.getRelays(); const nextRelays = currentRelays.filter((relay) => relay.url !== u); if (currentRelays.length !== nextRelays.length) { await this.switchRelays(nextRelays); } } hasRelay(url: string): boolean { const u = normalizeRelayUrl(url); return this.getRelays().some((relay) => relay.url === u); } canWriteRelay(url: string): boolean { const u = normalizeRelayUrl(url); return this.getRelays().some((relay) => relay.url === u && relay.write); } canReadRelay(url: string): boolean { const u = normalizeRelayUrl(url); return this.getRelays().some((relay) => relay.url === u && relay.read); } async fetchAllRelaysInfo(): Promise< Record<string, Nostr.Nip11.RelayInfo | null> > { const entries = await Promise.all( Array.from(this.connections.keys()).map( async (url): Promise<[string, Nostr.Nip11.RelayInfo | null]> => [ url, await fetchRelayInfo(url).catch(() => null), ] ) ); return Object.fromEntries(entries); } getAllRelayState(): Record<string, ConnectionState> { return Object.fromEntries( Array.from(this.connections.values()).map((e) => [ e.url, this.getRelayState(e.url), ]) ); } getRelayState(url: string): ConnectionState { const conn = this.connections.get(normalizeRelayUrl(url)); if (!conn) { throw new Error("RelayConfig not found"); } // this.relays[url] may be set before this.relays[url].websocket is initialized return conn?.getConnectionState() ?? "not-started"; } reconnect(url: string): void { if (this.canReadRelay(url)) { this.connections.get(normalizeRelayUrl(url))?.start(); } } setGlobalEventPacketPipe(pipe: MonoTypeOperatorFunction<EventPacket> | null) { this.globalEventPacketPipe = pipe; } use( rxReq: RxReq, options?: Partial<RxNostrUseOptions> ): Observable<EventPacket> { const { scope: _scope } = makeRxNostrUseOptions(options); const scope = _scope?.map(normalizeRelayUrl); const TIMEOUT = this.options.timeout; const strategy = rxReq.strategy; const rxReqId = rxReq.rxReqId; const message$ = this.messageOut$; const ongoings = this.ongoings; const getAllRelayState = this.getAllRelayState.bind(this); const createConnectionStateObservable = this.createConnectionStateObservable.bind(this); const ensureReq = this.ensureReq.bind(this); const finalizeReq = this.finalizeReq.bind(this); const subId$ = rxReq.getReqObservable().pipe( filter((filters): filters is LazyFilter[] => filters !== null), strategy === "oneshot" ? first() : identity, attachSubId(), strategy === "forward" ? manageActiveForwardReq() : identity, tap((req) => { ensureReq(req, { overwrite: strategy === "forward", scope }); }), map(([, subId]) => subId) ); if (strategy === "forward") { const subId = makeSubId({ rxReqId, }); const resource: Unsubscribable[] = []; const subject = new Subject<EventPacket>(); resource.push(subject); return subject.pipe( tap({ subscribe: () => { resource.push(subId$.subscribe()); resource.push( message$ .pipe(filterBySubId(subId), pickEvents()) .subscribe((v) => { subject.next(v); }) ); }, finalize: () => { for (const r of resource) { r.unsubscribe(); } finalizeReq({ subId }); }, }) ); } else { return subId$.pipe(map(createEoseManagedEventObservable), mergeAll()); } function attachSubId(): OperatorFunction<LazyFilter[], LazyREQ> { const makeId = (index?: number) => makeSubId({ rxReqId, index }); switch (strategy) { case "backward": return map((filters, index) => ["REQ", makeId(index), ...filters]); case "forward": case "oneshot": return map((filters) => ["REQ", makeId(), ...filters]); } } function manageActiveForwardReq(): MonoTypeOperatorFunction<LazyREQ> { const recordActiveReq = (req: LazyREQ) => { const subId = req[1]; ongoings.set(subId, { req, scope, }); }; const forgetActiveReq = (subId: string) => { ongoings.delete(subId); }; return tap({ next: (req: LazyREQ) => { recordActiveReq(req); }, finalize: () => { forgetActiveReq( makeSubId({ rxReqId, }) ); }, }); } function createEoseManagedEventObservable( subId: string ): Observable<EventPacket> { const eose$ = new Subject<void>(); const complete$ = new Subject<void>(); const eoseRelays = new Set<string>(); const manageCompletion = merge( eose$, createConnectionStateObservable() ).subscribe(() => { const status = getAllRelayState(); const shouldComplete = Object.entries(status).every( ([url, state]) => (scope && !scope.includes(url)) || state === "error" || state === "terminated" || (state === "ongoing" && eoseRelays.has(url)) ); if (shouldComplete) { complete$.next(); } }); return message$.pipe( takeUntil(complete$), completeOnTimeout(TIMEOUT), filterBySubId(subId), filter((e) => !eoseRelays.has(e.from)), tap((e) => { if (e.message[0] === "EOSE") { eoseRelays.add(e.from); finalizeReq({ subId, url: e.from }); eose$.next(); } }), pickEvents(), finalize(() => { finalizeReq({ subId }); complete$.unsubscribe(); eose$.unsubscribe(); manageCompletion.unsubscribe(); }) ); } function filterBySubId( subId: string ): MonoTypeOperatorFunction<MessagePacket> { return filter( (packet) => (packet.message[0] === "EVENT" || packet.message[0] === "EOSE") && packet.message[1] === subId ); } function pickEvents(): OperatorFunction<MessagePacket, EventPacket> { return mergeMap(({ from, message }) => message[0] === "EVENT" ? of({ from, subId: message[1], event: message[2] }) : EMPTY ); } } createAllEventObservable(): Observable<EventPacket> { return this.messageOut$.pipe( mergeMap(({ from, message }) => message[0] === "EVENT" ? of({ from, subId: message[1], event: message[2] }) : EMPTY ) ); } createAllErrorObservable(): Observable<ErrorPacket> { return this.error$.asObservable(); } createAllMessageObservable(): Observable<MessagePacket> { return this.messageOut$; } createConnectionStateObservable(): Observable<ConnectionStatePacket> { return this.status$.asObservable(); } send( params: Nostr.EventParameters, options?: RxNostrSendOptions ): Observable<OkPacket> { const { seckey, scope: _scope } = makeRxNostrSendOptions(options); const scope = _scope?.map(normalizeRelayUrl); const writableConns = Array.from(this.connections.values()).filter( (conn) => (!scope || scope.includes(conn.url)) && conn.write ); const subject = new ReplaySubject<OkPacket>(writableConns.length); let subscription: Subscription | null = null; getSignedEvent(params, seckey).then((event) => { if (!subject.closed) { subscription = this.createAllMessageObservable().subscribe( ({ from, message }) => { if (message[0] !== "OK") { return; } subject.next({ from, id: event.id, ok: message[2], }); } ); } for (const conn of writableConns) { conn.sendEVENT(["EVENT", event]); } }); return subject.pipe( take(writableConns.length), timeout(30 * 1000), finalize(() => { subject.complete(); subject.unsubscribe(); subscription?.unsubscribe(); }) ); } dispose(): void { this.disposed = true; this.messageIn$.complete(); this.error$.complete(); for (const conn of this.connections.values()) { conn.dispose(); } } private ensureReq( req: LazyREQ, options?: { scope?: string[] | null; overwrite?: boolean } ) { const scope = options?.scope; for (const url of this.connections.keys()) { const conn = this.connections.get(url); if (!conn || !conn.read || (scope && !scope.includes(url))) { continue; } conn.ensureReq(req, { overwrite: options?.overwrite }); } } private finalizeReq(params: { subId: string; url?: string }) { const { subId, url } = params; if (subId === undefined && url === undefined) { throw new Error(); } if (url) { const conn = this.connections.get(url); conn?.finalizeReq(subId); } else { for (const conn of this.connections.values()) { conn?.finalizeReq(subId); } } } } interface OngoingReq { req: LazyREQ; scope?: string[]; } function makeSubId(params: { rxReqId: string; index?: number }): string { return `${params.rxReqId}:${params.index ?? 0}`; }
src/rx-nostr.ts
penpenpng-rx-nostr-7c52614
[ { "filename": "src/util.ts", "retrieved_chunk": "export function normalizeRelayUrl(url: string) {\n return normalizeUrl(url, {\n normalizeProtocol: false,\n removeTrailingSlash: true,\n });\n}", "score": 28.932051077349218 }, { "filename": "src/__test__/sending.test.ts", "retrieved_chunk": " { url: RELAY_URL2, write: false, read: true },\n ]);\n await relay1.connected;\n await relay2.connected;\n });\n afterEach(() => {\n rxNostr.dispose();\n relay1.close({\n code: WebSocketCloseCode.DISPOSED_BY_RX_NOSTR,\n reason: \"Clean up on afterEach()\",", "score": 20.689807455987502 }, { "filename": "src/__test__/sending.test.ts", "retrieved_chunk": " let relay1: MockRelay;\n let relay2: MockRelay;\n let relay3: MockRelay;\n beforeEach(async () => {\n relay1 = createMockRelay(RELAY_URL1);\n relay2 = createMockRelay(RELAY_URL2);\n relay3 = createMockRelay(RELAY_URL3);\n rxNostr = createRxNostr();\n await rxNostr.switchRelays([\n { url: RELAY_URL1, write: true, read: false },", "score": 18.92688895086193 }, { "filename": "src/nostr/nip11.ts", "retrieved_chunk": "import * as Nostr from \"nostr-typedef\";\n/**\n * Fetch relay's information based on [NIP-11](https://github.com/nostr-protocol/nips/blob/master/11.md).\n */\nexport async function fetchRelayInfo(\n url: string\n): Promise<Nostr.Nip11.RelayInfo> {\n const u = new URL(url);\n u.protocol = u.protocol.replace(/^ws(s?):/, \"http$1:\");\n const res = await fetch(u.toString(), {", "score": 15.309709563737185 }, { "filename": "src/connection.ts", "retrieved_chunk": " return this.config.read;\n }\n set read(v) {\n this.config.read = v;\n }\n get write() {\n return this.config.write;\n }\n set write(v) {\n this.config.write = v;", "score": 12.713567765138576 } ]
typescript
url = normalizeRelayUrl(relay.url);
import Nostr from "nostr-typedef"; import { catchError, EMPTY, filter, finalize, first, identity, map, merge, mergeAll, mergeMap, type MonoTypeOperatorFunction, Observable, of, type OperatorFunction, ReplaySubject, Subject, Subscription, take, takeUntil, tap, timeout, type Unsubscribable, } from "rxjs"; import { BackoffConfig, Connection } from "./connection.js"; import { getSignedEvent } from "./nostr/event.js"; import { fetchRelayInfo } from "./nostr/nip11.js"; import { completeOnTimeout } from "./operator.js"; import type { ConnectionState, ConnectionStatePacket, ErrorPacket, EventPacket, LazyFilter, LazyREQ, MessagePacket, OkPacket, } from "./packet.js"; import type { RxReq } from "./req.js"; import { defineDefaultOptions, normalizeRelayUrl } from "./util.js"; /** * The core object of rx-nostr, which holds a connection to relays * and manages subscriptions as directed by the RxReq object connected by `use()`. * Use `createRxNostr()` to get the object. */ export interface RxNostr { /** * Return a list of relays used by this object. * The relay URLs are normalised so may not match the URLs set. */ getRelays(): RelayConfig[]; /** * Set the list of relays. * If a REQ subscription already exists, the same REQ is issued for the newly added relay * and CLOSE is sent for the removed relay. */ switchRelays(config: AcceptableRelaysConfig): Promise<void>; /** Utility wrapper for `switchRelays()`. */ addRelay(relay: string | RelayConfig): Promise<void>; /** Utility wrapper for `switchRelays()`. */ removeRelay(url: string): Promise<void>; /** Return true if the given relay is set to rxNostr. */ hasRelay(url: string): boolean; /** Return true if the given relay allows to be written. */ canWriteRelay(url: string): boolean; /** Return true if the given relay allows to be read. */ canReadRelay(url: string): boolean; /** Fetch all relays' info based on [NIP-11](https://github.com/nostr-protocol/nips/blob/master/11.md) */ fetchAllRelaysInfo(): Promise<Record<string, Nostr.Nip11.RelayInfo | null>>; /** * Return a dictionary in which you can look up connection state. * * **NOTE**: Keys are **normalized** URL, so may be different from one you set. */ getAllRelayState(): Record<string, ConnectionState>; /** * Return connection state of the given relay. * Throw if unknown URL is given. */ getRelayState(url: string): ConnectionState; /** * Attempt to reconnect the WebSocket if its state is `error` or `rejected`. * If not, do nothing. */ reconnect(url: string): void; // TODO: document /** * Set or unset a pipe to be applied to all EventPackets. */ setGlobalEventPacketPipe( pipe: MonoTypeOperatorFunction<EventPacket> | null ): void; /** * Associate RxReq with RxNostr. * When the associated RxReq is manipulated, * the RxNostr issues a new REQ to all relays allowed to be read. * The method returns an Observable that issues EventPackets * when an EVENT is received that is subscribed by RxReq. * You can unsubscribe the Observable to CLOSE. */ use( rxReq: RxReq, options?: Partial<RxNostrUseOptions> ): Observable<EventPacket>; /** * Create an Observable that receives all events (EVENT) from all websocket connections. * * Nothing happens when this Observable is unsubscribed. * */ createAllEventObservable(): Observable<EventPacket>; /** * Create an Observable that receives all errors from all websocket connections. * Note that an Observable is terminated when it receives any error, * so this method is the only way to receive errors arising from multiplexed websocket connections * (It means that Observables returned by `use()` never throw error). * * Nothing happens when this Observable is unsubscribed. * */ createAllErrorObservable(): Observable<ErrorPacket>; /** * Create an Observable that receives all messages from all websocket connections. * * Nothing happens when this Observable is unsubscribed. * */ createAllMessageObservable(): Observable<MessagePacket>; /** * Create an Observable that receives changing of WebSocket connection state. * * Nothing happens when this Observable is unsubscribed. */ createConnectionStateObservable(): Observable<ConnectionStatePacket>; /** * Attempt to send events to all relays that are allowed to write. * The `seckey` option accepts both nsec format and hex format, * and if omitted NIP-07 will be automatically used. */ send( params: Nostr.EventParameters, options?: RxNostrSendOptions ): Observable<OkPacket>; /** * Release all resources held by the RxNostr object. * Any Observable resulting from this RxNostr will be in the completed state * and will never receive messages again. * RxReq used by this object is not affected; in other words, if the RxReq is used * by another RxNostr, its use is not prevented. */ dispose(): void; } /** Create a RxNostr object. This is the only way to create that. */ export function createRxNostr(options?: Partial<RxNostrOptions>): RxNostr { return new RxNostrImpl(options); } export interface RxNostrOptions { /** Auto reconnection strategy. */ retry: BackoffConfig; /** * The time in milliseconds to timeout when following the backward strategy. * The observable is terminated when the specified amount of time has elapsed * during which no new events are available. */ timeout: number; globalRelayConfig?: { disableAutoFetchNip11Limitations?: boolean; maxConcurrentReqsFallback?: number; }; } const makeRxNostrOptions = defineDefaultOptions<RxNostrOptions>({ retry: { strategy: "exponential", maxCount: 5, initialDelay: 1000, }, timeout: 10000, globalRelayConfig: undefined, }); export interface RxNostrUseOptions { scope?: string[]; } const makeRxNostrUseOptions = defineDefaultOptions<RxNostrUseOptions>({ scope: undefined, }); export interface RxNostrSendOptions { scope?: string[]; seckey?: string; } const makeRxNostrSendOptions = defineDefaultOptions<RxNostrSendOptions>({ scope: undefined, seckey: undefined, }); /** Config object specifying WebSocket behavior. */ export interface RelayConfig { /** WebSocket endpoint URL. */ url: string; /** If true, rxNostr can publish REQ and subscribe EVENTs. */ read: boolean; /** If true, rxNostr can send EVENTs. */ write: boolean; disableAutoFetchNip11Limitations?: boolean; } /** Parameter of `rxNostr.switchRelays()` */ export type AcceptableRelaysConfig = | (string | RelayConfig)[] | Nostr.Nip07.GetRelayResult; class RxNostrImpl implements RxNostr { private options: RxNostrOptions; private connections: Map<string, Connection> = new Map(); private ongoings: Map<string, OngoingReq> = new Map(); private messageIn$: Subject<MessagePacket> = new Subject(); private error$: Subject<ErrorPacket> = new Subject(); private status$: Subject<ConnectionStatePacket> = new Subject(); private globalEventPacketPipe: MonoTypeOperatorFunction<EventPacket> | null = null; private disposed = false; private get messageOut$() { return this.messageIn$.pipe( mergeMap((packet) => { const pipe = this.globalEventPacketPipe; if (!pipe) { return of(packet); } const message = packet.message; if (message[0] !== "EVENT") { return of(packet); } return of({ from: packet.from, subId: message[1], event: message[2], }).pipe( pipe, map( ({ from, subId, event }): MessagePacket => ({ from, message: ["EVENT", subId, event], }) ) ); }) ); } constructor(options?: Partial<RxNostrOptions>) { const opt = makeRxNostrOptions(options); this.options = { ...opt, }; } getRelays(): RelayConfig[] { return Array.from(this.connections.values()).map( ({ url, read, write }) => ({ url, read, write, }) ); } private createConnection({ url, read, write, disableAutoFetchNip11Limitations, }: RelayConfig): Connection { const connection = new Connection(url, { backoff: this.options.retry, read, write, disableAutoFetchNip11Limitations: disableAutoFetchNip11Limitations ?? this.options.globalRelayConfig?.disableAutoFetchNip11Limitations, maxConcurrentReqsFallback: this.options.globalRelayConfig?.maxConcurrentReqsFallback, }); connection.getConnectionStateObservable().subscribe((state) => { this.status$.next({ from: url, state, }); }); connection.getErrorObservable().subscribe((reason) => { this.error$.next({ from: url, reason }); }); connection .getMessageObservable() .pipe( catchError((reason: unknown) => { this.error$.next({ from: url, reason }); return EMPTY; }) ) .subscribe((v) => { this.messageIn$.next(v); }); return connection; } async switchRelays(config: AcceptableRelaysConfig): Promise<void> { const nextConns: Map<string, Connection> = new Map(); for (const { url, read, write } of normalizeRelaysConfig(config)) { // pop a connection if exists const prevConn = this.connections.get(url); this.connections.delete(url); if (prevConn) { prevConn.read = read; prevConn.write = write; nextConns.set(url, prevConn); } else { nextConns.set(url, this.createConnection({ url, read, write })); } } // connections that are no longer used for (const conn of this.connections.values()) { conn.dispose(); } const ensureConns: Promise<unknown>[] = []; for (const conn of nextConns.values()) { if (conn.read) { ensureConns.push(conn.start()); } else { conn.stop(); } } await Promise.all(ensureConns); this.connections = nextConns; // If disposed during switchRelay processing if (this.disposed) { for (const conn of this.connections.values()) { conn.dispose(); } return; } for (const { req, scope } of this.ongoings.values()) { this.ensureReq(req, { scope }); } // --- scoped untility pure functions --- function normalizeRelaysConfig( config: AcceptableRelaysConfig ): RelayConfig[] { if (Array.isArray(config)) { return config.map((urlOrConfig) => { const relay: RelayConfig = typeof urlOrConfig === "string" ? { url: urlOrConfig, read: true, write: true, } : urlOrConfig; relay.url = normalizeRelayUrl(relay.url); return relay; }); } else { return Object.entries(config).map(([url, flags]) => ({ url: normalizeRelayUrl(url), ...flags, })); } } } async addRelay(relay: string | RelayConfig): Promise<void> { await this.switchRelays([...this.getRelays(), relay]); } async removeRelay(url: string): Promise<void> { const u = normalizeRelayUrl(url); const currentRelays = this.getRelays(); const nextRelays = currentRelays.filter((relay) => relay.url !== u); if (currentRelays.length !== nextRelays.length) { await this.switchRelays(nextRelays); } } hasRelay(url: string): boolean { const u = normalizeRelayUrl(url); return this.getRelays().some((relay) => relay.url === u); } canWriteRelay(url: string): boolean { const u = normalizeRelayUrl(url); return this.getRelays().some((relay) => relay.url === u && relay.write); } canReadRelay(url: string): boolean { const u = normalizeRelayUrl(url); return this.getRelays().some((relay) => relay.url === u && relay.read); } async fetchAllRelaysInfo(): Promise< Record<string, Nostr.Nip11.RelayInfo | null> > { const entries = await Promise.all( Array.from(this.connections.keys()).map( async (url): Promise<[string, Nostr.Nip11.RelayInfo | null]> => [ url, await fetchRelayInfo(url).catch(() => null), ] ) ); return Object.fromEntries(entries); } getAllRelayState(): Record<string, ConnectionState> { return Object.fromEntries( Array.from(this.connections.values()).map((e) => [ e.url, this.getRelayState(e.url), ]) ); } getRelayState(url: string): ConnectionState { const conn = this.connections.get(normalizeRelayUrl(url)); if (!conn) { throw new Error("RelayConfig not found"); } // this.relays[url] may be set before this.relays[url].websocket is initialized return conn?.getConnectionState() ?? "not-started"; } reconnect(url: string): void { if (this.canReadRelay(url)) { this.connections.get(normalizeRelayUrl(url))?.start(); } } setGlobalEventPacketPipe(pipe: MonoTypeOperatorFunction<EventPacket> | null) { this.globalEventPacketPipe = pipe; } use( rxReq: RxReq, options?: Partial<RxNostrUseOptions> ): Observable<EventPacket> { const { scope: _scope } = makeRxNostrUseOptions(options); const scope = _scope?.map(normalizeRelayUrl); const TIMEOUT = this.options.timeout; const strategy = rxReq.strategy; const rxReqId = rxReq.rxReqId; const message$ = this.messageOut$; const ongoings = this.ongoings; const getAllRelayState = this.getAllRelayState.bind(this); const createConnectionStateObservable = this.createConnectionStateObservable.bind(this); const ensureReq = this.ensureReq.bind(this); const finalizeReq = this.finalizeReq.bind(this); const subId$ = rxReq.getReqObservable().pipe( filter((filters): filters is LazyFilter[] => filters !== null), strategy === "oneshot" ? first() : identity, attachSubId(), strategy === "forward" ? manageActiveForwardReq() : identity, tap((req) => { ensureReq(req, { overwrite: strategy === "forward", scope }); }), map(([, subId]) => subId) ); if (strategy === "forward") { const subId = makeSubId({ rxReqId, }); const resource: Unsubscribable[] = []; const subject = new Subject<EventPacket>(); resource.push(subject); return subject.pipe( tap({ subscribe: () => { resource.push(subId$.subscribe()); resource.push( message$ .pipe(filterBySubId(subId), pickEvents()) .subscribe((v) => { subject.next(v); }) ); }, finalize: () => { for (const r of resource) { r.unsubscribe(); } finalizeReq({ subId }); }, }) ); } else { return subId$.pipe(map(createEoseManagedEventObservable), mergeAll()); }
function attachSubId(): OperatorFunction<LazyFilter[], LazyREQ> {
const makeId = (index?: number) => makeSubId({ rxReqId, index }); switch (strategy) { case "backward": return map((filters, index) => ["REQ", makeId(index), ...filters]); case "forward": case "oneshot": return map((filters) => ["REQ", makeId(), ...filters]); } } function manageActiveForwardReq(): MonoTypeOperatorFunction<LazyREQ> { const recordActiveReq = (req: LazyREQ) => { const subId = req[1]; ongoings.set(subId, { req, scope, }); }; const forgetActiveReq = (subId: string) => { ongoings.delete(subId); }; return tap({ next: (req: LazyREQ) => { recordActiveReq(req); }, finalize: () => { forgetActiveReq( makeSubId({ rxReqId, }) ); }, }); } function createEoseManagedEventObservable( subId: string ): Observable<EventPacket> { const eose$ = new Subject<void>(); const complete$ = new Subject<void>(); const eoseRelays = new Set<string>(); const manageCompletion = merge( eose$, createConnectionStateObservable() ).subscribe(() => { const status = getAllRelayState(); const shouldComplete = Object.entries(status).every( ([url, state]) => (scope && !scope.includes(url)) || state === "error" || state === "terminated" || (state === "ongoing" && eoseRelays.has(url)) ); if (shouldComplete) { complete$.next(); } }); return message$.pipe( takeUntil(complete$), completeOnTimeout(TIMEOUT), filterBySubId(subId), filter((e) => !eoseRelays.has(e.from)), tap((e) => { if (e.message[0] === "EOSE") { eoseRelays.add(e.from); finalizeReq({ subId, url: e.from }); eose$.next(); } }), pickEvents(), finalize(() => { finalizeReq({ subId }); complete$.unsubscribe(); eose$.unsubscribe(); manageCompletion.unsubscribe(); }) ); } function filterBySubId( subId: string ): MonoTypeOperatorFunction<MessagePacket> { return filter( (packet) => (packet.message[0] === "EVENT" || packet.message[0] === "EOSE") && packet.message[1] === subId ); } function pickEvents(): OperatorFunction<MessagePacket, EventPacket> { return mergeMap(({ from, message }) => message[0] === "EVENT" ? of({ from, subId: message[1], event: message[2] }) : EMPTY ); } } createAllEventObservable(): Observable<EventPacket> { return this.messageOut$.pipe( mergeMap(({ from, message }) => message[0] === "EVENT" ? of({ from, subId: message[1], event: message[2] }) : EMPTY ) ); } createAllErrorObservable(): Observable<ErrorPacket> { return this.error$.asObservable(); } createAllMessageObservable(): Observable<MessagePacket> { return this.messageOut$; } createConnectionStateObservable(): Observable<ConnectionStatePacket> { return this.status$.asObservable(); } send( params: Nostr.EventParameters, options?: RxNostrSendOptions ): Observable<OkPacket> { const { seckey, scope: _scope } = makeRxNostrSendOptions(options); const scope = _scope?.map(normalizeRelayUrl); const writableConns = Array.from(this.connections.values()).filter( (conn) => (!scope || scope.includes(conn.url)) && conn.write ); const subject = new ReplaySubject<OkPacket>(writableConns.length); let subscription: Subscription | null = null; getSignedEvent(params, seckey).then((event) => { if (!subject.closed) { subscription = this.createAllMessageObservable().subscribe( ({ from, message }) => { if (message[0] !== "OK") { return; } subject.next({ from, id: event.id, ok: message[2], }); } ); } for (const conn of writableConns) { conn.sendEVENT(["EVENT", event]); } }); return subject.pipe( take(writableConns.length), timeout(30 * 1000), finalize(() => { subject.complete(); subject.unsubscribe(); subscription?.unsubscribe(); }) ); } dispose(): void { this.disposed = true; this.messageIn$.complete(); this.error$.complete(); for (const conn of this.connections.values()) { conn.dispose(); } } private ensureReq( req: LazyREQ, options?: { scope?: string[] | null; overwrite?: boolean } ) { const scope = options?.scope; for (const url of this.connections.keys()) { const conn = this.connections.get(url); if (!conn || !conn.read || (scope && !scope.includes(url))) { continue; } conn.ensureReq(req, { overwrite: options?.overwrite }); } } private finalizeReq(params: { subId: string; url?: string }) { const { subId, url } = params; if (subId === undefined && url === undefined) { throw new Error(); } if (url) { const conn = this.connections.get(url); conn?.finalizeReq(subId); } else { for (const conn of this.connections.values()) { conn?.finalizeReq(subId); } } } } interface OngoingReq { req: LazyREQ; scope?: string[]; } function makeSubId(params: { rxReqId: string; index?: number }): string { return `${params.rxReqId}:${params.index ?? 0}`; }
src/rx-nostr.ts
penpenpng-rx-nostr-7c52614
[ { "filename": "src/connection.ts", "retrieved_chunk": " finalizeReq(subId: string) {\n if (this.connectionState === \"terminated\") {\n return;\n }\n const req = this.reqs.get(subId);\n if (!req) {\n return;\n }\n this.reqs.delete(subId);\n if (req.isOngoing) {", "score": 13.225700895307842 }, { "filename": "src/connection.ts", "retrieved_chunk": "function evalREQ([type, subId, ...filters]: LazyREQ): Nostr.ToRelayMessage.REQ {\n return [type, subId, ...evalFilters(filters)];\n}\nfunction isConcurrentAllowed(\n subId: string,\n reqs: Map<string, ReqState>,\n concurrent: number | null\n): boolean {\n if (concurrent === null) {\n return true;", "score": 12.719107826791149 }, { "filename": "src/operator.ts", "retrieved_chunk": " mergeAll,\n mergeMap,\n type MonoTypeOperatorFunction,\n type ObservableInput,\n of,\n type OperatorFunction,\n pipe,\n scan,\n tap,\n timeout,", "score": 12.185017935677052 }, { "filename": "src/operator.ts", "retrieved_chunk": "): MonoTypeOperatorFunction<EventPacket> {\n return pipe(groupBy(key), map(pipe(latest())), mergeAll());\n}\n/**\n * Only events with a valid signature are allowed to pass.\n */\nexport function verify(): MonoTypeOperatorFunction<EventPacket> {\n return filter<EventPacket>(({ event }) => _verify(event));\n}\n/**", "score": 11.89396561501368 }, { "filename": "src/helper.ts", "retrieved_chunk": " return filters.map(evalFilter);\n } else {\n return [evalFilter(filters)];\n }\n}\nfunction evalFilter(filter: LazyFilter): Nostr.Filter {\n return {\n ...filter,\n since: filter.since ? evalLazyNumber(filter.since) : undefined,\n until: filter.until ? evalLazyNumber(filter.until) : undefined,", "score": 10.069362557077056 } ]
typescript
function attachSubId(): OperatorFunction<LazyFilter[], LazyREQ> {
import { getInput, setFailed } from '@actions/core' import { context } from '@actions/github' import { commandValidator, isValidFileExt } from './validate' type CommandParams = { inputFilePath: string outputFilePath: string targetLang: string } type ManualParams = { inputFiles: string[] outputFiles: string[] languages: string[] } export const getCommandParams = async (): Promise<CommandParams> => { const comment = context.payload.comment?.body if (!comment) setFailed('Error: Comment could not be retrieved correctly.') const regex = /\/(?:gpt-translate|gt)\s+(\S+)\s+(\S+)\s+(\S+)/ const match = regex.exec(comment) return commandValidator(comment, match) } export const extractInput = (): ManualParams => { const inputFilesRaw = getInput('inputFiles') const outputFilesRaw = getInput('outputFiles') const languagesRaw = getInput('languages') const inputFiles = inputFilesRaw ? inputFilesRaw.split(' ').filter((v) => v) : [] const outputFiles = outputFilesRaw ? outputFilesRaw.split(' ').filter((v) => v) : [] const languages = languagesRaw ? languagesRaw.split(' ').filter((v) => v) : [] // validate input const isValidInput = inputFiles.every((v)
=> isValidFileExt(v)) && outputFiles.every((v) => isValidFileExt(v)) if (!isValidInput) {
throw new Error( 'Invalid input. Please check the inputFiles and outputFiles parameters', ) } return { inputFiles, outputFiles, languages, } }
src/extract.ts
3ru-gpt-translate-c5830ec
[ { "filename": "src/index.ts", "retrieved_chunk": " // Multiple output and target languages can be selected.\n // [IMPORTANT]\n // outputFiles must be specified using wildcards.\n const { inputFiles, outputFiles, languages } = extractInput()\n await translateByManual(inputFiles, outputFiles, languages)\n break\n default:\n await postError('This event is not supported.')\n }\n}", "score": 16.893777458504903 }, { "filename": "src/translate.ts", "retrieved_chunk": " info('No input files specified. Skip translation.')\n return\n }\n if (!inputFiles.length || !outputFiles.length || !languages.length) {\n throw new Error(\n 'Error: For push execution, all three parameters (inputFiles, outputFiles and language ) are required',\n )\n }\n if (outputFiles.length !== languages.length) {\n throw new Error('Error: outputFiles and language must be same length.')", "score": 15.76925329698648 }, { "filename": "src/translate.ts", "retrieved_chunk": " )\n await gitCreatePullRequest(branch, title, body)\n await gitPostComment('🎉Translation PR created!')\n}\nexport const translateByManual = async (\n inputFiles: string[],\n outputFiles: string[],\n languages: string[],\n) => {\n if (!inputFiles.length) {", "score": 14.792148286323794 }, { "filename": "src/validate.ts", "retrieved_chunk": " const [, inputFilePath, outputFilePath, targetLang] = match!\n if (!isValidFileExt(inputFilePath) || !isValidFileExt(outputFilePath)) {\n await postError(INVALID_FILE_EXTENSION)\n }\n const inputFileName = path.basename(inputFilePath)\n const outputFileName = path.basename(outputFilePath)\n // If multiple files are specified, input and output must be specified in the same way.\n if (\n (inputFileName.includes('*') && !outputFileName.includes('*')) ||\n (!inputFileName.includes('*') && outputFileName.includes('*'))", "score": 13.113386079338012 }, { "filename": "src/translate.ts", "retrieved_chunk": " }\n const outputFilePaths: string[][] = outputFiles.map((outputFile) => {\n return generateOutputFilePaths(inputFiles, outputFile)\n })\n // TODO: Dealing with request limits (503 error)\n await Promise.all(\n languages.map((language, index) =>\n createTranslatedFiles(inputFiles, outputFilePaths[index], language),\n ),\n )", "score": 12.91941374860325 } ]
typescript
=> isValidFileExt(v)) && outputFiles.every((v) => isValidFileExt(v)) if (!isValidInput) {
import { afterEach, assert, beforeEach, describe, expect, test } from "vitest"; import { createMockRelay, type MockRelay } from "vitest-nostr"; import { WebSocketCloseCode } from "../connection.js"; import { createRxBackwardReq, createRxForwardReq, createRxNostr, createRxOneshotReq, RxNostr, } from "../index.js"; import { faker, spyEvent, spySub } from "./helper.js"; describe("Basic subscription behavior (single relay)", () => { const RELAY_URL = "ws://localhost:1234"; let rxNostr: RxNostr; let relay: MockRelay; beforeEach(async () => { relay = createMockRelay(RELAY_URL); rxNostr = createRxNostr({ retry: { strategy: "immediately", maxCount: 1 }, globalRelayConfig: { disableAutoFetchNip11Limitations: true, }, }); await rxNostr.switchRelays([RELAY_URL]); await relay.connected; }); afterEach(() => { rxNostr.dispose(); relay.close({ code: WebSocketCloseCode.DISPOSED_BY_RX_NOSTR, reason: "Clean up on afterEach()", wasClean: true, }); }); test("[forward] Each REQ is published with the same subId.", async () => { const req = createRxForwardReq("sub"); rxNostr.use(req).subscribe(); req.emit(faker.filter()); await expect(relay).toReceiveREQ("sub:0");
req.emit(faker.filters());
await expect(relay).toReceiveREQ("sub:0"); req.emit(faker.filters()); await expect(relay).toReceiveREQ("sub:0"); }); test("[forward] If connection is abnormally closed, REQ will be retried.", async () => { const req = createRxForwardReq("sub"); const spy = spyEvent(); rxNostr.use(req).pipe(spy.tap()).subscribe(); req.emit(faker.filters()); await expect(relay).toReceiveREQ("sub:0"); // Emulate an abnormal disconnection of a relay. const socket = await relay.getSocket(0); socket.close({ code: WebSocketCloseCode.ABNORMAL_CLOSURE, reason: "Relay's internal error", wasClean: true, }); await expect(relay).toReceiveREQ("sub:0"); relay.emitEVENT("sub:0"); await expect(spy).toSeeEVENT(); }); test("[forward] Backoff option `maxCount` works.", async () => { const req = createRxForwardReq("sub"); const spy = spyEvent(); rxNostr.use(req).pipe(spy.tap()).subscribe(); req.emit(faker.filters()); await expect(relay).toReceiveREQ("sub:0"); (await relay.getSocket(0)).close({ code: WebSocketCloseCode.ABNORMAL_CLOSURE, reason: "Relay's internal error", wasClean: true, }); await expect(relay).toReceiveREQ("sub:0"); (await relay.getSocket(1)).close({ code: WebSocketCloseCode.ABNORMAL_CLOSURE, reason: "Relay's internal error", wasClean: true, }); // FIXME: dirty return new Promise((resolve) => { setTimeout(resolve, 100); }).then(async () => { await expect(rxNostr.getRelayState(RELAY_URL)).toBe("error"); }); }); test("[forward] If connection is closed with 4000, REQ will not be retried.", async () => { const req = createRxForwardReq("sub"); const spy = spyEvent(); rxNostr.use(req).pipe(spy.tap()).subscribe(); req.emit(faker.filters()); await expect(relay).toReceiveREQ("sub:0"); // Emulate an abnormal disconnection of a relay. const socket = await relay.getSocket(0); socket.close({ code: WebSocketCloseCode.DONT_RETRY, reason: "Relay's internal error, but should not retry.", wasClean: true, }); rxNostr.send(faker.event()); // REQ will not be retried, so next message is EVENT await expect(relay).toReceiveEVENT(); }); test("[forward] since/until is reevaluated when a lazy REQ is resubmitted.", async () => { const req = createRxForwardReq("sub"); rxNostr.use(req).subscribe(); let since = 0; req.emit({ ...faker.filter(), since: () => since++ }); await expect(relay).toReceiveREQ([ "sub:0", { ...faker.filter(), since: 0 }, ]); // Emulate an abnormal disconnection of a relay. const socket = await relay.getSocket(0); socket.close({ code: WebSocketCloseCode.ABNORMAL_CLOSURE, reason: "Relay's internal error", wasClean: true, }); await expect(relay).toReceiveREQ([ "sub:0", { ...faker.filter(), since: 1 }, ]); }); test("[forward] Reject EVENTs that do not match the given filters.", async () => { const req = createRxForwardReq("sub"); const spy = spyEvent(); rxNostr.use(req).pipe(spy.tap()).subscribe(); req.emit({ kinds: [1] }); await expect(relay).toReceiveREQ("sub:0"); relay.emitEVENT("sub:0", faker.event({ kind: 1, content: "pass" })); await expect(spy).toSeeEVENT([ "sub:0", faker.event({ kind: 1, content: "pass" }), ]); relay.emitEVENT("sub:0", faker.event({ kind: 0, content: "rejected" })); relay.emitEVENT("sub:0", faker.event({ kind: 1, content: "pass" })); await expect(spy).toSeeEVENT([ "sub:0", faker.event({ kind: 1, content: "pass" }), ]); }); test("[backward] After receiving EOSE, CLOSE is sent out.", async () => { const req = createRxBackwardReq("sub"); rxNostr.use(req).pipe().subscribe(); req.emit(faker.filters()); await expect(relay).toReceiveREQ("sub:0"); relay.emitEOSE("sub:0"); await expect(relay).toReceiveCLOSE("sub:0"); }); test("[backward] Receipt of EOSE does not terminate the Observable.", async () => { const req = createRxBackwardReq("sub"); const spy = spySub(); rxNostr.use(req).pipe(spy.tap()).subscribe(); req.emit(faker.filters()); await expect(relay).toReceiveREQ(); relay.emitEOSE("sub:0"); assert(!spy.completed()); }); test("[backward] Each EOSE CLOSEs the REQ in the order of arrival.", async () => { const req = createRxBackwardReq("sub"); rxNostr.use(req).subscribe(); req.emit(faker.filters()); await expect(relay).toReceiveREQ("sub:0"); req.emit(faker.filters()); await expect(relay).toReceiveREQ("sub:1"); req.emit(faker.filters()); await expect(relay).toReceiveREQ("sub:2"); relay.emitEOSE("sub:2"); await expect(relay).toReceiveCLOSE("sub:2"); relay.emitEOSE("sub:1"); await expect(relay).toReceiveCLOSE("sub:1"); relay.emitEOSE("sub:0"); await expect(relay).toReceiveCLOSE("sub:0"); }); test("[backward] Even if a newer REQ emits EOSE, EVENTs from older but still active REQ can be received.", async () => { const req = createRxBackwardReq("sub"); const spy = spyEvent(); rxNostr.use(req).pipe(spy.tap()).subscribe(); req.emit(faker.filters()); await expect(relay).toReceiveREQ("sub:0"); req.emit(faker.filters()); await expect(relay).toReceiveREQ("sub:1"); relay.emitEOSE("sub:1"); await expect(relay).toReceiveCLOSE("sub:1"); relay.emitEVENT("sub:0"); await expect(spy).toSeeEVENT("sub:0"); relay.emitEOSE("sub:0"); await expect(relay).toReceiveCLOSE("sub:0"); }); test("[backward] If connection is abnormally closed before receiving EOSE, REQ will be retried.", async () => { const req = createRxBackwardReq("sub"); const spy = spyEvent(); rxNostr.use(req).pipe(spy.tap()).subscribe(); req.emit(faker.filters()); await expect(relay).toReceiveREQ("sub:0"); // Emulate an abnormal disconnection of a relay. const socket = await relay.getSocket(0); socket.close({ code: WebSocketCloseCode.ABNORMAL_CLOSURE, reason: "Relay's internal error", wasClean: true, }); await expect(relay).toReceiveREQ("sub:0"); relay.emitEVENT("sub:0"); await expect(spy).toSeeEVENT(); relay.emitEOSE("sub:0"); await expect(relay).toReceiveCLOSE("sub:0"); }); test("[backward] If connection is abnormally closed after receiving EOSE, REQ will not be retried.", async () => { const req = createRxBackwardReq("sub"); rxNostr.use(req).subscribe(); req.emit(faker.filters()); await expect(relay).toReceiveREQ("sub:0"); relay.emitEOSE("sub:0"); await expect(relay).toReceiveCLOSE("sub:0"); // Emulate an abnormal disconnection of a relay. const socket = await relay.getSocket(0); socket.close({ code: WebSocketCloseCode.ABNORMAL_CLOSURE, reason: "Relay's internal error", wasClean: true, }); rxNostr.send(faker.event()); await expect(relay).toReceiveEVENT(); }); test("[oneshot] Receipt of EOSE terminates the Observable.", async () => { const req = createRxOneshotReq({ subId: "sub", filters: faker.filter(), }); const spy = spySub(); rxNostr.use(req).pipe(spy.tap()).subscribe(); await expect(relay).toReceiveREQ("sub:0"); assert(!spy.completed()); relay.emitEOSE("sub:0"); assert(spy.completed()); }); }); describe("Basic subscription behavior (multiple relays)", () => { const RELAY_URL1 = "ws://localhost:1234"; const RELAY_URL2 = "ws://localhost:1235"; const RELAY_URL3 = "ws://localhost:1236"; let rxNostr: RxNostr; let relay1: MockRelay; let relay2: MockRelay; let relay3: MockRelay; beforeEach(async () => { relay1 = createMockRelay(RELAY_URL1); relay2 = createMockRelay(RELAY_URL2); relay3 = createMockRelay(RELAY_URL3); rxNostr = createRxNostr({ globalRelayConfig: { disableAutoFetchNip11Limitations: true, }, }); await rxNostr.switchRelays([RELAY_URL1, RELAY_URL2]); await relay1.connected; await relay2.connected; }); afterEach(() => { rxNostr.dispose(); relay1.close({ code: WebSocketCloseCode.DISPOSED_BY_RX_NOSTR, reason: "Clean up on afterEach()", wasClean: true, }); relay2.close({ code: WebSocketCloseCode.DISPOSED_BY_RX_NOSTR, reason: "Clean up on afterEach()", wasClean: true, }); relay3.close({ code: WebSocketCloseCode.DISPOSED_BY_RX_NOSTR, reason: "Clean up on afterEach()", wasClean: true, }); }); test("[forward] Adding new relay affects existing REQ.", async () => { const req = createRxForwardReq("sub"); rxNostr.use(req).subscribe(); req.emit(faker.filters()); await expect(relay1).toReceiveREQ("sub:0"); await expect(relay2).toReceiveREQ("sub:0"); await rxNostr.addRelay(RELAY_URL3); await expect(relay3).toReceiveREQ("sub:0"); }); test("[forward] Removing a relay affects existing REQ.", async () => { const req = createRxForwardReq("sub"); rxNostr.use(req).subscribe(); req.emit(faker.filters()); await expect(relay1).toReceiveREQ("sub:0"); await expect(relay2).toReceiveREQ("sub:0"); await rxNostr.removeRelay(RELAY_URL2); await expect(relay2).toReceiveCLOSE("sub:0"); }); test("[forward] Adding new relay doesn't affect existing subset-REQ when the relay is not in subset.", async () => { const req = createRxForwardReq("sub"); rxNostr.use(req, { scope: [RELAY_URL1] }).subscribe(); req.emit(faker.filters()); await expect(relay1).toReceiveREQ("sub:0"); await rxNostr.addRelay(RELAY_URL3); rxNostr.send(faker.event()); await expect(relay1).toReceiveEVENT(); await expect(relay2).toReceiveEVENT(); await expect(relay3).toReceiveEVENT(); expect(relay2.messagesToConsume.pendingItems.length).toBe(0); expect(relay3.messagesToConsume.pendingItems.length).toBe(0); }); test("[forward] Adding new relay affects existing subset-REQ when the relay is in subset.", async () => { const req = createRxForwardReq("sub"); rxNostr.use(req, { scope: [RELAY_URL1, RELAY_URL3] }).subscribe(); req.emit(faker.filters()); await expect(relay1).toReceiveREQ("sub:0"); await rxNostr.addRelay(RELAY_URL3); await expect(relay3).toReceiveREQ("sub:0"); expect(relay2.messagesToConsume.pendingItems.length).toBe(0); }); test("[backward] EOSE on all subset relays triggers CLOSE.", async () => { const req = createRxBackwardReq("sub"); rxNostr.use(req, { scope: [RELAY_URL1] }).subscribe(); req.emit(faker.filters()); await expect(relay1).toReceiveREQ("sub:0"); relay1.emitEOSE("sub:0"); await expect(relay1).toReceiveCLOSE("sub:0"); expect(relay2.messagesToConsume.pendingItems.length).toBe(0); expect(relay3.messagesToConsume.pendingItems.length).toBe(0); }); test("[backward] EOSE on all subset and active relays triggers CLOSE.", async () => { const req = createRxBackwardReq("sub"); rxNostr.use(req, { scope: [RELAY_URL1, RELAY_URL3] }).subscribe(); req.emit(faker.filters()); await expect(relay1).toReceiveREQ("sub:0"); relay1.emitEOSE("sub:0"); await expect(relay1).toReceiveCLOSE("sub:0"); expect(relay2.messagesToConsume.pendingItems.length).toBe(0); }); test("[oneshot] EOSE on all subset and active relays triggers completion.", async () => { const req = createRxOneshotReq({ subId: "sub", filters: faker.filters(), }); const spy = spySub(); rxNostr .use(req, { scope: [RELAY_URL1, RELAY_URL3] }) .pipe(spy.tap()) .subscribe(); await expect(relay1).toReceiveREQ("sub:0"); relay1.emitEOSE("sub:0"); await expect(relay1).toReceiveCLOSE("sub:0"); assert(spy.completed()); }); test("[oneshot] Collect all events under different timing EOSE.", async () => { const req = createRxOneshotReq({ subId: "sub", filters: faker.filters(), }); const spy = spyEvent(); rxNostr.use(req).pipe(spy.tap()).subscribe(); await expect(relay1).toReceiveREQ("sub:0"); await expect(relay2).toReceiveREQ("sub:0"); relay1.emitEVENT("sub:0"); await expect(spy).toSeeEVENT("sub:0"); relay2.emitEVENT("sub:0"); await expect(spy).toSeeEVENT("sub:0"); relay1.emitEOSE("sub:0"); await expect(relay1).toReceiveCLOSE("sub:0"); relay2.emitEVENT("sub:0"); await expect(spy).toSeeEVENT("sub:0"); relay2.emitEOSE("sub:0"); await expect(relay2).toReceiveCLOSE("sub:0"); }); }); describe("Under limited REQ concurency (single relay)", () => { const RELAY_URL = "ws://localhost:1234"; let rxNostr: RxNostr; let relay: MockRelay; beforeEach(async () => { relay = createMockRelay(RELAY_URL); rxNostr = createRxNostr({ retry: { strategy: "immediately", maxCount: 1 }, globalRelayConfig: { disableAutoFetchNip11Limitations: true, maxConcurrentReqsFallback: 1, }, }); await rxNostr.switchRelays([RELAY_URL]); await relay.connected; }); afterEach(() => { rxNostr.dispose(); relay.close({ code: WebSocketCloseCode.DISPOSED_BY_RX_NOSTR, reason: "Clean up on afterEach()", wasClean: true, }); }); test("[backward] Overflowed REQs will be enqueued.", async () => { const req = createRxBackwardReq("sub"); rxNostr.use(req).pipe().subscribe(); req.emit(faker.filters()); req.emit(faker.filters()); req.emit(faker.filters()); await expect(relay).toReceiveREQ("sub:0"); relay.emitEOSE("sub:0"); await expect(relay).toReceiveCLOSE("sub:0"); await expect(relay).toReceiveREQ("sub:1"); relay.emitEOSE("sub:1"); await expect(relay).toReceiveCLOSE("sub:1"); await expect(relay).toReceiveREQ("sub:2"); relay.emitEOSE("sub:2"); await expect(relay).toReceiveCLOSE("sub:2"); }); });
src/__test__/subscription.test.ts
penpenpng-rx-nostr-7c52614
[ { "filename": "src/__test__/sending.test.ts", "retrieved_chunk": " wasClean: true,\n });\n relay2.close({\n code: WebSocketCloseCode.DISPOSED_BY_RX_NOSTR,\n reason: \"Clean up on afterEach()\",\n wasClean: true,\n });\n relay3.close({\n code: WebSocketCloseCode.DISPOSED_BY_RX_NOSTR,\n reason: \"Clean up on afterEach()\",", "score": 39.63994788157286 }, { "filename": "src/__test__/sending.test.ts", "retrieved_chunk": " { url: RELAY_URL2, write: false, read: true },\n ]);\n await relay1.connected;\n await relay2.connected;\n });\n afterEach(() => {\n rxNostr.dispose();\n relay1.close({\n code: WebSocketCloseCode.DISPOSED_BY_RX_NOSTR,\n reason: \"Clean up on afterEach()\",", "score": 32.44737619813647 }, { "filename": "src/__test__/sending.test.ts", "retrieved_chunk": " wasClean: true,\n });\n });\n test(\"send() sends only to writable relays.\", async () => {\n rxNostr.send(faker.event());\n await expect(relay1).toReceiveEVENT();\n expect(relay2.messagesToConsume.pendingItems.length).toBe(0);\n });\n test(\"send() doesn't wait for OK from relays added later.\", async () => {\n const spy = spySub();", "score": 29.634247218090007 }, { "filename": "src/req.ts", "retrieved_chunk": "class RxOneshotReq extends RxReqBase {\n constructor(req: { filters: LazyFilter | LazyFilter[]; subId?: string }) {\n super(req?.subId);\n this.emit(req.filters);\n }\n override get strategy(): \"oneshot\" {\n return \"oneshot\";\n }\n}\nexport interface Mixin<R, T> {", "score": 28.06920003358988 }, { "filename": "src/__test__/sending.test.ts", "retrieved_chunk": " rxNostr.send(faker.event()).pipe(spy.tap()).subscribe();\n rxNostr.addRelay(RELAY_URL3);\n await expect(relay1).toReceiveEVENT();\n expect(relay2.messagesToConsume.pendingItems.length).toBe(0);\n expect(relay3.messagesToConsume.pendingItems.length).toBe(0);\n relay1.emitOK(\"*\", true);\n expect(spy.completed()).toBe(true);\n });\n test(\"send() doesn't wait for OK from out of scope.\", async () => {\n const spy = spySub();", "score": 26.883303991513955 } ]
typescript
req.emit(faker.filters());
import { getInput, setFailed } from '@actions/core' import { context } from '@actions/github' import { commandValidator, isValidFileExt } from './validate' type CommandParams = { inputFilePath: string outputFilePath: string targetLang: string } type ManualParams = { inputFiles: string[] outputFiles: string[] languages: string[] } export const getCommandParams = async (): Promise<CommandParams> => { const comment = context.payload.comment?.body if (!comment) setFailed('Error: Comment could not be retrieved correctly.') const regex = /\/(?:gpt-translate|gt)\s+(\S+)\s+(\S+)\s+(\S+)/ const match = regex.exec(comment) return commandValidator(comment, match) } export const extractInput = (): ManualParams => { const inputFilesRaw = getInput('inputFiles') const outputFilesRaw = getInput('outputFiles') const languagesRaw = getInput('languages') const inputFiles = inputFilesRaw ? inputFilesRaw.split(' ').filter((v) => v) : [] const outputFiles = outputFilesRaw ? outputFilesRaw.split(' ').filter((v) => v) : [] const languages = languagesRaw ? languagesRaw.split(' ').filter((v) => v) : [] // validate input const isValidInput = inputFiles.every((v) =>
isValidFileExt(v)) && outputFiles.every((v) => isValidFileExt(v)) if (!isValidInput) {
throw new Error( 'Invalid input. Please check the inputFiles and outputFiles parameters', ) } return { inputFiles, outputFiles, languages, } }
src/extract.ts
3ru-gpt-translate-c5830ec
[ { "filename": "src/index.ts", "retrieved_chunk": " // Multiple output and target languages can be selected.\n // [IMPORTANT]\n // outputFiles must be specified using wildcards.\n const { inputFiles, outputFiles, languages } = extractInput()\n await translateByManual(inputFiles, outputFiles, languages)\n break\n default:\n await postError('This event is not supported.')\n }\n}", "score": 16.893777458504903 }, { "filename": "src/translate.ts", "retrieved_chunk": " info('No input files specified. Skip translation.')\n return\n }\n if (!inputFiles.length || !outputFiles.length || !languages.length) {\n throw new Error(\n 'Error: For push execution, all three parameters (inputFiles, outputFiles and language ) are required',\n )\n }\n if (outputFiles.length !== languages.length) {\n throw new Error('Error: outputFiles and language must be same length.')", "score": 15.76925329698648 }, { "filename": "src/translate.ts", "retrieved_chunk": " )\n await gitCreatePullRequest(branch, title, body)\n await gitPostComment('🎉Translation PR created!')\n}\nexport const translateByManual = async (\n inputFiles: string[],\n outputFiles: string[],\n languages: string[],\n) => {\n if (!inputFiles.length) {", "score": 14.792148286323794 }, { "filename": "src/validate.ts", "retrieved_chunk": " const [, inputFilePath, outputFilePath, targetLang] = match!\n if (!isValidFileExt(inputFilePath) || !isValidFileExt(outputFilePath)) {\n await postError(INVALID_FILE_EXTENSION)\n }\n const inputFileName = path.basename(inputFilePath)\n const outputFileName = path.basename(outputFilePath)\n // If multiple files are specified, input and output must be specified in the same way.\n if (\n (inputFileName.includes('*') && !outputFileName.includes('*')) ||\n (!inputFileName.includes('*') && outputFileName.includes('*'))", "score": 13.113386079338012 }, { "filename": "src/translate.ts", "retrieved_chunk": " }\n const outputFilePaths: string[][] = outputFiles.map((outputFile) => {\n return generateOutputFilePaths(inputFiles, outputFile)\n })\n // TODO: Dealing with request limits (503 error)\n await Promise.all(\n languages.map((language, index) =>\n createTranslatedFiles(inputFiles, outputFilePaths[index], language),\n ),\n )", "score": 12.91941374860325 } ]
typescript
isValidFileExt(v)) && outputFiles.every((v) => isValidFileExt(v)) if (!isValidInput) {
import Nostr from "nostr-typedef"; import { catchError, delay, distinct, distinctUntilChanged, EMPTY, filter, groupBy, map, mergeAll, mergeMap, type MonoTypeOperatorFunction, type ObservableInput, of, type OperatorFunction, pipe, scan, tap, timeout, TimeoutError, } from "rxjs"; import { evalFilters } from "./helper.js"; import { compareEvents, verify as _verify } from "./nostr/event.js"; import { isFiltered, MatchFilterOptions } from "./nostr/filter.js"; import { EventPacket, LazyFilter, MessagePacket, ReqPacket } from "./packet.js"; import { defineDefaultOptions } from "./util.js"; // --------------------- // // EventPacket operators // // --------------------- // /** * Remove the events once seen. */ export function uniq( flushes?: ObservableInput<unknown> ): MonoTypeOperatorFunction<EventPacket> { return distinct<EventPacket, string>(({ event }) => event.id, flushes); } /** * Create a customizable uniq operator. * * If `keyFn()` returns a non-null key, the key is stored in `Set`. * The operator filters packets with keys already stored. * * The `Set` returned in the second value of the tuple * can be manipulated externally or in optional event handlers. * For example, you can call `Set#clear()` to forget all keys. */ export function createUniq<T>( keyFn: (packet: EventPacket) => T | null, options?: CreateUniqOptions<T> ): [MonoTypeOperatorFunction<EventPacket>, Set<T>] { const cache = new Set<T>(); return [ filter((packet) => { const key = keyFn(packet); if (key === null) { return true; } if (cache.has(key)) { options?.onHit?.(packet, cache); return false; } else { cache.add(key); options?.onCache?.(packet, cache); return true; } }), cache, ]; } /** * Only the latest events are allowed to pass. */ export function latest(): MonoTypeOperatorFunction<EventPacket> { return pipe( scan<EventPacket>((acc, packet) => compareEvents(acc.event, packet.event) < 0 ? packet : acc ), distinctUntilChanged( (a, b) => a === b, ({ event }) => event.id ) ); } /** * For each key, only the latest events are allowed to pass. */ export function latestEach<K>( key: (packet: EventPacket) => K ): MonoTypeOperatorFunction<EventPacket> { return pipe(groupBy(key), map(pipe(latest())), mergeAll()); } /** * Only events with a valid signature are allowed to pass. */ export function verify(): MonoTypeOperatorFunction<EventPacket> { return filter<EventPacket>(({ event }) => _verify(event)); } /** * Only events with given kind are allowed to pass. */ export function filterKind<K extends number>( kind: K ): MonoTypeOperatorFunction<EventPacket> { return filter<EventPacket>(({ event }) => event.kind === kind); } /** * Filter events based on a REQ filter object. */ export function filterBy( filters: LazyFilter | LazyFilter[], options?: MatchFilterOptions & FilterByOptions ): MonoTypeOperatorFunction<EventPacket> { const { not } = makeFilterByOptions(options); const evaledFilter = evalFilters(filters); return filter(({ event }) => { const match = isFiltered(event, evaledFilter, options); return not ? !match : match; }); } /** * Accumulate latest events in order of new arrival (based on `created_at`). */ export function timeline( limit?: number ): OperatorFunction<EventPacket, EventPacket[]> { return scan<EventPacket, EventPacket[]>((acc, packet) => { const next = [...acc, packet].sort( (a, b) => -1 * compareEvents(a.event, b.event) ); if (limit !== undefined) { next.splice(limit); } return next; }, []); } export function sortEvents( bufferTime: number, compareFn?: (a: EventPacket, b: EventPacket) => number ): MonoTypeOperatorFunction<EventPacket> { return sort( bufferTime, compareFn ?? ((a, b) => compareEvents(a.event, b.event)) ); } // ----------------------- // // MessagePacket operators // // ----------------------- // export function filterType<T extends Nostr.ToClientMessage.Type>( type: T ): OperatorFunction< MessagePacket, MessagePacket<Nostr.ToClientMessage.Message<T>> > { return filter( (packet): packet is MessagePacket<Nostr.ToClientMessage.Message<T>> => packet.message[0] === type ); } // ------------------- // // ReqPacket operators // // ------------------- // /** * Map REQ packets into a single REQ packet. * * It is useful to reduce REQ requests in a time interval. */ export function batch( /** Function used for merge REQ filters. Default behavior is simple concatenation. */ mergeFilter?: MergeFilter ): OperatorFunction<ReqPacket[], ReqPacket> { return map((f) => f.reduce((acc, v) => { if (acc === null) { return v; } if (v === null) { return acc; } return (mergeFilter ?? defaultMergeFilter)(acc, v); }, null) ); } /** * Chunk a REQ packet into multiple REQ packets. * * It is useful to avoid to send large REQ filter. */ export function chunk( predicate: (f: LazyFilter[]) => boolean, toChunk: (f: LazyFilter[]) => LazyFilter[][] ): MonoTypeOperatorFunction<ReqPacket> { return mergeMap((f) => f !== null && predicate(f) ? of(...toChunk(f)) : of(f) ); } // ----------------- // // General operators // // ----------------- // /** * Almost RxJS's `timeout`, but won't throw. */ export function completeOnTimeout<T>( time: number ): MonoTypeOperatorFunction<T> { return pipe( timeout(time), catchError((error: unknown) => { if (error instanceof TimeoutError) { return EMPTY; } else { throw error; } }) ); } /** * Buffer the received values for a specified time * and return the values in sorted order as possible. */ export function sort<T>( bufferTime: number, compareFn: (a: T, b: T) => number ): MonoTypeOperatorFunction<T> { const buffer: T[] = []; return pipe( tap((v) => { buffer.push(v); buffer.sort(compareFn); }), delay(bufferTime), map(() => { if (buffer.length <= 0) { throw new Error("Logic Error: This is rx-nostr's internal bug."); } // Non-null is valid because the lenght has been checked. // eslint-disable-next-line @typescript-eslint/no-non-null-assertion return buffer.shift()!; }) ); } // ----------- // // Other stuff // // ----------- // export type MergeFilter = (
a: LazyFilter[], b: LazyFilter[]) => LazyFilter[];
function defaultMergeFilter(a: LazyFilter[], b: LazyFilter[]): LazyFilter[] { return [...a, ...b]; } export interface CreateUniqOptions<T> { onCache?: (packet: EventPacket, cache: Set<T>) => void; onHit?: (packet: EventPacket, cache: Set<T>) => void; } export interface FilterByOptions { not: boolean; } const makeFilterByOptions = defineDefaultOptions<FilterByOptions>({ not: false, });
src/operator.ts
penpenpng-rx-nostr-7c52614
[ { "filename": "src/req.ts", "retrieved_chunk": " ): RxReq;\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n pipe(...operators: OperatorFunction<any, any>[]): RxReq {\n const rxReqId = this.rxReqId;\n const strategy = this.strategy;\n return {\n ...this,\n get rxReqId() {\n return rxReqId;\n },", "score": 32.497949221959274 }, { "filename": "src/__test__/operator.test.ts", "retrieved_chunk": " faker.messagePacket(faker.toClientMessage.EVENT(\"*\")),\n ];\n const packet$ = of(...packets).pipe(filterType(\"NOTICE\"));\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n expectObservable(packet$).toEqual(of<any[]>(packets[0], packets[3]));\n });\n});", "score": 31.835121670986155 }, { "filename": "src/util.ts", "retrieved_chunk": "import normalizeUrl from \"normalize-url\";\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport function defineDefaultOptions<T extends Record<string, any>>(\n defaultParams: T\n): (givenParams?: Partial<T>) => T {\n return (givenParams) =>\n Object.fromEntries(\n Object.keys(defaultParams).map((key) => [\n key,\n givenParams?.[key] ?? defaultParams[key],", "score": 29.318637406622567 }, { "filename": "src/req.ts", "retrieved_chunk": " }\n if (Object.keys(res).length <= 0) {\n return null;\n }\n return res;\n}\nfunction normalizeFilters(\n filters: LazyFilter | LazyFilter[] | null\n): LazyFilter[] | null {\n if (!filters) {", "score": 20.153586494424413 }, { "filename": "src/packet.ts", "retrieved_chunk": " */\nexport type LazyFilter = Omit<Nostr.Filter, \"since\" | \"until\"> & {\n since?: number | (() => number);\n until?: number | (() => number);\n};\nexport type LazyREQ = [\"REQ\", string, ...LazyFilter[]];\n/**\n * Packets from websocket that represents an EVENT.\n */\nexport interface EventPacket {", "score": 18.412537700879028 } ]
typescript
a: LazyFilter[], b: LazyFilter[]) => LazyFilter[];
import Nostr from "nostr-typedef"; import { EMPTY, filter, identity, mergeMap, type MonoTypeOperatorFunction, Observable, type ObservableInput, of, retry, Subject, tap, timer, } from "rxjs"; import { evalFilters } from "./helper.js"; import { fetchRelayInfo } from "./index.js"; import { isFiltered } from "./nostr/filter.js"; import { ConnectionState, LazyREQ, MessagePacket } from "./packet.js"; export class Connection { private socket: WebSocket | null = null; private message$ = new Subject<MessagePacket | WebSocketError>(); private error$ = new Subject<unknown>(); private connectionState$ = new Subject<ConnectionState>(); private connectionState: ConnectionState = "not-started"; private queuedEvents: Nostr.ToRelayMessage.EVENT[] = []; private reqs: Map<string /* subId */, ReqState> = new Map(); private serverLimitations: Nostr.Nip11.ServerLimitations | null = null; private canRetry = false; get read() { return this.config.read; } set read(v) { this.config.read = v; } get write() { return this.config.write; } set write(v) { this.config.write = v; } get maxConcurrentReqs(): number | null { return ( this.serverLimitations?.max_subscriptions ?? this.config.maxConcurrentReqsFallback ?? null ); } constructor(public url: string, private config: ConnectionConfig) { this.connectionState$.next("not-started"); } private setConnectionState(state: ConnectionState) { if (this.connectionState === "terminated") { return; } this.connectionState = state; this.connectionState$.next(state); } private async fetchServerLimitationsIfNeeded() { if ( this.config.disableAutoFetchNip11Limitations || this.serverLimitations ) { return; } try { const info = await fetchRelayInfo(this.url); this.serverLimitations = info.limitation ?? null; } catch { // do nothing } } async start() { if ( !this.canRetry && (this.connectionState === "reconnecting" || this.connectionState === "starting" || this.connectionState === "ongoing") ) { return Promise.resolve(); } this.canRetry = false; if (this.connectionState === "not-started") { this.setConnectionState("starting"); } else { this.setConnectionState("reconnecting"); } await this.fetchServerLimitationsIfNeeded(); let completeStartingProcess: () => void; const succeededOrFailed = new Promise<void>((_resolve) => { completeStartingProcess = _resolve; }); const onopen = () => { this.setConnectionState("ongoing"); completeStartingProcess(); for (const event of this.queuedEvents) { this.sendEVENT(event); } this.queuedEvents = []; this.ensureReqs(); }; const onmessage = ({ data }: MessageEvent) => { if (this.connectionState === "terminated") { return; } try { this.message$.next({ from: this.url, message: JSON.parse(data) }); } catch (err) { this.error$.next(err); } }; const onerror = () => { completeStartingProcess(); }; const onclose = ({ code }: CloseEvent) => { if ( code === WebSocketCloseCode.DISPOSED_BY_RX_NOSTR || this.connectionState === "terminated" ) { return; } websocket.removeEventListener("open", onopen); websocket.removeEventListener("message", onmessage); websocket.removeEventListener("error", onerror); websocket.removeEventListener("close", onclose); websocket.close(); this.socket = null; for (const req of this.reqs.values()) { req.isOngoing = false; } if (code === WebSocketCloseCode.DESIRED_BY_RX_NOSTR) { this.setConnectionState("not-started"); } else if (code === WebSocketCloseCode.DONT_RETRY) { this.setConnectionState("rejected"); this.message$.next(new WebSocketError(code)); completeStartingProcess(); } else { this.canRetry = true; this.message$.next(new WebSocketError(code)); completeStartingProcess(); } }; if (this.connectionState === "terminated") { return Promise.resolve(); } const websocket = new WebSocket(this.url); websocket.addEventListener("open", onopen); websocket.addEventListener("message", onmessage); websocket.addEventListener("error", onerror); websocket.addEventListener("close", onclose); this.socket = websocket; return succeededOrFailed; } stop() { this.finalizeAllReqs(); this.socket?.close(WebSocketCloseCode.DESIRED_BY_RX_NOSTR); } getConnectionState() { return this.connectionState; } getMessageObservable(): Observable<MessagePacket> { const reqs = this.reqs; return this.message$.asObservable().pipe( mergeMap((data) => { if (data instanceof WebSocketError) { if (data.code === WebSocketCloseCode.DONT_RETRY) { return EMPTY; } else { throw data; } } else { return of(data); } }), tap({ subscribe: () => { this.start(); }, }), this.config.backoff.strategy === "off" ? identity : retry({ delay: (_, retryCount) => backoffSignal(this.config.backoff, retryCount), count: this.config.backoff.maxCount, }), tap({ error: () => { this.setConnectionState("error"); }, }), rejectFilterUnmatchEvents() ); function rejectFilterUnmatchEvents(): MonoTypeOperatorFunction<MessagePacket> { return filter((packet) => { const [type, subId, event] = packet.message; if (type !== "EVENT") { return true; } const req = reqs.get(subId); if (!req) { return true; } const [, , ...filters] = req.actual; return isFiltered(event, filters); }); } } getConnectionStateObservable() { return this.connectionState$.asObservable(); } getErrorObservable() { return this.error$.asObservable(); } ensureReq(req: LazyREQ, options?: { overwrite?: boolean }) { const subId = req[1]; if (this.connectionState === "terminated") { return; } if (!this.read) { // REQ is not allowed. return; } if (!options?.overwrite) { if (this.reqs.get(subId)?.isOngoing) { // REQ is already ongoing return; } if ( this.reqs.has(subId) && !isConcurrentAllowed(subId, this.reqs, this.maxConcurrentReqs) ) { // REQ is already queued return; } } const message = evalREQ(req); // enqueue or overwrite this.reqs.set(subId, { original: req, actual: message, isOngoing: false, }); if (isConcurrentAllowed(subId, this.reqs, this.maxConcurrentReqs)) { const req = this.reqs.get(subId); if (req && this.socket?.readyState === WebSocket.OPEN) { req.isOngoing = true; this.socket.send(JSON.stringify(message)); } } } private ensureReqs() { const reqs = Array.from(this.reqs.values()).slice( 0, this.maxConcurrentReqs ?? undefined ); for (const req of reqs) { this.ensureReq(req.original); } } finalizeReq(subId: string) { if (this.connectionState === "terminated") { return; } const req = this.reqs.get(subId); if (!req) { return; } this.reqs.delete(subId); if (req.isOngoing) { if (this.socket?.readyState === WebSocket.OPEN) { const message: Nostr.ToRelayMessage.CLOSE = ["CLOSE", subId]; this.socket.send(JSON.stringify(message)); } this.ensureReqs(); } } private finalizeAllReqs() { if (this.connectionState === "terminated") { return; } for (const subId of this.reqs.keys()) { if (this.socket?.readyState === WebSocket.OPEN) { const message: Nostr.ToRelayMessage.CLOSE = ["CLOSE", subId]; this.socket.send(JSON.stringify(message)); } } this.reqs.clear(); } sendEVENT(message: Nostr.ToRelayMessage.EVENT) { if (this.connectionState === "terminated") { return; } if (!this.write) { return; } if (this.socket?.readyState === WebSocket.OPEN) { this.socket.send(JSON.stringify(message)); } else { if (this.socket?.readyState === WebSocket.CONNECTING) { // Enqueue this.queuedEvents.push(message); } else { // Create a temporary socket to send message. const socket = new WebSocket(this.url); socket.addEventListener("open", () => { socket.send(JSON.stringify(message)); }); // Close the temporary socket after receiveing OK or timed out. socket.addEventListener("message", ({ data }) => { try { const response: Nostr.ToClientMessage.Any = JSON.parse(data); if (response[0] === "OK") { socket.close(); } this.message$.next({ from: this.url, message: response }); } catch (err) { this.message$.error(err); } }); setTimeout(() => { if ( socket.readyState === WebSocket.OPEN || socket.readyState === WebSocket.CONNECTING ) { socket.close(); } }, 10 * 1000); } } } dispose() { this.finalizeAllReqs(); this.setConnectionState("terminated"); this.socket?.close(WebSocketCloseCode.DISPOSED_BY_RX_NOSTR); this.socket = null; this.reqs.clear(); this.message$.complete(); this.message$.unsubscribe(); this.connectionState$.complete(); this.connectionState$.unsubscribe(); this.error$.complete(); this.error$.unsubscribe(); } } export const WebSocketCloseCode = { /** * 1006 is a reserved value and MUST NOT be set as a status code in a * Close control frame by an endpoint. It is designated for use in * applications expecting a status code to indicate that the * connection was closed abnormally, e.g., without sending or * receiving a Close control frame. * * See also: https://www.rfc-editor.org/rfc/rfc6455.html#section-7.4.1 */ ABNORMAL_CLOSURE: 1006, /** * When a websocket is closed by the relay with a status code 4000 * that means the client shouldn't try to connect again. * * See also: https://github.com/nostr-protocol/nips/blob/fab6a21a779460f696f11169ddf343b437327592/01.md?plain=1#L113 */ DONT_RETRY: 4000, /** @internal rx-nostr uses it internally. */ DESIRED_BY_RX_NOSTR: 4537, /** @internal rx-nostr uses it internally. */ DISPOSED_BY_RX_NOSTR: 4538, } as const; export interface ConnectionConfig { backoff: BackoffConfig; read: boolean; write: boolean; disableAutoFetchNip11Limitations?: boolean; maxConcurrentReqsFallback?: number; } export type BackoffConfig = | { // Exponential backoff and jitter strategy strategy: "exponential"; maxCount: number; initialDelay: number; } | { // Retry at regular intervals strategy: "linear"; maxCount: number; interval: number; } | { // Retry immediately strategy: "immediately"; maxCount: number; } | { // Won't retry strategy: "off"; }; interface ReqState { original: LazyREQ; actual: Nostr.ToRelayMessage.REQ; isOngoing: boolean; } function backoffSignal( config: BackoffConfig, count: number ): ObservableInput<unknown> { if (config.strategy === "exponential") { const time = Math.max( config.initialDelay * 2 ** (count - 1) + (Math.random() - 0.5) * 1000, 1000 ); return timer(time); } else if (config.strategy === "linear") { return timer(config.interval); } else if (config.strategy === "immediately") { return of(0); } else { return EMPTY; } } function evalREQ([type, subId, ...filters]: LazyREQ): Nostr.ToRelayMessage.REQ {
return [type, subId, ...evalFilters(filters)];
} function isConcurrentAllowed( subId: string, reqs: Map<string, ReqState>, concurrent: number | null ): boolean { if (concurrent === null) { return true; } const reqOrdinal = Array.from(reqs.keys()).findIndex((e) => e === subId); if (reqOrdinal === undefined) { return false; } return reqOrdinal < concurrent; } class WebSocketError extends Error { constructor(public code?: number) { super(`WebSocket Error: Socket was closed with code ${code}`); } }
src/connection.ts
penpenpng-rx-nostr-7c52614
[ { "filename": "src/rx-nostr.ts", "retrieved_chunk": " );\n } else {\n return subId$.pipe(map(createEoseManagedEventObservable), mergeAll());\n }\n function attachSubId(): OperatorFunction<LazyFilter[], LazyREQ> {\n const makeId = (index?: number) => makeSubId({ rxReqId, index });\n switch (strategy) {\n case \"backward\":\n return map((filters, index) => [\"REQ\", makeId(index), ...filters]);\n case \"forward\":", "score": 29.289110430600868 }, { "filename": "src/rx-nostr.ts", "retrieved_chunk": " for (const { req, scope } of this.ongoings.values()) {\n this.ensureReq(req, { scope });\n }\n // --- scoped untility pure functions ---\n function normalizeRelaysConfig(\n config: AcceptableRelaysConfig\n ): RelayConfig[] {\n if (Array.isArray(config)) {\n return config.map((urlOrConfig) => {\n const relay: RelayConfig =", "score": 26.438415841508306 }, { "filename": "src/rx-nostr.ts", "retrieved_chunk": " } else {\n return Object.entries(config).map(([url, flags]) => ({\n url: normalizeRelayUrl(url),\n ...flags,\n }));\n }\n }\n }\n async addRelay(relay: string | RelayConfig): Promise<void> {\n await this.switchRelays([...this.getRelays(), relay]);", "score": 23.791669692198056 }, { "filename": "src/rx-nostr.ts", "retrieved_chunk": " case \"oneshot\":\n return map((filters) => [\"REQ\", makeId(), ...filters]);\n }\n }\n function manageActiveForwardReq(): MonoTypeOperatorFunction<LazyREQ> {\n const recordActiveReq = (req: LazyREQ) => {\n const subId = req[1];\n ongoings.set(subId, {\n req,\n scope,", "score": 21.02299478880606 }, { "filename": "src/nostr/event.ts", "retrieved_chunk": " }\n } else {\n if (seckey) {\n if (seckey.startsWith(\"nsec1\")) {\n return getPublicKey(toHex(seckey));\n } else {\n return getPublicKey(seckey);\n }\n } else {\n const pubkey = await window?.nostr?.getPublicKey();", "score": 20.99525334776062 } ]
typescript
return [type, subId, ...evalFilters(filters)];
import Nostr from "nostr-typedef"; import { EMPTY, filter, identity, mergeMap, type MonoTypeOperatorFunction, Observable, type ObservableInput, of, retry, Subject, tap, timer, } from "rxjs"; import { evalFilters } from "./helper.js"; import { fetchRelayInfo } from "./index.js"; import { isFiltered } from "./nostr/filter.js"; import { ConnectionState, LazyREQ, MessagePacket } from "./packet.js"; export class Connection { private socket: WebSocket | null = null; private message$ = new Subject<MessagePacket | WebSocketError>(); private error$ = new Subject<unknown>(); private connectionState$ = new Subject<ConnectionState>(); private connectionState: ConnectionState = "not-started"; private queuedEvents: Nostr.ToRelayMessage.EVENT[] = []; private reqs: Map<string /* subId */, ReqState> = new Map(); private serverLimitations: Nostr.Nip11.ServerLimitations | null = null; private canRetry = false; get read() { return this.config.read; } set read(v) { this.config.read = v; } get write() { return this.config.write; } set write(v) { this.config.write = v; } get maxConcurrentReqs(): number | null { return ( this.serverLimitations?.max_subscriptions ?? this.config.maxConcurrentReqsFallback ?? null ); } constructor(public url: string, private config: ConnectionConfig) { this.connectionState$.next("not-started"); } private setConnectionState(state: ConnectionState) { if (this.connectionState === "terminated") { return; } this.connectionState = state; this.connectionState$.next(state); } private async fetchServerLimitationsIfNeeded() { if ( this.config.disableAutoFetchNip11Limitations || this.serverLimitations ) { return; } try { const info = await fetchRelayInfo(this.url); this.serverLimitations = info.limitation ?? null; } catch { // do nothing } } async start() { if ( !this.canRetry && (this.connectionState === "reconnecting" || this.connectionState === "starting" || this.connectionState === "ongoing") ) { return Promise.resolve(); } this.canRetry = false; if (this.connectionState === "not-started") { this.setConnectionState("starting"); } else { this.setConnectionState("reconnecting"); } await this.fetchServerLimitationsIfNeeded(); let completeStartingProcess: () => void; const succeededOrFailed = new Promise<void>((_resolve) => { completeStartingProcess = _resolve; }); const onopen = () => { this.setConnectionState("ongoing"); completeStartingProcess(); for (const event of this.queuedEvents) { this.sendEVENT(event); } this.queuedEvents = []; this.ensureReqs(); }; const onmessage = ({ data }: MessageEvent) => { if (this.connectionState === "terminated") { return; } try { this.message$.next({ from: this.url, message: JSON.parse(data) }); } catch (err) { this.error$.next(err); } }; const onerror = () => { completeStartingProcess(); }; const onclose = ({ code }: CloseEvent) => { if ( code === WebSocketCloseCode.DISPOSED_BY_RX_NOSTR || this.connectionState === "terminated" ) { return; } websocket.removeEventListener("open", onopen); websocket.removeEventListener("message", onmessage); websocket.removeEventListener("error", onerror); websocket.removeEventListener("close", onclose); websocket.close(); this.socket = null; for (const req of this.reqs.values()) { req.isOngoing = false; } if (code === WebSocketCloseCode.DESIRED_BY_RX_NOSTR) { this.setConnectionState("not-started"); } else if (code === WebSocketCloseCode.DONT_RETRY) { this.setConnectionState("rejected"); this.message$.next(new WebSocketError(code)); completeStartingProcess(); } else { this.canRetry = true; this.message$.next(new WebSocketError(code)); completeStartingProcess(); } }; if (this.connectionState === "terminated") { return Promise.resolve(); } const websocket = new WebSocket(this.url); websocket.addEventListener("open", onopen); websocket.addEventListener("message", onmessage); websocket.addEventListener("error", onerror); websocket.addEventListener("close", onclose); this.socket = websocket; return succeededOrFailed; } stop() { this.finalizeAllReqs(); this.socket?.close(WebSocketCloseCode.DESIRED_BY_RX_NOSTR); } getConnectionState() { return this.connectionState; } getMessageObservable(): Observable<MessagePacket> { const reqs = this.reqs; return this.message$.asObservable().pipe( mergeMap((data) => { if (data instanceof WebSocketError) { if (data.code === WebSocketCloseCode.DONT_RETRY) { return EMPTY; } else { throw data; } } else { return of(data); } }), tap({ subscribe: () => { this.start(); }, }), this.config.backoff.strategy === "off" ? identity : retry({ delay: (_, retryCount) => backoffSignal(this.config.backoff, retryCount), count: this.config.backoff.maxCount, }), tap({ error: () => { this.setConnectionState("error"); }, }), rejectFilterUnmatchEvents() ); function rejectFilterUnmatchEvents(): MonoTypeOperatorFunction<MessagePacket> { return filter((packet) => { const [type, subId, event] = packet.message; if (type !== "EVENT") { return true; } const req = reqs.get(subId); if (!req) { return true; } const [, , ...filters] = req.actual; return isFiltered(event, filters); }); } } getConnectionStateObservable() { return this.connectionState$.asObservable(); } getErrorObservable() { return this.error$.asObservable(); } ensureReq(
req: LazyREQ, options?: { overwrite?: boolean }) {
const subId = req[1]; if (this.connectionState === "terminated") { return; } if (!this.read) { // REQ is not allowed. return; } if (!options?.overwrite) { if (this.reqs.get(subId)?.isOngoing) { // REQ is already ongoing return; } if ( this.reqs.has(subId) && !isConcurrentAllowed(subId, this.reqs, this.maxConcurrentReqs) ) { // REQ is already queued return; } } const message = evalREQ(req); // enqueue or overwrite this.reqs.set(subId, { original: req, actual: message, isOngoing: false, }); if (isConcurrentAllowed(subId, this.reqs, this.maxConcurrentReqs)) { const req = this.reqs.get(subId); if (req && this.socket?.readyState === WebSocket.OPEN) { req.isOngoing = true; this.socket.send(JSON.stringify(message)); } } } private ensureReqs() { const reqs = Array.from(this.reqs.values()).slice( 0, this.maxConcurrentReqs ?? undefined ); for (const req of reqs) { this.ensureReq(req.original); } } finalizeReq(subId: string) { if (this.connectionState === "terminated") { return; } const req = this.reqs.get(subId); if (!req) { return; } this.reqs.delete(subId); if (req.isOngoing) { if (this.socket?.readyState === WebSocket.OPEN) { const message: Nostr.ToRelayMessage.CLOSE = ["CLOSE", subId]; this.socket.send(JSON.stringify(message)); } this.ensureReqs(); } } private finalizeAllReqs() { if (this.connectionState === "terminated") { return; } for (const subId of this.reqs.keys()) { if (this.socket?.readyState === WebSocket.OPEN) { const message: Nostr.ToRelayMessage.CLOSE = ["CLOSE", subId]; this.socket.send(JSON.stringify(message)); } } this.reqs.clear(); } sendEVENT(message: Nostr.ToRelayMessage.EVENT) { if (this.connectionState === "terminated") { return; } if (!this.write) { return; } if (this.socket?.readyState === WebSocket.OPEN) { this.socket.send(JSON.stringify(message)); } else { if (this.socket?.readyState === WebSocket.CONNECTING) { // Enqueue this.queuedEvents.push(message); } else { // Create a temporary socket to send message. const socket = new WebSocket(this.url); socket.addEventListener("open", () => { socket.send(JSON.stringify(message)); }); // Close the temporary socket after receiveing OK or timed out. socket.addEventListener("message", ({ data }) => { try { const response: Nostr.ToClientMessage.Any = JSON.parse(data); if (response[0] === "OK") { socket.close(); } this.message$.next({ from: this.url, message: response }); } catch (err) { this.message$.error(err); } }); setTimeout(() => { if ( socket.readyState === WebSocket.OPEN || socket.readyState === WebSocket.CONNECTING ) { socket.close(); } }, 10 * 1000); } } } dispose() { this.finalizeAllReqs(); this.setConnectionState("terminated"); this.socket?.close(WebSocketCloseCode.DISPOSED_BY_RX_NOSTR); this.socket = null; this.reqs.clear(); this.message$.complete(); this.message$.unsubscribe(); this.connectionState$.complete(); this.connectionState$.unsubscribe(); this.error$.complete(); this.error$.unsubscribe(); } } export const WebSocketCloseCode = { /** * 1006 is a reserved value and MUST NOT be set as a status code in a * Close control frame by an endpoint. It is designated for use in * applications expecting a status code to indicate that the * connection was closed abnormally, e.g., without sending or * receiving a Close control frame. * * See also: https://www.rfc-editor.org/rfc/rfc6455.html#section-7.4.1 */ ABNORMAL_CLOSURE: 1006, /** * When a websocket is closed by the relay with a status code 4000 * that means the client shouldn't try to connect again. * * See also: https://github.com/nostr-protocol/nips/blob/fab6a21a779460f696f11169ddf343b437327592/01.md?plain=1#L113 */ DONT_RETRY: 4000, /** @internal rx-nostr uses it internally. */ DESIRED_BY_RX_NOSTR: 4537, /** @internal rx-nostr uses it internally. */ DISPOSED_BY_RX_NOSTR: 4538, } as const; export interface ConnectionConfig { backoff: BackoffConfig; read: boolean; write: boolean; disableAutoFetchNip11Limitations?: boolean; maxConcurrentReqsFallback?: number; } export type BackoffConfig = | { // Exponential backoff and jitter strategy strategy: "exponential"; maxCount: number; initialDelay: number; } | { // Retry at regular intervals strategy: "linear"; maxCount: number; interval: number; } | { // Retry immediately strategy: "immediately"; maxCount: number; } | { // Won't retry strategy: "off"; }; interface ReqState { original: LazyREQ; actual: Nostr.ToRelayMessage.REQ; isOngoing: boolean; } function backoffSignal( config: BackoffConfig, count: number ): ObservableInput<unknown> { if (config.strategy === "exponential") { const time = Math.max( config.initialDelay * 2 ** (count - 1) + (Math.random() - 0.5) * 1000, 1000 ); return timer(time); } else if (config.strategy === "linear") { return timer(config.interval); } else if (config.strategy === "immediately") { return of(0); } else { return EMPTY; } } function evalREQ([type, subId, ...filters]: LazyREQ): Nostr.ToRelayMessage.REQ { return [type, subId, ...evalFilters(filters)]; } function isConcurrentAllowed( subId: string, reqs: Map<string, ReqState>, concurrent: number | null ): boolean { if (concurrent === null) { return true; } const reqOrdinal = Array.from(reqs.keys()).findIndex((e) => e === subId); if (reqOrdinal === undefined) { return false; } return reqOrdinal < concurrent; } class WebSocketError extends Error { constructor(public code?: number) { super(`WebSocket Error: Socket was closed with code ${code}`); } }
src/connection.ts
penpenpng-rx-nostr-7c52614
[ { "filename": "src/rx-nostr.ts", "retrieved_chunk": " for (const conn of this.connections.values()) {\n conn.dispose();\n }\n }\n private ensureReq(\n req: LazyREQ,\n options?: { scope?: string[] | null; overwrite?: boolean }\n ) {\n const scope = options?.scope;\n for (const url of this.connections.keys()) {", "score": 24.042426147367372 }, { "filename": "src/rx-nostr.ts", "retrieved_chunk": " maxConcurrentReqsFallback:\n this.options.globalRelayConfig?.maxConcurrentReqsFallback,\n });\n connection.getConnectionStateObservable().subscribe((state) => {\n this.status$.next({\n from: url,\n state,\n });\n });\n connection.getErrorObservable().subscribe((reason) => {", "score": 20.460753107090387 }, { "filename": "src/rx-nostr.ts", "retrieved_chunk": " createAllMessageObservable(): Observable<MessagePacket> {\n return this.messageOut$;\n }\n createConnectionStateObservable(): Observable<ConnectionStatePacket> {\n return this.status$.asObservable();\n }\n send(\n params: Nostr.EventParameters,\n options?: RxNostrSendOptions\n ): Observable<OkPacket> {", "score": 19.03461385037092 }, { "filename": "src/rx-nostr.ts", "retrieved_chunk": " mergeMap(({ from, message }) =>\n message[0] === \"EVENT\"\n ? of({ from, subId: message[1], event: message[2] })\n : EMPTY\n )\n );\n }\n createAllErrorObservable(): Observable<ErrorPacket> {\n return this.error$.asObservable();\n }", "score": 16.70172723445369 }, { "filename": "src/req.ts", "retrieved_chunk": " get rxReqId() {\n return this._rxReqId;\n }\n constructor(rxReqId?: string) {\n this._rxReqId = rxReqId ?? getRandomDigitsString();\n }\n getReqObservable(): Observable<ReqPacket> {\n return this.filters$.asObservable();\n }\n emit(filters: LazyFilter | LazyFilter[] | null) {", "score": 16.094513728272112 } ]
typescript
req: LazyREQ, options?: { overwrite?: boolean }) {
import Nostr from "nostr-typedef"; import { EMPTY, filter, identity, mergeMap, type MonoTypeOperatorFunction, Observable, type ObservableInput, of, retry, Subject, tap, timer, } from "rxjs"; import { evalFilters } from "./helper.js"; import { fetchRelayInfo } from "./index.js"; import { isFiltered } from "./nostr/filter.js"; import { ConnectionState, LazyREQ, MessagePacket } from "./packet.js"; export class Connection { private socket: WebSocket | null = null; private message$ = new Subject<MessagePacket | WebSocketError>(); private error$ = new Subject<unknown>(); private connectionState$ = new Subject<ConnectionState>(); private connectionState: ConnectionState = "not-started"; private queuedEvents: Nostr.ToRelayMessage.EVENT[] = []; private reqs: Map<string /* subId */, ReqState> = new Map(); private serverLimitations: Nostr.Nip11.ServerLimitations | null = null; private canRetry = false; get read() { return this.config.read; } set read(v) { this.config.read = v; } get write() { return this.config.write; } set write(v) { this.config.write = v; } get maxConcurrentReqs(): number | null { return ( this.serverLimitations?.max_subscriptions ?? this.config.maxConcurrentReqsFallback ?? null ); } constructor(public url: string, private config: ConnectionConfig) { this.connectionState$.next("not-started"); } private setConnectionState(state: ConnectionState) { if (this.connectionState === "terminated") { return; } this.connectionState = state; this.connectionState$.next(state); } private async fetchServerLimitationsIfNeeded() { if ( this.config.disableAutoFetchNip11Limitations || this.serverLimitations ) { return; } try { const info = await fetchRelayInfo(this.url); this.serverLimitations = info.limitation ?? null; } catch { // do nothing } } async start() { if ( !this.canRetry && (this.connectionState === "reconnecting" || this.connectionState === "starting" || this.connectionState === "ongoing") ) { return Promise.resolve(); } this.canRetry = false; if (this.connectionState === "not-started") { this.setConnectionState("starting"); } else { this.setConnectionState("reconnecting"); } await this.fetchServerLimitationsIfNeeded(); let completeStartingProcess: () => void; const succeededOrFailed = new Promise<void>((_resolve) => { completeStartingProcess = _resolve; }); const onopen = () => { this.setConnectionState("ongoing"); completeStartingProcess(); for (const event of this.queuedEvents) { this.sendEVENT(event); } this.queuedEvents = []; this.ensureReqs(); }; const onmessage = ({ data }: MessageEvent) => { if (this.connectionState === "terminated") { return; } try { this.message$.next({ from: this.url, message: JSON.parse(data) }); } catch (err) { this.error$.next(err); } }; const onerror = () => { completeStartingProcess(); }; const onclose = ({ code }: CloseEvent) => { if ( code === WebSocketCloseCode.DISPOSED_BY_RX_NOSTR || this.connectionState === "terminated" ) { return; } websocket.removeEventListener("open", onopen); websocket.removeEventListener("message", onmessage); websocket.removeEventListener("error", onerror); websocket.removeEventListener("close", onclose); websocket.close(); this.socket = null; for (const req of this.reqs.values()) { req.isOngoing = false; } if (code === WebSocketCloseCode.DESIRED_BY_RX_NOSTR) { this.setConnectionState("not-started"); } else if (code === WebSocketCloseCode.DONT_RETRY) { this.setConnectionState("rejected"); this.message$.next(new WebSocketError(code)); completeStartingProcess(); } else { this.canRetry = true; this.message$.next(new WebSocketError(code)); completeStartingProcess(); } }; if (this.connectionState === "terminated") { return Promise.resolve(); } const websocket = new WebSocket(this.url); websocket.addEventListener("open", onopen); websocket.addEventListener("message", onmessage); websocket.addEventListener("error", onerror); websocket.addEventListener("close", onclose); this.socket = websocket; return succeededOrFailed; } stop() { this.finalizeAllReqs(); this.socket?.close(WebSocketCloseCode.DESIRED_BY_RX_NOSTR); } getConnectionState() { return this.connectionState; } getMessageObservable(): Observable<MessagePacket> { const reqs = this.reqs; return this.message$.asObservable().pipe( mergeMap((data) => { if (data instanceof WebSocketError) { if (data.code === WebSocketCloseCode.DONT_RETRY) { return EMPTY; } else { throw data; } } else { return of(data); } }), tap({ subscribe: () => { this.start(); }, }), this.config.backoff.strategy === "off" ? identity : retry({ delay: (_, retryCount) => backoffSignal(this.config.backoff, retryCount), count: this.config.backoff.maxCount, }), tap({ error: () => { this.setConnectionState("error"); }, }), rejectFilterUnmatchEvents() ); function rejectFilterUnmatchEvents(): MonoTypeOperatorFunction<MessagePacket> { return filter((packet) => { const [type, subId, event] = packet.message; if (type !== "EVENT") { return true; } const req = reqs.get(subId); if (!req) { return true; } const [, , ...filters] = req.actual; return isFiltered(event, filters); }); } } getConnectionStateObservable() { return this.connectionState$.asObservable(); } getErrorObservable() { return this.error$.asObservable(); } ensureReq(req:
LazyREQ, options?: { overwrite?: boolean }) {
const subId = req[1]; if (this.connectionState === "terminated") { return; } if (!this.read) { // REQ is not allowed. return; } if (!options?.overwrite) { if (this.reqs.get(subId)?.isOngoing) { // REQ is already ongoing return; } if ( this.reqs.has(subId) && !isConcurrentAllowed(subId, this.reqs, this.maxConcurrentReqs) ) { // REQ is already queued return; } } const message = evalREQ(req); // enqueue or overwrite this.reqs.set(subId, { original: req, actual: message, isOngoing: false, }); if (isConcurrentAllowed(subId, this.reqs, this.maxConcurrentReqs)) { const req = this.reqs.get(subId); if (req && this.socket?.readyState === WebSocket.OPEN) { req.isOngoing = true; this.socket.send(JSON.stringify(message)); } } } private ensureReqs() { const reqs = Array.from(this.reqs.values()).slice( 0, this.maxConcurrentReqs ?? undefined ); for (const req of reqs) { this.ensureReq(req.original); } } finalizeReq(subId: string) { if (this.connectionState === "terminated") { return; } const req = this.reqs.get(subId); if (!req) { return; } this.reqs.delete(subId); if (req.isOngoing) { if (this.socket?.readyState === WebSocket.OPEN) { const message: Nostr.ToRelayMessage.CLOSE = ["CLOSE", subId]; this.socket.send(JSON.stringify(message)); } this.ensureReqs(); } } private finalizeAllReqs() { if (this.connectionState === "terminated") { return; } for (const subId of this.reqs.keys()) { if (this.socket?.readyState === WebSocket.OPEN) { const message: Nostr.ToRelayMessage.CLOSE = ["CLOSE", subId]; this.socket.send(JSON.stringify(message)); } } this.reqs.clear(); } sendEVENT(message: Nostr.ToRelayMessage.EVENT) { if (this.connectionState === "terminated") { return; } if (!this.write) { return; } if (this.socket?.readyState === WebSocket.OPEN) { this.socket.send(JSON.stringify(message)); } else { if (this.socket?.readyState === WebSocket.CONNECTING) { // Enqueue this.queuedEvents.push(message); } else { // Create a temporary socket to send message. const socket = new WebSocket(this.url); socket.addEventListener("open", () => { socket.send(JSON.stringify(message)); }); // Close the temporary socket after receiveing OK or timed out. socket.addEventListener("message", ({ data }) => { try { const response: Nostr.ToClientMessage.Any = JSON.parse(data); if (response[0] === "OK") { socket.close(); } this.message$.next({ from: this.url, message: response }); } catch (err) { this.message$.error(err); } }); setTimeout(() => { if ( socket.readyState === WebSocket.OPEN || socket.readyState === WebSocket.CONNECTING ) { socket.close(); } }, 10 * 1000); } } } dispose() { this.finalizeAllReqs(); this.setConnectionState("terminated"); this.socket?.close(WebSocketCloseCode.DISPOSED_BY_RX_NOSTR); this.socket = null; this.reqs.clear(); this.message$.complete(); this.message$.unsubscribe(); this.connectionState$.complete(); this.connectionState$.unsubscribe(); this.error$.complete(); this.error$.unsubscribe(); } } export const WebSocketCloseCode = { /** * 1006 is a reserved value and MUST NOT be set as a status code in a * Close control frame by an endpoint. It is designated for use in * applications expecting a status code to indicate that the * connection was closed abnormally, e.g., without sending or * receiving a Close control frame. * * See also: https://www.rfc-editor.org/rfc/rfc6455.html#section-7.4.1 */ ABNORMAL_CLOSURE: 1006, /** * When a websocket is closed by the relay with a status code 4000 * that means the client shouldn't try to connect again. * * See also: https://github.com/nostr-protocol/nips/blob/fab6a21a779460f696f11169ddf343b437327592/01.md?plain=1#L113 */ DONT_RETRY: 4000, /** @internal rx-nostr uses it internally. */ DESIRED_BY_RX_NOSTR: 4537, /** @internal rx-nostr uses it internally. */ DISPOSED_BY_RX_NOSTR: 4538, } as const; export interface ConnectionConfig { backoff: BackoffConfig; read: boolean; write: boolean; disableAutoFetchNip11Limitations?: boolean; maxConcurrentReqsFallback?: number; } export type BackoffConfig = | { // Exponential backoff and jitter strategy strategy: "exponential"; maxCount: number; initialDelay: number; } | { // Retry at regular intervals strategy: "linear"; maxCount: number; interval: number; } | { // Retry immediately strategy: "immediately"; maxCount: number; } | { // Won't retry strategy: "off"; }; interface ReqState { original: LazyREQ; actual: Nostr.ToRelayMessage.REQ; isOngoing: boolean; } function backoffSignal( config: BackoffConfig, count: number ): ObservableInput<unknown> { if (config.strategy === "exponential") { const time = Math.max( config.initialDelay * 2 ** (count - 1) + (Math.random() - 0.5) * 1000, 1000 ); return timer(time); } else if (config.strategy === "linear") { return timer(config.interval); } else if (config.strategy === "immediately") { return of(0); } else { return EMPTY; } } function evalREQ([type, subId, ...filters]: LazyREQ): Nostr.ToRelayMessage.REQ { return [type, subId, ...evalFilters(filters)]; } function isConcurrentAllowed( subId: string, reqs: Map<string, ReqState>, concurrent: number | null ): boolean { if (concurrent === null) { return true; } const reqOrdinal = Array.from(reqs.keys()).findIndex((e) => e === subId); if (reqOrdinal === undefined) { return false; } return reqOrdinal < concurrent; } class WebSocketError extends Error { constructor(public code?: number) { super(`WebSocket Error: Socket was closed with code ${code}`); } }
src/connection.ts
penpenpng-rx-nostr-7c52614
[ { "filename": "src/rx-nostr.ts", "retrieved_chunk": " for (const conn of this.connections.values()) {\n conn.dispose();\n }\n }\n private ensureReq(\n req: LazyREQ,\n options?: { scope?: string[] | null; overwrite?: boolean }\n ) {\n const scope = options?.scope;\n for (const url of this.connections.keys()) {", "score": 24.042426147367372 }, { "filename": "src/rx-nostr.ts", "retrieved_chunk": " maxConcurrentReqsFallback:\n this.options.globalRelayConfig?.maxConcurrentReqsFallback,\n });\n connection.getConnectionStateObservable().subscribe((state) => {\n this.status$.next({\n from: url,\n state,\n });\n });\n connection.getErrorObservable().subscribe((reason) => {", "score": 20.460753107090387 }, { "filename": "src/rx-nostr.ts", "retrieved_chunk": " createAllMessageObservable(): Observable<MessagePacket> {\n return this.messageOut$;\n }\n createConnectionStateObservable(): Observable<ConnectionStatePacket> {\n return this.status$.asObservable();\n }\n send(\n params: Nostr.EventParameters,\n options?: RxNostrSendOptions\n ): Observable<OkPacket> {", "score": 19.03461385037092 }, { "filename": "src/rx-nostr.ts", "retrieved_chunk": " mergeMap(({ from, message }) =>\n message[0] === \"EVENT\"\n ? of({ from, subId: message[1], event: message[2] })\n : EMPTY\n )\n );\n }\n createAllErrorObservable(): Observable<ErrorPacket> {\n return this.error$.asObservable();\n }", "score": 16.70172723445369 }, { "filename": "src/req.ts", "retrieved_chunk": " get rxReqId() {\n return this._rxReqId;\n }\n constructor(rxReqId?: string) {\n this._rxReqId = rxReqId ?? getRandomDigitsString();\n }\n getReqObservable(): Observable<ReqPacket> {\n return this.filters$.asObservable();\n }\n emit(filters: LazyFilter | LazyFilter[] | null) {", "score": 16.094513728272112 } ]
typescript
LazyREQ, options?: { overwrite?: boolean }) {
import Nostr from "nostr-typedef"; import { BehaviorSubject, Observable, type OperatorFunction } from "rxjs"; import { LazyFilter, ReqPacket } from "./packet.js"; import type { Override } from "./util.js"; /** * The RxReq interface that is provided for RxNostr (**not for users**). */ export interface RxReq<S extends RxReqStrategy = RxReqStrategy> { /** @internal User should not use this directly.The RxReq strategy. It is read-only and must not change. */ get strategy(): S; /** @internal User should not use this directly. Used to construct subId. */ get rxReqId(): string; /** @internal User should not use this directly. Get an Observable of ReqPacket. */ getReqObservable(): Observable<ReqPacket>; } /** * REQ strategy. * * See comments on `createRxForwardReq()`, `createRxBackwardReq()` and `createRxOneshotReq() */ export type RxReqStrategy = "forward" | "backward" | "oneshot"; /** * The RxReq interface that is provided for users (not for RxNostr). */ export interface RxReqController { /** Start new REQ or stop REQ on the RxNostr with witch the RxReq is associated. */ emit(filters: LazyFilter | LazyFilter[] | null): void; /** * Returns itself overriding only `getReqObservable()`. * It is useful for throttling and other control purposes. */ pipe(): RxReq; pipe(op1: OperatorFunction<ReqPacket, ReqPacket>): RxReq; pipe<A>( op1: OperatorFunction<ReqPacket, A>, op2: OperatorFunction<A, ReqPacket> ): RxReq; pipe<A, B>( op1: OperatorFunction<ReqPacket, A>, op2: OperatorFunction<A, B>, op3: OperatorFunction<B, ReqPacket> ): RxReq; pipe<A, B, C>( op1: OperatorFunction<ReqPacket, A>, op2: OperatorFunction<A, B>, op3: OperatorFunction<B, C>, op4: OperatorFunction<C, ReqPacket> ): RxReq; pipe<A, B, C, D>( op1: OperatorFunction<ReqPacket, A>, op2: OperatorFunction<A, B>, op3: OperatorFunction<B, C>, op4: OperatorFunction<C, D>, op5: OperatorFunction<D, ReqPacket> ): RxReq; } abstract class RxReqBase implements RxReq { protected filters$ = new BehaviorSubject<ReqPacket>(null); private _rxReqId: string; abstract get strategy(): RxReqStrategy; get rxReqId() { return this._rxReqId; } constructor(rxReqId?: string) { this._rxReqId = rxReqId ?? getRandomDigitsString(); } getReqObservable(): Observable<ReqPacket> { return this.filters$.asObservable(); } emit(filters: LazyFilter | LazyFilter[] | null) { const normalized = normalizeFilters(filters); if (normalized) { this.filters$.next(normalized); } else { this.filters$.next(null); } } pipe(): RxReq; pipe(op1: OperatorFunction<ReqPacket, ReqPacket>): RxReq; pipe<A>( op1: OperatorFunction<ReqPacket, A>, op2: OperatorFunction<A, ReqPacket> ): RxReq; pipe<A, B>( op1: OperatorFunction<ReqPacket, A>, op2: OperatorFunction<A, B>, op3: OperatorFunction<B, ReqPacket> ): RxReq; pipe<A, B, C>( op1: OperatorFunction<ReqPacket, A>, op2: OperatorFunction<A, B>, op3: OperatorFunction<B, C>, op4: OperatorFunction<C, ReqPacket> ): RxReq; pipe<A, B, C, D>( op1: OperatorFunction<ReqPacket, A>, op2: OperatorFunction<A, B>, op3: OperatorFunction<B, C>, op4: OperatorFunction<C, D>, op5: OperatorFunction<D, ReqPacket> ): RxReq; // eslint-disable-next-line @typescript-eslint/no-explicit-any pipe(...operators: OperatorFunction<any, any>[]): RxReq { const rxReqId = this.rxReqId; const strategy = this.strategy; return { ...this, get rxReqId() { return rxReqId; }, get strategy() { return strategy; }, getReqObservable: () => this.getReqObservable().pipe(...(operators as [])), }; } } /** * Create a RxReq instance based on the backward strategy. * It is useful if you want to retrieve past events that have already been published. * * In backward strategy: * - All REQs have different subIds. * - All REQ-subscriptions keep alive until timeout or getting EOSE. * - In most cases, you should specify `until` or `limit` for filters. * * For more information, see [document](https://penpenpng.github.io/rx-nostr/docs/req-strategy.html#backward-strategy). */ export function createRxBackwardReq( subIdBase?: string ): RxReq<"backward"> & RxReqController { return new RxBackwardReq(subIdBase); } class RxBackwardReq extends RxReqBase implements RxReqController { constructor(rxReqId?: string) { super(rxReqId); } override get strategy(): "backward" { return "backward"; } } /** * Create a RxReq instance based on the forward strategy. * It is useful if you want to listen future events. * * In forward strategy: * - All REQs have the same subId. * - When a new REQ is issued, the old REQ is overwritten and terminated immediately. * The latest REQ keeps alive until it is overwritten or explicitly terminated. * - In most cases, you should not specify `limit` for filters. * * For more information, see [document](https://penpenpng.github.io/rx-nostr/docs/req-strategy.html#forward-strategy). */ export function createRxForwardReq( subId?: string ): RxReq<"forward"> & RxReqController { return new RxForwardReq(subId); } class RxForwardReq extends RxReqBase implements RxReqController { constructor(rxReqId?: string) { super(rxReqId); } override get strategy(): "forward" { return "forward"; } } /** * Create a RxReq instance based on the oneshot strategy. * It is almost the same as backward strategy, however can publish only one REQ * and the Observable completes on EOSE. * * For more information, see [document](https://penpenpng.github.io/rx-nostr/docs/req-strategy.html#oneshot-strategy). */ export function createRxOneshotReq(req: { filters: LazyFilter | LazyFilter[]; subId?: string; }): RxReq<"oneshot"> { return new RxOneshotReq(req); } class RxOneshotReq extends RxReqBase { constructor(req: { filters: LazyFilter | LazyFilter[]; subId?: string }) { super(req?.subId); this.emit(req.filters); } override get strategy(): "oneshot" { return "oneshot"; } } export interface Mixin<R, T> { (): ThisType<R> & T; } /** NOTE: unstable feature */ export function mixin<R extends object, T extends object>( def: () => ThisType<R> & T ): Mixin<R, T> { return def; } /** NOTE: unstable feature */ export function extend<B extends R, R extends object, T extends object>( base: B, mixin: Mixin<R, T> ): Override<B, T> { return Object.assign(base, mixin()) as Override<B, T>; } function getRandomDigitsString() { return `${Math.floor(Math.random() * 1000000)}`; } function normalizeFilter(filter: LazyFilter): LazyFilter | null { const res: LazyFilter = {}; const isTagName = (s: string): s is Nostr.TagName => /^#[a-zA-Z]$/.test(s); for (const key of Object.keys(filter)) { if (key === "limit" && (filter[key] ?? -1) >= 0) { res[key] = filter[key]; continue; } if (key === "since" || key === "until") { const f = filter[key]; if (typeof f !== "number" || (f ?? -1) >= 0) { res[key] = f; continue; } } if ( (isTagName(key) || key === "ids" || key === "authors") && filter[key] !== undefined && (filter[key]?.length ?? -1) > 0 ) { res[key] = filter[key]; continue; } if ( key === "kinds" && filter[key] !== undefined && (filter[key]?.length ?? -1) > 0 ) { res[key] = filter[key]; continue; } if (key === "search" && filter[key] !== undefined) { res[key] = filter[key]; continue; } } const timeRangeIsValid = typeof res
.since !== "number" || typeof res.until !== "number" || res.since <= res.until;
if (!timeRangeIsValid) { return null; } if (Object.keys(res).length <= 0) { return null; } return res; } function normalizeFilters( filters: LazyFilter | LazyFilter[] | null ): LazyFilter[] | null { if (!filters) { return null; } const normalized = (Array.isArray(filters) ? filters : [filters]).flatMap( (e) => normalizeFilter(e) ?? [] ); return normalized.length > 0 ? normalized : null; }
src/req.ts
penpenpng-rx-nostr-7c52614
[ { "filename": "src/helper.ts", "retrieved_chunk": " return filters.map(evalFilter);\n } else {\n return [evalFilter(filters)];\n }\n}\nfunction evalFilter(filter: LazyFilter): Nostr.Filter {\n return {\n ...filter,\n since: filter.since ? evalLazyNumber(filter.since) : undefined,\n until: filter.until ? evalLazyNumber(filter.until) : undefined,", "score": 36.90930928989603 }, { "filename": "src/nostr/filter.ts", "retrieved_chunk": " return false;\n }\n if (\n filter.until &&\n ((untilInclusive && !(event.created_at <= filter.until)) ||\n (!untilInclusive && !(event.created_at < filter.until)))\n ) {\n return false;\n }\n for (const [key, needleValues] of Object.entries(filter)) {", "score": 34.84736819106094 }, { "filename": "src/nostr/nip11.ts", "retrieved_chunk": " headers: { Accept: \"application/nostr+json\" },\n });\n return res.json();\n}", "score": 34.37581438140943 }, { "filename": "src/packet.ts", "retrieved_chunk": " */\nexport type LazyFilter = Omit<Nostr.Filter, \"since\" | \"until\"> & {\n since?: number | (() => number);\n until?: number | (() => number);\n};\nexport type LazyREQ = [\"REQ\", string, ...LazyFilter[]];\n/**\n * Packets from websocket that represents an EVENT.\n */\nexport interface EventPacket {", "score": 28.815281535821015 }, { "filename": "src/operator.ts", "retrieved_chunk": " options?: CreateUniqOptions<T>\n): [MonoTypeOperatorFunction<EventPacket>, Set<T>] {\n const cache = new Set<T>();\n return [\n filter((packet) => {\n const key = keyFn(packet);\n if (key === null) {\n return true;\n }\n if (cache.has(key)) {", "score": 26.600303781440488 } ]
typescript
.since !== "number" || typeof res.until !== "number" || res.since <= res.until;
import Nostr from "nostr-typedef"; import { catchError, delay, distinct, distinctUntilChanged, EMPTY, filter, groupBy, map, mergeAll, mergeMap, type MonoTypeOperatorFunction, type ObservableInput, of, type OperatorFunction, pipe, scan, tap, timeout, TimeoutError, } from "rxjs"; import { evalFilters } from "./helper.js"; import { compareEvents, verify as _verify } from "./nostr/event.js"; import { isFiltered, MatchFilterOptions } from "./nostr/filter.js"; import { EventPacket, LazyFilter, MessagePacket, ReqPacket } from "./packet.js"; import { defineDefaultOptions } from "./util.js"; // --------------------- // // EventPacket operators // // --------------------- // /** * Remove the events once seen. */ export function uniq( flushes?: ObservableInput<unknown> ): MonoTypeOperatorFunction<EventPacket> { return distinct<EventPacket, string>(({ event }) => event.id, flushes); } /** * Create a customizable uniq operator. * * If `keyFn()` returns a non-null key, the key is stored in `Set`. * The operator filters packets with keys already stored. * * The `Set` returned in the second value of the tuple * can be manipulated externally or in optional event handlers. * For example, you can call `Set#clear()` to forget all keys. */ export function createUniq<T>( keyFn: (packet: EventPacket) => T | null, options?: CreateUniqOptions<T> ): [MonoTypeOperatorFunction<EventPacket>, Set<T>] { const cache = new Set<T>(); return [ filter((packet) => { const key = keyFn(packet); if (key === null) { return true; } if (cache.has(key)) { options?.onHit?.(packet, cache); return false; } else { cache.add(key); options?.onCache?.(packet, cache); return true; } }), cache, ]; } /** * Only the latest events are allowed to pass. */ export function latest(): MonoTypeOperatorFunction<EventPacket> { return pipe( scan<EventPacket>((acc, packet) => compareEvents(acc.event, packet.event) < 0 ? packet : acc ), distinctUntilChanged( (a, b) => a === b, ({ event }) => event.id ) ); } /** * For each key, only the latest events are allowed to pass. */ export function latestEach<K>( key: (packet: EventPacket) => K ): MonoTypeOperatorFunction<EventPacket> { return pipe(groupBy(key), map(pipe(latest())), mergeAll()); } /** * Only events with a valid signature are allowed to pass. */ export function verify(): MonoTypeOperatorFunction<EventPacket> { return filter<EventPacket>(({ event }) => _verify(event)); } /** * Only events with given kind are allowed to pass. */ export function filterKind<K extends number>( kind: K ): MonoTypeOperatorFunction<EventPacket> { return filter<EventPacket>(({ event }) => event.kind === kind); } /** * Filter events based on a REQ filter object. */ export function filterBy( filters: LazyFilter | LazyFilter[], options?: MatchFilterOptions & FilterByOptions ): MonoTypeOperatorFunction<EventPacket> { const { not } = makeFilterByOptions(options); const evaledFilter = evalFilters(filters); return filter(({ event }) => { const match = isFiltered(event, evaledFilter, options); return not ? !match : match; }); } /** * Accumulate latest events in order of new arrival (based on `created_at`). */ export function timeline( limit?: number ): OperatorFunction<EventPacket, EventPacket[]> { return scan<EventPacket, EventPacket[]>((acc, packet) => { const next = [...acc, packet].sort( (a, b) => -1 * compareEvents(a.event, b.event) ); if (limit !== undefined) { next.splice(limit); } return next; }, []); } export function sortEvents( bufferTime: number, compareFn?: (a: EventPacket, b: EventPacket) => number ): MonoTypeOperatorFunction<EventPacket> { return sort( bufferTime, compareFn ?? ((a, b) => compareEvents(a.event, b.event)) ); } // ----------------------- // // MessagePacket operators // // ----------------------- // export function filterType<T extends Nostr.ToClientMessage.Type>( type: T ): OperatorFunction< MessagePacket, MessagePacket<Nostr.ToClientMessage.Message<T>> > { return filter( (packet): packet is MessagePacket<Nostr.ToClientMessage.Message<T>> => packet.message[0] === type ); } // ------------------- // // ReqPacket operators // // ------------------- // /** * Map REQ packets into a single REQ packet. * * It is useful to reduce REQ requests in a time interval. */ export function batch( /** Function used for merge REQ filters. Default behavior is simple concatenation. */ mergeFilter?: MergeFilter
): OperatorFunction<ReqPacket[], ReqPacket> {
return map((f) => f.reduce((acc, v) => { if (acc === null) { return v; } if (v === null) { return acc; } return (mergeFilter ?? defaultMergeFilter)(acc, v); }, null) ); } /** * Chunk a REQ packet into multiple REQ packets. * * It is useful to avoid to send large REQ filter. */ export function chunk( predicate: (f: LazyFilter[]) => boolean, toChunk: (f: LazyFilter[]) => LazyFilter[][] ): MonoTypeOperatorFunction<ReqPacket> { return mergeMap((f) => f !== null && predicate(f) ? of(...toChunk(f)) : of(f) ); } // ----------------- // // General operators // // ----------------- // /** * Almost RxJS's `timeout`, but won't throw. */ export function completeOnTimeout<T>( time: number ): MonoTypeOperatorFunction<T> { return pipe( timeout(time), catchError((error: unknown) => { if (error instanceof TimeoutError) { return EMPTY; } else { throw error; } }) ); } /** * Buffer the received values for a specified time * and return the values in sorted order as possible. */ export function sort<T>( bufferTime: number, compareFn: (a: T, b: T) => number ): MonoTypeOperatorFunction<T> { const buffer: T[] = []; return pipe( tap((v) => { buffer.push(v); buffer.sort(compareFn); }), delay(bufferTime), map(() => { if (buffer.length <= 0) { throw new Error("Logic Error: This is rx-nostr's internal bug."); } // Non-null is valid because the lenght has been checked. // eslint-disable-next-line @typescript-eslint/no-non-null-assertion return buffer.shift()!; }) ); } // ----------- // // Other stuff // // ----------- // export type MergeFilter = (a: LazyFilter[], b: LazyFilter[]) => LazyFilter[]; function defaultMergeFilter(a: LazyFilter[], b: LazyFilter[]): LazyFilter[] { return [...a, ...b]; } export interface CreateUniqOptions<T> { onCache?: (packet: EventPacket, cache: Set<T>) => void; onHit?: (packet: EventPacket, cache: Set<T>) => void; } export interface FilterByOptions { not: boolean; } const makeFilterByOptions = defineDefaultOptions<FilterByOptions>({ not: false, });
src/operator.ts
penpenpng-rx-nostr-7c52614
[ { "filename": "src/req.ts", "retrieved_chunk": " }\n}\n/**\n * Create a RxReq instance based on the forward strategy.\n * It is useful if you want to listen future events.\n *\n * In forward strategy:\n * - All REQs have the same subId.\n * - When a new REQ is issued, the old REQ is overwritten and terminated immediately.\n * The latest REQ keeps alive until it is overwritten or explicitly terminated.", "score": 28.65818688799521 }, { "filename": "src/req.ts", "retrieved_chunk": "export type RxReqStrategy = \"forward\" | \"backward\" | \"oneshot\";\n/**\n * The RxReq interface that is provided for users (not for RxNostr).\n */\nexport interface RxReqController {\n /** Start new REQ or stop REQ on the RxNostr with witch the RxReq is associated. */\n emit(filters: LazyFilter | LazyFilter[] | null): void;\n /**\n * Returns itself overriding only `getReqObservable()`.\n * It is useful for throttling and other control purposes.", "score": 25.493932427304593 }, { "filename": "src/connection.ts", "retrieved_chunk": " this.connectionState$.unsubscribe();\n this.error$.complete();\n this.error$.unsubscribe();\n }\n}\nexport const WebSocketCloseCode = {\n /**\n * 1006 is a reserved value and MUST NOT be set as a status code in a\n * Close control frame by an endpoint. It is designated for use in\n * applications expecting a status code to indicate that the", "score": 21.435357130552564 }, { "filename": "src/rx-nostr.ts", "retrieved_chunk": " */\n getRelays(): RelayConfig[];\n /**\n * Set the list of relays.\n * If a REQ subscription already exists, the same REQ is issued for the newly added relay\n * and CLOSE is sent for the removed relay.\n */\n switchRelays(config: AcceptableRelaysConfig): Promise<void>;\n /** Utility wrapper for `switchRelays()`. */\n addRelay(relay: string | RelayConfig): Promise<void>;", "score": 20.241843166968486 }, { "filename": "src/rx-nostr.ts", "retrieved_chunk": "/** Create a RxNostr object. This is the only way to create that. */\nexport function createRxNostr(options?: Partial<RxNostrOptions>): RxNostr {\n return new RxNostrImpl(options);\n}\nexport interface RxNostrOptions {\n /** Auto reconnection strategy. */\n retry: BackoffConfig;\n /**\n * The time in milliseconds to timeout when following the backward strategy.\n * The observable is terminated when the specified amount of time has elapsed", "score": 19.84778247072039 } ]
typescript
): OperatorFunction<ReqPacket[], ReqPacket> {
import Nostr from "nostr-typedef"; import { BehaviorSubject, Observable, type OperatorFunction } from "rxjs"; import { LazyFilter, ReqPacket } from "./packet.js"; import type { Override } from "./util.js"; /** * The RxReq interface that is provided for RxNostr (**not for users**). */ export interface RxReq<S extends RxReqStrategy = RxReqStrategy> { /** @internal User should not use this directly.The RxReq strategy. It is read-only and must not change. */ get strategy(): S; /** @internal User should not use this directly. Used to construct subId. */ get rxReqId(): string; /** @internal User should not use this directly. Get an Observable of ReqPacket. */ getReqObservable(): Observable<ReqPacket>; } /** * REQ strategy. * * See comments on `createRxForwardReq()`, `createRxBackwardReq()` and `createRxOneshotReq() */ export type RxReqStrategy = "forward" | "backward" | "oneshot"; /** * The RxReq interface that is provided for users (not for RxNostr). */ export interface RxReqController { /** Start new REQ or stop REQ on the RxNostr with witch the RxReq is associated. */ emit(filters: LazyFilter | LazyFilter[] | null): void; /** * Returns itself overriding only `getReqObservable()`. * It is useful for throttling and other control purposes. */ pipe(): RxReq; pipe(op1: OperatorFunction<ReqPacket, ReqPacket>): RxReq; pipe<A>( op1: OperatorFunction<ReqPacket, A>, op2: OperatorFunction<A, ReqPacket> ): RxReq; pipe<A, B>( op1: OperatorFunction<ReqPacket, A>, op2: OperatorFunction<A, B>, op3: OperatorFunction<B, ReqPacket> ): RxReq; pipe<A, B, C>( op1: OperatorFunction<ReqPacket, A>, op2: OperatorFunction<A, B>, op3: OperatorFunction<B, C>, op4: OperatorFunction<C, ReqPacket> ): RxReq; pipe<A, B, C, D>( op1: OperatorFunction<ReqPacket, A>, op2: OperatorFunction<A, B>, op3: OperatorFunction<B, C>, op4: OperatorFunction<C, D>, op5: OperatorFunction<D, ReqPacket> ): RxReq; } abstract class RxReqBase implements RxReq { protected filters$ = new BehaviorSubject<ReqPacket>(null); private _rxReqId: string; abstract get strategy(): RxReqStrategy; get rxReqId() { return this._rxReqId; } constructor(rxReqId?: string) { this._rxReqId = rxReqId ?? getRandomDigitsString(); } getReqObservable(): Observable<ReqPacket> { return this.filters$.asObservable(); } emit(filters: LazyFilter | LazyFilter[] | null) { const normalized = normalizeFilters(filters); if (normalized) { this.filters$.next(normalized); } else { this.filters$.next(null); } } pipe(): RxReq; pipe(op1: OperatorFunction<ReqPacket, ReqPacket>): RxReq; pipe<A>( op1: OperatorFunction<ReqPacket, A>, op2: OperatorFunction<A, ReqPacket> ): RxReq; pipe<A, B>( op1: OperatorFunction<ReqPacket, A>, op2: OperatorFunction<A, B>, op3: OperatorFunction<B, ReqPacket> ): RxReq; pipe<A, B, C>( op1: OperatorFunction<ReqPacket, A>, op2: OperatorFunction<A, B>, op3: OperatorFunction<B, C>, op4: OperatorFunction<C, ReqPacket> ): RxReq; pipe<A, B, C, D>( op1: OperatorFunction<ReqPacket, A>, op2: OperatorFunction<A, B>, op3: OperatorFunction<B, C>, op4: OperatorFunction<C, D>, op5: OperatorFunction<D, ReqPacket> ): RxReq; // eslint-disable-next-line @typescript-eslint/no-explicit-any pipe(...operators: OperatorFunction<any, any>[]): RxReq { const rxReqId = this.rxReqId; const strategy = this.strategy; return { ...this, get rxReqId() { return rxReqId; }, get strategy() { return strategy; }, getReqObservable: () => this.getReqObservable().pipe(...(operators as [])), }; } } /** * Create a RxReq instance based on the backward strategy. * It is useful if you want to retrieve past events that have already been published. * * In backward strategy: * - All REQs have different subIds. * - All REQ-subscriptions keep alive until timeout or getting EOSE. * - In most cases, you should specify `until` or `limit` for filters. * * For more information, see [document](https://penpenpng.github.io/rx-nostr/docs/req-strategy.html#backward-strategy). */ export function createRxBackwardReq( subIdBase?: string ): RxReq<"backward"> & RxReqController { return new RxBackwardReq(subIdBase); } class RxBackwardReq extends RxReqBase implements RxReqController { constructor(rxReqId?: string) { super(rxReqId); } override get strategy(): "backward" { return "backward"; } } /** * Create a RxReq instance based on the forward strategy. * It is useful if you want to listen future events. * * In forward strategy: * - All REQs have the same subId. * - When a new REQ is issued, the old REQ is overwritten and terminated immediately. * The latest REQ keeps alive until it is overwritten or explicitly terminated. * - In most cases, you should not specify `limit` for filters. * * For more information, see [document](https://penpenpng.github.io/rx-nostr/docs/req-strategy.html#forward-strategy). */ export function createRxForwardReq( subId?: string ): RxReq<"forward"> & RxReqController { return new RxForwardReq(subId); } class RxForwardReq extends RxReqBase implements RxReqController { constructor(rxReqId?: string) { super(rxReqId); } override get strategy(): "forward" { return "forward"; } } /** * Create a RxReq instance based on the oneshot strategy. * It is almost the same as backward strategy, however can publish only one REQ * and the Observable completes on EOSE. * * For more information, see [document](https://penpenpng.github.io/rx-nostr/docs/req-strategy.html#oneshot-strategy). */ export function createRxOneshotReq(req: { filters: LazyFilter | LazyFilter[]; subId?: string; }): RxReq<"oneshot"> { return new RxOneshotReq(req); } class RxOneshotReq extends RxReqBase { constructor(req: { filters: LazyFilter | LazyFilter[]; subId?: string }) { super(req?.subId); this.emit(req.filters); } override get strategy(): "oneshot" { return "oneshot"; } } export interface Mixin<R, T> { (): ThisType<R> & T; } /** NOTE: unstable feature */ export function mixin<R extends object, T extends object>( def: () => ThisType<R> & T ): Mixin<R, T> { return def; } /** NOTE: unstable feature */ export function extend<B extends R, R extends object, T extends object>( base: B, mixin: Mixin<R, T> ): Override<B, T> { return Object.assign(base, mixin()) as Override<B, T>; } function getRandomDigitsString() { return `${Math.floor(Math.random() * 1000000)}`; } function normalizeFilter(filter: LazyFilter): LazyFilter | null { const res: LazyFilter = {}; const isTagName = (s: string): s is Nostr.TagName => /^#[a-zA-Z]$/.test(s); for (const key of Object.keys(filter)) { if (key === "limit" && (filter[key] ?? -1) >= 0) { res[key] = filter[key]; continue; } if (key === "since" || key === "until") {
const f = filter[key];
if (typeof f !== "number" || (f ?? -1) >= 0) { res[key] = f; continue; } } if ( (isTagName(key) || key === "ids" || key === "authors") && filter[key] !== undefined && (filter[key]?.length ?? -1) > 0 ) { res[key] = filter[key]; continue; } if ( key === "kinds" && filter[key] !== undefined && (filter[key]?.length ?? -1) > 0 ) { res[key] = filter[key]; continue; } if (key === "search" && filter[key] !== undefined) { res[key] = filter[key]; continue; } } const timeRangeIsValid = typeof res.since !== "number" || typeof res.until !== "number" || res.since <= res.until; if (!timeRangeIsValid) { return null; } if (Object.keys(res).length <= 0) { return null; } return res; } function normalizeFilters( filters: LazyFilter | LazyFilter[] | null ): LazyFilter[] | null { if (!filters) { return null; } const normalized = (Array.isArray(filters) ? filters : [filters]).flatMap( (e) => normalizeFilter(e) ?? [] ); return normalized.length > 0 ? normalized : null; }
src/req.ts
penpenpng-rx-nostr-7c52614
[ { "filename": "src/nostr/filter.ts", "retrieved_chunk": " return false;\n }\n if (\n filter.until &&\n ((untilInclusive && !(event.created_at <= filter.until)) ||\n (!untilInclusive && !(event.created_at < filter.until)))\n ) {\n return false;\n }\n for (const [key, needleValues] of Object.entries(filter)) {", "score": 63.14659906527308 }, { "filename": "src/operator.ts", "retrieved_chunk": " options?: CreateUniqOptions<T>\n): [MonoTypeOperatorFunction<EventPacket>, Set<T>] {\n const cache = new Set<T>();\n return [\n filter((packet) => {\n const key = keyFn(packet);\n if (key === null) {\n return true;\n }\n if (cache.has(key)) {", "score": 58.361547894579076 }, { "filename": "src/util.ts", "retrieved_chunk": "import normalizeUrl from \"normalize-url\";\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport function defineDefaultOptions<T extends Record<string, any>>(\n defaultParams: T\n): (givenParams?: Partial<T>) => T {\n return (givenParams) =>\n Object.fromEntries(\n Object.keys(defaultParams).map((key) => [\n key,\n givenParams?.[key] ?? defaultParams[key],", "score": 51.35367146321939 }, { "filename": "src/nostr/filter.ts", "retrieved_chunk": " if (!key.startsWith(\"#\") || !Array.isArray(needleValues)) {\n continue;\n }\n const needleTagName = key.slice(1);\n if (\n !event.tags.find(\n ([tagName, tagValue]) =>\n needleTagName === tagName &&\n (needleValues as string[]).includes(tagValue)\n )", "score": 49.61471864852044 }, { "filename": "src/helper.ts", "retrieved_chunk": " return filters.map(evalFilter);\n } else {\n return [evalFilter(filters)];\n }\n}\nfunction evalFilter(filter: LazyFilter): Nostr.Filter {\n return {\n ...filter,\n since: filter.since ? evalLazyNumber(filter.since) : undefined,\n until: filter.until ? evalLazyNumber(filter.until) : undefined,", "score": 44.590486821397334 } ]
typescript
const f = filter[key];
import Nostr from "nostr-typedef"; import { catchError, delay, distinct, distinctUntilChanged, EMPTY, filter, groupBy, map, mergeAll, mergeMap, type MonoTypeOperatorFunction, type ObservableInput, of, type OperatorFunction, pipe, scan, tap, timeout, TimeoutError, } from "rxjs"; import { evalFilters } from "./helper.js"; import { compareEvents, verify as _verify } from "./nostr/event.js"; import { isFiltered, MatchFilterOptions } from "./nostr/filter.js"; import { EventPacket, LazyFilter, MessagePacket, ReqPacket } from "./packet.js"; import { defineDefaultOptions } from "./util.js"; // --------------------- // // EventPacket operators // // --------------------- // /** * Remove the events once seen. */ export function uniq( flushes?: ObservableInput<unknown> ): MonoTypeOperatorFunction<EventPacket> { return distinct<EventPacket, string>(({ event }) => event.id, flushes); } /** * Create a customizable uniq operator. * * If `keyFn()` returns a non-null key, the key is stored in `Set`. * The operator filters packets with keys already stored. * * The `Set` returned in the second value of the tuple * can be manipulated externally or in optional event handlers. * For example, you can call `Set#clear()` to forget all keys. */ export function createUniq<T>( keyFn: (packet: EventPacket) => T | null, options?: CreateUniqOptions<T> ): [MonoTypeOperatorFunction<EventPacket>, Set<T>] { const cache = new Set<T>(); return [ filter((packet) => { const key = keyFn(packet); if (key === null) { return true; } if (cache.has(key)) { options?.onHit?.(packet, cache); return false; } else { cache.add(key); options?.onCache?.(packet, cache); return true; } }), cache, ]; } /** * Only the latest events are allowed to pass. */ export function latest(): MonoTypeOperatorFunction<EventPacket> { return pipe( scan<EventPacket>((acc, packet) => compareEvents(acc.event, packet.event) < 0 ? packet : acc ), distinctUntilChanged( (a, b) => a === b, ({ event }) => event.id ) ); } /** * For each key, only the latest events are allowed to pass. */ export function latestEach<K>( key: (packet: EventPacket) => K ): MonoTypeOperatorFunction<EventPacket> { return pipe(groupBy(key), map(pipe(latest())), mergeAll()); } /** * Only events with a valid signature are allowed to pass. */ export function verify(): MonoTypeOperatorFunction<EventPacket> { return filter<EventPacket>((
{ event }) => _verify(event));
} /** * Only events with given kind are allowed to pass. */ export function filterKind<K extends number>( kind: K ): MonoTypeOperatorFunction<EventPacket> { return filter<EventPacket>(({ event }) => event.kind === kind); } /** * Filter events based on a REQ filter object. */ export function filterBy( filters: LazyFilter | LazyFilter[], options?: MatchFilterOptions & FilterByOptions ): MonoTypeOperatorFunction<EventPacket> { const { not } = makeFilterByOptions(options); const evaledFilter = evalFilters(filters); return filter(({ event }) => { const match = isFiltered(event, evaledFilter, options); return not ? !match : match; }); } /** * Accumulate latest events in order of new arrival (based on `created_at`). */ export function timeline( limit?: number ): OperatorFunction<EventPacket, EventPacket[]> { return scan<EventPacket, EventPacket[]>((acc, packet) => { const next = [...acc, packet].sort( (a, b) => -1 * compareEvents(a.event, b.event) ); if (limit !== undefined) { next.splice(limit); } return next; }, []); } export function sortEvents( bufferTime: number, compareFn?: (a: EventPacket, b: EventPacket) => number ): MonoTypeOperatorFunction<EventPacket> { return sort( bufferTime, compareFn ?? ((a, b) => compareEvents(a.event, b.event)) ); } // ----------------------- // // MessagePacket operators // // ----------------------- // export function filterType<T extends Nostr.ToClientMessage.Type>( type: T ): OperatorFunction< MessagePacket, MessagePacket<Nostr.ToClientMessage.Message<T>> > { return filter( (packet): packet is MessagePacket<Nostr.ToClientMessage.Message<T>> => packet.message[0] === type ); } // ------------------- // // ReqPacket operators // // ------------------- // /** * Map REQ packets into a single REQ packet. * * It is useful to reduce REQ requests in a time interval. */ export function batch( /** Function used for merge REQ filters. Default behavior is simple concatenation. */ mergeFilter?: MergeFilter ): OperatorFunction<ReqPacket[], ReqPacket> { return map((f) => f.reduce((acc, v) => { if (acc === null) { return v; } if (v === null) { return acc; } return (mergeFilter ?? defaultMergeFilter)(acc, v); }, null) ); } /** * Chunk a REQ packet into multiple REQ packets. * * It is useful to avoid to send large REQ filter. */ export function chunk( predicate: (f: LazyFilter[]) => boolean, toChunk: (f: LazyFilter[]) => LazyFilter[][] ): MonoTypeOperatorFunction<ReqPacket> { return mergeMap((f) => f !== null && predicate(f) ? of(...toChunk(f)) : of(f) ); } // ----------------- // // General operators // // ----------------- // /** * Almost RxJS's `timeout`, but won't throw. */ export function completeOnTimeout<T>( time: number ): MonoTypeOperatorFunction<T> { return pipe( timeout(time), catchError((error: unknown) => { if (error instanceof TimeoutError) { return EMPTY; } else { throw error; } }) ); } /** * Buffer the received values for a specified time * and return the values in sorted order as possible. */ export function sort<T>( bufferTime: number, compareFn: (a: T, b: T) => number ): MonoTypeOperatorFunction<T> { const buffer: T[] = []; return pipe( tap((v) => { buffer.push(v); buffer.sort(compareFn); }), delay(bufferTime), map(() => { if (buffer.length <= 0) { throw new Error("Logic Error: This is rx-nostr's internal bug."); } // Non-null is valid because the lenght has been checked. // eslint-disable-next-line @typescript-eslint/no-non-null-assertion return buffer.shift()!; }) ); } // ----------- // // Other stuff // // ----------- // export type MergeFilter = (a: LazyFilter[], b: LazyFilter[]) => LazyFilter[]; function defaultMergeFilter(a: LazyFilter[], b: LazyFilter[]): LazyFilter[] { return [...a, ...b]; } export interface CreateUniqOptions<T> { onCache?: (packet: EventPacket, cache: Set<T>) => void; onHit?: (packet: EventPacket, cache: Set<T>) => void; } export interface FilterByOptions { not: boolean; } const makeFilterByOptions = defineDefaultOptions<FilterByOptions>({ not: false, });
src/operator.ts
penpenpng-rx-nostr-7c52614
[ { "filename": "src/__test__/helper.ts", "retrieved_chunk": " ..._faker,\n eventPacket(\n packetOrEvent?: Partial<\n EventPacket[\"event\"] &\n Omit<EventPacket, \"event\"> & {\n event?: Partial<EventPacket[\"event\"]>;\n }\n >\n ): EventPacket {\n return {", "score": 29.606051342223118 }, { "filename": "src/__test__/helper.ts", "retrieved_chunk": " tap((packet) => {\n tapNext(packet.message);\n }),\n ...spy,\n };\n}\nexport function spyEvent(): {\n tap: () => MonoTypeOperatorFunction<EventPacket>;\n} {\n let tapNext: (message: Nostr.ToClientMessage.Any) => void;", "score": 28.711464008495334 }, { "filename": "src/rx-nostr.ts", "retrieved_chunk": " function pickEvents(): OperatorFunction<MessagePacket, EventPacket> {\n return mergeMap(({ from, message }) =>\n message[0] === \"EVENT\"\n ? of({ from, subId: message[1], event: message[2] })\n : EMPTY\n );\n }\n }\n createAllEventObservable(): Observable<EventPacket> {\n return this.messageOut$.pipe(", "score": 26.220649655547884 }, { "filename": "src/rx-nostr.ts", "retrieved_chunk": " setGlobalEventPacketPipe(\n pipe: MonoTypeOperatorFunction<EventPacket> | null\n ): void;\n /**\n * Associate RxReq with RxNostr.\n * When the associated RxReq is manipulated,\n * the RxNostr issues a new REQ to all relays allowed to be read.\n * The method returns an Observable that issues EventPackets\n * when an EVENT is received that is subscribed by RxReq.\n * You can unsubscribe the Observable to CLOSE.", "score": 26.17291327395084 }, { "filename": "src/rx-nostr.ts", "retrieved_chunk": " private error$: Subject<ErrorPacket> = new Subject();\n private status$: Subject<ConnectionStatePacket> = new Subject();\n private globalEventPacketPipe: MonoTypeOperatorFunction<EventPacket> | null =\n null;\n private disposed = false;\n private get messageOut$() {\n return this.messageIn$.pipe(\n mergeMap((packet) => {\n const pipe = this.globalEventPacketPipe;\n if (!pipe) {", "score": 24.817109962568512 } ]
typescript
{ event }) => _verify(event));
/** * @since 1.0.0 */ import chalk from "chalk" import * as doctrine from "doctrine" import { Context, Effect, Option, Order, pipe, Predicate, ReadonlyArray, ReadonlyRecord, String } from "effect" import { flow } from "effect/Function" import * as NodePath from "node:path" import * as ast from "ts-morph" import * as Config from "./Config" import * as Domain from "./Domain" import type * as FileSystem from "./FileSystem" /** @internal */ export interface Source { readonly path: ReadonlyArray.NonEmptyReadonlyArray<string> readonly sourceFile: ast.SourceFile } /** @internal */ export const Source = Context.Tag<Source>() interface Comment { readonly description: Option.Option<string> readonly tags: ReadonlyRecord.ReadonlyRecord< ReadonlyArray.NonEmptyReadonlyArray<Option.Option<string>> > } interface CommentInfo { readonly description: Option.Option<string> readonly since: Option.Option<string> readonly deprecated: boolean
readonly examples: ReadonlyArray<Domain.Example> readonly category: Option.Option<string> }
const createCommentInfo = ( description: Option.Option<string>, since: Option.Option<string>, deprecated: boolean, examples: ReadonlyArray<Domain.Example>, category: Option.Option<string> ): CommentInfo => ({ description, since, deprecated, examples, category }) const every = <A>(predicates: ReadonlyArray<Predicate.Predicate<A>>) => (a: A): boolean => predicates.every((p) => p(a)) const some = <A>(predicates: ReadonlyArray<Predicate.Predicate<A>>) => (a: A): boolean => predicates.some((p) => p(a)) const byName = pipe( String.Order, Order.mapInput(({ name }: { name: string }) => name) ) const sortModules = ReadonlyArray.sort(Domain.Order) const isNonEmptyString = (s: string) => s.length > 0 /** * @internal */ export const stripImportTypes = (s: string): string => s.replace(/import\("((?!").)*"\)./g, "") const getJSDocText: (jsdocs: ReadonlyArray<ast.JSDoc>) => string = ReadonlyArray.matchRight({ onEmpty: () => "", onNonEmpty: (_, last) => last.getText() }) const hasTag = (tag: string) => (comment: Comment) => pipe(comment.tags, ReadonlyRecord.get(tag), Option.isSome) const hasInternalTag = hasTag("internal") const hasIgnoreTag = hasTag("ignore") const shouldIgnore: Predicate.Predicate<Comment> = some([hasInternalTag, hasIgnoreTag]) const isVariableDeclarationList = ( u: ast.VariableDeclarationList | ast.CatchClause ): u is ast.VariableDeclarationList => u.getKind() === ast.ts.SyntaxKind.VariableDeclarationList const isVariableStatement = ( u: | ast.VariableStatement | ast.ForStatement | ast.ForOfStatement | ast.ForInStatement ): u is ast.VariableStatement => u.getKind() === ast.ts.SyntaxKind.VariableStatement const getMissingError = ( what: string, path: ReadonlyArray<string>, name: string ): string => `Missing ${chalk.bold(what)} in ${ chalk.bold( path.join("/") + "#" + name ) } documentation` const getMissingTagError = ( tag: string, path: ReadonlyArray<string>, name: string ): string => `Missing ${chalk.bold(tag)} tag in ${ chalk.bold( path.join("/") + "#" + name ) } documentation` const getSinceTag = (name: string, comment: Comment) => pipe( Effect.all([Config.Config, Source]), Effect.flatMap(([config, source]) => pipe( comment.tags, ReadonlyRecord.get("since"), Option.flatMap(ReadonlyArray.headNonEmpty), Effect.asSome, Effect.catchAll(() => config.enforceVersion ? Effect.fail(getMissingTagError("@since", source.path, name)) : Effect.succeed(Option.none<string>()) ) ) ) ) const getCategoryTag = (name: string, comment: Comment) => Effect.flatMap(Source, (source) => pipe( comment.tags, ReadonlyRecord.get("category"), Option.flatMap(ReadonlyArray.headNonEmpty), Effect.asSome, Effect.catchAll(() => ReadonlyRecord.has(comment.tags, "category") ? Effect.fail(getMissingTagError("@category", source.path, name)) : Effect.succeed(Option.none<string>()) ) )) const getDescription = (name: string, comment: Comment) => pipe( Effect.all([Config.Config, Source]), Effect.flatMap(([config, source]) => pipe( comment.description, Option.match({ onNone: () => config.enforceDescriptions ? Effect.fail(getMissingError("description", source.path, name)) : Effect.succeed(Option.none()), onSome: (description) => Effect.succeed(Option.some(description)) }) ) ) ) const getExamples = (name: string, comment: Comment, isModule: boolean) => pipe( Effect.all([Config.Config, Source]), Effect.flatMap(([config, source]) => pipe( comment.tags, ReadonlyRecord.get("example"), Option.map(ReadonlyArray.compact), Option.match({ onNone: () => config.enforceExamples && !isModule ? Effect.fail(getMissingTagError("@example", source.path, name)) : Effect.succeed([]), onSome: (examples) => config.enforceExamples && ReadonlyArray.isEmptyArray(examples) && !isModule ? Effect.fail(getMissingTagError("@example", source.path, name)) : Effect.succeed(examples) }) ) ) ) /** * @internal */ export const getCommentInfo = (name: string, isModule = false) => (text: string) => pipe( Effect.Do, Effect.let("comment", () => parseComment(text)), Effect.bind("since", ({ comment }) => getSinceTag(name, comment)), Effect.bind("category", ({ comment }) => getCategoryTag(name, comment)), Effect.bind("description", ({ comment }) => getDescription(name, comment)), Effect.bind("examples", ({ comment }) => getExamples(name, comment, isModule)), Effect.bind("deprecated", ({ comment }) => Effect.succeed( pipe(comment.tags, ReadonlyRecord.get("deprecated"), Option.isSome) )), Effect.map(({ category, deprecated, description, examples, since }) => { return createCommentInfo( description, since, deprecated, examples, category ) }) ) /** * @internal */ export const parseComment = (text: string): Comment => { const annotation: doctrine.Annotation = doctrine.parse(text, { unwrap: true }) const tags = pipe( annotation.tags, ReadonlyArray.groupBy((tag) => tag.title), ReadonlyRecord.map( ReadonlyArray.mapNonEmpty((tag) => pipe( Option.fromNullable(tag.description), Option.filter(isNonEmptyString) ) ) ) ) const description = pipe( Option.fromNullable(annotation.description), Option.filter(isNonEmptyString) ) return { description, tags } } // ------------------------------------------------------------------------------------- // interfaces // ------------------------------------------------------------------------------------- const parseInterfaceDeclaration = (id: ast.InterfaceDeclaration) => pipe( getJSDocText(id.getJsDocs()), getCommentInfo(id.getName()), Effect.map((info) => Domain.createInterface( Domain.createDocumentable( id.getName(), info.description, info.since, info.deprecated, info.examples, info.category ), id.getText() ) ) ) /** * @category parsers * @since 1.0.0 */ export const parseInterfaces = pipe( Effect.map(Source, (source) => pipe( source.sourceFile.getInterfaces(), ReadonlyArray.filter( every<ast.InterfaceDeclaration>([ (id) => id.isExported(), (id) => pipe( id.getJsDocs(), Predicate.not(flow(getJSDocText, parseComment, shouldIgnore)) ) ]) ) )), Effect.flatMap((interfaceDeclarations) => pipe( interfaceDeclarations, Effect.validateAll(parseInterfaceDeclaration), Effect.map(ReadonlyArray.sort(byName)) ) ) ) // ------------------------------------------------------------------------------------- // functions // ------------------------------------------------------------------------------------- const getFunctionDeclarationSignature = ( f: ast.FunctionDeclaration ): string => { const text = f.getText() return pipe( Option.fromNullable(f.compilerNode.body), Option.match({ onNone: () => text.replace("export function ", "export declare function "), onSome: (body) => { const end = body.getStart() - f.getStart() - 1 return text .substring(0, end) .replace("export function ", "export declare function ") } }) ) } const getFunctionDeclarationJSDocs = ( fd: ast.FunctionDeclaration ): Array<ast.JSDoc> => pipe( fd.getOverloads(), ReadonlyArray.matchLeft({ onEmpty: () => fd.getJsDocs(), onNonEmpty: (firstOverload) => firstOverload.getJsDocs() }) ) const parseFunctionDeclaration = (fd: ast.FunctionDeclaration) => pipe( Effect.flatMap(Source, (source) => pipe( Option.fromNullable(fd.getName()), Option.flatMap(Option.liftPredicate((name) => name.length > 0)), Effect.mapError( () => `Missing function name in module ${source.path.join("/")}` ) )), Effect.flatMap((name) => pipe( getJSDocText(getFunctionDeclarationJSDocs(fd)), getCommentInfo(name), Effect.map((info) => { const signatures = pipe( fd.getOverloads(), ReadonlyArray.matchRight({ onEmpty: () => [getFunctionDeclarationSignature(fd)], onNonEmpty: (init, last) => pipe( init.map(getFunctionDeclarationSignature), ReadonlyArray.append(getFunctionDeclarationSignature(last)) ) }) ) return Domain.createFunction( Domain.createDocumentable( name, info.description, info.since, info.deprecated, info.examples, info.category ), signatures ) }) ) ) ) const parseFunctionVariableDeclaration = (vd: ast.VariableDeclaration) => { const vs: any = vd.getParent().getParent() const name = vd.getName() return pipe( getJSDocText(vs.getJsDocs()), getCommentInfo(name), Effect.map((info) => { const signature = `export declare const ${name}: ${ stripImportTypes( vd.getType().getText(vd) ) }` return Domain.createFunction( Domain.createDocumentable( name, info.description, info.since, info.deprecated, info.examples, info.category ), [signature] ) }) ) } const getFunctionDeclarations = pipe( Effect.map(Source, (source) => ({ functions: pipe( source.sourceFile.getFunctions(), ReadonlyArray.filter( every<ast.FunctionDeclaration>([ (fd) => fd.isExported(), Predicate.not( flow( getFunctionDeclarationJSDocs, getJSDocText, parseComment, shouldIgnore ) ) ]) ) ), arrows: pipe( source.sourceFile.getVariableDeclarations(), ReadonlyArray.filter( every<ast.VariableDeclaration>([ (vd) => isVariableDeclarationList(vd.getParent()), (vd) => isVariableStatement(vd.getParent().getParent() as any), (vd) => pipe( vd.getInitializer(), every([ flow( Option.fromNullable, Option.flatMap( Option.liftPredicate(ast.Node.isFunctionLikeDeclaration) ), Option.isSome ), () => pipe( ( vd.getParent().getParent() as ast.VariableStatement ).getJsDocs(), Predicate.not(flow(getJSDocText, parseComment, shouldIgnore)) ), () => ( vd.getParent().getParent() as ast.VariableStatement ).isExported() ]) ) ]) ) ) })) ) /** * @category parsers * @since 1.0.0 */ export const parseFunctions = pipe( Effect.Do, Effect.bind("getFunctionDeclarations", () => getFunctionDeclarations), Effect.bind("functionDeclarations", ({ getFunctionDeclarations }) => pipe( getFunctionDeclarations.functions, Effect.validateAll(parseFunctionDeclaration) )), Effect.bind("variableDeclarations", ({ getFunctionDeclarations }) => pipe( getFunctionDeclarations.arrows, Effect.validateAll(parseFunctionVariableDeclaration) )), Effect.map(({ functionDeclarations, variableDeclarations }) => functionDeclarations.concat(variableDeclarations) ) ) // ------------------------------------------------------------------------------------- // type aliases // ------------------------------------------------------------------------------------- const parseTypeAliasDeclaration = (ta: ast.TypeAliasDeclaration) => pipe( Effect.succeed(ta.getName()), Effect.flatMap((name) => pipe( getJSDocText(ta.getJsDocs()), getCommentInfo(name), Effect.map((info) => Domain.createTypeAlias( Domain.createDocumentable( name, info.description, info.since, info.deprecated, info.examples, info.category ), ta.getText() ) ) ) ) ) /** * @category parsers * @since 1.0.0 */ export const parseTypeAliases = pipe( Effect.map(Source, (source) => pipe( source.sourceFile.getTypeAliases(), ReadonlyArray.filter( every<ast.TypeAliasDeclaration>([ (alias) => alias.isExported(), (alias) => pipe( alias.getJsDocs(), Predicate.not(flow(getJSDocText, parseComment, shouldIgnore)) ) ]) ) )), Effect.flatMap((typeAliasDeclarations) => pipe(typeAliasDeclarations, Effect.validateAll(parseTypeAliasDeclaration)) ), Effect.map(ReadonlyArray.sort(byName)) ) // ------------------------------------------------------------------------------------- // constants // ------------------------------------------------------------------------------------- const parseConstantVariableDeclaration = (vd: ast.VariableDeclaration) => { const vs: any = vd.getParent().getParent() const name = vd.getName() return pipe( getJSDocText(vs.getJsDocs()), getCommentInfo(name), Effect.map((info) => { const type = stripImportTypes(vd.getType().getText(vd)) const signature = `export declare const ${name}: ${type}` return Domain.createConstant( Domain.createDocumentable( name, info.description, info.since, info.deprecated, info.examples, info.category ), signature ) }) ) } /** * @category parsers * @since 1.0.0 */ export const parseConstants = pipe( Effect.map(Source, (source) => pipe( source.sourceFile.getVariableDeclarations(), ReadonlyArray.filter( every<ast.VariableDeclaration>([ (vd) => isVariableDeclarationList(vd.getParent()), (vd) => isVariableStatement(vd.getParent().getParent() as any), (vd) => pipe( vd.getInitializer(), every([ flow( Option.fromNullable, Option.flatMap( Option.liftPredicate( Predicate.not(ast.Node.isFunctionLikeDeclaration) ) ), Option.isSome ), () => pipe( ( vd.getParent().getParent() as ast.VariableStatement ).getJsDocs(), Predicate.not(flow(getJSDocText, parseComment, shouldIgnore)) ), () => ( vd.getParent().getParent() as ast.VariableStatement ).isExported() ]) ) ]) ) )), Effect.flatMap((variableDeclarations) => pipe( variableDeclarations, Effect.validateAll(parseConstantVariableDeclaration) ) ) ) // ------------------------------------------------------------------------------------- // exports // ------------------------------------------------------------------------------------- const parseExportSpecifier = (es: ast.ExportSpecifier) => Effect.flatMap(Source, (source) => pipe( Effect.Do, Effect.bind("name", () => Effect.succeed(es.compilerNode.name.text)), Effect.bind("type", () => Effect.succeed(stripImportTypes(es.getType().getText(es)))), Effect.bind( "signature", ({ name, type }) => Effect.succeed(`export declare const ${name}: ${type}`) ), Effect.flatMap(({ name, signature }) => pipe( es.getLeadingCommentRanges(), ReadonlyArray.head, Effect.mapError( () => `Missing ${name} documentation in ${source.path.join("/")}` ), Effect.flatMap((commentRange) => pipe(commentRange.getText(), getCommentInfo(name))), Effect.map((info) => Domain.createExport( Domain.createDocumentable( name, info.description, info.since, info.deprecated, info.examples, info.category ), signature ) ) ) ) )) const parseExportDeclaration = (ed: ast.ExportDeclaration) => pipe(ed.getNamedExports(), Effect.validateAll(parseExportSpecifier)) /** * @category parsers * @since 1.0.0 */ export const parseExports = pipe( Effect.map(Source, (source) => source.sourceFile.getExportDeclarations()), Effect.flatMap((exportDeclarations) => pipe(exportDeclarations, Effect.validateAll(parseExportDeclaration)) ), Effect.mapBoth({ onFailure: ReadonlyArray.flatten, onSuccess: ReadonlyArray.flatten }) ) // ------------------------------------------------------------------------------------- // classes // ------------------------------------------------------------------------------------- const getTypeParameters = ( tps: ReadonlyArray<ast.TypeParameterDeclaration> ): string => tps.length === 0 ? "" : `<${tps.map((p) => p.getName()).join(", ")}>` const getMethodSignature = (md: ast.MethodDeclaration): string => pipe( Option.fromNullable(md.compilerNode.body), Option.match({ onNone: () => md.getText(), onSome: (body) => { const end = body.getStart() - md.getStart() - 1 return md.getText().substring(0, end) } }) ) const parseMethod = (md: ast.MethodDeclaration) => pipe( Effect.Do, Effect.bind("name", () => Effect.succeed(md.getName())), Effect.bind("overloads", () => Effect.succeed(md.getOverloads())), Effect.bind("jsdocs", ({ overloads }) => Effect.succeed( pipe( overloads, ReadonlyArray.matchLeft({ onEmpty: () => md.getJsDocs(), onNonEmpty: (x) => x.getJsDocs() }) ) )), Effect.flatMap(({ jsdocs, name, overloads }) => shouldIgnore(parseComment(getJSDocText(jsdocs))) ? Effect.succeed(Option.none()) : pipe( getJSDocText(jsdocs), getCommentInfo(name), Effect.map((info) => { const signatures = pipe( overloads, ReadonlyArray.matchRight({ onEmpty: () => [getMethodSignature(md)], onNonEmpty: (init, last) => pipe( init.map((md) => md.getText()), ReadonlyArray.append(getMethodSignature(last)) ) }) ) return Option.some( Domain.createMethod( Domain.createDocumentable( name, info.description, info.since, info.deprecated, info.examples, info.category ), signatures ) ) }) ) ) ) const parseProperty = (classname: string) => (pd: ast.PropertyDeclaration) => { const name = pd.getName() return pipe( getJSDocText(pd.getJsDocs()), getCommentInfo(`${classname}#${name}`), Effect.map((info) => { const type = stripImportTypes(pd.getType().getText(pd)) const readonly = pipe( Option.fromNullable( pd.getFirstModifierByKind(ast.ts.SyntaxKind.ReadonlyKeyword) ), Option.match({ onNone: () => "", onSome: () => "readonly " }) ) const signature = `${readonly}${name}: ${type}` return Domain.createProperty( Domain.createDocumentable( name, info.description, info.since, info.deprecated, info.examples, info.category ), signature ) }) ) } const parseProperties = (name: string, c: ast.ClassDeclaration) => pipe( c.getProperties(), ReadonlyArray.filter( every<ast.PropertyDeclaration>([ (prop) => !prop.isStatic(), (prop) => pipe( prop.getFirstModifierByKind(ast.ts.SyntaxKind.PrivateKeyword), Option.fromNullable, Option.isNone ), (prop) => pipe( prop.getJsDocs(), Predicate.not(flow(getJSDocText, parseComment, shouldIgnore)) ) ]) ), (propertyDeclarations) => pipe(propertyDeclarations, Effect.validateAll(parseProperty(name))) ) /** * @internal */ export const getConstructorDeclarationSignature = ( c: ast.ConstructorDeclaration ): string => pipe( Option.fromNullable(c.compilerNode.body), Option.match({ onNone: () => c.getText(), onSome: (body) => { const end = body.getStart() - c.getStart() - 1 return c.getText().substring(0, end) } }) ) const getClassName = (c: ast.ClassDeclaration) => Effect.flatMap(Source, (source) => Effect.mapError( Option.fromNullable(c.getName()), () => `Missing class name in module ${source.path.join("/")}` )) const getClassCommentInfo = (name: string, c: ast.ClassDeclaration) => pipe(c.getJsDocs(), getJSDocText, getCommentInfo(name)) const getClassDeclarationSignature = (name: string, c: ast.ClassDeclaration) => pipe( Effect.succeed(getTypeParameters(c.getTypeParameters())), Effect.map((typeParameters) => pipe( c.getConstructors(), ReadonlyArray.matchLeft({ onEmpty: () => `export declare class ${name}${typeParameters}`, onNonEmpty: (head) => `export declare class ${name}${typeParameters} { ${ getConstructorDeclarationSignature( head ) } }` }) ) ) ) const parseClass = (c: ast.ClassDeclaration) => pipe( Effect.Do, Effect.bind("name", () => Effect.mapError(getClassName(c), (e) => [e])), Effect.bind("info", ({ name }) => Effect.mapError(getClassCommentInfo(name, c), (e) => [e])), Effect.bind("signature", ({ name }) => getClassDeclarationSignature(name, c)), Effect.bind("methods", () => pipe( c.getInstanceMethods(), Effect.validateAll(parseMethod), Effect.map(ReadonlyArray.compact) )), Effect.bind("staticMethods", () => pipe( c.getStaticMethods(), Effect.validateAll(parseMethod), Effect.map(ReadonlyArray.compact) )), Effect.bind("properties", ({ name }) => parseProperties(name, c)), Effect.map( ({ info, methods, name, properties, signature, staticMethods }) => Domain.createClass( Domain.createDocumentable( name, info.description, info.since, info.deprecated, info.examples, info.category ), signature, methods, staticMethods, properties ) ) ) /** * @category parsers * @since 1.0.0 */ export const parseClasses = pipe( Effect.map(Source, (source) => pipe( source.sourceFile.getClasses(), ReadonlyArray.filter(every<ast.ClassDeclaration>([ (id) => id.isExported(), (id) => pipe( id.getJsDocs(), Predicate.not(flow(getJSDocText, parseComment, shouldIgnore)) ) ])) )), Effect.flatMap((classDeclarations) => pipe( classDeclarations, Effect.validateAll(parseClass), Effect.mapBoth({ onFailure: ReadonlyArray.flatten, onSuccess: ReadonlyArray.sort(byName) }) ) ) ) // ------------------------------------------------------------------------------------- // modules // ------------------------------------------------------------------------------------- const getModuleName = ( path: ReadonlyArray.NonEmptyReadonlyArray<string> ): string => NodePath.parse(ReadonlyArray.lastNonEmpty(path)).name /** * @internal */ export const parseModuleDocumentation = pipe( Effect.all([Config.Config, Source]), Effect.flatMap(([config, source]) => { const name = getModuleName(source.path) // if any of the settings enforcing documentation are set to `true`, then // a module should have associated documentation const isDocumentationRequired = config.enforceDescriptions || config.enforceVersion const onMissingDocumentation = () => isDocumentationRequired ? Effect.fail( `Missing documentation in ${source.path.join("/")} module` ) : Effect.succeed( Domain.createDocumentable( name, Option.none(), Option.none(), false, [], Option.none() ) ) return ReadonlyArray.matchLeft(source.sourceFile.getStatements(), { onEmpty: onMissingDocumentation, onNonEmpty: (statement) => ReadonlyArray.matchLeft(statement.getLeadingCommentRanges(), { onEmpty: onMissingDocumentation, onNonEmpty: (commentRange) => pipe( getCommentInfo(name, true)(commentRange.getText()), Effect.map((info) => Domain.createDocumentable( name, info.description, info.since, info.deprecated, info.examples, info.category ) ) ) }) }) }) ) /** * @category parsers * @since 1.0.0 */ export const parseModule = pipe( Effect.flatMap(Source, (source) => pipe( Effect.Do, Effect.bind("documentation", () => Effect.mapError(parseModuleDocumentation, (e) => [e])), Effect.bind("interfaces", () => parseInterfaces), Effect.bind("functions", () => parseFunctions), Effect.bind("typeAliases", () => parseTypeAliases), Effect.bind("classes", () => parseClasses), Effect.bind("constants", () => parseConstants), Effect.bind("exports", () => parseExports), Effect.map( ({ classes, constants, documentation, exports, functions, interfaces, typeAliases }) => Domain.createModule( documentation, source.path, classes, interfaces, functions, typeAliases, constants, exports ) ) )) ) /** * @internal */ export const parseFile = (project: ast.Project) => ( file: FileSystem.File ): Effect.Effect<Config.Config, Array<string>, Domain.Module> => { const path = file.path.split( NodePath.sep ) as any as ReadonlyArray.NonEmptyReadonlyArray<string> const sourceFile = project.getSourceFile(file.path) if (sourceFile !== undefined) { return pipe( parseModule, Effect.provideService(Source, { path, sourceFile }) ) } return Effect.fail([`Unable to locate file: ${file.path}`]) } const createProject = (files: ReadonlyArray<FileSystem.File>) => pipe( Config.Config, Effect.map((config) => { const options: ast.ProjectOptions = { compilerOptions: { strict: true, ...config.parseCompilerOptions } } const project = new ast.Project(options) for (const file of files) { project.addSourceFileAtPath(file.path) } return project }) ) /** * @category parsers * @since 1.0.0 */ export const parseFiles = (files: ReadonlyArray<FileSystem.File>) => pipe( createProject(files), Effect.flatMap((project) => pipe( files, Effect.validateAll(parseFile(project)), Effect.map( flow( ReadonlyArray.filter((module) => !module.deprecated), sortModules ) ) ) ) )
src/Parser.ts
Effect-TS-docgen-56278b8
[ { "filename": "src/Domain.ts", "retrieved_chunk": " * @since 1.0.0\n */\nexport interface Documentable {\n readonly name: string\n readonly description: Option.Option<string>\n readonly since: Option.Option<string>\n readonly deprecated: boolean\n readonly examples: ReadonlyArray<Example>\n readonly category: Option.Option<string>\n}", "score": 75.22939808596146 }, { "filename": "src/Domain.ts", "retrieved_chunk": " since: Option.Option<string>,\n deprecated: boolean,\n examples: ReadonlyArray<Example>,\n category: Option.Option<string>\n): Documentable => ({\n name,\n description,\n since,\n deprecated,\n examples,", "score": 62.27624922135198 }, { "filename": "src/Domain.ts", "retrieved_chunk": "// -------------------------------------------------------------------------------------\n// constructors\n// -------------------------------------------------------------------------------------\n/**\n * @category constructors\n * @since 1.0.0\n */\nexport const createDocumentable = (\n name: string,\n description: Option.Option<string>,", "score": 40.67214022735147 }, { "filename": "src/Markdown.ts", "retrieved_chunk": " const name = s.trim() === \"hasOwnProperty\" ? `${s} (function)` : s\n const title = deprecated ? strikethrough(name) : name\n return Option.fromNullable(type).pipe(\n Option.match({\n onNone: () => title,\n onSome: (t) => title + ` ${t}`\n })\n )\n}\nconst getDescription = (d: Option.Option<string>): string =>", "score": 39.44954468927972 }, { "filename": "src/Markdown.ts", "retrieved_chunk": "const createHeader = (level: number) => (content: string): string =>\n \"#\".repeat(level) + \" \" + content + \"\\n\\n\"\nconst h1 = createHeader(1)\nconst h2 = createHeader(2)\nconst h3 = createHeader(3)\nconst getSince: (v: Option.Option<string>) => string = Option.match({\n onNone: () => \"\",\n onSome: (v) => paragraph(`Added in v${v}`)\n})\nconst getTitle = (s: string, deprecated: boolean, type?: string): string => {", "score": 36.60647402492747 } ]
typescript
readonly examples: ReadonlyArray<Domain.Example> readonly category: Option.Option<string> }
import Nostr from "nostr-typedef"; import { EMPTY, filter, identity, mergeMap, type MonoTypeOperatorFunction, Observable, type ObservableInput, of, retry, Subject, tap, timer, } from "rxjs"; import { evalFilters } from "./helper.js"; import { fetchRelayInfo } from "./index.js"; import { isFiltered } from "./nostr/filter.js"; import { ConnectionState, LazyREQ, MessagePacket } from "./packet.js"; export class Connection { private socket: WebSocket | null = null; private message$ = new Subject<MessagePacket | WebSocketError>(); private error$ = new Subject<unknown>(); private connectionState$ = new Subject<ConnectionState>(); private connectionState: ConnectionState = "not-started"; private queuedEvents: Nostr.ToRelayMessage.EVENT[] = []; private reqs: Map<string /* subId */, ReqState> = new Map(); private serverLimitations: Nostr.Nip11.ServerLimitations | null = null; private canRetry = false; get read() { return this.config.read; } set read(v) { this.config.read = v; } get write() { return this.config.write; } set write(v) { this.config.write = v; } get maxConcurrentReqs(): number | null { return ( this.serverLimitations?.max_subscriptions ?? this.config.maxConcurrentReqsFallback ?? null ); } constructor(public url: string, private config: ConnectionConfig) { this.connectionState$.next("not-started"); } private setConnectionState(state: ConnectionState) { if (this.connectionState === "terminated") { return; } this.connectionState = state; this.connectionState$.next(state); } private async fetchServerLimitationsIfNeeded() { if ( this.config.disableAutoFetchNip11Limitations || this.serverLimitations ) { return; } try { const info = await fetchRelayInfo(this.url); this.serverLimitations = info.limitation ?? null; } catch { // do nothing } } async start() { if ( !this.canRetry && (this.connectionState === "reconnecting" || this.connectionState === "starting" || this.connectionState === "ongoing") ) { return Promise.resolve(); } this.canRetry = false; if (this.connectionState === "not-started") { this.setConnectionState("starting"); } else { this.setConnectionState("reconnecting"); } await this.fetchServerLimitationsIfNeeded(); let completeStartingProcess: () => void; const succeededOrFailed = new Promise<void>((_resolve) => { completeStartingProcess = _resolve; }); const onopen = () => { this.setConnectionState("ongoing"); completeStartingProcess(); for (const event of this.queuedEvents) { this.sendEVENT(event); } this.queuedEvents = []; this.ensureReqs(); }; const onmessage = ({ data }: MessageEvent) => { if (this.connectionState === "terminated") { return; } try { this.message$.next({ from: this.url, message: JSON.parse(data) }); } catch (err) { this.error$.next(err); } }; const onerror = () => { completeStartingProcess(); }; const onclose = ({ code }: CloseEvent) => { if ( code === WebSocketCloseCode.DISPOSED_BY_RX_NOSTR || this.connectionState === "terminated" ) { return; } websocket.removeEventListener("open", onopen); websocket.removeEventListener("message", onmessage); websocket.removeEventListener("error", onerror); websocket.removeEventListener("close", onclose); websocket.close(); this.socket = null; for (const req of this.reqs.values()) { req.isOngoing = false; } if (code === WebSocketCloseCode.DESIRED_BY_RX_NOSTR) { this.setConnectionState("not-started"); } else if (code === WebSocketCloseCode.DONT_RETRY) { this.setConnectionState("rejected"); this.message$.next(new WebSocketError(code)); completeStartingProcess(); } else { this.canRetry = true; this.message$.next(new WebSocketError(code)); completeStartingProcess(); } }; if (this.connectionState === "terminated") { return Promise.resolve(); } const websocket = new WebSocket(this.url); websocket.addEventListener("open", onopen); websocket.addEventListener("message", onmessage); websocket.addEventListener("error", onerror); websocket.addEventListener("close", onclose); this.socket = websocket; return succeededOrFailed; } stop() { this.finalizeAllReqs(); this.socket?.close(WebSocketCloseCode.DESIRED_BY_RX_NOSTR); } getConnectionState() { return this.connectionState; } getMessageObservable(): Observable<MessagePacket> { const reqs = this.reqs; return this.message$.asObservable().pipe( mergeMap((data) => { if (data instanceof WebSocketError) { if (data.code === WebSocketCloseCode.DONT_RETRY) { return EMPTY; } else { throw data; } } else { return of(data); } }), tap({ subscribe: () => { this.start(); }, }), this.config.backoff.strategy === "off" ? identity : retry({ delay: (_, retryCount) => backoffSignal(this.config.backoff, retryCount), count: this.config.backoff.maxCount, }), tap({ error: () => { this.setConnectionState("error"); }, }), rejectFilterUnmatchEvents() ); function rejectFilterUnmatchEvents(): MonoTypeOperatorFunction<MessagePacket> { return filter((packet) => { const [type, subId, event] = packet.message; if (type !== "EVENT") { return true; } const req = reqs.get(subId); if (!req) { return true; } const [, , ...filters] = req.actual; return
isFiltered(event, filters);
}); } } getConnectionStateObservable() { return this.connectionState$.asObservable(); } getErrorObservable() { return this.error$.asObservable(); } ensureReq(req: LazyREQ, options?: { overwrite?: boolean }) { const subId = req[1]; if (this.connectionState === "terminated") { return; } if (!this.read) { // REQ is not allowed. return; } if (!options?.overwrite) { if (this.reqs.get(subId)?.isOngoing) { // REQ is already ongoing return; } if ( this.reqs.has(subId) && !isConcurrentAllowed(subId, this.reqs, this.maxConcurrentReqs) ) { // REQ is already queued return; } } const message = evalREQ(req); // enqueue or overwrite this.reqs.set(subId, { original: req, actual: message, isOngoing: false, }); if (isConcurrentAllowed(subId, this.reqs, this.maxConcurrentReqs)) { const req = this.reqs.get(subId); if (req && this.socket?.readyState === WebSocket.OPEN) { req.isOngoing = true; this.socket.send(JSON.stringify(message)); } } } private ensureReqs() { const reqs = Array.from(this.reqs.values()).slice( 0, this.maxConcurrentReqs ?? undefined ); for (const req of reqs) { this.ensureReq(req.original); } } finalizeReq(subId: string) { if (this.connectionState === "terminated") { return; } const req = this.reqs.get(subId); if (!req) { return; } this.reqs.delete(subId); if (req.isOngoing) { if (this.socket?.readyState === WebSocket.OPEN) { const message: Nostr.ToRelayMessage.CLOSE = ["CLOSE", subId]; this.socket.send(JSON.stringify(message)); } this.ensureReqs(); } } private finalizeAllReqs() { if (this.connectionState === "terminated") { return; } for (const subId of this.reqs.keys()) { if (this.socket?.readyState === WebSocket.OPEN) { const message: Nostr.ToRelayMessage.CLOSE = ["CLOSE", subId]; this.socket.send(JSON.stringify(message)); } } this.reqs.clear(); } sendEVENT(message: Nostr.ToRelayMessage.EVENT) { if (this.connectionState === "terminated") { return; } if (!this.write) { return; } if (this.socket?.readyState === WebSocket.OPEN) { this.socket.send(JSON.stringify(message)); } else { if (this.socket?.readyState === WebSocket.CONNECTING) { // Enqueue this.queuedEvents.push(message); } else { // Create a temporary socket to send message. const socket = new WebSocket(this.url); socket.addEventListener("open", () => { socket.send(JSON.stringify(message)); }); // Close the temporary socket after receiveing OK or timed out. socket.addEventListener("message", ({ data }) => { try { const response: Nostr.ToClientMessage.Any = JSON.parse(data); if (response[0] === "OK") { socket.close(); } this.message$.next({ from: this.url, message: response }); } catch (err) { this.message$.error(err); } }); setTimeout(() => { if ( socket.readyState === WebSocket.OPEN || socket.readyState === WebSocket.CONNECTING ) { socket.close(); } }, 10 * 1000); } } } dispose() { this.finalizeAllReqs(); this.setConnectionState("terminated"); this.socket?.close(WebSocketCloseCode.DISPOSED_BY_RX_NOSTR); this.socket = null; this.reqs.clear(); this.message$.complete(); this.message$.unsubscribe(); this.connectionState$.complete(); this.connectionState$.unsubscribe(); this.error$.complete(); this.error$.unsubscribe(); } } export const WebSocketCloseCode = { /** * 1006 is a reserved value and MUST NOT be set as a status code in a * Close control frame by an endpoint. It is designated for use in * applications expecting a status code to indicate that the * connection was closed abnormally, e.g., without sending or * receiving a Close control frame. * * See also: https://www.rfc-editor.org/rfc/rfc6455.html#section-7.4.1 */ ABNORMAL_CLOSURE: 1006, /** * When a websocket is closed by the relay with a status code 4000 * that means the client shouldn't try to connect again. * * See also: https://github.com/nostr-protocol/nips/blob/fab6a21a779460f696f11169ddf343b437327592/01.md?plain=1#L113 */ DONT_RETRY: 4000, /** @internal rx-nostr uses it internally. */ DESIRED_BY_RX_NOSTR: 4537, /** @internal rx-nostr uses it internally. */ DISPOSED_BY_RX_NOSTR: 4538, } as const; export interface ConnectionConfig { backoff: BackoffConfig; read: boolean; write: boolean; disableAutoFetchNip11Limitations?: boolean; maxConcurrentReqsFallback?: number; } export type BackoffConfig = | { // Exponential backoff and jitter strategy strategy: "exponential"; maxCount: number; initialDelay: number; } | { // Retry at regular intervals strategy: "linear"; maxCount: number; interval: number; } | { // Retry immediately strategy: "immediately"; maxCount: number; } | { // Won't retry strategy: "off"; }; interface ReqState { original: LazyREQ; actual: Nostr.ToRelayMessage.REQ; isOngoing: boolean; } function backoffSignal( config: BackoffConfig, count: number ): ObservableInput<unknown> { if (config.strategy === "exponential") { const time = Math.max( config.initialDelay * 2 ** (count - 1) + (Math.random() - 0.5) * 1000, 1000 ); return timer(time); } else if (config.strategy === "linear") { return timer(config.interval); } else if (config.strategy === "immediately") { return of(0); } else { return EMPTY; } } function evalREQ([type, subId, ...filters]: LazyREQ): Nostr.ToRelayMessage.REQ { return [type, subId, ...evalFilters(filters)]; } function isConcurrentAllowed( subId: string, reqs: Map<string, ReqState>, concurrent: number | null ): boolean { if (concurrent === null) { return true; } const reqOrdinal = Array.from(reqs.keys()).findIndex((e) => e === subId); if (reqOrdinal === undefined) { return false; } return reqOrdinal < concurrent; } class WebSocketError extends Error { constructor(public code?: number) { super(`WebSocket Error: Socket was closed with code ${code}`); } }
src/connection.ts
penpenpng-rx-nostr-7c52614
[ { "filename": "src/nostr/filter.ts", "retrieved_chunk": "/**\n * Return true if the given filter matches the given filters.\n */\nexport function isFiltered(\n event: Nostr.Event,\n filters: Nostr.Filter | Nostr.Filter[],\n options?: Partial<MatchFilterOptions>\n): boolean {\n if (Array.isArray(filters)) {\n return filters.some((filter) => _isFiltered(event, filter, options));", "score": 18.57804852441274 }, { "filename": "src/req.ts", "retrieved_chunk": "class RxOneshotReq extends RxReqBase {\n constructor(req: { filters: LazyFilter | LazyFilter[]; subId?: string }) {\n super(req?.subId);\n this.emit(req.filters);\n }\n override get strategy(): \"oneshot\" {\n return \"oneshot\";\n }\n}\nexport interface Mixin<R, T> {", "score": 18.308591091896876 }, { "filename": "src/rx-nostr.ts", "retrieved_chunk": " case \"oneshot\":\n return map((filters) => [\"REQ\", makeId(), ...filters]);\n }\n }\n function manageActiveForwardReq(): MonoTypeOperatorFunction<LazyREQ> {\n const recordActiveReq = (req: LazyREQ) => {\n const subId = req[1];\n ongoings.set(subId, {\n req,\n scope,", "score": 17.50158415245222 }, { "filename": "src/__test__/subscription.test.ts", "retrieved_chunk": " wasClean: true,\n });\n rxNostr.send(faker.event());\n await expect(relay).toReceiveEVENT();\n });\n test(\"[oneshot] Receipt of EOSE terminates the Observable.\", async () => {\n const req = createRxOneshotReq({\n subId: \"sub\",\n filters: faker.filter(),\n });", "score": 15.984685319870927 }, { "filename": "src/__test__/subscription.test.ts", "retrieved_chunk": " reason: \"Clean up on afterEach()\",\n wasClean: true,\n });\n });\n test(\"[forward] Each REQ is published with the same subId.\", async () => {\n const req = createRxForwardReq(\"sub\");\n rxNostr.use(req).subscribe();\n req.emit(faker.filter());\n await expect(relay).toReceiveREQ(\"sub:0\");\n req.emit(faker.filters());", "score": 15.745750638856144 } ]
typescript
isFiltered(event, filters);
import Nostr from "nostr-typedef"; import { catchError, delay, distinct, distinctUntilChanged, EMPTY, filter, groupBy, map, mergeAll, mergeMap, type MonoTypeOperatorFunction, type ObservableInput, of, type OperatorFunction, pipe, scan, tap, timeout, TimeoutError, } from "rxjs"; import { evalFilters } from "./helper.js"; import { compareEvents, verify as _verify } from "./nostr/event.js"; import { isFiltered, MatchFilterOptions } from "./nostr/filter.js"; import { EventPacket, LazyFilter, MessagePacket, ReqPacket } from "./packet.js"; import { defineDefaultOptions } from "./util.js"; // --------------------- // // EventPacket operators // // --------------------- // /** * Remove the events once seen. */ export function uniq( flushes?: ObservableInput<unknown> ): MonoTypeOperatorFunction<EventPacket> { return distinct<EventPacket, string>(({ event }) => event.id, flushes); } /** * Create a customizable uniq operator. * * If `keyFn()` returns a non-null key, the key is stored in `Set`. * The operator filters packets with keys already stored. * * The `Set` returned in the second value of the tuple * can be manipulated externally or in optional event handlers. * For example, you can call `Set#clear()` to forget all keys. */ export function createUniq<T>( keyFn: (packet: EventPacket) => T | null, options?: CreateUniqOptions<T> ): [MonoTypeOperatorFunction<EventPacket>, Set<T>] { const cache = new Set<T>(); return [ filter((packet) => { const key = keyFn(packet); if (key === null) { return true; } if (cache.has(key)) { options?.onHit?.(packet, cache); return false; } else { cache.add(key); options?.onCache?.(packet, cache); return true; } }), cache, ]; } /** * Only the latest events are allowed to pass. */ export function latest(): MonoTypeOperatorFunction<EventPacket> { return pipe( scan<EventPacket>((acc, packet) => compareEvents(acc.event, packet.event) < 0 ? packet : acc ), distinctUntilChanged( (a, b) => a === b, ({ event }) => event.id ) ); } /** * For each key, only the latest events are allowed to pass. */ export function latestEach<K>( key: (packet: EventPacket) => K ): MonoTypeOperatorFunction<EventPacket> { return pipe(groupBy(key), map(pipe(latest())), mergeAll()); } /** * Only events with a valid signature are allowed to pass. */ export function verify(): MonoTypeOperatorFunction<EventPacket> { return filter<EventPacket>(({ event }) => _verify(event)); } /** * Only events with given kind are allowed to pass. */ export function filterKind<K extends number>( kind: K ): MonoTypeOperatorFunction<EventPacket> { return filter<EventPacket>(({ event }) => event.kind === kind); } /** * Filter events based on a REQ filter object. */ export function filterBy(
filters: LazyFilter | LazyFilter[], options?: MatchFilterOptions & FilterByOptions ): MonoTypeOperatorFunction<EventPacket> {
const { not } = makeFilterByOptions(options); const evaledFilter = evalFilters(filters); return filter(({ event }) => { const match = isFiltered(event, evaledFilter, options); return not ? !match : match; }); } /** * Accumulate latest events in order of new arrival (based on `created_at`). */ export function timeline( limit?: number ): OperatorFunction<EventPacket, EventPacket[]> { return scan<EventPacket, EventPacket[]>((acc, packet) => { const next = [...acc, packet].sort( (a, b) => -1 * compareEvents(a.event, b.event) ); if (limit !== undefined) { next.splice(limit); } return next; }, []); } export function sortEvents( bufferTime: number, compareFn?: (a: EventPacket, b: EventPacket) => number ): MonoTypeOperatorFunction<EventPacket> { return sort( bufferTime, compareFn ?? ((a, b) => compareEvents(a.event, b.event)) ); } // ----------------------- // // MessagePacket operators // // ----------------------- // export function filterType<T extends Nostr.ToClientMessage.Type>( type: T ): OperatorFunction< MessagePacket, MessagePacket<Nostr.ToClientMessage.Message<T>> > { return filter( (packet): packet is MessagePacket<Nostr.ToClientMessage.Message<T>> => packet.message[0] === type ); } // ------------------- // // ReqPacket operators // // ------------------- // /** * Map REQ packets into a single REQ packet. * * It is useful to reduce REQ requests in a time interval. */ export function batch( /** Function used for merge REQ filters. Default behavior is simple concatenation. */ mergeFilter?: MergeFilter ): OperatorFunction<ReqPacket[], ReqPacket> { return map((f) => f.reduce((acc, v) => { if (acc === null) { return v; } if (v === null) { return acc; } return (mergeFilter ?? defaultMergeFilter)(acc, v); }, null) ); } /** * Chunk a REQ packet into multiple REQ packets. * * It is useful to avoid to send large REQ filter. */ export function chunk( predicate: (f: LazyFilter[]) => boolean, toChunk: (f: LazyFilter[]) => LazyFilter[][] ): MonoTypeOperatorFunction<ReqPacket> { return mergeMap((f) => f !== null && predicate(f) ? of(...toChunk(f)) : of(f) ); } // ----------------- // // General operators // // ----------------- // /** * Almost RxJS's `timeout`, but won't throw. */ export function completeOnTimeout<T>( time: number ): MonoTypeOperatorFunction<T> { return pipe( timeout(time), catchError((error: unknown) => { if (error instanceof TimeoutError) { return EMPTY; } else { throw error; } }) ); } /** * Buffer the received values for a specified time * and return the values in sorted order as possible. */ export function sort<T>( bufferTime: number, compareFn: (a: T, b: T) => number ): MonoTypeOperatorFunction<T> { const buffer: T[] = []; return pipe( tap((v) => { buffer.push(v); buffer.sort(compareFn); }), delay(bufferTime), map(() => { if (buffer.length <= 0) { throw new Error("Logic Error: This is rx-nostr's internal bug."); } // Non-null is valid because the lenght has been checked. // eslint-disable-next-line @typescript-eslint/no-non-null-assertion return buffer.shift()!; }) ); } // ----------- // // Other stuff // // ----------- // export type MergeFilter = (a: LazyFilter[], b: LazyFilter[]) => LazyFilter[]; function defaultMergeFilter(a: LazyFilter[], b: LazyFilter[]): LazyFilter[] { return [...a, ...b]; } export interface CreateUniqOptions<T> { onCache?: (packet: EventPacket, cache: Set<T>) => void; onHit?: (packet: EventPacket, cache: Set<T>) => void; } export interface FilterByOptions { not: boolean; } const makeFilterByOptions = defineDefaultOptions<FilterByOptions>({ not: false, });
src/operator.ts
penpenpng-rx-nostr-7c52614
[ { "filename": "src/nostr/filter.ts", "retrieved_chunk": "/**\n * Return true if the given filter matches the given filters.\n */\nexport function isFiltered(\n event: Nostr.Event,\n filters: Nostr.Filter | Nostr.Filter[],\n options?: Partial<MatchFilterOptions>\n): boolean {\n if (Array.isArray(filters)) {\n return filters.some((filter) => _isFiltered(event, filter, options));", "score": 29.88576918050161 }, { "filename": "src/nostr/filter.ts", "retrieved_chunk": " } else {\n return _isFiltered(event, filters, options);\n }\n}\nfunction _isFiltered(\n event: Nostr.Event,\n filter: Nostr.Filter,\n options?: Partial<MatchFilterOptions>\n): boolean {\n const { sinceInclusive, untilInclusive } = makeMatchFilterOptions(options);", "score": 27.857638504723425 }, { "filename": "src/packet.ts", "retrieved_chunk": " */\nexport type LazyFilter = Omit<Nostr.Filter, \"since\" | \"until\"> & {\n since?: number | (() => number);\n until?: number | (() => number);\n};\nexport type LazyREQ = [\"REQ\", string, ...LazyFilter[]];\n/**\n * Packets from websocket that represents an EVENT.\n */\nexport interface EventPacket {", "score": 24.71775335679331 }, { "filename": "src/__test__/helper.ts", "retrieved_chunk": " ..._faker,\n eventPacket(\n packetOrEvent?: Partial<\n EventPacket[\"event\"] &\n Omit<EventPacket, \"event\"> & {\n event?: Partial<EventPacket[\"event\"]>;\n }\n >\n ): EventPacket {\n return {", "score": 23.502919139697028 }, { "filename": "src/helper.ts", "retrieved_chunk": " return filters.map(evalFilter);\n } else {\n return [evalFilter(filters)];\n }\n}\nfunction evalFilter(filter: LazyFilter): Nostr.Filter {\n return {\n ...filter,\n since: filter.since ? evalLazyNumber(filter.since) : undefined,\n until: filter.until ? evalLazyNumber(filter.until) : undefined,", "score": 22.98504250215325 } ]
typescript
filters: LazyFilter | LazyFilter[], options?: MatchFilterOptions & FilterByOptions ): MonoTypeOperatorFunction<EventPacket> {
import Nostr from "nostr-typedef"; import { BehaviorSubject, Observable, type OperatorFunction } from "rxjs"; import { LazyFilter, ReqPacket } from "./packet.js"; import type { Override } from "./util.js"; /** * The RxReq interface that is provided for RxNostr (**not for users**). */ export interface RxReq<S extends RxReqStrategy = RxReqStrategy> { /** @internal User should not use this directly.The RxReq strategy. It is read-only and must not change. */ get strategy(): S; /** @internal User should not use this directly. Used to construct subId. */ get rxReqId(): string; /** @internal User should not use this directly. Get an Observable of ReqPacket. */ getReqObservable(): Observable<ReqPacket>; } /** * REQ strategy. * * See comments on `createRxForwardReq()`, `createRxBackwardReq()` and `createRxOneshotReq() */ export type RxReqStrategy = "forward" | "backward" | "oneshot"; /** * The RxReq interface that is provided for users (not for RxNostr). */ export interface RxReqController { /** Start new REQ or stop REQ on the RxNostr with witch the RxReq is associated. */ emit(filters: LazyFilter | LazyFilter[] | null): void; /** * Returns itself overriding only `getReqObservable()`. * It is useful for throttling and other control purposes. */ pipe(): RxReq; pipe(op1: OperatorFunction<ReqPacket, ReqPacket>): RxReq; pipe<A>( op1: OperatorFunction<ReqPacket, A>, op2: OperatorFunction<A, ReqPacket> ): RxReq; pipe<A, B>( op1: OperatorFunction<ReqPacket, A>, op2: OperatorFunction<A, B>, op3: OperatorFunction<B, ReqPacket> ): RxReq; pipe<A, B, C>( op1: OperatorFunction<ReqPacket, A>, op2: OperatorFunction<A, B>, op3: OperatorFunction<B, C>, op4: OperatorFunction<C, ReqPacket> ): RxReq; pipe<A, B, C, D>( op1: OperatorFunction<ReqPacket, A>, op2: OperatorFunction<A, B>, op3: OperatorFunction<B, C>, op4: OperatorFunction<C, D>, op5: OperatorFunction<D, ReqPacket> ): RxReq; } abstract class RxReqBase implements RxReq { protected filters$
= new BehaviorSubject<ReqPacket>(null);
private _rxReqId: string; abstract get strategy(): RxReqStrategy; get rxReqId() { return this._rxReqId; } constructor(rxReqId?: string) { this._rxReqId = rxReqId ?? getRandomDigitsString(); } getReqObservable(): Observable<ReqPacket> { return this.filters$.asObservable(); } emit(filters: LazyFilter | LazyFilter[] | null) { const normalized = normalizeFilters(filters); if (normalized) { this.filters$.next(normalized); } else { this.filters$.next(null); } } pipe(): RxReq; pipe(op1: OperatorFunction<ReqPacket, ReqPacket>): RxReq; pipe<A>( op1: OperatorFunction<ReqPacket, A>, op2: OperatorFunction<A, ReqPacket> ): RxReq; pipe<A, B>( op1: OperatorFunction<ReqPacket, A>, op2: OperatorFunction<A, B>, op3: OperatorFunction<B, ReqPacket> ): RxReq; pipe<A, B, C>( op1: OperatorFunction<ReqPacket, A>, op2: OperatorFunction<A, B>, op3: OperatorFunction<B, C>, op4: OperatorFunction<C, ReqPacket> ): RxReq; pipe<A, B, C, D>( op1: OperatorFunction<ReqPacket, A>, op2: OperatorFunction<A, B>, op3: OperatorFunction<B, C>, op4: OperatorFunction<C, D>, op5: OperatorFunction<D, ReqPacket> ): RxReq; // eslint-disable-next-line @typescript-eslint/no-explicit-any pipe(...operators: OperatorFunction<any, any>[]): RxReq { const rxReqId = this.rxReqId; const strategy = this.strategy; return { ...this, get rxReqId() { return rxReqId; }, get strategy() { return strategy; }, getReqObservable: () => this.getReqObservable().pipe(...(operators as [])), }; } } /** * Create a RxReq instance based on the backward strategy. * It is useful if you want to retrieve past events that have already been published. * * In backward strategy: * - All REQs have different subIds. * - All REQ-subscriptions keep alive until timeout or getting EOSE. * - In most cases, you should specify `until` or `limit` for filters. * * For more information, see [document](https://penpenpng.github.io/rx-nostr/docs/req-strategy.html#backward-strategy). */ export function createRxBackwardReq( subIdBase?: string ): RxReq<"backward"> & RxReqController { return new RxBackwardReq(subIdBase); } class RxBackwardReq extends RxReqBase implements RxReqController { constructor(rxReqId?: string) { super(rxReqId); } override get strategy(): "backward" { return "backward"; } } /** * Create a RxReq instance based on the forward strategy. * It is useful if you want to listen future events. * * In forward strategy: * - All REQs have the same subId. * - When a new REQ is issued, the old REQ is overwritten and terminated immediately. * The latest REQ keeps alive until it is overwritten or explicitly terminated. * - In most cases, you should not specify `limit` for filters. * * For more information, see [document](https://penpenpng.github.io/rx-nostr/docs/req-strategy.html#forward-strategy). */ export function createRxForwardReq( subId?: string ): RxReq<"forward"> & RxReqController { return new RxForwardReq(subId); } class RxForwardReq extends RxReqBase implements RxReqController { constructor(rxReqId?: string) { super(rxReqId); } override get strategy(): "forward" { return "forward"; } } /** * Create a RxReq instance based on the oneshot strategy. * It is almost the same as backward strategy, however can publish only one REQ * and the Observable completes on EOSE. * * For more information, see [document](https://penpenpng.github.io/rx-nostr/docs/req-strategy.html#oneshot-strategy). */ export function createRxOneshotReq(req: { filters: LazyFilter | LazyFilter[]; subId?: string; }): RxReq<"oneshot"> { return new RxOneshotReq(req); } class RxOneshotReq extends RxReqBase { constructor(req: { filters: LazyFilter | LazyFilter[]; subId?: string }) { super(req?.subId); this.emit(req.filters); } override get strategy(): "oneshot" { return "oneshot"; } } export interface Mixin<R, T> { (): ThisType<R> & T; } /** NOTE: unstable feature */ export function mixin<R extends object, T extends object>( def: () => ThisType<R> & T ): Mixin<R, T> { return def; } /** NOTE: unstable feature */ export function extend<B extends R, R extends object, T extends object>( base: B, mixin: Mixin<R, T> ): Override<B, T> { return Object.assign(base, mixin()) as Override<B, T>; } function getRandomDigitsString() { return `${Math.floor(Math.random() * 1000000)}`; } function normalizeFilter(filter: LazyFilter): LazyFilter | null { const res: LazyFilter = {}; const isTagName = (s: string): s is Nostr.TagName => /^#[a-zA-Z]$/.test(s); for (const key of Object.keys(filter)) { if (key === "limit" && (filter[key] ?? -1) >= 0) { res[key] = filter[key]; continue; } if (key === "since" || key === "until") { const f = filter[key]; if (typeof f !== "number" || (f ?? -1) >= 0) { res[key] = f; continue; } } if ( (isTagName(key) || key === "ids" || key === "authors") && filter[key] !== undefined && (filter[key]?.length ?? -1) > 0 ) { res[key] = filter[key]; continue; } if ( key === "kinds" && filter[key] !== undefined && (filter[key]?.length ?? -1) > 0 ) { res[key] = filter[key]; continue; } if (key === "search" && filter[key] !== undefined) { res[key] = filter[key]; continue; } } const timeRangeIsValid = typeof res.since !== "number" || typeof res.until !== "number" || res.since <= res.until; if (!timeRangeIsValid) { return null; } if (Object.keys(res).length <= 0) { return null; } return res; } function normalizeFilters( filters: LazyFilter | LazyFilter[] | null ): LazyFilter[] | null { if (!filters) { return null; } const normalized = (Array.isArray(filters) ? filters : [filters]).flatMap( (e) => normalizeFilter(e) ?? [] ); return normalized.length > 0 ? normalized : null; }
src/req.ts
penpenpng-rx-nostr-7c52614
[ { "filename": "src/operator.ts", "retrieved_chunk": " mergeFilter?: MergeFilter\n): OperatorFunction<ReqPacket[], ReqPacket> {\n return map((f) =>\n f.reduce((acc, v) => {\n if (acc === null) {\n return v;\n }\n if (v === null) {\n return acc;\n }", "score": 41.48004214856678 }, { "filename": "src/rx-nostr.ts", "retrieved_chunk": " mergeAll,\n mergeMap,\n type MonoTypeOperatorFunction,\n Observable,\n of,\n type OperatorFunction,\n ReplaySubject,\n Subject,\n Subscription,\n take,", "score": 23.892186110171934 }, { "filename": "src/operator.ts", "retrieved_chunk": " mergeAll,\n mergeMap,\n type MonoTypeOperatorFunction,\n type ObservableInput,\n of,\n type OperatorFunction,\n pipe,\n scan,\n tap,\n timeout,", "score": 23.424896827147904 }, { "filename": "src/operator.ts", "retrieved_chunk": " type: T\n): OperatorFunction<\n MessagePacket,\n MessagePacket<Nostr.ToClientMessage.Message<T>>\n> {\n return filter(\n (packet): packet is MessagePacket<Nostr.ToClientMessage.Message<T>> =>\n packet.message[0] === type\n );\n}", "score": 19.592874074435095 }, { "filename": "src/rx-nostr.ts", "retrieved_chunk": " );\n } else {\n return subId$.pipe(map(createEoseManagedEventObservable), mergeAll());\n }\n function attachSubId(): OperatorFunction<LazyFilter[], LazyREQ> {\n const makeId = (index?: number) => makeSubId({ rxReqId, index });\n switch (strategy) {\n case \"backward\":\n return map((filters, index) => [\"REQ\", makeId(index), ...filters]);\n case \"forward\":", "score": 19.42271408465088 } ]
typescript
= new BehaviorSubject<ReqPacket>(null);
import Nostr from "nostr-typedef"; import { BehaviorSubject, Observable, type OperatorFunction } from "rxjs"; import { LazyFilter, ReqPacket } from "./packet.js"; import type { Override } from "./util.js"; /** * The RxReq interface that is provided for RxNostr (**not for users**). */ export interface RxReq<S extends RxReqStrategy = RxReqStrategy> { /** @internal User should not use this directly.The RxReq strategy. It is read-only and must not change. */ get strategy(): S; /** @internal User should not use this directly. Used to construct subId. */ get rxReqId(): string; /** @internal User should not use this directly. Get an Observable of ReqPacket. */ getReqObservable(): Observable<ReqPacket>; } /** * REQ strategy. * * See comments on `createRxForwardReq()`, `createRxBackwardReq()` and `createRxOneshotReq() */ export type RxReqStrategy = "forward" | "backward" | "oneshot"; /** * The RxReq interface that is provided for users (not for RxNostr). */ export interface RxReqController { /** Start new REQ or stop REQ on the RxNostr with witch the RxReq is associated. */ emit(filters: LazyFilter | LazyFilter[] | null): void; /** * Returns itself overriding only `getReqObservable()`. * It is useful for throttling and other control purposes. */ pipe(): RxReq; pipe(op1: OperatorFunction<ReqPacket, ReqPacket>): RxReq; pipe<A>( op1: OperatorFunction<ReqPacket, A>, op2: OperatorFunction<A, ReqPacket> ): RxReq; pipe<A, B>( op1: OperatorFunction<ReqPacket, A>, op2: OperatorFunction<A, B>, op3: OperatorFunction<B, ReqPacket> ): RxReq; pipe<A, B, C>( op1: OperatorFunction<ReqPacket, A>, op2: OperatorFunction<A, B>, op3: OperatorFunction<B, C>, op4: OperatorFunction<C, ReqPacket> ): RxReq; pipe<A, B, C, D>( op1: OperatorFunction<ReqPacket, A>, op2: OperatorFunction<A, B>, op3: OperatorFunction<B, C>, op4: OperatorFunction<C, D>, op5: OperatorFunction<D, ReqPacket> ): RxReq; } abstract class RxReqBase implements RxReq { protected filters$ = new BehaviorSubject<ReqPacket>(null); private _rxReqId: string; abstract get strategy(): RxReqStrategy; get rxReqId() { return this._rxReqId; } constructor(rxReqId?: string) { this._rxReqId = rxReqId ?? getRandomDigitsString(); } getReqObservable(): Observable<ReqPacket> { return this.filters$.asObservable(); } emit(filters: LazyFilter | LazyFilter[] | null) { const normalized = normalizeFilters(filters); if (normalized) { this.filters$.next(normalized); } else { this.filters$.next(null); } } pipe(): RxReq; pipe(op1: OperatorFunction<ReqPacket, ReqPacket>): RxReq; pipe<A>( op1: OperatorFunction<ReqPacket, A>, op2: OperatorFunction<A, ReqPacket> ): RxReq; pipe<A, B>( op1: OperatorFunction<ReqPacket, A>, op2: OperatorFunction<A, B>, op3: OperatorFunction<B, ReqPacket> ): RxReq; pipe<A, B, C>( op1: OperatorFunction<ReqPacket, A>, op2: OperatorFunction<A, B>, op3: OperatorFunction<B, C>, op4: OperatorFunction<C, ReqPacket> ): RxReq; pipe<A, B, C, D>( op1: OperatorFunction<ReqPacket, A>, op2: OperatorFunction<A, B>, op3: OperatorFunction<B, C>, op4: OperatorFunction<C, D>, op5: OperatorFunction<D, ReqPacket> ): RxReq; // eslint-disable-next-line @typescript-eslint/no-explicit-any pipe(...operators: OperatorFunction<any, any>[]): RxReq { const rxReqId = this.rxReqId; const strategy = this.strategy; return { ...this, get rxReqId() { return rxReqId; }, get strategy() { return strategy; }, getReqObservable: () => this.getReqObservable().pipe(...(operators as [])), }; } } /** * Create a RxReq instance based on the backward strategy. * It is useful if you want to retrieve past events that have already been published. * * In backward strategy: * - All REQs have different subIds. * - All REQ-subscriptions keep alive until timeout or getting EOSE. * - In most cases, you should specify `until` or `limit` for filters. * * For more information, see [document](https://penpenpng.github.io/rx-nostr/docs/req-strategy.html#backward-strategy). */ export function createRxBackwardReq( subIdBase?: string ): RxReq<"backward"> & RxReqController { return new RxBackwardReq(subIdBase); } class RxBackwardReq extends RxReqBase implements RxReqController { constructor(rxReqId?: string) { super(rxReqId); } override get strategy(): "backward" { return "backward"; } } /** * Create a RxReq instance based on the forward strategy. * It is useful if you want to listen future events. * * In forward strategy: * - All REQs have the same subId. * - When a new REQ is issued, the old REQ is overwritten and terminated immediately. * The latest REQ keeps alive until it is overwritten or explicitly terminated. * - In most cases, you should not specify `limit` for filters. * * For more information, see [document](https://penpenpng.github.io/rx-nostr/docs/req-strategy.html#forward-strategy). */ export function createRxForwardReq( subId?: string ): RxReq<"forward"> & RxReqController { return new RxForwardReq(subId); } class RxForwardReq extends RxReqBase implements RxReqController { constructor(rxReqId?: string) { super(rxReqId); } override get strategy(): "forward" { return "forward"; } } /** * Create a RxReq instance based on the oneshot strategy. * It is almost the same as backward strategy, however can publish only one REQ * and the Observable completes on EOSE. * * For more information, see [document](https://penpenpng.github.io/rx-nostr/docs/req-strategy.html#oneshot-strategy). */ export function createRxOneshotReq(req: { filters: LazyFilter | LazyFilter[]; subId?: string; }): RxReq<"oneshot"> { return new RxOneshotReq(req); } class RxOneshotReq extends RxReqBase { constructor(req: { filters: LazyFilter | LazyFilter[]; subId?: string }) { super(req?.subId); this.emit(req.filters); } override get strategy(): "oneshot" { return "oneshot"; } } export interface Mixin<R, T> { (): ThisType<R> & T; } /** NOTE: unstable feature */ export function mixin<R extends object, T extends object>( def: () => ThisType<R> & T ): Mixin<R, T> { return def; } /** NOTE: unstable feature */ export function extend<B extends R, R extends object, T extends object>( base: B, mixin: Mixin<R, T> ): Override<B, T> { return Object.assign(base, mixin()) as Override<B, T>; } function getRandomDigitsString() { return `${Math.floor(Math.random() * 1000000)}`; } function normalizeFilter(filter: LazyFilter): LazyFilter | null { const res: LazyFilter = {}; const isTagName = (s: string): s is Nostr.TagName => /^#[a-zA-Z]$/.test(s); for (const key of Object.keys(filter)) { if (key === "limit" && (filter[key] ?? -1) >= 0) { res[key] = filter[key]; continue; } if (key === "since" || key === "until") { const f = filter[key]; if (typeof f !== "number" || (f ?? -1) >= 0) { res[key] = f; continue; } } if ( (isTagName(key) || key === "ids" || key === "authors") && filter[key] !== undefined && (filter[key]?.length ?? -1) > 0 ) { res[key] = filter[key]; continue; } if ( key === "kinds" && filter[key] !== undefined && (filter[key]?.length ?? -1) > 0 ) { res[key] = filter[key]; continue; }
if (key === "search" && filter[key] !== undefined) {
res[key] = filter[key]; continue; } } const timeRangeIsValid = typeof res.since !== "number" || typeof res.until !== "number" || res.since <= res.until; if (!timeRangeIsValid) { return null; } if (Object.keys(res).length <= 0) { return null; } return res; } function normalizeFilters( filters: LazyFilter | LazyFilter[] | null ): LazyFilter[] | null { if (!filters) { return null; } const normalized = (Array.isArray(filters) ? filters : [filters]).flatMap( (e) => normalizeFilter(e) ?? [] ); return normalized.length > 0 ? normalized : null; }
src/req.ts
penpenpng-rx-nostr-7c52614
[ { "filename": "src/operator.ts", "retrieved_chunk": " options?: CreateUniqOptions<T>\n): [MonoTypeOperatorFunction<EventPacket>, Set<T>] {\n const cache = new Set<T>();\n return [\n filter((packet) => {\n const key = keyFn(packet);\n if (key === null) {\n return true;\n }\n if (cache.has(key)) {", "score": 47.469925822610506 }, { "filename": "src/nostr/filter.ts", "retrieved_chunk": " if (!key.startsWith(\"#\") || !Array.isArray(needleValues)) {\n continue;\n }\n const needleTagName = key.slice(1);\n if (\n !event.tags.find(\n ([tagName, tagValue]) =>\n needleTagName === tagName &&\n (needleValues as string[]).includes(tagValue)\n )", "score": 42.563014568832045 }, { "filename": "src/nostr/filter.ts", "retrieved_chunk": " return false;\n }\n if (\n filter.until &&\n ((untilInclusive && !(event.created_at <= filter.until)) ||\n (!untilInclusive && !(event.created_at < filter.until)))\n ) {\n return false;\n }\n for (const [key, needleValues] of Object.entries(filter)) {", "score": 40.684590394018 }, { "filename": "src/util.ts", "retrieved_chunk": "import normalizeUrl from \"normalize-url\";\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport function defineDefaultOptions<T extends Record<string, any>>(\n defaultParams: T\n): (givenParams?: Partial<T>) => T {\n return (givenParams) =>\n Object.fromEntries(\n Object.keys(defaultParams).map((key) => [\n key,\n givenParams?.[key] ?? defaultParams[key],", "score": 35.78495797911306 }, { "filename": "src/operator.ts", "retrieved_chunk": " (a, b) => a === b,\n ({ event }) => event.id\n )\n );\n}\n/**\n * For each key, only the latest events are allowed to pass.\n */\nexport function latestEach<K>(\n key: (packet: EventPacket) => K", "score": 31.8116504459679 } ]
typescript
if (key === "search" && filter[key] !== undefined) {
import Nostr from "nostr-typedef"; import { catchError, delay, distinct, distinctUntilChanged, EMPTY, filter, groupBy, map, mergeAll, mergeMap, type MonoTypeOperatorFunction, type ObservableInput, of, type OperatorFunction, pipe, scan, tap, timeout, TimeoutError, } from "rxjs"; import { evalFilters } from "./helper.js"; import { compareEvents, verify as _verify } from "./nostr/event.js"; import { isFiltered, MatchFilterOptions } from "./nostr/filter.js"; import { EventPacket, LazyFilter, MessagePacket, ReqPacket } from "./packet.js"; import { defineDefaultOptions } from "./util.js"; // --------------------- // // EventPacket operators // // --------------------- // /** * Remove the events once seen. */ export function uniq( flushes?: ObservableInput<unknown> ): MonoTypeOperatorFunction<EventPacket> { return distinct<EventPacket, string>(({ event }) => event.id, flushes); } /** * Create a customizable uniq operator. * * If `keyFn()` returns a non-null key, the key is stored in `Set`. * The operator filters packets with keys already stored. * * The `Set` returned in the second value of the tuple * can be manipulated externally or in optional event handlers. * For example, you can call `Set#clear()` to forget all keys. */ export function createUniq<T>( keyFn: (packet: EventPacket) => T | null, options?: CreateUniqOptions<T> ): [MonoTypeOperatorFunction<EventPacket>, Set<T>] { const cache = new Set<T>(); return [ filter((packet) => { const key = keyFn(packet); if (key === null) { return true; } if (cache.has(key)) { options?.onHit?.(packet, cache); return false; } else { cache.add(key); options?.onCache?.(packet, cache); return true; } }), cache, ]; } /** * Only the latest events are allowed to pass. */ export function latest(): MonoTypeOperatorFunction<EventPacket> { return pipe( scan<EventPacket>((acc, packet) => compareEvents(acc.event, packet.event) < 0 ? packet : acc ), distinctUntilChanged( (a, b) => a === b, ({ event }) => event.id ) ); } /** * For each key, only the latest events are allowed to pass. */ export function latestEach<K>( key: (packet: EventPacket) => K ): MonoTypeOperatorFunction<EventPacket> { return pipe(groupBy(key), map(pipe(latest())), mergeAll()); } /** * Only events with a valid signature are allowed to pass. */ export function verify(): MonoTypeOperatorFunction<EventPacket> { return filter<EventPacket>(({ event }) => _verify(event)); } /** * Only events with given kind are allowed to pass. */ export function filterKind<K extends number>( kind: K ): MonoTypeOperatorFunction<EventPacket> { return filter<EventPacket>(({ event }) => event.kind === kind); } /** * Filter events based on a REQ filter object. */ export function filterBy( filters: LazyFilter | LazyFilter[], options?: MatchFilterOptions & FilterByOptions ): MonoTypeOperatorFunction<EventPacket> { const { not } = makeFilterByOptions(options); const evaledFilter = evalFilters(filters); return filter(({ event }) => { const match = isFiltered(event, evaledFilter, options); return not ? !match : match; }); } /** * Accumulate latest events in order of new arrival (based on `created_at`). */ export function timeline( limit?: number ): OperatorFunction<EventPacket, EventPacket[]> { return scan<EventPacket, EventPacket[]>((acc, packet) => { const next = [...acc, packet].sort( (a, b) => -1 * compareEvents(a.event, b.event) ); if (limit !== undefined) { next.splice(limit); } return next; }, []); } export function sortEvents( bufferTime: number, compareFn?: (a: EventPacket, b: EventPacket) => number ): MonoTypeOperatorFunction<EventPacket> { return sort( bufferTime, compareFn ?? ((a, b) => compareEvents(a.event, b.event)) ); } // ----------------------- // // MessagePacket operators // // ----------------------- // export function filterType<T extends Nostr.ToClientMessage.Type>( type: T ): OperatorFunction< MessagePacket, MessagePacket<Nostr.ToClientMessage.Message<T>> > { return filter( (packet): packet is MessagePacket<Nostr.ToClientMessage.Message<T>> => packet.message[0] === type ); } // ------------------- // // ReqPacket operators // // ------------------- // /** * Map REQ packets into a single REQ packet. * * It is useful to reduce REQ requests in a time interval. */ export function batch( /** Function used for merge REQ filters. Default behavior is simple concatenation. */ mergeFilter?: MergeFilter ): OperatorFunction<ReqPacket[], ReqPacket> { return map((f) => f.reduce((acc, v) => { if (acc === null) { return v; } if (v === null) { return acc; } return (mergeFilter ?? defaultMergeFilter)(acc, v); }, null) ); } /** * Chunk a REQ packet into multiple REQ packets. * * It is useful to avoid to send large REQ filter. */ export function chunk( predicate: (f: LazyFilter[]) => boolean, toChunk: (f: LazyFilter[]
) => LazyFilter[][] ): MonoTypeOperatorFunction<ReqPacket> {
return mergeMap((f) => f !== null && predicate(f) ? of(...toChunk(f)) : of(f) ); } // ----------------- // // General operators // // ----------------- // /** * Almost RxJS's `timeout`, but won't throw. */ export function completeOnTimeout<T>( time: number ): MonoTypeOperatorFunction<T> { return pipe( timeout(time), catchError((error: unknown) => { if (error instanceof TimeoutError) { return EMPTY; } else { throw error; } }) ); } /** * Buffer the received values for a specified time * and return the values in sorted order as possible. */ export function sort<T>( bufferTime: number, compareFn: (a: T, b: T) => number ): MonoTypeOperatorFunction<T> { const buffer: T[] = []; return pipe( tap((v) => { buffer.push(v); buffer.sort(compareFn); }), delay(bufferTime), map(() => { if (buffer.length <= 0) { throw new Error("Logic Error: This is rx-nostr's internal bug."); } // Non-null is valid because the lenght has been checked. // eslint-disable-next-line @typescript-eslint/no-non-null-assertion return buffer.shift()!; }) ); } // ----------- // // Other stuff // // ----------- // export type MergeFilter = (a: LazyFilter[], b: LazyFilter[]) => LazyFilter[]; function defaultMergeFilter(a: LazyFilter[], b: LazyFilter[]): LazyFilter[] { return [...a, ...b]; } export interface CreateUniqOptions<T> { onCache?: (packet: EventPacket, cache: Set<T>) => void; onHit?: (packet: EventPacket, cache: Set<T>) => void; } export interface FilterByOptions { not: boolean; } const makeFilterByOptions = defineDefaultOptions<FilterByOptions>({ not: false, });
src/operator.ts
penpenpng-rx-nostr-7c52614
[ { "filename": "src/req.ts", "retrieved_chunk": "export type RxReqStrategy = \"forward\" | \"backward\" | \"oneshot\";\n/**\n * The RxReq interface that is provided for users (not for RxNostr).\n */\nexport interface RxReqController {\n /** Start new REQ or stop REQ on the RxNostr with witch the RxReq is associated. */\n emit(filters: LazyFilter | LazyFilter[] | null): void;\n /**\n * Returns itself overriding only `getReqObservable()`.\n * It is useful for throttling and other control purposes.", "score": 26.431970714742114 }, { "filename": "src/helper.ts", "retrieved_chunk": "/** Return a function that is lazily evaluated for since/until parameters of `LazyFilter`. */\nimport Nostr from \"nostr-typedef\";\nimport { LazyFilter } from \"./packet.js\";\nexport function now(): number {\n return Math.floor(new Date().getTime() / 1000);\n}\nexport function evalFilters(\n filters: LazyFilter | LazyFilter[]\n): Nostr.Filter[] {\n if (\"length\" in filters) {", "score": 23.467198765982044 }, { "filename": "src/req.ts", "retrieved_chunk": " }\n}\n/**\n * Create a RxReq instance based on the forward strategy.\n * It is useful if you want to listen future events.\n *\n * In forward strategy:\n * - All REQs have the same subId.\n * - When a new REQ is issued, the old REQ is overwritten and terminated immediately.\n * The latest REQ keeps alive until it is overwritten or explicitly terminated.", "score": 22.907119667966025 }, { "filename": "src/req.ts", "retrieved_chunk": " const isTagName = (s: string): s is Nostr.TagName => /^#[a-zA-Z]$/.test(s);\n for (const key of Object.keys(filter)) {\n if (key === \"limit\" && (filter[key] ?? -1) >= 0) {\n res[key] = filter[key];\n continue;\n }\n if (key === \"since\" || key === \"until\") {\n const f = filter[key];\n if (typeof f !== \"number\" || (f ?? -1) >= 0) {\n res[key] = f;", "score": 22.771995874076588 }, { "filename": "src/packet.ts", "retrieved_chunk": " */\nexport type LazyFilter = Omit<Nostr.Filter, \"since\" | \"until\"> & {\n since?: number | (() => number);\n until?: number | (() => number);\n};\nexport type LazyREQ = [\"REQ\", string, ...LazyFilter[]];\n/**\n * Packets from websocket that represents an EVENT.\n */\nexport interface EventPacket {", "score": 20.81751510468797 } ]
typescript
) => LazyFilter[][] ): MonoTypeOperatorFunction<ReqPacket> {
import Nostr from "nostr-typedef"; import { catchError, delay, distinct, distinctUntilChanged, EMPTY, filter, groupBy, map, mergeAll, mergeMap, type MonoTypeOperatorFunction, type ObservableInput, of, type OperatorFunction, pipe, scan, tap, timeout, TimeoutError, } from "rxjs"; import { evalFilters } from "./helper.js"; import { compareEvents, verify as _verify } from "./nostr/event.js"; import { isFiltered, MatchFilterOptions } from "./nostr/filter.js"; import { EventPacket, LazyFilter, MessagePacket, ReqPacket } from "./packet.js"; import { defineDefaultOptions } from "./util.js"; // --------------------- // // EventPacket operators // // --------------------- // /** * Remove the events once seen. */ export function uniq( flushes?: ObservableInput<unknown> ): MonoTypeOperatorFunction<EventPacket> { return distinct<EventPacket, string>(({ event }) => event.id, flushes); } /** * Create a customizable uniq operator. * * If `keyFn()` returns a non-null key, the key is stored in `Set`. * The operator filters packets with keys already stored. * * The `Set` returned in the second value of the tuple * can be manipulated externally or in optional event handlers. * For example, you can call `Set#clear()` to forget all keys. */ export function createUniq<T>( keyFn: (
packet: EventPacket) => T | null, options?: CreateUniqOptions<T> ): [MonoTypeOperatorFunction<EventPacket>, Set<T>] {
const cache = new Set<T>(); return [ filter((packet) => { const key = keyFn(packet); if (key === null) { return true; } if (cache.has(key)) { options?.onHit?.(packet, cache); return false; } else { cache.add(key); options?.onCache?.(packet, cache); return true; } }), cache, ]; } /** * Only the latest events are allowed to pass. */ export function latest(): MonoTypeOperatorFunction<EventPacket> { return pipe( scan<EventPacket>((acc, packet) => compareEvents(acc.event, packet.event) < 0 ? packet : acc ), distinctUntilChanged( (a, b) => a === b, ({ event }) => event.id ) ); } /** * For each key, only the latest events are allowed to pass. */ export function latestEach<K>( key: (packet: EventPacket) => K ): MonoTypeOperatorFunction<EventPacket> { return pipe(groupBy(key), map(pipe(latest())), mergeAll()); } /** * Only events with a valid signature are allowed to pass. */ export function verify(): MonoTypeOperatorFunction<EventPacket> { return filter<EventPacket>(({ event }) => _verify(event)); } /** * Only events with given kind are allowed to pass. */ export function filterKind<K extends number>( kind: K ): MonoTypeOperatorFunction<EventPacket> { return filter<EventPacket>(({ event }) => event.kind === kind); } /** * Filter events based on a REQ filter object. */ export function filterBy( filters: LazyFilter | LazyFilter[], options?: MatchFilterOptions & FilterByOptions ): MonoTypeOperatorFunction<EventPacket> { const { not } = makeFilterByOptions(options); const evaledFilter = evalFilters(filters); return filter(({ event }) => { const match = isFiltered(event, evaledFilter, options); return not ? !match : match; }); } /** * Accumulate latest events in order of new arrival (based on `created_at`). */ export function timeline( limit?: number ): OperatorFunction<EventPacket, EventPacket[]> { return scan<EventPacket, EventPacket[]>((acc, packet) => { const next = [...acc, packet].sort( (a, b) => -1 * compareEvents(a.event, b.event) ); if (limit !== undefined) { next.splice(limit); } return next; }, []); } export function sortEvents( bufferTime: number, compareFn?: (a: EventPacket, b: EventPacket) => number ): MonoTypeOperatorFunction<EventPacket> { return sort( bufferTime, compareFn ?? ((a, b) => compareEvents(a.event, b.event)) ); } // ----------------------- // // MessagePacket operators // // ----------------------- // export function filterType<T extends Nostr.ToClientMessage.Type>( type: T ): OperatorFunction< MessagePacket, MessagePacket<Nostr.ToClientMessage.Message<T>> > { return filter( (packet): packet is MessagePacket<Nostr.ToClientMessage.Message<T>> => packet.message[0] === type ); } // ------------------- // // ReqPacket operators // // ------------------- // /** * Map REQ packets into a single REQ packet. * * It is useful to reduce REQ requests in a time interval. */ export function batch( /** Function used for merge REQ filters. Default behavior is simple concatenation. */ mergeFilter?: MergeFilter ): OperatorFunction<ReqPacket[], ReqPacket> { return map((f) => f.reduce((acc, v) => { if (acc === null) { return v; } if (v === null) { return acc; } return (mergeFilter ?? defaultMergeFilter)(acc, v); }, null) ); } /** * Chunk a REQ packet into multiple REQ packets. * * It is useful to avoid to send large REQ filter. */ export function chunk( predicate: (f: LazyFilter[]) => boolean, toChunk: (f: LazyFilter[]) => LazyFilter[][] ): MonoTypeOperatorFunction<ReqPacket> { return mergeMap((f) => f !== null && predicate(f) ? of(...toChunk(f)) : of(f) ); } // ----------------- // // General operators // // ----------------- // /** * Almost RxJS's `timeout`, but won't throw. */ export function completeOnTimeout<T>( time: number ): MonoTypeOperatorFunction<T> { return pipe( timeout(time), catchError((error: unknown) => { if (error instanceof TimeoutError) { return EMPTY; } else { throw error; } }) ); } /** * Buffer the received values for a specified time * and return the values in sorted order as possible. */ export function sort<T>( bufferTime: number, compareFn: (a: T, b: T) => number ): MonoTypeOperatorFunction<T> { const buffer: T[] = []; return pipe( tap((v) => { buffer.push(v); buffer.sort(compareFn); }), delay(bufferTime), map(() => { if (buffer.length <= 0) { throw new Error("Logic Error: This is rx-nostr's internal bug."); } // Non-null is valid because the lenght has been checked. // eslint-disable-next-line @typescript-eslint/no-non-null-assertion return buffer.shift()!; }) ); } // ----------- // // Other stuff // // ----------- // export type MergeFilter = (a: LazyFilter[], b: LazyFilter[]) => LazyFilter[]; function defaultMergeFilter(a: LazyFilter[], b: LazyFilter[]): LazyFilter[] { return [...a, ...b]; } export interface CreateUniqOptions<T> { onCache?: (packet: EventPacket, cache: Set<T>) => void; onHit?: (packet: EventPacket, cache: Set<T>) => void; } export interface FilterByOptions { not: boolean; } const makeFilterByOptions = defineDefaultOptions<FilterByOptions>({ not: false, });
src/operator.ts
penpenpng-rx-nostr-7c52614
[ { "filename": "src/util.ts", "retrieved_chunk": " ])\n ) as T;\n}\nexport type Override<T extends object, U extends object> = {\n [K in keyof T | keyof U]: K extends keyof U\n ? U[K]\n : K extends keyof T\n ? T[K]\n : never;\n};", "score": 35.79619835409659 }, { "filename": "src/rx-nostr.ts", "retrieved_chunk": " setGlobalEventPacketPipe(\n pipe: MonoTypeOperatorFunction<EventPacket> | null\n ): void;\n /**\n * Associate RxReq with RxNostr.\n * When the associated RxReq is manipulated,\n * the RxNostr issues a new REQ to all relays allowed to be read.\n * The method returns an Observable that issues EventPackets\n * when an EVENT is received that is subscribed by RxReq.\n * You can unsubscribe the Observable to CLOSE.", "score": 33.40328345160375 }, { "filename": "src/__test__/helper.ts", "retrieved_chunk": " },\n};\nexport function spySub(): {\n completed: () => boolean;\n error: () => boolean;\n count: () => number;\n subscribed: () => boolean;\n unsubscribed: () => boolean;\n tap: <T>() => MonoTypeOperatorFunction<T>;\n} {", "score": 32.01581576151763 }, { "filename": "src/req.ts", "retrieved_chunk": " (): ThisType<R> & T;\n}\n/** NOTE: unstable feature */\nexport function mixin<R extends object, T extends object>(\n def: () => ThisType<R> & T\n): Mixin<R, T> {\n return def;\n}\n/** NOTE: unstable feature */\nexport function extend<B extends R, R extends object, T extends object>(", "score": 30.987356035171018 }, { "filename": "src/util.ts", "retrieved_chunk": "import normalizeUrl from \"normalize-url\";\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport function defineDefaultOptions<T extends Record<string, any>>(\n defaultParams: T\n): (givenParams?: Partial<T>) => T {\n return (givenParams) =>\n Object.fromEntries(\n Object.keys(defaultParams).map((key) => [\n key,\n givenParams?.[key] ?? defaultParams[key],", "score": 30.477017206465987 } ]
typescript
packet: EventPacket) => T | null, options?: CreateUniqOptions<T> ): [MonoTypeOperatorFunction<EventPacket>, Set<T>] {
import Nostr from "nostr-typedef"; import { catchError, delay, distinct, distinctUntilChanged, EMPTY, filter, groupBy, map, mergeAll, mergeMap, type MonoTypeOperatorFunction, type ObservableInput, of, type OperatorFunction, pipe, scan, tap, timeout, TimeoutError, } from "rxjs"; import { evalFilters } from "./helper.js"; import { compareEvents, verify as _verify } from "./nostr/event.js"; import { isFiltered, MatchFilterOptions } from "./nostr/filter.js"; import { EventPacket, LazyFilter, MessagePacket, ReqPacket } from "./packet.js"; import { defineDefaultOptions } from "./util.js"; // --------------------- // // EventPacket operators // // --------------------- // /** * Remove the events once seen. */ export function uniq( flushes?: ObservableInput<unknown> ): MonoTypeOperatorFunction<EventPacket> { return distinct<EventPacket, string>(({ event }) => event.id, flushes); } /** * Create a customizable uniq operator. * * If `keyFn()` returns a non-null key, the key is stored in `Set`. * The operator filters packets with keys already stored. * * The `Set` returned in the second value of the tuple * can be manipulated externally or in optional event handlers. * For example, you can call `Set#clear()` to forget all keys. */ export function createUniq<T>( keyFn: (packet: EventPacket) => T | null, options?: CreateUniqOptions<T> ): [MonoTypeOperatorFunction<EventPacket>, Set<T>] { const cache = new Set<T>(); return [ filter((packet) => { const key = keyFn(packet); if (key === null) { return true; } if (cache.has(key)) { options?.onHit?.(packet, cache); return false; } else { cache.add(key); options?.onCache?.(packet, cache); return true; } }), cache, ]; } /** * Only the latest events are allowed to pass. */ export function latest(): MonoTypeOperatorFunction<EventPacket> { return pipe( scan<EventPacket>((acc, packet) => compareEvents(acc.event, packet.event) < 0 ? packet : acc ), distinctUntilChanged( (a, b) => a === b, ({ event }) => event.id ) ); } /** * For each key, only the latest events are allowed to pass. */ export function latestEach<K>( key: (packet: EventPacket) => K ): MonoTypeOperatorFunction<EventPacket> { return pipe(groupBy(key), map(pipe(latest())), mergeAll()); } /** * Only events with a valid signature are allowed to pass. */ export function verify(): MonoTypeOperatorFunction<EventPacket> { return filter<EventPacket>(({ event }) => _verify(event)); } /** * Only events with given kind are allowed to pass. */ export function filterKind<K extends number>( kind: K ): MonoTypeOperatorFunction<EventPacket> { return filter<EventPacket>(({ event }) => event.kind === kind); } /** * Filter events based on a REQ filter object. */ export function filterBy( filters: LazyFilter | LazyFilter[], options?: MatchFilterOptions & FilterByOptions ): MonoTypeOperatorFunction<EventPacket> { const { not } = makeFilterByOptions(options); const evaledFilter = evalFilters(filters); return filter(({ event }) => { const match = isFiltered(event, evaledFilter, options); return not ? !match : match; }); } /** * Accumulate latest events in order of new arrival (based on `created_at`). */ export function timeline( limit?: number ): OperatorFunction<EventPacket, EventPacket[]> { return scan<EventPacket, EventPacket[]>((acc, packet) => { const next = [...acc, packet].sort( (a, b) => -1 * compareEvents(a.event, b.event) ); if (limit !== undefined) { next.splice(limit); } return next; }, []); } export function sortEvents( bufferTime: number, compareFn?: (a: EventPacket, b: EventPacket) => number ): MonoTypeOperatorFunction<EventPacket> { return sort( bufferTime, compareFn ?? ((a, b) => compareEvents(a.event, b.event)) ); } // ----------------------- // // MessagePacket operators // // ----------------------- // export function filterType<T extends Nostr.ToClientMessage.Type>( type: T ): OperatorFunction< MessagePacket, MessagePacket<Nostr.ToClientMessage.Message<T>> > { return filter( (packet): packet is MessagePacket<Nostr.ToClientMessage.Message<T>> => packet.message[0] === type ); } // ------------------- // // ReqPacket operators // // ------------------- // /** * Map REQ packets into a single REQ packet. * * It is useful to reduce REQ requests in a time interval. */ export function batch( /** Function used for merge REQ filters. Default behavior is simple concatenation. */ mergeFilter?: MergeFilter ): OperatorFunction<ReqPacket[], ReqPacket> { return map((f) => f.reduce((acc, v) => { if (acc === null) { return v; } if (v === null) { return acc; } return (mergeFilter ?? defaultMergeFilter)(acc, v); }, null) ); } /** * Chunk a REQ packet into multiple REQ packets. * * It is useful to avoid to send large REQ filter. */ export function chunk( predicate: (f: LazyFilter[]) => boolean, toChunk: (f: LazyFilter[]) => LazyFilter[][] ): MonoTypeOperatorFunction<ReqPacket> { return mergeMap((f) => f !== null && predicate(f) ? of(...toChunk(f)) : of(f) ); } // ----------------- // // General operators // // ----------------- // /** * Almost RxJS's `timeout`, but won't throw. */ export function completeOnTimeout<T>( time: number ): MonoTypeOperatorFunction<T> { return pipe( timeout(time), catchError((error: unknown) => { if (error instanceof TimeoutError) { return EMPTY; } else { throw error; } }) ); } /** * Buffer the received values for a specified time * and return the values in sorted order as possible. */ export function sort<T>( bufferTime: number, compareFn: (a: T, b: T) => number ): MonoTypeOperatorFunction<T> { const buffer: T[] = []; return pipe( tap((v) => { buffer.push(v); buffer.sort(compareFn); }), delay(bufferTime), map(() => { if (buffer.length <= 0) { throw new Error("Logic Error: This is rx-nostr's internal bug."); } // Non-null is valid because the lenght has been checked. // eslint-disable-next-line @typescript-eslint/no-non-null-assertion return buffer.shift()!; }) ); } // ----------- // // Other stuff // // ----------- // export type MergeFilter = (a:
LazyFilter[], b: LazyFilter[]) => LazyFilter[];
function defaultMergeFilter(a: LazyFilter[], b: LazyFilter[]): LazyFilter[] { return [...a, ...b]; } export interface CreateUniqOptions<T> { onCache?: (packet: EventPacket, cache: Set<T>) => void; onHit?: (packet: EventPacket, cache: Set<T>) => void; } export interface FilterByOptions { not: boolean; } const makeFilterByOptions = defineDefaultOptions<FilterByOptions>({ not: false, });
src/operator.ts
penpenpng-rx-nostr-7c52614
[ { "filename": "src/req.ts", "retrieved_chunk": " ): RxReq;\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n pipe(...operators: OperatorFunction<any, any>[]): RxReq {\n const rxReqId = this.rxReqId;\n const strategy = this.strategy;\n return {\n ...this,\n get rxReqId() {\n return rxReqId;\n },", "score": 32.497949221959274 }, { "filename": "src/__test__/operator.test.ts", "retrieved_chunk": " faker.messagePacket(faker.toClientMessage.EVENT(\"*\")),\n ];\n const packet$ = of(...packets).pipe(filterType(\"NOTICE\"));\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n expectObservable(packet$).toEqual(of<any[]>(packets[0], packets[3]));\n });\n});", "score": 31.835121670986155 }, { "filename": "src/util.ts", "retrieved_chunk": "import normalizeUrl from \"normalize-url\";\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport function defineDefaultOptions<T extends Record<string, any>>(\n defaultParams: T\n): (givenParams?: Partial<T>) => T {\n return (givenParams) =>\n Object.fromEntries(\n Object.keys(defaultParams).map((key) => [\n key,\n givenParams?.[key] ?? defaultParams[key],", "score": 29.318637406622567 }, { "filename": "src/req.ts", "retrieved_chunk": " }\n if (Object.keys(res).length <= 0) {\n return null;\n }\n return res;\n}\nfunction normalizeFilters(\n filters: LazyFilter | LazyFilter[] | null\n): LazyFilter[] | null {\n if (!filters) {", "score": 20.153586494424413 }, { "filename": "src/packet.ts", "retrieved_chunk": " */\nexport type LazyFilter = Omit<Nostr.Filter, \"since\" | \"until\"> & {\n since?: number | (() => number);\n until?: number | (() => number);\n};\nexport type LazyREQ = [\"REQ\", string, ...LazyFilter[]];\n/**\n * Packets from websocket that represents an EVENT.\n */\nexport interface EventPacket {", "score": 18.412537700879028 } ]
typescript
LazyFilter[], b: LazyFilter[]) => LazyFilter[];
import Nostr from "nostr-typedef"; import { catchError, delay, distinct, distinctUntilChanged, EMPTY, filter, groupBy, map, mergeAll, mergeMap, type MonoTypeOperatorFunction, type ObservableInput, of, type OperatorFunction, pipe, scan, tap, timeout, TimeoutError, } from "rxjs"; import { evalFilters } from "./helper.js"; import { compareEvents, verify as _verify } from "./nostr/event.js"; import { isFiltered, MatchFilterOptions } from "./nostr/filter.js"; import { EventPacket, LazyFilter, MessagePacket, ReqPacket } from "./packet.js"; import { defineDefaultOptions } from "./util.js"; // --------------------- // // EventPacket operators // // --------------------- // /** * Remove the events once seen. */ export function uniq( flushes?: ObservableInput<unknown> ): MonoTypeOperatorFunction<EventPacket> { return distinct<EventPacket, string>(({ event }) => event.id, flushes); } /** * Create a customizable uniq operator. * * If `keyFn()` returns a non-null key, the key is stored in `Set`. * The operator filters packets with keys already stored. * * The `Set` returned in the second value of the tuple * can be manipulated externally or in optional event handlers. * For example, you can call `Set#clear()` to forget all keys. */ export function createUniq<T>( keyFn: (packet: EventPacket) => T | null, options?: CreateUniqOptions<T> ): [MonoTypeOperatorFunction<EventPacket>, Set<T>] { const cache = new Set<T>(); return [ filter((packet) => { const key = keyFn(packet); if (key === null) { return true; } if (cache.has(key)) { options?.onHit?.(packet, cache); return false; } else { cache.add(key); options?.onCache?.(packet, cache); return true; } }), cache, ]; } /** * Only the latest events are allowed to pass. */ export function latest(): MonoTypeOperatorFunction<EventPacket> { return pipe( scan<EventPacket>((acc, packet) => compareEvents(acc.event, packet.event) < 0 ? packet : acc ), distinctUntilChanged( (a, b) => a === b, ({ event }) => event.id ) ); } /** * For each key, only the latest events are allowed to pass. */ export function latestEach<K>( key: (packet: EventPacket) => K ): MonoTypeOperatorFunction<EventPacket> { return pipe(groupBy(key), map(pipe(latest())), mergeAll()); } /** * Only events with a valid signature are allowed to pass. */ export function verify(): MonoTypeOperatorFunction<EventPacket> { return filter<EventPacket>(({ event }) => _verify(event)); } /** * Only events with given kind are allowed to pass. */ export function filterKind<K extends number>( kind: K ): MonoTypeOperatorFunction<EventPacket> { return filter<EventPacket>(({ event }) => event.kind === kind); } /** * Filter events based on a REQ filter object. */ export function filterBy( filters: LazyFilter | LazyFilter[], options?: MatchFilterOptions & FilterByOptions ): MonoTypeOperatorFunction<EventPacket> { const { not } = makeFilterByOptions(options); const evaledFilter = evalFilters(filters); return filter(({ event }) => { const match = isFiltered(event, evaledFilter, options); return not ? !match : match; }); } /** * Accumulate latest events in order of new arrival (based on `created_at`). */ export function timeline( limit?: number ): OperatorFunction<EventPacket, EventPacket[]> { return scan<EventPacket, EventPacket[]>((acc, packet) => { const next = [...acc, packet].sort( (a, b) => -1 * compareEvents(a.event, b.event) ); if (limit !== undefined) { next.splice(limit); } return next; }, []); } export function sortEvents( bufferTime: number, compareFn?: (a: EventPacket, b: EventPacket) => number ): MonoTypeOperatorFunction<EventPacket> { return sort( bufferTime, compareFn ?? ((a, b) => compareEvents(a.event, b.event)) ); } // ----------------------- // // MessagePacket operators // // ----------------------- // export function filterType<T extends Nostr.ToClientMessage.Type>( type: T ): OperatorFunction< MessagePacket, MessagePacket<Nostr.ToClientMessage.Message<T>> > { return filter( (packet): packet is MessagePacket<Nostr.ToClientMessage.Message<T>> => packet.message[0] === type ); } // ------------------- // // ReqPacket operators // // ------------------- // /** * Map REQ packets into a single REQ packet. * * It is useful to reduce REQ requests in a time interval. */ export function batch( /** Function used for merge REQ filters. Default behavior is simple concatenation. */ mergeFilter?: MergeFilter ): OperatorFunction<ReqPacket[], ReqPacket> { return map((f) => f.reduce((acc, v) => { if (acc === null) { return v; } if (v === null) { return acc; } return (mergeFilter ?? defaultMergeFilter)(acc, v); }, null) ); } /** * Chunk a REQ packet into multiple REQ packets. * * It is useful to avoid to send large REQ filter. */ export function chunk( predicate: (f: LazyFilter[]) => boolean, toChunk: (f: LazyFilter[]) => LazyFilter[][] ): MonoTypeOperatorFunction<ReqPacket> { return mergeMap((f) => f !== null && predicate(f) ? of(...toChunk(f)) : of(f) ); } // ----------------- // // General operators // // ----------------- // /** * Almost RxJS's `timeout`, but won't throw. */ export function completeOnTimeout<T>( time: number ): MonoTypeOperatorFunction<T> { return pipe( timeout(time), catchError((error: unknown) => { if (error instanceof TimeoutError) { return EMPTY; } else { throw error; } }) ); } /** * Buffer the received values for a specified time * and return the values in sorted order as possible. */ export function sort<T>( bufferTime: number, compareFn: (a: T, b: T) => number ): MonoTypeOperatorFunction<T> { const buffer: T[] = []; return pipe( tap((v) => { buffer.push(v); buffer.sort(compareFn); }), delay(bufferTime), map(() => { if (buffer.length <= 0) { throw new Error("Logic Error: This is rx-nostr's internal bug."); } // Non-null is valid because the lenght has been checked. // eslint-disable-next-line @typescript-eslint/no-non-null-assertion return buffer.shift()!; }) ); } // ----------- // // Other stuff // // ----------- // export type MergeFilter = (a: LazyFilter[], b: LazyFilter[]) => LazyFilter[]; function defaultMergeFilter(a: LazyFilter[], b: LazyFilter[]): LazyFilter[] { return [...a, ...b]; } export interface CreateUniqOptions<T> { onCache?: (packet: EventPacket, cache: Set<T>) => void; onHit?: (packet: EventPacket, cache: Set<T>) => void; } export interface FilterByOptions { not: boolean; }
const makeFilterByOptions = defineDefaultOptions<FilterByOptions>({
not: false, });
src/operator.ts
penpenpng-rx-nostr-7c52614
[ { "filename": "src/__test__/helper.ts", "retrieved_chunk": " },\n};\nexport function spySub(): {\n completed: () => boolean;\n error: () => boolean;\n count: () => number;\n subscribed: () => boolean;\n unsubscribed: () => boolean;\n tap: <T>() => MonoTypeOperatorFunction<T>;\n} {", "score": 27.314797922984734 }, { "filename": "src/__test__/helper.ts", "retrieved_chunk": " tap((packet) => {\n tapNext(packet.message);\n }),\n ...spy,\n };\n}\nexport function spyEvent(): {\n tap: () => MonoTypeOperatorFunction<EventPacket>;\n} {\n let tapNext: (message: Nostr.ToClientMessage.Any) => void;", "score": 25.60736175224719 }, { "filename": "src/util.ts", "retrieved_chunk": "import normalizeUrl from \"normalize-url\";\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport function defineDefaultOptions<T extends Record<string, any>>(\n defaultParams: T\n): (givenParams?: Partial<T>) => T {\n return (givenParams) =>\n Object.fromEntries(\n Object.keys(defaultParams).map((key) => [\n key,\n givenParams?.[key] ?? defaultParams[key],", "score": 24.84021476169264 }, { "filename": "src/req.ts", "retrieved_chunk": " (): ThisType<R> & T;\n}\n/** NOTE: unstable feature */\nexport function mixin<R extends object, T extends object>(\n def: () => ThisType<R> & T\n): Mixin<R, T> {\n return def;\n}\n/** NOTE: unstable feature */\nexport function extend<B extends R, R extends object, T extends object>(", "score": 24.48750208762965 }, { "filename": "src/util.ts", "retrieved_chunk": " ])\n ) as T;\n}\nexport type Override<T extends object, U extends object> = {\n [K in keyof T | keyof U]: K extends keyof U\n ? U[K]\n : K extends keyof T\n ? T[K]\n : never;\n};", "score": 24.137615429873225 } ]
typescript
const makeFilterByOptions = defineDefaultOptions<FilterByOptions>({
import Nostr from "nostr-typedef"; import { catchError, EMPTY, filter, finalize, first, identity, map, merge, mergeAll, mergeMap, type MonoTypeOperatorFunction, Observable, of, type OperatorFunction, ReplaySubject, Subject, Subscription, take, takeUntil, tap, timeout, type Unsubscribable, } from "rxjs"; import { BackoffConfig, Connection } from "./connection.js"; import { getSignedEvent } from "./nostr/event.js"; import { fetchRelayInfo } from "./nostr/nip11.js"; import { completeOnTimeout } from "./operator.js"; import type { ConnectionState, ConnectionStatePacket, ErrorPacket, EventPacket, LazyFilter, LazyREQ, MessagePacket, OkPacket, } from "./packet.js"; import type { RxReq } from "./req.js"; import { defineDefaultOptions, normalizeRelayUrl } from "./util.js"; /** * The core object of rx-nostr, which holds a connection to relays * and manages subscriptions as directed by the RxReq object connected by `use()`. * Use `createRxNostr()` to get the object. */ export interface RxNostr { /** * Return a list of relays used by this object. * The relay URLs are normalised so may not match the URLs set. */ getRelays(): RelayConfig[]; /** * Set the list of relays. * If a REQ subscription already exists, the same REQ is issued for the newly added relay * and CLOSE is sent for the removed relay. */ switchRelays(config: AcceptableRelaysConfig): Promise<void>; /** Utility wrapper for `switchRelays()`. */ addRelay(relay: string | RelayConfig): Promise<void>; /** Utility wrapper for `switchRelays()`. */ removeRelay(url: string): Promise<void>; /** Return true if the given relay is set to rxNostr. */ hasRelay(url: string): boolean; /** Return true if the given relay allows to be written. */ canWriteRelay(url: string): boolean; /** Return true if the given relay allows to be read. */ canReadRelay(url: string): boolean; /** Fetch all relays' info based on [NIP-11](https://github.com/nostr-protocol/nips/blob/master/11.md) */ fetchAllRelaysInfo(): Promise<Record<string, Nostr.Nip11.RelayInfo | null>>; /** * Return a dictionary in which you can look up connection state. * * **NOTE**: Keys are **normalized** URL, so may be different from one you set. */ getAllRelayState(): Record<string, ConnectionState>; /** * Return connection state of the given relay. * Throw if unknown URL is given. */ getRelayState(url: string): ConnectionState; /** * Attempt to reconnect the WebSocket if its state is `error` or `rejected`. * If not, do nothing. */ reconnect(url: string): void; // TODO: document /** * Set or unset a pipe to be applied to all EventPackets. */ setGlobalEventPacketPipe( pipe: MonoTypeOperatorFunction<EventPacket> | null ): void; /** * Associate RxReq with RxNostr. * When the associated RxReq is manipulated, * the RxNostr issues a new REQ to all relays allowed to be read. * The method returns an Observable that issues EventPackets * when an EVENT is received that is subscribed by RxReq. * You can unsubscribe the Observable to CLOSE. */ use( rxReq: RxReq, options?: Partial<RxNostrUseOptions> ): Observable<EventPacket>; /** * Create an Observable that receives all events (EVENT) from all websocket connections. * * Nothing happens when this Observable is unsubscribed. * */ createAllEventObservable(): Observable<EventPacket>; /** * Create an Observable that receives all errors from all websocket connections. * Note that an Observable is terminated when it receives any error, * so this method is the only way to receive errors arising from multiplexed websocket connections * (It means that Observables returned by `use()` never throw error). * * Nothing happens when this Observable is unsubscribed. * */ createAllErrorObservable(): Observable<ErrorPacket>; /** * Create an Observable that receives all messages from all websocket connections. * * Nothing happens when this Observable is unsubscribed. * */ createAllMessageObservable(): Observable<MessagePacket>; /** * Create an Observable that receives changing of WebSocket connection state. * * Nothing happens when this Observable is unsubscribed. */ createConnectionStateObservable(): Observable<ConnectionStatePacket>; /** * Attempt to send events to all relays that are allowed to write. * The `seckey` option accepts both nsec format and hex format, * and if omitted NIP-07 will be automatically used. */ send( params: Nostr.EventParameters, options?: RxNostrSendOptions ): Observable<OkPacket>; /** * Release all resources held by the RxNostr object. * Any Observable resulting from this RxNostr will be in the completed state * and will never receive messages again. * RxReq used by this object is not affected; in other words, if the RxReq is used * by another RxNostr, its use is not prevented. */ dispose(): void; } /** Create a RxNostr object. This is the only way to create that. */ export function createRxNostr(options?: Partial<RxNostrOptions>): RxNostr { return new RxNostrImpl(options); } export interface RxNostrOptions { /** Auto reconnection strategy. */ retry: BackoffConfig; /** * The time in milliseconds to timeout when following the backward strategy. * The observable is terminated when the specified amount of time has elapsed * during which no new events are available. */ timeout: number; globalRelayConfig?: { disableAutoFetchNip11Limitations?: boolean; maxConcurrentReqsFallback?: number; }; } const makeRxNostrOptions
= defineDefaultOptions<RxNostrOptions>({
retry: { strategy: "exponential", maxCount: 5, initialDelay: 1000, }, timeout: 10000, globalRelayConfig: undefined, }); export interface RxNostrUseOptions { scope?: string[]; } const makeRxNostrUseOptions = defineDefaultOptions<RxNostrUseOptions>({ scope: undefined, }); export interface RxNostrSendOptions { scope?: string[]; seckey?: string; } const makeRxNostrSendOptions = defineDefaultOptions<RxNostrSendOptions>({ scope: undefined, seckey: undefined, }); /** Config object specifying WebSocket behavior. */ export interface RelayConfig { /** WebSocket endpoint URL. */ url: string; /** If true, rxNostr can publish REQ and subscribe EVENTs. */ read: boolean; /** If true, rxNostr can send EVENTs. */ write: boolean; disableAutoFetchNip11Limitations?: boolean; } /** Parameter of `rxNostr.switchRelays()` */ export type AcceptableRelaysConfig = | (string | RelayConfig)[] | Nostr.Nip07.GetRelayResult; class RxNostrImpl implements RxNostr { private options: RxNostrOptions; private connections: Map<string, Connection> = new Map(); private ongoings: Map<string, OngoingReq> = new Map(); private messageIn$: Subject<MessagePacket> = new Subject(); private error$: Subject<ErrorPacket> = new Subject(); private status$: Subject<ConnectionStatePacket> = new Subject(); private globalEventPacketPipe: MonoTypeOperatorFunction<EventPacket> | null = null; private disposed = false; private get messageOut$() { return this.messageIn$.pipe( mergeMap((packet) => { const pipe = this.globalEventPacketPipe; if (!pipe) { return of(packet); } const message = packet.message; if (message[0] !== "EVENT") { return of(packet); } return of({ from: packet.from, subId: message[1], event: message[2], }).pipe( pipe, map( ({ from, subId, event }): MessagePacket => ({ from, message: ["EVENT", subId, event], }) ) ); }) ); } constructor(options?: Partial<RxNostrOptions>) { const opt = makeRxNostrOptions(options); this.options = { ...opt, }; } getRelays(): RelayConfig[] { return Array.from(this.connections.values()).map( ({ url, read, write }) => ({ url, read, write, }) ); } private createConnection({ url, read, write, disableAutoFetchNip11Limitations, }: RelayConfig): Connection { const connection = new Connection(url, { backoff: this.options.retry, read, write, disableAutoFetchNip11Limitations: disableAutoFetchNip11Limitations ?? this.options.globalRelayConfig?.disableAutoFetchNip11Limitations, maxConcurrentReqsFallback: this.options.globalRelayConfig?.maxConcurrentReqsFallback, }); connection.getConnectionStateObservable().subscribe((state) => { this.status$.next({ from: url, state, }); }); connection.getErrorObservable().subscribe((reason) => { this.error$.next({ from: url, reason }); }); connection .getMessageObservable() .pipe( catchError((reason: unknown) => { this.error$.next({ from: url, reason }); return EMPTY; }) ) .subscribe((v) => { this.messageIn$.next(v); }); return connection; } async switchRelays(config: AcceptableRelaysConfig): Promise<void> { const nextConns: Map<string, Connection> = new Map(); for (const { url, read, write } of normalizeRelaysConfig(config)) { // pop a connection if exists const prevConn = this.connections.get(url); this.connections.delete(url); if (prevConn) { prevConn.read = read; prevConn.write = write; nextConns.set(url, prevConn); } else { nextConns.set(url, this.createConnection({ url, read, write })); } } // connections that are no longer used for (const conn of this.connections.values()) { conn.dispose(); } const ensureConns: Promise<unknown>[] = []; for (const conn of nextConns.values()) { if (conn.read) { ensureConns.push(conn.start()); } else { conn.stop(); } } await Promise.all(ensureConns); this.connections = nextConns; // If disposed during switchRelay processing if (this.disposed) { for (const conn of this.connections.values()) { conn.dispose(); } return; } for (const { req, scope } of this.ongoings.values()) { this.ensureReq(req, { scope }); } // --- scoped untility pure functions --- function normalizeRelaysConfig( config: AcceptableRelaysConfig ): RelayConfig[] { if (Array.isArray(config)) { return config.map((urlOrConfig) => { const relay: RelayConfig = typeof urlOrConfig === "string" ? { url: urlOrConfig, read: true, write: true, } : urlOrConfig; relay.url = normalizeRelayUrl(relay.url); return relay; }); } else { return Object.entries(config).map(([url, flags]) => ({ url: normalizeRelayUrl(url), ...flags, })); } } } async addRelay(relay: string | RelayConfig): Promise<void> { await this.switchRelays([...this.getRelays(), relay]); } async removeRelay(url: string): Promise<void> { const u = normalizeRelayUrl(url); const currentRelays = this.getRelays(); const nextRelays = currentRelays.filter((relay) => relay.url !== u); if (currentRelays.length !== nextRelays.length) { await this.switchRelays(nextRelays); } } hasRelay(url: string): boolean { const u = normalizeRelayUrl(url); return this.getRelays().some((relay) => relay.url === u); } canWriteRelay(url: string): boolean { const u = normalizeRelayUrl(url); return this.getRelays().some((relay) => relay.url === u && relay.write); } canReadRelay(url: string): boolean { const u = normalizeRelayUrl(url); return this.getRelays().some((relay) => relay.url === u && relay.read); } async fetchAllRelaysInfo(): Promise< Record<string, Nostr.Nip11.RelayInfo | null> > { const entries = await Promise.all( Array.from(this.connections.keys()).map( async (url): Promise<[string, Nostr.Nip11.RelayInfo | null]> => [ url, await fetchRelayInfo(url).catch(() => null), ] ) ); return Object.fromEntries(entries); } getAllRelayState(): Record<string, ConnectionState> { return Object.fromEntries( Array.from(this.connections.values()).map((e) => [ e.url, this.getRelayState(e.url), ]) ); } getRelayState(url: string): ConnectionState { const conn = this.connections.get(normalizeRelayUrl(url)); if (!conn) { throw new Error("RelayConfig not found"); } // this.relays[url] may be set before this.relays[url].websocket is initialized return conn?.getConnectionState() ?? "not-started"; } reconnect(url: string): void { if (this.canReadRelay(url)) { this.connections.get(normalizeRelayUrl(url))?.start(); } } setGlobalEventPacketPipe(pipe: MonoTypeOperatorFunction<EventPacket> | null) { this.globalEventPacketPipe = pipe; } use( rxReq: RxReq, options?: Partial<RxNostrUseOptions> ): Observable<EventPacket> { const { scope: _scope } = makeRxNostrUseOptions(options); const scope = _scope?.map(normalizeRelayUrl); const TIMEOUT = this.options.timeout; const strategy = rxReq.strategy; const rxReqId = rxReq.rxReqId; const message$ = this.messageOut$; const ongoings = this.ongoings; const getAllRelayState = this.getAllRelayState.bind(this); const createConnectionStateObservable = this.createConnectionStateObservable.bind(this); const ensureReq = this.ensureReq.bind(this); const finalizeReq = this.finalizeReq.bind(this); const subId$ = rxReq.getReqObservable().pipe( filter((filters): filters is LazyFilter[] => filters !== null), strategy === "oneshot" ? first() : identity, attachSubId(), strategy === "forward" ? manageActiveForwardReq() : identity, tap((req) => { ensureReq(req, { overwrite: strategy === "forward", scope }); }), map(([, subId]) => subId) ); if (strategy === "forward") { const subId = makeSubId({ rxReqId, }); const resource: Unsubscribable[] = []; const subject = new Subject<EventPacket>(); resource.push(subject); return subject.pipe( tap({ subscribe: () => { resource.push(subId$.subscribe()); resource.push( message$ .pipe(filterBySubId(subId), pickEvents()) .subscribe((v) => { subject.next(v); }) ); }, finalize: () => { for (const r of resource) { r.unsubscribe(); } finalizeReq({ subId }); }, }) ); } else { return subId$.pipe(map(createEoseManagedEventObservable), mergeAll()); } function attachSubId(): OperatorFunction<LazyFilter[], LazyREQ> { const makeId = (index?: number) => makeSubId({ rxReqId, index }); switch (strategy) { case "backward": return map((filters, index) => ["REQ", makeId(index), ...filters]); case "forward": case "oneshot": return map((filters) => ["REQ", makeId(), ...filters]); } } function manageActiveForwardReq(): MonoTypeOperatorFunction<LazyREQ> { const recordActiveReq = (req: LazyREQ) => { const subId = req[1]; ongoings.set(subId, { req, scope, }); }; const forgetActiveReq = (subId: string) => { ongoings.delete(subId); }; return tap({ next: (req: LazyREQ) => { recordActiveReq(req); }, finalize: () => { forgetActiveReq( makeSubId({ rxReqId, }) ); }, }); } function createEoseManagedEventObservable( subId: string ): Observable<EventPacket> { const eose$ = new Subject<void>(); const complete$ = new Subject<void>(); const eoseRelays = new Set<string>(); const manageCompletion = merge( eose$, createConnectionStateObservable() ).subscribe(() => { const status = getAllRelayState(); const shouldComplete = Object.entries(status).every( ([url, state]) => (scope && !scope.includes(url)) || state === "error" || state === "terminated" || (state === "ongoing" && eoseRelays.has(url)) ); if (shouldComplete) { complete$.next(); } }); return message$.pipe( takeUntil(complete$), completeOnTimeout(TIMEOUT), filterBySubId(subId), filter((e) => !eoseRelays.has(e.from)), tap((e) => { if (e.message[0] === "EOSE") { eoseRelays.add(e.from); finalizeReq({ subId, url: e.from }); eose$.next(); } }), pickEvents(), finalize(() => { finalizeReq({ subId }); complete$.unsubscribe(); eose$.unsubscribe(); manageCompletion.unsubscribe(); }) ); } function filterBySubId( subId: string ): MonoTypeOperatorFunction<MessagePacket> { return filter( (packet) => (packet.message[0] === "EVENT" || packet.message[0] === "EOSE") && packet.message[1] === subId ); } function pickEvents(): OperatorFunction<MessagePacket, EventPacket> { return mergeMap(({ from, message }) => message[0] === "EVENT" ? of({ from, subId: message[1], event: message[2] }) : EMPTY ); } } createAllEventObservable(): Observable<EventPacket> { return this.messageOut$.pipe( mergeMap(({ from, message }) => message[0] === "EVENT" ? of({ from, subId: message[1], event: message[2] }) : EMPTY ) ); } createAllErrorObservable(): Observable<ErrorPacket> { return this.error$.asObservable(); } createAllMessageObservable(): Observable<MessagePacket> { return this.messageOut$; } createConnectionStateObservable(): Observable<ConnectionStatePacket> { return this.status$.asObservable(); } send( params: Nostr.EventParameters, options?: RxNostrSendOptions ): Observable<OkPacket> { const { seckey, scope: _scope } = makeRxNostrSendOptions(options); const scope = _scope?.map(normalizeRelayUrl); const writableConns = Array.from(this.connections.values()).filter( (conn) => (!scope || scope.includes(conn.url)) && conn.write ); const subject = new ReplaySubject<OkPacket>(writableConns.length); let subscription: Subscription | null = null; getSignedEvent(params, seckey).then((event) => { if (!subject.closed) { subscription = this.createAllMessageObservable().subscribe( ({ from, message }) => { if (message[0] !== "OK") { return; } subject.next({ from, id: event.id, ok: message[2], }); } ); } for (const conn of writableConns) { conn.sendEVENT(["EVENT", event]); } }); return subject.pipe( take(writableConns.length), timeout(30 * 1000), finalize(() => { subject.complete(); subject.unsubscribe(); subscription?.unsubscribe(); }) ); } dispose(): void { this.disposed = true; this.messageIn$.complete(); this.error$.complete(); for (const conn of this.connections.values()) { conn.dispose(); } } private ensureReq( req: LazyREQ, options?: { scope?: string[] | null; overwrite?: boolean } ) { const scope = options?.scope; for (const url of this.connections.keys()) { const conn = this.connections.get(url); if (!conn || !conn.read || (scope && !scope.includes(url))) { continue; } conn.ensureReq(req, { overwrite: options?.overwrite }); } } private finalizeReq(params: { subId: string; url?: string }) { const { subId, url } = params; if (subId === undefined && url === undefined) { throw new Error(); } if (url) { const conn = this.connections.get(url); conn?.finalizeReq(subId); } else { for (const conn of this.connections.values()) { conn?.finalizeReq(subId); } } } } interface OngoingReq { req: LazyREQ; scope?: string[]; } function makeSubId(params: { rxReqId: string; index?: number }): string { return `${params.rxReqId}:${params.index ?? 0}`; }
src/rx-nostr.ts
penpenpng-rx-nostr-7c52614
[ { "filename": "src/connection.ts", "retrieved_chunk": " read: boolean;\n write: boolean;\n disableAutoFetchNip11Limitations?: boolean;\n maxConcurrentReqsFallback?: number;\n}\nexport type BackoffConfig =\n | {\n // Exponential backoff and jitter strategy\n strategy: \"exponential\";\n maxCount: number;", "score": 21.23369670609905 }, { "filename": "src/__test__/subscription.test.ts", "retrieved_chunk": " globalRelayConfig: {\n disableAutoFetchNip11Limitations: true,\n maxConcurrentReqsFallback: 1,\n },\n });\n await rxNostr.switchRelays([RELAY_URL]);\n await relay.connected;\n });\n afterEach(() => {\n rxNostr.dispose();", "score": 15.405087770431606 }, { "filename": "src/operator.ts", "retrieved_chunk": " * Only events with given kind are allowed to pass.\n */\nexport function filterKind<K extends number>(\n kind: K\n): MonoTypeOperatorFunction<EventPacket> {\n return filter<EventPacket>(({ event }) => event.kind === kind);\n}\n/**\n * Filter events based on a REQ filter object.\n */", "score": 11.85596520844915 }, { "filename": "src/__test__/helper.ts", "retrieved_chunk": " },\n};\nexport function spySub(): {\n completed: () => boolean;\n error: () => boolean;\n count: () => number;\n subscribed: () => boolean;\n unsubscribed: () => boolean;\n tap: <T>() => MonoTypeOperatorFunction<T>;\n} {", "score": 10.951435604517584 }, { "filename": "src/operator.ts", "retrieved_chunk": "/**\n * Almost RxJS's `timeout`, but won't throw.\n */\nexport function completeOnTimeout<T>(\n time: number\n): MonoTypeOperatorFunction<T> {\n return pipe(\n timeout(time),\n catchError((error: unknown) => {\n if (error instanceof TimeoutError) {", "score": 10.604807981070653 } ]
typescript
= defineDefaultOptions<RxNostrOptions>({
import Nostr from "nostr-typedef"; import { catchError, EMPTY, filter, finalize, first, identity, map, merge, mergeAll, mergeMap, type MonoTypeOperatorFunction, Observable, of, type OperatorFunction, ReplaySubject, Subject, Subscription, take, takeUntil, tap, timeout, type Unsubscribable, } from "rxjs"; import { BackoffConfig, Connection } from "./connection.js"; import { getSignedEvent } from "./nostr/event.js"; import { fetchRelayInfo } from "./nostr/nip11.js"; import { completeOnTimeout } from "./operator.js"; import type { ConnectionState, ConnectionStatePacket, ErrorPacket, EventPacket, LazyFilter, LazyREQ, MessagePacket, OkPacket, } from "./packet.js"; import type { RxReq } from "./req.js"; import { defineDefaultOptions, normalizeRelayUrl } from "./util.js"; /** * The core object of rx-nostr, which holds a connection to relays * and manages subscriptions as directed by the RxReq object connected by `use()`. * Use `createRxNostr()` to get the object. */ export interface RxNostr { /** * Return a list of relays used by this object. * The relay URLs are normalised so may not match the URLs set. */ getRelays(): RelayConfig[]; /** * Set the list of relays. * If a REQ subscription already exists, the same REQ is issued for the newly added relay * and CLOSE is sent for the removed relay. */ switchRelays(config: AcceptableRelaysConfig): Promise<void>; /** Utility wrapper for `switchRelays()`. */ addRelay(relay: string | RelayConfig): Promise<void>; /** Utility wrapper for `switchRelays()`. */ removeRelay(url: string): Promise<void>; /** Return true if the given relay is set to rxNostr. */ hasRelay(url: string): boolean; /** Return true if the given relay allows to be written. */ canWriteRelay(url: string): boolean; /** Return true if the given relay allows to be read. */ canReadRelay(url: string): boolean; /** Fetch all relays' info based on [NIP-11](https://github.com/nostr-protocol/nips/blob/master/11.md) */ fetchAllRelaysInfo(): Promise<Record<string, Nostr.Nip11.RelayInfo | null>>; /** * Return a dictionary in which you can look up connection state. * * **NOTE**: Keys are **normalized** URL, so may be different from one you set. */ getAllRelayState(): Record<string, ConnectionState>; /** * Return connection state of the given relay. * Throw if unknown URL is given. */ getRelayState(url: string): ConnectionState; /** * Attempt to reconnect the WebSocket if its state is `error` or `rejected`. * If not, do nothing. */ reconnect(url: string): void; // TODO: document /** * Set or unset a pipe to be applied to all EventPackets. */ setGlobalEventPacketPipe( pipe: MonoTypeOperatorFunction<EventPacket> | null ): void; /** * Associate RxReq with RxNostr. * When the associated RxReq is manipulated, * the RxNostr issues a new REQ to all relays allowed to be read. * The method returns an Observable that issues EventPackets * when an EVENT is received that is subscribed by RxReq. * You can unsubscribe the Observable to CLOSE. */ use( rxReq: RxReq, options?: Partial<RxNostrUseOptions> ): Observable<EventPacket>; /** * Create an Observable that receives all events (EVENT) from all websocket connections. * * Nothing happens when this Observable is unsubscribed. * */ createAllEventObservable(): Observable<EventPacket>; /** * Create an Observable that receives all errors from all websocket connections. * Note that an Observable is terminated when it receives any error, * so this method is the only way to receive errors arising from multiplexed websocket connections * (It means that Observables returned by `use()` never throw error). * * Nothing happens when this Observable is unsubscribed. * */ createAllErrorObservable(): Observable<ErrorPacket>; /** * Create an Observable that receives all messages from all websocket connections. * * Nothing happens when this Observable is unsubscribed. * */ createAllMessageObservable(): Observable<MessagePacket>; /** * Create an Observable that receives changing of WebSocket connection state. * * Nothing happens when this Observable is unsubscribed. */ createConnectionStateObservable(): Observable<ConnectionStatePacket>; /** * Attempt to send events to all relays that are allowed to write. * The `seckey` option accepts both nsec format and hex format, * and if omitted NIP-07 will be automatically used. */ send( params: Nostr.EventParameters, options?: RxNostrSendOptions ): Observable<OkPacket>; /** * Release all resources held by the RxNostr object. * Any Observable resulting from this RxNostr will be in the completed state * and will never receive messages again. * RxReq used by this object is not affected; in other words, if the RxReq is used * by another RxNostr, its use is not prevented. */ dispose(): void; } /** Create a RxNostr object. This is the only way to create that. */ export function createRxNostr(options?: Partial<RxNostrOptions>): RxNostr { return new RxNostrImpl(options); } export interface RxNostrOptions { /** Auto reconnection strategy. */ retry: BackoffConfig; /** * The time in milliseconds to timeout when following the backward strategy. * The observable is terminated when the specified amount of time has elapsed * during which no new events are available. */ timeout: number; globalRelayConfig?: { disableAutoFetchNip11Limitations?: boolean; maxConcurrentReqsFallback?: number; }; } const makeRxNostrOptions = defineDefaultOptions<RxNostrOptions>({ retry: { strategy: "exponential", maxCount: 5, initialDelay: 1000, }, timeout: 10000, globalRelayConfig: undefined, }); export interface RxNostrUseOptions { scope?: string[]; } const makeRxNostrUseOptions = defineDefaultOptions<RxNostrUseOptions>({ scope: undefined, }); export interface RxNostrSendOptions { scope?: string[]; seckey?: string; } const makeRxNostrSendOptions = defineDefaultOptions<RxNostrSendOptions>({ scope: undefined, seckey: undefined, }); /** Config object specifying WebSocket behavior. */ export interface RelayConfig { /** WebSocket endpoint URL. */ url: string; /** If true, rxNostr can publish REQ and subscribe EVENTs. */ read: boolean; /** If true, rxNostr can send EVENTs. */ write: boolean; disableAutoFetchNip11Limitations?: boolean; } /** Parameter of `rxNostr.switchRelays()` */ export type AcceptableRelaysConfig = | (string | RelayConfig)[] | Nostr.Nip07.GetRelayResult; class RxNostrImpl implements RxNostr { private options: RxNostrOptions; private connections: Map<string, Connection> = new Map(); private ongoings: Map<string, OngoingReq> = new Map(); private messageIn$: Subject<MessagePacket> = new Subject(); private error$: Subject<ErrorPacket> = new Subject(); private status$: Subject<ConnectionStatePacket> = new Subject(); private globalEventPacketPipe: MonoTypeOperatorFunction<EventPacket> | null = null; private disposed = false; private get messageOut$() { return this.messageIn$.pipe( mergeMap((packet) => { const pipe = this.globalEventPacketPipe; if (!pipe) { return of(packet); } const message = packet.message; if (message[0] !== "EVENT") { return of(packet); } return of({ from: packet.from, subId: message[1], event: message[2], }).pipe( pipe, map( ({ from, subId, event }): MessagePacket => ({ from, message: ["EVENT", subId, event], }) ) ); }) ); } constructor(options?: Partial<RxNostrOptions>) { const opt = makeRxNostrOptions(options); this.options = { ...opt, }; } getRelays(): RelayConfig[] { return Array.from(this.connections.values()).map( ({ url, read, write }) => ({ url, read, write, }) ); } private createConnection({ url, read, write, disableAutoFetchNip11Limitations, }: RelayConfig): Connection { const connection = new Connection(url, { backoff: this.options.retry, read, write, disableAutoFetchNip11Limitations: disableAutoFetchNip11Limitations ?? this.options.globalRelayConfig?.disableAutoFetchNip11Limitations, maxConcurrentReqsFallback: this.options.globalRelayConfig?.maxConcurrentReqsFallback, }); connection.getConnectionStateObservable().subscribe((state) => { this.status$.next({ from: url, state, }); }); connection.getErrorObservable().subscribe((reason) => { this.error$.next({ from: url, reason }); }); connection
.getMessageObservable() .pipe( catchError((reason: unknown) => {
this.error$.next({ from: url, reason }); return EMPTY; }) ) .subscribe((v) => { this.messageIn$.next(v); }); return connection; } async switchRelays(config: AcceptableRelaysConfig): Promise<void> { const nextConns: Map<string, Connection> = new Map(); for (const { url, read, write } of normalizeRelaysConfig(config)) { // pop a connection if exists const prevConn = this.connections.get(url); this.connections.delete(url); if (prevConn) { prevConn.read = read; prevConn.write = write; nextConns.set(url, prevConn); } else { nextConns.set(url, this.createConnection({ url, read, write })); } } // connections that are no longer used for (const conn of this.connections.values()) { conn.dispose(); } const ensureConns: Promise<unknown>[] = []; for (const conn of nextConns.values()) { if (conn.read) { ensureConns.push(conn.start()); } else { conn.stop(); } } await Promise.all(ensureConns); this.connections = nextConns; // If disposed during switchRelay processing if (this.disposed) { for (const conn of this.connections.values()) { conn.dispose(); } return; } for (const { req, scope } of this.ongoings.values()) { this.ensureReq(req, { scope }); } // --- scoped untility pure functions --- function normalizeRelaysConfig( config: AcceptableRelaysConfig ): RelayConfig[] { if (Array.isArray(config)) { return config.map((urlOrConfig) => { const relay: RelayConfig = typeof urlOrConfig === "string" ? { url: urlOrConfig, read: true, write: true, } : urlOrConfig; relay.url = normalizeRelayUrl(relay.url); return relay; }); } else { return Object.entries(config).map(([url, flags]) => ({ url: normalizeRelayUrl(url), ...flags, })); } } } async addRelay(relay: string | RelayConfig): Promise<void> { await this.switchRelays([...this.getRelays(), relay]); } async removeRelay(url: string): Promise<void> { const u = normalizeRelayUrl(url); const currentRelays = this.getRelays(); const nextRelays = currentRelays.filter((relay) => relay.url !== u); if (currentRelays.length !== nextRelays.length) { await this.switchRelays(nextRelays); } } hasRelay(url: string): boolean { const u = normalizeRelayUrl(url); return this.getRelays().some((relay) => relay.url === u); } canWriteRelay(url: string): boolean { const u = normalizeRelayUrl(url); return this.getRelays().some((relay) => relay.url === u && relay.write); } canReadRelay(url: string): boolean { const u = normalizeRelayUrl(url); return this.getRelays().some((relay) => relay.url === u && relay.read); } async fetchAllRelaysInfo(): Promise< Record<string, Nostr.Nip11.RelayInfo | null> > { const entries = await Promise.all( Array.from(this.connections.keys()).map( async (url): Promise<[string, Nostr.Nip11.RelayInfo | null]> => [ url, await fetchRelayInfo(url).catch(() => null), ] ) ); return Object.fromEntries(entries); } getAllRelayState(): Record<string, ConnectionState> { return Object.fromEntries( Array.from(this.connections.values()).map((e) => [ e.url, this.getRelayState(e.url), ]) ); } getRelayState(url: string): ConnectionState { const conn = this.connections.get(normalizeRelayUrl(url)); if (!conn) { throw new Error("RelayConfig not found"); } // this.relays[url] may be set before this.relays[url].websocket is initialized return conn?.getConnectionState() ?? "not-started"; } reconnect(url: string): void { if (this.canReadRelay(url)) { this.connections.get(normalizeRelayUrl(url))?.start(); } } setGlobalEventPacketPipe(pipe: MonoTypeOperatorFunction<EventPacket> | null) { this.globalEventPacketPipe = pipe; } use( rxReq: RxReq, options?: Partial<RxNostrUseOptions> ): Observable<EventPacket> { const { scope: _scope } = makeRxNostrUseOptions(options); const scope = _scope?.map(normalizeRelayUrl); const TIMEOUT = this.options.timeout; const strategy = rxReq.strategy; const rxReqId = rxReq.rxReqId; const message$ = this.messageOut$; const ongoings = this.ongoings; const getAllRelayState = this.getAllRelayState.bind(this); const createConnectionStateObservable = this.createConnectionStateObservable.bind(this); const ensureReq = this.ensureReq.bind(this); const finalizeReq = this.finalizeReq.bind(this); const subId$ = rxReq.getReqObservable().pipe( filter((filters): filters is LazyFilter[] => filters !== null), strategy === "oneshot" ? first() : identity, attachSubId(), strategy === "forward" ? manageActiveForwardReq() : identity, tap((req) => { ensureReq(req, { overwrite: strategy === "forward", scope }); }), map(([, subId]) => subId) ); if (strategy === "forward") { const subId = makeSubId({ rxReqId, }); const resource: Unsubscribable[] = []; const subject = new Subject<EventPacket>(); resource.push(subject); return subject.pipe( tap({ subscribe: () => { resource.push(subId$.subscribe()); resource.push( message$ .pipe(filterBySubId(subId), pickEvents()) .subscribe((v) => { subject.next(v); }) ); }, finalize: () => { for (const r of resource) { r.unsubscribe(); } finalizeReq({ subId }); }, }) ); } else { return subId$.pipe(map(createEoseManagedEventObservable), mergeAll()); } function attachSubId(): OperatorFunction<LazyFilter[], LazyREQ> { const makeId = (index?: number) => makeSubId({ rxReqId, index }); switch (strategy) { case "backward": return map((filters, index) => ["REQ", makeId(index), ...filters]); case "forward": case "oneshot": return map((filters) => ["REQ", makeId(), ...filters]); } } function manageActiveForwardReq(): MonoTypeOperatorFunction<LazyREQ> { const recordActiveReq = (req: LazyREQ) => { const subId = req[1]; ongoings.set(subId, { req, scope, }); }; const forgetActiveReq = (subId: string) => { ongoings.delete(subId); }; return tap({ next: (req: LazyREQ) => { recordActiveReq(req); }, finalize: () => { forgetActiveReq( makeSubId({ rxReqId, }) ); }, }); } function createEoseManagedEventObservable( subId: string ): Observable<EventPacket> { const eose$ = new Subject<void>(); const complete$ = new Subject<void>(); const eoseRelays = new Set<string>(); const manageCompletion = merge( eose$, createConnectionStateObservable() ).subscribe(() => { const status = getAllRelayState(); const shouldComplete = Object.entries(status).every( ([url, state]) => (scope && !scope.includes(url)) || state === "error" || state === "terminated" || (state === "ongoing" && eoseRelays.has(url)) ); if (shouldComplete) { complete$.next(); } }); return message$.pipe( takeUntil(complete$), completeOnTimeout(TIMEOUT), filterBySubId(subId), filter((e) => !eoseRelays.has(e.from)), tap((e) => { if (e.message[0] === "EOSE") { eoseRelays.add(e.from); finalizeReq({ subId, url: e.from }); eose$.next(); } }), pickEvents(), finalize(() => { finalizeReq({ subId }); complete$.unsubscribe(); eose$.unsubscribe(); manageCompletion.unsubscribe(); }) ); } function filterBySubId( subId: string ): MonoTypeOperatorFunction<MessagePacket> { return filter( (packet) => (packet.message[0] === "EVENT" || packet.message[0] === "EOSE") && packet.message[1] === subId ); } function pickEvents(): OperatorFunction<MessagePacket, EventPacket> { return mergeMap(({ from, message }) => message[0] === "EVENT" ? of({ from, subId: message[1], event: message[2] }) : EMPTY ); } } createAllEventObservable(): Observable<EventPacket> { return this.messageOut$.pipe( mergeMap(({ from, message }) => message[0] === "EVENT" ? of({ from, subId: message[1], event: message[2] }) : EMPTY ) ); } createAllErrorObservable(): Observable<ErrorPacket> { return this.error$.asObservable(); } createAllMessageObservable(): Observable<MessagePacket> { return this.messageOut$; } createConnectionStateObservable(): Observable<ConnectionStatePacket> { return this.status$.asObservable(); } send( params: Nostr.EventParameters, options?: RxNostrSendOptions ): Observable<OkPacket> { const { seckey, scope: _scope } = makeRxNostrSendOptions(options); const scope = _scope?.map(normalizeRelayUrl); const writableConns = Array.from(this.connections.values()).filter( (conn) => (!scope || scope.includes(conn.url)) && conn.write ); const subject = new ReplaySubject<OkPacket>(writableConns.length); let subscription: Subscription | null = null; getSignedEvent(params, seckey).then((event) => { if (!subject.closed) { subscription = this.createAllMessageObservable().subscribe( ({ from, message }) => { if (message[0] !== "OK") { return; } subject.next({ from, id: event.id, ok: message[2], }); } ); } for (const conn of writableConns) { conn.sendEVENT(["EVENT", event]); } }); return subject.pipe( take(writableConns.length), timeout(30 * 1000), finalize(() => { subject.complete(); subject.unsubscribe(); subscription?.unsubscribe(); }) ); } dispose(): void { this.disposed = true; this.messageIn$.complete(); this.error$.complete(); for (const conn of this.connections.values()) { conn.dispose(); } } private ensureReq( req: LazyREQ, options?: { scope?: string[] | null; overwrite?: boolean } ) { const scope = options?.scope; for (const url of this.connections.keys()) { const conn = this.connections.get(url); if (!conn || !conn.read || (scope && !scope.includes(url))) { continue; } conn.ensureReq(req, { overwrite: options?.overwrite }); } } private finalizeReq(params: { subId: string; url?: string }) { const { subId, url } = params; if (subId === undefined && url === undefined) { throw new Error(); } if (url) { const conn = this.connections.get(url); conn?.finalizeReq(subId); } else { for (const conn of this.connections.values()) { conn?.finalizeReq(subId); } } } } interface OngoingReq { req: LazyREQ; scope?: string[]; } function makeSubId(params: { rxReqId: string; index?: number }): string { return `${params.rxReqId}:${params.index ?? 0}`; }
src/rx-nostr.ts
penpenpng-rx-nostr-7c52614
[ { "filename": "src/packet.ts", "retrieved_chunk": "/**\n * Packets emitted when WebSocket connection state is changed.\n */\nexport interface ConnectionStatePacket {\n from: string;\n state: ConnectionState;\n}\n/**\n * WebSocket connection state.\n *", "score": 20.68644968230517 }, { "filename": "src/packet.ts", "retrieved_chunk": " from: string;\n subId: string;\n event: Nostr.Event;\n}\n/**\n * Packets from websocket that represents an error.\n */\nexport interface ErrorPacket {\n from: string;\n reason: unknown;", "score": 18.657249418460587 }, { "filename": "src/connection.ts", "retrieved_chunk": " }\n private setConnectionState(state: ConnectionState) {\n if (this.connectionState === \"terminated\") {\n return;\n }\n this.connectionState = state;\n this.connectionState$.next(state);\n }\n private async fetchServerLimitationsIfNeeded() {\n if (", "score": 13.895697432795142 }, { "filename": "src/connection.ts", "retrieved_chunk": " };\n const onmessage = ({ data }: MessageEvent) => {\n if (this.connectionState === \"terminated\") {\n return;\n }\n try {\n this.message$.next({ from: this.url, message: JSON.parse(data) });\n } catch (err) {\n this.error$.next(err);\n }", "score": 13.677047967360922 }, { "filename": "src/operator.ts", "retrieved_chunk": "/**\n * Almost RxJS's `timeout`, but won't throw.\n */\nexport function completeOnTimeout<T>(\n time: number\n): MonoTypeOperatorFunction<T> {\n return pipe(\n timeout(time),\n catchError((error: unknown) => {\n if (error instanceof TimeoutError) {", "score": 13.295760712898169 } ]
typescript
.getMessageObservable() .pipe( catchError((reason: unknown) => {
import Nostr from "nostr-typedef"; import { BehaviorSubject, Observable, type OperatorFunction } from "rxjs"; import { LazyFilter, ReqPacket } from "./packet.js"; import type { Override } from "./util.js"; /** * The RxReq interface that is provided for RxNostr (**not for users**). */ export interface RxReq<S extends RxReqStrategy = RxReqStrategy> { /** @internal User should not use this directly.The RxReq strategy. It is read-only and must not change. */ get strategy(): S; /** @internal User should not use this directly. Used to construct subId. */ get rxReqId(): string; /** @internal User should not use this directly. Get an Observable of ReqPacket. */ getReqObservable(): Observable<ReqPacket>; } /** * REQ strategy. * * See comments on `createRxForwardReq()`, `createRxBackwardReq()` and `createRxOneshotReq() */ export type RxReqStrategy = "forward" | "backward" | "oneshot"; /** * The RxReq interface that is provided for users (not for RxNostr). */ export interface RxReqController { /** Start new REQ or stop REQ on the RxNostr with witch the RxReq is associated. */ emit(filters: LazyFilter | LazyFilter[] | null): void; /** * Returns itself overriding only `getReqObservable()`. * It is useful for throttling and other control purposes. */ pipe(): RxReq; pipe(op1: OperatorFunction<ReqPacket, ReqPacket>): RxReq; pipe<A>( op1: OperatorFunction<ReqPacket, A>, op2: OperatorFunction<A, ReqPacket> ): RxReq; pipe<A, B>( op1: OperatorFunction<ReqPacket, A>, op2: OperatorFunction<A, B>, op3: OperatorFunction<B, ReqPacket> ): RxReq; pipe<A, B, C>( op1: OperatorFunction<ReqPacket, A>, op2: OperatorFunction<A, B>, op3: OperatorFunction<B, C>, op4: OperatorFunction<C, ReqPacket> ): RxReq; pipe<A, B, C, D>( op1: OperatorFunction<ReqPacket, A>, op2: OperatorFunction<A, B>, op3: OperatorFunction<B, C>, op4: OperatorFunction<C, D>, op5: OperatorFunction<D, ReqPacket> ): RxReq; } abstract class RxReqBase implements RxReq { protected filters$ = new BehaviorSubject<ReqPacket>(null); private _rxReqId: string; abstract get strategy(): RxReqStrategy; get rxReqId() { return this._rxReqId; } constructor(rxReqId?: string) { this._rxReqId = rxReqId ?? getRandomDigitsString(); } getReqObservable(): Observable<ReqPacket> { return this.filters$.asObservable(); } emit(filters: LazyFilter | LazyFilter[] | null) { const normalized = normalizeFilters(filters); if (normalized) { this.filters$.next(normalized); } else { this.filters$.next(null); } } pipe(): RxReq; pipe(op1: OperatorFunction<ReqPacket, ReqPacket>): RxReq; pipe<A>( op1: OperatorFunction<ReqPacket, A>, op2: OperatorFunction<A, ReqPacket> ): RxReq; pipe<A, B>( op1: OperatorFunction<ReqPacket, A>, op2: OperatorFunction<A, B>, op3: OperatorFunction<B, ReqPacket> ): RxReq; pipe<A, B, C>( op1: OperatorFunction<ReqPacket, A>, op2: OperatorFunction<A, B>, op3: OperatorFunction<B, C>, op4: OperatorFunction<C, ReqPacket> ): RxReq; pipe<A, B, C, D>( op1: OperatorFunction<ReqPacket, A>, op2: OperatorFunction<A, B>, op3: OperatorFunction<B, C>, op4: OperatorFunction<C, D>, op5: OperatorFunction<D, ReqPacket> ): RxReq; // eslint-disable-next-line @typescript-eslint/no-explicit-any pipe(...operators: OperatorFunction<any, any>[]): RxReq { const rxReqId = this.rxReqId; const strategy = this.strategy; return { ...this, get rxReqId() { return rxReqId; }, get strategy() { return strategy; }, getReqObservable: () => this.getReqObservable().pipe(...(operators as [])), }; } } /** * Create a RxReq instance based on the backward strategy. * It is useful if you want to retrieve past events that have already been published. * * In backward strategy: * - All REQs have different subIds. * - All REQ-subscriptions keep alive until timeout or getting EOSE. * - In most cases, you should specify `until` or `limit` for filters. * * For more information, see [document](https://penpenpng.github.io/rx-nostr/docs/req-strategy.html#backward-strategy). */ export function createRxBackwardReq( subIdBase?: string ): RxReq<"backward"> & RxReqController { return new RxBackwardReq(subIdBase); } class RxBackwardReq extends RxReqBase implements RxReqController { constructor(rxReqId?: string) { super(rxReqId); } override get strategy(): "backward" { return "backward"; } } /** * Create a RxReq instance based on the forward strategy. * It is useful if you want to listen future events. * * In forward strategy: * - All REQs have the same subId. * - When a new REQ is issued, the old REQ is overwritten and terminated immediately. * The latest REQ keeps alive until it is overwritten or explicitly terminated. * - In most cases, you should not specify `limit` for filters. * * For more information, see [document](https://penpenpng.github.io/rx-nostr/docs/req-strategy.html#forward-strategy). */ export function createRxForwardReq( subId?: string ): RxReq<"forward"> & RxReqController { return new RxForwardReq(subId); } class RxForwardReq extends RxReqBase implements RxReqController { constructor(rxReqId?: string) { super(rxReqId); } override get strategy(): "forward" { return "forward"; } } /** * Create a RxReq instance based on the oneshot strategy. * It is almost the same as backward strategy, however can publish only one REQ * and the Observable completes on EOSE. * * For more information, see [document](https://penpenpng.github.io/rx-nostr/docs/req-strategy.html#oneshot-strategy). */ export function createRxOneshotReq(req: { filters: LazyFilter | LazyFilter[]; subId?: string; }): RxReq<"oneshot"> { return new RxOneshotReq(req); } class RxOneshotReq extends RxReqBase { constructor(req: { filters: LazyFilter | LazyFilter[]; subId?: string }) { super(req?.subId); this.emit(req.filters); } override get strategy(): "oneshot" { return "oneshot"; } } export interface Mixin<R, T> { (): ThisType<R> & T; } /** NOTE: unstable feature */ export function mixin<R extends object, T extends object>( def: () => ThisType<R> & T ): Mixin<R, T> { return def; } /** NOTE: unstable feature */ export function extend<B extends R, R extends object, T extends object>( base: B, mixin: Mixin<R, T> ): Override<B, T> { return Object.assign(base, mixin()) as Override<B, T>; } function getRandomDigitsString() { return `${Math.floor(Math.random() * 1000000)}`; } function normalizeFilter(filter: LazyFilter): LazyFilter | null { const res: LazyFilter = {}; const isTagName = (s: string): s is Nostr.TagName => /^#[a-zA-Z]$/.test(s); for (const key of Object.keys(filter)) { if (key === "limit" && (filter[key] ?? -1) >= 0) { res[key] = filter[key]; continue; } if (key === "since" || key === "until") { const f = filter[key]; if (typeof f !== "number" || (f ?? -1) >= 0) { res[key] = f; continue; } } if ( (isTagName(key) || key === "ids" || key === "authors") && filter[key] !== undefined && (filter[key]?.length ?? -1) > 0 ) { res[key] = filter[key]; continue; } if ( key === "kinds" && filter[key] !== undefined && (filter[key]?.length ?? -1) > 0 ) { res[key] = filter[key]; continue; } if (key === "search" && filter[key] !== undefined) { res[key] = filter[key]; continue; } } const timeRangeIsValid = typeof res.since !== "number" ||
typeof res.until !== "number" || res.since <= res.until;
if (!timeRangeIsValid) { return null; } if (Object.keys(res).length <= 0) { return null; } return res; } function normalizeFilters( filters: LazyFilter | LazyFilter[] | null ): LazyFilter[] | null { if (!filters) { return null; } const normalized = (Array.isArray(filters) ? filters : [filters]).flatMap( (e) => normalizeFilter(e) ?? [] ); return normalized.length > 0 ? normalized : null; }
src/req.ts
penpenpng-rx-nostr-7c52614
[ { "filename": "src/helper.ts", "retrieved_chunk": " return filters.map(evalFilter);\n } else {\n return [evalFilter(filters)];\n }\n}\nfunction evalFilter(filter: LazyFilter): Nostr.Filter {\n return {\n ...filter,\n since: filter.since ? evalLazyNumber(filter.since) : undefined,\n until: filter.until ? evalLazyNumber(filter.until) : undefined,", "score": 36.90930928989603 }, { "filename": "src/nostr/filter.ts", "retrieved_chunk": " return false;\n }\n if (\n filter.until &&\n ((untilInclusive && !(event.created_at <= filter.until)) ||\n (!untilInclusive && !(event.created_at < filter.until)))\n ) {\n return false;\n }\n for (const [key, needleValues] of Object.entries(filter)) {", "score": 34.84736819106094 }, { "filename": "src/nostr/nip11.ts", "retrieved_chunk": " headers: { Accept: \"application/nostr+json\" },\n });\n return res.json();\n}", "score": 34.37581438140943 }, { "filename": "src/packet.ts", "retrieved_chunk": " */\nexport type LazyFilter = Omit<Nostr.Filter, \"since\" | \"until\"> & {\n since?: number | (() => number);\n until?: number | (() => number);\n};\nexport type LazyREQ = [\"REQ\", string, ...LazyFilter[]];\n/**\n * Packets from websocket that represents an EVENT.\n */\nexport interface EventPacket {", "score": 28.815281535821015 }, { "filename": "src/operator.ts", "retrieved_chunk": " options?: CreateUniqOptions<T>\n): [MonoTypeOperatorFunction<EventPacket>, Set<T>] {\n const cache = new Set<T>();\n return [\n filter((packet) => {\n const key = keyFn(packet);\n if (key === null) {\n return true;\n }\n if (cache.has(key)) {", "score": 26.600303781440488 } ]
typescript
typeof res.until !== "number" || res.since <= res.until;
import Nostr from "nostr-typedef"; import { catchError, EMPTY, filter, finalize, first, identity, map, merge, mergeAll, mergeMap, type MonoTypeOperatorFunction, Observable, of, type OperatorFunction, ReplaySubject, Subject, Subscription, take, takeUntil, tap, timeout, type Unsubscribable, } from "rxjs"; import { BackoffConfig, Connection } from "./connection.js"; import { getSignedEvent } from "./nostr/event.js"; import { fetchRelayInfo } from "./nostr/nip11.js"; import { completeOnTimeout } from "./operator.js"; import type { ConnectionState, ConnectionStatePacket, ErrorPacket, EventPacket, LazyFilter, LazyREQ, MessagePacket, OkPacket, } from "./packet.js"; import type { RxReq } from "./req.js"; import { defineDefaultOptions, normalizeRelayUrl } from "./util.js"; /** * The core object of rx-nostr, which holds a connection to relays * and manages subscriptions as directed by the RxReq object connected by `use()`. * Use `createRxNostr()` to get the object. */ export interface RxNostr { /** * Return a list of relays used by this object. * The relay URLs are normalised so may not match the URLs set. */ getRelays(): RelayConfig[]; /** * Set the list of relays. * If a REQ subscription already exists, the same REQ is issued for the newly added relay * and CLOSE is sent for the removed relay. */ switchRelays(config: AcceptableRelaysConfig): Promise<void>; /** Utility wrapper for `switchRelays()`. */ addRelay(relay: string | RelayConfig): Promise<void>; /** Utility wrapper for `switchRelays()`. */ removeRelay(url: string): Promise<void>; /** Return true if the given relay is set to rxNostr. */ hasRelay(url: string): boolean; /** Return true if the given relay allows to be written. */ canWriteRelay(url: string): boolean; /** Return true if the given relay allows to be read. */ canReadRelay(url: string): boolean; /** Fetch all relays' info based on [NIP-11](https://github.com/nostr-protocol/nips/blob/master/11.md) */ fetchAllRelaysInfo(): Promise<Record<string, Nostr.Nip11.RelayInfo | null>>; /** * Return a dictionary in which you can look up connection state. * * **NOTE**: Keys are **normalized** URL, so may be different from one you set. */ getAllRelayState(): Record<string, ConnectionState>; /** * Return connection state of the given relay. * Throw if unknown URL is given. */ getRelayState(url: string): ConnectionState; /** * Attempt to reconnect the WebSocket if its state is `error` or `rejected`. * If not, do nothing. */ reconnect(url: string): void; // TODO: document /** * Set or unset a pipe to be applied to all EventPackets. */ setGlobalEventPacketPipe( pipe: MonoTypeOperatorFunction<EventPacket> | null ): void; /** * Associate RxReq with RxNostr. * When the associated RxReq is manipulated, * the RxNostr issues a new REQ to all relays allowed to be read. * The method returns an Observable that issues EventPackets * when an EVENT is received that is subscribed by RxReq. * You can unsubscribe the Observable to CLOSE. */ use( rxReq: RxReq, options?: Partial<RxNostrUseOptions> ): Observable<EventPacket>; /** * Create an Observable that receives all events (EVENT) from all websocket connections. * * Nothing happens when this Observable is unsubscribed. * */ createAllEventObservable(): Observable<EventPacket>; /** * Create an Observable that receives all errors from all websocket connections. * Note that an Observable is terminated when it receives any error, * so this method is the only way to receive errors arising from multiplexed websocket connections * (It means that Observables returned by `use()` never throw error). * * Nothing happens when this Observable is unsubscribed. * */ createAllErrorObservable(): Observable<ErrorPacket>; /** * Create an Observable that receives all messages from all websocket connections. * * Nothing happens when this Observable is unsubscribed. * */ createAllMessageObservable(): Observable<MessagePacket>; /** * Create an Observable that receives changing of WebSocket connection state. * * Nothing happens when this Observable is unsubscribed. */ createConnectionStateObservable(): Observable<ConnectionStatePacket>; /** * Attempt to send events to all relays that are allowed to write. * The `seckey` option accepts both nsec format and hex format, * and if omitted NIP-07 will be automatically used. */ send( params: Nostr.EventParameters, options?: RxNostrSendOptions ): Observable<OkPacket>; /** * Release all resources held by the RxNostr object. * Any Observable resulting from this RxNostr will be in the completed state * and will never receive messages again. * RxReq used by this object is not affected; in other words, if the RxReq is used * by another RxNostr, its use is not prevented. */ dispose(): void; } /** Create a RxNostr object. This is the only way to create that. */ export function createRxNostr(options?: Partial<RxNostrOptions>): RxNostr { return new RxNostrImpl(options); } export interface RxNostrOptions { /** Auto reconnection strategy. */ retry: BackoffConfig; /** * The time in milliseconds to timeout when following the backward strategy. * The observable is terminated when the specified amount of time has elapsed * during which no new events are available. */ timeout: number; globalRelayConfig?: { disableAutoFetchNip11Limitations?: boolean; maxConcurrentReqsFallback?: number; }; } const makeRxNostrOptions = defineDefaultOptions<RxNostrOptions>({ retry: { strategy: "exponential", maxCount: 5, initialDelay: 1000, }, timeout: 10000, globalRelayConfig: undefined, }); export interface RxNostrUseOptions { scope?: string[]; } const makeRxNostrUseOptions = defineDefaultOptions<RxNostrUseOptions>({ scope: undefined, }); export interface RxNostrSendOptions { scope?: string[]; seckey?: string; } const makeRxNostrSendOptions = defineDefaultOptions<RxNostrSendOptions>({ scope: undefined, seckey: undefined, }); /** Config object specifying WebSocket behavior. */ export interface RelayConfig { /** WebSocket endpoint URL. */ url: string; /** If true, rxNostr can publish REQ and subscribe EVENTs. */ read: boolean; /** If true, rxNostr can send EVENTs. */ write: boolean; disableAutoFetchNip11Limitations?: boolean; } /** Parameter of `rxNostr.switchRelays()` */ export type AcceptableRelaysConfig = | (string | RelayConfig)[] | Nostr.Nip07.GetRelayResult; class RxNostrImpl implements RxNostr { private options: RxNostrOptions; private connections: Map<string, Connection> = new Map(); private ongoings: Map<string, OngoingReq> = new Map(); private messageIn$: Subject<MessagePacket> = new Subject(); private error$: Subject<ErrorPacket> = new Subject(); private status$: Subject<ConnectionStatePacket> = new Subject(); private globalEventPacketPipe: MonoTypeOperatorFunction<EventPacket> | null = null; private disposed = false; private get messageOut$() { return this.messageIn$.pipe( mergeMap((packet) => { const pipe = this.globalEventPacketPipe; if (!pipe) { return of(packet); } const message = packet.message; if (message[0] !== "EVENT") { return of(packet); } return of({ from: packet.from, subId: message[1], event: message[2], }).pipe( pipe, map( ({ from, subId, event }): MessagePacket => ({ from, message: ["EVENT", subId, event], }) ) ); }) ); } constructor(options?: Partial<RxNostrOptions>) { const opt = makeRxNostrOptions(options); this.options = { ...opt, }; } getRelays(): RelayConfig[] { return Array.from(this.connections.values()).map( ({ url, read, write }) => ({ url, read, write, }) ); } private createConnection({ url, read, write, disableAutoFetchNip11Limitations, }: RelayConfig): Connection { const connection = new Connection(url, { backoff: this.options.retry, read, write, disableAutoFetchNip11Limitations: disableAutoFetchNip11Limitations ?? this.options.globalRelayConfig?.disableAutoFetchNip11Limitations, maxConcurrentReqsFallback: this.options.globalRelayConfig?.maxConcurrentReqsFallback, }); connection.getConnectionStateObservable().subscribe((state) => { this.status$.next({ from: url, state, }); }); connection.getErrorObservable().subscribe((reason) => { this.error$.next({ from: url, reason }); }); connection .getMessageObservable() .pipe( catchError((reason: unknown) => { this.error$.next({ from: url, reason }); return EMPTY; }) ) .subscribe((v) => { this.messageIn$.next(v); }); return connection; } async switchRelays(config: AcceptableRelaysConfig): Promise<void> { const nextConns: Map<string, Connection> = new Map(); for (const { url, read, write } of normalizeRelaysConfig(config)) { // pop a connection if exists const prevConn = this.connections.get(url); this.connections.delete(url); if (prevConn) { prevConn.read = read; prevConn.write = write; nextConns.set(url, prevConn); } else { nextConns.set(url, this.createConnection({ url, read, write })); } } // connections that are no longer used for (const conn of this.connections.values()) { conn.dispose(); } const ensureConns: Promise<unknown>[] = []; for (const conn of nextConns.values()) { if (conn.read) { ensureConns.push(conn.start()); } else { conn.stop(); } } await Promise.all(ensureConns); this.connections = nextConns; // If disposed during switchRelay processing if (this.disposed) { for (const conn of this.connections.values()) { conn.dispose(); } return; } for (const { req, scope } of this.ongoings.values()) { this.ensureReq(req, { scope }); } // --- scoped untility pure functions --- function normalizeRelaysConfig( config: AcceptableRelaysConfig ): RelayConfig[] { if (Array.isArray(config)) { return config.map((urlOrConfig) => { const relay: RelayConfig = typeof urlOrConfig === "string" ? { url: urlOrConfig, read: true, write: true, } : urlOrConfig; relay.url = normalizeRelayUrl(relay.url); return relay; }); } else { return Object.entries(config).map(([url, flags]) => ({ url: normalizeRelayUrl(url), ...flags, })); } } } async addRelay(relay: string | RelayConfig): Promise<void> { await this.switchRelays([...this.getRelays(), relay]); } async removeRelay(url: string): Promise<void> { const u = normalizeRelayUrl(url); const currentRelays = this.getRelays(); const nextRelays = currentRelays.filter((relay) => relay.url !== u); if (currentRelays.length !== nextRelays.length) { await this.switchRelays(nextRelays); } } hasRelay(url: string): boolean { const u = normalizeRelayUrl(url); return this.getRelays().some((relay) => relay.url === u); } canWriteRelay(url: string): boolean { const u = normalizeRelayUrl(url); return this.getRelays().some((relay) => relay.url === u && relay.write); } canReadRelay(url: string): boolean { const u = normalizeRelayUrl(url); return this.getRelays().some((relay) => relay.url === u && relay.read); } async fetchAllRelaysInfo(): Promise< Record<string, Nostr.Nip11.RelayInfo | null> > { const entries = await Promise.all( Array.from(this.connections.keys()).map( async (url): Promise<[string, Nostr.Nip11.RelayInfo | null]> => [ url, await fetchRelayInfo(url).catch(() => null), ] ) ); return Object.fromEntries(entries); } getAllRelayState(): Record<string, ConnectionState> { return Object.fromEntries( Array.from(this.connections.values()).map((e) => [ e.url, this.getRelayState(e.url), ]) ); } getRelayState(url: string): ConnectionState { const conn = this.connections.get(normalizeRelayUrl(url)); if (!conn) { throw new Error("RelayConfig not found"); } // this.relays[url] may be set before this.relays[url].websocket is initialized
return conn?.getConnectionState() ?? "not-started";
} reconnect(url: string): void { if (this.canReadRelay(url)) { this.connections.get(normalizeRelayUrl(url))?.start(); } } setGlobalEventPacketPipe(pipe: MonoTypeOperatorFunction<EventPacket> | null) { this.globalEventPacketPipe = pipe; } use( rxReq: RxReq, options?: Partial<RxNostrUseOptions> ): Observable<EventPacket> { const { scope: _scope } = makeRxNostrUseOptions(options); const scope = _scope?.map(normalizeRelayUrl); const TIMEOUT = this.options.timeout; const strategy = rxReq.strategy; const rxReqId = rxReq.rxReqId; const message$ = this.messageOut$; const ongoings = this.ongoings; const getAllRelayState = this.getAllRelayState.bind(this); const createConnectionStateObservable = this.createConnectionStateObservable.bind(this); const ensureReq = this.ensureReq.bind(this); const finalizeReq = this.finalizeReq.bind(this); const subId$ = rxReq.getReqObservable().pipe( filter((filters): filters is LazyFilter[] => filters !== null), strategy === "oneshot" ? first() : identity, attachSubId(), strategy === "forward" ? manageActiveForwardReq() : identity, tap((req) => { ensureReq(req, { overwrite: strategy === "forward", scope }); }), map(([, subId]) => subId) ); if (strategy === "forward") { const subId = makeSubId({ rxReqId, }); const resource: Unsubscribable[] = []; const subject = new Subject<EventPacket>(); resource.push(subject); return subject.pipe( tap({ subscribe: () => { resource.push(subId$.subscribe()); resource.push( message$ .pipe(filterBySubId(subId), pickEvents()) .subscribe((v) => { subject.next(v); }) ); }, finalize: () => { for (const r of resource) { r.unsubscribe(); } finalizeReq({ subId }); }, }) ); } else { return subId$.pipe(map(createEoseManagedEventObservable), mergeAll()); } function attachSubId(): OperatorFunction<LazyFilter[], LazyREQ> { const makeId = (index?: number) => makeSubId({ rxReqId, index }); switch (strategy) { case "backward": return map((filters, index) => ["REQ", makeId(index), ...filters]); case "forward": case "oneshot": return map((filters) => ["REQ", makeId(), ...filters]); } } function manageActiveForwardReq(): MonoTypeOperatorFunction<LazyREQ> { const recordActiveReq = (req: LazyREQ) => { const subId = req[1]; ongoings.set(subId, { req, scope, }); }; const forgetActiveReq = (subId: string) => { ongoings.delete(subId); }; return tap({ next: (req: LazyREQ) => { recordActiveReq(req); }, finalize: () => { forgetActiveReq( makeSubId({ rxReqId, }) ); }, }); } function createEoseManagedEventObservable( subId: string ): Observable<EventPacket> { const eose$ = new Subject<void>(); const complete$ = new Subject<void>(); const eoseRelays = new Set<string>(); const manageCompletion = merge( eose$, createConnectionStateObservable() ).subscribe(() => { const status = getAllRelayState(); const shouldComplete = Object.entries(status).every( ([url, state]) => (scope && !scope.includes(url)) || state === "error" || state === "terminated" || (state === "ongoing" && eoseRelays.has(url)) ); if (shouldComplete) { complete$.next(); } }); return message$.pipe( takeUntil(complete$), completeOnTimeout(TIMEOUT), filterBySubId(subId), filter((e) => !eoseRelays.has(e.from)), tap((e) => { if (e.message[0] === "EOSE") { eoseRelays.add(e.from); finalizeReq({ subId, url: e.from }); eose$.next(); } }), pickEvents(), finalize(() => { finalizeReq({ subId }); complete$.unsubscribe(); eose$.unsubscribe(); manageCompletion.unsubscribe(); }) ); } function filterBySubId( subId: string ): MonoTypeOperatorFunction<MessagePacket> { return filter( (packet) => (packet.message[0] === "EVENT" || packet.message[0] === "EOSE") && packet.message[1] === subId ); } function pickEvents(): OperatorFunction<MessagePacket, EventPacket> { return mergeMap(({ from, message }) => message[0] === "EVENT" ? of({ from, subId: message[1], event: message[2] }) : EMPTY ); } } createAllEventObservable(): Observable<EventPacket> { return this.messageOut$.pipe( mergeMap(({ from, message }) => message[0] === "EVENT" ? of({ from, subId: message[1], event: message[2] }) : EMPTY ) ); } createAllErrorObservable(): Observable<ErrorPacket> { return this.error$.asObservable(); } createAllMessageObservable(): Observable<MessagePacket> { return this.messageOut$; } createConnectionStateObservable(): Observable<ConnectionStatePacket> { return this.status$.asObservable(); } send( params: Nostr.EventParameters, options?: RxNostrSendOptions ): Observable<OkPacket> { const { seckey, scope: _scope } = makeRxNostrSendOptions(options); const scope = _scope?.map(normalizeRelayUrl); const writableConns = Array.from(this.connections.values()).filter( (conn) => (!scope || scope.includes(conn.url)) && conn.write ); const subject = new ReplaySubject<OkPacket>(writableConns.length); let subscription: Subscription | null = null; getSignedEvent(params, seckey).then((event) => { if (!subject.closed) { subscription = this.createAllMessageObservable().subscribe( ({ from, message }) => { if (message[0] !== "OK") { return; } subject.next({ from, id: event.id, ok: message[2], }); } ); } for (const conn of writableConns) { conn.sendEVENT(["EVENT", event]); } }); return subject.pipe( take(writableConns.length), timeout(30 * 1000), finalize(() => { subject.complete(); subject.unsubscribe(); subscription?.unsubscribe(); }) ); } dispose(): void { this.disposed = true; this.messageIn$.complete(); this.error$.complete(); for (const conn of this.connections.values()) { conn.dispose(); } } private ensureReq( req: LazyREQ, options?: { scope?: string[] | null; overwrite?: boolean } ) { const scope = options?.scope; for (const url of this.connections.keys()) { const conn = this.connections.get(url); if (!conn || !conn.read || (scope && !scope.includes(url))) { continue; } conn.ensureReq(req, { overwrite: options?.overwrite }); } } private finalizeReq(params: { subId: string; url?: string }) { const { subId, url } = params; if (subId === undefined && url === undefined) { throw new Error(); } if (url) { const conn = this.connections.get(url); conn?.finalizeReq(subId); } else { for (const conn of this.connections.values()) { conn?.finalizeReq(subId); } } } } interface OngoingReq { req: LazyREQ; scope?: string[]; } function makeSubId(params: { rxReqId: string; index?: number }): string { return `${params.rxReqId}:${params.index ?? 0}`; }
src/rx-nostr.ts
penpenpng-rx-nostr-7c52614
[ { "filename": "src/connection.ts", "retrieved_chunk": " }\n get maxConcurrentReqs(): number | null {\n return (\n this.serverLimitations?.max_subscriptions ??\n this.config.maxConcurrentReqsFallback ??\n null\n );\n }\n constructor(public url: string, private config: ConnectionConfig) {\n this.connectionState$.next(\"not-started\");", "score": 34.690982158890804 }, { "filename": "src/util.ts", "retrieved_chunk": "export function normalizeRelayUrl(url: string) {\n return normalizeUrl(url, {\n normalizeProtocol: false,\n removeTrailingSlash: true,\n });\n}", "score": 29.787621772339 }, { "filename": "src/connection.ts", "retrieved_chunk": " }\n };\n if (this.connectionState === \"terminated\") {\n return Promise.resolve();\n }\n const websocket = new WebSocket(this.url);\n websocket.addEventListener(\"open\", onopen);\n websocket.addEventListener(\"message\", onmessage);\n websocket.addEventListener(\"error\", onerror);\n websocket.addEventListener(\"close\", onclose);", "score": 28.365011631076147 }, { "filename": "src/connection.ts", "retrieved_chunk": " this.config.disableAutoFetchNip11Limitations ||\n this.serverLimitations\n ) {\n return;\n }\n try {\n const info = await fetchRelayInfo(this.url);\n this.serverLimitations = info.limitation ?? null;\n } catch {\n // do nothing", "score": 22.60036048827829 }, { "filename": "src/connection.ts", "retrieved_chunk": " };\n const onmessage = ({ data }: MessageEvent) => {\n if (this.connectionState === \"terminated\") {\n return;\n }\n try {\n this.message$.next({ from: this.url, message: JSON.parse(data) });\n } catch (err) {\n this.error$.next(err);\n }", "score": 22.414560236549335 } ]
typescript
return conn?.getConnectionState() ?? "not-started";
import Nostr from "nostr-typedef"; import { catchError, EMPTY, filter, finalize, first, identity, map, merge, mergeAll, mergeMap, type MonoTypeOperatorFunction, Observable, of, type OperatorFunction, ReplaySubject, Subject, Subscription, take, takeUntil, tap, timeout, type Unsubscribable, } from "rxjs"; import { BackoffConfig, Connection } from "./connection.js"; import { getSignedEvent } from "./nostr/event.js"; import { fetchRelayInfo } from "./nostr/nip11.js"; import { completeOnTimeout } from "./operator.js"; import type { ConnectionState, ConnectionStatePacket, ErrorPacket, EventPacket, LazyFilter, LazyREQ, MessagePacket, OkPacket, } from "./packet.js"; import type { RxReq } from "./req.js"; import { defineDefaultOptions, normalizeRelayUrl } from "./util.js"; /** * The core object of rx-nostr, which holds a connection to relays * and manages subscriptions as directed by the RxReq object connected by `use()`. * Use `createRxNostr()` to get the object. */ export interface RxNostr { /** * Return a list of relays used by this object. * The relay URLs are normalised so may not match the URLs set. */ getRelays(): RelayConfig[]; /** * Set the list of relays. * If a REQ subscription already exists, the same REQ is issued for the newly added relay * and CLOSE is sent for the removed relay. */ switchRelays(config: AcceptableRelaysConfig): Promise<void>; /** Utility wrapper for `switchRelays()`. */ addRelay(relay: string | RelayConfig): Promise<void>; /** Utility wrapper for `switchRelays()`. */ removeRelay(url: string): Promise<void>; /** Return true if the given relay is set to rxNostr. */ hasRelay(url: string): boolean; /** Return true if the given relay allows to be written. */ canWriteRelay(url: string): boolean; /** Return true if the given relay allows to be read. */ canReadRelay(url: string): boolean; /** Fetch all relays' info based on [NIP-11](https://github.com/nostr-protocol/nips/blob/master/11.md) */ fetchAllRelaysInfo(): Promise<Record<string, Nostr.Nip11.RelayInfo | null>>; /** * Return a dictionary in which you can look up connection state. * * **NOTE**: Keys are **normalized** URL, so may be different from one you set. */ getAllRelayState(): Record<string, ConnectionState>; /** * Return connection state of the given relay. * Throw if unknown URL is given. */ getRelayState(url: string): ConnectionState; /** * Attempt to reconnect the WebSocket if its state is `error` or `rejected`. * If not, do nothing. */ reconnect(url: string): void; // TODO: document /** * Set or unset a pipe to be applied to all EventPackets. */ setGlobalEventPacketPipe( pipe: MonoTypeOperatorFunction<EventPacket> | null ): void; /** * Associate RxReq with RxNostr. * When the associated RxReq is manipulated, * the RxNostr issues a new REQ to all relays allowed to be read. * The method returns an Observable that issues EventPackets * when an EVENT is received that is subscribed by RxReq. * You can unsubscribe the Observable to CLOSE. */ use( rxReq: RxReq, options?: Partial<RxNostrUseOptions> ): Observable<EventPacket>; /** * Create an Observable that receives all events (EVENT) from all websocket connections. * * Nothing happens when this Observable is unsubscribed. * */ createAllEventObservable(): Observable<EventPacket>; /** * Create an Observable that receives all errors from all websocket connections. * Note that an Observable is terminated when it receives any error, * so this method is the only way to receive errors arising from multiplexed websocket connections * (It means that Observables returned by `use()` never throw error). * * Nothing happens when this Observable is unsubscribed. * */ createAllErrorObservable(): Observable<ErrorPacket>; /** * Create an Observable that receives all messages from all websocket connections. * * Nothing happens when this Observable is unsubscribed. * */ createAllMessageObservable(): Observable<MessagePacket>; /** * Create an Observable that receives changing of WebSocket connection state. * * Nothing happens when this Observable is unsubscribed. */ createConnectionStateObservable(): Observable<ConnectionStatePacket>; /** * Attempt to send events to all relays that are allowed to write. * The `seckey` option accepts both nsec format and hex format, * and if omitted NIP-07 will be automatically used. */ send( params: Nostr.EventParameters, options?: RxNostrSendOptions ): Observable<OkPacket>; /** * Release all resources held by the RxNostr object. * Any Observable resulting from this RxNostr will be in the completed state * and will never receive messages again. * RxReq used by this object is not affected; in other words, if the RxReq is used * by another RxNostr, its use is not prevented. */ dispose(): void; } /** Create a RxNostr object. This is the only way to create that. */ export function createRxNostr(options?: Partial<RxNostrOptions>): RxNostr { return new RxNostrImpl(options); } export interface RxNostrOptions { /** Auto reconnection strategy. */ retry: BackoffConfig; /** * The time in milliseconds to timeout when following the backward strategy. * The observable is terminated when the specified amount of time has elapsed * during which no new events are available. */ timeout: number; globalRelayConfig?: { disableAutoFetchNip11Limitations?: boolean; maxConcurrentReqsFallback?: number; }; } const makeRxNostrOptions = defineDefaultOptions<RxNostrOptions>({ retry: { strategy: "exponential", maxCount: 5, initialDelay: 1000, }, timeout: 10000, globalRelayConfig: undefined, }); export interface RxNostrUseOptions { scope?: string[]; } const makeRxNostrUseOptions = defineDefaultOptions<RxNostrUseOptions>({ scope: undefined, }); export interface RxNostrSendOptions { scope?: string[]; seckey?: string; } const makeRxNostrSendOptions = defineDefaultOptions<RxNostrSendOptions>({ scope: undefined, seckey: undefined, }); /** Config object specifying WebSocket behavior. */ export interface RelayConfig { /** WebSocket endpoint URL. */ url: string; /** If true, rxNostr can publish REQ and subscribe EVENTs. */ read: boolean; /** If true, rxNostr can send EVENTs. */ write: boolean; disableAutoFetchNip11Limitations?: boolean; } /** Parameter of `rxNostr.switchRelays()` */ export type AcceptableRelaysConfig = | (string | RelayConfig)[] | Nostr.Nip07.GetRelayResult; class RxNostrImpl implements RxNostr { private options: RxNostrOptions; private connections: Map<string, Connection> = new Map(); private ongoings: Map<string, OngoingReq> = new Map(); private messageIn$: Subject<MessagePacket> = new Subject(); private error$: Subject<ErrorPacket> = new Subject(); private status$: Subject<ConnectionStatePacket> = new Subject(); private globalEventPacketPipe: MonoTypeOperatorFunction<EventPacket> | null = null; private disposed = false; private get messageOut$() { return this.messageIn$.pipe( mergeMap((packet) => { const pipe = this.globalEventPacketPipe; if (!pipe) { return of(packet); } const message = packet.message; if (message[0] !== "EVENT") { return of(packet); } return of({ from: packet.from, subId: message[1], event: message[2], }).pipe( pipe, map( ({ from, subId, event }): MessagePacket => ({ from, message: ["EVENT", subId, event], }) ) ); }) ); } constructor(options?: Partial<RxNostrOptions>) { const opt = makeRxNostrOptions(options); this.options = { ...opt, }; } getRelays(): RelayConfig[] { return Array.from(this.connections.values()).map( ({ url, read, write }) => ({ url, read, write, }) ); } private createConnection({ url, read, write, disableAutoFetchNip11Limitations, }: RelayConfig): Connection { const connection = new Connection(url, { backoff: this.options.retry, read, write, disableAutoFetchNip11Limitations: disableAutoFetchNip11Limitations ?? this.options.globalRelayConfig?.disableAutoFetchNip11Limitations, maxConcurrentReqsFallback: this.options.globalRelayConfig?.maxConcurrentReqsFallback, }); connection.getConnectionStateObservable().subscribe((state) => { this.status$.next({ from: url, state, }); }); connection.getErrorObservable().subscribe((reason) => { this.error$.next({ from: url, reason }); }); connection .getMessageObservable() .pipe( catchError((reason: unknown) => { this.error$.next({ from: url, reason }); return EMPTY; }) ) .subscribe((v) => { this.messageIn$.next(v); }); return connection; } async switchRelays(config: AcceptableRelaysConfig): Promise<void> { const nextConns: Map<string, Connection> = new Map(); for (const { url, read, write } of normalizeRelaysConfig(config)) { // pop a connection if exists const prevConn = this.connections.get(url); this.connections.delete(url); if (prevConn) { prevConn.read = read; prevConn.write = write; nextConns.set(url, prevConn); } else { nextConns.set(url, this.createConnection({ url, read, write })); } } // connections that are no longer used for (const conn of this.connections.values()) { conn.dispose(); } const ensureConns: Promise<unknown>[] = []; for (const conn of nextConns.values()) { if (conn.read) { ensureConns.push
(conn.start());
} else { conn.stop(); } } await Promise.all(ensureConns); this.connections = nextConns; // If disposed during switchRelay processing if (this.disposed) { for (const conn of this.connections.values()) { conn.dispose(); } return; } for (const { req, scope } of this.ongoings.values()) { this.ensureReq(req, { scope }); } // --- scoped untility pure functions --- function normalizeRelaysConfig( config: AcceptableRelaysConfig ): RelayConfig[] { if (Array.isArray(config)) { return config.map((urlOrConfig) => { const relay: RelayConfig = typeof urlOrConfig === "string" ? { url: urlOrConfig, read: true, write: true, } : urlOrConfig; relay.url = normalizeRelayUrl(relay.url); return relay; }); } else { return Object.entries(config).map(([url, flags]) => ({ url: normalizeRelayUrl(url), ...flags, })); } } } async addRelay(relay: string | RelayConfig): Promise<void> { await this.switchRelays([...this.getRelays(), relay]); } async removeRelay(url: string): Promise<void> { const u = normalizeRelayUrl(url); const currentRelays = this.getRelays(); const nextRelays = currentRelays.filter((relay) => relay.url !== u); if (currentRelays.length !== nextRelays.length) { await this.switchRelays(nextRelays); } } hasRelay(url: string): boolean { const u = normalizeRelayUrl(url); return this.getRelays().some((relay) => relay.url === u); } canWriteRelay(url: string): boolean { const u = normalizeRelayUrl(url); return this.getRelays().some((relay) => relay.url === u && relay.write); } canReadRelay(url: string): boolean { const u = normalizeRelayUrl(url); return this.getRelays().some((relay) => relay.url === u && relay.read); } async fetchAllRelaysInfo(): Promise< Record<string, Nostr.Nip11.RelayInfo | null> > { const entries = await Promise.all( Array.from(this.connections.keys()).map( async (url): Promise<[string, Nostr.Nip11.RelayInfo | null]> => [ url, await fetchRelayInfo(url).catch(() => null), ] ) ); return Object.fromEntries(entries); } getAllRelayState(): Record<string, ConnectionState> { return Object.fromEntries( Array.from(this.connections.values()).map((e) => [ e.url, this.getRelayState(e.url), ]) ); } getRelayState(url: string): ConnectionState { const conn = this.connections.get(normalizeRelayUrl(url)); if (!conn) { throw new Error("RelayConfig not found"); } // this.relays[url] may be set before this.relays[url].websocket is initialized return conn?.getConnectionState() ?? "not-started"; } reconnect(url: string): void { if (this.canReadRelay(url)) { this.connections.get(normalizeRelayUrl(url))?.start(); } } setGlobalEventPacketPipe(pipe: MonoTypeOperatorFunction<EventPacket> | null) { this.globalEventPacketPipe = pipe; } use( rxReq: RxReq, options?: Partial<RxNostrUseOptions> ): Observable<EventPacket> { const { scope: _scope } = makeRxNostrUseOptions(options); const scope = _scope?.map(normalizeRelayUrl); const TIMEOUT = this.options.timeout; const strategy = rxReq.strategy; const rxReqId = rxReq.rxReqId; const message$ = this.messageOut$; const ongoings = this.ongoings; const getAllRelayState = this.getAllRelayState.bind(this); const createConnectionStateObservable = this.createConnectionStateObservable.bind(this); const ensureReq = this.ensureReq.bind(this); const finalizeReq = this.finalizeReq.bind(this); const subId$ = rxReq.getReqObservable().pipe( filter((filters): filters is LazyFilter[] => filters !== null), strategy === "oneshot" ? first() : identity, attachSubId(), strategy === "forward" ? manageActiveForwardReq() : identity, tap((req) => { ensureReq(req, { overwrite: strategy === "forward", scope }); }), map(([, subId]) => subId) ); if (strategy === "forward") { const subId = makeSubId({ rxReqId, }); const resource: Unsubscribable[] = []; const subject = new Subject<EventPacket>(); resource.push(subject); return subject.pipe( tap({ subscribe: () => { resource.push(subId$.subscribe()); resource.push( message$ .pipe(filterBySubId(subId), pickEvents()) .subscribe((v) => { subject.next(v); }) ); }, finalize: () => { for (const r of resource) { r.unsubscribe(); } finalizeReq({ subId }); }, }) ); } else { return subId$.pipe(map(createEoseManagedEventObservable), mergeAll()); } function attachSubId(): OperatorFunction<LazyFilter[], LazyREQ> { const makeId = (index?: number) => makeSubId({ rxReqId, index }); switch (strategy) { case "backward": return map((filters, index) => ["REQ", makeId(index), ...filters]); case "forward": case "oneshot": return map((filters) => ["REQ", makeId(), ...filters]); } } function manageActiveForwardReq(): MonoTypeOperatorFunction<LazyREQ> { const recordActiveReq = (req: LazyREQ) => { const subId = req[1]; ongoings.set(subId, { req, scope, }); }; const forgetActiveReq = (subId: string) => { ongoings.delete(subId); }; return tap({ next: (req: LazyREQ) => { recordActiveReq(req); }, finalize: () => { forgetActiveReq( makeSubId({ rxReqId, }) ); }, }); } function createEoseManagedEventObservable( subId: string ): Observable<EventPacket> { const eose$ = new Subject<void>(); const complete$ = new Subject<void>(); const eoseRelays = new Set<string>(); const manageCompletion = merge( eose$, createConnectionStateObservable() ).subscribe(() => { const status = getAllRelayState(); const shouldComplete = Object.entries(status).every( ([url, state]) => (scope && !scope.includes(url)) || state === "error" || state === "terminated" || (state === "ongoing" && eoseRelays.has(url)) ); if (shouldComplete) { complete$.next(); } }); return message$.pipe( takeUntil(complete$), completeOnTimeout(TIMEOUT), filterBySubId(subId), filter((e) => !eoseRelays.has(e.from)), tap((e) => { if (e.message[0] === "EOSE") { eoseRelays.add(e.from); finalizeReq({ subId, url: e.from }); eose$.next(); } }), pickEvents(), finalize(() => { finalizeReq({ subId }); complete$.unsubscribe(); eose$.unsubscribe(); manageCompletion.unsubscribe(); }) ); } function filterBySubId( subId: string ): MonoTypeOperatorFunction<MessagePacket> { return filter( (packet) => (packet.message[0] === "EVENT" || packet.message[0] === "EOSE") && packet.message[1] === subId ); } function pickEvents(): OperatorFunction<MessagePacket, EventPacket> { return mergeMap(({ from, message }) => message[0] === "EVENT" ? of({ from, subId: message[1], event: message[2] }) : EMPTY ); } } createAllEventObservable(): Observable<EventPacket> { return this.messageOut$.pipe( mergeMap(({ from, message }) => message[0] === "EVENT" ? of({ from, subId: message[1], event: message[2] }) : EMPTY ) ); } createAllErrorObservable(): Observable<ErrorPacket> { return this.error$.asObservable(); } createAllMessageObservable(): Observable<MessagePacket> { return this.messageOut$; } createConnectionStateObservable(): Observable<ConnectionStatePacket> { return this.status$.asObservable(); } send( params: Nostr.EventParameters, options?: RxNostrSendOptions ): Observable<OkPacket> { const { seckey, scope: _scope } = makeRxNostrSendOptions(options); const scope = _scope?.map(normalizeRelayUrl); const writableConns = Array.from(this.connections.values()).filter( (conn) => (!scope || scope.includes(conn.url)) && conn.write ); const subject = new ReplaySubject<OkPacket>(writableConns.length); let subscription: Subscription | null = null; getSignedEvent(params, seckey).then((event) => { if (!subject.closed) { subscription = this.createAllMessageObservable().subscribe( ({ from, message }) => { if (message[0] !== "OK") { return; } subject.next({ from, id: event.id, ok: message[2], }); } ); } for (const conn of writableConns) { conn.sendEVENT(["EVENT", event]); } }); return subject.pipe( take(writableConns.length), timeout(30 * 1000), finalize(() => { subject.complete(); subject.unsubscribe(); subscription?.unsubscribe(); }) ); } dispose(): void { this.disposed = true; this.messageIn$.complete(); this.error$.complete(); for (const conn of this.connections.values()) { conn.dispose(); } } private ensureReq( req: LazyREQ, options?: { scope?: string[] | null; overwrite?: boolean } ) { const scope = options?.scope; for (const url of this.connections.keys()) { const conn = this.connections.get(url); if (!conn || !conn.read || (scope && !scope.includes(url))) { continue; } conn.ensureReq(req, { overwrite: options?.overwrite }); } } private finalizeReq(params: { subId: string; url?: string }) { const { subId, url } = params; if (subId === undefined && url === undefined) { throw new Error(); } if (url) { const conn = this.connections.get(url); conn?.finalizeReq(subId); } else { for (const conn of this.connections.values()) { conn?.finalizeReq(subId); } } } } interface OngoingReq { req: LazyREQ; scope?: string[]; } function makeSubId(params: { rxReqId: string; index?: number }): string { return `${params.rxReqId}:${params.index ?? 0}`; }
src/rx-nostr.ts
penpenpng-rx-nostr-7c52614
[ { "filename": "src/connection.ts", "retrieved_chunk": " }\n private ensureReqs() {\n const reqs = Array.from(this.reqs.values()).slice(\n 0,\n this.maxConcurrentReqs ?? undefined\n );\n for (const req of reqs) {\n this.ensureReq(req.original);\n }\n }", "score": 21.692622560157535 }, { "filename": "src/connection.ts", "retrieved_chunk": " }\n websocket.removeEventListener(\"open\", onopen);\n websocket.removeEventListener(\"message\", onmessage);\n websocket.removeEventListener(\"error\", onerror);\n websocket.removeEventListener(\"close\", onclose);\n websocket.close();\n this.socket = null;\n for (const req of this.reqs.values()) {\n req.isOngoing = false;\n }", "score": 18.72751256947269 }, { "filename": "src/operator.ts", "retrieved_chunk": " return EMPTY;\n } else {\n throw error;\n }\n })\n );\n}\n/**\n * Buffer the received values for a specified time\n * and return the values in sorted order as possible.", "score": 17.209973932530946 }, { "filename": "src/connection.ts", "retrieved_chunk": " }\n }\n async start() {\n if (\n !this.canRetry &&\n (this.connectionState === \"reconnecting\" ||\n this.connectionState === \"starting\" ||\n this.connectionState === \"ongoing\")\n ) {\n return Promise.resolve();", "score": 14.227609153952917 }, { "filename": "src/packet.ts", "retrieved_chunk": " * - `not-started`: Not started yet, or closed by expected ways.\n * - `starting`: Attempting to connect (for reasons other than error recovery).\n * - `ongoing`: Active, but may be temporarily closed as idling.\n * - `reconnecting`: Trying to reconnect for error recovery.\n * - `error`: Inactive because of an unexpected error. You can try to recover by reconnect()\n * - `rejected`: Inactive because of closing code 4000. You can try to reconnect, but should not do.\n * - `terminated`: No longer available because of dispose()\n */\nexport type ConnectionState =\n | \"not-started\"", "score": 13.783509130079297 } ]
typescript
(conn.start());
import Nostr from "nostr-typedef"; import { catchError, EMPTY, filter, finalize, first, identity, map, merge, mergeAll, mergeMap, type MonoTypeOperatorFunction, Observable, of, type OperatorFunction, ReplaySubject, Subject, Subscription, take, takeUntil, tap, timeout, type Unsubscribable, } from "rxjs"; import { BackoffConfig, Connection } from "./connection.js"; import { getSignedEvent } from "./nostr/event.js"; import { fetchRelayInfo } from "./nostr/nip11.js"; import { completeOnTimeout } from "./operator.js"; import type { ConnectionState, ConnectionStatePacket, ErrorPacket, EventPacket, LazyFilter, LazyREQ, MessagePacket, OkPacket, } from "./packet.js"; import type { RxReq } from "./req.js"; import { defineDefaultOptions, normalizeRelayUrl } from "./util.js"; /** * The core object of rx-nostr, which holds a connection to relays * and manages subscriptions as directed by the RxReq object connected by `use()`. * Use `createRxNostr()` to get the object. */ export interface RxNostr { /** * Return a list of relays used by this object. * The relay URLs are normalised so may not match the URLs set. */ getRelays(): RelayConfig[]; /** * Set the list of relays. * If a REQ subscription already exists, the same REQ is issued for the newly added relay * and CLOSE is sent for the removed relay. */ switchRelays(config: AcceptableRelaysConfig): Promise<void>; /** Utility wrapper for `switchRelays()`. */ addRelay(relay: string | RelayConfig): Promise<void>; /** Utility wrapper for `switchRelays()`. */ removeRelay(url: string): Promise<void>; /** Return true if the given relay is set to rxNostr. */ hasRelay(url: string): boolean; /** Return true if the given relay allows to be written. */ canWriteRelay(url: string): boolean; /** Return true if the given relay allows to be read. */ canReadRelay(url: string): boolean; /** Fetch all relays' info based on [NIP-11](https://github.com/nostr-protocol/nips/blob/master/11.md) */ fetchAllRelaysInfo(): Promise<Record<string, Nostr.Nip11.RelayInfo | null>>; /** * Return a dictionary in which you can look up connection state. * * **NOTE**: Keys are **normalized** URL, so may be different from one you set. */ getAllRelayState(): Record<string, ConnectionState>; /** * Return connection state of the given relay. * Throw if unknown URL is given. */ getRelayState(url: string): ConnectionState; /** * Attempt to reconnect the WebSocket if its state is `error` or `rejected`. * If not, do nothing. */ reconnect(url: string): void; // TODO: document /** * Set or unset a pipe to be applied to all EventPackets. */ setGlobalEventPacketPipe( pipe: MonoTypeOperatorFunction<EventPacket> | null ): void; /** * Associate RxReq with RxNostr. * When the associated RxReq is manipulated, * the RxNostr issues a new REQ to all relays allowed to be read. * The method returns an Observable that issues EventPackets * when an EVENT is received that is subscribed by RxReq. * You can unsubscribe the Observable to CLOSE. */ use( rxReq: RxReq, options?: Partial<RxNostrUseOptions> ): Observable<EventPacket>; /** * Create an Observable that receives all events (EVENT) from all websocket connections. * * Nothing happens when this Observable is unsubscribed. * */ createAllEventObservable(): Observable<EventPacket>; /** * Create an Observable that receives all errors from all websocket connections. * Note that an Observable is terminated when it receives any error, * so this method is the only way to receive errors arising from multiplexed websocket connections * (It means that Observables returned by `use()` never throw error). * * Nothing happens when this Observable is unsubscribed. * */ createAllErrorObservable(): Observable<ErrorPacket>; /** * Create an Observable that receives all messages from all websocket connections. * * Nothing happens when this Observable is unsubscribed. * */ createAllMessageObservable(): Observable<MessagePacket>; /** * Create an Observable that receives changing of WebSocket connection state. * * Nothing happens when this Observable is unsubscribed. */ createConnectionStateObservable(): Observable<ConnectionStatePacket>; /** * Attempt to send events to all relays that are allowed to write. * The `seckey` option accepts both nsec format and hex format, * and if omitted NIP-07 will be automatically used. */ send( params: Nostr.EventParameters, options?: RxNostrSendOptions ): Observable<OkPacket>; /** * Release all resources held by the RxNostr object. * Any Observable resulting from this RxNostr will be in the completed state * and will never receive messages again. * RxReq used by this object is not affected; in other words, if the RxReq is used * by another RxNostr, its use is not prevented. */ dispose(): void; } /** Create a RxNostr object. This is the only way to create that. */ export function createRxNostr(options?: Partial<RxNostrOptions>): RxNostr { return new RxNostrImpl(options); } export interface RxNostrOptions { /** Auto reconnection strategy. */ retry: BackoffConfig; /** * The time in milliseconds to timeout when following the backward strategy. * The observable is terminated when the specified amount of time has elapsed * during which no new events are available. */ timeout: number; globalRelayConfig?: { disableAutoFetchNip11Limitations?: boolean; maxConcurrentReqsFallback?: number; }; } const makeRxNostrOptions = defineDefaultOptions<RxNostrOptions>({ retry: { strategy: "exponential", maxCount: 5, initialDelay: 1000, }, timeout: 10000, globalRelayConfig: undefined, }); export interface RxNostrUseOptions { scope?: string[]; } const makeRxNostrUseOptions = defineDefaultOptions<RxNostrUseOptions>({ scope: undefined, }); export interface RxNostrSendOptions { scope?: string[]; seckey?: string; } const makeRxNostrSendOptions = defineDefaultOptions<RxNostrSendOptions>({ scope: undefined, seckey: undefined, }); /** Config object specifying WebSocket behavior. */ export interface RelayConfig { /** WebSocket endpoint URL. */ url: string; /** If true, rxNostr can publish REQ and subscribe EVENTs. */ read: boolean; /** If true, rxNostr can send EVENTs. */ write: boolean; disableAutoFetchNip11Limitations?: boolean; } /** Parameter of `rxNostr.switchRelays()` */ export type AcceptableRelaysConfig = | (string | RelayConfig)[] | Nostr.Nip07.GetRelayResult; class RxNostrImpl implements RxNostr { private options: RxNostrOptions; private connections: Map<string, Connection> = new Map(); private ongoings: Map<string, OngoingReq> = new Map(); private messageIn$: Subject<MessagePacket> = new Subject(); private error$: Subject<ErrorPacket> = new Subject(); private status$: Subject<ConnectionStatePacket> = new Subject(); private globalEventPacketPipe: MonoTypeOperatorFunction<EventPacket> | null = null; private disposed = false; private get messageOut$() { return this.messageIn$.pipe( mergeMap((packet) => { const pipe = this.globalEventPacketPipe; if (!pipe) { return of(packet); } const message = packet.message; if (message[0] !== "EVENT") { return of(packet); } return of({ from: packet.from, subId: message[1], event: message[2], }).pipe( pipe, map( ({ from, subId, event }): MessagePacket => ({ from, message: ["EVENT", subId, event], }) ) ); }) ); } constructor(options?: Partial<RxNostrOptions>) { const opt = makeRxNostrOptions(options); this.options = { ...opt, }; } getRelays(): RelayConfig[] { return Array.from(this.connections.values()).map( ({ url, read, write }) => ({ url, read, write, }) ); } private createConnection({ url, read, write, disableAutoFetchNip11Limitations, }: RelayConfig): Connection {
const connection = new Connection(url, {
backoff: this.options.retry, read, write, disableAutoFetchNip11Limitations: disableAutoFetchNip11Limitations ?? this.options.globalRelayConfig?.disableAutoFetchNip11Limitations, maxConcurrentReqsFallback: this.options.globalRelayConfig?.maxConcurrentReqsFallback, }); connection.getConnectionStateObservable().subscribe((state) => { this.status$.next({ from: url, state, }); }); connection.getErrorObservable().subscribe((reason) => { this.error$.next({ from: url, reason }); }); connection .getMessageObservable() .pipe( catchError((reason: unknown) => { this.error$.next({ from: url, reason }); return EMPTY; }) ) .subscribe((v) => { this.messageIn$.next(v); }); return connection; } async switchRelays(config: AcceptableRelaysConfig): Promise<void> { const nextConns: Map<string, Connection> = new Map(); for (const { url, read, write } of normalizeRelaysConfig(config)) { // pop a connection if exists const prevConn = this.connections.get(url); this.connections.delete(url); if (prevConn) { prevConn.read = read; prevConn.write = write; nextConns.set(url, prevConn); } else { nextConns.set(url, this.createConnection({ url, read, write })); } } // connections that are no longer used for (const conn of this.connections.values()) { conn.dispose(); } const ensureConns: Promise<unknown>[] = []; for (const conn of nextConns.values()) { if (conn.read) { ensureConns.push(conn.start()); } else { conn.stop(); } } await Promise.all(ensureConns); this.connections = nextConns; // If disposed during switchRelay processing if (this.disposed) { for (const conn of this.connections.values()) { conn.dispose(); } return; } for (const { req, scope } of this.ongoings.values()) { this.ensureReq(req, { scope }); } // --- scoped untility pure functions --- function normalizeRelaysConfig( config: AcceptableRelaysConfig ): RelayConfig[] { if (Array.isArray(config)) { return config.map((urlOrConfig) => { const relay: RelayConfig = typeof urlOrConfig === "string" ? { url: urlOrConfig, read: true, write: true, } : urlOrConfig; relay.url = normalizeRelayUrl(relay.url); return relay; }); } else { return Object.entries(config).map(([url, flags]) => ({ url: normalizeRelayUrl(url), ...flags, })); } } } async addRelay(relay: string | RelayConfig): Promise<void> { await this.switchRelays([...this.getRelays(), relay]); } async removeRelay(url: string): Promise<void> { const u = normalizeRelayUrl(url); const currentRelays = this.getRelays(); const nextRelays = currentRelays.filter((relay) => relay.url !== u); if (currentRelays.length !== nextRelays.length) { await this.switchRelays(nextRelays); } } hasRelay(url: string): boolean { const u = normalizeRelayUrl(url); return this.getRelays().some((relay) => relay.url === u); } canWriteRelay(url: string): boolean { const u = normalizeRelayUrl(url); return this.getRelays().some((relay) => relay.url === u && relay.write); } canReadRelay(url: string): boolean { const u = normalizeRelayUrl(url); return this.getRelays().some((relay) => relay.url === u && relay.read); } async fetchAllRelaysInfo(): Promise< Record<string, Nostr.Nip11.RelayInfo | null> > { const entries = await Promise.all( Array.from(this.connections.keys()).map( async (url): Promise<[string, Nostr.Nip11.RelayInfo | null]> => [ url, await fetchRelayInfo(url).catch(() => null), ] ) ); return Object.fromEntries(entries); } getAllRelayState(): Record<string, ConnectionState> { return Object.fromEntries( Array.from(this.connections.values()).map((e) => [ e.url, this.getRelayState(e.url), ]) ); } getRelayState(url: string): ConnectionState { const conn = this.connections.get(normalizeRelayUrl(url)); if (!conn) { throw new Error("RelayConfig not found"); } // this.relays[url] may be set before this.relays[url].websocket is initialized return conn?.getConnectionState() ?? "not-started"; } reconnect(url: string): void { if (this.canReadRelay(url)) { this.connections.get(normalizeRelayUrl(url))?.start(); } } setGlobalEventPacketPipe(pipe: MonoTypeOperatorFunction<EventPacket> | null) { this.globalEventPacketPipe = pipe; } use( rxReq: RxReq, options?: Partial<RxNostrUseOptions> ): Observable<EventPacket> { const { scope: _scope } = makeRxNostrUseOptions(options); const scope = _scope?.map(normalizeRelayUrl); const TIMEOUT = this.options.timeout; const strategy = rxReq.strategy; const rxReqId = rxReq.rxReqId; const message$ = this.messageOut$; const ongoings = this.ongoings; const getAllRelayState = this.getAllRelayState.bind(this); const createConnectionStateObservable = this.createConnectionStateObservable.bind(this); const ensureReq = this.ensureReq.bind(this); const finalizeReq = this.finalizeReq.bind(this); const subId$ = rxReq.getReqObservable().pipe( filter((filters): filters is LazyFilter[] => filters !== null), strategy === "oneshot" ? first() : identity, attachSubId(), strategy === "forward" ? manageActiveForwardReq() : identity, tap((req) => { ensureReq(req, { overwrite: strategy === "forward", scope }); }), map(([, subId]) => subId) ); if (strategy === "forward") { const subId = makeSubId({ rxReqId, }); const resource: Unsubscribable[] = []; const subject = new Subject<EventPacket>(); resource.push(subject); return subject.pipe( tap({ subscribe: () => { resource.push(subId$.subscribe()); resource.push( message$ .pipe(filterBySubId(subId), pickEvents()) .subscribe((v) => { subject.next(v); }) ); }, finalize: () => { for (const r of resource) { r.unsubscribe(); } finalizeReq({ subId }); }, }) ); } else { return subId$.pipe(map(createEoseManagedEventObservable), mergeAll()); } function attachSubId(): OperatorFunction<LazyFilter[], LazyREQ> { const makeId = (index?: number) => makeSubId({ rxReqId, index }); switch (strategy) { case "backward": return map((filters, index) => ["REQ", makeId(index), ...filters]); case "forward": case "oneshot": return map((filters) => ["REQ", makeId(), ...filters]); } } function manageActiveForwardReq(): MonoTypeOperatorFunction<LazyREQ> { const recordActiveReq = (req: LazyREQ) => { const subId = req[1]; ongoings.set(subId, { req, scope, }); }; const forgetActiveReq = (subId: string) => { ongoings.delete(subId); }; return tap({ next: (req: LazyREQ) => { recordActiveReq(req); }, finalize: () => { forgetActiveReq( makeSubId({ rxReqId, }) ); }, }); } function createEoseManagedEventObservable( subId: string ): Observable<EventPacket> { const eose$ = new Subject<void>(); const complete$ = new Subject<void>(); const eoseRelays = new Set<string>(); const manageCompletion = merge( eose$, createConnectionStateObservable() ).subscribe(() => { const status = getAllRelayState(); const shouldComplete = Object.entries(status).every( ([url, state]) => (scope && !scope.includes(url)) || state === "error" || state === "terminated" || (state === "ongoing" && eoseRelays.has(url)) ); if (shouldComplete) { complete$.next(); } }); return message$.pipe( takeUntil(complete$), completeOnTimeout(TIMEOUT), filterBySubId(subId), filter((e) => !eoseRelays.has(e.from)), tap((e) => { if (e.message[0] === "EOSE") { eoseRelays.add(e.from); finalizeReq({ subId, url: e.from }); eose$.next(); } }), pickEvents(), finalize(() => { finalizeReq({ subId }); complete$.unsubscribe(); eose$.unsubscribe(); manageCompletion.unsubscribe(); }) ); } function filterBySubId( subId: string ): MonoTypeOperatorFunction<MessagePacket> { return filter( (packet) => (packet.message[0] === "EVENT" || packet.message[0] === "EOSE") && packet.message[1] === subId ); } function pickEvents(): OperatorFunction<MessagePacket, EventPacket> { return mergeMap(({ from, message }) => message[0] === "EVENT" ? of({ from, subId: message[1], event: message[2] }) : EMPTY ); } } createAllEventObservable(): Observable<EventPacket> { return this.messageOut$.pipe( mergeMap(({ from, message }) => message[0] === "EVENT" ? of({ from, subId: message[1], event: message[2] }) : EMPTY ) ); } createAllErrorObservable(): Observable<ErrorPacket> { return this.error$.asObservable(); } createAllMessageObservable(): Observable<MessagePacket> { return this.messageOut$; } createConnectionStateObservable(): Observable<ConnectionStatePacket> { return this.status$.asObservable(); } send( params: Nostr.EventParameters, options?: RxNostrSendOptions ): Observable<OkPacket> { const { seckey, scope: _scope } = makeRxNostrSendOptions(options); const scope = _scope?.map(normalizeRelayUrl); const writableConns = Array.from(this.connections.values()).filter( (conn) => (!scope || scope.includes(conn.url)) && conn.write ); const subject = new ReplaySubject<OkPacket>(writableConns.length); let subscription: Subscription | null = null; getSignedEvent(params, seckey).then((event) => { if (!subject.closed) { subscription = this.createAllMessageObservable().subscribe( ({ from, message }) => { if (message[0] !== "OK") { return; } subject.next({ from, id: event.id, ok: message[2], }); } ); } for (const conn of writableConns) { conn.sendEVENT(["EVENT", event]); } }); return subject.pipe( take(writableConns.length), timeout(30 * 1000), finalize(() => { subject.complete(); subject.unsubscribe(); subscription?.unsubscribe(); }) ); } dispose(): void { this.disposed = true; this.messageIn$.complete(); this.error$.complete(); for (const conn of this.connections.values()) { conn.dispose(); } } private ensureReq( req: LazyREQ, options?: { scope?: string[] | null; overwrite?: boolean } ) { const scope = options?.scope; for (const url of this.connections.keys()) { const conn = this.connections.get(url); if (!conn || !conn.read || (scope && !scope.includes(url))) { continue; } conn.ensureReq(req, { overwrite: options?.overwrite }); } } private finalizeReq(params: { subId: string; url?: string }) { const { subId, url } = params; if (subId === undefined && url === undefined) { throw new Error(); } if (url) { const conn = this.connections.get(url); conn?.finalizeReq(subId); } else { for (const conn of this.connections.values()) { conn?.finalizeReq(subId); } } } } interface OngoingReq { req: LazyREQ; scope?: string[]; } function makeSubId(params: { rxReqId: string; index?: number }): string { return `${params.rxReqId}:${params.index ?? 0}`; }
src/rx-nostr.ts
penpenpng-rx-nostr-7c52614
[ { "filename": "src/__test__/sending.test.ts", "retrieved_chunk": " { url: RELAY_URL2, write: false, read: true },\n ]);\n await relay1.connected;\n await relay2.connected;\n });\n afterEach(() => {\n rxNostr.dispose();\n relay1.close({\n code: WebSocketCloseCode.DISPOSED_BY_RX_NOSTR,\n reason: \"Clean up on afterEach()\",", "score": 13.915492741783748 }, { "filename": "src/__test__/sending.test.ts", "retrieved_chunk": " let relay1: MockRelay;\n let relay2: MockRelay;\n let relay3: MockRelay;\n beforeEach(async () => {\n relay1 = createMockRelay(RELAY_URL1);\n relay2 = createMockRelay(RELAY_URL2);\n relay3 = createMockRelay(RELAY_URL3);\n rxNostr = createRxNostr();\n await rxNostr.switchRelays([\n { url: RELAY_URL1, write: true, read: false },", "score": 12.729793951951283 }, { "filename": "src/connection.ts", "retrieved_chunk": " return this.config.read;\n }\n set read(v) {\n this.config.read = v;\n }\n get write() {\n return this.config.write;\n }\n set write(v) {\n this.config.write = v;", "score": 12.713567765138576 }, { "filename": "src/connection.ts", "retrieved_chunk": " read: boolean;\n write: boolean;\n disableAutoFetchNip11Limitations?: boolean;\n maxConcurrentReqsFallback?: number;\n}\nexport type BackoffConfig =\n | {\n // Exponential backoff and jitter strategy\n strategy: \"exponential\";\n maxCount: number;", "score": 12.608067815854746 }, { "filename": "src/connection.ts", "retrieved_chunk": " private socket: WebSocket | null = null;\n private message$ = new Subject<MessagePacket | WebSocketError>();\n private error$ = new Subject<unknown>();\n private connectionState$ = new Subject<ConnectionState>();\n private connectionState: ConnectionState = \"not-started\";\n private queuedEvents: Nostr.ToRelayMessage.EVENT[] = [];\n private reqs: Map<string /* subId */, ReqState> = new Map();\n private serverLimitations: Nostr.Nip11.ServerLimitations | null = null;\n private canRetry = false;\n get read() {", "score": 12.588818979742104 } ]
typescript
const connection = new Connection(url, {
import Nostr from "nostr-typedef"; import { catchError, EMPTY, filter, finalize, first, identity, map, merge, mergeAll, mergeMap, type MonoTypeOperatorFunction, Observable, of, type OperatorFunction, ReplaySubject, Subject, Subscription, take, takeUntil, tap, timeout, type Unsubscribable, } from "rxjs"; import { BackoffConfig, Connection } from "./connection.js"; import { getSignedEvent } from "./nostr/event.js"; import { fetchRelayInfo } from "./nostr/nip11.js"; import { completeOnTimeout } from "./operator.js"; import type { ConnectionState, ConnectionStatePacket, ErrorPacket, EventPacket, LazyFilter, LazyREQ, MessagePacket, OkPacket, } from "./packet.js"; import type { RxReq } from "./req.js"; import { defineDefaultOptions, normalizeRelayUrl } from "./util.js"; /** * The core object of rx-nostr, which holds a connection to relays * and manages subscriptions as directed by the RxReq object connected by `use()`. * Use `createRxNostr()` to get the object. */ export interface RxNostr { /** * Return a list of relays used by this object. * The relay URLs are normalised so may not match the URLs set. */ getRelays(): RelayConfig[]; /** * Set the list of relays. * If a REQ subscription already exists, the same REQ is issued for the newly added relay * and CLOSE is sent for the removed relay. */ switchRelays(config: AcceptableRelaysConfig): Promise<void>; /** Utility wrapper for `switchRelays()`. */ addRelay(relay: string | RelayConfig): Promise<void>; /** Utility wrapper for `switchRelays()`. */ removeRelay(url: string): Promise<void>; /** Return true if the given relay is set to rxNostr. */ hasRelay(url: string): boolean; /** Return true if the given relay allows to be written. */ canWriteRelay(url: string): boolean; /** Return true if the given relay allows to be read. */ canReadRelay(url: string): boolean; /** Fetch all relays' info based on [NIP-11](https://github.com/nostr-protocol/nips/blob/master/11.md) */ fetchAllRelaysInfo(): Promise<Record<string, Nostr.Nip11.RelayInfo | null>>; /** * Return a dictionary in which you can look up connection state. * * **NOTE**: Keys are **normalized** URL, so may be different from one you set. */ getAllRelayState(): Record<string, ConnectionState>; /** * Return connection state of the given relay. * Throw if unknown URL is given. */ getRelayState(url: string): ConnectionState; /** * Attempt to reconnect the WebSocket if its state is `error` or `rejected`. * If not, do nothing. */ reconnect(url: string): void; // TODO: document /** * Set or unset a pipe to be applied to all EventPackets. */ setGlobalEventPacketPipe( pipe: MonoTypeOperatorFunction<EventPacket> | null ): void; /** * Associate RxReq with RxNostr. * When the associated RxReq is manipulated, * the RxNostr issues a new REQ to all relays allowed to be read. * The method returns an Observable that issues EventPackets * when an EVENT is received that is subscribed by RxReq. * You can unsubscribe the Observable to CLOSE. */ use( rxReq: RxReq, options?: Partial<RxNostrUseOptions> ): Observable<EventPacket>; /** * Create an Observable that receives all events (EVENT) from all websocket connections. * * Nothing happens when this Observable is unsubscribed. * */ createAllEventObservable(): Observable<EventPacket>; /** * Create an Observable that receives all errors from all websocket connections. * Note that an Observable is terminated when it receives any error, * so this method is the only way to receive errors arising from multiplexed websocket connections * (It means that Observables returned by `use()` never throw error). * * Nothing happens when this Observable is unsubscribed. * */ createAllErrorObservable(): Observable<ErrorPacket>; /** * Create an Observable that receives all messages from all websocket connections. * * Nothing happens when this Observable is unsubscribed. * */ createAllMessageObservable(): Observable<MessagePacket>; /** * Create an Observable that receives changing of WebSocket connection state. * * Nothing happens when this Observable is unsubscribed. */ createConnectionStateObservable(): Observable<ConnectionStatePacket>; /** * Attempt to send events to all relays that are allowed to write. * The `seckey` option accepts both nsec format and hex format, * and if omitted NIP-07 will be automatically used. */ send( params: Nostr.EventParameters, options?: RxNostrSendOptions ): Observable<OkPacket>; /** * Release all resources held by the RxNostr object. * Any Observable resulting from this RxNostr will be in the completed state * and will never receive messages again. * RxReq used by this object is not affected; in other words, if the RxReq is used * by another RxNostr, its use is not prevented. */ dispose(): void; } /** Create a RxNostr object. This is the only way to create that. */ export function createRxNostr(options?: Partial<RxNostrOptions>): RxNostr { return new RxNostrImpl(options); } export interface RxNostrOptions { /** Auto reconnection strategy. */ retry: BackoffConfig; /** * The time in milliseconds to timeout when following the backward strategy. * The observable is terminated when the specified amount of time has elapsed * during which no new events are available. */ timeout: number; globalRelayConfig?: { disableAutoFetchNip11Limitations?: boolean; maxConcurrentReqsFallback?: number; }; } const makeRxNostrOptions = defineDefaultOptions<RxNostrOptions>({ retry: { strategy: "exponential", maxCount: 5, initialDelay: 1000, }, timeout: 10000, globalRelayConfig: undefined, }); export interface RxNostrUseOptions { scope?: string[]; } const makeRxNostrUseOptions = defineDefaultOptions<RxNostrUseOptions>({ scope: undefined, }); export interface RxNostrSendOptions { scope?: string[]; seckey?: string; } const makeRxNostrSendOptions = defineDefaultOptions<RxNostrSendOptions>({ scope: undefined, seckey: undefined, }); /** Config object specifying WebSocket behavior. */ export interface RelayConfig { /** WebSocket endpoint URL. */ url: string; /** If true, rxNostr can publish REQ and subscribe EVENTs. */ read: boolean; /** If true, rxNostr can send EVENTs. */ write: boolean; disableAutoFetchNip11Limitations?: boolean; } /** Parameter of `rxNostr.switchRelays()` */ export type AcceptableRelaysConfig = | (string | RelayConfig)[] | Nostr.Nip07.GetRelayResult; class RxNostrImpl implements RxNostr { private options: RxNostrOptions; private connections: Map<string, Connection> = new Map(); private ongoings: Map<string, OngoingReq> = new Map(); private messageIn$: Subject<MessagePacket> = new Subject(); private error$: Subject<ErrorPacket> = new Subject(); private status$: Subject<ConnectionStatePacket> = new Subject(); private globalEventPacketPipe: MonoTypeOperatorFunction<EventPacket> | null = null; private disposed = false; private get messageOut$() { return this.messageIn$.pipe( mergeMap((packet) => { const pipe = this.globalEventPacketPipe; if (!pipe) { return of(packet); } const message = packet.message; if (message[0] !== "EVENT") { return of(packet); } return of({ from: packet.from, subId: message[1], event: message[2], }).pipe( pipe, map( ({ from, subId, event }): MessagePacket => ({ from, message: ["EVENT", subId, event], }) ) ); }) ); } constructor(options?: Partial<RxNostrOptions>) { const opt = makeRxNostrOptions(options); this.options = { ...opt, }; } getRelays(): RelayConfig[] { return Array.from(this.connections.values()).map( ({ url, read, write }) => ({ url, read, write, }) ); } private createConnection({ url, read, write, disableAutoFetchNip11Limitations, }: RelayConfig): Connection { const connection = new Connection(url, { backoff: this.options.retry, read, write, disableAutoFetchNip11Limitations: disableAutoFetchNip11Limitations ?? this.options.globalRelayConfig?.disableAutoFetchNip11Limitations, maxConcurrentReqsFallback: this.options.globalRelayConfig?.maxConcurrentReqsFallback, }); connection.getConnectionStateObservable().subscribe((state) => { this.status$.next({ from: url, state, }); }); connection.getErrorObservable().subscribe((reason) => { this.error$.next({ from: url, reason }); }); connection .getMessageObservable() .pipe( catchError((reason: unknown) => { this.error$.next({ from: url, reason }); return EMPTY; }) ) .subscribe((v) => { this.messageIn$.next(v); }); return connection; } async switchRelays(config: AcceptableRelaysConfig): Promise<void> { const nextConns: Map<string, Connection> = new Map(); for (const { url, read, write } of normalizeRelaysConfig(config)) { // pop a connection if exists const prevConn = this.connections.get(url); this.connections.delete(url); if (prevConn) { prevConn.read = read; prevConn.write = write; nextConns.set(url, prevConn); } else { nextConns.set(url, this.createConnection({ url, read, write })); } } // connections that are no longer used for (const conn of this.connections.values()) { conn.dispose(); } const ensureConns: Promise<unknown>[] = []; for (const conn of nextConns.values()) { if (conn.read) { ensureConns.push(conn.start()); } else { conn.stop(); } } await Promise.all(ensureConns); this.connections = nextConns; // If disposed during switchRelay processing if (this.disposed) { for (const conn of this.connections.values()) { conn.dispose(); } return; } for (const { req, scope } of this.ongoings.values()) { this.ensureReq(req, { scope }); } // --- scoped untility pure functions --- function normalizeRelaysConfig( config: AcceptableRelaysConfig ): RelayConfig[] { if (Array.isArray(config)) { return config.map((urlOrConfig) => { const relay: RelayConfig = typeof urlOrConfig === "string" ? { url: urlOrConfig, read: true, write: true, } : urlOrConfig; relay.url = normalizeRelayUrl(relay.url); return relay; }); } else { return Object.entries(config).map(([url, flags]) => ({ url: normalizeRelayUrl(url), ...flags, })); } } } async addRelay(relay: string | RelayConfig): Promise<void> { await this.switchRelays([...this.getRelays(), relay]); } async removeRelay(url: string): Promise<void> { const u = normalizeRelayUrl(url); const currentRelays = this.getRelays(); const nextRelays = currentRelays.filter((relay) => relay.url !== u); if (currentRelays.length !== nextRelays.length) { await this.switchRelays(nextRelays); } } hasRelay(url: string): boolean { const u = normalizeRelayUrl(url); return this.getRelays().some((relay) => relay.url === u); } canWriteRelay(url: string): boolean { const u = normalizeRelayUrl(url); return this.getRelays().some((relay) => relay.url === u && relay.write); } canReadRelay(url: string): boolean { const u = normalizeRelayUrl(url); return this.getRelays().some((relay) => relay.url === u && relay.read); } async fetchAllRelaysInfo(): Promise< Record<string, Nostr.Nip11.RelayInfo | null> > { const entries = await Promise.all( Array.from(this.connections.keys()).map( async (url): Promise<[string, Nostr.Nip11.RelayInfo | null]> => [ url, await fetchRelayInfo(url).catch(() => null), ] ) ); return Object.fromEntries(entries); } getAllRelayState(): Record<string, ConnectionState> { return Object.fromEntries( Array.from(this.connections.values()).map((e) => [ e.url, this.getRelayState(e.url), ]) ); } getRelayState(url: string): ConnectionState { const conn = this.connections.get(normalizeRelayUrl(url)); if (!conn) { throw new Error("RelayConfig not found"); } // this.relays[url] may be set before this.relays[url].websocket is initialized return conn?.getConnectionState() ?? "not-started"; } reconnect(url: string): void { if (this.canReadRelay(url)) { this.connections.get(normalizeRelayUrl(url))?.start(); } } setGlobalEventPacketPipe(pipe: MonoTypeOperatorFunction<EventPacket> | null) { this.globalEventPacketPipe = pipe; } use( rxReq: RxReq, options?: Partial<RxNostrUseOptions> ): Observable<EventPacket> { const { scope: _scope } = makeRxNostrUseOptions(options); const scope = _scope?.map(normalizeRelayUrl); const TIMEOUT = this.options.timeout; const strategy = rxReq.strategy;
const rxReqId = rxReq.rxReqId;
const message$ = this.messageOut$; const ongoings = this.ongoings; const getAllRelayState = this.getAllRelayState.bind(this); const createConnectionStateObservable = this.createConnectionStateObservable.bind(this); const ensureReq = this.ensureReq.bind(this); const finalizeReq = this.finalizeReq.bind(this); const subId$ = rxReq.getReqObservable().pipe( filter((filters): filters is LazyFilter[] => filters !== null), strategy === "oneshot" ? first() : identity, attachSubId(), strategy === "forward" ? manageActiveForwardReq() : identity, tap((req) => { ensureReq(req, { overwrite: strategy === "forward", scope }); }), map(([, subId]) => subId) ); if (strategy === "forward") { const subId = makeSubId({ rxReqId, }); const resource: Unsubscribable[] = []; const subject = new Subject<EventPacket>(); resource.push(subject); return subject.pipe( tap({ subscribe: () => { resource.push(subId$.subscribe()); resource.push( message$ .pipe(filterBySubId(subId), pickEvents()) .subscribe((v) => { subject.next(v); }) ); }, finalize: () => { for (const r of resource) { r.unsubscribe(); } finalizeReq({ subId }); }, }) ); } else { return subId$.pipe(map(createEoseManagedEventObservable), mergeAll()); } function attachSubId(): OperatorFunction<LazyFilter[], LazyREQ> { const makeId = (index?: number) => makeSubId({ rxReqId, index }); switch (strategy) { case "backward": return map((filters, index) => ["REQ", makeId(index), ...filters]); case "forward": case "oneshot": return map((filters) => ["REQ", makeId(), ...filters]); } } function manageActiveForwardReq(): MonoTypeOperatorFunction<LazyREQ> { const recordActiveReq = (req: LazyREQ) => { const subId = req[1]; ongoings.set(subId, { req, scope, }); }; const forgetActiveReq = (subId: string) => { ongoings.delete(subId); }; return tap({ next: (req: LazyREQ) => { recordActiveReq(req); }, finalize: () => { forgetActiveReq( makeSubId({ rxReqId, }) ); }, }); } function createEoseManagedEventObservable( subId: string ): Observable<EventPacket> { const eose$ = new Subject<void>(); const complete$ = new Subject<void>(); const eoseRelays = new Set<string>(); const manageCompletion = merge( eose$, createConnectionStateObservable() ).subscribe(() => { const status = getAllRelayState(); const shouldComplete = Object.entries(status).every( ([url, state]) => (scope && !scope.includes(url)) || state === "error" || state === "terminated" || (state === "ongoing" && eoseRelays.has(url)) ); if (shouldComplete) { complete$.next(); } }); return message$.pipe( takeUntil(complete$), completeOnTimeout(TIMEOUT), filterBySubId(subId), filter((e) => !eoseRelays.has(e.from)), tap((e) => { if (e.message[0] === "EOSE") { eoseRelays.add(e.from); finalizeReq({ subId, url: e.from }); eose$.next(); } }), pickEvents(), finalize(() => { finalizeReq({ subId }); complete$.unsubscribe(); eose$.unsubscribe(); manageCompletion.unsubscribe(); }) ); } function filterBySubId( subId: string ): MonoTypeOperatorFunction<MessagePacket> { return filter( (packet) => (packet.message[0] === "EVENT" || packet.message[0] === "EOSE") && packet.message[1] === subId ); } function pickEvents(): OperatorFunction<MessagePacket, EventPacket> { return mergeMap(({ from, message }) => message[0] === "EVENT" ? of({ from, subId: message[1], event: message[2] }) : EMPTY ); } } createAllEventObservable(): Observable<EventPacket> { return this.messageOut$.pipe( mergeMap(({ from, message }) => message[0] === "EVENT" ? of({ from, subId: message[1], event: message[2] }) : EMPTY ) ); } createAllErrorObservable(): Observable<ErrorPacket> { return this.error$.asObservable(); } createAllMessageObservable(): Observable<MessagePacket> { return this.messageOut$; } createConnectionStateObservable(): Observable<ConnectionStatePacket> { return this.status$.asObservable(); } send( params: Nostr.EventParameters, options?: RxNostrSendOptions ): Observable<OkPacket> { const { seckey, scope: _scope } = makeRxNostrSendOptions(options); const scope = _scope?.map(normalizeRelayUrl); const writableConns = Array.from(this.connections.values()).filter( (conn) => (!scope || scope.includes(conn.url)) && conn.write ); const subject = new ReplaySubject<OkPacket>(writableConns.length); let subscription: Subscription | null = null; getSignedEvent(params, seckey).then((event) => { if (!subject.closed) { subscription = this.createAllMessageObservable().subscribe( ({ from, message }) => { if (message[0] !== "OK") { return; } subject.next({ from, id: event.id, ok: message[2], }); } ); } for (const conn of writableConns) { conn.sendEVENT(["EVENT", event]); } }); return subject.pipe( take(writableConns.length), timeout(30 * 1000), finalize(() => { subject.complete(); subject.unsubscribe(); subscription?.unsubscribe(); }) ); } dispose(): void { this.disposed = true; this.messageIn$.complete(); this.error$.complete(); for (const conn of this.connections.values()) { conn.dispose(); } } private ensureReq( req: LazyREQ, options?: { scope?: string[] | null; overwrite?: boolean } ) { const scope = options?.scope; for (const url of this.connections.keys()) { const conn = this.connections.get(url); if (!conn || !conn.read || (scope && !scope.includes(url))) { continue; } conn.ensureReq(req, { overwrite: options?.overwrite }); } } private finalizeReq(params: { subId: string; url?: string }) { const { subId, url } = params; if (subId === undefined && url === undefined) { throw new Error(); } if (url) { const conn = this.connections.get(url); conn?.finalizeReq(subId); } else { for (const conn of this.connections.values()) { conn?.finalizeReq(subId); } } } } interface OngoingReq { req: LazyREQ; scope?: string[]; } function makeSubId(params: { rxReqId: string; index?: number }): string { return `${params.rxReqId}:${params.index ?? 0}`; }
src/rx-nostr.ts
penpenpng-rx-nostr-7c52614
[ { "filename": "src/req.ts", "retrieved_chunk": " ): RxReq;\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n pipe(...operators: OperatorFunction<any, any>[]): RxReq {\n const rxReqId = this.rxReqId;\n const strategy = this.strategy;\n return {\n ...this,\n get rxReqId() {\n return rxReqId;\n },", "score": 27.420957467960548 }, { "filename": "src/nostr/filter.ts", "retrieved_chunk": " } else {\n return _isFiltered(event, filters, options);\n }\n}\nfunction _isFiltered(\n event: Nostr.Event,\n filter: Nostr.Filter,\n options?: Partial<MatchFilterOptions>\n): boolean {\n const { sinceInclusive, untilInclusive } = makeMatchFilterOptions(options);", "score": 23.204832780979725 }, { "filename": "src/operator.ts", "retrieved_chunk": "export function filterBy(\n filters: LazyFilter | LazyFilter[],\n options?: MatchFilterOptions & FilterByOptions\n): MonoTypeOperatorFunction<EventPacket> {\n const { not } = makeFilterByOptions(options);\n const evaledFilter = evalFilters(filters);\n return filter(({ event }) => {\n const match = isFiltered(event, evaledFilter, options);\n return not ? !match : match;\n });", "score": 20.83407527315525 }, { "filename": "src/req.ts", "retrieved_chunk": " constructor(rxReqId?: string) {\n super(rxReqId);\n }\n override get strategy(): \"forward\" {\n return \"forward\";\n }\n}\n/**\n * Create a RxReq instance based on the oneshot strategy.\n * It is almost the same as backward strategy, however can publish only one REQ", "score": 19.513523351514124 }, { "filename": "src/req.ts", "retrieved_chunk": " /** @internal User should not use this directly. Used to construct subId. */\n get rxReqId(): string;\n /** @internal User should not use this directly. Get an Observable of ReqPacket. */\n getReqObservable(): Observable<ReqPacket>;\n}\n/**\n * REQ strategy.\n *\n * See comments on `createRxForwardReq()`, `createRxBackwardReq()` and `createRxOneshotReq()\n */", "score": 19.319979654954032 } ]
typescript
const rxReqId = rxReq.rxReqId;
import Nostr from "nostr-typedef"; import { catchError, EMPTY, filter, finalize, first, identity, map, merge, mergeAll, mergeMap, type MonoTypeOperatorFunction, Observable, of, type OperatorFunction, ReplaySubject, Subject, Subscription, take, takeUntil, tap, timeout, type Unsubscribable, } from "rxjs"; import { BackoffConfig, Connection } from "./connection.js"; import { getSignedEvent } from "./nostr/event.js"; import { fetchRelayInfo } from "./nostr/nip11.js"; import { completeOnTimeout } from "./operator.js"; import type { ConnectionState, ConnectionStatePacket, ErrorPacket, EventPacket, LazyFilter, LazyREQ, MessagePacket, OkPacket, } from "./packet.js"; import type { RxReq } from "./req.js"; import { defineDefaultOptions, normalizeRelayUrl } from "./util.js"; /** * The core object of rx-nostr, which holds a connection to relays * and manages subscriptions as directed by the RxReq object connected by `use()`. * Use `createRxNostr()` to get the object. */ export interface RxNostr { /** * Return a list of relays used by this object. * The relay URLs are normalised so may not match the URLs set. */ getRelays(): RelayConfig[]; /** * Set the list of relays. * If a REQ subscription already exists, the same REQ is issued for the newly added relay * and CLOSE is sent for the removed relay. */ switchRelays(config: AcceptableRelaysConfig): Promise<void>; /** Utility wrapper for `switchRelays()`. */ addRelay(relay: string | RelayConfig): Promise<void>; /** Utility wrapper for `switchRelays()`. */ removeRelay(url: string): Promise<void>; /** Return true if the given relay is set to rxNostr. */ hasRelay(url: string): boolean; /** Return true if the given relay allows to be written. */ canWriteRelay(url: string): boolean; /** Return true if the given relay allows to be read. */ canReadRelay(url: string): boolean; /** Fetch all relays' info based on [NIP-11](https://github.com/nostr-protocol/nips/blob/master/11.md) */ fetchAllRelaysInfo(): Promise<Record<string, Nostr.Nip11.RelayInfo | null>>; /** * Return a dictionary in which you can look up connection state. * * **NOTE**: Keys are **normalized** URL, so may be different from one you set. */ getAllRelayState(): Record<string, ConnectionState>; /** * Return connection state of the given relay. * Throw if unknown URL is given. */ getRelayState(url: string): ConnectionState; /** * Attempt to reconnect the WebSocket if its state is `error` or `rejected`. * If not, do nothing. */ reconnect(url: string): void; // TODO: document /** * Set or unset a pipe to be applied to all EventPackets. */ setGlobalEventPacketPipe( pipe: MonoTypeOperatorFunction<EventPacket> | null ): void; /** * Associate RxReq with RxNostr. * When the associated RxReq is manipulated, * the RxNostr issues a new REQ to all relays allowed to be read. * The method returns an Observable that issues EventPackets * when an EVENT is received that is subscribed by RxReq. * You can unsubscribe the Observable to CLOSE. */ use( rxReq: RxReq, options?: Partial<RxNostrUseOptions> ): Observable<EventPacket>; /** * Create an Observable that receives all events (EVENT) from all websocket connections. * * Nothing happens when this Observable is unsubscribed. * */ createAllEventObservable(): Observable<EventPacket>; /** * Create an Observable that receives all errors from all websocket connections. * Note that an Observable is terminated when it receives any error, * so this method is the only way to receive errors arising from multiplexed websocket connections * (It means that Observables returned by `use()` never throw error). * * Nothing happens when this Observable is unsubscribed. * */ createAllErrorObservable(): Observable<ErrorPacket>; /** * Create an Observable that receives all messages from all websocket connections. * * Nothing happens when this Observable is unsubscribed. * */ createAllMessageObservable(): Observable<MessagePacket>; /** * Create an Observable that receives changing of WebSocket connection state. * * Nothing happens when this Observable is unsubscribed. */ createConnectionStateObservable(): Observable<ConnectionStatePacket>; /** * Attempt to send events to all relays that are allowed to write. * The `seckey` option accepts both nsec format and hex format, * and if omitted NIP-07 will be automatically used. */ send( params: Nostr.EventParameters, options?: RxNostrSendOptions ): Observable<OkPacket>; /** * Release all resources held by the RxNostr object. * Any Observable resulting from this RxNostr will be in the completed state * and will never receive messages again. * RxReq used by this object is not affected; in other words, if the RxReq is used * by another RxNostr, its use is not prevented. */ dispose(): void; } /** Create a RxNostr object. This is the only way to create that. */ export function createRxNostr(options?: Partial<RxNostrOptions>): RxNostr { return new RxNostrImpl(options); } export interface RxNostrOptions { /** Auto reconnection strategy. */ retry: BackoffConfig; /** * The time in milliseconds to timeout when following the backward strategy. * The observable is terminated when the specified amount of time has elapsed * during which no new events are available. */ timeout: number; globalRelayConfig?: { disableAutoFetchNip11Limitations?: boolean; maxConcurrentReqsFallback?: number; }; } const makeRxNostrOptions = defineDefaultOptions<RxNostrOptions>({ retry: { strategy: "exponential", maxCount: 5, initialDelay: 1000, }, timeout: 10000, globalRelayConfig: undefined, }); export interface RxNostrUseOptions { scope?: string[]; } const makeRxNostrUseOptions = defineDefaultOptions<RxNostrUseOptions>({ scope: undefined, }); export interface RxNostrSendOptions { scope?: string[]; seckey?: string; } const makeRxNostrSendOptions = defineDefaultOptions<RxNostrSendOptions>({ scope: undefined, seckey: undefined, }); /** Config object specifying WebSocket behavior. */ export interface RelayConfig { /** WebSocket endpoint URL. */ url: string; /** If true, rxNostr can publish REQ and subscribe EVENTs. */ read: boolean; /** If true, rxNostr can send EVENTs. */ write: boolean; disableAutoFetchNip11Limitations?: boolean; } /** Parameter of `rxNostr.switchRelays()` */ export type AcceptableRelaysConfig = | (string | RelayConfig)[] | Nostr.Nip07.GetRelayResult; class RxNostrImpl implements RxNostr { private options: RxNostrOptions; private connections: Map<string, Connection> = new Map(); private ongoings: Map<string, OngoingReq> = new Map(); private messageIn$: Subject<MessagePacket> = new Subject(); private error$: Subject<ErrorPacket> = new Subject(); private status$: Subject<ConnectionStatePacket> = new Subject(); private globalEventPacketPipe: MonoTypeOperatorFunction<EventPacket> | null = null; private disposed = false; private get messageOut$() { return this.messageIn$.pipe( mergeMap((packet) => { const pipe = this.globalEventPacketPipe; if (!pipe) { return of(packet); } const message = packet.message; if (message[0] !== "EVENT") { return of(packet); } return of({ from: packet.from, subId: message[1], event: message[2], }).pipe( pipe, map( ({ from, subId, event }): MessagePacket => ({ from, message: ["EVENT", subId, event], }) ) ); }) ); } constructor(options?: Partial<RxNostrOptions>) { const opt = makeRxNostrOptions(options); this.options = { ...opt, }; } getRelays(): RelayConfig[] { return Array.from(this.connections.values()).map( ({ url, read, write }) => ({ url, read, write, }) ); } private createConnection({ url, read, write, disableAutoFetchNip11Limitations, }: RelayConfig): Connection { const connection = new Connection(url, { backoff: this.options.retry, read, write, disableAutoFetchNip11Limitations: disableAutoFetchNip11Limitations ?? this.options.globalRelayConfig?.disableAutoFetchNip11Limitations, maxConcurrentReqsFallback: this.options.globalRelayConfig?.maxConcurrentReqsFallback, }); connection.getConnectionStateObservable().subscribe((state) => { this.status$.next({ from: url, state, }); });
connection.getErrorObservable().subscribe((reason) => {
this.error$.next({ from: url, reason }); }); connection .getMessageObservable() .pipe( catchError((reason: unknown) => { this.error$.next({ from: url, reason }); return EMPTY; }) ) .subscribe((v) => { this.messageIn$.next(v); }); return connection; } async switchRelays(config: AcceptableRelaysConfig): Promise<void> { const nextConns: Map<string, Connection> = new Map(); for (const { url, read, write } of normalizeRelaysConfig(config)) { // pop a connection if exists const prevConn = this.connections.get(url); this.connections.delete(url); if (prevConn) { prevConn.read = read; prevConn.write = write; nextConns.set(url, prevConn); } else { nextConns.set(url, this.createConnection({ url, read, write })); } } // connections that are no longer used for (const conn of this.connections.values()) { conn.dispose(); } const ensureConns: Promise<unknown>[] = []; for (const conn of nextConns.values()) { if (conn.read) { ensureConns.push(conn.start()); } else { conn.stop(); } } await Promise.all(ensureConns); this.connections = nextConns; // If disposed during switchRelay processing if (this.disposed) { for (const conn of this.connections.values()) { conn.dispose(); } return; } for (const { req, scope } of this.ongoings.values()) { this.ensureReq(req, { scope }); } // --- scoped untility pure functions --- function normalizeRelaysConfig( config: AcceptableRelaysConfig ): RelayConfig[] { if (Array.isArray(config)) { return config.map((urlOrConfig) => { const relay: RelayConfig = typeof urlOrConfig === "string" ? { url: urlOrConfig, read: true, write: true, } : urlOrConfig; relay.url = normalizeRelayUrl(relay.url); return relay; }); } else { return Object.entries(config).map(([url, flags]) => ({ url: normalizeRelayUrl(url), ...flags, })); } } } async addRelay(relay: string | RelayConfig): Promise<void> { await this.switchRelays([...this.getRelays(), relay]); } async removeRelay(url: string): Promise<void> { const u = normalizeRelayUrl(url); const currentRelays = this.getRelays(); const nextRelays = currentRelays.filter((relay) => relay.url !== u); if (currentRelays.length !== nextRelays.length) { await this.switchRelays(nextRelays); } } hasRelay(url: string): boolean { const u = normalizeRelayUrl(url); return this.getRelays().some((relay) => relay.url === u); } canWriteRelay(url: string): boolean { const u = normalizeRelayUrl(url); return this.getRelays().some((relay) => relay.url === u && relay.write); } canReadRelay(url: string): boolean { const u = normalizeRelayUrl(url); return this.getRelays().some((relay) => relay.url === u && relay.read); } async fetchAllRelaysInfo(): Promise< Record<string, Nostr.Nip11.RelayInfo | null> > { const entries = await Promise.all( Array.from(this.connections.keys()).map( async (url): Promise<[string, Nostr.Nip11.RelayInfo | null]> => [ url, await fetchRelayInfo(url).catch(() => null), ] ) ); return Object.fromEntries(entries); } getAllRelayState(): Record<string, ConnectionState> { return Object.fromEntries( Array.from(this.connections.values()).map((e) => [ e.url, this.getRelayState(e.url), ]) ); } getRelayState(url: string): ConnectionState { const conn = this.connections.get(normalizeRelayUrl(url)); if (!conn) { throw new Error("RelayConfig not found"); } // this.relays[url] may be set before this.relays[url].websocket is initialized return conn?.getConnectionState() ?? "not-started"; } reconnect(url: string): void { if (this.canReadRelay(url)) { this.connections.get(normalizeRelayUrl(url))?.start(); } } setGlobalEventPacketPipe(pipe: MonoTypeOperatorFunction<EventPacket> | null) { this.globalEventPacketPipe = pipe; } use( rxReq: RxReq, options?: Partial<RxNostrUseOptions> ): Observable<EventPacket> { const { scope: _scope } = makeRxNostrUseOptions(options); const scope = _scope?.map(normalizeRelayUrl); const TIMEOUT = this.options.timeout; const strategy = rxReq.strategy; const rxReqId = rxReq.rxReqId; const message$ = this.messageOut$; const ongoings = this.ongoings; const getAllRelayState = this.getAllRelayState.bind(this); const createConnectionStateObservable = this.createConnectionStateObservable.bind(this); const ensureReq = this.ensureReq.bind(this); const finalizeReq = this.finalizeReq.bind(this); const subId$ = rxReq.getReqObservable().pipe( filter((filters): filters is LazyFilter[] => filters !== null), strategy === "oneshot" ? first() : identity, attachSubId(), strategy === "forward" ? manageActiveForwardReq() : identity, tap((req) => { ensureReq(req, { overwrite: strategy === "forward", scope }); }), map(([, subId]) => subId) ); if (strategy === "forward") { const subId = makeSubId({ rxReqId, }); const resource: Unsubscribable[] = []; const subject = new Subject<EventPacket>(); resource.push(subject); return subject.pipe( tap({ subscribe: () => { resource.push(subId$.subscribe()); resource.push( message$ .pipe(filterBySubId(subId), pickEvents()) .subscribe((v) => { subject.next(v); }) ); }, finalize: () => { for (const r of resource) { r.unsubscribe(); } finalizeReq({ subId }); }, }) ); } else { return subId$.pipe(map(createEoseManagedEventObservable), mergeAll()); } function attachSubId(): OperatorFunction<LazyFilter[], LazyREQ> { const makeId = (index?: number) => makeSubId({ rxReqId, index }); switch (strategy) { case "backward": return map((filters, index) => ["REQ", makeId(index), ...filters]); case "forward": case "oneshot": return map((filters) => ["REQ", makeId(), ...filters]); } } function manageActiveForwardReq(): MonoTypeOperatorFunction<LazyREQ> { const recordActiveReq = (req: LazyREQ) => { const subId = req[1]; ongoings.set(subId, { req, scope, }); }; const forgetActiveReq = (subId: string) => { ongoings.delete(subId); }; return tap({ next: (req: LazyREQ) => { recordActiveReq(req); }, finalize: () => { forgetActiveReq( makeSubId({ rxReqId, }) ); }, }); } function createEoseManagedEventObservable( subId: string ): Observable<EventPacket> { const eose$ = new Subject<void>(); const complete$ = new Subject<void>(); const eoseRelays = new Set<string>(); const manageCompletion = merge( eose$, createConnectionStateObservable() ).subscribe(() => { const status = getAllRelayState(); const shouldComplete = Object.entries(status).every( ([url, state]) => (scope && !scope.includes(url)) || state === "error" || state === "terminated" || (state === "ongoing" && eoseRelays.has(url)) ); if (shouldComplete) { complete$.next(); } }); return message$.pipe( takeUntil(complete$), completeOnTimeout(TIMEOUT), filterBySubId(subId), filter((e) => !eoseRelays.has(e.from)), tap((e) => { if (e.message[0] === "EOSE") { eoseRelays.add(e.from); finalizeReq({ subId, url: e.from }); eose$.next(); } }), pickEvents(), finalize(() => { finalizeReq({ subId }); complete$.unsubscribe(); eose$.unsubscribe(); manageCompletion.unsubscribe(); }) ); } function filterBySubId( subId: string ): MonoTypeOperatorFunction<MessagePacket> { return filter( (packet) => (packet.message[0] === "EVENT" || packet.message[0] === "EOSE") && packet.message[1] === subId ); } function pickEvents(): OperatorFunction<MessagePacket, EventPacket> { return mergeMap(({ from, message }) => message[0] === "EVENT" ? of({ from, subId: message[1], event: message[2] }) : EMPTY ); } } createAllEventObservable(): Observable<EventPacket> { return this.messageOut$.pipe( mergeMap(({ from, message }) => message[0] === "EVENT" ? of({ from, subId: message[1], event: message[2] }) : EMPTY ) ); } createAllErrorObservable(): Observable<ErrorPacket> { return this.error$.asObservable(); } createAllMessageObservable(): Observable<MessagePacket> { return this.messageOut$; } createConnectionStateObservable(): Observable<ConnectionStatePacket> { return this.status$.asObservable(); } send( params: Nostr.EventParameters, options?: RxNostrSendOptions ): Observable<OkPacket> { const { seckey, scope: _scope } = makeRxNostrSendOptions(options); const scope = _scope?.map(normalizeRelayUrl); const writableConns = Array.from(this.connections.values()).filter( (conn) => (!scope || scope.includes(conn.url)) && conn.write ); const subject = new ReplaySubject<OkPacket>(writableConns.length); let subscription: Subscription | null = null; getSignedEvent(params, seckey).then((event) => { if (!subject.closed) { subscription = this.createAllMessageObservable().subscribe( ({ from, message }) => { if (message[0] !== "OK") { return; } subject.next({ from, id: event.id, ok: message[2], }); } ); } for (const conn of writableConns) { conn.sendEVENT(["EVENT", event]); } }); return subject.pipe( take(writableConns.length), timeout(30 * 1000), finalize(() => { subject.complete(); subject.unsubscribe(); subscription?.unsubscribe(); }) ); } dispose(): void { this.disposed = true; this.messageIn$.complete(); this.error$.complete(); for (const conn of this.connections.values()) { conn.dispose(); } } private ensureReq( req: LazyREQ, options?: { scope?: string[] | null; overwrite?: boolean } ) { const scope = options?.scope; for (const url of this.connections.keys()) { const conn = this.connections.get(url); if (!conn || !conn.read || (scope && !scope.includes(url))) { continue; } conn.ensureReq(req, { overwrite: options?.overwrite }); } } private finalizeReq(params: { subId: string; url?: string }) { const { subId, url } = params; if (subId === undefined && url === undefined) { throw new Error(); } if (url) { const conn = this.connections.get(url); conn?.finalizeReq(subId); } else { for (const conn of this.connections.values()) { conn?.finalizeReq(subId); } } } } interface OngoingReq { req: LazyREQ; scope?: string[]; } function makeSubId(params: { rxReqId: string; index?: number }): string { return `${params.rxReqId}:${params.index ?? 0}`; }
src/rx-nostr.ts
penpenpng-rx-nostr-7c52614
[ { "filename": "src/packet.ts", "retrieved_chunk": "/**\n * Packets emitted when WebSocket connection state is changed.\n */\nexport interface ConnectionStatePacket {\n from: string;\n state: ConnectionState;\n}\n/**\n * WebSocket connection state.\n *", "score": 28.92005812025353 }, { "filename": "src/connection.ts", "retrieved_chunk": " }\n private setConnectionState(state: ConnectionState) {\n if (this.connectionState === \"terminated\") {\n return;\n }\n this.connectionState = state;\n this.connectionState$.next(state);\n }\n private async fetchServerLimitationsIfNeeded() {\n if (", "score": 24.684896900905017 }, { "filename": "src/connection.ts", "retrieved_chunk": " }\n get maxConcurrentReqs(): number | null {\n return (\n this.serverLimitations?.max_subscriptions ??\n this.config.maxConcurrentReqsFallback ??\n null\n );\n }\n constructor(public url: string, private config: ConnectionConfig) {\n this.connectionState$.next(\"not-started\");", "score": 20.475928812596813 }, { "filename": "src/__test__/subscription.test.ts", "retrieved_chunk": " globalRelayConfig: {\n disableAutoFetchNip11Limitations: true,\n maxConcurrentReqsFallback: 1,\n },\n });\n await rxNostr.switchRelays([RELAY_URL]);\n await relay.connected;\n });\n afterEach(() => {\n rxNostr.dispose();", "score": 16.01348873699806 }, { "filename": "src/connection.ts", "retrieved_chunk": " }\n getErrorObservable() {\n return this.error$.asObservable();\n }\n ensureReq(req: LazyREQ, options?: { overwrite?: boolean }) {\n const subId = req[1];\n if (this.connectionState === \"terminated\") {\n return;\n }\n if (!this.read) {", "score": 14.651608735641812 } ]
typescript
connection.getErrorObservable().subscribe((reason) => {
import Nostr from "nostr-typedef"; import { catchError, EMPTY, filter, finalize, first, identity, map, merge, mergeAll, mergeMap, type MonoTypeOperatorFunction, Observable, of, type OperatorFunction, ReplaySubject, Subject, Subscription, take, takeUntil, tap, timeout, type Unsubscribable, } from "rxjs"; import { BackoffConfig, Connection } from "./connection.js"; import { getSignedEvent } from "./nostr/event.js"; import { fetchRelayInfo } from "./nostr/nip11.js"; import { completeOnTimeout } from "./operator.js"; import type { ConnectionState, ConnectionStatePacket, ErrorPacket, EventPacket, LazyFilter, LazyREQ, MessagePacket, OkPacket, } from "./packet.js"; import type { RxReq } from "./req.js"; import { defineDefaultOptions, normalizeRelayUrl } from "./util.js"; /** * The core object of rx-nostr, which holds a connection to relays * and manages subscriptions as directed by the RxReq object connected by `use()`. * Use `createRxNostr()` to get the object. */ export interface RxNostr { /** * Return a list of relays used by this object. * The relay URLs are normalised so may not match the URLs set. */ getRelays(): RelayConfig[]; /** * Set the list of relays. * If a REQ subscription already exists, the same REQ is issued for the newly added relay * and CLOSE is sent for the removed relay. */ switchRelays(config: AcceptableRelaysConfig): Promise<void>; /** Utility wrapper for `switchRelays()`. */ addRelay(relay: string | RelayConfig): Promise<void>; /** Utility wrapper for `switchRelays()`. */ removeRelay(url: string): Promise<void>; /** Return true if the given relay is set to rxNostr. */ hasRelay(url: string): boolean; /** Return true if the given relay allows to be written. */ canWriteRelay(url: string): boolean; /** Return true if the given relay allows to be read. */ canReadRelay(url: string): boolean; /** Fetch all relays' info based on [NIP-11](https://github.com/nostr-protocol/nips/blob/master/11.md) */ fetchAllRelaysInfo(): Promise<Record<string, Nostr.Nip11.RelayInfo | null>>; /** * Return a dictionary in which you can look up connection state. * * **NOTE**: Keys are **normalized** URL, so may be different from one you set. */ getAllRelayState(): Record<string, ConnectionState>; /** * Return connection state of the given relay. * Throw if unknown URL is given. */ getRelayState(url: string): ConnectionState; /** * Attempt to reconnect the WebSocket if its state is `error` or `rejected`. * If not, do nothing. */ reconnect(url: string): void; // TODO: document /** * Set or unset a pipe to be applied to all EventPackets. */ setGlobalEventPacketPipe( pipe: MonoTypeOperatorFunction<EventPacket> | null ): void; /** * Associate RxReq with RxNostr. * When the associated RxReq is manipulated, * the RxNostr issues a new REQ to all relays allowed to be read. * The method returns an Observable that issues EventPackets * when an EVENT is received that is subscribed by RxReq. * You can unsubscribe the Observable to CLOSE. */ use( rxReq: RxReq, options?: Partial<RxNostrUseOptions> ): Observable<EventPacket>; /** * Create an Observable that receives all events (EVENT) from all websocket connections. * * Nothing happens when this Observable is unsubscribed. * */ createAllEventObservable(): Observable<EventPacket>; /** * Create an Observable that receives all errors from all websocket connections. * Note that an Observable is terminated when it receives any error, * so this method is the only way to receive errors arising from multiplexed websocket connections * (It means that Observables returned by `use()` never throw error). * * Nothing happens when this Observable is unsubscribed. * */ createAllErrorObservable(): Observable<ErrorPacket>; /** * Create an Observable that receives all messages from all websocket connections. * * Nothing happens when this Observable is unsubscribed. * */ createAllMessageObservable(): Observable<MessagePacket>; /** * Create an Observable that receives changing of WebSocket connection state. * * Nothing happens when this Observable is unsubscribed. */ createConnectionStateObservable(): Observable<ConnectionStatePacket>; /** * Attempt to send events to all relays that are allowed to write. * The `seckey` option accepts both nsec format and hex format, * and if omitted NIP-07 will be automatically used. */ send( params: Nostr.EventParameters, options?: RxNostrSendOptions ): Observable<OkPacket>; /** * Release all resources held by the RxNostr object. * Any Observable resulting from this RxNostr will be in the completed state * and will never receive messages again. * RxReq used by this object is not affected; in other words, if the RxReq is used * by another RxNostr, its use is not prevented. */ dispose(): void; } /** Create a RxNostr object. This is the only way to create that. */ export function createRxNostr(options?: Partial<RxNostrOptions>): RxNostr { return new RxNostrImpl(options); } export interface RxNostrOptions { /** Auto reconnection strategy. */ retry: BackoffConfig; /** * The time in milliseconds to timeout when following the backward strategy. * The observable is terminated when the specified amount of time has elapsed * during which no new events are available. */ timeout: number; globalRelayConfig?: { disableAutoFetchNip11Limitations?: boolean; maxConcurrentReqsFallback?: number; }; } const makeRxNostrOptions = defineDefaultOptions<RxNostrOptions>({ retry: { strategy: "exponential", maxCount: 5, initialDelay: 1000, }, timeout: 10000, globalRelayConfig: undefined, }); export interface RxNostrUseOptions { scope?: string[]; } const makeRxNostrUseOptions = defineDefaultOptions<RxNostrUseOptions>({ scope: undefined, }); export interface RxNostrSendOptions { scope?: string[]; seckey?: string; } const makeRxNostrSendOptions = defineDefaultOptions<RxNostrSendOptions>({ scope: undefined, seckey: undefined, }); /** Config object specifying WebSocket behavior. */ export interface RelayConfig { /** WebSocket endpoint URL. */ url: string; /** If true, rxNostr can publish REQ and subscribe EVENTs. */ read: boolean; /** If true, rxNostr can send EVENTs. */ write: boolean; disableAutoFetchNip11Limitations?: boolean; } /** Parameter of `rxNostr.switchRelays()` */ export type AcceptableRelaysConfig = | (string | RelayConfig)[] | Nostr.Nip07.GetRelayResult; class RxNostrImpl implements RxNostr { private options: RxNostrOptions; private connections: Map<string, Connection> = new Map(); private ongoings: Map<string, OngoingReq> = new Map(); private messageIn$: Subject<MessagePacket> = new Subject(); private error$: Subject<ErrorPacket> = new Subject(); private status$: Subject<ConnectionStatePacket> = new Subject(); private globalEventPacketPipe: MonoTypeOperatorFunction<EventPacket> | null = null; private disposed = false; private get messageOut$() { return this.messageIn$.pipe( mergeMap((packet) => { const pipe = this.globalEventPacketPipe; if (!pipe) { return of(packet); } const message = packet.message; if (message[0] !== "EVENT") { return of(packet); } return of({ from: packet.from, subId: message[1], event: message[2], }).pipe( pipe, map( ({ from, subId, event }): MessagePacket => ({ from, message: ["EVENT", subId, event], }) ) ); }) ); } constructor(options?: Partial<RxNostrOptions>) { const opt = makeRxNostrOptions(options); this.options = { ...opt, }; } getRelays(): RelayConfig[] { return Array.from(this.connections.values()).map( ({ url, read, write }) => ({ url, read, write, }) ); } private createConnection({ url, read, write, disableAutoFetchNip11Limitations, }: RelayConfig): Connection { const connection = new Connection(url, { backoff: this.options.retry, read, write, disableAutoFetchNip11Limitations: disableAutoFetchNip11Limitations ?? this.options.globalRelayConfig?.disableAutoFetchNip11Limitations, maxConcurrentReqsFallback: this.options.globalRelayConfig?.maxConcurrentReqsFallback, }); connection.getConnectionStateObservable().subscribe((state) => { this.status$.next({ from: url, state, }); }); connection.getErrorObservable().subscribe((reason) => { this.error$.next({ from: url, reason }); }); connection .getMessageObservable() .pipe( catchError((reason: unknown) => { this.error$.next({ from: url, reason }); return EMPTY; }) )
.subscribe((v) => {
this.messageIn$.next(v); }); return connection; } async switchRelays(config: AcceptableRelaysConfig): Promise<void> { const nextConns: Map<string, Connection> = new Map(); for (const { url, read, write } of normalizeRelaysConfig(config)) { // pop a connection if exists const prevConn = this.connections.get(url); this.connections.delete(url); if (prevConn) { prevConn.read = read; prevConn.write = write; nextConns.set(url, prevConn); } else { nextConns.set(url, this.createConnection({ url, read, write })); } } // connections that are no longer used for (const conn of this.connections.values()) { conn.dispose(); } const ensureConns: Promise<unknown>[] = []; for (const conn of nextConns.values()) { if (conn.read) { ensureConns.push(conn.start()); } else { conn.stop(); } } await Promise.all(ensureConns); this.connections = nextConns; // If disposed during switchRelay processing if (this.disposed) { for (const conn of this.connections.values()) { conn.dispose(); } return; } for (const { req, scope } of this.ongoings.values()) { this.ensureReq(req, { scope }); } // --- scoped untility pure functions --- function normalizeRelaysConfig( config: AcceptableRelaysConfig ): RelayConfig[] { if (Array.isArray(config)) { return config.map((urlOrConfig) => { const relay: RelayConfig = typeof urlOrConfig === "string" ? { url: urlOrConfig, read: true, write: true, } : urlOrConfig; relay.url = normalizeRelayUrl(relay.url); return relay; }); } else { return Object.entries(config).map(([url, flags]) => ({ url: normalizeRelayUrl(url), ...flags, })); } } } async addRelay(relay: string | RelayConfig): Promise<void> { await this.switchRelays([...this.getRelays(), relay]); } async removeRelay(url: string): Promise<void> { const u = normalizeRelayUrl(url); const currentRelays = this.getRelays(); const nextRelays = currentRelays.filter((relay) => relay.url !== u); if (currentRelays.length !== nextRelays.length) { await this.switchRelays(nextRelays); } } hasRelay(url: string): boolean { const u = normalizeRelayUrl(url); return this.getRelays().some((relay) => relay.url === u); } canWriteRelay(url: string): boolean { const u = normalizeRelayUrl(url); return this.getRelays().some((relay) => relay.url === u && relay.write); } canReadRelay(url: string): boolean { const u = normalizeRelayUrl(url); return this.getRelays().some((relay) => relay.url === u && relay.read); } async fetchAllRelaysInfo(): Promise< Record<string, Nostr.Nip11.RelayInfo | null> > { const entries = await Promise.all( Array.from(this.connections.keys()).map( async (url): Promise<[string, Nostr.Nip11.RelayInfo | null]> => [ url, await fetchRelayInfo(url).catch(() => null), ] ) ); return Object.fromEntries(entries); } getAllRelayState(): Record<string, ConnectionState> { return Object.fromEntries( Array.from(this.connections.values()).map((e) => [ e.url, this.getRelayState(e.url), ]) ); } getRelayState(url: string): ConnectionState { const conn = this.connections.get(normalizeRelayUrl(url)); if (!conn) { throw new Error("RelayConfig not found"); } // this.relays[url] may be set before this.relays[url].websocket is initialized return conn?.getConnectionState() ?? "not-started"; } reconnect(url: string): void { if (this.canReadRelay(url)) { this.connections.get(normalizeRelayUrl(url))?.start(); } } setGlobalEventPacketPipe(pipe: MonoTypeOperatorFunction<EventPacket> | null) { this.globalEventPacketPipe = pipe; } use( rxReq: RxReq, options?: Partial<RxNostrUseOptions> ): Observable<EventPacket> { const { scope: _scope } = makeRxNostrUseOptions(options); const scope = _scope?.map(normalizeRelayUrl); const TIMEOUT = this.options.timeout; const strategy = rxReq.strategy; const rxReqId = rxReq.rxReqId; const message$ = this.messageOut$; const ongoings = this.ongoings; const getAllRelayState = this.getAllRelayState.bind(this); const createConnectionStateObservable = this.createConnectionStateObservable.bind(this); const ensureReq = this.ensureReq.bind(this); const finalizeReq = this.finalizeReq.bind(this); const subId$ = rxReq.getReqObservable().pipe( filter((filters): filters is LazyFilter[] => filters !== null), strategy === "oneshot" ? first() : identity, attachSubId(), strategy === "forward" ? manageActiveForwardReq() : identity, tap((req) => { ensureReq(req, { overwrite: strategy === "forward", scope }); }), map(([, subId]) => subId) ); if (strategy === "forward") { const subId = makeSubId({ rxReqId, }); const resource: Unsubscribable[] = []; const subject = new Subject<EventPacket>(); resource.push(subject); return subject.pipe( tap({ subscribe: () => { resource.push(subId$.subscribe()); resource.push( message$ .pipe(filterBySubId(subId), pickEvents()) .subscribe((v) => { subject.next(v); }) ); }, finalize: () => { for (const r of resource) { r.unsubscribe(); } finalizeReq({ subId }); }, }) ); } else { return subId$.pipe(map(createEoseManagedEventObservable), mergeAll()); } function attachSubId(): OperatorFunction<LazyFilter[], LazyREQ> { const makeId = (index?: number) => makeSubId({ rxReqId, index }); switch (strategy) { case "backward": return map((filters, index) => ["REQ", makeId(index), ...filters]); case "forward": case "oneshot": return map((filters) => ["REQ", makeId(), ...filters]); } } function manageActiveForwardReq(): MonoTypeOperatorFunction<LazyREQ> { const recordActiveReq = (req: LazyREQ) => { const subId = req[1]; ongoings.set(subId, { req, scope, }); }; const forgetActiveReq = (subId: string) => { ongoings.delete(subId); }; return tap({ next: (req: LazyREQ) => { recordActiveReq(req); }, finalize: () => { forgetActiveReq( makeSubId({ rxReqId, }) ); }, }); } function createEoseManagedEventObservable( subId: string ): Observable<EventPacket> { const eose$ = new Subject<void>(); const complete$ = new Subject<void>(); const eoseRelays = new Set<string>(); const manageCompletion = merge( eose$, createConnectionStateObservable() ).subscribe(() => { const status = getAllRelayState(); const shouldComplete = Object.entries(status).every( ([url, state]) => (scope && !scope.includes(url)) || state === "error" || state === "terminated" || (state === "ongoing" && eoseRelays.has(url)) ); if (shouldComplete) { complete$.next(); } }); return message$.pipe( takeUntil(complete$), completeOnTimeout(TIMEOUT), filterBySubId(subId), filter((e) => !eoseRelays.has(e.from)), tap((e) => { if (e.message[0] === "EOSE") { eoseRelays.add(e.from); finalizeReq({ subId, url: e.from }); eose$.next(); } }), pickEvents(), finalize(() => { finalizeReq({ subId }); complete$.unsubscribe(); eose$.unsubscribe(); manageCompletion.unsubscribe(); }) ); } function filterBySubId( subId: string ): MonoTypeOperatorFunction<MessagePacket> { return filter( (packet) => (packet.message[0] === "EVENT" || packet.message[0] === "EOSE") && packet.message[1] === subId ); } function pickEvents(): OperatorFunction<MessagePacket, EventPacket> { return mergeMap(({ from, message }) => message[0] === "EVENT" ? of({ from, subId: message[1], event: message[2] }) : EMPTY ); } } createAllEventObservable(): Observable<EventPacket> { return this.messageOut$.pipe( mergeMap(({ from, message }) => message[0] === "EVENT" ? of({ from, subId: message[1], event: message[2] }) : EMPTY ) ); } createAllErrorObservable(): Observable<ErrorPacket> { return this.error$.asObservable(); } createAllMessageObservable(): Observable<MessagePacket> { return this.messageOut$; } createConnectionStateObservable(): Observable<ConnectionStatePacket> { return this.status$.asObservable(); } send( params: Nostr.EventParameters, options?: RxNostrSendOptions ): Observable<OkPacket> { const { seckey, scope: _scope } = makeRxNostrSendOptions(options); const scope = _scope?.map(normalizeRelayUrl); const writableConns = Array.from(this.connections.values()).filter( (conn) => (!scope || scope.includes(conn.url)) && conn.write ); const subject = new ReplaySubject<OkPacket>(writableConns.length); let subscription: Subscription | null = null; getSignedEvent(params, seckey).then((event) => { if (!subject.closed) { subscription = this.createAllMessageObservable().subscribe( ({ from, message }) => { if (message[0] !== "OK") { return; } subject.next({ from, id: event.id, ok: message[2], }); } ); } for (const conn of writableConns) { conn.sendEVENT(["EVENT", event]); } }); return subject.pipe( take(writableConns.length), timeout(30 * 1000), finalize(() => { subject.complete(); subject.unsubscribe(); subscription?.unsubscribe(); }) ); } dispose(): void { this.disposed = true; this.messageIn$.complete(); this.error$.complete(); for (const conn of this.connections.values()) { conn.dispose(); } } private ensureReq( req: LazyREQ, options?: { scope?: string[] | null; overwrite?: boolean } ) { const scope = options?.scope; for (const url of this.connections.keys()) { const conn = this.connections.get(url); if (!conn || !conn.read || (scope && !scope.includes(url))) { continue; } conn.ensureReq(req, { overwrite: options?.overwrite }); } } private finalizeReq(params: { subId: string; url?: string }) { const { subId, url } = params; if (subId === undefined && url === undefined) { throw new Error(); } if (url) { const conn = this.connections.get(url); conn?.finalizeReq(subId); } else { for (const conn of this.connections.values()) { conn?.finalizeReq(subId); } } } } interface OngoingReq { req: LazyREQ; scope?: string[]; } function makeSubId(params: { rxReqId: string; index?: number }): string { return `${params.rxReqId}:${params.index ?? 0}`; }
src/rx-nostr.ts
penpenpng-rx-nostr-7c52614
[ { "filename": "src/packet.ts", "retrieved_chunk": " from: string;\n subId: string;\n event: Nostr.Event;\n}\n/**\n * Packets from websocket that represents an error.\n */\nexport interface ErrorPacket {\n from: string;\n reason: unknown;", "score": 15.602169351121836 }, { "filename": "src/connection.ts", "retrieved_chunk": " };\n const onmessage = ({ data }: MessageEvent) => {\n if (this.connectionState === \"terminated\") {\n return;\n }\n try {\n this.message$.next({ from: this.url, message: JSON.parse(data) });\n } catch (err) {\n this.error$.next(err);\n }", "score": 13.956445977584735 }, { "filename": "src/operator.ts", "retrieved_chunk": "/**\n * Almost RxJS's `timeout`, but won't throw.\n */\nexport function completeOnTimeout<T>(\n time: number\n): MonoTypeOperatorFunction<T> {\n return pipe(\n timeout(time),\n catchError((error: unknown) => {\n if (error instanceof TimeoutError) {", "score": 13.56687003060607 }, { "filename": "src/connection.ts", "retrieved_chunk": " getMessageObservable(): Observable<MessagePacket> {\n const reqs = this.reqs;\n return this.message$.asObservable().pipe(\n mergeMap((data) => {\n if (data instanceof WebSocketError) {\n if (data.code === WebSocketCloseCode.DONT_RETRY) {\n return EMPTY;\n } else {\n throw data;\n }", "score": 13.497746766376885 }, { "filename": "src/operator.ts", "retrieved_chunk": "import Nostr from \"nostr-typedef\";\nimport {\n catchError,\n delay,\n distinct,\n distinctUntilChanged,\n EMPTY,\n filter,\n groupBy,\n map,", "score": 12.857457868092297 } ]
typescript
.subscribe((v) => {
import Nostr from "nostr-typedef"; import { BehaviorSubject, Observable, type OperatorFunction } from "rxjs"; import { LazyFilter, ReqPacket } from "./packet.js"; import type { Override } from "./util.js"; /** * The RxReq interface that is provided for RxNostr (**not for users**). */ export interface RxReq<S extends RxReqStrategy = RxReqStrategy> { /** @internal User should not use this directly.The RxReq strategy. It is read-only and must not change. */ get strategy(): S; /** @internal User should not use this directly. Used to construct subId. */ get rxReqId(): string; /** @internal User should not use this directly. Get an Observable of ReqPacket. */ getReqObservable(): Observable<ReqPacket>; } /** * REQ strategy. * * See comments on `createRxForwardReq()`, `createRxBackwardReq()` and `createRxOneshotReq() */ export type RxReqStrategy = "forward" | "backward" | "oneshot"; /** * The RxReq interface that is provided for users (not for RxNostr). */ export interface RxReqController { /** Start new REQ or stop REQ on the RxNostr with witch the RxReq is associated. */ emit(filters: LazyFilter | LazyFilter[] | null): void; /** * Returns itself overriding only `getReqObservable()`. * It is useful for throttling and other control purposes. */ pipe(): RxReq; pipe(op1: OperatorFunction<ReqPacket, ReqPacket>): RxReq; pipe<A>( op1: OperatorFunction<ReqPacket, A>, op2: OperatorFunction<A, ReqPacket> ): RxReq; pipe<A, B>( op1: OperatorFunction<ReqPacket, A>, op2: OperatorFunction<A, B>, op3: OperatorFunction<B, ReqPacket> ): RxReq; pipe<A, B, C>( op1: OperatorFunction<ReqPacket, A>, op2: OperatorFunction<A, B>, op3: OperatorFunction<B, C>, op4: OperatorFunction<C, ReqPacket> ): RxReq; pipe<A, B, C, D>( op1: OperatorFunction<ReqPacket, A>, op2: OperatorFunction<A, B>, op3: OperatorFunction<B, C>, op4: OperatorFunction<C, D>, op5: OperatorFunction<D, ReqPacket> ): RxReq; } abstract class RxReqBase implements RxReq { protected filters$ = new BehaviorSubject<ReqPacket>(null); private _rxReqId: string; abstract get strategy(): RxReqStrategy; get rxReqId() { return this._rxReqId; } constructor(rxReqId?: string) { this._rxReqId = rxReqId ?? getRandomDigitsString(); } getReqObservable(): Observable<ReqPacket> { return this.filters$.asObservable(); } emit(filters: LazyFilter | LazyFilter[] | null) { const normalized = normalizeFilters(filters); if (normalized) { this.filters$.next(normalized); } else { this.filters$.next(null); } } pipe(): RxReq; pipe(op1: OperatorFunction<ReqPacket, ReqPacket>): RxReq; pipe<A>( op1: OperatorFunction<ReqPacket, A>, op2: OperatorFunction<A, ReqPacket> ): RxReq; pipe<A, B>( op1: OperatorFunction<ReqPacket, A>, op2: OperatorFunction<A, B>, op3: OperatorFunction<B, ReqPacket> ): RxReq; pipe<A, B, C>( op1: OperatorFunction<ReqPacket, A>, op2: OperatorFunction<A, B>, op3: OperatorFunction<B, C>, op4: OperatorFunction<C, ReqPacket> ): RxReq; pipe<A, B, C, D>( op1: OperatorFunction<ReqPacket, A>, op2: OperatorFunction<A, B>, op3: OperatorFunction<B, C>, op4: OperatorFunction<C, D>, op5: OperatorFunction<D, ReqPacket> ): RxReq; // eslint-disable-next-line @typescript-eslint/no-explicit-any pipe(...operators: OperatorFunction<any, any>[]): RxReq { const rxReqId = this.rxReqId; const strategy = this.strategy; return { ...this, get rxReqId() { return rxReqId; }, get strategy() { return strategy; }, getReqObservable: () => this.getReqObservable().pipe(...(operators as [])), }; } } /** * Create a RxReq instance based on the backward strategy. * It is useful if you want to retrieve past events that have already been published. * * In backward strategy: * - All REQs have different subIds. * - All REQ-subscriptions keep alive until timeout or getting EOSE. * - In most cases, you should specify `until` or `limit` for filters. * * For more information, see [document](https://penpenpng.github.io/rx-nostr/docs/req-strategy.html#backward-strategy). */ export function createRxBackwardReq( subIdBase?: string ): RxReq<"backward"> & RxReqController { return new RxBackwardReq(subIdBase); } class RxBackwardReq extends RxReqBase implements RxReqController { constructor(rxReqId?: string) { super(rxReqId); } override get strategy(): "backward" { return "backward"; } } /** * Create a RxReq instance based on the forward strategy. * It is useful if you want to listen future events. * * In forward strategy: * - All REQs have the same subId. * - When a new REQ is issued, the old REQ is overwritten and terminated immediately. * The latest REQ keeps alive until it is overwritten or explicitly terminated. * - In most cases, you should not specify `limit` for filters. * * For more information, see [document](https://penpenpng.github.io/rx-nostr/docs/req-strategy.html#forward-strategy). */ export function createRxForwardReq( subId?: string ): RxReq<"forward"> & RxReqController { return new RxForwardReq(subId); } class RxForwardReq extends RxReqBase implements RxReqController { constructor(rxReqId?: string) { super(rxReqId); } override get strategy(): "forward" { return "forward"; } } /** * Create a RxReq instance based on the oneshot strategy. * It is almost the same as backward strategy, however can publish only one REQ * and the Observable completes on EOSE. * * For more information, see [document](https://penpenpng.github.io/rx-nostr/docs/req-strategy.html#oneshot-strategy). */ export function createRxOneshotReq(req: { filters: LazyFilter | LazyFilter[]; subId?: string; }): RxReq<"oneshot"> { return new RxOneshotReq(req); } class RxOneshotReq extends RxReqBase { constructor(req: { filters: LazyFilter | LazyFilter[]; subId?: string }) { super(req?.subId); this.emit(req.filters); } override get strategy(): "oneshot" { return "oneshot"; } } export interface Mixin<R, T> { (): ThisType<R> & T; } /** NOTE: unstable feature */ export function mixin<R extends object, T extends object>( def: () => ThisType<R> & T ): Mixin<R, T> { return def; } /** NOTE: unstable feature */ export function extend<B extends R, R extends object, T extends object>( base: B, mixin: Mixin<R, T> ): Override<B, T> { return Object.assign(base, mixin()) as Override<B, T>; } function getRandomDigitsString() { return `${Math.floor(Math.random() * 1000000)}`; } function normalizeFilter(filter: LazyFilter): LazyFilter | null { const res: LazyFilter = {}; const isTagName = (s: string): s is Nostr.TagName => /^#[a-zA-Z]$/.test(s); for (const key of Object.keys(filter)) { if (
key === "limit" && (filter[key] ?? -1) >= 0) {
res[key] = filter[key]; continue; } if (key === "since" || key === "until") { const f = filter[key]; if (typeof f !== "number" || (f ?? -1) >= 0) { res[key] = f; continue; } } if ( (isTagName(key) || key === "ids" || key === "authors") && filter[key] !== undefined && (filter[key]?.length ?? -1) > 0 ) { res[key] = filter[key]; continue; } if ( key === "kinds" && filter[key] !== undefined && (filter[key]?.length ?? -1) > 0 ) { res[key] = filter[key]; continue; } if (key === "search" && filter[key] !== undefined) { res[key] = filter[key]; continue; } } const timeRangeIsValid = typeof res.since !== "number" || typeof res.until !== "number" || res.since <= res.until; if (!timeRangeIsValid) { return null; } if (Object.keys(res).length <= 0) { return null; } return res; } function normalizeFilters( filters: LazyFilter | LazyFilter[] | null ): LazyFilter[] | null { if (!filters) { return null; } const normalized = (Array.isArray(filters) ? filters : [filters]).flatMap( (e) => normalizeFilter(e) ?? [] ); return normalized.length > 0 ? normalized : null; }
src/req.ts
penpenpng-rx-nostr-7c52614
[ { "filename": "src/helper.ts", "retrieved_chunk": "/** Return a function that is lazily evaluated for since/until parameters of `LazyFilter`. */\nimport Nostr from \"nostr-typedef\";\nimport { LazyFilter } from \"./packet.js\";\nexport function now(): number {\n return Math.floor(new Date().getTime() / 1000);\n}\nexport function evalFilters(\n filters: LazyFilter | LazyFilter[]\n): Nostr.Filter[] {\n if (\"length\" in filters) {", "score": 38.768196696175146 }, { "filename": "src/nostr/filter.ts", "retrieved_chunk": " return false;\n }\n if (\n filter.until &&\n ((untilInclusive && !(event.created_at <= filter.until)) ||\n (!untilInclusive && !(event.created_at < filter.until)))\n ) {\n return false;\n }\n for (const [key, needleValues] of Object.entries(filter)) {", "score": 31.83929443312733 }, { "filename": "src/util.ts", "retrieved_chunk": "import normalizeUrl from \"normalize-url\";\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport function defineDefaultOptions<T extends Record<string, any>>(\n defaultParams: T\n): (givenParams?: Partial<T>) => T {\n return (givenParams) =>\n Object.fromEntries(\n Object.keys(defaultParams).map((key) => [\n key,\n givenParams?.[key] ?? defaultParams[key],", "score": 27.325462273402284 }, { "filename": "src/operator.ts", "retrieved_chunk": " options?: CreateUniqOptions<T>\n): [MonoTypeOperatorFunction<EventPacket>, Set<T>] {\n const cache = new Set<T>();\n return [\n filter((packet) => {\n const key = keyFn(packet);\n if (key === null) {\n return true;\n }\n if (cache.has(key)) {", "score": 27.122784575714714 }, { "filename": "src/helper.ts", "retrieved_chunk": " return filters.map(evalFilter);\n } else {\n return [evalFilter(filters)];\n }\n}\nfunction evalFilter(filter: LazyFilter): Nostr.Filter {\n return {\n ...filter,\n since: filter.since ? evalLazyNumber(filter.since) : undefined,\n until: filter.until ? evalLazyNumber(filter.until) : undefined,", "score": 26.588601891372843 } ]
typescript
key === "limit" && (filter[key] ?? -1) >= 0) {
import Nostr from "nostr-typedef"; import { catchError, EMPTY, filter, finalize, first, identity, map, merge, mergeAll, mergeMap, type MonoTypeOperatorFunction, Observable, of, type OperatorFunction, ReplaySubject, Subject, Subscription, take, takeUntil, tap, timeout, type Unsubscribable, } from "rxjs"; import { BackoffConfig, Connection } from "./connection.js"; import { getSignedEvent } from "./nostr/event.js"; import { fetchRelayInfo } from "./nostr/nip11.js"; import { completeOnTimeout } from "./operator.js"; import type { ConnectionState, ConnectionStatePacket, ErrorPacket, EventPacket, LazyFilter, LazyREQ, MessagePacket, OkPacket, } from "./packet.js"; import type { RxReq } from "./req.js"; import { defineDefaultOptions, normalizeRelayUrl } from "./util.js"; /** * The core object of rx-nostr, which holds a connection to relays * and manages subscriptions as directed by the RxReq object connected by `use()`. * Use `createRxNostr()` to get the object. */ export interface RxNostr { /** * Return a list of relays used by this object. * The relay URLs are normalised so may not match the URLs set. */ getRelays(): RelayConfig[]; /** * Set the list of relays. * If a REQ subscription already exists, the same REQ is issued for the newly added relay * and CLOSE is sent for the removed relay. */ switchRelays(config: AcceptableRelaysConfig): Promise<void>; /** Utility wrapper for `switchRelays()`. */ addRelay(relay: string | RelayConfig): Promise<void>; /** Utility wrapper for `switchRelays()`. */ removeRelay(url: string): Promise<void>; /** Return true if the given relay is set to rxNostr. */ hasRelay(url: string): boolean; /** Return true if the given relay allows to be written. */ canWriteRelay(url: string): boolean; /** Return true if the given relay allows to be read. */ canReadRelay(url: string): boolean; /** Fetch all relays' info based on [NIP-11](https://github.com/nostr-protocol/nips/blob/master/11.md) */ fetchAllRelaysInfo(): Promise<Record<string, Nostr.Nip11.RelayInfo | null>>; /** * Return a dictionary in which you can look up connection state. * * **NOTE**: Keys are **normalized** URL, so may be different from one you set. */ getAllRelayState(): Record<string, ConnectionState>; /** * Return connection state of the given relay. * Throw if unknown URL is given. */ getRelayState(url: string): ConnectionState; /** * Attempt to reconnect the WebSocket if its state is `error` or `rejected`. * If not, do nothing. */ reconnect(url: string): void; // TODO: document /** * Set or unset a pipe to be applied to all EventPackets. */ setGlobalEventPacketPipe( pipe: MonoTypeOperatorFunction<EventPacket> | null ): void; /** * Associate RxReq with RxNostr. * When the associated RxReq is manipulated, * the RxNostr issues a new REQ to all relays allowed to be read. * The method returns an Observable that issues EventPackets * when an EVENT is received that is subscribed by RxReq. * You can unsubscribe the Observable to CLOSE. */ use( rxReq: RxReq, options?: Partial<RxNostrUseOptions> ): Observable<EventPacket>; /** * Create an Observable that receives all events (EVENT) from all websocket connections. * * Nothing happens when this Observable is unsubscribed. * */ createAllEventObservable(): Observable<EventPacket>; /** * Create an Observable that receives all errors from all websocket connections. * Note that an Observable is terminated when it receives any error, * so this method is the only way to receive errors arising from multiplexed websocket connections * (It means that Observables returned by `use()` never throw error). * * Nothing happens when this Observable is unsubscribed. * */ createAllErrorObservable(): Observable<ErrorPacket>; /** * Create an Observable that receives all messages from all websocket connections. * * Nothing happens when this Observable is unsubscribed. * */ createAllMessageObservable(): Observable<MessagePacket>; /** * Create an Observable that receives changing of WebSocket connection state. * * Nothing happens when this Observable is unsubscribed. */ createConnectionStateObservable(): Observable<ConnectionStatePacket>; /** * Attempt to send events to all relays that are allowed to write. * The `seckey` option accepts both nsec format and hex format, * and if omitted NIP-07 will be automatically used. */ send( params: Nostr.EventParameters, options?: RxNostrSendOptions ): Observable<OkPacket>; /** * Release all resources held by the RxNostr object. * Any Observable resulting from this RxNostr will be in the completed state * and will never receive messages again. * RxReq used by this object is not affected; in other words, if the RxReq is used * by another RxNostr, its use is not prevented. */ dispose(): void; } /** Create a RxNostr object. This is the only way to create that. */ export function createRxNostr(options?: Partial<RxNostrOptions>): RxNostr { return new RxNostrImpl(options); } export interface RxNostrOptions { /** Auto reconnection strategy. */ retry: BackoffConfig; /** * The time in milliseconds to timeout when following the backward strategy. * The observable is terminated when the specified amount of time has elapsed * during which no new events are available. */ timeout: number; globalRelayConfig?: { disableAutoFetchNip11Limitations?: boolean; maxConcurrentReqsFallback?: number; }; } const makeRxNostrOptions = defineDefaultOptions<RxNostrOptions>({ retry: { strategy: "exponential", maxCount: 5, initialDelay: 1000, }, timeout: 10000, globalRelayConfig: undefined, }); export interface RxNostrUseOptions { scope?: string[]; } const makeRxNostrUseOptions = defineDefaultOptions<RxNostrUseOptions>({ scope: undefined, }); export interface RxNostrSendOptions { scope?: string[]; seckey?: string; } const makeRxNostrSendOptions = defineDefaultOptions<RxNostrSendOptions>({ scope: undefined, seckey: undefined, }); /** Config object specifying WebSocket behavior. */ export interface RelayConfig { /** WebSocket endpoint URL. */ url: string; /** If true, rxNostr can publish REQ and subscribe EVENTs. */ read: boolean; /** If true, rxNostr can send EVENTs. */ write: boolean; disableAutoFetchNip11Limitations?: boolean; } /** Parameter of `rxNostr.switchRelays()` */ export type AcceptableRelaysConfig = | (string | RelayConfig)[] | Nostr.Nip07.GetRelayResult; class RxNostrImpl implements RxNostr { private options: RxNostrOptions; private connections: Map<string, Connection> = new Map(); private ongoings: Map<string, OngoingReq> = new Map(); private messageIn$: Subject<MessagePacket> = new Subject(); private error$: Subject<ErrorPacket> = new Subject(); private status$: Subject<ConnectionStatePacket> = new Subject(); private globalEventPacketPipe: MonoTypeOperatorFunction<EventPacket> | null = null; private disposed = false; private get messageOut$() { return this.messageIn$.pipe( mergeMap((packet) => { const pipe = this.globalEventPacketPipe; if (!pipe) { return of(packet); } const message = packet.message; if (message[0] !== "EVENT") { return of(packet); } return of({ from: packet.from, subId: message[1], event: message[2], }).pipe( pipe, map( ({ from, subId, event }): MessagePacket => ({ from, message: ["EVENT", subId, event], }) ) ); }) ); } constructor(options?: Partial<RxNostrOptions>) { const opt = makeRxNostrOptions(options); this.options = { ...opt, }; } getRelays(): RelayConfig[] { return Array.from(this.connections.values()).map( ({ url, read, write }) => ({ url, read, write, }) ); } private createConnection({ url, read, write, disableAutoFetchNip11Limitations, }: RelayConfig): Connection { const connection = new Connection(url, { backoff: this.options.retry, read, write, disableAutoFetchNip11Limitations: disableAutoFetchNip11Limitations ?? this.options.globalRelayConfig?.disableAutoFetchNip11Limitations, maxConcurrentReqsFallback: this.options.globalRelayConfig?.maxConcurrentReqsFallback, }); connection.getConnectionStateObservable().subscribe((state) => { this.status$.next({ from: url, state, }); }); connection.getErrorObservable().subscribe((reason) => { this.error$.next({ from: url, reason }); }); connection .getMessageObservable() .pipe( catchError((reason: unknown) => { this.error$.next({ from: url, reason }); return EMPTY; }) ) .subscribe((v) => { this.messageIn$.next(v); }); return connection; } async switchRelays(config: AcceptableRelaysConfig): Promise<void> { const nextConns: Map<string, Connection> = new Map(); for (const { url, read, write } of normalizeRelaysConfig(config)) { // pop a connection if exists const prevConn = this.connections.get(url); this.connections.delete(url); if (prevConn) { prevConn.read = read; prevConn.write = write; nextConns.set(url, prevConn); } else { nextConns.set(url, this.createConnection({ url, read, write })); } } // connections that are no longer used for (const conn of this.connections.values()) { conn.dispose(); } const ensureConns: Promise<unknown>[] = []; for (const conn of nextConns.values()) { if (conn.read) { ensureConns.push(conn.start()); } else { conn.stop(); } } await Promise.all(ensureConns); this.connections = nextConns; // If disposed during switchRelay processing if (this.disposed) { for (const conn of this.connections.values()) { conn.dispose(); } return; } for (const { req, scope } of this.ongoings.values()) { this.ensureReq(req, { scope }); } // --- scoped untility pure functions --- function normalizeRelaysConfig( config: AcceptableRelaysConfig ): RelayConfig[] { if (Array.isArray(config)) { return config.map((urlOrConfig) => { const relay: RelayConfig = typeof urlOrConfig === "string" ? { url: urlOrConfig, read: true, write: true, } : urlOrConfig; relay.url = normalizeRelayUrl(relay.url); return relay; }); } else { return Object.entries(config).map(([url, flags]) => ({ url: normalizeRelayUrl(url), ...flags, })); } } } async addRelay(relay: string | RelayConfig): Promise<void> { await this.switchRelays([...this.getRelays(), relay]); } async removeRelay(url: string): Promise<void> { const u = normalizeRelayUrl(url); const currentRelays = this.getRelays(); const nextRelays = currentRelays.filter((relay) => relay.url !== u); if (currentRelays.length !== nextRelays.length) { await this.switchRelays(nextRelays); } } hasRelay(url: string): boolean { const u = normalizeRelayUrl(url); return this.getRelays().some((relay) => relay.url === u); } canWriteRelay(url: string): boolean { const u = normalizeRelayUrl(url); return this.getRelays().some((relay) => relay.url === u && relay.write); } canReadRelay(url: string): boolean { const u = normalizeRelayUrl(url); return this.getRelays().some((relay) => relay.url === u && relay.read); } async fetchAllRelaysInfo(): Promise< Record<string, Nostr.Nip11.RelayInfo | null> > { const entries = await Promise.all( Array.from(this.connections.keys()).map( async (url): Promise<[string, Nostr.Nip11.RelayInfo | null]> => [ url, await fetchRelayInfo(url).catch(() => null), ] ) ); return Object.fromEntries(entries); } getAllRelayState(): Record<string, ConnectionState> { return Object.fromEntries( Array.from(this.connections.values()).map((e) => [ e.url, this.getRelayState(e.url), ]) ); } getRelayState(url: string): ConnectionState { const conn = this.connections.get(normalizeRelayUrl(url)); if (!conn) { throw new Error("RelayConfig not found"); } // this.relays[url] may be set before this.relays[url].websocket is initialized return conn?.getConnectionState() ?? "not-started"; } reconnect(url: string): void { if (this.canReadRelay(url)) { this.connections.get(normalizeRelayUrl(url))?.start(); } } setGlobalEventPacketPipe(pipe: MonoTypeOperatorFunction<EventPacket> | null) { this.globalEventPacketPipe = pipe; } use( rxReq: RxReq, options?: Partial<RxNostrUseOptions> ): Observable<EventPacket> { const { scope: _scope } = makeRxNostrUseOptions(options); const scope = _scope?.map(normalizeRelayUrl); const TIMEOUT = this.options.timeout;
const strategy = rxReq.strategy;
const rxReqId = rxReq.rxReqId; const message$ = this.messageOut$; const ongoings = this.ongoings; const getAllRelayState = this.getAllRelayState.bind(this); const createConnectionStateObservable = this.createConnectionStateObservable.bind(this); const ensureReq = this.ensureReq.bind(this); const finalizeReq = this.finalizeReq.bind(this); const subId$ = rxReq.getReqObservable().pipe( filter((filters): filters is LazyFilter[] => filters !== null), strategy === "oneshot" ? first() : identity, attachSubId(), strategy === "forward" ? manageActiveForwardReq() : identity, tap((req) => { ensureReq(req, { overwrite: strategy === "forward", scope }); }), map(([, subId]) => subId) ); if (strategy === "forward") { const subId = makeSubId({ rxReqId, }); const resource: Unsubscribable[] = []; const subject = new Subject<EventPacket>(); resource.push(subject); return subject.pipe( tap({ subscribe: () => { resource.push(subId$.subscribe()); resource.push( message$ .pipe(filterBySubId(subId), pickEvents()) .subscribe((v) => { subject.next(v); }) ); }, finalize: () => { for (const r of resource) { r.unsubscribe(); } finalizeReq({ subId }); }, }) ); } else { return subId$.pipe(map(createEoseManagedEventObservable), mergeAll()); } function attachSubId(): OperatorFunction<LazyFilter[], LazyREQ> { const makeId = (index?: number) => makeSubId({ rxReqId, index }); switch (strategy) { case "backward": return map((filters, index) => ["REQ", makeId(index), ...filters]); case "forward": case "oneshot": return map((filters) => ["REQ", makeId(), ...filters]); } } function manageActiveForwardReq(): MonoTypeOperatorFunction<LazyREQ> { const recordActiveReq = (req: LazyREQ) => { const subId = req[1]; ongoings.set(subId, { req, scope, }); }; const forgetActiveReq = (subId: string) => { ongoings.delete(subId); }; return tap({ next: (req: LazyREQ) => { recordActiveReq(req); }, finalize: () => { forgetActiveReq( makeSubId({ rxReqId, }) ); }, }); } function createEoseManagedEventObservable( subId: string ): Observable<EventPacket> { const eose$ = new Subject<void>(); const complete$ = new Subject<void>(); const eoseRelays = new Set<string>(); const manageCompletion = merge( eose$, createConnectionStateObservable() ).subscribe(() => { const status = getAllRelayState(); const shouldComplete = Object.entries(status).every( ([url, state]) => (scope && !scope.includes(url)) || state === "error" || state === "terminated" || (state === "ongoing" && eoseRelays.has(url)) ); if (shouldComplete) { complete$.next(); } }); return message$.pipe( takeUntil(complete$), completeOnTimeout(TIMEOUT), filterBySubId(subId), filter((e) => !eoseRelays.has(e.from)), tap((e) => { if (e.message[0] === "EOSE") { eoseRelays.add(e.from); finalizeReq({ subId, url: e.from }); eose$.next(); } }), pickEvents(), finalize(() => { finalizeReq({ subId }); complete$.unsubscribe(); eose$.unsubscribe(); manageCompletion.unsubscribe(); }) ); } function filterBySubId( subId: string ): MonoTypeOperatorFunction<MessagePacket> { return filter( (packet) => (packet.message[0] === "EVENT" || packet.message[0] === "EOSE") && packet.message[1] === subId ); } function pickEvents(): OperatorFunction<MessagePacket, EventPacket> { return mergeMap(({ from, message }) => message[0] === "EVENT" ? of({ from, subId: message[1], event: message[2] }) : EMPTY ); } } createAllEventObservable(): Observable<EventPacket> { return this.messageOut$.pipe( mergeMap(({ from, message }) => message[0] === "EVENT" ? of({ from, subId: message[1], event: message[2] }) : EMPTY ) ); } createAllErrorObservable(): Observable<ErrorPacket> { return this.error$.asObservable(); } createAllMessageObservable(): Observable<MessagePacket> { return this.messageOut$; } createConnectionStateObservable(): Observable<ConnectionStatePacket> { return this.status$.asObservable(); } send( params: Nostr.EventParameters, options?: RxNostrSendOptions ): Observable<OkPacket> { const { seckey, scope: _scope } = makeRxNostrSendOptions(options); const scope = _scope?.map(normalizeRelayUrl); const writableConns = Array.from(this.connections.values()).filter( (conn) => (!scope || scope.includes(conn.url)) && conn.write ); const subject = new ReplaySubject<OkPacket>(writableConns.length); let subscription: Subscription | null = null; getSignedEvent(params, seckey).then((event) => { if (!subject.closed) { subscription = this.createAllMessageObservable().subscribe( ({ from, message }) => { if (message[0] !== "OK") { return; } subject.next({ from, id: event.id, ok: message[2], }); } ); } for (const conn of writableConns) { conn.sendEVENT(["EVENT", event]); } }); return subject.pipe( take(writableConns.length), timeout(30 * 1000), finalize(() => { subject.complete(); subject.unsubscribe(); subscription?.unsubscribe(); }) ); } dispose(): void { this.disposed = true; this.messageIn$.complete(); this.error$.complete(); for (const conn of this.connections.values()) { conn.dispose(); } } private ensureReq( req: LazyREQ, options?: { scope?: string[] | null; overwrite?: boolean } ) { const scope = options?.scope; for (const url of this.connections.keys()) { const conn = this.connections.get(url); if (!conn || !conn.read || (scope && !scope.includes(url))) { continue; } conn.ensureReq(req, { overwrite: options?.overwrite }); } } private finalizeReq(params: { subId: string; url?: string }) { const { subId, url } = params; if (subId === undefined && url === undefined) { throw new Error(); } if (url) { const conn = this.connections.get(url); conn?.finalizeReq(subId); } else { for (const conn of this.connections.values()) { conn?.finalizeReq(subId); } } } } interface OngoingReq { req: LazyREQ; scope?: string[]; } function makeSubId(params: { rxReqId: string; index?: number }): string { return `${params.rxReqId}:${params.index ?? 0}`; }
src/rx-nostr.ts
penpenpng-rx-nostr-7c52614
[ { "filename": "src/nostr/filter.ts", "retrieved_chunk": " } else {\n return _isFiltered(event, filters, options);\n }\n}\nfunction _isFiltered(\n event: Nostr.Event,\n filter: Nostr.Filter,\n options?: Partial<MatchFilterOptions>\n): boolean {\n const { sinceInclusive, untilInclusive } = makeMatchFilterOptions(options);", "score": 22.91200576091872 }, { "filename": "src/operator.ts", "retrieved_chunk": "export function filterBy(\n filters: LazyFilter | LazyFilter[],\n options?: MatchFilterOptions & FilterByOptions\n): MonoTypeOperatorFunction<EventPacket> {\n const { not } = makeFilterByOptions(options);\n const evaledFilter = evalFilters(filters);\n return filter(({ event }) => {\n const match = isFiltered(event, evaledFilter, options);\n return not ? !match : match;\n });", "score": 20.40819675271701 }, { "filename": "src/req.ts", "retrieved_chunk": " ): RxReq;\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n pipe(...operators: OperatorFunction<any, any>[]): RxReq {\n const rxReqId = this.rxReqId;\n const strategy = this.strategy;\n return {\n ...this,\n get rxReqId() {\n return rxReqId;\n },", "score": 18.125731167493054 }, { "filename": "src/connection.ts", "retrieved_chunk": " }\n getErrorObservable() {\n return this.error$.asObservable();\n }\n ensureReq(req: LazyREQ, options?: { overwrite?: boolean }) {\n const subId = req[1];\n if (this.connectionState === \"terminated\") {\n return;\n }\n if (!this.read) {", "score": 17.69616701416096 }, { "filename": "src/nostr/filter.ts", "retrieved_chunk": "/**\n * Return true if the given filter matches the given filters.\n */\nexport function isFiltered(\n event: Nostr.Event,\n filters: Nostr.Filter | Nostr.Filter[],\n options?: Partial<MatchFilterOptions>\n): boolean {\n if (Array.isArray(filters)) {\n return filters.some((filter) => _isFiltered(event, filter, options));", "score": 16.7520762931222 } ]
typescript
const strategy = rxReq.strategy;
import Nostr from "nostr-typedef"; import { catchError, EMPTY, filter, finalize, first, identity, map, merge, mergeAll, mergeMap, type MonoTypeOperatorFunction, Observable, of, type OperatorFunction, ReplaySubject, Subject, Subscription, take, takeUntil, tap, timeout, type Unsubscribable, } from "rxjs"; import { BackoffConfig, Connection } from "./connection.js"; import { getSignedEvent } from "./nostr/event.js"; import { fetchRelayInfo } from "./nostr/nip11.js"; import { completeOnTimeout } from "./operator.js"; import type { ConnectionState, ConnectionStatePacket, ErrorPacket, EventPacket, LazyFilter, LazyREQ, MessagePacket, OkPacket, } from "./packet.js"; import type { RxReq } from "./req.js"; import { defineDefaultOptions, normalizeRelayUrl } from "./util.js"; /** * The core object of rx-nostr, which holds a connection to relays * and manages subscriptions as directed by the RxReq object connected by `use()`. * Use `createRxNostr()` to get the object. */ export interface RxNostr { /** * Return a list of relays used by this object. * The relay URLs are normalised so may not match the URLs set. */ getRelays(): RelayConfig[]; /** * Set the list of relays. * If a REQ subscription already exists, the same REQ is issued for the newly added relay * and CLOSE is sent for the removed relay. */ switchRelays(config: AcceptableRelaysConfig): Promise<void>; /** Utility wrapper for `switchRelays()`. */ addRelay(relay: string | RelayConfig): Promise<void>; /** Utility wrapper for `switchRelays()`. */ removeRelay(url: string): Promise<void>; /** Return true if the given relay is set to rxNostr. */ hasRelay(url: string): boolean; /** Return true if the given relay allows to be written. */ canWriteRelay(url: string): boolean; /** Return true if the given relay allows to be read. */ canReadRelay(url: string): boolean; /** Fetch all relays' info based on [NIP-11](https://github.com/nostr-protocol/nips/blob/master/11.md) */ fetchAllRelaysInfo(): Promise<Record<string, Nostr.Nip11.RelayInfo | null>>; /** * Return a dictionary in which you can look up connection state. * * **NOTE**: Keys are **normalized** URL, so may be different from one you set. */ getAllRelayState(): Record<string, ConnectionState>; /** * Return connection state of the given relay. * Throw if unknown URL is given. */ getRelayState(url: string): ConnectionState; /** * Attempt to reconnect the WebSocket if its state is `error` or `rejected`. * If not, do nothing. */ reconnect(url: string): void; // TODO: document /** * Set or unset a pipe to be applied to all EventPackets. */ setGlobalEventPacketPipe( pipe: MonoTypeOperatorFunction<EventPacket> | null ): void; /** * Associate RxReq with RxNostr. * When the associated RxReq is manipulated, * the RxNostr issues a new REQ to all relays allowed to be read. * The method returns an Observable that issues EventPackets * when an EVENT is received that is subscribed by RxReq. * You can unsubscribe the Observable to CLOSE. */ use( rxReq: RxReq, options?: Partial<RxNostrUseOptions> ): Observable<EventPacket>; /** * Create an Observable that receives all events (EVENT) from all websocket connections. * * Nothing happens when this Observable is unsubscribed. * */ createAllEventObservable(): Observable<EventPacket>; /** * Create an Observable that receives all errors from all websocket connections. * Note that an Observable is terminated when it receives any error, * so this method is the only way to receive errors arising from multiplexed websocket connections * (It means that Observables returned by `use()` never throw error). * * Nothing happens when this Observable is unsubscribed. * */ createAllErrorObservable(): Observable<ErrorPacket>; /** * Create an Observable that receives all messages from all websocket connections. * * Nothing happens when this Observable is unsubscribed. * */ createAllMessageObservable(): Observable<MessagePacket>; /** * Create an Observable that receives changing of WebSocket connection state. * * Nothing happens when this Observable is unsubscribed. */ createConnectionStateObservable(): Observable<ConnectionStatePacket>; /** * Attempt to send events to all relays that are allowed to write. * The `seckey` option accepts both nsec format and hex format, * and if omitted NIP-07 will be automatically used. */ send( params: Nostr.EventParameters, options?: RxNostrSendOptions ): Observable<OkPacket>; /** * Release all resources held by the RxNostr object. * Any Observable resulting from this RxNostr will be in the completed state * and will never receive messages again. * RxReq used by this object is not affected; in other words, if the RxReq is used * by another RxNostr, its use is not prevented. */ dispose(): void; } /** Create a RxNostr object. This is the only way to create that. */ export function createRxNostr(options?: Partial<RxNostrOptions>): RxNostr { return new RxNostrImpl(options); } export interface RxNostrOptions { /** Auto reconnection strategy. */ retry: BackoffConfig; /** * The time in milliseconds to timeout when following the backward strategy. * The observable is terminated when the specified amount of time has elapsed * during which no new events are available. */ timeout: number; globalRelayConfig?: { disableAutoFetchNip11Limitations?: boolean; maxConcurrentReqsFallback?: number; }; } const makeRxNostrOptions = defineDefaultOptions<RxNostrOptions>({ retry: { strategy: "exponential", maxCount: 5, initialDelay: 1000, }, timeout: 10000, globalRelayConfig: undefined, }); export interface RxNostrUseOptions { scope?: string[]; } const makeRxNostrUseOptions = defineDefaultOptions<RxNostrUseOptions>({ scope: undefined, }); export interface RxNostrSendOptions { scope?: string[]; seckey?: string; } const makeRxNostrSendOptions = defineDefaultOptions<RxNostrSendOptions>({ scope: undefined, seckey: undefined, }); /** Config object specifying WebSocket behavior. */ export interface RelayConfig { /** WebSocket endpoint URL. */ url: string; /** If true, rxNostr can publish REQ and subscribe EVENTs. */ read: boolean; /** If true, rxNostr can send EVENTs. */ write: boolean; disableAutoFetchNip11Limitations?: boolean; } /** Parameter of `rxNostr.switchRelays()` */ export type AcceptableRelaysConfig = | (string | RelayConfig)[] | Nostr.Nip07.GetRelayResult; class RxNostrImpl implements RxNostr { private options: RxNostrOptions; private connections: Map<string, Connection> = new Map(); private ongoings: Map<string, OngoingReq> = new Map(); private messageIn$: Subject<MessagePacket> = new Subject(); private error$: Subject<ErrorPacket> = new Subject(); private status$: Subject<ConnectionStatePacket> = new Subject(); private globalEventPacketPipe: MonoTypeOperatorFunction<EventPacket> | null = null; private disposed = false; private get messageOut$() { return this.messageIn$.pipe( mergeMap((packet) => { const pipe = this.globalEventPacketPipe; if (!pipe) { return of(packet); } const message = packet.message; if (message[0] !== "EVENT") { return of(packet); } return of({ from: packet.from, subId: message[1], event: message[2], }).pipe( pipe, map( ({ from, subId, event }): MessagePacket => ({ from, message: ["EVENT", subId, event], }) ) ); }) ); } constructor(options?: Partial<RxNostrOptions>) { const opt = makeRxNostrOptions(options); this.options = { ...opt, }; } getRelays(): RelayConfig[] { return Array.from(this.connections.values()).map( ({ url, read, write }) => ({ url, read, write, }) ); } private createConnection({ url, read, write, disableAutoFetchNip11Limitations, }: RelayConfig): Connection { const connection = new Connection(url, { backoff: this.options.retry, read, write, disableAutoFetchNip11Limitations: disableAutoFetchNip11Limitations ?? this.options.globalRelayConfig?.disableAutoFetchNip11Limitations, maxConcurrentReqsFallback: this.options.globalRelayConfig?.maxConcurrentReqsFallback, }); connection.getConnectionStateObservable().subscribe((state) => { this.status$.next({ from: url, state, }); }); connection.getErrorObservable().subscribe((reason) => { this.error$.next({ from: url, reason }); }); connection .getMessageObservable() .pipe( catchError((reason: unknown) => { this.error$.next({ from: url, reason }); return EMPTY; }) ) .subscribe((v) => { this.messageIn$.next(v); }); return connection; } async switchRelays(config: AcceptableRelaysConfig): Promise<void> { const nextConns: Map<string, Connection> = new Map(); for (const { url, read, write } of normalizeRelaysConfig(config)) { // pop a connection if exists const prevConn = this.connections.get(url); this.connections.delete(url); if (prevConn) { prevConn.read = read; prevConn.write = write; nextConns.set(url, prevConn); } else { nextConns.set(url, this.createConnection({ url, read, write })); } } // connections that are no longer used for (const conn of this.connections.values()) { conn.dispose(); } const ensureConns: Promise<unknown>[] = []; for (const conn of nextConns.values()) { if (conn.read) { ensureConns.push(conn.start()); } else { conn.stop(); } } await Promise.all(ensureConns); this.connections = nextConns; // If disposed during switchRelay processing if (this.disposed) { for (const conn of this.connections.values()) { conn.dispose(); } return; } for (const { req, scope } of this.ongoings.values()) { this.ensureReq(req, { scope }); } // --- scoped untility pure functions --- function normalizeRelaysConfig( config: AcceptableRelaysConfig ): RelayConfig[] { if (Array.isArray(config)) { return config.map((urlOrConfig) => { const relay: RelayConfig = typeof urlOrConfig === "string" ? { url: urlOrConfig, read: true, write: true, } : urlOrConfig; relay.url = normalizeRelayUrl(relay.url); return relay; }); } else { return Object.entries(config).map(([url, flags]) => ({ url: normalizeRelayUrl(url), ...flags, })); } } } async addRelay(relay: string | RelayConfig): Promise<void> { await this.switchRelays([...this.getRelays(), relay]); } async removeRelay(url: string): Promise<void> { const u = normalizeRelayUrl(url); const currentRelays = this.getRelays(); const nextRelays = currentRelays.filter((relay) => relay.url !== u); if (currentRelays.length !== nextRelays.length) { await this.switchRelays(nextRelays); } } hasRelay(url: string): boolean { const u = normalizeRelayUrl(url); return this.getRelays().some((relay) => relay.url === u); } canWriteRelay(url: string): boolean { const u = normalizeRelayUrl(url); return this.getRelays().some((relay) => relay.url === u && relay.write); } canReadRelay(url: string): boolean { const u = normalizeRelayUrl(url); return this.getRelays().some((relay) => relay.url === u && relay.read); } async fetchAllRelaysInfo(): Promise< Record<string, Nostr.Nip11.RelayInfo | null> > { const entries = await Promise.all( Array.from(this.connections.keys()).map( async (url): Promise<[string, Nostr.Nip11.RelayInfo | null]> => [ url, await fetchRelayInfo(url).catch(() => null), ] ) ); return Object.fromEntries(entries); } getAllRelayState(): Record<string, ConnectionState> { return Object.fromEntries( Array.from(this.connections.values()).map((e) => [ e.url, this.getRelayState(e.url), ]) ); } getRelayState(url: string): ConnectionState { const conn = this.connections.get(normalizeRelayUrl(url)); if (!conn) { throw new Error("RelayConfig not found"); } // this.relays[url] may be set before this.relays[url].websocket is initialized return conn?.getConnectionState() ?? "not-started"; } reconnect(url: string): void { if (this.canReadRelay(url)) { this.connections.get(normalizeRelayUrl(url))?.start(); } } setGlobalEventPacketPipe(pipe: MonoTypeOperatorFunction<EventPacket> | null) { this.globalEventPacketPipe = pipe; } use( rxReq: RxReq, options?: Partial<RxNostrUseOptions> ): Observable<EventPacket> { const { scope: _scope } = makeRxNostrUseOptions(options); const scope = _scope?.map(normalizeRelayUrl); const TIMEOUT = this.options.timeout; const strategy = rxReq.strategy; const rxReqId = rxReq.rxReqId; const message$ = this.messageOut$; const ongoings = this.ongoings; const getAllRelayState = this.getAllRelayState.bind(this); const createConnectionStateObservable = this.createConnectionStateObservable.bind(this); const ensureReq = this.ensureReq.bind(this); const finalizeReq = this.finalizeReq.bind(this); const subId$ = rxReq.getReqObservable().pipe( filter((filters): filters is LazyFilter[] => filters !== null), strategy === "oneshot" ? first() : identity, attachSubId(), strategy === "forward" ? manageActiveForwardReq() : identity, tap((req) => { ensureReq(req, { overwrite: strategy === "forward", scope }); }), map(([, subId]) => subId) ); if (strategy === "forward") { const subId = makeSubId({ rxReqId, }); const resource: Unsubscribable[] = []; const subject = new Subject<EventPacket>(); resource.push(subject); return subject.pipe( tap({ subscribe: () => { resource.push(subId$.subscribe()); resource.push( message$ .pipe(filterBySubId(subId), pickEvents()) .subscribe((v) => { subject.next(v); }) ); }, finalize: () => { for (const r of resource) { r.unsubscribe(); } finalizeReq({ subId }); }, }) ); } else { return subId$.pipe(map(createEoseManagedEventObservable), mergeAll()); } function attachSubId(
): OperatorFunction<LazyFilter[], LazyREQ> {
const makeId = (index?: number) => makeSubId({ rxReqId, index }); switch (strategy) { case "backward": return map((filters, index) => ["REQ", makeId(index), ...filters]); case "forward": case "oneshot": return map((filters) => ["REQ", makeId(), ...filters]); } } function manageActiveForwardReq(): MonoTypeOperatorFunction<LazyREQ> { const recordActiveReq = (req: LazyREQ) => { const subId = req[1]; ongoings.set(subId, { req, scope, }); }; const forgetActiveReq = (subId: string) => { ongoings.delete(subId); }; return tap({ next: (req: LazyREQ) => { recordActiveReq(req); }, finalize: () => { forgetActiveReq( makeSubId({ rxReqId, }) ); }, }); } function createEoseManagedEventObservable( subId: string ): Observable<EventPacket> { const eose$ = new Subject<void>(); const complete$ = new Subject<void>(); const eoseRelays = new Set<string>(); const manageCompletion = merge( eose$, createConnectionStateObservable() ).subscribe(() => { const status = getAllRelayState(); const shouldComplete = Object.entries(status).every( ([url, state]) => (scope && !scope.includes(url)) || state === "error" || state === "terminated" || (state === "ongoing" && eoseRelays.has(url)) ); if (shouldComplete) { complete$.next(); } }); return message$.pipe( takeUntil(complete$), completeOnTimeout(TIMEOUT), filterBySubId(subId), filter((e) => !eoseRelays.has(e.from)), tap((e) => { if (e.message[0] === "EOSE") { eoseRelays.add(e.from); finalizeReq({ subId, url: e.from }); eose$.next(); } }), pickEvents(), finalize(() => { finalizeReq({ subId }); complete$.unsubscribe(); eose$.unsubscribe(); manageCompletion.unsubscribe(); }) ); } function filterBySubId( subId: string ): MonoTypeOperatorFunction<MessagePacket> { return filter( (packet) => (packet.message[0] === "EVENT" || packet.message[0] === "EOSE") && packet.message[1] === subId ); } function pickEvents(): OperatorFunction<MessagePacket, EventPacket> { return mergeMap(({ from, message }) => message[0] === "EVENT" ? of({ from, subId: message[1], event: message[2] }) : EMPTY ); } } createAllEventObservable(): Observable<EventPacket> { return this.messageOut$.pipe( mergeMap(({ from, message }) => message[0] === "EVENT" ? of({ from, subId: message[1], event: message[2] }) : EMPTY ) ); } createAllErrorObservable(): Observable<ErrorPacket> { return this.error$.asObservable(); } createAllMessageObservable(): Observable<MessagePacket> { return this.messageOut$; } createConnectionStateObservable(): Observable<ConnectionStatePacket> { return this.status$.asObservable(); } send( params: Nostr.EventParameters, options?: RxNostrSendOptions ): Observable<OkPacket> { const { seckey, scope: _scope } = makeRxNostrSendOptions(options); const scope = _scope?.map(normalizeRelayUrl); const writableConns = Array.from(this.connections.values()).filter( (conn) => (!scope || scope.includes(conn.url)) && conn.write ); const subject = new ReplaySubject<OkPacket>(writableConns.length); let subscription: Subscription | null = null; getSignedEvent(params, seckey).then((event) => { if (!subject.closed) { subscription = this.createAllMessageObservable().subscribe( ({ from, message }) => { if (message[0] !== "OK") { return; } subject.next({ from, id: event.id, ok: message[2], }); } ); } for (const conn of writableConns) { conn.sendEVENT(["EVENT", event]); } }); return subject.pipe( take(writableConns.length), timeout(30 * 1000), finalize(() => { subject.complete(); subject.unsubscribe(); subscription?.unsubscribe(); }) ); } dispose(): void { this.disposed = true; this.messageIn$.complete(); this.error$.complete(); for (const conn of this.connections.values()) { conn.dispose(); } } private ensureReq( req: LazyREQ, options?: { scope?: string[] | null; overwrite?: boolean } ) { const scope = options?.scope; for (const url of this.connections.keys()) { const conn = this.connections.get(url); if (!conn || !conn.read || (scope && !scope.includes(url))) { continue; } conn.ensureReq(req, { overwrite: options?.overwrite }); } } private finalizeReq(params: { subId: string; url?: string }) { const { subId, url } = params; if (subId === undefined && url === undefined) { throw new Error(); } if (url) { const conn = this.connections.get(url); conn?.finalizeReq(subId); } else { for (const conn of this.connections.values()) { conn?.finalizeReq(subId); } } } } interface OngoingReq { req: LazyREQ; scope?: string[]; } function makeSubId(params: { rxReqId: string; index?: number }): string { return `${params.rxReqId}:${params.index ?? 0}`; }
src/rx-nostr.ts
penpenpng-rx-nostr-7c52614
[ { "filename": "src/connection.ts", "retrieved_chunk": " finalizeReq(subId: string) {\n if (this.connectionState === \"terminated\") {\n return;\n }\n const req = this.reqs.get(subId);\n if (!req) {\n return;\n }\n this.reqs.delete(subId);\n if (req.isOngoing) {", "score": 13.225700895307842 }, { "filename": "src/connection.ts", "retrieved_chunk": "function evalREQ([type, subId, ...filters]: LazyREQ): Nostr.ToRelayMessage.REQ {\n return [type, subId, ...evalFilters(filters)];\n}\nfunction isConcurrentAllowed(\n subId: string,\n reqs: Map<string, ReqState>,\n concurrent: number | null\n): boolean {\n if (concurrent === null) {\n return true;", "score": 12.719107826791149 }, { "filename": "src/operator.ts", "retrieved_chunk": " mergeAll,\n mergeMap,\n type MonoTypeOperatorFunction,\n type ObservableInput,\n of,\n type OperatorFunction,\n pipe,\n scan,\n tap,\n timeout,", "score": 12.185017935677052 }, { "filename": "src/operator.ts", "retrieved_chunk": "): MonoTypeOperatorFunction<EventPacket> {\n return pipe(groupBy(key), map(pipe(latest())), mergeAll());\n}\n/**\n * Only events with a valid signature are allowed to pass.\n */\nexport function verify(): MonoTypeOperatorFunction<EventPacket> {\n return filter<EventPacket>(({ event }) => _verify(event));\n}\n/**", "score": 11.89396561501368 }, { "filename": "src/helper.ts", "retrieved_chunk": " return filters.map(evalFilter);\n } else {\n return [evalFilter(filters)];\n }\n}\nfunction evalFilter(filter: LazyFilter): Nostr.Filter {\n return {\n ...filter,\n since: filter.since ? evalLazyNumber(filter.since) : undefined,\n until: filter.until ? evalLazyNumber(filter.until) : undefined,", "score": 10.069362557077056 } ]
typescript
): OperatorFunction<LazyFilter[], LazyREQ> {
import { OperandImm, OperandMem, OperandPtr, OperandReg } from "./common.js"; import { BranchType, BroadcastMode, ConversionMode, DecoderMode, ElementType, ExceptionClass, ISAExt, ISASet, InsnCategory, InstructionEncoding, MachineMode, MaskMode, MemoryOperandType, Mnemonic, OpcodeMap, OperandAction, OperandEncoding, OperandType, OperandVisibility, Register, RoundingMode, StackWidth, SwizzleMode, } from "./enums.js"; import zydis from "./native.js"; import { Resource, ZyjsField, withStack } from "./util.js"; const OpField = ZyjsField("DecOp"); export class DecodedOperand { resource: Resource; constructor(rsrc: Resource) { this.resource = rsrc; } @OpField id!: number; @OpField visiblity!: OperandVisibility; @OpField actions!: OperandAction; @OpField encoding!: OperandEncoding; @OpField size!: number; @OpField elementType!: ElementType; @OpField elementSize!: number; @OpField elementCount!: number; @OpField attributes!: number; @OpField type!: OperandType; @OpField protected immSigned!: number; @OpField protected immRelative!: number; @OpField protected imms!: bigint; @OpField protected immu!: bigint; @OpField protected ptrSeg!: number; @OpField protected ptrOff!: number; @OpField protected regv!: Register; @OpField protected memType!: MemoryOperandType; @OpField protected memSeg!: Register; @OpField protected memBase!: Register; @OpField protected memIndex!: Register; @OpField protected memScale!: Register; @OpField protected memHasDisp!: number; @OpField protected memDisp!: bigint; get imm(): OperandImm { if (this.type !== OperandType.IMMEDIATE) { throw TypeError("Operand type mismatch."); } if (this.immSigned) { return { s: this.imms, rel: !!this.immRelative, }; } else { return { u: this.immu, rel: !!this.immRelative, }; } } get reg(): OperandReg { if (this.type !== OperandType.REGISTER) { throw TypeError("Operand type mismatch."); } return { name: this.regv, is4: this.encoding === OperandEncoding.IS4, }; } get ptr(): OperandPtr { if (this.type !== OperandType.POINTER) { throw TypeError("Operand type mismatch."); } return { segv: this.ptrSeg, off: this.ptrOff, }; } get mem(): OperandMem { if (this.type !== OperandType.MEMORY) { throw TypeError("Operand type mismatch."); } const res: OperandMem = { type: this.memType, seg: this.memSeg, index: this.memIndex, base: this.memBase, scale: this.memScale, disp: this.memHasDisp ? this.memDisp : undefined, }; if (res.seg === Register.NONE) { delete res.seg; } if (res.base === Register.NONE) { delete res.base; } if (res.scale === 0 || res.index === Register.NONE) { delete res.index; delete res.scale; } return res; } get() { switch (this.type) { case OperandType.IMMEDIATE: return this.imm; case OperandType.REGISTER: return this.reg; case OperandType.POINTER: return this.ptr; case OperandType.MEMORY: return this.mem; default: throw Error(); } } } const FlagField = ZyjsField("FlagSet"); interface AccessedFlags { readonly tested: number; readonly modified: number; readonly set: number; readonly unset: number; readonly undef: number; } const flagsNoop: AccessedFlags = { tested: 0, modified: 0, set: 0, unset: 0, undef: 0, }; class AccessedFlagsByPtr implements AccessedFlags { resource: { ref(): number }; constructor(ptr: number) { this.resource = { ref() { return ptr; }, }; } @FlagField readonly tested!: number; @FlagField readonly modified!: number; @FlagField readonly set!: number; @FlagField readonly unset!: number; @FlagField readonly undef!: number; } const InsnField = ZyjsField("DecInsn"); export class DecodedInsn { resource: Resource; constructor(rsrc: Resource) { this.resource = rsrc; } // Common information. // @InsnField length!: number; @InsnField mnemonic!: Mnemonic; @InsnField encoding!: InstructionEncoding; @InsnField opcodeMap!: OpcodeMap; @InsnField opcode!: number; @InsnField stackWidth!: number; @InsnField operandWidth!: number; @InsnField addressWidth!: number; @InsnField operandCount!: number; @InsnField visibleOperandCount!: number; @InsnField attributes!: bigint; // AVX. // @InsnField vectorLength!: number; @InsnField maskMode!: MaskMode; @InsnField maskReg!: Register; @InsnField isBroadcastStatic!: 1 | 0; @InsnField broadcastMode!: BroadcastMode; @InsnField roundingMode!: RoundingMode; @InsnField swizzleMode!: SwizzleMode; @InsnField conversionMode!: ConversionMode; @InsnField hasSAE!: 1 | 0; @InsnField hasEvictionHint!: 1 | 0; // Meta. // @InsnField category!: InsnCategory; @InsnField isaSet!: ISASet; @InsnField isaExt!: ISAExt; @InsnField branchType!: BranchType; @InsnField exceptionClass!: ExceptionClass; // Flags. // @InsnField private cpuFlagsPtr!: number; @InsnField private fpuFlagsPtr!: number; get cpuFlags(): AccessedFlags { const ptr = this.cpuFlagsPtr; return ptr ? new AccessedFlagsByPtr(ptr) : flagsNoop; } get fpuFlags(): AccessedFlags { const ptr = this.fpuFlagsPtr; return ptr ? new AccessedFlagsByPtr(ptr) : flagsNoop; } operand(n: number): DecodedOperand { const ptr = zydis.asm.zyjsDecInsnRefOperand(this.resource.ref(), n); if (!ptr) { throw RangeError("Operand out of boundaries."); } return new DecodedOperand(this.resource.subresource(ptr)); } } export class Decoder { resource: Resource; constructor(mode: MachineMode, width: StackWidth) { this.resource = new Resource(zydis.asm.zyjsNewDecoder(mode, width)); } set(mode: DecoderMode, value: boolean) { zydis.asm.zyjsDecoderSetMode(this.resource.ref(), mode, value ? 1 : 0); return this; } decode(buffer: Uint8Array) { const ptr = withStack(
(a) => {
return zydis.asm.zyjsDecoderDecode(this.resource.ref(), a.buf(buffer), buffer.length); }); return new DecodedInsn(new Resource(ptr)); } }
src/decoder.ts
zyantific-zydis-js-4715bb8
[ { "filename": "src/encoder.ts", "retrieved_chunk": "\t}\n}\nconst ReqField = ZyjsField(\"EncReq\");\nexport class EncoderRequest {\n\tresource: Resource;\n\tconstructor(from?: DecodedInsn) {\n\t\tthis.resource = new Resource(zydis.asm.zyjsNewEncReq(from?.resource.ref() ?? 0));\n\t}\n\tencode(at?: bigint) {\n\t\treturn withStack((alloc) => {", "score": 47.45519616989094 }, { "filename": "src/formatter.ts", "retrieved_chunk": "\tproperty(prop: FormatterProperty, value: boolean | number) {\n\t\tzydis.asm.zyjsFormatterSetProperty(this.resource.ref(), prop, +value);\n\t\treturn this;\n\t}\n\tinsn(value: DecodedInsn, address: bigint = 0n) {\n\t\treturn withStack((alloc) => {\n\t\t\tconst buf = alloc.allocate(MAX_LENGTH);\n\t\t\tzydis.asm.zyjsFormatterFormatInsn(this.resource.ref(), buf, MAX_LENGTH, value.resource.ref(), address);\n\t\t\treturn zydis.UTF8ToString(buf, MAX_LENGTH);\n\t\t});", "score": 47.21107750621104 }, { "filename": "src/encoder.ts", "retrieved_chunk": "\t\t\tconst tmp = alloc.allocate(MAX_INSN_LENGTH);\n\t\t\tconst length =\n\t\t\t\tat != null\n\t\t\t\t\t? zydis.asm.zyjsEncReqEncodeAbs(this.resource.ref(), tmp, at)\n\t\t\t\t\t: zydis.asm.zyjsEncReqEncode(this.resource.ref(), tmp);\n\t\t\treturn zydis.HEAPU8.slice(tmp, tmp + length);\n\t\t});\n\t}\n\t@ReqField\n\tmachineMode!: MachineMode;", "score": 40.08572343805137 }, { "filename": "src/util.ts", "retrieved_chunk": "}\nexport function ZyjsField(ns: string) {\n\treturn function (target: any, propertyKey: any) {\n\t\tconst getter = zydis.asm[`zyjs${ns}Get_${propertyKey}`];\n\t\tconst setter = zydis.asm[`zyjs${ns}Set_${propertyKey}`];\n\t\tObject.defineProperty(target, propertyKey, {\n\t\t\tget(this: { resource: { ref(): number } }) {\n\t\t\t\treturn getter(this.resource.ref());\n\t\t\t},\n\t\t\tset(this: { resource: { ref(): number } }, value: any) {", "score": 39.07109539182882 }, { "filename": "src/register.ts", "retrieved_chunk": "export function getClass(reg: Register): RegisterClass {\n\treturn zydis.asm.zyjsRegisterGetClass(reg);\n}\nexport function getWidth(reg: Register, mode: MachineMode): number {\n\treturn zydis.asm.zyjsRegisterGetWidth(reg, mode);\n}\nexport function getWidthFromClass(cl: RegisterClass, mode: MachineMode): number {\n\treturn zydis.asm.zyjsRegisterClassGetWidth(cl, mode);\n}\nexport function getLargestEnclosing(reg: Register, mode: MachineMode): Register {", "score": 38.707756004147626 } ]
typescript
(a) => {
import { NativeModules } from 'react-native'; import { PitchDetectorErrors } from '../../../types'; import { PitchDetectorError } from '../../erros'; import { Permissions } from '../../permissions'; import { PitchDetector } from '..'; const Module = NativeModules.PitchDetectorModule; const asyncMock = <T>(value: T) => jest.fn().mockResolvedValue(value); const asyncMockThrow = <T>(error: T) => jest.fn().mockImplementation(() => { throw error; }); describe('PitchDetector', () => { beforeEach(() => jest.clearAllMocks()); it.each([ ['start', 1], ['isRecording', 1], ['stop', 1], ])( 'should call %s method %s time(s) from native module', async (method: string, times: number) => { const spy = jest.spyOn(Module, method as any); await Object(PitchDetector)[method](); expect(spy).toBeCalledTimes(times); } ); it.each([ ['hasPermissions', 'start'], ['getDefaultConfig', 'start'], ])( 'should call %s method when %s method will be called', async (target: string, method: string) => { const spy = jest.spyOn(PitchDetector, target as any); await Object(PitchDetector)[method](); expect(spy).toBeCalledTimes(1); } ); it('should call audio permission method when start method will be called', async () => { const spy = jest.spyOn(Permissions, 'audio'); await PitchDetector.start(); expect(spy).toBeCalledTimes(1); }); it('should throw error when start method will be called and not have audio record permission', async () => {
const error = new PitchDetectorError(PitchDetectorErrors.PERMISSIONS_ERROR);
const spy = jest.spyOn(console, 'warn'); Permissions.audio = asyncMock(false); await PitchDetector.start(); expect(spy).toBeCalledTimes(1); expect(spy).toHaveBeenCalledWith(error); }); it.each([ ['addListener', 'addListener'], ['removeAllListeners', 'removeAllListeners'], ['removeAllListeners', 'removeListener'], ])( 'should call %s method from event emitter when %s method will be called', async (target: string, method: string) => { const spy = jest.spyOn(Object(PitchDetector).event, target as any); await Object(PitchDetector)[method](1); expect(spy).toBeCalledTimes(1); } ); it.each([['start'], ['stop'], ['isRecording']])( 'should throw error when native %s method fail', async (method: string) => { const error = new Error('Some error message'); const spy = jest.spyOn(console, 'warn'); Permissions.audio = asyncMock(true); Object(PitchDetector).module[method] = asyncMockThrow(error); await Object(PitchDetector)[method](); expect(spy).toBeCalledTimes(1); expect(spy).toBeCalledWith(error); } ); });
src/internal/pitch-detector/__tests__/pitch-detector.spec.ts
1fabiopereira-react-native-pitch-detector-b70fc00
[ { "filename": "src/internal/erros/__tests__/errors.spec.ts", "retrieved_chunk": " it('should create link error', () => {\n const error: any = new PitchDetectorError(\n PitchDetectorErrors.LINKING_ERROR\n );\n expect(error?.message).toMatch(/doesn't seem to be linked/);\n });\n it('should create permission error', () => {\n const error: any = new PitchDetectorError(\n PitchDetectorErrors.PERMISSIONS_ERROR\n );", "score": 38.2546623979204 }, { "filename": "src/internal/utils/__tests__/utils.spec.ts", "retrieved_chunk": " 'should return %s when isObject function will be called with %s',\n (expected: boolean, _: string, params: any) => {\n const result = isObject(params);\n expect(result).toBe(expected);\n }\n );\n it('should not merge when one params is not object', () => {\n const result = merge({ name: 'Fábio' }, NaN);\n expect(result).toHaveProperty('name');\n });", "score": 37.72168003522165 }, { "filename": "src/internal/pitch-detector/index.ts", "retrieved_chunk": " * @returns Promise<void>\n */\n async start(config?: PitchDetectorConfig): Promise<void> {\n try {\n const permission = await this.hasPermissions();\n if (!permission) {\n throw new PitchDetectorError(PitchDetectorErrors.PERMISSIONS_ERROR);\n }\n const configuration = merge<PitchDetectorConfig>(\n this.getDefaultConfig(),", "score": 29.129851604854313 }, { "filename": "src/internal/erros/__tests__/errors.spec.ts", "retrieved_chunk": " expect(error?.message).toMatch(/need audio record permission/);\n });\n});", "score": 26.38496846397943 }, { "filename": "src/internal/erros/__tests__/errors.spec.ts", "retrieved_chunk": "import { PitchDetectorErrors } from '../../../types';\nimport { PitchDetectorError } from '..';\ndescribe('Errors', () => {\n it('should create base error', () => {\n const error: any = new PitchDetectorError(PitchDetectorErrors.BASE);\n expect(error?.message).toMatch(/You are not using Expo Go/);\n expect(error?.message).toMatch(\n /You rebuilt the app after installing the package/\n );\n });", "score": 24.864491894027218 } ]
typescript
const error = new PitchDetectorError(PitchDetectorErrors.PERMISSIONS_ERROR);
import { OperandImm, OperandMem, OperandPtr, OperandReg } from "./common.js"; import { BranchType, BroadcastMode, ConversionMode, DecoderMode, ElementType, ExceptionClass, ISAExt, ISASet, InsnCategory, InstructionEncoding, MachineMode, MaskMode, MemoryOperandType, Mnemonic, OpcodeMap, OperandAction, OperandEncoding, OperandType, OperandVisibility, Register, RoundingMode, StackWidth, SwizzleMode, } from "./enums.js"; import zydis from "./native.js"; import { Resource, ZyjsField, withStack } from "./util.js"; const OpField = ZyjsField("DecOp"); export class DecodedOperand { resource: Resource; constructor(rsrc: Resource) { this.resource = rsrc; } @OpField id!: number; @OpField visiblity!: OperandVisibility; @OpField actions!: OperandAction; @OpField encoding!: OperandEncoding; @OpField size!: number; @OpField elementType!: ElementType; @OpField elementSize!: number; @OpField elementCount!: number; @OpField attributes!: number; @OpField type!: OperandType; @OpField protected immSigned!: number; @OpField protected immRelative!: number; @OpField protected imms!: bigint; @OpField protected immu!: bigint; @OpField protected ptrSeg!: number; @OpField protected ptrOff!: number; @OpField protected regv!: Register; @OpField protected memType!: MemoryOperandType; @OpField protected memSeg!: Register; @OpField protected memBase!: Register; @OpField protected memIndex!: Register; @OpField protected memScale!: Register; @OpField protected memHasDisp!: number; @OpField protected memDisp!: bigint; get imm(): OperandImm { if (this.type !== OperandType.IMMEDIATE) { throw TypeError("Operand type mismatch."); } if (this.immSigned) { return { s: this.imms, rel: !!this.immRelative, }; } else { return { u: this.immu, rel: !!this.immRelative, }; } } get reg(): OperandReg { if (this.type !== OperandType.REGISTER) { throw TypeError("Operand type mismatch."); } return { name: this.regv, is4: this.encoding === OperandEncoding.IS4, }; } get ptr(): OperandPtr { if (this.type !== OperandType.POINTER) { throw TypeError("Operand type mismatch."); } return { segv: this.ptrSeg, off: this.ptrOff, }; } get mem(): OperandMem { if (this.type !== OperandType.MEMORY) { throw TypeError("Operand type mismatch."); } const res: OperandMem = { type: this.memType, seg: this.memSeg, index: this.memIndex, base: this.memBase, scale: this.memScale, disp: this.memHasDisp ? this.memDisp : undefined, }; if (res.seg === Register.NONE) { delete res.seg; } if (res.base === Register.NONE) { delete res.base; } if (res.scale === 0 || res.index === Register.NONE) { delete res.index; delete res.scale; } return res; } get() { switch (this.type) { case OperandType.IMMEDIATE: return this.imm; case OperandType.REGISTER: return this.reg; case OperandType.POINTER: return this.ptr; case OperandType.MEMORY: return this.mem; default: throw Error(); } } } const FlagField = ZyjsField("FlagSet"); interface AccessedFlags { readonly tested: number; readonly modified: number; readonly set: number; readonly unset: number; readonly undef: number; } const flagsNoop: AccessedFlags = { tested: 0, modified: 0, set: 0, unset: 0, undef: 0, }; class AccessedFlagsByPtr implements AccessedFlags { resource: { ref(): number }; constructor(ptr: number) { this.resource = { ref() { return ptr; }, }; } @FlagField readonly tested!: number; @FlagField readonly modified!: number; @FlagField readonly set!: number; @FlagField readonly unset!: number; @FlagField readonly undef!: number; } const InsnField = ZyjsField("DecInsn"); export class DecodedInsn { resource: Resource; constructor(rsrc: Resource) { this.resource = rsrc; } // Common information. // @InsnField length!: number; @InsnField mnemonic!: Mnemonic; @InsnField encoding!: InstructionEncoding; @InsnField opcodeMap!: OpcodeMap; @InsnField opcode!: number; @InsnField stackWidth!: number; @InsnField operandWidth!: number; @InsnField addressWidth!: number; @InsnField operandCount!: number; @InsnField visibleOperandCount!: number; @InsnField attributes!: bigint; // AVX. // @InsnField vectorLength!: number; @InsnField maskMode!: MaskMode; @InsnField maskReg!: Register; @InsnField isBroadcastStatic!: 1 | 0; @InsnField broadcastMode!: BroadcastMode; @InsnField roundingMode!: RoundingMode; @InsnField swizzleMode!: SwizzleMode; @InsnField conversionMode!: ConversionMode; @InsnField hasSAE!: 1 | 0; @InsnField hasEvictionHint!: 1 | 0; // Meta. // @InsnField category!: InsnCategory; @InsnField isaSet!: ISASet; @InsnField isaExt!: ISAExt; @InsnField branchType!: BranchType; @InsnField exceptionClass!: ExceptionClass; // Flags. // @InsnField private cpuFlagsPtr!: number; @InsnField private fpuFlagsPtr!: number; get cpuFlags(): AccessedFlags { const ptr = this.cpuFlagsPtr; return ptr ? new AccessedFlagsByPtr(ptr) : flagsNoop; } get fpuFlags(): AccessedFlags { const ptr = this.fpuFlagsPtr; return ptr ? new AccessedFlagsByPtr(ptr) : flagsNoop; } operand(n: number): DecodedOperand {
const ptr = zydis.asm.zyjsDecInsnRefOperand(this.resource.ref(), n);
if (!ptr) { throw RangeError("Operand out of boundaries."); } return new DecodedOperand(this.resource.subresource(ptr)); } } export class Decoder { resource: Resource; constructor(mode: MachineMode, width: StackWidth) { this.resource = new Resource(zydis.asm.zyjsNewDecoder(mode, width)); } set(mode: DecoderMode, value: boolean) { zydis.asm.zyjsDecoderSetMode(this.resource.ref(), mode, value ? 1 : 0); return this; } decode(buffer: Uint8Array) { const ptr = withStack((a) => { return zydis.asm.zyjsDecoderDecode(this.resource.ref(), a.buf(buffer), buffer.length); }); return new DecodedInsn(new Resource(ptr)); } }
src/decoder.ts
zyantific-zydis-js-4715bb8
[ { "filename": "src/util.ts", "retrieved_chunk": "\t\tif (this.#ptr) {\n\t\t\tResource.finalizer.register(this, this.#ptr, this.#token);\n\t\t}\n\t}\n\tsubresource(ptr: number) {\n\t\tconst res = new Resource(0);\n\t\tres.#parent = this;\n\t\tres.#ptr = ptr;\n\t\treturn res;\n\t}", "score": 70.06633134547498 }, { "filename": "src/util.ts", "retrieved_chunk": "\t\tzydis.stringToUTF8(s, ptr, Infinity);\n\t\treturn ptr;\n\t}\n\tbuf(input: ArrayLike<number>): number {\n\t\tconst ptr = this.allocate(input.length);\n\t\tzydis.HEAPU8.set(input, ptr);\n\t\treturn ptr;\n\t}\n}\nexport const StackAllocator = new (class StackAllocator extends IAllocator {", "score": 69.0289002498906 }, { "filename": "src/util.ts", "retrieved_chunk": "\tref() {\n\t\treturn this.#ptr;\n\t}\n\tunref() {\n\t\tif (this.#ptr && !this.#parent) {\n\t\t\tHeapAllocator.free(this.#ptr);\n\t\t\tthis.#ptr = 0;\n\t\t\tResource.finalizer.unregister(this.#token);\n\t\t}\n\t}", "score": 60.58392362097876 }, { "filename": "src/encoder.ts", "retrieved_chunk": "\t@ReqField\n\tmvexConversion!: ConversionMode;\n\t@ReqField\n\tmvexSwizzle!: SwizzleMode;\n\t@ReqField\n\tmvexEvictionHint!: 1 | 0;\n\toperand(n: number): EncoderOperand {\n\t\tconst ptr = zydis.asm.zyjsEncReqRefOperand(this.resource.ref(), n);\n\t\tif (!ptr) {\n\t\t\tthrow RangeError(\"Operand out of boundaries.\");", "score": 60.330428923482636 }, { "filename": "src/util.ts", "retrieved_chunk": "\t\tzydis.asm.free(p);\n\t}\n})();\nexport class Resource {\n\tstatic readonly finalizer = new FinalizationRegistry((heldValue) => {});\n\t#parent?: Resource;\n\t#ptr: number;\n\treadonly #token = {};\n\tconstructor(ptr: number) {\n\t\tthis.#ptr = ptr;", "score": 59.75860027530673 } ]
typescript
const ptr = zydis.asm.zyjsDecInsnRefOperand(this.resource.ref(), n);
import { NativeModules, NativeEventEmitter, Platform } from 'react-native'; import { Permissions } from '../permissions'; import { PitchDetectorError } from '../erros'; import { merge } from '../utils'; import { type Callback, type NativeModuleImplementation, type PitchDetectorConfig, type Subscription, type PitchDetectorAndroidConfig, type PitchDetectorIOSConfig, PitchDetectorErrors, } from '../../types'; export class InternalPitchDetector { private module?: NativeModuleImplementation; private event?: NativeEventEmitter; constructor() { this.module = NativeModules?.PitchDetectorModule; if (this.module) { this.event = new NativeEventEmitter(this.module); } else { /* istanbul ignore next */ throw new PitchDetectorError(PitchDetectorErrors.LINKING_ERROR); } } /** * Returns a default PitchDetector configs * @returns PitchDetectorConfig * @example * ```ts * { * android: { * algorithm: 'YIN', * bufferOverLap: 0, * bufferSize: 1024, * sampleRate: 22050, * }, * ios: { * algorithm: 'YIN', * bufferSize: 1024, * } * } */ private getDefaultConfig(): PitchDetectorConfig { return { android: { algorithm: 'YIN', bufferOverLap: 0, bufferSize: 1024, sampleRate: 22050, }, ios: { algorithm: 'YIN', bufferSize: 1024, }, }; } /** * Get current audio permission * @returns Promise<boolean> */ private async hasPermissions(): Promise<boolean> { return !!
(await Permissions.audio());
} /** * Get current status * @returns Promise<boolean> */ async isRecording(): Promise<boolean> { try { const status = await this.module?.isRecording(); return !!status; } catch (err) { console.warn(err); return false; } } /** * Trigger audio recording and pitch detection with provided configs * @param config * @returns Promise<void> */ async start(config?: PitchDetectorConfig): Promise<void> { try { const permission = await this.hasPermissions(); if (!permission) { throw new PitchDetectorError(PitchDetectorErrors.PERMISSIONS_ERROR); } const configuration = merge<PitchDetectorConfig>( this.getDefaultConfig(), config ?? {} ); const params = Platform.select({ android: configuration.android as unknown, ios: configuration.ios as unknown, }) as PitchDetectorIOSConfig | PitchDetectorAndroidConfig; await this.module?.start(params); } catch (err: unknown) { console.warn(err); } } /** * Stop audio recording and pitch detection * @returns Promise<void> */ async stop(): Promise<void> { try { await this.module?.stop(); } catch (err: unknown) { console.warn(err); } } /** * Register a event listener */ addListener(callback: Callback): Subscription { return this.event?.addListener('data', callback); } /** * Method event listeners * @alias removeAllListeners */ removeListener(): void { this.event?.removeAllListeners('data'); } /** * Method remove all event listeners */ removeAllListeners(): void { this.event?.removeAllListeners('data'); } } /** * Export an instance of InternalPitchDetector */ export const PitchDetector = new InternalPitchDetector();
src/internal/pitch-detector/index.ts
1fabiopereira-react-native-pitch-detector-b70fc00
[ { "filename": "src/types/index.ts", "retrieved_chunk": " * Stop audio recording and pitch detection\n * @returns Promise<void>\n */\n stop: () => Promise<void>;\n /**\n * Get current status\n * @returns Promise<boolean>\n */\n isRecording: () => Promise<void>;\n}", "score": 27.27789324058112 }, { "filename": "src/types/index.ts", "retrieved_chunk": "export type Response = Promise<PermissionStatus | boolean | null>;\n/**\n * Permission handler\n */\nexport type PermissionsHandlers = {\n /**\n * Request permission\n * @returns Promise<PermissionStatus | boolean | null>\n */\n RequestPermission: (rationale?: Rationale, compare?: Comparable) => Response;", "score": 16.130338689772763 }, { "filename": "src/types/index.ts", "retrieved_chunk": " /**\n * Check permission\n * @returns Promise<PermissionStatus | boolean | null>\n */\n CheckPermission: (compare?: Comparable) => Response;\n};\nexport enum PitchDetectorErrors {\n BASE,\n LINKING_ERROR,\n PERMISSIONS_ERROR,", "score": 14.56439907965408 }, { "filename": "src/types/index.ts", "retrieved_chunk": "export interface NativeModuleImplementation extends NativeModule {\n /**\n * Start audio recording and pitch detection with provided configs\n * @param config\n * @returns Promise<void>\n */\n start: (\n config: PitchDetectorAndroidConfig | PitchDetectorIOSConfig\n ) => Promise<void>;\n /**", "score": 12.052737228062485 }, { "filename": "src/internal/permissions/index.ts", "retrieved_chunk": " const denied = await audio.CheckPermission(isDenied);\n const blocked = await audio.CheckPermission(isBlocked);\n if (denied || blocked) {\n return await audio.RequestPermission(rationale, isGranted);\n }\n return await audio.CheckPermission(isGranted);\n }\n}", "score": 9.968566703665905 } ]
typescript
(await Permissions.audio());
import { DecodedInsn } from "./decoder.js"; import { Resource, ZyjsField, withHeap, withStack } from "./util.js"; import zydis from "./native.js"; import { AddressSizeHint, BranchType, BranchWidth, BroadcastMode, ConversionMode, EncodableEncoding, InsnAttribute, MAX_INSN_LENGTH, MachineMode, Mnemonic, OperandSizeHint, OperandType, Register, RoundingMode, SwizzleMode, } from "./enums.js"; import { OperandImm, OperandMem, OperandPtr, OperandReg } from "./common.js"; const OpField = ZyjsField("EncOp"); type OperandLike = keyof typeof Register | number | bigint | OperandImm | OperandReg | OperandPtr | OperandMem; export class EncoderOperand { resource: Resource; constructor(rsrc: Resource) { this.resource = rsrc; } @OpField type!: OperandType; @OpField imms!: bigint; @OpField immu!: bigint; @OpField ptrSeg!: number; @OpField ptrOff!: number; @OpField regv!: Register; @OpField regIs4!: 1 | 0; @OpField memBase!: Register; @OpField memIndex!: Register; @OpField memScale!: Register; @OpField memDisp!: bigint; get imm(): OperandImm { if (this.type !== OperandType.IMMEDIATE) { throw TypeError("Operand type mismatch."); } return { s: this.imms, u: this.immu, }; } set imm(o: OperandImm | bigint | number) { this.type = OperandType.IMMEDIATE; if (typeof o === "object") { if ("s" in o) { this.imms = o.s; } else { this.immu = o.u; } } else { this.imms = BigInt(o); } } get reg(): OperandReg { if (this.type !== OperandType.REGISTER) { throw TypeError("Operand type mismatch."); } return { name: this.regv, is4: !!this.regIs4, }; } set reg(o: OperandReg | Register | keyof typeof Register) { this.type = OperandType.REGISTER; if (typeof o === "string") { this.regv = Register[o]; } else if (typeof o === "number") { this.regv = o; } else { this.regv = o.name; this.regIs4 = o.is4 || false ? 1 : 0; } } get ptr(): OperandPtr { if (this.type !== OperandType.POINTER) { throw TypeError("Operand type mismatch."); } return { segv: this.ptrSeg, off: this.ptrOff, }; } set ptr(o:
OperandPtr) {
this.type = OperandType.POINTER; this.ptrOff = o.off; this.ptrSeg = o.segv; } get mem(): OperandMem { const res: OperandMem = { index: this.memIndex, base: this.memBase, scale: this.memScale, disp: this.memDisp, }; if (res.seg === Register.NONE) { delete res.seg; } if (res.base === Register.NONE) { delete res.base; } if (res.scale === 0 || res.index === Register.NONE) { delete res.index; delete res.scale; } return res; } set mem(o: OperandMem) { this.type = OperandType.MEMORY; this.memBase = o.base ?? Register.NONE; this.memDisp = o.disp ?? 0n; this.memIndex = o.index ?? Register.NONE; this.memScale = o.scale ?? 0; } get() { switch (this.type) { case OperandType.IMMEDIATE: return this.imm; case OperandType.REGISTER: return this.reg; case OperandType.POINTER: return this.ptr; case OperandType.MEMORY: return this.mem; default: throw Error(); } } set(o: OperandLike) { if (typeof o === "number" || typeof o === "bigint") { return void (this.imm = o); } else if (typeof o === "string") { return void (this.reg = o); } // OperandImm if ("s" in o || "u" in o) { this.imm = o; } // OperandReg. // else if ("name" in o) { this.reg = o; } // OperandPtr. // else if ("off" in o) { this.ptr = o; } // OperandMem. // else { this.mem = o; } } } const ReqField = ZyjsField("EncReq"); export class EncoderRequest { resource: Resource; constructor(from?: DecodedInsn) { this.resource = new Resource(zydis.asm.zyjsNewEncReq(from?.resource.ref() ?? 0)); } encode(at?: bigint) { return withStack((alloc) => { const tmp = alloc.allocate(MAX_INSN_LENGTH); const length = at != null ? zydis.asm.zyjsEncReqEncodeAbs(this.resource.ref(), tmp, at) : zydis.asm.zyjsEncReqEncode(this.resource.ref(), tmp); return zydis.HEAPU8.slice(tmp, tmp + length); }); } @ReqField machineMode!: MachineMode; @ReqField allowedEncodings!: EncodableEncoding; @ReqField mnemonic!: Mnemonic; @ReqField prefixes!: bigint; @ReqField branchType!: BranchType; @ReqField branchWidth!: BranchWidth; @ReqField addressSizeHint!: AddressSizeHint; @ReqField operandSizeHint!: OperandSizeHint; @ReqField operandCount!: number; @ReqField evexBroadcast!: BroadcastMode; @ReqField evexRounding!: RoundingMode; @ReqField evexSAE!: 1 | 0; @ReqField evexZeroingMask!: 1 | 0; @ReqField mvexBroadcast!: BroadcastMode; @ReqField mvexRounding!: RoundingMode; @ReqField mvexSAE!: 1 | 0; @ReqField mvexConversion!: ConversionMode; @ReqField mvexSwizzle!: SwizzleMode; @ReqField mvexEvictionHint!: 1 | 0; operand(n: number): EncoderOperand { const ptr = zydis.asm.zyjsEncReqRefOperand(this.resource.ref(), n); if (!ptr) { throw RangeError("Operand out of boundaries."); } return new EncoderOperand(this.resource.subresource(ptr)); } static from(machine: MachineMode, mnemonic: Mnemonic, ...operands: OperandLike[]) { const res = new EncoderRequest(); res.mnemonic = mnemonic; res.operandCount = operands.length; res.machineMode = machine; for (let n = 0; n !== operands.length; n++) { const op = operands[n]; res.operand(n).set(op); if (typeof op === "object" && "seg" in op) { switch (op.seg) { case Register.CS: res.prefixes = InsnAttribute.HAS_SEGMENT_CS; break; case Register.DS: res.prefixes = InsnAttribute.HAS_SEGMENT_DS; break; case Register.ES: res.prefixes = InsnAttribute.HAS_SEGMENT_ES; break; case Register.GS: res.prefixes = InsnAttribute.HAS_SEGMENT_GS; break; case Register.FS: res.prefixes = InsnAttribute.HAS_SEGMENT_FS; break; case Register.SS: res.prefixes = InsnAttribute.HAS_SEGMENT_SS; break; } } } return res; } } export function encode(machine: MachineMode, mnemonic: Mnemonic, ...operands: OperandLike[]) { return EncoderRequest.from(machine, mnemonic, ...operands).encode(); } export function encodeAt(at: bigint, machine: MachineMode, mnemonic: Mnemonic, ...operands: OperandLike[]) { return EncoderRequest.from(machine, mnemonic, ...operands).encode(at); } export function encodeNop(len: number) { return (len <= 512 ? withStack : withHeap)((alloc) => { const tmp = alloc.allocate(MAX_INSN_LENGTH); zydis.asm.zyjsEncNop(tmp, len); const res = zydis.HEAPU8.slice(tmp, tmp + len); alloc.free(tmp); return res; }); }
src/encoder.ts
zyantific-zydis-js-4715bb8
[ { "filename": "src/decoder.ts", "retrieved_chunk": "\t\t}\n\t\treturn {\n\t\t\tsegv: this.ptrSeg,\n\t\t\toff: this.ptrOff,\n\t\t};\n\t}\n\tget mem(): OperandMem {\n\t\tif (this.type !== OperandType.MEMORY) {\n\t\t\tthrow TypeError(\"Operand type mismatch.\");\n\t\t}", "score": 62.75500819124927 }, { "filename": "src/decoder.ts", "retrieved_chunk": "\t\t\tthrow TypeError(\"Operand type mismatch.\");\n\t\t}\n\t\treturn {\n\t\t\tname: this.regv,\n\t\t\tis4: this.encoding === OperandEncoding.IS4,\n\t\t};\n\t}\n\tget ptr(): OperandPtr {\n\t\tif (this.type !== OperandType.POINTER) {\n\t\t\tthrow TypeError(\"Operand type mismatch.\");", "score": 57.396545170206416 }, { "filename": "src/decoder.ts", "retrieved_chunk": "\t@OpField\n\tprotected memDisp!: bigint;\n\tget imm(): OperandImm {\n\t\tif (this.type !== OperandType.IMMEDIATE) {\n\t\t\tthrow TypeError(\"Operand type mismatch.\");\n\t\t}\n\t\tif (this.immSigned) {\n\t\t\treturn {\n\t\t\t\ts: this.imms,\n\t\t\t\trel: !!this.immRelative,", "score": 40.58397171067584 }, { "filename": "src/decoder.ts", "retrieved_chunk": "\tget() {\n\t\tswitch (this.type) {\n\t\t\tcase OperandType.IMMEDIATE:\n\t\t\t\treturn this.imm;\n\t\t\tcase OperandType.REGISTER:\n\t\t\t\treturn this.reg;\n\t\t\tcase OperandType.POINTER:\n\t\t\t\treturn this.ptr;\n\t\t\tcase OperandType.MEMORY:\n\t\t\t\treturn this.mem;", "score": 32.538269662274104 }, { "filename": "src/decoder.ts", "retrieved_chunk": "\t\t\t};\n\t\t} else {\n\t\t\treturn {\n\t\t\t\tu: this.immu,\n\t\t\t\trel: !!this.immRelative,\n\t\t\t};\n\t\t}\n\t}\n\tget reg(): OperandReg {\n\t\tif (this.type !== OperandType.REGISTER) {", "score": 27.15509289452538 } ]
typescript
OperandPtr) {
import { DecodedInsn } from "./decoder.js"; import { Resource, ZyjsField, withHeap, withStack } from "./util.js"; import zydis from "./native.js"; import { AddressSizeHint, BranchType, BranchWidth, BroadcastMode, ConversionMode, EncodableEncoding, InsnAttribute, MAX_INSN_LENGTH, MachineMode, Mnemonic, OperandSizeHint, OperandType, Register, RoundingMode, SwizzleMode, } from "./enums.js"; import { OperandImm, OperandMem, OperandPtr, OperandReg } from "./common.js"; const OpField = ZyjsField("EncOp"); type OperandLike = keyof typeof Register | number | bigint | OperandImm | OperandReg | OperandPtr | OperandMem; export class EncoderOperand { resource: Resource; constructor(rsrc: Resource) { this.resource = rsrc; } @OpField type!: OperandType; @OpField imms!: bigint; @OpField immu!: bigint; @OpField ptrSeg!: number; @OpField ptrOff!: number; @OpField regv!: Register; @OpField regIs4!: 1 | 0; @OpField memBase!: Register; @OpField memIndex!: Register; @OpField memScale!: Register; @OpField memDisp!: bigint; get imm(): OperandImm { if (this.type !== OperandType.IMMEDIATE) { throw TypeError("Operand type mismatch."); } return { s: this.imms, u: this.immu, }; } set imm(o: OperandImm | bigint | number) { this.type = OperandType.IMMEDIATE; if (typeof o === "object") { if ("s" in o) { this.imms = o.s; } else { this.immu = o.u; } } else { this.imms = BigInt(o); } } get reg(): OperandReg { if (this.type !== OperandType.REGISTER) { throw TypeError("Operand type mismatch."); } return { name: this.regv, is4: !!this.regIs4, }; } set reg(o:
OperandReg | Register | keyof typeof Register) {
this.type = OperandType.REGISTER; if (typeof o === "string") { this.regv = Register[o]; } else if (typeof o === "number") { this.regv = o; } else { this.regv = o.name; this.regIs4 = o.is4 || false ? 1 : 0; } } get ptr(): OperandPtr { if (this.type !== OperandType.POINTER) { throw TypeError("Operand type mismatch."); } return { segv: this.ptrSeg, off: this.ptrOff, }; } set ptr(o: OperandPtr) { this.type = OperandType.POINTER; this.ptrOff = o.off; this.ptrSeg = o.segv; } get mem(): OperandMem { const res: OperandMem = { index: this.memIndex, base: this.memBase, scale: this.memScale, disp: this.memDisp, }; if (res.seg === Register.NONE) { delete res.seg; } if (res.base === Register.NONE) { delete res.base; } if (res.scale === 0 || res.index === Register.NONE) { delete res.index; delete res.scale; } return res; } set mem(o: OperandMem) { this.type = OperandType.MEMORY; this.memBase = o.base ?? Register.NONE; this.memDisp = o.disp ?? 0n; this.memIndex = o.index ?? Register.NONE; this.memScale = o.scale ?? 0; } get() { switch (this.type) { case OperandType.IMMEDIATE: return this.imm; case OperandType.REGISTER: return this.reg; case OperandType.POINTER: return this.ptr; case OperandType.MEMORY: return this.mem; default: throw Error(); } } set(o: OperandLike) { if (typeof o === "number" || typeof o === "bigint") { return void (this.imm = o); } else if (typeof o === "string") { return void (this.reg = o); } // OperandImm if ("s" in o || "u" in o) { this.imm = o; } // OperandReg. // else if ("name" in o) { this.reg = o; } // OperandPtr. // else if ("off" in o) { this.ptr = o; } // OperandMem. // else { this.mem = o; } } } const ReqField = ZyjsField("EncReq"); export class EncoderRequest { resource: Resource; constructor(from?: DecodedInsn) { this.resource = new Resource(zydis.asm.zyjsNewEncReq(from?.resource.ref() ?? 0)); } encode(at?: bigint) { return withStack((alloc) => { const tmp = alloc.allocate(MAX_INSN_LENGTH); const length = at != null ? zydis.asm.zyjsEncReqEncodeAbs(this.resource.ref(), tmp, at) : zydis.asm.zyjsEncReqEncode(this.resource.ref(), tmp); return zydis.HEAPU8.slice(tmp, tmp + length); }); } @ReqField machineMode!: MachineMode; @ReqField allowedEncodings!: EncodableEncoding; @ReqField mnemonic!: Mnemonic; @ReqField prefixes!: bigint; @ReqField branchType!: BranchType; @ReqField branchWidth!: BranchWidth; @ReqField addressSizeHint!: AddressSizeHint; @ReqField operandSizeHint!: OperandSizeHint; @ReqField operandCount!: number; @ReqField evexBroadcast!: BroadcastMode; @ReqField evexRounding!: RoundingMode; @ReqField evexSAE!: 1 | 0; @ReqField evexZeroingMask!: 1 | 0; @ReqField mvexBroadcast!: BroadcastMode; @ReqField mvexRounding!: RoundingMode; @ReqField mvexSAE!: 1 | 0; @ReqField mvexConversion!: ConversionMode; @ReqField mvexSwizzle!: SwizzleMode; @ReqField mvexEvictionHint!: 1 | 0; operand(n: number): EncoderOperand { const ptr = zydis.asm.zyjsEncReqRefOperand(this.resource.ref(), n); if (!ptr) { throw RangeError("Operand out of boundaries."); } return new EncoderOperand(this.resource.subresource(ptr)); } static from(machine: MachineMode, mnemonic: Mnemonic, ...operands: OperandLike[]) { const res = new EncoderRequest(); res.mnemonic = mnemonic; res.operandCount = operands.length; res.machineMode = machine; for (let n = 0; n !== operands.length; n++) { const op = operands[n]; res.operand(n).set(op); if (typeof op === "object" && "seg" in op) { switch (op.seg) { case Register.CS: res.prefixes = InsnAttribute.HAS_SEGMENT_CS; break; case Register.DS: res.prefixes = InsnAttribute.HAS_SEGMENT_DS; break; case Register.ES: res.prefixes = InsnAttribute.HAS_SEGMENT_ES; break; case Register.GS: res.prefixes = InsnAttribute.HAS_SEGMENT_GS; break; case Register.FS: res.prefixes = InsnAttribute.HAS_SEGMENT_FS; break; case Register.SS: res.prefixes = InsnAttribute.HAS_SEGMENT_SS; break; } } } return res; } } export function encode(machine: MachineMode, mnemonic: Mnemonic, ...operands: OperandLike[]) { return EncoderRequest.from(machine, mnemonic, ...operands).encode(); } export function encodeAt(at: bigint, machine: MachineMode, mnemonic: Mnemonic, ...operands: OperandLike[]) { return EncoderRequest.from(machine, mnemonic, ...operands).encode(at); } export function encodeNop(len: number) { return (len <= 512 ? withStack : withHeap)((alloc) => { const tmp = alloc.allocate(MAX_INSN_LENGTH); zydis.asm.zyjsEncNop(tmp, len); const res = zydis.HEAPU8.slice(tmp, tmp + len); alloc.free(tmp); return res; }); }
src/encoder.ts
zyantific-zydis-js-4715bb8
[ { "filename": "src/decoder.ts", "retrieved_chunk": "\t\t\tthrow TypeError(\"Operand type mismatch.\");\n\t\t}\n\t\treturn {\n\t\t\tname: this.regv,\n\t\t\tis4: this.encoding === OperandEncoding.IS4,\n\t\t};\n\t}\n\tget ptr(): OperandPtr {\n\t\tif (this.type !== OperandType.POINTER) {\n\t\t\tthrow TypeError(\"Operand type mismatch.\");", "score": 58.98168001559301 }, { "filename": "src/decoder.ts", "retrieved_chunk": "\t\t}\n\t\treturn {\n\t\t\tsegv: this.ptrSeg,\n\t\t\toff: this.ptrOff,\n\t\t};\n\t}\n\tget mem(): OperandMem {\n\t\tif (this.type !== OperandType.MEMORY) {\n\t\t\tthrow TypeError(\"Operand type mismatch.\");\n\t\t}", "score": 43.910098698155316 }, { "filename": "src/decoder.ts", "retrieved_chunk": "\t\t\t};\n\t\t} else {\n\t\t\treturn {\n\t\t\t\tu: this.immu,\n\t\t\t\trel: !!this.immRelative,\n\t\t\t};\n\t\t}\n\t}\n\tget reg(): OperandReg {\n\t\tif (this.type !== OperandType.REGISTER) {", "score": 41.3733368767987 }, { "filename": "src/decoder.ts", "retrieved_chunk": "\t@OpField\n\tprotected memDisp!: bigint;\n\tget imm(): OperandImm {\n\t\tif (this.type !== OperandType.IMMEDIATE) {\n\t\t\tthrow TypeError(\"Operand type mismatch.\");\n\t\t}\n\t\tif (this.immSigned) {\n\t\t\treturn {\n\t\t\t\ts: this.imms,\n\t\t\t\trel: !!this.immRelative,", "score": 40.58397171067584 }, { "filename": "src/register.ts", "retrieved_chunk": "\treturn zydis.asm.zyjsRegisterGetLargestEnclosing(reg, mode);\n}\nexport function from(reg: string) {\n\treg = reg.toUpperCase();\n\tif (reg in Register) {\n\t\treturn Register[reg as keyof typeof Register];\n\t}\n\tthrow Error(\"Invalid register name\");\n}", "score": 37.552084046387534 } ]
typescript
OperandReg | Register | keyof typeof Register) {
import { NativeModules } from 'react-native'; import { PitchDetectorErrors } from '../../../types'; import { PitchDetectorError } from '../../erros'; import { Permissions } from '../../permissions'; import { PitchDetector } from '..'; const Module = NativeModules.PitchDetectorModule; const asyncMock = <T>(value: T) => jest.fn().mockResolvedValue(value); const asyncMockThrow = <T>(error: T) => jest.fn().mockImplementation(() => { throw error; }); describe('PitchDetector', () => { beforeEach(() => jest.clearAllMocks()); it.each([ ['start', 1], ['isRecording', 1], ['stop', 1], ])( 'should call %s method %s time(s) from native module', async (method: string, times: number) => { const spy = jest.spyOn(Module, method as any); await Object(PitchDetector)[method](); expect(spy).toBeCalledTimes(times); } ); it.each([ ['hasPermissions', 'start'], ['getDefaultConfig', 'start'], ])( 'should call %s method when %s method will be called', async (target: string, method: string) => { const spy = jest.spyOn(PitchDetector, target as any); await Object(PitchDetector)[method](); expect(spy).toBeCalledTimes(1); } ); it('should call audio permission method when start method will be called', async () => {
const spy = jest.spyOn(Permissions, 'audio');
await PitchDetector.start(); expect(spy).toBeCalledTimes(1); }); it('should throw error when start method will be called and not have audio record permission', async () => { const error = new PitchDetectorError(PitchDetectorErrors.PERMISSIONS_ERROR); const spy = jest.spyOn(console, 'warn'); Permissions.audio = asyncMock(false); await PitchDetector.start(); expect(spy).toBeCalledTimes(1); expect(spy).toHaveBeenCalledWith(error); }); it.each([ ['addListener', 'addListener'], ['removeAllListeners', 'removeAllListeners'], ['removeAllListeners', 'removeListener'], ])( 'should call %s method from event emitter when %s method will be called', async (target: string, method: string) => { const spy = jest.spyOn(Object(PitchDetector).event, target as any); await Object(PitchDetector)[method](1); expect(spy).toBeCalledTimes(1); } ); it.each([['start'], ['stop'], ['isRecording']])( 'should throw error when native %s method fail', async (method: string) => { const error = new Error('Some error message'); const spy = jest.spyOn(console, 'warn'); Permissions.audio = asyncMock(true); Object(PitchDetector).module[method] = asyncMockThrow(error); await Object(PitchDetector)[method](); expect(spy).toBeCalledTimes(1); expect(spy).toBeCalledWith(error); } ); });
src/internal/pitch-detector/__tests__/pitch-detector.spec.ts
1fabiopereira-react-native-pitch-detector-b70fc00
[ { "filename": "src/internal/utils/__tests__/utils.spec.ts", "retrieved_chunk": " 'should return %s when isObject function will be called with %s',\n (expected: boolean, _: string, params: any) => {\n const result = isObject(params);\n expect(result).toBe(expected);\n }\n );\n it('should not merge when one params is not object', () => {\n const result = merge({ name: 'Fábio' }, NaN);\n expect(result).toHaveProperty('name');\n });", "score": 43.60037168144081 }, { "filename": "src/internal/permissions/__tests__/permissions.spec.ts", "retrieved_chunk": " ['isBlocked', 'blocked', true, isBlocked],\n ['isDenied', 'denied', true, isDenied],\n ['isGranted', 'granted', true, isGranted],\n ['isLimited', 'limited', true, isLimited],\n ['isUnavailable', 'unavailable', true, isUnavailable],\n ])(\n 'should call %s function and return %s',\n (\n _: string,\n params: any,", "score": 24.83325961697717 }, { "filename": "src/internal/erros/__tests__/errors.spec.ts", "retrieved_chunk": " it('should create link error', () => {\n const error: any = new PitchDetectorError(\n PitchDetectorErrors.LINKING_ERROR\n );\n expect(error?.message).toMatch(/doesn't seem to be linked/);\n });\n it('should create permission error', () => {\n const error: any = new PitchDetectorError(\n PitchDetectorErrors.PERMISSIONS_ERROR\n );", "score": 18.77680073093968 }, { "filename": "src/internal/utils/index.ts", "retrieved_chunk": " const source = sources.shift();\n if (isObject(target) && isObject(source)) {\n for (const key in source) {\n if (isObject(source[key])) {\n if (!target[key]) Object.assign(target, { [key]: {} });\n merge(target[key], source[key]);\n } else {\n Object.assign(target, { [key]: source[key] });\n }\n }", "score": 15.297612768521985 }, { "filename": "src/internal/pitch-detector/index.ts", "retrieved_chunk": " */\n private async hasPermissions(): Promise<boolean> {\n return !!(await Permissions.audio());\n }\n /**\n * Get current status\n * @returns Promise<boolean>\n */\n async isRecording(): Promise<boolean> {\n try {", "score": 13.986598215459834 } ]
typescript
const spy = jest.spyOn(Permissions, 'audio');
import { OperandImm, OperandMem, OperandPtr, OperandReg } from "./common.js"; import { BranchType, BroadcastMode, ConversionMode, DecoderMode, ElementType, ExceptionClass, ISAExt, ISASet, InsnCategory, InstructionEncoding, MachineMode, MaskMode, MemoryOperandType, Mnemonic, OpcodeMap, OperandAction, OperandEncoding, OperandType, OperandVisibility, Register, RoundingMode, StackWidth, SwizzleMode, } from "./enums.js"; import zydis from "./native.js"; import { Resource, ZyjsField, withStack } from "./util.js"; const OpField = ZyjsField("DecOp"); export class DecodedOperand { resource: Resource; constructor(rsrc: Resource) { this.resource = rsrc; } @OpField id!: number; @OpField visiblity!: OperandVisibility; @OpField actions!: OperandAction; @OpField encoding!: OperandEncoding; @OpField size!: number; @OpField elementType!: ElementType; @OpField elementSize!: number; @OpField elementCount!: number; @OpField attributes!: number; @OpField type!: OperandType; @OpField protected immSigned!: number; @OpField protected immRelative!: number; @OpField protected imms!: bigint; @OpField protected immu!: bigint; @OpField protected ptrSeg!: number; @OpField protected ptrOff!: number; @OpField protected regv!: Register; @OpField protected memType!: MemoryOperandType; @OpField protected memSeg!: Register; @OpField protected memBase!: Register; @OpField protected memIndex!: Register; @OpField protected memScale!: Register; @OpField protected memHasDisp!: number; @OpField protected memDisp!: bigint; get imm(): OperandImm { if (this.type !== OperandType.IMMEDIATE) { throw TypeError("Operand type mismatch."); } if (this.immSigned) { return { s: this.imms, rel: !!this.immRelative, }; } else { return { u: this.immu, rel: !!this.immRelative, }; } } get reg(): OperandReg { if (this.type !== OperandType.REGISTER) { throw TypeError("Operand type mismatch."); } return { name: this.regv, is4: this.encoding === OperandEncoding.IS4, }; } get ptr(): OperandPtr { if (this.type !== OperandType.POINTER) { throw TypeError("Operand type mismatch."); } return { segv: this.ptrSeg, off: this.ptrOff, }; } get mem(): OperandMem { if (this.type !== OperandType.MEMORY) { throw TypeError("Operand type mismatch."); } const res: OperandMem = { type: this.memType, seg: this.memSeg, index: this.memIndex, base: this.memBase, scale: this.memScale, disp: this.memHasDisp ? this.memDisp : undefined, }; if (res.seg === Register.NONE) { delete res.seg; } if (res.base === Register.NONE) { delete res.base; } if (res.scale === 0 || res.index === Register.NONE) { delete res.index; delete res.scale; } return res; } get() { switch (this.type) { case OperandType.IMMEDIATE: return this.imm; case OperandType.REGISTER: return this.reg; case OperandType.POINTER: return this.ptr; case OperandType.MEMORY: return this.mem; default: throw Error(); } } } const FlagField = ZyjsField("FlagSet"); interface AccessedFlags { readonly tested: number; readonly modified: number; readonly set: number; readonly unset: number; readonly undef: number; } const flagsNoop: AccessedFlags = { tested: 0, modified: 0, set: 0, unset: 0, undef: 0, }; class AccessedFlagsByPtr implements AccessedFlags { resource: { ref(): number }; constructor(ptr: number) { this.resource = { ref() { return ptr; }, }; } @FlagField readonly tested!: number; @FlagField readonly modified!: number; @FlagField readonly set!: number; @FlagField readonly unset!: number; @FlagField readonly undef!: number; } const InsnField = ZyjsField("DecInsn"); export class DecodedInsn { resource: Resource; constructor(rsrc: Resource) { this.resource = rsrc; } // Common information. // @InsnField length!: number; @InsnField mnemonic!: Mnemonic; @InsnField encoding!: InstructionEncoding; @InsnField opcodeMap!: OpcodeMap; @InsnField opcode!: number; @InsnField stackWidth!: number; @InsnField operandWidth!: number; @InsnField addressWidth!: number; @InsnField operandCount!: number; @InsnField visibleOperandCount!: number; @InsnField attributes!: bigint; // AVX. // @InsnField vectorLength!: number; @InsnField maskMode!: MaskMode; @InsnField maskReg!: Register; @InsnField isBroadcastStatic!: 1 | 0; @InsnField broadcastMode!: BroadcastMode; @InsnField roundingMode!: RoundingMode; @InsnField swizzleMode!: SwizzleMode; @InsnField conversionMode!: ConversionMode; @InsnField hasSAE!: 1 | 0; @InsnField hasEvictionHint!: 1 | 0; // Meta. // @InsnField category!: InsnCategory; @InsnField isaSet!: ISASet; @InsnField isaExt!: ISAExt; @InsnField branchType!: BranchType; @InsnField exceptionClass!: ExceptionClass; // Flags. // @InsnField private cpuFlagsPtr!: number; @InsnField private fpuFlagsPtr!: number; get cpuFlags(): AccessedFlags { const ptr = this.cpuFlagsPtr; return ptr ? new AccessedFlagsByPtr(ptr) : flagsNoop; } get fpuFlags(): AccessedFlags { const ptr = this.fpuFlagsPtr; return ptr ? new AccessedFlagsByPtr(ptr) : flagsNoop; } operand(n: number): DecodedOperand { const ptr = zydis.asm.zyjsDecInsnRefOperand(this.resource.ref(), n); if (!ptr) { throw RangeError("Operand out of boundaries."); } return new DecodedOperand(this.resource.subresource(ptr)); } } export class Decoder { resource: Resource; constructor(mode: MachineMode, width: StackWidth) { this.resource = new Resource(zydis.asm.zyjsNewDecoder(mode, width)); } set(mode: DecoderMode, value: boolean) { zydis.asm.zyjsDecoderSetMode(this.resource.ref(), mode, value ? 1 : 0); return this; } decode(buffer: Uint8Array) {
const ptr = withStack((a) => {
return zydis.asm.zyjsDecoderDecode(this.resource.ref(), a.buf(buffer), buffer.length); }); return new DecodedInsn(new Resource(ptr)); } }
src/decoder.ts
zyantific-zydis-js-4715bb8
[ { "filename": "src/encoder.ts", "retrieved_chunk": "\t}\n}\nconst ReqField = ZyjsField(\"EncReq\");\nexport class EncoderRequest {\n\tresource: Resource;\n\tconstructor(from?: DecodedInsn) {\n\t\tthis.resource = new Resource(zydis.asm.zyjsNewEncReq(from?.resource.ref() ?? 0));\n\t}\n\tencode(at?: bigint) {\n\t\treturn withStack((alloc) => {", "score": 57.400892784371756 }, { "filename": "src/formatter.ts", "retrieved_chunk": "\tproperty(prop: FormatterProperty, value: boolean | number) {\n\t\tzydis.asm.zyjsFormatterSetProperty(this.resource.ref(), prop, +value);\n\t\treturn this;\n\t}\n\tinsn(value: DecodedInsn, address: bigint = 0n) {\n\t\treturn withStack((alloc) => {\n\t\t\tconst buf = alloc.allocate(MAX_LENGTH);\n\t\t\tzydis.asm.zyjsFormatterFormatInsn(this.resource.ref(), buf, MAX_LENGTH, value.resource.ref(), address);\n\t\t\treturn zydis.UTF8ToString(buf, MAX_LENGTH);\n\t\t});", "score": 51.483080780017985 }, { "filename": "src/encoder.ts", "retrieved_chunk": "\t\t\tconst tmp = alloc.allocate(MAX_INSN_LENGTH);\n\t\t\tconst length =\n\t\t\t\tat != null\n\t\t\t\t\t? zydis.asm.zyjsEncReqEncodeAbs(this.resource.ref(), tmp, at)\n\t\t\t\t\t: zydis.asm.zyjsEncReqEncode(this.resource.ref(), tmp);\n\t\t\treturn zydis.HEAPU8.slice(tmp, tmp + length);\n\t\t});\n\t}\n\t@ReqField\n\tmachineMode!: MachineMode;", "score": 44.07871637634402 }, { "filename": "src/util.ts", "retrieved_chunk": "}\nexport function ZyjsField(ns: string) {\n\treturn function (target: any, propertyKey: any) {\n\t\tconst getter = zydis.asm[`zyjs${ns}Get_${propertyKey}`];\n\t\tconst setter = zydis.asm[`zyjs${ns}Set_${propertyKey}`];\n\t\tObject.defineProperty(target, propertyKey, {\n\t\t\tget(this: { resource: { ref(): number } }) {\n\t\t\t\treturn getter(this.resource.ref());\n\t\t\t},\n\t\t\tset(this: { resource: { ref(): number } }, value: any) {", "score": 43.299503809954835 }, { "filename": "src/formatter.ts", "retrieved_chunk": "import { FormatterProperty, FormatterStyle } from \"./enums.js\";\nimport { HeapAllocator, Resource, withStack } from \"./util.js\";\nimport zydis from \"./native.js\";\nimport { DecodedInsn, DecodedOperand } from \"./decoder.js\";\nconst MAX_LENGTH = 512;\nexport class Formatter {\n\tresource: Resource;\n\tconstructor(style: FormatterStyle) {\n\t\tthis.resource = new Resource(zydis.asm.zyjsNewFormatter(style));\n\t}", "score": 41.73019455119197 } ]
typescript
const ptr = withStack((a) => {
import { DecodedInsn } from "./decoder.js"; import { Resource, ZyjsField, withHeap, withStack } from "./util.js"; import zydis from "./native.js"; import { AddressSizeHint, BranchType, BranchWidth, BroadcastMode, ConversionMode, EncodableEncoding, InsnAttribute, MAX_INSN_LENGTH, MachineMode, Mnemonic, OperandSizeHint, OperandType, Register, RoundingMode, SwizzleMode, } from "./enums.js"; import { OperandImm, OperandMem, OperandPtr, OperandReg } from "./common.js"; const OpField = ZyjsField("EncOp"); type OperandLike = keyof typeof Register | number | bigint | OperandImm | OperandReg | OperandPtr | OperandMem; export class EncoderOperand { resource: Resource; constructor(rsrc: Resource) { this.resource = rsrc; } @OpField type!: OperandType; @OpField imms!: bigint; @OpField immu!: bigint; @OpField ptrSeg!: number; @OpField ptrOff!: number; @OpField regv!: Register; @OpField regIs4!: 1 | 0; @OpField memBase!: Register; @OpField memIndex!: Register; @OpField memScale!: Register; @OpField memDisp!: bigint; get imm(): OperandImm { if (this.type !== OperandType.IMMEDIATE) { throw TypeError("Operand type mismatch."); } return { s: this.imms, u: this.immu, }; }
set imm(o: OperandImm | bigint | number) {
this.type = OperandType.IMMEDIATE; if (typeof o === "object") { if ("s" in o) { this.imms = o.s; } else { this.immu = o.u; } } else { this.imms = BigInt(o); } } get reg(): OperandReg { if (this.type !== OperandType.REGISTER) { throw TypeError("Operand type mismatch."); } return { name: this.regv, is4: !!this.regIs4, }; } set reg(o: OperandReg | Register | keyof typeof Register) { this.type = OperandType.REGISTER; if (typeof o === "string") { this.regv = Register[o]; } else if (typeof o === "number") { this.regv = o; } else { this.regv = o.name; this.regIs4 = o.is4 || false ? 1 : 0; } } get ptr(): OperandPtr { if (this.type !== OperandType.POINTER) { throw TypeError("Operand type mismatch."); } return { segv: this.ptrSeg, off: this.ptrOff, }; } set ptr(o: OperandPtr) { this.type = OperandType.POINTER; this.ptrOff = o.off; this.ptrSeg = o.segv; } get mem(): OperandMem { const res: OperandMem = { index: this.memIndex, base: this.memBase, scale: this.memScale, disp: this.memDisp, }; if (res.seg === Register.NONE) { delete res.seg; } if (res.base === Register.NONE) { delete res.base; } if (res.scale === 0 || res.index === Register.NONE) { delete res.index; delete res.scale; } return res; } set mem(o: OperandMem) { this.type = OperandType.MEMORY; this.memBase = o.base ?? Register.NONE; this.memDisp = o.disp ?? 0n; this.memIndex = o.index ?? Register.NONE; this.memScale = o.scale ?? 0; } get() { switch (this.type) { case OperandType.IMMEDIATE: return this.imm; case OperandType.REGISTER: return this.reg; case OperandType.POINTER: return this.ptr; case OperandType.MEMORY: return this.mem; default: throw Error(); } } set(o: OperandLike) { if (typeof o === "number" || typeof o === "bigint") { return void (this.imm = o); } else if (typeof o === "string") { return void (this.reg = o); } // OperandImm if ("s" in o || "u" in o) { this.imm = o; } // OperandReg. // else if ("name" in o) { this.reg = o; } // OperandPtr. // else if ("off" in o) { this.ptr = o; } // OperandMem. // else { this.mem = o; } } } const ReqField = ZyjsField("EncReq"); export class EncoderRequest { resource: Resource; constructor(from?: DecodedInsn) { this.resource = new Resource(zydis.asm.zyjsNewEncReq(from?.resource.ref() ?? 0)); } encode(at?: bigint) { return withStack((alloc) => { const tmp = alloc.allocate(MAX_INSN_LENGTH); const length = at != null ? zydis.asm.zyjsEncReqEncodeAbs(this.resource.ref(), tmp, at) : zydis.asm.zyjsEncReqEncode(this.resource.ref(), tmp); return zydis.HEAPU8.slice(tmp, tmp + length); }); } @ReqField machineMode!: MachineMode; @ReqField allowedEncodings!: EncodableEncoding; @ReqField mnemonic!: Mnemonic; @ReqField prefixes!: bigint; @ReqField branchType!: BranchType; @ReqField branchWidth!: BranchWidth; @ReqField addressSizeHint!: AddressSizeHint; @ReqField operandSizeHint!: OperandSizeHint; @ReqField operandCount!: number; @ReqField evexBroadcast!: BroadcastMode; @ReqField evexRounding!: RoundingMode; @ReqField evexSAE!: 1 | 0; @ReqField evexZeroingMask!: 1 | 0; @ReqField mvexBroadcast!: BroadcastMode; @ReqField mvexRounding!: RoundingMode; @ReqField mvexSAE!: 1 | 0; @ReqField mvexConversion!: ConversionMode; @ReqField mvexSwizzle!: SwizzleMode; @ReqField mvexEvictionHint!: 1 | 0; operand(n: number): EncoderOperand { const ptr = zydis.asm.zyjsEncReqRefOperand(this.resource.ref(), n); if (!ptr) { throw RangeError("Operand out of boundaries."); } return new EncoderOperand(this.resource.subresource(ptr)); } static from(machine: MachineMode, mnemonic: Mnemonic, ...operands: OperandLike[]) { const res = new EncoderRequest(); res.mnemonic = mnemonic; res.operandCount = operands.length; res.machineMode = machine; for (let n = 0; n !== operands.length; n++) { const op = operands[n]; res.operand(n).set(op); if (typeof op === "object" && "seg" in op) { switch (op.seg) { case Register.CS: res.prefixes = InsnAttribute.HAS_SEGMENT_CS; break; case Register.DS: res.prefixes = InsnAttribute.HAS_SEGMENT_DS; break; case Register.ES: res.prefixes = InsnAttribute.HAS_SEGMENT_ES; break; case Register.GS: res.prefixes = InsnAttribute.HAS_SEGMENT_GS; break; case Register.FS: res.prefixes = InsnAttribute.HAS_SEGMENT_FS; break; case Register.SS: res.prefixes = InsnAttribute.HAS_SEGMENT_SS; break; } } } return res; } } export function encode(machine: MachineMode, mnemonic: Mnemonic, ...operands: OperandLike[]) { return EncoderRequest.from(machine, mnemonic, ...operands).encode(); } export function encodeAt(at: bigint, machine: MachineMode, mnemonic: Mnemonic, ...operands: OperandLike[]) { return EncoderRequest.from(machine, mnemonic, ...operands).encode(at); } export function encodeNop(len: number) { return (len <= 512 ? withStack : withHeap)((alloc) => { const tmp = alloc.allocate(MAX_INSN_LENGTH); zydis.asm.zyjsEncNop(tmp, len); const res = zydis.HEAPU8.slice(tmp, tmp + len); alloc.free(tmp); return res; }); }
src/encoder.ts
zyantific-zydis-js-4715bb8
[ { "filename": "src/decoder.ts", "retrieved_chunk": "\t@OpField\n\tprotected memDisp!: bigint;\n\tget imm(): OperandImm {\n\t\tif (this.type !== OperandType.IMMEDIATE) {\n\t\t\tthrow TypeError(\"Operand type mismatch.\");\n\t\t}\n\t\tif (this.immSigned) {\n\t\t\treturn {\n\t\t\t\ts: this.imms,\n\t\t\t\trel: !!this.immRelative,", "score": 72.59409806062682 }, { "filename": "src/decoder.ts", "retrieved_chunk": "\t\t\tthrow TypeError(\"Operand type mismatch.\");\n\t\t}\n\t\treturn {\n\t\t\tname: this.regv,\n\t\t\tis4: this.encoding === OperandEncoding.IS4,\n\t\t};\n\t}\n\tget ptr(): OperandPtr {\n\t\tif (this.type !== OperandType.POINTER) {\n\t\t\tthrow TypeError(\"Operand type mismatch.\");", "score": 50.268411202061266 }, { "filename": "src/decoder.ts", "retrieved_chunk": "\t\t}\n\t\treturn {\n\t\t\tsegv: this.ptrSeg,\n\t\t\toff: this.ptrOff,\n\t\t};\n\t}\n\tget mem(): OperandMem {\n\t\tif (this.type !== OperandType.MEMORY) {\n\t\t\tthrow TypeError(\"Operand type mismatch.\");\n\t\t}", "score": 47.49711099722624 }, { "filename": "src/decoder.ts", "retrieved_chunk": "\t\t\t};\n\t\t} else {\n\t\t\treturn {\n\t\t\t\tu: this.immu,\n\t\t\t\trel: !!this.immRelative,\n\t\t\t};\n\t\t}\n\t}\n\tget reg(): OperandReg {\n\t\tif (this.type !== OperandType.REGISTER) {", "score": 41.61092318773385 }, { "filename": "src/decoder.ts", "retrieved_chunk": "\tget() {\n\t\tswitch (this.type) {\n\t\t\tcase OperandType.IMMEDIATE:\n\t\t\t\treturn this.imm;\n\t\t\tcase OperandType.REGISTER:\n\t\t\t\treturn this.reg;\n\t\t\tcase OperandType.POINTER:\n\t\t\t\treturn this.ptr;\n\t\t\tcase OperandType.MEMORY:\n\t\t\t\treturn this.mem;", "score": 39.85168061825073 } ]
typescript
set imm(o: OperandImm | bigint | number) {
import { DecodedInsn } from "./decoder.js"; import { Resource, ZyjsField, withHeap, withStack } from "./util.js"; import zydis from "./native.js"; import { AddressSizeHint, BranchType, BranchWidth, BroadcastMode, ConversionMode, EncodableEncoding, InsnAttribute, MAX_INSN_LENGTH, MachineMode, Mnemonic, OperandSizeHint, OperandType, Register, RoundingMode, SwizzleMode, } from "./enums.js"; import { OperandImm, OperandMem, OperandPtr, OperandReg } from "./common.js"; const OpField = ZyjsField("EncOp"); type OperandLike = keyof typeof Register | number | bigint | OperandImm | OperandReg | OperandPtr | OperandMem; export class EncoderOperand { resource: Resource; constructor(rsrc: Resource) { this.resource = rsrc; } @OpField type!: OperandType; @OpField imms!: bigint; @OpField immu!: bigint; @OpField ptrSeg!: number; @OpField ptrOff!: number; @OpField regv!: Register; @OpField regIs4!: 1 | 0; @OpField memBase!: Register; @OpField memIndex!: Register; @OpField memScale!: Register; @OpField memDisp!: bigint; get imm(): OperandImm { if (this.type !== OperandType.IMMEDIATE) { throw TypeError("Operand type mismatch."); } return { s: this.imms, u: this.immu, }; } set imm(o: OperandImm | bigint | number) { this.type = OperandType.IMMEDIATE; if (typeof o === "object") { if ("s" in o) { this.imms = o.s; } else { this.immu = o.u; } } else { this.imms = BigInt(o); } } get reg(): OperandReg { if (this.type !== OperandType.REGISTER) { throw TypeError("Operand type mismatch."); } return { name: this.regv, is4: !!this.regIs4, }; } set reg(o: OperandReg | Register | keyof typeof Register) { this.type = OperandType.REGISTER; if (typeof o === "string") { this.regv = Register[o]; } else if (typeof o === "number") { this.regv = o; } else { this.regv = o.name; this.regIs4 = o.is4 || false ? 1 : 0; } } get ptr(): OperandPtr { if (this.type !== OperandType.POINTER) { throw TypeError("Operand type mismatch."); } return { segv: this.ptrSeg, off: this.ptrOff, }; } set ptr(o: OperandPtr) { this.type = OperandType.POINTER; this.ptrOff = o.off; this.ptrSeg = o.segv; } get mem(): OperandMem { const res: OperandMem = { index: this.memIndex, base: this.memBase, scale: this.memScale, disp: this.memDisp, }; if (res.seg === Register.NONE) { delete res.seg; } if (res.base === Register.NONE) { delete res.base; } if (res.scale === 0 || res.index === Register.NONE) { delete res.index; delete res.scale; } return res; } set mem(o: OperandMem) { this.type = OperandType.MEMORY; this.memBase = o.base ?? Register.NONE; this.memDisp = o.disp ?? 0n; this.memIndex = o.index ?? Register.NONE; this.memScale = o.scale ?? 0; } get() { switch (this.type) { case OperandType.IMMEDIATE: return this.imm; case OperandType.REGISTER: return this.reg; case OperandType.POINTER: return this.ptr; case OperandType.MEMORY: return this.mem; default: throw Error(); } } set(o: OperandLike) { if (typeof o === "number" || typeof o === "bigint") { return void (this.imm = o); } else if (typeof o === "string") { return void (this.reg = o); } // OperandImm if ("s" in o || "u" in o) { this.imm = o; } // OperandReg. // else if ("name" in o) { this.reg = o; } // OperandPtr. // else if ("off" in o) { this.ptr = o; } // OperandMem. // else { this.mem = o; } } } const ReqField = ZyjsField("EncReq"); export class EncoderRequest { resource: Resource;
constructor(from?: DecodedInsn) {
this.resource = new Resource(zydis.asm.zyjsNewEncReq(from?.resource.ref() ?? 0)); } encode(at?: bigint) { return withStack((alloc) => { const tmp = alloc.allocate(MAX_INSN_LENGTH); const length = at != null ? zydis.asm.zyjsEncReqEncodeAbs(this.resource.ref(), tmp, at) : zydis.asm.zyjsEncReqEncode(this.resource.ref(), tmp); return zydis.HEAPU8.slice(tmp, tmp + length); }); } @ReqField machineMode!: MachineMode; @ReqField allowedEncodings!: EncodableEncoding; @ReqField mnemonic!: Mnemonic; @ReqField prefixes!: bigint; @ReqField branchType!: BranchType; @ReqField branchWidth!: BranchWidth; @ReqField addressSizeHint!: AddressSizeHint; @ReqField operandSizeHint!: OperandSizeHint; @ReqField operandCount!: number; @ReqField evexBroadcast!: BroadcastMode; @ReqField evexRounding!: RoundingMode; @ReqField evexSAE!: 1 | 0; @ReqField evexZeroingMask!: 1 | 0; @ReqField mvexBroadcast!: BroadcastMode; @ReqField mvexRounding!: RoundingMode; @ReqField mvexSAE!: 1 | 0; @ReqField mvexConversion!: ConversionMode; @ReqField mvexSwizzle!: SwizzleMode; @ReqField mvexEvictionHint!: 1 | 0; operand(n: number): EncoderOperand { const ptr = zydis.asm.zyjsEncReqRefOperand(this.resource.ref(), n); if (!ptr) { throw RangeError("Operand out of boundaries."); } return new EncoderOperand(this.resource.subresource(ptr)); } static from(machine: MachineMode, mnemonic: Mnemonic, ...operands: OperandLike[]) { const res = new EncoderRequest(); res.mnemonic = mnemonic; res.operandCount = operands.length; res.machineMode = machine; for (let n = 0; n !== operands.length; n++) { const op = operands[n]; res.operand(n).set(op); if (typeof op === "object" && "seg" in op) { switch (op.seg) { case Register.CS: res.prefixes = InsnAttribute.HAS_SEGMENT_CS; break; case Register.DS: res.prefixes = InsnAttribute.HAS_SEGMENT_DS; break; case Register.ES: res.prefixes = InsnAttribute.HAS_SEGMENT_ES; break; case Register.GS: res.prefixes = InsnAttribute.HAS_SEGMENT_GS; break; case Register.FS: res.prefixes = InsnAttribute.HAS_SEGMENT_FS; break; case Register.SS: res.prefixes = InsnAttribute.HAS_SEGMENT_SS; break; } } } return res; } } export function encode(machine: MachineMode, mnemonic: Mnemonic, ...operands: OperandLike[]) { return EncoderRequest.from(machine, mnemonic, ...operands).encode(); } export function encodeAt(at: bigint, machine: MachineMode, mnemonic: Mnemonic, ...operands: OperandLike[]) { return EncoderRequest.from(machine, mnemonic, ...operands).encode(at); } export function encodeNop(len: number) { return (len <= 512 ? withStack : withHeap)((alloc) => { const tmp = alloc.allocate(MAX_INSN_LENGTH); zydis.asm.zyjsEncNop(tmp, len); const res = zydis.HEAPU8.slice(tmp, tmp + len); alloc.free(tmp); return res; }); }
src/encoder.ts
zyantific-zydis-js-4715bb8
[ { "filename": "src/decoder.ts", "retrieved_chunk": "const InsnField = ZyjsField(\"DecInsn\");\nexport class DecodedInsn {\n\tresource: Resource;\n\tconstructor(rsrc: Resource) {\n\t\tthis.resource = rsrc;\n\t}\n\t// Common information.\n\t//\n\t@InsnField\n\tlength!: number;", "score": 30.5715663067595 }, { "filename": "src/formatter.ts", "retrieved_chunk": "import { FormatterProperty, FormatterStyle } from \"./enums.js\";\nimport { HeapAllocator, Resource, withStack } from \"./util.js\";\nimport zydis from \"./native.js\";\nimport { DecodedInsn, DecodedOperand } from \"./decoder.js\";\nconst MAX_LENGTH = 512;\nexport class Formatter {\n\tresource: Resource;\n\tconstructor(style: FormatterStyle) {\n\t\tthis.resource = new Resource(zydis.asm.zyjsNewFormatter(style));\n\t}", "score": 23.473752384664976 }, { "filename": "src/decoder.ts", "retrieved_chunk": "\tOperandVisibility,\n\tRegister,\n\tRoundingMode,\n\tStackWidth,\n\tSwizzleMode,\n} from \"./enums.js\";\nimport zydis from \"./native.js\";\nimport { Resource, ZyjsField, withStack } from \"./util.js\";\nconst OpField = ZyjsField(\"DecOp\");\nexport class DecodedOperand {", "score": 19.505763771976135 }, { "filename": "src/decoder.ts", "retrieved_chunk": "\t\tif (!ptr) {\n\t\t\tthrow RangeError(\"Operand out of boundaries.\");\n\t\t}\n\t\treturn new DecodedOperand(this.resource.subresource(ptr));\n\t}\n}\nexport class Decoder {\n\tresource: Resource;\n\tconstructor(mode: MachineMode, width: StackWidth) {\n\t\tthis.resource = new Resource(zydis.asm.zyjsNewDecoder(mode, width));", "score": 17.308185063536346 }, { "filename": "src/decoder.ts", "retrieved_chunk": "\tresource: Resource;\n\tconstructor(rsrc: Resource) {\n\t\tthis.resource = rsrc;\n\t}\n\t@OpField\n\tid!: number;\n\t@OpField\n\tvisiblity!: OperandVisibility;\n\t@OpField\n\tactions!: OperandAction;", "score": 16.672360866507077 } ]
typescript
constructor(from?: DecodedInsn) {
import { DecodedInsn } from "./decoder.js"; import { Resource, ZyjsField, withHeap, withStack } from "./util.js"; import zydis from "./native.js"; import { AddressSizeHint, BranchType, BranchWidth, BroadcastMode, ConversionMode, EncodableEncoding, InsnAttribute, MAX_INSN_LENGTH, MachineMode, Mnemonic, OperandSizeHint, OperandType, Register, RoundingMode, SwizzleMode, } from "./enums.js"; import { OperandImm, OperandMem, OperandPtr, OperandReg } from "./common.js"; const OpField = ZyjsField("EncOp"); type OperandLike = keyof typeof Register | number | bigint | OperandImm | OperandReg | OperandPtr | OperandMem; export class EncoderOperand { resource: Resource; constructor(rsrc: Resource) { this.resource = rsrc; } @OpField type!: OperandType; @OpField imms!: bigint; @OpField immu!: bigint; @OpField ptrSeg!: number; @OpField ptrOff!: number; @OpField regv!: Register; @OpField regIs4!: 1 | 0; @OpField memBase!: Register; @OpField memIndex!: Register; @OpField memScale!: Register; @OpField memDisp!: bigint; get imm(): OperandImm { if (this.type !== OperandType.IMMEDIATE) { throw TypeError("Operand type mismatch."); } return { s: this.imms, u: this.immu, }; } set imm(o: OperandImm | bigint | number) { this.type = OperandType.IMMEDIATE; if (typeof o === "object") { if ("s" in o) { this.imms = o.s; } else { this.immu = o.u; } } else { this.imms = BigInt(o); } } get reg(): OperandReg { if (this.type !== OperandType.REGISTER) { throw TypeError("Operand type mismatch."); } return { name: this.regv, is4: !!this.regIs4, }; } set reg(o: OperandReg | Register | keyof typeof Register) { this.type = OperandType.REGISTER; if (typeof o === "string") { this.regv = Register[o]; } else if (typeof o === "number") { this.regv = o; } else { this.regv = o.name; this.regIs4 = o.is4 || false ? 1 : 0; } } get ptr(): OperandPtr { if (this.type !== OperandType.POINTER) { throw TypeError("Operand type mismatch."); } return { segv: this.ptrSeg, off: this.ptrOff, }; } set ptr(o: OperandPtr) { this.type = OperandType.POINTER; this.ptrOff = o.off; this.ptrSeg = o.segv; } get mem(): OperandMem { const res: OperandMem = { index: this.memIndex, base: this.memBase, scale: this.memScale, disp: this.memDisp, }; if (res.seg === Register.NONE) { delete res.seg; } if (res.base === Register.NONE) { delete res.base; } if (res.scale === 0 || res.index === Register.NONE) { delete res.index; delete res.scale; } return res; } set mem(o: OperandMem) { this.type = OperandType.MEMORY; this.memBase = o.base ?? Register.NONE; this.memDisp = o.disp ?? 0n; this.memIndex = o.index ?? Register.NONE; this.memScale = o.scale ?? 0; } get() { switch (this.type) { case OperandType.IMMEDIATE: return this.imm; case OperandType.REGISTER: return this.reg; case OperandType.POINTER: return this.ptr; case OperandType.MEMORY: return this.mem; default: throw Error(); } } set(o: OperandLike) { if (typeof o === "number" || typeof o === "bigint") { return void (this.imm = o); } else if (typeof o === "string") { return void (this.reg = o); } // OperandImm if ("s" in o || "u" in o) { this.imm = o; } // OperandReg. // else if ("name" in o) { this.reg = o; } // OperandPtr. // else if ("off" in o) { this.ptr = o; } // OperandMem. // else { this.mem = o; } } } const ReqField = ZyjsField("EncReq"); export class EncoderRequest { resource: Resource; constructor(
from?: DecodedInsn) {
this.resource = new Resource(zydis.asm.zyjsNewEncReq(from?.resource.ref() ?? 0)); } encode(at?: bigint) { return withStack((alloc) => { const tmp = alloc.allocate(MAX_INSN_LENGTH); const length = at != null ? zydis.asm.zyjsEncReqEncodeAbs(this.resource.ref(), tmp, at) : zydis.asm.zyjsEncReqEncode(this.resource.ref(), tmp); return zydis.HEAPU8.slice(tmp, tmp + length); }); } @ReqField machineMode!: MachineMode; @ReqField allowedEncodings!: EncodableEncoding; @ReqField mnemonic!: Mnemonic; @ReqField prefixes!: bigint; @ReqField branchType!: BranchType; @ReqField branchWidth!: BranchWidth; @ReqField addressSizeHint!: AddressSizeHint; @ReqField operandSizeHint!: OperandSizeHint; @ReqField operandCount!: number; @ReqField evexBroadcast!: BroadcastMode; @ReqField evexRounding!: RoundingMode; @ReqField evexSAE!: 1 | 0; @ReqField evexZeroingMask!: 1 | 0; @ReqField mvexBroadcast!: BroadcastMode; @ReqField mvexRounding!: RoundingMode; @ReqField mvexSAE!: 1 | 0; @ReqField mvexConversion!: ConversionMode; @ReqField mvexSwizzle!: SwizzleMode; @ReqField mvexEvictionHint!: 1 | 0; operand(n: number): EncoderOperand { const ptr = zydis.asm.zyjsEncReqRefOperand(this.resource.ref(), n); if (!ptr) { throw RangeError("Operand out of boundaries."); } return new EncoderOperand(this.resource.subresource(ptr)); } static from(machine: MachineMode, mnemonic: Mnemonic, ...operands: OperandLike[]) { const res = new EncoderRequest(); res.mnemonic = mnemonic; res.operandCount = operands.length; res.machineMode = machine; for (let n = 0; n !== operands.length; n++) { const op = operands[n]; res.operand(n).set(op); if (typeof op === "object" && "seg" in op) { switch (op.seg) { case Register.CS: res.prefixes = InsnAttribute.HAS_SEGMENT_CS; break; case Register.DS: res.prefixes = InsnAttribute.HAS_SEGMENT_DS; break; case Register.ES: res.prefixes = InsnAttribute.HAS_SEGMENT_ES; break; case Register.GS: res.prefixes = InsnAttribute.HAS_SEGMENT_GS; break; case Register.FS: res.prefixes = InsnAttribute.HAS_SEGMENT_FS; break; case Register.SS: res.prefixes = InsnAttribute.HAS_SEGMENT_SS; break; } } } return res; } } export function encode(machine: MachineMode, mnemonic: Mnemonic, ...operands: OperandLike[]) { return EncoderRequest.from(machine, mnemonic, ...operands).encode(); } export function encodeAt(at: bigint, machine: MachineMode, mnemonic: Mnemonic, ...operands: OperandLike[]) { return EncoderRequest.from(machine, mnemonic, ...operands).encode(at); } export function encodeNop(len: number) { return (len <= 512 ? withStack : withHeap)((alloc) => { const tmp = alloc.allocate(MAX_INSN_LENGTH); zydis.asm.zyjsEncNop(tmp, len); const res = zydis.HEAPU8.slice(tmp, tmp + len); alloc.free(tmp); return res; }); }
src/encoder.ts
zyantific-zydis-js-4715bb8
[ { "filename": "src/decoder.ts", "retrieved_chunk": "const InsnField = ZyjsField(\"DecInsn\");\nexport class DecodedInsn {\n\tresource: Resource;\n\tconstructor(rsrc: Resource) {\n\t\tthis.resource = rsrc;\n\t}\n\t// Common information.\n\t//\n\t@InsnField\n\tlength!: number;", "score": 30.5715663067595 }, { "filename": "src/formatter.ts", "retrieved_chunk": "import { FormatterProperty, FormatterStyle } from \"./enums.js\";\nimport { HeapAllocator, Resource, withStack } from \"./util.js\";\nimport zydis from \"./native.js\";\nimport { DecodedInsn, DecodedOperand } from \"./decoder.js\";\nconst MAX_LENGTH = 512;\nexport class Formatter {\n\tresource: Resource;\n\tconstructor(style: FormatterStyle) {\n\t\tthis.resource = new Resource(zydis.asm.zyjsNewFormatter(style));\n\t}", "score": 23.473752384664976 }, { "filename": "src/decoder.ts", "retrieved_chunk": "\tOperandVisibility,\n\tRegister,\n\tRoundingMode,\n\tStackWidth,\n\tSwizzleMode,\n} from \"./enums.js\";\nimport zydis from \"./native.js\";\nimport { Resource, ZyjsField, withStack } from \"./util.js\";\nconst OpField = ZyjsField(\"DecOp\");\nexport class DecodedOperand {", "score": 19.505763771976135 }, { "filename": "src/decoder.ts", "retrieved_chunk": "\t\tif (!ptr) {\n\t\t\tthrow RangeError(\"Operand out of boundaries.\");\n\t\t}\n\t\treturn new DecodedOperand(this.resource.subresource(ptr));\n\t}\n}\nexport class Decoder {\n\tresource: Resource;\n\tconstructor(mode: MachineMode, width: StackWidth) {\n\t\tthis.resource = new Resource(zydis.asm.zyjsNewDecoder(mode, width));", "score": 17.308185063536346 }, { "filename": "src/decoder.ts", "retrieved_chunk": "\tresource: Resource;\n\tconstructor(rsrc: Resource) {\n\t\tthis.resource = rsrc;\n\t}\n\t@OpField\n\tid!: number;\n\t@OpField\n\tvisiblity!: OperandVisibility;\n\t@OpField\n\tactions!: OperandAction;", "score": 16.672360866507077 } ]
typescript
from?: DecodedInsn) {
import { DecodedInsn } from "./decoder.js"; import { Resource, ZyjsField, withHeap, withStack } from "./util.js"; import zydis from "./native.js"; import { AddressSizeHint, BranchType, BranchWidth, BroadcastMode, ConversionMode, EncodableEncoding, InsnAttribute, MAX_INSN_LENGTH, MachineMode, Mnemonic, OperandSizeHint, OperandType, Register, RoundingMode, SwizzleMode, } from "./enums.js"; import { OperandImm, OperandMem, OperandPtr, OperandReg } from "./common.js"; const OpField = ZyjsField("EncOp"); type OperandLike = keyof typeof Register | number | bigint | OperandImm | OperandReg | OperandPtr | OperandMem; export class EncoderOperand { resource: Resource; constructor(rsrc: Resource) { this.resource = rsrc; } @OpField type!: OperandType; @OpField imms!: bigint; @OpField immu!: bigint; @OpField ptrSeg!: number; @OpField ptrOff!: number; @OpField regv!: Register; @OpField regIs4!: 1 | 0; @OpField memBase!: Register; @OpField memIndex!: Register; @OpField memScale!: Register; @OpField memDisp!: bigint; get imm(): OperandImm { if (this.type !== OperandType.IMMEDIATE) { throw TypeError("Operand type mismatch."); } return { s: this.imms, u: this.immu, }; } set imm(o: OperandImm | bigint | number) { this.type = OperandType.IMMEDIATE; if (typeof o === "object") { if ("s" in o) { this.imms = o.s; } else { this.immu = o.u; } } else { this.imms = BigInt(o); } } get reg(): OperandReg { if (this.type !== OperandType.REGISTER) { throw TypeError("Operand type mismatch."); } return { name: this.regv, is4: !!this.regIs4, }; } set reg(o: OperandReg | Register | keyof typeof Register) { this.type = OperandType.REGISTER; if (typeof o === "string") { this.regv = Register[o]; } else if (typeof o === "number") { this.regv = o; } else { this.regv = o.name; this.regIs4 = o.is4 || false ? 1 : 0; } } get ptr(): OperandPtr { if (this.type !== OperandType.POINTER) { throw TypeError("Operand type mismatch."); } return { segv: this.ptrSeg, off: this.ptrOff, }; } set ptr(o: OperandPtr) { this.type = OperandType.POINTER; this.ptrOff = o.off; this.ptrSeg = o.segv; } get mem(): OperandMem { const res: OperandMem = { index: this.memIndex, base: this.memBase, scale: this.memScale, disp: this.memDisp, }; if (res.seg === Register.NONE) { delete res.seg; } if (res.base === Register.NONE) { delete res.base; } if (res.scale === 0 || res.index === Register.NONE) { delete res.index; delete res.scale; } return res; } set mem(o: OperandMem) { this.type = OperandType.MEMORY; this.memBase = o.base ?? Register.NONE; this.memDisp = o.disp ?? 0n; this.memIndex = o.index ?? Register.NONE; this.memScale = o.scale ?? 0; } get() { switch (this.type) { case OperandType.IMMEDIATE: return this.imm; case OperandType.REGISTER: return this.reg; case OperandType.POINTER: return this.ptr; case OperandType.MEMORY: return this.mem; default: throw Error(); } } set(o: OperandLike) { if (typeof o === "number" || typeof o === "bigint") { return void (this.imm = o); } else if (typeof o === "string") { return void (this.reg = o); } // OperandImm if ("s" in o || "u" in o) { this.imm = o; } // OperandReg. // else if ("name" in o) { this.reg = o; } // OperandPtr. // else if ("off" in o) { this.ptr = o; } // OperandMem. // else { this.mem = o; } } } const ReqField = ZyjsField("EncReq"); export class EncoderRequest { resource: Resource; constructor(from?: DecodedInsn) { this.resource = new Resource(zydis.asm.zyjsNewEncReq(from?.resource.ref() ?? 0)); } encode(at?: bigint) { return withStack((alloc) => { const tmp = alloc.allocate(MAX_INSN_LENGTH); const length = at != null ? zydis.asm.zyjsEncReqEncodeAbs(this.resource.ref(), tmp, at) : zydis.asm.zyjsEncReqEncode(this.resource.ref(), tmp); return zydis.HEAPU8.slice(tmp, tmp + length); }); } @ReqField machineMode!: MachineMode; @ReqField allowedEncodings!: EncodableEncoding; @ReqField mnemonic!: Mnemonic; @ReqField prefixes!: bigint; @ReqField branchType!: BranchType; @ReqField branchWidth!: BranchWidth; @ReqField addressSizeHint!: AddressSizeHint; @ReqField operandSizeHint!: OperandSizeHint; @ReqField operandCount!: number; @ReqField evexBroadcast!: BroadcastMode; @ReqField evexRounding!: RoundingMode; @ReqField evexSAE!: 1 | 0; @ReqField evexZeroingMask!: 1 | 0; @ReqField mvexBroadcast!: BroadcastMode; @ReqField mvexRounding!: RoundingMode; @ReqField mvexSAE!: 1 | 0; @ReqField mvexConversion!: ConversionMode; @ReqField mvexSwizzle!: SwizzleMode; @ReqField mvexEvictionHint!: 1 | 0; operand(n: number): EncoderOperand { const ptr = zydis.asm.zyjsEncReqRefOperand(this.resource.ref(), n); if (!ptr) { throw RangeError("Operand out of boundaries."); } return new EncoderOperand(this.resource.subresource(ptr)); } static from(machine: MachineMode, mnemonic: Mnemonic, ...operands: OperandLike[]) { const res = new EncoderRequest(); res.mnemonic = mnemonic; res.operandCount = operands.length; res.machineMode = machine; for (let n = 0; n !== operands.length; n++) { const op = operands[n]; res.operand(n).set(op); if (typeof op === "object" && "seg" in op) { switch (op.seg) { case Register.CS: res.prefixes = InsnAttribute.HAS_SEGMENT_CS; break; case Register.DS: res.prefixes = InsnAttribute.HAS_SEGMENT_DS; break; case Register.ES: res.prefixes = InsnAttribute.HAS_SEGMENT_ES; break; case Register.GS: res.prefixes = InsnAttribute.HAS_SEGMENT_GS; break; case Register.FS: res.prefixes = InsnAttribute.HAS_SEGMENT_FS; break; case Register.SS: res.prefixes = InsnAttribute.HAS_SEGMENT_SS; break; } } } return res; } } export function encode(machine: MachineMode, mnemonic: Mnemonic, ...operands: OperandLike[]) { return EncoderRequest.from(machine, mnemonic, ...operands).encode(); } export function encodeAt(at: bigint, machine: MachineMode, mnemonic: Mnemonic, ...operands: OperandLike[]) { return EncoderRequest.from(machine, mnemonic, ...operands).encode(at); } export function encodeNop(len: number) {
return (len <= 512 ? withStack : withHeap)((alloc) => {
const tmp = alloc.allocate(MAX_INSN_LENGTH); zydis.asm.zyjsEncNop(tmp, len); const res = zydis.HEAPU8.slice(tmp, tmp + len); alloc.free(tmp); return res; }); }
src/encoder.ts
zyantific-zydis-js-4715bb8
[ { "filename": "src/register.ts", "retrieved_chunk": "import { MachineMode, Register, RegisterClass } from \"./enums.js\";\nimport zydis from \"./native.js\";\nexport function encode(cl: RegisterClass, id: number): Register {\n\tconst res = zydis.asm.zyjsRegisterEncode(cl, id);\n\tif (res === Register.NONE) throw Error(\"Failed encoding register.\");\n\treturn res;\n}\nexport function getId(reg: Register): number {\n\treturn zydis.asm.zyjsRegisterGetId(reg);\n}", "score": 43.601568967355405 }, { "filename": "src/register.ts", "retrieved_chunk": "export function getClass(reg: Register): RegisterClass {\n\treturn zydis.asm.zyjsRegisterGetClass(reg);\n}\nexport function getWidth(reg: Register, mode: MachineMode): number {\n\treturn zydis.asm.zyjsRegisterGetWidth(reg, mode);\n}\nexport function getWidthFromClass(cl: RegisterClass, mode: MachineMode): number {\n\treturn zydis.asm.zyjsRegisterClassGetWidth(cl, mode);\n}\nexport function getLargestEnclosing(reg: Register, mode: MachineMode): Register {", "score": 42.826455777159175 }, { "filename": "src/decoder.ts", "retrieved_chunk": "\t@InsnField\n\tmnemonic!: Mnemonic;\n\t@InsnField\n\tencoding!: InstructionEncoding;\n\t@InsnField\n\topcodeMap!: OpcodeMap;\n\t@InsnField\n\topcode!: number;\n\t@InsnField\n\tstackWidth!: number;", "score": 34.14715428876806 }, { "filename": "src/util.ts", "retrieved_chunk": "\t} finally {\n\t\tstackRestore(stack);\n\t}\n}\nexport function withHeap<R extends any>(callback: (allocator: IAllocator) => R): R {\n\treturn callback(HeapAllocator);\n}", "score": 29.468749303292984 }, { "filename": "src/misc.ts", "retrieved_chunk": "export function getVersionString() {\n\tconst { major, minor, patch, build } = getVersion();\n\treturn `v${major}.${minor}.${patch}.${build}`;\n}\nexport function hasFeature(feature: Feature): boolean {\n\treturn !!zydis.asm.zyjsHasFeature(feature);\n}", "score": 29.17700256092266 } ]
typescript
return (len <= 512 ? withStack : withHeap)((alloc) => {
import { OperandImm, OperandMem, OperandPtr, OperandReg } from "./common.js"; import { BranchType, BroadcastMode, ConversionMode, DecoderMode, ElementType, ExceptionClass, ISAExt, ISASet, InsnCategory, InstructionEncoding, MachineMode, MaskMode, MemoryOperandType, Mnemonic, OpcodeMap, OperandAction, OperandEncoding, OperandType, OperandVisibility, Register, RoundingMode, StackWidth, SwizzleMode, } from "./enums.js"; import zydis from "./native.js"; import { Resource, ZyjsField, withStack } from "./util.js"; const OpField = ZyjsField("DecOp"); export class DecodedOperand { resource: Resource; constructor(rsrc: Resource) { this.resource = rsrc; } @OpField id!: number; @OpField visiblity!: OperandVisibility; @OpField actions!: OperandAction; @OpField encoding!: OperandEncoding; @OpField size!: number; @OpField elementType!: ElementType; @OpField elementSize!: number; @OpField elementCount!: number; @OpField attributes!: number; @OpField type!: OperandType; @OpField protected immSigned!: number; @OpField protected immRelative!: number; @OpField protected imms!: bigint; @OpField protected immu!: bigint; @OpField protected ptrSeg!: number; @OpField protected ptrOff!: number; @OpField protected regv!: Register; @OpField protected memType!: MemoryOperandType; @OpField protected memSeg!: Register; @OpField protected memBase!: Register; @OpField protected memIndex!: Register; @OpField protected memScale!: Register; @OpField protected memHasDisp!: number; @OpField protected memDisp!: bigint; get imm(): OperandImm { if (this.type !== OperandType.IMMEDIATE) { throw TypeError("Operand type mismatch."); } if (this.immSigned) { return { s: this.imms, rel: !!this.immRelative, }; } else { return { u: this.immu, rel: !!this.immRelative, }; } } get reg(): OperandReg { if (this.type !== OperandType.REGISTER) { throw TypeError("Operand type mismatch."); } return { name: this.regv, is4: this.encoding === OperandEncoding.IS4, }; } get ptr(): OperandPtr { if (this.type !== OperandType.POINTER) { throw TypeError("Operand type mismatch."); } return { segv: this.ptrSeg, off: this.ptrOff, }; } get mem(): OperandMem { if (this.type !== OperandType.MEMORY) { throw TypeError("Operand type mismatch."); } const res: OperandMem = { type: this.memType, seg: this.memSeg, index: this.memIndex, base: this.memBase, scale: this.memScale, disp: this.memHasDisp ? this.memDisp : undefined, }; if (res.seg === Register.NONE) { delete res.seg; } if (res.base === Register.NONE) { delete res.base; } if (res.scale === 0 || res.index === Register.NONE) { delete res.index; delete res.scale; } return res; } get() { switch (this.type) { case OperandType.IMMEDIATE: return this.imm; case OperandType.REGISTER: return this.reg; case OperandType.POINTER: return this.ptr; case OperandType.MEMORY: return this.mem; default: throw Error(); } } } const FlagField = ZyjsField("FlagSet"); interface AccessedFlags { readonly tested: number; readonly modified: number; readonly set: number; readonly unset: number; readonly undef: number; } const flagsNoop: AccessedFlags = { tested: 0, modified: 0, set: 0, unset: 0, undef: 0, }; class AccessedFlagsByPtr implements AccessedFlags { resource: { ref(): number }; constructor(ptr: number) { this.resource = { ref() { return ptr; }, }; } @FlagField readonly tested!: number; @FlagField readonly modified!: number; @FlagField readonly set!: number; @FlagField readonly unset!: number; @FlagField readonly undef!: number; } const InsnField = ZyjsField("DecInsn"); export class DecodedInsn { resource: Resource; constructor(rsrc: Resource) { this.resource = rsrc; } // Common information. // @InsnField length!: number; @InsnField mnemonic!: Mnemonic; @InsnField encoding!: InstructionEncoding; @InsnField opcodeMap!: OpcodeMap; @InsnField opcode!: number; @InsnField stackWidth!: number; @InsnField operandWidth!: number; @InsnField addressWidth!: number; @InsnField operandCount!: number; @InsnField visibleOperandCount!: number; @InsnField attributes!: bigint; // AVX. // @InsnField vectorLength!: number; @InsnField maskMode!: MaskMode; @InsnField maskReg!: Register; @InsnField isBroadcastStatic!: 1 | 0; @InsnField broadcastMode!: BroadcastMode; @InsnField roundingMode!: RoundingMode; @InsnField swizzleMode!: SwizzleMode; @InsnField conversionMode!: ConversionMode; @InsnField hasSAE!: 1 | 0; @InsnField hasEvictionHint!: 1 | 0; // Meta. // @InsnField category!: InsnCategory; @InsnField isaSet!: ISASet; @InsnField isaExt!: ISAExt; @InsnField branchType!: BranchType; @InsnField exceptionClass!: ExceptionClass; // Flags. // @InsnField private cpuFlagsPtr!: number; @InsnField private fpuFlagsPtr!: number; get cpuFlags(): AccessedFlags { const ptr = this.cpuFlagsPtr; return ptr ? new AccessedFlagsByPtr(ptr) : flagsNoop; } get fpuFlags(): AccessedFlags { const ptr = this.fpuFlagsPtr; return ptr ? new AccessedFlagsByPtr(ptr) : flagsNoop; } operand(n: number): DecodedOperand { const ptr =
zydis.asm.zyjsDecInsnRefOperand(this.resource.ref(), n);
if (!ptr) { throw RangeError("Operand out of boundaries."); } return new DecodedOperand(this.resource.subresource(ptr)); } } export class Decoder { resource: Resource; constructor(mode: MachineMode, width: StackWidth) { this.resource = new Resource(zydis.asm.zyjsNewDecoder(mode, width)); } set(mode: DecoderMode, value: boolean) { zydis.asm.zyjsDecoderSetMode(this.resource.ref(), mode, value ? 1 : 0); return this; } decode(buffer: Uint8Array) { const ptr = withStack((a) => { return zydis.asm.zyjsDecoderDecode(this.resource.ref(), a.buf(buffer), buffer.length); }); return new DecodedInsn(new Resource(ptr)); } }
src/decoder.ts
zyantific-zydis-js-4715bb8
[ { "filename": "src/util.ts", "retrieved_chunk": "\t\tif (this.#ptr) {\n\t\t\tResource.finalizer.register(this, this.#ptr, this.#token);\n\t\t}\n\t}\n\tsubresource(ptr: number) {\n\t\tconst res = new Resource(0);\n\t\tres.#parent = this;\n\t\tres.#ptr = ptr;\n\t\treturn res;\n\t}", "score": 70.06633134547498 }, { "filename": "src/util.ts", "retrieved_chunk": "\t\tzydis.stringToUTF8(s, ptr, Infinity);\n\t\treturn ptr;\n\t}\n\tbuf(input: ArrayLike<number>): number {\n\t\tconst ptr = this.allocate(input.length);\n\t\tzydis.HEAPU8.set(input, ptr);\n\t\treturn ptr;\n\t}\n}\nexport const StackAllocator = new (class StackAllocator extends IAllocator {", "score": 69.0289002498906 }, { "filename": "src/util.ts", "retrieved_chunk": "\tref() {\n\t\treturn this.#ptr;\n\t}\n\tunref() {\n\t\tif (this.#ptr && !this.#parent) {\n\t\t\tHeapAllocator.free(this.#ptr);\n\t\t\tthis.#ptr = 0;\n\t\t\tResource.finalizer.unregister(this.#token);\n\t\t}\n\t}", "score": 60.58392362097876 }, { "filename": "src/encoder.ts", "retrieved_chunk": "\t@ReqField\n\tmvexConversion!: ConversionMode;\n\t@ReqField\n\tmvexSwizzle!: SwizzleMode;\n\t@ReqField\n\tmvexEvictionHint!: 1 | 0;\n\toperand(n: number): EncoderOperand {\n\t\tconst ptr = zydis.asm.zyjsEncReqRefOperand(this.resource.ref(), n);\n\t\tif (!ptr) {\n\t\t\tthrow RangeError(\"Operand out of boundaries.\");", "score": 60.330428923482636 }, { "filename": "src/util.ts", "retrieved_chunk": "\t\tzydis.asm.free(p);\n\t}\n})();\nexport class Resource {\n\tstatic readonly finalizer = new FinalizationRegistry((heldValue) => {});\n\t#parent?: Resource;\n\t#ptr: number;\n\treadonly #token = {};\n\tconstructor(ptr: number) {\n\t\tthis.#ptr = ptr;", "score": 59.75860027530673 } ]
typescript
zydis.asm.zyjsDecInsnRefOperand(this.resource.ref(), n);
import { NativeModules, NativeEventEmitter, Platform } from 'react-native'; import { Permissions } from '../permissions'; import { PitchDetectorError } from '../erros'; import { merge } from '../utils'; import { type Callback, type NativeModuleImplementation, type PitchDetectorConfig, type Subscription, type PitchDetectorAndroidConfig, type PitchDetectorIOSConfig, PitchDetectorErrors, } from '../../types'; export class InternalPitchDetector { private module?: NativeModuleImplementation; private event?: NativeEventEmitter; constructor() { this.module = NativeModules?.PitchDetectorModule; if (this.module) { this.event = new NativeEventEmitter(this.module); } else { /* istanbul ignore next */ throw new PitchDetectorError(PitchDetectorErrors.LINKING_ERROR); } } /** * Returns a default PitchDetector configs * @returns PitchDetectorConfig * @example * ```ts * { * android: { * algorithm: 'YIN', * bufferOverLap: 0, * bufferSize: 1024, * sampleRate: 22050, * }, * ios: { * algorithm: 'YIN', * bufferSize: 1024, * } * } */ private getDefaultConfig(): PitchDetectorConfig { return { android: { algorithm: 'YIN', bufferOverLap: 0, bufferSize: 1024, sampleRate: 22050, }, ios: { algorithm: 'YIN', bufferSize: 1024, }, }; } /** * Get current audio permission * @returns Promise<boolean> */ private async hasPermissions(): Promise<boolean> {
return !!(await Permissions.audio());
} /** * Get current status * @returns Promise<boolean> */ async isRecording(): Promise<boolean> { try { const status = await this.module?.isRecording(); return !!status; } catch (err) { console.warn(err); return false; } } /** * Trigger audio recording and pitch detection with provided configs * @param config * @returns Promise<void> */ async start(config?: PitchDetectorConfig): Promise<void> { try { const permission = await this.hasPermissions(); if (!permission) { throw new PitchDetectorError(PitchDetectorErrors.PERMISSIONS_ERROR); } const configuration = merge<PitchDetectorConfig>( this.getDefaultConfig(), config ?? {} ); const params = Platform.select({ android: configuration.android as unknown, ios: configuration.ios as unknown, }) as PitchDetectorIOSConfig | PitchDetectorAndroidConfig; await this.module?.start(params); } catch (err: unknown) { console.warn(err); } } /** * Stop audio recording and pitch detection * @returns Promise<void> */ async stop(): Promise<void> { try { await this.module?.stop(); } catch (err: unknown) { console.warn(err); } } /** * Register a event listener */ addListener(callback: Callback): Subscription { return this.event?.addListener('data', callback); } /** * Method event listeners * @alias removeAllListeners */ removeListener(): void { this.event?.removeAllListeners('data'); } /** * Method remove all event listeners */ removeAllListeners(): void { this.event?.removeAllListeners('data'); } } /** * Export an instance of InternalPitchDetector */ export const PitchDetector = new InternalPitchDetector();
src/internal/pitch-detector/index.ts
1fabiopereira-react-native-pitch-detector-b70fc00
[ { "filename": "src/types/index.ts", "retrieved_chunk": " * Stop audio recording and pitch detection\n * @returns Promise<void>\n */\n stop: () => Promise<void>;\n /**\n * Get current status\n * @returns Promise<boolean>\n */\n isRecording: () => Promise<void>;\n}", "score": 27.27789324058112 }, { "filename": "src/types/index.ts", "retrieved_chunk": "export type Response = Promise<PermissionStatus | boolean | null>;\n/**\n * Permission handler\n */\nexport type PermissionsHandlers = {\n /**\n * Request permission\n * @returns Promise<PermissionStatus | boolean | null>\n */\n RequestPermission: (rationale?: Rationale, compare?: Comparable) => Response;", "score": 16.130338689772763 }, { "filename": "src/types/index.ts", "retrieved_chunk": " /**\n * Check permission\n * @returns Promise<PermissionStatus | boolean | null>\n */\n CheckPermission: (compare?: Comparable) => Response;\n};\nexport enum PitchDetectorErrors {\n BASE,\n LINKING_ERROR,\n PERMISSIONS_ERROR,", "score": 14.56439907965408 }, { "filename": "src/types/index.ts", "retrieved_chunk": "export interface NativeModuleImplementation extends NativeModule {\n /**\n * Start audio recording and pitch detection with provided configs\n * @param config\n * @returns Promise<void>\n */\n start: (\n config: PitchDetectorAndroidConfig | PitchDetectorIOSConfig\n ) => Promise<void>;\n /**", "score": 12.052737228062485 }, { "filename": "src/internal/permissions/index.ts", "retrieved_chunk": " const denied = await audio.CheckPermission(isDenied);\n const blocked = await audio.CheckPermission(isBlocked);\n if (denied || blocked) {\n return await audio.RequestPermission(rationale, isGranted);\n }\n return await audio.CheckPermission(isGranted);\n }\n}", "score": 9.968566703665905 } ]
typescript
return !!(await Permissions.audio());
import { DecodedInsn } from "./decoder.js"; import { Resource, ZyjsField, withHeap, withStack } from "./util.js"; import zydis from "./native.js"; import { AddressSizeHint, BranchType, BranchWidth, BroadcastMode, ConversionMode, EncodableEncoding, InsnAttribute, MAX_INSN_LENGTH, MachineMode, Mnemonic, OperandSizeHint, OperandType, Register, RoundingMode, SwizzleMode, } from "./enums.js"; import { OperandImm, OperandMem, OperandPtr, OperandReg } from "./common.js"; const OpField = ZyjsField("EncOp"); type OperandLike = keyof typeof Register | number | bigint | OperandImm | OperandReg | OperandPtr | OperandMem; export class EncoderOperand { resource: Resource; constructor(rsrc: Resource) { this.resource = rsrc; } @OpField type!: OperandType; @OpField imms!: bigint; @OpField immu!: bigint; @OpField ptrSeg!: number; @OpField ptrOff!: number; @OpField regv!: Register; @OpField regIs4!: 1 | 0; @OpField memBase!: Register; @OpField memIndex!: Register; @OpField memScale!: Register; @OpField memDisp!: bigint; get imm(): OperandImm { if (this.type !== OperandType.IMMEDIATE) { throw TypeError("Operand type mismatch."); } return { s: this.imms, u: this.immu, }; } set imm(o: OperandImm | bigint | number) { this.type = OperandType.IMMEDIATE; if (typeof o === "object") { if ("s" in o) { this.imms = o.s; } else { this.immu = o.u; } } else { this.imms = BigInt(o); } } get reg(): OperandReg { if (this.type !== OperandType.REGISTER) { throw TypeError("Operand type mismatch."); } return { name: this.regv, is4: !!this.regIs4, }; } set reg(o: OperandReg | Register | keyof typeof Register) { this.type = OperandType.REGISTER; if (typeof o === "string") { this.regv = Register[o]; } else if (typeof o === "number") { this.regv = o; } else { this.regv = o.name; this.regIs4 = o.is4 || false ? 1 : 0; } } get ptr(): OperandPtr { if (this.type !== OperandType.POINTER) { throw TypeError("Operand type mismatch."); } return { segv: this.ptrSeg, off: this.ptrOff, }; } set ptr(o: OperandPtr) { this.type = OperandType.POINTER; this.ptrOff = o.off; this.ptrSeg = o.segv; } get mem(): OperandMem { const res: OperandMem = { index: this.memIndex, base: this.memBase, scale: this.memScale, disp: this.memDisp, }; if (res.seg === Register.NONE) { delete res.seg; } if (res.base === Register.NONE) { delete res.base; } if (res.scale === 0 || res.index === Register.NONE) { delete res.index; delete res.scale; } return res; } set mem(o: OperandMem) { this.type = OperandType.MEMORY; this.memBase = o.base ?? Register.NONE; this.memDisp = o.disp ?? 0n; this.memIndex = o.index ?? Register.NONE; this.memScale = o.scale ?? 0; } get() { switch (this.type) { case OperandType.IMMEDIATE: return this.imm; case OperandType.REGISTER: return this.reg; case OperandType.POINTER: return this.ptr; case OperandType.MEMORY: return this.mem; default: throw Error(); } } set(o: OperandLike) { if (typeof o === "number" || typeof o === "bigint") { return void (this.imm = o); } else if (typeof o === "string") { return void (this.reg = o); } // OperandImm if ("s" in o || "u" in o) { this.imm = o; } // OperandReg. // else if ("name" in o) { this.reg = o; } // OperandPtr. // else if ("off" in o) { this.ptr = o; } // OperandMem. // else { this.mem = o; } } } const ReqField = ZyjsField("EncReq"); export class EncoderRequest { resource: Resource; constructor(from?: DecodedInsn) {
this.resource = new Resource(zydis.asm.zyjsNewEncReq(from?.resource.ref() ?? 0));
} encode(at?: bigint) { return withStack((alloc) => { const tmp = alloc.allocate(MAX_INSN_LENGTH); const length = at != null ? zydis.asm.zyjsEncReqEncodeAbs(this.resource.ref(), tmp, at) : zydis.asm.zyjsEncReqEncode(this.resource.ref(), tmp); return zydis.HEAPU8.slice(tmp, tmp + length); }); } @ReqField machineMode!: MachineMode; @ReqField allowedEncodings!: EncodableEncoding; @ReqField mnemonic!: Mnemonic; @ReqField prefixes!: bigint; @ReqField branchType!: BranchType; @ReqField branchWidth!: BranchWidth; @ReqField addressSizeHint!: AddressSizeHint; @ReqField operandSizeHint!: OperandSizeHint; @ReqField operandCount!: number; @ReqField evexBroadcast!: BroadcastMode; @ReqField evexRounding!: RoundingMode; @ReqField evexSAE!: 1 | 0; @ReqField evexZeroingMask!: 1 | 0; @ReqField mvexBroadcast!: BroadcastMode; @ReqField mvexRounding!: RoundingMode; @ReqField mvexSAE!: 1 | 0; @ReqField mvexConversion!: ConversionMode; @ReqField mvexSwizzle!: SwizzleMode; @ReqField mvexEvictionHint!: 1 | 0; operand(n: number): EncoderOperand { const ptr = zydis.asm.zyjsEncReqRefOperand(this.resource.ref(), n); if (!ptr) { throw RangeError("Operand out of boundaries."); } return new EncoderOperand(this.resource.subresource(ptr)); } static from(machine: MachineMode, mnemonic: Mnemonic, ...operands: OperandLike[]) { const res = new EncoderRequest(); res.mnemonic = mnemonic; res.operandCount = operands.length; res.machineMode = machine; for (let n = 0; n !== operands.length; n++) { const op = operands[n]; res.operand(n).set(op); if (typeof op === "object" && "seg" in op) { switch (op.seg) { case Register.CS: res.prefixes = InsnAttribute.HAS_SEGMENT_CS; break; case Register.DS: res.prefixes = InsnAttribute.HAS_SEGMENT_DS; break; case Register.ES: res.prefixes = InsnAttribute.HAS_SEGMENT_ES; break; case Register.GS: res.prefixes = InsnAttribute.HAS_SEGMENT_GS; break; case Register.FS: res.prefixes = InsnAttribute.HAS_SEGMENT_FS; break; case Register.SS: res.prefixes = InsnAttribute.HAS_SEGMENT_SS; break; } } } return res; } } export function encode(machine: MachineMode, mnemonic: Mnemonic, ...operands: OperandLike[]) { return EncoderRequest.from(machine, mnemonic, ...operands).encode(); } export function encodeAt(at: bigint, machine: MachineMode, mnemonic: Mnemonic, ...operands: OperandLike[]) { return EncoderRequest.from(machine, mnemonic, ...operands).encode(at); } export function encodeNop(len: number) { return (len <= 512 ? withStack : withHeap)((alloc) => { const tmp = alloc.allocate(MAX_INSN_LENGTH); zydis.asm.zyjsEncNop(tmp, len); const res = zydis.HEAPU8.slice(tmp, tmp + len); alloc.free(tmp); return res; }); }
src/encoder.ts
zyantific-zydis-js-4715bb8
[ { "filename": "src/formatter.ts", "retrieved_chunk": "import { FormatterProperty, FormatterStyle } from \"./enums.js\";\nimport { HeapAllocator, Resource, withStack } from \"./util.js\";\nimport zydis from \"./native.js\";\nimport { DecodedInsn, DecodedOperand } from \"./decoder.js\";\nconst MAX_LENGTH = 512;\nexport class Formatter {\n\tresource: Resource;\n\tconstructor(style: FormatterStyle) {\n\t\tthis.resource = new Resource(zydis.asm.zyjsNewFormatter(style));\n\t}", "score": 47.97496452720841 }, { "filename": "src/decoder.ts", "retrieved_chunk": "const InsnField = ZyjsField(\"DecInsn\");\nexport class DecodedInsn {\n\tresource: Resource;\n\tconstructor(rsrc: Resource) {\n\t\tthis.resource = rsrc;\n\t}\n\t// Common information.\n\t//\n\t@InsnField\n\tlength!: number;", "score": 47.21850686511473 }, { "filename": "src/decoder.ts", "retrieved_chunk": "\t\tif (!ptr) {\n\t\t\tthrow RangeError(\"Operand out of boundaries.\");\n\t\t}\n\t\treturn new DecodedOperand(this.resource.subresource(ptr));\n\t}\n}\nexport class Decoder {\n\tresource: Resource;\n\tconstructor(mode: MachineMode, width: StackWidth) {\n\t\tthis.resource = new Resource(zydis.asm.zyjsNewDecoder(mode, width));", "score": 41.26995596379069 }, { "filename": "src/decoder.ts", "retrieved_chunk": "\t}\n\tset(mode: DecoderMode, value: boolean) {\n\t\tzydis.asm.zyjsDecoderSetMode(this.resource.ref(), mode, value ? 1 : 0);\n\t\treturn this;\n\t}\n\tdecode(buffer: Uint8Array) {\n\t\tconst ptr = withStack((a) => {\n\t\t\treturn zydis.asm.zyjsDecoderDecode(this.resource.ref(), a.buf(buffer), buffer.length);\n\t\t});\n\t\treturn new DecodedInsn(new Resource(ptr));", "score": 39.0250720582389 }, { "filename": "src/decoder.ts", "retrieved_chunk": "\tresource: Resource;\n\tconstructor(rsrc: Resource) {\n\t\tthis.resource = rsrc;\n\t}\n\t@OpField\n\tid!: number;\n\t@OpField\n\tvisiblity!: OperandVisibility;\n\t@OpField\n\tactions!: OperandAction;", "score": 34.33091367339931 } ]
typescript
this.resource = new Resource(zydis.asm.zyjsNewEncReq(from?.resource.ref() ?? 0));
import { NativeModules } from 'react-native'; import { PitchDetectorErrors } from '../../../types'; import { PitchDetectorError } from '../../erros'; import { Permissions } from '../../permissions'; import { PitchDetector } from '..'; const Module = NativeModules.PitchDetectorModule; const asyncMock = <T>(value: T) => jest.fn().mockResolvedValue(value); const asyncMockThrow = <T>(error: T) => jest.fn().mockImplementation(() => { throw error; }); describe('PitchDetector', () => { beforeEach(() => jest.clearAllMocks()); it.each([ ['start', 1], ['isRecording', 1], ['stop', 1], ])( 'should call %s method %s time(s) from native module', async (method: string, times: number) => { const spy = jest.spyOn(Module, method as any); await Object(PitchDetector)[method](); expect(spy).toBeCalledTimes(times); } ); it.each([ ['hasPermissions', 'start'], ['getDefaultConfig', 'start'], ])( 'should call %s method when %s method will be called', async (target: string, method: string) => { const spy = jest.spyOn(PitchDetector, target as any); await Object(PitchDetector)[method](); expect(spy).toBeCalledTimes(1); } ); it('should call audio permission method when start method will be called', async () => { const spy = jest.spyOn(Permissions, 'audio'); await PitchDetector.start(); expect(spy).toBeCalledTimes(1); }); it('should throw error when start method will be called and not have audio record permission', async () => { const
error = new PitchDetectorError(PitchDetectorErrors.PERMISSIONS_ERROR);
const spy = jest.spyOn(console, 'warn'); Permissions.audio = asyncMock(false); await PitchDetector.start(); expect(spy).toBeCalledTimes(1); expect(spy).toHaveBeenCalledWith(error); }); it.each([ ['addListener', 'addListener'], ['removeAllListeners', 'removeAllListeners'], ['removeAllListeners', 'removeListener'], ])( 'should call %s method from event emitter when %s method will be called', async (target: string, method: string) => { const spy = jest.spyOn(Object(PitchDetector).event, target as any); await Object(PitchDetector)[method](1); expect(spy).toBeCalledTimes(1); } ); it.each([['start'], ['stop'], ['isRecording']])( 'should throw error when native %s method fail', async (method: string) => { const error = new Error('Some error message'); const spy = jest.spyOn(console, 'warn'); Permissions.audio = asyncMock(true); Object(PitchDetector).module[method] = asyncMockThrow(error); await Object(PitchDetector)[method](); expect(spy).toBeCalledTimes(1); expect(spy).toBeCalledWith(error); } ); });
src/internal/pitch-detector/__tests__/pitch-detector.spec.ts
1fabiopereira-react-native-pitch-detector-b70fc00
[ { "filename": "src/internal/erros/__tests__/errors.spec.ts", "retrieved_chunk": " it('should create link error', () => {\n const error: any = new PitchDetectorError(\n PitchDetectorErrors.LINKING_ERROR\n );\n expect(error?.message).toMatch(/doesn't seem to be linked/);\n });\n it('should create permission error', () => {\n const error: any = new PitchDetectorError(\n PitchDetectorErrors.PERMISSIONS_ERROR\n );", "score": 36.63913107286463 }, { "filename": "src/internal/utils/__tests__/utils.spec.ts", "retrieved_chunk": " 'should return %s when isObject function will be called with %s',\n (expected: boolean, _: string, params: any) => {\n const result = isObject(params);\n expect(result).toBe(expected);\n }\n );\n it('should not merge when one params is not object', () => {\n const result = merge({ name: 'Fábio' }, NaN);\n expect(result).toHaveProperty('name');\n });", "score": 35.55844553304729 }, { "filename": "src/internal/pitch-detector/index.ts", "retrieved_chunk": " * @returns Promise<void>\n */\n async start(config?: PitchDetectorConfig): Promise<void> {\n try {\n const permission = await this.hasPermissions();\n if (!permission) {\n throw new PitchDetectorError(PitchDetectorErrors.PERMISSIONS_ERROR);\n }\n const configuration = merge<PitchDetectorConfig>(\n this.getDefaultConfig(),", "score": 29.129851604854313 }, { "filename": "src/internal/erros/index.ts", "retrieved_chunk": " android: '',\n }) +\n '- You rebuilt the app after installing the package\\n' +\n '- You are not using Expo Go\\n';\nconst permission =\n `The package 'react-native-pitch-detector' need audio record permission. Make sure: \\n\\n` +\n Platform.select({\n ios: '- You have added Microphone access permission before start record.',\n android: `- You have added '<uses-permission android:name=\"android.permission.RECORD_AUDIO\" />' on AndroidManifest.xml and request permission before start record.\\n`,\n });", "score": 24.379884557800352 }, { "filename": "src/internal/erros/__tests__/errors.spec.ts", "retrieved_chunk": " expect(error?.message).toMatch(/need audio record permission/);\n });\n});", "score": 23.655130774112525 } ]
typescript
error = new PitchDetectorError(PitchDetectorErrors.PERMISSIONS_ERROR);
import { DecodedInsn } from "./decoder.js"; import { Resource, ZyjsField, withHeap, withStack } from "./util.js"; import zydis from "./native.js"; import { AddressSizeHint, BranchType, BranchWidth, BroadcastMode, ConversionMode, EncodableEncoding, InsnAttribute, MAX_INSN_LENGTH, MachineMode, Mnemonic, OperandSizeHint, OperandType, Register, RoundingMode, SwizzleMode, } from "./enums.js"; import { OperandImm, OperandMem, OperandPtr, OperandReg } from "./common.js"; const OpField = ZyjsField("EncOp"); type OperandLike = keyof typeof Register | number | bigint | OperandImm | OperandReg | OperandPtr | OperandMem; export class EncoderOperand { resource: Resource; constructor(rsrc: Resource) { this.resource = rsrc; } @OpField type!: OperandType; @OpField imms!: bigint; @OpField immu!: bigint; @OpField ptrSeg!: number; @OpField ptrOff!: number; @OpField regv!: Register; @OpField regIs4!: 1 | 0; @OpField memBase!: Register; @OpField memIndex!: Register; @OpField memScale!: Register; @OpField memDisp!: bigint; get imm(): OperandImm { if (this.type !== OperandType.IMMEDIATE) { throw TypeError("Operand type mismatch."); } return { s: this.imms, u: this.immu, }; } set imm(
o: OperandImm | bigint | number) {
this.type = OperandType.IMMEDIATE; if (typeof o === "object") { if ("s" in o) { this.imms = o.s; } else { this.immu = o.u; } } else { this.imms = BigInt(o); } } get reg(): OperandReg { if (this.type !== OperandType.REGISTER) { throw TypeError("Operand type mismatch."); } return { name: this.regv, is4: !!this.regIs4, }; } set reg(o: OperandReg | Register | keyof typeof Register) { this.type = OperandType.REGISTER; if (typeof o === "string") { this.regv = Register[o]; } else if (typeof o === "number") { this.regv = o; } else { this.regv = o.name; this.regIs4 = o.is4 || false ? 1 : 0; } } get ptr(): OperandPtr { if (this.type !== OperandType.POINTER) { throw TypeError("Operand type mismatch."); } return { segv: this.ptrSeg, off: this.ptrOff, }; } set ptr(o: OperandPtr) { this.type = OperandType.POINTER; this.ptrOff = o.off; this.ptrSeg = o.segv; } get mem(): OperandMem { const res: OperandMem = { index: this.memIndex, base: this.memBase, scale: this.memScale, disp: this.memDisp, }; if (res.seg === Register.NONE) { delete res.seg; } if (res.base === Register.NONE) { delete res.base; } if (res.scale === 0 || res.index === Register.NONE) { delete res.index; delete res.scale; } return res; } set mem(o: OperandMem) { this.type = OperandType.MEMORY; this.memBase = o.base ?? Register.NONE; this.memDisp = o.disp ?? 0n; this.memIndex = o.index ?? Register.NONE; this.memScale = o.scale ?? 0; } get() { switch (this.type) { case OperandType.IMMEDIATE: return this.imm; case OperandType.REGISTER: return this.reg; case OperandType.POINTER: return this.ptr; case OperandType.MEMORY: return this.mem; default: throw Error(); } } set(o: OperandLike) { if (typeof o === "number" || typeof o === "bigint") { return void (this.imm = o); } else if (typeof o === "string") { return void (this.reg = o); } // OperandImm if ("s" in o || "u" in o) { this.imm = o; } // OperandReg. // else if ("name" in o) { this.reg = o; } // OperandPtr. // else if ("off" in o) { this.ptr = o; } // OperandMem. // else { this.mem = o; } } } const ReqField = ZyjsField("EncReq"); export class EncoderRequest { resource: Resource; constructor(from?: DecodedInsn) { this.resource = new Resource(zydis.asm.zyjsNewEncReq(from?.resource.ref() ?? 0)); } encode(at?: bigint) { return withStack((alloc) => { const tmp = alloc.allocate(MAX_INSN_LENGTH); const length = at != null ? zydis.asm.zyjsEncReqEncodeAbs(this.resource.ref(), tmp, at) : zydis.asm.zyjsEncReqEncode(this.resource.ref(), tmp); return zydis.HEAPU8.slice(tmp, tmp + length); }); } @ReqField machineMode!: MachineMode; @ReqField allowedEncodings!: EncodableEncoding; @ReqField mnemonic!: Mnemonic; @ReqField prefixes!: bigint; @ReqField branchType!: BranchType; @ReqField branchWidth!: BranchWidth; @ReqField addressSizeHint!: AddressSizeHint; @ReqField operandSizeHint!: OperandSizeHint; @ReqField operandCount!: number; @ReqField evexBroadcast!: BroadcastMode; @ReqField evexRounding!: RoundingMode; @ReqField evexSAE!: 1 | 0; @ReqField evexZeroingMask!: 1 | 0; @ReqField mvexBroadcast!: BroadcastMode; @ReqField mvexRounding!: RoundingMode; @ReqField mvexSAE!: 1 | 0; @ReqField mvexConversion!: ConversionMode; @ReqField mvexSwizzle!: SwizzleMode; @ReqField mvexEvictionHint!: 1 | 0; operand(n: number): EncoderOperand { const ptr = zydis.asm.zyjsEncReqRefOperand(this.resource.ref(), n); if (!ptr) { throw RangeError("Operand out of boundaries."); } return new EncoderOperand(this.resource.subresource(ptr)); } static from(machine: MachineMode, mnemonic: Mnemonic, ...operands: OperandLike[]) { const res = new EncoderRequest(); res.mnemonic = mnemonic; res.operandCount = operands.length; res.machineMode = machine; for (let n = 0; n !== operands.length; n++) { const op = operands[n]; res.operand(n).set(op); if (typeof op === "object" && "seg" in op) { switch (op.seg) { case Register.CS: res.prefixes = InsnAttribute.HAS_SEGMENT_CS; break; case Register.DS: res.prefixes = InsnAttribute.HAS_SEGMENT_DS; break; case Register.ES: res.prefixes = InsnAttribute.HAS_SEGMENT_ES; break; case Register.GS: res.prefixes = InsnAttribute.HAS_SEGMENT_GS; break; case Register.FS: res.prefixes = InsnAttribute.HAS_SEGMENT_FS; break; case Register.SS: res.prefixes = InsnAttribute.HAS_SEGMENT_SS; break; } } } return res; } } export function encode(machine: MachineMode, mnemonic: Mnemonic, ...operands: OperandLike[]) { return EncoderRequest.from(machine, mnemonic, ...operands).encode(); } export function encodeAt(at: bigint, machine: MachineMode, mnemonic: Mnemonic, ...operands: OperandLike[]) { return EncoderRequest.from(machine, mnemonic, ...operands).encode(at); } export function encodeNop(len: number) { return (len <= 512 ? withStack : withHeap)((alloc) => { const tmp = alloc.allocate(MAX_INSN_LENGTH); zydis.asm.zyjsEncNop(tmp, len); const res = zydis.HEAPU8.slice(tmp, tmp + len); alloc.free(tmp); return res; }); }
src/encoder.ts
zyantific-zydis-js-4715bb8
[ { "filename": "src/decoder.ts", "retrieved_chunk": "\t@OpField\n\tprotected memDisp!: bigint;\n\tget imm(): OperandImm {\n\t\tif (this.type !== OperandType.IMMEDIATE) {\n\t\t\tthrow TypeError(\"Operand type mismatch.\");\n\t\t}\n\t\tif (this.immSigned) {\n\t\t\treturn {\n\t\t\t\ts: this.imms,\n\t\t\t\trel: !!this.immRelative,", "score": 62.24295657143853 }, { "filename": "src/decoder.ts", "retrieved_chunk": "\t\t\tthrow TypeError(\"Operand type mismatch.\");\n\t\t}\n\t\treturn {\n\t\t\tname: this.regv,\n\t\t\tis4: this.encoding === OperandEncoding.IS4,\n\t\t};\n\t}\n\tget ptr(): OperandPtr {\n\t\tif (this.type !== OperandType.POINTER) {\n\t\t\tthrow TypeError(\"Operand type mismatch.\");", "score": 47.21588015403422 }, { "filename": "src/decoder.ts", "retrieved_chunk": "\t\t}\n\t\treturn {\n\t\t\tsegv: this.ptrSeg,\n\t\t\toff: this.ptrOff,\n\t\t};\n\t}\n\tget mem(): OperandMem {\n\t\tif (this.type !== OperandType.MEMORY) {\n\t\t\tthrow TypeError(\"Operand type mismatch.\");\n\t\t}", "score": 43.910098698155316 }, { "filename": "src/decoder.ts", "retrieved_chunk": "\t\t\t};\n\t\t} else {\n\t\t\treturn {\n\t\t\t\tu: this.immu,\n\t\t\t\trel: !!this.immRelative,\n\t\t\t};\n\t\t}\n\t}\n\tget reg(): OperandReg {\n\t\tif (this.type !== OperandType.REGISTER) {", "score": 37.6251149395407 }, { "filename": "src/decoder.ts", "retrieved_chunk": "\tget() {\n\t\tswitch (this.type) {\n\t\t\tcase OperandType.IMMEDIATE:\n\t\t\t\treturn this.imm;\n\t\t\tcase OperandType.REGISTER:\n\t\t\t\treturn this.reg;\n\t\t\tcase OperandType.POINTER:\n\t\t\t\treturn this.ptr;\n\t\t\tcase OperandType.MEMORY:\n\t\t\t\treturn this.mem;", "score": 33.385514326148716 } ]
typescript
o: OperandImm | bigint | number) {
import { NativeModules } from 'react-native'; import { PitchDetectorErrors } from '../../../types'; import { PitchDetectorError } from '../../erros'; import { Permissions } from '../../permissions'; import { PitchDetector } from '..'; const Module = NativeModules.PitchDetectorModule; const asyncMock = <T>(value: T) => jest.fn().mockResolvedValue(value); const asyncMockThrow = <T>(error: T) => jest.fn().mockImplementation(() => { throw error; }); describe('PitchDetector', () => { beforeEach(() => jest.clearAllMocks()); it.each([ ['start', 1], ['isRecording', 1], ['stop', 1], ])( 'should call %s method %s time(s) from native module', async (method: string, times: number) => { const spy = jest.spyOn(Module, method as any); await Object(PitchDetector)[method](); expect(spy).toBeCalledTimes(times); } ); it.each([ ['hasPermissions', 'start'], ['getDefaultConfig', 'start'], ])( 'should call %s method when %s method will be called', async (target: string, method: string) => { const spy = jest.spyOn(PitchDetector, target as any); await Object(PitchDetector)[method](); expect(spy).toBeCalledTimes(1); } ); it('should call audio permission method when start method will be called', async () => { const spy = jest
.spyOn(Permissions, 'audio');
await PitchDetector.start(); expect(spy).toBeCalledTimes(1); }); it('should throw error when start method will be called and not have audio record permission', async () => { const error = new PitchDetectorError(PitchDetectorErrors.PERMISSIONS_ERROR); const spy = jest.spyOn(console, 'warn'); Permissions.audio = asyncMock(false); await PitchDetector.start(); expect(spy).toBeCalledTimes(1); expect(spy).toHaveBeenCalledWith(error); }); it.each([ ['addListener', 'addListener'], ['removeAllListeners', 'removeAllListeners'], ['removeAllListeners', 'removeListener'], ])( 'should call %s method from event emitter when %s method will be called', async (target: string, method: string) => { const spy = jest.spyOn(Object(PitchDetector).event, target as any); await Object(PitchDetector)[method](1); expect(spy).toBeCalledTimes(1); } ); it.each([['start'], ['stop'], ['isRecording']])( 'should throw error when native %s method fail', async (method: string) => { const error = new Error('Some error message'); const spy = jest.spyOn(console, 'warn'); Permissions.audio = asyncMock(true); Object(PitchDetector).module[method] = asyncMockThrow(error); await Object(PitchDetector)[method](); expect(spy).toBeCalledTimes(1); expect(spy).toBeCalledWith(error); } ); });
src/internal/pitch-detector/__tests__/pitch-detector.spec.ts
1fabiopereira-react-native-pitch-detector-b70fc00
[ { "filename": "src/internal/utils/__tests__/utils.spec.ts", "retrieved_chunk": " 'should return %s when isObject function will be called with %s',\n (expected: boolean, _: string, params: any) => {\n const result = isObject(params);\n expect(result).toBe(expected);\n }\n );\n it('should not merge when one params is not object', () => {\n const result = merge({ name: 'Fábio' }, NaN);\n expect(result).toHaveProperty('name');\n });", "score": 43.60037168144081 }, { "filename": "src/internal/permissions/__tests__/permissions.spec.ts", "retrieved_chunk": " ['isBlocked', 'blocked', true, isBlocked],\n ['isDenied', 'denied', true, isDenied],\n ['isGranted', 'granted', true, isGranted],\n ['isLimited', 'limited', true, isLimited],\n ['isUnavailable', 'unavailable', true, isUnavailable],\n ])(\n 'should call %s function and return %s',\n (\n _: string,\n params: any,", "score": 24.83325961697717 }, { "filename": "src/internal/erros/__tests__/errors.spec.ts", "retrieved_chunk": " it('should create link error', () => {\n const error: any = new PitchDetectorError(\n PitchDetectorErrors.LINKING_ERROR\n );\n expect(error?.message).toMatch(/doesn't seem to be linked/);\n });\n it('should create permission error', () => {\n const error: any = new PitchDetectorError(\n PitchDetectorErrors.PERMISSIONS_ERROR\n );", "score": 18.77680073093968 }, { "filename": "src/internal/utils/index.ts", "retrieved_chunk": " const source = sources.shift();\n if (isObject(target) && isObject(source)) {\n for (const key in source) {\n if (isObject(source[key])) {\n if (!target[key]) Object.assign(target, { [key]: {} });\n merge(target[key], source[key]);\n } else {\n Object.assign(target, { [key]: source[key] });\n }\n }", "score": 15.297612768521985 }, { "filename": "src/internal/pitch-detector/index.ts", "retrieved_chunk": " */\n private async hasPermissions(): Promise<boolean> {\n return !!(await Permissions.audio());\n }\n /**\n * Get current status\n * @returns Promise<boolean>\n */\n async isRecording(): Promise<boolean> {\n try {", "score": 13.986598215459834 } ]
typescript
.spyOn(Permissions, 'audio');
import { DecodedInsn } from "./decoder.js"; import { Resource, ZyjsField, withHeap, withStack } from "./util.js"; import zydis from "./native.js"; import { AddressSizeHint, BranchType, BranchWidth, BroadcastMode, ConversionMode, EncodableEncoding, InsnAttribute, MAX_INSN_LENGTH, MachineMode, Mnemonic, OperandSizeHint, OperandType, Register, RoundingMode, SwizzleMode, } from "./enums.js"; import { OperandImm, OperandMem, OperandPtr, OperandReg } from "./common.js"; const OpField = ZyjsField("EncOp"); type OperandLike = keyof typeof Register | number | bigint | OperandImm | OperandReg | OperandPtr | OperandMem; export class EncoderOperand { resource: Resource; constructor(rsrc: Resource) { this.resource = rsrc; } @OpField type!: OperandType; @OpField imms!: bigint; @OpField immu!: bigint; @OpField ptrSeg!: number; @OpField ptrOff!: number; @OpField regv!: Register; @OpField regIs4!: 1 | 0; @OpField memBase!: Register; @OpField memIndex!: Register; @OpField memScale!: Register; @OpField memDisp!: bigint; get imm(): OperandImm { if (this.type !== OperandType.IMMEDIATE) { throw TypeError("Operand type mismatch."); } return { s: this.imms, u: this.immu, }; } set imm(o: OperandImm | bigint | number) { this.type = OperandType.IMMEDIATE; if (typeof o === "object") { if ("s" in o) { this.imms = o.s; } else { this.immu = o.u; } } else { this.imms = BigInt(o); } } get reg(): OperandReg { if (this.type !== OperandType.REGISTER) { throw TypeError("Operand type mismatch."); } return { name: this.regv, is4: !!this.regIs4, }; }
set reg(o: OperandReg | Register | keyof typeof Register) {
this.type = OperandType.REGISTER; if (typeof o === "string") { this.regv = Register[o]; } else if (typeof o === "number") { this.regv = o; } else { this.regv = o.name; this.regIs4 = o.is4 || false ? 1 : 0; } } get ptr(): OperandPtr { if (this.type !== OperandType.POINTER) { throw TypeError("Operand type mismatch."); } return { segv: this.ptrSeg, off: this.ptrOff, }; } set ptr(o: OperandPtr) { this.type = OperandType.POINTER; this.ptrOff = o.off; this.ptrSeg = o.segv; } get mem(): OperandMem { const res: OperandMem = { index: this.memIndex, base: this.memBase, scale: this.memScale, disp: this.memDisp, }; if (res.seg === Register.NONE) { delete res.seg; } if (res.base === Register.NONE) { delete res.base; } if (res.scale === 0 || res.index === Register.NONE) { delete res.index; delete res.scale; } return res; } set mem(o: OperandMem) { this.type = OperandType.MEMORY; this.memBase = o.base ?? Register.NONE; this.memDisp = o.disp ?? 0n; this.memIndex = o.index ?? Register.NONE; this.memScale = o.scale ?? 0; } get() { switch (this.type) { case OperandType.IMMEDIATE: return this.imm; case OperandType.REGISTER: return this.reg; case OperandType.POINTER: return this.ptr; case OperandType.MEMORY: return this.mem; default: throw Error(); } } set(o: OperandLike) { if (typeof o === "number" || typeof o === "bigint") { return void (this.imm = o); } else if (typeof o === "string") { return void (this.reg = o); } // OperandImm if ("s" in o || "u" in o) { this.imm = o; } // OperandReg. // else if ("name" in o) { this.reg = o; } // OperandPtr. // else if ("off" in o) { this.ptr = o; } // OperandMem. // else { this.mem = o; } } } const ReqField = ZyjsField("EncReq"); export class EncoderRequest { resource: Resource; constructor(from?: DecodedInsn) { this.resource = new Resource(zydis.asm.zyjsNewEncReq(from?.resource.ref() ?? 0)); } encode(at?: bigint) { return withStack((alloc) => { const tmp = alloc.allocate(MAX_INSN_LENGTH); const length = at != null ? zydis.asm.zyjsEncReqEncodeAbs(this.resource.ref(), tmp, at) : zydis.asm.zyjsEncReqEncode(this.resource.ref(), tmp); return zydis.HEAPU8.slice(tmp, tmp + length); }); } @ReqField machineMode!: MachineMode; @ReqField allowedEncodings!: EncodableEncoding; @ReqField mnemonic!: Mnemonic; @ReqField prefixes!: bigint; @ReqField branchType!: BranchType; @ReqField branchWidth!: BranchWidth; @ReqField addressSizeHint!: AddressSizeHint; @ReqField operandSizeHint!: OperandSizeHint; @ReqField operandCount!: number; @ReqField evexBroadcast!: BroadcastMode; @ReqField evexRounding!: RoundingMode; @ReqField evexSAE!: 1 | 0; @ReqField evexZeroingMask!: 1 | 0; @ReqField mvexBroadcast!: BroadcastMode; @ReqField mvexRounding!: RoundingMode; @ReqField mvexSAE!: 1 | 0; @ReqField mvexConversion!: ConversionMode; @ReqField mvexSwizzle!: SwizzleMode; @ReqField mvexEvictionHint!: 1 | 0; operand(n: number): EncoderOperand { const ptr = zydis.asm.zyjsEncReqRefOperand(this.resource.ref(), n); if (!ptr) { throw RangeError("Operand out of boundaries."); } return new EncoderOperand(this.resource.subresource(ptr)); } static from(machine: MachineMode, mnemonic: Mnemonic, ...operands: OperandLike[]) { const res = new EncoderRequest(); res.mnemonic = mnemonic; res.operandCount = operands.length; res.machineMode = machine; for (let n = 0; n !== operands.length; n++) { const op = operands[n]; res.operand(n).set(op); if (typeof op === "object" && "seg" in op) { switch (op.seg) { case Register.CS: res.prefixes = InsnAttribute.HAS_SEGMENT_CS; break; case Register.DS: res.prefixes = InsnAttribute.HAS_SEGMENT_DS; break; case Register.ES: res.prefixes = InsnAttribute.HAS_SEGMENT_ES; break; case Register.GS: res.prefixes = InsnAttribute.HAS_SEGMENT_GS; break; case Register.FS: res.prefixes = InsnAttribute.HAS_SEGMENT_FS; break; case Register.SS: res.prefixes = InsnAttribute.HAS_SEGMENT_SS; break; } } } return res; } } export function encode(machine: MachineMode, mnemonic: Mnemonic, ...operands: OperandLike[]) { return EncoderRequest.from(machine, mnemonic, ...operands).encode(); } export function encodeAt(at: bigint, machine: MachineMode, mnemonic: Mnemonic, ...operands: OperandLike[]) { return EncoderRequest.from(machine, mnemonic, ...operands).encode(at); } export function encodeNop(len: number) { return (len <= 512 ? withStack : withHeap)((alloc) => { const tmp = alloc.allocate(MAX_INSN_LENGTH); zydis.asm.zyjsEncNop(tmp, len); const res = zydis.HEAPU8.slice(tmp, tmp + len); alloc.free(tmp); return res; }); }
src/encoder.ts
zyantific-zydis-js-4715bb8
[ { "filename": "src/decoder.ts", "retrieved_chunk": "\t\t\tthrow TypeError(\"Operand type mismatch.\");\n\t\t}\n\t\treturn {\n\t\t\tname: this.regv,\n\t\t\tis4: this.encoding === OperandEncoding.IS4,\n\t\t};\n\t}\n\tget ptr(): OperandPtr {\n\t\tif (this.type !== OperandType.POINTER) {\n\t\t\tthrow TypeError(\"Operand type mismatch.\");", "score": 62.034211063620056 }, { "filename": "src/decoder.ts", "retrieved_chunk": "\t\t\t};\n\t\t} else {\n\t\t\treturn {\n\t\t\t\tu: this.immu,\n\t\t\t\trel: !!this.immRelative,\n\t\t\t};\n\t\t}\n\t}\n\tget reg(): OperandReg {\n\t\tif (this.type !== OperandType.REGISTER) {", "score": 54.68434968068583 }, { "filename": "src/decoder.ts", "retrieved_chunk": "\t\t}\n\t\treturn {\n\t\t\tsegv: this.ptrSeg,\n\t\t\toff: this.ptrOff,\n\t\t};\n\t}\n\tget mem(): OperandMem {\n\t\tif (this.type !== OperandType.MEMORY) {\n\t\t\tthrow TypeError(\"Operand type mismatch.\");\n\t\t}", "score": 47.49711099722624 }, { "filename": "src/register.ts", "retrieved_chunk": "\treturn zydis.asm.zyjsRegisterGetLargestEnclosing(reg, mode);\n}\nexport function from(reg: string) {\n\treg = reg.toUpperCase();\n\tif (reg in Register) {\n\t\treturn Register[reg as keyof typeof Register];\n\t}\n\tthrow Error(\"Invalid register name\");\n}", "score": 45.35437921424662 }, { "filename": "src/decoder.ts", "retrieved_chunk": "\t@OpField\n\tprotected memDisp!: bigint;\n\tget imm(): OperandImm {\n\t\tif (this.type !== OperandType.IMMEDIATE) {\n\t\t\tthrow TypeError(\"Operand type mismatch.\");\n\t\t}\n\t\tif (this.immSigned) {\n\t\t\treturn {\n\t\t\t\ts: this.imms,\n\t\t\t\trel: !!this.immRelative,", "score": 43.63650275870289 } ]
typescript
set reg(o: OperandReg | Register | keyof typeof Register) {
import { NativeModules } from 'react-native'; import { PitchDetectorErrors } from '../../../types'; import { PitchDetectorError } from '../../erros'; import { Permissions } from '../../permissions'; import { PitchDetector } from '..'; const Module = NativeModules.PitchDetectorModule; const asyncMock = <T>(value: T) => jest.fn().mockResolvedValue(value); const asyncMockThrow = <T>(error: T) => jest.fn().mockImplementation(() => { throw error; }); describe('PitchDetector', () => { beforeEach(() => jest.clearAllMocks()); it.each([ ['start', 1], ['isRecording', 1], ['stop', 1], ])( 'should call %s method %s time(s) from native module', async (method: string, times: number) => { const spy = jest.spyOn(Module, method as any); await Object(PitchDetector)[method](); expect(spy).toBeCalledTimes(times); } ); it.each([ ['hasPermissions', 'start'], ['getDefaultConfig', 'start'], ])( 'should call %s method when %s method will be called', async (target: string, method: string) => { const spy = jest.spyOn(PitchDetector, target as any); await Object(PitchDetector)[method](); expect(spy).toBeCalledTimes(1); } ); it('should call audio permission method when start method will be called', async () => { const spy = jest.spyOn(Permissions, 'audio'); await PitchDetector.start(); expect(spy).toBeCalledTimes(1); }); it('should throw error when start method will be called and not have audio record permission', async () => { const error = new PitchDetectorError(PitchDetectorErrors.PERMISSIONS_ERROR); const spy = jest.spyOn(console, 'warn'); Permissions.
audio = asyncMock(false);
await PitchDetector.start(); expect(spy).toBeCalledTimes(1); expect(spy).toHaveBeenCalledWith(error); }); it.each([ ['addListener', 'addListener'], ['removeAllListeners', 'removeAllListeners'], ['removeAllListeners', 'removeListener'], ])( 'should call %s method from event emitter when %s method will be called', async (target: string, method: string) => { const spy = jest.spyOn(Object(PitchDetector).event, target as any); await Object(PitchDetector)[method](1); expect(spy).toBeCalledTimes(1); } ); it.each([['start'], ['stop'], ['isRecording']])( 'should throw error when native %s method fail', async (method: string) => { const error = new Error('Some error message'); const spy = jest.spyOn(console, 'warn'); Permissions.audio = asyncMock(true); Object(PitchDetector).module[method] = asyncMockThrow(error); await Object(PitchDetector)[method](); expect(spy).toBeCalledTimes(1); expect(spy).toBeCalledWith(error); } ); });
src/internal/pitch-detector/__tests__/pitch-detector.spec.ts
1fabiopereira-react-native-pitch-detector-b70fc00
[ { "filename": "src/internal/erros/__tests__/errors.spec.ts", "retrieved_chunk": " it('should create link error', () => {\n const error: any = new PitchDetectorError(\n PitchDetectorErrors.LINKING_ERROR\n );\n expect(error?.message).toMatch(/doesn't seem to be linked/);\n });\n it('should create permission error', () => {\n const error: any = new PitchDetectorError(\n PitchDetectorErrors.PERMISSIONS_ERROR\n );", "score": 37.18431249905554 }, { "filename": "src/internal/utils/__tests__/utils.spec.ts", "retrieved_chunk": " 'should return %s when isObject function will be called with %s',\n (expected: boolean, _: string, params: any) => {\n const result = isObject(params);\n expect(result).toBe(expected);\n }\n );\n it('should not merge when one params is not object', () => {\n const result = merge({ name: 'Fábio' }, NaN);\n expect(result).toHaveProperty('name');\n });", "score": 36.048444214916664 }, { "filename": "src/internal/pitch-detector/index.ts", "retrieved_chunk": " * @returns Promise<void>\n */\n async start(config?: PitchDetectorConfig): Promise<void> {\n try {\n const permission = await this.hasPermissions();\n if (!permission) {\n throw new PitchDetectorError(PitchDetectorErrors.PERMISSIONS_ERROR);\n }\n const configuration = merge<PitchDetectorConfig>(\n this.getDefaultConfig(),", "score": 29.72168918802767 }, { "filename": "src/internal/erros/__tests__/errors.spec.ts", "retrieved_chunk": " expect(error?.message).toMatch(/need audio record permission/);\n });\n});", "score": 25.644736197264887 }, { "filename": "src/internal/erros/index.ts", "retrieved_chunk": " android: '',\n }) +\n '- You rebuilt the app after installing the package\\n' +\n '- You are not using Expo Go\\n';\nconst permission =\n `The package 'react-native-pitch-detector' need audio record permission. Make sure: \\n\\n` +\n Platform.select({\n ios: '- You have added Microphone access permission before start record.',\n android: `- You have added '<uses-permission android:name=\"android.permission.RECORD_AUDIO\" />' on AndroidManifest.xml and request permission before start record.\\n`,\n });", "score": 25.43076541351273 } ]
typescript
audio = asyncMock(false);
import { NativeModules, NativeEventEmitter, Platform } from 'react-native'; import { Permissions } from '../permissions'; import { PitchDetectorError } from '../erros'; import { merge } from '../utils'; import { type Callback, type NativeModuleImplementation, type PitchDetectorConfig, type Subscription, type PitchDetectorAndroidConfig, type PitchDetectorIOSConfig, PitchDetectorErrors, } from '../../types'; export class InternalPitchDetector { private module?: NativeModuleImplementation; private event?: NativeEventEmitter; constructor() { this.module = NativeModules?.PitchDetectorModule; if (this.module) { this.event = new NativeEventEmitter(this.module); } else { /* istanbul ignore next */ throw new PitchDetectorError(PitchDetectorErrors.LINKING_ERROR); } } /** * Returns a default PitchDetector configs * @returns PitchDetectorConfig * @example * ```ts * { * android: { * algorithm: 'YIN', * bufferOverLap: 0, * bufferSize: 1024, * sampleRate: 22050, * }, * ios: { * algorithm: 'YIN', * bufferSize: 1024, * } * } */ private getDefaultConfig(): PitchDetectorConfig { return { android: { algorithm: 'YIN', bufferOverLap: 0, bufferSize: 1024, sampleRate: 22050, }, ios: { algorithm: 'YIN', bufferSize: 1024, }, }; } /** * Get current audio permission * @returns Promise<boolean> */ private async hasPermissions(): Promise<boolean> { return !!(await Permissions.audio()); } /** * Get current status * @returns Promise<boolean> */ async isRecording(): Promise<boolean> { try { const status = await this.module?.isRecording(); return !!status; } catch (err) { console.warn(err); return false; } } /** * Trigger audio recording and pitch detection with provided configs * @param config * @returns Promise<void> */ async start(config?: PitchDetectorConfig): Promise<void> { try { const permission = await this.hasPermissions(); if (!permission) { throw new PitchDetectorError(PitchDetectorErrors.PERMISSIONS_ERROR); }
const configuration = merge<PitchDetectorConfig>( this.getDefaultConfig(), config ?? {}
); const params = Platform.select({ android: configuration.android as unknown, ios: configuration.ios as unknown, }) as PitchDetectorIOSConfig | PitchDetectorAndroidConfig; await this.module?.start(params); } catch (err: unknown) { console.warn(err); } } /** * Stop audio recording and pitch detection * @returns Promise<void> */ async stop(): Promise<void> { try { await this.module?.stop(); } catch (err: unknown) { console.warn(err); } } /** * Register a event listener */ addListener(callback: Callback): Subscription { return this.event?.addListener('data', callback); } /** * Method event listeners * @alias removeAllListeners */ removeListener(): void { this.event?.removeAllListeners('data'); } /** * Method remove all event listeners */ removeAllListeners(): void { this.event?.removeAllListeners('data'); } } /** * Export an instance of InternalPitchDetector */ export const PitchDetector = new InternalPitchDetector();
src/internal/pitch-detector/index.ts
1fabiopereira-react-native-pitch-detector-b70fc00
[ { "filename": "src/types/index.ts", "retrieved_chunk": "export interface NativeModuleImplementation extends NativeModule {\n /**\n * Start audio recording and pitch detection with provided configs\n * @param config\n * @returns Promise<void>\n */\n start: (\n config: PitchDetectorAndroidConfig | PitchDetectorIOSConfig\n ) => Promise<void>;\n /**", "score": 19.630165729516342 }, { "filename": "src/internal/erros/index.ts", "retrieved_chunk": "export class PitchDetectorError {\n constructor(type: PitchDetectorErrors) {\n if (type === PitchDetectorErrors.LINKING_ERROR) {\n return new Error(linking);\n }\n if (type === PitchDetectorErrors.PERMISSIONS_ERROR) {\n return new Error(permission);\n }\n return new Error(base);\n }", "score": 17.422634309391825 }, { "filename": "src/internal/pitch-detector/__tests__/pitch-detector.spec.ts", "retrieved_chunk": " });\n it('should throw error when start method will be called and not have audio record permission', async () => {\n const error = new PitchDetectorError(PitchDetectorErrors.PERMISSIONS_ERROR);\n const spy = jest.spyOn(console, 'warn');\n Permissions.audio = asyncMock(false);\n await PitchDetector.start();\n expect(spy).toBeCalledTimes(1);\n expect(spy).toHaveBeenCalledWith(error);\n });\n it.each([", "score": 15.635817505041956 }, { "filename": "src/types/index.ts", "retrieved_chunk": "/**\n * Pitch detector configuration.\n */\nexport type PitchDetectorConfig = {\n android?: PitchDetectorAndroidConfig;\n ios?: PitchDetectorIOSConfig;\n};\n/**\n * PitchDetector native module interface\n */", "score": 12.925558673949048 }, { "filename": "src/internal/erros/__tests__/errors.spec.ts", "retrieved_chunk": " it('should create link error', () => {\n const error: any = new PitchDetectorError(\n PitchDetectorErrors.LINKING_ERROR\n );\n expect(error?.message).toMatch(/doesn't seem to be linked/);\n });\n it('should create permission error', () => {\n const error: any = new PitchDetectorError(\n PitchDetectorErrors.PERMISSIONS_ERROR\n );", "score": 12.752934980751013 } ]
typescript
const configuration = merge<PitchDetectorConfig>( this.getDefaultConfig(), config ?? {}
import { DecodedInsn } from "./decoder.js"; import { Resource, ZyjsField, withHeap, withStack } from "./util.js"; import zydis from "./native.js"; import { AddressSizeHint, BranchType, BranchWidth, BroadcastMode, ConversionMode, EncodableEncoding, InsnAttribute, MAX_INSN_LENGTH, MachineMode, Mnemonic, OperandSizeHint, OperandType, Register, RoundingMode, SwizzleMode, } from "./enums.js"; import { OperandImm, OperandMem, OperandPtr, OperandReg } from "./common.js"; const OpField = ZyjsField("EncOp"); type OperandLike = keyof typeof Register | number | bigint | OperandImm | OperandReg | OperandPtr | OperandMem; export class EncoderOperand { resource: Resource; constructor(rsrc: Resource) { this.resource = rsrc; } @OpField type!: OperandType; @OpField imms!: bigint; @OpField immu!: bigint; @OpField ptrSeg!: number; @OpField ptrOff!: number; @OpField regv!: Register; @OpField regIs4!: 1 | 0; @OpField memBase!: Register; @OpField memIndex!: Register; @OpField memScale!: Register; @OpField memDisp!: bigint; get imm(): OperandImm { if (this.type !== OperandType.IMMEDIATE) { throw TypeError("Operand type mismatch."); } return { s: this.imms, u: this.immu, }; } set imm(o: OperandImm | bigint | number) { this.type = OperandType.IMMEDIATE; if (typeof o === "object") { if ("s" in o) { this.imms = o.s; } else { this.immu = o.u; } } else { this.imms = BigInt(o); } } get reg(): OperandReg { if (this.type !== OperandType.REGISTER) { throw TypeError("Operand type mismatch."); } return { name: this.regv, is4: !!this.regIs4, }; } set reg(o: OperandReg | Register | keyof typeof Register) { this.type = OperandType.REGISTER; if (typeof o === "string") { this.regv = Register[o]; } else if (typeof o === "number") { this.regv = o; } else { this.regv = o.name; this.regIs4 = o.is4 || false ? 1 : 0; } } get ptr(): OperandPtr { if (this.type !== OperandType.POINTER) { throw TypeError("Operand type mismatch."); } return { segv: this.ptrSeg, off: this.ptrOff, }; } set ptr(
o: OperandPtr) {
this.type = OperandType.POINTER; this.ptrOff = o.off; this.ptrSeg = o.segv; } get mem(): OperandMem { const res: OperandMem = { index: this.memIndex, base: this.memBase, scale: this.memScale, disp: this.memDisp, }; if (res.seg === Register.NONE) { delete res.seg; } if (res.base === Register.NONE) { delete res.base; } if (res.scale === 0 || res.index === Register.NONE) { delete res.index; delete res.scale; } return res; } set mem(o: OperandMem) { this.type = OperandType.MEMORY; this.memBase = o.base ?? Register.NONE; this.memDisp = o.disp ?? 0n; this.memIndex = o.index ?? Register.NONE; this.memScale = o.scale ?? 0; } get() { switch (this.type) { case OperandType.IMMEDIATE: return this.imm; case OperandType.REGISTER: return this.reg; case OperandType.POINTER: return this.ptr; case OperandType.MEMORY: return this.mem; default: throw Error(); } } set(o: OperandLike) { if (typeof o === "number" || typeof o === "bigint") { return void (this.imm = o); } else if (typeof o === "string") { return void (this.reg = o); } // OperandImm if ("s" in o || "u" in o) { this.imm = o; } // OperandReg. // else if ("name" in o) { this.reg = o; } // OperandPtr. // else if ("off" in o) { this.ptr = o; } // OperandMem. // else { this.mem = o; } } } const ReqField = ZyjsField("EncReq"); export class EncoderRequest { resource: Resource; constructor(from?: DecodedInsn) { this.resource = new Resource(zydis.asm.zyjsNewEncReq(from?.resource.ref() ?? 0)); } encode(at?: bigint) { return withStack((alloc) => { const tmp = alloc.allocate(MAX_INSN_LENGTH); const length = at != null ? zydis.asm.zyjsEncReqEncodeAbs(this.resource.ref(), tmp, at) : zydis.asm.zyjsEncReqEncode(this.resource.ref(), tmp); return zydis.HEAPU8.slice(tmp, tmp + length); }); } @ReqField machineMode!: MachineMode; @ReqField allowedEncodings!: EncodableEncoding; @ReqField mnemonic!: Mnemonic; @ReqField prefixes!: bigint; @ReqField branchType!: BranchType; @ReqField branchWidth!: BranchWidth; @ReqField addressSizeHint!: AddressSizeHint; @ReqField operandSizeHint!: OperandSizeHint; @ReqField operandCount!: number; @ReqField evexBroadcast!: BroadcastMode; @ReqField evexRounding!: RoundingMode; @ReqField evexSAE!: 1 | 0; @ReqField evexZeroingMask!: 1 | 0; @ReqField mvexBroadcast!: BroadcastMode; @ReqField mvexRounding!: RoundingMode; @ReqField mvexSAE!: 1 | 0; @ReqField mvexConversion!: ConversionMode; @ReqField mvexSwizzle!: SwizzleMode; @ReqField mvexEvictionHint!: 1 | 0; operand(n: number): EncoderOperand { const ptr = zydis.asm.zyjsEncReqRefOperand(this.resource.ref(), n); if (!ptr) { throw RangeError("Operand out of boundaries."); } return new EncoderOperand(this.resource.subresource(ptr)); } static from(machine: MachineMode, mnemonic: Mnemonic, ...operands: OperandLike[]) { const res = new EncoderRequest(); res.mnemonic = mnemonic; res.operandCount = operands.length; res.machineMode = machine; for (let n = 0; n !== operands.length; n++) { const op = operands[n]; res.operand(n).set(op); if (typeof op === "object" && "seg" in op) { switch (op.seg) { case Register.CS: res.prefixes = InsnAttribute.HAS_SEGMENT_CS; break; case Register.DS: res.prefixes = InsnAttribute.HAS_SEGMENT_DS; break; case Register.ES: res.prefixes = InsnAttribute.HAS_SEGMENT_ES; break; case Register.GS: res.prefixes = InsnAttribute.HAS_SEGMENT_GS; break; case Register.FS: res.prefixes = InsnAttribute.HAS_SEGMENT_FS; break; case Register.SS: res.prefixes = InsnAttribute.HAS_SEGMENT_SS; break; } } } return res; } } export function encode(machine: MachineMode, mnemonic: Mnemonic, ...operands: OperandLike[]) { return EncoderRequest.from(machine, mnemonic, ...operands).encode(); } export function encodeAt(at: bigint, machine: MachineMode, mnemonic: Mnemonic, ...operands: OperandLike[]) { return EncoderRequest.from(machine, mnemonic, ...operands).encode(at); } export function encodeNop(len: number) { return (len <= 512 ? withStack : withHeap)((alloc) => { const tmp = alloc.allocate(MAX_INSN_LENGTH); zydis.asm.zyjsEncNop(tmp, len); const res = zydis.HEAPU8.slice(tmp, tmp + len); alloc.free(tmp); return res; }); }
src/encoder.ts
zyantific-zydis-js-4715bb8
[ { "filename": "src/decoder.ts", "retrieved_chunk": "\t\t}\n\t\treturn {\n\t\t\tsegv: this.ptrSeg,\n\t\t\toff: this.ptrOff,\n\t\t};\n\t}\n\tget mem(): OperandMem {\n\t\tif (this.type !== OperandType.MEMORY) {\n\t\t\tthrow TypeError(\"Operand type mismatch.\");\n\t\t}", "score": 62.75500819124927 }, { "filename": "src/decoder.ts", "retrieved_chunk": "\t\t\tthrow TypeError(\"Operand type mismatch.\");\n\t\t}\n\t\treturn {\n\t\t\tname: this.regv,\n\t\t\tis4: this.encoding === OperandEncoding.IS4,\n\t\t};\n\t}\n\tget ptr(): OperandPtr {\n\t\tif (this.type !== OperandType.POINTER) {\n\t\t\tthrow TypeError(\"Operand type mismatch.\");", "score": 57.396545170206416 }, { "filename": "src/decoder.ts", "retrieved_chunk": "\t@OpField\n\tprotected memDisp!: bigint;\n\tget imm(): OperandImm {\n\t\tif (this.type !== OperandType.IMMEDIATE) {\n\t\t\tthrow TypeError(\"Operand type mismatch.\");\n\t\t}\n\t\tif (this.immSigned) {\n\t\t\treturn {\n\t\t\t\ts: this.imms,\n\t\t\t\trel: !!this.immRelative,", "score": 40.58397171067584 }, { "filename": "src/decoder.ts", "retrieved_chunk": "\tget() {\n\t\tswitch (this.type) {\n\t\t\tcase OperandType.IMMEDIATE:\n\t\t\t\treturn this.imm;\n\t\t\tcase OperandType.REGISTER:\n\t\t\t\treturn this.reg;\n\t\t\tcase OperandType.POINTER:\n\t\t\t\treturn this.ptr;\n\t\t\tcase OperandType.MEMORY:\n\t\t\t\treturn this.mem;", "score": 32.538269662274104 }, { "filename": "src/decoder.ts", "retrieved_chunk": "\t\t\t};\n\t\t} else {\n\t\t\treturn {\n\t\t\t\tu: this.immu,\n\t\t\t\trel: !!this.immRelative,\n\t\t\t};\n\t\t}\n\t}\n\tget reg(): OperandReg {\n\t\tif (this.type !== OperandType.REGISTER) {", "score": 27.15509289452538 } ]
typescript
o: OperandPtr) {
import { OperandImm, OperandMem, OperandPtr, OperandReg } from "./common.js"; import { BranchType, BroadcastMode, ConversionMode, DecoderMode, ElementType, ExceptionClass, ISAExt, ISASet, InsnCategory, InstructionEncoding, MachineMode, MaskMode, MemoryOperandType, Mnemonic, OpcodeMap, OperandAction, OperandEncoding, OperandType, OperandVisibility, Register, RoundingMode, StackWidth, SwizzleMode, } from "./enums.js"; import zydis from "./native.js"; import { Resource, ZyjsField, withStack } from "./util.js"; const OpField = ZyjsField("DecOp"); export class DecodedOperand { resource: Resource; constructor(rsrc: Resource) { this.resource = rsrc; } @OpField id!: number; @OpField visiblity!: OperandVisibility; @OpField actions!: OperandAction; @OpField encoding!: OperandEncoding; @OpField size!: number; @OpField elementType!: ElementType; @OpField elementSize!: number; @OpField elementCount!: number; @OpField attributes!: number; @OpField type!: OperandType; @OpField protected immSigned!: number; @OpField protected immRelative!: number; @OpField protected imms!: bigint; @OpField protected immu!: bigint; @OpField protected ptrSeg!: number; @OpField protected ptrOff!: number; @OpField protected regv!: Register; @OpField protected memType!: MemoryOperandType; @OpField protected memSeg!: Register; @OpField protected memBase!: Register; @OpField protected memIndex!: Register; @OpField protected memScale!: Register; @OpField protected memHasDisp!: number; @OpField protected memDisp!: bigint; get imm(): OperandImm { if (this.type !== OperandType.IMMEDIATE) { throw TypeError("Operand type mismatch."); } if (this.immSigned) { return { s: this.imms, rel: !!this.immRelative, }; } else { return { u: this.immu, rel: !!this.immRelative, }; } } get reg(): OperandReg { if (this.type !== OperandType.REGISTER) { throw TypeError("Operand type mismatch."); } return { name: this.regv, is4: this.encoding === OperandEncoding.IS4, }; } get ptr(): OperandPtr { if (this.type !== OperandType.POINTER) { throw TypeError("Operand type mismatch."); } return { segv: this.ptrSeg, off: this.ptrOff, }; } get mem(): OperandMem { if (this.type !== OperandType.MEMORY) { throw TypeError("Operand type mismatch."); } const res: OperandMem = { type: this.memType, seg: this.memSeg, index: this.memIndex, base: this.memBase, scale: this.memScale, disp: this.memHasDisp ? this.memDisp : undefined, }; if (res.seg === Register.NONE) { delete res.seg; } if (res.base === Register.NONE) { delete res.base; } if (res.scale === 0 || res.index === Register.NONE) { delete res.index; delete res.scale; } return res; } get() { switch (this.type) { case OperandType.IMMEDIATE: return this.imm; case OperandType.REGISTER: return this.reg; case OperandType.POINTER: return this.ptr; case OperandType.MEMORY: return this.mem; default: throw Error(); } } } const FlagField = ZyjsField("FlagSet"); interface AccessedFlags { readonly tested: number; readonly modified: number; readonly set: number; readonly unset: number; readonly undef: number; } const flagsNoop: AccessedFlags = { tested: 0, modified: 0, set: 0, unset: 0, undef: 0, }; class AccessedFlagsByPtr implements AccessedFlags { resource: { ref(): number }; constructor(ptr: number) { this.resource = { ref() { return ptr; }, }; } @FlagField readonly tested!: number; @FlagField readonly modified!: number; @FlagField readonly set!: number; @FlagField readonly unset!: number; @FlagField readonly undef!: number; } const InsnField = ZyjsField("DecInsn"); export class DecodedInsn { resource: Resource; constructor(rsrc: Resource) { this.resource = rsrc; } // Common information. // @InsnField length!: number; @InsnField mnemonic!: Mnemonic; @InsnField encoding!: InstructionEncoding; @InsnField opcodeMap!: OpcodeMap; @InsnField opcode!: number; @InsnField stackWidth!: number; @InsnField operandWidth!: number; @InsnField addressWidth!: number; @InsnField operandCount!: number; @InsnField visibleOperandCount!: number; @InsnField attributes!: bigint; // AVX. // @InsnField vectorLength!: number; @InsnField maskMode!: MaskMode; @InsnField maskReg!: Register; @InsnField isBroadcastStatic!: 1 | 0; @InsnField broadcastMode!: BroadcastMode; @InsnField roundingMode!: RoundingMode; @InsnField swizzleMode!: SwizzleMode; @InsnField conversionMode!: ConversionMode; @InsnField hasSAE!: 1 | 0; @InsnField hasEvictionHint!: 1 | 0; // Meta. // @InsnField category!: InsnCategory; @InsnField isaSet!: ISASet; @InsnField isaExt!: ISAExt; @InsnField branchType!: BranchType; @InsnField exceptionClass!: ExceptionClass; // Flags. // @InsnField private cpuFlagsPtr!: number; @InsnField private fpuFlagsPtr!: number; get cpuFlags(): AccessedFlags { const ptr = this.cpuFlagsPtr; return ptr ? new AccessedFlagsByPtr(ptr) : flagsNoop; } get fpuFlags(): AccessedFlags { const ptr = this.fpuFlagsPtr; return ptr ? new AccessedFlagsByPtr(ptr) : flagsNoop; } operand(n: number): DecodedOperand { const ptr = zydis.asm.zyjsDecInsnRefOperand(this.resource.ref(), n); if (!ptr) { throw RangeError("Operand out of boundaries."); } return new DecodedOperand(this.resource.subresource(ptr)); } } export class Decoder { resource: Resource; constructor(mode: MachineMode, width: StackWidth) { this.resource = new Resource(zydis.asm.zyjsNewDecoder(mode, width)); } set(mode: DecoderMode, value: boolean) { zydis.asm.zyjsDecoderSetMode(this.resource.ref(), mode, value ? 1 : 0); return this; } decode(buffer: Uint8Array) { const ptr = withStack((
a) => {
return zydis.asm.zyjsDecoderDecode(this.resource.ref(), a.buf(buffer), buffer.length); }); return new DecodedInsn(new Resource(ptr)); } }
src/decoder.ts
zyantific-zydis-js-4715bb8
[ { "filename": "src/encoder.ts", "retrieved_chunk": "\t}\n}\nconst ReqField = ZyjsField(\"EncReq\");\nexport class EncoderRequest {\n\tresource: Resource;\n\tconstructor(from?: DecodedInsn) {\n\t\tthis.resource = new Resource(zydis.asm.zyjsNewEncReq(from?.resource.ref() ?? 0));\n\t}\n\tencode(at?: bigint) {\n\t\treturn withStack((alloc) => {", "score": 47.45519616989094 }, { "filename": "src/formatter.ts", "retrieved_chunk": "\tproperty(prop: FormatterProperty, value: boolean | number) {\n\t\tzydis.asm.zyjsFormatterSetProperty(this.resource.ref(), prop, +value);\n\t\treturn this;\n\t}\n\tinsn(value: DecodedInsn, address: bigint = 0n) {\n\t\treturn withStack((alloc) => {\n\t\t\tconst buf = alloc.allocate(MAX_LENGTH);\n\t\t\tzydis.asm.zyjsFormatterFormatInsn(this.resource.ref(), buf, MAX_LENGTH, value.resource.ref(), address);\n\t\t\treturn zydis.UTF8ToString(buf, MAX_LENGTH);\n\t\t});", "score": 47.21107750621104 }, { "filename": "src/encoder.ts", "retrieved_chunk": "\t\t\tconst tmp = alloc.allocate(MAX_INSN_LENGTH);\n\t\t\tconst length =\n\t\t\t\tat != null\n\t\t\t\t\t? zydis.asm.zyjsEncReqEncodeAbs(this.resource.ref(), tmp, at)\n\t\t\t\t\t: zydis.asm.zyjsEncReqEncode(this.resource.ref(), tmp);\n\t\t\treturn zydis.HEAPU8.slice(tmp, tmp + length);\n\t\t});\n\t}\n\t@ReqField\n\tmachineMode!: MachineMode;", "score": 40.08572343805137 }, { "filename": "src/util.ts", "retrieved_chunk": "}\nexport function ZyjsField(ns: string) {\n\treturn function (target: any, propertyKey: any) {\n\t\tconst getter = zydis.asm[`zyjs${ns}Get_${propertyKey}`];\n\t\tconst setter = zydis.asm[`zyjs${ns}Set_${propertyKey}`];\n\t\tObject.defineProperty(target, propertyKey, {\n\t\t\tget(this: { resource: { ref(): number } }) {\n\t\t\t\treturn getter(this.resource.ref());\n\t\t\t},\n\t\t\tset(this: { resource: { ref(): number } }, value: any) {", "score": 39.07109539182882 }, { "filename": "src/register.ts", "retrieved_chunk": "export function getClass(reg: Register): RegisterClass {\n\treturn zydis.asm.zyjsRegisterGetClass(reg);\n}\nexport function getWidth(reg: Register, mode: MachineMode): number {\n\treturn zydis.asm.zyjsRegisterGetWidth(reg, mode);\n}\nexport function getWidthFromClass(cl: RegisterClass, mode: MachineMode): number {\n\treturn zydis.asm.zyjsRegisterClassGetWidth(cl, mode);\n}\nexport function getLargestEnclosing(reg: Register, mode: MachineMode): Register {", "score": 38.707756004147626 } ]
typescript
a) => {
import _ from 'lodash' import { assign } from 'power-assign' import type { HydratedDocument, Model, MongooseQueryMiddleware, QueryOptions, Schema, ToObjectOptions, UpdateQuery, UpdateWithAggregationPipeline } from 'mongoose' import type IPluginOptions from './interfaces/IPluginOptions' import type IContext from './interfaces/IContext' import type IHookContext from './interfaces/IHookContext' import { createPatch, updatePatch, deletePatch } from './patch' import { isMongooseLessThan7 } from './version' import em from './em' const remove = isMongooseLessThan7 ? 'remove' : 'deleteOne' const toObjectOptions: ToObjectOptions = { depopulate: true, virtuals: false } const updateMethods = [ 'update', 'updateOne', 'replaceOne', 'updateMany', 'findOneAndUpdate', 'findOneAndReplace', 'findByIdAndUpdate' ] const deleteMethods = [ 'remove', 'findOneAndDelete', 'findOneAndRemove', 'findByIdAndDelete', 'findByIdAndRemove', 'deleteOne', 'deleteMany' ] function isHookIgnored<T> (options: QueryOptions<T>): boolean { return options.ignoreHook === true || (options.ignoreEvent === true && options.ignorePatchHistory === true) } function splitUpdateAndCommands<T> (updateQuery: UpdateWithAggregationPipeline | UpdateQuery<T> | null): { update: UpdateQuery<T>, commands: Record<string, unknown>[] } { let update: UpdateQuery<T> = {} const commands: Record<string, unknown>[] = [] if (!_.isEmpty(updateQuery) && !_.isArray(updateQuery) && _.isObjectLike(updateQuery)) { update = _.cloneDeep(updateQuery) const keys = _.keys(update).filter((key) => key.startsWith('$')) if (!_.isEmpty(keys)) { _.forEach(keys, (key) => { commands.push({ [key]: update[key] as unknown }) // eslint-disable-next-line @typescript-eslint/no-dynamic-delete delete update[key] }) } } return { update, commands } } function assignUpdate<T> (document: HydratedDocument<T>, update: UpdateQuery<T>, commands: Record<string, unknown>[]): HydratedDocument<T> { let updated = assign(document, update) _.forEach(commands, (command) => { try { updated = assign(updated, command) } catch { // we catch assign keys that are not implemented } }) return updated } /** * @description Patch patch event emitter */ export const patchEventEmitter = em /** * @description Patch history plugin * @param {Schema} schema * @param {IPluginOptions} opts * @returns {void} */
export const patchHistoryPlugin = function plugin<T> (schema: Schema<T>, opts: IPluginOptions<T>): void {
schema.pre('save', async function () { const current = this.toObject(toObjectOptions) as HydratedDocument<T> const model = this.constructor as Model<T> const context: IContext<T> = { op: this.isNew ? 'create' : 'update', modelName: opts.modelName ?? model.modelName, collectionName: opts.collectionName ?? model.collection.collectionName, createdDocs: [current] } if (this.isNew) { await createPatch(opts, context) } else { const original = await model.findById(current._id).lean().exec() if (original) { await updatePatch(opts, context, current, original as HydratedDocument<T>) } } }) schema.post('insertMany', async function (docs) { const context = { op: 'create', modelName: opts.modelName ?? this.modelName, collectionName: opts.collectionName ?? this.collection.collectionName, createdDocs: docs as unknown as HydratedDocument<T>[] } await createPatch(opts, context) }) schema.pre(updateMethods as MongooseQueryMiddleware[], async function (this: IHookContext<T>) { const options = this.getOptions() if (isHookIgnored(options)) return const model = this.model as Model<T> const filter = this.getFilter() const count = await this.model.count(filter).exec() this._context = { op: this.op, modelName: opts.modelName ?? this.model.modelName, collectionName: opts.collectionName ?? this.model.collection.collectionName, isNew: options.upsert && count === 0, ignoreEvent: options.ignoreEvent as boolean, ignorePatchHistory: options.ignorePatchHistory as boolean } const updateQuery = this.getUpdate() const { update, commands } = splitUpdateAndCommands(updateQuery) const cursor = model.find(filter).lean().cursor() await cursor.eachAsync(async (doc: HydratedDocument<T>) => { await updatePatch(opts, this._context, assignUpdate(doc, update, commands), doc) }) }) schema.post(updateMethods as MongooseQueryMiddleware[], async function (this: IHookContext<T>) { const options = this.getOptions() if (isHookIgnored(options)) return if (!this._context.isNew) return const model = this.model as Model<T> const updateQuery = this.getUpdate() const { update, commands } = splitUpdateAndCommands(updateQuery) const filter = assignUpdate({} as HydratedDocument<T>, update, commands) if (!_.isEmpty(filter)) { const current = await model.findOne(update).lean().exec() if (current) { this._context.createdDocs = [current] as HydratedDocument<T>[] await createPatch(opts, this._context) } } }) schema.pre(remove, { document: true, query: false }, async function () { const original = this.toObject(toObjectOptions) as HydratedDocument<T> if (opts.preDelete && !_.isEmpty(original)) { await opts.preDelete([original]) } }) schema.post(remove, { document: true, query: false }, async function (this: HydratedDocument<T>) { const original = this.toObject(toObjectOptions) as HydratedDocument<T> const model = this.constructor as Model<T> const context: IContext<T> = { op: 'delete', modelName: opts.modelName ?? model.modelName, collectionName: opts.collectionName ?? model.collection.collectionName, deletedDocs: [original] } await deletePatch(opts, context) }) schema.pre(deleteMethods as MongooseQueryMiddleware[], { document: false, query: true }, async function (this: IHookContext<T>) { const options = this.getOptions() if (isHookIgnored(options)) return const model = this.model as Model<T> const filter = this.getFilter() this._context = { op: this.op, modelName: opts.modelName ?? this.model.modelName, collectionName: opts.collectionName ?? this.model.collection.collectionName, ignoreEvent: options.ignoreEvent as boolean, ignorePatchHistory: options.ignorePatchHistory as boolean } if (['remove', 'deleteMany'].includes(this._context.op) && !options.single) { const docs = await model.find(filter).lean().exec() if (!_.isEmpty(docs)) { this._context.deletedDocs = docs as HydratedDocument<T>[] } } else { const doc = await model.findOne(filter).lean().exec() if (!_.isEmpty(doc)) { this._context.deletedDocs = [doc] as HydratedDocument<T>[] } } if (opts.preDelete && _.isArray(this._context.deletedDocs) && !_.isEmpty(this._context.deletedDocs)) { await opts.preDelete(this._context.deletedDocs) } }) schema.post(deleteMethods as MongooseQueryMiddleware[], { document: false, query: true }, async function (this: IHookContext<T>) { const options = this.getOptions() if (isHookIgnored(options)) return await deletePatch(opts, this._context) }) }
src/plugin.ts
ilovepixelart-ts-patch-mongoose-3306d83
[ { "filename": "src/patch.ts", "retrieved_chunk": " getValue(metadata)\n ]\n })\n}\nexport function emitEvent<T> (context: IContext<T>, event: string | undefined, data: IEvent<T>): void {\n if (event && !context.ignoreEvent) {\n em.emit(event, data)\n }\n}\nexport async function bulkPatch<T> (opts: IPluginOptions<T>, context: IContext<T>, eventKey: 'eventCreated' | 'eventDeleted', docsKey: 'createdDocs' | 'deletedDocs'): Promise<void> {", "score": 14.441572807321847 }, { "filename": "src/patch.ts", "retrieved_chunk": " await History.bulkWrite(bulk, { ordered: false })\n }\n }\n}\nexport async function createPatch<T> (opts: IPluginOptions<T>, context: IContext<T>): Promise<void> {\n await bulkPatch(opts, context, 'eventCreated', 'createdDocs')\n}\nexport async function updatePatch<T> (opts: IPluginOptions<T>, context: IContext<T>, current: HydratedDocument<T>, original: HydratedDocument<T>): Promise<void> {\n const history = isPatchHistoryEnabled(opts, context)\n const { currentObject, originalObject } = getObjects(opts, current, original)", "score": 13.507400745989667 }, { "filename": "src/patch.ts", "retrieved_chunk": " version\n })\n }\n}\nexport async function deletePatch<T> (opts: IPluginOptions<T>, context: IContext<T>): Promise<void> {\n await bulkPatch(opts, context, 'eventDeleted', 'deletedDocs')\n}", "score": 10.741124861844602 }, { "filename": "src/patch.ts", "retrieved_chunk": " const history = isPatchHistoryEnabled(opts, context)\n const event = opts[eventKey]\n const docs = context[docsKey]\n const key = eventKey === 'eventCreated' ? 'doc' : 'oldDoc'\n if (_.isEmpty(docs) || (!event && !history)) return\n const [user, reason, metadata] = await getData(opts)\n const chunks = _.chunk(docs, 1000)\n for await (const chunk of chunks) {\n const bulk = []\n for (const doc of chunk) {", "score": 9.552579506078915 }, { "filename": "src/models/History.ts", "retrieved_chunk": "import { Schema, model } from 'mongoose'\nimport type IHistory from '../interfaces/IHistory'\nconst HistorySchema = new Schema<IHistory>({\n op: {\n type: String,\n required: true\n },\n modelName: {\n type: String,\n required: true", "score": 9.223893520184678 } ]
typescript
export const patchHistoryPlugin = function plugin<T> (schema: Schema<T>, opts: IPluginOptions<T>): void {
import _ from 'lodash' import omit from 'omit-deep' import jsonpatch from 'fast-json-patch' import type { HydratedDocument, Types } from 'mongoose' import type IEvent from './interfaces/IEvent' import type IContext from './interfaces/IContext' import type IPluginOptions from './interfaces/IPluginOptions' import type { User, Metadata } from './interfaces/IPluginOptions' import History from './models/History' import em from './em' function isPatchHistoryEnabled<T> (opts: IPluginOptions<T>, context: IContext<T>): boolean { return !opts.patchHistoryDisabled && !context.ignorePatchHistory } export function getObjects<T> (opts: IPluginOptions<T>, current: HydratedDocument<T>, original: HydratedDocument<T>): { currentObject: Partial<T>, originalObject: Partial<T> } { let currentObject = JSON.parse(JSON.stringify(current)) as Partial<T> let originalObject = JSON.parse(JSON.stringify(original)) as Partial<T> if (opts.omit) { currentObject = omit(currentObject, opts.omit) originalObject = omit(originalObject, opts.omit) } return { currentObject, originalObject } } export async function getUser<T> (opts: IPluginOptions<T>): Promise<User | undefined> { if (_.isFunction(opts.getUser)) { return await opts.getUser() } return undefined } export async function getReason<T> (opts: IPluginOptions<T>): Promise<string | undefined> { if (_.isFunction(opts.getReason)) { return await opts.getReason() } return undefined } export async function getMetadata<T> (opts: IPluginOptions<T>): Promise<Metadata | undefined> { if (_.isFunction(opts.getMetadata)) { return await opts.getMetadata() } return undefined } export function getValue <T> (item: PromiseSettledResult<T>): T | undefined { return item.status === 'fulfilled' ? item.value : undefined } export async function getData<T> (opts: IPluginOptions<T>): Promise<[User | undefined, string | undefined, Metadata | undefined]> { return Promise .allSettled([getUser(opts), getReason(opts), getMetadata(opts)]) .then(([user, reason, metadata]) => { return [ getValue(user), getValue(reason), getValue(metadata) ] }) }
export function emitEvent<T> (context: IContext<T>, event: string | undefined, data: IEvent<T>): void {
if (event && !context.ignoreEvent) { em.emit(event, data) } } export async function bulkPatch<T> (opts: IPluginOptions<T>, context: IContext<T>, eventKey: 'eventCreated' | 'eventDeleted', docsKey: 'createdDocs' | 'deletedDocs'): Promise<void> { const history = isPatchHistoryEnabled(opts, context) const event = opts[eventKey] const docs = context[docsKey] const key = eventKey === 'eventCreated' ? 'doc' : 'oldDoc' if (_.isEmpty(docs) || (!event && !history)) return const [user, reason, metadata] = await getData(opts) const chunks = _.chunk(docs, 1000) for await (const chunk of chunks) { const bulk = [] for (const doc of chunk) { emitEvent(context, event, { [key]: doc }) if (history) { bulk.push({ insertOne: { document: { op: context.op, modelName: context.modelName, collectionName: context.collectionName, collectionId: doc._id as Types.ObjectId, doc, user, reason, metadata, version: 0 } } }) } } if (history) { await History.bulkWrite(bulk, { ordered: false }) } } } export async function createPatch<T> (opts: IPluginOptions<T>, context: IContext<T>): Promise<void> { await bulkPatch(opts, context, 'eventCreated', 'createdDocs') } export async function updatePatch<T> (opts: IPluginOptions<T>, context: IContext<T>, current: HydratedDocument<T>, original: HydratedDocument<T>): Promise<void> { const history = isPatchHistoryEnabled(opts, context) const { currentObject, originalObject } = getObjects(opts, current, original) if (_.isEmpty(originalObject) || _.isEmpty(currentObject)) return const patch = jsonpatch.compare(originalObject, currentObject, true) if (_.isEmpty(patch)) return emitEvent(context, opts.eventUpdated, { oldDoc: original, doc: current, patch }) if (history) { let version = 0 const lastHistory = await History.findOne({ collectionId: original._id as Types.ObjectId }).sort('-version').exec() if (lastHistory && lastHistory.version >= 0) { version = lastHistory.version + 1 } const [user, reason, metadata] = await getData(opts) await History.create({ op: context.op, modelName: context.modelName, collectionName: context.collectionName, collectionId: original._id as Types.ObjectId, patch, user, reason, metadata, version }) } } export async function deletePatch<T> (opts: IPluginOptions<T>, context: IContext<T>): Promise<void> { await bulkPatch(opts, context, 'eventDeleted', 'deletedDocs') }
src/patch.ts
ilovepixelart-ts-patch-mongoose-3306d83
[ { "filename": "src/interfaces/IHistory.ts", "retrieved_chunk": " reason?: string\n metadata?: object\n patch?: Operation[]\n}\nexport default IHistory", "score": 16.67004665587134 }, { "filename": "src/interfaces/IPluginOptions.ts", "retrieved_chunk": " getReason?: () => Promise<string> | string\n getMetadata?: () => Promise<Metadata> | Metadata\n omit?: string[]\n patchHistoryDisabled?: boolean\n preDelete?: (docs: HydratedDocument<T>[]) => Promise<void>\n}\nexport default IPluginOptions", "score": 15.105212114567497 }, { "filename": "src/models/History.ts", "retrieved_chunk": " type: Object\n },\n patch: {\n type: Array\n },\n user: {\n type: Object\n },\n reason: {\n type: String", "score": 13.709029020692718 }, { "filename": "src/plugin.ts", "retrieved_chunk": " * @returns {void}\n */\nexport const patchHistoryPlugin = function plugin<T> (schema: Schema<T>, opts: IPluginOptions<T>): void {\n schema.pre('save', async function () {\n const current = this.toObject(toObjectOptions) as HydratedDocument<T>\n const model = this.constructor as Model<T>\n const context: IContext<T> = {\n op: this.isNew ? 'create' : 'update',\n modelName: opts.modelName ?? model.modelName,\n collectionName: opts.collectionName ?? model.collection.collectionName,", "score": 12.637797722883063 }, { "filename": "src/plugin.ts", "retrieved_chunk": " await opts.preDelete([original])\n }\n })\n schema.post(remove, { document: true, query: false }, async function (this: HydratedDocument<T>) {\n const original = this.toObject(toObjectOptions) as HydratedDocument<T>\n const model = this.constructor as Model<T>\n const context: IContext<T> = {\n op: 'delete',\n modelName: opts.modelName ?? model.modelName,\n collectionName: opts.collectionName ?? model.collection.collectionName,", "score": 9.072734549059456 } ]
typescript
export function emitEvent<T> (context: IContext<T>, event: string | undefined, data: IEvent<T>): void {
import { FormatterProperty, FormatterStyle } from "./enums.js"; import { HeapAllocator, Resource, withStack } from "./util.js"; import zydis from "./native.js"; import { DecodedInsn, DecodedOperand } from "./decoder.js"; const MAX_LENGTH = 512; export class Formatter { resource: Resource; constructor(style: FormatterStyle) { this.resource = new Resource(zydis.asm.zyjsNewFormatter(style)); } property(prop: FormatterProperty, value: boolean | number) { zydis.asm.zyjsFormatterSetProperty(this.resource.ref(), prop, +value); return this; } insn(value: DecodedInsn, address: bigint = 0n) { return withStack((alloc) => { const buf = alloc.allocate(MAX_LENGTH); zydis.asm.zyjsFormatterFormatInsn(this.resource.ref(), buf, MAX_LENGTH, value.resource.ref(), address); return zydis.UTF8ToString(buf, MAX_LENGTH); }); } operand(insn: DecodedInsn, op: DecodedOperand, address: bigint = 0n) { return withStack((alloc) => { const buf = alloc.allocate(MAX_LENGTH); zydis.asm.zyjsFormatterFormatOperand( this.resource.ref(), buf, MAX_LENGTH, insn.resource.ref(), op.resource.ref(), address ); return zydis.UTF8ToString(buf, MAX_LENGTH); }); } // Lazy shader instances. // static #lazyMap = new Map<FormatterStyle, Formatter>(); static #getLazy(style: FormatterStyle) { let instance = this.#lazyMap.get(style); if (!instance) { instance = new Formatter(style); this.#lazyMap.set(style, instance); } return instance; } static intel(): Omit<Formatter, "property"> { return this.#getLazy(FormatterStyle.INTEL); } static att(): Omit<Formatter, "property"> {
return this.#getLazy(FormatterStyle.ATT);
} }
src/formatter.ts
zyantific-zydis-js-4715bb8
[ { "filename": "src/enums.ts", "retrieved_chunk": "\t/**\n\t * Maximum value of this enum.\n\t */\n\t// MAX_VALUE = XSAVEOPT,\n}\nexport enum FormatterStyle {\n\t/**\n\t * Generates `AT&T`-style disassembly.\n\t */\n\tATT,", "score": 32.366558444631806 }, { "filename": "src/decoder.ts", "retrieved_chunk": "\tget() {\n\t\tswitch (this.type) {\n\t\t\tcase OperandType.IMMEDIATE:\n\t\t\t\treturn this.imm;\n\t\t\tcase OperandType.REGISTER:\n\t\t\t\treturn this.reg;\n\t\t\tcase OperandType.POINTER:\n\t\t\t\treturn this.ptr;\n\t\t\tcase OperandType.MEMORY:\n\t\t\t\treturn this.mem;", "score": 21.94064498806039 }, { "filename": "src/decoder.ts", "retrieved_chunk": "\t}\n\tset(mode: DecoderMode, value: boolean) {\n\t\tzydis.asm.zyjsDecoderSetMode(this.resource.ref(), mode, value ? 1 : 0);\n\t\treturn this;\n\t}\n\tdecode(buffer: Uint8Array) {\n\t\tconst ptr = withStack((a) => {\n\t\t\treturn zydis.asm.zyjsDecoderDecode(this.resource.ref(), a.buf(buffer), buffer.length);\n\t\t});\n\t\treturn new DecodedInsn(new Resource(ptr));", "score": 21.774895676816232 }, { "filename": "src/encoder.ts", "retrieved_chunk": "\t}\n\tget() {\n\t\tswitch (this.type) {\n\t\t\tcase OperandType.IMMEDIATE:\n\t\t\t\treturn this.imm;\n\t\t\tcase OperandType.REGISTER:\n\t\t\t\treturn this.reg;\n\t\t\tcase OperandType.POINTER:\n\t\t\t\treturn this.ptr;\n\t\t\tcase OperandType.MEMORY:", "score": 20.661126522285016 }, { "filename": "src/encoder.ts", "retrieved_chunk": "\t\t\t\treturn this.mem;\n\t\t\tdefault:\n\t\t\t\tthrow Error();\n\t\t}\n\t}\n\tset(o: OperandLike) {\n\t\tif (typeof o === \"number\" || typeof o === \"bigint\") {\n\t\t\treturn void (this.imm = o);\n\t\t} else if (typeof o === \"string\") {\n\t\t\treturn void (this.reg = o);", "score": 20.626189335546307 } ]
typescript
return this.#getLazy(FormatterStyle.ATT);
import _ from 'lodash' import omit from 'omit-deep' import jsonpatch from 'fast-json-patch' import type { HydratedDocument, Types } from 'mongoose' import type IEvent from './interfaces/IEvent' import type IContext from './interfaces/IContext' import type IPluginOptions from './interfaces/IPluginOptions' import type { User, Metadata } from './interfaces/IPluginOptions' import History from './models/History' import em from './em' function isPatchHistoryEnabled<T> (opts: IPluginOptions<T>, context: IContext<T>): boolean { return !opts.patchHistoryDisabled && !context.ignorePatchHistory } export function getObjects<T> (opts: IPluginOptions<T>, current: HydratedDocument<T>, original: HydratedDocument<T>): { currentObject: Partial<T>, originalObject: Partial<T> } { let currentObject = JSON.parse(JSON.stringify(current)) as Partial<T> let originalObject = JSON.parse(JSON.stringify(original)) as Partial<T> if (opts.omit) { currentObject = omit(currentObject, opts.omit) originalObject = omit(originalObject, opts.omit) } return { currentObject, originalObject } } export async function getUser<T> (opts: IPluginOptions<T>): Promise<User | undefined> { if (_.isFunction(opts.getUser)) { return await opts.getUser() } return undefined } export async function getReason<T> (opts: IPluginOptions<T>): Promise<string | undefined> { if (_.isFunction(opts.getReason)) { return await opts.getReason() } return undefined } export async function getMetadata<T> (opts: IPluginOptions<T>): Promise<Metadata | undefined> { if (_.isFunction(opts.getMetadata)) { return await opts.getMetadata() } return undefined } export function getValue <T> (item: PromiseSettledResult<T>): T | undefined { return item.status === 'fulfilled' ? item.value : undefined } export async function getData<T> (opts: IPluginOptions<T>): Promise<[User | undefined, string | undefined, Metadata | undefined]> { return Promise .allSettled([getUser(opts), getReason(opts), getMetadata(opts)]) .then(([user, reason, metadata]) => { return [ getValue(user), getValue(reason), getValue(metadata) ] }) } export function emitEvent<T> (context: IContext<T>, event: string | undefined, data: IEvent<T>): void { if (event && !context.ignoreEvent) { em.emit(event, data) } } export async function bulkPatch<T> (opts: IPluginOptions<T>, context: IContext<T>, eventKey: 'eventCreated' | 'eventDeleted', docsKey: 'createdDocs' | 'deletedDocs'): Promise<void> { const history = isPatchHistoryEnabled(opts, context) const event = opts[eventKey] const docs = context[docsKey] const key = eventKey === 'eventCreated' ? 'doc' : 'oldDoc' if (_.isEmpty(docs) || (!event && !history)) return const [user, reason, metadata] = await getData(opts) const chunks = _.chunk(docs, 1000) for await (const chunk of chunks) { const bulk = [] for (const doc of chunk) { emitEvent(context, event, { [key]: doc }) if (history) { bulk.push({ insertOne: { document: { op: context.op, modelName: context.modelName, collectionName: context.collectionName, collectionId: doc._id as Types.ObjectId, doc, user, reason, metadata, version: 0 } } }) } } if (history) {
await History.bulkWrite(bulk, { ordered: false }) }
} } export async function createPatch<T> (opts: IPluginOptions<T>, context: IContext<T>): Promise<void> { await bulkPatch(opts, context, 'eventCreated', 'createdDocs') } export async function updatePatch<T> (opts: IPluginOptions<T>, context: IContext<T>, current: HydratedDocument<T>, original: HydratedDocument<T>): Promise<void> { const history = isPatchHistoryEnabled(opts, context) const { currentObject, originalObject } = getObjects(opts, current, original) if (_.isEmpty(originalObject) || _.isEmpty(currentObject)) return const patch = jsonpatch.compare(originalObject, currentObject, true) if (_.isEmpty(patch)) return emitEvent(context, opts.eventUpdated, { oldDoc: original, doc: current, patch }) if (history) { let version = 0 const lastHistory = await History.findOne({ collectionId: original._id as Types.ObjectId }).sort('-version').exec() if (lastHistory && lastHistory.version >= 0) { version = lastHistory.version + 1 } const [user, reason, metadata] = await getData(opts) await History.create({ op: context.op, modelName: context.modelName, collectionName: context.collectionName, collectionId: original._id as Types.ObjectId, patch, user, reason, metadata, version }) } } export async function deletePatch<T> (opts: IPluginOptions<T>, context: IContext<T>): Promise<void> { await bulkPatch(opts, context, 'eventDeleted', 'deletedDocs') }
src/patch.ts
ilovepixelart-ts-patch-mongoose-3306d83
[ { "filename": "src/models/History.ts", "retrieved_chunk": " },\n metadata: {\n type: Object\n },\n version: {\n type: Number,\n min: 0,\n default: 0\n }\n}, { timestamps: true })", "score": 10.666063951679432 }, { "filename": "src/models/History.ts", "retrieved_chunk": "HistorySchema.index({ collectionId: 1, version: -1 })\nHistorySchema.index({ op: 1, modelName: 1, collectionName: 1, collectionId: 1, reason: 1, version: 1 })\nconst History = model('History', HistorySchema, 'history')\nexport default History", "score": 10.553447249421463 }, { "filename": "src/version.ts", "retrieved_chunk": "import { satisfies } from 'semver'\nimport mongoose from 'mongoose'\nexport const isMongooseLessThan7 = satisfies(mongoose.version, '<7')\nexport const isMongoose6 = satisfies(mongoose.version, '6')\nif (isMongoose6) {\n mongoose.set('strictQuery', false)\n}", "score": 4.990965582105831 }, { "filename": "src/interfaces/IHistory.ts", "retrieved_chunk": " reason?: string\n metadata?: object\n patch?: Operation[]\n}\nexport default IHistory", "score": 3.843371081362858 }, { "filename": "src/plugin.ts", "retrieved_chunk": " })\n schema.post(deleteMethods as MongooseQueryMiddleware[], { document: false, query: true }, async function (this: IHookContext<T>) {\n const options = this.getOptions()\n if (isHookIgnored(options)) return\n await deletePatch(opts, this._context)\n })\n}", "score": 3.4446542042480974 } ]
typescript
await History.bulkWrite(bulk, { ordered: false }) }
import _ from 'lodash' import { assign } from 'power-assign' import type { HydratedDocument, Model, MongooseQueryMiddleware, QueryOptions, Schema, ToObjectOptions, UpdateQuery, UpdateWithAggregationPipeline } from 'mongoose' import type IPluginOptions from './interfaces/IPluginOptions' import type IContext from './interfaces/IContext' import type IHookContext from './interfaces/IHookContext' import { createPatch, updatePatch, deletePatch } from './patch' import { isMongooseLessThan7 } from './version' import em from './em' const remove = isMongooseLessThan7 ? 'remove' : 'deleteOne' const toObjectOptions: ToObjectOptions = { depopulate: true, virtuals: false } const updateMethods = [ 'update', 'updateOne', 'replaceOne', 'updateMany', 'findOneAndUpdate', 'findOneAndReplace', 'findByIdAndUpdate' ] const deleteMethods = [ 'remove', 'findOneAndDelete', 'findOneAndRemove', 'findByIdAndDelete', 'findByIdAndRemove', 'deleteOne', 'deleteMany' ] function isHookIgnored<T> (options: QueryOptions<T>): boolean { return options.ignoreHook === true || (options.ignoreEvent === true && options.ignorePatchHistory === true) } function splitUpdateAndCommands<T> (updateQuery: UpdateWithAggregationPipeline | UpdateQuery<T> | null): { update: UpdateQuery<T>, commands: Record<string, unknown>[] } { let update: UpdateQuery<T> = {} const commands: Record<string, unknown>[] = [] if (!_.isEmpty(updateQuery) && !_.isArray(updateQuery) && _.isObjectLike(updateQuery)) { update = _.cloneDeep(updateQuery) const keys = _.keys(update).filter((key) => key.startsWith('$')) if (!_.isEmpty(keys)) { _.forEach(keys, (key) => { commands.push({ [key]: update[key] as unknown }) // eslint-disable-next-line @typescript-eslint/no-dynamic-delete delete update[key] }) } } return { update, commands } } function assignUpdate<T> (document: HydratedDocument<T>, update: UpdateQuery<T>, commands: Record<string, unknown>[]): HydratedDocument<T> { let updated = assign(document, update) _.forEach(commands, (command) => { try { updated = assign(updated, command) } catch { // we catch assign keys that are not implemented } }) return updated } /** * @description Patch patch event emitter */ export const patchEventEmitter = em /** * @description Patch history plugin * @param {Schema} schema * @param {IPluginOptions} opts * @returns {void} */ export const patchHistoryPlugin = function plugin<T> (schema: Schema<T>, opts: IPluginOptions<T>): void { schema.pre('save', async function () { const current = this.toObject(toObjectOptions) as HydratedDocument<T> const model = this.constructor as Model<T> const context: IContext<T> = { op: this.isNew ? 'create' : 'update', modelName: opts.modelName ?? model.modelName, collectionName: opts.collectionName ?? model.collection.collectionName, createdDocs: [current] } if (this.isNew) { await createPatch(opts, context) } else { const original = await model.findById(current._id).lean().exec() if (original) { await updatePatch(opts, context, current, original as HydratedDocument<T>) } } }) schema.post('insertMany', async function (docs) { const context = { op: 'create', modelName: opts.modelName ?? this.modelName, collectionName: opts.collectionName ?? this.collection.collectionName, createdDocs: docs as unknown as HydratedDocument<T>[] } await createPatch(opts, context) }) schema.pre(updateMethods as MongooseQueryMiddleware[], async function (this:
IHookContext<T>) {
const options = this.getOptions() if (isHookIgnored(options)) return const model = this.model as Model<T> const filter = this.getFilter() const count = await this.model.count(filter).exec() this._context = { op: this.op, modelName: opts.modelName ?? this.model.modelName, collectionName: opts.collectionName ?? this.model.collection.collectionName, isNew: options.upsert && count === 0, ignoreEvent: options.ignoreEvent as boolean, ignorePatchHistory: options.ignorePatchHistory as boolean } const updateQuery = this.getUpdate() const { update, commands } = splitUpdateAndCommands(updateQuery) const cursor = model.find(filter).lean().cursor() await cursor.eachAsync(async (doc: HydratedDocument<T>) => { await updatePatch(opts, this._context, assignUpdate(doc, update, commands), doc) }) }) schema.post(updateMethods as MongooseQueryMiddleware[], async function (this: IHookContext<T>) { const options = this.getOptions() if (isHookIgnored(options)) return if (!this._context.isNew) return const model = this.model as Model<T> const updateQuery = this.getUpdate() const { update, commands } = splitUpdateAndCommands(updateQuery) const filter = assignUpdate({} as HydratedDocument<T>, update, commands) if (!_.isEmpty(filter)) { const current = await model.findOne(update).lean().exec() if (current) { this._context.createdDocs = [current] as HydratedDocument<T>[] await createPatch(opts, this._context) } } }) schema.pre(remove, { document: true, query: false }, async function () { const original = this.toObject(toObjectOptions) as HydratedDocument<T> if (opts.preDelete && !_.isEmpty(original)) { await opts.preDelete([original]) } }) schema.post(remove, { document: true, query: false }, async function (this: HydratedDocument<T>) { const original = this.toObject(toObjectOptions) as HydratedDocument<T> const model = this.constructor as Model<T> const context: IContext<T> = { op: 'delete', modelName: opts.modelName ?? model.modelName, collectionName: opts.collectionName ?? model.collection.collectionName, deletedDocs: [original] } await deletePatch(opts, context) }) schema.pre(deleteMethods as MongooseQueryMiddleware[], { document: false, query: true }, async function (this: IHookContext<T>) { const options = this.getOptions() if (isHookIgnored(options)) return const model = this.model as Model<T> const filter = this.getFilter() this._context = { op: this.op, modelName: opts.modelName ?? this.model.modelName, collectionName: opts.collectionName ?? this.model.collection.collectionName, ignoreEvent: options.ignoreEvent as boolean, ignorePatchHistory: options.ignorePatchHistory as boolean } if (['remove', 'deleteMany'].includes(this._context.op) && !options.single) { const docs = await model.find(filter).lean().exec() if (!_.isEmpty(docs)) { this._context.deletedDocs = docs as HydratedDocument<T>[] } } else { const doc = await model.findOne(filter).lean().exec() if (!_.isEmpty(doc)) { this._context.deletedDocs = [doc] as HydratedDocument<T>[] } } if (opts.preDelete && _.isArray(this._context.deletedDocs) && !_.isEmpty(this._context.deletedDocs)) { await opts.preDelete(this._context.deletedDocs) } }) schema.post(deleteMethods as MongooseQueryMiddleware[], { document: false, query: true }, async function (this: IHookContext<T>) { const options = this.getOptions() if (isHookIgnored(options)) return await deletePatch(opts, this._context) }) }
src/plugin.ts
ilovepixelart-ts-patch-mongoose-3306d83
[ { "filename": "src/patch.ts", "retrieved_chunk": " const [user, reason, metadata] = await getData(opts)\n await History.create({\n op: context.op,\n modelName: context.modelName,\n collectionName: context.collectionName,\n collectionId: original._id as Types.ObjectId,\n patch,\n user,\n reason,\n metadata,", "score": 27.54125712322702 }, { "filename": "src/patch.ts", "retrieved_chunk": " emitEvent(context, event, { [key]: doc })\n if (history) {\n bulk.push({\n insertOne: {\n document: {\n op: context.op,\n modelName: context.modelName,\n collectionName: context.collectionName,\n collectionId: doc._id as Types.ObjectId,\n doc,", "score": 20.736965147650338 }, { "filename": "src/patch.ts", "retrieved_chunk": " await History.bulkWrite(bulk, { ordered: false })\n }\n }\n}\nexport async function createPatch<T> (opts: IPluginOptions<T>, context: IContext<T>): Promise<void> {\n await bulkPatch(opts, context, 'eventCreated', 'createdDocs')\n}\nexport async function updatePatch<T> (opts: IPluginOptions<T>, context: IContext<T>, current: HydratedDocument<T>, original: HydratedDocument<T>): Promise<void> {\n const history = isPatchHistoryEnabled(opts, context)\n const { currentObject, originalObject } = getObjects(opts, current, original)", "score": 16.254387251039716 }, { "filename": "src/patch.ts", "retrieved_chunk": "function isPatchHistoryEnabled<T> (opts: IPluginOptions<T>, context: IContext<T>): boolean {\n return !opts.patchHistoryDisabled && !context.ignorePatchHistory\n}\nexport function getObjects<T> (opts: IPluginOptions<T>, current: HydratedDocument<T>, original: HydratedDocument<T>): { currentObject: Partial<T>, originalObject: Partial<T> } {\n let currentObject = JSON.parse(JSON.stringify(current)) as Partial<T>\n let originalObject = JSON.parse(JSON.stringify(original)) as Partial<T>\n if (opts.omit) {\n currentObject = omit(currentObject, opts.omit)\n originalObject = omit(originalObject, opts.omit)\n }", "score": 13.115946182725537 }, { "filename": "src/interfaces/IContext.ts", "retrieved_chunk": "import type { HydratedDocument } from 'mongoose'\ninterface IContext<T> {\n op: string\n modelName: string\n collectionName: string\n isNew?: boolean\n createdDocs?: HydratedDocument<T>[]\n deletedDocs?: HydratedDocument<T>[]\n ignoreEvent?: boolean\n ignorePatchHistory?: boolean", "score": 12.925935048965835 } ]
typescript
IHookContext<T>) {
import _ from 'lodash' import { assign } from 'power-assign' import type { HydratedDocument, Model, MongooseQueryMiddleware, QueryOptions, Schema, ToObjectOptions, UpdateQuery, UpdateWithAggregationPipeline } from 'mongoose' import type IPluginOptions from './interfaces/IPluginOptions' import type IContext from './interfaces/IContext' import type IHookContext from './interfaces/IHookContext' import { createPatch, updatePatch, deletePatch } from './patch' import { isMongooseLessThan7 } from './version' import em from './em' const remove = isMongooseLessThan7 ? 'remove' : 'deleteOne' const toObjectOptions: ToObjectOptions = { depopulate: true, virtuals: false } const updateMethods = [ 'update', 'updateOne', 'replaceOne', 'updateMany', 'findOneAndUpdate', 'findOneAndReplace', 'findByIdAndUpdate' ] const deleteMethods = [ 'remove', 'findOneAndDelete', 'findOneAndRemove', 'findByIdAndDelete', 'findByIdAndRemove', 'deleteOne', 'deleteMany' ] function isHookIgnored<T> (options: QueryOptions<T>): boolean { return options.ignoreHook === true || (options.ignoreEvent === true && options.ignorePatchHistory === true) } function splitUpdateAndCommands<T> (updateQuery: UpdateWithAggregationPipeline | UpdateQuery<T> | null): { update: UpdateQuery<T>, commands: Record<string, unknown>[] } { let update: UpdateQuery<T> = {} const commands: Record<string, unknown>[] = [] if (!_.isEmpty(updateQuery) && !_.isArray(updateQuery) && _.isObjectLike(updateQuery)) { update = _.cloneDeep(updateQuery) const keys = _.keys(update).filter((key) => key.startsWith('$')) if (!_.isEmpty(keys)) { _.forEach(keys, (key) => { commands.push({ [key]: update[key] as unknown }) // eslint-disable-next-line @typescript-eslint/no-dynamic-delete delete update[key] }) } } return { update, commands } } function assignUpdate<T> (document: HydratedDocument<T>, update: UpdateQuery<T>, commands: Record<string, unknown>[]): HydratedDocument<T> { let updated = assign(document, update) _.forEach(commands, (command) => { try { updated = assign(updated, command) } catch { // we catch assign keys that are not implemented } }) return updated } /** * @description Patch patch event emitter */ export const patchEventEmitter = em /** * @description Patch history plugin * @param {Schema} schema * @param {IPluginOptions} opts * @returns {void} */ export const patchHistoryPlugin = function plugin<T> (schema: Schema<T>, opts: IPluginOptions<T>): void { schema.pre('save', async function () { const current = this.toObject(toObjectOptions) as HydratedDocument<T> const model = this.constructor as Model<T> const context
: IContext<T> = {
op: this.isNew ? 'create' : 'update', modelName: opts.modelName ?? model.modelName, collectionName: opts.collectionName ?? model.collection.collectionName, createdDocs: [current] } if (this.isNew) { await createPatch(opts, context) } else { const original = await model.findById(current._id).lean().exec() if (original) { await updatePatch(opts, context, current, original as HydratedDocument<T>) } } }) schema.post('insertMany', async function (docs) { const context = { op: 'create', modelName: opts.modelName ?? this.modelName, collectionName: opts.collectionName ?? this.collection.collectionName, createdDocs: docs as unknown as HydratedDocument<T>[] } await createPatch(opts, context) }) schema.pre(updateMethods as MongooseQueryMiddleware[], async function (this: IHookContext<T>) { const options = this.getOptions() if (isHookIgnored(options)) return const model = this.model as Model<T> const filter = this.getFilter() const count = await this.model.count(filter).exec() this._context = { op: this.op, modelName: opts.modelName ?? this.model.modelName, collectionName: opts.collectionName ?? this.model.collection.collectionName, isNew: options.upsert && count === 0, ignoreEvent: options.ignoreEvent as boolean, ignorePatchHistory: options.ignorePatchHistory as boolean } const updateQuery = this.getUpdate() const { update, commands } = splitUpdateAndCommands(updateQuery) const cursor = model.find(filter).lean().cursor() await cursor.eachAsync(async (doc: HydratedDocument<T>) => { await updatePatch(opts, this._context, assignUpdate(doc, update, commands), doc) }) }) schema.post(updateMethods as MongooseQueryMiddleware[], async function (this: IHookContext<T>) { const options = this.getOptions() if (isHookIgnored(options)) return if (!this._context.isNew) return const model = this.model as Model<T> const updateQuery = this.getUpdate() const { update, commands } = splitUpdateAndCommands(updateQuery) const filter = assignUpdate({} as HydratedDocument<T>, update, commands) if (!_.isEmpty(filter)) { const current = await model.findOne(update).lean().exec() if (current) { this._context.createdDocs = [current] as HydratedDocument<T>[] await createPatch(opts, this._context) } } }) schema.pre(remove, { document: true, query: false }, async function () { const original = this.toObject(toObjectOptions) as HydratedDocument<T> if (opts.preDelete && !_.isEmpty(original)) { await opts.preDelete([original]) } }) schema.post(remove, { document: true, query: false }, async function (this: HydratedDocument<T>) { const original = this.toObject(toObjectOptions) as HydratedDocument<T> const model = this.constructor as Model<T> const context: IContext<T> = { op: 'delete', modelName: opts.modelName ?? model.modelName, collectionName: opts.collectionName ?? model.collection.collectionName, deletedDocs: [original] } await deletePatch(opts, context) }) schema.pre(deleteMethods as MongooseQueryMiddleware[], { document: false, query: true }, async function (this: IHookContext<T>) { const options = this.getOptions() if (isHookIgnored(options)) return const model = this.model as Model<T> const filter = this.getFilter() this._context = { op: this.op, modelName: opts.modelName ?? this.model.modelName, collectionName: opts.collectionName ?? this.model.collection.collectionName, ignoreEvent: options.ignoreEvent as boolean, ignorePatchHistory: options.ignorePatchHistory as boolean } if (['remove', 'deleteMany'].includes(this._context.op) && !options.single) { const docs = await model.find(filter).lean().exec() if (!_.isEmpty(docs)) { this._context.deletedDocs = docs as HydratedDocument<T>[] } } else { const doc = await model.findOne(filter).lean().exec() if (!_.isEmpty(doc)) { this._context.deletedDocs = [doc] as HydratedDocument<T>[] } } if (opts.preDelete && _.isArray(this._context.deletedDocs) && !_.isEmpty(this._context.deletedDocs)) { await opts.preDelete(this._context.deletedDocs) } }) schema.post(deleteMethods as MongooseQueryMiddleware[], { document: false, query: true }, async function (this: IHookContext<T>) { const options = this.getOptions() if (isHookIgnored(options)) return await deletePatch(opts, this._context) }) }
src/plugin.ts
ilovepixelart-ts-patch-mongoose-3306d83
[ { "filename": "src/patch.ts", "retrieved_chunk": " await History.bulkWrite(bulk, { ordered: false })\n }\n }\n}\nexport async function createPatch<T> (opts: IPluginOptions<T>, context: IContext<T>): Promise<void> {\n await bulkPatch(opts, context, 'eventCreated', 'createdDocs')\n}\nexport async function updatePatch<T> (opts: IPluginOptions<T>, context: IContext<T>, current: HydratedDocument<T>, original: HydratedDocument<T>): Promise<void> {\n const history = isPatchHistoryEnabled(opts, context)\n const { currentObject, originalObject } = getObjects(opts, current, original)", "score": 23.25386487163529 }, { "filename": "src/patch.ts", "retrieved_chunk": " version\n })\n }\n}\nexport async function deletePatch<T> (opts: IPluginOptions<T>, context: IContext<T>): Promise<void> {\n await bulkPatch(opts, context, 'eventDeleted', 'deletedDocs')\n}", "score": 15.952309197376673 }, { "filename": "src/patch.ts", "retrieved_chunk": "function isPatchHistoryEnabled<T> (opts: IPluginOptions<T>, context: IContext<T>): boolean {\n return !opts.patchHistoryDisabled && !context.ignorePatchHistory\n}\nexport function getObjects<T> (opts: IPluginOptions<T>, current: HydratedDocument<T>, original: HydratedDocument<T>): { currentObject: Partial<T>, originalObject: Partial<T> } {\n let currentObject = JSON.parse(JSON.stringify(current)) as Partial<T>\n let originalObject = JSON.parse(JSON.stringify(original)) as Partial<T>\n if (opts.omit) {\n currentObject = omit(currentObject, opts.omit)\n originalObject = omit(originalObject, opts.omit)\n }", "score": 15.452780018793476 }, { "filename": "src/patch.ts", "retrieved_chunk": " getValue(metadata)\n ]\n })\n}\nexport function emitEvent<T> (context: IContext<T>, event: string | undefined, data: IEvent<T>): void {\n if (event && !context.ignoreEvent) {\n em.emit(event, data)\n }\n}\nexport async function bulkPatch<T> (opts: IPluginOptions<T>, context: IContext<T>, eventKey: 'eventCreated' | 'eventDeleted', docsKey: 'createdDocs' | 'deletedDocs'): Promise<void> {", "score": 14.464022093650284 }, { "filename": "src/models/History.ts", "retrieved_chunk": "import { Schema, model } from 'mongoose'\nimport type IHistory from '../interfaces/IHistory'\nconst HistorySchema = new Schema<IHistory>({\n op: {\n type: String,\n required: true\n },\n modelName: {\n type: String,\n required: true", "score": 13.811564714000522 } ]
typescript
: IContext<T> = {
import _ from 'lodash' import { assign } from 'power-assign' import type { HydratedDocument, Model, MongooseQueryMiddleware, QueryOptions, Schema, ToObjectOptions, UpdateQuery, UpdateWithAggregationPipeline } from 'mongoose' import type IPluginOptions from './interfaces/IPluginOptions' import type IContext from './interfaces/IContext' import type IHookContext from './interfaces/IHookContext' import { createPatch, updatePatch, deletePatch } from './patch' import { isMongooseLessThan7 } from './version' import em from './em' const remove = isMongooseLessThan7 ? 'remove' : 'deleteOne' const toObjectOptions: ToObjectOptions = { depopulate: true, virtuals: false } const updateMethods = [ 'update', 'updateOne', 'replaceOne', 'updateMany', 'findOneAndUpdate', 'findOneAndReplace', 'findByIdAndUpdate' ] const deleteMethods = [ 'remove', 'findOneAndDelete', 'findOneAndRemove', 'findByIdAndDelete', 'findByIdAndRemove', 'deleteOne', 'deleteMany' ] function isHookIgnored<T> (options: QueryOptions<T>): boolean { return options.ignoreHook === true || (options.ignoreEvent === true && options.ignorePatchHistory === true) } function splitUpdateAndCommands<T> (updateQuery: UpdateWithAggregationPipeline | UpdateQuery<T> | null): { update: UpdateQuery<T>, commands: Record<string, unknown>[] } { let update: UpdateQuery<T> = {} const commands: Record<string, unknown>[] = [] if (!_.isEmpty(updateQuery) && !_.isArray(updateQuery) && _.isObjectLike(updateQuery)) { update = _.cloneDeep(updateQuery) const keys = _.keys(update).filter((key) => key.startsWith('$')) if (!_.isEmpty(keys)) { _.forEach(keys, (key) => { commands.push({ [key]: update[key] as unknown }) // eslint-disable-next-line @typescript-eslint/no-dynamic-delete delete update[key] }) } } return { update, commands } } function assignUpdate<T> (document: HydratedDocument<T>, update: UpdateQuery<T>, commands: Record<string, unknown>[]): HydratedDocument<T> { let updated = assign(document, update) _.forEach(commands, (command) => { try { updated = assign(updated, command) } catch { // we catch assign keys that are not implemented } }) return updated } /** * @description Patch patch event emitter */ export const patchEventEmitter = em /** * @description Patch history plugin * @param {Schema} schema * @param {IPluginOptions} opts * @returns {void} */ export const patchHistoryPlugin = function plugin<T> (schema: Schema<T>, opts: IPluginOptions<T>): void { schema.pre('save', async function () { const current = this.toObject(toObjectOptions) as HydratedDocument<T> const model = this.constructor as Model<T> const context: IContext<T> = { op: this.isNew ? 'create' : 'update', modelName: opts.modelName ?? model.modelName, collectionName: opts.collectionName ?? model.collection.collectionName, createdDocs: [current] } if (this.isNew) {
await createPatch(opts, context) } else {
const original = await model.findById(current._id).lean().exec() if (original) { await updatePatch(opts, context, current, original as HydratedDocument<T>) } } }) schema.post('insertMany', async function (docs) { const context = { op: 'create', modelName: opts.modelName ?? this.modelName, collectionName: opts.collectionName ?? this.collection.collectionName, createdDocs: docs as unknown as HydratedDocument<T>[] } await createPatch(opts, context) }) schema.pre(updateMethods as MongooseQueryMiddleware[], async function (this: IHookContext<T>) { const options = this.getOptions() if (isHookIgnored(options)) return const model = this.model as Model<T> const filter = this.getFilter() const count = await this.model.count(filter).exec() this._context = { op: this.op, modelName: opts.modelName ?? this.model.modelName, collectionName: opts.collectionName ?? this.model.collection.collectionName, isNew: options.upsert && count === 0, ignoreEvent: options.ignoreEvent as boolean, ignorePatchHistory: options.ignorePatchHistory as boolean } const updateQuery = this.getUpdate() const { update, commands } = splitUpdateAndCommands(updateQuery) const cursor = model.find(filter).lean().cursor() await cursor.eachAsync(async (doc: HydratedDocument<T>) => { await updatePatch(opts, this._context, assignUpdate(doc, update, commands), doc) }) }) schema.post(updateMethods as MongooseQueryMiddleware[], async function (this: IHookContext<T>) { const options = this.getOptions() if (isHookIgnored(options)) return if (!this._context.isNew) return const model = this.model as Model<T> const updateQuery = this.getUpdate() const { update, commands } = splitUpdateAndCommands(updateQuery) const filter = assignUpdate({} as HydratedDocument<T>, update, commands) if (!_.isEmpty(filter)) { const current = await model.findOne(update).lean().exec() if (current) { this._context.createdDocs = [current] as HydratedDocument<T>[] await createPatch(opts, this._context) } } }) schema.pre(remove, { document: true, query: false }, async function () { const original = this.toObject(toObjectOptions) as HydratedDocument<T> if (opts.preDelete && !_.isEmpty(original)) { await opts.preDelete([original]) } }) schema.post(remove, { document: true, query: false }, async function (this: HydratedDocument<T>) { const original = this.toObject(toObjectOptions) as HydratedDocument<T> const model = this.constructor as Model<T> const context: IContext<T> = { op: 'delete', modelName: opts.modelName ?? model.modelName, collectionName: opts.collectionName ?? model.collection.collectionName, deletedDocs: [original] } await deletePatch(opts, context) }) schema.pre(deleteMethods as MongooseQueryMiddleware[], { document: false, query: true }, async function (this: IHookContext<T>) { const options = this.getOptions() if (isHookIgnored(options)) return const model = this.model as Model<T> const filter = this.getFilter() this._context = { op: this.op, modelName: opts.modelName ?? this.model.modelName, collectionName: opts.collectionName ?? this.model.collection.collectionName, ignoreEvent: options.ignoreEvent as boolean, ignorePatchHistory: options.ignorePatchHistory as boolean } if (['remove', 'deleteMany'].includes(this._context.op) && !options.single) { const docs = await model.find(filter).lean().exec() if (!_.isEmpty(docs)) { this._context.deletedDocs = docs as HydratedDocument<T>[] } } else { const doc = await model.findOne(filter).lean().exec() if (!_.isEmpty(doc)) { this._context.deletedDocs = [doc] as HydratedDocument<T>[] } } if (opts.preDelete && _.isArray(this._context.deletedDocs) && !_.isEmpty(this._context.deletedDocs)) { await opts.preDelete(this._context.deletedDocs) } }) schema.post(deleteMethods as MongooseQueryMiddleware[], { document: false, query: true }, async function (this: IHookContext<T>) { const options = this.getOptions() if (isHookIgnored(options)) return await deletePatch(opts, this._context) }) }
src/plugin.ts
ilovepixelart-ts-patch-mongoose-3306d83
[ { "filename": "src/patch.ts", "retrieved_chunk": " const [user, reason, metadata] = await getData(opts)\n await History.create({\n op: context.op,\n modelName: context.modelName,\n collectionName: context.collectionName,\n collectionId: original._id as Types.ObjectId,\n patch,\n user,\n reason,\n metadata,", "score": 24.972571280347715 }, { "filename": "src/interfaces/IContext.ts", "retrieved_chunk": "import type { HydratedDocument } from 'mongoose'\ninterface IContext<T> {\n op: string\n modelName: string\n collectionName: string\n isNew?: boolean\n createdDocs?: HydratedDocument<T>[]\n deletedDocs?: HydratedDocument<T>[]\n ignoreEvent?: boolean\n ignorePatchHistory?: boolean", "score": 18.232547328210234 }, { "filename": "src/patch.ts", "retrieved_chunk": " emitEvent(context, event, { [key]: doc })\n if (history) {\n bulk.push({\n insertOne: {\n document: {\n op: context.op,\n modelName: context.modelName,\n collectionName: context.collectionName,\n collectionId: doc._id as Types.ObjectId,\n doc,", "score": 17.92438197105396 }, { "filename": "src/models/History.ts", "retrieved_chunk": "HistorySchema.index({ collectionId: 1, version: -1 })\nHistorySchema.index({ op: 1, modelName: 1, collectionName: 1, collectionId: 1, reason: 1, version: 1 })\nconst History = model('History', HistorySchema, 'history')\nexport default History", "score": 17.21561384275218 }, { "filename": "src/patch.ts", "retrieved_chunk": " await History.bulkWrite(bulk, { ordered: false })\n }\n }\n}\nexport async function createPatch<T> (opts: IPluginOptions<T>, context: IContext<T>): Promise<void> {\n await bulkPatch(opts, context, 'eventCreated', 'createdDocs')\n}\nexport async function updatePatch<T> (opts: IPluginOptions<T>, context: IContext<T>, current: HydratedDocument<T>, original: HydratedDocument<T>): Promise<void> {\n const history = isPatchHistoryEnabled(opts, context)\n const { currentObject, originalObject } = getObjects(opts, current, original)", "score": 17.04758464767197 } ]
typescript
await createPatch(opts, context) } else {
import _ from 'lodash' import { assign } from 'power-assign' import type { HydratedDocument, Model, MongooseQueryMiddleware, QueryOptions, Schema, ToObjectOptions, UpdateQuery, UpdateWithAggregationPipeline } from 'mongoose' import type IPluginOptions from './interfaces/IPluginOptions' import type IContext from './interfaces/IContext' import type IHookContext from './interfaces/IHookContext' import { createPatch, updatePatch, deletePatch } from './patch' import { isMongooseLessThan7 } from './version' import em from './em' const remove = isMongooseLessThan7 ? 'remove' : 'deleteOne' const toObjectOptions: ToObjectOptions = { depopulate: true, virtuals: false } const updateMethods = [ 'update', 'updateOne', 'replaceOne', 'updateMany', 'findOneAndUpdate', 'findOneAndReplace', 'findByIdAndUpdate' ] const deleteMethods = [ 'remove', 'findOneAndDelete', 'findOneAndRemove', 'findByIdAndDelete', 'findByIdAndRemove', 'deleteOne', 'deleteMany' ] function isHookIgnored<T> (options: QueryOptions<T>): boolean { return options.ignoreHook === true || (options.ignoreEvent === true && options.ignorePatchHistory === true) } function splitUpdateAndCommands<T> (updateQuery: UpdateWithAggregationPipeline | UpdateQuery<T> | null): { update: UpdateQuery<T>, commands: Record<string, unknown>[] } { let update: UpdateQuery<T> = {} const commands: Record<string, unknown>[] = [] if (!_.isEmpty(updateQuery) && !_.isArray(updateQuery) && _.isObjectLike(updateQuery)) { update = _.cloneDeep(updateQuery) const keys = _.keys(update).filter((key) => key.startsWith('$')) if (!_.isEmpty(keys)) { _.forEach(keys, (key) => { commands.push({ [key]: update[key] as unknown }) // eslint-disable-next-line @typescript-eslint/no-dynamic-delete delete update[key] }) } } return { update, commands } } function assignUpdate<T> (document: HydratedDocument<T>, update: UpdateQuery<T>, commands: Record<string, unknown>[]): HydratedDocument<T> { let updated = assign(document, update) _.forEach(commands, (command) => { try { updated = assign(updated, command) } catch { // we catch assign keys that are not implemented } }) return updated } /** * @description Patch patch event emitter */ export const patchEventEmitter = em /** * @description Patch history plugin * @param {Schema} schema * @param {IPluginOptions} opts * @returns {void} */ export const patchHistoryPlugin = function plugin<T> (schema: Schema<T>, opts: IPluginOptions<T>): void { schema.pre('save', async function () { const current = this.toObject(toObjectOptions) as HydratedDocument<T> const model = this.constructor as Model<T> const context: IContext<T> = { op: this.isNew ? 'create' : 'update', modelName: opts.modelName ?? model.modelName, collectionName: opts.collectionName ?? model.collection.collectionName, createdDocs: [current] } if (this.isNew) { await createPatch(opts, context) } else { const original = await model.findById(current._id).lean().exec() if (original) { await updatePatch(opts, context, current, original as HydratedDocument<T>) } } }) schema.post('insertMany', async function (docs) { const context = { op: 'create', modelName: opts.modelName ?? this.modelName, collectionName: opts.collectionName ?? this.collection.collectionName, createdDocs: docs as unknown as HydratedDocument<T>[] } await createPatch(opts, context) })
schema.pre(updateMethods as MongooseQueryMiddleware[], async function (this: IHookContext<T>) {
const options = this.getOptions() if (isHookIgnored(options)) return const model = this.model as Model<T> const filter = this.getFilter() const count = await this.model.count(filter).exec() this._context = { op: this.op, modelName: opts.modelName ?? this.model.modelName, collectionName: opts.collectionName ?? this.model.collection.collectionName, isNew: options.upsert && count === 0, ignoreEvent: options.ignoreEvent as boolean, ignorePatchHistory: options.ignorePatchHistory as boolean } const updateQuery = this.getUpdate() const { update, commands } = splitUpdateAndCommands(updateQuery) const cursor = model.find(filter).lean().cursor() await cursor.eachAsync(async (doc: HydratedDocument<T>) => { await updatePatch(opts, this._context, assignUpdate(doc, update, commands), doc) }) }) schema.post(updateMethods as MongooseQueryMiddleware[], async function (this: IHookContext<T>) { const options = this.getOptions() if (isHookIgnored(options)) return if (!this._context.isNew) return const model = this.model as Model<T> const updateQuery = this.getUpdate() const { update, commands } = splitUpdateAndCommands(updateQuery) const filter = assignUpdate({} as HydratedDocument<T>, update, commands) if (!_.isEmpty(filter)) { const current = await model.findOne(update).lean().exec() if (current) { this._context.createdDocs = [current] as HydratedDocument<T>[] await createPatch(opts, this._context) } } }) schema.pre(remove, { document: true, query: false }, async function () { const original = this.toObject(toObjectOptions) as HydratedDocument<T> if (opts.preDelete && !_.isEmpty(original)) { await opts.preDelete([original]) } }) schema.post(remove, { document: true, query: false }, async function (this: HydratedDocument<T>) { const original = this.toObject(toObjectOptions) as HydratedDocument<T> const model = this.constructor as Model<T> const context: IContext<T> = { op: 'delete', modelName: opts.modelName ?? model.modelName, collectionName: opts.collectionName ?? model.collection.collectionName, deletedDocs: [original] } await deletePatch(opts, context) }) schema.pre(deleteMethods as MongooseQueryMiddleware[], { document: false, query: true }, async function (this: IHookContext<T>) { const options = this.getOptions() if (isHookIgnored(options)) return const model = this.model as Model<T> const filter = this.getFilter() this._context = { op: this.op, modelName: opts.modelName ?? this.model.modelName, collectionName: opts.collectionName ?? this.model.collection.collectionName, ignoreEvent: options.ignoreEvent as boolean, ignorePatchHistory: options.ignorePatchHistory as boolean } if (['remove', 'deleteMany'].includes(this._context.op) && !options.single) { const docs = await model.find(filter).lean().exec() if (!_.isEmpty(docs)) { this._context.deletedDocs = docs as HydratedDocument<T>[] } } else { const doc = await model.findOne(filter).lean().exec() if (!_.isEmpty(doc)) { this._context.deletedDocs = [doc] as HydratedDocument<T>[] } } if (opts.preDelete && _.isArray(this._context.deletedDocs) && !_.isEmpty(this._context.deletedDocs)) { await opts.preDelete(this._context.deletedDocs) } }) schema.post(deleteMethods as MongooseQueryMiddleware[], { document: false, query: true }, async function (this: IHookContext<T>) { const options = this.getOptions() if (isHookIgnored(options)) return await deletePatch(opts, this._context) }) }
src/plugin.ts
ilovepixelart-ts-patch-mongoose-3306d83
[ { "filename": "src/patch.ts", "retrieved_chunk": " const [user, reason, metadata] = await getData(opts)\n await History.create({\n op: context.op,\n modelName: context.modelName,\n collectionName: context.collectionName,\n collectionId: original._id as Types.ObjectId,\n patch,\n user,\n reason,\n metadata,", "score": 27.54125712322702 }, { "filename": "src/patch.ts", "retrieved_chunk": " emitEvent(context, event, { [key]: doc })\n if (history) {\n bulk.push({\n insertOne: {\n document: {\n op: context.op,\n modelName: context.modelName,\n collectionName: context.collectionName,\n collectionId: doc._id as Types.ObjectId,\n doc,", "score": 20.736965147650338 }, { "filename": "src/patch.ts", "retrieved_chunk": " await History.bulkWrite(bulk, { ordered: false })\n }\n }\n}\nexport async function createPatch<T> (opts: IPluginOptions<T>, context: IContext<T>): Promise<void> {\n await bulkPatch(opts, context, 'eventCreated', 'createdDocs')\n}\nexport async function updatePatch<T> (opts: IPluginOptions<T>, context: IContext<T>, current: HydratedDocument<T>, original: HydratedDocument<T>): Promise<void> {\n const history = isPatchHistoryEnabled(opts, context)\n const { currentObject, originalObject } = getObjects(opts, current, original)", "score": 18.60747687331272 }, { "filename": "src/patch.ts", "retrieved_chunk": " const history = isPatchHistoryEnabled(opts, context)\n const event = opts[eventKey]\n const docs = context[docsKey]\n const key = eventKey === 'eventCreated' ? 'doc' : 'oldDoc'\n if (_.isEmpty(docs) || (!event && !history)) return\n const [user, reason, metadata] = await getData(opts)\n const chunks = _.chunk(docs, 1000)\n for await (const chunk of chunks) {\n const bulk = []\n for (const doc of chunk) {", "score": 14.981376656541299 }, { "filename": "src/patch.ts", "retrieved_chunk": "function isPatchHistoryEnabled<T> (opts: IPluginOptions<T>, context: IContext<T>): boolean {\n return !opts.patchHistoryDisabled && !context.ignorePatchHistory\n}\nexport function getObjects<T> (opts: IPluginOptions<T>, current: HydratedDocument<T>, original: HydratedDocument<T>): { currentObject: Partial<T>, originalObject: Partial<T> } {\n let currentObject = JSON.parse(JSON.stringify(current)) as Partial<T>\n let originalObject = JSON.parse(JSON.stringify(original)) as Partial<T>\n if (opts.omit) {\n currentObject = omit(currentObject, opts.omit)\n originalObject = omit(originalObject, opts.omit)\n }", "score": 13.935472156013752 } ]
typescript
schema.pre(updateMethods as MongooseQueryMiddleware[], async function (this: IHookContext<T>) {
import { DecodedInsn } from "./decoder.js"; import { Resource, ZyjsField, withHeap, withStack } from "./util.js"; import zydis from "./native.js"; import { AddressSizeHint, BranchType, BranchWidth, BroadcastMode, ConversionMode, EncodableEncoding, InsnAttribute, MAX_INSN_LENGTH, MachineMode, Mnemonic, OperandSizeHint, OperandType, Register, RoundingMode, SwizzleMode, } from "./enums.js"; import { OperandImm, OperandMem, OperandPtr, OperandReg } from "./common.js"; const OpField = ZyjsField("EncOp"); type OperandLike = keyof typeof Register | number | bigint | OperandImm | OperandReg | OperandPtr | OperandMem; export class EncoderOperand { resource: Resource; constructor(rsrc: Resource) { this.resource = rsrc; } @OpField type!: OperandType; @OpField imms!: bigint; @OpField immu!: bigint; @OpField ptrSeg!: number; @OpField ptrOff!: number; @OpField regv!: Register; @OpField regIs4!: 1 | 0; @OpField memBase!: Register; @OpField memIndex!: Register; @OpField memScale!: Register; @OpField memDisp!: bigint; get imm(): OperandImm { if (this.type !== OperandType.IMMEDIATE) { throw TypeError("Operand type mismatch."); } return { s: this.imms, u: this.immu, }; } set imm(o: OperandImm | bigint | number) { this.type = OperandType.IMMEDIATE; if (typeof o === "object") { if ("s" in o) { this.imms = o.s; } else { this.immu = o.u; } } else { this.imms = BigInt(o); } } get reg(): OperandReg { if (this.type !== OperandType.REGISTER) { throw TypeError("Operand type mismatch."); } return { name: this.regv, is4: !!this.regIs4, }; } set reg(o: OperandReg | Register | keyof typeof Register) { this.type = OperandType.REGISTER; if (typeof o === "string") { this.regv = Register[o]; } else if (typeof o === "number") { this.regv = o; } else { this.regv = o.name; this.regIs4 = o.is4 || false ? 1 : 0; } } get ptr(): OperandPtr { if (this.type !== OperandType.POINTER) { throw TypeError("Operand type mismatch."); } return { segv: this.ptrSeg, off: this.ptrOff, }; } set ptr(o: OperandPtr) { this.type = OperandType.POINTER; this.ptrOff = o.off; this.ptrSeg = o.segv; } get mem(): OperandMem { const res: OperandMem = { index: this.memIndex, base: this.memBase, scale: this.memScale, disp: this.memDisp, }; if (res.seg === Register.NONE) { delete res.seg; } if (res.base === Register.NONE) { delete res.base; } if (res.scale === 0 || res.index === Register.NONE) { delete res.index; delete res.scale; } return res; } set mem(o: OperandMem) { this.type = OperandType.MEMORY; this.memBase = o.base ?? Register.NONE; this.memDisp = o.disp ?? 0n; this.memIndex = o.index ?? Register.NONE; this.memScale = o.scale ?? 0; } get() { switch (this.type) { case OperandType.IMMEDIATE: return this.imm; case OperandType.REGISTER: return this.reg; case OperandType.POINTER: return this.ptr; case OperandType.MEMORY: return this.mem; default: throw Error(); } } set(o: OperandLike) { if (typeof o === "number" || typeof o === "bigint") { return void (this.imm = o); } else if (typeof o === "string") { return void (this.reg = o); } // OperandImm if ("s" in o || "u" in o) { this.imm = o; } // OperandReg. // else if ("name" in o) { this.reg = o; } // OperandPtr. // else if ("off" in o) { this.ptr = o; } // OperandMem. // else { this.mem = o; } } } const ReqField = ZyjsField("EncReq"); export class EncoderRequest { resource: Resource; constructor(from?: DecodedInsn) { this.resource = new Resource(zydis.asm.zyjsNewEncReq(from?.resource.ref() ?? 0)); } encode(at?: bigint) { return withStack((alloc) => { const tmp = alloc.allocate(MAX_INSN_LENGTH); const length = at != null ? zydis.asm.zyjsEncReqEncodeAbs(this.resource.ref(), tmp, at) : zydis.asm.zyjsEncReqEncode(this.resource.ref(), tmp); return zydis.HEAPU8.slice(tmp, tmp + length); }); } @ReqField machineMode!: MachineMode; @ReqField allowedEncodings!: EncodableEncoding; @ReqField mnemonic!: Mnemonic; @ReqField prefixes!: bigint; @ReqField branchType!: BranchType; @ReqField branchWidth!: BranchWidth; @ReqField addressSizeHint!: AddressSizeHint; @ReqField operandSizeHint!: OperandSizeHint; @ReqField operandCount!: number; @ReqField evexBroadcast!: BroadcastMode; @ReqField evexRounding!: RoundingMode; @ReqField evexSAE!: 1 | 0; @ReqField evexZeroingMask!: 1 | 0; @ReqField mvexBroadcast!: BroadcastMode; @ReqField mvexRounding!: RoundingMode; @ReqField mvexSAE!: 1 | 0; @ReqField mvexConversion!: ConversionMode; @ReqField mvexSwizzle!: SwizzleMode; @ReqField mvexEvictionHint!: 1 | 0; operand(n: number): EncoderOperand { const ptr = zydis.asm.zyjsEncReqRefOperand(this.resource.ref(), n); if (!ptr) { throw RangeError("Operand out of boundaries."); }
return new EncoderOperand(this.resource.subresource(ptr));
} static from(machine: MachineMode, mnemonic: Mnemonic, ...operands: OperandLike[]) { const res = new EncoderRequest(); res.mnemonic = mnemonic; res.operandCount = operands.length; res.machineMode = machine; for (let n = 0; n !== operands.length; n++) { const op = operands[n]; res.operand(n).set(op); if (typeof op === "object" && "seg" in op) { switch (op.seg) { case Register.CS: res.prefixes = InsnAttribute.HAS_SEGMENT_CS; break; case Register.DS: res.prefixes = InsnAttribute.HAS_SEGMENT_DS; break; case Register.ES: res.prefixes = InsnAttribute.HAS_SEGMENT_ES; break; case Register.GS: res.prefixes = InsnAttribute.HAS_SEGMENT_GS; break; case Register.FS: res.prefixes = InsnAttribute.HAS_SEGMENT_FS; break; case Register.SS: res.prefixes = InsnAttribute.HAS_SEGMENT_SS; break; } } } return res; } } export function encode(machine: MachineMode, mnemonic: Mnemonic, ...operands: OperandLike[]) { return EncoderRequest.from(machine, mnemonic, ...operands).encode(); } export function encodeAt(at: bigint, machine: MachineMode, mnemonic: Mnemonic, ...operands: OperandLike[]) { return EncoderRequest.from(machine, mnemonic, ...operands).encode(at); } export function encodeNop(len: number) { return (len <= 512 ? withStack : withHeap)((alloc) => { const tmp = alloc.allocate(MAX_INSN_LENGTH); zydis.asm.zyjsEncNop(tmp, len); const res = zydis.HEAPU8.slice(tmp, tmp + len); alloc.free(tmp); return res; }); }
src/encoder.ts
zyantific-zydis-js-4715bb8
[ { "filename": "src/decoder.ts", "retrieved_chunk": "\t\tif (!ptr) {\n\t\t\tthrow RangeError(\"Operand out of boundaries.\");\n\t\t}\n\t\treturn new DecodedOperand(this.resource.subresource(ptr));\n\t}\n}\nexport class Decoder {\n\tresource: Resource;\n\tconstructor(mode: MachineMode, width: StackWidth) {\n\t\tthis.resource = new Resource(zydis.asm.zyjsNewDecoder(mode, width));", "score": 58.83746754187248 }, { "filename": "src/decoder.ts", "retrieved_chunk": "\tget cpuFlags(): AccessedFlags {\n\t\tconst ptr = this.cpuFlagsPtr;\n\t\treturn ptr ? new AccessedFlagsByPtr(ptr) : flagsNoop;\n\t}\n\tget fpuFlags(): AccessedFlags {\n\t\tconst ptr = this.fpuFlagsPtr;\n\t\treturn ptr ? new AccessedFlagsByPtr(ptr) : flagsNoop;\n\t}\n\toperand(n: number): DecodedOperand {\n\t\tconst ptr = zydis.asm.zyjsDecInsnRefOperand(this.resource.ref(), n);", "score": 56.195930564428174 }, { "filename": "src/decoder.ts", "retrieved_chunk": "\t}\n\tset(mode: DecoderMode, value: boolean) {\n\t\tzydis.asm.zyjsDecoderSetMode(this.resource.ref(), mode, value ? 1 : 0);\n\t\treturn this;\n\t}\n\tdecode(buffer: Uint8Array) {\n\t\tconst ptr = withStack((a) => {\n\t\t\treturn zydis.asm.zyjsDecoderDecode(this.resource.ref(), a.buf(buffer), buffer.length);\n\t\t});\n\t\treturn new DecodedInsn(new Resource(ptr));", "score": 43.57993348660162 }, { "filename": "src/util.ts", "retrieved_chunk": "\t\tif (this.#ptr) {\n\t\t\tResource.finalizer.register(this, this.#ptr, this.#token);\n\t\t}\n\t}\n\tsubresource(ptr: number) {\n\t\tconst res = new Resource(0);\n\t\tres.#parent = this;\n\t\tres.#ptr = ptr;\n\t\treturn res;\n\t}", "score": 43.142238891327466 }, { "filename": "src/decoder.ts", "retrieved_chunk": "class AccessedFlagsByPtr implements AccessedFlags {\n\tresource: { ref(): number };\n\tconstructor(ptr: number) {\n\t\tthis.resource = {\n\t\t\tref() {\n\t\t\t\treturn ptr;\n\t\t\t},\n\t\t};\n\t}\n\t@FlagField", "score": 41.845604626759 } ]
typescript
return new EncoderOperand(this.resource.subresource(ptr));
import _ from 'lodash' import { assign } from 'power-assign' import type { HydratedDocument, Model, MongooseQueryMiddleware, QueryOptions, Schema, ToObjectOptions, UpdateQuery, UpdateWithAggregationPipeline } from 'mongoose' import type IPluginOptions from './interfaces/IPluginOptions' import type IContext from './interfaces/IContext' import type IHookContext from './interfaces/IHookContext' import { createPatch, updatePatch, deletePatch } from './patch' import { isMongooseLessThan7 } from './version' import em from './em' const remove = isMongooseLessThan7 ? 'remove' : 'deleteOne' const toObjectOptions: ToObjectOptions = { depopulate: true, virtuals: false } const updateMethods = [ 'update', 'updateOne', 'replaceOne', 'updateMany', 'findOneAndUpdate', 'findOneAndReplace', 'findByIdAndUpdate' ] const deleteMethods = [ 'remove', 'findOneAndDelete', 'findOneAndRemove', 'findByIdAndDelete', 'findByIdAndRemove', 'deleteOne', 'deleteMany' ] function isHookIgnored<T> (options: QueryOptions<T>): boolean { return options.ignoreHook === true || (options.ignoreEvent === true && options.ignorePatchHistory === true) } function splitUpdateAndCommands<T> (updateQuery: UpdateWithAggregationPipeline | UpdateQuery<T> | null): { update: UpdateQuery<T>, commands: Record<string, unknown>[] } { let update: UpdateQuery<T> = {} const commands: Record<string, unknown>[] = [] if (!_.isEmpty(updateQuery) && !_.isArray(updateQuery) && _.isObjectLike(updateQuery)) { update = _.cloneDeep(updateQuery) const keys = _.keys(update).filter((key) => key.startsWith('$')) if (!_.isEmpty(keys)) { _.forEach(keys, (key) => { commands.push({ [key]: update[key] as unknown }) // eslint-disable-next-line @typescript-eslint/no-dynamic-delete delete update[key] }) } } return { update, commands } } function assignUpdate<T> (document: HydratedDocument<T>, update: UpdateQuery<T>, commands: Record<string, unknown>[]): HydratedDocument<T> { let updated = assign(document, update) _.forEach(commands, (command) => { try { updated = assign(updated, command) } catch { // we catch assign keys that are not implemented } }) return updated } /** * @description Patch patch event emitter */ export const patchEventEmitter = em /** * @description Patch history plugin * @param {Schema} schema * @param {IPluginOptions} opts * @returns {void} */ export const patchHistoryPlugin = function plugin<T> (schema: Schema<T>, opts: IPluginOptions<T>): void { schema.pre('save', async function () { const current = this.toObject(toObjectOptions) as HydratedDocument<T> const model = this.constructor as Model<T> const context: IContext<T> = { op: this.isNew ? 'create' : 'update', modelName: opts.modelName ?? model.modelName, collectionName: opts.collectionName ?? model.collection.collectionName, createdDocs: [current] } if (this.isNew) { await createPatch(opts, context) } else { const original = await model.findById(current._id).lean().exec() if (original) { await updatePatch(opts, context, current, original as HydratedDocument<T>) } } }) schema.post('insertMany', async function (docs) { const context = { op: 'create', modelName: opts.modelName ?? this.modelName, collectionName: opts.collectionName ?? this.collection.collectionName, createdDocs: docs as unknown as HydratedDocument<T>[] } await createPatch(opts, context) }) schema.pre(updateMethods as MongooseQueryMiddleware[], async function (this: IHookContext<T>) { const options = this.getOptions() if (isHookIgnored(options)) return const model = this.model as Model<T> const filter = this.getFilter() const count = await this.model.count(filter).exec() this._context = { op: this.op, modelName: opts.modelName ?? this.model.modelName, collectionName: opts.collectionName ?? this.model.collection.collectionName, isNew: options.upsert && count === 0, ignoreEvent: options.ignoreEvent as boolean, ignorePatchHistory: options.ignorePatchHistory as boolean } const updateQuery = this.getUpdate() const { update, commands } = splitUpdateAndCommands(updateQuery) const cursor = model.find(filter).lean().cursor() await cursor.eachAsync(async (doc: HydratedDocument<T>) => { await updatePatch(opts, this._context, assignUpdate(doc, update, commands), doc) }) }) schema.post(updateMethods as MongooseQueryMiddleware[], async function (this: IHookContext<T>) { const options = this.getOptions() if (isHookIgnored(options)) return if (!this._context.isNew) return const model = this.model as Model<T> const updateQuery = this.getUpdate() const { update, commands } = splitUpdateAndCommands(updateQuery) const
filter = assignUpdate({} as HydratedDocument<T>, update, commands) if (!_.isEmpty(filter)) {
const current = await model.findOne(update).lean().exec() if (current) { this._context.createdDocs = [current] as HydratedDocument<T>[] await createPatch(opts, this._context) } } }) schema.pre(remove, { document: true, query: false }, async function () { const original = this.toObject(toObjectOptions) as HydratedDocument<T> if (opts.preDelete && !_.isEmpty(original)) { await opts.preDelete([original]) } }) schema.post(remove, { document: true, query: false }, async function (this: HydratedDocument<T>) { const original = this.toObject(toObjectOptions) as HydratedDocument<T> const model = this.constructor as Model<T> const context: IContext<T> = { op: 'delete', modelName: opts.modelName ?? model.modelName, collectionName: opts.collectionName ?? model.collection.collectionName, deletedDocs: [original] } await deletePatch(opts, context) }) schema.pre(deleteMethods as MongooseQueryMiddleware[], { document: false, query: true }, async function (this: IHookContext<T>) { const options = this.getOptions() if (isHookIgnored(options)) return const model = this.model as Model<T> const filter = this.getFilter() this._context = { op: this.op, modelName: opts.modelName ?? this.model.modelName, collectionName: opts.collectionName ?? this.model.collection.collectionName, ignoreEvent: options.ignoreEvent as boolean, ignorePatchHistory: options.ignorePatchHistory as boolean } if (['remove', 'deleteMany'].includes(this._context.op) && !options.single) { const docs = await model.find(filter).lean().exec() if (!_.isEmpty(docs)) { this._context.deletedDocs = docs as HydratedDocument<T>[] } } else { const doc = await model.findOne(filter).lean().exec() if (!_.isEmpty(doc)) { this._context.deletedDocs = [doc] as HydratedDocument<T>[] } } if (opts.preDelete && _.isArray(this._context.deletedDocs) && !_.isEmpty(this._context.deletedDocs)) { await opts.preDelete(this._context.deletedDocs) } }) schema.post(deleteMethods as MongooseQueryMiddleware[], { document: false, query: true }, async function (this: IHookContext<T>) { const options = this.getOptions() if (isHookIgnored(options)) return await deletePatch(opts, this._context) }) }
src/plugin.ts
ilovepixelart-ts-patch-mongoose-3306d83
[ { "filename": "src/patch.ts", "retrieved_chunk": " if (_.isEmpty(originalObject) || _.isEmpty(currentObject)) return\n const patch = jsonpatch.compare(originalObject, currentObject, true)\n if (_.isEmpty(patch)) return\n emitEvent(context, opts.eventUpdated, { oldDoc: original, doc: current, patch })\n if (history) {\n let version = 0\n const lastHistory = await History.findOne({ collectionId: original._id as Types.ObjectId }).sort('-version').exec()\n if (lastHistory && lastHistory.version >= 0) {\n version = lastHistory.version + 1\n }", "score": 20.921636924430516 }, { "filename": "src/patch.ts", "retrieved_chunk": " const history = isPatchHistoryEnabled(opts, context)\n const event = opts[eventKey]\n const docs = context[docsKey]\n const key = eventKey === 'eventCreated' ? 'doc' : 'oldDoc'\n if (_.isEmpty(docs) || (!event && !history)) return\n const [user, reason, metadata] = await getData(opts)\n const chunks = _.chunk(docs, 1000)\n for await (const chunk of chunks) {\n const bulk = []\n for (const doc of chunk) {", "score": 16.8941240756655 }, { "filename": "src/patch.ts", "retrieved_chunk": " return { currentObject, originalObject }\n}\nexport async function getUser<T> (opts: IPluginOptions<T>): Promise<User | undefined> {\n if (_.isFunction(opts.getUser)) {\n return await opts.getUser()\n }\n return undefined\n}\nexport async function getReason<T> (opts: IPluginOptions<T>): Promise<string | undefined> {\n if (_.isFunction(opts.getReason)) {", "score": 12.368718657311526 }, { "filename": "src/patch.ts", "retrieved_chunk": " return await opts.getReason()\n }\n return undefined\n}\nexport async function getMetadata<T> (opts: IPluginOptions<T>): Promise<Metadata | undefined> {\n if (_.isFunction(opts.getMetadata)) {\n return await opts.getMetadata()\n }\n return undefined\n}", "score": 11.630221851235364 }, { "filename": "src/patch.ts", "retrieved_chunk": "function isPatchHistoryEnabled<T> (opts: IPluginOptions<T>, context: IContext<T>): boolean {\n return !opts.patchHistoryDisabled && !context.ignorePatchHistory\n}\nexport function getObjects<T> (opts: IPluginOptions<T>, current: HydratedDocument<T>, original: HydratedDocument<T>): { currentObject: Partial<T>, originalObject: Partial<T> } {\n let currentObject = JSON.parse(JSON.stringify(current)) as Partial<T>\n let originalObject = JSON.parse(JSON.stringify(original)) as Partial<T>\n if (opts.omit) {\n currentObject = omit(currentObject, opts.omit)\n originalObject = omit(originalObject, opts.omit)\n }", "score": 11.268567977800798 } ]
typescript
filter = assignUpdate({} as HydratedDocument<T>, update, commands) if (!_.isEmpty(filter)) {
import _ from 'lodash' import { assign } from 'power-assign' import type { HydratedDocument, Model, MongooseQueryMiddleware, QueryOptions, Schema, ToObjectOptions, UpdateQuery, UpdateWithAggregationPipeline } from 'mongoose' import type IPluginOptions from './interfaces/IPluginOptions' import type IContext from './interfaces/IContext' import type IHookContext from './interfaces/IHookContext' import { createPatch, updatePatch, deletePatch } from './patch' import { isMongooseLessThan7 } from './version' import em from './em' const remove = isMongooseLessThan7 ? 'remove' : 'deleteOne' const toObjectOptions: ToObjectOptions = { depopulate: true, virtuals: false } const updateMethods = [ 'update', 'updateOne', 'replaceOne', 'updateMany', 'findOneAndUpdate', 'findOneAndReplace', 'findByIdAndUpdate' ] const deleteMethods = [ 'remove', 'findOneAndDelete', 'findOneAndRemove', 'findByIdAndDelete', 'findByIdAndRemove', 'deleteOne', 'deleteMany' ] function isHookIgnored<T> (options: QueryOptions<T>): boolean { return options.ignoreHook === true || (options.ignoreEvent === true && options.ignorePatchHistory === true) } function splitUpdateAndCommands<T> (updateQuery: UpdateWithAggregationPipeline | UpdateQuery<T> | null): { update: UpdateQuery<T>, commands: Record<string, unknown>[] } { let update: UpdateQuery<T> = {} const commands: Record<string, unknown>[] = [] if (!_.isEmpty(updateQuery) && !_.isArray(updateQuery) && _.isObjectLike(updateQuery)) { update = _.cloneDeep(updateQuery) const keys = _.keys(update).filter((key) => key.startsWith('$')) if (!_.isEmpty(keys)) { _.forEach(keys, (key) => { commands.push({ [key]: update[key] as unknown }) // eslint-disable-next-line @typescript-eslint/no-dynamic-delete delete update[key] }) } } return { update, commands } } function assignUpdate<T> (document: HydratedDocument<T>, update: UpdateQuery<T>, commands: Record<string, unknown>[]): HydratedDocument<T> { let updated = assign(document, update) _.forEach(commands, (command) => { try { updated = assign(updated, command) } catch { // we catch assign keys that are not implemented } }) return updated } /** * @description Patch patch event emitter */ export const patchEventEmitter = em /** * @description Patch history plugin * @param {Schema} schema * @param {IPluginOptions} opts * @returns {void} */ export const patchHistoryPlugin = function plugin<T> (schema: Schema<T>, opts: IPluginOptions<T>): void { schema.pre('save', async function () { const current = this.toObject(toObjectOptions) as HydratedDocument<T> const model = this.constructor as Model<T> const context: IContext<T> = { op: this.isNew ? 'create' : 'update', modelName: opts.modelName ?? model.modelName, collectionName: opts.collectionName ?? model.collection.collectionName, createdDocs: [current] } if (this.isNew) { await createPatch(opts, context) } else { const original = await model.findById(current._id).lean().exec() if (original) { await
updatePatch(opts, context, current, original as HydratedDocument<T>) }
} }) schema.post('insertMany', async function (docs) { const context = { op: 'create', modelName: opts.modelName ?? this.modelName, collectionName: opts.collectionName ?? this.collection.collectionName, createdDocs: docs as unknown as HydratedDocument<T>[] } await createPatch(opts, context) }) schema.pre(updateMethods as MongooseQueryMiddleware[], async function (this: IHookContext<T>) { const options = this.getOptions() if (isHookIgnored(options)) return const model = this.model as Model<T> const filter = this.getFilter() const count = await this.model.count(filter).exec() this._context = { op: this.op, modelName: opts.modelName ?? this.model.modelName, collectionName: opts.collectionName ?? this.model.collection.collectionName, isNew: options.upsert && count === 0, ignoreEvent: options.ignoreEvent as boolean, ignorePatchHistory: options.ignorePatchHistory as boolean } const updateQuery = this.getUpdate() const { update, commands } = splitUpdateAndCommands(updateQuery) const cursor = model.find(filter).lean().cursor() await cursor.eachAsync(async (doc: HydratedDocument<T>) => { await updatePatch(opts, this._context, assignUpdate(doc, update, commands), doc) }) }) schema.post(updateMethods as MongooseQueryMiddleware[], async function (this: IHookContext<T>) { const options = this.getOptions() if (isHookIgnored(options)) return if (!this._context.isNew) return const model = this.model as Model<T> const updateQuery = this.getUpdate() const { update, commands } = splitUpdateAndCommands(updateQuery) const filter = assignUpdate({} as HydratedDocument<T>, update, commands) if (!_.isEmpty(filter)) { const current = await model.findOne(update).lean().exec() if (current) { this._context.createdDocs = [current] as HydratedDocument<T>[] await createPatch(opts, this._context) } } }) schema.pre(remove, { document: true, query: false }, async function () { const original = this.toObject(toObjectOptions) as HydratedDocument<T> if (opts.preDelete && !_.isEmpty(original)) { await opts.preDelete([original]) } }) schema.post(remove, { document: true, query: false }, async function (this: HydratedDocument<T>) { const original = this.toObject(toObjectOptions) as HydratedDocument<T> const model = this.constructor as Model<T> const context: IContext<T> = { op: 'delete', modelName: opts.modelName ?? model.modelName, collectionName: opts.collectionName ?? model.collection.collectionName, deletedDocs: [original] } await deletePatch(opts, context) }) schema.pre(deleteMethods as MongooseQueryMiddleware[], { document: false, query: true }, async function (this: IHookContext<T>) { const options = this.getOptions() if (isHookIgnored(options)) return const model = this.model as Model<T> const filter = this.getFilter() this._context = { op: this.op, modelName: opts.modelName ?? this.model.modelName, collectionName: opts.collectionName ?? this.model.collection.collectionName, ignoreEvent: options.ignoreEvent as boolean, ignorePatchHistory: options.ignorePatchHistory as boolean } if (['remove', 'deleteMany'].includes(this._context.op) && !options.single) { const docs = await model.find(filter).lean().exec() if (!_.isEmpty(docs)) { this._context.deletedDocs = docs as HydratedDocument<T>[] } } else { const doc = await model.findOne(filter).lean().exec() if (!_.isEmpty(doc)) { this._context.deletedDocs = [doc] as HydratedDocument<T>[] } } if (opts.preDelete && _.isArray(this._context.deletedDocs) && !_.isEmpty(this._context.deletedDocs)) { await opts.preDelete(this._context.deletedDocs) } }) schema.post(deleteMethods as MongooseQueryMiddleware[], { document: false, query: true }, async function (this: IHookContext<T>) { const options = this.getOptions() if (isHookIgnored(options)) return await deletePatch(opts, this._context) }) }
src/plugin.ts
ilovepixelart-ts-patch-mongoose-3306d83
[ { "filename": "src/patch.ts", "retrieved_chunk": " await History.bulkWrite(bulk, { ordered: false })\n }\n }\n}\nexport async function createPatch<T> (opts: IPluginOptions<T>, context: IContext<T>): Promise<void> {\n await bulkPatch(opts, context, 'eventCreated', 'createdDocs')\n}\nexport async function updatePatch<T> (opts: IPluginOptions<T>, context: IContext<T>, current: HydratedDocument<T>, original: HydratedDocument<T>): Promise<void> {\n const history = isPatchHistoryEnabled(opts, context)\n const { currentObject, originalObject } = getObjects(opts, current, original)", "score": 29.779290344402934 }, { "filename": "src/patch.ts", "retrieved_chunk": " if (_.isEmpty(originalObject) || _.isEmpty(currentObject)) return\n const patch = jsonpatch.compare(originalObject, currentObject, true)\n if (_.isEmpty(patch)) return\n emitEvent(context, opts.eventUpdated, { oldDoc: original, doc: current, patch })\n if (history) {\n let version = 0\n const lastHistory = await History.findOne({ collectionId: original._id as Types.ObjectId }).sort('-version').exec()\n if (lastHistory && lastHistory.version >= 0) {\n version = lastHistory.version + 1\n }", "score": 23.036500376949235 }, { "filename": "src/patch.ts", "retrieved_chunk": "function isPatchHistoryEnabled<T> (opts: IPluginOptions<T>, context: IContext<T>): boolean {\n return !opts.patchHistoryDisabled && !context.ignorePatchHistory\n}\nexport function getObjects<T> (opts: IPluginOptions<T>, current: HydratedDocument<T>, original: HydratedDocument<T>): { currentObject: Partial<T>, originalObject: Partial<T> } {\n let currentObject = JSON.parse(JSON.stringify(current)) as Partial<T>\n let originalObject = JSON.parse(JSON.stringify(original)) as Partial<T>\n if (opts.omit) {\n currentObject = omit(currentObject, opts.omit)\n originalObject = omit(originalObject, opts.omit)\n }", "score": 19.934676797003647 }, { "filename": "src/patch.ts", "retrieved_chunk": " const [user, reason, metadata] = await getData(opts)\n await History.create({\n op: context.op,\n modelName: context.modelName,\n collectionName: context.collectionName,\n collectionId: original._id as Types.ObjectId,\n patch,\n user,\n reason,\n metadata,", "score": 19.578707311123825 }, { "filename": "src/patch.ts", "retrieved_chunk": " const history = isPatchHistoryEnabled(opts, context)\n const event = opts[eventKey]\n const docs = context[docsKey]\n const key = eventKey === 'eventCreated' ? 'doc' : 'oldDoc'\n if (_.isEmpty(docs) || (!event && !history)) return\n const [user, reason, metadata] = await getData(opts)\n const chunks = _.chunk(docs, 1000)\n for await (const chunk of chunks) {\n const bulk = []\n for (const doc of chunk) {", "score": 11.088524895098285 } ]
typescript
updatePatch(opts, context, current, original as HydratedDocument<T>) }
import _ from 'lodash' import { assign } from 'power-assign' import type { HydratedDocument, Model, MongooseQueryMiddleware, QueryOptions, Schema, ToObjectOptions, UpdateQuery, UpdateWithAggregationPipeline } from 'mongoose' import type IPluginOptions from './interfaces/IPluginOptions' import type IContext from './interfaces/IContext' import type IHookContext from './interfaces/IHookContext' import { createPatch, updatePatch, deletePatch } from './patch' import { isMongooseLessThan7 } from './version' import em from './em' const remove = isMongooseLessThan7 ? 'remove' : 'deleteOne' const toObjectOptions: ToObjectOptions = { depopulate: true, virtuals: false } const updateMethods = [ 'update', 'updateOne', 'replaceOne', 'updateMany', 'findOneAndUpdate', 'findOneAndReplace', 'findByIdAndUpdate' ] const deleteMethods = [ 'remove', 'findOneAndDelete', 'findOneAndRemove', 'findByIdAndDelete', 'findByIdAndRemove', 'deleteOne', 'deleteMany' ] function isHookIgnored<T> (options: QueryOptions<T>): boolean { return options.ignoreHook === true || (options.ignoreEvent === true && options.ignorePatchHistory === true) } function splitUpdateAndCommands<T> (updateQuery: UpdateWithAggregationPipeline | UpdateQuery<T> | null): { update: UpdateQuery<T>, commands: Record<string, unknown>[] } { let update: UpdateQuery<T> = {} const commands: Record<string, unknown>[] = [] if (!_.isEmpty(updateQuery) && !_.isArray(updateQuery) && _.isObjectLike(updateQuery)) { update = _.cloneDeep(updateQuery) const keys = _.keys(update).filter((key) => key.startsWith('$')) if (!_.isEmpty(keys)) { _.forEach(keys, (key) => { commands.push({ [key]: update[key] as unknown }) // eslint-disable-next-line @typescript-eslint/no-dynamic-delete delete update[key] }) } } return { update, commands } } function assignUpdate<T> (document: HydratedDocument<T>, update: UpdateQuery<T>, commands: Record<string, unknown>[]): HydratedDocument<T> { let updated = assign(document, update) _.forEach(commands, (command) => { try { updated = assign(updated, command) } catch { // we catch assign keys that are not implemented } }) return updated } /** * @description Patch patch event emitter */ export const patchEventEmitter = em /** * @description Patch history plugin * @param {Schema} schema * @param {IPluginOptions} opts * @returns {void} */ export const patchHistoryPlugin = function plugin<T> (schema: Schema<T>, opts: IPluginOptions<T>): void { schema.pre('save', async function () { const current = this.toObject(toObjectOptions) as HydratedDocument<T> const model = this.constructor as Model<T>
const context: IContext<T> = {
op: this.isNew ? 'create' : 'update', modelName: opts.modelName ?? model.modelName, collectionName: opts.collectionName ?? model.collection.collectionName, createdDocs: [current] } if (this.isNew) { await createPatch(opts, context) } else { const original = await model.findById(current._id).lean().exec() if (original) { await updatePatch(opts, context, current, original as HydratedDocument<T>) } } }) schema.post('insertMany', async function (docs) { const context = { op: 'create', modelName: opts.modelName ?? this.modelName, collectionName: opts.collectionName ?? this.collection.collectionName, createdDocs: docs as unknown as HydratedDocument<T>[] } await createPatch(opts, context) }) schema.pre(updateMethods as MongooseQueryMiddleware[], async function (this: IHookContext<T>) { const options = this.getOptions() if (isHookIgnored(options)) return const model = this.model as Model<T> const filter = this.getFilter() const count = await this.model.count(filter).exec() this._context = { op: this.op, modelName: opts.modelName ?? this.model.modelName, collectionName: opts.collectionName ?? this.model.collection.collectionName, isNew: options.upsert && count === 0, ignoreEvent: options.ignoreEvent as boolean, ignorePatchHistory: options.ignorePatchHistory as boolean } const updateQuery = this.getUpdate() const { update, commands } = splitUpdateAndCommands(updateQuery) const cursor = model.find(filter).lean().cursor() await cursor.eachAsync(async (doc: HydratedDocument<T>) => { await updatePatch(opts, this._context, assignUpdate(doc, update, commands), doc) }) }) schema.post(updateMethods as MongooseQueryMiddleware[], async function (this: IHookContext<T>) { const options = this.getOptions() if (isHookIgnored(options)) return if (!this._context.isNew) return const model = this.model as Model<T> const updateQuery = this.getUpdate() const { update, commands } = splitUpdateAndCommands(updateQuery) const filter = assignUpdate({} as HydratedDocument<T>, update, commands) if (!_.isEmpty(filter)) { const current = await model.findOne(update).lean().exec() if (current) { this._context.createdDocs = [current] as HydratedDocument<T>[] await createPatch(opts, this._context) } } }) schema.pre(remove, { document: true, query: false }, async function () { const original = this.toObject(toObjectOptions) as HydratedDocument<T> if (opts.preDelete && !_.isEmpty(original)) { await opts.preDelete([original]) } }) schema.post(remove, { document: true, query: false }, async function (this: HydratedDocument<T>) { const original = this.toObject(toObjectOptions) as HydratedDocument<T> const model = this.constructor as Model<T> const context: IContext<T> = { op: 'delete', modelName: opts.modelName ?? model.modelName, collectionName: opts.collectionName ?? model.collection.collectionName, deletedDocs: [original] } await deletePatch(opts, context) }) schema.pre(deleteMethods as MongooseQueryMiddleware[], { document: false, query: true }, async function (this: IHookContext<T>) { const options = this.getOptions() if (isHookIgnored(options)) return const model = this.model as Model<T> const filter = this.getFilter() this._context = { op: this.op, modelName: opts.modelName ?? this.model.modelName, collectionName: opts.collectionName ?? this.model.collection.collectionName, ignoreEvent: options.ignoreEvent as boolean, ignorePatchHistory: options.ignorePatchHistory as boolean } if (['remove', 'deleteMany'].includes(this._context.op) && !options.single) { const docs = await model.find(filter).lean().exec() if (!_.isEmpty(docs)) { this._context.deletedDocs = docs as HydratedDocument<T>[] } } else { const doc = await model.findOne(filter).lean().exec() if (!_.isEmpty(doc)) { this._context.deletedDocs = [doc] as HydratedDocument<T>[] } } if (opts.preDelete && _.isArray(this._context.deletedDocs) && !_.isEmpty(this._context.deletedDocs)) { await opts.preDelete(this._context.deletedDocs) } }) schema.post(deleteMethods as MongooseQueryMiddleware[], { document: false, query: true }, async function (this: IHookContext<T>) { const options = this.getOptions() if (isHookIgnored(options)) return await deletePatch(opts, this._context) }) }
src/plugin.ts
ilovepixelart-ts-patch-mongoose-3306d83
[ { "filename": "src/patch.ts", "retrieved_chunk": " await History.bulkWrite(bulk, { ordered: false })\n }\n }\n}\nexport async function createPatch<T> (opts: IPluginOptions<T>, context: IContext<T>): Promise<void> {\n await bulkPatch(opts, context, 'eventCreated', 'createdDocs')\n}\nexport async function updatePatch<T> (opts: IPluginOptions<T>, context: IContext<T>, current: HydratedDocument<T>, original: HydratedDocument<T>): Promise<void> {\n const history = isPatchHistoryEnabled(opts, context)\n const { currentObject, originalObject } = getObjects(opts, current, original)", "score": 24.186984963733078 }, { "filename": "src/patch.ts", "retrieved_chunk": " version\n })\n }\n}\nexport async function deletePatch<T> (opts: IPluginOptions<T>, context: IContext<T>): Promise<void> {\n await bulkPatch(opts, context, 'eventDeleted', 'deletedDocs')\n}", "score": 15.952309197376673 }, { "filename": "src/patch.ts", "retrieved_chunk": "function isPatchHistoryEnabled<T> (opts: IPluginOptions<T>, context: IContext<T>): boolean {\n return !opts.patchHistoryDisabled && !context.ignorePatchHistory\n}\nexport function getObjects<T> (opts: IPluginOptions<T>, current: HydratedDocument<T>, original: HydratedDocument<T>): { currentObject: Partial<T>, originalObject: Partial<T> } {\n let currentObject = JSON.parse(JSON.stringify(current)) as Partial<T>\n let originalObject = JSON.parse(JSON.stringify(original)) as Partial<T>\n if (opts.omit) {\n currentObject = omit(currentObject, opts.omit)\n originalObject = omit(originalObject, opts.omit)\n }", "score": 15.452780018793476 }, { "filename": "src/patch.ts", "retrieved_chunk": " getValue(metadata)\n ]\n })\n}\nexport function emitEvent<T> (context: IContext<T>, event: string | undefined, data: IEvent<T>): void {\n if (event && !context.ignoreEvent) {\n em.emit(event, data)\n }\n}\nexport async function bulkPatch<T> (opts: IPluginOptions<T>, context: IContext<T>, eventKey: 'eventCreated' | 'eventDeleted', docsKey: 'createdDocs' | 'deletedDocs'): Promise<void> {", "score": 14.464022093650284 }, { "filename": "src/models/History.ts", "retrieved_chunk": "import { Schema, model } from 'mongoose'\nimport type IHistory from '../interfaces/IHistory'\nconst HistorySchema = new Schema<IHistory>({\n op: {\n type: String,\n required: true\n },\n modelName: {\n type: String,\n required: true", "score": 13.811564714000522 } ]
typescript
const context: IContext<T> = {
import { produce } from "immer"; import { get, isEqual, set } from "lodash-es"; import { nanoid } from "nanoid"; import { useEffect, useState } from "react"; import { StoreApi } from "zustand"; import { shallow } from "zustand/shallow"; import { useLeitenRequests } from "../hooks/useLeitenRequest"; import { DotNestedKeys, DotNestedValue } from "../interfaces/dotNestedKeys"; import { ILeitenLoading, ILoadingStatus, initialLeitenLoading, } from "../interfaces/IContentLoading"; /* eslint-disable @typescript-eslint/no-explicit-any */ export type UseRequestType<Payload, Result> = < U = ILeitenLoading<Payload, Result> >( selector?: (state: ILeitenLoading<Payload, Result>) => U, equals?: (a: U, b: U) => boolean ) => U; export interface ILeitenRequest<Payload, Result> extends UseRequestType<Payload, Result> { abort: () => void; clear: () => void; action: ( params: Payload, extraParams?: { status?: ILoadingStatus; requestId?: string } ) => void; set: (value: Partial<Result> | void, rewrite?: boolean) => void; key: string; get: () => ILeitenLoading<Payload, Result>; } export interface ILeitenRequestCallback<Payload, Result> { previousResult: Result; result: Result; payload: Payload; requestId: string; error?: string; } export interface ILeitenRequestOptions<Payload, Result> { fulfilled?: ( options: Omit<ILeitenRequestCallback<Payload, Result>, "error"> ) => void; rejected?: ( options: Omit<ILeitenRequestCallback<Payload, Result>, "result"> ) => void; abort?: ( options: Omit<ILeitenRequestCallback<Payload, Result>, "error" | "result"> ) => void; resolved?: ( options: Omit<ILeitenRequestCallback<Payload, Result>, "result" | "error"> ) => void; action?: ( options: Omit<ILeitenRequestCallback<Payload, Result>, "error" | "result"> ) => void; initialStatus?: ILoadingStatus; optimisticUpdate?: (params: Payload) => Result; } export const leitenRequest = < Store extends object, P extends DotNestedKeys<Store>, Payload, Result extends DotNestedValue<Store, P> | null | void >( store: StoreApi<Store>, path: P extends string ? Result extends void ? P : DotNestedValue<Store, P> extends Result | null ? P : never : never, payloadCreator: ( params: Payload, extraArgument?: IExtraArgument ) => Promise<Result>, options?: ILeitenRequestOptions<Payload, Result> ): ILeitenRequest<Payload, Result> => { const key = nanoid(12); const initialState = initialLeitenLoading<Payload, Result>( options?.initialStatus ); const initialContent = get(store.getState(), path, null) as Result; const setState = (state: ILeitenLoading<Payload, Result>) => { useLeitenRequests.setState({ [key]: state }); }; setState(initialState); //init request const setContent = (content: Result) => { const nextState = produce(store.getState(), (draft) => { set(draft, path, content); }); store.setState(nextState); }; const getState = (): ILeitenLoading<Payload, Result> => { return useLeitenRequests.getState()[key] || initialState; }; const getContent = (): Result => { const result = get(store.getState(), path, "_empty") as Result | "_empty"; if (result !== "_empty") { return result || initialContent; } else { return initialContent; } }; const _set = (value: Partial<Result> | void, rewrite = false) => { if (typeof value === "object") { const state = getContent(); const objectContent = rewrite ? ({ ...value } as Result)
: ({ ...state, ...value } as Result);
const content = typeof value === "object" ? objectContent : value; setContent(content); } else { value !== undefined && value !== null && setContent(value); } }; let previousResult: Result = getContent(); const reactions = { action: (payload: Payload, status?: ILoadingStatus, requestId?: string) => { setState({ status: status ?? "loading", payload, error: undefined, requestId: requestId, }); options?.action?.({ previousResult, requestId: requestId || "", payload, }); previousResult = getContent(); if (options?.optimisticUpdate) { setContent(options.optimisticUpdate(payload)); } }, fulfilled: (result: Result, payload: Payload, requestId: string) => { const state = getState(); if (requestId === state.requestId) { setState({ ...state, status: "loaded" }); if ( result !== undefined && (!options?.optimisticUpdate || !isEqual(previousResult, result)) ) { setContent(result); } options?.fulfilled?.({ previousResult, requestId, payload, result }); } }, rejected: (payload: Payload, error: string, requestId?: string) => { const state = getState(); setState({ ...state, status: "error", error }); options?.rejected?.({ previousResult, requestId: requestId || "", payload, error, }); if (options?.optimisticUpdate) { setContent(previousResult); } }, abort: (payload: Payload, requestId: string) => { setState(initialState); options?.abort?.({ previousResult, requestId, payload }); if (options?.optimisticUpdate) { setContent(previousResult); } }, resolved: (payload: Payload, requestId: string) => { options?.resolved?.({ previousResult, requestId, payload }); }, }; const { action, abort } = createAsyncActions(payloadCreator, reactions); const _abort = () => { abort(); }; const clear = () => { setState(initialState); setContent(initialContent); }; const usages: Record<string, boolean> = {}; const useRequest: UseRequestType<Payload, Result> = (selector, equals) => { const [id] = useState(() => nanoid()); useEffect(() => { usages[id] = true; return () => { usages[id] = false; }; }, []); return useLeitenRequests( (state) => (selector || nonTypedReturn)(state[key] || initialState), shallow || equals ); }; resettableStoreSubscription(store, () => setState(initialState)); const _get = () => { return useLeitenRequests.getState()[key]; }; return Object.assign(useRequest, { abort: _abort, action, clear, set: _set, key, get: _get, _usages: usages, }); }; // eslint-disable-next-line @typescript-eslint/no-explicit-any const nonTypedReturn = (value: any) => value; export const resettableStoreSubscription = ( store: StoreApi<object>, callback: () => void ) => { setTimeout(() => { const resettable = (store.getState() as any)["_resettableLifeCycle"] !== undefined; if (resettable) { store.subscribe((next, prev) => { if ( (next as any)["_resettableLifeCycle"] === false && (prev as any)["_resettableLifeCycle"] === true ) callback(); }); } }, 0); }; function createAsyncActions<Payload, Result>( payloadCreator: ( params: Payload, extraArgument?: IExtraArgument ) => Promise<Result>, extra?: IReaction<Payload, Result> ) { let controller = new AbortController(); let signal = controller.signal; const abort = () => { controller.abort(); controller = new AbortController(); signal = controller.signal; }; const action = ( params: Payload, options?: { status?: ILoadingStatus; requestId?: string } ) => { const requestId = options?.requestId || nanoid(); extra?.action?.(params, options?.status, requestId); payloadCreator(params, { signal }) .then((result) => { extra?.fulfilled?.(result, params, requestId); }) .catch((error) => { if (error.message === "The user aborted a request.") { extra?.abort?.(params, requestId); } else { extra?.rejected?.(params, error, requestId); } }) .finally(() => { extra?.resolved?.(params, requestId); }); }; return { action, abort }; } interface IReaction<Payload, Result> { fulfilled?: (result: Result, params: Payload, requestId: string) => void; rejected?: (params: Payload, error: string, requestId?: string) => void; abort?: (params: Payload, requestId: string) => void; resolved?: (params: Payload, requestId: string) => void; action?: ( params: Payload, status?: ILoadingStatus, requestId?: string ) => void; } export type IExtraArgument = { signal: AbortSignal; // requestId: string };
src/helpers/leitenRequest.ts
Hecmatyar-leiten-zustand-0248dac
[ { "filename": "src/helpers/leitenGroupRequest.ts", "retrieved_chunk": " } else {\n return useGroupRequest(first as any, second as any) as any;\n }\n }\n resettableStoreSubscription(store, () => clear());\n return Object.assign(hook, { clear, action, requests, call: action });\n};\nconst nonTypedReturn = (value: any) => value;\nconst checkInitial = <Result>(\n value: Result | ((key: string) => Result)", "score": 28.410680295981056 }, { "filename": "src/helpers/leitenGroupRequest.ts", "retrieved_chunk": " key: value.key,\n }));\n const requestsStore: typeof state = keys.reduce((acc, { id, key }) => {\n return Object.assign(acc, { [id]: state[key] });\n }, {} as typeof state);\n return (selector || nonTypedReturn)(requestsStore);\n },\n shallow || equals\n );\n };", "score": 22.23818763158618 }, { "filename": "src/helpers/leitenGroupRequest.ts", "retrieved_chunk": "): value is (key: string) => Result => typeof value === \"function\";", "score": 21.872318303517172 }, { "filename": "src/helpers/leitenGroupRequest.ts", "retrieved_chunk": " }, shallow || equals);\n };\n const useGroupRequest: UseGroupRequestType<Payload, Result> = (\n selector,\n equals\n ) => {\n return useLeitenRequests(\n (state: Record<string, LeitenState<Payload, Result>>) => {\n const keys = Object.entries(requests).map(([id, value]) => ({\n id,", "score": 21.44992443277428 }, { "filename": "src/helpers/leitenRecord.ts", "retrieved_chunk": " const initialValue = get(store.getState(), path, \"_empty\") as VALUE;\n if (initialValue === \"_empty\" || typeof initialValue !== \"object\") {\n throw new Error(\n \"[leitenRecord] The defined path does not match the required structure\"\n );\n }\n const getState = (): VALUE => {\n const value = get(store.getState(), path, \"_empty\") as VALUE | \"_empty\";\n return value !== \"_empty\" ? value : initialValue;\n };", "score": 20.842275982888474 } ]
typescript
: ({ ...state, ...value } as Result);
import { cloneDeep, get, isEqual } from "lodash-es"; import { StoreApi, UseBoundStore } from "zustand"; import { DotNestedKeys, DotNestedValue } from "../interfaces/dotNestedKeys"; import { ILeitenPrimitive, leitenPrimitive } from "./leitenPrimitive"; import { ILeitenRecord, ILeitenRecordEffects, leitenRecord, } from "./leitenRecord"; import { IExtraArgument, ILeitenRequestOptions, leitenRequest, resettableStoreSubscription, } from "./leitenRequest"; /* eslint-disable @typescript-eslint/no-explicit-any */ type RecordFilter<T> = { (initialObject?: T): IObjectDifferent[]; } & ILeitenRecord<T>; type PrimitiveFilter<Y> = { (initialObject?: Y): IObjectDifferent[]; } & ILeitenPrimitive<Y>; export const leitenFilterRequest = < Store extends object, Path extends DotNestedKeys<Store>, Result extends DotNestedValue<Store, Path> | null | void >( store: UseBoundStore<StoreApi<Store>>, path: Path extends string ? Result extends void ? Path : DotNestedValue<Store, Path> extends Result | null ? Path : never : never, request: (params: void, extraArgument?: IExtraArgument) => Promise<Result>, options?: ILeitenRequestOptions<void, Result> ) => { const leiten = leitenRequest(store, path, request, { ...options, action: (args) => { updatePrevFilters(); return options?.action?.(args); }, }); const filters: Record<string, ILeitenRecord<any> | ILeitenPrimitive<any>> = {}; let prevFilters: Record<string, any> = {}; const initialFilters: Record<string, any> = {}; const createFilter = <Path extends DotNestedKeys<Store>>( path: Path extends string ? Path : never, options?: ILeitenRecordEffects<DotNestedValue<Store, Path>, Store> ): DotNestedValue<Store, Path> extends object ? RecordFilter<DotNestedValue<Store, Path>> : PrimitiveFilter<DotNestedValue<Store, Path>> => { type Response = DotNestedValue<Store, Path> extends object ? RecordFilter<DotNestedValue<Store, Path>> : PrimitiveFilter<DotNestedValue<Store, Path>>; const initial = get(store.getState(), path, undefined); function hook( initialObject?: DotNestedValue<Store, Path> ): IObjectDifferent[] { return store((state) => getObjectDifference( get(state, path, initialObject || initial), initialObject || initial ) ); } const controller = typeof initial !== "object" ? leitenPrimitive : leitenRecord; const record = controller(store, path, { sideEffect:
(side) => {
options?.sideEffect?.(side); action(path).then(); }, patchEffect: options?.patchEffect, }); prevFilters[path] = record.get(); filters[path] = record; initialFilters[path] = record.get(); return Object.assign(hook, record) as Response; }; const listen = < ExternalStore extends object, ExternalPath extends DotNestedKeys<ExternalStore> >( store: UseBoundStore<StoreApi<ExternalStore>>, path: ExternalPath extends string ? ExternalPath : never, options?: { sideEffect?: (value: { prev: DotNestedValue<ExternalStore, ExternalPath>; next: DotNestedValue<ExternalStore, ExternalPath>; }) => void; } ) => { let prevValue = get(store.getState(), path); const haveSubscription = () => !!Object.values((leiten as any)._usages || {}).filter((item) => item) .length; return store.subscribe((state) => { const value = get(state, path); if (haveSubscription() && !isEqual(prevValue, value)) { prevValue = value; options?.sideEffect?.({ prev: prevValue, next: value }); pureAction().then(); } }); }; const pureAction = async () => { updatePrevFilters(); leiten.action(); }; const action = async (path: string) => { const filter = get(store.getState(), path); if (JSON.stringify(filter) !== JSON.stringify(prevFilters[path])) { leiten.action(); } }; const updatePrevFilters = () => { prevFilters = Object.keys(filters).reduce( (acc, item) => ({ ...acc, [item]: get(store.getState(), item), }), {} ); }; resettableStoreSubscription(store, () => { prevFilters = cloneDeep(initialFilters); }); const clearAll = () => { leiten.clear(); Object.keys(filters).forEach((key) => filters[key].clear()); leiten.action(); }; return Object.assign(leiten, { createFilter, clearAll, listen }); }; export interface IObjectDifferent<S = any> { field: string; prev: S; next: S; } export const getObjectDifference = <S>( next: any, prev: any ): IObjectDifferent<S>[] => { if (typeof next !== "object") { if (!isEqual(next, prev)) { return [{ field: "_", prev, next }]; } else { return []; } } else { return Object.keys(next).reduce((result, field) => { if (!isEqual(next[field], prev[field])) { result.push({ field, prev: prev[field], next: next[field] }); } return result; }, [] as IObjectDifferent<S>[]); } };
src/helpers/leitenFilterRequest.ts
Hecmatyar-leiten-zustand-0248dac
[ { "filename": "src/helpers/leitenGroupRequest.ts", "retrieved_chunk": " }\n };\n } else {\n pathWithKey = (path + `.${key}`) as DotNestedKeys<Store>;\n if (options?.initialContent) {\n const initial = checkInitial(options.initialContent)\n ? options.initialContent(key)\n : options.initialContent;\n const nextState = produce(store.getState(), (draft) => {\n set(draft, pathWithKey, initial);", "score": 26.10883806033935 }, { "filename": "src/helpers/leitenRequest.ts", "retrieved_chunk": " params: Payload,\n extraArgument?: IExtraArgument\n ) => Promise<Result>,\n extra?: IReaction<Payload, Result>\n) {\n let controller = new AbortController();\n let signal = controller.signal;\n const abort = () => {\n controller.abort();\n controller = new AbortController();", "score": 17.876161934571705 }, { "filename": "src/helpers/leitenGroupFilterRequest.ts", "retrieved_chunk": " const prev: object = (getState(key) || options.initialValue) as any;\n setState(key, { ...prev, ...value } as VALUE);\n };\n const record = { clear, patch, set: setState, get: getState };\n filters[path] = record;\n return Object.assign(hook, record);\n };\n const action = async (key: string, path: string, initialValue: any) => {\n const nextFilters = get(store.getState(), `${path}.${key}`);\n if (", "score": 17.768952651985753 }, { "filename": "src/helpers/leitenRecord.ts", "retrieved_chunk": " const initialValue = get(store.getState(), path, \"_empty\") as VALUE;\n if (initialValue === \"_empty\" || typeof initialValue !== \"object\") {\n throw new Error(\n \"[leitenRecord] The defined path does not match the required structure\"\n );\n }\n const getState = (): VALUE => {\n const value = get(store.getState(), path, \"_empty\") as VALUE | \"_empty\";\n return value !== \"_empty\" ? value : initialValue;\n };", "score": 16.255507417569056 }, { "filename": "src/helpers/leitenPrimitive.ts", "retrieved_chunk": " get: () => VALUE;\n clear: () => void;\n};\nexport const leitenPrimitive = <\n Store extends object,\n P extends DotNestedKeys<Store>\n>(\n store: StoreApi<Store>,\n path: P extends string ? P : never,\n effects?: ILeitenPrimitiveEffects<DotNestedValue<Store, P>, Store>", "score": 12.417487357801626 } ]
typescript
(side) => {
import JSZip from "jszip"; import { js2Lua } from "./js-2-lua"; import { options } from "./files/options"; import { mapResource } from "./files/mapResource"; import { warehouses } from "./files/warehouses"; import { mission, MissionProps, planeGroup } from "./files/mission"; import { dictionary } from "./files/dictionary"; import { Dictionary } from "./dictionary"; export class DcsMission { public readonly dictionary: Dictionary; constructor(public readonly props: Omit<MissionProps, "maxDictId">) { this.dictionary = new Dictionary(); } getFiles(): Array<[string, string]> { const descriptionText = this.dictionary.addEntry( "descriptionText", this.props.descriptionText, ); const descriptionBlueTask = this.dictionary.addEntry( "descriptionBlueTask", this.props.descriptionBlueTask, ); const descriptionRedTask = this.dictionary.addEntry( "descriptionRedTask", this.props.descriptionRedTask, ); const descriptionNeutralsTask = this.dictionary.addEntry( "descriptionNeutralsTask", this.props.descriptionNeutralsTask, ); const sortie = this.dictionary.addEntry("sortie", this.props.sortie); const _mission = mission({ ...this.props, descriptionText, descriptionBlueTask, descriptionRedTask, descriptionNeutralsTask, sortie, maxDictId: this.dictionary.maxDictId, }); return [ ["options", `options = ${js2Lua(options({}))}`], ["l10n/DEFAULT/mapResource", `mapResource = ${js2Lua(mapResource({}))}`], ["warehouses", `warehouses = ${js2Lua(warehouses({}))}`], ["theatre", _mission.theatre], ["mission", `mission = ${js2Lua(_mission)}`], [ "l10n/DEFAULT/dictionary", `
dictionary = ${js2Lua(dictionary(this.dictionary.entries))}`, ], ];
} build() { const zip = new JSZip(); this.getFiles().forEach(([path, data]) => zip.file(path, data)); return zip.generateAsync({ compression: "DEFLATE", type: "arraybuffer" }); } }
src/dcs-mission.ts
JonathanTurnock-dcs-mission-maker-9989db3
[ { "filename": "src/dcs-mission.test.ts", "retrieved_chunk": "import { DcsMission } from \"./dcs-mission\";\nimport { plane, planeGroup } from \"./files/mission\";\ndescribe(\"given a simple empty mission\", () => {\n it(\"should build a mission file\", () => {\n const dcsMission = new DcsMission({\n theatre: \"Caucasus\",\n descriptionNeutralsTask: \"\",\n descriptionRedTask: \"\",\n descriptionBlueTask: \"\",\n sortie: \"\",", "score": 20.8843680785655 }, { "filename": "src/js-2-lua/js-2-lua.test.ts", "retrieved_chunk": " js2Lua({ _1: 1, _2: 1, _3: 1, name: \"Enfield11\", _ignoreme: \"hello\" }),\n ).toMatchSnapshot();\n });\n});", "score": 19.231015155683473 }, { "filename": "src/dcs-mission.test.ts", "retrieved_chunk": " descriptionText: \"\",\n });\n expect(dcsMission.getFiles()).toMatchSnapshot();\n });\n});\ndescribe(\"given a simple mission with 1 plane group\", () => {\n it(\"should build a mission file\", () => {\n const dcsMission = new DcsMission({\n theatre: \"Caucasus\",\n descriptionNeutralsTask: \"\",", "score": 18.97235721087455 }, { "filename": "src/files/dictionary.ts", "retrieved_chunk": "import { z } from \"zod\";\nexport const dictionarySchema = z.record(z.string(), z.string());\nexport type DictionaryProps = z.input<typeof dictionarySchema>;\nexport const dictionary = (props: DictionaryProps) =>\n dictionarySchema.parse(props);", "score": 18.664278504603175 }, { "filename": "src/files/warehouses.ts", "retrieved_chunk": " weapons: z.object({}),\n OperatingLevel_Fuel: operatingLevelSchema,\n jet_fuel: airportFuelSchema,\n});\nconst warehousesSchema = z.object({\n airports: z.record(z.number(), airportSchema).default({}),\n warehouses: z.record(z.number(), z.object({})).default({}),\n});\nexport type WarehousesProps = z.input<typeof warehousesSchema>;\nexport const warehouses = (props: WarehousesProps) =>", "score": 18.342302004639393 } ]
typescript
dictionary = ${js2Lua(dictionary(this.dictionary.entries))}`, ], ];
import { cloneDeep, get, isEqual } from "lodash-es"; import { StoreApi, UseBoundStore } from "zustand"; import { DotNestedKeys, DotNestedValue } from "../interfaces/dotNestedKeys"; import { ILeitenPrimitive, leitenPrimitive } from "./leitenPrimitive"; import { ILeitenRecord, ILeitenRecordEffects, leitenRecord, } from "./leitenRecord"; import { IExtraArgument, ILeitenRequestOptions, leitenRequest, resettableStoreSubscription, } from "./leitenRequest"; /* eslint-disable @typescript-eslint/no-explicit-any */ type RecordFilter<T> = { (initialObject?: T): IObjectDifferent[]; } & ILeitenRecord<T>; type PrimitiveFilter<Y> = { (initialObject?: Y): IObjectDifferent[]; } & ILeitenPrimitive<Y>; export const leitenFilterRequest = < Store extends object, Path extends DotNestedKeys<Store>, Result extends DotNestedValue<Store, Path> | null | void >( store: UseBoundStore<StoreApi<Store>>, path: Path extends string ? Result extends void ? Path : DotNestedValue<Store, Path> extends Result | null ? Path : never : never, request: (params: void, extraArgument?: IExtraArgument) => Promise<Result>, options?: ILeitenRequestOptions<void, Result> ) => { const leiten = leitenRequest(store, path, request, { ...options, action: (args) => { updatePrevFilters(); return options?.action?.(args); }, }); const filters: Record<string, ILeitenRecord<any> | ILeitenPrimitive<any>> = {}; let prevFilters: Record<string, any> = {}; const initialFilters: Record<string, any> = {}; const createFilter = <Path extends DotNestedKeys<Store>>( path: Path extends string ? Path : never, options?: ILeitenRecordEffects<DotNestedValue<Store, Path>, Store> ): DotNestedValue<Store, Path> extends object ? RecordFilter<DotNestedValue<Store, Path>> : PrimitiveFilter<DotNestedValue<Store, Path>> => { type Response = DotNestedValue<Store, Path> extends object ? RecordFilter<DotNestedValue<Store, Path>> : PrimitiveFilter<DotNestedValue<Store, Path>>; const initial = get(store.getState(), path, undefined); function hook( initialObject?: DotNestedValue<Store, Path> ): IObjectDifferent[] { return store((state) => getObjectDifference( get(state, path, initialObject || initial), initialObject || initial ) ); } const controller =
typeof initial !== "object" ? leitenPrimitive : leitenRecord;
const record = controller(store, path, { sideEffect: (side) => { options?.sideEffect?.(side); action(path).then(); }, patchEffect: options?.patchEffect, }); prevFilters[path] = record.get(); filters[path] = record; initialFilters[path] = record.get(); return Object.assign(hook, record) as Response; }; const listen = < ExternalStore extends object, ExternalPath extends DotNestedKeys<ExternalStore> >( store: UseBoundStore<StoreApi<ExternalStore>>, path: ExternalPath extends string ? ExternalPath : never, options?: { sideEffect?: (value: { prev: DotNestedValue<ExternalStore, ExternalPath>; next: DotNestedValue<ExternalStore, ExternalPath>; }) => void; } ) => { let prevValue = get(store.getState(), path); const haveSubscription = () => !!Object.values((leiten as any)._usages || {}).filter((item) => item) .length; return store.subscribe((state) => { const value = get(state, path); if (haveSubscription() && !isEqual(prevValue, value)) { prevValue = value; options?.sideEffect?.({ prev: prevValue, next: value }); pureAction().then(); } }); }; const pureAction = async () => { updatePrevFilters(); leiten.action(); }; const action = async (path: string) => { const filter = get(store.getState(), path); if (JSON.stringify(filter) !== JSON.stringify(prevFilters[path])) { leiten.action(); } }; const updatePrevFilters = () => { prevFilters = Object.keys(filters).reduce( (acc, item) => ({ ...acc, [item]: get(store.getState(), item), }), {} ); }; resettableStoreSubscription(store, () => { prevFilters = cloneDeep(initialFilters); }); const clearAll = () => { leiten.clear(); Object.keys(filters).forEach((key) => filters[key].clear()); leiten.action(); }; return Object.assign(leiten, { createFilter, clearAll, listen }); }; export interface IObjectDifferent<S = any> { field: string; prev: S; next: S; } export const getObjectDifference = <S>( next: any, prev: any ): IObjectDifferent<S>[] => { if (typeof next !== "object") { if (!isEqual(next, prev)) { return [{ field: "_", prev, next }]; } else { return []; } } else { return Object.keys(next).reduce((result, field) => { if (!isEqual(next[field], prev[field])) { result.push({ field, prev: prev[field], next: next[field] }); } return result; }, [] as IObjectDifferent<S>[]); } };
src/helpers/leitenFilterRequest.ts
Hecmatyar-leiten-zustand-0248dac
[ { "filename": "src/helpers/leitenGroupRequest.ts", "retrieved_chunk": " }\n };\n } else {\n pathWithKey = (path + `.${key}`) as DotNestedKeys<Store>;\n if (options?.initialContent) {\n const initial = checkInitial(options.initialContent)\n ? options.initialContent(key)\n : options.initialContent;\n const nextState = produce(store.getState(), (draft) => {\n set(draft, pathWithKey, initial);", "score": 23.505904139135023 }, { "filename": "src/helpers/leitenGroupFilterRequest.ts", "retrieved_chunk": " ) => {\n prevFilters[path] = {};\n type VALUE = ValueOf<DotNestedValue<Store, Path>>;\n function hook(\n key: string,\n referenceObject?: VALUE\n ): IObjectDifferent<VALUE>[] {\n return store((state) =>\n getObjectDifference(get(state, `${path}.${key}`), referenceObject)\n );", "score": 19.18410598157324 }, { "filename": "src/helpers/leitenRecord.ts", "retrieved_chunk": " const initialValue = get(store.getState(), path, \"_empty\") as VALUE;\n if (initialValue === \"_empty\" || typeof initialValue !== \"object\") {\n throw new Error(\n \"[leitenRecord] The defined path does not match the required structure\"\n );\n }\n const getState = (): VALUE => {\n const value = get(store.getState(), path, \"_empty\") as VALUE | \"_empty\";\n return value !== \"_empty\" ? value : initialValue;\n };", "score": 13.612339159916637 }, { "filename": "src/helpers/leitenRequest.ts", "retrieved_chunk": " } else {\n return initialContent;\n }\n };\n const _set = (value: Partial<Result> | void, rewrite = false) => {\n if (typeof value === \"object\") {\n const state = getContent();\n const objectContent = rewrite\n ? ({ ...value } as Result)\n : ({ ...state, ...value } as Result);", "score": 12.308682609409646 }, { "filename": "src/helpers/leitenGroupRequest.ts", "retrieved_chunk": " key: value.key,\n }));\n const requestsStore: typeof state = keys.reduce((acc, { id, key }) => {\n return Object.assign(acc, { [id]: state[key] });\n }, {} as typeof state);\n return (selector || nonTypedReturn)(requestsStore);\n },\n shallow || equals\n );\n };", "score": 11.93093762890328 } ]
typescript
typeof initial !== "object" ? leitenPrimitive : leitenRecord;
import { cloneDeep, get, isEqual } from "lodash-es"; import { StoreApi, UseBoundStore } from "zustand"; import { DotNestedKeys, DotNestedValue } from "../interfaces/dotNestedKeys"; import { ILeitenPrimitive, leitenPrimitive } from "./leitenPrimitive"; import { ILeitenRecord, ILeitenRecordEffects, leitenRecord, } from "./leitenRecord"; import { IExtraArgument, ILeitenRequestOptions, leitenRequest, resettableStoreSubscription, } from "./leitenRequest"; /* eslint-disable @typescript-eslint/no-explicit-any */ type RecordFilter<T> = { (initialObject?: T): IObjectDifferent[]; } & ILeitenRecord<T>; type PrimitiveFilter<Y> = { (initialObject?: Y): IObjectDifferent[]; } & ILeitenPrimitive<Y>; export const leitenFilterRequest = < Store extends object, Path extends DotNestedKeys<Store>, Result extends DotNestedValue<Store, Path> | null | void >( store: UseBoundStore<StoreApi<Store>>, path: Path extends string ? Result extends void ? Path : DotNestedValue<Store, Path> extends Result | null ? Path : never : never, request: (params: void, extraArgument?: IExtraArgument) => Promise<Result>, options?: ILeitenRequestOptions<void, Result> ) => { const leiten = leitenRequest(store, path, request, { ...options, action: (args) => { updatePrevFilters(); return options?.action?.(args); }, }); const filters: Record<string, ILeitenRecord<any> | ILeitenPrimitive<any>> = {}; let prevFilters: Record<string, any> = {}; const initialFilters: Record<string, any> = {}; const createFilter = <Path extends DotNestedKeys<Store>>( path: Path extends string ? Path : never, options?: ILeitenRecordEffects<DotNestedValue<Store, Path>, Store> ): DotNestedValue<Store, Path> extends object ? RecordFilter<DotNestedValue<Store, Path>> : PrimitiveFilter<DotNestedValue<Store, Path>> => { type Response = DotNestedValue<Store, Path> extends object ? RecordFilter<DotNestedValue<Store, Path>> : PrimitiveFilter<DotNestedValue<Store, Path>>; const initial = get(store.getState(), path, undefined); function hook( initialObject?: DotNestedValue<Store, Path> ): IObjectDifferent[] { return store((state) => getObjectDifference( get(state, path, initialObject || initial), initialObject || initial ) ); } const controller = typeof initial !== "object"
? leitenPrimitive : leitenRecord;
const record = controller(store, path, { sideEffect: (side) => { options?.sideEffect?.(side); action(path).then(); }, patchEffect: options?.patchEffect, }); prevFilters[path] = record.get(); filters[path] = record; initialFilters[path] = record.get(); return Object.assign(hook, record) as Response; }; const listen = < ExternalStore extends object, ExternalPath extends DotNestedKeys<ExternalStore> >( store: UseBoundStore<StoreApi<ExternalStore>>, path: ExternalPath extends string ? ExternalPath : never, options?: { sideEffect?: (value: { prev: DotNestedValue<ExternalStore, ExternalPath>; next: DotNestedValue<ExternalStore, ExternalPath>; }) => void; } ) => { let prevValue = get(store.getState(), path); const haveSubscription = () => !!Object.values((leiten as any)._usages || {}).filter((item) => item) .length; return store.subscribe((state) => { const value = get(state, path); if (haveSubscription() && !isEqual(prevValue, value)) { prevValue = value; options?.sideEffect?.({ prev: prevValue, next: value }); pureAction().then(); } }); }; const pureAction = async () => { updatePrevFilters(); leiten.action(); }; const action = async (path: string) => { const filter = get(store.getState(), path); if (JSON.stringify(filter) !== JSON.stringify(prevFilters[path])) { leiten.action(); } }; const updatePrevFilters = () => { prevFilters = Object.keys(filters).reduce( (acc, item) => ({ ...acc, [item]: get(store.getState(), item), }), {} ); }; resettableStoreSubscription(store, () => { prevFilters = cloneDeep(initialFilters); }); const clearAll = () => { leiten.clear(); Object.keys(filters).forEach((key) => filters[key].clear()); leiten.action(); }; return Object.assign(leiten, { createFilter, clearAll, listen }); }; export interface IObjectDifferent<S = any> { field: string; prev: S; next: S; } export const getObjectDifference = <S>( next: any, prev: any ): IObjectDifferent<S>[] => { if (typeof next !== "object") { if (!isEqual(next, prev)) { return [{ field: "_", prev, next }]; } else { return []; } } else { return Object.keys(next).reduce((result, field) => { if (!isEqual(next[field], prev[field])) { result.push({ field, prev: prev[field], next: next[field] }); } return result; }, [] as IObjectDifferent<S>[]); } };
src/helpers/leitenFilterRequest.ts
Hecmatyar-leiten-zustand-0248dac
[ { "filename": "src/helpers/leitenGroupRequest.ts", "retrieved_chunk": " }\n };\n } else {\n pathWithKey = (path + `.${key}`) as DotNestedKeys<Store>;\n if (options?.initialContent) {\n const initial = checkInitial(options.initialContent)\n ? options.initialContent(key)\n : options.initialContent;\n const nextState = produce(store.getState(), (draft) => {\n set(draft, pathWithKey, initial);", "score": 23.505904139135023 }, { "filename": "src/helpers/leitenGroupFilterRequest.ts", "retrieved_chunk": " ) => {\n prevFilters[path] = {};\n type VALUE = ValueOf<DotNestedValue<Store, Path>>;\n function hook(\n key: string,\n referenceObject?: VALUE\n ): IObjectDifferent<VALUE>[] {\n return store((state) =>\n getObjectDifference(get(state, `${path}.${key}`), referenceObject)\n );", "score": 14.728335330592705 }, { "filename": "src/helpers/leitenRecord.ts", "retrieved_chunk": " const initialValue = get(store.getState(), path, \"_empty\") as VALUE;\n if (initialValue === \"_empty\" || typeof initialValue !== \"object\") {\n throw new Error(\n \"[leitenRecord] The defined path does not match the required structure\"\n );\n }\n const getState = (): VALUE => {\n const value = get(store.getState(), path, \"_empty\") as VALUE | \"_empty\";\n return value !== \"_empty\" ? value : initialValue;\n };", "score": 13.612339159916637 }, { "filename": "src/helpers/leitenRequest.ts", "retrieved_chunk": " } else {\n return initialContent;\n }\n };\n const _set = (value: Partial<Result> | void, rewrite = false) => {\n if (typeof value === \"object\") {\n const state = getContent();\n const objectContent = rewrite\n ? ({ ...value } as Result)\n : ({ ...state, ...value } as Result);", "score": 12.308682609409646 }, { "filename": "src/helpers/leitenGroupRequest.ts", "retrieved_chunk": " key: value.key,\n }));\n const requestsStore: typeof state = keys.reduce((acc, { id, key }) => {\n return Object.assign(acc, { [id]: state[key] });\n }, {} as typeof state);\n return (selector || nonTypedReturn)(requestsStore);\n },\n shallow || equals\n );\n };", "score": 11.93093762890328 } ]
typescript
? leitenPrimitive : leitenRecord;
import { produce } from "immer"; import { get, set } from "lodash-es"; import { StoreApi } from "zustand"; import { UseBoundStore } from "zustand/esm"; import { DotNestedKeys, DotNestedValue, ValueOf, } from "../interfaces/dotNestedKeys"; import { getObjectDifference, IObjectDifferent } from "./leitenFilterRequest"; import { AcceptableGroupRequestType, ILeitenGroupRequestArrayOption, ILeitenGroupRequestOption, ILeitenGroupRequestParams, leitenGroupRequest, } from "./leitenGroupRequest"; import { ILeitenRecordEffects } from "./leitenRecord"; import { resettableStoreSubscription } from "./leitenRequest"; /* eslint-disable @typescript-eslint/no-explicit-any */ export const leitenGroupFilterRequest = < Store extends object, P extends DotNestedKeys<Store>, Result extends DotNestedValue<Store, P> extends Record< string, AcceptableGroupRequestType<Store> > ? NonNullable<DotNestedValue<Store, P>[string]> : DotNestedValue<Store, P> extends Array<AcceptableGroupRequestType<Store>> ? NonNullable<DotNestedValue<Store, P>[number]> : DotNestedValue<Store, P> >( store: UseBoundStore<StoreApi<Store>>, path: P extends string ? Result extends void ? P : DotNestedValue<Store, P> extends Record<string, Result> | Array<Result> ? P : never : never, request: (params: ILeitenGroupRequestParams<void>) => Promise<Result>, options?: DotNestedValue<Store, P> extends Record< string, AcceptableGroupRequestType<Store> > ? ILeitenGroupRequestOption<void, Result> : ILeitenGroupRequestArrayOption<void, Result> ) => { const leiten = leitenGroupRequest(store, path, request, { ...options, action: (args) => { const key = args.payload.key; updatePrevFilters(key); return options?.action?.(args); }, } as DotNestedValue<Store, P> extends Record<string, AcceptableGroupRequestType<Store>> ? ILeitenGroupRequestOption<void, Result> : ILeitenGroupRequestArrayOption<void, Result>); const filters: Record<string, IGroupRecord<any>> = {}; const prevFilters: Record<string, Record<string, any>> = {}; const createFilter = <Path extends DotNestedKeys<Store>>( path: Path extends string ? DotNestedValue<Store, Path> extends Record<string, unknown> ? Path : never : never, options
: ILeitenRecordEffects< ValueOf<DotNestedValue<Store, Path>>, Store > & {
initialValue: ValueOf<DotNestedValue<Store, Path>>; } ) => { prevFilters[path] = {}; type VALUE = ValueOf<DotNestedValue<Store, Path>>; function hook( key: string, referenceObject?: VALUE ): IObjectDifferent<VALUE>[] { return store((state) => getObjectDifference(get(state, `${path}.${key}`), referenceObject) ); } const getState = (key: string): VALUE | undefined => { return get(store.getState(), `${path}.${key}`, undefined); }; const setState = (key: string, next: VALUE) => { const prev = getState(key); const draftState = produce(store.getState(), (draft) => { set(draft, `${path}.${key}`, next); }); const nextState = options.patchEffect ? { ...draftState, ...options.patchEffect(next) } : draftState; store.setState(nextState); options.sideEffect?.({ prev: prev || options.initialValue, next }); action(key, path, options.initialValue); }; const clear = (key: string) => { setState(key, options.initialValue); }; const patch = (key: string, value: Partial<VALUE>) => { const prev: object = (getState(key) || options.initialValue) as any; setState(key, { ...prev, ...value } as VALUE); }; const record = { clear, patch, set: setState, get: getState }; filters[path] = record; return Object.assign(hook, record); }; const action = async (key: string, path: string, initialValue: any) => { const nextFilters = get(store.getState(), `${path}.${key}`); if ( JSON.stringify(nextFilters) !== JSON.stringify(prevFilters[path][key] || initialValue) ) { leiten.action([{ key, params: undefined }]); } }; const updatePrevFilters = (key: string) => { Object.keys(filters).forEach((item) => { prevFilters[item][key] = get(store.getState(), `${item}.${key}`); }); }; resettableStoreSubscription(store, () => { Object.keys(filters).forEach((path) => { prevFilters[path] = {}; }); }); const clearAll = () => { leiten.clear(); const keys = Object.keys(leiten.requests); Object.keys(filters).forEach((key) => keys.forEach((k) => filters[key]?.clear(k)) ); }; return Object.assign(leiten, { createFilter, clearAll }); }; interface IGroupRecord<VALUE> { clear: (key: string) => void; patch: (key: string, value: Partial<VALUE>) => void; set: (key: string, next: VALUE) => void; get: (key: string) => VALUE | undefined; }
src/helpers/leitenGroupFilterRequest.ts
Hecmatyar-leiten-zustand-0248dac
[ { "filename": "src/helpers/leitenFilterRequest.ts", "retrieved_chunk": " path: Path extends string ? Path : never,\n options?: ILeitenRecordEffects<DotNestedValue<Store, Path>, Store>\n ): DotNestedValue<Store, Path> extends object\n ? RecordFilter<DotNestedValue<Store, Path>>\n : PrimitiveFilter<DotNestedValue<Store, Path>> => {\n type Response = DotNestedValue<Store, Path> extends object\n ? RecordFilter<DotNestedValue<Store, Path>>\n : PrimitiveFilter<DotNestedValue<Store, Path>>;\n const initial = get(store.getState(), path, undefined);\n function hook(", "score": 56.78262283052826 }, { "filename": "src/helpers/leitenFilterRequest.ts", "retrieved_chunk": " ? Path\n : DotNestedValue<Store, Path> extends Result | null\n ? Path\n : never\n : never,\n request: (params: void, extraArgument?: IExtraArgument) => Promise<Result>,\n options?: ILeitenRequestOptions<void, Result>\n) => {\n const leiten = leitenRequest(store, path, request, {\n ...options,", "score": 44.15876577813622 }, { "filename": "src/helpers/leitenFilterRequest.ts", "retrieved_chunk": " (initialObject?: Y): IObjectDifferent[];\n} & ILeitenPrimitive<Y>;\nexport const leitenFilterRequest = <\n Store extends object,\n Path extends DotNestedKeys<Store>,\n Result extends DotNestedValue<Store, Path> | null | void\n>(\n store: UseBoundStore<StoreApi<Store>>,\n path: Path extends string\n ? Result extends void", "score": 41.40974245442587 }, { "filename": "src/interfaces/dotNestedKeys.ts", "retrieved_chunk": "> = Path extends `${infer Head}.${infer Tail}`\n ? DotNestedValue<O[Head], Tail>\n : O[Path];\nexport type ValueOf<T> = T extends Record<infer _K, infer V> ? V : never;", "score": 38.709367141028736 }, { "filename": "src/helpers/leitenNormalizedList.ts", "retrieved_chunk": " P extends DotNestedKeys<Store>\n>(\n store: StoreApi<Store>,\n path: P extends string\n ? DotNestedValue<Store, P> extends Record<string, unknown>\n ? P\n : never\n : never,\n params: {\n compare?: (", "score": 36.948992859571746 } ]
typescript
: ILeitenRecordEffects< ValueOf<DotNestedValue<Store, Path>>, Store > & {
import { produce } from "immer"; import { get, isEqual, set } from "lodash-es"; import { nanoid } from "nanoid"; import { useEffect, useState } from "react"; import { StoreApi } from "zustand"; import { shallow } from "zustand/shallow"; import { useLeitenRequests } from "../hooks/useLeitenRequest"; import { DotNestedKeys, DotNestedValue } from "../interfaces/dotNestedKeys"; import { ILeitenLoading, ILoadingStatus, initialLeitenLoading, } from "../interfaces/IContentLoading"; /* eslint-disable @typescript-eslint/no-explicit-any */ export type UseRequestType<Payload, Result> = < U = ILeitenLoading<Payload, Result> >( selector?: (state: ILeitenLoading<Payload, Result>) => U, equals?: (a: U, b: U) => boolean ) => U; export interface ILeitenRequest<Payload, Result> extends UseRequestType<Payload, Result> { abort: () => void; clear: () => void; action: ( params: Payload, extraParams?: { status?: ILoadingStatus; requestId?: string } ) => void; set: (value: Partial<Result> | void, rewrite?: boolean) => void; key: string; get: () => ILeitenLoading<Payload, Result>; } export interface ILeitenRequestCallback<Payload, Result> { previousResult: Result; result: Result; payload: Payload; requestId: string; error?: string; } export interface ILeitenRequestOptions<Payload, Result> { fulfilled?: ( options: Omit<ILeitenRequestCallback<Payload, Result>, "error"> ) => void; rejected?: ( options: Omit<ILeitenRequestCallback<Payload, Result>, "result"> ) => void; abort?: ( options: Omit<ILeitenRequestCallback<Payload, Result>, "error" | "result"> ) => void; resolved?: ( options: Omit<ILeitenRequestCallback<Payload, Result>, "result" | "error"> ) => void; action?: ( options: Omit<ILeitenRequestCallback<Payload, Result>, "error" | "result"> ) => void; initialStatus?: ILoadingStatus; optimisticUpdate?: (params: Payload) => Result; } export const leitenRequest = < Store extends object, P extends DotNestedKeys<Store>, Payload, Result extends DotNestedValue<Store, P> | null | void >( store: StoreApi<Store>, path: P extends string ? Result extends void ? P : DotNestedValue<Store, P> extends Result | null ? P : never : never, payloadCreator: ( params: Payload, extraArgument?: IExtraArgument ) => Promise<Result>, options?: ILeitenRequestOptions<Payload, Result> ): ILeitenRequest<Payload, Result> => { const key = nanoid(12); const initialState = initialLeitenLoading<Payload, Result>( options?.initialStatus ); const initialContent = get(store.getState(), path, null) as Result; const setState = (state: ILeitenLoading<Payload, Result>) => { useLeitenRequests.setState({ [key]: state }); }; setState(initialState); //init request const setContent = (content: Result) => { const nextState = produce(store.getState(), (draft) => { set(draft, path, content); }); store.setState(nextState); }; const getState = (): ILeitenLoading<Payload, Result> => { return
useLeitenRequests.getState()[key] || initialState;
}; const getContent = (): Result => { const result = get(store.getState(), path, "_empty") as Result | "_empty"; if (result !== "_empty") { return result || initialContent; } else { return initialContent; } }; const _set = (value: Partial<Result> | void, rewrite = false) => { if (typeof value === "object") { const state = getContent(); const objectContent = rewrite ? ({ ...value } as Result) : ({ ...state, ...value } as Result); const content = typeof value === "object" ? objectContent : value; setContent(content); } else { value !== undefined && value !== null && setContent(value); } }; let previousResult: Result = getContent(); const reactions = { action: (payload: Payload, status?: ILoadingStatus, requestId?: string) => { setState({ status: status ?? "loading", payload, error: undefined, requestId: requestId, }); options?.action?.({ previousResult, requestId: requestId || "", payload, }); previousResult = getContent(); if (options?.optimisticUpdate) { setContent(options.optimisticUpdate(payload)); } }, fulfilled: (result: Result, payload: Payload, requestId: string) => { const state = getState(); if (requestId === state.requestId) { setState({ ...state, status: "loaded" }); if ( result !== undefined && (!options?.optimisticUpdate || !isEqual(previousResult, result)) ) { setContent(result); } options?.fulfilled?.({ previousResult, requestId, payload, result }); } }, rejected: (payload: Payload, error: string, requestId?: string) => { const state = getState(); setState({ ...state, status: "error", error }); options?.rejected?.({ previousResult, requestId: requestId || "", payload, error, }); if (options?.optimisticUpdate) { setContent(previousResult); } }, abort: (payload: Payload, requestId: string) => { setState(initialState); options?.abort?.({ previousResult, requestId, payload }); if (options?.optimisticUpdate) { setContent(previousResult); } }, resolved: (payload: Payload, requestId: string) => { options?.resolved?.({ previousResult, requestId, payload }); }, }; const { action, abort } = createAsyncActions(payloadCreator, reactions); const _abort = () => { abort(); }; const clear = () => { setState(initialState); setContent(initialContent); }; const usages: Record<string, boolean> = {}; const useRequest: UseRequestType<Payload, Result> = (selector, equals) => { const [id] = useState(() => nanoid()); useEffect(() => { usages[id] = true; return () => { usages[id] = false; }; }, []); return useLeitenRequests( (state) => (selector || nonTypedReturn)(state[key] || initialState), shallow || equals ); }; resettableStoreSubscription(store, () => setState(initialState)); const _get = () => { return useLeitenRequests.getState()[key]; }; return Object.assign(useRequest, { abort: _abort, action, clear, set: _set, key, get: _get, _usages: usages, }); }; // eslint-disable-next-line @typescript-eslint/no-explicit-any const nonTypedReturn = (value: any) => value; export const resettableStoreSubscription = ( store: StoreApi<object>, callback: () => void ) => { setTimeout(() => { const resettable = (store.getState() as any)["_resettableLifeCycle"] !== undefined; if (resettable) { store.subscribe((next, prev) => { if ( (next as any)["_resettableLifeCycle"] === false && (prev as any)["_resettableLifeCycle"] === true ) callback(); }); } }, 0); }; function createAsyncActions<Payload, Result>( payloadCreator: ( params: Payload, extraArgument?: IExtraArgument ) => Promise<Result>, extra?: IReaction<Payload, Result> ) { let controller = new AbortController(); let signal = controller.signal; const abort = () => { controller.abort(); controller = new AbortController(); signal = controller.signal; }; const action = ( params: Payload, options?: { status?: ILoadingStatus; requestId?: string } ) => { const requestId = options?.requestId || nanoid(); extra?.action?.(params, options?.status, requestId); payloadCreator(params, { signal }) .then((result) => { extra?.fulfilled?.(result, params, requestId); }) .catch((error) => { if (error.message === "The user aborted a request.") { extra?.abort?.(params, requestId); } else { extra?.rejected?.(params, error, requestId); } }) .finally(() => { extra?.resolved?.(params, requestId); }); }; return { action, abort }; } interface IReaction<Payload, Result> { fulfilled?: (result: Result, params: Payload, requestId: string) => void; rejected?: (params: Payload, error: string, requestId?: string) => void; abort?: (params: Payload, requestId: string) => void; resolved?: (params: Payload, requestId: string) => void; action?: ( params: Payload, status?: ILoadingStatus, requestId?: string ) => void; } export type IExtraArgument = { signal: AbortSignal; // requestId: string };
src/helpers/leitenRequest.ts
Hecmatyar-leiten-zustand-0248dac
[ { "filename": "src/helpers/leitenPrimitive.ts", "retrieved_chunk": " };\n const clear = () => {\n const nextState = produce(store.getState(), (draft) => {\n set(draft, path, initialValue);\n });\n store.setState(nextState);\n };\n return { set: setState, get: getState, clear };\n};", "score": 39.405546052060835 }, { "filename": "src/helpers/leitenModal.ts", "retrieved_chunk": " const key = nanoid(10);\n const setContent = (value: Data) => {\n const nextState = produce(store.getState(), (draft) => {\n set(draft, path, value);\n });\n store.setState(nextState);\n };\n const setState = (value: boolean, replace?: boolean) => {\n leitenModalManagerAction(key, value, replace);\n };", "score": 37.804674948995135 }, { "filename": "src/helpers/leitenRecord.ts", "retrieved_chunk": " };\n const clear = () => {\n const nextState = produce(store.getState(), (draft) => {\n set(draft, path, initialValue);\n });\n store.setState(nextState);\n };\n const patch = (value: Partial<VALUE>) => {\n setState({ ...getState(), ...value });\n };", "score": 37.625296895012546 }, { "filename": "src/helpers/leitenGroupFilterRequest.ts", "retrieved_chunk": " }\n const getState = (key: string): VALUE | undefined => {\n return get(store.getState(), `${path}.${key}`, undefined);\n };\n const setState = (key: string, next: VALUE) => {\n const prev = getState(key);\n const draftState = produce(store.getState(), (draft) => {\n set(draft, `${path}.${key}`, next);\n });\n const nextState = options.patchEffect", "score": 34.03451713446721 }, { "filename": "src/helpers/leitenRecord.ts", "retrieved_chunk": " const setState = (next: VALUE) => {\n const prev = getState();\n const draftState = produce(store.getState(), (draft) => {\n set(draft, path, next);\n });\n const nextState = effects?.patchEffect\n ? { ...draftState, ...effects.patchEffect(next) }\n : draftState;\n store.setState(nextState);\n effects?.sideEffect?.({ prev, next });", "score": 33.81119555371888 } ]
typescript
useLeitenRequests.getState()[key] || initialState;
import { produce } from "immer"; import { get, set } from "lodash-es"; import { StoreApi } from "zustand/esm"; import { shallow } from "zustand/shallow"; import { useLeitenRequests } from "../hooks/useLeitenRequest"; import { DotNestedKeys, DotNestedValue } from "../interfaces/dotNestedKeys"; import { ILeitenLoading, ILoadingStatus, initialLeitenLoading, } from "../interfaces/IContentLoading"; import { ILeitenRequest, ILeitenRequestOptions, leitenRequest, resettableStoreSubscription, } from "./leitenRequest"; /* eslint-disable @typescript-eslint/no-explicit-any */ export interface ILeitenGroupRequestParams<Params> { key: string; params: Params; } export interface IGroupCallOptions { status?: ILoadingStatus; requestId?: string; } export type AcceptableGroupRequestType<Store extends object> = void | DotNestedValue<Store, DotNestedKeys<Store>> | null; type LeitenState<Payload, Result> = ILeitenLoading< ILeitenGroupRequestParams<Payload>, Result >; type UseRequestType<Payload, Result> = <U = LeitenState<Payload, Result>>( key: string, selector?: (state: LeitenState<Payload, Result>) => U, equals?: (a: U, b: U) => boolean ) => U; type UseGroupRequestType<Payload, Result> = <U = LeitenState<Payload, Result>>( selector?: (state: Record<string, LeitenState<Payload, Result>>) => U, equals?: (a: U, b: U) => boolean ) => U; export type ILeitenGroupRequest<Payload, Result> = { clear: (key?: string) => void; action: ( params: ILeitenGroupRequestParams<Payload>[], options?: IGroupCallOptions ) => void; requests: Record< string, ILeitenRequest<ILeitenGroupRequestParams<Payload>, Result> >; } & UseRequestType<Payload, Result> & UseGroupRequestType<Payload, Result>; export interface ILeitenGroupRequestOption<Payload, Result> extends ILeitenRequestOptions<ILeitenGroupRequestParams<Payload>, Result> { initialContent?: Result | ((key: string) => Result); } export interface ILeitenGroupRequestArrayOption<Payload, Result> extends ILeitenGroupRequestOption<Payload, Result> { getKey: (value: Result) => string; } export const leitenGroupRequest = < Store extends object, P extends DotNestedKeys<Store>, Payload, Result extends DotNestedValue<Store, P> extends Record< string, AcceptableGroupRequestType<Store> > ? DotNestedValue<Store, P>[string] : DotNestedValue<Store, P> extends Array<AcceptableGroupRequestType<Store>> ? DotNestedValue<Store, P>[number] : DotNestedValue<Store, P> >( store: StoreApi<Store>, path: P extends string ? Result extends void ? P : DotNestedValue<Store, P> extends Record<string, Result> | Array<Result> ? P : never : never, payloadCreator: ( params: ILeitenGroupRequestParams<Payload> ) => Promise<Result>, options?: DotNestedValue<Store, P> extends Record< string, AcceptableGroupRequestType<Store> > ? ILeitenGroupRequestOption<Payload, Result> : ILeitenGroupRequestArrayOption<Payload, Result> ): ILeitenGroupRequest<Payload, Result> => { const initialRequestState = initialLeitenLoading< ILeitenGroupRequestParams<Payload>, Result >(options?.initialStatus); let requests: Record< string, ILeitenRequest<ILeitenGroupRequestParams<Payload>, Result> > = {}; const isArray = Array.isArray(get(store.getState(), path)); const getPathToArrayItem = (key: string) => { const raw = get(store.getState(), path, []); const source = Array.isArray(raw) ? raw : []; const find = source.findIndex( (s) => (options as ILeitenGroupRequestArrayOption<Payload, Result>)?.getKey?.( s ) === key ); const index = find !== -1 ? find : source.length; const withKey = (path + `["${index}"]`) as DotNestedKeys<Store>; return { withKey, isNew: find === -1 }; }; const add = (key: string) => { let pathWithKey = "" as DotNestedKeys<Store>; let payload = payloadCreator; if (isArray) { const before = getPathToArrayItem(key); pathWithKey = before.withKey; // eslint-disable-next-line // @ts-ignore payload = async (params: ILeitenGroupRequestParams<Payload>) => { const result = await payloadCreator(params); const after = getPathToArrayItem(key); if ((before.isNew && after.isNew) || !after.isNew) { const nextState = produce(store.getState(), (draft) => { set(draft, after.withKey, result); }); store.setState(nextState); } }; } else { pathWithKey = (path + `.${key}`) as DotNestedKeys<Store>; if (options?.initialContent) { const initial = checkInitial(options.initialContent) ? options.initialContent(key) : options.initialContent; const nextState = produce(store.getState(), (draft) => { set(draft, pathWithKey, initial); }); store.setState(nextState); } } requests[key] = leitenRequest(store, pathWithKey, payload, options); }; const action = ( params: ILeitenGroupRequestParams<Payload>[], options?: IGroupCallOptions ) => { params.forEach(({ key, params }) => { const request = requests[key]; const payload = { key, params }; if (request && !isArray) { request.action(payload, options); } else { add(key); requests[key].action(payload); } }); }; const clear = (key?: string) => { if (key) { !isArray && requests[key].clear(); delete requests[key]; } else { set(store, path, {}); requests = {}; } }; const useRequest: UseRequestType<Payload, Result> = ( key, selector, equals ) => { return
useLeitenRequests((state) => {
const id = requests[key]?.key; return (selector || nonTypedReturn)( (id && state[id]) || initialRequestState ); }, shallow || equals); }; const useGroupRequest: UseGroupRequestType<Payload, Result> = ( selector, equals ) => { return useLeitenRequests( (state: Record<string, LeitenState<Payload, Result>>) => { const keys = Object.entries(requests).map(([id, value]) => ({ id, key: value.key, })); const requestsStore: typeof state = keys.reduce((acc, { id, key }) => { return Object.assign(acc, { [id]: state[key] }); }, {} as typeof state); return (selector || nonTypedReturn)(requestsStore); }, shallow || equals ); }; function hook<Payload, Result, U = LeitenState<Payload, Result>>( key: string, selector?: (state: LeitenState<Payload, Result>) => U, equals?: (a: U, b: U) => boolean ): U; function hook<Payload, Result, U = LeitenState<Payload, Result>>( selector?: (state: Record<string, LeitenState<Payload, Result>>) => U, equals?: (a: U, b: U) => boolean ): U; function hook<Payload, Result, U = LeitenState<Payload, Result>>( first?: | string | ((state: Record<string, LeitenState<Payload, Result>>) => U), second?: | ((state: LeitenState<Payload, Result>) => U) | ((a: U, b: U) => boolean), third?: (a: U, b: U) => boolean ): U { if (first !== undefined && typeof first === "string") { return useRequest(first, second as any, third); } else { return useGroupRequest(first as any, second as any) as any; } } resettableStoreSubscription(store, () => clear()); return Object.assign(hook, { clear, action, requests, call: action }); }; const nonTypedReturn = (value: any) => value; const checkInitial = <Result>( value: Result | ((key: string) => Result) ): value is (key: string) => Result => typeof value === "function";
src/helpers/leitenGroupRequest.ts
Hecmatyar-leiten-zustand-0248dac
[ { "filename": "src/helpers/leitenRequest.ts", "retrieved_chunk": " const usages: Record<string, boolean> = {};\n const useRequest: UseRequestType<Payload, Result> = (selector, equals) => {\n const [id] = useState(() => nanoid());\n useEffect(() => {\n usages[id] = true;\n return () => {\n usages[id] = false;\n };\n }, []);\n return useLeitenRequests(", "score": 24.80430704650096 }, { "filename": "src/helpers/leitenRequest.ts", "retrieved_chunk": " (state) => (selector || nonTypedReturn)(state[key] || initialState),\n shallow || equals\n );\n };\n resettableStoreSubscription(store, () => setState(initialState));\n const _get = () => {\n return useLeitenRequests.getState()[key];\n };\n return Object.assign(useRequest, {\n abort: _abort,", "score": 21.82461567389254 }, { "filename": "src/helpers/leitenRequest.ts", "retrieved_chunk": " ILoadingStatus,\n initialLeitenLoading,\n} from \"../interfaces/IContentLoading\";\n/* eslint-disable @typescript-eslint/no-explicit-any */\nexport type UseRequestType<Payload, Result> = <\n U = ILeitenLoading<Payload, Result>\n>(\n selector?: (state: ILeitenLoading<Payload, Result>) => U,\n equals?: (a: U, b: U) => boolean\n) => U;", "score": 17.079415023530185 }, { "filename": "src/helpers/leitenRequest.ts", "retrieved_chunk": " options?.initialStatus\n );\n const initialContent = get(store.getState(), path, null) as Result;\n const setState = (state: ILeitenLoading<Payload, Result>) => {\n useLeitenRequests.setState({ [key]: state });\n };\n setState(initialState); //init request\n const setContent = (content: Result) => {\n const nextState = produce(store.getState(), (draft) => {\n set(draft, path, content);", "score": 11.833778409282282 }, { "filename": "src/helpers/leitenRequest.ts", "retrieved_chunk": " });\n store.setState(nextState);\n };\n const getState = (): ILeitenLoading<Payload, Result> => {\n return useLeitenRequests.getState()[key] || initialState;\n };\n const getContent = (): Result => {\n const result = get(store.getState(), path, \"_empty\") as Result | \"_empty\";\n if (result !== \"_empty\") {\n return result || initialContent;", "score": 11.378471477443597 } ]
typescript
useLeitenRequests((state) => {
import { produce } from "immer"; import { get, set } from "lodash-es"; import { StoreApi } from "zustand"; import { UseBoundStore } from "zustand/esm"; import { DotNestedKeys, DotNestedValue, ValueOf, } from "../interfaces/dotNestedKeys"; import { getObjectDifference, IObjectDifferent } from "./leitenFilterRequest"; import { AcceptableGroupRequestType, ILeitenGroupRequestArrayOption, ILeitenGroupRequestOption, ILeitenGroupRequestParams, leitenGroupRequest, } from "./leitenGroupRequest"; import { ILeitenRecordEffects } from "./leitenRecord"; import { resettableStoreSubscription } from "./leitenRequest"; /* eslint-disable @typescript-eslint/no-explicit-any */ export const leitenGroupFilterRequest = < Store extends object, P extends DotNestedKeys<Store>, Result extends DotNestedValue<Store, P> extends Record< string, AcceptableGroupRequestType<Store> > ? NonNullable<DotNestedValue<Store, P>[string]> : DotNestedValue<Store, P> extends Array<AcceptableGroupRequestType<Store>> ? NonNullable<DotNestedValue<Store, P>[number]> : DotNestedValue<Store, P> >( store: UseBoundStore<StoreApi<Store>>, path: P extends string ? Result extends void ? P : DotNestedValue<Store, P> extends Record<string, Result> | Array<Result> ? P : never : never, request: (params: ILeitenGroupRequestParams<void>) => Promise<Result>, options?: DotNestedValue<Store, P> extends Record< string, AcceptableGroupRequestType<Store> > ? ILeitenGroupRequestOption<void, Result> : ILeitenGroupRequestArrayOption<void, Result> ) => { const leiten = leitenGroupRequest(store, path, request, { ...options, action: (args) => { const key = args.payload.key; updatePrevFilters(key); return options?.action?.(args); }, } as DotNestedValue<Store, P> extends Record<string, AcceptableGroupRequestType<Store>> ? ILeitenGroupRequestOption<void, Result> : ILeitenGroupRequestArrayOption<void, Result>); const filters: Record<string, IGroupRecord<any>> = {}; const prevFilters: Record<string, Record<string, any>> = {}; const createFilter = <Path extends DotNestedKeys<Store>>( path: Path extends string ? DotNestedValue<Store, Path> extends Record<string, unknown> ? Path : never : never, options: ILeitenRecordEffects< ValueOf<DotNestedValue<Store, Path>>, Store > & { initialValue: ValueOf<DotNestedValue<Store, Path>>; } ) => { prevFilters[path] = {}; type VALUE = ValueOf<DotNestedValue<Store, Path>>; function hook( key: string, referenceObject?: VALUE
): IObjectDifferent<VALUE>[] {
return store((state) => getObjectDifference(get(state, `${path}.${key}`), referenceObject) ); } const getState = (key: string): VALUE | undefined => { return get(store.getState(), `${path}.${key}`, undefined); }; const setState = (key: string, next: VALUE) => { const prev = getState(key); const draftState = produce(store.getState(), (draft) => { set(draft, `${path}.${key}`, next); }); const nextState = options.patchEffect ? { ...draftState, ...options.patchEffect(next) } : draftState; store.setState(nextState); options.sideEffect?.({ prev: prev || options.initialValue, next }); action(key, path, options.initialValue); }; const clear = (key: string) => { setState(key, options.initialValue); }; const patch = (key: string, value: Partial<VALUE>) => { const prev: object = (getState(key) || options.initialValue) as any; setState(key, { ...prev, ...value } as VALUE); }; const record = { clear, patch, set: setState, get: getState }; filters[path] = record; return Object.assign(hook, record); }; const action = async (key: string, path: string, initialValue: any) => { const nextFilters = get(store.getState(), `${path}.${key}`); if ( JSON.stringify(nextFilters) !== JSON.stringify(prevFilters[path][key] || initialValue) ) { leiten.action([{ key, params: undefined }]); } }; const updatePrevFilters = (key: string) => { Object.keys(filters).forEach((item) => { prevFilters[item][key] = get(store.getState(), `${item}.${key}`); }); }; resettableStoreSubscription(store, () => { Object.keys(filters).forEach((path) => { prevFilters[path] = {}; }); }); const clearAll = () => { leiten.clear(); const keys = Object.keys(leiten.requests); Object.keys(filters).forEach((key) => keys.forEach((k) => filters[key]?.clear(k)) ); }; return Object.assign(leiten, { createFilter, clearAll }); }; interface IGroupRecord<VALUE> { clear: (key: string) => void; patch: (key: string, value: Partial<VALUE>) => void; set: (key: string, next: VALUE) => void; get: (key: string) => VALUE | undefined; }
src/helpers/leitenGroupFilterRequest.ts
Hecmatyar-leiten-zustand-0248dac
[ { "filename": "src/helpers/leitenFilterRequest.ts", "retrieved_chunk": " path: Path extends string ? Path : never,\n options?: ILeitenRecordEffects<DotNestedValue<Store, Path>, Store>\n ): DotNestedValue<Store, Path> extends object\n ? RecordFilter<DotNestedValue<Store, Path>>\n : PrimitiveFilter<DotNestedValue<Store, Path>> => {\n type Response = DotNestedValue<Store, Path> extends object\n ? RecordFilter<DotNestedValue<Store, Path>>\n : PrimitiveFilter<DotNestedValue<Store, Path>>;\n const initial = get(store.getState(), path, undefined);\n function hook(", "score": 34.16369455753257 }, { "filename": "src/helpers/leitenPrimitive.ts", "retrieved_chunk": "): ILeitenPrimitive<DotNestedValue<Store, P>> => {\n type VALUE = DotNestedValue<Store, P>;\n const initialValue = get(store.getState(), path, \"_empty\") as VALUE;\n if (initialValue === \"_empty\") {\n throw new Error(\"[leitenPrimitive] The defined path does not exist\");\n }\n const getState = (): VALUE => {\n const value = get(store.getState(), path, \"_empty\") as VALUE | \"_empty\";\n return value !== \"_empty\" ? value : initialValue;\n };", "score": 27.807586689399013 }, { "filename": "src/helpers/leitenNormalizedList.ts", "retrieved_chunk": " left: ValueOf<DotNestedValue<Store, P>>,\n right: ValueOf<DotNestedValue<Store, P>>\n ) => boolean;\n sideEffect?: () => void;\n patchEffect?: (\n items: NormalizedType<ValueOf<DotNestedValue<Store, P>>>\n ) => Partial<Store>;\n getKey: (item: ValueOf<DotNestedValue<Store, P>>) => string;\n }\n): ILeitenList<ValueOf<DotNestedValue<Store, P>>> & {", "score": 27.27390170597644 }, { "filename": "src/helpers/leitenNormalizedList.ts", "retrieved_chunk": " removeByKey: (value: string | string[]) => void;\n get: () => NormalizedType<ValueOf<DotNestedValue<Store, P>>>;\n} => {\n type ITEM = ValueOf<DotNestedValue<Store, P>>;\n const initialValue = get(\n store.getState(),\n path,\n \"_empty\"\n ) as NormalizedType<ITEM>;\n if ((initialValue as any) === \"_empty\" || typeof initialValue !== \"object\") {", "score": 26.32612928365518 }, { "filename": "src/helpers/leitenFilterRequest.ts", "retrieved_chunk": " (initialObject?: Y): IObjectDifferent[];\n} & ILeitenPrimitive<Y>;\nexport const leitenFilterRequest = <\n Store extends object,\n Path extends DotNestedKeys<Store>,\n Result extends DotNestedValue<Store, Path> | null | void\n>(\n store: UseBoundStore<StoreApi<Store>>,\n path: Path extends string\n ? Result extends void", "score": 24.100571449606107 } ]
typescript
): IObjectDifferent<VALUE>[] {
import { cloneDeep, get, isEqual } from "lodash-es"; import { StoreApi, UseBoundStore } from "zustand"; import { DotNestedKeys, DotNestedValue } from "../interfaces/dotNestedKeys"; import { ILeitenPrimitive, leitenPrimitive } from "./leitenPrimitive"; import { ILeitenRecord, ILeitenRecordEffects, leitenRecord, } from "./leitenRecord"; import { IExtraArgument, ILeitenRequestOptions, leitenRequest, resettableStoreSubscription, } from "./leitenRequest"; /* eslint-disable @typescript-eslint/no-explicit-any */ type RecordFilter<T> = { (initialObject?: T): IObjectDifferent[]; } & ILeitenRecord<T>; type PrimitiveFilter<Y> = { (initialObject?: Y): IObjectDifferent[]; } & ILeitenPrimitive<Y>; export const leitenFilterRequest = < Store extends object, Path extends DotNestedKeys<Store>, Result extends DotNestedValue<Store, Path> | null | void >( store: UseBoundStore<StoreApi<Store>>, path: Path extends string ? Result extends void ? Path : DotNestedValue<Store, Path> extends Result | null ? Path : never : never, request: (params: void, extraArgument?: IExtraArgument) => Promise<Result>, options?: ILeitenRequestOptions<void, Result> ) => { const leiten = leitenRequest(store, path, request, { ...options, action: (args) => { updatePrevFilters(); return options?.action?.(args); }, }); const filters: Record<string, ILeitenRecord<any> | ILeitenPrimitive<any>> = {}; let prevFilters: Record<string, any> = {}; const initialFilters: Record<string, any> = {}; const createFilter = <Path extends DotNestedKeys<Store>>( path: Path extends string ? Path : never, options?: ILeitenRecordEffects<DotNestedValue<Store, Path>, Store> ): DotNestedValue<Store, Path> extends object ? RecordFilter<DotNestedValue<Store, Path>> : PrimitiveFilter<DotNestedValue<Store, Path>> => { type Response = DotNestedValue<Store, Path> extends object ? RecordFilter<DotNestedValue<Store, Path>> : PrimitiveFilter<DotNestedValue<Store, Path>>; const initial = get(store.getState(), path, undefined); function hook( initialObject?: DotNestedValue<Store, Path> ): IObjectDifferent[] { return store((state) => getObjectDifference( get(state, path, initialObject || initial), initialObject || initial ) ); } const controller = typeof initial !== "object" ? leitenPrimitive : leitenRecord; const record = controller(store, path, {
sideEffect: (side) => {
options?.sideEffect?.(side); action(path).then(); }, patchEffect: options?.patchEffect, }); prevFilters[path] = record.get(); filters[path] = record; initialFilters[path] = record.get(); return Object.assign(hook, record) as Response; }; const listen = < ExternalStore extends object, ExternalPath extends DotNestedKeys<ExternalStore> >( store: UseBoundStore<StoreApi<ExternalStore>>, path: ExternalPath extends string ? ExternalPath : never, options?: { sideEffect?: (value: { prev: DotNestedValue<ExternalStore, ExternalPath>; next: DotNestedValue<ExternalStore, ExternalPath>; }) => void; } ) => { let prevValue = get(store.getState(), path); const haveSubscription = () => !!Object.values((leiten as any)._usages || {}).filter((item) => item) .length; return store.subscribe((state) => { const value = get(state, path); if (haveSubscription() && !isEqual(prevValue, value)) { prevValue = value; options?.sideEffect?.({ prev: prevValue, next: value }); pureAction().then(); } }); }; const pureAction = async () => { updatePrevFilters(); leiten.action(); }; const action = async (path: string) => { const filter = get(store.getState(), path); if (JSON.stringify(filter) !== JSON.stringify(prevFilters[path])) { leiten.action(); } }; const updatePrevFilters = () => { prevFilters = Object.keys(filters).reduce( (acc, item) => ({ ...acc, [item]: get(store.getState(), item), }), {} ); }; resettableStoreSubscription(store, () => { prevFilters = cloneDeep(initialFilters); }); const clearAll = () => { leiten.clear(); Object.keys(filters).forEach((key) => filters[key].clear()); leiten.action(); }; return Object.assign(leiten, { createFilter, clearAll, listen }); }; export interface IObjectDifferent<S = any> { field: string; prev: S; next: S; } export const getObjectDifference = <S>( next: any, prev: any ): IObjectDifferent<S>[] => { if (typeof next !== "object") { if (!isEqual(next, prev)) { return [{ field: "_", prev, next }]; } else { return []; } } else { return Object.keys(next).reduce((result, field) => { if (!isEqual(next[field], prev[field])) { result.push({ field, prev: prev[field], next: next[field] }); } return result; }, [] as IObjectDifferent<S>[]); } };
src/helpers/leitenFilterRequest.ts
Hecmatyar-leiten-zustand-0248dac
[ { "filename": "src/helpers/leitenGroupRequest.ts", "retrieved_chunk": " }\n };\n } else {\n pathWithKey = (path + `.${key}`) as DotNestedKeys<Store>;\n if (options?.initialContent) {\n const initial = checkInitial(options.initialContent)\n ? options.initialContent(key)\n : options.initialContent;\n const nextState = produce(store.getState(), (draft) => {\n set(draft, pathWithKey, initial);", "score": 26.10883806033935 }, { "filename": "src/helpers/leitenRequest.ts", "retrieved_chunk": " params: Payload,\n extraArgument?: IExtraArgument\n ) => Promise<Result>,\n extra?: IReaction<Payload, Result>\n) {\n let controller = new AbortController();\n let signal = controller.signal;\n const abort = () => {\n controller.abort();\n controller = new AbortController();", "score": 17.876161934571705 }, { "filename": "src/helpers/leitenGroupFilterRequest.ts", "retrieved_chunk": " const prev: object = (getState(key) || options.initialValue) as any;\n setState(key, { ...prev, ...value } as VALUE);\n };\n const record = { clear, patch, set: setState, get: getState };\n filters[path] = record;\n return Object.assign(hook, record);\n };\n const action = async (key: string, path: string, initialValue: any) => {\n const nextFilters = get(store.getState(), `${path}.${key}`);\n if (", "score": 17.768952651985753 }, { "filename": "src/helpers/leitenRecord.ts", "retrieved_chunk": " const initialValue = get(store.getState(), path, \"_empty\") as VALUE;\n if (initialValue === \"_empty\" || typeof initialValue !== \"object\") {\n throw new Error(\n \"[leitenRecord] The defined path does not match the required structure\"\n );\n }\n const getState = (): VALUE => {\n const value = get(store.getState(), path, \"_empty\") as VALUE | \"_empty\";\n return value !== \"_empty\" ? value : initialValue;\n };", "score": 16.255507417569056 }, { "filename": "src/helpers/leitenGroupFilterRequest.ts", "retrieved_chunk": " ) => {\n prevFilters[path] = {};\n type VALUE = ValueOf<DotNestedValue<Store, Path>>;\n function hook(\n key: string,\n referenceObject?: VALUE\n ): IObjectDifferent<VALUE>[] {\n return store((state) =>\n getObjectDifference(get(state, `${path}.${key}`), referenceObject)\n );", "score": 13.507639118446837 } ]
typescript
sideEffect: (side) => {
import { produce } from "immer"; import { get, set } from "lodash-es"; import { StoreApi } from "zustand"; import { UseBoundStore } from "zustand/esm"; import { DotNestedKeys, DotNestedValue, ValueOf, } from "../interfaces/dotNestedKeys"; import { getObjectDifference, IObjectDifferent } from "./leitenFilterRequest"; import { AcceptableGroupRequestType, ILeitenGroupRequestArrayOption, ILeitenGroupRequestOption, ILeitenGroupRequestParams, leitenGroupRequest, } from "./leitenGroupRequest"; import { ILeitenRecordEffects } from "./leitenRecord"; import { resettableStoreSubscription } from "./leitenRequest"; /* eslint-disable @typescript-eslint/no-explicit-any */ export const leitenGroupFilterRequest = < Store extends object, P extends DotNestedKeys<Store>, Result extends DotNestedValue<Store, P> extends Record< string, AcceptableGroupRequestType<Store> > ? NonNullable<DotNestedValue<Store, P>[string]> : DotNestedValue<Store, P> extends Array<AcceptableGroupRequestType<Store>> ? NonNullable<DotNestedValue<Store, P>[number]> : DotNestedValue<Store, P> >( store: UseBoundStore<StoreApi<Store>>, path: P extends string ? Result extends void ? P : DotNestedValue<Store, P> extends Record<string, Result> | Array<Result> ? P : never : never, request: (params: ILeitenGroupRequestParams<void>) => Promise<Result>, options?: DotNestedValue<Store, P> extends Record< string, AcceptableGroupRequestType<Store> > ? ILeitenGroupRequestOption<void, Result> : ILeitenGroupRequestArrayOption<void, Result> ) => { const leiten = leitenGroupRequest(store, path, request, { ...options, action: (args) => { const key = args.payload.key; updatePrevFilters(key); return options?.action?.(args); }, } as DotNestedValue<Store, P> extends Record<string, AcceptableGroupRequestType<Store>> ? ILeitenGroupRequestOption<void, Result> : ILeitenGroupRequestArrayOption<void, Result>); const filters: Record<string, IGroupRecord<any>> = {}; const prevFilters: Record<string, Record<string, any>> = {}; const createFilter = <Path extends DotNestedKeys<Store>>( path: Path extends string ? DotNestedValue<Store, Path> extends Record<string, unknown> ? Path : never : never, options: ILeitenRecordEffects< ValueOf<DotNestedValue<Store, Path>>, Store > & { initialValue: ValueOf<DotNestedValue<Store, Path>>; } ) => { prevFilters[path] = {}; type VALUE = ValueOf<DotNestedValue<Store, Path>>; function hook( key: string, referenceObject?: VALUE ):
IObjectDifferent<VALUE>[] {
return store((state) => getObjectDifference(get(state, `${path}.${key}`), referenceObject) ); } const getState = (key: string): VALUE | undefined => { return get(store.getState(), `${path}.${key}`, undefined); }; const setState = (key: string, next: VALUE) => { const prev = getState(key); const draftState = produce(store.getState(), (draft) => { set(draft, `${path}.${key}`, next); }); const nextState = options.patchEffect ? { ...draftState, ...options.patchEffect(next) } : draftState; store.setState(nextState); options.sideEffect?.({ prev: prev || options.initialValue, next }); action(key, path, options.initialValue); }; const clear = (key: string) => { setState(key, options.initialValue); }; const patch = (key: string, value: Partial<VALUE>) => { const prev: object = (getState(key) || options.initialValue) as any; setState(key, { ...prev, ...value } as VALUE); }; const record = { clear, patch, set: setState, get: getState }; filters[path] = record; return Object.assign(hook, record); }; const action = async (key: string, path: string, initialValue: any) => { const nextFilters = get(store.getState(), `${path}.${key}`); if ( JSON.stringify(nextFilters) !== JSON.stringify(prevFilters[path][key] || initialValue) ) { leiten.action([{ key, params: undefined }]); } }; const updatePrevFilters = (key: string) => { Object.keys(filters).forEach((item) => { prevFilters[item][key] = get(store.getState(), `${item}.${key}`); }); }; resettableStoreSubscription(store, () => { Object.keys(filters).forEach((path) => { prevFilters[path] = {}; }); }); const clearAll = () => { leiten.clear(); const keys = Object.keys(leiten.requests); Object.keys(filters).forEach((key) => keys.forEach((k) => filters[key]?.clear(k)) ); }; return Object.assign(leiten, { createFilter, clearAll }); }; interface IGroupRecord<VALUE> { clear: (key: string) => void; patch: (key: string, value: Partial<VALUE>) => void; set: (key: string, next: VALUE) => void; get: (key: string) => VALUE | undefined; }
src/helpers/leitenGroupFilterRequest.ts
Hecmatyar-leiten-zustand-0248dac
[ { "filename": "src/helpers/leitenFilterRequest.ts", "retrieved_chunk": " path: Path extends string ? Path : never,\n options?: ILeitenRecordEffects<DotNestedValue<Store, Path>, Store>\n ): DotNestedValue<Store, Path> extends object\n ? RecordFilter<DotNestedValue<Store, Path>>\n : PrimitiveFilter<DotNestedValue<Store, Path>> => {\n type Response = DotNestedValue<Store, Path> extends object\n ? RecordFilter<DotNestedValue<Store, Path>>\n : PrimitiveFilter<DotNestedValue<Store, Path>>;\n const initial = get(store.getState(), path, undefined);\n function hook(", "score": 34.16369455753257 }, { "filename": "src/helpers/leitenPrimitive.ts", "retrieved_chunk": "): ILeitenPrimitive<DotNestedValue<Store, P>> => {\n type VALUE = DotNestedValue<Store, P>;\n const initialValue = get(store.getState(), path, \"_empty\") as VALUE;\n if (initialValue === \"_empty\") {\n throw new Error(\"[leitenPrimitive] The defined path does not exist\");\n }\n const getState = (): VALUE => {\n const value = get(store.getState(), path, \"_empty\") as VALUE | \"_empty\";\n return value !== \"_empty\" ? value : initialValue;\n };", "score": 27.807586689399013 }, { "filename": "src/helpers/leitenNormalizedList.ts", "retrieved_chunk": " left: ValueOf<DotNestedValue<Store, P>>,\n right: ValueOf<DotNestedValue<Store, P>>\n ) => boolean;\n sideEffect?: () => void;\n patchEffect?: (\n items: NormalizedType<ValueOf<DotNestedValue<Store, P>>>\n ) => Partial<Store>;\n getKey: (item: ValueOf<DotNestedValue<Store, P>>) => string;\n }\n): ILeitenList<ValueOf<DotNestedValue<Store, P>>> & {", "score": 27.27390170597644 }, { "filename": "src/helpers/leitenNormalizedList.ts", "retrieved_chunk": " removeByKey: (value: string | string[]) => void;\n get: () => NormalizedType<ValueOf<DotNestedValue<Store, P>>>;\n} => {\n type ITEM = ValueOf<DotNestedValue<Store, P>>;\n const initialValue = get(\n store.getState(),\n path,\n \"_empty\"\n ) as NormalizedType<ITEM>;\n if ((initialValue as any) === \"_empty\" || typeof initialValue !== \"object\") {", "score": 26.32612928365518 }, { "filename": "src/helpers/leitenFilterRequest.ts", "retrieved_chunk": " (initialObject?: Y): IObjectDifferent[];\n} & ILeitenPrimitive<Y>;\nexport const leitenFilterRequest = <\n Store extends object,\n Path extends DotNestedKeys<Store>,\n Result extends DotNestedValue<Store, Path> | null | void\n>(\n store: UseBoundStore<StoreApi<Store>>,\n path: Path extends string\n ? Result extends void", "score": 24.100571449606107 } ]
typescript
IObjectDifferent<VALUE>[] {
import { produce } from "immer"; import { get, isEqual, set } from "lodash-es"; import { nanoid } from "nanoid"; import { useEffect, useState } from "react"; import { StoreApi } from "zustand"; import { shallow } from "zustand/shallow"; import { useLeitenRequests } from "../hooks/useLeitenRequest"; import { DotNestedKeys, DotNestedValue } from "../interfaces/dotNestedKeys"; import { ILeitenLoading, ILoadingStatus, initialLeitenLoading, } from "../interfaces/IContentLoading"; /* eslint-disable @typescript-eslint/no-explicit-any */ export type UseRequestType<Payload, Result> = < U = ILeitenLoading<Payload, Result> >( selector?: (state: ILeitenLoading<Payload, Result>) => U, equals?: (a: U, b: U) => boolean ) => U; export interface ILeitenRequest<Payload, Result> extends UseRequestType<Payload, Result> { abort: () => void; clear: () => void; action: ( params: Payload, extraParams?: { status?: ILoadingStatus; requestId?: string } ) => void; set: (value: Partial<Result> | void, rewrite?: boolean) => void; key: string; get: () => ILeitenLoading<Payload, Result>; } export interface ILeitenRequestCallback<Payload, Result> { previousResult: Result; result: Result; payload: Payload; requestId: string; error?: string; } export interface ILeitenRequestOptions<Payload, Result> { fulfilled?: ( options: Omit<ILeitenRequestCallback<Payload, Result>, "error"> ) => void; rejected?: ( options: Omit<ILeitenRequestCallback<Payload, Result>, "result"> ) => void; abort?: ( options: Omit<ILeitenRequestCallback<Payload, Result>, "error" | "result"> ) => void; resolved?: ( options: Omit<ILeitenRequestCallback<Payload, Result>, "result" | "error"> ) => void; action?: ( options: Omit<ILeitenRequestCallback<Payload, Result>, "error" | "result"> ) => void; initialStatus?: ILoadingStatus; optimisticUpdate?: (params: Payload) => Result; } export const leitenRequest = < Store extends object, P extends DotNestedKeys<Store>, Payload, Result extends DotNestedValue<Store, P> | null | void >( store: StoreApi<Store>, path: P extends string ? Result extends void ? P : DotNestedValue<Store, P> extends Result | null ? P : never : never, payloadCreator: ( params: Payload, extraArgument?: IExtraArgument ) => Promise<Result>, options?: ILeitenRequestOptions<Payload, Result> ): ILeitenRequest<Payload, Result> => { const key = nanoid(12); const initialState = initialLeitenLoading<Payload, Result>( options?.initialStatus ); const initialContent = get(store.getState(), path, null) as Result; const setState = (state: ILeitenLoading<Payload, Result>) => { useLeitenRequests.setState({ [key]: state }); }; setState(initialState); //init request const setContent = (content: Result) => { const nextState = produce(store.getState(), (draft) => { set(draft, path, content); }); store.setState(nextState); }; const getState = (): ILeitenLoading<Payload, Result> => { return useLeitenRequests.getState()[key] || initialState; }; const getContent = (): Result => { const result = get(store.getState(), path, "_empty") as Result | "_empty"; if (result !== "_empty") { return result || initialContent; } else { return initialContent; } }; const _set = (value: Partial<Result> | void, rewrite = false) => { if (typeof value === "object") { const state = getContent(); const objectContent = rewrite ? ({ ...value } as Result) : ({ ...state, ...value } as Result); const content = typeof value === "object" ? objectContent : value; setContent(content); } else { value !== undefined && value !== null && setContent(value); } }; let previousResult: Result = getContent(); const reactions = { action: (payload: Payload, status?: ILoadingStatus, requestId?: string) => { setState({ status: status ?? "loading", payload, error: undefined, requestId: requestId, }); options?.action?.({ previousResult, requestId: requestId || "", payload, }); previousResult = getContent(); if (options?.optimisticUpdate) { setContent(options.optimisticUpdate(payload)); } }, fulfilled: (result: Result, payload: Payload, requestId: string) => { const state = getState(); if (requestId === state.requestId) { setState({ ...state, status: "loaded" }); if ( result !== undefined && (!options?.optimisticUpdate || !isEqual(previousResult, result)) ) { setContent(result); } options?.fulfilled?.({ previousResult, requestId, payload, result }); } }, rejected: (payload: Payload, error: string, requestId?: string) => { const state = getState(); setState({ ...state, status: "error", error }); options?.rejected?.({ previousResult, requestId: requestId || "", payload, error, }); if (options?.optimisticUpdate) { setContent(previousResult); } }, abort: (payload: Payload, requestId: string) => { setState(initialState); options?.abort?.({ previousResult, requestId, payload }); if (options?.optimisticUpdate) { setContent(previousResult); } }, resolved: (payload: Payload, requestId: string) => { options?.resolved?.({ previousResult, requestId, payload }); }, }; const { action, abort } = createAsyncActions(payloadCreator, reactions); const _abort = () => { abort(); }; const clear = () => { setState(initialState); setContent(initialContent); }; const usages: Record<string, boolean> = {}; const useRequest: UseRequestType<Payload, Result> = (selector, equals) => { const [id] = useState(() => nanoid()); useEffect(() => { usages[id] = true; return () => { usages[id] = false; }; }, []);
return useLeitenRequests( (state) => (selector || nonTypedReturn)(state[key] || initialState), shallow || equals );
}; resettableStoreSubscription(store, () => setState(initialState)); const _get = () => { return useLeitenRequests.getState()[key]; }; return Object.assign(useRequest, { abort: _abort, action, clear, set: _set, key, get: _get, _usages: usages, }); }; // eslint-disable-next-line @typescript-eslint/no-explicit-any const nonTypedReturn = (value: any) => value; export const resettableStoreSubscription = ( store: StoreApi<object>, callback: () => void ) => { setTimeout(() => { const resettable = (store.getState() as any)["_resettableLifeCycle"] !== undefined; if (resettable) { store.subscribe((next, prev) => { if ( (next as any)["_resettableLifeCycle"] === false && (prev as any)["_resettableLifeCycle"] === true ) callback(); }); } }, 0); }; function createAsyncActions<Payload, Result>( payloadCreator: ( params: Payload, extraArgument?: IExtraArgument ) => Promise<Result>, extra?: IReaction<Payload, Result> ) { let controller = new AbortController(); let signal = controller.signal; const abort = () => { controller.abort(); controller = new AbortController(); signal = controller.signal; }; const action = ( params: Payload, options?: { status?: ILoadingStatus; requestId?: string } ) => { const requestId = options?.requestId || nanoid(); extra?.action?.(params, options?.status, requestId); payloadCreator(params, { signal }) .then((result) => { extra?.fulfilled?.(result, params, requestId); }) .catch((error) => { if (error.message === "The user aborted a request.") { extra?.abort?.(params, requestId); } else { extra?.rejected?.(params, error, requestId); } }) .finally(() => { extra?.resolved?.(params, requestId); }); }; return { action, abort }; } interface IReaction<Payload, Result> { fulfilled?: (result: Result, params: Payload, requestId: string) => void; rejected?: (params: Payload, error: string, requestId?: string) => void; abort?: (params: Payload, requestId: string) => void; resolved?: (params: Payload, requestId: string) => void; action?: ( params: Payload, status?: ILoadingStatus, requestId?: string ) => void; } export type IExtraArgument = { signal: AbortSignal; // requestId: string };
src/helpers/leitenRequest.ts
Hecmatyar-leiten-zustand-0248dac
[ { "filename": "src/helpers/leitenGroupRequest.ts", "retrieved_chunk": " const useRequest: UseRequestType<Payload, Result> = (\n key,\n selector,\n equals\n ) => {\n return useLeitenRequests((state) => {\n const id = requests[key]?.key;\n return (selector || nonTypedReturn)(\n (id && state[id]) || initialRequestState\n );", "score": 33.860516971160116 }, { "filename": "src/helpers/leitenGroupRequest.ts", "retrieved_chunk": " key: value.key,\n }));\n const requestsStore: typeof state = keys.reduce((acc, { id, key }) => {\n return Object.assign(acc, { [id]: state[key] });\n }, {} as typeof state);\n return (selector || nonTypedReturn)(requestsStore);\n },\n shallow || equals\n );\n };", "score": 30.51424609151845 }, { "filename": "src/helpers/leitenGroupRequest.ts", "retrieved_chunk": " }, shallow || equals);\n };\n const useGroupRequest: UseGroupRequestType<Payload, Result> = (\n selector,\n equals\n ) => {\n return useLeitenRequests(\n (state: Record<string, LeitenState<Payload, Result>>) => {\n const keys = Object.entries(requests).map(([id, value]) => ({\n id,", "score": 27.066775973560503 }, { "filename": "src/store/resettableStore.tsx", "retrieved_chunk": " const reset = { _resettableLifeCycle: true } as Store;\n useStore.setState(reset);\n return () => {\n useStore.setState(\n { ...(getState?.() || initialState), _resettableLifeCycle: false },\n true\n );\n };\n }, []);\n return <>{children}</>;", "score": 18.314868046435897 }, { "filename": "src/examples/controllers/6_Controller_GroupRequest.tsx", "retrieved_chunk": "};\nconst Card = ({ id }: { id: string }) => {\n const content = useExampleStore((state) => state.cards[id]);\n const status = useGroupController(id, (state) => state.status);\n return (\n <>\n {status === \"waiting\" ? (\n \"waiting...\"\n ) : status === \"init\" ? (\n <>init</>", "score": 17.26180199392694 } ]
typescript
return useLeitenRequests( (state) => (selector || nonTypedReturn)(state[key] || initialState), shallow || equals );
import { produce } from "immer"; import { get, isEqual, set } from "lodash-es"; import { nanoid } from "nanoid"; import { useEffect, useState } from "react"; import { StoreApi } from "zustand"; import { shallow } from "zustand/shallow"; import { useLeitenRequests } from "../hooks/useLeitenRequest"; import { DotNestedKeys, DotNestedValue } from "../interfaces/dotNestedKeys"; import { ILeitenLoading, ILoadingStatus, initialLeitenLoading, } from "../interfaces/IContentLoading"; /* eslint-disable @typescript-eslint/no-explicit-any */ export type UseRequestType<Payload, Result> = < U = ILeitenLoading<Payload, Result> >( selector?: (state: ILeitenLoading<Payload, Result>) => U, equals?: (a: U, b: U) => boolean ) => U; export interface ILeitenRequest<Payload, Result> extends UseRequestType<Payload, Result> { abort: () => void; clear: () => void; action: ( params: Payload, extraParams?: { status?: ILoadingStatus; requestId?: string } ) => void; set: (value: Partial<Result> | void, rewrite?: boolean) => void; key: string; get: () => ILeitenLoading<Payload, Result>; } export interface ILeitenRequestCallback<Payload, Result> { previousResult: Result; result: Result; payload: Payload; requestId: string; error?: string; } export interface ILeitenRequestOptions<Payload, Result> { fulfilled?: ( options: Omit<ILeitenRequestCallback<Payload, Result>, "error"> ) => void; rejected?: ( options: Omit<ILeitenRequestCallback<Payload, Result>, "result"> ) => void; abort?: ( options: Omit<ILeitenRequestCallback<Payload, Result>, "error" | "result"> ) => void; resolved?: ( options: Omit<ILeitenRequestCallback<Payload, Result>, "result" | "error"> ) => void; action?: ( options: Omit<ILeitenRequestCallback<Payload, Result>, "error" | "result"> ) => void; initialStatus?: ILoadingStatus; optimisticUpdate?: (params: Payload) => Result; } export const leitenRequest = < Store extends object, P extends DotNestedKeys<Store>, Payload, Result extends DotNestedValue<Store, P> | null | void >( store: StoreApi<Store>, path: P extends string ? Result extends void ? P : DotNestedValue<Store, P> extends Result | null ? P : never : never, payloadCreator: ( params: Payload, extraArgument?: IExtraArgument ) => Promise<Result>, options?: ILeitenRequestOptions<Payload, Result> ): ILeitenRequest<Payload, Result> => { const key = nanoid(12); const initialState = initialLeitenLoading<Payload, Result>( options?.initialStatus ); const initialContent = get(store.getState(), path, null) as Result; const setState = (state: ILeitenLoading<Payload, Result>) => { useLeitenRequests.setState({ [key]: state }); }; setState(initialState); //init request const setContent = (content: Result) => { const nextState = produce(store.getState(), (draft) => { set(draft, path, content); }); store.setState(nextState); }; const getState = (): ILeitenLoading<Payload, Result> => { return useLeitenRequests.getState()[key] || initialState; }; const getContent = (): Result => { const result = get(store.getState(), path, "_empty") as Result | "_empty"; if (result !== "_empty") { return result || initialContent; } else { return initialContent; } }; const _set = (value: Partial<Result> | void, rewrite = false) => { if (typeof value === "object") { const state = getContent(); const objectContent = rewrite ? ({ ...value } as Result) : ({ ...state, ...value } as Result); const content = typeof value === "object" ? objectContent : value; setContent(content); } else { value !== undefined && value !== null && setContent(value); } }; let previousResult: Result = getContent(); const reactions = { action: (payload: Payload, status?: ILoadingStatus, requestId?: string) => { setState({ status: status ?? "loading", payload, error: undefined, requestId: requestId, }); options?.action?.({ previousResult, requestId: requestId || "", payload, }); previousResult = getContent(); if (options?.optimisticUpdate) { setContent(options.optimisticUpdate(payload)); } }, fulfilled: (result: Result, payload: Payload, requestId: string) => { const state = getState(); if (requestId === state.requestId) { setState({ ...state, status: "loaded" }); if ( result !== undefined && (!options?.optimisticUpdate || !isEqual(previousResult, result)) ) { setContent(result); } options?.fulfilled?.({ previousResult, requestId, payload, result }); } }, rejected: (payload: Payload, error: string, requestId?: string) => { const state = getState(); setState({ ...state, status: "error", error }); options?.rejected?.({ previousResult, requestId: requestId || "", payload, error, }); if (options?.optimisticUpdate) { setContent(previousResult); } }, abort: (payload: Payload, requestId: string) => { setState(initialState); options?.abort?.({ previousResult, requestId, payload }); if (options?.optimisticUpdate) { setContent(previousResult); } }, resolved: (payload: Payload, requestId: string) => { options?.resolved?.({ previousResult, requestId, payload }); }, }; const { action, abort } = createAsyncActions(payloadCreator, reactions); const _abort = () => { abort(); }; const clear = () => { setState(initialState); setContent(initialContent); }; const usages: Record<string, boolean> = {}; const useRequest: UseRequestType<Payload, Result> = (selector, equals) => { const [id] = useState(() => nanoid()); useEffect(() => { usages[id] = true; return () => { usages[id] = false; }; }, []); return useLeitenRequests(
(state) => (selector || nonTypedReturn)(state[key] || initialState), shallow || equals );
}; resettableStoreSubscription(store, () => setState(initialState)); const _get = () => { return useLeitenRequests.getState()[key]; }; return Object.assign(useRequest, { abort: _abort, action, clear, set: _set, key, get: _get, _usages: usages, }); }; // eslint-disable-next-line @typescript-eslint/no-explicit-any const nonTypedReturn = (value: any) => value; export const resettableStoreSubscription = ( store: StoreApi<object>, callback: () => void ) => { setTimeout(() => { const resettable = (store.getState() as any)["_resettableLifeCycle"] !== undefined; if (resettable) { store.subscribe((next, prev) => { if ( (next as any)["_resettableLifeCycle"] === false && (prev as any)["_resettableLifeCycle"] === true ) callback(); }); } }, 0); }; function createAsyncActions<Payload, Result>( payloadCreator: ( params: Payload, extraArgument?: IExtraArgument ) => Promise<Result>, extra?: IReaction<Payload, Result> ) { let controller = new AbortController(); let signal = controller.signal; const abort = () => { controller.abort(); controller = new AbortController(); signal = controller.signal; }; const action = ( params: Payload, options?: { status?: ILoadingStatus; requestId?: string } ) => { const requestId = options?.requestId || nanoid(); extra?.action?.(params, options?.status, requestId); payloadCreator(params, { signal }) .then((result) => { extra?.fulfilled?.(result, params, requestId); }) .catch((error) => { if (error.message === "The user aborted a request.") { extra?.abort?.(params, requestId); } else { extra?.rejected?.(params, error, requestId); } }) .finally(() => { extra?.resolved?.(params, requestId); }); }; return { action, abort }; } interface IReaction<Payload, Result> { fulfilled?: (result: Result, params: Payload, requestId: string) => void; rejected?: (params: Payload, error: string, requestId?: string) => void; abort?: (params: Payload, requestId: string) => void; resolved?: (params: Payload, requestId: string) => void; action?: ( params: Payload, status?: ILoadingStatus, requestId?: string ) => void; } export type IExtraArgument = { signal: AbortSignal; // requestId: string };
src/helpers/leitenRequest.ts
Hecmatyar-leiten-zustand-0248dac
[ { "filename": "src/helpers/leitenGroupRequest.ts", "retrieved_chunk": " const useRequest: UseRequestType<Payload, Result> = (\n key,\n selector,\n equals\n ) => {\n return useLeitenRequests((state) => {\n const id = requests[key]?.key;\n return (selector || nonTypedReturn)(\n (id && state[id]) || initialRequestState\n );", "score": 33.860516971160116 }, { "filename": "src/helpers/leitenGroupRequest.ts", "retrieved_chunk": " key: value.key,\n }));\n const requestsStore: typeof state = keys.reduce((acc, { id, key }) => {\n return Object.assign(acc, { [id]: state[key] });\n }, {} as typeof state);\n return (selector || nonTypedReturn)(requestsStore);\n },\n shallow || equals\n );\n };", "score": 30.51424609151845 }, { "filename": "src/helpers/leitenGroupRequest.ts", "retrieved_chunk": " }, shallow || equals);\n };\n const useGroupRequest: UseGroupRequestType<Payload, Result> = (\n selector,\n equals\n ) => {\n return useLeitenRequests(\n (state: Record<string, LeitenState<Payload, Result>>) => {\n const keys = Object.entries(requests).map(([id, value]) => ({\n id,", "score": 27.066775973560503 }, { "filename": "src/store/resettableStore.tsx", "retrieved_chunk": " const reset = { _resettableLifeCycle: true } as Store;\n useStore.setState(reset);\n return () => {\n useStore.setState(\n { ...(getState?.() || initialState), _resettableLifeCycle: false },\n true\n );\n };\n }, []);\n return <>{children}</>;", "score": 18.314868046435897 }, { "filename": "src/examples/controllers/6_Controller_GroupRequest.tsx", "retrieved_chunk": "};\nconst Card = ({ id }: { id: string }) => {\n const content = useExampleStore((state) => state.cards[id]);\n const status = useGroupController(id, (state) => state.status);\n return (\n <>\n {status === \"waiting\" ? (\n \"waiting...\"\n ) : status === \"init\" ? (\n <>init</>", "score": 17.26180199392694 } ]
typescript
(state) => (selector || nonTypedReturn)(state[key] || initialState), shallow || equals );
import { produce } from "immer"; import { get, isEqual, set } from "lodash-es"; import { nanoid } from "nanoid"; import { useEffect, useState } from "react"; import { StoreApi } from "zustand"; import { shallow } from "zustand/shallow"; import { useLeitenRequests } from "../hooks/useLeitenRequest"; import { DotNestedKeys, DotNestedValue } from "../interfaces/dotNestedKeys"; import { ILeitenLoading, ILoadingStatus, initialLeitenLoading, } from "../interfaces/IContentLoading"; /* eslint-disable @typescript-eslint/no-explicit-any */ export type UseRequestType<Payload, Result> = < U = ILeitenLoading<Payload, Result> >( selector?: (state: ILeitenLoading<Payload, Result>) => U, equals?: (a: U, b: U) => boolean ) => U; export interface ILeitenRequest<Payload, Result> extends UseRequestType<Payload, Result> { abort: () => void; clear: () => void; action: ( params: Payload, extraParams?: { status?: ILoadingStatus; requestId?: string } ) => void; set: (value: Partial<Result> | void, rewrite?: boolean) => void; key: string; get: () => ILeitenLoading<Payload, Result>; } export interface ILeitenRequestCallback<Payload, Result> { previousResult: Result; result: Result; payload: Payload; requestId: string; error?: string; } export interface ILeitenRequestOptions<Payload, Result> { fulfilled?: ( options: Omit<ILeitenRequestCallback<Payload, Result>, "error"> ) => void; rejected?: ( options: Omit<ILeitenRequestCallback<Payload, Result>, "result"> ) => void; abort?: ( options: Omit<ILeitenRequestCallback<Payload, Result>, "error" | "result"> ) => void; resolved?: ( options: Omit<ILeitenRequestCallback<Payload, Result>, "result" | "error"> ) => void; action?: ( options: Omit<ILeitenRequestCallback<Payload, Result>, "error" | "result"> ) => void; initialStatus?: ILoadingStatus; optimisticUpdate?: (params: Payload) => Result; } export const leitenRequest = < Store extends object, P extends DotNestedKeys<Store>, Payload, Result extends DotNestedValue<Store, P> | null | void >( store: StoreApi<Store>, path: P extends string ? Result extends void ? P : DotNestedValue<Store, P> extends Result | null ? P : never : never, payloadCreator: ( params: Payload, extraArgument?: IExtraArgument ) => Promise<Result>, options?: ILeitenRequestOptions<Payload, Result> ): ILeitenRequest<Payload, Result> => { const key = nanoid(12); const initialState = initialLeitenLoading<Payload, Result>( options?.initialStatus ); const initialContent = get(store.getState(), path, null) as Result; const setState = (state: ILeitenLoading<Payload, Result>) => { useLeitenRequests
.setState({ [key]: state });
}; setState(initialState); //init request const setContent = (content: Result) => { const nextState = produce(store.getState(), (draft) => { set(draft, path, content); }); store.setState(nextState); }; const getState = (): ILeitenLoading<Payload, Result> => { return useLeitenRequests.getState()[key] || initialState; }; const getContent = (): Result => { const result = get(store.getState(), path, "_empty") as Result | "_empty"; if (result !== "_empty") { return result || initialContent; } else { return initialContent; } }; const _set = (value: Partial<Result> | void, rewrite = false) => { if (typeof value === "object") { const state = getContent(); const objectContent = rewrite ? ({ ...value } as Result) : ({ ...state, ...value } as Result); const content = typeof value === "object" ? objectContent : value; setContent(content); } else { value !== undefined && value !== null && setContent(value); } }; let previousResult: Result = getContent(); const reactions = { action: (payload: Payload, status?: ILoadingStatus, requestId?: string) => { setState({ status: status ?? "loading", payload, error: undefined, requestId: requestId, }); options?.action?.({ previousResult, requestId: requestId || "", payload, }); previousResult = getContent(); if (options?.optimisticUpdate) { setContent(options.optimisticUpdate(payload)); } }, fulfilled: (result: Result, payload: Payload, requestId: string) => { const state = getState(); if (requestId === state.requestId) { setState({ ...state, status: "loaded" }); if ( result !== undefined && (!options?.optimisticUpdate || !isEqual(previousResult, result)) ) { setContent(result); } options?.fulfilled?.({ previousResult, requestId, payload, result }); } }, rejected: (payload: Payload, error: string, requestId?: string) => { const state = getState(); setState({ ...state, status: "error", error }); options?.rejected?.({ previousResult, requestId: requestId || "", payload, error, }); if (options?.optimisticUpdate) { setContent(previousResult); } }, abort: (payload: Payload, requestId: string) => { setState(initialState); options?.abort?.({ previousResult, requestId, payload }); if (options?.optimisticUpdate) { setContent(previousResult); } }, resolved: (payload: Payload, requestId: string) => { options?.resolved?.({ previousResult, requestId, payload }); }, }; const { action, abort } = createAsyncActions(payloadCreator, reactions); const _abort = () => { abort(); }; const clear = () => { setState(initialState); setContent(initialContent); }; const usages: Record<string, boolean> = {}; const useRequest: UseRequestType<Payload, Result> = (selector, equals) => { const [id] = useState(() => nanoid()); useEffect(() => { usages[id] = true; return () => { usages[id] = false; }; }, []); return useLeitenRequests( (state) => (selector || nonTypedReturn)(state[key] || initialState), shallow || equals ); }; resettableStoreSubscription(store, () => setState(initialState)); const _get = () => { return useLeitenRequests.getState()[key]; }; return Object.assign(useRequest, { abort: _abort, action, clear, set: _set, key, get: _get, _usages: usages, }); }; // eslint-disable-next-line @typescript-eslint/no-explicit-any const nonTypedReturn = (value: any) => value; export const resettableStoreSubscription = ( store: StoreApi<object>, callback: () => void ) => { setTimeout(() => { const resettable = (store.getState() as any)["_resettableLifeCycle"] !== undefined; if (resettable) { store.subscribe((next, prev) => { if ( (next as any)["_resettableLifeCycle"] === false && (prev as any)["_resettableLifeCycle"] === true ) callback(); }); } }, 0); }; function createAsyncActions<Payload, Result>( payloadCreator: ( params: Payload, extraArgument?: IExtraArgument ) => Promise<Result>, extra?: IReaction<Payload, Result> ) { let controller = new AbortController(); let signal = controller.signal; const abort = () => { controller.abort(); controller = new AbortController(); signal = controller.signal; }; const action = ( params: Payload, options?: { status?: ILoadingStatus; requestId?: string } ) => { const requestId = options?.requestId || nanoid(); extra?.action?.(params, options?.status, requestId); payloadCreator(params, { signal }) .then((result) => { extra?.fulfilled?.(result, params, requestId); }) .catch((error) => { if (error.message === "The user aborted a request.") { extra?.abort?.(params, requestId); } else { extra?.rejected?.(params, error, requestId); } }) .finally(() => { extra?.resolved?.(params, requestId); }); }; return { action, abort }; } interface IReaction<Payload, Result> { fulfilled?: (result: Result, params: Payload, requestId: string) => void; rejected?: (params: Payload, error: string, requestId?: string) => void; abort?: (params: Payload, requestId: string) => void; resolved?: (params: Payload, requestId: string) => void; action?: ( params: Payload, status?: ILoadingStatus, requestId?: string ) => void; } export type IExtraArgument = { signal: AbortSignal; // requestId: string };
src/helpers/leitenRequest.ts
Hecmatyar-leiten-zustand-0248dac
[ { "filename": "src/helpers/leitenGroupRequest.ts", "retrieved_chunk": " : ILeitenGroupRequestArrayOption<Payload, Result>\n): ILeitenGroupRequest<Payload, Result> => {\n const initialRequestState = initialLeitenLoading<\n ILeitenGroupRequestParams<Payload>,\n Result\n >(options?.initialStatus);\n let requests: Record<\n string,\n ILeitenRequest<ILeitenGroupRequestParams<Payload>, Result>\n > = {};", "score": 54.75685417723341 }, { "filename": "src/interfaces/IContentLoading.ts", "retrieved_chunk": " status: initialStatus || \"loading\",\n error: undefined,\n payload: undefined,\n requestId: undefined,\n});\nexport type ILeitenLoading<Payload, Result> = Omit<\n IContentLoading<Result, Payload>,\n \"content\"\n>;\nexport const initialLeitenLoading = <Payload, Result>(", "score": 45.785107060064675 }, { "filename": "src/helpers/leitenGroupRequest.ts", "retrieved_chunk": " >;\n} & UseRequestType<Payload, Result> &\n UseGroupRequestType<Payload, Result>;\nexport interface ILeitenGroupRequestOption<Payload, Result>\n extends ILeitenRequestOptions<ILeitenGroupRequestParams<Payload>, Result> {\n initialContent?: Result | ((key: string) => Result);\n}\nexport interface ILeitenGroupRequestArrayOption<Payload, Result>\n extends ILeitenGroupRequestOption<Payload, Result> {\n getKey: (value: Result) => string;", "score": 45.45030386941742 }, { "filename": "src/helpers/leitenGroupRequest.ts", "retrieved_chunk": ") => U;\nexport type ILeitenGroupRequest<Payload, Result> = {\n clear: (key?: string) => void;\n action: (\n params: ILeitenGroupRequestParams<Payload>[],\n options?: IGroupCallOptions\n ) => void;\n requests: Record<\n string,\n ILeitenRequest<ILeitenGroupRequestParams<Payload>, Result>", "score": 39.77734512064103 }, { "filename": "src/helpers/leitenGroupRequest.ts", "retrieved_chunk": " function hook<Payload, Result, U = LeitenState<Payload, Result>>(\n key: string,\n selector?: (state: LeitenState<Payload, Result>) => U,\n equals?: (a: U, b: U) => boolean\n ): U;\n function hook<Payload, Result, U = LeitenState<Payload, Result>>(\n selector?: (state: Record<string, LeitenState<Payload, Result>>) => U,\n equals?: (a: U, b: U) => boolean\n ): U;\n function hook<Payload, Result, U = LeitenState<Payload, Result>>(", "score": 39.37532319650741 } ]
typescript
.setState({ [key]: state });
import { produce } from "immer"; import { get, set } from "lodash-es"; import { StoreApi } from "zustand"; import { UseBoundStore } from "zustand/esm"; import { DotNestedKeys, DotNestedValue, ValueOf, } from "../interfaces/dotNestedKeys"; import { getObjectDifference, IObjectDifferent } from "./leitenFilterRequest"; import { AcceptableGroupRequestType, ILeitenGroupRequestArrayOption, ILeitenGroupRequestOption, ILeitenGroupRequestParams, leitenGroupRequest, } from "./leitenGroupRequest"; import { ILeitenRecordEffects } from "./leitenRecord"; import { resettableStoreSubscription } from "./leitenRequest"; /* eslint-disable @typescript-eslint/no-explicit-any */ export const leitenGroupFilterRequest = < Store extends object, P extends DotNestedKeys<Store>, Result extends DotNestedValue<Store, P> extends Record< string, AcceptableGroupRequestType<Store> > ? NonNullable<DotNestedValue<Store, P>[string]> : DotNestedValue<Store, P> extends Array<AcceptableGroupRequestType<Store>> ? NonNullable<DotNestedValue<Store, P>[number]> : DotNestedValue<Store, P> >( store: UseBoundStore<StoreApi<Store>>, path: P extends string ? Result extends void ? P : DotNestedValue<Store, P> extends Record<string, Result> | Array<Result> ? P : never : never, request: (params: ILeitenGroupRequestParams<void>) => Promise<Result>, options?: DotNestedValue<Store, P> extends Record< string, AcceptableGroupRequestType<Store> > ? ILeitenGroupRequestOption<void, Result> : ILeitenGroupRequestArrayOption<void, Result> ) => { const leiten = leitenGroupRequest(store, path, request, { ...options, action: (args) => { const key = args.payload.key; updatePrevFilters(key); return options?.action?.(args); }, } as DotNestedValue<Store, P> extends Record<string, AcceptableGroupRequestType<Store>> ? ILeitenGroupRequestOption<void, Result> : ILeitenGroupRequestArrayOption<void, Result>); const filters: Record<string, IGroupRecord<any>> = {}; const prevFilters: Record<string, Record<string, any>> = {}; const createFilter = <Path extends DotNestedKeys<Store>>( path: Path extends string ? DotNestedValue<Store, Path> extends Record<string, unknown> ? Path : never : never, options:
ILeitenRecordEffects< ValueOf<DotNestedValue<Store, Path>>, Store > & {
initialValue: ValueOf<DotNestedValue<Store, Path>>; } ) => { prevFilters[path] = {}; type VALUE = ValueOf<DotNestedValue<Store, Path>>; function hook( key: string, referenceObject?: VALUE ): IObjectDifferent<VALUE>[] { return store((state) => getObjectDifference(get(state, `${path}.${key}`), referenceObject) ); } const getState = (key: string): VALUE | undefined => { return get(store.getState(), `${path}.${key}`, undefined); }; const setState = (key: string, next: VALUE) => { const prev = getState(key); const draftState = produce(store.getState(), (draft) => { set(draft, `${path}.${key}`, next); }); const nextState = options.patchEffect ? { ...draftState, ...options.patchEffect(next) } : draftState; store.setState(nextState); options.sideEffect?.({ prev: prev || options.initialValue, next }); action(key, path, options.initialValue); }; const clear = (key: string) => { setState(key, options.initialValue); }; const patch = (key: string, value: Partial<VALUE>) => { const prev: object = (getState(key) || options.initialValue) as any; setState(key, { ...prev, ...value } as VALUE); }; const record = { clear, patch, set: setState, get: getState }; filters[path] = record; return Object.assign(hook, record); }; const action = async (key: string, path: string, initialValue: any) => { const nextFilters = get(store.getState(), `${path}.${key}`); if ( JSON.stringify(nextFilters) !== JSON.stringify(prevFilters[path][key] || initialValue) ) { leiten.action([{ key, params: undefined }]); } }; const updatePrevFilters = (key: string) => { Object.keys(filters).forEach((item) => { prevFilters[item][key] = get(store.getState(), `${item}.${key}`); }); }; resettableStoreSubscription(store, () => { Object.keys(filters).forEach((path) => { prevFilters[path] = {}; }); }); const clearAll = () => { leiten.clear(); const keys = Object.keys(leiten.requests); Object.keys(filters).forEach((key) => keys.forEach((k) => filters[key]?.clear(k)) ); }; return Object.assign(leiten, { createFilter, clearAll }); }; interface IGroupRecord<VALUE> { clear: (key: string) => void; patch: (key: string, value: Partial<VALUE>) => void; set: (key: string, next: VALUE) => void; get: (key: string) => VALUE | undefined; }
src/helpers/leitenGroupFilterRequest.ts
Hecmatyar-leiten-zustand-0248dac
[ { "filename": "src/helpers/leitenFilterRequest.ts", "retrieved_chunk": " path: Path extends string ? Path : never,\n options?: ILeitenRecordEffects<DotNestedValue<Store, Path>, Store>\n ): DotNestedValue<Store, Path> extends object\n ? RecordFilter<DotNestedValue<Store, Path>>\n : PrimitiveFilter<DotNestedValue<Store, Path>> => {\n type Response = DotNestedValue<Store, Path> extends object\n ? RecordFilter<DotNestedValue<Store, Path>>\n : PrimitiveFilter<DotNestedValue<Store, Path>>;\n const initial = get(store.getState(), path, undefined);\n function hook(", "score": 56.78262283052826 }, { "filename": "src/helpers/leitenFilterRequest.ts", "retrieved_chunk": " ? Path\n : DotNestedValue<Store, Path> extends Result | null\n ? Path\n : never\n : never,\n request: (params: void, extraArgument?: IExtraArgument) => Promise<Result>,\n options?: ILeitenRequestOptions<void, Result>\n) => {\n const leiten = leitenRequest(store, path, request, {\n ...options,", "score": 44.15876577813622 }, { "filename": "src/helpers/leitenFilterRequest.ts", "retrieved_chunk": " (initialObject?: Y): IObjectDifferent[];\n} & ILeitenPrimitive<Y>;\nexport const leitenFilterRequest = <\n Store extends object,\n Path extends DotNestedKeys<Store>,\n Result extends DotNestedValue<Store, Path> | null | void\n>(\n store: UseBoundStore<StoreApi<Store>>,\n path: Path extends string\n ? Result extends void", "score": 41.40974245442587 }, { "filename": "src/interfaces/dotNestedKeys.ts", "retrieved_chunk": "> = Path extends `${infer Head}.${infer Tail}`\n ? DotNestedValue<O[Head], Tail>\n : O[Path];\nexport type ValueOf<T> = T extends Record<infer _K, infer V> ? V : never;", "score": 38.709367141028736 }, { "filename": "src/helpers/leitenNormalizedList.ts", "retrieved_chunk": " P extends DotNestedKeys<Store>\n>(\n store: StoreApi<Store>,\n path: P extends string\n ? DotNestedValue<Store, P> extends Record<string, unknown>\n ? P\n : never\n : never,\n params: {\n compare?: (", "score": 36.948992859571746 } ]
typescript
ILeitenRecordEffects< ValueOf<DotNestedValue<Store, Path>>, Store > & {
import { produce } from "immer"; import { get, isEqual, set } from "lodash-es"; import { nanoid } from "nanoid"; import { useEffect, useState } from "react"; import { StoreApi } from "zustand"; import { shallow } from "zustand/shallow"; import { useLeitenRequests } from "../hooks/useLeitenRequest"; import { DotNestedKeys, DotNestedValue } from "../interfaces/dotNestedKeys"; import { ILeitenLoading, ILoadingStatus, initialLeitenLoading, } from "../interfaces/IContentLoading"; /* eslint-disable @typescript-eslint/no-explicit-any */ export type UseRequestType<Payload, Result> = < U = ILeitenLoading<Payload, Result> >( selector?: (state: ILeitenLoading<Payload, Result>) => U, equals?: (a: U, b: U) => boolean ) => U; export interface ILeitenRequest<Payload, Result> extends UseRequestType<Payload, Result> { abort: () => void; clear: () => void; action: ( params: Payload, extraParams?: { status?: ILoadingStatus; requestId?: string } ) => void; set: (value: Partial<Result> | void, rewrite?: boolean) => void; key: string; get: () => ILeitenLoading<Payload, Result>; } export interface ILeitenRequestCallback<Payload, Result> { previousResult: Result; result: Result; payload: Payload; requestId: string; error?: string; } export interface ILeitenRequestOptions<Payload, Result> { fulfilled?: ( options: Omit<ILeitenRequestCallback<Payload, Result>, "error"> ) => void; rejected?: ( options: Omit<ILeitenRequestCallback<Payload, Result>, "result"> ) => void; abort?: ( options: Omit<ILeitenRequestCallback<Payload, Result>, "error" | "result"> ) => void; resolved?: ( options: Omit<ILeitenRequestCallback<Payload, Result>, "result" | "error"> ) => void; action?: ( options: Omit<ILeitenRequestCallback<Payload, Result>, "error" | "result"> ) => void; initialStatus?: ILoadingStatus; optimisticUpdate?: (params: Payload) => Result; } export const leitenRequest = < Store extends object, P extends DotNestedKeys<Store>, Payload, Result extends DotNestedValue<Store, P> | null | void >( store: StoreApi<Store>, path: P extends string ? Result extends void ? P : DotNestedValue<Store, P> extends Result | null ? P : never : never, payloadCreator: ( params: Payload, extraArgument?: IExtraArgument ) => Promise<Result>, options?: ILeitenRequestOptions<Payload, Result> ): ILeitenRequest<Payload, Result> => { const key = nanoid(12); const initialState = initialLeitenLoading<Payload, Result>( options?.initialStatus ); const initialContent = get(store.getState(), path, null) as Result; const setState = (state: ILeitenLoading<Payload, Result>) => { useLeitenRequests.setState({ [key]: state }); }; setState(initialState); //init request const setContent = (content: Result) => { const nextState = produce(store.getState(), (draft) => { set(draft, path, content); }); store.setState(nextState); }; const getState = (): ILeitenLoading<Payload, Result> => { return useLeitenRequests.getState()[key] || initialState; }; const getContent = (): Result => { const result = get(store.getState(), path, "_empty") as Result | "_empty"; if (result !== "_empty") { return result || initialContent; } else { return initialContent; } }; const _set = (value: Partial<Result> | void, rewrite = false) => { if (typeof value === "object") { const state = getContent(); const objectContent = rewrite ? ({ ...value } as Result) : ({
...state, ...value } as Result);
const content = typeof value === "object" ? objectContent : value; setContent(content); } else { value !== undefined && value !== null && setContent(value); } }; let previousResult: Result = getContent(); const reactions = { action: (payload: Payload, status?: ILoadingStatus, requestId?: string) => { setState({ status: status ?? "loading", payload, error: undefined, requestId: requestId, }); options?.action?.({ previousResult, requestId: requestId || "", payload, }); previousResult = getContent(); if (options?.optimisticUpdate) { setContent(options.optimisticUpdate(payload)); } }, fulfilled: (result: Result, payload: Payload, requestId: string) => { const state = getState(); if (requestId === state.requestId) { setState({ ...state, status: "loaded" }); if ( result !== undefined && (!options?.optimisticUpdate || !isEqual(previousResult, result)) ) { setContent(result); } options?.fulfilled?.({ previousResult, requestId, payload, result }); } }, rejected: (payload: Payload, error: string, requestId?: string) => { const state = getState(); setState({ ...state, status: "error", error }); options?.rejected?.({ previousResult, requestId: requestId || "", payload, error, }); if (options?.optimisticUpdate) { setContent(previousResult); } }, abort: (payload: Payload, requestId: string) => { setState(initialState); options?.abort?.({ previousResult, requestId, payload }); if (options?.optimisticUpdate) { setContent(previousResult); } }, resolved: (payload: Payload, requestId: string) => { options?.resolved?.({ previousResult, requestId, payload }); }, }; const { action, abort } = createAsyncActions(payloadCreator, reactions); const _abort = () => { abort(); }; const clear = () => { setState(initialState); setContent(initialContent); }; const usages: Record<string, boolean> = {}; const useRequest: UseRequestType<Payload, Result> = (selector, equals) => { const [id] = useState(() => nanoid()); useEffect(() => { usages[id] = true; return () => { usages[id] = false; }; }, []); return useLeitenRequests( (state) => (selector || nonTypedReturn)(state[key] || initialState), shallow || equals ); }; resettableStoreSubscription(store, () => setState(initialState)); const _get = () => { return useLeitenRequests.getState()[key]; }; return Object.assign(useRequest, { abort: _abort, action, clear, set: _set, key, get: _get, _usages: usages, }); }; // eslint-disable-next-line @typescript-eslint/no-explicit-any const nonTypedReturn = (value: any) => value; export const resettableStoreSubscription = ( store: StoreApi<object>, callback: () => void ) => { setTimeout(() => { const resettable = (store.getState() as any)["_resettableLifeCycle"] !== undefined; if (resettable) { store.subscribe((next, prev) => { if ( (next as any)["_resettableLifeCycle"] === false && (prev as any)["_resettableLifeCycle"] === true ) callback(); }); } }, 0); }; function createAsyncActions<Payload, Result>( payloadCreator: ( params: Payload, extraArgument?: IExtraArgument ) => Promise<Result>, extra?: IReaction<Payload, Result> ) { let controller = new AbortController(); let signal = controller.signal; const abort = () => { controller.abort(); controller = new AbortController(); signal = controller.signal; }; const action = ( params: Payload, options?: { status?: ILoadingStatus; requestId?: string } ) => { const requestId = options?.requestId || nanoid(); extra?.action?.(params, options?.status, requestId); payloadCreator(params, { signal }) .then((result) => { extra?.fulfilled?.(result, params, requestId); }) .catch((error) => { if (error.message === "The user aborted a request.") { extra?.abort?.(params, requestId); } else { extra?.rejected?.(params, error, requestId); } }) .finally(() => { extra?.resolved?.(params, requestId); }); }; return { action, abort }; } interface IReaction<Payload, Result> { fulfilled?: (result: Result, params: Payload, requestId: string) => void; rejected?: (params: Payload, error: string, requestId?: string) => void; abort?: (params: Payload, requestId: string) => void; resolved?: (params: Payload, requestId: string) => void; action?: ( params: Payload, status?: ILoadingStatus, requestId?: string ) => void; } export type IExtraArgument = { signal: AbortSignal; // requestId: string };
src/helpers/leitenRequest.ts
Hecmatyar-leiten-zustand-0248dac
[ { "filename": "src/helpers/leitenGroupRequest.ts", "retrieved_chunk": " } else {\n return useGroupRequest(first as any, second as any) as any;\n }\n }\n resettableStoreSubscription(store, () => clear());\n return Object.assign(hook, { clear, action, requests, call: action });\n};\nconst nonTypedReturn = (value: any) => value;\nconst checkInitial = <Result>(\n value: Result | ((key: string) => Result)", "score": 26.234440913302922 }, { "filename": "src/helpers/leitenGroupRequest.ts", "retrieved_chunk": " key: value.key,\n }));\n const requestsStore: typeof state = keys.reduce((acc, { id, key }) => {\n return Object.assign(acc, { [id]: state[key] });\n }, {} as typeof state);\n return (selector || nonTypedReturn)(requestsStore);\n },\n shallow || equals\n );\n };", "score": 22.23818763158618 }, { "filename": "src/helpers/leitenGroupRequest.ts", "retrieved_chunk": "): value is (key: string) => Result => typeof value === \"function\";", "score": 21.872318303517172 }, { "filename": "src/helpers/leitenGroupRequest.ts", "retrieved_chunk": " }, shallow || equals);\n };\n const useGroupRequest: UseGroupRequestType<Payload, Result> = (\n selector,\n equals\n ) => {\n return useLeitenRequests(\n (state: Record<string, LeitenState<Payload, Result>>) => {\n const keys = Object.entries(requests).map(([id, value]) => ({\n id,", "score": 21.44992443277428 }, { "filename": "src/helpers/leitenRecord.ts", "retrieved_chunk": " const initialValue = get(store.getState(), path, \"_empty\") as VALUE;\n if (initialValue === \"_empty\" || typeof initialValue !== \"object\") {\n throw new Error(\n \"[leitenRecord] The defined path does not match the required structure\"\n );\n }\n const getState = (): VALUE => {\n const value = get(store.getState(), path, \"_empty\") as VALUE | \"_empty\";\n return value !== \"_empty\" ? value : initialValue;\n };", "score": 20.842275982888474 } ]
typescript
...state, ...value } as Result);
import type { IContext } from 'requete' import { RequestError } from './RequestError' export class Logger { constructor(private name: string, private level: number) { this.name = name && `[${name}]` } res(ctx: IContext) { const headers = (headers?: Headers) => { if (headers) { const obj: any = {} headers.forEach((value, key) => { obj[key] = value }) return obj } } return { request: { data: ctx.request.data, params: ctx.request.params, headers: headers(ctx.request.headers), }, response: { data: ctx.data, headers: headers(ctx.headers), responseText: ctx.responseText, }, } } info(...message: any[]) { if (this.level < 2) return console.log(this.name, ...message) } error(e: Error | string) { if (this.level < 1) return if (!(e instanceof RequestError)) return console.error(e) const { ctx } = e as RequestError const { request, url, status, statusText } = ctx console.error( `${this.name} ${request.method} ${url} ${status} (${ status === -1 ? 'Before Request' : statusText
})\n%o\n${e.stack}`, this.res(ctx) ) }
request(ctx: IContext) { this.info(`${ctx.request.method} ${ctx.url}`, this.res(ctx).request) } response(ctx: IContext) { this.info( `${ctx.request.method} ${ctx.url} ${ctx.status} (${ctx.statusText})`, this.res(ctx).response ) } }
src/shared/logger.ts
rexerwang-requete-e7d4979
[ { "filename": "src/shared/__tests__/logger.test.ts", "retrieved_chunk": " status: -1,\n })\n const error = new RequestError('error', ctx)\n logger.error(error)\n expect(errorSpy).toBeCalledWith(\n '[logger] GET /do-mock -1 (Before Request)\\n%o\\n' + error.stack,\n {\n request: {\n data: null,\n headers: {", "score": 56.1349279857099 }, { "filename": "src/core/Requete.ts", "retrieved_chunk": " this.request.timeout ?? 0\n )\n }\n return this.request.abort\n },\n throw(e) {\n if (e instanceof RequestError) throw e\n throw new RequestError(e, this)\n },\n assign(context) {", "score": 45.77969974405349 }, { "filename": "src/core/Requete.ts", "retrieved_chunk": " try {\n await compose(this.middlewares)(context, this.invoke.bind(this))\n if (!context.ok) {\n context.throw(\n `${context.request.method} ${context.url} ${context.status} (${context.statusText})`\n )\n }\n this.logger.response(context)\n return context\n } catch (e: any) {", "score": 35.63069554082573 }, { "filename": "src/core/__tests__/requete-exceptions.test.ts", "retrieved_chunk": " statusText: 'OK',\n url: '/do-mock',\n data: 'null',\n })\n )\n // disable console.error\n vi.spyOn(global.console, 'error').mockImplementation(toAny(vi.fn()))\n })\n it('should caught RequestError when response`s status != 200', async () => {\n vi.spyOn(FetchAdapter.prototype, 'request').mockImplementation(", "score": 33.81597525810007 }, { "filename": "src/core/Requete.ts", "retrieved_chunk": " this.logger.error(e)\n throw e\n } finally {\n context.request.abort?.clear()\n }\n }\n get<D = any>(url: string, config?: AliasConfig) {\n return this.request<D>({ ...config, url, method: 'GET' })\n }\n delete<D = any>(url: string, config?: AliasConfig) {", "score": 33.339913161095424 } ]
typescript
})\n%o\n${e.stack}`, this.res(ctx) ) }
import { FetchAdapter } from 'requete/adapter' import { RequestError } from 'requete/shared' import { toAny } from 'test/utils' import { Requete } from '../Requete' describe('Requete exceptions specs', () => { beforeEach(() => { vi.spyOn(FetchAdapter.prototype, 'request').mockImplementation( vi.fn().mockResolvedValue({ ok: true, status: 200, statusText: 'OK', url: '/do-mock', data: 'null', }) ) // disable console.error vi.spyOn(global.console, 'error').mockImplementation(toAny(vi.fn())) }) it('should caught RequestError when response`s status != 200', async () => { vi.spyOn(FetchAdapter.prototype, 'request').mockImplementation( vi.fn().mockResolvedValue({ ok: false, status: 500, statusText: 'Internal Server Error', url: '/do-mock', data: 'null', }) ) const requete = new Requete() await expect(requete.get('https:api.com/do-mock')).rejects.toThrow( RequestError ) await expect(requete.get('/do-mock')).rejects.toThrow( 'GET /do-mock 500 (Internal Server Error)' ) }) it('should caught RequestError when middleware throws', async () => { const requete = new
Requete().use(async (ctx, next) => {
if (ctx.request.method === 'GET') throw new Error('not allowed') await next() if (ctx.request.method === 'POST') throw new Error('post error') }) await expect(requete.get('/do-mock')).rejects.toThrow(RequestError) await expect(requete.get('/do-mock')).rejects.toThrow('not allowed') await expect(requete.post('/do-mock')).rejects.toThrow(RequestError) await expect(requete.post('/do-mock')).rejects.toThrow('post error') }) it('should caught when call ctx funcs in wrong way', async () => { // ctx.abort() await expect( new Requete() .use(async (ctx, next) => { await next() ctx.abort() }) .get('/do-mock') ).rejects.toThrow('Cannot set abortSignal after next().') // ctx.set() await expect( new Requete() .use(async (ctx, next) => { await next() ctx.set('a', 'b') }) .get('/do-mock') ).rejects.toThrow('Cannot set request headers after next().') }) it('should caught when request aborted', async () => { vi.spyOn(FetchAdapter.prototype, 'request').mockImplementation( async (ctx) => { const abort = ctx.abort() if (abort.signal.aborted) { throw new Error(abort.signal.reason) } return toAny({ ok: false, status: 500, statusText: 'Internal Server Error', url: '/do-mock', data: 'null', }) } ) const requete = new Requete().use(async (ctx, next) => { ctx.abort().abort('abort request') await next() }) await expect(requete.get('/do-mock', { timeout: 1000 })).rejects.toThrow( 'abort request' ) }) it('should caught RequestError when parse body with throws', async () => { vi.spyOn(FetchAdapter.prototype, 'request').mockImplementation( vi.fn().mockResolvedValue({ ok: true, status: 200, statusText: 'OK', url: '/do-mock', data: 'bad', }) ) const requete = new Requete() await expect(requete.get('/do-mock')).rejects.toThrow(RequestError) await expect(requete.get('/do-mock')).rejects.toThrow(/Unexpected token/) }) })
src/core/__tests__/requete-exceptions.test.ts
rexerwang-requete-e7d4979
[ { "filename": "src/core/__tests__/requete-middleware.test.ts", "retrieved_chunk": " await next()\n })\n await expect(requete.post('/do-mock')).rejects.toThrow(\n 'next() called multiple times'\n )\n })\n it('should set request header correctly in middleware', async () => {\n const requete = new Requete()\n requete.use(async (ctx, next) => {\n ctx.set('Authorization', 'mock')", "score": 58.00136122440267 }, { "filename": "src/core/__tests__/requete-request.test.ts", "retrieved_chunk": " )\n it('should make a request with correct url', async () => {\n const requete = new Requete({ baseURL: 'https://api.mock.com/api/v1/' })\n let ctx = await requete.post('https://api.mock.com/api/v2/do-mock?id=1')\n expect(ctx.request.url).toBe('https://api.mock.com/api/v2/do-mock?id=1')\n ctx = await requete.get('do-mock?id=1', { params: { id: '2' } })\n expect(ctx.request.url).toBe(\n 'https://api.mock.com/api/v1/do-mock?id=1&id=2'\n )\n ctx = await requete.get('do-mock', { params: 'id=2' })", "score": 51.6630322502446 }, { "filename": "src/core/__tests__/requete-middleware.test.ts", "retrieved_chunk": " expect(res.request.url).toBe('/do-mock?a=1&b=2')\n })\n it('should set abortSignal correctly in middleware', async () => {\n const requete = new Requete()\n let controller: any\n requete.use(async (ctx, next) => {\n controller = ctx.abort()\n await next()\n })\n const res = await requete.post('/do-mock', undefined)", "score": 44.963034495289776 }, { "filename": "src/core/__tests__/requete-middleware.test.ts", "retrieved_chunk": " })\n )\n })\n it('should set params correctly in middleware', async () => {\n const requete = new Requete()\n requete.use(async (ctx, next) => {\n ctx.params({ a: 1, b: 2 })\n await next()\n })\n const res = await requete.post('/do-mock', { params: { b: 1 } })", "score": 43.573434251373115 }, { "filename": "src/core/__tests__/requete-middleware.test.ts", "retrieved_chunk": " expect(res.request.abort).toEqual(controller)\n })\n it('should replay the request in middleware', async () => {\n const requete = new Requete()\n requete.use(async (ctx, next) => {\n await next()\n if (!ctx.request.custom?.replay) {\n await ctx.replay()\n }\n })", "score": 35.70277147855572 } ]
typescript
Requete().use(async (ctx, next) => {
import { Adapter, createAdapter } from 'requete/adapter' import { getUri, Logger, mergeHeaders, pick, RequestError, stringifyUrl, } from 'requete/shared' import { TimeoutAbortController } from './AbortController' import { compose } from './compose' export type Method = | 'GET' | 'DELETE' | 'HEAD' | 'OPTIONS' | 'POST' | 'PUT' | 'PATCH' export type RequestBody = | BodyInit | null | Record<string, any> | Record<string, any>[] export type RequestQueryRecord = Record< string, | string | number | boolean | null | undefined | Array<string | number | boolean> > export type RequestQuery = string | URLSearchParams | RequestQueryRecord export interface RequestConfig { baseURL?: string /** request timeout (ms) */ timeout?: number /** response body type */ responseType?: 'json' | 'formData' | 'text' | 'blob' | 'arrayBuffer' /** A string indicating how the request will interact with the browser's cache to set request's cache. */ cache?: RequestCache /** A string indicating whether credentials will be sent with the request always, never, or only when sent to a same-origin URL. Sets request's credentials. */ credentials?: RequestCredentials /** A Headers object, an object literal, or an array of two-item arrays to set request's headers. */ headers?: HeadersInit /** A cryptographic hash of the resource to be fetched by request. Sets request's integrity. */ integrity?: string /** A boolean to set request's keepalive. */ keepalive?: boolean /** A string to indicate whether the request will use CORS, or will be restricted to same-origin URLs. Sets request's mode. */ mode?: RequestMode /** A string indicating whether request follows redirects, results in an error upon encountering a redirect, or returns the redirect (in an opaque fashion). Sets request's redirect. */ redirect?: RequestRedirect /** A string whose value is a same-origin URL, "about:client", or the empty string, to set request's referrer. */ referrer?: string /** A referrer policy to set request's referrerPolicy. */ referrerPolicy?: ReferrerPolicy /** enable logger or set logger level # */ verbose?: boolean | number /** * parse json function * (for transform response) * @default JSON.parse */ toJSON?(body: string): any } export interface IRequest extends Omit<RequestConfig, 'verbose'> { url: string /** * A string to set request's method. * @default GET */ method?: Method /** A string or object to set querystring of url */ params?: RequestQuery /** request`s body */ data?: RequestBody /** * A TimeoutAbortController to set request's signal. * @default TimeoutAbortController */ abort?: TimeoutAbortController | null /** specify request adapter */ adapter?: Adapter /** flexible custom field */ custom?: Record<string, any> } /** {@link https://developer.mozilla.org/en-US/docs/Web/API/Response} */ export interface IResponse<Data = any> { headers: Headers ok: boolean redirected: boolean status: number statusText: string type: ResponseType url: string data: Data responseText?: string } export interface IContext<Data = any> extends IResponse<Data> { /** * request config. * and empty `Headers` object as default */ request: IRequest & { method: Method; headers: Headers } /** * set `ctx.request.headers` * * *And header names are matched by case-insensitive byte sequence.* * * @example * ```ts * // set a header * ctx.set('name', '<value>') * * // remove a header * ctx.set('name', null) * ctx.set('name') * * // set headers * ctx.set({ name1: '<value>', name2: '<value>' }) * ``` */ set(headerOrName: HeadersInit | string, value?: string | null): this /** * Add extra params to `request.url`. * If there are duplicate keys, then the original key-values will be removed. */ params(params: RequestQuery): this /** * get `ctx.request.abort`, * and **create one if not exist** * @throws {RequestError} */ abort(): TimeoutAbortController /** throw {@link RequestError} */ throw(e: string | Error): void /** * Assign to current context */ assign(context: Partial<IContext>): void /** * Replay current request * And assign new context to current, with replay`s response */ replay(): Promise<void> } export type Middleware = ( ctx: IContext, next: () => Promise<void> ) => Promise<void> type AliasConfig = Omit<IRequest, 'url' | 'data'> export class Requete { static defaults: RequestConfig = { timeout: 0, responseType: 'json', headers: { Accept: 'application/json, text/plain, */*', }, verbose: 1, toJSON: (text: string | null | undefined) => { if (text) return JSON.parse(text) }, } private configs?: RequestConfig private adapter: Adapter private middlewares: Middleware[] = [] logger: Logger constructor(config?: RequestConfig) { this.configs = Object.assign({ method: 'GET' }, Requete.defaults, config) this.adapter = createAdapter() this.logger = new Logger( 'Requete', this.configs.verbose === true ? 2 : Number(this.configs.verbose ?? 0) ) } /** * add middleware function * * @attention * - The calling order of middleware should follow the **Onion Model**. * like {@link https://github.com/koajs/koa/blob/master/docs/guide.md#writing-middleware Koajs}. * - `next()` must be called asynchronously in middleware * * @example * ```ts * http.use(async (ctx, next) => { * // set request header * ctx.set('Authorization', '<token>') * * // wait for request responding * await next() * * // transformed response body * console.log(ctx.data) * * // throw a request error * if (!ctx.data) ctx.throw('no response data') * }) * ``` */ use(middleware: Middleware) { this.middlewares.push(middleware) this.logger.info( `Use middleware #${this.middlewares.length}:`, middleware.name || middleware ) return this } private createRequest(config: IRequest) { const request: IRequest = Object.assign({}, this.configs, config) request.url = getUri(request) request.headers = mergeHeaders( Requete.defaults.headers, this.configs?.headers, config.headers ) // add default AbortController for timeout if (!request.abort && request.timeout && TimeoutAbortController.supported) { request.abort = new TimeoutAbortController(request.timeout) } return request as IContext['request'] } private createContext<D>(config: IRequest) { const request = this.createRequest(config) const doRequest = this.request.bind(this) const ctx: IContext<D> = { request, status: -1, data: undefined as D, ok: false, redirected: false, headers: undefined as unknown as Headers, statusText: undefined as unknown as string, type: undefined as unknown as ResponseType, url: request.url, set(headerOrName, value) { if (this.status !== -1) this.throw('Cannot set request headers after next().') let headers = this.request.headers if (typeof headerOrName === 'string') { value == null ? headers.delete(headerOrName) : headers.set(headerOrName, value) } else { headers = mergeHeaders(headers, headerOrName) } this.request.headers = headers return this }, params(params) { this.request.url = stringifyUrl(this.request.url, params, false) return this }, abort() { if (!this.request.abort) { if (this.status !== -1) this.throw('Cannot set abortSignal after next().') this.request.abort = new TimeoutAbortController( this.request.timeout ?? 0 ) } return this.request.abort }, throw(e) { if (e instanceof RequestError) throw e throw new RequestError(e, this) }, assign(context) { Object.assign(this, context) }, async replay() { // count replay # this.request.custom = Object.assign({}, this.request.custom, { replay: (this.request.custom?.replay ?? 0) + 1, }) const context = await doRequest(this.request) this.assign(context) }, } return ctx } private async invoke(ctx: IContext) { this.logger.request(ctx) const adapter = ctx.request.adapter ?? this.adapter const response = await adapter.request(ctx) // assign to ctx Object.assign( ctx, pick(response, [ 'ok', 'status', 'statusText', 'headers', 'data', 'responseText', 'redirected', 'type', 'url', ]) ) if (ctx.request.responseType === 'json') { ctx.data = ctx.request.toJSON!(response.data) } } async request<D = any>(config: IRequest) { // create context const context = this.createContext<D>(config) // exec middleware try {
await compose(this.middlewares)(context, this.invoke.bind(this)) if (!context.ok) {
context.throw( `${context.request.method} ${context.url} ${context.status} (${context.statusText})` ) } this.logger.response(context) return context } catch (e: any) { this.logger.error(e) throw e } finally { context.request.abort?.clear() } } get<D = any>(url: string, config?: AliasConfig) { return this.request<D>({ ...config, url, method: 'GET' }) } delete<D = any>(url: string, config?: AliasConfig) { return this.request<D>({ ...config, url, method: 'DELETE' }) } head<D = any>(url: string, config?: AliasConfig) { return this.request<D>({ ...config, url, method: 'HEAD' }) } options<D = any>(url: string, config?: AliasConfig) { return this.request<D>({ ...config, url, method: 'OPTIONS' }) } post<D = any>(url: string, data?: RequestBody, config?: AliasConfig) { return this.request<D>({ ...config, url, data, method: 'POST' }) } put<D = any>(url: string, data?: RequestBody, config?: AliasConfig) { return this.request<D>({ ...config, url, data, method: 'PUT' }) } patch<D = any>(url: string, data?: RequestBody, config?: AliasConfig) { return this.request<D>({ ...config, url, data, method: 'PATCH' }) } }
src/core/Requete.ts
rexerwang-requete-e7d4979
[ { "filename": "src/core/compose.ts", "retrieved_chunk": "import type { IContext, Middleware } from './Requete'\nexport function compose(middlewares: Middleware[]) {\n return (context: IContext, next: Middleware) => {\n // last called middleware #\n let index = -1\n return dispatch(0).catch((e) => context.throw(e))\n async function dispatch(i: number): Promise<void> {\n if (i <= index) throw new Error('next() called multiple times')\n index = i\n const middleware = i === middlewares.length ? next : middlewares[i]", "score": 32.73834170090856 }, { "filename": "src/core/compose.ts", "retrieved_chunk": " if (middleware) return await middleware(context, () => dispatch(i + 1))\n }\n }\n}", "score": 32.68208214971578 }, { "filename": "src/core/__tests__/requete-middleware.test.ts", "retrieved_chunk": " data: 'null',\n })\n )\n beforeEach(() => {\n spy.mockClear()\n })\n it('should execute middleware in order according to the onion model with request context', async () => {\n const before = vi.fn()\n const after = vi.fn()\n const requete = new Requete()", "score": 22.588132587296567 }, { "filename": "src/shared/logger.ts", "retrieved_chunk": " status === -1 ? 'Before Request' : statusText\n })\\n%o\\n${e.stack}`,\n this.res(ctx)\n )\n }\n request(ctx: IContext) {\n this.info(`${ctx.request.method} ${ctx.url}`, this.res(ctx).request)\n }\n response(ctx: IContext) {\n this.info(", "score": 22.009193830371835 }, { "filename": "src/core/AbortController.ts", "retrieved_chunk": " abort(reason?: any) {\n this.controller.abort(reason)\n this.clear()\n }\n clear() {\n if (this.timeoutId) {\n clearTimeout(this.timeoutId)\n this.timeoutId = null\n }\n }", "score": 21.191400526493908 } ]
typescript
await compose(this.middlewares)(context, this.invoke.bind(this)) if (!context.ok) {
import { API_URL, API } from './constants'; import { postData, getData, patchData } from './utils'; import { ChargeOptionsType, KeysendOptionsType, ChargeDataResponseType, WalletDataResponseType, BTCUSDDataResponseType, SendPaymentOptionsType, DecodeChargeOptionsType, DecodeChargeResponseType, ProdIPSDataResponseType, StaticChargeOptionsType, KeysendDataResponseType, InternalTransferOptionsType, StaticChargeDataResponseType, WithdrawalRequestOptionsType, SendGamertagPaymentOptionsType, InvoicePaymentDataResponseType, SupportedRegionDataResponseType, InternalTransferDataResponseType, GetWithdrawalRequestDataResponseType, CreateWithdrawalRequestDataResponseType, FetchChargeFromGamertagOptionsType, GamertagTransactionDataResponseType, FetchUserIdByGamertagDataResponseType, FetchGamertagByUserIdDataResponseType, SendLightningAddressPaymentOptionsType, FetchChargeFromGamertagDataResponseType, ValidateLightningAddressDataResponseType, SendLightningAddressPaymentDataResponseType, CreateChargeFromLightningAddressOptionsType, SendGamertagPaymentDataResponseType, FetchChargeFromLightningAddressDataResponseType, } from './types/index'; class zbd { apiBaseUrl: string; apiCoreHeaders: {apikey: string }; constructor(apiKey: string) { this.apiBaseUrl = API_URL; this.apiCoreHeaders = { apikey: apiKey }; } async createCharge(options: ChargeOptionsType) { const { amount, expiresIn, internalId, description, callbackUrl, } = options; const response : ChargeDataResponseType = await postData({ url
: `${API_URL}${API.CHARGES_ENDPOINT}`, headers: { ...this.apiCoreHeaders }, body: {
amount, expiresIn, internalId, description, callbackUrl, }, }); return response; } async getCharge(chargeId: string) { const response: ChargeDataResponseType = await getData({ url: `${API_URL}${API.CHARGES_ENDPOINT}/${chargeId}`, headers: { ...this.apiCoreHeaders }, }); return response; } async decodeCharge(options: DecodeChargeOptionsType) { const { invoice } = options; const response: DecodeChargeResponseType = await postData({ url: `${API_URL}${API.DECODE_INVOICE_ENDPOINT}`, headers: { ...this.apiCoreHeaders }, body: { invoice }, }); return response; } async createWithdrawalRequest(options: WithdrawalRequestOptionsType) { const { amount, expiresIn, internalId, callbackUrl, description, } = options; const response : CreateWithdrawalRequestDataResponseType = await postData({ url: `${API_URL}${API.WITHDRAWAL_REQUESTS_ENDPOINT}`, headers: { ...this.apiCoreHeaders }, body: { amount, expiresIn, internalId, callbackUrl, description, }, }); return response; } async getWithdrawalRequest(withdrawalRequestId: string) { const response : GetWithdrawalRequestDataResponseType = await getData({ url: `${API_URL}${API.WITHDRAWAL_REQUESTS_ENDPOINT}/${withdrawalRequestId}`, headers: { ...this.apiCoreHeaders }, }); return response; } async validateLightningAddress(lightningAddress: string) { const response : ValidateLightningAddressDataResponseType = await getData({ url: `${API_URL}${API.VALIDATE_LN_ADDRESS_ENDPOINT}/${lightningAddress}`, headers: { ...this.apiCoreHeaders }, }); return response; } async sendLightningAddressPayment(options: SendLightningAddressPaymentOptionsType) { const { amount, comment, lnAddress, internalId, callbackUrl, } = options; const response : SendLightningAddressPaymentDataResponseType = await postData({ url: `${API_URL}${API.SEND_LN_ADDRESS_PAYMENT_ENDPOINT}`, headers: { ...this.apiCoreHeaders }, body: { amount, comment, lnAddress, internalId, callbackUrl, }, }); return response; } async createChargeFromLightningAddress(options: CreateChargeFromLightningAddressOptionsType) { const { amount, lnaddress, lnAddress, description, } = options; // Addressing issue on ZBD API where it accepts `lnaddress` property // instead of `lnAddress` property as is standardized let lightningAddress = lnaddress || lnAddress; const response: FetchChargeFromLightningAddressDataResponseType = await postData({ url: `${API_URL}${API.CREATE_CHARGE_FROM_LN_ADDRESS_ENDPOINT}`, headers: { ...this.apiCoreHeaders }, body: { amount, description, lnaddress: lightningAddress, }, }); return response; } async getWallet() { const response : WalletDataResponseType = await getData({ url: `${API_URL}${API.WALLET_ENDPOINT}`, headers: { ...this.apiCoreHeaders }, }); return response; } async isSupportedRegion(ipAddress: string) { const response : SupportedRegionDataResponseType = await getData({ url: `${API_URL}${API.IS_SUPPORTED_REGION_ENDPOINT}/${ipAddress}`, headers: { ...this.apiCoreHeaders }, }); return response; } async getZBDProdIps() { const response: ProdIPSDataResponseType = await getData({ url: `${API_URL}${API.FETCH_ZBD_PROD_IPS_ENDPOINT}`, headers: { ...this.apiCoreHeaders }, }); return response; } async getBtcUsdExchangeRate() { const response: BTCUSDDataResponseType = await getData({ url: `${API_URL}${API.BTCUSD_PRICE_TICKER_ENDPOINT}`, headers: { ...this.apiCoreHeaders }, }); return response; } async internalTransfer(options: InternalTransferOptionsType) { const { amount, receiverWalletId } = options; const response: InternalTransferDataResponseType = await postData({ url: `${API_URL}${API.INTERNAL_TRANSFER_ENDPOINT}`, headers: { ...this.apiCoreHeaders }, body: { amount, receiverWalletId, }, }); return response; } async sendKeysendPayment(options: KeysendOptionsType) { const { amount, pubkey, metadata, tlvRecords, callbackUrl, } = options; const response: KeysendDataResponseType = await postData({ url: `${API_URL}${API.KEYSEND_PAYMENT_ENDPOINT}`, headers: { ...this.apiCoreHeaders }, body: { amount, pubkey, metadata, tlvRecords, callbackUrl, }, }); return response; } async sendPayment(options: SendPaymentOptionsType) { const { amount, invoice, internalId, description, callbackUrl, } = options; const response : InvoicePaymentDataResponseType = await postData({ url: `${API_URL}${API.PAYMENTS_ENDPOINT}`, headers: { ...this.apiCoreHeaders }, body: { amount, invoice, internalId, description, callbackUrl, }, }); return response; } async getPayment(paymentId: string) { const response = await getData({ url: `${API_URL}${API.PAYMENTS_ENDPOINT}/${paymentId}`, headers: { ...this.apiCoreHeaders }, }); return response; } async sendGamertagPayment(options: SendGamertagPaymentOptionsType) { const { amount, gamertag, description } = options; const response: SendGamertagPaymentDataResponseType = await postData({ url: `${API_URL}${API.SEND_GAMERTAG_PAYMENT_ENDPOINT}`, headers: { ...this.apiCoreHeaders }, body: { amount, gamertag, description, }, }); return response; } async getGamertagTransaction(transactionId: string) { const response: GamertagTransactionDataResponseType = await getData({ url: `${API_URL}${API.GET_GAMERTAG_PAYMENT_ENDPOINT}/${transactionId}`, headers: { ...this.apiCoreHeaders }, }); return response; } async getUserIdByGamertag(gamertag: string) { const response: FetchUserIdByGamertagDataResponseType = await getData({ url: `${API_URL}${API.GET_USERID_FROM_GAMERTAG_ENDPOINT}/${gamertag}`, headers: { ...this.apiCoreHeaders }, }); return response; } async getGamertagByUserId(userId: string) { const response: FetchGamertagByUserIdDataResponseType = await getData({ url: `${API_URL}${API.GET_GAMERTAG_FROM_USERID_ENDPOINT}/${userId}`, headers: { ...this.apiCoreHeaders }, }); return response; } async createGamertagCharge(options: FetchChargeFromGamertagOptionsType) { const { amount, gamertag, internalId, description, callbackUrl, } = options; const response : FetchChargeFromGamertagDataResponseType = await postData({ url: `${API_URL}${API.CREATE_CHARGE_FROM_GAMERTAG_ENDPOINT}`, headers: { ...this.apiCoreHeaders }, body: { amount, gamertag, internalId, description, callbackUrl, }, }); return response; } async createStaticCharge(options: StaticChargeOptionsType) { const { minAmount, maxAmount, internalId, description, callbackUrl, allowedSlots, successMessage, } = options; const response : StaticChargeDataResponseType = await postData({ url: `${API_URL}${API.STATIC_CHARGES_ENDPOINT}`, headers: { ...this.apiCoreHeaders }, body: { minAmount, maxAmount, internalId, callbackUrl, description, allowedSlots, successMessage, }, }); return response; } async updateStaticCharge(staticChargeId: string, updates: StaticChargeOptionsType) { const response = await patchData({ url: `${API_URL}${API.STATIC_CHARGES_ENDPOINT}/${staticChargeId}`, headers: { ...this.apiCoreHeaders }, body: updates, }); return response; } async getStaticCharge(staticChargeId: string) { const response = await getData({ url: `${API_URL}${API.STATIC_CHARGES_ENDPOINT}/${staticChargeId}`, headers: { ...this.apiCoreHeaders }, }); return response; } } export { zbd };
src/zbd.ts
zebedeeio-zbd-node-170d530
[ { "filename": "src/utils.ts", "retrieved_chunk": "export async function postData({\n url,\n body,\n headers,\n}: {\n url: string;\n body: any;\n headers?: any;\n}) {\n const response = await fetch(url, {", "score": 22.99176978730359 }, { "filename": "src/utils.ts", "retrieved_chunk": " body,\n headers,\n}: {\n url: string;\n body: any;\n headers?: any;\n}) {\n const response = await fetch(url, {\n method: \"PATCH\",\n headers: {", "score": 20.00436282177095 }, { "filename": "src/utils.d.ts", "retrieved_chunk": "export declare const cleanup: (obj: any) => {};\nexport declare function postData({ url, body, headers, }: {\n url: string;\n body: any;\n headers?: any;\n}): Promise<any>;\nexport declare function patchData({ url, body, headers, }: {\n url: string;\n body: any;\n headers?: any;", "score": 16.583760552311066 }, { "filename": "src/utils.ts", "retrieved_chunk": " 'Content-Type': 'application/json',\n ...headers,\n },\n body: JSON.stringify(cleanup(body)),\n });\n if (!response.ok) {\n const errorBody = await response.json();\n const error = {\n status: response.status,\n message: errorBody.message || 'API request failed',", "score": 15.802529575491432 }, { "filename": "src/utils.ts", "retrieved_chunk": " method: \"POST\",\n headers: {\n 'Content-Type': 'application/json',\n ...headers,\n },\n body: JSON.stringify(cleanup(body)),\n });\n if (!response.ok) {\n const errorBody = await response.json();\n const error = {", "score": 15.34612854859738 } ]
typescript
: `${API_URL}${API.CHARGES_ENDPOINT}`, headers: { ...this.apiCoreHeaders }, body: {
import { API_URL, API } from './constants'; import { postData, getData, patchData } from './utils'; import { ChargeOptionsType, KeysendOptionsType, ChargeDataResponseType, WalletDataResponseType, BTCUSDDataResponseType, SendPaymentOptionsType, DecodeChargeOptionsType, DecodeChargeResponseType, ProdIPSDataResponseType, StaticChargeOptionsType, KeysendDataResponseType, InternalTransferOptionsType, StaticChargeDataResponseType, WithdrawalRequestOptionsType, SendGamertagPaymentOptionsType, InvoicePaymentDataResponseType, SupportedRegionDataResponseType, InternalTransferDataResponseType, GetWithdrawalRequestDataResponseType, CreateWithdrawalRequestDataResponseType, FetchChargeFromGamertagOptionsType, GamertagTransactionDataResponseType, FetchUserIdByGamertagDataResponseType, FetchGamertagByUserIdDataResponseType, SendLightningAddressPaymentOptionsType, FetchChargeFromGamertagDataResponseType, ValidateLightningAddressDataResponseType, SendLightningAddressPaymentDataResponseType, CreateChargeFromLightningAddressOptionsType, SendGamertagPaymentDataResponseType, FetchChargeFromLightningAddressDataResponseType, } from './types/index'; class zbd { apiBaseUrl: string; apiCoreHeaders: {apikey: string }; constructor(apiKey: string) { this.apiBaseUrl = API_URL; this.apiCoreHeaders = { apikey: apiKey }; } async createCharge(options: ChargeOptionsType) { const { amount, expiresIn, internalId, description, callbackUrl, } = options; const response : ChargeDataResponseType = await postData({ url: `${API_URL}${API.CHARGES_ENDPOINT}`, headers: { ...this.apiCoreHeaders }, body: { amount, expiresIn, internalId, description, callbackUrl, }, }); return response; } async getCharge(chargeId: string) { const response: ChargeDataResponseType = await getData({ url: `${API_URL}${API.CHARGES_ENDPOINT}/${chargeId}`, headers: { ...this.apiCoreHeaders }, }); return response; } async decodeCharge(options: DecodeChargeOptionsType) { const { invoice } = options; const response: DecodeChargeResponseType = await postData({ url: `${API_URL}${API.DECODE_INVOICE_ENDPOINT}`, headers: { ...this.apiCoreHeaders }, body: { invoice }, }); return response; } async createWithdrawalRequest(options: WithdrawalRequestOptionsType) { const { amount, expiresIn, internalId, callbackUrl, description, } = options; const response : CreateWithdrawalRequestDataResponseType = await postData({ url: `${API_URL}${API.WITHDRAWAL_REQUESTS_ENDPOINT}`, headers: { ...this.apiCoreHeaders }, body: { amount, expiresIn, internalId, callbackUrl, description, }, }); return response; } async getWithdrawalRequest(withdrawalRequestId: string) { const response : GetWithdrawalRequestDataResponseType = await getData({ url: `${API_URL}${API.WITHDRAWAL_REQUESTS_ENDPOINT}/${withdrawalRequestId}`, headers: { ...this.apiCoreHeaders }, }); return response; } async validateLightningAddress(lightningAddress: string) { const response : ValidateLightningAddressDataResponseType = await getData({ url: `${API_URL}${API.VALIDATE_LN_ADDRESS_ENDPOINT}/${lightningAddress}`, headers: { ...this.apiCoreHeaders }, }); return response; } async sendLightningAddressPayment(options: SendLightningAddressPaymentOptionsType) { const { amount, comment, lnAddress, internalId, callbackUrl, } = options; const response : SendLightningAddressPaymentDataResponseType = await postData({ url: `${API_URL}${API.SEND_LN_ADDRESS_PAYMENT_ENDPOINT}`, headers: { ...this.apiCoreHeaders }, body: { amount, comment, lnAddress, internalId, callbackUrl, }, }); return response; } async createChargeFromLightningAddress(options: CreateChargeFromLightningAddressOptionsType) { const { amount, lnaddress, lnAddress, description, } = options; // Addressing issue on ZBD API where it accepts `lnaddress` property // instead of `lnAddress` property as is standardized let lightningAddress = lnaddress || lnAddress; const response: FetchChargeFromLightningAddressDataResponseType = await postData({ url
: `${API_URL}${API.CREATE_CHARGE_FROM_LN_ADDRESS_ENDPOINT}`, headers: { ...this.apiCoreHeaders }, body: {
amount, description, lnaddress: lightningAddress, }, }); return response; } async getWallet() { const response : WalletDataResponseType = await getData({ url: `${API_URL}${API.WALLET_ENDPOINT}`, headers: { ...this.apiCoreHeaders }, }); return response; } async isSupportedRegion(ipAddress: string) { const response : SupportedRegionDataResponseType = await getData({ url: `${API_URL}${API.IS_SUPPORTED_REGION_ENDPOINT}/${ipAddress}`, headers: { ...this.apiCoreHeaders }, }); return response; } async getZBDProdIps() { const response: ProdIPSDataResponseType = await getData({ url: `${API_URL}${API.FETCH_ZBD_PROD_IPS_ENDPOINT}`, headers: { ...this.apiCoreHeaders }, }); return response; } async getBtcUsdExchangeRate() { const response: BTCUSDDataResponseType = await getData({ url: `${API_URL}${API.BTCUSD_PRICE_TICKER_ENDPOINT}`, headers: { ...this.apiCoreHeaders }, }); return response; } async internalTransfer(options: InternalTransferOptionsType) { const { amount, receiverWalletId } = options; const response: InternalTransferDataResponseType = await postData({ url: `${API_URL}${API.INTERNAL_TRANSFER_ENDPOINT}`, headers: { ...this.apiCoreHeaders }, body: { amount, receiverWalletId, }, }); return response; } async sendKeysendPayment(options: KeysendOptionsType) { const { amount, pubkey, metadata, tlvRecords, callbackUrl, } = options; const response: KeysendDataResponseType = await postData({ url: `${API_URL}${API.KEYSEND_PAYMENT_ENDPOINT}`, headers: { ...this.apiCoreHeaders }, body: { amount, pubkey, metadata, tlvRecords, callbackUrl, }, }); return response; } async sendPayment(options: SendPaymentOptionsType) { const { amount, invoice, internalId, description, callbackUrl, } = options; const response : InvoicePaymentDataResponseType = await postData({ url: `${API_URL}${API.PAYMENTS_ENDPOINT}`, headers: { ...this.apiCoreHeaders }, body: { amount, invoice, internalId, description, callbackUrl, }, }); return response; } async getPayment(paymentId: string) { const response = await getData({ url: `${API_URL}${API.PAYMENTS_ENDPOINT}/${paymentId}`, headers: { ...this.apiCoreHeaders }, }); return response; } async sendGamertagPayment(options: SendGamertagPaymentOptionsType) { const { amount, gamertag, description } = options; const response: SendGamertagPaymentDataResponseType = await postData({ url: `${API_URL}${API.SEND_GAMERTAG_PAYMENT_ENDPOINT}`, headers: { ...this.apiCoreHeaders }, body: { amount, gamertag, description, }, }); return response; } async getGamertagTransaction(transactionId: string) { const response: GamertagTransactionDataResponseType = await getData({ url: `${API_URL}${API.GET_GAMERTAG_PAYMENT_ENDPOINT}/${transactionId}`, headers: { ...this.apiCoreHeaders }, }); return response; } async getUserIdByGamertag(gamertag: string) { const response: FetchUserIdByGamertagDataResponseType = await getData({ url: `${API_URL}${API.GET_USERID_FROM_GAMERTAG_ENDPOINT}/${gamertag}`, headers: { ...this.apiCoreHeaders }, }); return response; } async getGamertagByUserId(userId: string) { const response: FetchGamertagByUserIdDataResponseType = await getData({ url: `${API_URL}${API.GET_GAMERTAG_FROM_USERID_ENDPOINT}/${userId}`, headers: { ...this.apiCoreHeaders }, }); return response; } async createGamertagCharge(options: FetchChargeFromGamertagOptionsType) { const { amount, gamertag, internalId, description, callbackUrl, } = options; const response : FetchChargeFromGamertagDataResponseType = await postData({ url: `${API_URL}${API.CREATE_CHARGE_FROM_GAMERTAG_ENDPOINT}`, headers: { ...this.apiCoreHeaders }, body: { amount, gamertag, internalId, description, callbackUrl, }, }); return response; } async createStaticCharge(options: StaticChargeOptionsType) { const { minAmount, maxAmount, internalId, description, callbackUrl, allowedSlots, successMessage, } = options; const response : StaticChargeDataResponseType = await postData({ url: `${API_URL}${API.STATIC_CHARGES_ENDPOINT}`, headers: { ...this.apiCoreHeaders }, body: { minAmount, maxAmount, internalId, callbackUrl, description, allowedSlots, successMessage, }, }); return response; } async updateStaticCharge(staticChargeId: string, updates: StaticChargeOptionsType) { const response = await patchData({ url: `${API_URL}${API.STATIC_CHARGES_ENDPOINT}/${staticChargeId}`, headers: { ...this.apiCoreHeaders }, body: updates, }); return response; } async getStaticCharge(staticChargeId: string) { const response = await getData({ url: `${API_URL}${API.STATIC_CHARGES_ENDPOINT}/${staticChargeId}`, headers: { ...this.apiCoreHeaders }, }); return response; } } export { zbd };
src/zbd.ts
zebedeeio-zbd-node-170d530
[ { "filename": "src/utils.ts", "retrieved_chunk": "export async function postData({\n url,\n body,\n headers,\n}: {\n url: string;\n body: any;\n headers?: any;\n}) {\n const response = await fetch(url, {", "score": 22.99176978730359 }, { "filename": "src/utils.ts", "retrieved_chunk": " body,\n headers,\n}: {\n url: string;\n body: any;\n headers?: any;\n}) {\n const response = await fetch(url, {\n method: \"PATCH\",\n headers: {", "score": 20.00436282177095 }, { "filename": "src/utils.ts", "retrieved_chunk": " 'Content-Type': 'application/json',\n ...headers,\n },\n body: JSON.stringify(cleanup(body)),\n });\n if (!response.ok) {\n const errorBody = await response.json();\n const error = {\n status: response.status,\n message: errorBody.message || 'API request failed',", "score": 18.10764403622178 }, { "filename": "src/types/lightning.ts", "retrieved_chunk": "}\nexport interface CreateChargeFromLightningAddressOptionsType {\n amount: string\n lnaddress: string\n lnAddress?: string\n description: string\n}", "score": 16.66441198903179 }, { "filename": "src/utils.d.ts", "retrieved_chunk": "export declare const cleanup: (obj: any) => {};\nexport declare function postData({ url, body, headers, }: {\n url: string;\n body: any;\n headers?: any;\n}): Promise<any>;\nexport declare function patchData({ url, body, headers, }: {\n url: string;\n body: any;\n headers?: any;", "score": 16.583760552311066 } ]
typescript
: `${API_URL}${API.CREATE_CHARGE_FROM_LN_ADDRESS_ENDPOINT}`, headers: { ...this.apiCoreHeaders }, body: {