UNPKG

26.1 kBSource Map (JSON)View Raw
1{"version":3,"file":"server-opds-convert-v1-to-v2.js","sourceRoot":"","sources":["../../../../src/http/server-opds-convert-v1-to-v2.ts"],"names":[],"mappings":";;;;AAOA,mCAAqC;AACrC,8BAAgC;AAChC,kCAAoC;AACpC,iCAAmC;AACnC,wCAA0C;AAC1C,+BAAiC;AACjC,2BAA6B;AAC7B,iCAAmC;AACnC,uDAAyD;AACzD,+BAAiC;AAEjC,wDAA0D;AAC1D,wDAEoC;AACpC,oDAAmD;AACnD,gEAA0D;AAG1D,8DAE2C;AAC3C,2DAAoE;AACpE,sEAA+E;AAC/E,mEAAwD;AAExD,sEAAmE;AACnE,6CAAsE;AAEtE,mFAAyE;AAEzE,IAAM,KAAK,GAAG,MAAM,CAAC,+CAA+C,CAAC,CAAC;AAGzD,QAAA,gCAAgC,GAAG,qBAAqB,CAAC;AACtE,SAAgB,2BAA2B,CAAC,OAAe,EAAE,SAA8B;IAA3F,iBA4VC;IAzVG,IAAM,SAAS,GAAG,0VAsBrB,CAAC;IAGE,IAAM,2BAA2B,GAAG,OAAO,CAAC,MAAM,CAAC,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,CAAC;IACtE,2BAA2B,CAAC,GAAG,CAAC,MAAM,CAAC,UAAU,EAAE,EAAE,MAAM,EAAE,EAAE,KAAK,EAAE,UAAC,GAAQ,IAAK,OAAA,KAAK,CAAC,GAAG,CAAC,EAAV,CAAU,EAAE,EAAE,CAAC,CAAC,CAAC;IAErG,2BAA2B,CAAC,GAAG,CAAC,sDAAqB,CAAC,CAAC;IAEvD,2BAA2B,CAAC,GAAG,CAAC,GAAG,EAAE,UAAC,IAAqB,EAAE,GAAqB;QAE9E,IAAI,IAAI,GAAG,cAAc,CAAC;QAC1B,IAAI,IAAI,8EAA4E;YAChF,8DAA8D;YAC9D,oDAAkD;YAClD,oBAAoB;YACpB,+CAA+C;YAC/C,mBAAmB;aAGnB,OAAK,wCAAgC,SAAM,CAAA;YAC3C,s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,EAAA;;oBALF,QAAQ,GAAG,SAKT,CAAC;;;;oBAEH,OAAO,CAAC,KAAG,CAAC,CAAC;oBACb,WAAO;wBAGX,WAAM,OAAO,CAAC,QAAQ,CAAC,EAAA;;oBAAvB,SAAuB,CAAC;;;;;SAE/B,CAAC,CAAC;IAEH,SAAS,CAAC,GAAG,CAAC,wCAAgC,EAAE,2BAA2B,CAAC,CAAC;AACjF,CAAC;AA5VD,kEA4VC","sourcesContent":["// ==LICENSE-BEGIN==\n// Copyright 2017 European Digital Reading Lab. All rights reserved.\n// Licensed to the Readium Foundation under one or more contributor license agreements.\n// Use of this source code is governed by a BSD-style license\n// that can be found in the LICENSE file exposed on Github (readium) in the project repository.\n// ==LICENSE-END==\n\nimport * as css2json from \"css2json\";\nimport * as debug_ from \"debug\";\nimport * as DotProp from \"dot-prop\";\nimport * as express from \"express\";\nimport * as jsonMarkup from \"json-markup\";\nimport * as morgan from \"morgan\";\nimport * as path from \"path\";\nimport * as request from \"request\";\nimport * as requestPromise from \"request-promise-native\";\nimport * as xmldom from \"xmldom\";\n\nimport { TaJsonSerialize } from \"@r2-lcp-js/serializable\";\nimport {\n convertOpds1ToOpds2, convertOpds1ToOpds2_EntryToPublication,\n} from \"@r2-opds-js/opds/converter\";\nimport { OPDS } from \"@r2-opds-js/opds/opds1/opds\";\nimport { Entry } from \"@r2-opds-js/opds/opds1/opds-entry\";\nimport { OPDSFeed } from \"@r2-opds-js/opds/opds2/opds2\";\nimport { OPDSPublication } from \"@r2-opds-js/opds/opds2/opds2-publication\";\nimport {\n encodeURIComponent_RFC3986, ensureAbsolute, isHTTP,\n} from \"@r2-utils-js/_utils/http/UrlUtils\";\nimport { traverseJsonObjects } from \"@r2-utils-js/_utils/JsonUtils\";\nimport { streamToBufferPromise } from \"@r2-utils-js/_utils/stream/BufferUtils\";\nimport { XML } from \"@r2-utils-js/_utils/xml-js-mapper\";\n\nimport { jsonSchemaValidate } from \"../utils/json-schema-validate\";\nimport { IRequestPayloadExtension, _urlEncoded } from \"./request-ext\";\nimport { Server } from \"./server\";\nimport { trailingSlashRedirect } from \"./server-trailing-slash-redirect\";\n\nconst debug = debug_(\"r2:streamer#http/server-opds-convert-v1-to-v2\");\n\n// tslint:disable-next-line:variable-name\nexport const serverOPDS_convert_v1_to_v2_PATH = \"/opds-v1-v2-convert\";\nexport function serverOPDS_convert_v1_to_v2(_server: Server, topRouter: express.Application) {\n\n // https://github.com/mafintosh/json-markup/blob/master/style.css\n const jsonStyle = `\n.json-markup {\n line-height: 17px;\n font-size: 13px;\n font-family: monospace;\n white-space: pre;\n}\n.json-markup-key {\n font-weight: bold;\n}\n.json-markup-bool {\n color: firebrick;\n}\n.json-markup-string {\n color: green;\n}\n.json-markup-null {\n color: gray;\n}\n.json-markup-number {\n color: blue;\n}\n`;\n\n // tslint:disable-next-line:variable-name\n const routerOPDS_convert_v1_to_v2 = express.Router({ strict: false });\n routerOPDS_convert_v1_to_v2.use(morgan(\"combined\", { stream: { write: (msg: any) => debug(msg) } }));\n\n routerOPDS_convert_v1_to_v2.use(trailingSlashRedirect);\n\n routerOPDS_convert_v1_to_v2.get(\"/\", (_req: express.Request, res: express.Response) => {\n\n let html = \"<html><head>\";\n html += `<script type=\"text/javascript\">function encodeURIComponent_RFC3986(str) { ` +\n `return encodeURIComponent(str).replace(/[!'()*]/g, (c) => { ` +\n `return \"%\" + c.charCodeAt(0).toString(16); }); }` +\n `function go(evt) {` +\n `if (evt) { evt.preventDefault(); } var url = ` +\n `location.origin +` +\n // `location.protocol + '//' + location.hostname + ` +\n // `(location.port ? (':' + location.port) : '') + ` +\n ` '${serverOPDS_convert_v1_to_v2_PATH}/' +` +\n ` encodeURIComponent_RFC3986(document.getElementById(\"url\").value);` +\n `location.href = url;}</script>`;\n html += \"</head>\";\n\n html += \"<body><h1>OPDS 1 -> 2 converter</h1>\";\n\n html += `<form onsubmit=\"go();return false;\">` +\n `<input type=\"text\" name=\"url\" id=\"url\" size=\"80\">` +\n `<input type=\"submit\" value=\"Go!\"></form>`;\n\n html += \"</body></html>\";\n\n res.status(200).send(html);\n });\n\n routerOPDS_convert_v1_to_v2.param(\"urlEncoded\", (req, _res, next, value, _name) => {\n (req as IRequestPayloadExtension).urlEncoded = value;\n next();\n });\n\n routerOPDS_convert_v1_to_v2.get(\"/:\" + _urlEncoded + \"(*)\",\n async (req: express.Request, res: express.Response) => {\n\n const reqparams = (req as IRequestPayloadExtension).params;\n\n if (!reqparams.urlEncoded) {\n reqparams.urlEncoded = (req as IRequestPayloadExtension).urlEncoded;\n }\n\n const urlDecoded = reqparams.urlEncoded;\n // if (urlDecoded.substr(-1) === \"/\") {\n // urlDecoded = urlDecoded.substr(0, urlDecoded.length - 1);\n // }\n debug(urlDecoded);\n\n const isSecureHttp = req.secure ||\n req.protocol === \"https\" ||\n req.get(\"X-Forwarded-Proto\") === \"https\"\n ;\n const rootUrl = (isSecureHttp ? \"https://\" : \"http://\")\n + req.headers.host;\n\n const failure = (err: any) => {\n debug(err);\n res.status(500).send(\"<html><body><p>Internal Server Error</p><p>\"\n + err + \"</p></body></html>\");\n };\n\n const success = async (response: request.RequestResponse) => {\n\n // Object.keys(response.headers).forEach((header: string) => {\n // debug(header + \" => \" + response.headers[header]);\n // });\n\n if (response.statusCode && (response.statusCode < 200 || response.statusCode >= 300)) {\n failure(\"HTTP CODE \" + response.statusCode);\n return;\n }\n\n let responseData: Buffer;\n try {\n responseData = await streamToBufferPromise(response);\n } catch (err) {\n debug(err);\n res.status(500).send(\"<html><body><p>Internal Server Error</p><p>\"\n + err + \"</p></body></html>\");\n return;\n }\n const responseStr = responseData.toString(\"utf8\");\n const responseXml = new xmldom.DOMParser().parseFromString(responseStr);\n // debug(responseXml);\n if (!responseXml || !responseXml.documentElement) {\n res.status(500).send(\"<html><body><p>Internal Server Error</p><p>\"\n + \"XML parse fail\" + \"</p></body></html>\");\n return;\n }\n const isEntry = responseXml.documentElement.localName === \"entry\";\n let opds1Feed: OPDS | undefined;\n let opds1Entry: Entry | undefined;\n let opds2Feed: OPDSFeed | undefined;\n let opds2Publication: OPDSPublication | undefined;\n if (isEntry) {\n opds1Entry = XML.deserialize<Entry>(responseXml, Entry);\n\n try {\n opds2Publication = convertOpds1ToOpds2_EntryToPublication(opds1Entry);\n // debug(opds2Publication);\n\n // // breakLength: 100 maxArrayLength: undefined\n // console.log(util.inspect(opds2Publication,\n // { showHidden: false, depth: 1000, colors: true, customInspect: true }));\n } catch (err) {\n debug(\"OPDS 1 -> 2 conversion FAILED (Entry)\");\n debug(err);\n\n res.status(500).send(\"<html><body><p>Internal Server Error</p><p>\"\n + err + \"</p></body></html>\");\n return;\n }\n } else {\n opds1Feed = XML.deserialize<OPDS>(responseXml, OPDS);\n\n try {\n opds2Feed = convertOpds1ToOpds2(opds1Feed);\n // debug(opds2Feed);\n\n // // breakLength: 100 maxArrayLength: undefined\n // console.log(util.inspect(opds2Feed,\n // { showHidden: false, depth: 1000, colors: true, customInspect: true }));\n } catch (err) {\n debug(\"OPDS 1 -> 2 conversion FAILED\");\n debug(err);\n\n res.status(500).send(\"<html><body><p>Internal Server Error</p><p>\"\n + err + \"</p></body></html>\");\n return;\n }\n }\n\n const funk = (obj: any) => {\n if ((obj.href && typeof obj.href === \"string\") ||\n (obj.Href && typeof obj.Href === \"string\")) {\n\n let fullHref = obj.href ? obj.href as string : obj.Href as string;\n\n const notFull = !isHTTP(fullHref);\n if (notFull) {\n fullHref = ensureAbsolute(urlDecoded, fullHref);\n }\n\n if ((obj.type && obj.type.indexOf(\"application/atom+xml\") >= 0) ||\n (obj.Type && obj.Type.indexOf(\"application/atom+xml\") >= 0)) {\n obj.__href__ = rootUrl + req.originalUrl.substr(0,\n req.originalUrl.indexOf(serverOPDS_convert_v1_to_v2_PATH + \"/\")) +\n serverOPDS_convert_v1_to_v2_PATH + \"/\" + encodeURIComponent_RFC3986(fullHref);\n } else if (notFull) {\n obj.__href__ = fullHref;\n }\n }\n };\n\n const jsonObjOPDS1 = TaJsonSerialize(opds1Entry ? opds1Entry : opds1Feed);\n traverseJsonObjects(jsonObjOPDS1, funk);\n\n const jsonObjOPDS2 = TaJsonSerialize(opds2Publication ? opds2Publication : opds2Feed);\n\n let validationStr: string | undefined;\n const doValidate = !reqparams.jsonPath || reqparams.jsonPath === \"all\";\n if (doValidate) {\n\n const jsonSchemasRootpath = path.join(process.cwd(), \"misc\", \"json-schema\");\n const jsonSchemasNames = [\n \"opds/publication\",\n \"opds/acquisition-object\",\n \"opds/catalog-entry\",\n \"opds/feed-metadata\",\n \"opds/properties\",\n // \"opds/authentication\",\n \"webpub-manifest/publication\",\n \"webpub-manifest/contributor-object\",\n \"webpub-manifest/contributor\",\n \"webpub-manifest/link\",\n \"webpub-manifest/metadata\",\n \"webpub-manifest/subcollection\",\n \"webpub-manifest/properties\",\n \"webpub-manifest/subject\",\n \"webpub-manifest/subject-object\",\n \"webpub-manifest/extensions/epub/metadata\",\n \"webpub-manifest/extensions/epub/subcollections\",\n \"webpub-manifest/extensions/epub/properties\",\n \"webpub-manifest/extensions/presentation/metadata\",\n \"webpub-manifest/extensions/presentation/properties\",\n \"webpub-manifest/language-map\",\n ];\n if (!opds2Publication) {\n jsonSchemasNames.unshift(\"opds/feed\"); // must be first!\n }\n // debug(jsonSchemasNames);\n\n const validationErrors =\n jsonSchemaValidate(jsonSchemasRootpath, jsonSchemasNames, jsonObjOPDS2);\n if (validationErrors) {\n validationStr = \"\";\n\n for (const err of validationErrors) {\n\n debug(\"JSON Schema validation FAIL.\");\n debug(err);\n\n if (opds2Publication) {\n const val = DotProp.get(jsonObjOPDS2, err.jsonPath);\n const valueStr = (typeof val === \"string\") ?\n `${val}` :\n ((val instanceof Array || typeof val === \"object\") ?\n `${JSON.stringify(val)}` :\n \"\");\n debug(valueStr);\n\n const title = DotProp.get(jsonObjOPDS2, \"metadata.title\");\n debug(title);\n\n validationStr +=\n // tslint:disable-next-line:max-line-length\n `\\n\"${title}\"\\n\\n${err.ajvMessage}: ${valueStr}\\n\\n'${err.ajvDataPath.replace(/^\\./, \"\")}' (${err.ajvSchemaPath})\\n\\n`;\n } else {\n const val = DotProp.get(jsonObjOPDS2, err.jsonPath);\n const valueStr = (typeof val === \"string\") ?\n `${val}` :\n ((val instanceof Array || typeof val === \"object\") ?\n `${JSON.stringify(val)}` :\n \"\");\n debug(valueStr);\n\n let title = \"\";\n let pubIndex = \"\";\n if (/^publications\\.[0-9]+/.test(err.jsonPath)) {\n const jsonPubTitlePath =\n err.jsonPath.replace(/^(publications\\.[0-9]+).*/, \"$1.metadata.title\");\n debug(jsonPubTitlePath);\n title = DotProp.get(jsonObjOPDS2, jsonPubTitlePath);\n debug(title);\n\n pubIndex = err.jsonPath.replace(/^publications\\.([0-9]+).*/, \"$1\");\n debug(pubIndex);\n }\n\n validationStr +=\n // tslint:disable-next-line:max-line-length\n `\\n___________INDEX___________ #${pubIndex} \"${title}\"\\n\\n${err.ajvMessage}: ${valueStr}\\n\\n'${err.ajvDataPath.replace(/^\\./, \"\")}' (${err.ajvSchemaPath})\\n\\n`;\n }\n }\n }\n }\n\n traverseJsonObjects(jsonObjOPDS2, funk);\n\n const css = css2json(jsonStyle);\n const jsonPrettyOPDS1 = jsonMarkup(jsonObjOPDS1, css);\n const jsonPrettyOPDS2 = jsonMarkup(jsonObjOPDS2, css);\n\n res.status(200).send(\"<html><body>\" +\n \"<h1>OPDS2 JSON \" +\n (opds2Publication ? \"entry\" : \"feed\") +\n \" (converted from OPDS1 XML/ATOM)</h1>\" +\n \"<h2><a href=\\\"\" + urlDecoded + \"\\\">\" + urlDecoded + \"</a></h2>\" +\n \"<hr>\" +\n \"<table border=\\\"0\\\" cellpadding=\\\"0\\\" cellspacing=\\\"0\\\" width=\\\"90%\\\" \" +\n \"style=\\\"table-layout:fixed;width:90%\\\">\" +\n \"<thead><tr><th>OPDS1</th><th>OPDS2</th></tr></thead>\" +\n \"<tbody><tr>\" +\n \"<td valign=\\\"top\\\" width=\\\"50%\\\">\" +\n \"<div style=\\\"overflow-x: auto;margin:0;padding:0;width:100%;height:auto;\\\">\" +\n jsonPrettyOPDS1 + \"</div></td>\" +\n \"<td valign=\\\"top\\\" width=\\\"50%\\\">\" +\n \"<div style=\\\"overflow-x: auto;margin:0;padding:0;width:100%;height:auto;\\\">\" +\n jsonPrettyOPDS2 + \"</div></td>\" +\n \"</tbody></tr>\" +\n \"</table>\" +\n // tslint:disable-next-line:max-line-length\n (doValidate ? (validationStr ? (\"<hr><p><pre>\" + validationStr + \"</pre></p>\") : (\"<hr><p>JSON SCHEMA OK.</p>\")) : \"\") +\n // \"<p><pre>\" + jsonPretty + \"</pre></p>\" +\n // \"<hr><p><pre>\" + jsonStr + \"</pre></p>\" +\n // \"<p><pre>\" + dumpStr + \"</pre></p>\" +\n \"</body></html>\");\n };\n\n const headers = {\n \"Accept\": \"application/json,application/xml\",\n \"Accept-Language\": \"en-UK,en-US;q=0.7,en;q=0.5\",\n \"User-Agent\": \"READIUM2\",\n };\n\n // No response streaming! :(\n // https://github.com/request/request-promise/issues/90\n const needsStreamingResponse = true;\n if (needsStreamingResponse) {\n request.get({\n headers,\n method: \"GET\",\n uri: urlDecoded,\n })\n .on(\"response\", success)\n .on(\"error\", failure);\n } else {\n let response: requestPromise.FullResponse;\n try {\n // tslint:disable-next-line:await-promise no-floating-promises\n response = await requestPromise({\n headers,\n method: \"GET\",\n resolveWithFullResponse: true,\n uri: urlDecoded,\n });\n } catch (err) {\n failure(err);\n return;\n }\n\n await success(response);\n }\n });\n\n topRouter.use(serverOPDS_convert_v1_to_v2_PATH, routerOPDS_convert_v1_to_v2);\n}\n"]}
\No newline at end of file