-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathutils.ts
124 lines (111 loc) · 2.95 KB
/
utils.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import { version } from "../version.ts";
import https from "node:https";
import http from "node:http";
import qs from "node:querystring";
import { RequestTimeoutError } from "./errors.ts";
import { config } from "./config.ts";
import process from "node:process";
/**
* This `_internals` object is needed to support stubbing/spying of
* certain functions in this file.
* https://deno.land/[email protected]/basics/testing/mocking
*
* It's also useful to encapsulate functions that are polyfilled.
*/
export const _internals = {
execute: execute,
getHostnameAndPort: getHostnameAndPort,
};
/** Facilitates stubbing in tests, e.g. localhost as the base url */
function getHostnameAndPort() {
return {
hostname: "serpapi.com",
port: 443,
};
}
export function getSource() {
const moduleSource = `serpapi@${version}`;
if (typeof Deno == "object") {
const denoVersion = Deno.version?.deno;
if (denoVersion) {
return `deno@${denoVersion},${moduleSource}`;
}
} else if (typeof process == "object") {
const nodeVersion = process.versions?.node;
if (nodeVersion) {
return `nodejs@${nodeVersion},${moduleSource}`;
}
}
return `nodejs,${moduleSource}`;
}
export function buildRequestOptions(
path: string,
parameters: qs.ParsedUrlQueryInput,
): http.RequestOptions {
const clonedParams = { ...parameters };
for (const k in clonedParams) {
if (
k === "requestOptions" ||
k === "timeout" ||
clonedParams[k] === undefined
) {
delete clonedParams[k];
}
}
const basicOptions = {
..._internals.getHostnameAndPort(),
path: `${path}?${qs.stringify(clonedParams)}`,
method: "GET",
};
return {
...config.requestOptions,
...(parameters.requestOptions as http.RequestOptions),
...basicOptions,
};
}
export function execute(
path: string,
parameters: qs.ParsedUrlQueryInput,
timeout: number,
): Promise<string> {
const options = buildRequestOptions(path, {
...parameters,
source: getSource(),
});
return new Promise((resolve, reject) => {
let timer: number;
const handleResponse = (resp: http.IncomingMessage) => {
resp.setEncoding("utf8");
let data = "";
// A chunk of data has been received
resp.on("data", (chunk) => {
data += chunk;
});
// The whole response has been received
resp.on("end", () => {
try {
if (resp.statusCode == 200) {
resolve(data);
} else {
reject(data);
}
} catch (e) {
reject(e);
} finally {
if (timer) clearTimeout(timer);
}
});
};
const handleError = (err: Error) => {
reject(err);
if (timer) clearTimeout(timer);
};
const req = https.get(options, handleResponse).on("error", handleError);
if (timeout > 0) {
timer = setTimeout(() => {
reject(new RequestTimeoutError());
req.destroy();
}, timeout);
}
});
}