@peam-ai/next 0.1.4 → 0.1.5

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/index.mjs CHANGED
@@ -1,9 +1,7 @@
1
- import { readFileSync } from 'fs';
2
- import { loggers } from 'peam/logger';
3
- import { shouldIncludePath, parseHTML, loadRobotsTxt as loadRobotsTxt$1, createRobotsParser } from 'peam/parser';
4
- import { buildSearchIndex } from 'peam/search';
5
- import * as fs from 'fs/promises';
1
+ import { createRequire } from 'module';
6
2
  import * as path from 'path';
3
+ import * as fsSync from 'fs';
4
+ import * as fs from 'fs/promises';
7
5
 
8
6
  var __create = Object.create;
9
7
  var __defProp = Object.defineProperty;
@@ -108,125 +106,6 @@ var require_picocolors = __commonJS({
108
106
  module2.exports.createColors = createColors;
109
107
  }
110
108
  });
111
- var log = loggers.adapter;
112
- function extractRobotsFromPrerender(prerender) {
113
- var _a;
114
- try {
115
- if (prerender.pathname !== "/robots.txt") {
116
- return null;
117
- }
118
- if ((_a = prerender.fallback) == null ? void 0 : _a.filePath) {
119
- const content = readFileSync(prerender.fallback.filePath, "utf-8");
120
- return content;
121
- }
122
- } catch (error) {
123
- log.error("Error extracting robots from prerender:", error);
124
- }
125
- return null;
126
- }
127
- function loadRobotsTxt(projectDir, prerenders, robotsTxtPath) {
128
- try {
129
- let robotsContent = null;
130
- let foundPath = null;
131
- if (prerenders && prerenders.length > 0) {
132
- for (const prerender of prerenders) {
133
- const content = extractRobotsFromPrerender(prerender);
134
- if (content) {
135
- log.debug("Found dynamic robots.txt from prerenders");
136
- robotsContent = content;
137
- foundPath = prerender.pathname;
138
- break;
139
- }
140
- }
141
- }
142
- if (!robotsContent) {
143
- const searchPaths = ["public/robots.txt", "app/robots.txt", "src/app/robots.txt"];
144
- const result = loadRobotsTxt$1(projectDir, searchPaths, robotsTxtPath);
145
- if (result) {
146
- log.debug("Loaded robots.txt from:", result.path);
147
- return result;
148
- }
149
- return null;
150
- }
151
- return {
152
- parser: createRobotsParser(robotsContent),
153
- path: foundPath || ""
154
- };
155
- } catch (error) {
156
- log.error("Error loading robots.txt:", error);
157
- return null;
158
- }
159
- }
160
- function createPeamAdapter(config) {
161
- return {
162
- name: "peam-adapter",
163
- async onBuildComplete(ctx) {
164
- var _a, _b;
165
- log.debug("Extracting page content via adapter");
166
- const outputs = ctx.outputs;
167
- let prerenders;
168
- if (Array.isArray(outputs)) {
169
- prerenders = outputs.filter((output) => output.type === "PRERENDER");
170
- } else {
171
- prerenders = outputs.prerenders || [];
172
- }
173
- log.debug("Total prerenders:", prerenders.length);
174
- const projectDir = ctx.projectDir || process.cwd();
175
- const robotsResult = config.respectRobotsTxt ? loadRobotsTxt(projectDir, prerenders, config.robotsTxtPath) : null;
176
- if (robotsResult) {
177
- log.debug("Using robots.txt from:", robotsResult.path);
178
- }
179
- const pages = [];
180
- for (const prerender of prerenders) {
181
- const pathname = prerender.pathname;
182
- let fallbackFilePath = (_a = prerender.fallback) == null ? void 0 : _a.filePath;
183
- if (!fallbackFilePath) {
184
- continue;
185
- }
186
- if (fallbackFilePath == null ? void 0 : fallbackFilePath.endsWith("/.html")) {
187
- fallbackFilePath = fallbackFilePath.replace("/.html", "/index.html");
188
- }
189
- const filterResult = shouldIncludePath(
190
- pathname,
191
- (_b = robotsResult == null ? void 0 : robotsResult.parser) != null ? _b : null,
192
- config.exclude,
193
- config.respectRobotsTxt
194
- );
195
- if (!filterResult.included) {
196
- if (filterResult.reason === "robots-txt") {
197
- log.debug("Path excluded by robots.txt:", pathname);
198
- } else if (filterResult.reason === "exclude-pattern") {
199
- log.debug("Path excluded by user pattern:", pathname);
200
- }
201
- continue;
202
- }
203
- try {
204
- log.debug("Reading HTML from:", fallbackFilePath);
205
- const html = readFileSync(fallbackFilePath, "utf-8");
206
- const structuredPage = parseHTML(html);
207
- if (!structuredPage) {
208
- log.warn("No content extracted from", pathname);
209
- continue;
210
- }
211
- log.debug("Successfully extracted content from", pathname);
212
- pages.push({
213
- path: pathname,
214
- htmlFile: fallbackFilePath.replace(projectDir + "/", ""),
215
- structuredPage,
216
- type: "page"
217
- });
218
- } catch (error) {
219
- log.error("Error processing", pathname, error);
220
- }
221
- }
222
- log.debug("Creating search index...");
223
- const searchIndexData = await buildSearchIndex(pages);
224
- await config.searchIndexExporter.export(searchIndexData);
225
- log.debug("Saved search index via exporter");
226
- log.debug("Extraction complete with total pages:", pages.length);
227
- }
228
- };
229
- }
230
109
 
231
110
  // ../logger/dist/index.mjs
232
111
  var import_picocolors = __toESM(require_picocolors());
@@ -270,7 +149,8 @@ function print(message, args, { namespace, color = "white" }) {
270
149
  const ns = namespace ? `${import_picocolors.default.dim(namespace)} ` : "";
271
150
  console.log(header + ns + colorize(String(message)), ...args);
272
151
  }
273
- var loggers2 = {
152
+ var loggers = {
153
+ next: createLogger("peam:next"),
274
154
  search: createLogger("peam:search")
275
155
  };
276
156
 
@@ -2563,11 +2443,12 @@ var __async = (__this, __arguments, generator) => {
2563
2443
  step((generator = generator.apply(__this, __arguments)).next());
2564
2444
  });
2565
2445
  };
2566
- var log3 = loggers2.search;
2446
+ var log3 = loggers.search;
2567
2447
  var FileBasedSearchIndexExporter = class {
2568
2448
  constructor(options) {
2569
2449
  this.cachedData = null;
2570
- this.baseDir = options.baseDir;
2450
+ var _a;
2451
+ this.baseDir = (_a = options.baseDir) != null ? _a : process.cwd();
2571
2452
  this.indexPath = options.indexPath;
2572
2453
  }
2573
2454
  getFullPath() {
@@ -2605,10 +2486,18 @@ var FileBasedSearchIndexExporter = class {
2605
2486
  return data;
2606
2487
  });
2607
2488
  }
2608
- export(data) {
2609
- return __async(this, null, function* () {
2489
+ export(_0) {
2490
+ return __async(this, arguments, function* (data, options = { override: true }) {
2610
2491
  const fullPath = this.getFullPath();
2611
2492
  try {
2493
+ if (!(options == null ? void 0 : options.override)) {
2494
+ try {
2495
+ yield fs.access(fullPath);
2496
+ log3.debug("Search index file already exists and override is false, skipping export:", fullPath);
2497
+ return;
2498
+ } catch (e) {
2499
+ }
2500
+ }
2612
2501
  const dir = path.dirname(fullPath);
2613
2502
  yield fs.mkdir(dir, { recursive: true });
2614
2503
  yield fs.writeFile(fullPath, JSON.stringify(data, null, 2), "utf-8");
@@ -2619,6 +2508,26 @@ var FileBasedSearchIndexExporter = class {
2619
2508
  }
2620
2509
  });
2621
2510
  }
2511
+ exportSync(data, options = { override: true }) {
2512
+ const fullPath = this.getFullPath();
2513
+ try {
2514
+ if (!(options == null ? void 0 : options.override)) {
2515
+ try {
2516
+ fsSync.accessSync(fullPath);
2517
+ log3.debug("Search index file already exists and override is false, skipping export:", fullPath);
2518
+ return;
2519
+ } catch (e) {
2520
+ }
2521
+ }
2522
+ const dir = path.dirname(fullPath);
2523
+ fsSync.mkdirSync(dir, { recursive: true });
2524
+ fsSync.writeFileSync(fullPath, JSON.stringify(data, null, 2), "utf-8");
2525
+ log3.debug("Search index saved to file:", fullPath, "with", data.keys.length, "keys");
2526
+ } catch (error) {
2527
+ log3.error("Failed to save search index to file:", fullPath, error);
2528
+ throw error;
2529
+ }
2530
+ }
2622
2531
  };
2623
2532
  function createExporterFromConfig(exporterConfig) {
2624
2533
  if (exporterConfig.type === "fileBased") {
@@ -2631,7 +2540,7 @@ function createExporterFromConfig(exporterConfig) {
2631
2540
  var defaultConfig = {
2632
2541
  searchExporter: {
2633
2542
  type: "fileBased",
2634
- config: { baseDir: process.cwd(), indexPath: ".peam/index.json" }
2543
+ config: { indexPath: ".peam/index.json" }
2635
2544
  },
2636
2545
  respectRobotsTxt: true,
2637
2546
  exclude: []
@@ -2660,12 +2569,13 @@ var getConfig = () => {
2660
2569
  "Peam configuration not found. Make sure withPeam() is properly configured in your next.config file."
2661
2570
  );
2662
2571
  }
2663
- const searchExporter = {
2572
+ const searchExporterConfig = {
2664
2573
  type: process.env.PEAM_SEARCH_EXPORTER_TYPE,
2665
2574
  config: JSON.parse(process.env.PEAM_SEARCH_EXPORTER_CONFIG)
2666
2575
  };
2667
2576
  const resolvedConfig = {
2668
- searchIndexExporter: createExporterFromConfig(searchExporter),
2577
+ searchExporter: searchExporterConfig,
2578
+ searchIndexExporter: createExporterFromConfig(searchExporterConfig),
2669
2579
  respectRobotsTxt: process.env.PEAM_RESPECT_ROBOTS_TXT === "true",
2670
2580
  robotsTxtPath: process.env.PEAM_ROBOTS_TXT_PATH || void 0,
2671
2581
  exclude: process.env.PEAM_EXCLUDE ? JSON.parse(process.env.PEAM_EXCLUDE) : []
@@ -2674,19 +2584,117 @@ var getConfig = () => {
2674
2584
  };
2675
2585
 
2676
2586
  // src/withPeam.ts
2587
+ var require2 = createRequire(process.cwd() + "/");
2588
+ var log4 = loggers.next;
2589
+ var isProd = process.env.NODE_ENV === "production";
2590
+ function getNextVersion() {
2591
+ try {
2592
+ const [major, minor] = require2("next/package.json").version.split(".", 2).map(Number);
2593
+ return { major, minor };
2594
+ } catch (error) {
2595
+ log4.error("Could not resolve Next.js version.", error);
2596
+ return void 0;
2597
+ }
2598
+ }
2599
+ function addStubIndex() {
2600
+ var _a, _b, _c;
2601
+ try {
2602
+ if (!isProd) {
2603
+ return;
2604
+ }
2605
+ const config = getConfig();
2606
+ if (((_a = config.searchExporter) == null ? void 0 : _a.type) !== "fileBased") {
2607
+ return;
2608
+ }
2609
+ const stubData = { keys: [], data: {} };
2610
+ (_c = (_b = config.searchIndexExporter).exportSync) == null ? void 0 : _c.call(_b, stubData, { override: false });
2611
+ } catch (error) {
2612
+ log4.error("Failed to create stub index:", error);
2613
+ }
2614
+ }
2615
+ function addAdapter(config) {
2616
+ const nextVersion = getNextVersion();
2617
+ if (!nextVersion || nextVersion.major < 16) {
2618
+ log4.warn(
2619
+ "Peam adapter requires Next.js 16 or higher, skipping adapter configuration. Make sure the postbuild script is set up correctly, See more here: https://peam.ai/docs."
2620
+ );
2621
+ return config;
2622
+ }
2623
+ return {
2624
+ ...config,
2625
+ experimental: {
2626
+ ...config.experimental,
2627
+ adapterPath: require2.resolve(path.join(__dirname, "peam.adapter.js"))
2628
+ }
2629
+ };
2630
+ }
2631
+ function addOutputFileTracing(nextConfig, peamConfig) {
2632
+ var _a, _b, _c, _d;
2633
+ nextConfig = { ...nextConfig };
2634
+ if (((_a = peamConfig.searchExporter) == null ? void 0 : _a.type) !== "fileBased") {
2635
+ return nextConfig;
2636
+ }
2637
+ const exporterConfig = peamConfig.searchExporter.config;
2638
+ const indexDir = path.dirname(exporterConfig.indexPath);
2639
+ const tracingConfig = {
2640
+ "/api/peam": [`./${indexDir}/**/*`]
2641
+ };
2642
+ const nextVersion = getNextVersion();
2643
+ if (!nextVersion) {
2644
+ log4.warn(
2645
+ "Could not determine Next.js version. Adding outputFileTracingIncludes to both experimental and root config."
2646
+ );
2647
+ const existingExperimentalTracing = nextConfig.experimental && typeof nextConfig.experimental === "object" && "outputFileTracingIncludes" in nextConfig.experimental ? nextConfig.experimental.outputFileTracingIncludes : void 0;
2648
+ if (nextConfig.experimental) {
2649
+ Object.assign(nextConfig.experimental, {
2650
+ outputFileTracingIncludes: {
2651
+ ...existingExperimentalTracing || {},
2652
+ ...tracingConfig
2653
+ }
2654
+ });
2655
+ }
2656
+ const existingRootTracing = (_b = nextConfig.outputFileTracingIncludes) != null ? _b : void 0;
2657
+ Object.assign(nextConfig, {
2658
+ outputFileTracingIncludes: {
2659
+ ...existingRootTracing || {},
2660
+ ...tracingConfig
2661
+ }
2662
+ });
2663
+ } else if (nextVersion.major < 15) {
2664
+ const existingTracing = typeof nextConfig.experimental === "object" && "outputFileTracingIncludes" in nextConfig.experimental ? (_c = nextConfig.experimental) == null ? void 0 : _c.outputFileTracingIncludes : void 0;
2665
+ if (nextConfig.experimental) {
2666
+ Object.assign(nextConfig.experimental, {
2667
+ outputFileTracingIncludes: {
2668
+ ...existingTracing || {},
2669
+ ...tracingConfig
2670
+ }
2671
+ });
2672
+ }
2673
+ } else {
2674
+ const existingTracing = (_d = nextConfig.outputFileTracingIncludes) != null ? _d : void 0;
2675
+ Object.assign(nextConfig, {
2676
+ outputFileTracingIncludes: {
2677
+ ...existingTracing || {},
2678
+ ...tracingConfig
2679
+ }
2680
+ });
2681
+ }
2682
+ return nextConfig;
2683
+ }
2677
2684
  function withPeam(peamConfig) {
2678
2685
  return function(nextConfig = {}) {
2679
2686
  setNextConfig(nextConfig, peamConfig);
2680
- return {
2681
- ...nextConfig,
2682
- experimental: {
2683
- ...nextConfig.experimental,
2684
- adapterPath: __require.resolve("../dist/peam.adapter.js")
2685
- }
2686
- };
2687
+ addStubIndex();
2688
+ let updatedNextConfig = { ...nextConfig };
2689
+ updatedNextConfig = addAdapter(updatedNextConfig);
2690
+ updatedNextConfig = addOutputFileTracing(updatedNextConfig, getConfig());
2691
+ return updatedNextConfig;
2687
2692
  };
2688
2693
  }
2689
2694
 
2690
- export { createPeamAdapter, getConfig, setNextConfig, withPeam };
2695
+ // src/index.ts
2696
+ var index_default = withPeam;
2697
+
2698
+ export { index_default as default };
2691
2699
  //# sourceMappingURL=index.mjs.map
2692
2700
  //# sourceMappingURL=index.mjs.map