@arcjet/node 1.0.0-alpha.12 → 1.0.0-alpha.14
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/README.md +26 -12
- package/index.d.ts +2 -3
- package/index.js +37 -13
- package/index.ts +45 -17
- package/package.json +10 -7
package/README.md
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
<a href="https://arcjet.com" target="_arcjet-home">
|
|
2
2
|
<picture>
|
|
3
|
-
<source media="(prefers-color-scheme: dark)" srcset="https://arcjet.com/arcjet-
|
|
4
|
-
<img src="https://arcjet.com/arcjet-
|
|
3
|
+
<source media="(prefers-color-scheme: dark)" srcset="https://arcjet.com/logo/arcjet-dark-lockup-voyage-horizontal.svg">
|
|
4
|
+
<img src="https://arcjet.com/logo/arcjet-light-lockup-voyage-horizontal.svg" alt="Arcjet Logo" height="128" width="auto">
|
|
5
5
|
</picture>
|
|
6
6
|
</a>
|
|
7
7
|
|
|
@@ -17,13 +17,22 @@
|
|
|
17
17
|
</p>
|
|
18
18
|
|
|
19
19
|
[Arcjet][arcjet] helps developers protect their apps in just a few lines of
|
|
20
|
-
code. Implement rate limiting, bot protection, email verification
|
|
20
|
+
code. Implement rate limiting, bot protection, email verification, and defense
|
|
21
21
|
against common attacks.
|
|
22
22
|
|
|
23
23
|
This is the [Arcjet][arcjet] SDK for [Node.js][node-js].
|
|
24
24
|
|
|
25
25
|
**Looking for our Next.js framework SDK?** Check out the
|
|
26
|
-
[`@arcjet/next`]
|
|
26
|
+
[`@arcjet/next`][alt-sdk] package.
|
|
27
|
+
|
|
28
|
+
## Getting started
|
|
29
|
+
|
|
30
|
+
Visit the [quick start guide][quick-start] to get started.
|
|
31
|
+
|
|
32
|
+
## Example app
|
|
33
|
+
|
|
34
|
+
Try an Arcjet protected app live at [https://example.arcjet.com][example-url]
|
|
35
|
+
([source code][example-source]).
|
|
27
36
|
|
|
28
37
|
## Installation
|
|
29
38
|
|
|
@@ -82,19 +91,20 @@ server.listen(8000);
|
|
|
82
91
|
## Shield example
|
|
83
92
|
|
|
84
93
|
[Arcjet Shield][shield-concepts-docs] protects your application against common
|
|
85
|
-
attacks, including the OWASP Top 10.
|
|
86
|
-
|
|
94
|
+
attacks, including the OWASP Top 10. You can run Shield on every request with
|
|
95
|
+
negligible performance impact.
|
|
87
96
|
|
|
88
97
|
```ts
|
|
89
|
-
import arcjet from "@arcjet/node";
|
|
98
|
+
import arcjet, { shield } from "@arcjet/node";
|
|
90
99
|
import http from "node:http";
|
|
91
100
|
|
|
92
101
|
const aj = arcjet({
|
|
93
|
-
// Get your site key from https://app.arcjet.com
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
102
|
+
key: process.env.ARCJET_KEY!, // Get your site key from https://app.arcjet.com
|
|
103
|
+
rules: [
|
|
104
|
+
shield({
|
|
105
|
+
mode: "LIVE", // will block requests. Use "DRY_RUN" to log only
|
|
106
|
+
}),
|
|
107
|
+
],
|
|
98
108
|
});
|
|
99
109
|
|
|
100
110
|
const server = http.createServer(async function (
|
|
@@ -121,6 +131,10 @@ Licensed under the [Apache License, Version 2.0][apache-license].
|
|
|
121
131
|
|
|
122
132
|
[arcjet]: https://arcjet.com
|
|
123
133
|
[node-js]: https://nodejs.org/
|
|
134
|
+
[alt-sdk]: https://www.npmjs.com/package/@arcjet/next
|
|
135
|
+
[example-url]: https://example.arcjet.com
|
|
136
|
+
[quick-start]: https://docs.arcjet.com/get-started/nodejs
|
|
137
|
+
[example-source]: https://github.com/arcjet/arcjet-js-example
|
|
124
138
|
[rate-limit-concepts-docs]: https://docs.arcjet.com/rate-limiting/concepts
|
|
125
139
|
[shield-concepts-docs]: https://docs.arcjet.com/shield/concepts
|
|
126
140
|
[apache-license]: http://www.apache.org/licenses/LICENSE-2.0
|
package/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ArcjetDecision, ArcjetOptions, Primitive, Product,
|
|
1
|
+
import { ArcjetDecision, ArcjetOptions, Primitive, Product, ExtraProps, RemoteClient, RemoteClientOptions } from "arcjet";
|
|
2
2
|
export * from "arcjet";
|
|
3
3
|
type Simplify<T> = {
|
|
4
4
|
[KeyType in keyof T]: T[KeyType];
|
|
@@ -10,7 +10,7 @@ type WithoutCustomProps = {
|
|
|
10
10
|
type PlainObject = {
|
|
11
11
|
[key: string]: unknown;
|
|
12
12
|
};
|
|
13
|
-
export declare function createNodeRemoteClient(options?: RemoteClientOptions): RemoteClient;
|
|
13
|
+
export declare function createNodeRemoteClient(options?: Partial<RemoteClientOptions>): RemoteClient;
|
|
14
14
|
export interface ArcjetNodeRequest {
|
|
15
15
|
headers?: Record<string, string | string[] | undefined>;
|
|
16
16
|
socket?: Partial<{
|
|
@@ -26,7 +26,6 @@ export interface ArcjetNodeRequest {
|
|
|
26
26
|
* make a decision about how a Node.js request should be handled.
|
|
27
27
|
*/
|
|
28
28
|
export interface ArcjetNode<Props extends PlainObject> {
|
|
29
|
-
get runtime(): Runtime;
|
|
30
29
|
/**
|
|
31
30
|
* Runs a request through the configured protections. The request is
|
|
32
31
|
* analyzed and then a decision made on whether to allow, deny, or challenge
|
package/index.js
CHANGED
|
@@ -1,22 +1,34 @@
|
|
|
1
1
|
import { createConnectTransport } from '@connectrpc/connect-node';
|
|
2
|
-
import core__default, {
|
|
2
|
+
import core__default, { createRemoteClient } from 'arcjet';
|
|
3
3
|
export * from 'arcjet';
|
|
4
4
|
import findIP from '@arcjet/ip';
|
|
5
|
+
import ArcjetHeaders from '@arcjet/headers';
|
|
6
|
+
import { logLevel, baseUrl, isProduction, platform, isDevelopment } from '@arcjet/env';
|
|
7
|
+
import { Logger } from '@arcjet/logger';
|
|
5
8
|
|
|
6
9
|
function createNodeRemoteClient(options) {
|
|
7
10
|
// The base URL for the Arcjet API. Will default to the standard production
|
|
8
11
|
// API unless environment variable `ARCJET_BASE_URL` is set.
|
|
9
|
-
const
|
|
12
|
+
const url = options?.baseUrl ?? baseUrl(process.env);
|
|
13
|
+
// The timeout for the Arcjet API in milliseconds. This is set to a low value
|
|
14
|
+
// in production so calls fail open.
|
|
15
|
+
const timeout = options?.timeout ?? (isProduction(process.env) ? 500 : 1000);
|
|
10
16
|
// Transport is the HTTP client that the client uses to make requests.
|
|
11
17
|
const transport = options?.transport ??
|
|
12
18
|
createConnectTransport({
|
|
13
|
-
baseUrl,
|
|
19
|
+
baseUrl: url,
|
|
14
20
|
httpVersion: "2",
|
|
15
21
|
});
|
|
16
|
-
// TODO(#223):
|
|
22
|
+
// TODO(#223): Create separate options type to exclude these
|
|
17
23
|
const sdkStack = "NODEJS";
|
|
18
|
-
const sdkVersion = "1.0.0-alpha.
|
|
19
|
-
return createRemoteClient({
|
|
24
|
+
const sdkVersion = "1.0.0-alpha.14";
|
|
25
|
+
return createRemoteClient({
|
|
26
|
+
transport,
|
|
27
|
+
baseUrl: url,
|
|
28
|
+
timeout,
|
|
29
|
+
sdkStack,
|
|
30
|
+
sdkVersion,
|
|
31
|
+
});
|
|
20
32
|
}
|
|
21
33
|
function cookiesToString(cookies) {
|
|
22
34
|
if (typeof cookies === "undefined") {
|
|
@@ -29,9 +41,20 @@ function cookiesToString(cookies) {
|
|
|
29
41
|
return cookies;
|
|
30
42
|
}
|
|
31
43
|
function toArcjetRequest(request, props) {
|
|
44
|
+
// We pull the cookies from the request before wrapping them in ArcjetHeaders
|
|
45
|
+
const cookies = cookiesToString(request.headers?.cookie);
|
|
32
46
|
// We construct an ArcjetHeaders to normalize over Headers
|
|
33
47
|
const headers = new ArcjetHeaders(request.headers);
|
|
34
|
-
|
|
48
|
+
let ip = findIP(request, headers, { platform: platform(process.env) });
|
|
49
|
+
if (ip === "") {
|
|
50
|
+
// If the `ip` is empty but we're in development mode, we default the IP
|
|
51
|
+
// so the request doesn't fail.
|
|
52
|
+
if (isDevelopment(process.env)) {
|
|
53
|
+
// TODO: Log that the fingerprint is being overridden once the adapter
|
|
54
|
+
// constructs the logger
|
|
55
|
+
ip = "127.0.0.1";
|
|
56
|
+
}
|
|
57
|
+
}
|
|
35
58
|
const method = request.method ?? "";
|
|
36
59
|
const host = headers.get("host") ?? "";
|
|
37
60
|
let path = "";
|
|
@@ -61,7 +84,6 @@ function toArcjetRequest(request, props) {
|
|
|
61
84
|
else {
|
|
62
85
|
path = request.url ?? "";
|
|
63
86
|
}
|
|
64
|
-
const cookies = cookiesToString(request.headers?.cookie);
|
|
65
87
|
return {
|
|
66
88
|
...props,
|
|
67
89
|
ip,
|
|
@@ -76,9 +98,6 @@ function toArcjetRequest(request, props) {
|
|
|
76
98
|
}
|
|
77
99
|
function withClient(aj) {
|
|
78
100
|
return Object.freeze({
|
|
79
|
-
get runtime() {
|
|
80
|
-
return aj.runtime;
|
|
81
|
-
},
|
|
82
101
|
withRule(rule) {
|
|
83
102
|
const client = aj.withRule(rule);
|
|
84
103
|
return withClient(client);
|
|
@@ -88,7 +107,7 @@ function withClient(aj) {
|
|
|
88
107
|
// Further investigation makes it seem like it has something to do with
|
|
89
108
|
// the definition of `props` in the signature but it's hard to track down
|
|
90
109
|
const req = toArcjetRequest(request, props ?? {});
|
|
91
|
-
return aj.protect(req);
|
|
110
|
+
return aj.protect({}, req);
|
|
92
111
|
},
|
|
93
112
|
});
|
|
94
113
|
}
|
|
@@ -102,7 +121,12 @@ function withClient(aj) {
|
|
|
102
121
|
*/
|
|
103
122
|
function arcjet(options) {
|
|
104
123
|
const client = options.client ?? createNodeRemoteClient();
|
|
105
|
-
const
|
|
124
|
+
const log = options.log
|
|
125
|
+
? options.log
|
|
126
|
+
: new Logger({
|
|
127
|
+
level: logLevel(process.env),
|
|
128
|
+
});
|
|
129
|
+
const aj = core__default({ ...options, client, log });
|
|
106
130
|
return withClient(aj);
|
|
107
131
|
}
|
|
108
132
|
|
package/index.ts
CHANGED
|
@@ -4,17 +4,23 @@ import core, {
|
|
|
4
4
|
ArcjetOptions,
|
|
5
5
|
Primitive,
|
|
6
6
|
Product,
|
|
7
|
-
ArcjetHeaders,
|
|
8
|
-
Runtime,
|
|
9
7
|
ArcjetRequest,
|
|
10
8
|
ExtraProps,
|
|
11
9
|
RemoteClient,
|
|
12
10
|
RemoteClientOptions,
|
|
13
|
-
defaultBaseUrl,
|
|
14
11
|
createRemoteClient,
|
|
15
12
|
Arcjet,
|
|
16
13
|
} from "arcjet";
|
|
17
14
|
import findIP from "@arcjet/ip";
|
|
15
|
+
import ArcjetHeaders from "@arcjet/headers";
|
|
16
|
+
import {
|
|
17
|
+
baseUrl,
|
|
18
|
+
isDevelopment,
|
|
19
|
+
isProduction,
|
|
20
|
+
logLevel,
|
|
21
|
+
platform,
|
|
22
|
+
} from "@arcjet/env";
|
|
23
|
+
import { Logger } from "@arcjet/logger";
|
|
18
24
|
|
|
19
25
|
// Re-export all named exports from the generic SDK
|
|
20
26
|
export * from "arcjet";
|
|
@@ -57,25 +63,35 @@ type PlainObject = {
|
|
|
57
63
|
};
|
|
58
64
|
|
|
59
65
|
export function createNodeRemoteClient(
|
|
60
|
-
options?: RemoteClientOptions
|
|
66
|
+
options?: Partial<RemoteClientOptions>,
|
|
61
67
|
): RemoteClient {
|
|
62
68
|
// The base URL for the Arcjet API. Will default to the standard production
|
|
63
69
|
// API unless environment variable `ARCJET_BASE_URL` is set.
|
|
64
|
-
const
|
|
70
|
+
const url = options?.baseUrl ?? baseUrl(process.env);
|
|
71
|
+
|
|
72
|
+
// The timeout for the Arcjet API in milliseconds. This is set to a low value
|
|
73
|
+
// in production so calls fail open.
|
|
74
|
+
const timeout = options?.timeout ?? (isProduction(process.env) ? 500 : 1000);
|
|
65
75
|
|
|
66
76
|
// Transport is the HTTP client that the client uses to make requests.
|
|
67
77
|
const transport =
|
|
68
78
|
options?.transport ??
|
|
69
79
|
createConnectTransport({
|
|
70
|
-
baseUrl,
|
|
80
|
+
baseUrl: url,
|
|
71
81
|
httpVersion: "2",
|
|
72
82
|
});
|
|
73
83
|
|
|
74
|
-
// TODO(#223):
|
|
84
|
+
// TODO(#223): Create separate options type to exclude these
|
|
75
85
|
const sdkStack = "NODEJS";
|
|
76
86
|
const sdkVersion = "__ARCJET_SDK_VERSION__";
|
|
77
87
|
|
|
78
|
-
return createRemoteClient({
|
|
88
|
+
return createRemoteClient({
|
|
89
|
+
transport,
|
|
90
|
+
baseUrl: url,
|
|
91
|
+
timeout,
|
|
92
|
+
sdkStack,
|
|
93
|
+
sdkVersion,
|
|
94
|
+
});
|
|
79
95
|
}
|
|
80
96
|
|
|
81
97
|
// Interface of fields that the Arcjet Node.js SDK expects on `IncomingMessage`
|
|
@@ -106,7 +122,6 @@ function cookiesToString(cookies: string | string[] | undefined): string {
|
|
|
106
122
|
* make a decision about how a Node.js request should be handled.
|
|
107
123
|
*/
|
|
108
124
|
export interface ArcjetNode<Props extends PlainObject> {
|
|
109
|
-
get runtime(): Runtime;
|
|
110
125
|
/**
|
|
111
126
|
* Runs a request through the configured protections. The request is
|
|
112
127
|
* analyzed and then a decision made on whether to allow, deny, or challenge
|
|
@@ -139,10 +154,22 @@ function toArcjetRequest<Props extends PlainObject>(
|
|
|
139
154
|
request: ArcjetNodeRequest,
|
|
140
155
|
props: Props,
|
|
141
156
|
): ArcjetRequest<Props> {
|
|
157
|
+
// We pull the cookies from the request before wrapping them in ArcjetHeaders
|
|
158
|
+
const cookies = cookiesToString(request.headers?.cookie);
|
|
159
|
+
|
|
142
160
|
// We construct an ArcjetHeaders to normalize over Headers
|
|
143
161
|
const headers = new ArcjetHeaders(request.headers);
|
|
144
162
|
|
|
145
|
-
|
|
163
|
+
let ip = findIP(request, headers, { platform: platform(process.env) });
|
|
164
|
+
if (ip === "") {
|
|
165
|
+
// If the `ip` is empty but we're in development mode, we default the IP
|
|
166
|
+
// so the request doesn't fail.
|
|
167
|
+
if (isDevelopment(process.env)) {
|
|
168
|
+
// TODO: Log that the fingerprint is being overridden once the adapter
|
|
169
|
+
// constructs the logger
|
|
170
|
+
ip = "127.0.0.1";
|
|
171
|
+
}
|
|
172
|
+
}
|
|
146
173
|
const method = request.method ?? "";
|
|
147
174
|
const host = headers.get("host") ?? "";
|
|
148
175
|
let path = "";
|
|
@@ -172,8 +199,6 @@ function toArcjetRequest<Props extends PlainObject>(
|
|
|
172
199
|
path = request.url ?? "";
|
|
173
200
|
}
|
|
174
201
|
|
|
175
|
-
const cookies = cookiesToString(request.headers?.cookie);
|
|
176
|
-
|
|
177
202
|
return {
|
|
178
203
|
...props,
|
|
179
204
|
ip,
|
|
@@ -191,9 +216,6 @@ function withClient<const Rules extends (Primitive | Product)[]>(
|
|
|
191
216
|
aj: Arcjet<ExtraProps<Rules>>,
|
|
192
217
|
): ArcjetNode<ExtraProps<Rules>> {
|
|
193
218
|
return Object.freeze({
|
|
194
|
-
get runtime() {
|
|
195
|
-
return aj.runtime;
|
|
196
|
-
},
|
|
197
219
|
withRule(rule: Primitive | Product) {
|
|
198
220
|
const client = aj.withRule(rule);
|
|
199
221
|
return withClient(client);
|
|
@@ -211,7 +233,7 @@ function withClient<const Rules extends (Primitive | Product)[]>(
|
|
|
211
233
|
ExtraProps<Rules>
|
|
212
234
|
>;
|
|
213
235
|
|
|
214
|
-
return aj.protect(req);
|
|
236
|
+
return aj.protect({}, req);
|
|
215
237
|
},
|
|
216
238
|
});
|
|
217
239
|
}
|
|
@@ -229,7 +251,13 @@ export default function arcjet<const Rules extends (Primitive | Product)[]>(
|
|
|
229
251
|
): ArcjetNode<Simplify<ExtraProps<Rules>>> {
|
|
230
252
|
const client = options.client ?? createNodeRemoteClient();
|
|
231
253
|
|
|
232
|
-
const
|
|
254
|
+
const log = options.log
|
|
255
|
+
? options.log
|
|
256
|
+
: new Logger({
|
|
257
|
+
level: logLevel(process.env),
|
|
258
|
+
});
|
|
259
|
+
|
|
260
|
+
const aj = core({ ...options, client, log });
|
|
233
261
|
|
|
234
262
|
return withClient(aj);
|
|
235
263
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@arcjet/node",
|
|
3
|
-
"version": "1.0.0-alpha.
|
|
3
|
+
"version": "1.0.0-alpha.14",
|
|
4
4
|
"description": "Arcjet SDK for Node.js",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"homepage": "https://arcjet.com",
|
|
@@ -40,17 +40,20 @@
|
|
|
40
40
|
"test": "NODE_OPTIONS=--experimental-vm-modules jest --passWithNoTests"
|
|
41
41
|
},
|
|
42
42
|
"dependencies": {
|
|
43
|
-
"@arcjet/
|
|
43
|
+
"@arcjet/env": "1.0.0-alpha.14",
|
|
44
|
+
"@arcjet/headers": "1.0.0-alpha.14",
|
|
45
|
+
"@arcjet/ip": "1.0.0-alpha.14",
|
|
46
|
+
"@arcjet/logger": "1.0.0-alpha.14",
|
|
44
47
|
"@connectrpc/connect-node": "1.4.0",
|
|
45
|
-
"arcjet": "1.0.0-alpha.
|
|
48
|
+
"arcjet": "1.0.0-alpha.14"
|
|
46
49
|
},
|
|
47
50
|
"devDependencies": {
|
|
48
|
-
"@arcjet/eslint-config": "1.0.0-alpha.
|
|
49
|
-
"@arcjet/rollup-config": "1.0.0-alpha.
|
|
50
|
-
"@arcjet/tsconfig": "1.0.0-alpha.
|
|
51
|
+
"@arcjet/eslint-config": "1.0.0-alpha.14",
|
|
52
|
+
"@arcjet/rollup-config": "1.0.0-alpha.14",
|
|
53
|
+
"@arcjet/tsconfig": "1.0.0-alpha.14",
|
|
51
54
|
"@jest/globals": "29.7.0",
|
|
52
55
|
"@types/node": "18.18.0",
|
|
53
|
-
"@rollup/wasm-node": "4.
|
|
56
|
+
"@rollup/wasm-node": "4.18.0",
|
|
54
57
|
"jest": "29.7.0",
|
|
55
58
|
"typescript": "5.4.5"
|
|
56
59
|
},
|