Skip to content

Commit

Permalink
Merge pull request #2 from aarontravass/aaron/makefetch
Browse files Browse the repository at this point in the history
Used random port for initialization
  • Loading branch information
v 1 r t l authored Jun 10, 2023
2 parents f3e5b28 + 9214db3 commit a42dd5c
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 27 deletions.
1 change: 0 additions & 1 deletion deps.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export { getFreePort } from 'https://deno.land/x/free_port@v1.2.0/mod.ts'
export { expect } from 'https://deno.land/x/expect@v0.3.0/mod.ts'
export {
assertEquals,
Expand Down
71 changes: 46 additions & 25 deletions mod.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,26 @@
import { assertEquals, assertMatch, getFreePort } from './deps.ts'
import { HandlerOrListener } from './types.ts'
import { assertEquals, assertMatch } from './deps.ts'
import { Handler, HandlerOrListener } from './types.ts'

const port = await getFreePort(8080)
// credit - 'https://deno.land/x/free_port@v1.2.0/mod.ts'
function random(min: number, max: number): number {
return Math.round(Math.random() * (max - min)) + min
}

// credit - 'https://deno.land/x/free_port@v1.2.0/mod.ts'
const getFreeListener = (
port: number,
): { listener: Deno.Listener; port: number } => {
try {
const listener = Deno.listen({ port: port })
return { listener, port }
} catch (error) {
if (error instanceof Deno.errors.AddrInUse) {
const newPort = random(1024, 49151)
return getFreeListener(newPort)
}
}
throw Error
}

const fetchEndpoint = async (
port: number,
Expand All @@ -20,10 +39,12 @@ const fetchEndpoint = async (
else data = await res.arrayBuffer()
return { res, data }
}

const makeFetchPromise = (handlerOrListener: HandlerOrListener) => {
// listener
if ('rid' in handlerOrListener && 'addr' in handlerOrListener) {
if ('rid' in handlerOrListener && 'adr' in handlerOrListener) {
// this might never get invoked because of Deno's blocking issue
const port = (handlerOrListener.addr as Deno.NetAddr).port;
if(!port)
throw new Error("Port cannot be found");
return async (url: URL | string = '', params?: RequestInit) => {
const p = new Promise<{ res: Response; data?: unknown }>((resolve) => {
setTimeout(async () => {
Expand All @@ -36,31 +57,30 @@ const makeFetchPromise = (handlerOrListener: HandlerOrListener) => {
const conn = await handlerOrListener.accept()
return p
}
} // (req, conn) => Response listener
else {
const listener = Deno.listen({ port, hostname: 'localhost' })

} else {
const { listener, port } = getFreeListener(random(1024, 49151))
const serve = async (conn: Deno.Conn) => {
const requests = Deno.serveHttp(conn)
const { request, respondWith } = (await requests.nextRequest())!
const response = await handlerOrListener(request, conn)

const response = await (handlerOrListener as Handler)(request, conn)
if (response) {
respondWith(response)
}
}

return async (url: URL | string = '', params?: RequestInit) => {
const p = new Promise<{ res: Response; data?: unknown }>((resolve) => {
setTimeout(async () => {
const { res, data } = await fetchEndpoint(port, url, params)
resolve({ res, data })
Deno.close(conn.rid + 1)
listener.close()
})
})
const conn = await listener.accept()
await serve(conn)
return p
const connector = async () => {
const conn = await listener.accept()
await serve(conn)
return conn
}
const connection = connector()
const res = await fetchEndpoint(port, url, params)
await connection.then((con) => Deno.close(con.rid + 1)).finally(() =>
listener.close()
)
return res
}
}
}
Expand All @@ -69,6 +89,7 @@ export const makeFetch = (h: HandlerOrListener) => {
const resp = makeFetchPromise(h)
async function fetch(url: string | URL, options?: RequestInit) {
const { data, res } = await resp(url, options)

const expectStatus = (a: number, b?: string) => {
assertEquals(
res.status,
Expand All @@ -82,7 +103,7 @@ export const makeFetch = (h: HandlerOrListener) => {
expect: expectAll,
expectStatus,
expectHeader,
expectBody,
expectBody
}
}
const expectHeader = (a: string, b: string | RegExp | null | string[]) => {
Expand Down Expand Up @@ -121,7 +142,7 @@ export const makeFetch = (h: HandlerOrListener) => {
expect: expectAll,
expectStatus,
expectHeader,
expectBody,
expectBody
}
}
const expectBody = (a: unknown) => {
Expand All @@ -143,12 +164,12 @@ export const makeFetch = (h: HandlerOrListener) => {
expectBody,
}
}

return {
expect: expectAll,
expectStatus,
expectHeader,
expectBody,
...res
}
}
return fetch
Expand Down
13 changes: 12 additions & 1 deletion mod_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,17 @@ describe('makeFetch', () => {

res.expect('')
})
it('should assign different ports if called many times', async () => {
const handler: Handler = () => new Response("hello")
const fetch = makeFetch(handler)
const res = await fetch('/')
res.expect("hello")


const fetch_2 = makeFetch(handler)
const res_2 = await fetch_2('/')
res_2.expect("hello")
})
})
describe('expectStatus', () => {
it('should pass with a correct status', async () => {
Expand Down Expand Up @@ -178,4 +189,4 @@ describe('expect', () => {
})
})

run()
run()

0 comments on commit a42dd5c

Please sign in to comment.