@opra/common
Version:
Opra common package
48 lines (47 loc) • 1.43 kB
JavaScript
import { __decorate, __metadata } from "tslib";
import { isBase64, validator, vg } from 'valgen';
import { DECODER, ENCODER } from '../../constants.js';
import { SimpleType } from '../simple-type.js';
let Base64Type = class Base64Type {
designType;
constructor(attributes) {
if (attributes)
Object.assign(this, attributes);
}
[DECODER](properties) {
const fn = vg.isBase64({ coerce: true });
if (properties.designType === Buffer)
return vg.pipe([fn, toBuffer], { coerce: true });
if (properties.designType === Uint8Array)
return vg.pipe([fn, toUint8Array], { coerce: true });
return fn;
}
[ENCODER]() {
return fromBuffer;
}
};
Base64Type = __decorate([
SimpleType({
name: 'base64',
description: 'A stream of bytes, base64 encoded',
nameMappings: {
js: 'string',
json: 'string',
},
}),
__metadata("design:paramtypes", [Object])
], Base64Type);
export { Base64Type };
const toBuffer = validator((base64String) => {
return Buffer.from(base64String, 'base64');
});
const toUint8Array = validator((base64String) => {
return new Uint8Array(Buffer.from(base64String, 'base64'));
});
const fromBuffer = validator((input) => {
if (Buffer.isBuffer(input))
return input.toString('base64');
else {
return isBase64(input);
}
});