@arcjet/analyze 1.0.0-beta.8 → 1.0.0

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 (4) hide show
  1. package/README.md +52 -19
  2. package/index.d.ts +75 -9
  3. package/index.js +109 -34
  4. package/package.json +24 -22
package/README.md CHANGED
@@ -22,37 +22,70 @@ against common attacks.
22
22
 
23
23
  This is the [Arcjet][arcjet] local analysis engine.
24
24
 
25
- ## Installation
25
+ - [npm package (`@arcjet/analyze`)](https://www.npmjs.com/package/@arcjet/analyze)
26
+ - [GitHub source code (`analyze/` in `arcjet/arcjet-js`)](https://github.com/arcjet/arcjet-js/tree/main/analyze)
26
27
 
27
- ```shell
28
- npm install -S @arcjet/analyze
29
- ```
28
+ ## What is this?
30
29
 
31
- ## Example
30
+ This package provides functionality to analyze requests.
31
+ The work is done in WebAssembly but is called here from JavaScript.
32
+ The functionality is wrapped up into rules in our core package
33
+ ([`arcjet`][github-arcjet-arcjet]),
34
+ in turn exposed from our [SDKs][github-arcjet-sdks] (such as `@arcjet/next`).
32
35
 
33
- ```ts
34
- import { generateFingerprint, isValidEmail } from "@arcjet/analyze";
36
+ The WebAssembly files are in
37
+ [`@arcjet/analyze-wasm`][github-arcjet-analyze-wasm].
38
+ They are separate because we need to change the import structure for each
39
+ runtime that we support in the bindings.
40
+ Separate packages lets us not duplicate code while providing a combined
41
+ higher-level API for calling our core functionality.
42
+
43
+ ## When should I use this?
44
+
45
+ This is an internal Arcjet package not designed for public use.
46
+ See our [_Get started_ guide][arcjet-get-started] for how to use Arcjet in your
47
+ application.
35
48
 
36
- const fingerprint = generateFingerprint("127.0.0.1");
37
- console.log("fingerprint: ", fingerprint);
49
+ ## Install
38
50
 
39
- const valid = isValidEmail("hello@example.com");
40
- console.log("is email valid?", valid);
51
+ This package is ESM only.
52
+ Install with npm in Node.js:
53
+
54
+ ```sh
55
+ npm install @arcjet/analyze
41
56
  ```
42
57
 
43
- ## Implementation
58
+ ## Use
44
59
 
45
- This package uses the Wasm bindings provided by `@arcjet/analyze-wasm` to
46
- call various functions that are exported by our wasm bindings.
60
+ ```js
61
+ import { generateFingerprint, isValidEmail } from "@arcjet/analyze";
47
62
 
48
- We chose to put this logic in a separate package because we need to change the
49
- import structure for each runtime that we support in the wasm bindings. Moving
50
- this to a separate package allows us not to have to duplicate code while providing
51
- a combined higher-level api for calling our core functionality in Wasm.
63
+ const fingerprint = await generateFingerprint(
64
+ { characteristics: [] },
65
+ { ip: "127.0.0.1" },
66
+ );
67
+ console.log(fingerprint);
68
+ // => "fp::2::0d219da6100b99f95cf639b77e088c6df3c096aa5fd61dec5287c5cf94d5e545"
69
+
70
+ const result = await isValidEmail({}, "hello@example.com", {
71
+ tag: "allow-email-validation-config",
72
+ val: {
73
+ allowDomainLiteral: false,
74
+ allow: [],
75
+ requireTopLevelDomain: true,
76
+ },
77
+ });
78
+ console.log(result);
79
+ // => { blocked: [], validity: "valid" }
80
+ ```
52
81
 
53
82
  ## License
54
83
 
55
- Licensed under the [Apache License, Version 2.0][apache-license].
84
+ [Apache License, Version 2.0][apache-license] © [Arcjet Labs, Inc.][arcjet]
56
85
 
57
86
  [arcjet]: https://arcjet.com
87
+ [arcjet-get-started]: https://docs.arcjet.com/get-started
58
88
  [apache-license]: http://www.apache.org/licenses/LICENSE-2.0
89
+ [github-arcjet-analyze-wasm]: https://github.com/arcjet/arcjet-js/tree/main/analyze-wasm
90
+ [github-arcjet-arcjet]: https://github.com/arcjet/arcjet-js/tree/main/arcjet
91
+ [github-arcjet-sdks]: https://github.com/arcjet/arcjet-js#sdks
package/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { BotConfig, BotResult, DetectedSensitiveInfoEntity, DetectSensitiveInfoFunction, EmailValidationConfig, EmailValidationResult, SensitiveInfoEntities, SensitiveInfoEntity, SensitiveInfoResult } from "@arcjet/analyze-wasm";
1
+ import type { BotConfig, BotResult, DetectedSensitiveInfoEntity, DetectSensitiveInfoFunction, EmailValidationConfig, EmailValidationResult, FilterResult, SensitiveInfoEntities, SensitiveInfoEntity, SensitiveInfoResult } from "@arcjet/analyze-wasm";
2
2
  import type { ArcjetLogger } from "@arcjet/protocol";
3
3
  interface AnalyzeContext {
4
4
  log: ArcjetLogger;
@@ -15,15 +15,81 @@ type AnalyzeRequest = {
15
15
  query?: string;
16
16
  extra?: Record<string, string>;
17
17
  };
18
- export { type EmailValidationConfig, type BotConfig, type SensitiveInfoEntity, type DetectedSensitiveInfoEntity, };
18
+ export { type EmailValidationConfig, type BotConfig, type FilterResult, type SensitiveInfoEntity, type DetectedSensitiveInfoEntity, };
19
19
  /**
20
- * Generate a fingerprint for the client. This is used to identify the client
21
- * across multiple requests.
22
- * @param context - The Arcjet Analyze context.
23
- * @param request - The request to fingerprint.
24
- * @returns A SHA-256 string fingerprint.
20
+ * Generate a fingerprint.
21
+ *
22
+ * Fingerprints can be used to identify the client across multiple requests.
23
+ *
24
+ * This considers different things on the `request` based on the passed
25
+ * `context.characteristics`.
26
+ *
27
+ * See [*Fingerprints* on
28
+ * `docs.arcjet.com`](https://docs.arcjet.com/fingerprints/) for more info.
29
+ *
30
+ * @param context
31
+ * Context.
32
+ * @param request
33
+ * Request.
34
+ * @returns
35
+ * Promise for a SHA-256 fingerprint.
25
36
  */
26
37
  export declare function generateFingerprint(context: AnalyzeContext, request: AnalyzeRequest): Promise<string>;
27
- export declare function isValidEmail(context: AnalyzeContext, candidate: string, options: EmailValidationConfig): Promise<EmailValidationResult>;
38
+ /**
39
+ * Check whether an email is valid.
40
+ *
41
+ * @param context
42
+ * Context.
43
+ * @param value
44
+ * Value.
45
+ * @param options
46
+ * Configuration.
47
+ * @returns
48
+ * Promise for a result.
49
+ */
50
+ export declare function isValidEmail(context: AnalyzeContext, value: string, options: EmailValidationConfig): Promise<EmailValidationResult>;
51
+ /**
52
+ * Detect whether a request is by a bot.
53
+ *
54
+ * @param context
55
+ * Context.
56
+ * @param request
57
+ * Request.
58
+ * @param options
59
+ * Configuration.
60
+ * @returns
61
+ * Promise for a result.
62
+ */
28
63
  export declare function detectBot(context: AnalyzeContext, request: AnalyzeRequest, options: BotConfig): Promise<BotResult>;
29
- export declare function detectSensitiveInfo(context: AnalyzeContext, candidate: string, entities: SensitiveInfoEntities, contextWindowSize: number, detect?: DetectSensitiveInfoFunction): Promise<SensitiveInfoResult>;
64
+ /**
65
+ * Detect sensitive info in a value.
66
+ *
67
+ * @param context
68
+ * Context.
69
+ * @param value
70
+ * Value.
71
+ * @param entities
72
+ * Strategy to use for detecting sensitive info;
73
+ * either by denying everything and allowing certain tags or by allowing
74
+ * everything and denying certain tags.
75
+ * @param contextWindowSize
76
+ * Number of tokens to pass to `detect`.
77
+ * @param detect
78
+ * Function to detect sensitive info (optional).
79
+ * @returns
80
+ * Promise for a result.
81
+ */
82
+ export declare function detectSensitiveInfo(context: AnalyzeContext, value: string, entities: SensitiveInfoEntities, contextWindowSize: number, detect?: DetectSensitiveInfoFunction): Promise<SensitiveInfoResult>;
83
+ /**
84
+ * Check if a filter matches a request.
85
+ *
86
+ * @param context
87
+ * Arcjet context.
88
+ * @param request
89
+ * Request.
90
+ * @param expressions
91
+ * Filter expressions.
92
+ * @returns
93
+ * Promise to whether the filter matches the request.
94
+ */
95
+ export declare function matchFilters(context: AnalyzeContext, request: AnalyzeRequest, expressions: ReadonlyArray<string>, allowIfMatch: boolean): Promise<FilterResult>;
package/index.js CHANGED
@@ -38,9 +38,16 @@ function createCoreImports(detect) {
38
38
  return "unknown";
39
39
  },
40
40
  },
41
+ "arcjet:js-req/filter-overrides": {
42
+ ipLookup() {
43
+ return undefined;
44
+ },
45
+ },
46
+ // TODO(@wooorm-arcjet): figure out a test case for this with the default `detect`.
41
47
  "arcjet:js-req/sensitive-information-identifier": {
42
48
  detect,
43
49
  },
50
+ // TODO(@wooorm-arcjet): figure out a test case for this that calls `verify`.
44
51
  "arcjet:js-req/verify-bot": {
45
52
  verify() {
46
53
  return "unverifiable";
@@ -49,11 +56,22 @@ function createCoreImports(detect) {
49
56
  };
50
57
  }
51
58
  /**
52
- * Generate a fingerprint for the client. This is used to identify the client
53
- * across multiple requests.
54
- * @param context - The Arcjet Analyze context.
55
- * @param request - The request to fingerprint.
56
- * @returns A SHA-256 string fingerprint.
59
+ * Generate a fingerprint.
60
+ *
61
+ * Fingerprints can be used to identify the client across multiple requests.
62
+ *
63
+ * This considers different things on the `request` based on the passed
64
+ * `context.characteristics`.
65
+ *
66
+ * See [*Fingerprints* on
67
+ * `docs.arcjet.com`](https://docs.arcjet.com/fingerprints/) for more info.
68
+ *
69
+ * @param context
70
+ * Context.
71
+ * @param request
72
+ * Request.
73
+ * @returns
74
+ * Promise for a SHA-256 fingerprint.
57
75
  */
58
76
  async function generateFingerprint(context, request) {
59
77
  const { log } = context;
@@ -61,62 +79,119 @@ async function generateFingerprint(context, request) {
61
79
  const analyze = await initializeWasm(coreImports);
62
80
  if (typeof analyze !== "undefined") {
63
81
  return analyze.generateFingerprint(JSON.stringify(request), context.characteristics);
82
+ // Ignore the `else` branch as we test in places that have WebAssembly.
83
+ /* node:coverage ignore next 4 */
64
84
  }
65
- else {
66
- log.debug("WebAssembly is not supported in this runtime");
67
- }
85
+ log.debug("WebAssembly is not supported in this runtime");
68
86
  return "";
69
87
  }
70
- async function isValidEmail(context, candidate, options) {
88
+ /**
89
+ * Check whether an email is valid.
90
+ *
91
+ * @param context
92
+ * Context.
93
+ * @param value
94
+ * Value.
95
+ * @param options
96
+ * Configuration.
97
+ * @returns
98
+ * Promise for a result.
99
+ */
100
+ async function isValidEmail(context, value, options) {
71
101
  const { log } = context;
72
102
  const coreImports = createCoreImports();
73
103
  const analyze = await initializeWasm(coreImports);
74
104
  if (typeof analyze !== "undefined") {
75
- return analyze.isValidEmail(candidate, options);
76
- }
77
- else {
78
- log.debug("WebAssembly is not supported in this runtime");
79
- // Skip the local evaluation of the rule if WASM is not available
80
- return {
81
- validity: "valid",
82
- blocked: [],
83
- };
105
+ return analyze.isValidEmail(value, options);
106
+ // Ignore the `else` branch as we test in places that have WebAssembly.
107
+ /* node:coverage ignore next 4 */
84
108
  }
109
+ log.debug("WebAssembly is not supported in this runtime");
110
+ return { blocked: [], validity: "valid" };
85
111
  }
112
+ /**
113
+ * Detect whether a request is by a bot.
114
+ *
115
+ * @param context
116
+ * Context.
117
+ * @param request
118
+ * Request.
119
+ * @param options
120
+ * Configuration.
121
+ * @returns
122
+ * Promise for a result.
123
+ */
86
124
  async function detectBot(context, request, options) {
87
125
  const { log } = context;
88
126
  const coreImports = createCoreImports();
89
127
  const analyze = await initializeWasm(coreImports);
90
128
  if (typeof analyze !== "undefined") {
91
129
  return analyze.detectBot(JSON.stringify(request), options);
130
+ // Ignore the `else` branch as we test in places that have WebAssembly.
131
+ /* node:coverage ignore next 4 */
92
132
  }
93
- else {
94
- log.debug("WebAssembly is not supported in this runtime");
95
- // Skip the local evaluation of the rule if Wasm is not available
96
- return {
97
- allowed: [],
98
- denied: [],
99
- spoofed: false,
100
- verified: false,
101
- };
102
- }
133
+ log.debug("WebAssembly is not supported in this runtime");
134
+ return { allowed: [], denied: [], spoofed: false, verified: false };
103
135
  }
104
- async function detectSensitiveInfo(context, candidate, entities, contextWindowSize, detect) {
136
+ /**
137
+ * Detect sensitive info in a value.
138
+ *
139
+ * @param context
140
+ * Context.
141
+ * @param value
142
+ * Value.
143
+ * @param entities
144
+ * Strategy to use for detecting sensitive info;
145
+ * either by denying everything and allowing certain tags or by allowing
146
+ * everything and denying certain tags.
147
+ * @param contextWindowSize
148
+ * Number of tokens to pass to `detect`.
149
+ * @param detect
150
+ * Function to detect sensitive info (optional).
151
+ * @returns
152
+ * Promise for a result.
153
+ */
154
+ async function detectSensitiveInfo(context, value, entities, contextWindowSize, detect) {
105
155
  const { log } = context;
106
156
  const coreImports = createCoreImports(detect);
107
157
  const analyze = await initializeWasm(coreImports);
108
158
  if (typeof analyze !== "undefined") {
109
159
  const skipCustomDetect = typeof detect !== "function";
110
- return analyze.detectSensitiveInfo(candidate, {
160
+ return analyze.detectSensitiveInfo(value, {
111
161
  entities,
112
162
  contextWindowSize,
113
163
  skipCustomDetect,
114
164
  });
165
+ // Ignore the `else` branch as we test in places that have WebAssembly.
166
+ /* node:coverage ignore next 4 */
115
167
  }
116
- else {
117
- log.debug("WebAssembly is not supported in this runtime");
118
- throw new Error("SENSITIVE_INFO rule failed to run because Wasm is not supported in this environment.");
168
+ log.debug("WebAssembly is not supported in this runtime");
169
+ throw new Error("SENSITIVE_INFO rule failed to run because Wasm is not supported in this environment.");
170
+ }
171
+ /**
172
+ * Check if a filter matches a request.
173
+ *
174
+ * @param context
175
+ * Arcjet context.
176
+ * @param request
177
+ * Request.
178
+ * @param expressions
179
+ * Filter expressions.
180
+ * @returns
181
+ * Promise to whether the filter matches the request.
182
+ */
183
+ async function matchFilters(context, request, expressions, allowIfMatch) {
184
+ const coreImports = createCoreImports();
185
+ const analyze = await initializeWasm(coreImports);
186
+ if (typeof analyze !== "undefined") {
187
+ return analyze.matchFilters(JSON.stringify(request),
188
+ // @ts-expect-error: WebAssembly does not support readonly values.
189
+ expressions, allowIfMatch);
190
+ // Ignore the `else` branch as we test in places that have WebAssembly.
191
+ /* node:coverage ignore next 4 */
119
192
  }
193
+ context.log.debug("WebAssembly is not supported in this runtime");
194
+ throw new Error("FILTER rule failed to run because Wasm is not supported in this environment.");
120
195
  }
121
196
 
122
- export { detectBot, detectSensitiveInfo, generateFingerprint, isValidEmail };
197
+ export { detectBot, detectSensitiveInfo, generateFingerprint, isValidEmail, matchFilters };
package/package.json CHANGED
@@ -1,7 +1,15 @@
1
1
  {
2
2
  "name": "@arcjet/analyze",
3
- "version": "1.0.0-beta.8",
3
+ "version": "1.0.0",
4
4
  "description": "Arcjet local analysis engine",
5
+ "keywords": [
6
+ "analyze",
7
+ "arcjet",
8
+ "attack",
9
+ "limit",
10
+ "protect",
11
+ "verify"
12
+ ],
5
13
  "license": "Apache-2.0",
6
14
  "homepage": "https://arcjet.com",
7
15
  "repository": {
@@ -19,41 +27,35 @@
19
27
  "url": "https://arcjet.com"
20
28
  },
21
29
  "engines": {
22
- "node": ">=18"
30
+ "node": ">=20"
23
31
  },
24
32
  "type": "module",
25
33
  "main": "./index.js",
26
34
  "types": "./index.d.ts",
27
35
  "files": [
28
- "LICENSE",
29
- "README.md",
30
- "_virtual/",
31
- "wasm/",
32
- "*.js",
33
- "*.d.ts",
34
- "!*.config.js"
36
+ "index.d.ts",
37
+ "index.js"
35
38
  ],
36
39
  "scripts": {
37
- "prepublishOnly": "npm run build",
38
40
  "build": "rollup --config rollup.config.js",
39
41
  "lint": "eslint .",
40
- "pretest": "npm run build",
41
- "test": "node --test --experimental-test-coverage"
42
+ "prepublishOnly": "npm run build",
43
+ "test-api": "node --test -- test/*.test.js",
44
+ "test-coverage": "node --experimental-test-coverage --test -- test/*.test.js",
45
+ "test": "npm run build && npm run lint && npm run test-coverage"
42
46
  },
43
47
  "dependencies": {
44
- "@arcjet/analyze-wasm": "1.0.0-beta.8",
45
- "@arcjet/protocol": "1.0.0-beta.8"
48
+ "@arcjet/analyze-wasm": "1.0.0",
49
+ "@arcjet/protocol": "1.0.0"
46
50
  },
47
51
  "devDependencies": {
48
- "@arcjet/eslint-config": "1.0.0-beta.8",
49
- "@arcjet/rollup-config": "1.0.0-beta.8",
50
- "@arcjet/tsconfig": "1.0.0-beta.8",
52
+ "@arcjet/eslint-config": "1.0.0",
53
+ "@arcjet/rollup-config": "1.0.0",
51
54
  "@bytecodealliance/jco": "1.5.0",
52
- "@rollup/wasm-node": "4.41.1",
53
- "@types/node": "18.18.0",
54
- "eslint": "9.27.0",
55
- "expect": "29.7.0",
56
- "typescript": "5.8.3"
55
+ "@rollup/wasm-node": "4.55.1",
56
+ "@types/node": "25.0.8",
57
+ "eslint": "9.39.2",
58
+ "typescript": "5.9.3"
57
59
  },
58
60
  "publishConfig": {
59
61
  "access": "public",