Skip to content

Commit

Permalink
updating-lib (#412)
Browse files Browse the repository at this point in the history
  • Loading branch information
Shyam-Raghuwanshi authored Oct 7, 2024
1 parent d116798 commit c6527d5
Show file tree
Hide file tree
Showing 13 changed files with 99 additions and 335 deletions.
4 changes: 2 additions & 2 deletions JS/edgechains/arakoodev/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
"./arakooserver": "./dist/arakooserver/src/index.js",
"./db": "./dist/db/src/index.js",
"./scraper": "./dist/scraper/src/index.js",
"./sync-rpc": "./dist/sync-rpc/export.js"
"./sync-rpc": "./dist/sync-rpc/index.js"
},
"scripts": {
"build": "rm -rf dist && tsc -b && cp -r src/sync-rpc dist/sync-rpc",
"build": "rm -rf dist && tsc -b",
"lint": "eslint --ignore-path .eslintignore --ext .js,.ts",
"format": "prettier --ignore-path .gitignore --write \"**/*.+(js|ts|json)\"",
"test": "vitest"
Expand Down
9 changes: 0 additions & 9 deletions JS/edgechains/arakoodev/src/sync-rpc/export.js

This file was deleted.

14 changes: 0 additions & 14 deletions JS/edgechains/arakoodev/src/sync-rpc/find-port.js

This file was deleted.

178 changes: 0 additions & 178 deletions JS/edgechains/arakoodev/src/sync-rpc/index.js

This file was deleted.

2 changes: 2 additions & 0 deletions JS/edgechains/arakoodev/src/sync-rpc/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import createSyncRPC from "./lib/sync-rpc.js";
export { createSyncRPC };
49 changes: 0 additions & 49 deletions JS/edgechains/arakoodev/src/sync-rpc/json.js

This file was deleted.

85 changes: 85 additions & 0 deletions JS/edgechains/arakoodev/src/sync-rpc/lib/sync-rpc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import { execSync } from 'child_process';
import path from 'path';
import fs from 'fs';
import crypto from 'crypto';
import os from 'os';

function createSyncRPC(filename: string) {
const absolutePath = path.resolve(filename);

if (!fs.existsSync(absolutePath)) {
throw new Error(`File not found: ${absolutePath}`);
}

const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'sync-rpc-'));
const hash = crypto.createHash('md5').update(absolutePath).digest('hex');
const wrapperPath = path.join(tempDir, `wrapper_${hash}.js`);

const wrapperCode = `
const fn = require(${JSON.stringify(absolutePath)});
if (typeof fn !== 'function') {
throw new Error('Exported value is not a function');
}
process.on('message', (message) => {
fn(message)
.then(result => {
process.send({ success: true, result });
})
.catch(error => {
process.send({ success: false, error: error.message });
});
});
`;

fs.writeFileSync(wrapperPath, wrapperCode);

return function syncRPC(args: any) {
const scriptPath = path.join(tempDir, `script_${Date.now()}.js`);
const scriptContent = `
const cp = require('child_process');
const child = cp.fork(${JSON.stringify(wrapperPath)});
child.send(${JSON.stringify(args)});
child.on('message', (message) => {
console.log(JSON.stringify(message));
child.kill();
process.exit(0);
});
`;

fs.writeFileSync(scriptPath, scriptContent);

try {
const output = execSync(`node ${scriptPath}`, {
encoding: 'utf8',
stdio: ['pipe', 'pipe', 'inherit']
});

fs.unlinkSync(scriptPath);

const trimmedOutput = output.trim();

try {
const result = JSON.parse(trimmedOutput);
if (!result.success) {
throw new Error(result.error);
}
return JSON.stringify(result.result);
} catch (parseError: any) {
console.error('Raw output:', trimmedOutput);
throw new Error(`Failed to parse output as JSON: ${parseError.message}\nRaw output: ${trimmedOutput}`);
}
} catch (error: any) {
fs.unlinkSync(scriptPath);
if (error) {
console.error('Execution error:', error.message);
if (error.stderr) {
console.error('stderr:', error.stderr);
}
throw new Error(`Execution error: ${error.message}`);
}
throw error;
}
};
}

export = createSyncRPC as Function;
Loading

0 comments on commit c6527d5

Please sign in to comment.