@jmlq/logger 0.1.0-alpha.6 → 0.1.0-alpha.7
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 +190 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -103,7 +103,7 @@ src/
|
|
|
103
103
|
export type PiiReplacement = {
|
|
104
104
|
pattern: string;
|
|
105
105
|
replaceWith: string;
|
|
106
|
-
flags?: string;
|
|
106
|
+
flags?: string; // regex (g, i, m, s, u, y)
|
|
107
107
|
};
|
|
108
108
|
|
|
109
109
|
export interface PiiConfig {
|
|
@@ -123,6 +123,7 @@ export const DEFAULT_PII_PATTERNS: PiiReplacement[] = [
|
|
|
123
123
|
flags: "g",
|
|
124
124
|
},
|
|
125
125
|
];
|
|
126
|
+
// Ver abajo explicación flags
|
|
126
127
|
|
|
127
128
|
// patrones “del cliente/proyecto”:
|
|
128
129
|
export const clientPiiPatterns: PiiReplacement[] = [
|
|
@@ -183,6 +184,194 @@ export function buildPiiConfig(
|
|
|
183
184
|
}
|
|
184
185
|
```
|
|
185
186
|
|
|
187
|
+
> - **REGEX FLAGS**
|
|
188
|
+
|
|
189
|
+
> > - `g` (**global**): Encuentra `todas las coincidencias` en el texto, no solo la primera.
|
|
190
|
+
|
|
191
|
+
```ts
|
|
192
|
+
const regex = /\d{2}/g;
|
|
193
|
+
"123456".match(regex); // ["12", "34", "56"]
|
|
194
|
+
|
|
195
|
+
// /\d{2}/ busca pares de dígitos.
|
|
196
|
+
// "12" en índices 0-1
|
|
197
|
+
// "34" en índices 2-3
|
|
198
|
+
// "56" en índices 4-5
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
> > - `i` (**ignore case**): Ignora mayúsculas/minúsculas.
|
|
202
|
+
|
|
203
|
+
```ts
|
|
204
|
+
// Sin "i" (sensible a mayúsculas/minúsculas)
|
|
205
|
+
const regexCaseSensitive = /secret/;
|
|
206
|
+
"SECRET".match(regexCaseSensitive); // null
|
|
207
|
+
"secret".match(regexCaseSensitive); // ["secret"]
|
|
208
|
+
|
|
209
|
+
// Con "i" (ignora mayúsculas/minúsculas)
|
|
210
|
+
const regexIgnoreCase = /secret/i;
|
|
211
|
+
"SECRET".match(regexIgnoreCase); // ["SECRET"]
|
|
212
|
+
"Secret".match(regexIgnoreCase); // ["Secret"]
|
|
213
|
+
"sEcReT".match(regexIgnoreCase); // ["sEcReT"]
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
> > - `m` (**multiline**): Permite que `^` y `$` funcionen en cada línea, no solo al inicio/fin del string completo.
|
|
217
|
+
|
|
218
|
+
```ts
|
|
219
|
+
const texto = `uno
|
|
220
|
+
FOO
|
|
221
|
+
tres`;
|
|
222
|
+
|
|
223
|
+
// Sin "m": ^ solo reconoce el inicio de *todo* el string
|
|
224
|
+
const regexNormal = /^FOO/;
|
|
225
|
+
console.log(texto.match(regexNormal)); // null
|
|
226
|
+
|
|
227
|
+
// Con "m": ^ reconoce también el inicio de cada línea
|
|
228
|
+
const regexMultiline = /^FOO/m;
|
|
229
|
+
console.log(texto.match(regexMultiline)); // ["FOO"]
|
|
230
|
+
```
|
|
231
|
+
|
|
232
|
+
> > - `s` (**dotAll**): Permite que `.` coincida también con saltos de línea (`\n`).
|
|
233
|
+
|
|
234
|
+
```ts
|
|
235
|
+
const texto = "a\nb";
|
|
236
|
+
|
|
237
|
+
// Sin "s": el punto (.) no captura saltos de línea
|
|
238
|
+
const regexNormal = /a.b/;
|
|
239
|
+
texto.match(regexNormal); // null
|
|
240
|
+
|
|
241
|
+
// Con "s": el punto (.) sí captura saltos de línea
|
|
242
|
+
const regexDotAll = /a.b/s;
|
|
243
|
+
texto.match(regexDotAll); // ["a\nb"]
|
|
244
|
+
```
|
|
245
|
+
|
|
246
|
+
> > - `u` (**unicode**): Habilita soporte Unicode completo en regex (ejemplo, emojis o caracteres fuera del BMP).
|
|
247
|
+
|
|
248
|
+
```ts
|
|
249
|
+
const regex = /\u{1F600}/u; // 😀
|
|
250
|
+
"😀".match(regex); // ["😀"]
|
|
251
|
+
```
|
|
252
|
+
|
|
253
|
+
> > - `y` (**sticky**): Solo encuentra coincidencias en la posición exacta del índice actual (lastIndex).
|
|
254
|
+
|
|
255
|
+
```ts
|
|
256
|
+
const regexSticky = /\d{2}/y;
|
|
257
|
+
|
|
258
|
+
regexSticky.lastIndex = 0;
|
|
259
|
+
regexSticky.exec("123456");
|
|
260
|
+
// ["12"]
|
|
261
|
+
|
|
262
|
+
regexSticky.lastIndex = 2;
|
|
263
|
+
regexSticky.exec("123456");
|
|
264
|
+
// ["34"]
|
|
265
|
+
|
|
266
|
+
regexSticky.lastIndex = 4;
|
|
267
|
+
regexSticky.exec("123456");
|
|
268
|
+
// ["56"]
|
|
269
|
+
|
|
270
|
+
regexSticky.lastIndex = 1;
|
|
271
|
+
regexSticky.exec("123456");
|
|
272
|
+
// null (porque en índice 1 hay "2", pero necesita empezar justo ahí y no hay 2 dígitos completos desde esa posición)
|
|
273
|
+
```
|
|
274
|
+
|
|
275
|
+
Ejemplos:
|
|
276
|
+
|
|
277
|
+
```ts
|
|
278
|
+
const text = "Usuario: 12345, Otro: 67890";
|
|
279
|
+
const rules: PiiReplacement[] = [
|
|
280
|
+
{ pattern: "\\d{5}", replaceWith: "[ID]", flags: "g" },
|
|
281
|
+
];
|
|
282
|
+
console.log(redact(text, rules));
|
|
283
|
+
// Usuario: [ID], Otro: [ID]
|
|
284
|
+
|
|
285
|
+
// Donde:
|
|
286
|
+
// \d → significa un dígito (0–9).
|
|
287
|
+
// {5} → significa exactamente 5 repeticiones seguidas.
|
|
288
|
+
```
|
|
289
|
+
|
|
290
|
+
```ts
|
|
291
|
+
const text = "Password=1234; PASSWORD=5678; password=9999";
|
|
292
|
+
const rules: PiiReplacement[] = [
|
|
293
|
+
{ pattern: "password=\\d+", replaceWith: "password=[REDACTED]", flags: "gi" },
|
|
294
|
+
];
|
|
295
|
+
console.log(redact(text, rules));
|
|
296
|
+
// password=[REDACTED]; password=[REDACTED]; password=[REDACTED]
|
|
297
|
+
|
|
298
|
+
// Donde:
|
|
299
|
+
// \d → significa un dígito (0–9).
|
|
300
|
+
// + → uno o más dígitos consecutivos.
|
|
301
|
+
```
|
|
302
|
+
|
|
303
|
+
```ts
|
|
304
|
+
const text = `
|
|
305
|
+
linea1: ok
|
|
306
|
+
secret=12345
|
|
307
|
+
linea3: done
|
|
308
|
+
`;
|
|
309
|
+
|
|
310
|
+
const rules: PiiReplacement[] = [
|
|
311
|
+
{ pattern: "^secret=.*$", replaceWith: "secret=[REDACTED]", flags: "m" },
|
|
312
|
+
];
|
|
313
|
+
console.log(redact(text, rules));
|
|
314
|
+
/*
|
|
315
|
+
linea1: ok
|
|
316
|
+
secret=[REDACTED]
|
|
317
|
+
linea3: done
|
|
318
|
+
*/
|
|
319
|
+
|
|
320
|
+
// Donde
|
|
321
|
+
// . → cualquier carácter (excepto salto de línea, a menos que uses el flag s).
|
|
322
|
+
// * → cero o más repeticiones del carácter anterior (.).
|
|
323
|
+
// $ → final de la línea o final de la cadena (dependiendo si usas flag m).
|
|
324
|
+
```
|
|
325
|
+
|
|
326
|
+
```ts
|
|
327
|
+
const text = "BEGIN\n12345\nEND";
|
|
328
|
+
const rules: PiiReplacement[] = [
|
|
329
|
+
{ pattern: "BEGIN.*END", replaceWith: "[BLOCK REDACTED]", flags: "s" },
|
|
330
|
+
];
|
|
331
|
+
console.log(redact(text, rules));
|
|
332
|
+
// [BLOCK REDACTED]
|
|
333
|
+
|
|
334
|
+
// Donde
|
|
335
|
+
// . → cualquier carácter (excepto salto de línea, a menos que uses el flag s).
|
|
336
|
+
// * → cero o más repeticiones del carácter anterior (.).
|
|
337
|
+
```
|
|
338
|
+
|
|
339
|
+
```ts
|
|
340
|
+
const text = "Cliente: 😀 secreto=123";
|
|
341
|
+
const rules: PiiReplacement[] = [
|
|
342
|
+
{ pattern: "\\p{Emoji}", replaceWith: "[EMOJI]", flags: "gu" },
|
|
343
|
+
];
|
|
344
|
+
console.log(redact(text, rules));
|
|
345
|
+
// Cliente: [EMOJI] secreto=123
|
|
346
|
+
|
|
347
|
+
// Donde
|
|
348
|
+
// \p{...} → en regex con flag u (unicode), permite usar propiedades Unicode.
|
|
349
|
+
// \p{Emoji} → coincide con cualquier carácter que esté clasificado en Unicode como un emoji.
|
|
350
|
+
```
|
|
351
|
+
|
|
352
|
+
```ts
|
|
353
|
+
const text = "ID=1234 ID=5678";
|
|
354
|
+
const regexRule: PiiReplacement = {
|
|
355
|
+
pattern: "ID=\\d{4}",
|
|
356
|
+
replaceWith: "ID=[REDACTED]",
|
|
357
|
+
flags: "y",
|
|
358
|
+
};
|
|
359
|
+
|
|
360
|
+
const regex = new RegExp(regexRule.pattern, regexRule.flags);
|
|
361
|
+
regex.lastIndex = 0;
|
|
362
|
+
console.log(regex.exec(text)); // ["ID=1234"]
|
|
363
|
+
|
|
364
|
+
regex.lastIndex = 7;
|
|
365
|
+
console.log(regex.exec(text)); // ["ID=5678"]
|
|
366
|
+
|
|
367
|
+
regex.lastIndex = 3;
|
|
368
|
+
console.log(regex.exec(text)); // null (porque no empieza justo ahí)
|
|
369
|
+
|
|
370
|
+
// Donde:
|
|
371
|
+
// \d → significa un dígito (0–9).
|
|
372
|
+
// {4} → significa exactamente 4 repeticiones consecutivas.
|
|
373
|
+
```
|
|
374
|
+
|
|
186
375
|
##### 2.2) loglevel.settings.ts (normalización de nivel)
|
|
187
376
|
|
|
188
377
|
```ts
|