-
Notifications
You must be signed in to change notification settings - Fork 1
/
NodeSupport.ts
58 lines (52 loc) · 1.48 KB
/
NodeSupport.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import * as _url from "url"; // KEEP
import * as _http from "http"; // KEEP
import * as _https from "https"; // KEEP
import * as _tls from "tls"; // KEEP
import * as _util from "util"; // KEEP
// import * as _http2 from "http2"; // KEEP
declare var require: any;
declare var process: any;
/**
* Node.js 環境かどうか調べる
* @returns {boolean} Node.js 環境なら true
* @private
*/
export const isNodeJs = (): boolean => {
return typeof require !== "undefined" && typeof process !== "undefined";
};
/**
* Node.js の require。
* Node.js でのみ動作する。ブラウザ環境では何もしない。
* browserify/webpack などの展開対象外。
* @param {string} module
* @return {any}
* @private
*/
export const _node_require = (module: string): any => {
try {
if (isNodeJs()) {
// node.js
return eval('require')(module);
} else {
// browser
return null;
}
} catch (e) {
// maybe browser
return null;
}
};
// Node.js: モジュールロード
export const URL = _node_require('url');
export const https = _node_require('https');
export const http = _node_require('http');
export const tls = _node_require('tls');
export const fs = _node_require('fs');
export const util = _node_require('util');
let __http2 = null;
try {
__http2 = _node_require('http2'); // Node.js v8.4.x 以上が必要、experimental
} catch (e) {
// no http2 module
}
export const http2 = __http2;