MCPcopy Create free account
hub / github.com/PacktPublishing/Building-Your-Own-JavaScript-Framework /

Class

chapter2/hapi-libs/compression.js:15–119  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

13
14
15exports = module.exports = internals.Compression = class {
16
17 decoders = {
18 gzip: (options) => Zlib.createGunzip(options),
19 deflate: (options) => Zlib.createInflate(options)
20 };
21
22 encodings = ['identity', 'gzip', 'deflate'];
23
24 encoders = {
25 identity: null,
26 gzip: (options) => Zlib.createGzip(options),
27 deflate: (options) => Zlib.createDeflate(options)
28 };
29
30 #common = null;
31
32 constructor() {
33
34 this._updateCommons();
35 }
36
37 _updateCommons() {
38
39 this.#common = new Map();
40
41 for (const header of internals.common) {
42 this.#common.set(header, Accept.encoding(header, this.encodings));
43 }
44 }
45
46 addEncoder(encoding, encoder) {
47
48 Hoek.assert(this.encoders[encoding] === undefined, `Cannot override existing encoder for ${encoding}`);
49 Hoek.assert(typeof encoder === 'function', `Invalid encoder function for ${encoding}`);
50 this.encoders[encoding] = encoder;
51 this.encodings.unshift(encoding);
52 this._updateCommons();
53 }
54
55 addDecoder(encoding, decoder) {
56
57 Hoek.assert(this.decoders[encoding] === undefined, `Cannot override existing decoder for ${encoding}`);
58 Hoek.assert(typeof decoder === 'function', `Invalid decoder function for ${encoding}`);
59 this.decoders[encoding] = decoder;
60 }
61
62 accept(request) {
63
64 const header = request.headers['accept-encoding'];
65 if (!header) {
66 return 'identity';
67 }
68
69 const common = this.#common.get(header);
70 if (common) {
71 return common;
72 }

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected