From a0d60b0de8b1eb48bb15a87aa5bf9628f1f74a3c Mon Sep 17 00:00:00 2001 From: Alexander Zakharov Date: Wed, 12 May 2021 20:53:44 +0300 Subject: [PATCH] add getPluginVersion & getCspVersion methods --- package-lock.json | 4 +-- src/api/getCspVersion.test.ts | 35 +++++++++++++++++++++++++ src/api/getCspVersion.ts | 40 +++++++++++++++++++++++++++++ src/api/getPluginVersion.test.ts | 35 +++++++++++++++++++++++++ src/api/getPluginVersion.ts | 44 ++++++++++++++++++++++++++++++++ src/api/index.ts | 2 ++ 6 files changed, 158 insertions(+), 2 deletions(-) create mode 100644 src/api/getCspVersion.test.ts create mode 100644 src/api/getCspVersion.ts create mode 100644 src/api/getPluginVersion.test.ts create mode 100644 src/api/getPluginVersion.ts diff --git a/package-lock.json b/package-lock.json index 4f223c8..7d7fd84 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { - "name": "crypto-pro", - "version": "2.2.0", + "name": "@gaarutyunov/crypto-pro-fork", + "version": "2.2.2", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/src/api/getCspVersion.test.ts b/src/api/getCspVersion.test.ts new file mode 100644 index 0000000..5cf8a58 --- /dev/null +++ b/src/api/getCspVersion.test.ts @@ -0,0 +1,35 @@ +import 'cadesplugin'; +import { getCspVersion } from './getCspVersion'; + +const executionSteps = [ + Symbol('step 0'), + Symbol('step 1'), + Symbol('step 2'), + Symbol('step 3'), + Symbol('step 4'), + Symbol('step 5'), +]; + +// "any" because of using toString on the literal +const executionFlow: any = { + [executionSteps[0]]: { + CSPVersion: jest.fn(() => executionSteps[1]), + }, + [executionSteps[1]]: { + toString: jest.fn(() => executionSteps[2]), + }, + [executionSteps[2]]: '4.0.9971', +}; + +window.cadesplugin.__defineExecutionFlow(executionFlow); +window.cadesplugin.CreateObjectAsync.mockImplementation( + () => executionSteps[0], +); + +describe('getCspVersion', () => { + test('returns information about CSP', async () => { + const cspVersion = await getCspVersion(); + + expect(cspVersion).toStrictEqual('4.0.9971'); + }); +}); diff --git a/src/api/getCspVersion.ts b/src/api/getCspVersion.ts new file mode 100644 index 0000000..49534f9 --- /dev/null +++ b/src/api/getCspVersion.ts @@ -0,0 +1,40 @@ +import { _afterPluginsLoaded } from '../helpers/_afterPluginsLoaded'; +import { _extractMeaningfulErrorMessage } from '../helpers/_extractMeaningfulErrorMessage'; +import { + __cadesAsyncToken__, + __createCadesPluginObject__, + _generateCadesFn, +} from '../helpers/_generateCadesFn'; + +/** + * Предоставляет информацию о системе + * + * @returns информацию о CSP + */ +export const getCspVersion = _afterPluginsLoaded((): string => { + let cspVersion = null; + + return eval( + _generateCadesFn(function getCspVersion(): string { + let cadesAbout; + + try { + cadesAbout = + __cadesAsyncToken__ + __createCadesPluginObject__('CAdESCOM.About'); + + cspVersion = __cadesAsyncToken__ + cadesAbout.CSPVersion(); + + cspVersion = __cadesAsyncToken__ + cspVersion.toString(); + } catch (error) { + console.error(error); + + throw new Error( + _extractMeaningfulErrorMessage(error) || + 'Ошибка при получении версии системы', + ); + } + + return cspVersion; + }), + ); +}); diff --git a/src/api/getPluginVersion.test.ts b/src/api/getPluginVersion.test.ts new file mode 100644 index 0000000..9952e3b --- /dev/null +++ b/src/api/getPluginVersion.test.ts @@ -0,0 +1,35 @@ +import 'cadesplugin'; +import { getPluginVersion } from './getPluginVersion'; + +const executionSteps = [ + Symbol('step 0'), + Symbol('step 1'), + Symbol('step 2'), + Symbol('step 3'), +]; + +// "any" because of using toString on the literal +const executionFlow: any = { + [executionSteps[0]]: { + PluginVersion: executionSteps[1], + Version: executionSteps[2], + }, + [executionSteps[1]]: undefined, + [executionSteps[2]]: { + toString: jest.fn(() => executionSteps[3]), + }, + [executionSteps[3]]: '2.0.13771', +}; + +window.cadesplugin.__defineExecutionFlow(executionFlow); +window.cadesplugin.CreateObjectAsync.mockImplementation( + () => executionSteps[0], +); + +describe('getPluginVersion', () => { + test('returns information about plugin', async () => { + const pluginVersion = await getPluginVersion(); + + expect(pluginVersion).toStrictEqual('2.0.13771'); + }); +}); diff --git a/src/api/getPluginVersion.ts b/src/api/getPluginVersion.ts new file mode 100644 index 0000000..7e3ff82 --- /dev/null +++ b/src/api/getPluginVersion.ts @@ -0,0 +1,44 @@ +import { _afterPluginsLoaded } from '../helpers/_afterPluginsLoaded'; +import { + __cadesAsyncToken__, + __createCadesPluginObject__, + _generateCadesFn, +} from '../helpers/_generateCadesFn'; +import { _extractMeaningfulErrorMessage } from '../helpers/_extractMeaningfulErrorMessage'; + +/** + * Предоставляет информацию о системе + * + * @returns информацию о плагине + */ +export const getPluginVersion = _afterPluginsLoaded((): string => { + let cadesVersion = null; + + return eval( + _generateCadesFn(function getPluginVersion(): string { + let cadesAbout; + + try { + cadesAbout = + __cadesAsyncToken__ + __createCadesPluginObject__('CAdESCOM.About'); + + cadesVersion = __cadesAsyncToken__ + cadesAbout.PluginVersion; + + if (!cadesVersion) { + cadesVersion = __cadesAsyncToken__ + cadesAbout.Version; + } + + cadesVersion = __cadesAsyncToken__ + cadesVersion.toString(); + } catch (error) { + console.error(error); + + throw new Error( + _extractMeaningfulErrorMessage(error) || + 'Ошибка при получении информации о плагине', + ); + } + + return cadesVersion; + }), + ); +}); diff --git a/src/api/index.ts b/src/api/index.ts index e428ff6..18eb907 100644 --- a/src/api/index.ts +++ b/src/api/index.ts @@ -10,3 +10,5 @@ export * from './createAttachedSignature'; export * from './addAttachedSignature'; export * from './createHash'; export * from './certificate'; +export * from './getCspVersion'; +export * from './getPluginVersion';