@8ms/helpers 1.3.27 → 1.3.28

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.
@@ -6,16 +6,14 @@ Object.defineProperty(exports, "__esModule", { value: true });
6
6
  const durations_1 = __importDefault(require("../../date/durations"));
7
7
  const readFile_1 = __importDefault(require("../s3/readFile"));
8
8
  const getKey_1 = __importDefault(require("./getKey"));
9
+ const getBrotliDecompressed_1 = __importDefault(require("../../util/getBrotliDecompressed"));
9
10
  /**
10
11
  * Get the stored cache if it exists.
11
12
  */
12
13
  const getCache = async ({ bucket, cache, fileName, folder, isJson }) => {
13
14
  let response = undefined;
14
15
  // Store the cache key
15
- const s3CacheKey = (0, getKey_1.default)({
16
- fileName,
17
- folder,
18
- });
16
+ const s3CacheKey = (0, getKey_1.default)({ fileName, folder });
19
17
  // If a cache timeframe is provided and we're not preloading this data
20
18
  if (cache) {
21
19
  // Try to get the cache
@@ -23,7 +21,9 @@ const getCache = async ({ bucket, cache, fileName, folder, isJson }) => {
23
21
  const cached = await (0, readFile_1.default)({ bucket, key: s3CacheKey, isJson });
24
22
  // Cache exists and is still within our caching timeframe
25
23
  if (undefined !== cached.body && (durations_1.default.FOREVER === cache.timeframe || cached.modified[cache.from][cache.timeframe] < cache.duration)) {
26
- response = cached.body;
24
+ response = await (0, getBrotliDecompressed_1.default)({
25
+ input: cached.body,
26
+ });
27
27
  }
28
28
  }
29
29
  catch (error) {
@@ -13,6 +13,6 @@ const getEnvironment_1 = __importDefault(require("../../environment/getEnvironme
13
13
  const getKey = ({ fileName, folder }) => {
14
14
  const fileNameString = (0, getSha256_1.default)({ input: fileName });
15
15
  const folderPath = (0, trimStart_1.default)('' === folder ? '' : `${folder}/`, '/');
16
- return `cache/${(0, getEnvironment_1.default)()}/${folderPath}${fileNameString}.json`;
16
+ return `cache/${(0, getEnvironment_1.default)()}/${folderPath}${fileNameString}.json.br`;
17
17
  };
18
18
  exports.default = getKey;
@@ -4,10 +4,14 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  const writeFile_1 = __importDefault(require("../s3/writeFile"));
7
+ const getBrotliCompressed_1 = __importDefault(require("../../util/getBrotliCompressed"));
7
8
  /**
8
9
  * Save the cache.
9
10
  */
10
11
  const saveCache = async ({ bucket, data, key }) => {
11
- await (0, writeFile_1.default)({ bucket, data, key });
12
+ const brotliData = await (0, getBrotliCompressed_1.default)({
13
+ input: data,
14
+ });
15
+ await (0, writeFile_1.default)({ bucket, data: brotliData, key });
12
16
  };
13
17
  exports.default = saveCache;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@8ms/helpers",
3
3
  "license": "UNLICENSED",
4
- "version": "1.3.27",
4
+ "version": "1.3.28",
5
5
  "repository": {
6
6
  "type": "git",
7
7
  "url": "git+https://github.com/8millionstories-organisation/8ms-helpers-ts.git"
@@ -0,0 +1,9 @@
1
+ type GetBrotliCompressed = {
2
+ input?: any;
3
+ options?: object;
4
+ };
5
+ /**
6
+ * Compress a given input using Brotli.
7
+ */
8
+ declare const getBrotliCompressed: ({ input, options }?: GetBrotliCompressed) => Promise<unknown>;
9
+ export default getBrotliCompressed;
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ /**
4
+ * Compress a given input using Brotli.
5
+ */
6
+ const getBrotliCompressed = async ({ input, options } = {}) => {
7
+ return new Promise(async (resolve, reject) => {
8
+ const zlib = require('zlib');
9
+ zlib.brotliCompress(input || '', options || {}, (err, data) => {
10
+ if (err) {
11
+ reject(`Error compressing - ${err.code}`);
12
+ }
13
+ else {
14
+ resolve(data.toString('base64'));
15
+ }
16
+ });
17
+ });
18
+ };
19
+ exports.default = getBrotliCompressed;
@@ -0,0 +1,9 @@
1
+ type GetBrotliDecompressed = {
2
+ input?: any;
3
+ options?: object;
4
+ };
5
+ /**
6
+ * Decompress a given Brotli input into a string.
7
+ */
8
+ declare const getBrotliDecompressed: ({ input, options }?: GetBrotliDecompressed) => Promise<unknown>;
9
+ export default getBrotliDecompressed;
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ /**
4
+ * Decompress a given Brotli input into a string.
5
+ */
6
+ const getBrotliDecompressed = async ({ input, options } = {}) => {
7
+ return new Promise(async (resolve, reject) => {
8
+ const zlib = require('zlib');
9
+ const compressedData = Buffer.from(input || '', "base64");
10
+ zlib.brotliDecompress(compressedData, options || {}, (err, data) => {
11
+ if (err) {
12
+ reject(`Error decompressing - ${err.code}`);
13
+ }
14
+ else {
15
+ resolve(data.toString('utf8'));
16
+ }
17
+ });
18
+ });
19
+ };
20
+ exports.default = getBrotliDecompressed;