@arcjet/node 1.0.0-alpha.13 → 1.0.0-alpha.15

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 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-logo-dark-planet-arrival.svg">
4
- <img src="https://arcjet.com/arcjet-logo-light-planet-arrival.svg" alt="Arcjet Logo" height="144" width="auto">
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 & defend
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`](https://www.npmjs.com/package/@arcjet/next) package.
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. It’s enabled by default and runs on every
86
- request with negligible performance impact.
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
- // and set it as an environment variable rather than hard coding.
95
- // See: https://nextjs.org/docs/app/building-your-application/configuring/environment-variables
96
- key: process.env.ARCJET_KEY,
97
- rules: [], // Shield requires no rule configuration
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, Runtime, ExtraProps, RemoteClient, RemoteClientOptions } from "arcjet";
1
+ import { ArcjetDecision, ArcjetOptions, Primitive, Product, ExtraProps } from "arcjet";
2
2
  export * from "arcjet";
3
3
  type Simplify<T> = {
4
4
  [KeyType in keyof T]: T[KeyType];
@@ -10,7 +10,11 @@ type WithoutCustomProps = {
10
10
  type PlainObject = {
11
11
  [key: string]: unknown;
12
12
  };
13
- export declare function createNodeRemoteClient(options?: RemoteClientOptions): RemoteClient;
13
+ export type RemoteClientOptions = {
14
+ baseUrl?: string;
15
+ timeout?: number;
16
+ };
17
+ export declare function createRemoteClient(options?: RemoteClientOptions): import("@arcjet/protocol/client.js").Client;
14
18
  export interface ArcjetNodeRequest {
15
19
  headers?: Record<string, string | string[] | undefined>;
16
20
  socket?: Partial<{
@@ -26,7 +30,6 @@ export interface ArcjetNodeRequest {
26
30
  * make a decision about how a Node.js request should be handled.
27
31
  */
28
32
  export interface ArcjetNode<Props extends PlainObject> {
29
- get runtime(): Runtime;
30
33
  /**
31
34
  * Runs a request through the configured protections. The request is
32
35
  * analyzed and then a decision made on whether to allow, deny, or challenge
package/index.js CHANGED
@@ -1,22 +1,33 @@
1
1
  import { createConnectTransport } from '@connectrpc/connect-node';
2
- import core__default, { defaultBaseUrl, createRemoteClient, ArcjetHeaders } from 'arcjet';
2
+ import core__default 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';
8
+ import { createClient } from '@arcjet/protocol/client.js';
5
9
 
6
- function createNodeRemoteClient(options) {
10
+ function createRemoteClient(options) {
7
11
  // The base URL for the Arcjet API. Will default to the standard production
8
12
  // API unless environment variable `ARCJET_BASE_URL` is set.
9
- const baseUrl = options?.baseUrl ?? defaultBaseUrl();
13
+ const url = options?.baseUrl ?? baseUrl(process.env);
14
+ // The timeout for the Arcjet API in milliseconds. This is set to a low value
15
+ // in production so calls fail open.
16
+ const timeout = options?.timeout ?? (isProduction(process.env) ? 500 : 1000);
10
17
  // Transport is the HTTP client that the client uses to make requests.
11
- const transport = options?.transport ??
12
- createConnectTransport({
13
- baseUrl,
14
- httpVersion: "2",
15
- });
16
- // TODO(#223): Do we want to allow overrides to either of these? If not, we should probably define a separate type for `options`
18
+ const transport = createConnectTransport({
19
+ baseUrl: url,
20
+ httpVersion: "2",
21
+ });
17
22
  const sdkStack = "NODEJS";
18
- const sdkVersion = "1.0.0-alpha.13";
19
- return createRemoteClient({ ...options, transport, sdkStack, sdkVersion });
23
+ const sdkVersion = "1.0.0-alpha.15";
24
+ return createClient({
25
+ transport,
26
+ baseUrl: url,
27
+ timeout,
28
+ sdkStack,
29
+ sdkVersion,
30
+ });
20
31
  }
21
32
  function cookiesToString(cookies) {
22
33
  if (typeof cookies === "undefined") {
@@ -33,7 +44,16 @@ function toArcjetRequest(request, props) {
33
44
  const cookies = cookiesToString(request.headers?.cookie);
34
45
  // We construct an ArcjetHeaders to normalize over Headers
35
46
  const headers = new ArcjetHeaders(request.headers);
36
- const ip = findIP(request, headers);
47
+ let ip = findIP(request, headers, { platform: platform(process.env) });
48
+ if (ip === "") {
49
+ // If the `ip` is empty but we're in development mode, we default the IP
50
+ // so the request doesn't fail.
51
+ if (isDevelopment(process.env)) {
52
+ // TODO: Log that the fingerprint is being overridden once the adapter
53
+ // constructs the logger
54
+ ip = "127.0.0.1";
55
+ }
56
+ }
37
57
  const method = request.method ?? "";
38
58
  const host = headers.get("host") ?? "";
39
59
  let path = "";
@@ -77,9 +97,6 @@ function toArcjetRequest(request, props) {
77
97
  }
78
98
  function withClient(aj) {
79
99
  return Object.freeze({
80
- get runtime() {
81
- return aj.runtime;
82
- },
83
100
  withRule(rule) {
84
101
  const client = aj.withRule(rule);
85
102
  return withClient(client);
@@ -89,7 +106,7 @@ function withClient(aj) {
89
106
  // Further investigation makes it seem like it has something to do with
90
107
  // the definition of `props` in the signature but it's hard to track down
91
108
  const req = toArcjetRequest(request, props ?? {});
92
- return aj.protect(req);
109
+ return aj.protect({}, req);
93
110
  },
94
111
  });
95
112
  }
@@ -102,9 +119,14 @@ function withClient(aj) {
102
119
  * @param options - Arcjet configuration options to apply to all requests.
103
120
  */
104
121
  function arcjet(options) {
105
- const client = options.client ?? createNodeRemoteClient();
106
- const aj = core__default({ ...options, client });
122
+ const client = options.client ?? createRemoteClient();
123
+ const log = options.log
124
+ ? options.log
125
+ : new Logger({
126
+ level: logLevel(process.env),
127
+ });
128
+ const aj = core__default({ ...options, client, log });
107
129
  return withClient(aj);
108
130
  }
109
131
 
110
- export { createNodeRemoteClient, arcjet as default };
132
+ export { createRemoteClient, arcjet as default };
package/index.ts CHANGED
@@ -4,17 +4,21 @@ import core, {
4
4
  ArcjetOptions,
5
5
  Primitive,
6
6
  Product,
7
- ArcjetHeaders,
8
- Runtime,
9
7
  ArcjetRequest,
10
8
  ExtraProps,
11
- RemoteClient,
12
- RemoteClientOptions,
13
- defaultBaseUrl,
14
- createRemoteClient,
15
9
  Arcjet,
16
10
  } from "arcjet";
17
11
  import findIP from "@arcjet/ip";
12
+ import ArcjetHeaders from "@arcjet/headers";
13
+ import {
14
+ baseUrl,
15
+ isDevelopment,
16
+ isProduction,
17
+ logLevel,
18
+ platform,
19
+ } from "@arcjet/env";
20
+ import { Logger } from "@arcjet/logger";
21
+ import { createClient } from "@arcjet/protocol/client.js";
18
22
 
19
23
  // Re-export all named exports from the generic SDK
20
24
  export * from "arcjet";
@@ -56,26 +60,36 @@ type PlainObject = {
56
60
  [key: string]: unknown;
57
61
  };
58
62
 
59
- export function createNodeRemoteClient(
60
- options?: RemoteClientOptions,
61
- ): RemoteClient {
63
+ export type RemoteClientOptions = {
64
+ baseUrl?: string;
65
+ timeout?: number;
66
+ };
67
+
68
+ export function createRemoteClient(options?: RemoteClientOptions) {
62
69
  // The base URL for the Arcjet API. Will default to the standard production
63
70
  // API unless environment variable `ARCJET_BASE_URL` is set.
64
- const baseUrl = options?.baseUrl ?? defaultBaseUrl();
71
+ const url = options?.baseUrl ?? baseUrl(process.env);
72
+
73
+ // The timeout for the Arcjet API in milliseconds. This is set to a low value
74
+ // in production so calls fail open.
75
+ const timeout = options?.timeout ?? (isProduction(process.env) ? 500 : 1000);
65
76
 
66
77
  // Transport is the HTTP client that the client uses to make requests.
67
- const transport =
68
- options?.transport ??
69
- createConnectTransport({
70
- baseUrl,
71
- httpVersion: "2",
72
- });
73
-
74
- // TODO(#223): Do we want to allow overrides to either of these? If not, we should probably define a separate type for `options`
78
+ const transport = createConnectTransport({
79
+ baseUrl: url,
80
+ httpVersion: "2",
81
+ });
82
+
75
83
  const sdkStack = "NODEJS";
76
84
  const sdkVersion = "__ARCJET_SDK_VERSION__";
77
85
 
78
- return createRemoteClient({ ...options, transport, sdkStack, sdkVersion });
86
+ return createClient({
87
+ transport,
88
+ baseUrl: url,
89
+ timeout,
90
+ sdkStack,
91
+ sdkVersion,
92
+ });
79
93
  }
80
94
 
81
95
  // Interface of fields that the Arcjet Node.js SDK expects on `IncomingMessage`
@@ -106,7 +120,6 @@ function cookiesToString(cookies: string | string[] | undefined): string {
106
120
  * make a decision about how a Node.js request should be handled.
107
121
  */
108
122
  export interface ArcjetNode<Props extends PlainObject> {
109
- get runtime(): Runtime;
110
123
  /**
111
124
  * Runs a request through the configured protections. The request is
112
125
  * analyzed and then a decision made on whether to allow, deny, or challenge
@@ -145,7 +158,16 @@ function toArcjetRequest<Props extends PlainObject>(
145
158
  // We construct an ArcjetHeaders to normalize over Headers
146
159
  const headers = new ArcjetHeaders(request.headers);
147
160
 
148
- const ip = findIP(request, headers);
161
+ let ip = findIP(request, headers, { platform: platform(process.env) });
162
+ if (ip === "") {
163
+ // If the `ip` is empty but we're in development mode, we default the IP
164
+ // so the request doesn't fail.
165
+ if (isDevelopment(process.env)) {
166
+ // TODO: Log that the fingerprint is being overridden once the adapter
167
+ // constructs the logger
168
+ ip = "127.0.0.1";
169
+ }
170
+ }
149
171
  const method = request.method ?? "";
150
172
  const host = headers.get("host") ?? "";
151
173
  let path = "";
@@ -192,9 +214,6 @@ function withClient<const Rules extends (Primitive | Product)[]>(
192
214
  aj: Arcjet<ExtraProps<Rules>>,
193
215
  ): ArcjetNode<ExtraProps<Rules>> {
194
216
  return Object.freeze({
195
- get runtime() {
196
- return aj.runtime;
197
- },
198
217
  withRule(rule: Primitive | Product) {
199
218
  const client = aj.withRule(rule);
200
219
  return withClient(client);
@@ -212,7 +231,7 @@ function withClient<const Rules extends (Primitive | Product)[]>(
212
231
  ExtraProps<Rules>
213
232
  >;
214
233
 
215
- return aj.protect(req);
234
+ return aj.protect({}, req);
216
235
  },
217
236
  });
218
237
  }
@@ -228,9 +247,15 @@ function withClient<const Rules extends (Primitive | Product)[]>(
228
247
  export default function arcjet<const Rules extends (Primitive | Product)[]>(
229
248
  options: ArcjetOptions<Rules>,
230
249
  ): ArcjetNode<Simplify<ExtraProps<Rules>>> {
231
- const client = options.client ?? createNodeRemoteClient();
250
+ const client = options.client ?? createRemoteClient();
251
+
252
+ const log = options.log
253
+ ? options.log
254
+ : new Logger({
255
+ level: logLevel(process.env),
256
+ });
232
257
 
233
- const aj = core({ ...options, client });
258
+ const aj = core({ ...options, client, log });
234
259
 
235
260
  return withClient(aj);
236
261
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@arcjet/node",
3
- "version": "1.0.0-alpha.13",
3
+ "version": "1.0.0-alpha.15",
4
4
  "description": "Arcjet SDK for Node.js",
5
5
  "license": "Apache-2.0",
6
6
  "homepage": "https://arcjet.com",
@@ -40,17 +40,21 @@
40
40
  "test": "NODE_OPTIONS=--experimental-vm-modules jest --passWithNoTests"
41
41
  },
42
42
  "dependencies": {
43
- "@arcjet/ip": "1.0.0-alpha.13",
43
+ "@arcjet/env": "1.0.0-alpha.15",
44
+ "@arcjet/headers": "1.0.0-alpha.15",
45
+ "@arcjet/ip": "1.0.0-alpha.15",
46
+ "@arcjet/logger": "1.0.0-alpha.15",
47
+ "@arcjet/protocol": "1.0.0-alpha.15",
44
48
  "@connectrpc/connect-node": "1.4.0",
45
- "arcjet": "1.0.0-alpha.13"
49
+ "arcjet": "1.0.0-alpha.15"
46
50
  },
47
51
  "devDependencies": {
48
- "@arcjet/eslint-config": "1.0.0-alpha.13",
49
- "@arcjet/rollup-config": "1.0.0-alpha.13",
50
- "@arcjet/tsconfig": "1.0.0-alpha.13",
52
+ "@arcjet/eslint-config": "1.0.0-alpha.15",
53
+ "@arcjet/rollup-config": "1.0.0-alpha.15",
54
+ "@arcjet/tsconfig": "1.0.0-alpha.15",
51
55
  "@jest/globals": "29.7.0",
52
56
  "@types/node": "18.18.0",
53
- "@rollup/wasm-node": "4.17.2",
57
+ "@rollup/wasm-node": "4.18.0",
54
58
  "jest": "29.7.0",
55
59
  "typescript": "5.4.5"
56
60
  },