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

nodejs enterprise-client #44

Open
kking124 opened this issue Jan 3, 2024 · 0 comments
Open

nodejs enterprise-client #44

kking124 opened this issue Jan 3, 2024 · 0 comments
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@kking124
Copy link

kking124 commented Jan 3, 2024

I did not find an enterprise nodejs client available; is it possible to have a client created similar to the python and go clients?

I have been trying to use @grpc/grpc-js and @grpc/proto-loader to create it and this is what I have come up with so far (against the update-docs-template branch):

pull.sh:

#!/bin/bash

# Directory to ignore
LIB_DIR="lib"

# Git repositories to clone
REPO1="envoyproxy/data-plane-api"
TAG1="3a71d52b66bc661c25ba6f6b082d900b09ea7fd8"
REPO2="googleapis/googleapis"
TAG2="9a7d19079b5e3c22a5a08eaa94273f5d1eebb317"
REPO3="bufbuild/protoc-gen-validate"
TAG3="59da36e59fef2267fc2b1849a05159e3ecdf24f3"
REPO4="cncf/udpa"
TAG4="6414d713912e988471d192940b62bf552b11793a"

mkdir -p $LIB_DIR/$REPO1
curl -L https://api.github.com/repos/$REPO1/tarball/$TAG1 | tar xz -C $LIB_DIR/$REPO1 --strip-components=1
mkdir -p $LIB_DIR/$REPO2
curl -L https://api.github.com/repos/$REPO2/tarball/$TAG2 | tar xz -C $LIB_DIR/$REPO2 --strip-components=1
mkdir -p $LIB_DIR/$REPO3
curl -L https://api.github.com/repos/$REPO3/tarball/$TAG3 | tar xz -C $LIB_DIR/$REPO3 --strip-components=1
mkdir -p $LIB_DIR/$REPO4
curl -L https://api.github.com/repos/$REPO4/tarball/$TAG4 | tar xz -C $LIB_DIR/$REPO4 --strip-components=1

gulpfile.js

const gulp = require('gulp');
const through2 = require('through2');
const fs = require('fs');
const path = require('path');
const grpc = require('@grpc/grpc-js');
const protoLoader = require('@grpc/proto-loader');

gulp.task('generate-sdk', function () {
    const protoDir = 'proto';
    const includeDirs = [
        path.resolve(__dirname, 'lib/envoyproxy/data-plane-api'),
        path.resolve(__dirname, 'lib/googleapis/googleapis'),
        path.resolve(__dirname, 'lib/bufbuild/protoc-gen-validate'),
        path.resolve(__dirname, 'lib/cncf/udpa'),
        path.resolve(__dirname, 'proto/pomerium'),
    ]
    return gulp.src(`${protoDir}/**/*.proto`)
      .pipe(through2.obj(function (file, _, cb) {
        const packageDefinition = protoLoader.loadSync(file.path, {
          keepCase: true,
          longs: String,
          enums: String,
          defaults: true,
          oneofs: true,
          includeDirs
        });
        const protoDescriptor = grpc.loadPackageDefinition(packageDefinition);
        const serviceName = Object.keys(protoDescriptor)[0]; // Assume the service name is the first key
        const relativePath = path.relative(protoDir, path.dirname(file.path));
        const outputFilePath = path.join('build', relativePath, `${serviceName}Client.js`);
        const clientFactory = `
          const grpc = require('@grpc/grpc-js');
          const protoDescriptor = ${JSON.stringify(protoDescriptor)};
          class ${serviceName}Client {
            constructor(endpoint, channelCredentials) {
              this.service = new protoDescriptor.${serviceName}(endpoint, channelCredentials);
            }
          }
          module.exports = ${serviceName}Client;
        `;
        fs.mkdirSync(path.dirname(outputFilePath), { recursive: true });
        fs.writeFileSync(outputFilePath, clientFactory);
        cb(null, file);
      }));
  });
// Define a task that uses the generate file and includes the copy task
gulp.task('copy', () => {
    return gulp.src('src/**/*')
        .pipe(gulp.dest('build'))
});

// Define a task to clean the build directory
gulp.task('clean', async () => {
    const del = await import('del');
    return del(['build']);
});

// Default task

gulp.task('generate', gulp.series('copy','generate-sdk'));

gulp.task('default', gulp.series('clean', 'generate'));

src/package.json

{
  "name": "@pomerium/enterprise-client",
  "version": "1.0.0",
  "description": "javascript sdk to access the pomerium api",
  "main": "",
  "scripts": {},
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@grpc/grpc-js": "^1.9.13"
  }
}
@desimone desimone added enhancement New feature or request help wanted Extra attention is needed labels Jan 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants