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

Changed Node stblib require to go undetected by Webpack #203

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 26 additions & 11 deletions ts/src/transports/nodeHttp.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as http from "http";
import * as https from "https";
import * as url from "url";
import * as http_type from 'http';
import * as https_type from 'https';
import * as url_type from 'url';
import {Transport, TransportOptions} from "./Transport";
import {Metadata} from "../metadata";

Expand All @@ -11,9 +11,28 @@ export default function nodeHttpRequest(options: TransportOptions): Transport {
return new NodeHttp(options);
}

// fool webpack into allowing optional require
function _require(path: string): any {
return require(path);
}

export function detectNodeHTTPSupport(): boolean {
return typeof window === "undefined";
}

let http: typeof http_type = (undefined as any) as typeof http_type;
let https: typeof https_type = (undefined as any) as typeof https_type;
let url: typeof url_type = (undefined as any) as typeof url_type;

if (detectNodeHTTPSupport()) {
http = _require("http");
https = _require("https");
url = _require("url");
}

class NodeHttp implements Transport {
options: TransportOptions;
request: http.ClientRequest;
request: http_type.ClientRequest;

constructor(transportOptions: TransportOptions) {
this.options = transportOptions;
Expand All @@ -28,12 +47,12 @@ class NodeHttp implements Transport {

}

responseCallback(response: http.IncomingMessage) {
responseCallback(response: http_type.IncomingMessage) {
this.options.debug && console.log("NodeHttp.response", response.statusCode);
const headers = filterHeadersForUndefined(response.headers);
this.options.onHeaders(new Metadata(headers), response.statusCode!);

response.on("data", chunk => {
response.on("data", (chunk: any) => {
this.options.debug && console.log("NodeHttp.data", chunk);
this.options.onChunk(toArrayBuffer(chunk as Buffer));
});
Expand Down Expand Up @@ -63,7 +82,7 @@ class NodeHttp implements Transport {
} else {
this.request = http.request(httpOptions, this.responseCallback.bind(this));
}
this.request.on("error", err => {
this.request.on("error", (err: any) => {
this.options.debug && console.log("NodeHttp.error", err);
this.options.onEnd(err);
});
Expand Down Expand Up @@ -105,7 +124,3 @@ function toBuffer(ab: Uint8Array): Buffer {
}
return buf;
}

export function detectNodeHTTPSupport(): boolean {
return typeof module !== "undefined" && module.exports;
}