mdorigin 0.3.0 → 0.3.2

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.
Files changed (2) hide show
  1. package/dist/search.js +18 -86
  2. package/package.json +2 -2
package/dist/search.js CHANGED
@@ -1,14 +1,10 @@
1
- import { rmSync } from 'node:fs';
2
- import { mkdtemp, mkdir, readdir, readFile, realpath, stat, writeFile, } from 'node:fs/promises';
3
- import os from 'node:os';
1
+ import { mkdir, readdir, readFile, realpath, stat, writeFile, } from 'node:fs/promises';
4
2
  import path from 'node:path';
5
3
  import { inferDirectoryContentType } from './core/content-type.js';
6
4
  import { getDirectoryIndexCandidates } from './core/directory-index.js';
7
5
  import { getDocumentSummary, getDocumentTitle, parseMarkdownDocument, stripMachineOnlyMarkdownComments, stripManagedIndexBlock, } from './core/markdown.js';
8
6
  import { isIgnoredContentName } from './core/content-store.js';
9
7
  const OVERVIEW_CONTENT_FILENAMES = new Set(['readme.md', 'index.md', 'skill.md']);
10
- const materializedSearchBundleDirectories = new Set();
11
- let searchBundleDirectoryCleanupRegistered = false;
12
8
  export async function buildSearchBundle(options) {
13
9
  const buildModule = await loadIndexbindBuildModule();
14
10
  const rootDir = path.resolve(options.rootDir);
@@ -364,13 +360,9 @@ async function loadIndexbindCloudflareModule() {
364
360
  }
365
361
  }
366
362
  async function openWebIndexFromVirtualBundle(bundleEntries, loadResponse) {
367
- if (isNodeRuntime()) {
368
- return openWebIndexFromMaterializedBundle(bundleEntries, loadResponse);
369
- }
370
363
  const baseUrl = 'https://mdorigin-search.invalid/';
371
- const originalFetch = globalThis.fetch;
372
364
  const bundleMap = new Map(bundleEntries.map((entry) => [new URL(entry.path, baseUrl).toString(), entry]));
373
- globalThis.fetch = async (input, init) => {
365
+ const bundleFetch = async (input, init) => {
374
366
  const requestUrl = typeof input === 'string'
375
367
  ? input
376
368
  : input instanceof URL
@@ -380,23 +372,22 @@ async function openWebIndexFromVirtualBundle(bundleEntries, loadResponse) {
380
372
  if (entry) {
381
373
  return loadResponse(entry);
382
374
  }
383
- return originalFetch(input, init);
375
+ return fetch(input, init);
384
376
  };
385
377
  try {
386
- try {
387
- const cloudflareModule = await loadIndexbindCloudflareModule();
388
- return await cloudflareModule.openWebIndex(new URL(baseUrl));
389
- }
390
- catch (error) {
391
- if (!isCloudflareWasmImportError(error)) {
392
- throw error;
393
- }
394
- const webModule = await loadIndexbindWebModule();
395
- return await webModule.openWebIndex(new URL(baseUrl));
396
- }
378
+ const cloudflareModule = await loadIndexbindCloudflareModule();
379
+ return await cloudflareModule.openWebIndex(new URL(baseUrl), {
380
+ fetch: bundleFetch,
381
+ });
397
382
  }
398
- finally {
399
- globalThis.fetch = originalFetch;
383
+ catch (error) {
384
+ if (!isCloudflareWasmImportError(error)) {
385
+ throw error;
386
+ }
387
+ const webModule = await loadIndexbindWebModule();
388
+ return await webModule.openWebIndex(new URL(baseUrl), {
389
+ fetch: bundleFetch,
390
+ });
400
391
  }
401
392
  }
402
393
  async function openSearchContextFromDirectory(indexDir) {
@@ -414,25 +405,6 @@ async function openSearchContextFromBundle(bundleEntries, loadResponse) {
414
405
  ]);
415
406
  return { index, documentsById };
416
407
  }
417
- async function openWebIndexFromMaterializedBundle(bundleEntries, loadResponse) {
418
- const tempDir = await mkdtemp(path.join(os.tmpdir(), 'mdorigin-search-bundle-'));
419
- registerMaterializedSearchBundleDirectory(tempDir);
420
- for (const entry of bundleEntries) {
421
- const response = await loadResponse(entry);
422
- if (!response.ok) {
423
- throw new Error(`Failed to materialize search bundle file ${entry.path}: ${response.status} ${response.statusText}`);
424
- }
425
- const outputPath = resolveMaterializedSearchBundlePath(tempDir, entry.path);
426
- await mkdir(path.dirname(outputPath), { recursive: true });
427
- if (entry.mediaType.startsWith('text/') || entry.mediaType.includes('json')) {
428
- await writeFile(outputPath, await response.text(), 'utf8');
429
- continue;
430
- }
431
- await writeFile(outputPath, new Uint8Array(await response.arrayBuffer()));
432
- }
433
- const webModule = await loadIndexbindWebModule();
434
- return webModule.openWebIndex(tempDir);
435
- }
436
408
  function createInlineSearchBundleResponse(entry) {
437
409
  const headers = new Headers({ 'content-type': entry.mediaType });
438
410
  const binaryBody = decodeBase64(entry.base64 ?? '');
@@ -457,7 +429,9 @@ function isCloudflareWasmImportError(error) {
457
429
  visited.add(current);
458
430
  if (current instanceof Error) {
459
431
  const currentMessage = current.message;
460
- if (targetMessages.some((message) => currentMessage.includes(message))) {
432
+ if (targetMessages.some((message) => currentMessage.includes(message)) ||
433
+ (currentMessage.includes('Cannot find module') &&
434
+ currentMessage.includes('indexbind_wasm_bg.js'))) {
461
435
  return true;
462
436
  }
463
437
  }
@@ -539,48 +513,6 @@ async function loadBundleResponseText(entry, loadResponse) {
539
513
  }
540
514
  return response.text();
541
515
  }
542
- function isNodeRuntime() {
543
- return (typeof process !== 'undefined' &&
544
- typeof process.versions === 'object' &&
545
- typeof process.versions.node === 'string');
546
- }
547
- function resolveMaterializedSearchBundlePath(baseDir, entryPath) {
548
- if (path.isAbsolute(entryPath)) {
549
- throw new Error(`Search bundle path must be relative: ${entryPath}`);
550
- }
551
- const normalizedEntryPath = entryPath.replaceAll('/', path.sep);
552
- const outputPath = path.resolve(baseDir, normalizedEntryPath);
553
- const relativeOutputPath = path.relative(baseDir, outputPath);
554
- if (relativeOutputPath === '' ||
555
- relativeOutputPath.startsWith(`..${path.sep}`) ||
556
- relativeOutputPath === '..' ||
557
- path.isAbsolute(relativeOutputPath)) {
558
- throw new Error(`Search bundle path escapes materialized bundle directory: ${entryPath}`);
559
- }
560
- return outputPath;
561
- }
562
- function registerMaterializedSearchBundleDirectory(directoryPath) {
563
- materializedSearchBundleDirectories.add(directoryPath);
564
- if (searchBundleDirectoryCleanupRegistered || !isNodeRuntime()) {
565
- return;
566
- }
567
- const cleanup = () => {
568
- for (const stagedDirectory of materializedSearchBundleDirectories) {
569
- rmSync(stagedDirectory, { recursive: true, force: true });
570
- }
571
- materializedSearchBundleDirectories.clear();
572
- };
573
- process.once('exit', cleanup);
574
- process.once('SIGINT', () => {
575
- cleanup();
576
- process.exit(130);
577
- });
578
- process.once('SIGTERM', () => {
579
- cleanup();
580
- process.exit(143);
581
- });
582
- searchBundleDirectoryCleanupRegistered = true;
583
- }
584
516
  function rerankSearchHits(hits) {
585
517
  const remaining = [...hits];
586
518
  const ordered = [];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mdorigin",
3
- "version": "0.3.0",
3
+ "version": "0.3.2",
4
4
  "type": "module",
5
5
  "description": "Markdown-first publishing for humans and agents.",
6
6
  "repository": {
@@ -78,6 +78,6 @@
78
78
  "typescript": "^5.9.2"
79
79
  },
80
80
  "optionalDependencies": {
81
- "indexbind": "^0.3.0"
81
+ "indexbind": "^0.3.3"
82
82
  }
83
83
  }