• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Lumieducation / H5P-Nodejs-library / 5761e0b3-9bed-478e-9d39-fb32070e15c2

pending completion
5761e0b3-9bed-478e-9d39-fb32070e15c2

push

circleci

renovate[bot]
chore(deps): lock file maintenance

4682 of 8584 branches covered (54.54%)

Branch coverage included in aggregate %.

8640 of 11846 relevant lines covered (72.94%)

2234.9 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

15.63
/packages/h5p-server/src/ContentHub.ts
1
import { AxiosInstance } from 'axios';
2

3
import HttpClient from './helpers/HttpClient';
9✔
4
import { IH5PConfig, IKeyValueStorage } from './types';
5
import H5pError from './helpers/H5pError';
9✔
6
import Logger from './helpers/Logger';
9✔
7

8
const log = new Logger('ContentHub');
9✔
9

10
export default class ContentHub {
9✔
11
    /**
12
     *
13
     * @param config The configuration to use.
14
     * @param storage The storage object.
15
     */
16
    constructor(private config: IH5PConfig, private storage: IKeyValueStorage) {
33✔
17
        log.info(`initialize`);
33✔
18
        this.httpClient = HttpClient(config);
33✔
19
    }
20

21
    private httpClient: AxiosInstance;
22

23
    getMetadata = async (lang?: string): Promise<any> => {
33✔
24
        const updateKey = lang
×
25
            ? `contentHubMetadataUpdate-${lang}`
26
            : 'contentHubMetadataUpdate';
27
        const metadataKey = lang
×
28
            ? `contentHubMetadata-${lang}`
29
            : 'contentHubMetadata';
30

31
        log.debug(`Getting content hub metadata for language ${lang}`);
×
32
        const lastUpdate = await this.storage.load(updateKey);
×
33

34
        if (lastUpdate) {
×
35
            log.debug(
×
36
                `Last hub metadata update was at ${new Date(
37
                    lastUpdate
38
                ).toUTCString()}`
39
            );
40
        } else {
41
            log.debug('Content hub metadata has never been retrieved before.');
×
42
        }
43

44
        if (
×
45
            lastUpdate &&
×
46
            lastUpdate >
47
                Date.now() - this.config.contentHubMetadataRefreshInterval
48
        ) {
49
            log.debug('No refresh from upstream necessary.');
×
50
            const cached = this.storage.load(metadataKey);
×
51
            if (cached) {
×
52
                return cached;
×
53
            }
54
            log.error('Wanted to use cached hub metadata, but none was found.');
×
55
        }
56

57
        let isoLanguage = lang;
×
58
        if (lang) {
×
59
            const match = lang.match(/^([a-zA-Z]{2,3})-?[a-zA-Z]{0,7}$/);
×
60
            if (match) {
×
61
                // eslint-disable-next-line prefer-destructuring
62
                isoLanguage = match[1];
×
63
            }
64
        }
65

66
        const response = await this.httpClient.get(
×
67
            lang
×
68
                ? `${this.config.contentHubMetadataEndpoint}?lang=${isoLanguage}`
69
                : this.config.contentHubMetadataEndpoint,
70
            {
71
                headers: lastUpdate
×
72
                    ? {
73
                          'If-Modified-Since': new Date(
74
                              lastUpdate
75
                          ).toUTCString()
76
                      }
77
                    : undefined,
78
                validateStatus: (status) => status === 200 || status === 304
×
79
            }
80
        );
81

82
        if (response.status === 304) {
×
83
            log.debug(
×
84
                'The server reported that there was no change in the content hub metadata'
85
            );
86
            this.storage.save(updateKey, Date.now());
×
87
            const cached = this.storage.load(metadataKey);
×
88
            if (cached) {
×
89
                return cached;
×
90
            }
91
            log.error(
×
92
                'Wanted to use cached content hub metadata, but none was found.'
93
            );
94
        }
95
        if (response.status === 200) {
×
96
            log.debug('Received content hub metadata. Storing in cache.');
×
97
            this.storage.save(metadataKey, response.data);
×
98
            this.storage.save(updateKey, Date.now());
×
99
            return response.data;
×
100
        }
101
        throw new H5pError('h5p-hub-connection-failed');
×
102
    };
103
}
9✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc