Skip to content

Commit

Permalink
tidy up
Browse files Browse the repository at this point in the history
  • Loading branch information
fenos committed Jul 25, 2021
1 parent b094864 commit 108ce50
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 41 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "twirp-ts",
"version": "2.3.4",
"version": "2.3.0",
"description": "Typescript implementation of the Twirp protocol",
"main": "build/twirp/index.js",
"bin": {
Expand Down
33 changes: 33 additions & 0 deletions src/protoc-gen-twirp-ts/gen/index-file.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { DescriptorRegistry } from "@protobuf-ts/plugin-framework";
import { File } from "../file";

export function genIndexFile(registry: DescriptorRegistry, files: File[]) {
const fileToExport = registry.allFiles()
.filter((fileDescriptor) => {
let hasExports = false;
registry.visitTypes(fileDescriptor, descriptor => {
// we are not interested in synthetic types like map entry messages
if (registry.isSyntheticElement(descriptor)) return;
hasExports = true;
});

return hasExports;
})
.map((file => file.name?.replace(".proto", "")));

const compiledFiles = files.filter(file => file.getContent() !== "").map(file => {
return file.fileName.replace(".ts", "")
});

if (compiledFiles.length > 0) {
fileToExport.push(
...compiledFiles,
)
}

const indexFile = new File('index.ts');

return indexFile.setContent(fileToExport.map((fileName) => {
return `export * from "./${fileName}";`
}).join("\n"));
}
42 changes: 6 additions & 36 deletions src/protoc-gen-twirp-ts/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { genGateway } from "./gen/gateway";
import { createLocalTypeName } from "./local-type-name";
import { Interpreter } from "./interpreter";
import { genOpenAPI, OpenAPIType } from "./gen/open-api";
import { genIndexFile } from "./gen/index-file";

export class ProtobuftsPlugin extends PluginBase<File> {
parameters = {
Expand Down Expand Up @@ -130,19 +131,19 @@ export class ProtobuftsPlugin extends PluginBase<File> {
if (params.openapi_twirp) {
docs.push(
...(await genOpenAPI(ctx, registry.allFiles(), OpenAPIType.TWIRP)),
)
);
}

if (params.openapi_gateway) {
docs.push(
...(await genOpenAPI(ctx, registry.allFiles(), OpenAPIType.GATEWAY)),
)
);
}

docs.forEach((doc) => {
const file = new File(`${doc.fileName}`)
file.setContent(doc.content)
files.push(file)
const file = new File(`${doc.fileName}`);
file.setContent(doc.content);
files.push(file);
})

return files;
Expand All @@ -152,37 +153,6 @@ export class ProtobuftsPlugin extends PluginBase<File> {
protected getSupportedFeatures = () => [CodeGeneratorResponse_Feature.PROTO3_OPTIONAL];
}

function genIndexFile(registry: DescriptorRegistry, files: File[]) {
const fileToExport = registry.allFiles()
.filter((fileDescriptor) => {
let hasExports = false;
registry.visitTypes(fileDescriptor, descriptor => {
// we are not interested in synthetic types like map entry messages
if (registry.isSyntheticElement(descriptor)) return;
hasExports = true;
});

return hasExports;
})
.map((file => file.name?.replace(".proto", "")));

const compiledFiles = files.filter(file => file.getContent() !== "").map(file => {
return file.fileName.replace(".ts", "")
});

if (compiledFiles.length > 0) {
fileToExport.push(
...compiledFiles,
)
}

const indexFile = new File('index.ts');

return indexFile.setContent(fileToExport.map((fileName) => {
return `export * from "./${fileName}";`
}).join("\n"));
}

new ProtobuftsPlugin().run().then(() => {
process.exit(0);
})
Expand Down
2 changes: 0 additions & 2 deletions src/twirp/errors.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import * as http from 'http';

/**
* Represents a twirp error
*/
Expand Down
5 changes: 3 additions & 2 deletions src/twirp/http.client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ export interface Rpc {
}

export type HttpClientOptions = Omit<(http.RequestOptions | https.RequestOptions), "path" | "host" | "port"> & {
prefix?: string
baseUrl: string
}

Expand Down Expand Up @@ -50,7 +49,9 @@ export const NodeHttpRPC: (options: HttpClientOptions) => Rpc = (options) => ({
path: `${prefix}/${service}/${method}`,
headers: {
'Content-Type': contentType,
'Content-Length': contentType === "application/protobuf" ? Buffer.byteLength(requestData as Uint8Array) : Buffer.from(requestData as string).byteLength,
'Content-Length': contentType === "application/protobuf" ?
Buffer.byteLength(requestData as Uint8Array) :
Buffer.from(requestData as string).byteLength,
},
}, res => {
res.on('data', chunk => responseChunks.push(chunk));
Expand Down

0 comments on commit 108ce50

Please sign in to comment.