source: node_modules/@swagger-api/apidom-reference/cjs/parse/parsers/binary/index-browser.cjs@ d24f17c

main
Last change on this file since d24f17c was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago

Initial commit

  • Property mode set to 100644
File size: 2.0 KB
Line 
1"use strict";
2
3var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault").default;
4exports.__esModule = true;
5exports.default = void 0;
6var _stampit = _interopRequireDefault(require("stampit"));
7var _apidomCore = require("@swagger-api/apidom-core");
8var _ParserError = _interopRequireDefault(require("../../../errors/ParserError.cjs"));
9var _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
15const 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});
53var _default = exports.default = BinaryParser;
Note: See TracBrowser for help on using the repository browser.