@anonymask/core 0.4.3 β†’ 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 (2) hide show
  1. package/README.md +306 -47
  2. package/package.json +6 -6
package/README.md CHANGED
@@ -1,87 +1,346 @@
1
- # `@napi-rs/package-template`
1
+ # @anonymask/core
2
2
 
3
- ![https://github.com/napi-rs/package-template/actions](https://github.com/napi-rs/package-template/workflows/CI/badge.svg)
3
+ ![CI](https://github.com/gokul-viswanathan/anonymask/workflows/CI/badge.svg)
4
+ ![NPM Version](https://img.shields.io/npm/v/@anonymask/core)
5
+ ![License](https://img.shields.io/npm/l/@anonymask/core)
4
6
 
5
- > Template project for writing node packages with napi-rs.
7
+ > Secure anonymization/de-anonymization library for protecting Personally Identifiable Information (PII) in Node.js applications. Built with Rust for maximum performance.
6
8
 
7
- # Usage
9
+ ## ✨ Features
8
10
 
9
- 1. Click **Use this template**.
10
- 2. **Clone** your project.
11
- 3. Run `yarn install` to install dependencies.
12
- 4. Run `yarn napi rename -n [@your-scope/package-name] -b [binary-name]` command under the project folder to rename your package.
11
+ - **πŸš€ Blazing Fast**: Rust-powered core with < 5ms processing time
12
+ - **πŸ” Comprehensive Detection**: EMAIL, PHONE, SSN, CREDIT_CARD, IP_ADDRESS, URL
13
+ - **πŸ”’ Secure Placeholders**: Deterministic UUID-based anonymization
14
+ - **πŸ›‘οΈ Type-Safe**: Full TypeScript support with detailed definitions
15
+ - **⚑ Zero Dependencies**: No external runtime dependencies
16
+ - **🧡 Thread-Safe**: Safe for concurrent use
13
17
 
14
- ## Install this test package
18
+ ## πŸ“¦ Installation
15
19
 
16
20
  ```bash
17
- yarn add @napi-rs/package-template
21
+ npm install @anonymask/core
18
22
  ```
19
23
 
20
- ## Ability
24
+ ## πŸš€ Quick Start
21
25
 
22
- ### Build
26
+ ```javascript
27
+ const { Anonymizer } = require("@anonymask/core");
23
28
 
24
- After `yarn build/npm run build` command, you can see `package-template.[darwin|win32|linux].node` file in project root. This is the native addon built from [lib.rs](./src/lib.rs).
29
+ // Initialize with desired entity types
30
+ const anonymizer = new Anonymizer(["email", "phone", "ssn"]);
25
31
 
26
- ### Test
32
+ // Anonymize text
33
+ const text = "Contact john@email.com or call 555-123-4567. SSN: 123-45-6789";
34
+ const result = anonymizer.anonymize(text);
27
35
 
28
- With [ava](https://github.com/avajs/ava), run `yarn test/npm run test` to testing native addon. You can also switch to another testing framework if you want.
36
+ console.log(result.anonymizedText);
37
+ // "Contact EMAIL_xxx or call PHONE_xxx. SSN: SSN_xxx"
29
38
 
30
- ### CI
39
+ console.log(result.mapping);
40
+ // { EMAIL_xxx: 'john@email.com', PHONE_xxx: '555-123-4567', SSN_xxx: '123-45-6789' }
31
41
 
32
- With GitHub Actions, each commit and pull request will be built and tested automatically in [`node@20`, `@node22`] x [`macOS`, `Linux`, `Windows`] matrix. You will never be afraid of the native addon broken in these platforms.
42
+ console.log(result.entities);
43
+ // [
44
+ // { entity_type: 'email', value: 'john@email.com', start: 8, end: 22 },
45
+ // { entity_type: 'phone', value: '555-123-4567', start: 31, end: 43 },
46
+ // { entity_type: 'ssn', value: '123-45-6789', start: 50, end: 60 }
47
+ // ]
33
48
 
34
- ### Release
49
+ // Deanonymize back to original
50
+ const original = anonymizer.deanonymize(result.anonymized_text, result.mapping);
51
+ console.log(original);
52
+ // "Contact john@email.com or call 555-123-4567. SSN: 123-45-6789"
53
+ ```
35
54
 
36
- Release native package is very difficult in old days. Native packages may ask developers who use it to install `build toolchain` like `gcc/llvm`, `node-gyp` or something more.
55
+ ## 🎯 Supported Entity Types
37
56
 
38
- With `GitHub actions`, we can easily prebuild a `binary` for major platforms. And with `N-API`, we should never be afraid of **ABI Compatible**.
57
+ | Type | Description | Examples |
58
+ | ------------- | ----------------------- | --------------------------------------------------- |
59
+ | `email` | Email addresses | `user@domain.com`, `john.doe@company.co.uk` |
60
+ | `phone` | Phone numbers | `555-123-4567`, `555-123`, `(555) 123-4567`, `555.123.4567` |
61
+ | `ssn` | Social Security Numbers | `123-45-6789`, `123456789` |
62
+ | `credit_card` | Credit card numbers | `1234-5678-9012-3456`, `1234567890123456` |
63
+ | `ip_address` | IP addresses | `192.168.1.1`, `2001:0db8:85a3::8a2e:0370:7334` |
64
+ | `url` | URLs | `https://example.com`, `http://sub.domain.org/path` |
39
65
 
40
- The other problem is how to deliver prebuild `binary` to users. Downloading it in `postinstall` script is a common way that most packages do it right now. The problem with this solution is it introduced many other packages to download binary that has not been used by `runtime codes`. The other problem is some users may not easily download the binary from `GitHub/CDN` if they are behind a private network (But in most cases, they have a private NPM mirror).
66
+ ## πŸ“š API Reference
41
67
 
42
- In this package, we choose a better way to solve this problem. We release different `npm packages` for different platforms. And add it to `optionalDependencies` before releasing the `Major` package to npm.
68
+ ### Constructor
43
69
 
44
- `NPM` will choose which native package should download from `registry` automatically. You can see [npm](./npm) dir for details. And you can also run `yarn add @napi-rs/package-template` to see how it works.
70
+ ```javascript
71
+ const anonymizer = new Anonymizer(entityTypes: string[])
72
+ ```
45
73
 
46
- ## Develop requirements
74
+ - `entityTypes`: Array of entity types to detect (see supported types above)
47
75
 
48
- - Install the latest `Rust`
49
- - Install `Node.js@10+` which fully supported `Node-API`
50
- - Install `yarn@1.x`
76
+ ### Methods
51
77
 
52
- ## Test in local
78
+ #### `anonymize(text: string) => AnonymizationResult`
53
79
 
54
- - yarn
55
- - yarn build
56
- - yarn test
80
+ Anonymizes the input text using automatic detection and returns detailed result.
57
81
 
58
- And you will see:
82
+ **Returns:**
59
83
 
60
- ```bash
61
- $ ava --verbose
84
+ ```typescript
85
+ interface AnonymizationResult {
86
+ anonymizedText: string; // Text with PII replaced by placeholders
87
+ mapping: Record<string, string>; // Placeholder -> original value mapping
88
+ entities: Entity[]; // Array of detected entities with metadata
89
+ }
90
+
91
+ interface Entity {
92
+ entity_type: string; // Type of entity (email, phone, etc.)
93
+ value: string; // Original detected value
94
+ start: number; // Start position in original text
95
+ end: number; // End position in original text
96
+ }
97
+ ```
98
+
99
+ #### `anonymizeWithCustom(text: string, customEntities?: Record<string, string[]>) => AnonymizationResult`
100
+
101
+ Anonymizes the input text using both automatic detection and custom entities.
102
+
103
+ **Parameters:**
104
+ - `text`: The input text to anonymize
105
+ - `customEntities`: Optional map of entity types to arrays of custom values to anonymize
106
+
107
+ **Example:**
108
+ ```javascript
109
+ const customEntities = {
110
+ email: ["secret@company.com", "admin@internal.org"],
111
+ phone: ["555-999-0000"]
112
+ };
113
+
114
+ const result = anonymizer.anonymizeWithCustom(text, customEntities);
115
+ ```
116
+
117
+ #### `deanonymize(text: string, mapping: Record<string, string>) => string`
118
+
119
+ Restores original text using the provided mapping.
120
+
121
+ ## πŸ’‘ Use Cases
122
+
123
+ ### Express Middleware
124
+
125
+ ```javascript
126
+ const express = require("express");
127
+ const { Anonymizer } = require("@anonymask/core");
128
+
129
+ const app = express();
130
+ const anonymizer = new Anonymizer(["email", "phone", "ssn"]);
131
+
132
+ // Middleware to anonymize request bodies
133
+ app.use(express.json());
134
+ app.use((req, res, next) => {
135
+ if (req.body && req.body.text) {
136
+ const result = anonymizer.anonymize(req.body.text);
137
+ req.body.anonymized_text = result.anonymized_text;
138
+ req.body.pii_mapping = result.mapping;
139
+ }
140
+ next();
141
+ });
142
+
143
+ app.post("/api/chat", (req, res) => {
144
+ // Send req.body.anonymized_text to LLM
145
+ // Store req.body.pii_mapping for deanonymization
146
+ res.json({ message: "Processed securely" });
147
+ });
148
+ ```
149
+
150
+ ### LLM Integration
151
+
152
+ ```javascript
153
+ const { Anonymizer } = require("@anonymask/core");
154
+
155
+ class SecureLLMClient {
156
+ constructor() {
157
+ this.anonymizer = new Anonymizer(["email", "phone", "ssn", "credit_card"]);
158
+ }
159
+
160
+ async processMessage(userMessage, customEntities = null) {
161
+ // Anonymize user input with optional custom entities
162
+ const result = customEntities
163
+ ? this.anonymizer.anonymizeWithCustom(userMessage, customEntities)
164
+ : this.anonymizer.anonymize(userMessage);
165
+
166
+ // Send anonymized message to LLM
167
+ const llmResponse = await this.callLLM(result.anonymized_text);
168
+
169
+ // Deanonymize LLM response
170
+ const safeResponse = this.anonymizer.deanonymize(
171
+ llmResponse,
172
+ result.mapping,
173
+ );
174
+
175
+ return safeResponse;
176
+ }
177
+ }
178
+ ```
179
+
180
+ ### Custom Entity Anonymization
181
+
182
+ ```javascript
183
+ const { Anonymizer } = require("@anonymask/core");
184
+
185
+ // Initialize with basic detection
186
+ const anonymizer = new Anonymizer(["email"]);
187
+
188
+ // Define custom entities to anonymize
189
+ const customEntities = {
190
+ email: ["internal@company.com", "admin@secure.org"],
191
+ phone: ["555-999-0000", "555-888-1111"],
192
+ // You can even specify entity types not in the initial list
193
+ ssn: ["123-45-6789"]
194
+ };
195
+
196
+ const text = "Contact internal@company.com or call 555-999-0000";
197
+ const result = anonymizer.anonymizeWithCustom(text, customEntities);
62
198
 
63
- βœ” sync function from native code
64
- βœ” sleep function from native code (201ms)
65
- ─
199
+ console.log(result.anonymizedText);
200
+ // "Contact EMAIL_xxx or call PHONE_xxx"
66
201
 
67
- 2 tests passed
68
- ✨ Done in 1.12s.
202
+ console.log(result.mapping);
203
+ // { EMAIL_xxx: 'internal@company.com', PHONE_xxx: '555-999-0000' }
69
204
  ```
70
205
 
71
- ## Release package
206
+ ### Batch Processing
207
+
208
+ ```javascript
209
+ const { Anonymizer } = require("@anonymask/core");
210
+ const fs = require("fs").promises;
211
+
212
+ async function processDataset(filePath) {
213
+ const anonymizer = new Anonymizer(["email", "phone", "ssn"]);
214
+ const data = await fs.readFile(filePath, "utf8");
215
+ const records = JSON.parse(data);
216
+
217
+ const processedRecords = records.map((record) => {
218
+ const result = anonymizer.anonymize(record.text);
219
+ return {
220
+ ...record,
221
+ original_text: record.text,
222
+ anonymized_text: result.anonymized_text,
223
+ pii_mapping: result.mapping,
224
+ entities_detected: result.entities.length,
225
+ };
226
+ });
227
+
228
+ await fs.writeFile(
229
+ "processed_data.json",
230
+ JSON.stringify(processedRecords, null, 2),
231
+ );
232
+ return processedRecords;
233
+ }
234
+ ```
235
+
236
+ ## πŸ§ͺ Testing
72
237
 
73
- Ensure you have set your **NPM_TOKEN** in the `GitHub` project setting.
238
+ ```bash
239
+ # Install dependencies
240
+ npm install
74
241
 
75
- In `Settings -> Secrets`, add **NPM_TOKEN** into it.
242
+ # Run tests
243
+ npm test
76
244
 
77
- When you want to release the package:
245
+ # Run tests with coverage
246
+ npm run test:coverage
247
+
248
+ # Build the package
249
+ npm run build
250
+
251
+ # Run benchmarks
252
+ npm run bench
253
+ ```
254
+
255
+ ## πŸ”§ Development
256
+
257
+ ### Building from Source
78
258
 
79
259
  ```bash
80
- npm version [<newversion> | major | minor | patch | premajor | preminor | prepatch | prerelease [--preid=<prerelease-id>] | from-git]
260
+ # Clone the repository
261
+ git clone https://github.com/gokul-viswanathan/anonymask.git
262
+ cd anonymask/anonymask-node
263
+
264
+ # Install dependencies
265
+ npm install
266
+
267
+ # Build the native addon
268
+ npm run build
269
+
270
+ # Run tests
271
+ npm test
272
+ ```
273
+
274
+ ### Project Structure
81
275
 
82
- git push
83
276
  ```
277
+ anonymask-node/
278
+ β”œβ”€β”€ src/
279
+ β”‚ └── lib.rs # Rust NAPI bindings
280
+ β”œβ”€β”€ index.js # JavaScript entry point
281
+ β”œβ”€β”€ index.d.ts # TypeScript definitions
282
+ β”œβ”€β”€ tests/
283
+ β”‚ └── test_anonymask.test.js # Test suite
284
+ β”œβ”€β”€ package.json
285
+ └── README.md
286
+ ```
287
+
288
+ ## πŸ—οΈ Architecture
289
+
290
+ This package uses NAPI-RS to create high-performance Node.js bindings from the Rust core library:
291
+
292
+ ```
293
+ JavaScript/TypeScript β†’ NAPI-RS β†’ Rust Core β†’ Native Performance
294
+ ```
295
+
296
+ The Rust core provides:
297
+
298
+ - **Memory Safety**: No buffer overflows or memory leaks
299
+ - **Performance**: Near-native execution speed
300
+ - **Concurrency**: Thread-safe operations
301
+ - **Reliability**: Robust error handling
302
+
303
+ ## πŸ“Š Performance
304
+
305
+ - **Processing Speed**: < 5ms for typical messages (< 500 words)
306
+ - **Memory Usage**: Minimal footprint with zero-copy operations
307
+ - **Startup Time**: Fast initialization with lazy loading
308
+ - **Concurrency**: Safe for use in multi-threaded environments
309
+
310
+ ## πŸ”’ Security
311
+
312
+ - **Cryptographically Secure**: UUID v4 for unique placeholder generation
313
+ - **Deterministic**: Same input always produces same output
314
+ - **No Data Leakage**: Secure handling of PII throughout the process
315
+ - **Input Validation**: Comprehensive validation and error handling
316
+
317
+ ## πŸ“„ License
318
+
319
+ MIT License - see [LICENSE](../LICENSE) file for details.
320
+
321
+ ## 🀝 Contributing
322
+
323
+ 1. Fork the repository
324
+ 2. Create a feature branch (`git checkout -b feature/amazing-feature`)
325
+ 3. Add tests for new functionality
326
+ 4. Ensure all tests pass (`npm test`)
327
+ 5. Follow the existing code style
328
+ 6. Submit a pull request
329
+
330
+ ## πŸ—ΊοΈ Roadmap
331
+
332
+ - [ ] Streaming API for large texts
333
+ - [ ] Custom entity pattern support
334
+ - [ ] Persistent mapping storage
335
+ - [ ] Performance optimizations
336
+ - [ ] Additional entity types
337
+
338
+ ## πŸ“ž Support
339
+
340
+ - πŸ“– [Documentation](https://github.com/gokul-viswanathan/anonymask#readme)
341
+ - πŸ› [Issue Tracker](https://github.com/gokul-viswanathan/anonymask/issues)
342
+ - πŸ’¬ [Discussions](https://github.com/gokul-viswanathan/anonymask/discussions)
84
343
 
85
- GitHub actions will do the rest job for you.
344
+ ---
86
345
 
87
- > WARN: Don't run `npm publish` manually.
346
+ **Version**: 0.4.5 | **Built with ❀️ using Rust and NAPI-RS**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@anonymask/core",
3
- "version": "0.4.3",
3
+ "version": "1.0.0",
4
4
  "description": "Secure anonymization/de-anonymization library for PII data",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -68,10 +68,10 @@
68
68
  "typescript": "^5.9.2"
69
69
  },
70
70
  "optionalDependencies": {
71
- "@anonymask/core-win32-x64-msvc": "0.4.3",
72
- "@anonymask/core-darwin-x64": "0.4.3",
73
- "@anonymask/core-linux-x64-gnu": "0.4.3",
74
- "@anonymask/core-linux-arm64-gnu": "0.4.3",
75
- "@anonymask/core-darwin-arm64": "0.4.3"
71
+ "@anonymask/core-win32-x64-msvc": "1.0.0",
72
+ "@anonymask/core-darwin-x64": "1.0.0",
73
+ "@anonymask/core-linux-x64-gnu": "1.0.0",
74
+ "@anonymask/core-linux-arm64-gnu": "1.0.0",
75
+ "@anonymask/core-darwin-arm64": "1.0.0"
76
76
  }
77
77
  }