Files
sam-react-prod/scripts/patch-json-parse.cjs
유병철 ea6ca335f1 feat: CSP 다음/카카오 도메인 허용 + 입고 성적서 파일 백엔드 연동 + 팝업 이미지 중앙정렬
- middleware CSP: *.kakao.com, *.kakaocdn.net 추가 (다음 주소찾기 차단 해결)
- frame-src에 'self' 추가
- 공지 팝업 이미지 중앙정렬 ([&_img]:mx-auto)
- HR 사원관리, 결재, 품목, 생산 등 다수 개선
- API 에러 핸들링 및 JSON 파싱 안정화
2026-03-11 22:32:58 +09:00

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;
}
};