Skip to content

Commit

Permalink
feat: gzip compiler option
Browse files Browse the repository at this point in the history
  • Loading branch information
BCsabaEngine committed Aug 14, 2024
1 parent 3fcfdf0 commit c7e323b
Show file tree
Hide file tree
Showing 5 changed files with 175 additions and 80 deletions.
63 changes: 46 additions & 17 deletions demo/esp32/include/svelteesp32async.h

Large diffs are not rendered by default.

63 changes: 51 additions & 12 deletions demo/esp32/include/svelteesp32psychic.h

Large diffs are not rendered by default.

15 changes: 10 additions & 5 deletions src/commandLine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ interface ICopyFilesArguments {
outputfile: string;
espmethod: string;
define: string;
'no-gzip': boolean;
gzip: 'true' | 'false' | 'compiler';
etag: 'true' | 'false' | 'compiler';
help?: boolean;
}
Expand Down Expand Up @@ -36,10 +36,15 @@ export const cmdLine = parse<ICopyFilesArguments>(
description: 'Generated output file with path',
defaultValue: 'svelteesp32.h'
},
'no-gzip': {
type: Boolean,
description: 'Do not compress content with gzip',
defaultValue: false
gzip: {
type: (value) => {
if (value === 'true') return 'true';
if (value === 'false') return 'false';
if (value === 'compiler') return 'compiler';
throw new Error(`Invalid etag: ${value}`);
},
description: 'Compress content with gzip',
defaultValue: 'true'
},
etag: {
type: (value) => {
Expand Down
46 changes: 42 additions & 4 deletions src/cppCode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export type CppCodeSource = {
datanameUpperCase: string;
mime: string;
content: Buffer;
contentGzip: Buffer;
isGzip: boolean;
md5: string;
};
Expand Down Expand Up @@ -36,6 +37,12 @@ const psychicTemplate = `
{{#ifeq etag "false" }}
#undef {{definePrefix}}_ENABLE_ETAG
{{/ifeq}}
{{#ifeq gzip "true" }}
#define {{definePrefix}}_ENABLE_GZIP
{{/ifeq}}
{{#ifeq gzip "false" }}
#undef {{definePrefix}}_ENABLE_GZIP
{{/ifeq}}
#define {{definePrefix}}_COUNT {{fileCount}}
#define {{definePrefix}}_SIZE {{fileSize}}
Expand All @@ -48,9 +55,15 @@ const psychicTemplate = `
#define {{../definePrefix}}_{{this.extension}}_FILES {{this.count}}
{{/each}}
#ifdef {{definePrefix}}_ENABLE_GZIP
{{#each sources}}
const uint8_t datagz_{{this.dataname}}[{{this.lengthGzip}}] = { {{this.bytesGzip}} };
{{/each}}
#else
{{#each sources}}
const uint8_t data_{{this.dataname}}[{{this.length}}] = { {{this.bytes}} };
{{/each}}
#endif
#ifdef {{definePrefix}}_ENABLE_ETAG
{{#each sources}}
Expand All @@ -70,13 +83,19 @@ void {{methodName}}(PsychicHttpServer * server) {
#endif
PsychicResponse response(request);
response.setContentType("{{this.mime}}");
#ifdef {{../definePrefix}}_ENABLE_GZIP
{{#if this.isGzip}}
response.addHeader("Content-Encoding", "gzip");
{{/if}}
#endif
#ifdef {{../definePrefix}}_ENABLE_ETAG
response.addHeader("ETag", etag_{{this.dataname}});
#endif
response.setContent(data_{{this.dataname}}, sizeof(data_{{this.dataname}}));
#ifdef {{../definePrefix}}_ENABLE_GZIP
response.setContent(datagz_{{this.dataname}}, {{this.lengthGzip}});
#else
response.setContent(data_{{this.dataname}}, {{this.length}});
#endif
return response.send();
});
Expand All @@ -99,6 +118,12 @@ const asyncTemplate = `
{{#ifeq etag "false" }}
#undef {{definePrefix}}_ENABLE_ETAG
{{/ifeq}}
{{#ifeq gzip "true" }}
#define {{definePrefix}}_ENABLE_GZIP
{{/ifeq}}
{{#ifeq gzip "false" }}
#undef {{definePrefix}}_ENABLE_GZIP
{{/ifeq}}
#define {{definePrefix}}_COUNT {{fileCount}}
#define {{definePrefix}}_SIZE {{fileSize}}
Expand All @@ -111,9 +136,15 @@ const asyncTemplate = `
#define {{../definePrefix}}_{{this.extension}}_FILES {{this.count}}
{{/each}}
#ifdef {{definePrefix}}_ENABLE_GZIP
{{#each sources}}
const uint8_t data_{{this.dataname}}[{{this.length}}] = { {{this.bytes}} };
const uint8_t datagz_{{this.dataname}}[{{this.lengthGzip}}] PROGMEM = { {{this.bytesGzip}} };
{{/each}}
#else
{{#each sources}}
const uint8_t data_{{this.dataname}}[{{this.length}}] PROGMEM = { {{this.bytes}} };
{{/each}}
#endif
#ifdef {{definePrefix}}_ENABLE_ETAG
{{#each sources}}
Expand All @@ -129,11 +160,15 @@ void {{methodName}}(AsyncWebServer * server) {
request->send(304);
return;
}
#endif
AsyncWebServerResponse *response = request->beginResponse_P(200, "{{this.mime}}", data_{{this.dataname}}, {{this.length}});
#endif
#ifdef {{../definePrefix}}_ENABLE_GZIP
AsyncWebServerResponse *response = request->beginResponse_P(200, "{{this.mime}}", datagz_{{this.dataname}}, {{this.lengthGzip}});
{{#if this.isGzip}}
response->addHeader("Content-Encoding", "gzip");
{{/if}}
#else
AsyncWebServerResponse *response = request->beginResponse_P(200, "{{this.mime}}", data_{{this.dataname}}, {{this.length}});
#endif
#ifdef {{../definePrefix}}_ENABLE_ETAG
response->addHeader("ETag", etag_{{this.dataname}});
#endif
Expand All @@ -158,10 +193,13 @@ export const getCppCode = (sources: CppCodeSources, filesByExtension: ExtensionG
...s,
length: s.content.length,
bytes: [...s.content].map((v) => `0x${v.toString(16)}`).join(', '),
lengthGzip: s.contentGzip.length,
bytesGzip: [...s.contentGzip].map((v) => `0x${v.toString(16)}`).join(', '),
isDefault: s.filename.startsWith('index.htm')
})),
filesByExtension,
etag: cmdLine.etag,
gzip: cmdLine.gzip,
methodName: cmdLine.espmethod,
definePrefix: cmdLine.define
},
Expand Down
68 changes: 26 additions & 42 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,61 +38,45 @@ for (const file of files) {
if (group) group.count += 1;
else filesByExtension.push({ extension, count: 1 });

const rawContent = readFileSync(path.join(cmdLine.sourcepath, file), { flag: 'r' });
const md5 = createHash('md5').update(rawContent).digest('hex');
summary.size += rawContent.length;
if (cmdLine['no-gzip']) {
const content = readFileSync(path.join(cmdLine.sourcepath, file), { flag: 'r' });
const md5 = createHash('md5').update(content).digest('hex');
summary.size += content.length;
const zipContent = gzipSync(content, { level: 9 });
summary.gzipsize += zipContent.length;
if (content.length > 100 && zipContent.length < content.length * 0.85) {
sources.push({
filename: filename,
filename,
dataname,
datanameUpperCase: dataname.toUpperCase(),
content: rawContent,
isGzip: false,
content,
contentGzip: zipContent,
isGzip: true,
mime,
md5
});
console.log(`- gzip skipped (${rawContent.length})`);
console.log(` gzip used (${content.length} -> ${zipContent.length})`);
} else {
const zipContent = gzipSync(rawContent, { level: 9 });
summary.gzipsize += zipContent.length;
if (rawContent.length > 100 && zipContent.length < rawContent.length * 0.85) {
sources.push({
filename: filename,
dataname,
datanameUpperCase: dataname.toUpperCase(),
content: zipContent,
isGzip: true,
mime,
md5
});
console.log(`✓ gzip used (${rawContent.length} -> ${zipContent.length})`);
} else {
sources.push({
filename: filename,
dataname,
datanameUpperCase: dataname.toUpperCase(),
content: rawContent,
isGzip: false,
mime,
md5
});
console.log(`x gzip unused (${rawContent.length} -> ${zipContent.length})`);
}
sources.push({
filename,
dataname,
datanameUpperCase: dataname.toUpperCase(),
content,
contentGzip: content,
isGzip: false,
mime,
md5
});
console.log(`x gzip unused (${content.length} -> ${zipContent.length})`);
}

console.log('');
}
console.log('');
filesByExtension.sort((left, right) => left.extension.localeCompare(right.extension));

const cppFile = getCppCode(sources, filesByExtension);
writeFileSync(cmdLine.outputfile, cppFile);

if (cmdLine['no-gzip']) {
console.log(`${summary.filecount} files, ${Math.round(summary.size / 1024)}kB original size`);
} else {
console.log(
`${summary.filecount} files, ${Math.round(summary.size / 1024)}kB original size, ${Math.round(summary.gzipsize / 1024)}kB gzip size`
);
}
console.log(
`${summary.filecount} files, ${Math.round(summary.size / 1024)}kB original size, ${Math.round(summary.gzipsize / 1024)}kB gzip size`
);

console.log(`${cmdLine.outputfile} ${Math.round(cppFile.length / 1024)}kB size`);

0 comments on commit c7e323b

Please sign in to comment.