-
Notifications
You must be signed in to change notification settings - Fork 200
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add qwik-city middleware (#22)
* feat: add qwik-city middleware * add changeset
- Loading branch information
Showing
10 changed files
with
1,194 additions
and
9 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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@hono/qwik-city': patch | ||
--- | ||
|
||
initial release |
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,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 |
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 @@ | ||
module.exports = require('../../jest.config.js') |
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 @@ | ||
{ | ||
"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" | ||
} | ||
} |
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,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 |
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,8 @@ | ||
{ | ||
"extends": "./tsconfig.json", | ||
"compilerOptions": { | ||
"module": "CommonJS", | ||
"declaration": false, | ||
"outDir": "./dist/cjs" | ||
} | ||
} |
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,8 @@ | ||
{ | ||
"extends": "./tsconfig.json", | ||
"compilerOptions": { | ||
"module": "ESNext", | ||
"declaration": true, | ||
"outDir": "./dist/esm" | ||
} | ||
} |
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 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"compilerOptions": { | ||
"rootDir": "./src", | ||
}, | ||
"include": [ | ||
"src/**/*.ts" | ||
], | ||
} |
Oops, something went wrong.