Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Switch back to safe-stringify
Browse files Browse the repository at this point in the history
ascorbic committed Dec 18, 2024
1 parent 9ba81a8 commit df4760e
Showing 2 changed files with 25 additions and 3 deletions.
5 changes: 2 additions & 3 deletions packages/astro/src/content/content-layer.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { promises as fs, existsSync } from 'node:fs';
import { deterministicString } from 'deterministic-object-hash';
import PQueue from 'p-queue';
import type { FSWatcher } from 'vite';
import xxhash from 'xxhash-wasm';
import { AstroError, AstroErrorData } from '../core/errors/index.js';
import type { Logger } from '../core/logger/core.js';
import type { AstroSettings } from '../types/astro.js';

import type { ContentEntryType, RefreshContentOptions } from '../types/public/content.js';
import {
ASSET_IMPORTS_FILE,
@@ -21,6 +19,7 @@ import {
getEntryConfigByExtMap,
getEntryDataAndImages,
globalContentConfigObserver,
safeStringify,
} from './utils.js';

export interface ContentLayerOptions {
@@ -152,7 +151,7 @@ export class ContentLayer {
...hashableConfig
} = this.#settings.config;

const astroConfigDigest = deterministicString(hashableConfig);
const astroConfigDigest = safeStringify(hashableConfig);

const { digest: currentConfigDigest } = contentConfig.config;
this.#lastConfigDigest = currentConfigDigest;
23 changes: 23 additions & 0 deletions packages/astro/src/content/utils.ts
Original file line number Diff line number Diff line change
@@ -832,3 +832,26 @@ export function contentModuleToId(fileName: string) {
params.set(CONTENT_MODULE_FLAG, 'true');
return `${DEFERRED_MODULE}?${params.toString()}`;
}

// Based on https://github.com/sindresorhus/safe-stringify
function safeStringifyReplacer(seen: WeakSet<object>) {
return function (_key: string, value: unknown) {
if (!(value !== null && typeof value === 'object')) {
return value;
}
if (seen.has(value)) {
return '[Circular]';
}
seen.add(value);
const newValue = Array.isArray(value) ? [] : {};
for (const [key2, value2] of Object.entries(value)) {
(newValue as Record<string, unknown>)[key2] = safeStringifyReplacer(seen)(key2, value2);
}
seen.delete(value);
return newValue;
};
}
export function safeStringify(value: unknown) {
const seen = new WeakSet();
return JSON.stringify(value, safeStringifyReplacer(seen));
}

0 comments on commit df4760e

Please sign in to comment.