Skip to content

Commit

Permalink
fix vercel
Browse files Browse the repository at this point in the history
  • Loading branch information
xizeyoupan committed Sep 25, 2023
1 parent 6bf840b commit 4f38d8f
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 131 deletions.
97 changes: 4 additions & 93 deletions api/index.js
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)
37 changes: 0 additions & 37 deletions esbuild.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,6 @@ import { NodeModulesPolyfillPlugin } from '@esbuild-plugins/node-modules-polyfil
import { NodeGlobalsPolyfillPlugin } from '@esbuild-plugins/node-globals-polyfill'
import textReplace from 'esbuild-plugin-text-replace'

await esbuild.build({
entryPoints: ['./app.js'],
bundle: true,
format: 'esm',
outfile: './dist/cloudflare-workers-min.js',
external: [],
plugins: [
resolve({
crypto: 'crypto-browserify'
}),
NodeGlobalsPolyfillPlugin({
process: true,
buffer: true,
}),
NodeModulesPolyfillPlugin(),
],
minify: true,
});

await esbuild.build({
entryPoints: ['./app.js'],
bundle: true,
format: 'esm',
outfile: './dist/cloudflare-workers.js',
external: [],
plugins: [
resolve({
crypto: 'crypto-browserify'
}),
NodeGlobalsPolyfillPlugin({
process: true,
buffer: true,
}),
NodeModulesPolyfillPlugin(),
],
});

await esbuild.build({
entryPoints: ['./deno.js'],
bundle: true,
Expand Down
5 changes: 4 additions & 1 deletion src/providers/netease/crypto.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down

0 comments on commit 4f38d8f

Please sign in to comment.