Skip to content

Commit bfbbad1

Browse files
authored
Add Compression for bluegem.json (#319)
* Add compression for bluegem.json * omit original JSON in output * remove experimental flags * chore: run format * remove obsolete comment
1 parent 81ccb43 commit bfbbad1

File tree

5 files changed

+208
-6
lines changed

5 files changed

+208
-6
lines changed

package-lock.json

Lines changed: 179 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,14 @@
3131
"@eslint/eslintrc": "^3.3.1",
3232
"@eslint/js": "^9.24.0",
3333
"@types/chrome": "^0.0.313",
34+
"@types/compression-webpack-plugin": "^9.1.4",
3435
"@types/firefox-webext-browser": "^120.0.4",
3536
"@types/jquery": "^3.5.32",
3637
"@types/lodash": "^4.17.16",
38+
"@types/pako": "^2.0.3",
3739
"@typescript-eslint/eslint-plugin": "^8.29.1",
3840
"@typescript-eslint/parser": "^8.29.1",
41+
"compression-webpack-plugin": "^11.1.0",
3942
"copy-webpack-plugin": "^13.0.0",
4043
"csgo-fade-percentage-calculator": "^1.1.6",
4144
"css-loader": "^7.1.2",
@@ -55,6 +58,7 @@
5558
"lodash": "^4.17.21",
5659
"lodash-decorators": "^6.0.1",
5760
"mini-css-extract-plugin": "^2.9.2",
61+
"pako": "^2.1.0",
5862
"prettier": "^3.5.3",
5963
"rxjs": "^7.8.2",
6064
"sass-loader": "^16.0.5",

src/lib/bridge/handlers/fetch_bluegem.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {ItemInfo} from './fetch_inspect_info';
22
import {SimpleHandler} from './main';
33
import {RequestType} from './types';
4+
import {ungzip} from 'pako';
45

56
interface BluegemPatternData {
67
playside_blue: number;
@@ -39,16 +40,27 @@ export const FetchBluegem = new SimpleHandler<FetchBluegemRequest, FetchBluegemR
3940
}
4041

4142
if (Object.keys(bluegemCache).length === 0) {
42-
const url = chrome.runtime.getURL('data/bluegem.json');
43+
// Fetch the compressed data
44+
const url = chrome.runtime.getURL('data/bluegem.json.gz');
4345
try {
4446
const resp = await fetch(url);
45-
const json = await resp.json();
47+
if (!resp.ok) {
48+
throw new Error(`Failed to fetch bluegem data: ${resp.statusText}`);
49+
}
50+
51+
// Get the response as ArrayBuffer
52+
const compressedData = await resp.arrayBuffer();
53+
// Decompress the data using pako
54+
const decompressedData = ungzip(new Uint8Array(compressedData));
55+
// Parse the JSON from the decompressed buffer using TextDecoder
56+
const jsonString = new TextDecoder('utf-8').decode(decompressedData);
57+
const json = JSON.parse(jsonString);
4658
if (!json) {
47-
throw Error('Failed to fetch bluegem data');
59+
throw Error('Failed to parse bluegem data');
4860
}
4961
Object.assign(bluegemCache, json);
5062
} catch (e) {
51-
console.error('Failed to fetch bluegem data', e);
63+
console.error('Failed to fetch and process bluegem data', e);
5264
return undefined;
5365
}
5466
}

tsconfig.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
"rootDir": "src",
99
"outDir": "dist/js",
1010
"noEmitOnError": true,
11-
"typeRoots": ["node_modules/@types", "./src/lib/types"],
1211
"experimentalDecorators": true,
1312
"useDefineForClassFields": false,
1413
"moduleResolution": "Node"

webpack.config.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const path = require('path');
22
const glob = require('glob');
33
const CopyPlugin = require('copy-webpack-plugin');
44
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
5+
const CompressionPlugin = require("compression-webpack-plugin");
56
const webpack = require('webpack');
67
const resolve = require('path').resolve;
78

@@ -98,7 +99,7 @@ module.exports = (env) => {
9899
new CopyPlugin({
99100
patterns: [
100101
{from: 'icons', to: 'icons', context: '.'},
101-
{from: 'data', to: 'data', context: '.'},
102+
{from: 'data', to: 'data', context: '.', globOptions: { ignore: ['bluegem.json'] } },
102103
{from: 'src/global.css', to: 'src/', context: '.'},
103104
{from: 'src/background_ff.html', to: 'src/', context: '.'},
104105
{from: 'src/steamcommunity_ruleset.json', to: 'src/', context: '.'},
@@ -145,6 +146,13 @@ module.exports = (env) => {
145146
},
146147
],
147148
}),
149+
// Add Gzip compression for bluegem.json
150+
new CompressionPlugin({
151+
filename: "[path][base].gz", // Change extension to .gz
152+
algorithm: "gzip",
153+
test: /bluegem\.json$/,
154+
deleteOriginalAssets: true,
155+
}),
148156
],
149157
stats: {
150158
errorDetails: true,

0 commit comments

Comments
 (0)