OpenViSTAI · Agents · Blockchain · Crypto
Menu
OPENVIST RECIPES

Find segment-boundary prefix overlaps in route paths with JavaScript

JavaScript

Report route pairs where one supplied path contains the other as a segment-boundary prefix.

JavaScript code excerpt: Find segment-boundary prefix overlaps in route paths with JavaScript. Version 0.1.0. Read the full recipe for code, sources and recorded test scope.

Report route pairs where one supplied path contains the other as a segment-boundary prefix. This is a local data check under the documented conventions; it does not execute tools or contact a service.

Before you start

  • Save as recipe.cjs. Usage: node recipe.cjs < input.json. Supply a JSON array through stdin; no CLI arguments or help option are accepted.
  • Requires Node.js 22 CommonJS and standard library only. The only loaded module is node:fs, used solely for bounded readSync calls on stdin descriptor 0. The program does not open input paths, write files, execute external commands or contact a network.
  • Global TextDecoder uses fatal:true and ignoreBOM:true. Malformed UTF-8 is rejected. A leading UTF-8 BOM is preserved and rejected by JSON.parse. Native JSON.parse semantics apply.
  • Each path must match the entire ASCII grammar: /, or slash-separated nonempty segments containing only letters, digits, underscores and hyphens. Only / may end in /. Whitespace, repeated slashes, dots, backslashes and non-ASCII characters are rejected.
  • Comparisons are case-sensitive. Equal paths and any pair containing / overlap; otherwise one path must begin with the other followed by /. Output indices are one-based positions in the supplied array, with first < second. Records are ordered by first and then second. An empty array is valid. There are no input integer fields or keyed collections.
  • Successful processing exits 0, emits exactly one JSON object followed by a newline, and emits no stderr. Invalid arguments, input or stdin read failures emit no stdout, emit the generic lowercase diagnostic error: invalid input on stderr, and exit 2.

Build and check

  1. Use Node.js 22 CommonJS. Save the code as recipe.cjs.
  2. Save the supplied JSON example as input.json, encoded as UTF-8 without a BOM.
  3. Run: node recipe.cjs < input.json
  4. Compare the output with the expected result; handle exit code 2 as invalid input.

Limits to understand

  • Reads at most 65537 bytes and rejects inputs larger than 65536 bytes. Accepts at most 20 paths, each nonempty and at most 120 characters. Produces at most 190 overlap records.
  • Uses a fixed 65537-byte read buffer and holds decoded text, parsed input and results in memory. Stdin can block while waiting for EOF.
  • Successful output can reveal relationships between sensitive routes, even though it contains indices rather than route strings.
  • Output-write failures are excluded from the exit-status and diagnostic guarantees.
  • This is a lexical audit, not a router precedence or security analysis. It does not normalize paths, decode URL escapes or model provider integrations. No production suitability or uniqueness is claimed.

Run it

node recipe.cjs < input.json

Example input

["/api", "/api/v1", "/apix"]

Expected output

{"overlaps": [{"first": 1, "second": 2}]}
INSPECT THE SCRIPT

Inspect, copy and run the documented checks.

This browser preview builds code locally. It does not contact an API or run the code.

Default example ready.

JavaScript example
'use strict';

let result;

try {
  if (process.argv.length !== 2) {
    throw new Error();
  }

  const { readSync } = require('node:fs');
  const buffer = Buffer.alloc(65537);
  let used = 0;

  while (used < buffer.length) {
    const count = readSync(0, buffer, used, buffer.length - used, null);
    if (count === 0) break;
    used += count;
  }

  if (used > 65536) {
    throw new Error();
  }

  const decoder = new TextDecoder('utf-8', {
    fatal: true,
    ignoreBOM: true
  });
  const text = decoder.decode(buffer.subarray(0, used));
  const paths = JSON.parse(text);

  if (!Array.isArray(paths) || paths.length > 20) {
    throw new Error();
  }

  // Require the absolute end of the string, including after newlines.
  const pathPattern = /^\/(?:[A-Za-z0-9_-]+(?:\/[A-Za-z0-9_-]+)*)?(?![\s\S])/;
  for (const path of paths) {
    if (typeof path !== 'string' || path.length === 0 ||
        path.length > 120 || !pathPattern.test(path)) {
      throw new Error();
    }
  }

  const overlaps = [];
  for (let i = 0; i < paths.length; i++) {
    for (let j = i + 1; j < paths.length; j++) {
      const first = paths[i];
      const second = paths[j];
      if (first === second || first === '/' || second === '/' ||
          first.startsWith(second + '/') ||
          second.startsWith(first + '/')) {
        overlaps.push({ first: i + 1, second: j + 1 });
      }
    }
  }

  result = { overlaps };
} catch {
  process.stderr.write('error: invalid input\n');
  process.exit(2);
}

// Output-write failures are outside input-error handling.
process.stdout.write(JSON.stringify(result) + '\n');
What to verify after running
  • The supplied example must match the documented JSON output.
  • Error cases must exit 2 with no stdout. Review sensitive successful output before sharing it.

Request or format preview

Illustrative preview. No provider response, price or balance is fetched.

Change a setting to inspect the preview.

Official sources and related profiles

    Code license: MIT. Keep the license notice with your copy.

    Example version 0.1.0 · Documentation reviewed 2026-10-14.

    Six independent CLI fixtures and 10 curated supplementary checks passed in the pinned offline Node.js 22 CommonJS container. This does not establish exhaustive correctness, production performance or compatibility with an external provider.

    Related browser tools

    Inspect related data locally in your browser. These tools have their own input rules and limits; they do not execute this recipe.