lodash-es#zip TypeScript Examples

The following examples show how to use lodash-es#zip. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: torznab.ts    From cross-seed with Apache License 2.0 5 votes vote down vote up
async validateTorznabUrls() {
		const { torznab } = getRuntimeConfig();
		if (!torznab) return;

		const urls: URL[] = torznab.map((str) => new URL(str));
		for (const url of urls) {
			if (!url.pathname.endsWith("/api")) {
				throw new CrossSeedError(
					`Torznab url ${url} must have a path ending in /api`
				);
			}
			if (!url.searchParams.has("apikey")) {
				throw new CrossSeedError(
					`Torznab url ${url} does not specify an apikey`
				);
			}
		}

		const outcomes = await Promise.allSettled(
			urls.map((url) => this.fetchCaps(url))
		);

		const zipped: [URL, PromiseSettledResult<Caps>][] = zip(urls, outcomes);

		// handle promise rejections
		const rejected: [URL, PromiseRejectedResult][] = zipped.filter(
			(bundle): bundle is [URL, PromiseRejectedResult] =>
				bundle[1].status === "rejected"
		);

		for (const [url, outcome] of rejected) {
			logger.warn(`Failed to reach ${url}`);
			logger.debug(outcome.reason);
		}

		const fulfilled = zipped
			.filter(
				(bundle): bundle is [URL, PromiseFulfilledResult<Caps>] =>
					bundle[1].status === "fulfilled"
			)
			.map(
				([url, outcome]: [URL, PromiseFulfilledResult<Caps>]): [
					URL,
					Caps
				] => [url, outcome.value]
			);

		// handle trackers that can't search
		const trackersWithoutSearchingCaps = fulfilled.filter(
			([, caps]) => !caps.search
		);
		trackersWithoutSearchingCaps
			.map(
				([url]) =>
					`Ignoring indexer that doesn't support searching: ${url}`
			)
			.forEach(logger.warn);

		// store caps of usable trackers
		const trackersWithSearchingCaps = fulfilled.filter(
			([, caps]) => caps.search
		);

		for (const [url, caps] of trackersWithSearchingCaps) {
			this.capsMap.set(url, caps);
		}

		if (trackersWithSearchingCaps.length === 0) {
			throw new CrossSeedError("no working indexers available");
		}
	}
Example #2
Source File: torznab.ts    From cross-seed with Apache License 2.0 5 votes vote down vote up
async searchTorznab(
		name: string,
		nonceOptions: NonceOptions
	): Promise<Candidate[]> {
		const searchUrls = Array.from(this.capsMap).map(
			([url, caps]: [URL, Caps]) => {
				return this.assembleUrl(
					url,
					this.getBestSearchTechnique(name, caps)
				);
			}
		);
		searchUrls.forEach(
			(message) => void logger.verbose({ label: Label.TORZNAB, message })
		);
		const outcomes = await Promise.allSettled<Candidate[]>(
			searchUrls.map((url) =>
				fetch(url)
					.then((r) => r.text())
					.then(this.parseResults)
			)
		);
		const rejected = zip(Array.from(this.capsMap.keys()), outcomes).filter(
			([, outcome]) => outcome.status === "rejected"
		);
		rejected
			.map(
				([url, outcome]) =>
					`Failed searching ${url} for ${name} with reason: ${outcome.reason}`
			)
			.forEach(logger.warn);

		const fulfilled = outcomes
			.filter(
				(outcome): outcome is PromiseFulfilledResult<Candidate[]> =>
					outcome.status === "fulfilled"
			)
			.map((outcome) => outcome.value);
		return [].concat(...fulfilled);
	}