@jambudipa/spider 0.3.0 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import { Effect, Layer, Option, Chunk, MutableHashSet, Schema, Data, pipe, Context, DateTime, Console, Duration, MutableHashMap, Queue, Ref, HashMap, PubSub, MutableRef, Schedule, Stream, Fiber, Random, Struct } from "effect";
1
+ import { Effect, Layer, Option, Chunk, MutableHashSet, Schema, Data, pipe, Context, DateTime, Console, Duration, MutableHashMap, Queue, HashMap, PubSub, MutableRef, Schedule, Stream, Fiber, Random, Struct, Ref } from "effect";
2
2
  import * as cheerio from "cheerio";
3
3
  import * as fs from "fs";
4
4
  import * as path from "path";
@@ -1699,71 +1699,50 @@ const normalizeUrl = (url, strategy = DEFAULT_DEDUPLICATION_STRATEGY) => Effect.
1699
1699
  };
1700
1700
  });
1701
1701
  const deduplicateUrls = (urls, strategy = DEFAULT_DEDUPLICATION_STRATEGY) => Effect.gen(function* () {
1702
- const domainMap = yield* Ref.make(HashMap.empty());
1703
- const skipped = yield* Ref.make([]);
1702
+ let domainMap = HashMap.empty();
1703
+ const skipped = [];
1704
1704
  let invalidCount = 0;
1705
- yield* Effect.all(
1706
- urls.map(
1707
- (urlObj) => pipe(
1708
- normalizeUrl(urlObj.url, strategy),
1709
- Effect.tap(
1710
- (normalized) => Effect.gen(function* () {
1711
- const currentMap = yield* Ref.get(domainMap);
1712
- const key = strategy.wwwHandling === "preserve" ? normalized.normalized : normalized.domain;
1713
- const existingOption = HashMap.get(currentMap, key);
1714
- if (Option.isNone(existingOption)) {
1715
- yield* Ref.set(domainMap, HashMap.set(currentMap, key, urlObj));
1716
- } else {
1717
- const existing = existingOption.value;
1718
- let shouldReplace = false;
1719
- if (strategy.wwwHandling === "prefer-www") {
1720
- const existingHasWww = existing.url.includes("://www.");
1721
- const newHasWww = urlObj.url.includes("://www.");
1722
- shouldReplace = !existingHasWww && newHasWww;
1723
- } else if (strategy.wwwHandling === "prefer-non-www") {
1724
- const existingHasWww = existing.url.includes("://www.");
1725
- const newHasWww = urlObj.url.includes("://www.");
1726
- shouldReplace = existingHasWww && !newHasWww;
1727
- }
1728
- if (shouldReplace) {
1729
- yield* Ref.set(domainMap, HashMap.set(currentMap, key, urlObj));
1730
- yield* Ref.update(skipped, (arr) => [
1731
- ...arr,
1732
- { url: existing.url, reason: `Replaced by preferred variant: ${urlObj.url}` }
1733
- ]);
1734
- } else {
1735
- yield* Ref.update(skipped, (arr) => [
1736
- ...arr,
1737
- { url: urlObj.url, reason: `Duplicate of: ${existing.url}` }
1738
- ]);
1739
- }
1740
- }
1741
- })
1742
- ),
1743
- Effect.catchAll(
1744
- (error) => Effect.gen(function* () {
1745
- invalidCount++;
1746
- yield* Ref.update(skipped, (arr) => [
1747
- ...arr,
1748
- { url: urlObj.url, reason: `Invalid URL: ${error.message}` }
1749
- ]);
1750
- yield* Effect.logWarning(`Invalid URL skipped: ${urlObj.url}`);
1751
- })
1752
- )
1753
- )
1754
- ),
1755
- { concurrency: "unbounded" }
1756
- );
1757
- const finalMap = yield* Ref.get(domainMap);
1758
- const finalSkipped = yield* Ref.get(skipped);
1759
- const deduplicated = Array.from(HashMap.values(finalMap));
1705
+ for (const urlObj of urls) {
1706
+ const normalizeResult = yield* Effect.either(normalizeUrl(urlObj.url, strategy));
1707
+ if (normalizeResult._tag === "Left") {
1708
+ invalidCount++;
1709
+ skipped.push({ url: urlObj.url, reason: `Invalid URL: ${normalizeResult.left.message}` });
1710
+ yield* Effect.logWarning(`Invalid URL skipped: ${urlObj.url}`);
1711
+ continue;
1712
+ }
1713
+ const normalized = normalizeResult.right;
1714
+ const key = strategy.wwwHandling === "preserve" ? normalized.normalized : normalized.domain;
1715
+ const existingOption = HashMap.get(domainMap, key);
1716
+ if (Option.isNone(existingOption)) {
1717
+ domainMap = HashMap.set(domainMap, key, urlObj);
1718
+ } else {
1719
+ const existing = existingOption.value;
1720
+ let shouldReplace = false;
1721
+ if (strategy.wwwHandling === "prefer-www") {
1722
+ const existingHasWww = existing.url.includes("://www.");
1723
+ const newHasWww = urlObj.url.includes("://www.");
1724
+ shouldReplace = !existingHasWww && newHasWww;
1725
+ } else if (strategy.wwwHandling === "prefer-non-www") {
1726
+ const existingHasWww = existing.url.includes("://www.");
1727
+ const newHasWww = urlObj.url.includes("://www.");
1728
+ shouldReplace = existingHasWww && !newHasWww;
1729
+ }
1730
+ if (shouldReplace) {
1731
+ domainMap = HashMap.set(domainMap, key, urlObj);
1732
+ skipped.push({ url: existing.url, reason: `Replaced by preferred variant: ${urlObj.url}` });
1733
+ } else {
1734
+ skipped.push({ url: urlObj.url, reason: `Duplicate of: ${existing.url}` });
1735
+ }
1736
+ }
1737
+ }
1738
+ const deduplicated = Array.from(HashMap.values(domainMap));
1760
1739
  return {
1761
1740
  deduplicated,
1762
- skipped: finalSkipped,
1741
+ skipped,
1763
1742
  stats: {
1764
1743
  total: urls.length,
1765
1744
  unique: deduplicated.length,
1766
- duplicates: finalSkipped.filter((s) => s.reason.startsWith("Duplicate")).length,
1745
+ duplicates: skipped.filter((s) => s.reason.startsWith("Duplicate")).length,
1767
1746
  invalid: invalidCount
1768
1747
  }
1769
1748
  };