@arcjet/bun 1.0.0-beta.16 → 1.0.0-beta.18

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.
Files changed (3) hide show
  1. package/index.d.ts +16 -6
  2. package/index.js +26 -31
  3. package/package.json +14 -13
package/index.d.ts CHANGED
@@ -5,12 +5,22 @@ type Simplify<T> = {
5
5
  [KeyType in keyof T]: T[KeyType];
6
6
  } & {};
7
7
  declare const emptyObjectSymbol: unique symbol;
8
- type WithoutCustomProps = {
9
- [emptyObjectSymbol]?: never;
10
- };
11
8
  type PlainObject = {
12
9
  [key: string]: unknown;
13
10
  };
11
+ /**
12
+ * Dynamically generate whether zero or one `properties` object must or can be passed.
13
+ */
14
+ type MaybeProperties<T> = {
15
+ [P in keyof T]?: T[P];
16
+ } extends T ? T extends {
17
+ [emptyObjectSymbol]?: never;
18
+ } ? [
19
+ ] : [
20
+ properties?: T
21
+ ] : [
22
+ properties: T
23
+ ];
14
24
  /**
15
25
  * Configuration for {@linkcode createRemoteClient}.
16
26
  */
@@ -80,7 +90,7 @@ export interface ArcjetBun<Props extends PlainObject> {
80
90
  * Promise that resolves to an {@linkcode ArcjetDecision} indicating
81
91
  * Arcjet’s decision about the request.
82
92
  */
83
- protect(request: Request, ...props: Props extends WithoutCustomProps ? [] : [Props]): Promise<ArcjetDecision>;
93
+ protect(request: Request, ...properties: MaybeProperties<Props>): Promise<ArcjetDecision>;
84
94
  /**
85
95
  * Augment the client with another rule.
86
96
  *
@@ -94,7 +104,7 @@ export interface ArcjetBun<Props extends PlainObject> {
94
104
  * @returns
95
105
  * Arcjet instance augmented with the given rule.
96
106
  */
97
- withRule<Rule extends Primitive | Product>(rule: Rule): ArcjetBun<Simplify<Props & ExtraProps<Rule>>>;
107
+ withRule<ChildProperties extends PlainObject>(rule: Primitive<ChildProperties> | Product<ChildProperties>): ArcjetBun<Props & ChildProperties>;
98
108
  /**
99
109
  * Wrap the Bun `fetch` handler to provide additional details when calling
100
110
  * the `protect()` method.
@@ -118,4 +128,4 @@ export interface ArcjetBun<Props extends PlainObject> {
118
128
  * @returns
119
129
  * Bun integration of Arcjet.
120
130
  */
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>>>;
131
+ export default function arcjet<const Rules extends (Primitive | Product)[], const Characteristics extends readonly string[]>(options: ArcjetOptions<Rules, Characteristics>): ArcjetBun<ExtraProps<Rules> & CharacteristicProps<Characteristics>>;
package/index.js CHANGED
@@ -4,26 +4,14 @@ export * from 'arcjet';
4
4
  import findIp, { parseProxy } from '@arcjet/ip';
5
5
  import { ArcjetHeaders } from '@arcjet/headers';
6
6
  import { env } from 'bun';
7
+ import { readBodyWeb } from '@arcjet/body';
7
8
  import { logLevel, isDevelopment, baseUrl, platform } from '@arcjet/env';
8
9
  import { Logger } from '@arcjet/logger';
9
10
  import { createClient } from '@arcjet/protocol/client.js';
10
11
  import { createTransport } from '@arcjet/transport';
11
12
 
12
13
  /// <reference types="bun-types" />
13
- // TODO: Deduplicate with other packages
14
- function errorMessage(err) {
15
- if (err) {
16
- if (typeof err === "string") {
17
- return err;
18
- }
19
- if (typeof err === "object" &&
20
- "message" in err &&
21
- typeof err.message === "string") {
22
- return err.message;
23
- }
24
- }
25
- return "Unknown problem";
26
- }
14
+ let warnedForAutomaticBodyRead = false;
27
15
  /**
28
16
  * Create a remote client.
29
17
  *
@@ -38,7 +26,7 @@ function createRemoteClient(options) {
38
26
  // Transport is the HTTP client that the client uses to make requests.
39
27
  const transport = createTransport(url);
40
28
  const sdkStack = "BUN";
41
- const sdkVersion = "1.0.0-beta.16";
29
+ const sdkVersion = "1.0.0-beta.18";
42
30
  return createClient({
43
31
  transport,
44
32
  baseUrl: url,
@@ -77,7 +65,7 @@ function arcjet(options) {
77
65
  log.warn("Arcjet will use 127.0.0.1 when missing public IP address in development mode");
78
66
  }
79
67
  function toArcjetRequest(request, props) {
80
- const cookies = request.headers.get("cookie") ?? undefined;
68
+ const cookies = request.headers.get("cookie") ?? "";
81
69
  // We construct an ArcjetHeaders to normalize over Headers
82
70
  const headers = new ArcjetHeaders(request.headers);
83
71
  const url = new URL(request.url);
@@ -115,27 +103,33 @@ function arcjet(options) {
115
103
  };
116
104
  }
117
105
  function withClient(aj) {
118
- return Object.freeze({
106
+ const client = {
119
107
  withRule(rule) {
120
108
  const client = aj.withRule(rule);
121
109
  return withClient(client);
122
110
  },
123
- async protect(request, ...[props]) {
124
- // TODO(#220): The generic manipulations get really mad here, so we cast
125
- // Further investigation makes it seem like it has something to do with
126
- // the definition of `props` in the signature but it's hard to track down
127
- const req = toArcjetRequest(request, props ?? {});
111
+ async protect(request, props) {
112
+ // Cast of `{}` because here we switch from `undefined` to `Properties`.
113
+ const req = toArcjetRequest(request, props || {});
128
114
  const getBody = async () => {
129
- try {
130
- const clonedRequest = request.clone();
131
- // Awaited to throw if it rejects and we'll just return undefined
132
- const body = await clonedRequest.text();
133
- return body;
115
+ if (request.bodyUsed) {
116
+ throw new Error("Cannot read body: already read");
134
117
  }
135
- catch (e) {
136
- log.error("failed to get request body: %s", errorMessage(e));
137
- return;
118
+ const clonedRequest = request.clone();
119
+ let expectedLength;
120
+ const expectedLengthString = request.headers.get("content-length");
121
+ if (typeof expectedLengthString === "string") {
122
+ expectedLength = parseInt(expectedLengthString, 10);
138
123
  }
124
+ // HEAD and GET requests do not have a body.
125
+ if (!clonedRequest.body) {
126
+ throw new Error("Cannot read body: body is missing");
127
+ }
128
+ if (!warnedForAutomaticBodyRead) {
129
+ warnedForAutomaticBodyRead = true;
130
+ log.warn("Automatically reading the request body is deprecated; please pass an explicit `sensitiveInfoValue` field. See <https://docs.arcjet.com/upgrading/sdk-migration>.");
131
+ }
132
+ return readBodyWeb(clonedRequest.body, { expectedLength });
139
133
  };
140
134
  return aj.protect({ getBody }, req);
141
135
  },
@@ -148,7 +142,8 @@ function arcjet(options) {
148
142
  return fetch.call(server, request, server);
149
143
  };
150
144
  },
151
- });
145
+ };
146
+ return Object.freeze(client);
152
147
  }
153
148
  const aj = core__default({ ...options, client, log });
154
149
  return withClient(aj);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@arcjet/bun",
3
- "version": "1.0.0-beta.16",
3
+ "version": "1.0.0-beta.18",
4
4
  "description": "Arcjet SDK for Bun",
5
5
  "keywords": [
6
6
  "analyze",
@@ -45,22 +45,23 @@
45
45
  "test": "npm run build && npm run lint"
46
46
  },
47
47
  "dependencies": {
48
- "@arcjet/env": "1.0.0-beta.16",
49
- "@arcjet/headers": "1.0.0-beta.16",
50
- "@arcjet/ip": "1.0.0-beta.16",
51
- "@arcjet/logger": "1.0.0-beta.16",
52
- "@arcjet/protocol": "1.0.0-beta.16",
53
- "@arcjet/transport": "1.0.0-beta.16",
54
- "arcjet": "1.0.0-beta.16"
48
+ "@arcjet/body": "1.0.0-beta.18",
49
+ "@arcjet/env": "1.0.0-beta.18",
50
+ "@arcjet/headers": "1.0.0-beta.18",
51
+ "@arcjet/ip": "1.0.0-beta.18",
52
+ "@arcjet/logger": "1.0.0-beta.18",
53
+ "@arcjet/protocol": "1.0.0-beta.18",
54
+ "@arcjet/transport": "1.0.0-beta.18",
55
+ "arcjet": "1.0.0-beta.18"
55
56
  },
56
57
  "devDependencies": {
57
- "@arcjet/eslint-config": "1.0.0-beta.16",
58
- "@arcjet/rollup-config": "1.0.0-beta.16",
59
- "@rollup/wasm-node": "4.54.0",
60
- "bun-types": "1.3.5",
58
+ "@arcjet/eslint-config": "1.0.0-beta.18",
59
+ "@arcjet/rollup-config": "1.0.0-beta.18",
60
+ "@rollup/wasm-node": "4.55.1",
61
+ "bun-types": "1.3.6",
61
62
  "eslint": "9.39.2",
62
63
  "typescript": "5.9.3",
63
- "undici-types": "7.15.0"
64
+ "undici-types": "7.18.2"
64
65
  },
65
66
  "publishConfig": {
66
67
  "access": "public",