Skip to content

Commit ebc10ad

Browse files
esseswanntsufiev
authored andcommitted
fix: certificates path for bundling compilation
This is a poor man's fix for bundling compilers like esbuild\webpack that require paths to be relative to the entry point. Certs folder is moved into cjs/esm dists so we can utilize universal path './certs' that would work for default tsc compilation and would allow copying the certs folder to whatever path bundler is gonna output compiled entry point. Some sensible error communication is also added. This commit is not marked as breaking because if certs folder had not been found when grpcs is present in previous implementation the connection would not succeed and\or "File not found" error threw. Also extract version from imported package.json. Closes: #173
1 parent 3b6c6c0 commit ebc10ad

File tree

5 files changed

+37
-28
lines changed

5 files changed

+37
-28
lines changed

fixup.sh

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
#!/bin/sh
22

3-
cat >build/cjs/package.json <<!EOF
4-
{
5-
"type": "commonjs"
6-
}
7-
!EOF
3+
package_version=`npm show . version`
84

9-
cat >build/esm/package.json <<!EOF
5+
for target in esm cjs; do
6+
cp -R certs build/$target/src/
7+
[[ $target == "esm" ]] && type="module" || type="commonjs"
8+
cat >build/$target/package.json <<!EOF
109
{
11-
"type": "module"
10+
"version": "$package_version",
11+
"type": "$type"
1212
}
1313
!EOF
14+
done

package.json

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,16 @@
22
"name": "ydb-sdk",
33
"version": "3.4.0",
44
"description": "Node.js bindings for working with YDB API over gRPC",
5-
"main": "build/cjs/index.js",
6-
"module": "build/esm/index.js",
5+
"main": "build/cjs/src/index.js",
6+
"module": "build/esm/src/index.js",
77
"exports": {
88
".": {
9-
"import": "./build/esm/index.js",
10-
"require": "./build/cjs/index.js"
9+
"import": "./build/esm/src/index.js",
10+
"require": "./build/cjs/src/index.js"
1111
}
1212
},
1313
"files": [
14-
"build/**",
15-
"certs/"
14+
"build/**"
1615
],
1716
"scripts": {
1817
"test:unit": "exit 0",

src/ssl-credentials.ts

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,30 @@
11
// noinspection ES6PreferShortImport
2-
import {Logger} from './logging';
2+
import { Logger } from './logging';
33
import fs from 'fs';
44
import path from 'path';
5-
import {getRelTopLevelPath} from "./version";
65

7-
const FALLBACK_INTERNAL_ROOT_CERTS = path.join(__dirname, getRelTopLevelPath(), 'certs/internal.pem');
8-
const FALLBACK_SYSTEM_ROOT_CERTS = path.join(__dirname, getRelTopLevelPath(), 'certs/system.pem');
6+
const CERTIFICATES_FOLDER = 'certs'
7+
const RELATIVE_PATH = process.env.TEST_ENVIRONMENT ? '../' : './'
8+
const RESOLVED_PATH = path.join(__dirname, RELATIVE_PATH, CERTIFICATES_FOLDER)
9+
const FALLBACK_INTERNAL_ROOT_CERTS = path.join(RESOLVED_PATH, 'internal.pem');
10+
const FALLBACK_SYSTEM_ROOT_CERTS = path.join(RESOLVED_PATH, 'system.pem');
911

1012
function makeInternalRootCertificates() {
11-
const internalRootCertificates = fs.readFileSync(FALLBACK_INTERNAL_ROOT_CERTS);
13+
if (!fs.existsSync(FALLBACK_INTERNAL_ROOT_CERTS)
14+
|| !fs.existsSync(FALLBACK_SYSTEM_ROOT_CERTS)) {
15+
throw new Error(certificateNotFoundMessage)
16+
}
17+
18+
const internalRootCertificates = fs.readFileSync(FALLBACK_INTERNAL_ROOT_CERTS)
19+
const fallbackSystemRootCertificates = fs.readFileSync(FALLBACK_SYSTEM_ROOT_CERTS)
1220

13-
let systemRootCertificates;
21+
let systemRootCertificates: Buffer;
1422
const tls = require('tls');
1523
const nodeRootCertificates = tls.rootCertificates as string[] | undefined;
1624
if (nodeRootCertificates && nodeRootCertificates.length > 0) {
1725
systemRootCertificates = Buffer.from(nodeRootCertificates.join('\n'));
1826
} else {
19-
systemRootCertificates = fs.readFileSync(FALLBACK_SYSTEM_ROOT_CERTS);
27+
systemRootCertificates = fallbackSystemRootCertificates;
2028
}
2129

2230
return Buffer.concat([internalRootCertificates, systemRootCertificates]);
@@ -47,6 +55,12 @@ export function makeSslCredentials(endpoint: string, logger: Logger, sslCredenti
4755
return makeDefaultSslCredentials();
4856
}
4957

58+
const certificateNotFoundMessage = `No certificate found
59+
It seems that you are using grpcs (secure) endpoint in a bundled environment.
60+
Either provide YDB_SSL_ROOT_CERTIFICATES_FILE environment variable
61+
or copy contents of ydb-nodejs-sdk/certs to ./certs path relative to the bundled file
62+
`
63+
5064
export interface ISslCredentials {
5165
rootCertificates?: Buffer,
5266
clientPrivateKey?: Buffer,

src/version.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
1-
import path from 'path';
2-
3-
export function getRelTopLevelPath() {
4-
return process.env.TEST_ENVIRONMENT === 'dev' ? '..' : '../..';
5-
}
6-
7-
const pkgInfo = require(path.join(getRelTopLevelPath(), 'package.json'));
1+
import pkgInfo from "./../package.json";
82

93
function getVersion() {
104
return pkgInfo.version;

tsconfig-base.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
"skipLibCheck": true,
1616
"experimentalDecorators": true,
1717
"allowJs": true,
18-
"declaration": true
18+
"declaration": true,
19+
"resolveJsonModule": true
1920
},
2021
"include": [
2122
"src/**/*.ts"

0 commit comments

Comments
 (0)