Skip to content

Commit

Permalink
fix: use @supabase/node-fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
soedirgo committed Sep 11, 2023
1 parent ae31b44 commit 08a976d
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 40 deletions.
39 changes: 22 additions & 17 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
},
"homepage": "https://github.com/supabase/functions-js#readme",
"dependencies": {
"cross-fetch": "^3.1.5"
"@supabase/node-fetch": "^2.6.14"
},
"devDependencies": {
"@types/jest": "^28.1.0",
Expand Down
3 changes: 2 additions & 1 deletion src/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ export const resolveFetch = (customFetch?: Fetch): Fetch => {
if (customFetch) {
_fetch = customFetch
} else if (typeof fetch === 'undefined') {
_fetch = async (...args) => await (await import('cross-fetch')).fetch(...args)
_fetch = (...args) =>
import('@supabase/node-fetch' as any).then(({ default: fetch }) => fetch(...args))
} else {
_fetch = fetch
}
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export type FunctionInvokeOptions = {
/**
* The HTTP verb of the request
*/
method?: "POST"| "GET"| "PUT" | "PATCH" | "DELETE"
method?: 'POST' | 'GET' | 'PUT' | 'PATCH' | 'DELETE'
/**
* The body of the request.
*/
Expand Down
30 changes: 14 additions & 16 deletions test/functions/hijack/index.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
import { serve } from 'https://deno.land/[email protected]/http/server.ts'

serve((req) => {
const p = Deno.upgradeHttp(req);

(
// Run this async IIFE concurrently, first packet won't arrive
// until we return HTTP101 response.
async () => {
const [conn, firstPacket] = await p
const decoder = new TextDecoder()
const text = decoder.decode(firstPacket)
console.log(text)
// Hello
const uint8Array = new Uint8Array([72, 101, 108, 108, 111])
conn.write(uint8Array)
conn.close()
}
)()
const p = Deno.upgradeHttp(req)

// Run this async IIFE concurrently, first packet won't arrive
// until we return HTTP101 response.
;(async () => {
const [conn, firstPacket] = await p
const decoder = new TextDecoder()
const text = decoder.decode(firstPacket)
console.log(text)
// Hello
const uint8Array = new Uint8Array([72, 101, 108, 108, 111])
conn.write(uint8Array)
conn.close()
})()

// HTTP101 - Switching Protocols
return new Response(null, { status: 101 })
Expand Down
5 changes: 3 additions & 2 deletions test/relay/container.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as fs from 'fs'
import { nanoid } from 'nanoid'
import crossFetch from 'cross-fetch'
// @ts-ignore
import nodeFetch from '@supabase/node-fetch'
import { sign } from 'jsonwebtoken'
import { GenericContainer, Network, StartedTestContainer, Wait } from 'testcontainers'
import { ExecResult } from 'testcontainers/dist/docker/types'
Expand Down Expand Up @@ -87,7 +88,7 @@ export async function runRelay(
log(`check function is healthy: ${slug + '-' + id}`)
for (let ctr = 0; ctr < 30; ctr++) {
try {
const healthCheck = await crossFetch(
const healthCheck = await nodeFetch(
`http://localhost:${startedRelay.getMappedPort(8081)}/${slug}`,
{
method: 'POST',
Expand Down
5 changes: 3 additions & 2 deletions test/utils/fetch.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import crossFetch from 'cross-fetch'
// @ts-ignore
import nodeFetch from '@supabase/node-fetch'

/**
* It returns a crossFetch function that uses overridden input: RequestInfo and init?: RequestInit for
Expand All @@ -11,5 +12,5 @@ export function getCustomFetch(
reqInfo: RequestInfo | URL,
reqInit?: RequestInit | undefined
): (input: RequestInfo | URL, init?: RequestInit | undefined) => Promise<Response> {
return (input, init) => crossFetch(reqInfo, reqInit)
return (input, init) => nodeFetch(reqInfo, reqInit)
}

0 comments on commit 08a976d

Please sign in to comment.