@injectivelabs/grpc-web-node-http-transport
Advanced tools
Comparing version 0.0.1 to 0.0.2
/// <reference types="node" /> | ||
import * as https from "https"; | ||
import { grpc } from "@improbable-eng/grpc-web"; | ||
import { grpc } from "@injectivelabs/grpc-web"; | ||
export declare function NodeHttpTransport(httpsOptions?: https.RequestOptions): grpc.TransportFactory; |
@@ -18,3 +18,3 @@ "use strict"; | ||
var url = require("url"); | ||
var grpc_web_1 = require("@improbable-eng/grpc-web"); | ||
var grpc_web_1 = require("@injectivelabs/grpc-web"); | ||
function NodeHttpTransport(httpsOptions) { | ||
@@ -32,3 +32,4 @@ return function (opts) { | ||
NodeHttp.prototype.sendMessage = function (msgBytes) { | ||
if (!this.options.methodDefinition.requestStream && !this.options.methodDefinition.responseStream) { | ||
if (!this.options.methodDefinition.requestStream && | ||
!this.options.methodDefinition.responseStream) { | ||
this.request.setHeader("Content-Length", msgBytes.byteLength); | ||
@@ -39,4 +40,3 @@ } | ||
}; | ||
NodeHttp.prototype.finishSend = function () { | ||
}; | ||
NodeHttp.prototype.finishSend = function () { }; | ||
NodeHttp.prototype.responseCallback = function (response) { | ||
@@ -56,3 +56,2 @@ var _this = this; | ||
}; | ||
; | ||
NodeHttp.prototype.start = function (metadata) { | ||
@@ -70,3 +69,3 @@ var _this = this; | ||
headers: headers, | ||
method: "POST" | ||
method: "POST", | ||
}; | ||
@@ -73,0 +72,0 @@ if (parsedUrl.protocol === "https:") { |
{ | ||
"name": "@injectivelabs/grpc-web-node-http-transport", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "Node HTTP Transport for use with @injectivelabs/grpc-web", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -20,3 +20,3 @@ # @improbable-eng/grpc-web-node-http-transport | ||
```typescript | ||
import { grpc } from "@improbable-eng/grpc-web"; | ||
import { grpc } from "@injectivelabs/grpc-web"; | ||
import { NodeHttpTransport } from "@improbable-eng/grpc-web-node-http-transport"; | ||
@@ -26,2 +26,2 @@ | ||
grpc.setDefaultTransport(NodeHttpTransport()); | ||
``` | ||
``` |
import * as http from "http"; | ||
import * as https from "https"; | ||
import * as url from "url"; | ||
import { grpc } from "@improbable-eng/grpc-web"; | ||
import { grpc } from "@injectivelabs/grpc-web"; | ||
export function NodeHttpTransport(httpsOptions?: https.RequestOptions): grpc.TransportFactory { | ||
export function NodeHttpTransport( | ||
httpsOptions?: https.RequestOptions | ||
): grpc.TransportFactory { | ||
return (opts: grpc.TransportOptions) => { | ||
@@ -16,3 +18,6 @@ return new NodeHttp(opts, httpsOptions); | ||
constructor(transportOptions: grpc.TransportOptions, readonly httpsOptions?: https.RequestOptions) { | ||
constructor( | ||
transportOptions: grpc.TransportOptions, | ||
readonly httpsOptions?: https.RequestOptions | ||
) { | ||
this.options = transportOptions; | ||
@@ -22,5 +27,8 @@ } | ||
sendMessage(msgBytes: Uint8Array) { | ||
if (!this.options.methodDefinition.requestStream && !this.options.methodDefinition.responseStream) { | ||
// Disable chunked encoding if we are not using streams | ||
this.request.setHeader("Content-Length", msgBytes.byteLength); | ||
if ( | ||
!this.options.methodDefinition.requestStream && | ||
!this.options.methodDefinition.responseStream | ||
) { | ||
// Disable chunked encoding if we are not using streams | ||
this.request.setHeader("Content-Length", msgBytes.byteLength); | ||
} | ||
@@ -31,6 +39,4 @@ this.request.write(toBuffer(msgBytes)); | ||
finishSend() { | ||
finishSend() {} | ||
} | ||
responseCallback(response: http.IncomingMessage) { | ||
@@ -41,3 +47,3 @@ this.options.debug && console.log("NodeHttp.response", response.statusCode); | ||
response.on("data", chunk => { | ||
response.on("data", (chunk) => { | ||
this.options.debug && console.log("NodeHttp.data", chunk); | ||
@@ -51,3 +57,3 @@ this.options.onChunk(toArrayBuffer(chunk as Buffer)); | ||
}); | ||
}; | ||
} | ||
@@ -66,10 +72,16 @@ start(metadata: grpc.Metadata) { | ||
headers: headers, | ||
method: "POST" | ||
method: "POST", | ||
}; | ||
if (parsedUrl.protocol === "https:") { | ||
this.request = https.request({ ...httpOptions, ...this?.httpsOptions }, this.responseCallback.bind(this)); | ||
this.request = https.request( | ||
{ ...httpOptions, ...this?.httpsOptions }, | ||
this.responseCallback.bind(this) | ||
); | ||
} else { | ||
this.request = http.request(httpOptions, this.responseCallback.bind(this)); | ||
this.request = http.request( | ||
httpOptions, | ||
this.responseCallback.bind(this) | ||
); | ||
} | ||
this.request.on("error", err => { | ||
this.request.on("error", (err) => { | ||
this.options.debug && console.log("NodeHttp.error", err); | ||
@@ -86,4 +98,6 @@ this.options.onEnd(err); | ||
function filterHeadersForUndefined(headers: {[key: string]: string | string[] | undefined}): {[key: string]: string | string[]} { | ||
const filteredHeaders: {[key: string]: string | string[]} = {}; | ||
function filterHeadersForUndefined(headers: { | ||
[key: string]: string | string[] | undefined; | ||
}): { [key: string]: string | string[] } { | ||
const filteredHeaders: { [key: string]: string | string[] } = {}; | ||
@@ -90,0 +104,0 @@ for (let key in headers) { |
Sorry, the diff of this file is not supported yet
13079
248
26