@patchstack/connect 0.2.10 → 0.3.0

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 CHANGED
@@ -1161,13 +1161,17 @@ function injectMarker(html, snippet) {
1161
1161
 
1162
1162
  // src/protect/install.ts
1163
1163
  import { readFileSync, writeFileSync, existsSync as existsSync2, mkdirSync, copyFileSync } from "fs";
1164
- import { join } from "path";
1164
+ import { join, dirname } from "path";
1165
1165
  import { fileURLToPath } from "url";
1166
- var TEMPLATES = fileURLToPath(new URL("./protect/templates/", import.meta.url));
1166
+ var TEMPLATES = join(dirname(fileURLToPath(import.meta.url)), "protect", "templates");
1167
+ var APP = process.cwd();
1168
+ var PS_DIR = join(APP, "src/integrations/patchstack");
1169
+ var read = (p) => readFileSync(p, "utf8");
1170
+ var log = (msg) => console.log(`patchstack protect: ${msg}`);
1167
1171
  var CLIENT_TUNNEL = [
1168
1172
  "",
1169
- " // PATCHSTACK auto-guard: in the browser, tunnel Supabase traffic through the app's own",
1170
- " // server guard (same-origin) so payloads are inspected before they reach Supabase.",
1173
+ " // PATCHSTACK: in the browser, tunnel Supabase traffic through the app's own server guard",
1174
+ " // (same-origin) so payloads are inspected before they reach Supabase.",
1171
1175
  " if (typeof window !== 'undefined') {",
1172
1176
  " const target = typeof input === 'string' ? input : input instanceof Request ? input.url : String(input);",
1173
1177
  " const method = init?.method ?? (input instanceof Request ? input.method : 'GET');",
@@ -1183,7 +1187,7 @@ var START_IMPORTS = [
1183
1187
  ].join("\n");
1184
1188
  var START_MIDDLEWARE = [
1185
1189
  "",
1186
- "// Patchstack auto-guard: intercept the tunneled data traffic before anything else runs.",
1190
+ "// Patchstack guard: intercept the tunneled Supabase traffic before anything else runs.",
1187
1191
  "const patchstackGuard = createMiddleware().server(async ({ next }) => {",
1188
1192
  " const request = getRequest();",
1189
1193
  " if (request) {",
@@ -1194,76 +1198,38 @@ var START_MIDDLEWARE = [
1194
1198
  "});",
1195
1199
  ""
1196
1200
  ].join("\n");
1197
- var PS_DIR_REL = "src/integrations/patchstack";
1198
- function log(msg) {
1199
- console.log(`patchstack protect: ${msg}`);
1200
- }
1201
1201
  function detectSupportedStack(cwd) {
1202
1202
  const pkgPath = join(cwd, "package.json");
1203
1203
  if (!existsSync2(pkgPath)) return false;
1204
- const pkg = JSON.parse(readFileSync(pkgPath, "utf8"));
1204
+ const pkg = JSON.parse(read(pkgPath));
1205
1205
  const deps = { ...pkg.dependencies, ...pkg.devDependencies };
1206
1206
  return Boolean(deps["@tanstack/react-start"]) && existsSync2(join(cwd, "src/start.ts")) && existsSync2(join(cwd, "src/integrations/supabase/client.ts"));
1207
1207
  }
1208
- function generateManifest(cwd) {
1209
- const pkg = JSON.parse(readFileSync(join(cwd, "package.json"), "utf8"));
1210
- const deps = { ...pkg.dependencies, ...pkg.devDependencies };
1211
- const packages = {};
1212
- for (const name of Object.keys(deps)) {
1213
- let version = String(deps[name]).replace(/^[^\d]*/, "");
1214
- const installed = join(cwd, "node_modules", name, "package.json");
1215
- if (existsSync2(installed)) {
1216
- try {
1217
- version = JSON.parse(readFileSync(installed, "utf8")).version;
1218
- } catch {
1219
- }
1220
- }
1221
- packages[name] = version;
1222
- }
1223
- mkdirSync(join(cwd, PS_DIR_REL), { recursive: true });
1224
- const body = "// Patchstack auto-guard manifest \u2014 generated from the lockfile.\n// Do not edit; regenerated by `patchstack-connect protect --manifest` (wired into prebuild).\nexport const manifest = " + JSON.stringify({ packages }, null, 2) + ";\n";
1225
- writeFileSync(join(cwd, PS_DIR_REL, "manifest.js"), body);
1226
- log(`wrote manifest.js (${Object.keys(packages).length} packages)`);
1227
- }
1228
1208
  function scaffold(cwd) {
1229
- const dst = join(cwd, PS_DIR_REL);
1209
+ const dst = join(cwd, "src/integrations/patchstack");
1230
1210
  mkdirSync(dst, { recursive: true });
1231
- copyFileSync(join(TEMPLATES, "engine.js"), join(dst, "engine.js"));
1232
- copyFileSync(join(TEMPLATES, "engine.d.ts"), join(dst, "engine.d.ts"));
1233
- copyFileSync(join(TEMPLATES, "manifest.d.ts"), join(dst, "manifest.d.ts"));
1234
- copyFileSync(join(TEMPLATES, "rules.json"), join(dst, "rules.json"));
1235
1211
  copyFileSync(join(TEMPLATES, "guard.ts"), join(dst, "guard.ts"));
1236
- log("scaffolded engine.js (+types), rules.json, guard.ts");
1212
+ copyFileSync(join(TEMPLATES, "rules.json"), join(dst, "rules.json"));
1213
+ log("scaffolded guard.ts + rules.json");
1237
1214
  }
1238
1215
  function patchClient(cwd) {
1239
1216
  const p = join(cwd, "src/integrations/supabase/client.ts");
1240
- let s = readFileSync(p, "utf8");
1241
- if (s.includes("x-ps-target")) {
1242
- log("client.ts already wired");
1243
- return;
1244
- }
1217
+ let s = read(p);
1218
+ if (s.includes("x-ps-target")) return log("client.ts already wired");
1245
1219
  const anchor = "headers.set('apikey', supabaseKey);";
1246
- if (!s.includes(anchor)) {
1247
- log("client.ts anchor not found (template changed?) \u2014 skipping client patch");
1248
- return;
1249
- }
1250
- s = s.replace(anchor, anchor + "\n" + CLIENT_TUNNEL);
1251
- writeFileSync(p, s);
1220
+ if (!s.includes(anchor)) return log("client.ts anchor not found \u2014 skipping (template changed?)");
1221
+ writeFileSync(p, s.replace(anchor, anchor + "\n" + CLIENT_TUNNEL));
1252
1222
  log("patched client.ts (tunnel Supabase through the guard)");
1253
1223
  }
1254
1224
  function patchStart(cwd) {
1255
1225
  const p = join(cwd, "src/start.ts");
1256
- let s = readFileSync(p, "utf8");
1257
- if (s.includes("patchstackGuard")) {
1258
- log("start.ts already wired");
1259
- return;
1260
- }
1226
+ let s = read(p);
1227
+ if (s.includes("patchstackGuard")) return log("start.ts already wired");
1261
1228
  const importAnchor = 'import { createStart, createMiddleware } from "@tanstack/react-start";';
1262
1229
  const exportAnchor = "export const startInstance";
1263
1230
  const rmAnchor = "requestMiddleware: [";
1264
1231
  if (!s.includes(importAnchor) || !s.includes(exportAnchor) || !s.includes(rmAnchor)) {
1265
- log("start.ts anchors not found (template changed?) \u2014 skipping start patch");
1266
- return;
1232
+ return log("start.ts anchors not found \u2014 skipping (template changed?)");
1267
1233
  }
1268
1234
  s = s.replace(importAnchor, importAnchor + "\n" + START_IMPORTS);
1269
1235
  s = s.replace(exportAnchor, START_MIDDLEWARE + "\n" + exportAnchor);
@@ -1271,36 +1237,14 @@ function patchStart(cwd) {
1271
1237
  writeFileSync(p, s);
1272
1238
  log("patched start.ts (registered the guard as request middleware)");
1273
1239
  }
1274
- function wirePrebuild(cwd) {
1275
- const p = join(cwd, "package.json");
1276
- const pkg = JSON.parse(readFileSync(p, "utf8"));
1277
- pkg.scripts = pkg.scripts || {};
1278
- const step = "patchstack-connect protect --manifest";
1279
- const prebuild = pkg.scripts.prebuild || "";
1280
- if (prebuild.includes(step)) {
1281
- log("prebuild already refreshes the manifest");
1282
- return;
1283
- }
1284
- pkg.scripts.prebuild = prebuild ? prebuild + " && " + step : step;
1285
- writeFileSync(p, JSON.stringify(pkg, null, 2) + "\n");
1286
- log("wired manifest refresh into prebuild");
1287
- }
1288
- function runProtect(cwd, opts = {}) {
1289
- if (opts.manifestOnly) {
1290
- generateManifest(cwd);
1291
- return;
1292
- }
1240
+ function runProtect(cwd) {
1293
1241
  if (!detectSupportedStack(cwd)) {
1294
- log(
1295
- "runtime protection currently supports TanStack Start + Supabase apps; stack not detected \u2014 skipping."
1296
- );
1242
+ log("runtime protection currently supports TanStack Start + Supabase apps; stack not detected \u2014 skipping.");
1297
1243
  return;
1298
1244
  }
1299
1245
  scaffold(cwd);
1300
1246
  patchClient(cwd);
1301
1247
  patchStart(cwd);
1302
- generateManifest(cwd);
1303
- wirePrebuild(cwd);
1304
1248
  log("done \u2014 guard wired and always-on (blocks by default). Set PATCHSTACK_MODE=dry-run for log-only.");
1305
1249
  }
1306
1250
 
@@ -1316,9 +1260,8 @@ Usage:
1316
1260
  patchstack-connect status [options] Show current configuration
1317
1261
  patchstack-connect mark-build [options] Stamp built HTML with a production flag +
1318
1262
  build fingerprint (run as a postbuild step)
1319
- patchstack-connect protect [--manifest] Install runtime protection (the guard) into
1320
- a server-side JS app. --manifest only
1321
- regenerates the package manifest (prebuild).
1263
+ patchstack-connect protect Install always-on runtime protection (the
1264
+ guard) into a TanStack Start + Supabase app.
1322
1265
  patchstack-connect help Print this message
1323
1266
 
1324
1267
  Options (for scan and status):
@@ -1456,9 +1399,9 @@ async function runScan(args) {
1456
1399
  }
1457
1400
  return 0;
1458
1401
  }
1459
- async function runProtectCommand(args) {
1402
+ async function runProtectCommand(_args) {
1460
1403
  try {
1461
- runProtect(process.cwd(), { manifestOnly: args.flags.get("manifest") === true });
1404
+ runProtect(process.cwd());
1462
1405
  } catch (err) {
1463
1406
  console.warn(`patchstack protect: skipped (${err.message}).`);
1464
1407
  }