Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(web-sdk): Add simple nodejs script to generate top level exports for web-sdk #28

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 46 additions & 44 deletions gen/docs/docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,22 +55,22 @@
- [RedeemerPurpose](#utxorpc-v1alpha-cardano-RedeemerPurpose)

- [utxorpc/v1alpha/build/build.proto](#utxorpc_v1alpha_build_build-proto)
- [AnyChainUtxo](#utxorpc-v1-build-AnyChainUtxo)
- [ChainParam](#utxorpc-v1-build-ChainParam)
- [ChainPoint](#utxorpc-v1-build-ChainPoint)
- [GetChainParamRequest](#utxorpc-v1-build-GetChainParamRequest)
- [GetChainParamResponse](#utxorpc-v1-build-GetChainParamResponse)
- [GetChainTipRequest](#utxorpc-v1-build-GetChainTipRequest)
- [GetChainTipResponse](#utxorpc-v1-build-GetChainTipResponse)
- [GetUtxoByAddressRequest](#utxorpc-v1-build-GetUtxoByAddressRequest)
- [GetUtxoByAddressResponse](#utxorpc-v1-build-GetUtxoByAddressResponse)
- [GetUtxoByRefRequest](#utxorpc-v1-build-GetUtxoByRefRequest)
- [GetUtxoByRefResponse](#utxorpc-v1-build-GetUtxoByRefResponse)
- [HoldUtxoRequest](#utxorpc-v1-build-HoldUtxoRequest)
- [HoldUtxoResponse](#utxorpc-v1-build-HoldUtxoResponse)
- [UtxoRef](#utxorpc-v1-build-UtxoRef)
- [AnyChainUtxo](#utxorpc-v1alpha-build-AnyChainUtxo)
- [ChainParam](#utxorpc-v1alpha-build-ChainParam)
- [ChainPoint](#utxorpc-v1alpha-build-ChainPoint)
- [GetChainParamRequest](#utxorpc-v1alpha-build-GetChainParamRequest)
- [GetChainParamResponse](#utxorpc-v1alpha-build-GetChainParamResponse)
- [GetChainTipRequest](#utxorpc-v1alpha-build-GetChainTipRequest)
- [GetChainTipResponse](#utxorpc-v1alpha-build-GetChainTipResponse)
- [GetUtxoByAddressRequest](#utxorpc-v1alpha-build-GetUtxoByAddressRequest)
- [GetUtxoByAddressResponse](#utxorpc-v1alpha-build-GetUtxoByAddressResponse)
- [GetUtxoByRefRequest](#utxorpc-v1alpha-build-GetUtxoByRefRequest)
- [GetUtxoByRefResponse](#utxorpc-v1alpha-build-GetUtxoByRefResponse)
- [HoldUtxoRequest](#utxorpc-v1alpha-build-HoldUtxoRequest)
- [HoldUtxoResponse](#utxorpc-v1alpha-build-HoldUtxoResponse)
- [UtxoRef](#utxorpc-v1alpha-build-UtxoRef)

- [LedgerStateService](#utxorpc-v1-build-LedgerStateService)
- [LedgerStateService](#utxorpc-v1alpha-build-LedgerStateService)

- [utxorpc/v1alpha/submit/submit.proto](#utxorpc_v1alpha_submit_submit-proto)
- [AnyChainTx](#utxorpc-v1alpha-submit-AnyChainTx)
Expand Down Expand Up @@ -244,6 +244,7 @@ Contains the header information for a block.
| ----- | ---- | ----- | ----------- |
| slot | [uint64](#uint64) | | Slot number. |
| hash | [bytes](#bytes) | | Block hash. |
| height | [uint64](#uint64) | | Block height. |



Expand Down Expand Up @@ -766,6 +767,7 @@ Represents a transaction in the Cardano blockchain.
| validity | [TxValidity](#utxorpc-v1alpha-cardano-TxValidity) | | Validity interval of the transaction |
| successful | [bool](#bool) | | Flag indicating whether the transaction was successful |
| auxiliary | [AuxData](#utxorpc-v1alpha-cardano-AuxData) | | Auxiliary data not directly tied to the validation process |
| hash | [bytes](#bytes) | | Hash of the transaction that serves as main identifier |



Expand Down Expand Up @@ -937,7 +939,7 @@ Purpose of the redeemer in a transaction.



<a name="utxorpc-v1-build-AnyChainUtxo"></a>
<a name="utxorpc-v1alpha-build-AnyChainUtxo"></a>

### AnyChainUtxo
An evenlope that holds an UTxO from any of compatible chains
Expand All @@ -952,7 +954,7 @@ An evenlope that holds an UTxO from any of compatible chains



<a name="utxorpc-v1-build-ChainParam"></a>
<a name="utxorpc-v1alpha-build-ChainParam"></a>

### ChainParam
Represents a key-value pair for a chain parameter.
Expand All @@ -968,7 +970,7 @@ Represents a key-value pair for a chain parameter.



<a name="utxorpc-v1-build-ChainPoint"></a>
<a name="utxorpc-v1alpha-build-ChainPoint"></a>

### ChainPoint
Represents a specific point in the blockchain.
Expand All @@ -985,7 +987,7 @@ Represents a specific point in the blockchain.



<a name="utxorpc-v1-build-GetChainParamRequest"></a>
<a name="utxorpc-v1alpha-build-GetChainParamRequest"></a>

### GetChainParamRequest
Request to get specific chain parameters.
Expand All @@ -1000,22 +1002,22 @@ Request to get specific chain parameters.



<a name="utxorpc-v1-build-GetChainParamResponse"></a>
<a name="utxorpc-v1alpha-build-GetChainParamResponse"></a>

### GetChainParamResponse
Response containing the requested chain parameters.


| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| param | [ChainParam](#utxorpc-v1-build-ChainParam) | repeated | List of requested chain parameters. |
| param | [ChainParam](#utxorpc-v1alpha-build-ChainParam) | repeated | List of requested chain parameters. |






<a name="utxorpc-v1-build-GetChainTipRequest"></a>
<a name="utxorpc-v1alpha-build-GetChainTipRequest"></a>

### GetChainTipRequest
Request to get the current chain tip.
Expand All @@ -1025,22 +1027,22 @@ Request to get the current chain tip.



<a name="utxorpc-v1-build-GetChainTipResponse"></a>
<a name="utxorpc-v1alpha-build-GetChainTipResponse"></a>

### GetChainTipResponse
Response containing the current chain tip.


| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| tip | [ChainPoint](#utxorpc-v1-build-ChainPoint) | | Current chain tip. |
| tip | [ChainPoint](#utxorpc-v1alpha-build-ChainPoint) | | Current chain tip. |






<a name="utxorpc-v1-build-GetUtxoByAddressRequest"></a>
<a name="utxorpc-v1alpha-build-GetUtxoByAddressRequest"></a>

### GetUtxoByAddressRequest
Request to get UTxOs by their associated addresses.
Expand All @@ -1049,92 +1051,92 @@ Request to get UTxOs by their associated addresses.
| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| address | [bytes](#bytes) | repeated | List of addresses to query. |
| acquire_point | [ChainPoint](#utxorpc-v1-build-ChainPoint) | | Point in the chain to query from. |
| acquire_point | [ChainPoint](#utxorpc-v1alpha-build-ChainPoint) | | Point in the chain to query from. |






<a name="utxorpc-v1-build-GetUtxoByAddressResponse"></a>
<a name="utxorpc-v1alpha-build-GetUtxoByAddressResponse"></a>

### GetUtxoByAddressResponse
Response containing the UTxOs associated with the requested addresses.


| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| items | [AnyChainUtxo](#utxorpc-v1-build-AnyChainUtxo) | repeated | List of UTxOs. |
| items | [AnyChainUtxo](#utxorpc-v1alpha-build-AnyChainUtxo) | repeated | List of UTxOs. |
| next_token | [string](#string) | | Token for pagination. |






<a name="utxorpc-v1-build-GetUtxoByRefRequest"></a>
<a name="utxorpc-v1alpha-build-GetUtxoByRefRequest"></a>

### GetUtxoByRefRequest
Request to get UTxOs by their references.


| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| ref | [UtxoRef](#utxorpc-v1-build-UtxoRef) | repeated | List of UTxO references to query. |
| acquire_point | [ChainPoint](#utxorpc-v1-build-ChainPoint) | | Point in the chain to query from. |
| ref | [UtxoRef](#utxorpc-v1alpha-build-UtxoRef) | repeated | List of UTxO references to query. |
| acquire_point | [ChainPoint](#utxorpc-v1alpha-build-ChainPoint) | | Point in the chain to query from. |






<a name="utxorpc-v1-build-GetUtxoByRefResponse"></a>
<a name="utxorpc-v1alpha-build-GetUtxoByRefResponse"></a>

### GetUtxoByRefResponse
Response containing the UTxOs associated with the requested references.


| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| items | [AnyChainUtxo](#utxorpc-v1-build-AnyChainUtxo) | repeated | List of UTxOs. |
| items | [AnyChainUtxo](#utxorpc-v1alpha-build-AnyChainUtxo) | repeated | List of UTxOs. |
| next_token | [string](#string) | | Token for pagination. |






<a name="utxorpc-v1-build-HoldUtxoRequest"></a>
<a name="utxorpc-v1alpha-build-HoldUtxoRequest"></a>

### HoldUtxoRequest
Request to hold UTxOs.


| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| refs | [UtxoRef](#utxorpc-v1-build-UtxoRef) | repeated | List of UTxO references to hold. |
| refs | [UtxoRef](#utxorpc-v1alpha-build-UtxoRef) | repeated | List of UTxO references to hold. |






<a name="utxorpc-v1-build-HoldUtxoResponse"></a>
<a name="utxorpc-v1alpha-build-HoldUtxoResponse"></a>

### HoldUtxoResponse
Response containing information about lost UTxOs.


| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| lost | [UtxoRef](#utxorpc-v1-build-UtxoRef) | repeated | List of lost UTxO references. |
| lost | [UtxoRef](#utxorpc-v1alpha-build-UtxoRef) | repeated | List of lost UTxO references. |






<a name="utxorpc-v1-build-UtxoRef"></a>
<a name="utxorpc-v1alpha-build-UtxoRef"></a>

### UtxoRef
Represents a reference to a UTxO.
Expand All @@ -1156,18 +1158,18 @@ Represents a reference to a UTxO.



<a name="utxorpc-v1-build-LedgerStateService"></a>
<a name="utxorpc-v1alpha-build-LedgerStateService"></a>

### LedgerStateService
Service definition for querying the state of the ledger.

| Method Name | Request Type | Response Type | Description |
| ----------- | ------------ | ------------- | ------------|
| GetChainTip | [GetChainTipRequest](#utxorpc-v1-build-GetChainTipRequest) | [GetChainTipResponse](#utxorpc-v1-build-GetChainTipResponse) | Get the current chain tip. |
| GetChainParam | [GetChainParamRequest](#utxorpc-v1-build-GetChainParamRequest) | [GetChainParamResponse](#utxorpc-v1-build-GetChainParamResponse) | Get specific chain parameters. |
| GetUtxoByAddress | [GetUtxoByAddressRequest](#utxorpc-v1-build-GetUtxoByAddressRequest) | [GetUtxoByAddressResponse](#utxorpc-v1-build-GetUtxoByAddressResponse) | Get UTxOs by their associated addresses. |
| GetUtxoByRef | [GetUtxoByRefRequest](#utxorpc-v1-build-GetUtxoByRefRequest) | [GetUtxoByRefResponse](#utxorpc-v1-build-GetUtxoByRefResponse) | Get UTxOs by their references. |
| HoldUtxo | [HoldUtxoRequest](#utxorpc-v1-build-HoldUtxoRequest) | [HoldUtxoResponse](#utxorpc-v1-build-HoldUtxoResponse) stream | Hold UTxOs and receive updates about lost UTxOs |
| GetChainTip | [GetChainTipRequest](#utxorpc-v1alpha-build-GetChainTipRequest) | [GetChainTipResponse](#utxorpc-v1alpha-build-GetChainTipResponse) | Get the current chain tip. |
| GetChainParam | [GetChainParamRequest](#utxorpc-v1alpha-build-GetChainParamRequest) | [GetChainParamResponse](#utxorpc-v1alpha-build-GetChainParamResponse) | Get specific chain parameters. |
| GetUtxoByAddress | [GetUtxoByAddressRequest](#utxorpc-v1alpha-build-GetUtxoByAddressRequest) | [GetUtxoByAddressResponse](#utxorpc-v1alpha-build-GetUtxoByAddressResponse) | Get UTxOs by their associated addresses. |
| GetUtxoByRef | [GetUtxoByRefRequest](#utxorpc-v1alpha-build-GetUtxoByRefRequest) | [GetUtxoByRefResponse](#utxorpc-v1alpha-build-GetUtxoByRefResponse) | Get UTxOs by their references. |
| HoldUtxo | [HoldUtxoRequest](#utxorpc-v1alpha-build-HoldUtxoRequest) | [HoldUtxoResponse](#utxorpc-v1alpha-build-HoldUtxoResponse) stream | Hold UTxOs and receive updates about lost UTxOs |



Expand Down
3 changes: 2 additions & 1 deletion gen/node/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
# Ignore node modules
/node_modules

package-lock.json
package-lock.json
bun.lockb
4 changes: 2 additions & 2 deletions gen/node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"types": "./lib/index.d.ts",
"type": "module",
"scripts": {
"build": "tsc"
"build": "node ./scripts/gen-top-level-export.cjs src/utxorpc/v1alpha && tsc"
},
"engines": {
"node": ">=16.0.0"
Expand All @@ -32,4 +32,4 @@
"dependencies": {
"@bufbuild/protobuf": "^1.4"
}
}
}
66 changes: 66 additions & 0 deletions gen/node/scripts/gen-top-level-export.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
const fs = require('fs').promises;
const path = require('path');

async function readDirectory(directoryPath, srcBasePath, namespaceImports = {}) {
const items = await fs.readdir(directoryPath, { withFileTypes: true });

for (let item of items) {
if (item.isDirectory()) {
// Recursively process subdirectories
await readDirectory(path.join(directoryPath, item.name), srcBasePath, namespaceImports);
} else if (item.name.endsWith('.ts') && !item.name.startsWith('index')) {
// Derive the simplified alias from the file name
const moduleName = item.name.replace('.ts', '');
const namespace = path.relative(srcBasePath, directoryPath).split(path.sep).join('_');
const importAlias = `${moduleName}`; // Simplified alias

// Initialize the namespace array if it doesn't exist
if (!namespaceImports[namespace]) {
namespaceImports[namespace] = [];
}

const relativePath = path.relative(srcBasePath, path.join(directoryPath, item.name)).replace('.ts', '.js');
namespaceImports[namespace].push(`import * as ${importAlias} from './${relativePath.replace(/\\/g, '/')}';\n`);
}
}

return namespaceImports;
}

async function generateIndexContent(directoryPath, srcBasePath) {
const namespaceImports = await readDirectory(directoryPath, srcBasePath);
let imports = '';
let exports = '';

for (const [namespace, importStatements] of Object.entries(namespaceImports)) {
// Concatenate all import statements for this namespace
imports += importStatements.join('');

// Generate the export statement using the spread operator for each import alias
const exportAliases = importStatements.map(statement => {
const aliasMatch = statement.match(/as (\w+)/);
return aliasMatch ? `...${aliasMatch[1]}` : '';
}).filter(alias => alias);

const namespaceName = namespace.split('_').pop();
exports += `export const ${namespaceName} = { ${exportAliases.join(', ')} };\n`;
}

return `${imports}\n${exports}`;
}

(async () => {
const relativeDirectoryPath = process.argv[2];
if (!relativeDirectoryPath) {
console.error('No directory path provided. Usage: node script.js [relativePath]');
process.exit(1);
}

const directoryPath = path.join(process.cwd(), relativeDirectoryPath);
const srcPath = path.join(process.cwd(), 'src');
const indexPath = path.join(srcPath, 'index.ts');

const indexContent = await generateIndexContent(directoryPath, srcPath);
await fs.writeFile(indexPath, indexContent);
console.log('index.ts generated successfully in:', srcPath);
})();
Empty file removed gen/node/src/index.ts
Empty file.
4 changes: 2 additions & 2 deletions gen/node/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"compilerOptions": {
"target": "ES2018",
"module": "Node16",
"module": "commonjs",
"declaration": true,
"outDir": "./lib",
"strict": true,
"esModuleInterop": true,
"moduleResolution": "Node16"
"moduleResolution": "node"
},
"include": ["src"],
"exclude": ["node_modules"]
Expand Down
2 changes: 1 addition & 1 deletion gen/rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ pub mod utxorpc {
// @@protoc_insertion_point(utxorpc.v1alpha.watch)
}
}
}
}