@arcjet/redact 1.0.0-beta.10 → 1.0.0-beta.11

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 +64 -8
  2. package/index.js +43 -36
  3. package/package.json +7 -8
package/index.d.ts CHANGED
@@ -1,12 +1,68 @@
1
- export type ArcjetSensitiveInfoType = "email" | "phone-number" | "ip-address" | "credit-card-number";
2
- type DetectSensitiveInfoEntities<T> = (tokens: string[]) => Array<ArcjetSensitiveInfoType | T | undefined>;
3
- type ValidEntities<Detect> = Array<undefined extends Detect ? ArcjetSensitiveInfoType : Detect extends DetectSensitiveInfoEntities<infer CustomEntities> ? ArcjetSensitiveInfoType | CustomEntities : never>;
4
- export type RedactOptions<Detect> = {
5
- entities?: ValidEntities<Detect>;
1
+ import type { SensitiveInfoEntity } from "@arcjet/redact-wasm";
2
+ /**
3
+ * Types of standard sensitive information that can be detected.
4
+ */
5
+ export type ArcjetSensitiveInfoType = Exclude<SensitiveInfoEntity["tag"], "custom">;
6
+ /**
7
+ * Options for the `redact` function.
8
+ *
9
+ * @template DetectedEntities
10
+ * Custom entity names that are returned from `detect` and optionally listed in `entities`.
11
+ * @template ListedEntities
12
+ * Entity names that can be listed in the `entities` option.
13
+ */
14
+ export type RedactOptions<DetectedEntities extends string | undefined = undefined, ListedEntities extends ArcjetSensitiveInfoType | Exclude<DetectedEntities, undefined> = ArcjetSensitiveInfoType | Exclude<DetectedEntities, undefined>> = {
15
+ /**
16
+ * Entities to redact.
17
+ */
18
+ entities?: ReadonlyArray<ListedEntities | Exclude<DetectedEntities, undefined>>;
19
+ /**
20
+ * Size of tokens to consider.
21
+ */
6
22
  contextWindowSize?: number;
7
- detect?: Detect;
8
- replace?: (entity: ValidEntities<Detect>[number], plaintext: string) => string | undefined;
23
+ /**
24
+ * Custom detection function to identify sensitive information.
25
+ *
26
+ * @template DetectedEntities
27
+ * Custom entity names that are returned from `detect` and optionally listed in `entities`.
28
+ * @template ListedEntities
29
+ * Entity names that can be listed in the `entities` option.
30
+ * @param tokens
31
+ * Tokens.
32
+ * @returns
33
+ * List of entities (or undefined).
34
+ */
35
+ detect?: (tokens: string[]) => ReadonlyArray<DetectedEntities>;
36
+ /**
37
+ * Custom replace function to redact sensitive information.
38
+ *
39
+ * @template DetectedEntities
40
+ * Custom entity names that are returned from `detect` and optionally listed in `entities`.
41
+ * @template ListedEntities
42
+ * Entity names that can be listed in the `entities` option.
43
+ * @param entity
44
+ * Entity to redact.
45
+ * @param plaintext
46
+ * The plaintext string to redact.
47
+ * @returns
48
+ * Redacted string or nothing.
49
+ */
50
+ replace?: (entity: ListedEntities, plaintext: string) => string | undefined;
9
51
  };
10
52
  type Unredact = (input: string) => string;
11
- export declare function redact<const Detect extends DetectSensitiveInfoEntities<CustomEntities> | undefined, const CustomEntities extends string>(candidate: string, options?: RedactOptions<Detect>): Promise<[string, Unredact]>;
53
+ /**
54
+ * Redact sensitive info.
55
+ *
56
+ * @template DetectedEntities
57
+ * Custom entity names that are returned from `detect` and optionally listed in `entities`.
58
+ * @template ListedEntities
59
+ * Entity names that can be listed in the `entities` option.
60
+ * @param candidate
61
+ * Value to redact.
62
+ * @param options
63
+ * Configuration.
64
+ * @returns
65
+ * Promise to a tuple with the redacted string and a function to unredact it.
66
+ */
67
+ export declare function redact<const DetectedEntities extends string | undefined = undefined, const ListedEntities extends ArcjetSensitiveInfoType | Exclude<DetectedEntities, undefined> = ArcjetSensitiveInfoType | Exclude<DetectedEntities, undefined>>(candidate: string, options?: RedactOptions<DetectedEntities, ListedEntities>): Promise<[string, Unredact]>;
12
68
  export {};
package/index.js CHANGED
@@ -4,17 +4,11 @@ function userEntitiesToWasm(entity) {
4
4
  if (typeof entity !== "string") {
5
5
  throw new Error("redaction entities must be strings");
6
6
  }
7
- if (entity === "email") {
8
- return { tag: "email" };
9
- }
10
- if (entity === "phone-number") {
11
- return { tag: "phone-number" };
12
- }
13
- if (entity === "ip-address") {
14
- return { tag: "ip-address" };
15
- }
16
- if (entity === "credit-card-number") {
17
- return { tag: "credit-card-number" };
7
+ if (entity === "credit-card-number" ||
8
+ entity === "email" ||
9
+ entity === "ip-address" ||
10
+ entity === "phone-number") {
11
+ return { tag: entity };
18
12
  }
19
13
  return {
20
14
  tag: "custom",
@@ -22,17 +16,11 @@ function userEntitiesToWasm(entity) {
22
16
  };
23
17
  }
24
18
  function wasmEntitiesToString(entity) {
25
- if (entity.tag === "email") {
26
- return "email";
27
- }
28
- if (entity.tag === "ip-address") {
29
- return "ip-address";
30
- }
31
- if (entity.tag === "credit-card-number") {
32
- return "credit-card-number";
33
- }
34
- if (entity.tag === "phone-number") {
35
- return "phone-number";
19
+ if (entity.tag === "credit-card-number" ||
20
+ entity.tag === "email" ||
21
+ entity.tag === "ip-address" ||
22
+ entity.tag === "phone-number") {
23
+ return entity.tag;
36
24
  }
37
25
  return entity.val;
38
26
  }
@@ -50,26 +38,31 @@ function noOpReplace(_input, _plaintext) {
50
38
  }
51
39
  function getWasmOptions(options) {
52
40
  if (typeof options === "object" && options !== null) {
53
- if (typeof options.entities !== "undefined") {
54
- if (Array.isArray(options.entities)) {
55
- if (options.entities.length < 1) {
56
- throw new Error("no entities configured for redaction");
57
- }
58
- }
59
- else {
41
+ const entities = options.entities;
42
+ if (entities !== undefined) {
43
+ if (!Array.isArray(entities)) {
60
44
  throw new Error("entities must be an array");
61
45
  }
46
+ if (entities.length < 1) {
47
+ throw new Error("no entities configured for redaction");
48
+ }
62
49
  }
63
- return {
64
- entities: options.entities?.map(userEntitiesToWasm),
65
- contextWindowSize: options.contextWindowSize || 1,
66
- skipCustomDetect: typeof options.detect !== "function",
67
- skipCustomRedact: typeof options.replace !== "function",
68
- };
50
+ // `entities` is an optional field but not allowed to be `undefined`.
51
+ return entities
52
+ ? {
53
+ entities: entities.map(userEntitiesToWasm),
54
+ contextWindowSize: options.contextWindowSize || 1,
55
+ skipCustomDetect: typeof options.detect !== "function",
56
+ skipCustomRedact: typeof options.replace !== "function",
57
+ }
58
+ : {
59
+ contextWindowSize: options.contextWindowSize || 1,
60
+ skipCustomDetect: typeof options.detect !== "function",
61
+ skipCustomRedact: typeof options.replace !== "function",
62
+ };
69
63
  }
70
64
  else {
71
65
  return {
72
- entities: undefined,
73
66
  contextWindowSize: 1,
74
67
  skipCustomDetect: true,
75
68
  skipCustomRedact: true,
@@ -109,6 +102,20 @@ async function callRedactWasm(candidate, options) {
109
102
  throw new Error("redact failed to run because Wasm is not supported in this environment");
110
103
  }
111
104
  }
105
+ /**
106
+ * Redact sensitive info.
107
+ *
108
+ * @template DetectedEntities
109
+ * Custom entity names that are returned from `detect` and optionally listed in `entities`.
110
+ * @template ListedEntities
111
+ * Entity names that can be listed in the `entities` option.
112
+ * @param candidate
113
+ * Value to redact.
114
+ * @param options
115
+ * Configuration.
116
+ * @returns
117
+ * Promise to a tuple with the redacted string and a function to unredact it.
118
+ */
112
119
  async function redact(candidate, options) {
113
120
  const redactions = await callRedactWasm(candidate, options);
114
121
  // Need to apply the redactions in reverse order so that the offsets aren't changed
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@arcjet/redact",
3
- "version": "1.0.0-beta.10",
3
+ "version": "1.0.0-beta.11",
4
4
  "description": "Arcjet sensitive information redaction library",
5
5
  "keywords": [
6
6
  "arcjet",
@@ -43,15 +43,14 @@
43
43
  "test": "npm run build && npm run lint && npm run test-coverage"
44
44
  },
45
45
  "dependencies": {
46
- "@arcjet/redact-wasm": "1.0.0-beta.10"
46
+ "@arcjet/redact-wasm": "1.0.0-beta.11"
47
47
  },
48
48
  "devDependencies": {
49
- "@arcjet/eslint-config": "1.0.0-beta.10",
50
- "@arcjet/rollup-config": "1.0.0-beta.10",
51
- "@arcjet/tsconfig": "1.0.0-beta.10",
52
- "@rollup/wasm-node": "4.46.2",
53
- "@types/node": "18.18.0",
54
- "eslint": "9.32.0",
49
+ "@arcjet/eslint-config": "1.0.0-beta.11",
50
+ "@arcjet/rollup-config": "1.0.0-beta.11",
51
+ "@rollup/wasm-node": "4.50.0",
52
+ "@types/node": "24.3.0",
53
+ "eslint": "9.34.0",
55
54
  "typescript": "5.9.2"
56
55
  },
57
56
  "publishConfig": {