[d24f17c] | 1 | "use strict";
|
---|
| 2 |
|
---|
| 3 | var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault").default;
|
---|
| 4 | exports.__esModule = true;
|
---|
| 5 | exports.default = void 0;
|
---|
| 6 | var _stampit = _interopRequireDefault(require("stampit"));
|
---|
| 7 | var _apidomCore = require("@swagger-api/apidom-core");
|
---|
| 8 | var _ParserError = _interopRequireDefault(require("../../../errors/ParserError.cjs"));
|
---|
| 9 | var _Parser = _interopRequireDefault(require("../Parser.cjs"));
|
---|
| 10 | /**
|
---|
| 11 | * Everything that is not recognized by other parsers will be considered by this parser
|
---|
| 12 | * as a binary data and will be encoded to Base64 format.
|
---|
| 13 | */
|
---|
| 14 |
|
---|
| 15 | const BinaryParser = (0, _stampit.default)(_Parser.default, {
|
---|
| 16 | props: {
|
---|
| 17 | name: 'binary'
|
---|
| 18 | },
|
---|
| 19 | methods: {
|
---|
| 20 | async canParse(file) {
|
---|
| 21 | const hasSupportedFileExtension = this.fileExtensions.length === 0 ? true : this.fileExtensions.includes(file.extension);
|
---|
| 22 | return hasSupportedFileExtension;
|
---|
| 23 | },
|
---|
| 24 | async parse(file) {
|
---|
| 25 | try {
|
---|
| 26 | /**
|
---|
| 27 | * More information about binary strings and btoa function in following link:
|
---|
| 28 | * https://developer.mozilla.org/en-US/docs/Web/API/btoa
|
---|
| 29 | *
|
---|
| 30 | * @example
|
---|
| 31 | * ArrayBuffer to base64 conversion:
|
---|
| 32 | *
|
---|
| 33 | * const binaryString = String.fromCharCode.apply(null, file.data);
|
---|
| 34 | * base64String = btoa(binaryString);
|
---|
| 35 | */
|
---|
| 36 | const binaryString = unescape(encodeURIComponent(file.toString()));
|
---|
| 37 | const base64String = btoa(binaryString);
|
---|
| 38 | const parseResultElement = new _apidomCore.ParseResultElement();
|
---|
| 39 | if (base64String.length !== 0) {
|
---|
| 40 | const base64StringElement = new _apidomCore.StringElement(base64String);
|
---|
| 41 | base64StringElement.classes.push('result');
|
---|
| 42 | parseResultElement.push(base64StringElement);
|
---|
| 43 | }
|
---|
| 44 | return parseResultElement;
|
---|
| 45 | } catch (error) {
|
---|
| 46 | throw new _ParserError.default(`Error parsing "${file.uri}"`, {
|
---|
| 47 | cause: error
|
---|
| 48 | });
|
---|
| 49 | }
|
---|
| 50 | }
|
---|
| 51 | }
|
---|
| 52 | });
|
---|
| 53 | var _default = exports.default = BinaryParser; |
---|