@arcjet/node 1.0.0-alpha.13 → 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 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, 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, { defaultBaseUrl, createRemoteClient, ArcjetHeaders } from 'arcjet';
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 baseUrl = options?.baseUrl ?? defaultBaseUrl();
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): Do we want to allow overrides to either of these? If not, we should probably define a separate type for `options`
22
+ // TODO(#223): Create separate options type to exclude these
17
23
  const sdkStack = "NODEJS";
18
- const sdkVersion = "1.0.0-alpha.13";
19
- return createRemoteClient({ ...options, transport, sdkStack, sdkVersion });
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") {
@@ -33,7 +45,16 @@ function toArcjetRequest(request, props) {
33
45
  const cookies = cookiesToString(request.headers?.cookie);
34
46
  // We construct an ArcjetHeaders to normalize over Headers
35
47
  const headers = new ArcjetHeaders(request.headers);
36
- const ip = findIP(request, headers);
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
+ }
37
58
  const method = request.method ?? "";
38
59
  const host = headers.get("host") ?? "";
39
60
  let path = "";
@@ -77,9 +98,6 @@ function toArcjetRequest(request, props) {
77
98
  }
78
99
  function withClient(aj) {
79
100
  return Object.freeze({
80
- get runtime() {
81
- return aj.runtime;
82
- },
83
101
  withRule(rule) {
84
102
  const client = aj.withRule(rule);
85
103
  return withClient(client);
@@ -89,7 +107,7 @@ function withClient(aj) {
89
107
  // Further investigation makes it seem like it has something to do with
90
108
  // the definition of `props` in the signature but it's hard to track down
91
109
  const req = toArcjetRequest(request, props ?? {});
92
- return aj.protect(req);
110
+ return aj.protect({}, req);
93
111
  },
94
112
  });
95
113
  }
@@ -103,7 +121,12 @@ function withClient(aj) {
103
121
  */
104
122
  function arcjet(options) {
105
123
  const client = options.client ?? createNodeRemoteClient();
106
- const aj = core__default({ ...options, client });
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 });
107
130
  return withClient(aj);
108
131
  }
109
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 baseUrl = options?.baseUrl ?? defaultBaseUrl();
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): Do we want to allow overrides to either of these? If not, we should probably define a separate type for `options`
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({ ...options, transport, sdkStack, sdkVersion });
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
@@ -145,7 +160,16 @@ function toArcjetRequest<Props extends PlainObject>(
145
160
  // We construct an ArcjetHeaders to normalize over Headers
146
161
  const headers = new ArcjetHeaders(request.headers);
147
162
 
148
- const ip = findIP(request, headers);
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
+ }
149
173
  const method = request.method ?? "";
150
174
  const host = headers.get("host") ?? "";
151
175
  let path = "";
@@ -192,9 +216,6 @@ function withClient<const Rules extends (Primitive | Product)[]>(
192
216
  aj: Arcjet<ExtraProps<Rules>>,
193
217
  ): ArcjetNode<ExtraProps<Rules>> {
194
218
  return Object.freeze({
195
- get runtime() {
196
- return aj.runtime;
197
- },
198
219
  withRule(rule: Primitive | Product) {
199
220
  const client = aj.withRule(rule);
200
221
  return withClient(client);
@@ -212,7 +233,7 @@ function withClient<const Rules extends (Primitive | Product)[]>(
212
233
  ExtraProps<Rules>
213
234
  >;
214
235
 
215
- return aj.protect(req);
236
+ return aj.protect({}, req);
216
237
  },
217
238
  });
218
239
  }
@@ -230,7 +251,13 @@ export default function arcjet<const Rules extends (Primitive | Product)[]>(
230
251
  ): ArcjetNode<Simplify<ExtraProps<Rules>>> {
231
252
  const client = options.client ?? createNodeRemoteClient();
232
253
 
233
- const aj = core({ ...options, client });
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 });
234
261
 
235
262
  return withClient(aj);
236
263
  }
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.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/ip": "1.0.0-alpha.13",
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.13"
48
+ "arcjet": "1.0.0-alpha.14"
46
49
  },
47
50
  "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",
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.17.2",
56
+ "@rollup/wasm-node": "4.18.0",
54
57
  "jest": "29.7.0",
55
58
  "typescript": "5.4.5"
56
59
  },