source: node_modules/@swagger-api/apidom-reference/es/parse/parsers/binary/index-node.mjs

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

Initial commit

  • Property mode set to 100644
File size: 1.4 KB
RevLine 
[d24f17c]1import { Buffer } from '#buffer'; // eslint-disable-line import/order
2import stampit from 'stampit';
3import { ParseResultElement, StringElement } from '@swagger-api/apidom-core';
4import ParserError from "../../../errors/ParserError.mjs";
5import Parser from "../Parser.mjs";
6/**
7 * Everything that is not recognized by other parsers will be considered by this parser
8 * as a binary data and will be encoded to Base64 format.
9 */
10const BinaryParser = stampit(Parser, {
11 props: {
12 name: 'binary'
13 },
14 methods: {
15 async canParse(file) {
16 const hasSupportedFileExtension = this.fileExtensions.length === 0 ? true : this.fileExtensions.includes(file.extension);
17 return hasSupportedFileExtension;
18 },
19 async parse(file) {
20 let base64String;
21 try {
22 // @ts-ignore
23 base64String = Buffer.from(file.data).toString('base64');
24 } catch {
25 base64String = Buffer.from(file.toString()).toString('base64');
26 }
27 try {
28 const parseResultElement = new ParseResultElement();
29 if (base64String.length !== 0) {
30 const base64StringElement = new StringElement(base64String);
31 base64StringElement.classes.push('result');
32 parseResultElement.push(base64StringElement);
33 }
34 return parseResultElement;
35 } catch (error) {
36 throw new ParserError(`Error parsing "${file.uri}"`, {
37 cause: error
38 });
39 }
40 }
41 }
42});
43export default BinaryParser;
Note: See TracBrowser for help on using the repository browser.