njsparser/js/mod.js
2026-02-15 00:12:22 +01:00

148 lines
No EOL
3.7 KiB
JavaScript

/**
* NJSParser - NextJS data parser for JavaScript
*
* A powerful parser and explorer for any website built with NextJS.
* Parses flight data, next data, build manifests, and more.
*/
// Parser modules
import { hasFlightData, getRawFlightData, decodeRawFlightData, parseDecodedRawFlightData, getFlightData } from './parser/flight_data.js';
import { hasNextData, getNextData } from './parser/next_data.js';
import { parseBuildManifest, getBuildManifestPath } from './parser/manifests.js';
import { getNextStaticUrls, getBasePath } from './parser/urls.js';
// Type system
import {
Element,
HintPreload,
Module,
Text,
Data,
EmptyData,
SpecialData,
HTMLElement,
DataContainer,
DataParent,
URLQuery,
RSCPayload,
RSCPayloadVersion,
Error as FlightError,
isFlightDataObj,
resolveType,
T
} from './parser/types.js';
// High-level tools
import {
hasNextJS,
finditerInFlightData,
findallInFlightData,
findInFlightData,
findBuildId,
defaultSerializer,
BeautifulFD
} from './tools.js';
// API utilities
import {
getApiPath,
getIndexApiPath,
isApiExposedFromResponse,
listApiPaths
} from './api.js';
// Utilities
import { makeTree, join } from './utils.js';
/**
* NJSParser factory function
* @param {Object} options - Configuration options
* @param {DOMParser} options.DOMParser - DOMParser instance for HTML parsing
* @returns {Object} NJSParser API
*/
export const NJSParser = function({ DOMParser }) {
// Create wrapped functions that include DOMParser
const wrappedHasFlightData = (html) => hasFlightData(html, DOMParser);
const wrappedGetRawFlightData = (html) => getRawFlightData(html, DOMParser);
const wrappedGetFlightData = (html) => getFlightData(html, DOMParser);
const wrappedHasNextData = (html) => hasNextData(html, DOMParser);
const wrappedGetNextData = (html) => getNextData(html, DOMParser);
const wrappedGetNextStaticUrls = (html) => getNextStaticUrls(html, DOMParser);
const wrappedGetBasePath = (htmlOrUrls, removeDomain = false) => {
if (Array.isArray(htmlOrUrls)) {
return getBasePath(htmlOrUrls, null, removeDomain);
}
return getBasePath(htmlOrUrls, DOMParser, removeDomain);
};
const wrappedHasNextJS = (html) => hasNextJS(html, DOMParser);
const wrappedFindBuildId = (html) => findBuildId(html, DOMParser);
const wrappedMakeTree = (html) => makeTree(html, DOMParser);
// Wrapped BeautifulFD class
class WrappedBeautifulFD extends BeautifulFD {
constructor(value) {
super(value, DOMParser);
}
}
return {
// Parser functions
parser: {
hasFlightData: wrappedHasFlightData,
getRawFlightData: wrappedGetRawFlightData,
decodeRawFlightData,
parseDecodedRawFlightData,
getFlightData: wrappedGetFlightData,
hasNextData: wrappedHasNextData,
getNextData: wrappedGetNextData,
parseBuildManifest,
getBuildManifestPath,
getNextStaticUrls: wrappedGetNextStaticUrls,
getBasePath: wrappedGetBasePath
},
// Type system
types: {
Element,
HintPreload,
Module,
Text,
Data,
EmptyData,
SpecialData,
HTMLElement,
DataContainer,
DataParent,
URLQuery,
RSCPayload,
RSCPayloadVersion,
Error: FlightError,
isFlightDataObj,
resolveType,
T
},
// High-level tools
hasNextJS: wrappedHasNextJS,
finditerInFlightData,
findallInFlightData,
findInFlightData,
findBuildId: wrappedFindBuildId,
defaultSerializer,
BeautifulFD: WrappedBeautifulFD,
// API utilities
api: {
getApiPath,
getIndexApiPath,
isApiExposedFromResponse,
listApiPaths
},
// Utilities
utils: {
makeTree: wrappedMakeTree,
join
}
};
};