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