@opra/common
Version:
Opra common package
42 lines (41 loc) • 1.18 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 {
constructor(attributes) {
if (attributes)
Object.assign(this, attributes);
}
[DECODER](properties) {
const fn = vg.isBase64({ coerce: true });
if (properties.convertToNative)
return vg.pipe([fn, toBuffer], { 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 fromBuffer = validator((input) => {
if (Buffer.isBuffer(input))
return input.toString('base64');
else {
return isBase64(input);
}
});