@cheny56/node-client 1.0.5 → 1.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README-COMMONJS.md +70 -0
- package/index.cjs +37 -0
- package/package.json +14 -5
- package/tsconfig.json +27 -0
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
# CommonJS Usage
|
|
2
|
+
|
|
3
|
+
This library supports both ES modules (`import`) and CommonJS (`require`). However, due to the ES module nature of the source code, CommonJS usage requires async handling.
|
|
4
|
+
|
|
5
|
+
## Recommended: Use ES Modules
|
|
6
|
+
|
|
7
|
+
For the best experience, use ES modules:
|
|
8
|
+
|
|
9
|
+
```javascript
|
|
10
|
+
// ✅ Recommended
|
|
11
|
+
import { QuorumProvider, ECDSAWallet } from '@cheny56/node-client';
|
|
12
|
+
|
|
13
|
+
const provider = new QuorumProvider('http://localhost:8545');
|
|
14
|
+
const wallet = new ECDSAWallet('0x...', provider);
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## CommonJS Usage (with async/await)
|
|
18
|
+
|
|
19
|
+
If you must use CommonJS, you need to handle the async loading:
|
|
20
|
+
|
|
21
|
+
```javascript
|
|
22
|
+
// ⚠️ CommonJS - requires async handling
|
|
23
|
+
const client = await import('@cheny56/node-client');
|
|
24
|
+
const { QuorumProvider, ECDSAWallet } = client;
|
|
25
|
+
|
|
26
|
+
const provider = new QuorumProvider('http://localhost:8545');
|
|
27
|
+
const wallet = new ECDSAWallet('0x...', provider);
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
Or wrap in an async function:
|
|
31
|
+
|
|
32
|
+
```javascript
|
|
33
|
+
async function main() {
|
|
34
|
+
const { QuorumProvider, ECDSAWallet } = await import('@cheny56/node-client');
|
|
35
|
+
|
|
36
|
+
const provider = new QuorumProvider('http://localhost:8545');
|
|
37
|
+
const wallet = new ECDSAWallet('0x...', provider);
|
|
38
|
+
|
|
39
|
+
// Use provider and wallet...
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
main().catch(console.error);
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## Why Async is Required
|
|
46
|
+
|
|
47
|
+
The library source code uses ES modules (`export`/`import`). When using `require()` in CommonJS, Node.js needs to dynamically import the ES module, which is an async operation.
|
|
48
|
+
|
|
49
|
+
## Migration to ES Modules
|
|
50
|
+
|
|
51
|
+
To use ES modules in your project:
|
|
52
|
+
|
|
53
|
+
1. Add `"type": "module"` to your `package.json`, OR
|
|
54
|
+
2. Use `.mjs` extension for your files, OR
|
|
55
|
+
3. Use `import()` syntax in CommonJS files
|
|
56
|
+
|
|
57
|
+
Example `package.json`:
|
|
58
|
+
```json
|
|
59
|
+
{
|
|
60
|
+
"type": "module",
|
|
61
|
+
"scripts": {
|
|
62
|
+
"start": "node index.js"
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
Then you can use:
|
|
68
|
+
```javascript
|
|
69
|
+
import { QuorumProvider } from '@cheny56/node-client';
|
|
70
|
+
```
|
package/index.cjs
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* CommonJS entry point for @cheny56/node-client
|
|
3
|
+
*
|
|
4
|
+
* NOTE: This library is built as ES modules. For CommonJS compatibility,
|
|
5
|
+
* you need to use dynamic import() instead of require().
|
|
6
|
+
*
|
|
7
|
+
* Example:
|
|
8
|
+
* const { QuorumProvider } = await import('@cheny56/node-client');
|
|
9
|
+
*
|
|
10
|
+
* Or use ES modules in your project:
|
|
11
|
+
* import { QuorumProvider } from '@cheny56/node-client';
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
// Re-export using dynamic import
|
|
15
|
+
// This allows require() to work, but returns a Promise
|
|
16
|
+
const modPromise = import('./src/index.js');
|
|
17
|
+
|
|
18
|
+
// For synchronous require(), we need to throw a helpful error
|
|
19
|
+
// and suggest using import() or ES modules
|
|
20
|
+
module.exports = new Proxy({}, {
|
|
21
|
+
get(target, prop) {
|
|
22
|
+
if (prop === 'then' || prop === 'catch' || prop === 'finally') {
|
|
23
|
+
// Make it thenable so it works with await import()
|
|
24
|
+
return modPromise.then(mod => mod[prop]).bind(modPromise);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
throw new Error(
|
|
28
|
+
`Cannot access '${prop}' synchronously. ` +
|
|
29
|
+
`This package uses ES modules. Please use:\n` +
|
|
30
|
+
` const { ${prop} } = await import('@cheny56/node-client');\n` +
|
|
31
|
+
`Or switch your project to ES modules by adding "type": "module" to package.json`
|
|
32
|
+
);
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
// Also export as a Promise for await import() usage
|
|
37
|
+
module.exports.default = modPromise;
|
package/package.json
CHANGED
|
@@ -1,19 +1,28 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cheny56/node-client",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.6",
|
|
4
4
|
"description": "Client library for Quorum blockchain with Post-Quantum Cryptography (PQC) and Zero-Knowledge Proof (ZK) support",
|
|
5
|
-
"main": "
|
|
5
|
+
"main": "./index.cjs",
|
|
6
|
+
"module": "./src/index.js",
|
|
6
7
|
"types": "src/index.d.ts",
|
|
7
8
|
"type": "module",
|
|
8
9
|
"exports": {
|
|
9
10
|
".": {
|
|
11
|
+
"require": "./index.cjs",
|
|
10
12
|
"import": "./src/index.js",
|
|
11
|
-
"types": "./src/index.d.ts"
|
|
12
|
-
|
|
13
|
+
"types": "./src/index.d.ts",
|
|
14
|
+
"default": "./src/index.js"
|
|
15
|
+
},
|
|
16
|
+
"./package.json": "./package.json"
|
|
13
17
|
},
|
|
14
18
|
"files": [
|
|
15
19
|
"src/**/*.js",
|
|
16
|
-
"src/**/*.d.ts"
|
|
20
|
+
"src/**/*.d.ts",
|
|
21
|
+
"index.cjs",
|
|
22
|
+
"package.json",
|
|
23
|
+
"README.md",
|
|
24
|
+
"README-COMMONJS.md",
|
|
25
|
+
"tsconfig.json"
|
|
17
26
|
],
|
|
18
27
|
"scripts": {
|
|
19
28
|
"test": "echo \"Error: no test specified\" && exit 1"
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES2020",
|
|
4
|
+
"module": "ESNext",
|
|
5
|
+
"lib": ["ES2020"],
|
|
6
|
+
"moduleResolution": "node",
|
|
7
|
+
"declaration": true,
|
|
8
|
+
"declarationMap": true,
|
|
9
|
+
"outDir": "./dist",
|
|
10
|
+
"rootDir": "./src",
|
|
11
|
+
"strict": false,
|
|
12
|
+
"esModuleInterop": true,
|
|
13
|
+
"skipLibCheck": true,
|
|
14
|
+
"forceConsistentCasingInFileNames": true,
|
|
15
|
+
"resolveJsonModule": true,
|
|
16
|
+
"allowSyntheticDefaultImports": true,
|
|
17
|
+
"noEmit": true
|
|
18
|
+
},
|
|
19
|
+
"include": [
|
|
20
|
+
"src/**/*"
|
|
21
|
+
],
|
|
22
|
+
"exclude": [
|
|
23
|
+
"node_modules",
|
|
24
|
+
"dist",
|
|
25
|
+
"examples"
|
|
26
|
+
]
|
|
27
|
+
}
|