@@ -6,36 +6,22 @@ import type {
66 ReadSuccessResponse ,
77 ScrapedData ,
88} from '@deepcrawl/types/index' ;
9-
10- import { NodeHtmlMarkdown } from 'node-html-markdown' ;
11-
9+ import type { NodeHtmlMarkdown } from 'node-html-markdown' ;
1210import { KV_CACHE_EXPIRATION_TTL } from '@/config/constants' ;
1311import { ENABLE_READ_CACHE } from '@/config/default-options' ;
1412import type { ORPCContext } from '@/lib/context' ;
15- import { ScrapeService } from '@/services/scrape/scrape.service' ;
1613import { formatDuration } from '@/utils/formater' ;
1714import { getReadCacheKey } from '@/utils/kv/read-kv-key' ;
1815import { kvPutWithRetry } from '@/utils/kv/retry' ;
16+ import { logDebug , logError } from '@/utils/loggers' ;
1917import {
2018 fixCodeBlockFormatting ,
21- nhmCustomTranslators ,
22- nhmTranslators ,
2319 processMultiLineLinks ,
2420 removeNavigationAidLinks ,
2521} from '@/utils/markdown' ;
2622import { cleanEmptyValues } from '@/utils/response/clean-empty-values' ;
2723import { targetUrlHelper } from '@/utils/url/target-url-helper' ;
2824
29- // Create service instance at module level for reuse across requests
30- const scrapeService = new ScrapeService ( ) ;
31-
32- // Pre-configure NodeHtmlMarkdown at module level to avoid per-request setup
33- const markdownConverter = new NodeHtmlMarkdown (
34- /* options */ { } ,
35- /* customTransformers */ nhmCustomTranslators ,
36- /* customCodeBlockTranslators */ nhmTranslators ,
37- ) ;
38-
3925/**
4026 * Calculates performance metrics for the read operation.
4127 * @param startTime - Timestamp in milliseconds when the operation started.
@@ -109,15 +95,19 @@ function hasMeaningfulMarkdown(markdown: string): boolean {
10995}
11096
11197/**
112- * Convert HTML to Markdown
113- * @param param0 - HTML content to convert
98+ * Convert HTML to Markdown using app-level converter
99+ * @param param0 - HTML content and markdown converter
114100 * @returns Markdown content
115101 */
116- function getMarkdown ( { html } : { html : string } ) : string {
102+ function getMarkdown ( {
103+ html,
104+ markdownConverter,
105+ } : {
106+ html : string ;
107+ markdownConverter : NodeHtmlMarkdown ;
108+ } ) : string {
117109 try {
118- const nhm = markdownConverter ;
119-
120- let nhmMarkdown = nhm . translate ( html ) ;
110+ let nhmMarkdown = markdownConverter . translate ( html ) ;
121111 nhmMarkdown = processMultiLineLinks ( nhmMarkdown ) ;
122112 nhmMarkdown = removeNavigationAidLinks ( nhmMarkdown ) ;
123113 nhmMarkdown = fixCodeBlockFormatting ( nhmMarkdown ) ;
@@ -303,6 +293,19 @@ export async function processReadRequest(
303293 requestId : c . var . requestId ,
304294 } ) ;
305295
296+ // Use app-level service instance for optimal performance
297+ const serviceAccessStart = Date . now ( ) ;
298+ const scrapeService = c . var . scrapeService ;
299+ const serviceAccessTime = Date . now ( ) - serviceAccessStart ;
300+
301+ console . log ( '[PERF] Read processor using request-scoped service:' , {
302+ url : targetUrl ,
303+ serviceAccessTime,
304+ hasService : ! ! scrapeService ,
305+ timestamp : new Date ( ) . toISOString ( ) ,
306+ requestId : c . var . requestId ,
307+ } ) ;
308+
306309 const {
307310 title,
308311 rawHtml,
@@ -342,17 +345,20 @@ export async function processReadRequest(
342345 const markdownStart = Date . now ( ) ;
343346
344347 if ( cleanedHtml ) {
345- console . log ( '[PERF] Read processor markdown conversion started:' , {
348+ logDebug ( '[PERF] Read processor markdown conversion started:' , {
346349 url : targetUrl ,
347350 htmlSize : cleanedHtml . length ,
348351 timestamp : new Date ( ) . toISOString ( ) ,
349352 requestId : c . var . requestId ,
350353 } ) ;
351354
352- const convertedMarkdown = getMarkdown ( { html : cleanedHtml } ) ;
355+ const convertedMarkdown = getMarkdown ( {
356+ html : cleanedHtml ,
357+ markdownConverter : c . var . markdownConverter ,
358+ } ) ;
353359 const markdownTime = Date . now ( ) - markdownStart ;
354360
355- console . log ( '[PERF] Read processor markdown conversion completed:' , {
361+ logDebug ( '[PERF] Read processor markdown conversion completed:' , {
356362 url : targetUrl ,
357363 markdownTime,
358364 markdownSize : convertedMarkdown . length ,
@@ -368,7 +374,7 @@ export async function processReadRequest(
368374 // For POST requests with markdown=true but no meaningful content,
369375 // provide informative default markdown instead of undefined
370376 markdown = getDefaultMarkdown ( title , targetUrl , description ) ;
371- console . log (
377+ logDebug (
372378 '[PERF] Read processor using default markdown (no meaningful content):' ,
373379 {
374380 url : targetUrl ,
@@ -381,7 +387,7 @@ export async function processReadRequest(
381387 // For POST requests with markdown=true but no extractable content,
382388 // provide informative default markdown instead of undefined
383389 markdown = getDefaultMarkdown ( title , targetUrl , description ) ;
384- console . log (
390+ logDebug (
385391 '[PERF] Read processor using default markdown (no cleaned HTML):' ,
386392 {
387393 url : targetUrl ,
@@ -418,7 +424,7 @@ export async function processReadRequest(
418424 } ) ;
419425 const responseTime = Date . now ( ) - responseStart ;
420426
421- console . log ( '[PERF] Read processor response prepared:' , {
427+ logDebug ( '[PERF] Read processor response prepared:' , {
422428 url : targetUrl ,
423429 responseTime,
424430 hasResponse : ! ! readResponse ,
@@ -443,7 +449,7 @@ export async function processReadRequest(
443449 )
444450 : JSON . stringify ( readResponse ) ;
445451
446- console . log ( '[PERF] Read processor cache write started:' , {
452+ logDebug ( '[PERF] Read processor cache write started:' , {
447453 url : targetUrl ,
448454 cacheValueSize : valueToCache . length ,
449455 isGETRequest,
@@ -466,15 +472,15 @@ export async function processReadRequest(
466472 ) ;
467473
468474 const cacheWriteTime = Date . now ( ) - cacheWriteStart ;
469- console . log ( '[PERF] Read processor cache write completed:' , {
475+ logDebug ( '[PERF] Read processor cache write completed:' , {
470476 url : targetUrl ,
471477 cacheWriteTime,
472478 timestamp : new Date ( ) . toISOString ( ) ,
473479 requestId : c . var . requestId ,
474480 } ) ;
475481 } catch ( error ) {
476482 const cacheWriteErrorTime = Date . now ( ) - processorStart ;
477- console . error ( '[PERF] Read processor cache write error:' , {
483+ logError ( '[PERF] Read processor cache write error:' , {
478484 url : targetUrl ,
479485 cacheWriteErrorTime,
480486 error : error instanceof Error ? error . message : String ( error ) ,
@@ -487,7 +493,7 @@ export async function processReadRequest(
487493
488494 const totalProcessorTime = Date . now ( ) - processorStart ;
489495
490- console . log ( '[PERF] Read processor completed:' , {
496+ logDebug ( '[PERF] Read processor completed:' , {
491497 url : targetUrl ,
492498 totalProcessorTime,
493499 scrapeTime,
0 commit comments