Skip to content

Commit

Permalink
Merge pull request #1375 from opencomponents/compress-compiled-client
Browse files Browse the repository at this point in the history
compress compiled client
  • Loading branch information
ricardo-devis-agullo authored Sep 8, 2024
2 parents 8450cce + bed6925 commit 28489e4
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 7 deletions.
19 changes: 18 additions & 1 deletion src/registry/domain/options-sanitiser.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import zlib from 'node:zlib';
import { compileSync } from 'oc-client-browser';
import settings from '../../resources/settings';
import type { Config } from '../../types';
Expand Down Expand Up @@ -68,10 +69,26 @@ export default function optionsSanitiser(input: RegistryOptions): Config {
const clientOptions =
typeof options.compileClient === 'boolean' ? {} : options.compileClient;

options.compiledClient = compileSync({
const compiled = compileSync({
templates: options.templates,
...clientOptions
});
const minified = compiled.code;
const brotli = zlib.brotliCompressSync(minified, {
params: {
[zlib.constants.BROTLI_PARAM_QUALITY]: 1,
[zlib.constants.BROTLI_PARAM_SIZE_HINT]: minified.length
}
});
const gzip = zlib.gzipSync(minified, { level: 7 });
options.compiledClient = {
...compiled,
code: {
minified,
gzip,
brotli
}
};
}

if (!options.dependencies) {
Expand Down
22 changes: 22 additions & 0 deletions src/registry/middleware/compression.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import type { Request, Response } from 'express';

export default function compress(
data: { uncompressed: string; brotli: Buffer; gzip: Buffer },
req: Request,
res: Response
) {
const accept = req.headers['accept-encoding'];
const encoding =
typeof accept === 'string' &&
(accept.match(/\bbr\b/) || accept.match(/\bgzip\b/) || [])[0];

if (!encoding) {
res.send(data.uncompressed);
return;
}

res.setHeader('Content-Encoding', encoding);
const compressed = encoding === 'br' ? data.brotli : data.gzip;

res.send(compressed);
}
19 changes: 14 additions & 5 deletions src/registry/routes/static-redirector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import fs from 'fs-extra';

import * as storageUtils from 'oc-storage-adapters-utils';
import type { Repository } from '../domain/repository';
import compress from '../middleware/compression';

export default function staticRedirector(repository: Repository) {
return (req: Request, res: Response): void => {
Expand All @@ -28,11 +29,19 @@ export default function staticRedirector(repository: Repository) {
} else {
if (res.conf.compiledClient) {
res.type('application/javascript');
res.send(
req.route.path === clientPath
? res.conf.compiledClient.code
: res.conf.compiledClient.dev
);
if (req.route.path === clientPath) {
compress(
{
uncompressed: res.conf.compiledClient.code.minified,
brotli: res.conf.compiledClient.code.brotli,
gzip: res.conf.compiledClient.code.gzip
},
req,
res
);
} else {
res.send(res.conf.compiledClient.dev);
}
return;
}
res.redirect(
Expand Down
6 changes: 5 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,11 @@ export type PublishAuthConfig =

export interface Config<T = any> {
baseUrl: string;
compiledClient?: { code: string; map: string; dev: string };
compiledClient?: {
code: { gzip: Buffer; brotli: Buffer; minified: string };
map: string;
dev: string;
};
baseUrlFunc?: (opts: { host?: string; secure: boolean }) => string;
beforePublish: (req: Request, res: Response, next: NextFunction) => void;
customHeadersToSkipOnWeakVersion: string[];
Expand Down

0 comments on commit 28489e4

Please sign in to comment.