parent
2f16551b23
commit
a5e33129c5
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
'@hono/qwik-city': patch
|
||||
---
|
||||
|
||||
initial release
|
|
@ -8,7 +8,8 @@
|
|||
"scripts": {
|
||||
"build:hello": "yarn workspace @hono/hello build",
|
||||
"build:zod-validator": "yarn workspace @hono/zod-validator build",
|
||||
"build": "yarn build:hello && yarn build:zod-validator"
|
||||
"build:qwik-city": "yarn workspace @hono/qwik-city build",
|
||||
"build": "run-p build:*"
|
||||
},
|
||||
"license": "MIT",
|
||||
"private": true,
|
||||
|
@ -32,6 +33,7 @@
|
|||
"eslint-plugin-node": "^11.1.0",
|
||||
"jest": "^28.1.2",
|
||||
"jest-environment-miniflare": "^2.10.0",
|
||||
"npm-run-all": "^4.1.5",
|
||||
"prettier": "^2.7.1",
|
||||
"rimraf": "^3.0.2",
|
||||
"ts-jest": "^28.0.5",
|
||||
|
|
|
@ -0,0 +1,26 @@
|
|||
# Qwik City middleware for Hono
|
||||
|
||||
**WIP**
|
||||
|
||||
## Usage
|
||||
|
||||
```ts
|
||||
import { qwikMiddleware } from '@hono/qwik-city'
|
||||
import qwikCityPlan from '@qwik-city-plan'
|
||||
import render from './entry.ssr'
|
||||
import { Hono } from 'hono'
|
||||
|
||||
const app = new Hono()
|
||||
|
||||
app.get('*', qwikMiddleware({ render, qwikCityPlan }))
|
||||
|
||||
export default app
|
||||
```
|
||||
|
||||
## Author
|
||||
|
||||
Yusuke Wada <https://github.com/yusukebe>
|
||||
|
||||
## License
|
||||
|
||||
MIT
|
|
@ -0,0 +1 @@
|
|||
module.exports = require('../../jest.config.js')
|
|
@ -0,0 +1,40 @@
|
|||
{
|
||||
"name": "@hono/qwik-city",
|
||||
"version": "0.0.0",
|
||||
"description": "Qwik City middleware for Hono",
|
||||
"main": "dist/cjs/index.js",
|
||||
"module": "dist/esm/index.js",
|
||||
"types": "dist/esm/index.d.ts",
|
||||
"files": [
|
||||
"dist"
|
||||
],
|
||||
"scripts": {
|
||||
"build:cjs": "tsc -p tsconfig.cjs.json",
|
||||
"build:esm": "tsc -p tsconfig.esm.json",
|
||||
"build": "rimraf dist && yarn build:cjs && yarn build:esm",
|
||||
"prerelease": "yarn build && yarn test",
|
||||
"release": "yarn publish"
|
||||
},
|
||||
"license": "MIT",
|
||||
"private": false,
|
||||
"publishConfig": {
|
||||
"registry": "https://registry.npmjs.org",
|
||||
"access": "public"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/honojs/middleware.git"
|
||||
},
|
||||
"homepage": "https://github.com/honojs/middleware",
|
||||
"peerDependencies": {
|
||||
"@builder.io/qwik-city": "^0.1.0-beta9",
|
||||
"hono": "^3.0.0-rc.4"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@builder.io/qwik-city": "^0.1.0-beta9",
|
||||
"hono": "3.0.0-rc.4"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=18"
|
||||
}
|
||||
}
|
|
@ -0,0 +1,79 @@
|
|||
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||
import type {
|
||||
ServerRenderOptions,
|
||||
ServerRequestEvent,
|
||||
} from '@builder.io/qwik-city/middleware/request-handler'
|
||||
import {
|
||||
mergeHeadersCookies,
|
||||
requestHandler,
|
||||
} from '@builder.io/qwik-city/middleware/request-handler'
|
||||
|
||||
import type { MiddlewareHandler } from 'hono'
|
||||
|
||||
export const qwikMiddleware = (opts: ServerRenderOptions): MiddlewareHandler => {
|
||||
return async (c, next) => {
|
||||
const url = new URL(c.req.url)
|
||||
const serverRequestEv: ServerRequestEvent<Response> = {
|
||||
mode: 'server',
|
||||
locale: undefined,
|
||||
url,
|
||||
request: c.req.raw,
|
||||
getWritableStream: (status, headers, cookies, resolve) => {
|
||||
const { readable, writable } = new TransformStream()
|
||||
const response = new Response(readable, {
|
||||
status,
|
||||
headers: mergeHeadersCookies(headers, cookies),
|
||||
})
|
||||
resolve(response)
|
||||
return writable
|
||||
},
|
||||
platform: {},
|
||||
}
|
||||
const handledResponse = await requestHandler(serverRequestEv, opts)
|
||||
if (handledResponse) {
|
||||
const response = await handledResponse.response
|
||||
if (response) {
|
||||
return response
|
||||
}
|
||||
}
|
||||
await next()
|
||||
}
|
||||
}
|
||||
|
||||
const resolved = Promise.resolve()
|
||||
|
||||
class TextEncoderStream {
|
||||
// minimal polyfill implementation of TextEncoderStream
|
||||
_writer: any
|
||||
readable: any
|
||||
writable: any
|
||||
|
||||
constructor() {
|
||||
this._writer = null
|
||||
this.readable = {
|
||||
pipeTo: (writableStream: any) => {
|
||||
this._writer = writableStream.getWriter()
|
||||
},
|
||||
}
|
||||
this.writable = {
|
||||
getWriter: () => {
|
||||
if (!this._writer) {
|
||||
throw new Error('No writable stream')
|
||||
}
|
||||
const encoder = new TextEncoder()
|
||||
return {
|
||||
write: async (chunk: any) => {
|
||||
if (chunk != null) {
|
||||
await this._writer.write(encoder.encode(chunk))
|
||||
}
|
||||
},
|
||||
close: () => this._writer.close(),
|
||||
ready: resolved,
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-extra-semi
|
||||
;(globalThis as any).TextEncoderStream = TextEncoderStream
|
|
@ -0,0 +1,8 @@
|
|||
{
|
||||
"extends": "./tsconfig.json",
|
||||
"compilerOptions": {
|
||||
"module": "CommonJS",
|
||||
"declaration": false,
|
||||
"outDir": "./dist/cjs"
|
||||
}
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
{
|
||||
"extends": "./tsconfig.json",
|
||||
"compilerOptions": {
|
||||
"module": "ESNext",
|
||||
"declaration": true,
|
||||
"outDir": "./dist/esm"
|
||||
}
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
{
|
||||
"extends": "../../tsconfig.json",
|
||||
"compilerOptions": {
|
||||
"rootDir": "./src",
|
||||
},
|
||||
"include": [
|
||||
"src/**/*.ts"
|
||||
],
|
||||
}
|
Loading…
Reference in New Issue