Created
June 1, 2022 21:51
-
-
Save valterbarros/474e118c3652c5c3848a7f95581aa542 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class OldCaf { | |
constructor(param) { | |
const SdkImport = window.PassiveFaceLivenessSdk; | |
this._sdk = new SdkImport(param); | |
} | |
open() { | |
} | |
captureSelfie() { | |
} | |
} | |
class newCaf { | |
constructor(param) { | |
const { PassiveFaceLivenessSdk } = window['@combateafraude/passive-face-liveness']; | |
this._sdk = new PassiveFaceLivenessSdk(param); | |
this.token = param.token; | |
} | |
open(...args) { | |
console.log(args); | |
const sum = args.reduce((acc, curr) => acc += curr, 0) | |
return this._sdk.open(sum); | |
} | |
captureSelfie(...args) { | |
const arg1 = arguments[0]; | |
const arg3 = arguments[2]; | |
return `custom [[captureSelfie]] ${this.token}, ${arg1}, ${arg3}` | |
} | |
getOriginalSdkFunc(name) { | |
return this._sdk[name]; | |
} | |
} | |
function buildProxyCafSdk(proxyTarget) { | |
return new Proxy(proxyTarget, { | |
get(target, methodOrAttributeName) { | |
// if (methodOrAttributeName === 'open') { | |
// return target.open; | |
// } | |
// if (methodOrAttributeName === 'captureSelfie') { | |
// return target.captureSelfie; | |
// } | |
if ( | |
target.constructor.prototype.hasOwnProperty(methodOrAttributeName) || | |
target.hasOwnProperty(methodOrAttributeName) | |
) { | |
return Reflect.get(...arguments); | |
} | |
// Original class call | |
return target.getOriginalSdkFunc(methodOrAttributeName); | |
} | |
}); | |
} | |
afterEach(jest.clearAllMocks); | |
describe('Proxy for caf sdk', () => { | |
test('caf v4.4.0', () => { | |
window['@combateafraude/passive-face-liveness'] = { | |
PassiveFaceLivenessSdk: class PassiveFaceLivenessSdk { | |
constructor(param) { | |
this.token = param.token; | |
} | |
open(arg1) { | |
return `${arg1}, token: ${this.token}`; | |
} | |
captureSelfie(arg1, arg2, arg3) { | |
return `${captureSelfie}, ${arg1}, ${arg2}, ${arg3}`; | |
} | |
close(arg1, arg2, arg3) { | |
return `close: ${arg1}, ${arg2}, ${arg3}`; | |
} | |
otherMethod(arg1, arg2, arg3) { | |
console.log('otherMethod', arg1, arg2, arg3); | |
} | |
} | |
} | |
const params = { | |
'v4.4.0.params': { | |
token: '123', | |
appearenceSettings: { | |
hideCaptureTitle: true, | |
hideCameraSwitchButton: true, | |
hideCaptureMask: true, | |
}, | |
}, | |
'v3.2.1.params': { | |
sdkToken: '4321', | |
hideTitle: true, | |
sentry: { | |
dsn: null, | |
hideSwitchCameraButton: false, | |
}, | |
}, | |
} | |
const newCafInstance = new newCaf(params['v4.4.0.params']); | |
console.log(newCaf.prototype) | |
const p = buildProxyCafSdk(newCafInstance); | |
expect(p.open(1,2,3)).toEqual('6, token: 123'); | |
expect(() => p.noExist(1,2,3,10)).toThrowError(TypeError); | |
expect(p.captureSelfie(1,2,3)).toEqual('custom [[captureSelfie]] 123, 1, 3'); | |
expect(p.close(1,2,3)).toEqual('close: 1, 2, 3'); | |
// expect(commit) | |
// .toHaveBeenCalledTimes(1) | |
// .toHaveBeenCalledWith('SET_BRAND', mockPayload); | |
}); | |
test.todo('caf v3.2.1') | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class OldCaf { | |
static version = '3.2.1'; | |
constructor(param) { | |
const SdkImport = window.PassiveFaceLivenessSdk; | |
this._internalSdk = new SdkImport(param); | |
} | |
async open(...args) { | |
return await this._internalSdk.open(...[{}, ...args]); | |
} | |
async captureSelfie(...args) { | |
return await this._internalSdk.captureSelfie(...args); | |
} | |
getSdk() { | |
return this._internalSdk; | |
} | |
} | |
async function downloadAndConvertToBlob(imageUrl) { | |
const req = await fetch(imageUrl); | |
const buffer = await req.arrayBuffer(); | |
const blob = new Blob([buffer], { type: 'image/png' }); | |
return { blob, imageUrl }; | |
} | |
class NewCaf { | |
static version = '4.4.1'; | |
constructor(param) { | |
const { PassiveFaceLivenessSdk } = window['@combateafraude/passive-face-liveness']; | |
this._internalSdk = new PassiveFaceLivenessSdk(param); | |
} | |
async open(...args) { | |
return await this._internalSdk.open(...args); | |
} | |
async captureSelfie(...args) { | |
try { | |
const { imageUrl } = await this._internalSdk.captureSelfie(...args); | |
const blobConvert = await downloadAndConvertToBlob(imageUrl); | |
return blobConvert; | |
} catch (e) { | |
throw e; | |
} | |
} | |
getSdk() { | |
return this._internalSdk; | |
} | |
} | |
function buildProxyCafSdk(proxyTarget) { | |
return new Proxy(proxyTarget, { | |
get(target, methodOrAttributeName) { | |
if ( | |
target.constructor.prototype.hasOwnProperty(methodOrAttributeName) || | |
target.hasOwnProperty(methodOrAttributeName) | |
) { | |
return Reflect.get(...arguments); | |
} | |
// Original class call | |
return Reflect.get(target.getSdk(), methodOrAttributeName); | |
}, | |
set(target, prop, value) { | |
return Reflect.set(target.getSdk(), prop, value); | |
} | |
}); | |
} | |
/* eslint-disable no-param-reassign */ | |
import { ref, reactive } from '@vue/composition-api'; | |
function cafSdkWrapper(params, | |
// TOGGLE_USE_NEW_CAF_VERSION_ENABLED | |
// TODO: Remove toggleUseNewCafVersionEnabled param | |
toggleUseNewCafVersionEnabled) { | |
if (toggleUseNewCafVersionEnabled) { | |
// TOGGLE_USE_NEW_CAF_VERSION_ENABLED | |
// TODO: keep only the code from this if and remove other | |
const param = params['v4.4.0.params']; | |
const newCafInstance = new NewCaf(param); | |
const pSdk = buildProxyCafSdk(newCafInstance); | |
return pSdk; | |
} | |
const param = params['v3.2.1.params']; | |
const oldCafInstance = new OldCaf(param); | |
const pSdk = buildProxyCafSdk(oldCafInstance); | |
return pSdk; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment