@allthings/cloud-toolkit 5.3.1 → 5.3.2-beta.2

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.
@@ -222,6 +222,27 @@ const stringifyIfNotStringifiedJson = (data) => {
222
222
  };
223
223
  const json = (responseData, _, body, statusCode, options) => makeResponseObject(responseData, stringifyIfNotStringifiedJson(body), statusCode, options, 'application/json');
224
224
 
225
+ const splitDirectives = (value) => typeof value === 'string'
226
+ ? value
227
+ .split(',')
228
+ .map((directive) => directive.trim())
229
+ .filter(Boolean)
230
+ : [];
231
+ const withAcceptEncodingVary = (headers = {}) => {
232
+ const varyKeys = Object.keys(headers).filter((key) => key.toLowerCase() === 'vary');
233
+ const directives = varyKeys
234
+ .flatMap((key) => splitDirectives(headers[key]))
235
+ .filter((directive, index, all) => all.findIndex((other) => other.toLowerCase() === directive.toLowerCase()) === index);
236
+ const hasAcceptEncoding = directives.some((directive) => directive.toLowerCase() === 'accept-encoding');
237
+ const merged = directives.includes('*')
238
+ ? ['*']
239
+ : hasAcceptEncoding
240
+ ? directives
241
+ : [...directives, 'Accept-Encoding'];
242
+ const otherHeaders = Object.fromEntries(Object.entries(headers).filter(([key]) => !varyKeys.includes(key)));
243
+ return { ...otherHeaders, [varyKeys[0] ?? 'vary']: merged.join(', ') };
244
+ };
245
+
225
246
  const COMPRESSIBLE = [
226
247
  'application/activity+json',
227
248
  'application/alto-costmap+json',
@@ -403,11 +424,11 @@ const compressResponse = (response, encoding) => {
403
424
  return {
404
425
  ...rest,
405
426
  body: compressed.toString('base64'),
406
- headers: {
427
+ headers: withAcceptEncodingVary({
407
428
  ...headers,
408
429
  'content-encoding': encoding,
409
430
  'content-length': compressed.byteLength,
410
- },
431
+ }),
411
432
  isBase64Encoded: true,
412
433
  };
413
434
  };
@@ -467,10 +488,10 @@ const cspHeaders = (response, _, options) => {
467
488
 
468
489
  const enforcedHeaders = (response, _, options) => ({
469
490
  ...response,
470
- headers: {
491
+ headers: withAcceptEncodingVary({
471
492
  ...response.headers,
472
493
  ...(options.headers ?? {}),
473
- },
494
+ }),
474
495
  });
475
496
 
476
497
  const EMPTY_ENTITY_TAG = '"0-2jmj7l5rSw0yVb/vlWAYkK/YBwk"';
@@ -199,6 +199,27 @@ const stringifyIfNotStringifiedJson = (data) => {
199
199
  };
200
200
  const json = (responseData, _, body, statusCode, options) => makeResponseObject(responseData, stringifyIfNotStringifiedJson(body), statusCode, options, 'application/json');
201
201
 
202
+ const splitDirectives = (value) => typeof value === 'string'
203
+ ? value
204
+ .split(',')
205
+ .map((directive) => directive.trim())
206
+ .filter(Boolean)
207
+ : [];
208
+ const withAcceptEncodingVary = (headers = {}) => {
209
+ const varyKeys = Object.keys(headers).filter((key) => key.toLowerCase() === 'vary');
210
+ const directives = varyKeys
211
+ .flatMap((key) => splitDirectives(headers[key]))
212
+ .filter((directive, index, all) => all.findIndex((other) => other.toLowerCase() === directive.toLowerCase()) === index);
213
+ const hasAcceptEncoding = directives.some((directive) => directive.toLowerCase() === 'accept-encoding');
214
+ const merged = directives.includes('*')
215
+ ? ['*']
216
+ : hasAcceptEncoding
217
+ ? directives
218
+ : [...directives, 'Accept-Encoding'];
219
+ const otherHeaders = Object.fromEntries(Object.entries(headers).filter(([key]) => !varyKeys.includes(key)));
220
+ return { ...otherHeaders, [varyKeys[0] ?? 'vary']: merged.join(', ') };
221
+ };
222
+
202
223
  const COMPRESSIBLE = [
203
224
  'application/activity+json',
204
225
  'application/alto-costmap+json',
@@ -380,11 +401,11 @@ const compressResponse = (response, encoding) => {
380
401
  return {
381
402
  ...rest,
382
403
  body: compressed.toString('base64'),
383
- headers: {
404
+ headers: withAcceptEncodingVary({
384
405
  ...headers,
385
406
  'content-encoding': encoding,
386
407
  'content-length': compressed.byteLength,
387
- },
408
+ }),
388
409
  isBase64Encoded: true,
389
410
  };
390
411
  };
@@ -444,10 +465,10 @@ const cspHeaders = (response, _, options) => {
444
465
 
445
466
  const enforcedHeaders = (response, _, options) => ({
446
467
  ...response,
447
- headers: {
468
+ headers: withAcceptEncodingVary({
448
469
  ...response.headers,
449
470
  ...(options.headers ?? {}),
450
- },
471
+ }),
451
472
  });
452
473
 
453
474
  const EMPTY_ENTITY_TAG = '"0-2jmj7l5rSw0yVb/vlWAYkK/YBwk"';
package/dist/lib.cjs.js CHANGED
@@ -231,6 +231,27 @@ const stringifyIfNotStringifiedJson = (data) => {
231
231
  };
232
232
  const json = (responseData, _, body, statusCode, options) => makeResponseObject(responseData, stringifyIfNotStringifiedJson(body), statusCode, options, 'application/json');
233
233
 
234
+ const splitDirectives = (value) => typeof value === 'string'
235
+ ? value
236
+ .split(',')
237
+ .map((directive) => directive.trim())
238
+ .filter(Boolean)
239
+ : [];
240
+ const withAcceptEncodingVary = (headers = {}) => {
241
+ const varyKeys = Object.keys(headers).filter((key) => key.toLowerCase() === 'vary');
242
+ const directives = varyKeys
243
+ .flatMap((key) => splitDirectives(headers[key]))
244
+ .filter((directive, index, all) => all.findIndex((other) => other.toLowerCase() === directive.toLowerCase()) === index);
245
+ const hasAcceptEncoding = directives.some((directive) => directive.toLowerCase() === 'accept-encoding');
246
+ const merged = directives.includes('*')
247
+ ? ['*']
248
+ : hasAcceptEncoding
249
+ ? directives
250
+ : [...directives, 'Accept-Encoding'];
251
+ const otherHeaders = Object.fromEntries(Object.entries(headers).filter(([key]) => !varyKeys.includes(key)));
252
+ return { ...otherHeaders, [varyKeys[0] ?? 'vary']: merged.join(', ') };
253
+ };
254
+
234
255
  const COMPRESSIBLE = [
235
256
  'application/activity+json',
236
257
  'application/alto-costmap+json',
@@ -412,11 +433,11 @@ const compressResponse = (response, encoding) => {
412
433
  return {
413
434
  ...rest,
414
435
  body: compressed.toString('base64'),
415
- headers: {
436
+ headers: withAcceptEncodingVary({
416
437
  ...headers,
417
438
  'content-encoding': encoding,
418
439
  'content-length': compressed.byteLength,
419
- },
440
+ }),
420
441
  isBase64Encoded: true,
421
442
  };
422
443
  };
@@ -476,10 +497,10 @@ const cspHeaders = (response, _, options) => {
476
497
 
477
498
  const enforcedHeaders = (response, _, options) => ({
478
499
  ...response,
479
- headers: {
500
+ headers: withAcceptEncodingVary({
480
501
  ...response.headers,
481
502
  ...(options.headers ?? {}),
482
- },
503
+ }),
483
504
  });
484
505
 
485
506
  const EMPTY_ENTITY_TAG = '"0-2jmj7l5rSw0yVb/vlWAYkK/YBwk"';
package/dist/lib.esm.mjs CHANGED
@@ -209,6 +209,27 @@ const stringifyIfNotStringifiedJson = (data) => {
209
209
  };
210
210
  const json = (responseData, _, body, statusCode, options) => makeResponseObject(responseData, stringifyIfNotStringifiedJson(body), statusCode, options, 'application/json');
211
211
 
212
+ const splitDirectives = (value) => typeof value === 'string'
213
+ ? value
214
+ .split(',')
215
+ .map((directive) => directive.trim())
216
+ .filter(Boolean)
217
+ : [];
218
+ const withAcceptEncodingVary = (headers = {}) => {
219
+ const varyKeys = Object.keys(headers).filter((key) => key.toLowerCase() === 'vary');
220
+ const directives = varyKeys
221
+ .flatMap((key) => splitDirectives(headers[key]))
222
+ .filter((directive, index, all) => all.findIndex((other) => other.toLowerCase() === directive.toLowerCase()) === index);
223
+ const hasAcceptEncoding = directives.some((directive) => directive.toLowerCase() === 'accept-encoding');
224
+ const merged = directives.includes('*')
225
+ ? ['*']
226
+ : hasAcceptEncoding
227
+ ? directives
228
+ : [...directives, 'Accept-Encoding'];
229
+ const otherHeaders = Object.fromEntries(Object.entries(headers).filter(([key]) => !varyKeys.includes(key)));
230
+ return { ...otherHeaders, [varyKeys[0] ?? 'vary']: merged.join(', ') };
231
+ };
232
+
212
233
  const COMPRESSIBLE = [
213
234
  'application/activity+json',
214
235
  'application/alto-costmap+json',
@@ -390,11 +411,11 @@ const compressResponse = (response, encoding) => {
390
411
  return {
391
412
  ...rest,
392
413
  body: compressed.toString('base64'),
393
- headers: {
414
+ headers: withAcceptEncodingVary({
394
415
  ...headers,
395
416
  'content-encoding': encoding,
396
417
  'content-length': compressed.byteLength,
397
- },
418
+ }),
398
419
  isBase64Encoded: true,
399
420
  };
400
421
  };
@@ -454,10 +475,10 @@ const cspHeaders = (response, _, options) => {
454
475
 
455
476
  const enforcedHeaders = (response, _, options) => ({
456
477
  ...response,
457
- headers: {
478
+ headers: withAcceptEncodingVary({
458
479
  ...response.headers,
459
480
  ...(options.headers ?? {}),
460
- },
481
+ }),
461
482
  });
462
483
 
463
484
  const EMPTY_ENTITY_TAG = '"0-2jmj7l5rSw0yVb/vlWAYkK/YBwk"';
@@ -0,0 +1,2 @@
1
+ declare const withAcceptEncodingVary: (headers?: Record<string, unknown>) => Record<string, unknown>;
2
+ export default withAcceptEncodingVary;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@allthings/cloud-toolkit",
3
- "version": "5.3.1",
3
+ "version": "5.3.2-beta.2",
4
4
  "description": "",
5
5
  "author": "Allthings GmbH",
6
6
  "keywords": [