hanzio 1.0.2 → 1.0.3
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.d.ts +3 -0
- package/dist/index.js +149 -0
- package/package.json +1 -1
- package/src/index.ts +3 -0
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -879,6 +879,150 @@ class PQueue {
|
|
|
879
879
|
}
|
|
880
880
|
}
|
|
881
881
|
|
|
882
|
+
// src/jwt/index.ts
|
|
883
|
+
import * as jose from "jose";
|
|
884
|
+
async function jwtSign(payload, secret) {
|
|
885
|
+
const secretKey = new TextEncoder().encode(secret);
|
|
886
|
+
return await new jose.SignJWT(payload).setProtectedHeader({ alg: "HS256" }).setIssuedAt().sign(secretKey);
|
|
887
|
+
}
|
|
888
|
+
async function jwtVerify2(token, secret) {
|
|
889
|
+
try {
|
|
890
|
+
const secretKey = new TextEncoder().encode(secret);
|
|
891
|
+
const { payload } = await jose.jwtVerify(token, secretKey);
|
|
892
|
+
return payload;
|
|
893
|
+
} catch (_error) {
|
|
894
|
+
return null;
|
|
895
|
+
}
|
|
896
|
+
}
|
|
897
|
+
|
|
898
|
+
// src/gzip/index.ts
|
|
899
|
+
import pako from "pako";
|
|
900
|
+
function compressString(input) {
|
|
901
|
+
const uint8Array = new TextEncoder().encode(input);
|
|
902
|
+
const compressed = pako.deflate(uint8Array);
|
|
903
|
+
return btoa(String.fromCharCode.apply(null, compressed));
|
|
904
|
+
}
|
|
905
|
+
function decompressString(compressedString) {
|
|
906
|
+
const compressedData = new Uint8Array(atob(compressedString).split("").map((char) => char.charCodeAt(0)));
|
|
907
|
+
const decompressed = pako.inflate(compressedData);
|
|
908
|
+
return new TextDecoder().decode(decompressed);
|
|
909
|
+
}
|
|
910
|
+
|
|
911
|
+
// src/sitemap/index.ts
|
|
912
|
+
var createTimeoutController = (timeout) => {
|
|
913
|
+
const controller = new AbortController;
|
|
914
|
+
setTimeout(() => controller.abort(), timeout);
|
|
915
|
+
return controller;
|
|
916
|
+
};
|
|
917
|
+
var discoverSitemaps = async (domain) => {
|
|
918
|
+
const normalizedDomain = domain.replace(/\/$/, "");
|
|
919
|
+
const commonLocations = [
|
|
920
|
+
"/sitemap.xml",
|
|
921
|
+
"/sitemap_index.xml",
|
|
922
|
+
"/sitemap/",
|
|
923
|
+
"/sitemaps.xml",
|
|
924
|
+
"/sitemap/sitemap.xml"
|
|
925
|
+
];
|
|
926
|
+
const sitemaps = new Set;
|
|
927
|
+
const requestOptions = {
|
|
928
|
+
headers: {
|
|
929
|
+
"User-Agent": "SitemapCrawler/1.0"
|
|
930
|
+
},
|
|
931
|
+
signal: createTimeoutController(5000).signal
|
|
932
|
+
};
|
|
933
|
+
try {
|
|
934
|
+
const robotsTxt = await fetch(`${normalizedDomain}/robots.txt`, {
|
|
935
|
+
...requestOptions
|
|
936
|
+
});
|
|
937
|
+
if (robotsTxt.ok) {
|
|
938
|
+
const text = await robotsTxt.text();
|
|
939
|
+
const sitemapMatches = text.match(/Sitemap: (.*)/gi);
|
|
940
|
+
if (sitemapMatches) {
|
|
941
|
+
sitemapMatches.forEach((match) => {
|
|
942
|
+
const url = match.replace("Sitemap:", "").trim();
|
|
943
|
+
sitemaps.add(url);
|
|
944
|
+
});
|
|
945
|
+
}
|
|
946
|
+
if (sitemaps.size > 0) {
|
|
947
|
+
return [...sitemaps];
|
|
948
|
+
}
|
|
949
|
+
}
|
|
950
|
+
} catch (error) {
|
|
951
|
+
console.warn("Could not fetch robots.txt:", error instanceof Error ? error.message : "Unknown error");
|
|
952
|
+
}
|
|
953
|
+
for (const location of commonLocations) {
|
|
954
|
+
try {
|
|
955
|
+
const response = await fetch(`${normalizedDomain}${location}`, {
|
|
956
|
+
...requestOptions,
|
|
957
|
+
method: "HEAD"
|
|
958
|
+
});
|
|
959
|
+
if (response.ok) {
|
|
960
|
+
return [`${normalizedDomain}${location}`];
|
|
961
|
+
}
|
|
962
|
+
} catch (_error) {}
|
|
963
|
+
}
|
|
964
|
+
return [];
|
|
965
|
+
};
|
|
966
|
+
var getDomainSitemap = async (domain, options = {}) => {
|
|
967
|
+
const {
|
|
968
|
+
filterIndexes,
|
|
969
|
+
concurrency = 10,
|
|
970
|
+
maxDepth = 5,
|
|
971
|
+
timeout = 1e4
|
|
972
|
+
} = options;
|
|
973
|
+
let normalizedDomain = domain.replace(/\/$/, "");
|
|
974
|
+
if (!normalizedDomain.startsWith("http")) {
|
|
975
|
+
normalizedDomain = `https://${normalizedDomain}`;
|
|
976
|
+
}
|
|
977
|
+
const sitemapUrls = await discoverSitemaps(normalizedDomain);
|
|
978
|
+
if (sitemapUrls.length === 0) {
|
|
979
|
+
throw new Error("No sitemaps found for the domain");
|
|
980
|
+
}
|
|
981
|
+
const queue = new PQueue({ concurrency });
|
|
982
|
+
const result = {
|
|
983
|
+
links: new Set,
|
|
984
|
+
visited: new Set
|
|
985
|
+
};
|
|
986
|
+
const locRegex = /<loc>([^<]+)<\/loc>/g;
|
|
987
|
+
const isXmlSitemap = (url) => url.endsWith(".xml") || url.toLowerCase().includes("sitemap");
|
|
988
|
+
const processSitemap = async (sitemapUrl, depth = 0) => {
|
|
989
|
+
if (depth > maxDepth || result.visited.has(sitemapUrl)) {
|
|
990
|
+
return;
|
|
991
|
+
}
|
|
992
|
+
result.visited.add(sitemapUrl);
|
|
993
|
+
try {
|
|
994
|
+
const controller = createTimeoutController(timeout);
|
|
995
|
+
const response = await fetch(sitemapUrl, {
|
|
996
|
+
headers: {
|
|
997
|
+
"User-Agent": "SitemapCrawler/1.0"
|
|
998
|
+
},
|
|
999
|
+
signal: controller.signal
|
|
1000
|
+
});
|
|
1001
|
+
if (!response.ok)
|
|
1002
|
+
throw new Error(`HTTP error! status: ${response.status}`);
|
|
1003
|
+
const data = await response.text();
|
|
1004
|
+
const matches = [...data.matchAll(locRegex)].map((match) => match[1].trim()).filter((link) => {
|
|
1005
|
+
if (filterIndexes && isXmlSitemap(link)) {
|
|
1006
|
+
return link.includes(filterIndexes);
|
|
1007
|
+
}
|
|
1008
|
+
return true;
|
|
1009
|
+
});
|
|
1010
|
+
for (const link of matches) {
|
|
1011
|
+
if (isXmlSitemap(link)) {
|
|
1012
|
+
queue.add(() => processSitemap(link, depth + 1));
|
|
1013
|
+
} else {
|
|
1014
|
+
result.links.add(link);
|
|
1015
|
+
}
|
|
1016
|
+
}
|
|
1017
|
+
} catch (error) {
|
|
1018
|
+
console.warn(`Failed to process sitemap ${sitemapUrl}:`, error instanceof Error ? error.message : "Unknown error");
|
|
1019
|
+
}
|
|
1020
|
+
};
|
|
1021
|
+
await Promise.all(sitemapUrls.map((sitemapUrl) => queue.add(() => processSitemap(sitemapUrl))));
|
|
1022
|
+
await queue.onIdle();
|
|
1023
|
+
return [...result.links];
|
|
1024
|
+
};
|
|
1025
|
+
|
|
882
1026
|
// src/typedswitch/index.ts
|
|
883
1027
|
function runSwitch(value, keyOrCases, casesOrDefault, defaultCase) {
|
|
884
1028
|
let key;
|
|
@@ -1640,6 +1784,8 @@ export {
|
|
|
1640
1784
|
logBanner,
|
|
1641
1785
|
linearRegressionTrend,
|
|
1642
1786
|
keyBy,
|
|
1787
|
+
jwtVerify2 as jwtVerify,
|
|
1788
|
+
jwtSign,
|
|
1643
1789
|
jsonToZodSchemaMapper,
|
|
1644
1790
|
isSuccess,
|
|
1645
1791
|
isFailure,
|
|
@@ -1649,16 +1795,19 @@ export {
|
|
|
1649
1795
|
getUniqueValues,
|
|
1650
1796
|
getPreviousReportPeriod,
|
|
1651
1797
|
getHeatmapCellStyle,
|
|
1798
|
+
getDomainSitemap,
|
|
1652
1799
|
getDomainFaviconUrl,
|
|
1653
1800
|
generateId,
|
|
1654
1801
|
formatPeriodLabel,
|
|
1655
1802
|
findArrayDifferenceByKey,
|
|
1656
1803
|
extractNumber,
|
|
1804
|
+
decompressString,
|
|
1657
1805
|
createZId,
|
|
1658
1806
|
createResolveColorsByKeys,
|
|
1659
1807
|
createCoolLogger,
|
|
1660
1808
|
createApiWrapper,
|
|
1661
1809
|
createApiClient,
|
|
1810
|
+
compressString,
|
|
1662
1811
|
compact,
|
|
1663
1812
|
colorize,
|
|
1664
1813
|
chunkArray,
|
package/package.json
CHANGED