Skip to content

Commit

Permalink
feat: Separate @arcjet/headers package from core
Browse files Browse the repository at this point in the history
  • Loading branch information
blaine-arcjet committed May 24, 2024
1 parent 8b1c811 commit 245fefb
Show file tree
Hide file tree
Showing 25 changed files with 751 additions and 116 deletions.
1 change: 1 addition & 0 deletions .github/.release-please-manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"decorate": "1.0.0-alpha.13",
"duration": "1.0.0-alpha.13",
"eslint-config": "1.0.0-alpha.13",
"headers": "1.0.0-alpha.13",
"ip": "1.0.0-alpha.13",
"logger": "1.0.0-alpha.13",
"protocol": "1.0.0-alpha.13",
Expand Down
5 changes: 5 additions & 0 deletions .github/release-please-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@
"component": "@arcjet/eslint-config",
"skip-github-release": true
},
"headers": {
"component": "@arcjet/headers",
"skip-github-release": true
},
"ip": {
"component": "@arcjet/ip",
"skip-github-release": true
Expand Down Expand Up @@ -105,6 +109,7 @@
"@arjcet/decorate",
"@arjcet/duration",
"@arcjet/eslint-config",
"@arcjet/headers",
"@arcjet/ip",
"@arcjet/logger",
"@arcjet/protocol",
Expand Down
2 changes: 1 addition & 1 deletion arcjet-bun/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import core, {
ArcjetOptions,
Primitive,
Product,
ArcjetHeaders,
Runtime,
ArcjetRequest,
ExtraProps,
Expand All @@ -16,6 +15,7 @@ import core, {
Arcjet,
} from "arcjet";
import findIP from "@arcjet/ip";
import ArcjetHeaders from "@arcjet/headers";
import type { Server } from "bun";

// Re-export all named exports from the generic SDK
Expand Down
1 change: 1 addition & 0 deletions arcjet-bun/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"test": "NODE_OPTIONS=--experimental-vm-modules jest --passWithNoTests"
},
"dependencies": {
"@arcjet/headers": "1.0.0-alpha.13",
"@arcjet/ip": "1.0.0-alpha.13",
"@connectrpc/connect-node": "1.4.0",
"arcjet": "1.0.0-alpha.13"
Expand Down
2 changes: 1 addition & 1 deletion arcjet-next/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import arcjet, {
ArcjetOptions,
Primitive,
Product,
ArcjetHeaders,
Runtime,
ArcjetRequest,
ExtraProps,
Expand All @@ -23,6 +22,7 @@ import arcjet, {
Arcjet,
} from "arcjet";
import findIP from "@arcjet/ip";
import ArcjetHeaders from "@arcjet/headers";

// Re-export all named exports from the generic SDK
export * from "arcjet";
Expand Down
1 change: 1 addition & 0 deletions arcjet-next/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"test": "NODE_OPTIONS=--experimental-vm-modules jest --passWithNoTests"
},
"dependencies": {
"@arcjet/headers": "1.0.0-alpha.13",
"@arcjet/ip": "1.0.0-alpha.13",
"@connectrpc/connect-web": "1.4.0",
"arcjet": "1.0.0-alpha.13"
Expand Down
2 changes: 1 addition & 1 deletion arcjet-node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import core, {
ArcjetOptions,
Primitive,
Product,
ArcjetHeaders,
Runtime,
ArcjetRequest,
ExtraProps,
Expand All @@ -15,6 +14,7 @@ import core, {
Arcjet,
} from "arcjet";
import findIP from "@arcjet/ip";
import ArcjetHeaders from "@arcjet/headers";

// Re-export all named exports from the generic SDK
export * from "arcjet";
Expand Down
1 change: 1 addition & 0 deletions arcjet-node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"test": "NODE_OPTIONS=--experimental-vm-modules jest --passWithNoTests"
},
"dependencies": {
"@arcjet/headers": "1.0.0-alpha.13",
"@arcjet/ip": "1.0.0-alpha.13",
"@connectrpc/connect-node": "1.4.0",
"arcjet": "1.0.0-alpha.13"
Expand Down
2 changes: 1 addition & 1 deletion arcjet-sveltekit/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import core, {
ArcjetOptions,
Primitive,
Product,
ArcjetHeaders,
Runtime,
ArcjetRequest,
ExtraProps,
Expand All @@ -16,6 +15,7 @@ import core, {
Arcjet,
} from "arcjet";
import findIP from "@arcjet/ip";
import ArcjetHeaders from "@arcjet/headers";

// Re-export all named exports from the generic SDK
export * from "arcjet";
Expand Down
1 change: 1 addition & 0 deletions arcjet-sveltekit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"test": "NODE_OPTIONS=--experimental-vm-modules jest --passWithNoTests"
},
"dependencies": {
"@arcjet/headers": "1.0.0-alpha.13",
"@arcjet/ip": "1.0.0-alpha.13",
"@connectrpc/connect-node": "1.4.0",
"@connectrpc/connect-web": "1.4.0",
Expand Down
64 changes: 1 addition & 63 deletions arcjet/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import {
import * as analyze from "@arcjet/analyze";
import * as duration from "@arcjet/duration";
import logger from "@arcjet/logger";
import ArcjetHeaders from "@arcjet/headers";

export * from "@arcjet/protocol";

Expand All @@ -49,10 +50,6 @@ function assert(condition: boolean, msg: string) {
}
}

function isIterable(val: any): val is Iterable<any> {
return typeof val?.[Symbol.iterator] === "function";
}

function nowInSeconds(): number {
return Math.floor(Date.now() / 1000);
}
Expand Down Expand Up @@ -557,65 +554,6 @@ export type EmailOptions = {
allowDomainLiteral?: boolean;
};

export class ArcjetHeaders extends Headers {
constructor(
init?: HeadersInit | Record<string, string | string[] | undefined>,
) {
super();
if (typeof init !== "undefined") {
if (isIterable(init)) {
for (const [key, value] of init) {
this.append(key, value);
}
} else {
for (const [key, value] of Object.entries(
init as Record<string, string | string[] | undefined>,
)) {
if (typeof value === "undefined") {
continue;
}

if (Array.isArray(value)) {
for (const singleValue of value) {
this.append(key, singleValue);
}
} else {
this.append(key, value);
}
}
}
}
}

/**
* Append a key and value to the headers, while filtering any key named
* `cookie`.
*
* [MDN Reference](https://developer.mozilla.org/docs/Web/API/Headers/append)
*
* @param key The key to append in the headers
* @param value The value to append for the key in the headers
*/
append(key: string, value: string): void {
if (key.toLowerCase() !== "cookie") {
super.append(key, value);
}
}
/**
* Set a key and value in the headers, but filtering any key named `cookie`.
*
* [MDN Reference](https://developer.mozilla.org/docs/Web/API/Headers/set)
*
* @param key The key to set in the headers
* @param value The value to set for the key in the headers
*/
set(key: string, value: string): void {
if (key.toLowerCase() !== "cookie") {
super.set(key, value);
}
}
}

const Priority = {
Shield: 1,
RateLimit: 2,
Expand Down
1 change: 1 addition & 0 deletions arcjet/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"dependencies": {
"@arcjet/analyze": "1.0.0-alpha.13",
"@arcjet/duration": "1.0.0-alpha.13",
"@arcjet/headers": "1.0.0-alpha.13",
"@arcjet/logger": "1.0.0-alpha.13",
"@arcjet/protocol": "1.0.0-alpha.13"
},
Expand Down
49 changes: 0 additions & 49 deletions arcjet/test/index.node.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import arcjet, {
rateLimit,
ArcjetRule,
defaultBaseUrl,
ArcjetHeaders,
Runtime,
validateEmail,
protectSignup,
Expand Down Expand Up @@ -1223,54 +1222,6 @@ describe("createRemoteClient", () => {
});
});

describe("ArcjetHeaders", () => {
test("can be constructed no initializer", () => {
const headers = new ArcjetHeaders();
expect(headers).toBeInstanceOf(ArcjetHeaders);
expect(headers).toBeInstanceOf(Headers);
});

test("can be constructed with a Headers instance", () => {
const init = new Headers();
init.set("foobar", "baz");
const headers = new ArcjetHeaders(init);
expect(headers.get("foobar")).toEqual("baz");
});

test("can be constructed with an ArcjetHeaders instance", () => {
const init = new ArcjetHeaders();
init.set("foobar", "baz");
const headers = new ArcjetHeaders(init);
expect(headers.get("foobar")).toEqual("baz");
});

test("can be constructed with an array of tuples", () => {
const headers = new ArcjetHeaders([["foobar", "baz"]]);
expect(headers.get("foobar")).toEqual("baz");
});

test("can be constructed with an object", () => {
const headers = new ArcjetHeaders({
foobar: "baz",
});
expect(headers.get("foobar")).toEqual("baz");
});

test("filters undefined values in an object", () => {
const headers = new ArcjetHeaders({
foobar: undefined,
});
expect(headers.has("foobar")).toEqual(false);
});

test("combines array values in an object", () => {
const headers = new ArcjetHeaders({
foo: ["bar", "baz"],
});
expect(headers.get("foo")).toEqual("bar, baz");
});
});

describe("ArcjetDecision", () => {
test("will default the `id` property if not specified", () => {
const decision = new ArcjetAllowDecision({
Expand Down
6 changes: 6 additions & 0 deletions headers/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/.turbo/
/coverage/
/node_modules/
*.d.ts
*.js
!*.config.js
4 changes: 4 additions & 0 deletions headers/.eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
root: true,
extends: ["@arcjet/eslint-config"],
};
Loading

0 comments on commit 245fefb

Please sign in to comment.