@arcjet/astro 1.7.0-rc.1 → 1.7.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/internal.js DELETED
@@ -1,120 +0,0 @@
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.7.0-rc.1";
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 };
@@ -1,4 +0,0 @@
1
- //#region src/middleware.d.ts
2
- declare const onRequest: import("astro").MiddlewareHandler;
3
- //#endregion
4
- export { onRequest };
@@ -1,9 +0,0 @@
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 };