Repository navigation
Expand file tree
/
Copy pathutils.ts
More file actions
188 lines (172 loc) · 4.61 KB
/
Copy pathutils.ts
File metadata and controls
188 lines (172 loc) · 4.61 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import { version } from "../version.ts";
import https from "node:https";
import http from "node:http";
import qs from "node:querystring";
import process from "node:process";
import { RequestTimeoutError } from "./errors.ts";
import { config } from "./config.ts";
import { createMultipartBody } from "./multipart.ts";
/**
* This `_internals` object is needed to support stubbing/spying of
* certain functions in this file.
* https://deno.land/manual@v1.28.3/basics/testing/mocking
*
* It's also useful to encapsulate functions that are polyfilled.
*/
export const _internals = {
execute: execute,
uploadImage: uploadImage,
getHostnameAndPort: getHostnameAndPort,
};
/** Facilitates stubbing in tests */
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: ReturnType<typeof setTimeout>;
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);
}
});
}
export function uploadImage(
image: Uint8Array | ArrayBuffer,
parameters: {
api_key: string;
requestOptions?: http.RequestOptions;
},
timeout: number,
): Promise<string> {
const bytes = image instanceof ArrayBuffer ? new Uint8Array(image) : image;
const multipart = createMultipartBody([
{ name: "api_key", value: parameters.api_key },
{ name: "source", value: getSource() },
{
name: "image",
value: bytes,
filename: "image",
contentType: "application/octet-stream",
},
]);
const customOptions = {
...config.requestOptions,
...parameters.requestOptions,
};
const options: http.RequestOptions = {
...customOptions,
..._internals.getHostnameAndPort(),
path: "/image",
method: "POST",
headers: {
...(customOptions.headers || {}),
"Content-Type": multipart.contentType,
"Content-Length": multipart.body.length,
},
};
return new Promise((resolve, reject) => {
let timer: ReturnType<typeof setTimeout>;
const req = https.request(options, (resp) => {
resp.setEncoding("utf8");
let data = "";
resp.on("data", (chunk) => data += chunk);
resp.on("end", () => {
if (timer) clearTimeout(timer);
if (resp.statusCode === 200) resolve(data);
else reject(data);
});
});
req.on("error", (error) => {
if (timer) clearTimeout(timer);
reject(error);
});
if (timeout > 0) {
timer = setTimeout(() => {
reject(new RequestTimeoutError());
req.destroy();
}, timeout);
}
req.end(multipart.body);
});
}