-
Notifications
You must be signed in to change notification settings - Fork 5
Add support for request options #29
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
Open
zyc9012
wants to merge
19
commits into
master
Choose a base branch
from
support-proxy
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+918
−614
Open
Changes from 12 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
9da684f
Add support for proxy
zyc9012 39ca5c3
Update workflows
zyc9012 b74eb37
Upgrade dnt
zyc9012 776d5be
Fix lint
zyc9012 86cc3bb
Fix deno fmt
zyc9012 e986287
Fix deno warning
zyc9012 94e3413
Remove HttpsProxyAgent, allow users to pass http.RequestOptions instead
zyc9012 f6c53ea
Backwards compatibility for Node <= 9
zyc9012 291b207
Add tests for requestOptions
zyc9012 faca477
Update README
zyc9012 6b45021
Disallow modifying basic request options
zyc9012 9032a1e
Remove type annotation for chunk
zyc9012 51f4f33
Add more versions to test matrix
zyc9012 ad87eb3
Add instructions for yarn
zyc9012 73d6b8a
Remove testing against localhost
zyc9012 11e3114
Fix type errors
zyc9012 12b551a
Fix formatting
zyc9012 0546919
Remove files in deno.json
zyc9012 67ce0d4
Adjust import order
zyc9012 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
*/ | ||
|
||
const Dotenv = require("dotenv"); | ||
const process = require("process"); | ||
const { | ||
config, | ||
getJson, | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ | |
*/ | ||
|
||
import Dotenv from "dotenv"; | ||
import process from "process"; | ||
import { | ||
config, | ||
getAccount, | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -1,7 +1,10 @@ | ||||||||
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"; | ||||||||
zyc9012 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
|
||||||||
/** | ||||||||
* This `_internals` object is needed to support stubbing/spying of | ||||||||
|
@@ -12,12 +15,15 @@ import { RequestTimeoutError } from "./errors.ts"; | |||||||
*/ | ||||||||
export const _internals = { | ||||||||
execute: execute, | ||||||||
getBaseUrl: getBaseUrl, | ||||||||
getHostnameAndPort: getHostnameAndPort, | ||||||||
}; | ||||||||
|
||||||||
/** Facilitates stubbing in tests, e.g. localhost as the base url */ | ||||||||
function getBaseUrl() { | ||||||||
return "https://serpapi.com"; | ||||||||
function getHostnameAndPort() { | ||||||||
return { | ||||||||
hostname: "serpapi.com", | ||||||||
port: 443, | ||||||||
}; | ||||||||
} | ||||||||
|
||||||||
export function getSource() { | ||||||||
|
@@ -27,9 +33,7 @@ export function getSource() { | |||||||
if (denoVersion) { | ||||||||
return `deno@${denoVersion},${moduleSource}`; | ||||||||
} | ||||||||
// @ts-ignore: scope of nodejs | ||||||||
} else if (typeof process == "object") { | ||||||||
// @ts-ignore: scope of nodejs | ||||||||
const nodeVersion = process.versions?.node; | ||||||||
if (nodeVersion) { | ||||||||
return `nodejs@${nodeVersion},${moduleSource}`; | ||||||||
|
@@ -38,40 +42,56 @@ export function getSource() { | |||||||
return `nodejs,${moduleSource}`; | ||||||||
} | ||||||||
|
||||||||
export function buildUrl( | ||||||||
export function buildRequestOptions( | ||||||||
path: string, | ||||||||
parameters: qs.ParsedUrlQueryInput, | ||||||||
): string { | ||||||||
): http.RequestOptions { | ||||||||
const clonedParams = { ...parameters }; | ||||||||
for (const k in clonedParams) { | ||||||||
if (clonedParams[k] === undefined) { | ||||||||
if ( | ||||||||
k === "requestOptions" || | ||||||||
k === "timeout" || | ||||||||
clonedParams[k] === undefined | ||||||||
) { | ||||||||
delete clonedParams[k]; | ||||||||
} | ||||||||
} | ||||||||
return `${_internals.getBaseUrl()}${path}?${qs.stringify(clonedParams)}`; | ||||||||
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 url = buildUrl(path, { | ||||||||
const options = buildRequestOptions(path, { | ||||||||
...parameters, | ||||||||
source: getSource(), | ||||||||
}); | ||||||||
|
||||||||
return new Promise((resolve, reject) => { | ||||||||
let timer: number; | ||||||||
const req = https.get(url, (resp) => { | ||||||||
|
||||||||
const handleResponse = (resp: http.IncomingMessage) => { | ||||||||
resp.setEncoding("utf8"); | ||||||||
let data = ""; | ||||||||
|
||||||||
// A chunk of data has been recieved. | ||||||||
// A chunk of data has been received | ||||||||
resp.on("data", (chunk) => { | ||||||||
data += chunk; | ||||||||
}); | ||||||||
|
||||||||
// The whole response has been received. Print out the result. | ||||||||
// The whole response has been received | ||||||||
resp.on("end", () => { | ||||||||
try { | ||||||||
if (resp.statusCode == 200) { | ||||||||
|
@@ -85,10 +105,14 @@ export function execute( | |||||||
if (timer) clearTimeout(timer); | ||||||||
} | ||||||||
}); | ||||||||
}).on("error", (err) => { | ||||||||
}; | ||||||||
|
||||||||
const handleError = (err: Error) => { | ||||||||
reject(err); | ||||||||
if (timer) clearTimeout(timer); | ||||||||
}); | ||||||||
}; | ||||||||
|
||||||||
const req = https.get(options, handleResponse).on("error", handleError); | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
|
||||||||
zyc9012 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
if (timeout > 0) { | ||||||||
timer = setTimeout(() => { | ||||||||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.