47 lines
1.7 KiB
JavaScript
47 lines
1.7 KiB
JavaScript
|
|
/**
|
||
|
|
* JSON.parse 글로벌 패치 - macOS 26 파일시스템 손상 대응
|
||
|
|
*
|
||
|
|
* macOS 26에서 atomic write(tmp + rename)가 실패하면
|
||
|
|
* .next/prerender-manifest.json 등의 파일에 데이터가 중복 기록됨.
|
||
|
|
* 이로 인해 "Unexpected non-whitespace character after JSON at position N" 발생.
|
||
|
|
*
|
||
|
|
* 이 패치는 JSON.parse 실패 시 유효한 JSON 부분만 추출하여 자동 복구.
|
||
|
|
* NODE_OPTIONS='--require ./scripts/patch-json-parse.cjs' 로 로드.
|
||
|
|
*/
|
||
|
|
'use strict';
|
||
|
|
|
||
|
|
const originalParse = JSON.parse;
|
||
|
|
|
||
|
|
JSON.parse = function patchedJsonParse(text, reviver) {
|
||
|
|
try {
|
||
|
|
return originalParse.call(this, text, reviver);
|
||
|
|
} catch (e) {
|
||
|
|
if (e instanceof SyntaxError && typeof text === 'string') {
|
||
|
|
// "Unexpected non-whitespace character after JSON at position N"
|
||
|
|
// → position N까지가 유효한 JSON
|
||
|
|
const match = e.message.match(/after JSON at position\s+(\d+)/);
|
||
|
|
if (match) {
|
||
|
|
const pos = parseInt(match[1], 10);
|
||
|
|
if (pos > 0) {
|
||
|
|
try {
|
||
|
|
const result = originalParse.call(this, text.substring(0, pos), reviver);
|
||
|
|
// 한 번만 경고 (같은 position이면 반복 출력 방지)
|
||
|
|
if (!patchedJsonParse._warned) patchedJsonParse._warned = new Set();
|
||
|
|
const key = pos + ':' + text.length;
|
||
|
|
if (!patchedJsonParse._warned.has(key)) {
|
||
|
|
patchedJsonParse._warned.add(key);
|
||
|
|
console.warn(
|
||
|
|
`[patch-json-parse] macOS 파일 손상 자동 복구 (position ${pos}, total ${text.length} bytes)`
|
||
|
|
);
|
||
|
|
}
|
||
|
|
return result;
|
||
|
|
} catch {
|
||
|
|
// truncation으로도 실패하면 원래 에러 throw
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
throw e;
|
||
|
|
}
|
||
|
|
};
|