Skip to content

Commit

Permalink
add support for utilizing npm package in ESM environments.
Browse files Browse the repository at this point in the history
  • Loading branch information
solpkr1 committed Jan 7, 2025
1 parent 974dde2 commit 230cf85
Show file tree
Hide file tree
Showing 7 changed files with 270 additions and 9 deletions.
98 changes: 98 additions & 0 deletions yellowstone-grpc-client-nodejs/add-js-extensions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
const fs = require('fs');
const path = require('path');
const recast = require('recast');

//list of external packages that require '.js' extensions
const packagesRequiringJsExtension = [
'protobufjs/minimal',
//add other package paths as needed
];

function shouldAppendJsExtension(source) {
//check if the path has an extension already
if (path.extname(source)) {
return false;
}

//check if the path is relative
if (source.startsWith('./') || source.startsWith('../')) {
return true;
}

//check if the path is in the whitelist of external packages
return packagesRequiringJsExtension.some(pkg => source === pkg || source.startsWith(`${pkg}/`));
}


function processFile(filePath) {
const code = fs.readFileSync(filePath, 'utf8');
const ast = recast.parse(code, {
parser: require('recast/parsers/babel'), // Use Babel parser
});

let modified = false;

recast.types.visit(ast, {
visitImportDeclaration(pathNode) {
const source = pathNode.node.source.value;
if (shouldAppendJsExtension(source)) {
pathNode.node.source.value = `${source}.js`;
modified = true;
}
this.traverse(pathNode);
},
visitExportNamedDeclaration(pathNode) {
if (pathNode.node.source && pathNode.node.source.value) {
const source = pathNode.node.source.value;
if (shouldAppendJsExtension(source)) {
pathNode.node.source.value = `${source}.js`;
modified = true;
}
}
this.traverse(pathNode);
},
visitExportAllDeclaration(pathNode) {
if (pathNode.node.source && pathNode.node.source.value) {
const source = pathNode.node.source.value;
if (shouldAppendJsExtension(source)) {
pathNode.node.source.value = `${source}.js`;
modified = true;
}
}
this.traverse(pathNode);
},
});

if (modified) {
const output = recast.print(ast).code;
fs.writeFileSync(filePath, output, 'utf8');
console.log(`Updated import/export paths in: ${filePath}`);
}
}


function traverseDir(dir) {
fs.readdirSync(dir).forEach((file) => {
const fullPath = path.join(dir, file);
const stat = fs.statSync(fullPath);

if (stat.isDirectory()) {
traverseDir(fullPath);
} else if (stat.isFile() && path.extname(fullPath) === '.js') {
processFile(fullPath);
}
});
}

function main() {
const esmDir = path.resolve(__dirname, './dist/esm');

if (!fs.existsSync(esmDir)) {
console.error(`Directory not found: ${esmDir}`);
process.exit(1);
}

traverseDir(esmDir);
}

main();
126 changes: 123 additions & 3 deletions yellowstone-grpc-client-nodejs/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 14 additions & 5 deletions yellowstone-grpc-client-nodejs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
"license": "Apache-2.0",
"author": "Triton One",
"description": "Yellowstone gRPC Geyser Node.js Client",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"main": "dist/cjs/index.js",
"types": "dist/types/index.d.ts",
"scripts": {
"build": "npm run grpc-generate && tsc -p .",
"build": "npm run grpc-generate && tsc --project tsconfig.esm.json && tsc --project tsconfig.cjs.json && npm run cp-encoding-files && node add-js-extensions.js",
"cp-encoding-files": "mkdir -p dist/esm/encoding && cp -r src/encoding/* dist/cjs/encoding/ && mkdir -p dist/esm/encoding && cp -r src/encoding/* dist/esm/encoding/",
"fmt": "prettier -w .",
"grpc-generate": "mkdir -p src/grpc && protoc -I../yellowstone-grpc-proto/proto --plugin=node_modules/.bin/protoc-gen-ts_proto --ts_proto_opt=forceLong=string --ts_proto_opt=outputServices=grpc-js --experimental_allow_proto3_optional --ts_proto_out=src/grpc geyser.proto"
"grpc-generate": "mkdir -p src/grpc && protoc -I../yellowstone-grpc-proto/proto --plugin=node_modules/.bin/protoc-gen-ts_proto --ts_proto_opt=forceLong=string --ts_proto_opt=outputServices=grpc-js --experimental_allow_proto3_optional --ts_proto_out=src/grpc geyser.proto --ts_proto_opt=esModuleInterop=true"
},
"repository": {
"type": "git",
Expand All @@ -29,8 +30,10 @@
"@grpc/grpc-js": "^1.8.0"
},
"devDependencies": {
"@babel/parser": "^7.26.3",
"@solana/rpc-api": "=2.0.0",
"prettier": "^2.8.3",
"recast": "^0.23.9",
"ts-proto": "^1.139.0",
"typescript": "=5.2.2"
},
Expand All @@ -39,5 +42,11 @@
},
"files": [
"dist"
]
],
"exports": {
".": {
"import": "./dist/esm/index.js",
"require": "./dist/cjs/index.js"
}
}
}
2 changes: 1 addition & 1 deletion yellowstone-grpc-client-nodejs/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ export default class Client {
commitment,
accountsDataSlice,
},
(err) => {
(err: any) => {
if (err === null || err === undefined) {
resolve();
} else {
Expand Down
18 changes: 18 additions & 0 deletions yellowstone-grpc-client-nodejs/tsconfig.cjs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"compilerOptions": {
"module": "CommonJS",
"esModuleInterop": true,
"noImplicitAny": true,
"removeComments": true,
"preserveConstEnums": true,
"sourceMap": true,
"allowJs": true,
"outDir": "dist/cjs",
"moduleResolution": "Node",
"declaration": true,
"declarationDir": "dist/types"
},
"files": ["src/index.ts"],
"include": ["src/**/*"],
"exclude": ["node_modules", "**/*.test.js"]
}
15 changes: 15 additions & 0 deletions yellowstone-grpc-client-nodejs/tsconfig.esm.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"compilerOptions": {
"module": "ESNext",
"target": "ES2022",
"moduleResolution": "Node",
"esModuleInterop": true,
"outDir": "dist/esm",
"declaration": true,
"declarationDir": "dist/types"
},
"files": ["src/index.ts"],
"include": ["src/**/*"],
"exclude": ["node_modules", "**/*.test.js"]
}

1 change: 1 addition & 0 deletions yellowstone-grpc-client-nodejs/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"compilerOptions": {
"esModuleInterop": true,
"outDir": "dist",
"rootDir": "src",
"declaration": true
Expand Down

0 comments on commit 230cf85

Please sign in to comment.