Skip to content

Commit

Permalink
Merging e69d857 into trunk-temp/pr-841/8c58969a-cf34-48b9-b13c-785699…
Browse files Browse the repository at this point in the history
…984538
  • Loading branch information
trunk-io[bot] authored May 30, 2024
2 parents d1089ad + e69d857 commit b0f6ed0
Show file tree
Hide file tree
Showing 9 changed files with 137 additions and 53 deletions.
2 changes: 1 addition & 1 deletion arcjet-bun/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ function withClient<const Rules extends (Primitive | Product)[]>(
props ?? {},
) as ArcjetRequest<ExtraProps<Rules>>;

return aj.protect(req);
return aj.protect({}, req);
},
handler(
fetch: (
Expand Down
2 changes: 1 addition & 1 deletion arcjet-next/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ function withClient<const Rules extends (Primitive | Product)[]>(
ExtraProps<Rules>
>;

return aj.protect(req);
return aj.protect({}, req);
},
});
}
Expand Down
2 changes: 1 addition & 1 deletion arcjet-node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ function withClient<const Rules extends (Primitive | Product)[]>(
ExtraProps<Rules>
>;

return aj.protect(req);
return aj.protect({}, req);
},
});
}
Expand Down
2 changes: 1 addition & 1 deletion arcjet-sveltekit/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ function withClient<const Rules extends (Primitive | Product)[]>(
ExtraProps<Rules>
>;

return aj.protect(req);
return aj.protect({}, req);
},
});
}
Expand Down
6 changes: 5 additions & 1 deletion arcjet/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ const server = http.createServer(async function (
req: http.IncomingMessage,
res: http.ServerResponse,
) {
// Any sort of additional context that might want to be included for the
// execution of `protect()`. This is mostly only useful for writing adapters.
const ctx = {};

// Construct an object with Arcjet request details
const path = new URL(req.url || "", `http://${req.headers.host}`);
const details = {
Expand All @@ -71,7 +75,7 @@ const server = http.createServer(async function (
path: path.pathname,
};

const decision = await aj.protect(details);
const decision = await aj.protect(ctx, details);
console.log(decision);

if (decision.isDenied()) {
Expand Down
23 changes: 19 additions & 4 deletions arcjet/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,14 @@ export type ExtraProps<Rules> = Rules extends []
? UnionToIntersection<PropsForRule<Rules[number]>>
: never;

/**
* Additional context that can be provided by adapters.
*
* Among other things, this could include the Arcjet API Key if it were only
* available in a runtime handler or IP details provided by a platform.
*/
export type ArcjetAdapterContext = Record<string, unknown>;

/**
* @property {string} ip - The IP address of the client.
* @property {string} method - The HTTP method of the request.
Expand Down Expand Up @@ -1064,10 +1072,14 @@ export interface Arcjet<Props extends PlainObject> {
* Make a decision about how to handle a request. This will analyze the
* request locally where possible and call the Arcjet decision API.
*
* @param {ArcjetAdapterContext} ctx - Additional context for this function call.
* @param {ArcjetRequest} request - Details about the {@link ArcjetRequest} that Arcjet needs to make a decision.
* @returns An {@link ArcjetDecision} indicating Arcjet's decision about the request.
*/
protect(request: ArcjetRequest<Props>): Promise<ArcjetDecision>;
protect(
ctx: ArcjetAdapterContext,
request: ArcjetRequest<Props>,
): Promise<ArcjetDecision>;

/**
* Augments the client with another rule. Useful for varying rules based on
Expand Down Expand Up @@ -1112,6 +1124,7 @@ export default function arcjet<

async function protect<Props extends PlainObject>(
rules: ArcjetRule[],
ctx: ArcjetAdapterContext,
request: ArcjetRequest<Props>,
) {
// This goes against the type definition above, but users might call
Expand Down Expand Up @@ -1149,7 +1162,7 @@ export default function arcjet<
logger.debug("fingerprint (%s): %s", runtime(), fingerprint);
logger.timeEnd("fingerprint");

const context: ArcjetContext = { key, fingerprint };
const context: ArcjetContext = { key, ...ctx, fingerprint };

if (rules.length < 1) {
// TODO(#607): Error if no rules configured after deprecation period
Expand Down Expand Up @@ -1372,9 +1385,10 @@ export default function arcjet<
return withRule(rule);
},
async protect(
ctx: ArcjetContext,
request: ArcjetRequest<ExtraProps<typeof rules>>,
): Promise<ArcjetDecision> {
return protect(rules, request);
return protect(rules, ctx, request);
},
});
}
Expand All @@ -1387,9 +1401,10 @@ export default function arcjet<
return withRule(rule);
},
async protect(
ctx: ArcjetContext,
request: ArcjetRequest<ExtraProps<typeof rootRules>>,
): Promise<ArcjetDecision> {
return protect(rootRules, request);
return protect(rootRules, ctx, request);
},
});
}
31 changes: 17 additions & 14 deletions arcjet/test/index.edge.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,20 +69,23 @@ describe("Arcjet: Env = Edge runtime", () => {

const aj2 = aj.withRule(foobarbaz());

const decision = await aj2.protect({
abc: 123,
requested: 1,
email: "",
ip: "",
method: "",
protocol: "",
host: "",
path: "",
headers: new Headers(),
extra: {},
userId: "user123",
foobar: 123,
});
const decision = await aj2.protect(
{},
{
abc: 123,
requested: 1,
email: "",
ip: "",
method: "",
protocol: "",
host: "",
path: "",
headers: new Headers(),
extra: {},
userId: "user123",
foobar: 123,
},
);

expect(decision.isErrored()).toBe(false);
});
Expand Down
Loading

0 comments on commit b0f6ed0

Please sign in to comment.