@arcjet/node 1.0.0-alpha.31 → 1.0.0-alpha.33
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.js +1 -1
- package/package.json +14 -15
- package/index.ts +0 -349
package/index.js
CHANGED
|
@@ -32,7 +32,7 @@ function createRemoteClient(options) {
|
|
|
32
32
|
// Transport is the HTTP client that the client uses to make requests.
|
|
33
33
|
const transport = createTransport(url);
|
|
34
34
|
const sdkStack = "NODEJS";
|
|
35
|
-
const sdkVersion = "1.0.0-alpha.
|
|
35
|
+
const sdkVersion = "1.0.0-alpha.33";
|
|
36
36
|
return createClient({
|
|
37
37
|
transport,
|
|
38
38
|
baseUrl: url,
|
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.33",
|
|
4
4
|
"description": "Arcjet SDK for Node.js",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"homepage": "https://arcjet.com",
|
|
@@ -29,7 +29,6 @@
|
|
|
29
29
|
"README.md",
|
|
30
30
|
"*.js",
|
|
31
31
|
"*.d.ts",
|
|
32
|
-
"*.ts",
|
|
33
32
|
"!*.config.js"
|
|
34
33
|
],
|
|
35
34
|
"scripts": {
|
|
@@ -40,24 +39,24 @@
|
|
|
40
39
|
"test": "NODE_OPTIONS=--experimental-vm-modules jest --passWithNoTests"
|
|
41
40
|
},
|
|
42
41
|
"dependencies": {
|
|
43
|
-
"@arcjet/env": "1.0.0-alpha.
|
|
44
|
-
"@arcjet/headers": "1.0.0-alpha.
|
|
45
|
-
"@arcjet/ip": "1.0.0-alpha.
|
|
46
|
-
"@arcjet/logger": "1.0.0-alpha.
|
|
47
|
-
"@arcjet/protocol": "1.0.0-alpha.
|
|
48
|
-
"@arcjet/transport": "1.0.0-alpha.
|
|
49
|
-
"@arcjet/body": "1.0.0-alpha.
|
|
50
|
-
"arcjet": "1.0.0-alpha.
|
|
42
|
+
"@arcjet/env": "1.0.0-alpha.33",
|
|
43
|
+
"@arcjet/headers": "1.0.0-alpha.33",
|
|
44
|
+
"@arcjet/ip": "1.0.0-alpha.33",
|
|
45
|
+
"@arcjet/logger": "1.0.0-alpha.33",
|
|
46
|
+
"@arcjet/protocol": "1.0.0-alpha.33",
|
|
47
|
+
"@arcjet/transport": "1.0.0-alpha.33",
|
|
48
|
+
"@arcjet/body": "1.0.0-alpha.33",
|
|
49
|
+
"arcjet": "1.0.0-alpha.33"
|
|
51
50
|
},
|
|
52
51
|
"devDependencies": {
|
|
53
|
-
"@arcjet/eslint-config": "1.0.0-alpha.
|
|
54
|
-
"@arcjet/rollup-config": "1.0.0-alpha.
|
|
55
|
-
"@arcjet/tsconfig": "1.0.0-alpha.
|
|
52
|
+
"@arcjet/eslint-config": "1.0.0-alpha.33",
|
|
53
|
+
"@arcjet/rollup-config": "1.0.0-alpha.33",
|
|
54
|
+
"@arcjet/tsconfig": "1.0.0-alpha.33",
|
|
56
55
|
"@jest/globals": "29.7.0",
|
|
57
56
|
"@types/node": "18.18.0",
|
|
58
|
-
"@rollup/wasm-node": "4.27.
|
|
57
|
+
"@rollup/wasm-node": "4.27.4",
|
|
59
58
|
"jest": "29.7.0",
|
|
60
|
-
"typescript": "5.
|
|
59
|
+
"typescript": "5.7.2"
|
|
61
60
|
},
|
|
62
61
|
"publishConfig": {
|
|
63
62
|
"access": "public",
|
package/index.ts
DELETED
|
@@ -1,349 +0,0 @@
|
|
|
1
|
-
import core from "arcjet";
|
|
2
|
-
import type {
|
|
3
|
-
ArcjetDecision,
|
|
4
|
-
ArcjetOptions,
|
|
5
|
-
Primitive,
|
|
6
|
-
Product,
|
|
7
|
-
ArcjetRequest,
|
|
8
|
-
ExtraProps,
|
|
9
|
-
Arcjet,
|
|
10
|
-
CharacteristicProps,
|
|
11
|
-
} from "arcjet";
|
|
12
|
-
import findIP from "@arcjet/ip";
|
|
13
|
-
import ArcjetHeaders from "@arcjet/headers";
|
|
14
|
-
import { baseUrl, isDevelopment, logLevel, platform } from "@arcjet/env";
|
|
15
|
-
import { Logger } from "@arcjet/logger";
|
|
16
|
-
import { createClient } from "@arcjet/protocol/client.js";
|
|
17
|
-
import { createTransport } from "@arcjet/transport";
|
|
18
|
-
import { readBody } from "@arcjet/body";
|
|
19
|
-
|
|
20
|
-
// Re-export all named exports from the generic SDK
|
|
21
|
-
export * from "arcjet";
|
|
22
|
-
|
|
23
|
-
// TODO: Deduplicate with other packages
|
|
24
|
-
function errorMessage(err: unknown): string {
|
|
25
|
-
if (err) {
|
|
26
|
-
if (typeof err === "string") {
|
|
27
|
-
return err;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
if (
|
|
31
|
-
typeof err === "object" &&
|
|
32
|
-
"message" in err &&
|
|
33
|
-
typeof err.message === "string"
|
|
34
|
-
) {
|
|
35
|
-
return err.message;
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
return "Unknown problem";
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
// Type helpers from https://github.com/sindresorhus/type-fest but adjusted for
|
|
43
|
-
// our use.
|
|
44
|
-
//
|
|
45
|
-
// Simplify:
|
|
46
|
-
// https://github.com/sindresorhus/type-fest/blob/964466c9d59c711da57a5297ad954c13132a0001/source/simplify.d.ts
|
|
47
|
-
// EmptyObject:
|
|
48
|
-
// https://github.com/sindresorhus/type-fest/blob/b9723d4785f01f8d2487c09ee5871a1f615781aa/source/empty-object.d.ts
|
|
49
|
-
//
|
|
50
|
-
// Licensed: MIT License Copyright (c) Sindre Sorhus <sindresorhus@gmail.com>
|
|
51
|
-
// (https://sindresorhus.com)
|
|
52
|
-
//
|
|
53
|
-
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
54
|
-
// of this software and associated documentation files (the "Software"), to deal
|
|
55
|
-
// in the Software without restriction, including without limitation the rights
|
|
56
|
-
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
57
|
-
// copies of the Software, and to permit persons to whom the Software is
|
|
58
|
-
// furnished to do so, subject to the following conditions: The above copyright
|
|
59
|
-
// notice and this permission notice shall be included in all copies or
|
|
60
|
-
// substantial portions of the Software.
|
|
61
|
-
//
|
|
62
|
-
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
63
|
-
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
64
|
-
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
65
|
-
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
66
|
-
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
67
|
-
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
68
|
-
// SOFTWARE.
|
|
69
|
-
type Simplify<T> = { [KeyType in keyof T]: T[KeyType] } & {};
|
|
70
|
-
declare const emptyObjectSymbol: unique symbol;
|
|
71
|
-
type WithoutCustomProps = {
|
|
72
|
-
[emptyObjectSymbol]?: never;
|
|
73
|
-
};
|
|
74
|
-
|
|
75
|
-
type PlainObject = {
|
|
76
|
-
[key: string]: unknown;
|
|
77
|
-
};
|
|
78
|
-
|
|
79
|
-
export type RemoteClientOptions = {
|
|
80
|
-
baseUrl?: string;
|
|
81
|
-
timeout?: number;
|
|
82
|
-
};
|
|
83
|
-
|
|
84
|
-
export function createRemoteClient(options?: RemoteClientOptions) {
|
|
85
|
-
// The base URL for the Arcjet API. Will default to the standard production
|
|
86
|
-
// API unless environment variable `ARCJET_BASE_URL` is set.
|
|
87
|
-
const url = options?.baseUrl ?? baseUrl(process.env);
|
|
88
|
-
|
|
89
|
-
// The timeout for the Arcjet API in milliseconds. This is set to a low value
|
|
90
|
-
// in production so calls fail open.
|
|
91
|
-
const timeout = options?.timeout ?? (isDevelopment(process.env) ? 1000 : 500);
|
|
92
|
-
|
|
93
|
-
// Transport is the HTTP client that the client uses to make requests.
|
|
94
|
-
const transport = createTransport(url);
|
|
95
|
-
|
|
96
|
-
const sdkStack = "NODEJS";
|
|
97
|
-
const sdkVersion = "__ARCJET_SDK_VERSION__";
|
|
98
|
-
|
|
99
|
-
return createClient({
|
|
100
|
-
transport,
|
|
101
|
-
baseUrl: url,
|
|
102
|
-
timeout,
|
|
103
|
-
sdkStack,
|
|
104
|
-
sdkVersion,
|
|
105
|
-
});
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
type EventHandlerLike = (
|
|
109
|
-
event: string,
|
|
110
|
-
listener: (...args: any[]) => void,
|
|
111
|
-
) => void;
|
|
112
|
-
|
|
113
|
-
// Interface of fields that the Arcjet Node.js SDK expects on `IncomingMessage`
|
|
114
|
-
// objects.
|
|
115
|
-
export interface ArcjetNodeRequest {
|
|
116
|
-
headers?: Record<string, string | string[] | undefined>;
|
|
117
|
-
socket?: Partial<{ remoteAddress: string; encrypted: boolean }>;
|
|
118
|
-
method?: string;
|
|
119
|
-
httpVersion?: string;
|
|
120
|
-
url?: string;
|
|
121
|
-
// Things needed for getting a body
|
|
122
|
-
body?: unknown;
|
|
123
|
-
on?: EventHandlerLike;
|
|
124
|
-
removeListener?: EventHandlerLike;
|
|
125
|
-
readable?: boolean;
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
function cookiesToString(cookies: string | string[] | undefined): string {
|
|
129
|
-
if (typeof cookies === "undefined") {
|
|
130
|
-
return "";
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
// This should never be the case with a Node.js cookie header, but we are safe
|
|
134
|
-
if (Array.isArray(cookies)) {
|
|
135
|
-
return cookies.join("; ");
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
return cookies;
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
/**
|
|
142
|
-
* The ArcjetNode client provides a public `protect()` method to
|
|
143
|
-
* make a decision about how a Node.js request should be handled.
|
|
144
|
-
*/
|
|
145
|
-
export interface ArcjetNode<Props extends PlainObject> {
|
|
146
|
-
/**
|
|
147
|
-
* Runs a request through the configured protections. The request is
|
|
148
|
-
* analyzed and then a decision made on whether to allow, deny, or challenge
|
|
149
|
-
* the request.
|
|
150
|
-
*
|
|
151
|
-
* @param req - An `IncomingMessage` provided to the request handler.
|
|
152
|
-
* @param props - Additonal properties required for running rules against a request.
|
|
153
|
-
* @returns An {@link ArcjetDecision} indicating Arcjet's decision about the request.
|
|
154
|
-
*/
|
|
155
|
-
protect(
|
|
156
|
-
request: ArcjetNodeRequest,
|
|
157
|
-
// We use this neat trick from https://stackoverflow.com/a/52318137 to make a single spread parameter
|
|
158
|
-
// that is required if the ExtraProps aren't strictly an empty object
|
|
159
|
-
...props: Props extends WithoutCustomProps ? [] : [Props]
|
|
160
|
-
): Promise<ArcjetDecision>;
|
|
161
|
-
|
|
162
|
-
/**
|
|
163
|
-
* Augments the client with another rule. Useful for varying rules based on
|
|
164
|
-
* criteria in your handler—e.g. different rate limit for logged in users.
|
|
165
|
-
*
|
|
166
|
-
* @param rule The rule to add to this execution.
|
|
167
|
-
* @returns An augmented {@link ArcjetNode} client.
|
|
168
|
-
*/
|
|
169
|
-
withRule<Rule extends Primitive | Product>(
|
|
170
|
-
rule: Rule,
|
|
171
|
-
): ArcjetNode<Simplify<Props & ExtraProps<Rule>>>;
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
/**
|
|
175
|
-
* Create a new {@link ArcjetNode} client. Always build your initial client
|
|
176
|
-
* outside of a request handler so it persists across requests. If you need to
|
|
177
|
-
* augment a client inside a handler, call the `withRule()` function on the base
|
|
178
|
-
* client.
|
|
179
|
-
*
|
|
180
|
-
* @param options - Arcjet configuration options to apply to all requests.
|
|
181
|
-
*/
|
|
182
|
-
export default function arcjet<
|
|
183
|
-
const Rules extends (Primitive | Product)[],
|
|
184
|
-
const Characteristics extends readonly string[],
|
|
185
|
-
>(
|
|
186
|
-
options: ArcjetOptions<Rules, Characteristics>,
|
|
187
|
-
): ArcjetNode<
|
|
188
|
-
Simplify<ExtraProps<Rules> & CharacteristicProps<Characteristics>>
|
|
189
|
-
> {
|
|
190
|
-
const client = options.client ?? createRemoteClient();
|
|
191
|
-
|
|
192
|
-
const log = options.log
|
|
193
|
-
? options.log
|
|
194
|
-
: new Logger({
|
|
195
|
-
level: logLevel(process.env),
|
|
196
|
-
});
|
|
197
|
-
|
|
198
|
-
function toArcjetRequest<Props extends PlainObject>(
|
|
199
|
-
request: ArcjetNodeRequest,
|
|
200
|
-
props: Props,
|
|
201
|
-
): ArcjetRequest<Props> {
|
|
202
|
-
// We pull the cookies from the request before wrapping them in ArcjetHeaders
|
|
203
|
-
const cookies = cookiesToString(request.headers?.cookie);
|
|
204
|
-
|
|
205
|
-
// We construct an ArcjetHeaders to normalize over Headers
|
|
206
|
-
const headers = new ArcjetHeaders(request.headers);
|
|
207
|
-
|
|
208
|
-
let ip = findIP(
|
|
209
|
-
{
|
|
210
|
-
socket: request.socket,
|
|
211
|
-
headers,
|
|
212
|
-
},
|
|
213
|
-
{ platform: platform(process.env) },
|
|
214
|
-
);
|
|
215
|
-
if (ip === "") {
|
|
216
|
-
// If the `ip` is empty but we're in development mode, we default the IP
|
|
217
|
-
// so the request doesn't fail.
|
|
218
|
-
if (isDevelopment(process.env)) {
|
|
219
|
-
log.warn("Using 127.0.0.1 as IP address in development mode");
|
|
220
|
-
ip = "127.0.0.1";
|
|
221
|
-
} else {
|
|
222
|
-
log.warn(
|
|
223
|
-
`Client IP address is missing. If this is a dev environment set the ARCJET_ENV env var to "development"`,
|
|
224
|
-
);
|
|
225
|
-
}
|
|
226
|
-
}
|
|
227
|
-
const method = request.method ?? "";
|
|
228
|
-
const host = headers.get("host") ?? "";
|
|
229
|
-
let path = "";
|
|
230
|
-
let query = "";
|
|
231
|
-
let protocol = "";
|
|
232
|
-
|
|
233
|
-
if (typeof request.socket?.encrypted !== "undefined") {
|
|
234
|
-
protocol = request.socket.encrypted ? "https:" : "http:";
|
|
235
|
-
} else {
|
|
236
|
-
protocol = "http:";
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
// Do some very simple validation, but also try/catch around URL parsing
|
|
240
|
-
if (
|
|
241
|
-
typeof request.url !== "undefined" &&
|
|
242
|
-
request.url !== "" &&
|
|
243
|
-
host !== ""
|
|
244
|
-
) {
|
|
245
|
-
try {
|
|
246
|
-
const url = new URL(request.url, `${protocol}//${host}`);
|
|
247
|
-
path = url.pathname;
|
|
248
|
-
query = url.search;
|
|
249
|
-
protocol = url.protocol;
|
|
250
|
-
} catch {
|
|
251
|
-
// If the parsing above fails, just set the path as whatever url we
|
|
252
|
-
// received.
|
|
253
|
-
path = request.url ?? "";
|
|
254
|
-
log.warn('Unable to parse URL. Using "%s" as `path`.', path);
|
|
255
|
-
}
|
|
256
|
-
} else {
|
|
257
|
-
path = request.url ?? "";
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
return {
|
|
261
|
-
...props,
|
|
262
|
-
ip,
|
|
263
|
-
method,
|
|
264
|
-
protocol,
|
|
265
|
-
host,
|
|
266
|
-
path,
|
|
267
|
-
headers,
|
|
268
|
-
cookies,
|
|
269
|
-
query,
|
|
270
|
-
};
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
function withClient<const Rules extends (Primitive | Product)[]>(
|
|
274
|
-
aj: Arcjet<ExtraProps<Rules>>,
|
|
275
|
-
): ArcjetNode<ExtraProps<Rules>> {
|
|
276
|
-
return Object.freeze({
|
|
277
|
-
withRule(rule: Primitive | Product) {
|
|
278
|
-
const client = aj.withRule(rule);
|
|
279
|
-
return withClient(client);
|
|
280
|
-
},
|
|
281
|
-
async protect(
|
|
282
|
-
request: ArcjetNodeRequest,
|
|
283
|
-
...[props]: ExtraProps<Rules> extends WithoutCustomProps
|
|
284
|
-
? []
|
|
285
|
-
: [ExtraProps<Rules>]
|
|
286
|
-
): Promise<ArcjetDecision> {
|
|
287
|
-
// TODO(#220): The generic manipulations get really mad here, so we cast
|
|
288
|
-
// Further investigation makes it seem like it has something to do with
|
|
289
|
-
// the definition of `props` in the signature but it's hard to track down
|
|
290
|
-
const req = toArcjetRequest(request, props ?? {}) as ArcjetRequest<
|
|
291
|
-
ExtraProps<Rules>
|
|
292
|
-
>;
|
|
293
|
-
|
|
294
|
-
const getBody = async () => {
|
|
295
|
-
try {
|
|
296
|
-
// If request.body is present then the body was likely read by a package like express' `body-parser`.
|
|
297
|
-
// If it's not present then we attempt to read the bytes from the IncomingMessage ourselves.
|
|
298
|
-
if (typeof request.body === "string") {
|
|
299
|
-
return request.body;
|
|
300
|
-
} else if (
|
|
301
|
-
typeof request.body !== "undefined" &&
|
|
302
|
-
// BigInt cannot be serialized with JSON.stringify
|
|
303
|
-
typeof request.body !== "bigint"
|
|
304
|
-
) {
|
|
305
|
-
return JSON.stringify(request.body);
|
|
306
|
-
}
|
|
307
|
-
|
|
308
|
-
if (
|
|
309
|
-
typeof request.on === "function" &&
|
|
310
|
-
typeof request.removeListener === "function"
|
|
311
|
-
) {
|
|
312
|
-
let expectedLength: number | undefined;
|
|
313
|
-
// TODO: This shouldn't need to build headers again but the type
|
|
314
|
-
// for `req` above is overly relaxed
|
|
315
|
-
const headers = new ArcjetHeaders(request.headers);
|
|
316
|
-
const expectedLengthStr = headers.get("content-length");
|
|
317
|
-
if (typeof expectedLengthStr === "string") {
|
|
318
|
-
try {
|
|
319
|
-
expectedLength = parseInt(expectedLengthStr, 10);
|
|
320
|
-
} catch {
|
|
321
|
-
// If the expected length couldn't be parsed we'll just not set one.
|
|
322
|
-
}
|
|
323
|
-
}
|
|
324
|
-
// Awaited to throw if it rejects and we'll just return undefined
|
|
325
|
-
const body = await readBody(request, {
|
|
326
|
-
// We will process 1mb bodies
|
|
327
|
-
limit: 1048576,
|
|
328
|
-
expectedLength,
|
|
329
|
-
});
|
|
330
|
-
return body;
|
|
331
|
-
}
|
|
332
|
-
|
|
333
|
-
log.warn("no body available");
|
|
334
|
-
return;
|
|
335
|
-
} catch (e) {
|
|
336
|
-
log.error("failed to get request body: %s", errorMessage(e));
|
|
337
|
-
return;
|
|
338
|
-
}
|
|
339
|
-
};
|
|
340
|
-
|
|
341
|
-
return aj.protect({ getBody }, req);
|
|
342
|
-
},
|
|
343
|
-
});
|
|
344
|
-
}
|
|
345
|
-
|
|
346
|
-
const aj = core({ ...options, client, log });
|
|
347
|
-
|
|
348
|
-
return withClient(aj);
|
|
349
|
-
}
|