-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
131 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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 |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { IncomingMessage } from '../../node/http/request' | ||
import { ServerResponse } from '../../node/http/response' | ||
|
||
export type Handle = (req: IncomingMessage, res: ServerResponse) => Promise <any> | ||
|
||
export type CallHandle = ReturnType<typeof createCall> | ||
|
||
export interface CallContext { | ||
[key: string]: any | ||
url?: string | ||
method?: string | ||
headers?: { [key: string]: string | string[] } | ||
protocol?: string | ||
body?: any | ||
} | ||
|
||
export function createCall (handle: Handle) { | ||
return function callHandle (context: CallContext) { | ||
const req = new IncomingMessage() | ||
const res = new ServerResponse(req) | ||
|
||
req.url = context.url || '/' | ||
req.method = context.method || 'GET' | ||
req.headers = context.headers || {} | ||
req.headers.host = req.headers.host || context.host || undefined | ||
|
||
// @ts-ignore | ||
req.connection.encrypted = req.connection.encrypted || context.protocol === 'https' | ||
|
||
// @ts-ignore | ||
req.body = context.body || null | ||
|
||
return handle(req, res).then(() => ({ | ||
data: res._data.toString(), | ||
headers: res._headers, | ||
status: res.statusCode, | ||
statusText: res.statusMessage | ||
})) | ||
} | ||
} |
This file contains 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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { CallContext, CallHandle } from './call' | ||
|
||
export type FetchOptions = globalThis.RequestInit & CallContext | ||
|
||
export function createFetch (call: CallHandle) { | ||
return async function localFetch (input: string | Request, init: FetchOptions): Promise<Response> { | ||
const url = input.toString() | ||
if (!url.startsWith('/')) { | ||
return fetch(url, init) | ||
} | ||
try { | ||
const r = await call({ url, ...init }) | ||
return new Response(r.data, { | ||
status: r.status, | ||
statusText: r.statusText, | ||
headers: Object.fromEntries(Object.entries(r.headers).map( | ||
([name, value]) => [name, Array.isArray(value) ? value.join(',') : (value || '')]) | ||
) | ||
}) | ||
} catch (error) { | ||
return new Response(error.toString(), { | ||
status: parseInt(error.statusCode || error.code) || 500, | ||
statusText: error.statusText | ||
}) | ||
} | ||
} | ||
} |
This file contains 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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './call' | ||
export * from './fetch' |
This file contains 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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import fetch, { Request, Response, Headers } from 'node-fetch' | ||
|
||
global.fetch = global.fetch || fetch | ||
global.Request = global.Request || Request | ||
global.Response = global.Response || Response | ||
global.Headers = global.Headers || Headers |
This file contains 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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export default ((typeof globalThis !== 'undefined') | ||
? globalThis | ||
: (typeof self !== 'undefined') | ||
? self | ||
: (typeof global !== 'undefined') | ||
? global | ||
: (typeof window !== 'undefined') | ||
? window | ||
: {}) as typeof globalThis |
This file contains 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