@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.
- package/README.md +260 -47
- package/package.json +6 -6
package/README.md
CHANGED
|
@@ -1,87 +1,300 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @anonymask/core
|
|
2
2
|
|
|
3
|
-

|
|
4
|
+

|
|
5
|
+

|
|
4
6
|
|
|
5
|
-
>
|
|
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
|
-
|
|
9
|
+
## β¨ Features
|
|
8
10
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
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
|
-
##
|
|
18
|
+
## π¦ Installation
|
|
15
19
|
|
|
16
20
|
```bash
|
|
17
|
-
|
|
21
|
+
npm install @anonymask/core
|
|
18
22
|
```
|
|
19
23
|
|
|
20
|
-
##
|
|
24
|
+
## π Quick Start
|
|
21
25
|
|
|
22
|
-
|
|
26
|
+
```javascript
|
|
27
|
+
const { Anonymizer } = require("@anonymask/core");
|
|
23
28
|
|
|
24
|
-
|
|
29
|
+
// Initialize with desired entity types
|
|
30
|
+
const anonymizer = new Anonymizer(["email", "phone", "ssn"]);
|
|
25
31
|
|
|
26
|
-
|
|
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
|
-
|
|
36
|
+
console.log(result.anonymizedText);
|
|
37
|
+
// "Contact EMAIL_xxx or call PHONE_xxx. SSN: SSN_xxx"
|
|
29
38
|
|
|
30
|
-
|
|
39
|
+
console.log(result.mapping);
|
|
40
|
+
// { EMAIL_xxx: 'john@email.com', PHONE_xxx: '555-123-4567', SSN_xxx: '123-45-6789' }
|
|
31
41
|
|
|
32
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
66
|
+
## π API Reference
|
|
39
67
|
|
|
40
|
-
|
|
68
|
+
### Constructor
|
|
41
69
|
|
|
42
|
-
|
|
70
|
+
```javascript
|
|
71
|
+
const anonymizer = new Anonymizer(entityTypes: string[])
|
|
72
|
+
```
|
|
43
73
|
|
|
44
|
-
`
|
|
74
|
+
- `entityTypes`: Array of entity types to detect (see supported types above)
|
|
45
75
|
|
|
46
|
-
|
|
76
|
+
### Methods
|
|
47
77
|
|
|
48
|
-
|
|
49
|
-
- Install `Node.js@10+` which fully supported `Node-API`
|
|
50
|
-
- Install `yarn@1.x`
|
|
78
|
+
#### `anonymize(text: string) => AnonymizationResult`
|
|
51
79
|
|
|
52
|
-
|
|
80
|
+
Anonymizes the input text and returns detailed result.
|
|
53
81
|
|
|
54
|
-
|
|
55
|
-
- yarn build
|
|
56
|
-
- yarn test
|
|
82
|
+
**Returns:**
|
|
57
83
|
|
|
58
|
-
|
|
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
|
-
|
|
61
|
-
|
|
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
|
-
|
|
64
|
-
|
|
65
|
-
|
|
142
|
+
async processMessage(userMessage) {
|
|
143
|
+
// Anonymize user input
|
|
144
|
+
const result = this.anonymizer.anonymize(userMessage);
|
|
66
145
|
|
|
67
|
-
|
|
68
|
-
|
|
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
|
-
|
|
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
|
-
|
|
205
|
+
# Run benchmarks
|
|
206
|
+
npm run bench
|
|
207
|
+
```
|
|
74
208
|
|
|
75
|
-
|
|
209
|
+
## π§ Development
|
|
76
210
|
|
|
77
|
-
|
|
211
|
+
### Building from Source
|
|
78
212
|
|
|
79
213
|
```bash
|
|
80
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
+
"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.
|
|
72
|
-
"@anonymask/core-darwin-x64": "0.4.
|
|
73
|
-
"@anonymask/core-linux-x64-gnu": "0.4.
|
|
74
|
-
"@anonymask/core-linux-arm64-gnu": "0.4.
|
|
75
|
-
"@anonymask/core-darwin-arm64": "0.4.
|
|
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
|
}
|