Skip to content

fix: replace axios with fetch #288

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

Merged
merged 1 commit into from
May 15, 2024
Merged
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"publisher": "DevCycle",
"icon": "media/togglebot.png",
"engines": {
"vscode": "^1.64.0"
"vscode": "^1.64.0",
"node": ">=18.0.0"
},
"repository": {
"type": "git",
Expand Down Expand Up @@ -302,7 +303,6 @@
"@types/vscode-webview": "^1.57.3",
"@vscode/codicons": "^0.0.33",
"@vscode/webview-ui-toolkit": "^1.2.2",
"axios": "^0.28.1",
"change-case": "^4.1.2",
"fuse.js": "^6.6.2",
"js-yaml": "^4.1.0",
Expand Down
56 changes: 28 additions & 28 deletions src/RudderStackService.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,44 @@
import axios from "axios";
import * as vscode from 'vscode'
import { RUDDERSTACK_KEY } from "./analytics";
import { KEYS, StateManager } from "./StateManager";

type RudderstackEvent = {
event: string,
userId: string,
properties: Record<string, unknown>
}

const rudderstackClient = axios.create({
baseURL: 'https://taplyticsncs.dataplane.rudderstack.com/v1/',
headers: {
Authorization: `Basic ${RUDDERSTACK_KEY}`,
'Content-Type': 'application/json'
}
})
import { RUDDERSTACK_KEY } from './analytics'
import { KEYS, StateManager } from './StateManager'

export const trackRudderstackEvent = async (
eventName: string,
orgId?: string,
): Promise<void> => {
const sendMetrics = vscode.workspace.getConfiguration('devcycle-feature-flags').get('sendMetrics')
const sendMetrics = vscode.workspace
.getConfiguration('devcycle-feature-flags')
.get('sendMetrics')
if (sendMetrics) {
const userId = StateManager.getWorkspaceState(KEYS.AUTH0_USER_ID)
if (!userId) { return }
if (!userId) {
return
}
const event = {
event: eventName,
userId: userId,
properties: {
a0_organization: orgId
}
a0_organization: orgId,
},
}
await rudderstackClient.post('track', event).catch((error) => {
if (!axios.isAxiosError(error)) { return }
if (error?.response?.status === 401) {
console.error('Failed to send event. Analytics key is invalid.')
} else {
console.error('Failed to send event. Status: ', error?.response?.status)

try {
const response = await fetch(
'https://taplyticsncs.dataplane.rudderstack.com/v1/track',
{
method: 'POST',
body: JSON.stringify(event),
headers: {
Authorization: `Basic ${RUDDERSTACK_KEY}`,
'Content-Type': 'application/json',
},
},
)
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`)
}
})
} catch (e) {
console.error('Failed to send event. Error: ', e)
}
}
}
40 changes: 24 additions & 16 deletions src/cli/utils/loadCli.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import * as vscode from 'vscode'
import * as path from 'path'
import * as fs from 'fs'
import axios from 'axios'
import tar from 'tar'
import * as tar from 'tar'
import * as stream from 'stream'
import { finished } from 'stream/promises'
import { CLI_VERSION } from '../../constants'
import { showDebugOutput } from '../../utils/showDebugOutput'
import { hideBusyMessage, showBusyMessage } from '../../components/statusBarItem'
import {
hideBusyMessage,
showBusyMessage,
} from '../../components/statusBarItem'

const CLI_ARTIFACTS = 'https://github.com/DevCycleHQ/cli/releases/download'
const SUPPORTED_PLATFORMS = [
Expand All @@ -14,7 +18,7 @@ const SUPPORTED_PLATFORMS = [
'linux-arm',
'linux-x64',
'win32-x64',
'win32-x86'
'win32-x86',
]
const OUTPUT_DIR = path.join(path.resolve(__dirname), '..')
const CLI_ROOT = path.join(OUTPUT_DIR, 'dvc')
Expand All @@ -39,7 +43,9 @@ function isCliLoaded() {
const manifestPath = path.join(CLI_ROOT, 'oclif.manifest.json')
return (
fs.existsSync(CLI_EXEC) &&
fs.existsSync(path.join(CLI_ROOT, 'node_modules/@oclif/core/package.json')) &&
fs.existsSync(
path.join(CLI_ROOT, 'node_modules/@oclif/core/package.json'),
) &&
fs.existsSync(manifestPath) &&
JSON.parse(fs.readFileSync(manifestPath, 'utf8')).version === CLI_VERSION
)
Expand All @@ -51,21 +57,23 @@ function isCliLoaded() {
async function downloadCli() {
const sourceUrl = getTarPath()

showDebugOutput('Attempting to download DevCycle CLI...')

const writeStream = tar.x({ cwd: OUTPUT_DIR })
const response = await axios.get(sourceUrl, {
responseType: 'stream',
const response = await fetch(sourceUrl, {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Validate the response status code from fetch to ensure the stream is valid.

Checking the response status before processing the stream can prevent issues related to unexpected response statuses, such as 404 or 500 errors.

headers: { 'accept-encoding': 'gzip' },
})
response.data.pipe(writeStream)

await new Promise<void>((resolve, reject) => {
writeStream.on('error', (err: Error) => {
showDebugOutput(`Failed to download ${sourceUrl}: ${err.message}`)
reject(err)
})

writeStream.on('close', () => resolve())
})
try {
await finished(
stream.Readable.fromWeb(response.body as any).pipe(writeStream),
)
} catch (e) {
if (e instanceof Error) {
showDebugOutput(`Failed to download ${sourceUrl}: ${e.message}`)
}
throw e
}
showDebugOutput('DevCycle CLI download complete!')
}

Expand Down
47 changes: 1 addition & 46 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -736,20 +736,6 @@ assertion-error@^1.1.0:
resolved "https://registry.npmjs.org/assertion-error/-/assertion-error-1.1.0.tgz"
integrity sha512-jgsaNduz+ndvGyFt3uSuWqvy4lCnIJiovtouQN5JZHOKCS2QuhEdbcQHFhVksz2N2U9hXJo8odG7ETyWlEeuDw==

asynckit@^0.4.0:
version "0.4.0"
resolved "https://registry.yarnpkg.com/asynckit/-/asynckit-0.4.0.tgz#c79ed97f7f34cb8f2ba1bc9790bcc366474b4b79"
integrity sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==

axios@^0.28.1:
version "0.28.1"
resolved "https://registry.yarnpkg.com/axios/-/axios-0.28.1.tgz#2a7bcd34a3837b71ee1a5ca3762214b86b703e70"
integrity sha512-iUcGA5a7p0mVb4Gm/sy+FSECNkPFT4y7wt6OM/CDpO/OnNCvSs3PoMG8ibrC9jRoGYU0gUK5pXVC4NPXq6lHRQ==
dependencies:
follow-redirects "^1.15.0"
form-data "^4.0.0"
proxy-from-env "^1.1.0"

azure-devops-node-api@^11.0.1:
version "11.2.0"
resolved "https://registry.npmjs.org/azure-devops-node-api/-/azure-devops-node-api-11.2.0.tgz"
Expand Down Expand Up @@ -1039,13 +1025,6 @@ colorette@^2.0.14:
resolved "https://registry.yarnpkg.com/colorette/-/colorette-2.0.20.tgz#9eb793e6833067f7235902fcd3b09917a000a95a"
integrity sha512-IfEDxwoWIjkeXL1eXcDiow4UbKjhLdq6/EuSVR9GMN7KVH3r9gQ83e73hsz1Nd1T3ijd5xv1wcWRYO+D6kCI2w==

combined-stream@^1.0.8:
version "1.0.8"
resolved "https://registry.yarnpkg.com/combined-stream/-/combined-stream-1.0.8.tgz#c3d45a8b34fd730631a110a8a2520682b31d5a7f"
integrity sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==
dependencies:
delayed-stream "~1.0.0"

commander@^10.0.1:
version "10.0.1"
resolved "https://registry.yarnpkg.com/commander/-/commander-10.0.1.tgz#881ee46b4f77d1c1dccc5823433aa39b022cbe06"
Expand Down Expand Up @@ -1160,11 +1139,6 @@ deep-is@^0.1.3:
resolved "https://registry.npmjs.org/deep-is/-/deep-is-0.1.4.tgz"
integrity sha512-oIPzksmTg4/MriiaYGO+okXDT7ztn/w3Eptv/+gSIdMdKsJo0u4CfYNFJPy+4SKMuCqGw2wxnA+URMg3t8a/bQ==

delayed-stream@~1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/delayed-stream/-/delayed-stream-1.0.0.tgz#df3ae199acadfb7d440aaae0b29e2272b24ec619"
integrity sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ==

detect-libc@^2.0.0:
version "2.0.2"
resolved "https://registry.npmjs.org/detect-libc/-/detect-libc-2.0.2.tgz"
Expand Down Expand Up @@ -1513,11 +1487,6 @@ flatted@^3.1.0:
resolved "https://registry.npmjs.org/flatted/-/flatted-3.2.7.tgz"
integrity sha512-5nqDSxl8nn5BSNxyR3n4I6eDmbolI6WT+QqR547RwxQapgjQBmtktdP+HTBb/a/zLsbzERTONyUB5pefh5TtjQ==

follow-redirects@^1.14.8@^1.15.6, follow-redirects@^1.15.0:
version "1.15.6"
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.15.6.tgz#7f815c0cda4249c74ff09e95ef97c23b5fd0399b"
integrity sha512-wWN62YITEaOpSK584EZXJafH1AGpO8RVgElfkuXbTOrPX4fIfOyEpW/CsiNd8JdYrAoOvafRTOEnvsO++qCqFA==

foreground-child@^3.1.0:
version "3.1.1"
resolved "https://registry.yarnpkg.com/foreground-child/-/foreground-child-3.1.1.tgz#1d173e776d75d2772fed08efe4a0de1ea1b12d0d"
Expand All @@ -1526,15 +1495,6 @@ foreground-child@^3.1.0:
cross-spawn "^7.0.0"
signal-exit "^4.0.1"

form-data@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/form-data/-/form-data-4.0.0.tgz#93919daeaf361ee529584b9b31664dc12c9fa452"
integrity sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==
dependencies:
asynckit "^0.4.0"
combined-stream "^1.0.8"
mime-types "^2.1.12"

fs-constants@^1.0.0:
version "1.0.0"
resolved "https://registry.npmjs.org/fs-constants/-/fs-constants-1.0.0.tgz"
Expand Down Expand Up @@ -2114,7 +2074,7 @@ mime-db@1.52.0:
resolved "https://registry.npmjs.org/mime-db/-/mime-db-1.52.0.tgz"
integrity sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg==

mime-types@^2.1.12, mime-types@^2.1.27:
mime-types@^2.1.27:
version "2.1.35"
resolved "https://registry.yarnpkg.com/mime-types/-/mime-types-2.1.35.tgz#381a871b62a734450660ae3deee44813f70d959a"
integrity sha512-ZDY+bPm5zTTF+YpCrAU9nK0UgICYPT0QtT1NZWFv4s++TNkcgVaT0g6+4R2uI4MjQjzysHB1zxuWL50hzaeXiw==
Expand Down Expand Up @@ -2530,11 +2490,6 @@ process-nextick-args@~2.0.0:
resolved "https://registry.npmjs.org/process-nextick-args/-/process-nextick-args-2.0.1.tgz"
integrity sha512-3ouUOpQhtgrbOa17J7+uxOTpITYWaGP7/AhoR3+A+/1e9skrzelGi/dXzEYyvbxubEF6Wn2ypscTKiKJFFn1ag==

proxy-from-env@^1.1.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/proxy-from-env/-/proxy-from-env-1.1.0.tgz#e102f16ca355424865755d2c9e8ea4f24d58c3e2"
integrity sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg==

pump@^3.0.0:
version "3.0.0"
resolved "https://registry.npmjs.org/pump/-/pump-3.0.0.tgz"
Expand Down
Loading