@mantra-ai/core 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/ai/google/client.d.ts +67 -0
- package/dist/ai/google/client.d.ts.map +1 -0
- package/dist/ai/google/client.js +169 -0
- package/dist/ai/google/client.js.map +1 -0
- package/dist/ai/google/generate.d.ts +10 -0
- package/dist/ai/google/generate.d.ts.map +1 -0
- package/dist/ai/google/generate.js +137 -0
- package/dist/ai/google/generate.js.map +1 -0
- package/dist/ai/google/index.d.ts +4 -0
- package/dist/ai/google/index.d.ts.map +1 -0
- package/dist/ai/google/index.js +4 -0
- package/dist/ai/google/index.js.map +1 -0
- package/dist/ai/google/types.d.ts +88 -0
- package/dist/ai/google/types.d.ts.map +1 -0
- package/dist/ai/google/types.js +55 -0
- package/dist/ai/google/types.js.map +1 -0
- package/dist/ai/index.d.ts +3 -0
- package/dist/ai/index.d.ts.map +1 -0
- package/dist/ai/index.js +3 -0
- package/dist/ai/index.js.map +1 -0
- package/dist/ai/openai/client.d.ts +22 -0
- package/dist/ai/openai/client.d.ts.map +1 -0
- package/dist/ai/openai/client.js +49 -0
- package/dist/ai/openai/client.js.map +1 -0
- package/dist/ai/openai/generate.d.ts +14 -0
- package/dist/ai/openai/generate.d.ts.map +1 -0
- package/dist/ai/openai/generate.js +178 -0
- package/dist/ai/openai/generate.js.map +1 -0
- package/dist/ai/openai/index.d.ts +4 -0
- package/dist/ai/openai/index.d.ts.map +1 -0
- package/dist/ai/openai/index.js +4 -0
- package/dist/ai/openai/index.js.map +1 -0
- package/dist/ai/openai/types.d.ts +86 -0
- package/dist/ai/openai/types.d.ts.map +1 -0
- package/dist/ai/openai/types.js +56 -0
- package/dist/ai/openai/types.js.map +1 -0
- package/dist/ai/prompts/index.d.ts +1 -0
- package/dist/ai/prompts/index.d.ts.map +1 -0
- package/dist/ai/prompts/index.js +2 -0
- package/dist/ai/prompts/index.js.map +1 -0
- package/dist/errors/index.d.ts +3 -0
- package/dist/errors/index.d.ts.map +1 -0
- package/dist/errors/index.js +4 -0
- package/dist/errors/index.js.map +1 -0
- package/dist/errors/schemas.d.ts +304 -0
- package/dist/errors/schemas.d.ts.map +1 -0
- package/dist/errors/schemas.js +57 -0
- package/dist/errors/schemas.js.map +1 -0
- package/dist/errors/types.d.ts +30 -0
- package/dist/errors/types.d.ts.map +1 -0
- package/dist/errors/types.js +33 -0
- package/dist/errors/types.js.map +1 -0
- package/dist/index.d.ts +21 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +21 -0
- package/dist/index.js.map +1 -0
- package/dist/normalization/jats/index.d.ts +4 -0
- package/dist/normalization/jats/index.d.ts.map +1 -0
- package/dist/normalization/jats/index.js +3 -0
- package/dist/normalization/jats/index.js.map +1 -0
- package/dist/normalization/jats/normalize.d.ts +7 -0
- package/dist/normalization/jats/normalize.d.ts.map +1 -0
- package/dist/normalization/jats/normalize.js +213 -0
- package/dist/normalization/jats/normalize.js.map +1 -0
- package/dist/normalization/jats/utils/build/finalize.d.ts +3 -0
- package/dist/normalization/jats/utils/build/finalize.d.ts.map +1 -0
- package/dist/normalization/jats/utils/build/finalize.js +462 -0
- package/dist/normalization/jats/utils/build/finalize.js.map +1 -0
- package/dist/normalization/jats/utils/build/flatten.d.ts +20 -0
- package/dist/normalization/jats/utils/build/flatten.d.ts.map +1 -0
- package/dist/normalization/jats/utils/build/flatten.js +502 -0
- package/dist/normalization/jats/utils/build/flatten.js.map +1 -0
- package/dist/normalization/jats/utils/build/meta.d.ts +10 -0
- package/dist/normalization/jats/utils/build/meta.d.ts.map +1 -0
- package/dist/normalization/jats/utils/build/meta.js +32 -0
- package/dist/normalization/jats/utils/build/meta.js.map +1 -0
- package/dist/normalization/jats/utils/build/version.d.ts +3 -0
- package/dist/normalization/jats/utils/build/version.d.ts.map +1 -0
- package/dist/normalization/jats/utils/build/version.js +11 -0
- package/dist/normalization/jats/utils/build/version.js.map +1 -0
- package/dist/normalization/jats/utils/category.d.ts +11 -0
- package/dist/normalization/jats/utils/category.d.ts.map +1 -0
- package/dist/normalization/jats/utils/category.js +431 -0
- package/dist/normalization/jats/utils/category.js.map +1 -0
- package/dist/normalization/jats/utils/collectors/abstracts.d.ts +3 -0
- package/dist/normalization/jats/utils/collectors/abstracts.d.ts.map +1 -0
- package/dist/normalization/jats/utils/collectors/abstracts.js +168 -0
- package/dist/normalization/jats/utils/collectors/abstracts.js.map +1 -0
- package/dist/normalization/jats/utils/collectors/back.d.ts +35 -0
- package/dist/normalization/jats/utils/collectors/back.d.ts.map +1 -0
- package/dist/normalization/jats/utils/collectors/back.js +801 -0
- package/dist/normalization/jats/utils/collectors/back.js.map +1 -0
- package/dist/normalization/jats/utils/collectors/contributors.d.ts +4 -0
- package/dist/normalization/jats/utils/collectors/contributors.d.ts.map +1 -0
- package/dist/normalization/jats/utils/collectors/contributors.js +77 -0
- package/dist/normalization/jats/utils/collectors/contributors.js.map +1 -0
- package/dist/normalization/jats/utils/collectors/keywords.d.ts +2 -0
- package/dist/normalization/jats/utils/collectors/keywords.d.ts.map +1 -0
- package/dist/normalization/jats/utils/collectors/keywords.js +14 -0
- package/dist/normalization/jats/utils/collectors/keywords.js.map +1 -0
- package/dist/normalization/jats/utils/collectors/meta.d.ts +6 -0
- package/dist/normalization/jats/utils/collectors/meta.d.ts.map +1 -0
- package/dist/normalization/jats/utils/collectors/meta.js +103 -0
- package/dist/normalization/jats/utils/collectors/meta.js.map +1 -0
- package/dist/normalization/jats/utils/collectors/sections.d.ts +7 -0
- package/dist/normalization/jats/utils/collectors/sections.d.ts.map +1 -0
- package/dist/normalization/jats/utils/collectors/sections.js +484 -0
- package/dist/normalization/jats/utils/collectors/sections.js.map +1 -0
- package/dist/normalization/jats/utils/licenses.d.ts +5 -0
- package/dist/normalization/jats/utils/licenses.d.ts.map +1 -0
- package/dist/normalization/jats/utils/licenses.js +64 -0
- package/dist/normalization/jats/utils/licenses.js.map +1 -0
- package/dist/normalization/jats/utils/po/nodes.d.ts +6 -0
- package/dist/normalization/jats/utils/po/nodes.d.ts.map +1 -0
- package/dist/normalization/jats/utils/po/nodes.js +60 -0
- package/dist/normalization/jats/utils/po/nodes.js.map +1 -0
- package/dist/normalization/jats/utils/po/query.d.ts +7 -0
- package/dist/normalization/jats/utils/po/query.d.ts.map +1 -0
- package/dist/normalization/jats/utils/po/query.js +67 -0
- package/dist/normalization/jats/utils/po/query.js.map +1 -0
- package/dist/normalization/jats/utils/po/serialize.d.ts +4 -0
- package/dist/normalization/jats/utils/po/serialize.d.ts.map +1 -0
- package/dist/normalization/jats/utils/po/serialize.js +329 -0
- package/dist/normalization/jats/utils/po/serialize.js.map +1 -0
- package/dist/normalization/jats/utils/po/text.d.ts +7 -0
- package/dist/normalization/jats/utils/po/text.d.ts.map +1 -0
- package/dist/normalization/jats/utils/po/text.js +114 -0
- package/dist/normalization/jats/utils/po/text.js.map +1 -0
- package/dist/normalization/jats/utils/references.d.ts +26 -0
- package/dist/normalization/jats/utils/references.d.ts.map +1 -0
- package/dist/normalization/jats/utils/references.js +371 -0
- package/dist/normalization/jats/utils/references.js.map +1 -0
- package/dist/normalization/jats/utils/strings.d.ts +8 -0
- package/dist/normalization/jats/utils/strings.d.ts.map +1 -0
- package/dist/normalization/jats/utils/strings.js +197 -0
- package/dist/normalization/jats/utils/strings.js.map +1 -0
- package/dist/normalization/jats/utils/types.d.ts +233 -0
- package/dist/normalization/jats/utils/types.d.ts.map +1 -0
- package/dist/normalization/jats/utils/types.js +2 -0
- package/dist/normalization/jats/utils/types.js.map +1 -0
- package/dist/normalization/jats/utils/xml.d.ts +5 -0
- package/dist/normalization/jats/utils/xml.d.ts.map +1 -0
- package/dist/normalization/jats/utils/xml.js +69 -0
- package/dist/normalization/jats/utils/xml.js.map +1 -0
- package/dist/normalization/normalized-doc-schema.d.ts +1094 -0
- package/dist/normalization/normalized-doc-schema.d.ts.map +1 -0
- package/dist/normalization/normalized-doc-schema.js +410 -0
- package/dist/normalization/normalized-doc-schema.js.map +1 -0
- package/dist/normalization/pdf/index.d.ts +4 -0
- package/dist/normalization/pdf/index.d.ts.map +1 -0
- package/dist/normalization/pdf/index.js +3 -0
- package/dist/normalization/pdf/index.js.map +1 -0
- package/dist/normalization/pdf/normalize.d.ts +31 -0
- package/dist/normalization/pdf/normalize.d.ts.map +1 -0
- package/dist/normalization/pdf/normalize.js +321 -0
- package/dist/normalization/pdf/normalize.js.map +1 -0
- package/dist/normalization/pdf/prompt.d.ts +3 -0
- package/dist/normalization/pdf/prompt.d.ts.map +1 -0
- package/dist/normalization/pdf/prompt.js +118 -0
- package/dist/normalization/pdf/prompt.js.map +1 -0
- package/dist/sources/arxiv/client.d.ts +4 -0
- package/dist/sources/arxiv/client.d.ts.map +1 -0
- package/dist/sources/arxiv/client.js +13 -0
- package/dist/sources/arxiv/client.js.map +1 -0
- package/dist/sources/biorxiv/client.d.ts +21 -0
- package/dist/sources/biorxiv/client.d.ts.map +1 -0
- package/dist/sources/biorxiv/client.js +173 -0
- package/dist/sources/biorxiv/client.js.map +1 -0
- package/dist/sources/crossref/client.d.ts +3 -0
- package/dist/sources/crossref/client.d.ts.map +1 -0
- package/dist/sources/crossref/client.js +24 -0
- package/dist/sources/crossref/client.js.map +1 -0
- package/dist/sources/europepmc/client.d.ts +3 -0
- package/dist/sources/europepmc/client.d.ts.map +1 -0
- package/dist/sources/europepmc/client.js +29 -0
- package/dist/sources/europepmc/client.js.map +1 -0
- package/dist/sources/medrxiv/browser.d.ts +16 -0
- package/dist/sources/medrxiv/browser.d.ts.map +1 -0
- package/dist/sources/medrxiv/browser.js +210 -0
- package/dist/sources/medrxiv/browser.js.map +1 -0
- package/dist/sources/medrxiv/client.d.ts +34 -0
- package/dist/sources/medrxiv/client.d.ts.map +1 -0
- package/dist/sources/medrxiv/client.js +673 -0
- package/dist/sources/medrxiv/client.js.map +1 -0
- package/dist/sources/medrxiv/shared.d.ts +7 -0
- package/dist/sources/medrxiv/shared.d.ts.map +1 -0
- package/dist/sources/medrxiv/shared.js +18 -0
- package/dist/sources/medrxiv/shared.js.map +1 -0
- package/dist/sources/plos/client.d.ts +13 -0
- package/dist/sources/plos/client.d.ts.map +1 -0
- package/dist/sources/plos/client.js +147 -0
- package/dist/sources/plos/client.js.map +1 -0
- package/dist/sources/preprint-discovery.d.ts +55 -0
- package/dist/sources/preprint-discovery.d.ts.map +1 -0
- package/dist/sources/preprint-discovery.js +115 -0
- package/dist/sources/preprint-discovery.js.map +1 -0
- package/dist/types/expand.d.ts +5 -0
- package/dist/types/expand.d.ts.map +1 -0
- package/dist/types/expand.js +20 -0
- package/dist/types/expand.js.map +1 -0
- package/dist/types/methods-types.d.ts +37 -0
- package/dist/types/methods-types.d.ts.map +1 -0
- package/dist/types/methods-types.js +2 -0
- package/dist/types/methods-types.js.map +1 -0
- package/dist/types/multi-input-types.d.ts +57 -0
- package/dist/types/multi-input-types.d.ts.map +1 -0
- package/dist/types/multi-input-types.js +2 -0
- package/dist/types/multi-input-types.js.map +1 -0
- package/dist/types/paper/types.d.ts +41 -0
- package/dist/types/paper/types.d.ts.map +1 -0
- package/dist/types/paper/types.js +2 -0
- package/dist/types/paper/types.js.map +1 -0
- package/dist/types/results-types.d.ts +122 -0
- package/dist/types/results-types.d.ts.map +1 -0
- package/dist/types/results-types.js +17 -0
- package/dist/types/results-types.js.map +1 -0
- package/dist/types/supp-types.d.ts +6 -0
- package/dist/types/supp-types.d.ts.map +1 -0
- package/dist/types/supp-types.js +2 -0
- package/dist/types/supp-types.js.map +1 -0
- package/dist/types/version.d.ts +1828 -0
- package/dist/types/version.d.ts.map +1 -0
- package/dist/types/version.js +311 -0
- package/dist/types/version.js.map +1 -0
- package/dist/types/work.d.ts +4455 -0
- package/dist/types/work.d.ts.map +1 -0
- package/dist/types/work.js +330 -0
- package/dist/types/work.js.map +1 -0
- package/dist/works/adapters/crossref.d.ts +28 -0
- package/dist/works/adapters/crossref.d.ts.map +1 -0
- package/dist/works/adapters/crossref.js +43 -0
- package/dist/works/adapters/crossref.js.map +1 -0
- package/dist/works/adapters/europepmc.d.ts +14 -0
- package/dist/works/adapters/europepmc.d.ts.map +1 -0
- package/dist/works/adapters/europepmc.js +46 -0
- package/dist/works/adapters/europepmc.js.map +1 -0
- package/dist/works/adapters/openalex.d.ts +5 -0
- package/dist/works/adapters/openalex.d.ts.map +1 -0
- package/dist/works/adapters/openalex.js +75 -0
- package/dist/works/adapters/openalex.js.map +1 -0
- package/dist/works/errors.d.ts +23 -0
- package/dist/works/errors.d.ts.map +1 -0
- package/dist/works/errors.js +37 -0
- package/dist/works/errors.js.map +1 -0
- package/dist/works/id/detect-identifier.d.ts +15 -0
- package/dist/works/id/detect-identifier.d.ts.map +1 -0
- package/dist/works/id/detect-identifier.js +50 -0
- package/dist/works/id/detect-identifier.js.map +1 -0
- package/dist/works/id/normalize-external-id.d.ts +3 -0
- package/dist/works/id/normalize-external-id.d.ts.map +1 -0
- package/dist/works/id/normalize-external-id.js +44 -0
- package/dist/works/id/normalize-external-id.js.map +1 -0
- package/dist/works/id/normalize-ids.d.ts +66 -0
- package/dist/works/id/normalize-ids.d.ts.map +1 -0
- package/dist/works/id/normalize-ids.js +112 -0
- package/dist/works/id/normalize-ids.js.map +1 -0
- package/dist/works/id/normalize-internals.d.ts +7 -0
- package/dist/works/id/normalize-internals.d.ts.map +1 -0
- package/dist/works/id/normalize-internals.js +65 -0
- package/dist/works/id/normalize-internals.js.map +1 -0
- package/dist/works/id/resolve.d.ts +31 -0
- package/dist/works/id/resolve.d.ts.map +1 -0
- package/dist/works/id/resolve.js +123 -0
- package/dist/works/id/resolve.js.map +1 -0
- package/dist/works/id/resolveIds/assign.d.ts +4 -0
- package/dist/works/id/resolveIds/assign.d.ts.map +1 -0
- package/dist/works/id/resolveIds/assign.js +15 -0
- package/dist/works/id/resolveIds/assign.js.map +1 -0
- package/dist/works/id/resolveIds/flags.d.ts +11 -0
- package/dist/works/id/resolveIds/flags.d.ts.map +1 -0
- package/dist/works/id/resolveIds/flags.js +27 -0
- package/dist/works/id/resolveIds/flags.js.map +1 -0
- package/dist/works/id/resolveIds/idctx.d.ts +4 -0
- package/dist/works/id/resolveIds/idctx.d.ts.map +1 -0
- package/dist/works/id/resolveIds/idctx.js +25 -0
- package/dist/works/id/resolveIds/idctx.js.map +1 -0
- package/dist/works/id/resolveIds/index.d.ts +13 -0
- package/dist/works/id/resolveIds/index.d.ts.map +1 -0
- package/dist/works/id/resolveIds/index.js +498 -0
- package/dist/works/id/resolveIds/index.js.map +1 -0
- package/dist/works/id/resolveIds/versioning.d.ts +27 -0
- package/dist/works/id/resolveIds/versioning.d.ts.map +1 -0
- package/dist/works/id/resolveIds/versioning.js +156 -0
- package/dist/works/id/resolveIds/versioning.js.map +1 -0
- package/dist/works/id/resolveIds/workWhere.d.ts +3 -0
- package/dist/works/id/resolveIds/workWhere.d.ts.map +1 -0
- package/dist/works/id/resolveIds/workWhere.js +35 -0
- package/dist/works/id/resolveIds/workWhere.js.map +1 -0
- package/dist/works/id/types.d.ts +6 -0
- package/dist/works/id/types.d.ts.map +1 -0
- package/dist/works/id/types.js +2 -0
- package/dist/works/id/types.js.map +1 -0
- package/dist/works/pdf-fallback/candidates.d.ts +12 -0
- package/dist/works/pdf-fallback/candidates.d.ts.map +1 -0
- package/dist/works/pdf-fallback/candidates.js +51 -0
- package/dist/works/pdf-fallback/candidates.js.map +1 -0
- package/dist/works/pdf-fallback/fetch.d.ts +21 -0
- package/dist/works/pdf-fallback/fetch.d.ts.map +1 -0
- package/dist/works/pdf-fallback/fetch.js +89 -0
- package/dist/works/pdf-fallback/fetch.js.map +1 -0
- package/dist/works/pdf-fallback/index.d.ts +28 -0
- package/dist/works/pdf-fallback/index.d.ts.map +1 -0
- package/dist/works/pdf-fallback/index.js +35 -0
- package/dist/works/pdf-fallback/index.js.map +1 -0
- package/dist/works/plan.d.ts +8 -0
- package/dist/works/plan.d.ts.map +1 -0
- package/dist/works/plan.js +62 -0
- package/dist/works/plan.js.map +1 -0
- package/dist/works/strategies/arxiv.d.ts +3 -0
- package/dist/works/strategies/arxiv.d.ts.map +1 -0
- package/dist/works/strategies/arxiv.js +56 -0
- package/dist/works/strategies/arxiv.js.map +1 -0
- package/dist/works/strategies/biorxiv.d.ts +3 -0
- package/dist/works/strategies/biorxiv.d.ts.map +1 -0
- package/dist/works/strategies/biorxiv.js +63 -0
- package/dist/works/strategies/biorxiv.js.map +1 -0
- package/dist/works/strategies/europepmc.d.ts +3 -0
- package/dist/works/strategies/europepmc.d.ts.map +1 -0
- package/dist/works/strategies/europepmc.js +15 -0
- package/dist/works/strategies/europepmc.js.map +1 -0
- package/dist/works/strategies/index.d.ts +12 -0
- package/dist/works/strategies/index.d.ts.map +1 -0
- package/dist/works/strategies/index.js +19 -0
- package/dist/works/strategies/index.js.map +1 -0
- package/dist/works/strategies/landing-url.d.ts +3 -0
- package/dist/works/strategies/landing-url.d.ts.map +1 -0
- package/dist/works/strategies/landing-url.js +10 -0
- package/dist/works/strategies/landing-url.js.map +1 -0
- package/dist/works/strategies/medrxiv.d.ts +3 -0
- package/dist/works/strategies/medrxiv.d.ts.map +1 -0
- package/dist/works/strategies/medrxiv.js +47 -0
- package/dist/works/strategies/medrxiv.js.map +1 -0
- package/dist/works/strategies/plos.d.ts +3 -0
- package/dist/works/strategies/plos.d.ts.map +1 -0
- package/dist/works/strategies/plos.js +15 -0
- package/dist/works/strategies/plos.js.map +1 -0
- package/dist/works/strategies/shared.d.ts +11 -0
- package/dist/works/strategies/shared.d.ts.map +1 -0
- package/dist/works/strategies/shared.js +97 -0
- package/dist/works/strategies/shared.js.map +1 -0
- package/dist/works/strategies/ten1101.d.ts +3 -0
- package/dist/works/strategies/ten1101.d.ts.map +1 -0
- package/dist/works/strategies/ten1101.js +84 -0
- package/dist/works/strategies/ten1101.js.map +1 -0
- package/dist/works/text/acquire-fulltext.d.ts +7 -0
- package/dist/works/text/acquire-fulltext.d.ts.map +1 -0
- package/dist/works/text/acquire-fulltext.js +62 -0
- package/dist/works/text/acquire-fulltext.js.map +1 -0
- package/dist/works/text/normalize.d.ts +40 -0
- package/dist/works/text/normalize.d.ts.map +1 -0
- package/dist/works/text/normalize.js +188 -0
- package/dist/works/text/normalize.js.map +1 -0
- package/dist/works/types.d.ts +215 -0
- package/dist/works/types.d.ts.map +1 -0
- package/dist/works/types.js +6 -0
- package/dist/works/types.js.map +1 -0
- package/dist/works/util/debug.d.ts +7 -0
- package/dist/works/util/debug.d.ts.map +1 -0
- package/dist/works/util/debug.js +9 -0
- package/dist/works/util/debug.js.map +1 -0
- package/dist/works/util/license.d.ts +9 -0
- package/dist/works/util/license.d.ts.map +1 -0
- package/dist/works/util/license.js +39 -0
- package/dist/works/util/license.js.map +1 -0
- package/dist/works/util/normalize.d.ts +2 -0
- package/dist/works/util/normalize.d.ts.map +1 -0
- package/dist/works/util/normalize.js +76 -0
- package/dist/works/util/normalize.js.map +1 -0
- package/dist/works/util/parse.d.ts +8 -0
- package/dist/works/util/parse.d.ts.map +1 -0
- package/dist/works/util/parse.js +32 -0
- package/dist/works/util/parse.js.map +1 -0
- package/dist/works/util/source.d.ts +10 -0
- package/dist/works/util/source.d.ts.map +1 -0
- package/dist/works/util/source.js +48 -0
- package/dist/works/util/source.js.map +1 -0
- package/dist/works/util/version-label.d.ts +2 -0
- package/dist/works/util/version-label.d.ts.map +1 -0
- package/dist/works/util/version-label.js +8 -0
- package/dist/works/util/version-label.js.map +1 -0
- package/dist/works/util/work-id.d.ts +2 -0
- package/dist/works/util/work-id.d.ts.map +1 -0
- package/dist/works/util/work-id.js +27 -0
- package/dist/works/util/work-id.js.map +1 -0
- package/package.json +208 -0
|
@@ -0,0 +1,673 @@
|
|
|
1
|
+
// services/sources/medrxiv/client.ts
|
|
2
|
+
import { gunzipSync, inflateRawSync, inflateSync } from "node:zlib";
|
|
3
|
+
import JSZip from "jszip";
|
|
4
|
+
import { XMLParser } from "fast-xml-parser";
|
|
5
|
+
import { fetchMedrxivSourceXmlViaBrowser, peekBrowserCookies } from "./browser";
|
|
6
|
+
import { MEDRXIV_ACCEPT_LANGUAGE, MEDRXIV_ACCEPT_XML, MEDRXIV_BROWSER_UA, MEDRXIV_HTML_ACCEPT, looksLikeJatsXml, safePreview, } from "./shared";
|
|
7
|
+
import { normPreprintCore, discoverPreprintRecord, parseVersionNumber, } from "../preprint-discovery";
|
|
8
|
+
const DEBUG = process.env.DEBUG_MEDRXIV_FETCH === "1";
|
|
9
|
+
const summarizeDetails = (details) => {
|
|
10
|
+
if (!details)
|
|
11
|
+
return "";
|
|
12
|
+
try {
|
|
13
|
+
const str = JSON.stringify(details);
|
|
14
|
+
return str.length > 500 ? `${str.slice(0, 500)}…` : str;
|
|
15
|
+
}
|
|
16
|
+
catch {
|
|
17
|
+
return String(details);
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
const logStrategyFailure = (result) => {
|
|
21
|
+
if (!DEBUG)
|
|
22
|
+
return;
|
|
23
|
+
const summary = summarizeDetails(result.details);
|
|
24
|
+
if (summary) {
|
|
25
|
+
console.log(`[medrxiv] ${result.name} failed: ${result.reason} ${summary}`);
|
|
26
|
+
}
|
|
27
|
+
else {
|
|
28
|
+
console.log(`[medrxiv] ${result.name} failed: ${result.reason}`);
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
function extractVersionFromString(value) {
|
|
32
|
+
if (!value)
|
|
33
|
+
return null;
|
|
34
|
+
const match = String(value)
|
|
35
|
+
.trim()
|
|
36
|
+
.match(/v(\d+)(?:[^0-9]|$)/i);
|
|
37
|
+
if (!match || !match[1])
|
|
38
|
+
return null;
|
|
39
|
+
const n = parseInt(match[1], 10);
|
|
40
|
+
return Number.isFinite(n) ? n : null;
|
|
41
|
+
}
|
|
42
|
+
const resolveVersion = (ctx) => {
|
|
43
|
+
if (ctx.inferredVersion && Number.isFinite(ctx.inferredVersion)) {
|
|
44
|
+
return Number(ctx.inferredVersion);
|
|
45
|
+
}
|
|
46
|
+
if (ctx.opts?.preferVersion && Number.isFinite(ctx.opts.preferVersion)) {
|
|
47
|
+
return Number(ctx.opts.preferVersion);
|
|
48
|
+
}
|
|
49
|
+
return null;
|
|
50
|
+
};
|
|
51
|
+
const directFetchStrategy = async (ctx) => {
|
|
52
|
+
const attempts = [];
|
|
53
|
+
const ua = ctx.opts?.UA || "drylab-reveng-api/1.0";
|
|
54
|
+
const browserUA = ua && /^Mozilla\//.test(ua) ? ua : MEDRXIV_BROWSER_UA;
|
|
55
|
+
const attempt = async (label, overrides) => {
|
|
56
|
+
try {
|
|
57
|
+
const res = await fetch(ctx.jatsUrl, {
|
|
58
|
+
headers: {
|
|
59
|
+
Accept: MEDRXIV_ACCEPT_XML,
|
|
60
|
+
"Accept-Language": MEDRXIV_ACCEPT_LANGUAGE,
|
|
61
|
+
Referer: ctx.refererUrl,
|
|
62
|
+
"User-Agent": browserUA,
|
|
63
|
+
...overrides,
|
|
64
|
+
},
|
|
65
|
+
redirect: "follow",
|
|
66
|
+
});
|
|
67
|
+
const contentType = res.headers.get("content-type") || "?";
|
|
68
|
+
DEBUG &&
|
|
69
|
+
console.log(`[medrxiv] ${label} status=`, res.status, "ct=", contentType);
|
|
70
|
+
if (!res.ok) {
|
|
71
|
+
attempts.push({ label, status: res.status, contentType });
|
|
72
|
+
return null;
|
|
73
|
+
}
|
|
74
|
+
const text = await res.text();
|
|
75
|
+
if (looksLikeJatsXml(text)) {
|
|
76
|
+
return { url: res.url || ctx.jatsUrl, xml: text };
|
|
77
|
+
}
|
|
78
|
+
attempts.push({
|
|
79
|
+
label,
|
|
80
|
+
status: res.status,
|
|
81
|
+
contentType,
|
|
82
|
+
reason: "non-jats",
|
|
83
|
+
preview: safePreview(text),
|
|
84
|
+
});
|
|
85
|
+
return null;
|
|
86
|
+
}
|
|
87
|
+
catch (err) {
|
|
88
|
+
attempts.push({ label, error: err?.message ?? String(err) });
|
|
89
|
+
return null;
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
const withCookies = async () => {
|
|
93
|
+
try {
|
|
94
|
+
const priming = await fetch(ctx.landingUrl, {
|
|
95
|
+
headers: {
|
|
96
|
+
Accept: MEDRXIV_HTML_ACCEPT,
|
|
97
|
+
"Accept-Language": MEDRXIV_ACCEPT_LANGUAGE,
|
|
98
|
+
"User-Agent": browserUA,
|
|
99
|
+
},
|
|
100
|
+
redirect: "follow",
|
|
101
|
+
});
|
|
102
|
+
const contentType = priming.headers.get("content-type") || "?";
|
|
103
|
+
const raw = priming?.headers?.raw?.();
|
|
104
|
+
const setCookies = raw?.["set-cookie"] || [];
|
|
105
|
+
const cookieHeader = setCookies
|
|
106
|
+
.map((c) => String(c).split(";")[0])
|
|
107
|
+
.filter(Boolean)
|
|
108
|
+
.join("; ");
|
|
109
|
+
attempts.push({
|
|
110
|
+
label: "cookie-prime",
|
|
111
|
+
status: priming.status,
|
|
112
|
+
contentType,
|
|
113
|
+
cookies: setCookies.length,
|
|
114
|
+
});
|
|
115
|
+
DEBUG &&
|
|
116
|
+
console.log("[medrxiv] priming status:", priming.status, "content-type:", contentType, "cookies:", setCookies.length);
|
|
117
|
+
return cookieHeader || null;
|
|
118
|
+
}
|
|
119
|
+
catch (err) {
|
|
120
|
+
attempts.push({
|
|
121
|
+
label: "cookie-prime",
|
|
122
|
+
error: err?.message ?? String(err),
|
|
123
|
+
});
|
|
124
|
+
return null;
|
|
125
|
+
}
|
|
126
|
+
};
|
|
127
|
+
const attempt1 = await attempt("attempt#1");
|
|
128
|
+
if (attempt1) {
|
|
129
|
+
return {
|
|
130
|
+
success: true,
|
|
131
|
+
name: "direct",
|
|
132
|
+
data: { ...attempt1, version: resolveVersion(ctx) },
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
const attempt2 = await attempt("attempt#2");
|
|
136
|
+
if (attempt2) {
|
|
137
|
+
return {
|
|
138
|
+
success: true,
|
|
139
|
+
name: "direct",
|
|
140
|
+
data: { ...attempt2, version: resolveVersion(ctx) },
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
const cookies = await withCookies();
|
|
144
|
+
if (cookies) {
|
|
145
|
+
ctx.shared.cookies = cookies;
|
|
146
|
+
const attempt3 = await attempt("attempt#3", { Cookie: cookies });
|
|
147
|
+
if (attempt3) {
|
|
148
|
+
return {
|
|
149
|
+
success: true,
|
|
150
|
+
name: "direct",
|
|
151
|
+
data: { ...attempt3, version: resolveVersion(ctx) },
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
return {
|
|
156
|
+
success: false,
|
|
157
|
+
name: "direct",
|
|
158
|
+
reason: "blocked-or-non-jats",
|
|
159
|
+
details: { attempts },
|
|
160
|
+
};
|
|
161
|
+
};
|
|
162
|
+
const browserFetchStrategy = async (ctx) => {
|
|
163
|
+
const version = resolveVersion(ctx);
|
|
164
|
+
const payload = await fetchMedrxivSourceXmlViaBrowser({
|
|
165
|
+
coreId: ctx.coreId,
|
|
166
|
+
jatsUrl: ctx.jatsUrl,
|
|
167
|
+
inferredVersion: version,
|
|
168
|
+
refererUrl: ctx.refererUrl,
|
|
169
|
+
sourceOptions: ctx.opts,
|
|
170
|
+
});
|
|
171
|
+
const cookieHeader = payload?.cookies ?? peekBrowserCookies();
|
|
172
|
+
if (cookieHeader) {
|
|
173
|
+
ctx.shared.browserCookies = cookieHeader;
|
|
174
|
+
}
|
|
175
|
+
if (payload) {
|
|
176
|
+
return {
|
|
177
|
+
success: true,
|
|
178
|
+
name: "browser",
|
|
179
|
+
data: {
|
|
180
|
+
url: payload.url,
|
|
181
|
+
xml: payload.xml,
|
|
182
|
+
version: payload.version ?? version,
|
|
183
|
+
},
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
return {
|
|
187
|
+
success: false,
|
|
188
|
+
name: "browser",
|
|
189
|
+
reason: "browser-fetch-failed",
|
|
190
|
+
};
|
|
191
|
+
};
|
|
192
|
+
const proxyFetchStrategy = async (ctx) => {
|
|
193
|
+
const ua = ctx.opts?.UA || "drylab-reveng-api/1.0";
|
|
194
|
+
const browserUA = ua && /^Mozilla\//.test(ua) ? ua : MEDRXIV_BROWSER_UA;
|
|
195
|
+
const proxyUrl = `https://r.jina.ai/http://${ctx.jatsUrl.replace(/^https?:\/\//i, "")}`;
|
|
196
|
+
try {
|
|
197
|
+
DEBUG && console.log("[medrxiv] proxy fetch:", proxyUrl);
|
|
198
|
+
const res = await fetch(proxyUrl, {
|
|
199
|
+
headers: {
|
|
200
|
+
Accept: MEDRXIV_ACCEPT_XML,
|
|
201
|
+
"Accept-Language": MEDRXIV_ACCEPT_LANGUAGE,
|
|
202
|
+
"User-Agent": browserUA,
|
|
203
|
+
},
|
|
204
|
+
});
|
|
205
|
+
DEBUG && console.log("[medrxiv] proxy status:", res.status);
|
|
206
|
+
if (!res.ok) {
|
|
207
|
+
return {
|
|
208
|
+
success: false,
|
|
209
|
+
name: "proxy",
|
|
210
|
+
reason: `status-${res.status}`,
|
|
211
|
+
};
|
|
212
|
+
}
|
|
213
|
+
const text = await res.text();
|
|
214
|
+
const head = text.slice(0, 512);
|
|
215
|
+
DEBUG &&
|
|
216
|
+
console.log("[medrxiv] proxy head:", head.replace(/\s+/g, " ").slice(0, 200));
|
|
217
|
+
if (!looksLikeJatsXml(text)) {
|
|
218
|
+
return {
|
|
219
|
+
success: false,
|
|
220
|
+
name: "proxy",
|
|
221
|
+
reason: "non-jats",
|
|
222
|
+
details: { preview: safePreview(text) },
|
|
223
|
+
};
|
|
224
|
+
}
|
|
225
|
+
return {
|
|
226
|
+
success: true,
|
|
227
|
+
name: "proxy",
|
|
228
|
+
data: {
|
|
229
|
+
url: ctx.jatsUrl,
|
|
230
|
+
xml: text,
|
|
231
|
+
version: resolveVersion(ctx),
|
|
232
|
+
},
|
|
233
|
+
};
|
|
234
|
+
}
|
|
235
|
+
catch (err) {
|
|
236
|
+
return {
|
|
237
|
+
success: false,
|
|
238
|
+
name: "proxy",
|
|
239
|
+
reason: err?.message ?? String(err),
|
|
240
|
+
};
|
|
241
|
+
}
|
|
242
|
+
};
|
|
243
|
+
const connectFetchStrategy = async (ctx) => {
|
|
244
|
+
const version = resolveVersion(ctx);
|
|
245
|
+
if (!version) {
|
|
246
|
+
return {
|
|
247
|
+
success: false,
|
|
248
|
+
name: "connect",
|
|
249
|
+
reason: "missing-version",
|
|
250
|
+
};
|
|
251
|
+
}
|
|
252
|
+
const fallback = await fetchMedrxivFromConnect(ctx.coreId, version, ctx.opts, ctx.shared.browserCookies || ctx.shared.cookies);
|
|
253
|
+
if (fallback) {
|
|
254
|
+
return {
|
|
255
|
+
success: true,
|
|
256
|
+
name: "connect",
|
|
257
|
+
data: fallback,
|
|
258
|
+
};
|
|
259
|
+
}
|
|
260
|
+
return {
|
|
261
|
+
success: false,
|
|
262
|
+
name: "connect",
|
|
263
|
+
reason: "connect-fallback-null",
|
|
264
|
+
};
|
|
265
|
+
};
|
|
266
|
+
/** Return the versioned .source.xml record for the best-matching version (or null) */
|
|
267
|
+
export async function buildMedrxivLatestSourceXmlUrlViaApi(idOrDoi, opts) {
|
|
268
|
+
return discoverPreprintRecord(idOrDoi, "medrxiv", {
|
|
269
|
+
UA: opts?.UA,
|
|
270
|
+
preferVersion: opts?.preferVersion,
|
|
271
|
+
debug: DEBUG,
|
|
272
|
+
});
|
|
273
|
+
}
|
|
274
|
+
/** Fetch the JATS XML content via the API-discovered URL */
|
|
275
|
+
export async function fetchMedrxivSourceXmlViaApi(idOrDoi, opts) {
|
|
276
|
+
const record = await buildMedrxivLatestSourceXmlUrlViaApi(idOrDoi, opts);
|
|
277
|
+
const core = normPreprintCore(idOrDoi, "medrxiv");
|
|
278
|
+
const inferredVersion = record?.version ??
|
|
279
|
+
opts?.preferVersion ??
|
|
280
|
+
extractVersionFromString(record?.url);
|
|
281
|
+
const jatsUrl = record?.url;
|
|
282
|
+
if (!jatsUrl) {
|
|
283
|
+
return await fetchMedrxivFromConnect(core, inferredVersion, opts);
|
|
284
|
+
}
|
|
285
|
+
const landingUrl = `https://www.medrxiv.org/content/10.1101/${core}`;
|
|
286
|
+
const versionSuffix = (() => {
|
|
287
|
+
if (inferredVersion && Number.isFinite(Number(inferredVersion))) {
|
|
288
|
+
return `v${Number(inferredVersion)}`;
|
|
289
|
+
}
|
|
290
|
+
const match = record?.url && record.url.match(/v(\d+)/i);
|
|
291
|
+
return match ? match[0].toLowerCase() : "";
|
|
292
|
+
})();
|
|
293
|
+
const refererUrl = versionSuffix
|
|
294
|
+
? `${landingUrl}${versionSuffix}`
|
|
295
|
+
: landingUrl;
|
|
296
|
+
DEBUG && console.log("[medrxiv] trying jats url:", jatsUrl);
|
|
297
|
+
const ctx = {
|
|
298
|
+
coreId: core,
|
|
299
|
+
jatsUrl,
|
|
300
|
+
landingUrl,
|
|
301
|
+
refererUrl,
|
|
302
|
+
inferredVersion: inferredVersion ?? null,
|
|
303
|
+
opts,
|
|
304
|
+
shared: {},
|
|
305
|
+
};
|
|
306
|
+
const strategies = [
|
|
307
|
+
directFetchStrategy,
|
|
308
|
+
browserFetchStrategy,
|
|
309
|
+
proxyFetchStrategy,
|
|
310
|
+
connectFetchStrategy,
|
|
311
|
+
];
|
|
312
|
+
for (const strategy of strategies) {
|
|
313
|
+
const result = await strategy(ctx);
|
|
314
|
+
if (result.success) {
|
|
315
|
+
DEBUG && console.log(`[medrxiv] ${result.name} strategy succeeded`);
|
|
316
|
+
return result.data;
|
|
317
|
+
}
|
|
318
|
+
logStrategyFailure(result);
|
|
319
|
+
}
|
|
320
|
+
DEBUG && console.log("[medrxiv] all strategies failed");
|
|
321
|
+
return null;
|
|
322
|
+
}
|
|
323
|
+
async function fetchMedrxivFromConnect(core, version, opts, cookieHeader) {
|
|
324
|
+
if (!version || !Number.isFinite(version))
|
|
325
|
+
return null;
|
|
326
|
+
const defaultUA = opts?.UA || "drylab-reveng-api/1.0";
|
|
327
|
+
const browserUA = defaultUA && /^Mozilla\//.test(defaultUA) ? defaultUA : MEDRXIV_BROWSER_UA;
|
|
328
|
+
const suffix = `v${version}`;
|
|
329
|
+
const baseFile = `${core}${suffix}`;
|
|
330
|
+
const candidates = [
|
|
331
|
+
`https://connect.medrxiv.org/relate/content/181/files/${baseFile}.source.xml`,
|
|
332
|
+
`https://connect.medrxiv.org/relate/content/181/files/${baseFile}.source.xml?download=1`,
|
|
333
|
+
`https://connect.medrxiv.org/relate/content/181/files/${baseFile}.full.xml`,
|
|
334
|
+
`https://connect.medrxiv.org/relate/content/181/files/${baseFile}.full.xml?download=1`,
|
|
335
|
+
`https://connect.medrxiv.org/relate/feed/181/files/${baseFile}.source.xml`,
|
|
336
|
+
`https://connect.medrxiv.org/relate/feed/181/files/${baseFile}.source.xml?download=1`,
|
|
337
|
+
`https://connect.medrxiv.org/relate/feed/181/files/${baseFile}.full.xml`,
|
|
338
|
+
`https://connect.medrxiv.org/relate/feed/181/files/${baseFile}.full.xml?download=1`,
|
|
339
|
+
];
|
|
340
|
+
const queue = [...candidates];
|
|
341
|
+
const tried = new Set();
|
|
342
|
+
let currentCookieHeader = cookieHeader ?? null;
|
|
343
|
+
const maxAttempts = 20;
|
|
344
|
+
while (queue.length && tried.size < maxAttempts) {
|
|
345
|
+
const url = queue.shift();
|
|
346
|
+
if (tried.has(url))
|
|
347
|
+
continue;
|
|
348
|
+
tried.add(url);
|
|
349
|
+
try {
|
|
350
|
+
DEBUG && console.log("[medrxiv] connect fallback url:", url);
|
|
351
|
+
const res = await fetch(url, {
|
|
352
|
+
headers: {
|
|
353
|
+
Accept: MEDRXIV_ACCEPT_XML,
|
|
354
|
+
"User-Agent": browserUA,
|
|
355
|
+
Referer: `https://www.medrxiv.org/content/10.1101/${core}${suffix}`,
|
|
356
|
+
"Accept-Language": MEDRXIV_ACCEPT_LANGUAGE,
|
|
357
|
+
...(currentCookieHeader ? { Cookie: currentCookieHeader } : {}),
|
|
358
|
+
},
|
|
359
|
+
redirect: "follow",
|
|
360
|
+
});
|
|
361
|
+
DEBUG &&
|
|
362
|
+
console.log("[medrxiv] connect status:", res.status, res.headers.get("content-type") || "?");
|
|
363
|
+
if (!res.ok)
|
|
364
|
+
continue;
|
|
365
|
+
const rawSetCookie = res?.headers?.raw?.()?.["set-cookie"] || [];
|
|
366
|
+
if (Array.isArray(rawSetCookie) && rawSetCookie.length) {
|
|
367
|
+
const merged = new Map();
|
|
368
|
+
if (currentCookieHeader) {
|
|
369
|
+
currentCookieHeader.split(/;\s*/).forEach((pair) => {
|
|
370
|
+
const [name, value] = pair.split("=");
|
|
371
|
+
if (name && value !== undefined)
|
|
372
|
+
merged.set(name.trim(), value);
|
|
373
|
+
});
|
|
374
|
+
}
|
|
375
|
+
for (const setCookie of rawSetCookie) {
|
|
376
|
+
const [pair] = String(setCookie).split(";");
|
|
377
|
+
if (!pair)
|
|
378
|
+
continue;
|
|
379
|
+
const [name, value] = pair.split("=");
|
|
380
|
+
if (name && value !== undefined)
|
|
381
|
+
merged.set(name.trim(), value);
|
|
382
|
+
}
|
|
383
|
+
currentCookieHeader = Array.from(merged.entries())
|
|
384
|
+
.map(([name, value]) => `${name}=${value}`)
|
|
385
|
+
.join("; ");
|
|
386
|
+
}
|
|
387
|
+
const rawBuffer = Buffer.from(await res.arrayBuffer());
|
|
388
|
+
const decoded = await decodeConnectResponse(rawBuffer, res.headers);
|
|
389
|
+
if (!decoded) {
|
|
390
|
+
const asText = bufferToUtf8(rawBuffer);
|
|
391
|
+
if (asText) {
|
|
392
|
+
if (DEBUG) {
|
|
393
|
+
console.log("[medrxiv] connect non-jats response", safePreview(asText));
|
|
394
|
+
}
|
|
395
|
+
const extra = collectFollowupUrlsFromText(asText, url);
|
|
396
|
+
if (DEBUG && extra.length) {
|
|
397
|
+
console.log("[medrxiv] connect discovered urls", extra);
|
|
398
|
+
}
|
|
399
|
+
for (const next of extra) {
|
|
400
|
+
if (!tried.has(next))
|
|
401
|
+
queue.push(next);
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
continue;
|
|
405
|
+
}
|
|
406
|
+
if (!looksLikeJatsXml(decoded)) {
|
|
407
|
+
if (DEBUG) {
|
|
408
|
+
console.log("[medrxiv] connect non-jats decoded", safePreview(decoded));
|
|
409
|
+
}
|
|
410
|
+
const extra = collectFollowupUrlsFromText(decoded, url);
|
|
411
|
+
if (DEBUG && extra.length) {
|
|
412
|
+
console.log("[medrxiv] connect discovered urls", extra);
|
|
413
|
+
}
|
|
414
|
+
for (const next of extra) {
|
|
415
|
+
if (!tried.has(next))
|
|
416
|
+
queue.push(next);
|
|
417
|
+
}
|
|
418
|
+
continue;
|
|
419
|
+
}
|
|
420
|
+
return { url, xml: decoded, version };
|
|
421
|
+
}
|
|
422
|
+
catch (err) {
|
|
423
|
+
DEBUG &&
|
|
424
|
+
console.log("[medrxiv] connect fetch error", err?.message ?? String(err));
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
return null;
|
|
428
|
+
}
|
|
429
|
+
async function decodeConnectResponse(rawBuffer, headers) {
|
|
430
|
+
if (!rawBuffer || !rawBuffer.length)
|
|
431
|
+
return null;
|
|
432
|
+
const attempts = [];
|
|
433
|
+
const encoding = (headers.get("content-encoding") || "").toLowerCase().trim();
|
|
434
|
+
if (encoding.includes("gzip")) {
|
|
435
|
+
attempts.push(tryGunzip(rawBuffer));
|
|
436
|
+
}
|
|
437
|
+
else if (encoding.includes("deflate")) {
|
|
438
|
+
attempts.push(tryInflate(rawBuffer));
|
|
439
|
+
}
|
|
440
|
+
// Always attempt common encodings regardless of explicit headers
|
|
441
|
+
attempts.push(tryGunzip(rawBuffer));
|
|
442
|
+
attempts.push(tryInflate(rawBuffer));
|
|
443
|
+
attempts.push(rawBuffer);
|
|
444
|
+
for (const candidate of attempts) {
|
|
445
|
+
if (!candidate || !candidate.length)
|
|
446
|
+
continue;
|
|
447
|
+
const text = bufferToUtf8(candidate);
|
|
448
|
+
if (DEBUG) {
|
|
449
|
+
console.log("[medrxiv] connect decode candidate", {
|
|
450
|
+
size: candidate.length,
|
|
451
|
+
looksLike: text ? looksLikeJatsXml(text) : false,
|
|
452
|
+
preview: text ? safePreview(text, 200) : null,
|
|
453
|
+
});
|
|
454
|
+
}
|
|
455
|
+
if (text && looksLikeJatsXml(text))
|
|
456
|
+
return text;
|
|
457
|
+
}
|
|
458
|
+
if (isZipArchive(rawBuffer)) {
|
|
459
|
+
try {
|
|
460
|
+
const zip = await JSZip.loadAsync(rawBuffer);
|
|
461
|
+
DEBUG &&
|
|
462
|
+
console.log("[medrxiv] connect decode zip entries", Object.keys(zip.files));
|
|
463
|
+
const preferred = Object.values(zip.files)
|
|
464
|
+
.filter((f) => !f.dir)
|
|
465
|
+
.sort((a, b) => scoreZipName(a.name) - scoreZipName(b.name));
|
|
466
|
+
for (const file of preferred) {
|
|
467
|
+
const txt = await file.async("string");
|
|
468
|
+
const cleaned = txt.replace(/^\uFEFF/, "");
|
|
469
|
+
DEBUG &&
|
|
470
|
+
console.log("[medrxiv] connect decode zip entry", file.name, cleaned.slice(0, 200).replace(/\s+/g, " "));
|
|
471
|
+
if (cleaned && looksLikeJatsXml(cleaned))
|
|
472
|
+
return cleaned;
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
catch { }
|
|
476
|
+
}
|
|
477
|
+
return null;
|
|
478
|
+
}
|
|
479
|
+
function tryGunzip(buf) {
|
|
480
|
+
if (!buf || !buf.length)
|
|
481
|
+
return null;
|
|
482
|
+
try {
|
|
483
|
+
return gunzipSync(buf);
|
|
484
|
+
}
|
|
485
|
+
catch { }
|
|
486
|
+
// Some servers return gzip payloads without headers
|
|
487
|
+
if (buf.length > 2 && buf[0] === 0x1f && buf[1] === 0x8b) {
|
|
488
|
+
try {
|
|
489
|
+
return gunzipSync(buf);
|
|
490
|
+
}
|
|
491
|
+
catch { }
|
|
492
|
+
}
|
|
493
|
+
return null;
|
|
494
|
+
}
|
|
495
|
+
function tryInflate(buf) {
|
|
496
|
+
if (!buf || !buf.length)
|
|
497
|
+
return null;
|
|
498
|
+
try {
|
|
499
|
+
return inflateSync(buf);
|
|
500
|
+
}
|
|
501
|
+
catch { }
|
|
502
|
+
try {
|
|
503
|
+
return inflateRawSync(buf);
|
|
504
|
+
}
|
|
505
|
+
catch { }
|
|
506
|
+
return null;
|
|
507
|
+
}
|
|
508
|
+
function bufferToUtf8(buf) {
|
|
509
|
+
if (!buf || !buf.length)
|
|
510
|
+
return null;
|
|
511
|
+
const text = buf.toString("utf-8");
|
|
512
|
+
const stripped = text.replace(/^\uFEFF/, "");
|
|
513
|
+
return stripped.length ? stripped : null;
|
|
514
|
+
}
|
|
515
|
+
function collectFollowupUrlsFromText(text, baseUrl) {
|
|
516
|
+
if (!text.trim())
|
|
517
|
+
return [];
|
|
518
|
+
if (/<html/i.test(text))
|
|
519
|
+
return extractHtmlDownloadUrls(text, baseUrl);
|
|
520
|
+
if (/<rss\b/i.test(text) || /<feed\b/i.test(text))
|
|
521
|
+
return extractFeedDownloadUrls(text, baseUrl);
|
|
522
|
+
return extractGenericXmlLinks(text, baseUrl);
|
|
523
|
+
}
|
|
524
|
+
function extractHtmlDownloadUrls(html, baseUrl) {
|
|
525
|
+
const urls = new Set();
|
|
526
|
+
const normalized = html.replace(/&/gi, "&");
|
|
527
|
+
const metaMatch = normalized.match(/http-equiv=["']refresh["'][^>]*content=["'][^"']*url=([^"' >]+)/i);
|
|
528
|
+
if (metaMatch && metaMatch[1]) {
|
|
529
|
+
urls.add(resolveRelative(metaMatch[1], baseUrl));
|
|
530
|
+
}
|
|
531
|
+
const hrefRegex = /href\s*=\s*["']([^"']+\.(?:source|full)\.xml[^"']*)["']/gi;
|
|
532
|
+
let hrefMatch;
|
|
533
|
+
while ((hrefMatch = hrefRegex.exec(normalized))) {
|
|
534
|
+
urls.add(resolveRelative(hrefMatch[1], baseUrl));
|
|
535
|
+
}
|
|
536
|
+
const locationRegex = /location\s*\.\s*(?:href|replace)\s*=\s*["']([^"']+\.xml[^"']*)["']/gi;
|
|
537
|
+
let locMatch;
|
|
538
|
+
while ((locMatch = locationRegex.exec(normalized))) {
|
|
539
|
+
urls.add(resolveRelative(locMatch[1], baseUrl));
|
|
540
|
+
}
|
|
541
|
+
return Array.from(urls);
|
|
542
|
+
}
|
|
543
|
+
const feedParser = new XMLParser({
|
|
544
|
+
ignoreAttributes: false,
|
|
545
|
+
attributeNamePrefix: "@_",
|
|
546
|
+
textNodeName: "#text",
|
|
547
|
+
});
|
|
548
|
+
function extractFeedDownloadUrls(xmlText, baseUrl) {
|
|
549
|
+
const urls = new Set();
|
|
550
|
+
try {
|
|
551
|
+
const doc = feedParser.parse(xmlText);
|
|
552
|
+
const addCandidate = (candidate) => {
|
|
553
|
+
if (!candidate)
|
|
554
|
+
return;
|
|
555
|
+
const str = String(candidate).trim();
|
|
556
|
+
if (!str)
|
|
557
|
+
return;
|
|
558
|
+
if (/\.xml($|\?|#)/i.test(str))
|
|
559
|
+
urls.add(resolveRelative(str, baseUrl));
|
|
560
|
+
};
|
|
561
|
+
if (doc.feed) {
|
|
562
|
+
const entries = Array.isArray(doc.feed.entry)
|
|
563
|
+
? doc.feed.entry
|
|
564
|
+
: doc.feed.entry
|
|
565
|
+
? [doc.feed.entry]
|
|
566
|
+
: [];
|
|
567
|
+
for (const entry of entries) {
|
|
568
|
+
const links = entry?.link;
|
|
569
|
+
if (Array.isArray(links)) {
|
|
570
|
+
for (const link of links)
|
|
571
|
+
addCandidate(link?.["@_href"] || link?.href);
|
|
572
|
+
}
|
|
573
|
+
else if (links) {
|
|
574
|
+
addCandidate(links?.["@_href"] || links?.href);
|
|
575
|
+
}
|
|
576
|
+
addCandidate(entry?.content?.["@_src"] || entry?.content?.src);
|
|
577
|
+
if (typeof entry?.content === "string")
|
|
578
|
+
addCandidate(entry.content);
|
|
579
|
+
if (typeof entry?.summary === "string")
|
|
580
|
+
addCandidate(entry.summary);
|
|
581
|
+
addCandidate(entry?.id);
|
|
582
|
+
}
|
|
583
|
+
addCandidate(doc.feed?.link?.["@_href"] || doc.feed?.link);
|
|
584
|
+
}
|
|
585
|
+
if (doc.rss?.channel) {
|
|
586
|
+
const channel = doc.rss.channel;
|
|
587
|
+
const items = Array.isArray(channel.item)
|
|
588
|
+
? channel.item
|
|
589
|
+
: channel.item
|
|
590
|
+
? [channel.item]
|
|
591
|
+
: [];
|
|
592
|
+
for (const item of items) {
|
|
593
|
+
const enc = item?.enclosure;
|
|
594
|
+
if (Array.isArray(enc)) {
|
|
595
|
+
for (const e of enc)
|
|
596
|
+
addCandidate(e?.["@_url"] || e?.url);
|
|
597
|
+
}
|
|
598
|
+
else if (enc)
|
|
599
|
+
addCandidate(enc?.["@_url"] || enc?.url);
|
|
600
|
+
addCandidate(item?.link);
|
|
601
|
+
addCandidate(item?.guid);
|
|
602
|
+
if (typeof item?.description === "string")
|
|
603
|
+
addCandidate(item.description);
|
|
604
|
+
}
|
|
605
|
+
addCandidate(channel?.link);
|
|
606
|
+
}
|
|
607
|
+
}
|
|
608
|
+
catch { }
|
|
609
|
+
if (!urls.size) {
|
|
610
|
+
const fallback = extractGenericXmlLinks(xmlText, baseUrl);
|
|
611
|
+
for (const u of fallback)
|
|
612
|
+
urls.add(u);
|
|
613
|
+
}
|
|
614
|
+
return Array.from(urls);
|
|
615
|
+
}
|
|
616
|
+
function extractGenericXmlLinks(text, baseUrl) {
|
|
617
|
+
const urls = new Set();
|
|
618
|
+
const regex = /https?:\/\/[^\s"'>]+\.xml[^\s"'>]*/gi;
|
|
619
|
+
let match;
|
|
620
|
+
while ((match = regex.exec(text))) {
|
|
621
|
+
urls.add(resolveRelative(match[0], baseUrl));
|
|
622
|
+
}
|
|
623
|
+
return Array.from(urls);
|
|
624
|
+
}
|
|
625
|
+
function resolveRelative(candidate, baseUrl) {
|
|
626
|
+
try {
|
|
627
|
+
const trimmed = candidate.trim();
|
|
628
|
+
if (!trimmed)
|
|
629
|
+
return baseUrl;
|
|
630
|
+
const absolute = new URL(trimmed, baseUrl);
|
|
631
|
+
return absolute.toString();
|
|
632
|
+
}
|
|
633
|
+
catch {
|
|
634
|
+
return baseUrl;
|
|
635
|
+
}
|
|
636
|
+
}
|
|
637
|
+
function isZipArchive(buf) {
|
|
638
|
+
return (buf.length > 4 &&
|
|
639
|
+
buf[0] === 0x50 &&
|
|
640
|
+
buf[1] === 0x4b &&
|
|
641
|
+
buf[2] === 0x03 &&
|
|
642
|
+
buf[3] === 0x04);
|
|
643
|
+
}
|
|
644
|
+
function scoreZipName(name) {
|
|
645
|
+
const lower = name.toLowerCase();
|
|
646
|
+
if (lower.endsWith(".source.xml"))
|
|
647
|
+
return 0;
|
|
648
|
+
if (lower.endsWith(".full.xml"))
|
|
649
|
+
return 1;
|
|
650
|
+
if (lower.endsWith(".xml"))
|
|
651
|
+
return 2;
|
|
652
|
+
return 10;
|
|
653
|
+
}
|
|
654
|
+
export const __medrxivTestExports = {
|
|
655
|
+
resolveVersion,
|
|
656
|
+
collectFollowupUrlsFromText,
|
|
657
|
+
};
|
|
658
|
+
/** Get the latest version number for a medRxiv core/DOI (null if unknown). */
|
|
659
|
+
export async function fetchLatestMedrxivVersionNumber(idOrDoi, opts) {
|
|
660
|
+
const rec = await buildMedrxivLatestSourceXmlUrlViaApi(idOrDoi, opts);
|
|
661
|
+
if (!rec)
|
|
662
|
+
return null;
|
|
663
|
+
if (typeof rec.version === "number" && Number.isFinite(rec.version))
|
|
664
|
+
return rec.version;
|
|
665
|
+
// Fallback: try to parse ...vN from the returned URL
|
|
666
|
+
const m = rec.url && rec.url.match(/v(\d+)(?:\D|$)/i);
|
|
667
|
+
if (m && m[1]) {
|
|
668
|
+
const n = parseInt(m[1], 10);
|
|
669
|
+
return Number.isFinite(n) ? n : null;
|
|
670
|
+
}
|
|
671
|
+
return null;
|
|
672
|
+
}
|
|
673
|
+
//# sourceMappingURL=client.js.map
|