This repository has been archived by the owner on Feb 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(utils): add cache-utils for merging cache-control headers
- Loading branch information
Showing
4 changed files
with
156 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* Copyright 2020 Adobe. All rights reserved. | ||
* This file is licensed to you under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. You may obtain a copy | ||
* of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under | ||
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS | ||
* OF ANY KIND, either express or implied. See the License for the specific language | ||
* governing permissions and limitations under the License. | ||
*/ | ||
function directives(expression = '') { | ||
const retval = expression.split(',') | ||
.map((s) => s.trim()) | ||
.filter((s) => !!s) | ||
.map((s) => s.split('=')) | ||
.map(([ | ||
directive, | ||
value]) => [directive, Number.isNaN(Number.parseInt(value, 10)) | ||
? true | ||
: Number.parseInt(value, 10)]) | ||
.reduce((obj, [directive, value]) => { | ||
obj[directive] = value; | ||
return obj; | ||
}, {}); | ||
return retval; | ||
} | ||
|
||
function format(dirs = {}) { | ||
return Object.entries(dirs) | ||
.map(([directive, value]) => ((value === true || value === 1) ? directive : `${directive}=${value}`)) | ||
.join(', '); | ||
} | ||
|
||
function merge(in1 = '', in2 = '') { | ||
const dirs1 = typeof in1 === 'string' ? directives(in1) : in1; | ||
const dirs2 = typeof in2 === 'string' ? directives(in2) : in2; | ||
|
||
const keys = [...Object.keys(dirs1), ...Object.keys(dirs2)]; | ||
|
||
const mergeval = keys.reduce((merged, key) => { | ||
merged[key] = Math.min( | ||
dirs1[key] || Number.MAX_SAFE_INTEGER, | ||
dirs2[key] || Number.MAX_SAFE_INTEGER, | ||
); | ||
return merged; | ||
}, {}); | ||
|
||
return typeof in1 === 'string' ? format(mergeval) : mergeval; | ||
} | ||
|
||
module.exports = { directives, format, merge }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
* Copyright 2020 Adobe. All rights reserved. | ||
* This file is licensed to you under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. You may obtain a copy | ||
* of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under | ||
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS | ||
* OF ANY KIND, either express or implied. See the License for the specific language | ||
* governing permissions and limitations under the License. | ||
*/ | ||
/* eslint-env mocha */ | ||
const assert = require('assert'); | ||
const { directives, format, merge } = require('../src/utils/cache-helper'); | ||
|
||
describe('Cache Helper Tests (surrogate)', () => { | ||
it('directive parses directives', () => { | ||
assert.deepStrictEqual(directives('max-age=300'), { | ||
'max-age': 300, | ||
}); | ||
|
||
assert.deepStrictEqual(directives('s-maxage=300, max-age=300'), { | ||
's-maxage': 300, | ||
'max-age': 300, | ||
}); | ||
|
||
assert.deepStrictEqual(directives('s-maxage=300, max-age=300, public'), { | ||
's-maxage': 300, | ||
'max-age': 300, | ||
public: true, | ||
}); | ||
|
||
assert.deepStrictEqual(directives(''), {}); | ||
|
||
assert.deepStrictEqual(directives(), {}); | ||
}); | ||
|
||
it('format formats directives', () => { | ||
assert.equal(format({}), ''); | ||
assert.equal(format(undefined), ''); | ||
|
||
assert.equal(format({ | ||
'max-age': 300, | ||
public: true, | ||
}), 'max-age=300, public'); | ||
}); | ||
|
||
it('merge merges two directives', () => { | ||
assert.deepEqual(merge({}, {}), {}); | ||
|
||
assert.deepEqual(merge({ | ||
'max-age': 300, | ||
}, { | ||
's-maxage': 300, | ||
}), { | ||
's-maxage': 300, | ||
'max-age': 300, | ||
}); | ||
|
||
assert.deepEqual(merge({ | ||
'max-age': 300, | ||
's-maxage': 600, | ||
}, { | ||
's-maxage': 300, | ||
'max-age': 600, | ||
}), { | ||
's-maxage': 300, | ||
'max-age': 300, | ||
}); | ||
|
||
assert.deepEqual(merge({ | ||
public: true, | ||
}, { | ||
private: true, | ||
}), { | ||
public: true, | ||
private: true, | ||
}); | ||
|
||
assert.equal(merge('max-age=300, public', 'max-age=600'), 'max-age=300, public'); | ||
|
||
assert.equal(merge(), ''); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters