@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 +39 -60
- package/dist/index.js.map +1 -1
- package/dist/lib/utils/url-deduplication.d.ts.map +1 -1
- package/package.json +1 -1
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,
|
|
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
|
-
|
|
1703
|
-
const skipped =
|
|
1702
|
+
let domainMap = HashMap.empty();
|
|
1703
|
+
const skipped = [];
|
|
1704
1704
|
let invalidCount = 0;
|
|
1705
|
-
|
|
1706
|
-
|
|
1707
|
-
|
|
1708
|
-
|
|
1709
|
-
|
|
1710
|
-
|
|
1711
|
-
|
|
1712
|
-
|
|
1713
|
-
|
|
1714
|
-
|
|
1715
|
-
|
|
1716
|
-
|
|
1717
|
-
|
|
1718
|
-
|
|
1719
|
-
|
|
1720
|
-
|
|
1721
|
-
|
|
1722
|
-
|
|
1723
|
-
|
|
1724
|
-
|
|
1725
|
-
|
|
1726
|
-
|
|
1727
|
-
|
|
1728
|
-
|
|
1729
|
-
|
|
1730
|
-
|
|
1731
|
-
|
|
1732
|
-
|
|
1733
|
-
|
|
1734
|
-
|
|
1735
|
-
|
|
1736
|
-
|
|
1737
|
-
|
|
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
|
|
1741
|
+
skipped,
|
|
1763
1742
|
stats: {
|
|
1764
1743
|
total: urls.length,
|
|
1765
1744
|
unique: deduplicated.length,
|
|
1766
|
-
duplicates:
|
|
1745
|
+
duplicates: skipped.filter((s) => s.reason.startsWith("Duplicate")).length,
|
|
1767
1746
|
invalid: invalidCount
|
|
1768
1747
|
}
|
|
1769
1748
|
};
|