// ---> ---> utils/XMLTools.ts
import { Effect, Context, Layer } from 'effect/index';
import libxmljs, { XMLDocument } from "libxmljs";
const XSD = `
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
.....
</xs:schema>
`
class XMLParserError extends Error {
readonly _tag = "XMLParserError";
constructor () { super() }
}
class ValidateError extends Error {
readonly _tag = "ValidateError";
public errors: any[];
constructor (errors: any[]) {
super();
this.errors = errors;
}
}
const wrapHtmlWithCData = (xmlString: string): string => {
const htmlTagRegex = /<html>(.*?)<\/html>/gs;
return xmlString.replace(htmlTagRegex, (_, innerHtml: string) => `<html><![CDATA[${innerHtml}]]></html>`);
};
interface XMLParserService {
parser: (xml: string) => Effect.Effect<never, XMLParserError, XMLDocument>
validate: (xml: XMLDocument) => Effect.Effect<never, ValidateError, boolean>
}
const XMLParserService = Context.Tag<XMLParserService>();
export const XMLParserLayer = Layer.succeed(
XMLParserService,
XMLParserService.of({
parser: (xml: string) => Effect.tryPromise({
try: () => libxmljs.parseXmlAsync(xml),
catch: () => new XMLParserError(),
}),
validate: (xml: XMLDocument) => Effect.tryPromise({
try: () => libxmljs.parseXmlAsync(XSD)
.then(xsd => Boolean(xml.validate(xsd)))
.then(bool => {
if (bool) return bool;
else throw new ValidateError(xml.validationErrors);
}),
catch: () => new ValidateError(xml.validationErrors),
})
})
)
export const parserXML = (xmlData: string) =>
Effect.gen(function* (_) {
const XMLParser = yield* _(XMLParserService);
const resultXML = yield* _(XMLParser.parser(wrapHtmlWithCData(xmlData)));
yield* _( XMLParser.validate(resultXML) );
return resultXML;
})
// ---> ---> ---> api/slides/route.ts
export async function POST(request: NextRequest) {
if (!request.body) return NextResponse.json(new BodyIsEmptyError("Body пустое"))
const xml = await streamToText(request.body)
const result = await Effect.runPromise(
Effect.provideLayer(
parserXML(xml),
XMLParserLayer,
)
);
console.log(result);
return NextResponse.json({ message: "Hello" })
}
// ---> ---> Error
//Browser:
timeout.js:12 POST http://localhost:3000/api/slides 500 (Internal Server Error)
// Terminal
- error TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received undefined
at new NodeError (node:internal/errors:399:5)
at validateString (node:internal/validators:163:11)
at dirname (node:path:1276:5)
at Function.getRoot (webpack-internal:///(rsc)/./node_modules/.pnpm/bindings@1.3.1/node_modules/bindings/bindings.js:164:15)
at bindings (webpack-internal:///(rsc)/./node_modules/.pnpm/bindings@1.3.1/node_modules/bindings/bindings.js:94:36)
at eval (webpack-internal:///(rsc)/./node_modules/.pnpm/libxmljs@1.0.9/node_modules/libxmljs/dist/lib/bindings/index.js:7:16)
at (rsc)/./node_modules/.pnpm/libxmljs@1.0.9/node_modules/libxmljs/dist/lib/bindings/index.js (/Users/viooi/Documents/Code/NextJS/reveler/.next/server/app/api/slides/route.js:132:1)
at __webpack_require__ (/Users/viooi/Documents/Code/NextJS/reveler/.next/server/webpack-runtime.js:33:42)
at eval (webpack-internal:///(rsc)/./node_modules/.pnpm/libxmljs@1.0.9/node_modules/libxmljs/dist/lib/node.js:26:18)
at (rsc)/./node_modules/.pnpm/libxmljs@1.0.9/node_modules/libxmljs/dist/lib/node.js (/Users/viooi/Documents/Code/NextJS/reveler/.next/server/app/api/slides/route.js:172:1)
at __webpack_require__ (/Users/viooi/Documents/Code/NextJS/reveler/.next/server/webpack-runtime.js:33:42)
at eval (webpack-internal:///(rsc)/./node_modules/.pnpm/libxmljs@1.0.9/node_modules/libxmljs/dist/lib/document.js:158:14)
at (rsc)/./node_modules/.pnpm/libxmljs@1.0.9/node_modules/libxmljs/dist/lib/document.js (/Users/viooi/Documents/Code/NextJS/reveler/.next/server/app/api/slides/route.js:152:1)
at __webpack_require__ (/Users/viooi/Documents/Code/NextJS/reveler/.next/server/webpack-runtime.js:33:42)
at eval (webpack-internal:///(rsc)/./node_modules/.pnpm/libxmljs@1.0.9/node_modules/libxmljs/dist/lib/index.js:25:18)
at (rsc)/./node_modules/.pnpm/libxmljs@1.0.9/node_modules/libxmljs/dist/lib/index.js (/Users/viooi/Documents/Code/NextJS/reveler/.next/server/app/api/slides/route.js:162:1)
at __webpack_require__ (/Users/viooi/Documents/Code/NextJS/reveler/.next/server/webpack-runtime.js:33:42)
at eval (webpack-internal:///(rsc)/./node_modules/.pnpm/libxmljs@1.0.9/node_modules/libxmljs/dist/index.js:34:14)
at (rsc)/./node_modules/.pnpm/libxmljs@1.0.9/node_modules/libxmljs/dist/index.js (/Users/viooi/Documents/Code/NextJS/reveler/.next/server/app/api/slides/route.js:92:1)
at __webpack_require__ (/Users/viooi/Documents/Code/NextJS/reveler/.next/server/webpack-runtime.js:33:42)
at eval (webpack-internal:///(rsc)/./utils/XMLTools.ts:9:66)
at (rsc)/./utils/XMLTools.ts (/Users/viooi/Documents/Code/NextJS/reveler/.next/server/app/api/slides/route.js:762:1)
at __webpack_require__ (/Users/viooi/Documents/Code/NextJS/reveler/.next/server/webpack-runtime.js:33:42)
at eval (webpack-internal:///(rsc)/./app/api/slides/route.ts:5:73)
at (rsc)/./app/api/slides/route.ts (/Users/viooi/Documents/Code/NextJS/reveler/.next/server/app/api/slides/route.js:72:1)
at __webpack_require__ (/Users/viooi/Documents/Code/NextJS/reveler/.next/server/webpack-runtime.js:33:42)
at eval (webpack-internal:///(rsc)/./node_modules/.pnpm/next@13.4.19_react-dom@18.2.0_react@18.2.0/node_modules/next/dist/build/webpack/loaders/next-app-loader.js?name=app%2Fapi%2Fslides%2Froute&page=%2Fapi%2Fslides%2Froute&appPaths=&pagePath=private-next-app-dir%2Fapi%2Fslides%2Froute.ts&appDir=%2FUsers%2Fviooi%2FDocuments%2FCode%2FNextJS%2Freveler%2Fapp&pageExtensions=tsx&pageExtensions=ts&pageExtensions=jsx&pageExtensions=js&rootDir=%2FUsers%2Fviooi%2FDocuments%2FCode%2FNextJS%2Freveler&isDev=true&tsconfigPath=tsconfig.json&basePath=&assetPrefix=&nextConfigOutput=&preferredRegion=&middlewareConfig=e30%3D!:16:124)
at (rsc)/./node_modules/.pnpm/next@13.4.19_react-dom@18.2.0_react@18.2.0/node_modules/next/dist/build/webpack/loaders/next-app-loader.js?name=app%2Fapi%2Fslides%2Froute&page=%2Fapi%2Fslides%2Froute&appPaths=&pagePath=private-next-app-dir%2Fapi%2Fslides%2Froute.ts&appDir=%2FUsers%2Fviooi%2FDocuments%2FCode%2FNextJS%2Freveler%2Fapp&pageExtensions=tsx&pageExtensions=ts&pageExtensions=jsx&pageExtensions=js&rootDir=%2FUsers%2Fviooi%2FDocuments%2FCode%2FNextJS%2Freveler&isDev=true&tsconfigPath=tsconfig.json&basePath=&assetPrefix=&nextConfigOutput=&preferredRegion=&middlewareConfig=e30%3D! (/Users/viooi/Documents/Code/NextJS/reveler/.next/server/app/api/slides/route.js:62:1)
at __webpack_require__ (/Users/viooi/Documents/Code/NextJS/reveler/.next/server/webpack-runtime.js:33:42)
at __webpack_exec__ (/Users/viooi/Documents/Code/NextJS/reveler/.next/server/app/api/slides/route.js:2222:39)
at /Users/viooi/Documents/Code/NextJS/reveler/.next/server/app/api/slides/route.js:2223:28
at Object.<anonymous> (/Users/viooi/Documents/Code/NextJS/reveler/.next/server/app/api/slides/route.js:2226:3)
at Module._compile (node:internal/modules/cjs/loader:1254:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1308:10)
at Module.load (node:internal/modules/cjs/loader:1117:32)
at Module._load (node:internal/modules/cjs/loader:958:12)
at Module.require (node:internal/modules/cjs/loader:1141:19)
at require (node:internal/modules/cjs/helpers:110:18)
at requirePage (/Users/viooi/Documents/Code/NextJS/reveler/node_modules/.pnpm/next@13.4.19_react-dom@18.2.0_react@18.2.0/node_modules/next/dist/server/require.js:112:75)
at /Users/viooi/Documents/Code/NextJS/reveler/node_modules/.pnpm/next@13.4.19_react-dom@18.2.0_react@18.2.0/node_modules/next/dist/server/load-components.js:80:84
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async loadComponentsImpl (/Users/viooi/Documents/Code/NextJS/reveler/node_modules/.pnpm/next@13.4.19_react-dom@18.2.0_react@18.2.0/node_modules/next/dist/server/load-components.js:80:26)
at async DevServer.findPageComponentsImpl (/Users/viooi/Documents/Code/NextJS/reveler/node_modules/.pnpm/next@13.4.19_react-dom@18.2.0_react@18.2.0/node_modules/next/dist/server/next-server.js:439:36) {
digest: undefined
}