main
Last change
on this file since 65b6638 was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago |
Initial commit
|
-
Property mode
set to
100644
|
File size:
1.4 KB
|
Line | |
---|
1 | import { Buffer } from '#buffer'; // eslint-disable-line import/order
|
---|
2 | import stampit from 'stampit';
|
---|
3 | import { ParseResultElement, StringElement } from '@swagger-api/apidom-core';
|
---|
4 | import ParserError from "../../../errors/ParserError.mjs";
|
---|
5 | import 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 | */
|
---|
10 | const 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 | });
|
---|
43 | export default BinaryParser; |
---|
Note:
See
TracBrowser
for help on using the repository browser.