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

Lumieducation / H5P-Nodejs-library / 7a60f4f2-f2a2-4420-8516-1f893b349e16

16 Jun 2024 06:40AM UTC coverage: 65.662%. Remained the same
7a60f4f2-f2a2-4420-8516-1f893b349e16

Pull #3644

circleci

web-flow
chore(deps): update dependency json-autotranslate to v1.14.1
Pull Request #3644: chore(deps): update dependency json-autotranslate to v1.14.1

6261 of 10849 branches covered (57.71%)

Branch coverage included in aggregate %.

8641 of 11846 relevant lines covered (72.94%)

4597.12 hits per line

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

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

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

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

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

21
    private httpClient: AxiosInstance;
22

23
    getMetadata = async (lang?: string): Promise<any> => {
66✔
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
}
18✔
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