Skip to content

nodejs enterprise-client #44

@kking124

Description

@kking124

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"
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requesthelp wantedExtra attention is needed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions