Skip to content

Commit

Permalink
fix(h5p-server): added getLocalIdOverride to h5p editor options (#2251)
Browse files Browse the repository at this point in the history
* fix: added getLocalIdOverride to h5p editor options

* fix: optional options object

* refactor: regenerate package-lock
  • Loading branch information
sr258 authored Apr 30, 2022
1 parent d5f075a commit da2ce76
Show file tree
Hide file tree
Showing 8 changed files with 129 additions and 87 deletions.
98 changes: 45 additions & 53 deletions packages/h5p-express/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/h5p-express/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"types": "./build/index.d.ts",
"devDependencies": {
"@types/express": "4.17.13",
"@types/express-fileupload": "^1.2.2",
"@types/express-serve-static-core": "4.17.28",
"@types/supertest": "2.0.12",
"axios": "0.27.2",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export default function (
router.post(
h5pEditor.config.ajaxUrl,
catchAndPassOnErrors(
h5pController.postAjax,
h5pController.postAjax as any,
routeOptions.handleErrors
)
);
Expand Down
34 changes: 18 additions & 16 deletions packages/h5p-express/src/expressTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,22 @@ export interface IRequestWithUser extends Request {
}

export interface IActionRequest extends IRequestWithUser {
files: {
file: {
data?: Buffer;
mimetype: string;
name: string;
size: number;
tempFilePath?: string;
};
h5p: {
data?: Buffer;
mimetype: string;
name: string;
size: number;
tempFilePath?: string;
};
};
files:
| {
file: {
data?: Buffer;
mimetype: string;
name: string;
size: number;
tempFilePath?: string;
};
h5p: {
data?: Buffer;
mimetype: string;
name: string;
size: number;
tempFilePath?: string;
};
}
| any;
}
37 changes: 21 additions & 16 deletions packages/h5p-server/src/ContentTypeCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ export default class ContentTypeCache {
* @param config The configuration to use.
* @param storage The storage object.
*/
constructor(config: IH5PConfig, storage: IKeyValueStorage) {
log.info(`initialize`);
this.config = config;
this.storage = storage;
constructor(
private config: IH5PConfig,
private storage: IKeyValueStorage,
private getLocalIdOverride?: () => string
) {
log.info('initialize');
this.httpClient = HttpClient(config);
}

private config: IH5PConfig;
private storage: IKeyValueStorage;
private httpClient: AxiosInstance;

/**
Expand Down Expand Up @@ -79,15 +79,6 @@ export default class ContentTypeCache {
};
}

/**
* Creates an identifier for the running instance.
* @returns id
*/
private static generateLocalId(): string {
log.debug(`Generating local id`);
return machineIdSync();
}

/**
* Downloads information about available content types from the H5P Hub. This method will
* create a UUID to identify this site if required.
Expand Down Expand Up @@ -274,7 +265,7 @@ export default class ContentTypeCache {
core_api_version: `${this.config.coreApiVersion.major}.${this.config.coreApiVersion.minor}`,
disabled: this.config.fetchingDisabled,
h5p_version: this.config.h5pVersion,
local_id: ContentTypeCache.generateLocalId(),
local_id: this.getLocalId(),
platform_name: this.config.platformName,
platform_version: this.config.platformVersion,
type: this.config.siteType,
Expand All @@ -292,4 +283,18 @@ export default class ContentTypeCache {
num_authors: 0 // number of active authors
};
}

/**
* Creates an identifier for the running instance.
* @returns id
*/
private getLocalId(): string {
if (this.getLocalIdOverride) {
log.debug('Getting local ID from override');
return this.getLocalIdOverride();
} else {
log.debug('Generating local id with node-machine-id');
return machineIdSync();
}
}
}
6 changes: 5 additions & 1 deletion packages/h5p-server/src/H5PEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,11 @@ export default class H5PEditor {
this.config = config;

this.renderer = defaultRenderer;
this.contentTypeCache = new ContentTypeCache(config, cache);
this.contentTypeCache = new ContentTypeCache(
config,
cache,
this.options?.getLocalIdOverride
);
this.contentHub = new ContentHub(config, cache);
this.libraryManager = new LibraryManager(
libraryStorage,
Expand Down
12 changes: 12 additions & 0 deletions packages/h5p-server/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1969,6 +1969,18 @@ export interface IH5PEditorOptions {
user: IUser
) => Promise<void>;
};

/**
* Allows you to inject custom machine ids. This id is sent to the H5P.org
* Hub server when registering to get a UUID for the local instance or when
* refreshing the list of available content types.
*
* By default the server gets an ID for the local machine by calling
* node-machine-id, so you don't have to use the override. There are cases
* in which calling node-machine-id might not work, so you can use the
* override.
*/
getLocalIdOverride?: () => string;
}

/**
Expand Down
26 changes: 26 additions & 0 deletions packages/h5p-server/test/ContentTypeCache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ import InMemoryStorage from '../src/implementation/InMemoryStorage';
const axiosMock = new AxiosMockAdapter(axios);

describe('registering the site at H5P Hub', () => {
afterEach(() => {
axiosMock.reset();
});

it('returns a uuid', async () => {
const storage = new InMemoryStorage();
const config = new H5PConfig(storage);
Expand Down Expand Up @@ -48,6 +52,28 @@ describe('registering the site at H5P Hub', () => {
}
expect(error).toBeDefined();
});

it("get ids from external if there's an override", async () => {
const storage = new InMemoryStorage();
const config = new H5PConfig(storage);
const getIdSpy = jest.fn(() => 'overriden id');
const cache = new ContentTypeCache(config, storage, getIdSpy);
axiosMock.reset();
axiosMock
.onPost(config.hubRegistrationEndpoint)
.reply(
200,
fsExtra.readJSONSync(
path.resolve(
'test/data/content-type-cache/registration.json'
)
)
);
await expect(cache.registerOrGetUuid()).resolves.toEqual(
'8de62c47-f335-42f6-909d-2d8f4b7fb7f5'
);
expect(getIdSpy).toHaveBeenCalled();
});
});

describe('getting H5P Hub content types', () => {
Expand Down

0 comments on commit da2ce76

Please sign in to comment.