@arcjet/bun 1.0.0-beta.1 → 1.0.0-beta.10
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.js +11 -6
- package/package.json +32 -24
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.js
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
import core__default from 'arcjet';
|
|
2
2
|
export * from 'arcjet';
|
|
3
|
-
import findIP from '@arcjet/ip';
|
|
3
|
+
import findIP, { parseProxy } from '@arcjet/ip';
|
|
4
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) {
|
|
@@ -33,7 +33,7 @@ function createRemoteClient(options) {
|
|
|
33
33
|
// Transport is the HTTP client that the client uses to make requests.
|
|
34
34
|
const transport = createTransport(url);
|
|
35
35
|
const sdkStack = "BUN";
|
|
36
|
-
const sdkVersion = "1.0.0-beta.
|
|
36
|
+
const sdkVersion = "1.0.0-beta.10";
|
|
37
37
|
return createClient({
|
|
38
38
|
transport,
|
|
39
39
|
baseUrl: url,
|
|
@@ -61,6 +61,12 @@ function arcjet(options) {
|
|
|
61
61
|
: new Logger({
|
|
62
62
|
level: logLevel(env),
|
|
63
63
|
});
|
|
64
|
+
const proxies = Array.isArray(options.proxies)
|
|
65
|
+
? options.proxies.map(parseProxy)
|
|
66
|
+
: undefined;
|
|
67
|
+
if (isDevelopment(process.env)) {
|
|
68
|
+
log.warn("Arcjet will use 127.0.0.1 when missing public IP address in development mode");
|
|
69
|
+
}
|
|
64
70
|
function toArcjetRequest(request, props) {
|
|
65
71
|
const cookies = request.headers.get("cookie") ?? undefined;
|
|
66
72
|
// We construct an ArcjetHeaders to normalize over Headers
|
|
@@ -72,12 +78,11 @@ function arcjet(options) {
|
|
|
72
78
|
// `Server` to lookup an IP.
|
|
73
79
|
ip: ipCache.get(request),
|
|
74
80
|
headers,
|
|
75
|
-
}, { platform: platform(env), proxies
|
|
81
|
+
}, { platform: platform(env), proxies });
|
|
76
82
|
if (ip === "") {
|
|
77
83
|
// If the `ip` is empty but we're in development mode, we default the IP
|
|
78
84
|
// so the request doesn't fail.
|
|
79
85
|
if (isDevelopment(env)) {
|
|
80
|
-
log.warn("Using 127.0.0.1 as IP address in development mode");
|
|
81
86
|
ip = "127.0.0.1";
|
|
82
87
|
}
|
|
83
88
|
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.10",
|
|
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,33 @@
|
|
|
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.10",
|
|
49
|
+
"@arcjet/headers": "1.0.0-beta.10",
|
|
50
|
+
"@arcjet/ip": "1.0.0-beta.10",
|
|
51
|
+
"@arcjet/logger": "1.0.0-beta.10",
|
|
52
|
+
"@arcjet/protocol": "1.0.0-beta.10",
|
|
53
|
+
"@arcjet/transport": "1.0.0-beta.10",
|
|
54
|
+
"arcjet": "1.0.0-beta.10"
|
|
47
55
|
},
|
|
48
56
|
"devDependencies": {
|
|
49
|
-
"@arcjet/eslint-config": "1.0.0-beta.
|
|
50
|
-
"@arcjet/rollup-config": "1.0.0-beta.
|
|
51
|
-
"@arcjet/tsconfig": "1.0.0-beta.
|
|
57
|
+
"@arcjet/eslint-config": "1.0.0-beta.10",
|
|
58
|
+
"@arcjet/rollup-config": "1.0.0-beta.10",
|
|
59
|
+
"@arcjet/tsconfig": "1.0.0-beta.10",
|
|
52
60
|
"@types/node": "20.12.12",
|
|
53
|
-
"@rollup/wasm-node": "4.
|
|
54
|
-
"bun-types": "1.
|
|
55
|
-
"
|
|
56
|
-
"typescript": "5.
|
|
61
|
+
"@rollup/wasm-node": "4.46.2",
|
|
62
|
+
"bun-types": "1.2.19",
|
|
63
|
+
"eslint": "9.32.0",
|
|
64
|
+
"typescript": "5.9.2"
|
|
57
65
|
},
|
|
58
66
|
"publishConfig": {
|
|
59
67
|
"access": "public",
|