@arcjet/astro 1.6.1 → 1.7.0-rc.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.
@@ -0,0 +1,118 @@
1
+ import { findIp, parseProxies } from "@arcjet/ip";
2
+ import { readBodyWeb } from "@arcjet/body";
3
+ import { baseUrl, isDevelopment, logLevel, platform } from "@arcjet/env";
4
+ import { ArcjetHeaders } from "@arcjet/headers";
5
+ import { Logger } from "@arcjet/logger";
6
+ import { createClient } from "@arcjet/protocol/client.js";
7
+ import { createTransport } from "@arcjet/transport";
8
+ import core from "arcjet";
9
+ import { ARCJET_BASE_URL, ARCJET_ENV, ARCJET_KEY, ARCJET_LOG_LEVEL, FIREBASE_CONFIG, FLY_APP_NAME, RENDER, VERCEL } from "astro:env/server";
10
+ export * from "arcjet";
11
+ //#region src/internal.ts
12
+ let warnedForAutomaticBodyRead = false;
13
+ const ipSymbol = Symbol.for("arcjet.ip");
14
+ const env = {
15
+ ARCJET_BASE_URL,
16
+ ARCJET_ENV,
17
+ ARCJET_KEY,
18
+ ARCJET_LOG_LEVEL,
19
+ FIREBASE_CONFIG,
20
+ FLY_APP_NAME,
21
+ MODE: import.meta.env.MODE,
22
+ RENDER,
23
+ VERCEL
24
+ };
25
+ /**
26
+ * Create a remote client.
27
+ *
28
+ * @param options
29
+ * Configuration (optional).
30
+ * @returns
31
+ * Client.
32
+ */
33
+ function createRemoteClient(options) {
34
+ const url = options?.baseUrl ?? baseUrl(env);
35
+ const timeout = options?.timeout ?? (isDevelopment(env) ? 1e3 : 500);
36
+ return createClient({
37
+ transport: createTransport(url),
38
+ baseUrl: url,
39
+ timeout,
40
+ sdkStack: "ASTRO",
41
+ sdkVersion: "__ARCJET_SDK_VERSION__"
42
+ });
43
+ }
44
+ /**
45
+ * Create a new Astro integration of Arcjet.
46
+ *
47
+ * @template Rules
48
+ * List of rules.
49
+ * @template Characteristics
50
+ * Characteristics to track a user by.
51
+ * @param options
52
+ * Configuration.
53
+ * @returns
54
+ * Astro integration of Arcjet.
55
+ */
56
+ function createArcjetClient(options) {
57
+ const client = options.client ?? createRemoteClient();
58
+ const log = options.log ? options.log : new Logger({ level: logLevel(env) });
59
+ const proxies = Array.isArray(options.proxies) ? parseProxies(options.proxies) : void 0;
60
+ if (isDevelopment(process.env)) log.warn("Arcjet will use 127.0.0.1 when missing public IP address in development mode");
61
+ function toArcjetRequest(request, props) {
62
+ const clientAddress = Reflect.get(request, ipSymbol);
63
+ if (!clientAddress) throw new Error("`protect()` cannot be used in prerendered pages");
64
+ const cookies = request.headers.get("cookie") ?? "";
65
+ const headers = new ArcjetHeaders(request.headers);
66
+ const url = new URL(request.url);
67
+ let ip = (isDevelopment(env) ? headers.get("x-arcjet-ip") : void 0) || findIp({
68
+ ip: clientAddress,
69
+ headers
70
+ }, {
71
+ platform: platform(env),
72
+ proxies
73
+ });
74
+ if (ip === "") if (isDevelopment(env)) ip = "127.0.0.1";
75
+ else log.warn(`Client IP address is missing. If this is a dev environment set the ARCJET_ENV env var to "development"`);
76
+ return {
77
+ ...props,
78
+ ip,
79
+ method: request.method,
80
+ protocol: url.protocol,
81
+ host: url.host,
82
+ path: url.pathname,
83
+ headers,
84
+ cookies,
85
+ query: url.search
86
+ };
87
+ }
88
+ function withClient(aj) {
89
+ return Object.freeze({
90
+ withRule(rule) {
91
+ return withClient(aj.withRule(rule));
92
+ },
93
+ async protect(request, props) {
94
+ const req = toArcjetRequest(request, props || {});
95
+ const getBody = async () => {
96
+ const clonedRequest = request.clone();
97
+ let expectedLength;
98
+ const expectedLengthString = request.headers.get("content-length");
99
+ if (typeof expectedLengthString === "string") expectedLength = parseInt(expectedLengthString, 10);
100
+ if (!clonedRequest.body) throw new Error("Cannot read body: body is missing");
101
+ if (!warnedForAutomaticBodyRead) {
102
+ warnedForAutomaticBodyRead = true;
103
+ log.warn("Automatically reading the request body is deprecated; please pass an explicit `sensitiveInfoValue` field. See <https://docs.arcjet.com/upgrading/sdk-migration>.");
104
+ }
105
+ return readBodyWeb(clonedRequest.body, { expectedLength });
106
+ };
107
+ return aj.protect({ getBody }, req);
108
+ }
109
+ });
110
+ }
111
+ return withClient(core({
112
+ ...options,
113
+ client,
114
+ log
115
+ }));
116
+ }
117
+ //#endregion
118
+ export { createArcjetClient, createRemoteClient };
@@ -0,0 +1,4 @@
1
+ //#region src/middleware.d.ts
2
+ declare const onRequest: import("astro").MiddlewareHandler;
3
+ //#endregion
4
+ export { onRequest };
@@ -0,0 +1,9 @@
1
+ import { defineMiddleware } from "astro/middleware";
2
+ //#region src/middleware.ts
3
+ const ipSymbol = Symbol.for("arcjet.ip");
4
+ const onRequest = defineMiddleware((ctx, next) => {
5
+ if (!ctx.isPrerendered) Reflect.set(ctx.request, ipSymbol, ctx.clientAddress);
6
+ return next();
7
+ });
8
+ //#endregion
9
+ export { onRequest };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@arcjet/astro",
3
- "version": "1.6.1",
4
- "description": "Arcjet runtime security SDK for Astro bot protection, rate limiting, prompt injection detection, PII blocking, and WAF",
3
+ "version": "1.7.0-rc.0",
4
+ "description": "Arcjet runtime security SDK for Astro \u2014 bot protection, rate limiting, prompt injection detection, PII blocking, and WAF",
5
5
  "keywords": [
6
6
  "ai",
7
7
  "analyze",
@@ -22,64 +22,63 @@
22
22
  "waf",
23
23
  "withastro"
24
24
  ],
25
- "license": "Apache-2.0",
26
25
  "homepage": "https://arcjet.com",
27
- "repository": {
28
- "type": "git",
29
- "url": "git+https://github.com/arcjet/arcjet-js.git",
30
- "directory": "arcjet-astro"
31
- },
32
26
  "bugs": {
33
27
  "url": "https://github.com/arcjet/arcjet-js/issues",
34
28
  "email": "support@arcjet.com"
35
29
  },
30
+ "license": "Apache-2.0",
36
31
  "author": {
37
32
  "name": "Arcjet",
38
33
  "email": "support@arcjet.com",
39
34
  "url": "https://arcjet.com"
40
35
  },
41
- "engines": {},
42
- "type": "module",
43
- "main": "./index.js",
44
- "types": "./index.d.ts",
36
+ "repository": {
37
+ "type": "git",
38
+ "url": "git+https://github.com/arcjet/arcjet-js.git",
39
+ "directory": "arcjet-astro"
40
+ },
45
41
  "files": [
46
- "index.d.ts",
47
- "index.js",
48
- "internal.d.ts",
49
- "internal.js",
50
- "middleware.d.ts",
51
- "middleware.js"
42
+ "dist"
52
43
  ],
44
+ "type": "module",
45
+ "main": "./dist/index.js",
46
+ "types": "./dist/index.d.ts",
47
+ "exports": {
48
+ ".": {
49
+ "types": "./dist/index.d.ts",
50
+ "default": "./dist/index.js"
51
+ },
52
+ "./package.json": "./package.json"
53
+ },
54
+ "publishConfig": {
55
+ "access": "public",
56
+ "tag": "latest"
57
+ },
53
58
  "scripts": {
54
- "build": "rollup --config rollup.config.js",
55
- "lint": "eslint .",
56
- "test-api": "node --test -- test/*.test.js",
57
- "test-coverage": "node --experimental-test-coverage --test -- test/*.test.js",
58
- "test": "npm run build && npm run lint && npm run test-coverage"
59
+ "build": "tsdown",
60
+ "typecheck": "tsgo --noEmit",
61
+ "test-api": "node --test -- test/*.test.ts",
62
+ "test-coverage": "node --experimental-test-coverage --test -- test/*.test.ts",
63
+ "test": "npm run build && npm run test-coverage"
59
64
  },
60
65
  "dependencies": {
61
- "@arcjet/body": "1.6.1",
62
- "@arcjet/env": "1.6.1",
63
- "@arcjet/headers": "1.6.1",
64
- "@arcjet/ip": "1.6.1",
65
- "@arcjet/logger": "1.6.1",
66
- "@arcjet/protocol": "1.6.1",
67
- "@arcjet/transport": "1.6.1",
68
- "arcjet": "1.6.1"
69
- },
70
- "peerDependencies": {
71
- "astro": "^5.9.3 || ^6.0.0 || ^7.0.0"
66
+ "@arcjet/body": "1.7.0-rc.0",
67
+ "@arcjet/env": "1.7.0-rc.0",
68
+ "@arcjet/headers": "1.7.0-rc.0",
69
+ "@arcjet/ip": "1.7.0-rc.0",
70
+ "@arcjet/logger": "1.7.0-rc.0",
71
+ "@arcjet/protocol": "1.7.0-rc.0",
72
+ "@arcjet/transport": "1.7.0-rc.0",
73
+ "arcjet": "1.7.0-rc.0"
72
74
  },
73
75
  "devDependencies": {
74
- "@arcjet/eslint-config": "1.6.1",
75
- "@arcjet/rollup-config": "1.6.1",
76
- "@rollup/wasm-node": "4.62.2",
77
76
  "astro": "7.0.3",
78
- "eslint": "9.39.4",
79
- "typescript": "5.9.3"
77
+ "tsdown": "0.22.3",
78
+ "typescript": "6.0.3"
80
79
  },
81
- "publishConfig": {
82
- "access": "public",
83
- "tag": "latest"
84
- }
80
+ "peerDependencies": {
81
+ "astro": "^5.9.3 || ^6.0.0 || ^7.0.0"
82
+ },
83
+ "engines": {}
85
84
  }