Skip to content

Commit 8551720

Browse files
committed
fix: add runtime guards for process and document (based on PR #62)
Applied fixes from PR #62 to enable Cloudflare Workers support. Modified to work with current codebase which already removed process.env.CI check from transformer.js. Changes: - Added document existence check in tryParseWithNativeDom - Added defensive guards for process.env.LOG_PERF access - Prevents crashes in environments without process/document globals This fixes issue #42 (Cloudflare Workers compatibility) by using runtime guards instead of build-time flags. Related: PR #62, Issue #42
1 parent 8c5aa49 commit 8551720

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

src/utilities.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,8 @@ export const nodeHtmlParserConfig: NodeHtmlParserOptions = {
141141
function tryParseWithNativeDom(html: string): ElementNode | undefined {
142142
try {
143143
if (!(window?.DOMParser && (new window.DOMParser()).parseFromString('', 'text/html'))) return void 0;
144+
145+
if (!document) return void 0;
144146
}
145147
catch {
146148
return void 0;
@@ -225,11 +227,11 @@ export function getChildNodes(node: HtmlNode | Node): (Node | HtmlNode)[] {
225227
}
226228

227229
export function perfStart(label: string) {
228-
if (process.env.LOG_PERF) console.time(label);
230+
if (process && process.env && process.env.LOG_PERF) console.time(label);
229231
}
230232

231233
export function perfStop(label: string) {
232-
if (process.env.LOG_PERF) console.timeEnd(label);
234+
if (process && process.env && process.env.LOG_PERF) console.timeEnd(label);
233235
}
234236

235237
// endregion

0 commit comments

Comments
 (0)