Skip to content

Commit

Permalink
fix(types): remove any and fix types of adapter/deno (honojs#3291)
Browse files Browse the repository at this point in the history
* fix(types): remove any and fix types of adapter/deno

* fix some errors

* refactor jsdoc
  • Loading branch information
EdamAme-x authored and TinsFox committed Nov 11, 2024
1 parent f0a508f commit 29157cd
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 28 deletions.
57 changes: 54 additions & 3 deletions src/adapter/deno/deno.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,28 @@
declare namespace Deno {
interface FileHandleLike {
readonly readable: ReadableStream<Uint8Array>
}

/**
* Open the file using the specified path.
*
* @param path The path to open the file.
* @returns FileHandle object.
*/
export function open(path: string): Promise<FileHandleLike>

interface StatsLike {
isDirectory: boolean
}

/**
* Get stats with the specified path.
*
* @param path The path to get stats.
* @returns Stats object.
*/
export function lstatSync(path: string): StatsLike

/**
* Creates a new directory with the specified path.
*
Expand All @@ -12,17 +36,44 @@ declare namespace Deno {
* Write a new file, with the specified path and data.
*
* @param path The path to the file to write.
* @param data The data to write to the file.
* @param data The data to write into the file.
* @returns A promise that resolves when the file is written.
*/
export function writeFile(path: string, data: Uint8Array): Promise<void>

/**
* Errors of Deno
*/
export const errors: Record<string, Function>

export function upgradeWebSocket(
req: Request,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
options: any
options: UpgradeWebSocketOptions
): {
response: Response
socket: WebSocket
}

/**
* Options of `upgradeWebSocket`
*/
export interface UpgradeWebSocketOptions {
/**
* Sets the `.protocol` property on the client-side web socket to the
* value provided here, which should be one of the strings specified in the
* `protocols` parameter when requesting the web socket. This is intended
* for clients and servers to specify sub-protocols to use to communicate to
* each other.
*/
protocol?: string
/**
* If the client does not respond to this frame with a
* `pong` within the timeout specified, the connection is deemed
* unhealthy and is closed. The `close` and `error` events will be emitted.
*
* The unit is seconds, with a default of 30.
* Set to `0` to disable timeouts.
*/
idleTimeout?: number
}
}
6 changes: 2 additions & 4 deletions src/adapter/deno/serve-static.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import type { ServeStaticOptions } from '../../middleware/serve-static'
import { serveStatic as baseServeStatic } from '../../middleware/serve-static'
import type { Env, MiddlewareHandler } from '../../types'

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const { open, lstatSync, errors } = Deno

export const serveStatic = <E extends Env = Env>(
Expand All @@ -13,12 +11,12 @@ export const serveStatic = <E extends Env = Env>(
const getContent = async (path: string) => {
try {
const file = await open(path)
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return file ? (file.readable as any) : null
return file.readable
} catch (e) {
if (!(e instanceof errors.NotFound)) {
console.warn(`${e}`)
}
return null
}
}
const pathResolve = (path: string) => {
Expand Down
22 changes: 1 addition & 21 deletions src/adapter/deno/websocket.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,7 @@
import type { UpgradeWebSocket, WSReadyState } from '../../helper/websocket'
import { WSContext, defineWebSocketHelper } from '../../helper/websocket'

export interface UpgradeWebSocketOptions {
/**
* Sets the `.protocol` property on the client side web socket to the
* value provided here, which should be one of the strings specified in the
* `protocols` parameter when requesting the web socket. This is intended
* for clients and servers to specify sub-protocols to use to communicate to
* each other.
*/
protocol?: string
/**
* If the client does not respond to this frame with a
* `pong` within the timeout specified, the connection is deemed
* unhealthy and is closed. The `close` and `error` event will be emitted.
*
* The unit is seconds, with a default of 30.
* Set to `0` to disable timeouts.
*/
idleTimeout?: number
}

export const upgradeWebSocket: UpgradeWebSocket<WebSocket, UpgradeWebSocketOptions> =
export const upgradeWebSocket: UpgradeWebSocket<WebSocket, Deno.UpgradeWebSocketOptions> =
defineWebSocketHelper(async (c, events, options) => {
if (c.req.header('upgrade') !== 'websocket') {
return
Expand Down

0 comments on commit 29157cd

Please sign in to comment.