@punks/backend-core 0.0.76 → 0.0.78

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.
@@ -0,0 +1,5 @@
1
+ /// <reference types="node" />
2
+ export declare const encodeUtf8: (value: Uint8Array) => string;
3
+ export declare const decodeUtf8: (value: string) => Buffer;
4
+ export declare const encodeBase64: (value: Uint8Array) => string;
5
+ export declare const decodeBase64: (value: string) => Buffer;
@@ -0,0 +1,61 @@
1
+ type QuoteCharacter = '"' | "'";
2
+ export interface StateMachineOptions {
3
+ readonly allowedTags?: Set<string>;
4
+ readonly disallowedTags?: Set<string>;
5
+ readonly tagReplacementText: string;
6
+ readonly encodePlaintextTagDelimiters: boolean;
7
+ }
8
+ export type StateTransitionFunction = (next: State) => void;
9
+ export interface State {
10
+ consume(character: string, transition: StateTransitionFunction): string;
11
+ }
12
+ type InPlaintextStateTransitionFunction = (next: InTagNameState) => void;
13
+ export declare class InPlaintextState implements State {
14
+ private readonly options;
15
+ constructor(options: StateMachineOptions);
16
+ consume(character: string, transition: InPlaintextStateTransitionFunction): string;
17
+ }
18
+ export declare const enum TagMode {
19
+ Allowed = 0,
20
+ Disallowed = 1
21
+ }
22
+ type InTagNameStateTransitionFunction = (next: InPlaintextState | InTagState<TagMode.Allowed> | InTagState<TagMode.Disallowed> | InCommentState) => void;
23
+ export declare class InTagNameState implements State {
24
+ private readonly options;
25
+ private nameBuffer;
26
+ private isClosingTag;
27
+ constructor(options: StateMachineOptions);
28
+ consume(character: string, transition: InTagNameStateTransitionFunction): string;
29
+ private isNameBufferAnAllowedTag;
30
+ }
31
+ type InTagStateTransitionFunction<T extends TagMode> = (next: InPlaintextState | InQuotedStringInTagState<T>) => void;
32
+ export declare class InTagState<T extends TagMode> implements State {
33
+ readonly mode: T;
34
+ private readonly options;
35
+ constructor(mode: T, options: StateMachineOptions);
36
+ consume(character: string, transition: InTagStateTransitionFunction<T>): string;
37
+ }
38
+ type InQuotedStringInTagStateTransitionFunction<T extends TagMode> = (next: InTagState<T>) => void;
39
+ export declare class InQuotedStringInTagState<T extends TagMode> implements State {
40
+ readonly mode: T;
41
+ readonly quoteCharacter: QuoteCharacter;
42
+ private readonly options;
43
+ constructor(mode: T, quoteCharacter: QuoteCharacter, options: StateMachineOptions);
44
+ consume(character: string, transition: InQuotedStringInTagStateTransitionFunction<T>): string;
45
+ }
46
+ type InCommentStateTransitionFunction = (next: InPlaintextState) => void;
47
+ export declare class InCommentState implements State {
48
+ private readonly options;
49
+ private consecutiveHyphens;
50
+ constructor(options: StateMachineOptions);
51
+ consume(character: string, transition: InCommentStateTransitionFunction): string;
52
+ }
53
+ export declare const DefaultStateMachineOptions: StateMachineOptions;
54
+ export declare class StateMachine {
55
+ private state;
56
+ private transitionFunction;
57
+ constructor(partialOptions?: Partial<StateMachineOptions>);
58
+ consume(text: string): string;
59
+ }
60
+ export declare function stripTags(text: string, options?: Partial<StateMachineOptions>): string;
61
+ export {};
@@ -6,6 +6,8 @@ export { getDirectoryFilePaths } from "./files";
6
6
  export { joinPath, splitPath, ensureDirectory, getDirectoryPath, createDayPath, } from "./paths";
7
7
  export * from "./strings";
8
8
  export * from "./objects";
9
+ export * from "./encoding";
10
+ export { stripTags } from "./html";
9
11
  export { maskVariable } from "./variables";
10
12
  export * from "./text";
11
13
  export * from "./threading";
@@ -1 +1,2 @@
1
1
  export declare const newUuid: () => string;
2
+ export declare const generateHash: (length: number) => string;
package/dist/esm/index.js CHANGED
@@ -412,6 +412,200 @@ const jsonSerialize = (obj, options) => {
412
412
  return JSON.stringify(obj, replacer, spaces);
413
413
  };
414
414
 
415
+ const encodeUtf8 = (value) => Buffer.from(value).toString("utf-8");
416
+ const decodeUtf8 = (value) => Buffer.from(value, "utf-8");
417
+ const encodeBase64 = (value) => Buffer.from(value).toString("base64");
418
+ const decodeBase64 = (value) => Buffer.from(value, "base64");
419
+
420
+ // borrowed from https://github.com/ericnorris/striptags
421
+ function isSpace(character) {
422
+ return (character == " " ||
423
+ character == "\n" ||
424
+ character == "\r" ||
425
+ character == "\t");
426
+ }
427
+ function isQuote(character) {
428
+ return character == '"' || character == "'";
429
+ }
430
+ const TAG_START = "<";
431
+ const TAG_END = ">";
432
+ const ENCODED_TAG_START = "&lt;";
433
+ const ENCODED_TAG_END = "&gt;";
434
+ class InPlaintextState {
435
+ constructor(options) {
436
+ this.options = options;
437
+ }
438
+ consume(character, transition) {
439
+ if (character == TAG_START) {
440
+ transition(new InTagNameState(this.options));
441
+ return "";
442
+ }
443
+ else if (character == TAG_END &&
444
+ this.options.encodePlaintextTagDelimiters) {
445
+ return ENCODED_TAG_END;
446
+ }
447
+ return character;
448
+ }
449
+ }
450
+ class InTagNameState {
451
+ constructor(options) {
452
+ this.options = options;
453
+ this.nameBuffer = "";
454
+ this.isClosingTag = false;
455
+ }
456
+ consume(character, transition) {
457
+ if (this.nameBuffer.length == 0) {
458
+ if (isSpace(character)) {
459
+ transition(new InPlaintextState(this.options));
460
+ return ((this.options.encodePlaintextTagDelimiters
461
+ ? ENCODED_TAG_START
462
+ : "<") + character);
463
+ }
464
+ if (character == "/") {
465
+ this.isClosingTag = true;
466
+ return "";
467
+ }
468
+ }
469
+ if (isSpace(character)) {
470
+ if (this.isNameBufferAnAllowedTag()) {
471
+ transition(new InTagState(0 /* TagMode.Allowed */, this.options));
472
+ return (TAG_START +
473
+ (this.isClosingTag ? "/" : "") +
474
+ this.nameBuffer +
475
+ character);
476
+ }
477
+ else {
478
+ transition(new InTagState(1 /* TagMode.Disallowed */, this.options));
479
+ return this.options.tagReplacementText;
480
+ }
481
+ }
482
+ if (character == TAG_START) {
483
+ this.nameBuffer += ENCODED_TAG_START;
484
+ return "";
485
+ }
486
+ if (character == TAG_END) {
487
+ transition(new InPlaintextState(this.options));
488
+ if (this.isNameBufferAnAllowedTag()) {
489
+ return (TAG_START +
490
+ (this.isClosingTag ? "/" : "") +
491
+ this.nameBuffer +
492
+ character);
493
+ }
494
+ else {
495
+ return this.options.tagReplacementText;
496
+ }
497
+ }
498
+ if (character == "-" && this.nameBuffer == "!-") {
499
+ transition(new InCommentState(this.options));
500
+ return "";
501
+ }
502
+ this.nameBuffer += character;
503
+ return "";
504
+ }
505
+ isNameBufferAnAllowedTag() {
506
+ const tagName = this.nameBuffer.toLowerCase();
507
+ if (this.options.allowedTags) {
508
+ return this.options.allowedTags.has(tagName);
509
+ }
510
+ else if (this.options.disallowedTags) {
511
+ return !this.options.disallowedTags.has(tagName);
512
+ }
513
+ else {
514
+ return false;
515
+ }
516
+ }
517
+ }
518
+ class InTagState {
519
+ constructor(mode, options) {
520
+ this.mode = mode;
521
+ this.options = options;
522
+ }
523
+ consume(character, transition) {
524
+ if (character == TAG_END) {
525
+ transition(new InPlaintextState(this.options));
526
+ }
527
+ else if (isQuote(character)) {
528
+ transition(new InQuotedStringInTagState(this.mode, character, this.options));
529
+ }
530
+ if (this.mode == 1 /* TagMode.Disallowed */) {
531
+ return "";
532
+ }
533
+ if (character == TAG_START) {
534
+ return ENCODED_TAG_START;
535
+ }
536
+ else {
537
+ return character;
538
+ }
539
+ }
540
+ }
541
+ class InQuotedStringInTagState {
542
+ constructor(mode, quoteCharacter, options) {
543
+ this.mode = mode;
544
+ this.quoteCharacter = quoteCharacter;
545
+ this.options = options;
546
+ }
547
+ consume(character, transition) {
548
+ if (character == this.quoteCharacter) {
549
+ transition(new InTagState(this.mode, this.options));
550
+ }
551
+ if (this.mode == 1 /* TagMode.Disallowed */) {
552
+ return "";
553
+ }
554
+ if (character == TAG_START) {
555
+ return ENCODED_TAG_START;
556
+ }
557
+ else if (character == TAG_END) {
558
+ return ENCODED_TAG_END;
559
+ }
560
+ else {
561
+ return character;
562
+ }
563
+ }
564
+ }
565
+ class InCommentState {
566
+ constructor(options) {
567
+ this.options = options;
568
+ this.consecutiveHyphens = 0;
569
+ }
570
+ consume(character, transition) {
571
+ if (character == ">" && this.consecutiveHyphens >= 2) {
572
+ transition(new InPlaintextState(this.options));
573
+ }
574
+ else if (character == "-") {
575
+ this.consecutiveHyphens++;
576
+ }
577
+ else {
578
+ this.consecutiveHyphens = 0;
579
+ }
580
+ return "";
581
+ }
582
+ }
583
+ const DefaultStateMachineOptions = {
584
+ tagReplacementText: "",
585
+ encodePlaintextTagDelimiters: true,
586
+ };
587
+ class StateMachine {
588
+ constructor(partialOptions = {}) {
589
+ this.state = new InPlaintextState({
590
+ ...DefaultStateMachineOptions,
591
+ ...partialOptions,
592
+ });
593
+ this.transitionFunction = ((next) => {
594
+ this.state = next;
595
+ }).bind(this);
596
+ }
597
+ consume(text) {
598
+ let outputBuffer = "";
599
+ for (const character of text) {
600
+ outputBuffer += this.state.consume(character, this.transitionFunction);
601
+ }
602
+ return outputBuffer;
603
+ }
604
+ }
605
+ function stripTags(text, options = {}) {
606
+ return new StateMachine(options).consume(text);
607
+ }
608
+
415
609
  const maskVariable = (value, unmaskedLength = 4) => value
416
610
  ? value.substring(0, unmaskedLength).concat(Array.from(value.substring(unmaskedLength))
417
611
  .map(() => "*")
@@ -478,6 +672,13 @@ const newUuid = () => {
478
672
  return v.toString(16);
479
673
  });
480
674
  };
675
+ const generateHash = (length) => {
676
+ let text = "";
677
+ const possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
678
+ for (let i = 0; i < length; i++)
679
+ text += possible.charAt(Math.floor(Math.random() * possible.length));
680
+ return text;
681
+ };
481
682
 
482
683
  function serializeQueryString(obj) {
483
684
  let queryString = "";
@@ -31247,5 +31448,5 @@ const processArrayItemMove = (items, input) => {
31247
31448
  }));
31248
31449
  };
31249
31450
 
31250
- export { ConsoleLogger, DatadogLogger, ExcelKeyTransform, FileLogger, Log, LogLevel, MetaSerializationType, addTime, buildObject, buildTree, buildUrl, byField, byFieldDesc, camelToKebabCase, camelToSnakeCase, createDayPath, csvBuild, csvParse, deserializeQueryString, distinct, distinctElements, ensureDirectory, ensureStartSlash, ensureTailingSlash, excelBuild, excelParse, first, flatten, floorDateToSecond, getDirectoryFilePaths, getDirectoryPath, getQueryParameter, groupBy, indexes, isNullOrUndefined, iterate, joinPath, joinUrl, jsonDistinct, jsonSerialize, last, logMemoryUsage, mapAsync, mapOrThrow, maskVariable, mergeDeep, multipleSplit, newUuid, notNull, notUndefined, pluralize, processArrayDifferences, processArrayItemMove, range, removeUndefinedProps, selectMany, serializeQueryString, sleep, sort, splitPath, subArrays, subtractTime, testInternetConnection, toArrayDict, toCamelCase, toDict, toItemsDict, toItemsMap, toMap, toTitleCase, trim$1 as trim, trimEnd, trimStart, updateQueryParameters };
31451
+ export { ConsoleLogger, DatadogLogger, ExcelKeyTransform, FileLogger, Log, LogLevel, MetaSerializationType, addTime, buildObject, buildTree, buildUrl, byField, byFieldDesc, camelToKebabCase, camelToSnakeCase, createDayPath, csvBuild, csvParse, decodeBase64, decodeUtf8, deserializeQueryString, distinct, distinctElements, encodeBase64, encodeUtf8, ensureDirectory, ensureStartSlash, ensureTailingSlash, excelBuild, excelParse, first, flatten, floorDateToSecond, generateHash, getDirectoryFilePaths, getDirectoryPath, getQueryParameter, groupBy, indexes, isNullOrUndefined, iterate, joinPath, joinUrl, jsonDistinct, jsonSerialize, last, logMemoryUsage, mapAsync, mapOrThrow, maskVariable, mergeDeep, multipleSplit, newUuid, notNull, notUndefined, pluralize, processArrayDifferences, processArrayItemMove, range, removeUndefinedProps, selectMany, serializeQueryString, sleep, sort, splitPath, stripTags, subArrays, subtractTime, testInternetConnection, toArrayDict, toCamelCase, toDict, toItemsDict, toItemsMap, toMap, toTitleCase, trim$1 as trim, trimEnd, trimStart, updateQueryParameters };
31251
31452
  //# sourceMappingURL=index.js.map