@arcjet/node 1.0.0-beta.10 → 1.0.0-beta.11
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 +122 -20
- package/index.js +27 -13
- package/package.json +14 -15
package/index.d.ts
CHANGED
|
@@ -10,66 +10,168 @@ type WithoutCustomProps = {
|
|
|
10
10
|
type PlainObject = {
|
|
11
11
|
[key: string]: unknown;
|
|
12
12
|
};
|
|
13
|
+
/**
|
|
14
|
+
* Configuration for {@linkcode createRemoteClient}.
|
|
15
|
+
*/
|
|
13
16
|
export type RemoteClientOptions = {
|
|
17
|
+
/**
|
|
18
|
+
* Base URI for HTTP requests to Decide API (optional).
|
|
19
|
+
*
|
|
20
|
+
* Defaults to the environment variable `ARCJET_BASE_URL` (if that value
|
|
21
|
+
* is known and allowed) and the standard production API otherwise.
|
|
22
|
+
*/
|
|
14
23
|
baseUrl?: string;
|
|
24
|
+
/**
|
|
25
|
+
* Timeout in milliseconds for the Decide API (optional).
|
|
26
|
+
*
|
|
27
|
+
* Defaults to `500` in production and `1000` in development.
|
|
28
|
+
*/
|
|
15
29
|
timeout?: number;
|
|
16
30
|
};
|
|
31
|
+
/**
|
|
32
|
+
* Create a remote client.
|
|
33
|
+
*
|
|
34
|
+
* @param options
|
|
35
|
+
* Configuration (optional).
|
|
36
|
+
* @returns
|
|
37
|
+
* Client.
|
|
38
|
+
*/
|
|
17
39
|
export declare function createRemoteClient(options?: RemoteClientOptions): import("@arcjet/protocol/client.js").Client;
|
|
18
40
|
type EventHandlerLike = (event: string, listener: (...args: any[]) => void) => void;
|
|
41
|
+
/**
|
|
42
|
+
* Request for the Node.js integration of Arcjet.
|
|
43
|
+
*
|
|
44
|
+
* This is the minimum interface similar to `http.IncomingMessage`.
|
|
45
|
+
*/
|
|
19
46
|
export interface ArcjetNodeRequest {
|
|
47
|
+
/**
|
|
48
|
+
* Headers of the request.
|
|
49
|
+
*/
|
|
20
50
|
headers?: Record<string, string | string[] | undefined>;
|
|
51
|
+
/**
|
|
52
|
+
* `net.Socket` object associated with the connection.
|
|
53
|
+
*
|
|
54
|
+
* See <https://nodejs.org/api/http.html#messagesocket>.
|
|
55
|
+
*/
|
|
21
56
|
socket?: Partial<{
|
|
22
57
|
remoteAddress: string;
|
|
23
58
|
encrypted: boolean;
|
|
24
59
|
}>;
|
|
60
|
+
/**
|
|
61
|
+
* HTTP method of the request.
|
|
62
|
+
*/
|
|
25
63
|
method?: string;
|
|
64
|
+
/**
|
|
65
|
+
* HTTP version sent by the client.
|
|
66
|
+
*
|
|
67
|
+
* See <https://nodejs.org/api/http.html#messagehttpversion>.
|
|
68
|
+
*/
|
|
26
69
|
httpVersion?: string;
|
|
70
|
+
/**
|
|
71
|
+
* URL.
|
|
72
|
+
*/
|
|
27
73
|
url?: string;
|
|
74
|
+
/**
|
|
75
|
+
* Request body.
|
|
76
|
+
*/
|
|
28
77
|
body?: unknown;
|
|
78
|
+
/**
|
|
79
|
+
* Add event handlers.
|
|
80
|
+
*
|
|
81
|
+
* This field is available through `stream.Readable` from `EventEmitter`.
|
|
82
|
+
*
|
|
83
|
+
* See <https://nodejs.org/api/events.html#emitteroneventname-listener>.
|
|
84
|
+
*/
|
|
29
85
|
on?: EventHandlerLike;
|
|
86
|
+
/**
|
|
87
|
+
* Remove event handlers.
|
|
88
|
+
*
|
|
89
|
+
* This field is available through `stream.Readable` from `EventEmitter`.
|
|
90
|
+
*
|
|
91
|
+
* See <https://nodejs.org/api/events.html#emitterremovelistenereventname-listener>.
|
|
92
|
+
*/
|
|
30
93
|
removeListener?: EventHandlerLike;
|
|
94
|
+
/**
|
|
95
|
+
* Whether the readable stream is readable.
|
|
96
|
+
*
|
|
97
|
+
* This field is available from `stream.Readable`.
|
|
98
|
+
*
|
|
99
|
+
* See <https://nodejs.org/api/stream.html#readablereadable>.
|
|
100
|
+
*/
|
|
31
101
|
readable?: boolean;
|
|
32
102
|
}
|
|
33
103
|
/**
|
|
34
|
-
*
|
|
104
|
+
* Configuration for the Node.js integration of Arcjet.
|
|
105
|
+
*
|
|
106
|
+
* @template Rules
|
|
107
|
+
* List of rules.
|
|
108
|
+
* @template Characteristics
|
|
109
|
+
* Characteristics to track a user by.
|
|
35
110
|
*/
|
|
36
111
|
export type ArcjetOptions<Rules extends [...Array<Primitive | Product>], Characteristics extends readonly string[]> = Simplify<CoreOptions<Rules, Characteristics> & {
|
|
37
112
|
/**
|
|
38
|
-
*
|
|
39
|
-
*
|
|
113
|
+
* IP addresses and CIDR ranges of trusted load balancers and proxies
|
|
114
|
+
* (optional, example: `["100.100.100.100", "100.100.100.0/24"]`).
|
|
40
115
|
*/
|
|
41
116
|
proxies?: Array<string>;
|
|
42
117
|
}>;
|
|
43
118
|
/**
|
|
44
|
-
*
|
|
45
|
-
*
|
|
119
|
+
* Instance of the Node.js integration of Arcjet.
|
|
120
|
+
*
|
|
121
|
+
* Primarily has a `protect()` method to make a decision about how a Node request
|
|
122
|
+
* should be handled.
|
|
123
|
+
*
|
|
124
|
+
* @template Props
|
|
125
|
+
* Configuration.
|
|
46
126
|
*/
|
|
47
127
|
export interface ArcjetNode<Props extends PlainObject> {
|
|
48
128
|
/**
|
|
49
|
-
*
|
|
50
|
-
*
|
|
51
|
-
* the request
|
|
129
|
+
* Make a decision about how to handle a request.
|
|
130
|
+
*
|
|
131
|
+
* This will analyze the request locally where possible and otherwise call
|
|
132
|
+
* the Arcjet decision API.
|
|
52
133
|
*
|
|
53
|
-
* @param
|
|
54
|
-
*
|
|
55
|
-
*
|
|
134
|
+
* @param request
|
|
135
|
+
* Details about the {@linkcode ArcjetNodeRequest} that Arcjet needs to make a
|
|
136
|
+
* decision.
|
|
137
|
+
* @param props
|
|
138
|
+
* Additional properties required for running rules against a request.
|
|
139
|
+
* @returns
|
|
140
|
+
* Promise that resolves to an {@linkcode ArcjetDecision} indicating
|
|
141
|
+
* Arcjet’s decision about the request.
|
|
56
142
|
*/
|
|
57
143
|
protect(request: ArcjetNodeRequest, ...props: Props extends WithoutCustomProps ? [] : [Props]): Promise<ArcjetDecision>;
|
|
58
144
|
/**
|
|
59
|
-
*
|
|
60
|
-
* criteria in your handler—e.g. different rate limit for logged in users.
|
|
145
|
+
* Augment the client with another rule.
|
|
61
146
|
*
|
|
62
|
-
*
|
|
63
|
-
*
|
|
147
|
+
* Useful for varying rules based on criteria in your handler such as
|
|
148
|
+
* different rate limit for logged in users.
|
|
149
|
+
*
|
|
150
|
+
* @template Rule
|
|
151
|
+
* Type of rule.
|
|
152
|
+
* @param rule
|
|
153
|
+
* Rule to add to Arcjet.
|
|
154
|
+
* @returns
|
|
155
|
+
* Arcjet instance augmented with the given rule.
|
|
64
156
|
*/
|
|
65
157
|
withRule<Rule extends Primitive | Product>(rule: Rule): ArcjetNode<Simplify<Props & ExtraProps<Rule>>>;
|
|
66
158
|
}
|
|
67
159
|
/**
|
|
68
|
-
* Create a new
|
|
69
|
-
*
|
|
70
|
-
*
|
|
71
|
-
* client
|
|
160
|
+
* Create a new Node.js integration of Arcjet.
|
|
161
|
+
*
|
|
162
|
+
* > 👉 **Tip**:
|
|
163
|
+
* > build your initial base client with as many rules as possible outside of a
|
|
164
|
+
* > request handler;
|
|
165
|
+
* > if you need more rules inside handlers later then you can call `withRule()`
|
|
166
|
+
* > on that base client.
|
|
72
167
|
*
|
|
73
|
-
* @
|
|
168
|
+
* @template Rules
|
|
169
|
+
* List of rules.
|
|
170
|
+
* @template Characteristics
|
|
171
|
+
* Characteristics to track a user by.
|
|
172
|
+
* @param options
|
|
173
|
+
* Configuration.
|
|
174
|
+
* @returns
|
|
175
|
+
* Node.js integration of Arcjet.
|
|
74
176
|
*/
|
|
75
177
|
export default function arcjet<const Rules extends (Primitive | Product)[], const Characteristics extends readonly string[]>(options: ArcjetOptions<Rules, Characteristics>): ArcjetNode<Simplify<ExtraProps<Rules> & CharacteristicProps<Characteristics>>>;
|
package/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import core__default from 'arcjet';
|
|
2
2
|
export * from 'arcjet';
|
|
3
|
-
import
|
|
4
|
-
import ArcjetHeaders from '@arcjet/headers';
|
|
3
|
+
import findIp, { parseProxy } from '@arcjet/ip';
|
|
4
|
+
import { ArcjetHeaders } from '@arcjet/headers';
|
|
5
5
|
import { logLevel, isDevelopment, baseUrl, platform } from '@arcjet/env';
|
|
6
6
|
import { Logger } from '@arcjet/logger';
|
|
7
7
|
import { createClient } from '@arcjet/protocol/client.js';
|
|
@@ -54,17 +54,21 @@ function errorMessage(err) {
|
|
|
54
54
|
}
|
|
55
55
|
return "Unknown problem";
|
|
56
56
|
}
|
|
57
|
+
/**
|
|
58
|
+
* Create a remote client.
|
|
59
|
+
*
|
|
60
|
+
* @param options
|
|
61
|
+
* Configuration (optional).
|
|
62
|
+
* @returns
|
|
63
|
+
* Client.
|
|
64
|
+
*/
|
|
57
65
|
function createRemoteClient(options) {
|
|
58
|
-
// The base URL for the Arcjet API. Will default to the standard production
|
|
59
|
-
// API unless environment variable `ARCJET_BASE_URL` is set.
|
|
60
66
|
const url = options?.baseUrl ?? baseUrl(env);
|
|
61
|
-
// The timeout for the Arcjet API in milliseconds. This is set to a low value
|
|
62
|
-
// in production so calls fail open.
|
|
63
67
|
const timeout = options?.timeout ?? (isDevelopment(env) ? 1000 : 500);
|
|
64
68
|
// Transport is the HTTP client that the client uses to make requests.
|
|
65
69
|
const transport = createTransport(url);
|
|
66
70
|
const sdkStack = "NODEJS";
|
|
67
|
-
const sdkVersion = "1.0.0-beta.
|
|
71
|
+
const sdkVersion = "1.0.0-beta.11";
|
|
68
72
|
return createClient({
|
|
69
73
|
transport,
|
|
70
74
|
baseUrl: url,
|
|
@@ -84,12 +88,22 @@ function cookiesToString(cookies) {
|
|
|
84
88
|
return cookies;
|
|
85
89
|
}
|
|
86
90
|
/**
|
|
87
|
-
* Create a new
|
|
88
|
-
*
|
|
89
|
-
*
|
|
90
|
-
* client
|
|
91
|
+
* Create a new Node.js integration of Arcjet.
|
|
92
|
+
*
|
|
93
|
+
* > 👉 **Tip**:
|
|
94
|
+
* > build your initial base client with as many rules as possible outside of a
|
|
95
|
+
* > request handler;
|
|
96
|
+
* > if you need more rules inside handlers later then you can call `withRule()`
|
|
97
|
+
* > on that base client.
|
|
91
98
|
*
|
|
92
|
-
* @
|
|
99
|
+
* @template Rules
|
|
100
|
+
* List of rules.
|
|
101
|
+
* @template Characteristics
|
|
102
|
+
* Characteristics to track a user by.
|
|
103
|
+
* @param options
|
|
104
|
+
* Configuration.
|
|
105
|
+
* @returns
|
|
106
|
+
* Node.js integration of Arcjet.
|
|
93
107
|
*/
|
|
94
108
|
function arcjet(options) {
|
|
95
109
|
const client = options.client ?? createRemoteClient();
|
|
@@ -109,7 +123,7 @@ function arcjet(options) {
|
|
|
109
123
|
const cookies = cookiesToString(request.headers?.cookie);
|
|
110
124
|
// We construct an ArcjetHeaders to normalize over Headers
|
|
111
125
|
const headers = new ArcjetHeaders(request.headers);
|
|
112
|
-
let ip =
|
|
126
|
+
let ip = findIp({
|
|
113
127
|
socket: request.socket,
|
|
114
128
|
headers,
|
|
115
129
|
}, { platform: platform(env), proxies });
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@arcjet/node",
|
|
3
|
-
"version": "1.0.0-beta.
|
|
3
|
+
"version": "1.0.0-beta.11",
|
|
4
4
|
"description": "Arcjet SDK for Node.js",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"analyze",
|
|
@@ -47,22 +47,21 @@
|
|
|
47
47
|
"test": "npm run build && npm run lint"
|
|
48
48
|
},
|
|
49
49
|
"dependencies": {
|
|
50
|
-
"@arcjet/env": "1.0.0-beta.
|
|
51
|
-
"@arcjet/headers": "1.0.0-beta.
|
|
52
|
-
"@arcjet/ip": "1.0.0-beta.
|
|
53
|
-
"@arcjet/logger": "1.0.0-beta.
|
|
54
|
-
"@arcjet/protocol": "1.0.0-beta.
|
|
55
|
-
"@arcjet/transport": "1.0.0-beta.
|
|
56
|
-
"@arcjet/body": "1.0.0-beta.
|
|
57
|
-
"arcjet": "1.0.0-beta.
|
|
50
|
+
"@arcjet/env": "1.0.0-beta.11",
|
|
51
|
+
"@arcjet/headers": "1.0.0-beta.11",
|
|
52
|
+
"@arcjet/ip": "1.0.0-beta.11",
|
|
53
|
+
"@arcjet/logger": "1.0.0-beta.11",
|
|
54
|
+
"@arcjet/protocol": "1.0.0-beta.11",
|
|
55
|
+
"@arcjet/transport": "1.0.0-beta.11",
|
|
56
|
+
"@arcjet/body": "1.0.0-beta.11",
|
|
57
|
+
"arcjet": "1.0.0-beta.11"
|
|
58
58
|
},
|
|
59
59
|
"devDependencies": {
|
|
60
|
-
"@arcjet/eslint-config": "1.0.0-beta.
|
|
61
|
-
"@arcjet/rollup-config": "1.0.0-beta.
|
|
62
|
-
"@
|
|
63
|
-
"@
|
|
64
|
-
"
|
|
65
|
-
"eslint": "9.32.0",
|
|
60
|
+
"@arcjet/eslint-config": "1.0.0-beta.11",
|
|
61
|
+
"@arcjet/rollup-config": "1.0.0-beta.11",
|
|
62
|
+
"@types/node": "24.3.0",
|
|
63
|
+
"@rollup/wasm-node": "4.50.0",
|
|
64
|
+
"eslint": "9.34.0",
|
|
66
65
|
"typescript": "5.9.2"
|
|
67
66
|
},
|
|
68
67
|
"publishConfig": {
|