@simplr-ai/express 1.0.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/LICENSE +21 -0
- package/README.md +122 -0
- package/dist/fastify.cjs +241 -0
- package/dist/fastify.cjs.map +1 -0
- package/dist/fastify.d.cts +11 -0
- package/dist/fastify.d.ts +11 -0
- package/dist/fastify.js +214 -0
- package/dist/fastify.js.map +1 -0
- package/dist/hono.cjs +246 -0
- package/dist/hono.cjs.map +1 -0
- package/dist/hono.d.cts +25 -0
- package/dist/hono.d.ts +25 -0
- package/dist/hono.js +218 -0
- package/dist/hono.js.map +1 -0
- package/dist/index.cjs +244 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +96 -0
- package/dist/index.d.ts +96 -0
- package/dist/index.js +210 -0
- package/dist/index.js.map +1 -0
- package/dist/next.cjs +269 -0
- package/dist/next.cjs.map +1 -0
- package/dist/next.d.cts +41 -0
- package/dist/next.d.ts +41 -0
- package/dist/next.js +241 -0
- package/dist/next.js.map +1 -0
- package/dist/types-CmzpR5S4.d.cts +115 -0
- package/dist/types-CmzpR5S4.d.ts +115 -0
- package/package.json +108 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/client.ts","../src/core.ts","../src/express.ts"],"sourcesContent":["/**\n * Thin internal client.\n *\n * This is a ~40-line port of the minimal call path from `@simplr-ai/node`\n * (`apiRequest` + `check` + `edge.ingestLogs`) so that this package builds and\n * tests without an unbuilt workspace dependency or any network access.\n *\n * In production this package pairs with `@simplr-ai/node`; the endpoints, auth\n * header (`X-API-Key`), `{ success, message, content }` envelope unwrapping, and\n * 15s default timeout here are byte-for-byte compatible with that SDK and the\n * Simplr API contract.\n */\nimport type { CheckInput, CheckResult, EdgeLogEntry } from \"./types.js\";\n\nconst DEFAULT_BASE_URL = \"https://api.simplr.sh\";\n\n/** Thrown when the Simplr API returns a non-2xx response or the request fails. */\nexport class SimplrError extends Error {\n readonly status: number;\n readonly body: unknown;\n constructor(message: string, status: number, body: unknown) {\n super(message);\n this.name = \"SimplrError\";\n this.status = status;\n this.body = body;\n }\n}\n\nexport interface ClientConfig {\n apiKey: string;\n baseUrl?: string;\n timeoutMs?: number;\n fetch?: typeof fetch;\n}\n\nexport interface SimplrClient {\n check(input: CheckInput): Promise<CheckResult>;\n ingestLogs(deviceId: string, logs: EdgeLogEntry[]): Promise<unknown>;\n}\n\nexport function createClient(config: ClientConfig): SimplrClient {\n if (!config?.apiKey) throw new Error(\"Simplr: `apiKey` is required\");\n const baseUrl = (config.baseUrl || DEFAULT_BASE_URL).replace(/\\/+$/, \"\");\n const timeoutMs = config.timeoutMs ?? 15000;\n const fetchImpl = config.fetch ?? globalThis.fetch;\n if (typeof fetchImpl !== \"function\") {\n throw new Error(\n \"Simplr: no global fetch available — use Node 18+ or pass `fetch` in options\",\n );\n }\n\n async function apiRequest<T>(method: string, path: string, body?: unknown): Promise<T> {\n const controller = new AbortController();\n const timer = setTimeout(() => controller.abort(), timeoutMs);\n try {\n const res = await fetchImpl(`${baseUrl}${path}`, {\n method,\n headers: { \"Content-Type\": \"application/json\", \"X-API-Key\": config.apiKey },\n body: body !== undefined ? JSON.stringify(body) : undefined,\n signal: controller.signal,\n });\n const text = await res.text();\n let parsed: any;\n try {\n parsed = text ? JSON.parse(text) : undefined;\n } catch {\n parsed = text;\n }\n if (!res.ok) {\n const message =\n (parsed && (parsed.message || parsed.error)) || `Simplr API error ${res.status}`;\n throw new SimplrError(message, res.status, parsed);\n }\n return (parsed && typeof parsed === \"object\" && \"content\" in parsed\n ? parsed.content\n : parsed) as T;\n } catch (err) {\n if (err instanceof SimplrError) throw err;\n if (err instanceof Error && err.name === \"AbortError\") {\n throw new SimplrError(`Request to ${path} timed out after ${timeoutMs}ms`, 0, null);\n }\n throw new SimplrError(err instanceof Error ? err.message : \"Network error\", 0, null);\n } finally {\n clearTimeout(timer);\n }\n }\n\n return {\n check: (input) => apiRequest<CheckResult>(\"POST\", \"/v1/check\", input),\n ingestLogs: (deviceId, logs) =>\n apiRequest(\"POST\", \"/v1/edge/logs\", { device_id: deviceId, logs }),\n };\n}\n","/**\n * Framework-agnostic guard engine.\n *\n * `createGuard` returns an object that, given any request-like object, builds a\n * `CheckInput`, calls `/v1/check`, decides whether to block, optionally ships an\n * edge log, and fails open on error. Framework adapters (express/fastify/hono/\n * next) are thin wrappers over this engine.\n */\nimport { createClient, type SimplrClient } from \"./client.js\";\nimport type {\n BlockThreshold,\n CheckInput,\n CheckResult,\n GuardContext,\n GuardOptions,\n GuardOutcome,\n RiskLevel,\n} from \"./types.js\";\n\nconst RISK_ORDER: Record<RiskLevel, number> = {\n low: 0,\n medium: 1,\n high: 2,\n critical: 3,\n};\n\nconst DEFAULT_DEVICE_ID = \"simplr-edge-middleware\";\nconst DEFAULT_THRESHOLD: RiskLevel = \"high\";\n\n/** Numeric rank of a risk level. Unknown levels rank as the lowest (0). */\nexport function riskRank(level: RiskLevel | string | undefined): number {\n return RISK_ORDER[level as RiskLevel] ?? 0;\n}\n\n/** True when `level` is at least `min` in the low<medium<high<critical ordering. */\nexport function riskAtLeast(level: RiskLevel | string | undefined, min: RiskLevel): boolean {\n return riskRank(level) >= riskRank(min);\n}\n\n/** Read a header from either a plain bag (lowercase keys) or a Headers-like getter. */\nfunction readHeader(headers: unknown, name: string): string | undefined {\n if (!headers) return undefined;\n const h = headers as any;\n if (typeof h.get === \"function\") {\n const v = h.get(name);\n return v == null ? undefined : String(v);\n }\n // Plain object: try the lowercase key, then a case-insensitive scan.\n const lower = name.toLowerCase();\n if (lower in h && h[lower] != null) return String(h[lower]);\n for (const key of Object.keys(h)) {\n if (key.toLowerCase() === lower && h[key] != null) return String(h[key]);\n }\n return undefined;\n}\n\n/** Best-effort client IP from common header and socket locations. */\nfunction extractIp(req: any): string | undefined {\n const fwd = readHeader(req?.headers, \"x-forwarded-for\");\n if (fwd) return fwd.split(\",\")[0]!.trim();\n const real = readHeader(req?.headers, \"x-real-ip\");\n if (real) return real;\n return (\n req?.ip ||\n req?.socket?.remoteAddress ||\n req?.connection?.remoteAddress ||\n undefined\n );\n}\n\n/**\n * Default request → CheckInput extractor. Pulls the client IP and user-agent into\n * `device`, and lifts `email`/`phone` from a parsed JSON body when present.\n */\nexport function defaultExtract(req: any): CheckInput {\n const input: CheckInput = {};\n\n const device: Record<string, unknown> = {};\n const ip = extractIp(req);\n if (ip) device.ip = ip;\n const ua = readHeader(req?.headers, \"user-agent\");\n if (ua) device.user_agent = ua;\n if (Object.keys(device).length > 0) input.device = device;\n\n const body = req?.body;\n if (body && typeof body === \"object\") {\n const b = body as Record<string, unknown>;\n if (typeof b.email === \"string\") input.email = b.email;\n if (typeof b.phone === \"string\") input.phone = b.phone;\n }\n\n return input;\n}\n\nfunction normalizeBlock(\n block: GuardOptions[\"block\"],\n): (result: CheckResult) => boolean {\n if (typeof block === \"function\") return block;\n const threshold = (block as BlockThreshold | undefined)?.whenRiskAtLeast ?? DEFAULT_THRESHOLD;\n return (result) => riskAtLeast(result.risk_level, threshold);\n}\n\nexport interface Guard<Req = any> {\n /** The underlying thin Simplr client (check + edge log ingestion). */\n readonly client: SimplrClient;\n /** Run the full engine against a request; never throws when `failOpen`. */\n run(req: Req): Promise<GuardOutcome>;\n}\n\nexport function createGuard<Req = any>(options: GuardOptions<Req>): Guard<Req> {\n if (!options?.apiKey) throw new Error(\"createGuard: `apiKey` is required\");\n\n const client = createClient({\n apiKey: options.apiKey,\n baseUrl: options.baseUrl,\n timeoutMs: options.timeoutMs,\n fetch: options.fetch,\n });\n\n const extract = options.extract ?? defaultExtract;\n const shouldBlock = normalizeBlock(options.block);\n const failOpen = options.failOpen ?? true;\n const ingestLogs = options.ingestLogs ?? false;\n const deviceId = options.deviceId ?? DEFAULT_DEVICE_ID;\n\n async function run(req: Req): Promise<GuardOutcome> {\n const input = await extract(req);\n let result: CheckResult;\n try {\n result = await client.check(input);\n } catch (error) {\n if (failOpen) {\n return { result: null, blocked: false, input, error };\n }\n throw error;\n }\n\n const blocked = shouldBlock(result);\n const ctx: GuardContext<Req> = { req, input, blocked };\n\n if (options.onResult) {\n try {\n await options.onResult(result, ctx);\n } catch {\n // onResult is observational; never let it break the request.\n }\n }\n\n if (ingestLogs) {\n try {\n await client.ingestLogs(deviceId, [\n {\n category: \"security\",\n level: blocked ? \"warn\" : \"info\",\n message: blocked ? \"simplr guard blocked request\" : \"simplr guard checked request\",\n risk_level: result.risk_level,\n risk_score: result.risk_score,\n },\n ]);\n } catch {\n // Log shipping is best-effort and must never affect the request.\n }\n }\n\n return { result, blocked, input };\n }\n\n return { client, run };\n}\n\n/** Standard JSON envelope returned to the client on a blocked request. */\nexport function blockEnvelope(result: CheckResult) {\n return {\n error: \"request_blocked\",\n message: \"This request was blocked by Simplr fraud protection.\",\n risk_level: result.risk_level,\n risk_score: result.risk_score,\n };\n}\n","/**\n * Express adapter. Attaches `req.simplr` (the CheckResult) and, on block, sends\n * a 403 JSON envelope. On a Simplr error it fails open by default (calls next()).\n */\nimport { blockEnvelope, createGuard } from \"./core.js\";\nimport type { CheckResult, GuardOptions } from \"./types.js\";\n\n// Loosely typed so we never hard-depend on `express` at type or runtime level.\ntype ExpressRequest = any;\ntype ExpressResponse = {\n status(code: number): ExpressResponse;\n json(body: unknown): unknown;\n};\ntype ExpressNext = (err?: unknown) => void;\n\nexport type ExpressMiddleware = (\n req: ExpressRequest,\n res: ExpressResponse,\n next: ExpressNext,\n) => Promise<void> | void;\n\n/**\n * Build Express middleware that runs a Simplr check per request.\n *\n * ```ts\n * import express from \"express\";\n * import { simplrExpress } from \"@simplr-ai/express\";\n * const app = express();\n * app.use(express.json());\n * app.use(simplrExpress({ apiKey: process.env.SIMPLR_API_KEY! }));\n * ```\n */\nexport function simplrExpress(options: GuardOptions): ExpressMiddleware {\n const guard = createGuard(options);\n const failOpen = options.failOpen ?? true;\n\n return async function simplrMiddleware(req, res, next) {\n try {\n const outcome = await guard.run(req);\n // Attach the result (null when failed-open) for downstream handlers.\n (req as any).simplr = outcome.result;\n if (outcome.blocked && outcome.result) {\n res.status(403).json(blockEnvelope(outcome.result));\n return;\n }\n next();\n } catch (err) {\n // guard.run already swallows check errors when failOpen; this catches\n // anything else (e.g. a throwing custom extract). Honor failOpen here too.\n if (failOpen) {\n (req as any).simplr = null;\n next();\n return;\n }\n next(err);\n }\n };\n}\n\ndeclare global {\n // eslint-disable-next-line @typescript-eslint/no-namespace\n namespace Express {\n interface Request {\n /** Simplr check result for this request (null if the check failed open). */\n simplr?: CheckResult | null;\n }\n }\n}\n"],"mappings":";AAcA,IAAM,mBAAmB;AAGlB,IAAM,cAAN,cAA0B,MAAM;AAAA,EAC5B;AAAA,EACA;AAAA,EACT,YAAY,SAAiB,QAAgB,MAAe;AAC1D,UAAM,OAAO;AACb,SAAK,OAAO;AACZ,SAAK,SAAS;AACd,SAAK,OAAO;AAAA,EACd;AACF;AAcO,SAAS,aAAa,QAAoC;AAC/D,MAAI,CAAC,QAAQ,OAAQ,OAAM,IAAI,MAAM,8BAA8B;AACnE,QAAM,WAAW,OAAO,WAAW,kBAAkB,QAAQ,QAAQ,EAAE;AACvE,QAAM,YAAY,OAAO,aAAa;AACtC,QAAM,YAAY,OAAO,SAAS,WAAW;AAC7C,MAAI,OAAO,cAAc,YAAY;AACnC,UAAM,IAAI;AAAA,MACR;AAAA,IACF;AAAA,EACF;AAEA,iBAAe,WAAc,QAAgB,MAAc,MAA4B;AACrF,UAAM,aAAa,IAAI,gBAAgB;AACvC,UAAM,QAAQ,WAAW,MAAM,WAAW,MAAM,GAAG,SAAS;AAC5D,QAAI;AACF,YAAM,MAAM,MAAM,UAAU,GAAG,OAAO,GAAG,IAAI,IAAI;AAAA,QAC/C;AAAA,QACA,SAAS,EAAE,gBAAgB,oBAAoB,aAAa,OAAO,OAAO;AAAA,QAC1E,MAAM,SAAS,SAAY,KAAK,UAAU,IAAI,IAAI;AAAA,QAClD,QAAQ,WAAW;AAAA,MACrB,CAAC;AACD,YAAM,OAAO,MAA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AAc,QAAqB;AACjD,SAAO;AAAA,IACL,OAAO;AAAA,IACP,SAAS;AAAA,IACT,YAAY,OAAO;AAAA,IACnB,YAAY,OAAO;AAAA,EACrB;AACF;;;AClJO,SAAS,cAAc,SAA0C;AACtE,QAAM,QAAQ,YAAY,OAAO;AACjC,QAAM,WAAW,QAAQ,YAAY;AAErC,SAAO,eAAe,iBAAiB,KAAK,KAAK,MAAM;AACrD,QAAI;AACF,YAAM,UAAU,MAAM,MAAM,IAAI,GAAG;AAEnC,MAAC,IAAY,SAAS,QAAQ;AAC9B,UAAI,QAAQ,WAAW,QAAQ,QAAQ;AACrC,YAAI,OAAO,GAAG,EAAE,KAAK,cAAc,QAAQ,MAAM,CAAC;AAClD;AAAA,MACF;AACA,WAAK;AAAA,IACP,SAAS,KAAK;AAGZ,UAAI,UAAU;AACZ,QAAC,IAAY,SAAS;AACtB,aAAK;AACL;AAAA,MACF;AACA,WAAK,GAAG;AAAA,IACV;AAAA,EACF;AACF;","names":[]}
|
package/dist/next.cjs
ADDED
|
@@ -0,0 +1,269 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/next.ts
|
|
21
|
+
var next_exports = {};
|
|
22
|
+
__export(next_exports, {
|
|
23
|
+
simplrNextMiddleware: () => simplrNextMiddleware,
|
|
24
|
+
withSimplr: () => withSimplr
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(next_exports);
|
|
27
|
+
|
|
28
|
+
// src/client.ts
|
|
29
|
+
var DEFAULT_BASE_URL = "https://api.simplr.sh";
|
|
30
|
+
var SimplrError = class extends Error {
|
|
31
|
+
status;
|
|
32
|
+
body;
|
|
33
|
+
constructor(message, status, body) {
|
|
34
|
+
super(message);
|
|
35
|
+
this.name = "SimplrError";
|
|
36
|
+
this.status = status;
|
|
37
|
+
this.body = body;
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
function createClient(config) {
|
|
41
|
+
if (!config?.apiKey) throw new Error("Simplr: `apiKey` is required");
|
|
42
|
+
const baseUrl = (config.baseUrl || DEFAULT_BASE_URL).replace(/\/+$/, "");
|
|
43
|
+
const timeoutMs = config.timeoutMs ?? 15e3;
|
|
44
|
+
const fetchImpl = config.fetch ?? globalThis.fetch;
|
|
45
|
+
if (typeof fetchImpl !== "function") {
|
|
46
|
+
throw new Error(
|
|
47
|
+
"Simplr: no global fetch available \u2014 use Node 18+ or pass `fetch` in options"
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
async function apiRequest(method, path, body) {
|
|
51
|
+
const controller = new AbortController();
|
|
52
|
+
const timer = setTimeout(() => controller.abort(), timeoutMs);
|
|
53
|
+
try {
|
|
54
|
+
const res = await fetchImpl(`${baseUrl}${path}`, {
|
|
55
|
+
method,
|
|
56
|
+
headers: { "Content-Type": "application/json", "X-API-Key": config.apiKey },
|
|
57
|
+
body: body !== void 0 ? JSON.stringify(body) : void 0,
|
|
58
|
+
signal: controller.signal
|
|
59
|
+
});
|
|
60
|
+
const text = await res.text();
|
|
61
|
+
let parsed;
|
|
62
|
+
try {
|
|
63
|
+
parsed = text ? JSON.parse(text) : void 0;
|
|
64
|
+
} catch {
|
|
65
|
+
parsed = text;
|
|
66
|
+
}
|
|
67
|
+
if (!res.ok) {
|
|
68
|
+
const message = parsed && (parsed.message || parsed.error) || `Simplr API error ${res.status}`;
|
|
69
|
+
throw new SimplrError(message, res.status, parsed);
|
|
70
|
+
}
|
|
71
|
+
return parsed && typeof parsed === "object" && "content" in parsed ? parsed.content : parsed;
|
|
72
|
+
} catch (err) {
|
|
73
|
+
if (err instanceof SimplrError) throw err;
|
|
74
|
+
if (err instanceof Error && err.name === "AbortError") {
|
|
75
|
+
throw new SimplrError(`Request to ${path} timed out after ${timeoutMs}ms`, 0, null);
|
|
76
|
+
}
|
|
77
|
+
throw new SimplrError(err instanceof Error ? err.message : "Network error", 0, null);
|
|
78
|
+
} finally {
|
|
79
|
+
clearTimeout(timer);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
return {
|
|
83
|
+
check: (input) => apiRequest("POST", "/v1/check", input),
|
|
84
|
+
ingestLogs: (deviceId, logs) => apiRequest("POST", "/v1/edge/logs", { device_id: deviceId, logs })
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// src/core.ts
|
|
89
|
+
var RISK_ORDER = {
|
|
90
|
+
low: 0,
|
|
91
|
+
medium: 1,
|
|
92
|
+
high: 2,
|
|
93
|
+
critical: 3
|
|
94
|
+
};
|
|
95
|
+
var DEFAULT_DEVICE_ID = "simplr-edge-middleware";
|
|
96
|
+
var DEFAULT_THRESHOLD = "high";
|
|
97
|
+
function riskRank(level) {
|
|
98
|
+
return RISK_ORDER[level] ?? 0;
|
|
99
|
+
}
|
|
100
|
+
function riskAtLeast(level, min) {
|
|
101
|
+
return riskRank(level) >= riskRank(min);
|
|
102
|
+
}
|
|
103
|
+
function readHeader(headers, name) {
|
|
104
|
+
if (!headers) return void 0;
|
|
105
|
+
const h = headers;
|
|
106
|
+
if (typeof h.get === "function") {
|
|
107
|
+
const v = h.get(name);
|
|
108
|
+
return v == null ? void 0 : String(v);
|
|
109
|
+
}
|
|
110
|
+
const lower = name.toLowerCase();
|
|
111
|
+
if (lower in h && h[lower] != null) return String(h[lower]);
|
|
112
|
+
for (const key of Object.keys(h)) {
|
|
113
|
+
if (key.toLowerCase() === lower && h[key] != null) return String(h[key]);
|
|
114
|
+
}
|
|
115
|
+
return void 0;
|
|
116
|
+
}
|
|
117
|
+
function extractIp(req) {
|
|
118
|
+
const fwd = readHeader(req?.headers, "x-forwarded-for");
|
|
119
|
+
if (fwd) return fwd.split(",")[0].trim();
|
|
120
|
+
const real = readHeader(req?.headers, "x-real-ip");
|
|
121
|
+
if (real) return real;
|
|
122
|
+
return req?.ip || req?.socket?.remoteAddress || req?.connection?.remoteAddress || void 0;
|
|
123
|
+
}
|
|
124
|
+
function defaultExtract(req) {
|
|
125
|
+
const input = {};
|
|
126
|
+
const device = {};
|
|
127
|
+
const ip = extractIp(req);
|
|
128
|
+
if (ip) device.ip = ip;
|
|
129
|
+
const ua = readHeader(req?.headers, "user-agent");
|
|
130
|
+
if (ua) device.user_agent = ua;
|
|
131
|
+
if (Object.keys(device).length > 0) input.device = device;
|
|
132
|
+
const body = req?.body;
|
|
133
|
+
if (body && typeof body === "object") {
|
|
134
|
+
const b = body;
|
|
135
|
+
if (typeof b.email === "string") input.email = b.email;
|
|
136
|
+
if (typeof b.phone === "string") input.phone = b.phone;
|
|
137
|
+
}
|
|
138
|
+
return input;
|
|
139
|
+
}
|
|
140
|
+
function normalizeBlock(block) {
|
|
141
|
+
if (typeof block === "function") return block;
|
|
142
|
+
const threshold = block?.whenRiskAtLeast ?? DEFAULT_THRESHOLD;
|
|
143
|
+
return (result) => riskAtLeast(result.risk_level, threshold);
|
|
144
|
+
}
|
|
145
|
+
function createGuard(options) {
|
|
146
|
+
if (!options?.apiKey) throw new Error("createGuard: `apiKey` is required");
|
|
147
|
+
const client = createClient({
|
|
148
|
+
apiKey: options.apiKey,
|
|
149
|
+
baseUrl: options.baseUrl,
|
|
150
|
+
timeoutMs: options.timeoutMs,
|
|
151
|
+
fetch: options.fetch
|
|
152
|
+
});
|
|
153
|
+
const extract = options.extract ?? defaultExtract;
|
|
154
|
+
const shouldBlock = normalizeBlock(options.block);
|
|
155
|
+
const failOpen = options.failOpen ?? true;
|
|
156
|
+
const ingestLogs = options.ingestLogs ?? false;
|
|
157
|
+
const deviceId = options.deviceId ?? DEFAULT_DEVICE_ID;
|
|
158
|
+
async function run(req) {
|
|
159
|
+
const input = await extract(req);
|
|
160
|
+
let result;
|
|
161
|
+
try {
|
|
162
|
+
result = await client.check(input);
|
|
163
|
+
} catch (error) {
|
|
164
|
+
if (failOpen) {
|
|
165
|
+
return { result: null, blocked: false, input, error };
|
|
166
|
+
}
|
|
167
|
+
throw error;
|
|
168
|
+
}
|
|
169
|
+
const blocked = shouldBlock(result);
|
|
170
|
+
const ctx = { req, input, blocked };
|
|
171
|
+
if (options.onResult) {
|
|
172
|
+
try {
|
|
173
|
+
await options.onResult(result, ctx);
|
|
174
|
+
} catch {
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
if (ingestLogs) {
|
|
178
|
+
try {
|
|
179
|
+
await client.ingestLogs(deviceId, [
|
|
180
|
+
{
|
|
181
|
+
category: "security",
|
|
182
|
+
level: blocked ? "warn" : "info",
|
|
183
|
+
message: blocked ? "simplr guard blocked request" : "simplr guard checked request",
|
|
184
|
+
risk_level: result.risk_level,
|
|
185
|
+
risk_score: result.risk_score
|
|
186
|
+
}
|
|
187
|
+
]);
|
|
188
|
+
} catch {
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
return { result, blocked, input };
|
|
192
|
+
}
|
|
193
|
+
return { client, run };
|
|
194
|
+
}
|
|
195
|
+
function blockEnvelope(result) {
|
|
196
|
+
return {
|
|
197
|
+
error: "request_blocked",
|
|
198
|
+
message: "This request was blocked by Simplr fraud protection.",
|
|
199
|
+
risk_level: result.risk_level,
|
|
200
|
+
risk_score: result.risk_score
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
// src/next.ts
|
|
205
|
+
async function loadNextResponse() {
|
|
206
|
+
try {
|
|
207
|
+
const spec = "next/server";
|
|
208
|
+
const mod = await import(
|
|
209
|
+
/* @vite-ignore */
|
|
210
|
+
spec
|
|
211
|
+
);
|
|
212
|
+
const NextResponse = mod.NextResponse;
|
|
213
|
+
if (!NextResponse) return null;
|
|
214
|
+
return {
|
|
215
|
+
next: () => NextResponse.next(),
|
|
216
|
+
json: (body, init) => NextResponse.json(body, init)
|
|
217
|
+
};
|
|
218
|
+
} catch {
|
|
219
|
+
return null;
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
function simplrNextMiddleware(options) {
|
|
223
|
+
const guard = createGuard(options);
|
|
224
|
+
const failOpen = options.failOpen ?? true;
|
|
225
|
+
return async function middleware(req) {
|
|
226
|
+
const next = await loadNextResponse();
|
|
227
|
+
const pass = () => next ? next.next() : new Response(null, { status: 200 });
|
|
228
|
+
const deny = (body) => next ? next.json(body, { status: 403 }) : jsonResponse(body, 403);
|
|
229
|
+
try {
|
|
230
|
+
const outcome = await guard.run(req);
|
|
231
|
+
if (outcome.blocked && outcome.result) {
|
|
232
|
+
return deny(blockEnvelope(outcome.result));
|
|
233
|
+
}
|
|
234
|
+
return pass();
|
|
235
|
+
} catch (err) {
|
|
236
|
+
if (failOpen) return pass();
|
|
237
|
+
throw err;
|
|
238
|
+
}
|
|
239
|
+
};
|
|
240
|
+
}
|
|
241
|
+
function withSimplr(handler, options) {
|
|
242
|
+
const guard = createGuard(options);
|
|
243
|
+
const failOpen = options.failOpen ?? true;
|
|
244
|
+
return async function wrapped(req, ...rest) {
|
|
245
|
+
try {
|
|
246
|
+
const outcome = await guard.run(req);
|
|
247
|
+
req.simplr = outcome.result;
|
|
248
|
+
if (outcome.blocked && outcome.result) {
|
|
249
|
+
return jsonResponse(blockEnvelope(outcome.result), 403);
|
|
250
|
+
}
|
|
251
|
+
} catch (err) {
|
|
252
|
+
if (!failOpen) throw err;
|
|
253
|
+
req.simplr = null;
|
|
254
|
+
}
|
|
255
|
+
return handler(req, ...rest);
|
|
256
|
+
};
|
|
257
|
+
}
|
|
258
|
+
function jsonResponse(body, status) {
|
|
259
|
+
return new Response(JSON.stringify(body), {
|
|
260
|
+
status,
|
|
261
|
+
headers: { "content-type": "application/json" }
|
|
262
|
+
});
|
|
263
|
+
}
|
|
264
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
265
|
+
0 && (module.exports = {
|
|
266
|
+
simplrNextMiddleware,
|
|
267
|
+
withSimplr
|
|
268
|
+
});
|
|
269
|
+
//# sourceMappingURL=next.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/next.ts","../src/client.ts","../src/core.ts"],"sourcesContent":["/**\n * Next.js adapters.\n *\n * - `simplrNextMiddleware(options)` → an Edge/Node `middleware`-style function\n * that takes a `NextRequest` and returns `NextResponse.next()` or a 403.\n * - `withSimplr(handler, options)` → wraps an app-router route handler so the\n * check runs before your handler; on block it returns a 403 `Response`.\n *\n * `next` is an optional peer dependency and is lazy-imported only when needed, so\n * importing this module never requires `next` to be installed.\n */\nimport { blockEnvelope, createGuard } from \"./core.js\";\nimport type { CheckResult, GuardOptions } from \"./types.js\";\n\n// Loose types so we never hard-depend on `next`'s type packages.\ntype NextRequestLike = {\n headers: { get(name: string): string | null };\n /** NextRequest exposes `.ip` on some runtimes. */\n ip?: string;\n json?: () => Promise<unknown>;\n [key: string]: unknown;\n};\ntype NextResponseLike = Response;\ntype RouteHandler = (req: NextRequestLike, ...rest: any[]) => Response | Promise<Response>;\n\nasync function loadNextResponse(): Promise<{\n next: () => NextResponseLike;\n json: (body: unknown, init?: ResponseInit) => NextResponseLike;\n} | null> {\n try {\n // Lazy, optional. Avoids a hard dependency on `next`. The module specifier is\n // built dynamically so the type checker does not require `next` to be present.\n const spec = \"next/server\";\n const mod: any = await import(/* @vite-ignore */ spec);\n const NextResponse = mod.NextResponse;\n if (!NextResponse) return null;\n return {\n next: () => NextResponse.next(),\n json: (body, init) => NextResponse.json(body, init),\n };\n } catch {\n return null;\n }\n}\n\n/**\n * Edge/Node middleware factory for `middleware.ts`.\n *\n * ```ts\n * // middleware.ts\n * import { simplrNextMiddleware } from \"@simplr-ai/express/next\";\n * export const middleware = simplrNextMiddleware({ apiKey: process.env.SIMPLR_API_KEY! });\n * export const config = { matcher: [\"/api/:path*\"] };\n * ```\n */\nexport function simplrNextMiddleware(options: GuardOptions<NextRequestLike>) {\n const guard = createGuard<NextRequestLike>(options);\n const failOpen = options.failOpen ?? true;\n\n return async function middleware(req: NextRequestLike): Promise<NextResponseLike> {\n const next = await loadNextResponse();\n const pass = () => (next ? next.next() : new Response(null, { status: 200 }));\n const deny = (body: unknown) =>\n next ? next.json(body, { status: 403 }) : jsonResponse(body, 403);\n\n try {\n const outcome = await guard.run(req);\n if (outcome.blocked && outcome.result) {\n return deny(blockEnvelope(outcome.result));\n }\n return pass();\n } catch (err) {\n if (failOpen) return pass();\n throw err;\n }\n };\n}\n\n/**\n * Wrap an app-router route handler. The `CheckResult` is attached to the request\n * as `req.simplr` for your handler; blocked requests return a 403 before it runs.\n *\n * ```ts\n * // app/api/signup/route.ts\n * import { withSimplr } from \"@simplr-ai/express/next\";\n * export const POST = withSimplr(async (req) => {\n * // req.simplr is the CheckResult\n * return Response.json({ ok: true });\n * }, { apiKey: process.env.SIMPLR_API_KEY! });\n * ```\n */\nexport function withSimplr(\n handler: RouteHandler,\n options: GuardOptions<NextRequestLike>,\n): RouteHandler {\n const guard = createGuard<NextRequestLike>(options);\n const failOpen = options.failOpen ?? true;\n\n return async function wrapped(req, ...rest) {\n try {\n const outcome = await guard.run(req);\n (req as any).simplr = outcome.result;\n if (outcome.blocked && outcome.result) {\n return jsonResponse(blockEnvelope(outcome.result), 403);\n }\n } catch (err) {\n if (!failOpen) throw err;\n (req as any).simplr = null;\n }\n return handler(req, ...rest);\n };\n}\n\nfunction jsonResponse(body: unknown, status: number): Response {\n return new Response(JSON.stringify(body), {\n status,\n headers: { \"content-type\": \"application/json\" },\n });\n}\n\nexport type { CheckResult };\n","/**\n * Thin internal client.\n *\n * This is a ~40-line port of the minimal call path from `@simplr-ai/node`\n * (`apiRequest` + `check` + `edge.ingestLogs`) so that this package builds and\n * tests without an unbuilt workspace dependency or any network access.\n *\n * In production this package pairs with `@simplr-ai/node`; the endpoints, auth\n * header (`X-API-Key`), `{ success, message, content }` envelope unwrapping, and\n * 15s default timeout here are byte-for-byte compatible with that SDK and the\n * Simplr API contract.\n */\nimport type { CheckInput, CheckResult, EdgeLogEntry } from \"./types.js\";\n\nconst DEFAULT_BASE_URL = \"https://api.simplr.sh\";\n\n/** Thrown when the Simplr API returns a non-2xx response or the request fails. */\nexport class SimplrError extends Error {\n readonly status: number;\n readonly body: unknown;\n constructor(message: string, status: number, body: unknown) {\n super(message);\n this.name = \"SimplrError\";\n this.status = status;\n this.body = body;\n }\n}\n\nexport interface ClientConfig {\n apiKey: string;\n baseUrl?: string;\n timeoutMs?: number;\n fetch?: typeof fetch;\n}\n\nexport interface SimplrClient {\n check(input: CheckInput): Promise<CheckResult>;\n ingestLogs(deviceId: string, logs: EdgeLogEntry[]): Promise<unknown>;\n}\n\nexport function createClient(config: ClientConfig): SimplrClient {\n if (!config?.apiKey) throw new Error(\"Simplr: `apiKey` is required\");\n const baseUrl = (config.baseUrl || DEFAULT_BASE_URL).replace(/\\/+$/, \"\");\n const timeoutMs = config.timeoutMs ?? 15000;\n const fetchImpl = config.fetch ?? globalThis.fetch;\n if (typeof fetchImpl !== \"function\") {\n throw new Error(\n \"Simplr: no global fetch available — use Node 18+ or pass `fetch` in options\",\n );\n }\n\n async function apiRequest<T>(method: string, path: string, body?: unknown): Promise<T> {\n const controller = new AbortController();\n const timer = setTimeout(() => controller.abort(), timeoutMs);\n try {\n const res = await fetchImpl(`${baseUrl}${path}`, {\n method,\n headers: { \"Content-Type\": \"application/json\", \"X-API-Key\": config.apiKey },\n body: body !== undefined ? JSON.stringify(body) : undefined,\n signal: controller.signal,\n });\n const text = await res.text();\n let parsed: any;\n try {\n parsed = text ? JSON.parse(text) : undefined;\n } catch {\n parsed = text;\n }\n if (!res.ok) {\n const message =\n (parsed && (parsed.message || parsed.error)) || `Simplr API error ${res.status}`;\n throw new SimplrError(message, res.status, parsed);\n }\n return (parsed && typeof parsed === \"object\" && \"content\" in parsed\n ? parsed.content\n : parsed) as T;\n } catch (err) {\n if (err instanceof SimplrError) throw err;\n if (err instanceof Error && err.name === \"AbortError\") {\n throw new SimplrError(`Request to ${path} timed out after ${timeoutMs}ms`, 0, null);\n }\n throw new SimplrError(err instanceof Error ? err.message : \"Network error\", 0, null);\n } finally {\n clearTimeout(timer);\n }\n }\n\n return {\n check: (input) => apiRequest<CheckResult>(\"POST\", \"/v1/check\", input),\n ingestLogs: (deviceId, logs) =>\n apiRequest(\"POST\", \"/v1/edge/logs\", { device_id: deviceId, logs }),\n };\n}\n","/**\n * Framework-agnostic guard engine.\n *\n * `createGuard` returns an object that, given any request-like object, builds a\n * `CheckInput`, calls `/v1/check`, decides whether to block, optionally ships an\n * edge log, and fails open on error. Framework adapters (express/fastify/hono/\n * next) are thin wrappers over this engine.\n */\nimport { createClient, type SimplrClient } from \"./client.js\";\nimport type {\n BlockThreshold,\n CheckInput,\n CheckResult,\n GuardContext,\n GuardOptions,\n GuardOutcome,\n RiskLevel,\n} from \"./types.js\";\n\nconst RISK_ORDER: Record<RiskLevel, number> = {\n low: 0,\n medium: 1,\n high: 2,\n critical: 3,\n};\n\nconst DEFAULT_DEVICE_ID = \"simplr-edge-middleware\";\nconst DEFAULT_THRESHOLD: RiskLevel = \"high\";\n\n/** Numeric rank of a risk level. Unknown levels rank as the lowest (0). */\nexport function riskRank(level: RiskLevel | string | undefined): number {\n return RISK_ORDER[level as RiskLevel] ?? 0;\n}\n\n/** True when `level` is at least `min` in the low<medium<high<critical ordering. */\nexport function riskAtLeast(level: RiskLevel | string | undefined, min: RiskLevel): boolean {\n return riskRank(level) >= riskRank(min);\n}\n\n/** Read a header from either a plain bag (lowercase keys) or a Headers-like getter. */\nfunction readHeader(headers: unknown, name: string): string | undefined {\n if (!headers) return undefined;\n const h = headers as any;\n if (typeof h.get === \"function\") {\n const v = h.get(name);\n return v == null ? undefined : String(v);\n }\n // Plain object: try the lowercase key, then a case-insensitive scan.\n const lower = name.toLowerCase();\n if (lower in h && h[lower] != null) return String(h[lower]);\n for (const key of Object.keys(h)) {\n if (key.toLowerCase() === lower && h[key] != null) return String(h[key]);\n }\n return undefined;\n}\n\n/** Best-effort client IP from common header and socket locations. */\nfunction extractIp(req: any): string | undefined {\n const fwd = readHeader(req?.headers, \"x-forwarded-for\");\n if (fwd) return fwd.split(\",\")[0]!.trim();\n const real = readHeader(req?.headers, \"x-real-ip\");\n if (real) return real;\n return (\n req?.ip ||\n req?.socket?.remoteAddress ||\n req?.connection?.remoteAddress ||\n undefined\n );\n}\n\n/**\n * Default request → CheckInput extractor. Pulls the client IP and user-agent into\n * `device`, and lifts `email`/`phone` from a parsed JSON body when present.\n */\nexport function defaultExtract(req: any): CheckInput {\n const input: CheckInput = {};\n\n const device: Record<string, unknown> = {};\n const ip = extractIp(req);\n if (ip) device.ip = ip;\n const ua = readHeader(req?.headers, \"user-agent\");\n if (ua) device.user_agent = ua;\n if (Object.keys(device).length > 0) input.device = device;\n\n const body = req?.body;\n if (body && typeof body === \"object\") {\n const b = body as Record<string, unknown>;\n if (typeof b.email === \"string\") input.email = b.email;\n if (typeof b.phone === \"string\") input.phone = b.phone;\n }\n\n return input;\n}\n\nfunction normalizeBlock(\n block: GuardOptions[\"block\"],\n): (result: CheckResult) => boolean {\n if (typeof block === \"function\") return block;\n const threshold = (block as BlockThreshold | undefined)?.whenRiskAtLeast ?? DEFAULT_THRESHOLD;\n return (result) => riskAtLeast(result.risk_level, threshold);\n}\n\nexport interface Guard<Req = any> {\n /** The underlying thin Simplr client (check + edge log ingestion). */\n readonly client: SimplrClient;\n /** Run the full engine against a request; never throws when `failOpen`. */\n run(req: Req): Promise<GuardOutcome>;\n}\n\nexport function createGuard<Req = any>(options: GuardOptions<Req>): Guard<Req> {\n if (!options?.apiKey) throw new Error(\"createGuard: `apiKey` is required\");\n\n const client = createClient({\n apiKey: options.apiKey,\n baseUrl: options.baseUrl,\n timeoutMs: options.timeoutMs,\n fetch: options.fetch,\n });\n\n const extract = options.extract ?? defaultExtract;\n const shouldBlock = normalizeBlock(options.block);\n const failOpen = options.failOpen ?? true;\n const ingestLogs = options.ingestLogs ?? false;\n const deviceId = options.deviceId ?? DEFAULT_DEVICE_ID;\n\n async function run(req: Req): Promise<GuardOutcome> {\n const input = await extract(req);\n let result: CheckResult;\n try {\n result = await client.check(input);\n } catch (error) {\n if (failOpen) {\n return { result: null, blocked: false, input, error };\n }\n throw error;\n }\n\n const blocked = shouldBlock(result);\n const ctx: GuardContext<Req> = { req, input, blocked };\n\n if (options.onResult) {\n try {\n await options.onResult(result, ctx);\n } catch {\n // onResult is observational; never let it break the request.\n }\n }\n\n if (ingestLogs) {\n try {\n await client.ingestLogs(deviceId, [\n {\n category: \"security\",\n level: blocked ? \"warn\" : \"info\",\n message: blocked ? \"simplr guard blocked request\" : \"simplr guard checked request\",\n risk_level: result.risk_level,\n risk_score: result.risk_score,\n },\n ]);\n } catch {\n // Log shipping is best-effort and must never affect the request.\n }\n }\n\n return { result, blocked, input };\n }\n\n return { client, run };\n}\n\n/** Standard JSON envelope returned to the client on a blocked request. */\nexport function blockEnvelope(result: CheckResult) {\n return {\n error: \"request_blocked\",\n message: \"This request was blocked by Simplr fraud protection.\",\n risk_level: result.risk_level,\n risk_score: result.risk_score,\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACcA,IAAM,mBAAmB;AAGlB,IAAM,cAAN,cAA0B,MAAM;AAAA,EAC5B;AAAA,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D,CAAC;AACH;","names":[]}
|
package/dist/next.d.cts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { G as GuardOptions } from './types-CmzpR5S4.cjs';
|
|
2
|
+
export { C as CheckResult } from './types-CmzpR5S4.cjs';
|
|
3
|
+
|
|
4
|
+
type NextRequestLike = {
|
|
5
|
+
headers: {
|
|
6
|
+
get(name: string): string | null;
|
|
7
|
+
};
|
|
8
|
+
/** NextRequest exposes `.ip` on some runtimes. */
|
|
9
|
+
ip?: string;
|
|
10
|
+
json?: () => Promise<unknown>;
|
|
11
|
+
[key: string]: unknown;
|
|
12
|
+
};
|
|
13
|
+
type NextResponseLike = Response;
|
|
14
|
+
type RouteHandler = (req: NextRequestLike, ...rest: any[]) => Response | Promise<Response>;
|
|
15
|
+
/**
|
|
16
|
+
* Edge/Node middleware factory for `middleware.ts`.
|
|
17
|
+
*
|
|
18
|
+
* ```ts
|
|
19
|
+
* // middleware.ts
|
|
20
|
+
* import { simplrNextMiddleware } from "@simplr-ai/express/next";
|
|
21
|
+
* export const middleware = simplrNextMiddleware({ apiKey: process.env.SIMPLR_API_KEY! });
|
|
22
|
+
* export const config = { matcher: ["/api/:path*"] };
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
declare function simplrNextMiddleware(options: GuardOptions<NextRequestLike>): (req: NextRequestLike) => Promise<NextResponseLike>;
|
|
26
|
+
/**
|
|
27
|
+
* Wrap an app-router route handler. The `CheckResult` is attached to the request
|
|
28
|
+
* as `req.simplr` for your handler; blocked requests return a 403 before it runs.
|
|
29
|
+
*
|
|
30
|
+
* ```ts
|
|
31
|
+
* // app/api/signup/route.ts
|
|
32
|
+
* import { withSimplr } from "@simplr-ai/express/next";
|
|
33
|
+
* export const POST = withSimplr(async (req) => {
|
|
34
|
+
* // req.simplr is the CheckResult
|
|
35
|
+
* return Response.json({ ok: true });
|
|
36
|
+
* }, { apiKey: process.env.SIMPLR_API_KEY! });
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
39
|
+
declare function withSimplr(handler: RouteHandler, options: GuardOptions<NextRequestLike>): RouteHandler;
|
|
40
|
+
|
|
41
|
+
export { simplrNextMiddleware, withSimplr };
|
package/dist/next.d.ts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { G as GuardOptions } from './types-CmzpR5S4.js';
|
|
2
|
+
export { C as CheckResult } from './types-CmzpR5S4.js';
|
|
3
|
+
|
|
4
|
+
type NextRequestLike = {
|
|
5
|
+
headers: {
|
|
6
|
+
get(name: string): string | null;
|
|
7
|
+
};
|
|
8
|
+
/** NextRequest exposes `.ip` on some runtimes. */
|
|
9
|
+
ip?: string;
|
|
10
|
+
json?: () => Promise<unknown>;
|
|
11
|
+
[key: string]: unknown;
|
|
12
|
+
};
|
|
13
|
+
type NextResponseLike = Response;
|
|
14
|
+
type RouteHandler = (req: NextRequestLike, ...rest: any[]) => Response | Promise<Response>;
|
|
15
|
+
/**
|
|
16
|
+
* Edge/Node middleware factory for `middleware.ts`.
|
|
17
|
+
*
|
|
18
|
+
* ```ts
|
|
19
|
+
* // middleware.ts
|
|
20
|
+
* import { simplrNextMiddleware } from "@simplr-ai/express/next";
|
|
21
|
+
* export const middleware = simplrNextMiddleware({ apiKey: process.env.SIMPLR_API_KEY! });
|
|
22
|
+
* export const config = { matcher: ["/api/:path*"] };
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
declare function simplrNextMiddleware(options: GuardOptions<NextRequestLike>): (req: NextRequestLike) => Promise<NextResponseLike>;
|
|
26
|
+
/**
|
|
27
|
+
* Wrap an app-router route handler. The `CheckResult` is attached to the request
|
|
28
|
+
* as `req.simplr` for your handler; blocked requests return a 403 before it runs.
|
|
29
|
+
*
|
|
30
|
+
* ```ts
|
|
31
|
+
* // app/api/signup/route.ts
|
|
32
|
+
* import { withSimplr } from "@simplr-ai/express/next";
|
|
33
|
+
* export const POST = withSimplr(async (req) => {
|
|
34
|
+
* // req.simplr is the CheckResult
|
|
35
|
+
* return Response.json({ ok: true });
|
|
36
|
+
* }, { apiKey: process.env.SIMPLR_API_KEY! });
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
39
|
+
declare function withSimplr(handler: RouteHandler, options: GuardOptions<NextRequestLike>): RouteHandler;
|
|
40
|
+
|
|
41
|
+
export { simplrNextMiddleware, withSimplr };
|
package/dist/next.js
ADDED
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
// src/client.ts
|
|
2
|
+
var DEFAULT_BASE_URL = "https://api.simplr.sh";
|
|
3
|
+
var SimplrError = class extends Error {
|
|
4
|
+
status;
|
|
5
|
+
body;
|
|
6
|
+
constructor(message, status, body) {
|
|
7
|
+
super(message);
|
|
8
|
+
this.name = "SimplrError";
|
|
9
|
+
this.status = status;
|
|
10
|
+
this.body = body;
|
|
11
|
+
}
|
|
12
|
+
};
|
|
13
|
+
function createClient(config) {
|
|
14
|
+
if (!config?.apiKey) throw new Error("Simplr: `apiKey` is required");
|
|
15
|
+
const baseUrl = (config.baseUrl || DEFAULT_BASE_URL).replace(/\/+$/, "");
|
|
16
|
+
const timeoutMs = config.timeoutMs ?? 15e3;
|
|
17
|
+
const fetchImpl = config.fetch ?? globalThis.fetch;
|
|
18
|
+
if (typeof fetchImpl !== "function") {
|
|
19
|
+
throw new Error(
|
|
20
|
+
"Simplr: no global fetch available \u2014 use Node 18+ or pass `fetch` in options"
|
|
21
|
+
);
|
|
22
|
+
}
|
|
23
|
+
async function apiRequest(method, path, body) {
|
|
24
|
+
const controller = new AbortController();
|
|
25
|
+
const timer = setTimeout(() => controller.abort(), timeoutMs);
|
|
26
|
+
try {
|
|
27
|
+
const res = await fetchImpl(`${baseUrl}${path}`, {
|
|
28
|
+
method,
|
|
29
|
+
headers: { "Content-Type": "application/json", "X-API-Key": config.apiKey },
|
|
30
|
+
body: body !== void 0 ? JSON.stringify(body) : void 0,
|
|
31
|
+
signal: controller.signal
|
|
32
|
+
});
|
|
33
|
+
const text = await res.text();
|
|
34
|
+
let parsed;
|
|
35
|
+
try {
|
|
36
|
+
parsed = text ? JSON.parse(text) : void 0;
|
|
37
|
+
} catch {
|
|
38
|
+
parsed = text;
|
|
39
|
+
}
|
|
40
|
+
if (!res.ok) {
|
|
41
|
+
const message = parsed && (parsed.message || parsed.error) || `Simplr API error ${res.status}`;
|
|
42
|
+
throw new SimplrError(message, res.status, parsed);
|
|
43
|
+
}
|
|
44
|
+
return parsed && typeof parsed === "object" && "content" in parsed ? parsed.content : parsed;
|
|
45
|
+
} catch (err) {
|
|
46
|
+
if (err instanceof SimplrError) throw err;
|
|
47
|
+
if (err instanceof Error && err.name === "AbortError") {
|
|
48
|
+
throw new SimplrError(`Request to ${path} timed out after ${timeoutMs}ms`, 0, null);
|
|
49
|
+
}
|
|
50
|
+
throw new SimplrError(err instanceof Error ? err.message : "Network error", 0, null);
|
|
51
|
+
} finally {
|
|
52
|
+
clearTimeout(timer);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
return {
|
|
56
|
+
check: (input) => apiRequest("POST", "/v1/check", input),
|
|
57
|
+
ingestLogs: (deviceId, logs) => apiRequest("POST", "/v1/edge/logs", { device_id: deviceId, logs })
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// src/core.ts
|
|
62
|
+
var RISK_ORDER = {
|
|
63
|
+
low: 0,
|
|
64
|
+
medium: 1,
|
|
65
|
+
high: 2,
|
|
66
|
+
critical: 3
|
|
67
|
+
};
|
|
68
|
+
var DEFAULT_DEVICE_ID = "simplr-edge-middleware";
|
|
69
|
+
var DEFAULT_THRESHOLD = "high";
|
|
70
|
+
function riskRank(level) {
|
|
71
|
+
return RISK_ORDER[level] ?? 0;
|
|
72
|
+
}
|
|
73
|
+
function riskAtLeast(level, min) {
|
|
74
|
+
return riskRank(level) >= riskRank(min);
|
|
75
|
+
}
|
|
76
|
+
function readHeader(headers, name) {
|
|
77
|
+
if (!headers) return void 0;
|
|
78
|
+
const h = headers;
|
|
79
|
+
if (typeof h.get === "function") {
|
|
80
|
+
const v = h.get(name);
|
|
81
|
+
return v == null ? void 0 : String(v);
|
|
82
|
+
}
|
|
83
|
+
const lower = name.toLowerCase();
|
|
84
|
+
if (lower in h && h[lower] != null) return String(h[lower]);
|
|
85
|
+
for (const key of Object.keys(h)) {
|
|
86
|
+
if (key.toLowerCase() === lower && h[key] != null) return String(h[key]);
|
|
87
|
+
}
|
|
88
|
+
return void 0;
|
|
89
|
+
}
|
|
90
|
+
function extractIp(req) {
|
|
91
|
+
const fwd = readHeader(req?.headers, "x-forwarded-for");
|
|
92
|
+
if (fwd) return fwd.split(",")[0].trim();
|
|
93
|
+
const real = readHeader(req?.headers, "x-real-ip");
|
|
94
|
+
if (real) return real;
|
|
95
|
+
return req?.ip || req?.socket?.remoteAddress || req?.connection?.remoteAddress || void 0;
|
|
96
|
+
}
|
|
97
|
+
function defaultExtract(req) {
|
|
98
|
+
const input = {};
|
|
99
|
+
const device = {};
|
|
100
|
+
const ip = extractIp(req);
|
|
101
|
+
if (ip) device.ip = ip;
|
|
102
|
+
const ua = readHeader(req?.headers, "user-agent");
|
|
103
|
+
if (ua) device.user_agent = ua;
|
|
104
|
+
if (Object.keys(device).length > 0) input.device = device;
|
|
105
|
+
const body = req?.body;
|
|
106
|
+
if (body && typeof body === "object") {
|
|
107
|
+
const b = body;
|
|
108
|
+
if (typeof b.email === "string") input.email = b.email;
|
|
109
|
+
if (typeof b.phone === "string") input.phone = b.phone;
|
|
110
|
+
}
|
|
111
|
+
return input;
|
|
112
|
+
}
|
|
113
|
+
function normalizeBlock(block) {
|
|
114
|
+
if (typeof block === "function") return block;
|
|
115
|
+
const threshold = block?.whenRiskAtLeast ?? DEFAULT_THRESHOLD;
|
|
116
|
+
return (result) => riskAtLeast(result.risk_level, threshold);
|
|
117
|
+
}
|
|
118
|
+
function createGuard(options) {
|
|
119
|
+
if (!options?.apiKey) throw new Error("createGuard: `apiKey` is required");
|
|
120
|
+
const client = createClient({
|
|
121
|
+
apiKey: options.apiKey,
|
|
122
|
+
baseUrl: options.baseUrl,
|
|
123
|
+
timeoutMs: options.timeoutMs,
|
|
124
|
+
fetch: options.fetch
|
|
125
|
+
});
|
|
126
|
+
const extract = options.extract ?? defaultExtract;
|
|
127
|
+
const shouldBlock = normalizeBlock(options.block);
|
|
128
|
+
const failOpen = options.failOpen ?? true;
|
|
129
|
+
const ingestLogs = options.ingestLogs ?? false;
|
|
130
|
+
const deviceId = options.deviceId ?? DEFAULT_DEVICE_ID;
|
|
131
|
+
async function run(req) {
|
|
132
|
+
const input = await extract(req);
|
|
133
|
+
let result;
|
|
134
|
+
try {
|
|
135
|
+
result = await client.check(input);
|
|
136
|
+
} catch (error) {
|
|
137
|
+
if (failOpen) {
|
|
138
|
+
return { result: null, blocked: false, input, error };
|
|
139
|
+
}
|
|
140
|
+
throw error;
|
|
141
|
+
}
|
|
142
|
+
const blocked = shouldBlock(result);
|
|
143
|
+
const ctx = { req, input, blocked };
|
|
144
|
+
if (options.onResult) {
|
|
145
|
+
try {
|
|
146
|
+
await options.onResult(result, ctx);
|
|
147
|
+
} catch {
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
if (ingestLogs) {
|
|
151
|
+
try {
|
|
152
|
+
await client.ingestLogs(deviceId, [
|
|
153
|
+
{
|
|
154
|
+
category: "security",
|
|
155
|
+
level: blocked ? "warn" : "info",
|
|
156
|
+
message: blocked ? "simplr guard blocked request" : "simplr guard checked request",
|
|
157
|
+
risk_level: result.risk_level,
|
|
158
|
+
risk_score: result.risk_score
|
|
159
|
+
}
|
|
160
|
+
]);
|
|
161
|
+
} catch {
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
return { result, blocked, input };
|
|
165
|
+
}
|
|
166
|
+
return { client, run };
|
|
167
|
+
}
|
|
168
|
+
function blockEnvelope(result) {
|
|
169
|
+
return {
|
|
170
|
+
error: "request_blocked",
|
|
171
|
+
message: "This request was blocked by Simplr fraud protection.",
|
|
172
|
+
risk_level: result.risk_level,
|
|
173
|
+
risk_score: result.risk_score
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
// src/next.ts
|
|
178
|
+
async function loadNextResponse() {
|
|
179
|
+
try {
|
|
180
|
+
const spec = "next/server";
|
|
181
|
+
const mod = await import(
|
|
182
|
+
/* @vite-ignore */
|
|
183
|
+
spec
|
|
184
|
+
);
|
|
185
|
+
const NextResponse = mod.NextResponse;
|
|
186
|
+
if (!NextResponse) return null;
|
|
187
|
+
return {
|
|
188
|
+
next: () => NextResponse.next(),
|
|
189
|
+
json: (body, init) => NextResponse.json(body, init)
|
|
190
|
+
};
|
|
191
|
+
} catch {
|
|
192
|
+
return null;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
function simplrNextMiddleware(options) {
|
|
196
|
+
const guard = createGuard(options);
|
|
197
|
+
const failOpen = options.failOpen ?? true;
|
|
198
|
+
return async function middleware(req) {
|
|
199
|
+
const next = await loadNextResponse();
|
|
200
|
+
const pass = () => next ? next.next() : new Response(null, { status: 200 });
|
|
201
|
+
const deny = (body) => next ? next.json(body, { status: 403 }) : jsonResponse(body, 403);
|
|
202
|
+
try {
|
|
203
|
+
const outcome = await guard.run(req);
|
|
204
|
+
if (outcome.blocked && outcome.result) {
|
|
205
|
+
return deny(blockEnvelope(outcome.result));
|
|
206
|
+
}
|
|
207
|
+
return pass();
|
|
208
|
+
} catch (err) {
|
|
209
|
+
if (failOpen) return pass();
|
|
210
|
+
throw err;
|
|
211
|
+
}
|
|
212
|
+
};
|
|
213
|
+
}
|
|
214
|
+
function withSimplr(handler, options) {
|
|
215
|
+
const guard = createGuard(options);
|
|
216
|
+
const failOpen = options.failOpen ?? true;
|
|
217
|
+
return async function wrapped(req, ...rest) {
|
|
218
|
+
try {
|
|
219
|
+
const outcome = await guard.run(req);
|
|
220
|
+
req.simplr = outcome.result;
|
|
221
|
+
if (outcome.blocked && outcome.result) {
|
|
222
|
+
return jsonResponse(blockEnvelope(outcome.result), 403);
|
|
223
|
+
}
|
|
224
|
+
} catch (err) {
|
|
225
|
+
if (!failOpen) throw err;
|
|
226
|
+
req.simplr = null;
|
|
227
|
+
}
|
|
228
|
+
return handler(req, ...rest);
|
|
229
|
+
};
|
|
230
|
+
}
|
|
231
|
+
function jsonResponse(body, status) {
|
|
232
|
+
return new Response(JSON.stringify(body), {
|
|
233
|
+
status,
|
|
234
|
+
headers: { "content-type": "application/json" }
|
|
235
|
+
});
|
|
236
|
+
}
|
|
237
|
+
export {
|
|
238
|
+
simplrNextMiddleware,
|
|
239
|
+
withSimplr
|
|
240
|
+
};
|
|
241
|
+
//# sourceMappingURL=next.js.map
|