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