Skip to content

Commit

Permalink
index: use Node's CommonJS JSON loader
Browse files Browse the repository at this point in the history
- 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).
  • Loading branch information
Arcitec committed Jan 15, 2025
1 parent 5aadc30 commit ea8e116
Showing 1 changed file with 3 additions and 5 deletions.
8 changes: 3 additions & 5 deletions src/process/index.js
Original file line number Diff line number Diff line change
@@ -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];
Expand Down

0 comments on commit ea8e116

Please sign in to comment.