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. However, if the package.json was changed to `"type": "commonjs"` we would be able to remove those two lines to make the code even cleaner, since `require()` would then be available automatically.

- Using `createRequire()` is a completely normal, supported interoperability feature of Node, and is documented here:

https://nodejs.org/api/module.html#modulecreaterequirefilename
  • Loading branch information
Arcitec committed Sep 10, 2024
1 parent 5aadc30 commit 8ccb660
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 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
7 changes: 5 additions & 2 deletions update_db.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@ import { get } from 'https';
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 path = join(__dirname, 'src', 'process', 'detectable.json');

const current = JSON.parse(readFileSync(path, 'utf8'));
const current = require(path);

const file = createWriteStream(path);
get('https://discord.com/api/v9/applications/detectable', res => {
Expand All @@ -18,7 +21,7 @@ get('https://discord.com/api/v9/applications/detectable', res => {
file.on('finish', () => {
file.close();

const updated = JSON.parse(readFileSync(path, 'utf8'));
const updated = require(path);
console.log('Updated detectable DB');
console.log(`${current.length} -> ${updated.length} games (+${updated.length - current.length})`);

Expand Down

0 comments on commit 8ccb660

Please sign in to comment.