From ea8e11681ce4711a37920074b91df654c422db6d Mon Sep 17 00:00:00 2001 From: Arcitec <38923130+Arcitec@users.noreply.github.com> Date: Tue, 10 Sep 2024 19:27:25 +0200 Subject: [PATCH] index: use Node's CommonJS JSON loader - Node natively supports loading JSON via `require()` since 2011, which is a cleaner and easier way to load JSON: https://nodejs.org/en/blog/release/v0.5.2 - Node's `require()` safely performs regular JSON decode whenever it detects a ".json" file extension, which is what our "detectable.json" filename is using. - The `createRequire()` API is only necessary because arRPC is not a CommonJS project. Using `createRequire()` is a completely normal, supported interoperability feature of Node, and is documented here: https://nodejs.org/api/module.html#modulecreaterequirefilename - This change was done to help downstream projects based on CommonJS or ASAR bundles. Without this change, arRPC will not work in certain downstream apps (depending on their bundling methods). --- src/process/index.js | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/process/index.js b/src/process/index.js index f445024..cd56b7d 100644 --- a/src/process/index.js +++ b/src/process/index.js @@ -1,12 +1,10 @@ const rgb = (r, g, b, msg) => `\x1b[38;2;${r};${g};${b}m${msg}\x1b[0m`; const log = (...args) => console.log(`[${rgb(88, 101, 242, 'arRPC')} > ${rgb(237, 66, 69, 'process')}]`, ...args); -import fs from 'node:fs'; -import { dirname, join } from 'path'; -import { fileURLToPath } from 'url'; +import { createRequire } from 'node:module'; +const require = createRequire(import.meta.url); -const __dirname = dirname(fileURLToPath(import.meta.url)); -const DetectableDB = JSON.parse(fs.readFileSync(join(__dirname, 'detectable.json'), 'utf8')); +const DetectableDB = require('./detectable.json'); import * as Natives from './native/index.js'; const Native = Natives[process.platform];