@netacea/netaceaintegrationbase 2.0.28 → 2.0.29

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/dist/index.cjs CHANGED
@@ -476,27 +476,54 @@ function extractCookieAttr(attributes, attribute) {
476
476
  ? extractedAttr?.replace(`${attribute}=`, '')
477
477
  : undefined;
478
478
  }
479
+ /**
480
+ * Remove duplicate attributes and return the result joined as a string
481
+ * Attributes are seperated by a semi-colon and a space: '; '
482
+ * By default the first attributes found for a given name are retained,
483
+ * however this can be overrided using the keepLast option.
484
+ * Attributes are compared for equality in a case-insensitive manner.
485
+ * @param attributes a string containing the attributes, or and array of strings
486
+ * containing one or more attributes.
487
+ * @param keepLast whether to prefer keeping the last value, default false.
488
+ * @returns
489
+ */
479
490
  function removeDuplicateAttrs(attributes, keepLast = false) {
491
+ if (typeof attributes !== 'string') {
492
+ attributes = attributes.join('; ');
493
+ }
480
494
  if (attributes === '') {
481
495
  return '';
482
496
  }
483
- const capitalizeAttrName = (attr) => attr.charAt(0).toUpperCase() + attr.slice(1);
484
- return attributes
485
- .replace(/ /g, '')
486
- .split(';')
487
- .map(capitalizeAttrName)
488
- .filter(s => s.trim() !== '')
489
- .filter((attr, i, attrs) => {
490
- const getAttrName = (attr) => attr.split('=')[0];
491
- const attrName = getAttrName(attr);
492
- const attrNames = attrs.map(getAttrName);
493
- if (keepLast) {
494
- return i === attrNames.lastIndexOf(attrName);
495
- }
496
- return i === attrNames.indexOf(attrName);
497
- })
497
+ return removeDuplicateAttributesSplit(attributes.split(';'), keepLast)
498
498
  .join('; ');
499
499
  }
500
+ /**
501
+ * PRIVATE method for removing duplicate attributes that have been pre-split
502
+ * @param attributes
503
+ * @param keepLast whether to prefer keeping the last value, default false.
504
+ * @returns An array of attributes.
505
+ */
506
+ function removeDuplicateAttributesSplit(attributes, keepLast = false) {
507
+ if (keepLast) {
508
+ return removeDuplicateAttributesSplit(attributes.reverse()).reverse();
509
+ }
510
+ const retainedAttributeNames = new Set();
511
+ const retainedAttributes = [];
512
+ for (let attribute of attributes) {
513
+ // Remove starting whitespace and skip empty strings entirely
514
+ attribute = attribute.trimStart();
515
+ if (attribute.trim() === '') {
516
+ continue;
517
+ }
518
+ // Keep non-duplicates
519
+ const attrNameUpper = attribute.split('=')[0].toUpperCase();
520
+ if (!retainedAttributeNames.has(attrNameUpper)) {
521
+ retainedAttributeNames.add(attrNameUpper);
522
+ retainedAttributes.push(attribute);
523
+ }
524
+ }
525
+ return retainedAttributes;
526
+ }
500
527
 
501
528
  var cookieAttributes = /*#__PURE__*/Object.freeze({
502
529
  __proto__: null,
@@ -535,8 +562,31 @@ var netaceaSessionCookie = /*#__PURE__*/Object.freeze({
535
562
  createSetCookieString: createSetCookieString
536
563
  });
537
564
 
565
+ function parseSetCookie(setCookie) {
566
+ const eqIndex = setCookie.indexOf('=');
567
+ if (eqIndex < 0) {
568
+ throw new Error('Could not parse the given set-cookie value.');
569
+ }
570
+ const name = setCookie.slice(0, eqIndex);
571
+ const valueWithAttributes = setCookie.slice(eqIndex + 1);
572
+ const semiIndex = valueWithAttributes.indexOf(';');
573
+ const value = valueWithAttributes.slice(0, semiIndex);
574
+ const attributes = valueWithAttributes.slice(semiIndex).trimStart();
575
+ return {
576
+ name,
577
+ value,
578
+ attributes
579
+ };
580
+ }
581
+
582
+ var parse = /*#__PURE__*/Object.freeze({
583
+ __proto__: null,
584
+ parseSetCookie: parseSetCookie
585
+ });
586
+
538
587
  const lib = {
539
588
  cookie: {
589
+ parse,
540
590
  attributes: cookieAttributes,
541
591
  netaceaSession: netaceaSessionCookie
542
592
  }
package/dist/index.d.ts CHANGED
@@ -532,7 +532,18 @@ declare namespace dictionary_d {
532
532
  declare function configureCookiesDomain(cookieAttr: string | undefined, captchaCookieAttr: string | undefined): CookiesAttributes;
533
533
  declare function extractAndRemoveCookieAttr(attributes: string, attribute: string): SlicedCookieAttributes;
534
534
  declare function extractCookieAttr(attributes: string, attribute: string): string | undefined;
535
- declare function removeDuplicateAttrs(attributes: string, keepLast?: boolean): string;
535
+ /**
536
+ * Remove duplicate attributes and return the result joined as a string
537
+ * Attributes are seperated by a semi-colon and a space: '; '
538
+ * By default the first attributes found for a given name are retained,
539
+ * however this can be overrided using the keepLast option.
540
+ * Attributes are compared for equality in a case-insensitive manner.
541
+ * @param attributes a string containing the attributes, or and array of strings
542
+ * containing one or more attributes.
543
+ * @param keepLast whether to prefer keeping the last value, default false.
544
+ * @returns
545
+ */
546
+ declare function removeDuplicateAttrs(attributes: string | string[], keepLast?: boolean): string;
536
547
 
537
548
  declare const cookieAttributes_configureCookiesDomain: typeof configureCookiesDomain;
538
549
  declare const cookieAttributes_extractAndRemoveCookieAttr: typeof extractAndRemoveCookieAttr;
@@ -562,8 +573,20 @@ declare namespace netaceaSessionCookie {
562
573
  export { netaceaSessionCookie_createNetaceaCaptchaSetCookieString as createNetaceaCaptchaSetCookieString, netaceaSessionCookie_createNetaceaSetCookieString as createNetaceaSetCookieString, netaceaSessionCookie_createSetCookieString as createSetCookieString };
563
574
  }
564
575
 
576
+ declare function parseSetCookie(setCookie: string): {
577
+ name: string;
578
+ value: string;
579
+ attributes: string;
580
+ };
581
+
582
+ declare const parse_parseSetCookie: typeof parseSetCookie;
583
+ declare namespace parse {
584
+ export { parse_parseSetCookie as parseSetCookie };
585
+ }
586
+
565
587
  declare const lib: {
566
588
  cookie: {
589
+ parse: typeof parse;
567
590
  attributes: typeof cookieAttributes;
568
591
  netaceaSession: typeof netaceaSessionCookie;
569
592
  };
package/dist/index.mjs CHANGED
@@ -474,27 +474,54 @@ function extractCookieAttr(attributes, attribute) {
474
474
  ? extractedAttr?.replace(`${attribute}=`, '')
475
475
  : undefined;
476
476
  }
477
+ /**
478
+ * Remove duplicate attributes and return the result joined as a string
479
+ * Attributes are seperated by a semi-colon and a space: '; '
480
+ * By default the first attributes found for a given name are retained,
481
+ * however this can be overrided using the keepLast option.
482
+ * Attributes are compared for equality in a case-insensitive manner.
483
+ * @param attributes a string containing the attributes, or and array of strings
484
+ * containing one or more attributes.
485
+ * @param keepLast whether to prefer keeping the last value, default false.
486
+ * @returns
487
+ */
477
488
  function removeDuplicateAttrs(attributes, keepLast = false) {
489
+ if (typeof attributes !== 'string') {
490
+ attributes = attributes.join('; ');
491
+ }
478
492
  if (attributes === '') {
479
493
  return '';
480
494
  }
481
- const capitalizeAttrName = (attr) => attr.charAt(0).toUpperCase() + attr.slice(1);
482
- return attributes
483
- .replace(/ /g, '')
484
- .split(';')
485
- .map(capitalizeAttrName)
486
- .filter(s => s.trim() !== '')
487
- .filter((attr, i, attrs) => {
488
- const getAttrName = (attr) => attr.split('=')[0];
489
- const attrName = getAttrName(attr);
490
- const attrNames = attrs.map(getAttrName);
491
- if (keepLast) {
492
- return i === attrNames.lastIndexOf(attrName);
493
- }
494
- return i === attrNames.indexOf(attrName);
495
- })
495
+ return removeDuplicateAttributesSplit(attributes.split(';'), keepLast)
496
496
  .join('; ');
497
497
  }
498
+ /**
499
+ * PRIVATE method for removing duplicate attributes that have been pre-split
500
+ * @param attributes
501
+ * @param keepLast whether to prefer keeping the last value, default false.
502
+ * @returns An array of attributes.
503
+ */
504
+ function removeDuplicateAttributesSplit(attributes, keepLast = false) {
505
+ if (keepLast) {
506
+ return removeDuplicateAttributesSplit(attributes.reverse()).reverse();
507
+ }
508
+ const retainedAttributeNames = new Set();
509
+ const retainedAttributes = [];
510
+ for (let attribute of attributes) {
511
+ // Remove starting whitespace and skip empty strings entirely
512
+ attribute = attribute.trimStart();
513
+ if (attribute.trim() === '') {
514
+ continue;
515
+ }
516
+ // Keep non-duplicates
517
+ const attrNameUpper = attribute.split('=')[0].toUpperCase();
518
+ if (!retainedAttributeNames.has(attrNameUpper)) {
519
+ retainedAttributeNames.add(attrNameUpper);
520
+ retainedAttributes.push(attribute);
521
+ }
522
+ }
523
+ return retainedAttributes;
524
+ }
498
525
 
499
526
  var cookieAttributes = /*#__PURE__*/Object.freeze({
500
527
  __proto__: null,
@@ -533,8 +560,31 @@ var netaceaSessionCookie = /*#__PURE__*/Object.freeze({
533
560
  createSetCookieString: createSetCookieString
534
561
  });
535
562
 
563
+ function parseSetCookie(setCookie) {
564
+ const eqIndex = setCookie.indexOf('=');
565
+ if (eqIndex < 0) {
566
+ throw new Error('Could not parse the given set-cookie value.');
567
+ }
568
+ const name = setCookie.slice(0, eqIndex);
569
+ const valueWithAttributes = setCookie.slice(eqIndex + 1);
570
+ const semiIndex = valueWithAttributes.indexOf(';');
571
+ const value = valueWithAttributes.slice(0, semiIndex);
572
+ const attributes = valueWithAttributes.slice(semiIndex).trimStart();
573
+ return {
574
+ name,
575
+ value,
576
+ attributes
577
+ };
578
+ }
579
+
580
+ var parse = /*#__PURE__*/Object.freeze({
581
+ __proto__: null,
582
+ parseSetCookie: parseSetCookie
583
+ });
584
+
536
585
  const lib = {
537
586
  cookie: {
587
+ parse,
538
588
  attributes: cookieAttributes,
539
589
  netaceaSession: netaceaSessionCookie
540
590
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@netacea/netaceaintegrationbase",
3
- "version": "2.0.28",
3
+ "version": "2.0.29",
4
4
  "description": "Base package for Netacea CDN integrations.",
5
5
  "type": "module",
6
6
  "types": "dist/index.d.ts",
@@ -24,7 +24,7 @@
24
24
  },
25
25
  "license": "UNLICENSED",
26
26
  "dependencies": {
27
- "@netacea/kinesisingest": "^1.5.46"
27
+ "@netacea/kinesisingest": "^1.5.47"
28
28
  },
29
- "gitHead": "9b917cee8b330d70482637ceb7459f23255347c9"
29
+ "gitHead": "729446935964643bd7f7e4d8c3016ec561ce39dd"
30
30
  }