npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@clickup/magination

v2.10.296

Published

An opinionated framework to build cursor-based pagination over multiple streams of hits

Downloads

4

Readme

magination: an opinionated framework to build cursor-based pagination over multiple streams of hits

See also Full API documentation.

CI run

The problem: for some search request (e.g. search-by-keywords), we have multiple search queries (sources) with different performance that deliver results (hits) with different relevancy. We want to merge that search hits using "cursor based pagination pattern" (allowing the user to scroll search results with the mouse).

For example purposes, considering the following 3 kinds of sources:

  1. "Full keyword match" query: finds the docs that have their lexems match the search keywords exactly. Highest relevancy and performance.
  2. "Prefix keyword match" query: considers the search keywords as lexem prefixes and finds all of the docs which have the lexems matching that prefixes. Moderate relevancy and performance.
  3. "Substring trigram match" query: finds docs which have search keywords as substrings. Lowest in relevancy and is super-slow.

When producing the cursored stream of pages, we must satisfy the following requirements:

  1. The results must never repeat: if some hit is seen on top, it's never shown at the bottom. E.g. if a "full keyword match" query returns document A, and the same document A is also returned by "substring trigram match" query, it should not be shown the 2nd time.
  2. The results must be exhaustive: i.e. eventually, all of the data will be returned, irregardless of its cardinality, even when the data is constantly appended to the database at some slow rate.
  3. The results from the source with highest relevancy should come earlier than the results from all other sources (at least for the very 1st set of pages). E.g. "full keyword match" source has higher relevancy than "substring trigram match", so the data from it should be returned earlier.
  4. The order of results should not depend on the performance: i.e. the same query run multiple times should produce the same results (considering the underlying database hasn't been changed).

Below is a cryptic diagram which illustrates, how it works:

source1:     [b, m, n]   |
source2: [a, b, c]       |     [d, e, f]
source3:   [q, r]        |  [s, t]
---------------------------------------------------> time

1. Magination.load():
   -> [b,m,n][a,c][q,r] + cursor
2. Magination.load(cursor):
   -> [d,e,f][s,t] + cursor=null

Here, [...] means search results docs from some source, and "|" means that a search query returns a cursor which allows to re-run it from where it left off.

To guarantee that the docs are never repeated in the resulting stream of pages, the engine remembers all of the hits previously emitted; they are stored in the cache. Where possible, it saves the docs hash codes, but for the actual sources, we store the entire hits, because we may want to return to the previous pages too (plus, we do "preload-ahead" for sources).