@arcjet/bun 1.0.0-beta.1 → 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/README.md +41 -62
- package/index.d.ts +76 -23
- package/index.js +30 -17
- package/package.json +32 -25
package/README.md
CHANGED
|
@@ -22,6 +22,9 @@ against common attacks.
|
|
|
22
22
|
|
|
23
23
|
This is the [Arcjet][arcjet] SDK for [Bun.sh][bun-sh].
|
|
24
24
|
|
|
25
|
+
- [npm package (`@arcjet/bun`)](https://www.npmjs.com/package/@arcjet/bun)
|
|
26
|
+
- [GitHub source code (`arcjet-bun/` in `arcjet/arcjet-js`)](https://github.com/arcjet/arcjet-js/tree/main/arcjet-bun)
|
|
27
|
+
|
|
25
28
|
## Getting started
|
|
26
29
|
|
|
27
30
|
Visit the [quick start guide][quick-start] to get started.
|
|
@@ -31,100 +34,76 @@ Visit the [quick start guide][quick-start] to get started.
|
|
|
31
34
|
Try an Arcjet protected app live at [https://example.arcjet.com][example-url]
|
|
32
35
|
([source code][example-source]).
|
|
33
36
|
|
|
34
|
-
##
|
|
37
|
+
## What is this?
|
|
35
38
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
+
This is our adapter to integrate Arcjet into Bun.
|
|
40
|
+
Arcjet helps you secure your Bun server.
|
|
41
|
+
This package exists so that we can provide the best possible experience to
|
|
42
|
+
Bun users.
|
|
39
43
|
|
|
40
|
-
##
|
|
44
|
+
## When should I use this?
|
|
41
45
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
10 tokens and refills by 5 tokens every 10 seconds. Each request consumes 5
|
|
46
|
-
tokens.
|
|
46
|
+
You can use this if you are using Bun.
|
|
47
|
+
See our [_Get started_ guide][arcjet-get-started] for other supported
|
|
48
|
+
runtimes.
|
|
47
49
|
|
|
48
|
-
|
|
49
|
-
import arcjet, { tokenBucket, detectBot } from "@arcjet/bun";
|
|
50
|
+
## Install
|
|
50
51
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
characteristics: ["userId"], // track requests by a custom user ID
|
|
54
|
-
rules: [
|
|
55
|
-
// Create a token bucket rate limit. Other algorithms are supported.
|
|
56
|
-
tokenBucket({
|
|
57
|
-
mode: "LIVE", // will block requests. Use "DRY_RUN" to log only
|
|
58
|
-
refillRate: 5, // refill 5 tokens per interval
|
|
59
|
-
interval: 10, // refill every 10 seconds
|
|
60
|
-
capacity: 10, // bucket maximum capacity of 10 tokens
|
|
61
|
-
}),
|
|
62
|
-
detectBot({
|
|
63
|
-
mode: "LIVE", // will block requests. Use "DRY_RUN" to log only
|
|
64
|
-
// configured with a list of bots to allow from
|
|
65
|
-
// https://arcjet.com/bot-list
|
|
66
|
-
allow: [], // "allow none" will block all detected bots
|
|
67
|
-
}),
|
|
68
|
-
],
|
|
69
|
-
});
|
|
70
|
-
|
|
71
|
-
export default {
|
|
72
|
-
port: 8000,
|
|
73
|
-
fetch: aj.handler(async function (req: Request) {
|
|
74
|
-
const userId = "user123"; // Replace with your authenticated user ID
|
|
75
|
-
const decision = await aj.protect(req, { userId, requested: 5 }); // Deduct 5 tokens from the bucket
|
|
76
|
-
console.log("Arcjet decision", decision);
|
|
52
|
+
This package is ESM only.
|
|
53
|
+
Install with Bun:
|
|
77
54
|
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
} else {
|
|
81
|
-
return Response.json({ message: "Hello world" });
|
|
82
|
-
}
|
|
83
|
-
}),
|
|
84
|
-
};
|
|
55
|
+
```sh
|
|
56
|
+
bun add @arcjet/bun
|
|
85
57
|
```
|
|
86
58
|
|
|
87
|
-
##
|
|
88
|
-
|
|
89
|
-
[Arcjet Shield][shield-concepts-docs] protects your application against common
|
|
90
|
-
attacks, including the OWASP Top 10. You can run Shield on every request with
|
|
91
|
-
negligible performance impact.
|
|
59
|
+
## Use
|
|
92
60
|
|
|
93
61
|
```ts
|
|
94
62
|
import arcjet, { shield } from "@arcjet/bun";
|
|
95
63
|
|
|
64
|
+
// Get your Arcjet key at <https://app.arcjet.com>.
|
|
65
|
+
// Set it as an environment variable instead of hard coding it.
|
|
66
|
+
const arcjetKey = Bun.env.ARCJET_KEY;
|
|
67
|
+
|
|
68
|
+
if (!arcjetKey) {
|
|
69
|
+
throw new Error("Cannot find `ARCJET_KEY` environment variable");
|
|
70
|
+
}
|
|
71
|
+
|
|
96
72
|
const aj = arcjet({
|
|
97
|
-
key:
|
|
73
|
+
key: arcjetKey,
|
|
98
74
|
rules: [
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
}),
|
|
75
|
+
// Shield protects your app from common attacks.
|
|
76
|
+
// Use `DRY_RUN` instead of `LIVE` to only log.
|
|
77
|
+
shield({ mode: "LIVE" }),
|
|
102
78
|
],
|
|
103
79
|
});
|
|
104
80
|
|
|
105
81
|
export default {
|
|
106
82
|
port: 8000,
|
|
107
|
-
fetch: aj.handler(async function (
|
|
108
|
-
const decision = await aj.protect(
|
|
83
|
+
fetch: aj.handler(async function (request: Request) {
|
|
84
|
+
const decision = await aj.protect(request);
|
|
109
85
|
|
|
110
86
|
if (decision.isDenied()) {
|
|
111
|
-
return Response.json({
|
|
112
|
-
} else {
|
|
113
|
-
return Response.json({ message: "Hello world" });
|
|
87
|
+
return Response.json({ message: "Forbidden" }, { status: 403 });
|
|
114
88
|
}
|
|
89
|
+
|
|
90
|
+
return Response.json({ message: "Hello world" });
|
|
115
91
|
}),
|
|
116
92
|
};
|
|
117
93
|
```
|
|
118
94
|
|
|
95
|
+
For more on how to configure Arcjet with Bun and how to protect Bun,
|
|
96
|
+
see the [Arcjet Bun SDK reference][arcjet-reference-bun] on our website.
|
|
97
|
+
|
|
119
98
|
## License
|
|
120
99
|
|
|
121
|
-
|
|
100
|
+
[Apache License, Version 2.0][apache-license] © [Arcjet Labs, Inc.][arcjet]
|
|
122
101
|
|
|
102
|
+
[arcjet-get-started]: https://docs.arcjet.com/get-started
|
|
103
|
+
[arcjet-reference-bun]: https://docs.arcjet.com/reference/bun
|
|
123
104
|
[arcjet]: https://arcjet.com
|
|
124
105
|
[bun-sh]: https://bun.sh/
|
|
125
106
|
[example-url]: https://example.arcjet.com
|
|
126
107
|
[quick-start]: https://docs.arcjet.com/get-started/bun
|
|
127
108
|
[example-source]: https://github.com/arcjet/arcjet-js-example
|
|
128
|
-
[rate-limit-concepts-docs]: https://docs.arcjet.com/rate-limiting/concepts
|
|
129
|
-
[shield-concepts-docs]: https://docs.arcjet.com/shield/concepts
|
|
130
109
|
[apache-license]: http://www.apache.org/licenses/LICENSE-2.0
|
package/index.d.ts
CHANGED
|
@@ -11,58 +11,111 @@ type WithoutCustomProps = {
|
|
|
11
11
|
type PlainObject = {
|
|
12
12
|
[key: string]: unknown;
|
|
13
13
|
};
|
|
14
|
+
/**
|
|
15
|
+
* Configuration for {@linkcode createRemoteClient}.
|
|
16
|
+
*/
|
|
14
17
|
export type RemoteClientOptions = {
|
|
18
|
+
/**
|
|
19
|
+
* Base URI for HTTP requests to Decide API (optional).
|
|
20
|
+
*
|
|
21
|
+
* Defaults to the environment variable `ARCJET_BASE_URL` (if that value
|
|
22
|
+
* is known and allowed) and the standard production API otherwise.
|
|
23
|
+
*/
|
|
15
24
|
baseUrl?: string;
|
|
25
|
+
/**
|
|
26
|
+
* Timeout in milliseconds for the Decide API (optional).
|
|
27
|
+
*
|
|
28
|
+
* Defaults to `500` in production and `1000` in development.
|
|
29
|
+
*/
|
|
16
30
|
timeout?: number;
|
|
17
31
|
};
|
|
32
|
+
/**
|
|
33
|
+
* Create a remote client.
|
|
34
|
+
*
|
|
35
|
+
* @param options
|
|
36
|
+
* Configuration (optional).
|
|
37
|
+
* @returns
|
|
38
|
+
* Client.
|
|
39
|
+
*/
|
|
18
40
|
export declare function createRemoteClient(options?: RemoteClientOptions): import("@arcjet/protocol/client.js").Client;
|
|
19
41
|
/**
|
|
20
|
-
*
|
|
42
|
+
* Configuration for the Bun integration of Arcjet.
|
|
43
|
+
*
|
|
44
|
+
* @template Rules
|
|
45
|
+
* List of rules.
|
|
46
|
+
* @template Characteristics
|
|
47
|
+
* Characteristics to track a user by.
|
|
21
48
|
*/
|
|
22
49
|
export type ArcjetOptions<Rules extends [...Array<Primitive | Product>], Characteristics extends readonly string[]> = Simplify<CoreOptions<Rules, Characteristics> & {
|
|
23
50
|
/**
|
|
24
|
-
*
|
|
25
|
-
*
|
|
51
|
+
* IP addresses and CIDR ranges of trusted load balancers and proxies
|
|
52
|
+
* (optional, example: `["100.100.100.100", "100.100.100.0/24"]`).
|
|
26
53
|
*/
|
|
27
54
|
proxies?: Array<string>;
|
|
28
55
|
}>;
|
|
29
56
|
/**
|
|
30
|
-
*
|
|
31
|
-
*
|
|
57
|
+
* Instance of the Bun integration of Arcjet.
|
|
58
|
+
*
|
|
59
|
+
* Primarily has a `protect()` method to make a decision about how a Bun request
|
|
60
|
+
* should be handled.
|
|
61
|
+
*
|
|
62
|
+
* @template Props
|
|
63
|
+
* Configuration.
|
|
32
64
|
*/
|
|
33
65
|
export interface ArcjetBun<Props extends PlainObject> {
|
|
34
66
|
/**
|
|
35
|
-
*
|
|
36
|
-
* analyzed and then a decision made on whether to allow, deny, or challenge
|
|
37
|
-
* the request.
|
|
67
|
+
* Make a decision about how to handle a request.
|
|
38
68
|
*
|
|
39
|
-
*
|
|
40
|
-
*
|
|
41
|
-
*
|
|
69
|
+
* This will analyze the request locally where possible and otherwise call
|
|
70
|
+
* the Arcjet decision API.
|
|
71
|
+
*
|
|
72
|
+
* @param ctx
|
|
73
|
+
* Additional context for this function call.
|
|
74
|
+
* @param request
|
|
75
|
+
* Details about the {@linkcode Request} that Arcjet needs to make a
|
|
76
|
+
* decision.
|
|
77
|
+
* @param props
|
|
78
|
+
* Additional properties required for running rules against a request.
|
|
79
|
+
* @returns
|
|
80
|
+
* Promise that resolves to an {@linkcode ArcjetDecision} indicating
|
|
81
|
+
* Arcjet’s decision about the request.
|
|
42
82
|
*/
|
|
43
83
|
protect(request: Request, ...props: Props extends WithoutCustomProps ? [] : [Props]): Promise<ArcjetDecision>;
|
|
44
84
|
/**
|
|
45
|
-
*
|
|
46
|
-
*
|
|
85
|
+
* Augment the client with another rule.
|
|
86
|
+
*
|
|
87
|
+
* Useful for varying rules based on criteria in your handler such as
|
|
88
|
+
* different rate limit for logged in users.
|
|
47
89
|
*
|
|
48
|
-
* @
|
|
49
|
-
*
|
|
90
|
+
* @template Rule
|
|
91
|
+
* Type of rule.
|
|
92
|
+
* @param rule
|
|
93
|
+
* Rule to add to Arcjet.
|
|
94
|
+
* @returns
|
|
95
|
+
* Arcjet instance augmented with the given rule.
|
|
50
96
|
*/
|
|
51
97
|
withRule<Rule extends Primitive | Product>(rule: Rule): ArcjetBun<Simplify<Props & ExtraProps<Rule>>>;
|
|
52
98
|
/**
|
|
53
|
-
*
|
|
54
|
-
*
|
|
99
|
+
* Wrap the Bun `fetch` handler to provide additional details when calling
|
|
100
|
+
* the `protect()` method.
|
|
55
101
|
*
|
|
56
|
-
* @param fetch
|
|
102
|
+
* @param fetch
|
|
103
|
+
* Original `fetch` from Bun.
|
|
104
|
+
* @returns
|
|
105
|
+
* Wrapped `fetch` handler.
|
|
57
106
|
*/
|
|
58
107
|
handler(fetch: (this: Server, request: Request, server: Server) => Response | Promise<Response>): (this: Server, request: Request, server: Server) => Response | Promise<Response>;
|
|
59
108
|
}
|
|
60
109
|
/**
|
|
61
|
-
* Create a new
|
|
62
|
-
* outside of a request handler so it persists across requests. If you need to
|
|
63
|
-
* augment a client inside a handler, call the `withRule()` function on the base
|
|
64
|
-
* client.
|
|
110
|
+
* Create a new Bun integration of Arcjet.
|
|
65
111
|
*
|
|
66
|
-
* @
|
|
112
|
+
* @template Rules
|
|
113
|
+
* List of rules.
|
|
114
|
+
* @template Characteristics
|
|
115
|
+
* Characteristics to track a user by.
|
|
116
|
+
* @param options
|
|
117
|
+
* Configuration.
|
|
118
|
+
* @returns
|
|
119
|
+
* Bun integration of Arcjet.
|
|
67
120
|
*/
|
|
68
121
|
export default function arcjet<const Rules extends (Primitive | Product)[], const Characteristics extends readonly string[]>(options: ArcjetOptions<Rules, Characteristics>): ArcjetBun<Simplify<ExtraProps<Rules> & CharacteristicProps<Characteristics>>>;
|
package/index.js
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
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 { env } from 'bun';
|
|
6
|
-
import { logLevel,
|
|
6
|
+
import { logLevel, isDevelopment, baseUrl, platform } from '@arcjet/env';
|
|
7
7
|
import { Logger } from '@arcjet/logger';
|
|
8
8
|
import { createClient } from '@arcjet/protocol/client.js';
|
|
9
9
|
import { createTransport } from '@arcjet/transport';
|
|
10
10
|
|
|
11
|
-
/// <reference types="bun-types
|
|
11
|
+
/// <reference types="bun-types" />
|
|
12
12
|
// TODO: Deduplicate with other packages
|
|
13
13
|
function errorMessage(err) {
|
|
14
14
|
if (err) {
|
|
@@ -23,17 +23,21 @@ function errorMessage(err) {
|
|
|
23
23
|
}
|
|
24
24
|
return "Unknown problem";
|
|
25
25
|
}
|
|
26
|
+
/**
|
|
27
|
+
* Create a remote client.
|
|
28
|
+
*
|
|
29
|
+
* @param options
|
|
30
|
+
* Configuration (optional).
|
|
31
|
+
* @returns
|
|
32
|
+
* Client.
|
|
33
|
+
*/
|
|
26
34
|
function createRemoteClient(options) {
|
|
27
|
-
// The base URL for the Arcjet API. Will default to the standard production
|
|
28
|
-
// API unless environment variable `ARCJET_BASE_URL` is set.
|
|
29
35
|
const url = options?.baseUrl ?? baseUrl(env);
|
|
30
|
-
// The timeout for the Arcjet API in milliseconds. This is set to a low value
|
|
31
|
-
// in production so calls fail open.
|
|
32
36
|
const timeout = options?.timeout ?? (isDevelopment(env) ? 1000 : 500);
|
|
33
37
|
// Transport is the HTTP client that the client uses to make requests.
|
|
34
38
|
const transport = createTransport(url);
|
|
35
39
|
const sdkStack = "BUN";
|
|
36
|
-
const sdkVersion = "1.0.0-beta.
|
|
40
|
+
const sdkVersion = "1.0.0-beta.11";
|
|
37
41
|
return createClient({
|
|
38
42
|
transport,
|
|
39
43
|
baseUrl: url,
|
|
@@ -43,12 +47,16 @@ function createRemoteClient(options) {
|
|
|
43
47
|
});
|
|
44
48
|
}
|
|
45
49
|
/**
|
|
46
|
-
* Create a new
|
|
47
|
-
* outside of a request handler so it persists across requests. If you need to
|
|
48
|
-
* augment a client inside a handler, call the `withRule()` function on the base
|
|
49
|
-
* client.
|
|
50
|
+
* Create a new Bun integration of Arcjet.
|
|
50
51
|
*
|
|
51
|
-
* @
|
|
52
|
+
* @template Rules
|
|
53
|
+
* List of rules.
|
|
54
|
+
* @template Characteristics
|
|
55
|
+
* Characteristics to track a user by.
|
|
56
|
+
* @param options
|
|
57
|
+
* Configuration.
|
|
58
|
+
* @returns
|
|
59
|
+
* Bun integration of Arcjet.
|
|
52
60
|
*/
|
|
53
61
|
function arcjet(options) {
|
|
54
62
|
const client = options.client ?? createRemoteClient();
|
|
@@ -61,23 +69,28 @@ function arcjet(options) {
|
|
|
61
69
|
: new Logger({
|
|
62
70
|
level: logLevel(env),
|
|
63
71
|
});
|
|
72
|
+
const proxies = Array.isArray(options.proxies)
|
|
73
|
+
? options.proxies.map(parseProxy)
|
|
74
|
+
: undefined;
|
|
75
|
+
if (isDevelopment(process.env)) {
|
|
76
|
+
log.warn("Arcjet will use 127.0.0.1 when missing public IP address in development mode");
|
|
77
|
+
}
|
|
64
78
|
function toArcjetRequest(request, props) {
|
|
65
79
|
const cookies = request.headers.get("cookie") ?? undefined;
|
|
66
80
|
// We construct an ArcjetHeaders to normalize over Headers
|
|
67
81
|
const headers = new ArcjetHeaders(request.headers);
|
|
68
82
|
const url = new URL(request.url);
|
|
69
|
-
let ip =
|
|
83
|
+
let ip = findIp({
|
|
70
84
|
// This attempts to lookup the IP in the `ipCache`. This is primarily a
|
|
71
85
|
// workaround to the API design in Bun that requires access to the
|
|
72
86
|
// `Server` to lookup an IP.
|
|
73
87
|
ip: ipCache.get(request),
|
|
74
88
|
headers,
|
|
75
|
-
}, { platform: platform(env), proxies
|
|
89
|
+
}, { platform: platform(env), proxies });
|
|
76
90
|
if (ip === "") {
|
|
77
91
|
// If the `ip` is empty but we're in development mode, we default the IP
|
|
78
92
|
// so the request doesn't fail.
|
|
79
93
|
if (isDevelopment(env)) {
|
|
80
|
-
log.warn("Using 127.0.0.1 as IP address in development mode");
|
|
81
94
|
ip = "127.0.0.1";
|
|
82
95
|
}
|
|
83
96
|
else {
|
package/package.json
CHANGED
|
@@ -1,7 +1,19 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@arcjet/bun",
|
|
3
|
-
"version": "1.0.0-beta.
|
|
4
|
-
"description": "Arcjet SDK for Bun
|
|
3
|
+
"version": "1.0.0-beta.11",
|
|
4
|
+
"description": "Arcjet SDK for Bun",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"analyze",
|
|
7
|
+
"arcjet",
|
|
8
|
+
"attack",
|
|
9
|
+
"bun.sh",
|
|
10
|
+
"bun",
|
|
11
|
+
"limit",
|
|
12
|
+
"protect",
|
|
13
|
+
"secure",
|
|
14
|
+
"security",
|
|
15
|
+
"verify"
|
|
16
|
+
],
|
|
5
17
|
"license": "Apache-2.0",
|
|
6
18
|
"homepage": "https://arcjet.com",
|
|
7
19
|
"repository": {
|
|
@@ -23,37 +35,32 @@
|
|
|
23
35
|
"main": "./index.js",
|
|
24
36
|
"types": "./index.d.ts",
|
|
25
37
|
"files": [
|
|
26
|
-
"
|
|
27
|
-
"
|
|
28
|
-
"*.js",
|
|
29
|
-
"*.d.ts",
|
|
30
|
-
"!*.config.js"
|
|
38
|
+
"index.d.ts",
|
|
39
|
+
"index.js"
|
|
31
40
|
],
|
|
32
41
|
"scripts": {
|
|
33
|
-
"prepublishOnly": "npm run build",
|
|
34
42
|
"build": "rollup --config rollup.config.js",
|
|
35
43
|
"lint": "eslint .",
|
|
36
|
-
"
|
|
37
|
-
"test": "
|
|
44
|
+
"prepublishOnly": "npm run build",
|
|
45
|
+
"test": "npm run build && npm run lint"
|
|
38
46
|
},
|
|
39
47
|
"dependencies": {
|
|
40
|
-
"@arcjet/env": "1.0.0-beta.
|
|
41
|
-
"@arcjet/headers": "1.0.0-beta.
|
|
42
|
-
"@arcjet/ip": "1.0.0-beta.
|
|
43
|
-
"@arcjet/logger": "1.0.0-beta.
|
|
44
|
-
"@arcjet/protocol": "1.0.0-beta.
|
|
45
|
-
"@arcjet/transport": "1.0.0-beta.
|
|
46
|
-
"arcjet": "1.0.0-beta.
|
|
48
|
+
"@arcjet/env": "1.0.0-beta.11",
|
|
49
|
+
"@arcjet/headers": "1.0.0-beta.11",
|
|
50
|
+
"@arcjet/ip": "1.0.0-beta.11",
|
|
51
|
+
"@arcjet/logger": "1.0.0-beta.11",
|
|
52
|
+
"@arcjet/protocol": "1.0.0-beta.11",
|
|
53
|
+
"@arcjet/transport": "1.0.0-beta.11",
|
|
54
|
+
"arcjet": "1.0.0-beta.11"
|
|
47
55
|
},
|
|
48
56
|
"devDependencies": {
|
|
49
|
-
"@arcjet/eslint-config": "1.0.0-beta.
|
|
50
|
-
"@arcjet/rollup-config": "1.0.0-beta.
|
|
51
|
-
"@
|
|
52
|
-
"
|
|
53
|
-
"
|
|
54
|
-
"
|
|
55
|
-
"
|
|
56
|
-
"typescript": "5.7.3"
|
|
57
|
+
"@arcjet/eslint-config": "1.0.0-beta.11",
|
|
58
|
+
"@arcjet/rollup-config": "1.0.0-beta.11",
|
|
59
|
+
"@rollup/wasm-node": "4.50.0",
|
|
60
|
+
"bun-types": "1.2.21",
|
|
61
|
+
"eslint": "9.34.0",
|
|
62
|
+
"typescript": "5.9.2",
|
|
63
|
+
"undici-types": "7.15.0"
|
|
57
64
|
},
|
|
58
65
|
"publishConfig": {
|
|
59
66
|
"access": "public",
|