From b25a5c9b8b22c9a7c334eb96568c8ea0fcbea301 Mon Sep 17 00:00:00 2001 From: chiri Date: Fri, 27 Mar 2026 15:18:23 +0300 Subject: [PATCH] support JSONC comments in config profile validation --- .../config-profiles/create-config-profile.command.ts | 2 +- .../config-profiles/update-config-profile.command.ts | 2 +- package.json | 1 + src/common/helpers/xray-config/xray-config.validator.ts | 5 +++-- 4 files changed, 6 insertions(+), 4 deletions(-) diff --git a/libs/contract/commands/config-profiles/create-config-profile.command.ts b/libs/contract/commands/config-profiles/create-config-profile.command.ts index deaddb92..1bd43c34 100644 --- a/libs/contract/commands/config-profiles/create-config-profile.command.ts +++ b/libs/contract/commands/config-profiles/create-config-profile.command.ts @@ -23,7 +23,7 @@ export namespace CreateConfigProfileCommand { /^[A-Za-z0-9_\s-]+$/, 'Name can only contain letters, numbers, underscores, dashes and spaces', ), - config: z.object({}).passthrough(), + config: z.union([z.string(), z.object({}).passthrough()]), }); export type Request = z.infer; diff --git a/libs/contract/commands/config-profiles/update-config-profile.command.ts b/libs/contract/commands/config-profiles/update-config-profile.command.ts index f357b642..aebb1f75 100644 --- a/libs/contract/commands/config-profiles/update-config-profile.command.ts +++ b/libs/contract/commands/config-profiles/update-config-profile.command.ts @@ -25,7 +25,7 @@ export namespace UpdateConfigProfileCommand { 'Name can only contain letters, numbers, underscores, dashes and spaces', ) .optional(), - config: z.object({}).passthrough().optional(), + config: z.union([z.string(), z.object({}).passthrough()]).optional(), }); export type Request = z.infer; diff --git a/package.json b/package.json index 7961a1c9..653d40a3 100644 --- a/package.json +++ b/package.json @@ -109,6 +109,7 @@ "husky": "9.1.7", "hygen": "^6.2.11", "ioredis": "^5.9.3", + "jsonc-parser": "^3.3.1", "jsonwebtoken": "^9.0.3", "kysely": "^0.28.11", "morgan": "^1.10.1", diff --git a/src/common/helpers/xray-config/xray-config.validator.ts b/src/common/helpers/xray-config/xray-config.validator.ts index 1b1650b3..d04f5aaa 100644 --- a/src/common/helpers/xray-config/xray-config.validator.ts +++ b/src/common/helpers/xray-config/xray-config.validator.ts @@ -1,6 +1,7 @@ import { createPublicKey, createPrivateKey, KeyObject } from 'node:crypto'; import { hasher } from 'node-object-hash'; import { readFileSync } from 'node:fs'; +import { parse } from 'jsonc-parser'; import { HashedSet } from '@remnawave/hashed-set'; @@ -61,9 +62,9 @@ export class XRayConfig { if (typeof configInput === 'string') { try { - config = JSON.parse(configInput) as IXrayConfig; + config = parse(configInput) as IXrayConfig; } catch (error) { - throw new Error(`Invalid JSON input or file path: ${error}`); + throw new Error(`Invalid JSON/JSONC input: ${error}`); } } else if (typeof configInput === 'object') { config = configInput as IXrayConfig;