@edirect/tokenization 0.0.10 → 11.0.25

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 (38) hide show
  1. package/README.md +10 -5
  2. package/dist/README.md +113 -0
  3. package/dist/package.json +36 -0
  4. package/dist/src/core/app.d.ts +17 -0
  5. package/dist/src/core/app.d.ts.map +1 -0
  6. package/dist/src/core/app.js +114 -0
  7. package/dist/src/core/domain.d.ts +25 -0
  8. package/dist/src/core/domain.d.ts.map +1 -0
  9. package/dist/src/core/domain.js +2 -0
  10. package/dist/src/core/evaluator/basic.d.ts +5 -0
  11. package/dist/src/core/evaluator/basic.d.ts.map +1 -0
  12. package/dist/src/core/evaluator/basic.js +32 -0
  13. package/dist/src/core/evaluator/index.d.ts +8 -0
  14. package/dist/src/core/evaluator/index.d.ts.map +1 -0
  15. package/dist/src/core/evaluator/index.js +26 -0
  16. package/dist/src/core/services/configuration.d.ts +12 -0
  17. package/dist/src/core/services/configuration.d.ts.map +1 -0
  18. package/dist/src/core/services/configuration.js +82 -0
  19. package/dist/src/core/services/tokenization.d.ts +9 -0
  20. package/dist/src/core/services/tokenization.d.ts.map +1 -0
  21. package/dist/src/core/services/tokenization.js +43 -0
  22. package/dist/src/core/traverser/index.d.ts +4 -0
  23. package/dist/src/core/traverser/index.d.ts.map +1 -0
  24. package/dist/src/core/traverser/index.js +19 -0
  25. package/dist/src/core/utils/object.d.ts +7 -0
  26. package/dist/src/core/utils/object.d.ts.map +1 -0
  27. package/dist/src/core/utils/object.js +33 -0
  28. package/dist/{index.d.ts → src/index.d.ts} +4 -19
  29. package/dist/src/index.d.ts.map +1 -0
  30. package/dist/src/index.js +70 -0
  31. package/dist/src/types.d.ts +56 -0
  32. package/dist/src/types.d.ts.map +1 -0
  33. package/dist/src/types.js +5 -0
  34. package/dist/tsconfig.lib.tsbuildinfo +1 -0
  35. package/package.json +44 -24
  36. package/dist/index.d.mts +0 -44
  37. package/dist/index.js +0 -443
  38. package/dist/index.mjs +0 -406
package/dist/index.js DELETED
@@ -1,443 +0,0 @@
1
- "use strict";
2
- var __create = Object.create;
3
- var __defProp = Object.defineProperty;
4
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
- var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
- var __hasOwnProp = Object.prototype.hasOwnProperty;
8
- var __export = (target, all) => {
9
- for (var name in all)
10
- __defProp(target, name, { get: all[name], enumerable: true });
11
- };
12
- var __copyProps = (to, from, except, desc) => {
13
- if (from && typeof from === "object" || typeof from === "function") {
14
- for (let key of __getOwnPropNames(from))
15
- if (!__hasOwnProp.call(to, key) && key !== except)
16
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
- }
18
- return to;
19
- };
20
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
- // If the importer is in node compatibility mode or this is not an ESM
22
- // file that has been converted to a CommonJS file using a Babel-
23
- // compatible transform (i.e. "__esModule" has not been set), then set
24
- // "default" to the CommonJS "module.exports" for node compatibility.
25
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
- mod
27
- ));
28
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
-
30
- // src/index.ts
31
- var src_exports = {};
32
- __export(src_exports, {
33
- Tokenization: () => Tokenization
34
- });
35
- module.exports = __toCommonJS(src_exports);
36
-
37
- // src/core/traverser/index.ts
38
- var traverse = (obj, callback, path = []) => {
39
- if (Array.isArray(obj)) {
40
- obj.forEach((value, index) => {
41
- traverse(value, callback, [...path, `${index}`]);
42
- });
43
- } else if (obj !== null && typeof obj === "object") {
44
- Object.keys(obj).forEach((key) => {
45
- traverse(obj[key], callback, [...path, key]);
46
- });
47
- } else {
48
- callback(obj, path);
49
- }
50
- };
51
-
52
- // src/core/utils/object.ts
53
- var get = (obj, path) => {
54
- let currentObj = obj;
55
- for (let i = 0; i < path.length; i++) {
56
- const key = path[i];
57
- if (currentObj[key] !== void 0) {
58
- currentObj = currentObj[key];
59
- } else {
60
- return void 0;
61
- }
62
- }
63
- return currentObj;
64
- };
65
- var set = (obj, path, value) => {
66
- let currentObj = obj;
67
- for (let i = 0; i < path.length; i++) {
68
- const key = path[i];
69
- if (i === path.length - 1) {
70
- currentObj[key] = value;
71
- } else {
72
- if (!currentObj[key] || typeof currentObj[key] !== "object") {
73
- currentObj[key] = isNaN(Number(path[i + 1])) ? {} : [];
74
- }
75
- currentObj = currentObj[key];
76
- }
77
- }
78
- };
79
-
80
- // src/core/evaluator/basic.ts
81
- var BasicEvaluator = class {
82
- Evaluate(path, ...properties) {
83
- const pathSegments = path.split(".");
84
- const propertiesSegments = properties;
85
- let i = 0, j = 0;
86
- while (i < pathSegments.length && j < propertiesSegments.length) {
87
- if (pathSegments[i] === "**") {
88
- if (i === pathSegments.length - 1) {
89
- return true;
90
- }
91
- i++;
92
- while (j < propertiesSegments.length && propertiesSegments[j] !== pathSegments[i]) {
93
- j++;
94
- }
95
- } else if (pathSegments[i] === "*" || pathSegments[i] === propertiesSegments[j]) {
96
- i++;
97
- j++;
98
- } else {
99
- return false;
100
- }
101
- }
102
- return i === pathSegments.length && j === propertiesSegments.length;
103
- }
104
- };
105
-
106
- // src/core/evaluator/index.ts
107
- var TokenEvaluator = class {
108
- evaluator;
109
- constructor(evaluator) {
110
- this.evaluator = evaluator || new BasicEvaluator();
111
- }
112
- shouldTokenizeField(config, path) {
113
- if (!config) {
114
- return false;
115
- }
116
- if (config.fields.length === 0) {
117
- return config.defaultClassification > 0;
118
- }
119
- const ret = config.fields.map((field) => {
120
- if (!field.classification) {
121
- return false;
122
- }
123
- return this.evaluator.Evaluate(field.path, ...path);
124
- });
125
- return ret.some((r) => r) || config.defaultClassification > 0;
126
- }
127
- };
128
-
129
- // src/core/app.ts
130
- var import_lru_cache = require("lru-cache");
131
- var TokenizationApp = class {
132
- cache;
133
- tokenizationService;
134
- configurationService;
135
- evaluator;
136
- constructor(tokenizationService, configurationService, evaluator) {
137
- this.tokenizationService = tokenizationService;
138
- this.configurationService = configurationService;
139
- this.evaluator = new TokenEvaluator(evaluator);
140
- this.cache = new import_lru_cache.LRUCache({
141
- max: 5e3,
142
- ttl: 1e3 * 60 * 1,
143
- // 1 minute
144
- allowStale: false,
145
- updateAgeOnGet: false,
146
- updateAgeOnHas: false
147
- });
148
- }
149
- async tokenize(auth, tenant, configKey, payload) {
150
- const config = await this.configurationService.get(auth, tenant, configKey);
151
- if (!config) return payload;
152
- if ((!config.fields || config.fields.length === 0) && config.defaultClassification === 0)
153
- return payload;
154
- const respPayload = payload;
155
- const processedPayload = await this.processRequest(config, payload);
156
- if (Object.keys(processedPayload.notFound).length > 0) {
157
- const tokenPayload = await this.tokenizationService.tokenize(
158
- auth,
159
- tenant,
160
- configKey,
161
- processedPayload.notFound
162
- );
163
- for (const key in tokenPayload) {
164
- const path = key.split(".");
165
- const token = tokenPayload[key];
166
- const original = get(respPayload, path);
167
- set(respPayload, path, token);
168
- if (typeof token === "string") {
169
- this.cache.set(token, original);
170
- this.cache.set(original.toString(), token);
171
- }
172
- }
173
- }
174
- if (Object.keys(processedPayload.found).length > 0) {
175
- for (const key in processedPayload.found) {
176
- set(respPayload, key.split("."), processedPayload.found[key]);
177
- }
178
- }
179
- return respPayload;
180
- }
181
- async detokenize(auth, tenant, configKey, payload) {
182
- const config = await this.configurationService.get(auth, tenant, configKey);
183
- if (!config) return payload;
184
- if ((!config.fields || config.fields.length === 0) && config.defaultClassification === 0)
185
- return payload;
186
- const respPayload = payload;
187
- const processedPayload = this.processRequest(config, payload);
188
- if (Object.keys(processedPayload.notFound).length > 0) {
189
- const tokenPayload = await this.tokenizationService.detokenize(
190
- auth,
191
- tenant,
192
- configKey,
193
- processedPayload.notFound
194
- );
195
- for (const key in tokenPayload) {
196
- const path = key.split(".");
197
- const token = get(respPayload, path);
198
- const original = tokenPayload[key];
199
- if (typeof original === "string") {
200
- this.cache.set(token, original);
201
- this.cache.set(original.toString(), token);
202
- }
203
- set(respPayload, key.split("."), original);
204
- }
205
- }
206
- if (Object.keys(processedPayload.found).length > 0) {
207
- for (const key in processedPayload.found) {
208
- set(respPayload, key.split("."), processedPayload.found[key]);
209
- }
210
- }
211
- return respPayload;
212
- }
213
- processRequest(config, payload) {
214
- if (config.fields.length === 0) {
215
- return {
216
- found: payload,
217
- notFound: {}
218
- };
219
- }
220
- const req = {
221
- found: {},
222
- notFound: {}
223
- };
224
- traverse(payload, (value, path) => {
225
- if (!value) return;
226
- if (this.evaluator.shouldTokenizeField(config, path)) {
227
- const fromCache = this.cache.get(value.toString());
228
- if (fromCache) {
229
- req.found[path.join(".")] = fromCache;
230
- } else {
231
- req.notFound[path.join(".")] = value;
232
- }
233
- }
234
- });
235
- return req;
236
- }
237
- };
238
-
239
- // src/core/services/configuration.ts
240
- var import_lru_cache2 = require("lru-cache");
241
- var import_axios = __toESM(require("axios"));
242
- var ConfigurationService = class {
243
- constructor(baseUrl) {
244
- this.baseUrl = baseUrl;
245
- if (!this.baseUrl) {
246
- throw new Error("Configuration Service BaseUrl is required");
247
- }
248
- this.cache = new import_lru_cache2.LRUCache({
249
- max: 100,
250
- ttl: 1e3 * 60 * 5,
251
- // 5 minutes
252
- allowStale: false,
253
- updateAgeOnGet: false,
254
- updateAgeOnHas: false
255
- });
256
- }
257
- cache;
258
- async get(auth, tenant, config) {
259
- const cached = this.cache.get(`${tenant}/${config}`);
260
- if (cached) {
261
- return cached;
262
- }
263
- try {
264
- const resp = await import_axios.default.get(
265
- `${this.baseUrl}/api/v1/${tenant}/${config}/config`,
266
- {
267
- headers: {
268
- Authorization: `Bearer ${auth}`
269
- }
270
- }
271
- );
272
- const data = resp.data;
273
- this.cache.set(`${tenant}/${config}`, data);
274
- return data;
275
- } catch (error) {
276
- if (import_axios.default.isAxiosError(error) && error.response?.status === 404) {
277
- return void 0;
278
- }
279
- throw new Error("Failed to get configuration");
280
- }
281
- }
282
- async create(auth, tenant, config) {
283
- try {
284
- await import_axios.default.post(
285
- `${this.baseUrl}/api/v1/${tenant}/${config.key}/config`,
286
- config,
287
- {
288
- headers: {
289
- Authorization: `Bearer ${auth}`,
290
- "Content-Type": "application/json"
291
- }
292
- }
293
- );
294
- return config;
295
- } catch (error) {
296
- throw new Error("Failed to create configuration");
297
- }
298
- }
299
- async update(auth, tenant, config) {
300
- try {
301
- await import_axios.default.put(
302
- `${this.baseUrl}/api/v1/${tenant}/${config.key}/config`,
303
- config,
304
- {
305
- headers: {
306
- Authorization: `Bearer ${auth}`,
307
- "Content-Type": "application/json"
308
- }
309
- }
310
- );
311
- return config;
312
- } catch (error) {
313
- throw new Error("Failed to update configuration");
314
- }
315
- }
316
- async delete(auth, tenant, config) {
317
- try {
318
- await import_axios.default.delete(
319
- `${this.baseUrl}/api/v1/${tenant}/${config}/config`,
320
- {
321
- headers: {
322
- Authorization: `Bearer ${auth}`
323
- }
324
- }
325
- );
326
- } catch (error) {
327
- throw new Error("Failed to delete configuration");
328
- }
329
- }
330
- };
331
-
332
- // src/core/services/tokenization.ts
333
- var import_axios2 = __toESM(require("axios"));
334
- var TokenizationService = class {
335
- constructor(baseUrl) {
336
- this.baseUrl = baseUrl;
337
- if (!this.baseUrl) {
338
- throw new Error("Tokenization Service BaseUrl is required");
339
- }
340
- }
341
- async tokenize(auth, tenant, config, payload) {
342
- try {
343
- const resp = await import_axios2.default.post(
344
- `${this.baseUrl}/api/v1/${tenant}/${config}/token/tokenize`,
345
- payload,
346
- {
347
- headers: {
348
- "Authorization": `Bearer ${auth}`,
349
- "Content-Type": "application/json"
350
- }
351
- }
352
- );
353
- return resp.data;
354
- } catch (error) {
355
- throw new Error("Failed to tokenize payload");
356
- }
357
- }
358
- async detokenize(auth, tenant, config, payload) {
359
- try {
360
- const resp = await import_axios2.default.post(
361
- `${this.baseUrl}/api/v1/${tenant}/${config}/token/detokenize`,
362
- payload,
363
- {
364
- headers: {
365
- "Authorization": `Bearer ${auth}`,
366
- "Content-Type": "application/json"
367
- }
368
- }
369
- );
370
- return resp.data;
371
- } catch (error) {
372
- throw new Error("Failed to detokenize payload");
373
- }
374
- }
375
- };
376
-
377
- // src/index.ts
378
- var Tokenization = class {
379
- app;
380
- /**
381
- * The constructor of the TokenizationClient class.
382
- * @param url The URL of the tokenization server.
383
- */
384
- constructor(baseUrlOrApp) {
385
- if (baseUrlOrApp && typeof baseUrlOrApp !== "string") {
386
- this.app = baseUrlOrApp;
387
- return;
388
- }
389
- const tokenizationService = new TokenizationService(baseUrlOrApp);
390
- const configurationService = new ConfigurationService(
391
- baseUrlOrApp
392
- );
393
- this.app = new TokenizationApp(tokenizationService, configurationService);
394
- return;
395
- }
396
- /**
397
- * Tokenizes the given payload.
398
- * @param tenant The tenant to tokenize the payload for.
399
- * @param payload The payload to tokenize.
400
- * @returns The tokenized payload.
401
- */
402
- tokenize(auth, tenant, config, payload) {
403
- return this.app.tokenize(auth, tenant, config, payload);
404
- }
405
- /**
406
- * Detokenizes the given payload.
407
- * @param tenant The tenant to detokenize the payload for.
408
- * @param payload The payload to detokenize.
409
- * @returns The detokenized payload.
410
- */
411
- detokenize(auth, tenant, config, payload) {
412
- return this.app.detokenize(auth, tenant, config, payload);
413
- }
414
- static parseToken(token) {
415
- const parts = token.split(":");
416
- if (parts.length !== 4) {
417
- return {
418
- isToken: false,
419
- tenant: "",
420
- type: "",
421
- hash: ""
422
- };
423
- }
424
- if (parts[0] !== "token") {
425
- return {
426
- isToken: false,
427
- tenant: "",
428
- type: "",
429
- hash: ""
430
- };
431
- }
432
- return {
433
- isToken: true,
434
- tenant: parts[1],
435
- type: parts[2],
436
- hash: parts[3]
437
- };
438
- }
439
- };
440
- // Annotate the CommonJS export names for ESM import in node:
441
- 0 && (module.exports = {
442
- Tokenization
443
- });