All files / src/api/catalog-search catalog-search.ts

94.05% Statements 95/101
86.95% Branches 40/46
95.83% Functions 23/24
94% Lines 94/100

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406                          3x                                                                                                                                                                                                   6x     503x     503x 503x 503x 503x   503x 202x           503x 5x   498x 498x     498x 197x       503x   503x                         503x     6x   6x 6x   6x 6x 6x   6x 42x 42x     6x 503x 503x                   19x   19x     19x 6x     19x   19x 7x     19x             3x   3x 3x   3x     2x 6x       3x 3x           3x 125x   125x 125x     125x                     106x 3x 3x 3x 3x       3x                   3x                   6x 6x   6x               6x 600x   600x 300x 117x       600x 117x       6x                   13x 13x   4695x                         19x   19x 1608x   1608x       1575x       19x             6x   503x 878x 878x     503x         6x                 6x   503x   503x 250x 250x   250x       5x     250x       503x                                   6x      
import lunr from "lunr";
import { CatalogSearchSort } from "./constants";
import {
  FILTER_FUNCTIONS,
  mapConstructFrameworks,
  renderAllKeywords,
  SORT_FUNCTIONS,
} from "./util";
import { CDKType } from "../../constants/constructs";
import { Language } from "../../constants/languages";
import { CatalogPackage } from "../package/packages";
import { PackageStats } from "../stats";
 
const INDEX_FIELDS = {
  AUTHOR_EMAIL: {
    name: "authorEmail",
    boost: 1,
  },
  AUTHOR_NAME: {
    name: "authorName",
    boost: 3,
  },
  DESCRIPTION: {
    name: "description",
    boost: 2,
  },
  NAME: {
    name: "name",
    boost: 5,
  },
  PACKAGE_NAME: {
    name: "packageName",
    boost: 5,
  },
  SCOPE: {
    name: "scope",
    boost: 5,
  },
  TAG_NAMES: {
    name: "tagNames",
    boost: 2,
  },
} as const;
 
export interface ExtendedCatalogPackage extends CatalogPackage {
  authorEmail?: string;
  authorName?: string;
  constructFrameworks: Map<CDKType, number | null>;
  downloads: number;
  id: string;
  packageName?: string;
  tagNames: lunr.Token[];
  scope?: string;
}
 
export interface CatalogConstructFrameworks {
  [CDKType.awscdk]: CatalogConstructFrameworkMeta;
  [CDKType.cdktf]: CatalogConstructFrameworkMeta;
  [CDKType.cdk8s]: CatalogConstructFrameworkMeta;
}
export interface CatalogConstructFrameworkMeta {
  pkgCount: number;
  majorVersions: number[];
}
 
export interface CatalogSearchFilters {
  /**
   * The CDK Output Type to filter by. This functionality is not yet deployed on any BE so the implementation on the client is not final.
   */
  cdkType?: CDKType;
  /**
   * The CDK Type's major version to filter by. This param is ignored if no major is set
   */
  cdkMajor?: number;
  /**
   * A list of languages to filter by. Constructs that are not yet filtered out, will be
   * returned if they support any of the languages in this list.
   */
  languages?: Language[];
  /**
   * A list of keywords to filter by.
   */
  keywords?: string[];
  /**
   * A list of tags to filter by.
   */
  tags?: string[];
}
 
export type CatalogSearchResults = Map<string, ExtendedCatalogPackage>;
 
export interface CatalogSearchParams {
  query?: string;
  filters?: CatalogSearchFilters;
  sort?: CatalogSearchSort;
}
 
export class CatalogSearchAPI {
  private readonly map: CatalogSearchResults;
  private index: lunr.Index;
  /**
   * A map of detected keywords with a key representing the keyword, and a value representing the amount of occurences
   * the keyword has in the catalog.
   */
  public readonly keywords: Map<string, number>;
  /**
   * A map of detected Construct Frameworks which provides a count of libraries for that framework and a set of major versions detected
   */
  public readonly constructFrameworks: CatalogConstructFrameworks;
 
  constructor(catalogData: CatalogPackage[], stats: PackageStats) {
    const catalogMap = catalogData
      // Packages with the "construct-hub/hide-from-search" keyword are shadow-banned from search results
      .filter(
        (pkg) => !pkg.keywords?.includes("construct-hub/hide-from-search")
      )
      .reduce((map, pkg) => {
        const { author, name, version } = pkg;
        const id = [name, version].join("@");
        const downloads = stats.packages[name]?.downloads?.npm ?? 0;
        let [scope, packageName] = name.split("/");
 
        if (!packageName) {
          packageName = scope;
        }
 
        let authorName: string | undefined;
        let authorEmail: string | undefined;
 
        if (typeof author === "string") {
          authorName = author;
        } else {
          Eif (author?.name) {
            authorName = author.name;
          }
 
          if (author?.email) {
            authorEmail = author.email;
          }
        }
 
        const keywords = renderAllKeywords(pkg);
 
        map.set(id, {
          ...pkg,
          authorName,
          authorEmail,
          constructFrameworks: mapConstructFrameworks(pkg.metadata),
          keywords,
          downloads,
          id,
          packageName,
          scope,
          tagNames: keywords.map(lunr.tokenizer).flat(),
        });
 
        return map;
      }, new Map<string, ExtendedCatalogPackage>());
 
    this.map = this.sort(catalogMap, CatalogSearchSort.PublishDateDesc);
 
    this.constructFrameworks = this.detectAllConstructFrameworks();
    this.keywords = this.detectKeywords();
 
    this.index = lunr(function () {
      this.tokenizer.separator = /[\s\-/@]+/;
      this.ref("id");
 
      for (const key in INDEX_FIELDS) {
        const field = INDEX_FIELDS[key as keyof typeof INDEX_FIELDS];
        this.field(field.name, { boost: field.boost });
      }
 
      [...catalogMap.values()].forEach((pkg) => {
        const downloadBoost = Math.log(Math.max(pkg.downloads, 1));
        this.add(pkg, { boost: downloadBoost });
      });
    });
  }
 
  /**
   * Performs a Search against the catalog and returns a Map with results ordered
   * by search score / relevance
   */
  public search(params?: CatalogSearchParams): CatalogSearchResults {
    const { query, filters, sort } = params ?? {};
 
    let results = query ? this.query(query) : new Map(this.map);
 
    // TODO: Investigate if we can leverage lunr for filtering
    if (filters) {
      results = this.filter(results, filters);
    }
 
    results = this.dedup(results);
 
    if (sort) {
      results = this.sort(results, sort);
    }
 
    return results;
  }
 
  /**
   * This calls the index search method and returns a map of results ordered by relevance.
   */
  private query(query: string): CatalogSearchResults {
    let refs: lunr.Index.Result[] = [];
 
    try {
      let tokenizedQuery = lunr.tokenizer(query);
 
      if (tokenizedQuery.length > 1) {
        // A large number of libraries include the term cdk within the title - which will lead to an
        // inflated result count. TODO: determine if there are other terms to filter out
        tokenizedQuery = tokenizedQuery.filter(
          (token) => token.toString() !== "cdk"
        );
      }
 
      refs = this.index.query((q) => {
        q.term(tokenizedQuery, {});
      });
    } catch (e) {
      console.error(e);
    }
 
    return refs.reduce((packages, { ref }) => {
      const pkg = this.map.get(ref);
 
      Eif (pkg) {
        packages.set(ref, pkg);
      }
 
      return packages;
    }, new Map() as CatalogSearchResults);
  }
 
  /**
   * Performs a Search against the catalog and returns an array of all packages matching the query.
   */
  public findByName(
    query: string,
    opts?: { dedup?: boolean }
  ): ExtendedCatalogPackage[] {
    const results = [...this.map.values()].filter((pkg) => pkg.name === query);
    const matches = new Array<ExtendedCatalogPackage>();
    for (const pkg of results.values()) {
      Eif (pkg.name === query) {
        matches.push(pkg);
      }
    }
 
    Iif (opts?.dedup) {
      const map = new Map<string, ExtendedCatalogPackage>();
 
      matches.forEach((pkg) => {
        map.set(pkg.name, pkg);
      });
 
      return [...this.dedup(map).values()];
    }
 
    return matches;
  }
 
  /**
   * Filters query results. Mutates the passed-in map
   */
  private filter(
    results: CatalogSearchResults,
    filters: CatalogSearchFilters
  ): CatalogSearchResults {
    const { cdkType, cdkMajor, keywords, languages, tags } = filters;
    const copiedResults = new Map(results);
 
    const filterFunctions = [
      FILTER_FUNCTIONS.cdkType(cdkType),
      FILTER_FUNCTIONS.cdkMajor({ cdkType, cdkMajor }),
      FILTER_FUNCTIONS.keywords(keywords),
      FILTER_FUNCTIONS.languages(languages),
      FILTER_FUNCTIONS.tags(tags),
    ].filter(Boolean) as ((pkg: ExtendedCatalogPackage) => boolean)[];
 
    copiedResults.forEach((result) => {
      let isFiltered = false;
 
      filterFunctions.forEach((filterFn) => {
        if (!isFiltered && !filterFn(result)) {
          isFiltered = true;
        }
      });
 
      if (isFiltered) {
        copiedResults.delete(result.id);
      }
    });
 
    return copiedResults;
  }
 
  /**
   * Sort filtered results
   */
  private sort(
    results: CatalogSearchResults,
    strategy: CatalogSearchSort
  ): CatalogSearchResults {
    if (strategy) {
      return new Map(
        [...results.entries()].sort(([, p1], [, p2]) =>
          SORT_FUNCTIONS[strategy](p1, p2)
        )
      );
    } else E{
      return results;
    }
  }
 
  /**
   * De-duplicates packages that appear multiple times, keeping the
   * most recently published one.
   */
  private dedup(results: CatalogSearchResults): CatalogSearchResults {
    const dedupedResults: Map<string, ExtendedCatalogPackage> = new Map();
 
    for (const [_key, pkg] of results) {
      const maybePkg = dedupedResults.get(pkg.name);
 
      if (
        !maybePkg ||
        new Date(maybePkg.metadata.date) < new Date(pkg.metadata.date)
      ) {
        dedupedResults.set(pkg.name, pkg);
      }
    }
 
    return dedupedResults;
  }
 
  /**
   * Creates a map of keywords with values representing the occurence of the keyword within the catalog.
   */
  private detectKeywords() {
    const results = [...this.map.values()].reduce(
      (keywords: Map<string, number>, pkg: ExtendedCatalogPackage) => {
        for (const keyword of renderAllKeywords(pkg)) {
          const entry = keywords.get(keyword);
          keywords.set(keyword, (entry ?? 0) + 1);
        }
 
        return keywords;
      },
      new Map<string, number>()
    );
 
    return results;
  }
 
  /**
   * Creates an object of found construct frameworks in the catalog map.
   * They are indexed by the name of the construct framework and record the count
   * of packages for that framework as well as a list of major versions.
   */
  private detectAllConstructFrameworks() {
    const results: CatalogConstructFrameworks = [...this.map.values()].reduce(
      (frameworks: CatalogConstructFrameworks, pkg: ExtendedCatalogPackage) => {
        const { constructFrameworks } = pkg;
 
        [...constructFrameworks.entries()]?.forEach(([name, majorVersion]) => {
          Eif (majorVersion !== undefined) {
            const entry = frameworks[name];
 
            if (
              majorVersion !== null &&
              !entry.majorVersions.includes(majorVersion)
            ) {
              entry.majorVersions.push(majorVersion);
            }
 
            entry.pkgCount += 1;
          }
        });
 
        return frameworks;
      },
      {
        [CDKType.awscdk]: {
          majorVersions: [],
          pkgCount: 0,
        },
        [CDKType.cdk8s]: {
          majorVersions: [],
          pkgCount: 0,
        },
        [CDKType.cdktf]: {
          majorVersions: [],
          pkgCount: 0,
        },
      }
    );
 
    return results;
  }
}