forked from metowolf/Meting-API
-
Notifications
You must be signed in to change notification settings - Fork 202
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
1 parent
6bf840b
commit 4f38d8f
Showing
3 changed files
with
8 additions
and
131 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,98 +1,9 @@ | ||
import app from "../app.js" | ||
|
||
async function writeReadableStreamToWritable(stream, writable) { | ||
let reader = stream.getReader() | ||
import { handle } from 'hono/vercel' | ||
|
||
async function read() { | ||
let { done, value } = await reader.read() | ||
|
||
if (done) { | ||
writable.end() | ||
return | ||
} | ||
|
||
writable.write(value) | ||
|
||
await read() | ||
} | ||
|
||
try { | ||
await read() | ||
} catch (error) { | ||
writable.destroy(error) | ||
throw error | ||
} | ||
} | ||
|
||
class NodeResponse extends Response { | ||
get headers() { | ||
return super.headers | ||
} | ||
|
||
clone() { | ||
return super.clone() | ||
} | ||
} | ||
const getRequestListener = (fetchCallback) => { | ||
return async (incoming, outgoing) => { | ||
const method = incoming.method || 'GET' | ||
const url = `http://${incoming.headers.host}${incoming.url}` | ||
|
||
const headerRecord = {} | ||
const len = incoming.rawHeaders.length | ||
for (let i = 0; i < len; i++) { | ||
if (i % 2 === 0) { | ||
const key = incoming.rawHeaders[i] | ||
headerRecord[key] = incoming.rawHeaders[i + 1] | ||
} | ||
} | ||
|
||
const init = { | ||
method: method, | ||
headers: headerRecord, | ||
} | ||
|
||
if (!(method === 'GET' || method === 'HEAD')) { | ||
const buffers = [] | ||
for await (const chunk of incoming) { | ||
buffers.push(chunk) | ||
} | ||
const buffer = Buffer.concat(buffers) | ||
init['body'] = buffer | ||
} | ||
|
||
let res | ||
|
||
try { | ||
res = (await fetchCallback(new Request(url.toString(), init))) | ||
} catch { | ||
res = new NodeResponse(null, { status: 500 }) | ||
} | ||
|
||
const contentType = res.headers.get('content-type') || '' | ||
const contentEncoding = res.headers.get('content-encoding') | ||
|
||
for (const [k, v] of res.headers) { | ||
if (k === 'set-cookie') { | ||
outgoing.setHeader(k, res.headers.getAll(k)) | ||
} else { | ||
outgoing.setHeader(k, v) | ||
} | ||
} | ||
outgoing.statusCode = res.status | ||
|
||
if (res.body) { | ||
if (!contentEncoding && contentType.startsWith('text')) { | ||
outgoing.end(await res.text()) | ||
} else if (!contentEncoding && contentType.startsWith('application/json')) { | ||
outgoing.end(await res.text()) | ||
} else { | ||
await writeReadableStreamToWritable(res.body, outgoing) | ||
} | ||
} else { | ||
outgoing.end() | ||
} | ||
} | ||
export const config = { | ||
runtime: 'edge', | ||
} | ||
|
||
export default getRequestListener(app.fetch) | ||
export default handle(app) |
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 |
---|---|---|
|
@@ -2,7 +2,10 @@ | |
// import { Buffer } from 'https://esm.sh/[email protected]' | ||
|
||
import crypto from 'crypto-browserify' | ||
import { Buffer } from 'buffer/index.js' | ||
// import { Buffer } from 'buffer/index.js' | ||
|
||
// import crypto from 'crypto' | ||
import { Buffer } from 'buffer' | ||
|
||
const iv = Buffer.from('0102030405060708') | ||
const presetKey = Buffer.from('0CoJUm6Qyw8W8jud') | ||
|