@anonymask/core 0.4.3 β†’ 0.4.5

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 +260 -47
  2. package/package.json +6 -6
package/README.md CHANGED
@@ -1,87 +1,300 @@
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
+ ```
54
+
55
+ ## 🎯 Supported Entity Types
35
56
 
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.
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-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` |
37
65
 
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**.
66
+ ## πŸ“š API Reference
39
67
 
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).
68
+ ### Constructor
41
69
 
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.
70
+ ```javascript
71
+ const anonymizer = new Anonymizer(entityTypes: string[])
72
+ ```
43
73
 
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.
74
+ - `entityTypes`: Array of entity types to detect (see supported types above)
45
75
 
46
- ## Develop requirements
76
+ ### Methods
47
77
 
48
- - Install the latest `Rust`
49
- - Install `Node.js@10+` which fully supported `Node-API`
50
- - Install `yarn@1.x`
78
+ #### `anonymize(text: string) => AnonymizationResult`
51
79
 
52
- ## Test in local
80
+ Anonymizes the input text and returns detailed result.
53
81
 
54
- - yarn
55
- - yarn build
56
- - yarn test
82
+ **Returns:**
57
83
 
58
- And you will see:
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
+ }
59
90
 
60
- ```bash
61
- $ ava --verbose
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
+ #### `deanonymize(text: string, mapping: Record<string, string>) => string`
100
+
101
+ Restores original text using the provided mapping.
102
+
103
+ ## πŸ’‘ Use Cases
104
+
105
+ ### Express Middleware
106
+
107
+ ```javascript
108
+ const express = require("express");
109
+ const { Anonymizer } = require("@anonymask/core");
110
+
111
+ const app = express();
112
+ const anonymizer = new Anonymizer(["email", "phone", "ssn"]);
113
+
114
+ // Middleware to anonymize request bodies
115
+ app.use(express.json());
116
+ app.use((req, res, next) => {
117
+ if (req.body && req.body.text) {
118
+ const result = anonymizer.anonymize(req.body.text);
119
+ req.body.anonymized_text = result.anonymized_text;
120
+ req.body.pii_mapping = result.mapping;
121
+ }
122
+ next();
123
+ });
124
+
125
+ app.post("/api/chat", (req, res) => {
126
+ // Send req.body.anonymized_text to LLM
127
+ // Store req.body.pii_mapping for deanonymization
128
+ res.json({ message: "Processed securely" });
129
+ });
130
+ ```
131
+
132
+ ### LLM Integration
133
+
134
+ ```javascript
135
+ const { Anonymizer } = require("@anonymask/core");
136
+
137
+ class SecureLLMClient {
138
+ constructor() {
139
+ this.anonymizer = new Anonymizer(["email", "phone", "ssn", "credit_card"]);
140
+ }
62
141
 
63
- βœ” sync function from native code
64
- βœ” sleep function from native code (201ms)
65
- ─
142
+ async processMessage(userMessage) {
143
+ // Anonymize user input
144
+ const result = this.anonymizer.anonymize(userMessage);
66
145
 
67
- 2 tests passed
68
- ✨ Done in 1.12s.
146
+ // Send anonymized message to LLM
147
+ const llmResponse = await this.callLLM(result.anonymized_text);
148
+
149
+ // Deanonymize LLM response
150
+ const safeResponse = this.anonymizer.deanonymize(
151
+ llmResponse,
152
+ result.mapping,
153
+ );
154
+
155
+ return safeResponse;
156
+ }
157
+ }
69
158
  ```
70
159
 
71
- ## Release package
160
+ ### Batch Processing
161
+
162
+ ```javascript
163
+ const { Anonymizer } = require("@anonymask/core");
164
+ const fs = require("fs").promises;
165
+
166
+ async function processDataset(filePath) {
167
+ const anonymizer = new Anonymizer(["email", "phone", "ssn"]);
168
+ const data = await fs.readFile(filePath, "utf8");
169
+ const records = JSON.parse(data);
170
+
171
+ const processedRecords = records.map((record) => {
172
+ const result = anonymizer.anonymize(record.text);
173
+ return {
174
+ ...record,
175
+ original_text: record.text,
176
+ anonymized_text: result.anonymized_text,
177
+ pii_mapping: result.mapping,
178
+ entities_detected: result.entities.length,
179
+ };
180
+ });
181
+
182
+ await fs.writeFile(
183
+ "processed_data.json",
184
+ JSON.stringify(processedRecords, null, 2),
185
+ );
186
+ return processedRecords;
187
+ }
188
+ ```
189
+
190
+ ## πŸ§ͺ Testing
191
+
192
+ ```bash
193
+ # Install dependencies
194
+ npm install
195
+
196
+ # Run tests
197
+ npm test
198
+
199
+ # Run tests with coverage
200
+ npm run test:coverage
201
+
202
+ # Build the package
203
+ npm run build
72
204
 
73
- Ensure you have set your **NPM_TOKEN** in the `GitHub` project setting.
205
+ # Run benchmarks
206
+ npm run bench
207
+ ```
74
208
 
75
- In `Settings -> Secrets`, add **NPM_TOKEN** into it.
209
+ ## πŸ”§ Development
76
210
 
77
- When you want to release the package:
211
+ ### Building from Source
78
212
 
79
213
  ```bash
80
- npm version [<newversion> | major | minor | patch | premajor | preminor | prepatch | prerelease [--preid=<prerelease-id>] | from-git]
214
+ # Clone the repository
215
+ git clone https://github.com/gokul-viswanathan/anonymask.git
216
+ cd anonymask/anonymask-node
217
+
218
+ # Install dependencies
219
+ npm install
81
220
 
82
- git push
221
+ # Build the native addon
222
+ npm run build
223
+
224
+ # Run tests
225
+ npm test
226
+ ```
227
+
228
+ ### Project Structure
229
+
230
+ ```
231
+ anonymask-node/
232
+ β”œβ”€β”€ src/
233
+ β”‚ └── lib.rs # Rust NAPI bindings
234
+ β”œβ”€β”€ index.js # JavaScript entry point
235
+ β”œβ”€β”€ index.d.ts # TypeScript definitions
236
+ β”œβ”€β”€ tests/
237
+ β”‚ └── test_anonymask.test.js # Test suite
238
+ β”œβ”€β”€ package.json
239
+ └── README.md
83
240
  ```
84
241
 
85
- GitHub actions will do the rest job for you.
242
+ ## πŸ—οΈ Architecture
243
+
244
+ This package uses NAPI-RS to create high-performance Node.js bindings from the Rust core library:
245
+
246
+ ```
247
+ JavaScript/TypeScript β†’ NAPI-RS β†’ Rust Core β†’ Native Performance
248
+ ```
249
+
250
+ The Rust core provides:
251
+
252
+ - **Memory Safety**: No buffer overflows or memory leaks
253
+ - **Performance**: Near-native execution speed
254
+ - **Concurrency**: Thread-safe operations
255
+ - **Reliability**: Robust error handling
256
+
257
+ ## πŸ“Š Performance
258
+
259
+ - **Processing Speed**: < 5ms for typical messages (< 500 words)
260
+ - **Memory Usage**: Minimal footprint with zero-copy operations
261
+ - **Startup Time**: Fast initialization with lazy loading
262
+ - **Concurrency**: Safe for use in multi-threaded environments
263
+
264
+ ## πŸ”’ Security
265
+
266
+ - **Cryptographically Secure**: UUID v4 for unique placeholder generation
267
+ - **Deterministic**: Same input always produces same output
268
+ - **No Data Leakage**: Secure handling of PII throughout the process
269
+ - **Input Validation**: Comprehensive validation and error handling
270
+
271
+ ## πŸ“„ License
272
+
273
+ MIT License - see [LICENSE](../LICENSE) file for details.
274
+
275
+ ## 🀝 Contributing
276
+
277
+ 1. Fork the repository
278
+ 2. Create a feature branch (`git checkout -b feature/amazing-feature`)
279
+ 3. Add tests for new functionality
280
+ 4. Ensure all tests pass (`npm test`)
281
+ 5. Follow the existing code style
282
+ 6. Submit a pull request
283
+
284
+ ## πŸ—ΊοΈ Roadmap
285
+
286
+ - [ ] Streaming API for large texts
287
+ - [ ] Custom entity pattern support
288
+ - [ ] Persistent mapping storage
289
+ - [ ] Performance optimizations
290
+ - [ ] Additional entity types
291
+
292
+ ## πŸ“ž Support
293
+
294
+ - πŸ“– [Documentation](https://github.com/gokul-viswanathan/anonymask#readme)
295
+ - πŸ› [Issue Tracker](https://github.com/gokul-viswanathan/anonymask/issues)
296
+ - πŸ’¬ [Discussions](https://github.com/gokul-viswanathan/anonymask/discussions)
297
+
298
+ ---
86
299
 
87
- > WARN: Don't run `npm publish` manually.
300
+ **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": "0.4.5",
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": "0.4.5",
72
+ "@anonymask/core-darwin-x64": "0.4.5",
73
+ "@anonymask/core-linux-x64-gnu": "0.4.5",
74
+ "@anonymask/core-linux-arm64-gnu": "0.4.5",
75
+ "@anonymask/core-darwin-arm64": "0.4.5"
76
76
  }
77
77
  }