Skip to content

Commit

Permalink
Migrate from 'request' package to 'node-fetch' in runtime (#2169)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lykoi18 authored Dec 8, 2021
1 parent 08558e7 commit 8e3be22
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 286 deletions.
4 changes: 2 additions & 2 deletions packages/runtime/runtimes/runtime-aws-serverless/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
"lodash.partial": "4.2.1",
"mime-types": "2.1.29",
"qs": "6.9.6",
"request": "2.88.2",
"node-fetch": "2.6.5",
"form-data": "4.0.0",
"resolve-cloud-common": "0.0.149",
"route-trie": "3.0.0",
"source-map-support": "0.5.19"
Expand All @@ -46,7 +47,6 @@
"@types/lodash.escaperegexp": "4.1.6",
"@types/lodash.partial": "4.2.6",
"@types/mime-types": "2.1.1",
"@types/request": "2.48.7",
"aws-sdk": "2.952.0",
"jest": "27.0.3",
"lodash.escaperegexp": "4.1.2",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import fs from 'fs'
import path from 'path'
import request from 'request'
import fetch from 'node-fetch'
import FormData from 'form-data'
import crypto from 'crypto'
import mime from 'mime-types'

Expand Down Expand Up @@ -71,21 +72,26 @@ export const upload = (
const fileStream = fs.createReadStream(filePath)
const contentType =
mime.contentType(path.extname(filePath)) || 'text/plain; charset=utf-8'
return new Promise<void>((resolve, reject) =>
request.put(
{
return new Promise<void>(async (resolve, reject) => {
try {
const res = await fetch(uploadUrl, {
method: 'PUT',
headers: {
'Content-Length': fileSizeInBytes,
'Content-Length': fileSizeInBytes.toString(),
'Content-Type': contentType,
},
uri: uploadUrl,
body: fileStream,
},
(error, _, body) => {
error ? reject(error) : body ? reject(body) : resolve()
})

if (!res.ok) {
throw new Error(await res.text())
}
)
)

resolve()
} catch (error) {
reject(error)
}
})
}

const createPresignedPost = async (
Expand Down Expand Up @@ -133,21 +139,33 @@ export const uploadFormData = (
mime.contentType(path.extname(filePath)) || 'text/plain; charset=utf-8'
form.fields.key = form.fields.Key
delete form.fields.Key
return new Promise<void>((resolve, reject) =>
request.post(
{
url: form.url,
formData: {
...form.fields,
const formData = new FormData()

for (const [key, value] of Object.entries(form)) {
formData.append(key, value)
}

formData.append('file', fileStream)

return new Promise<void>(async (resolve, reject) => {
try {
const res = await fetch(form.url, {
method: 'POST',
headers: {
'Content-Type': contentType,
file: fileStream,
},
},
(error) => {
error ? reject(error) : resolve()
body: formData,
})

if (!res.ok) {
throw new Error(await res.text())
}
)
)

resolve()
} catch (error) {
reject(error)
}
})
}

export const createToken = (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
"mime-types": "2.1.29",
"qs": "6.9.6",
"raw-body": "2.4.1",
"request": "2.88.2",
"node-fetch": "2.6.5",
"form-data": "4.0.0",
"route-trie": "3.0.0",
"source-map-support": "0.5.19",
"uuid": "8.3.2",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import fs from 'fs'
import request from 'request'
import { v4 as uuid } from 'uuid'
import crypto from 'crypto'
import type { Uploader, UploaderPool } from '@resolve-js/runtime-base'
import fetch from 'node-fetch'
import FormData from 'form-data'

export type UploaderPoolLocal = UploaderPool & {
directory: string
Expand Down Expand Up @@ -32,20 +33,25 @@ const createPreSignedPut = async (pool: Pool, dir: string) => {
export const upload = (uploadUrl: string, filePath: string) => {
const fileSizeInBytes = fs.statSync(filePath).size
const fileStream = fs.createReadStream(filePath)
return new Promise<void>((resolve, reject) =>
request.put(
{
return new Promise<void>(async (resolve, reject) => {
try {
const res = await fetch(uploadUrl, {
method: 'PUT',
headers: {
'Content-Length': fileSizeInBytes,
'Content-Length': fileSizeInBytes.toString(),
},
uri: uploadUrl,
body: fileStream,
},
(error, _, body) => {
error ? reject(error) : body ? reject(body) : resolve()
})

if (!res.ok) {
throw new Error(await res.text())
}
)
)

resolve()
} catch (error) {
reject(error)
}
})
}

const createPresignedPost = async (pool: Pool, dir: string) => {
Expand All @@ -64,19 +70,26 @@ const createPresignedPost = async (pool: Pool, dir: string) => {
export const uploadFormData = (form: { url: string }, filePath: string) => {
const fileStream = fs.createReadStream(filePath)

return new Promise<void>((resolve, reject) =>
request.post(
{
url: form.url,
formData: {
file: fileStream,
},
},
(error) => {
error ? reject(error) : resolve()
const formData = new FormData()

formData.append('file', fileStream)

return new Promise<void>(async (resolve, reject) => {
try {
const res = await fetch(form.url, {
method: 'POST',
body: formData,
})

if (!res.ok) {
throw new Error(await res.text())
}
)
)

resolve()
} catch (error) {
reject(error)
}
})
}

const createToken = (
Expand Down
Loading

0 comments on commit 8e3be22

Please sign in to comment.