@patchstack/connect 0.3.13 → 0.3.14
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/cli.js +28 -35
- package/dist/cli.js.map +1 -1
- package/dist/protect/templates/guard.ts +3 -9
- package/dist/protect.cjs +2 -82
- package/dist/protect.cjs.map +1 -1
- package/dist/protect.d.ts +0 -4
- package/dist/protect.js +2 -81
- package/dist/protect.js.map +1 -1
- package/package.json +1 -1
package/dist/protect.js
CHANGED
|
@@ -899,64 +899,6 @@ var RuleEngine = class {
|
|
|
899
899
|
}
|
|
900
900
|
};
|
|
901
901
|
|
|
902
|
-
// src/protect/engine/pulse-client.js
|
|
903
|
-
var DEFAULT_BASE_URL2 = "https://api.patchstack.com/monitor/pulse";
|
|
904
|
-
var DEFAULT_CACHE_TTL2 = 3e5;
|
|
905
|
-
var PulseRuleClient = class {
|
|
906
|
-
#siteUuid;
|
|
907
|
-
#baseUrl;
|
|
908
|
-
#cacheTtl;
|
|
909
|
-
#cache = null;
|
|
910
|
-
#cacheTime = null;
|
|
911
|
-
constructor({ siteUuid, baseUrl, cacheTtl } = {}) {
|
|
912
|
-
this.#siteUuid = siteUuid ?? process.env.PATCHSTACK_SITE_UUID;
|
|
913
|
-
this.#baseUrl = baseUrl ?? process.env.PATCHSTACK_PULSE_RULES_URL ?? DEFAULT_BASE_URL2;
|
|
914
|
-
this.#cacheTtl = cacheTtl ?? DEFAULT_CACHE_TTL2;
|
|
915
|
-
if (!this.#siteUuid) {
|
|
916
|
-
throw new Error("Patchstack site UUID is required. Pass { siteUuid } or set PATCHSTACK_SITE_UUID.");
|
|
917
|
-
}
|
|
918
|
-
}
|
|
919
|
-
async getRules() {
|
|
920
|
-
const now = Date.now();
|
|
921
|
-
if (this.#cache && this.#cacheTime && now - this.#cacheTime < this.#cacheTtl) {
|
|
922
|
-
return this.#cache;
|
|
923
|
-
}
|
|
924
|
-
const url = `${this.#baseUrl}/rules/${encodeURIComponent(this.#siteUuid)}`;
|
|
925
|
-
try {
|
|
926
|
-
const response = await fetch(url, {
|
|
927
|
-
method: "GET",
|
|
928
|
-
headers: { Accept: "application/json" },
|
|
929
|
-
signal: AbortSignal.timeout(3e4)
|
|
930
|
-
});
|
|
931
|
-
if (!response.ok) {
|
|
932
|
-
return { success: false, error: `API returned ${response.status}`, firewall: [], whitelists: [], whitelist_keys: {} };
|
|
933
|
-
}
|
|
934
|
-
const data = await response.json();
|
|
935
|
-
const result = {
|
|
936
|
-
success: true,
|
|
937
|
-
firewall: Array.isArray(data.firewall) ? data.firewall : [],
|
|
938
|
-
whitelists: Array.isArray(data.whitelists) ? data.whitelists : [],
|
|
939
|
-
whitelist_keys: data.whitelist_keys ?? {}
|
|
940
|
-
};
|
|
941
|
-
this.#cache = result;
|
|
942
|
-
this.#cacheTime = now;
|
|
943
|
-
return result;
|
|
944
|
-
} catch (err) {
|
|
945
|
-
return {
|
|
946
|
-
success: false,
|
|
947
|
-
error: err.name === "TimeoutError" ? "Request timed out" : err.message,
|
|
948
|
-
firewall: [],
|
|
949
|
-
whitelists: [],
|
|
950
|
-
whitelist_keys: {}
|
|
951
|
-
};
|
|
952
|
-
}
|
|
953
|
-
}
|
|
954
|
-
clearCache() {
|
|
955
|
-
this.#cache = null;
|
|
956
|
-
this.#cacheTime = null;
|
|
957
|
-
}
|
|
958
|
-
};
|
|
959
|
-
|
|
960
902
|
// src/protect/engine/fetch.js
|
|
961
903
|
async function fromFetchRequest(request) {
|
|
962
904
|
const url = new URL(request.url);
|
|
@@ -1579,25 +1521,8 @@ function leakResponse() {
|
|
|
1579
1521
|
});
|
|
1580
1522
|
}
|
|
1581
1523
|
async function resolveRules(options) {
|
|
1582
|
-
if (options.
|
|
1583
|
-
|
|
1584
|
-
const res = await client.getRules();
|
|
1585
|
-
if (res.success) {
|
|
1586
|
-
const bundle = normalizeBundle(res);
|
|
1587
|
-
cacheWrite(options.cacheDir, bundle);
|
|
1588
|
-
return bundle;
|
|
1589
|
-
}
|
|
1590
|
-
const cached = cacheRead(options.cacheDir);
|
|
1591
|
-
if (cached) {
|
|
1592
|
-
options.onError?.(new Error(`pulse rule fetch failed (${res.error}); using cached bundle`));
|
|
1593
|
-
return normalizeBundle(cached);
|
|
1594
|
-
}
|
|
1595
|
-
if (options.rules) {
|
|
1596
|
-
options.onError?.(new Error(`pulse rule fetch failed (${res.error}); using bundled fallback`));
|
|
1597
|
-
return normalizeBundle(options.rules);
|
|
1598
|
-
}
|
|
1599
|
-
options.onError?.(new Error(`pulse rule fetch failed (${res.error}); no cache \u2014 running with no rules`));
|
|
1600
|
-
return emptyBundle();
|
|
1524
|
+
if (options.rules) {
|
|
1525
|
+
return normalizeBundle(options.rules);
|
|
1601
1526
|
}
|
|
1602
1527
|
if (options.token) {
|
|
1603
1528
|
const client = new PatchstackRuleClient({ token: options.token, baseUrl: options.baseUrl });
|
|
@@ -1615,9 +1540,6 @@ async function resolveRules(options) {
|
|
|
1615
1540
|
options.onError?.(new Error(`rule fetch failed (${res.error}); no cache \u2014 running with no rules`));
|
|
1616
1541
|
return emptyBundle();
|
|
1617
1542
|
}
|
|
1618
|
-
if (options.rules) {
|
|
1619
|
-
return normalizeBundle(options.rules);
|
|
1620
|
-
}
|
|
1621
1543
|
return emptyBundle();
|
|
1622
1544
|
}
|
|
1623
1545
|
function normalizeBundle(b) {
|
|
@@ -1668,7 +1590,6 @@ function defaultOnDetect({ phase, mode, category, rule, message }) {
|
|
|
1668
1590
|
}
|
|
1669
1591
|
export {
|
|
1670
1592
|
GUARD_PATH,
|
|
1671
|
-
PulseRuleClient,
|
|
1672
1593
|
createProtection,
|
|
1673
1594
|
createServerFnGuard,
|
|
1674
1595
|
createSupabaseGuard
|