@patchstack/connect 0.5.24 → 0.5.25

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.
@@ -0,0 +1,54 @@
1
+ // Server-only helpers for Next.js middleware and App Router handlers.
2
+ import { createProtection } from "@patchstack/connect/protect";
3
+ import fallbackRules from "./patchstack.rules.json";
4
+
5
+ const PS_SITE_UUID = "__PATCHSTACK_SITE_UUID__";
6
+ type Protection = Awaited<ReturnType<typeof createProtection>>;
7
+ let _protection: Promise<Protection> | undefined;
8
+ let warned = false;
9
+
10
+ async function getProtection() {
11
+ if (!_protection) {
12
+ _protection = buildProtection().catch((error) => {
13
+ _protection = undefined;
14
+ throw error;
15
+ });
16
+ }
17
+ return _protection;
18
+ }
19
+
20
+ async function buildProtection() {
21
+ const mode = process.env.PATCHSTACK_MODE === "dry-run" ? "dry-run" : "block";
22
+ const siteUuid = PS_SITE_UUID.startsWith("__") ? process.env.PATCHSTACK_SITE_UUID : PS_SITE_UUID;
23
+ const token = process.env.PATCHSTACK_WAF_TOKEN;
24
+ const common = { mode, egress: true } as const;
25
+ return createProtection(
26
+ siteUuid
27
+ ? { ...common, siteUuid, rules: fallbackRules as never, cacheDir: ".patchstack" }
28
+ : token
29
+ ? { ...common, token, cacheDir: ".patchstack" }
30
+ : { ...common, rules: fallbackRules as never },
31
+ );
32
+ }
33
+
34
+ export async function getPatchstackProtection() {
35
+ return getProtection().catch((error: unknown) => {
36
+ if (!warned) {
37
+ warned = true;
38
+ console.warn(
39
+ "[patchstack] protection is unavailable; traffic may pass through unscreened until a later attempt succeeds. Reported once per process. Cause: " +
40
+ (error instanceof Error ? error.message : String(error)),
41
+ );
42
+ }
43
+ return null;
44
+ });
45
+ }
46
+
47
+ // The handler's expression is evaluated once; its rejection remains the application's error.
48
+ export async function screenPatchstackResponse(
49
+ response: Response | Promise<Response>,
50
+ request: Request,
51
+ protection: Protection | null,
52
+ ): Promise<Response> {
53
+ return protection ? protection.screenResponse(await response, request) : response;
54
+ }
package/dist/protect.cjs CHANGED
@@ -9450,7 +9450,7 @@ async function createProtection(options = {}) {
9450
9450
  allowHosts
9451
9451
  });
9452
9452
  }
9453
- const cwd = options.cwd ?? (typeof process !== "undefined" ? process.cwd() : void 0);
9453
+ const cwd = workingDirectory(options);
9454
9454
  const live = Boolean(options.siteUuid || options.token);
9455
9455
  const refreshSecret = options.refreshSecret ?? (typeof process !== "undefined" ? process.env.PATCHSTACK_REFRESH_SECRET : void 0);
9456
9456
  const refreshable = live && (options.refreshMs > 0 || Boolean(refreshSecret));
@@ -9544,8 +9544,17 @@ function resolveMode(options, bundle) {
9544
9544
  return "dry-run";
9545
9545
  }
9546
9546
  var CREDENTIAL_FILES = [".patchstackrc.local.json", ".patchstackrc.json"];
9547
+ function workingDirectory(options) {
9548
+ if (options?.cwd !== void 0) return options.cwd;
9549
+ try {
9550
+ return typeof process !== "undefined" && typeof process.cwd === "function" ? process.cwd() : void 0;
9551
+ } catch {
9552
+ return void 0;
9553
+ }
9554
+ }
9547
9555
  async function readCredentialField(options, field) {
9548
- if (typeof process === "undefined" || typeof process.cwd !== "function") return void 0;
9556
+ const cwd = workingDirectory(options);
9557
+ if (!cwd) return void 0;
9549
9558
  let readFileSync2;
9550
9559
  let join;
9551
9560
  try {
@@ -9555,7 +9564,6 @@ async function readCredentialField(options, field) {
9555
9564
  } catch {
9556
9565
  return void 0;
9557
9566
  }
9558
- const cwd = options?.cwd ?? process.cwd();
9559
9567
  for (const filename of CREDENTIAL_FILES) {
9560
9568
  try {
9561
9569
  const value = JSON.parse(readFileSync2(join(cwd, filename), "utf8"))?.[field];