Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(cloudflare-workers): remove @cloudflare/workers-types #2773

Merged
merged 3 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified bun.lockb
Binary file not shown.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,6 @@
"nodejs"
],
"devDependencies": {
"@cloudflare/workers-types": "^4.20231121.0",
"@hono/eslint-config": "^0.0.4",
"@hono/node-server": "^1.8.2",
"@types/crypto-js": "^4.1.1",
Expand Down
4 changes: 2 additions & 2 deletions src/adapter/cloudflare-workers/serve-static.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// @denoify-ignore
import type { KVNamespace } from '@cloudflare/workers-types'
import { serveStatic as baseServeStatic } from '../../middleware/serve-static'
import type { ServeStaticOptions as BaseServeStaticOptions } from '../../middleware/serve-static'
import type { Env, MiddlewareHandler } from '../../types'
import { getContentFromKVAsset } from './utils'

export type ServeStaticOptions<E extends Env = Env> = BaseServeStaticOptions<E> & {
namespace?: KVNamespace
// namespace is KVNamespace
namespace?: unknown
manifest: object | string
}

Expand Down
11 changes: 7 additions & 4 deletions src/adapter/cloudflare-workers/utils.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
// @denoify-ignore
import type { KVNamespace } from '@cloudflare/workers-types'
declare const __STATIC_CONTENT: KVNamespace
// __STATIC_CONTENT is KVNamespace
declare const __STATIC_CONTENT: unknown
declare const __STATIC_CONTENT_MANIFEST: string

export type KVAssetOptions = {
manifest?: object | string
namespace?: KVNamespace
// namespace is KVNamespace
namespace?: unknown
}

export const getContentFromKVAsset = async (
Expand All @@ -28,7 +29,8 @@ export const getContentFromKVAsset = async (
}
}

let ASSET_NAMESPACE: KVNamespace
// ASSET_NAMESPACE is KVNamespace
let ASSET_NAMESPACE: unknown
if (options && options.namespace) {
ASSET_NAMESPACE = options.namespace
} else {
Expand All @@ -40,6 +42,7 @@ export const getContentFromKVAsset = async (
return null
}

// @ts-expect-error ASSET_NAMESPACE is not typed
const content = await ASSET_NAMESPACE.get(key, { type: 'stream' })
if (!content) {
return null
Expand Down
27 changes: 9 additions & 18 deletions src/adapter/cloudflare-workers/websocket.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,19 @@
// @denoify-ignore
/* eslint-disable @typescript-eslint/ban-ts-comment */
import type { WebSocketPair } from '@cloudflare/workers-types'
import type { UpgradeWebSocket, WSContext, WSReadyState } from '../../helper/websocket'

// Based on https://github.com/honojs/hono/issues/1153#issuecomment-1767321332
export const upgradeWebSocket: UpgradeWebSocket = (createEvents) => async (c, next) => {
const CFWebSocketPair = (
globalThis as unknown as {
WebSocketPair: typeof WebSocketPair
}
).WebSocketPair

const events = await createEvents(c)

const upgradeHeader = c.req.header('Upgrade')
if (upgradeHeader !== 'websocket') {
return await next()
}

const webSocketPair = new CFWebSocketPair()
const client = webSocketPair[0]
const server = webSocketPair[1]
// @ts-expect-error WebSocketPair is not typed
const webSocketPair = new WebSocketPair()
const client: WebSocket = webSocketPair[0]
const server: WebSocket = webSocketPair[1]

const wsContext: WSContext = {
binaryType: 'arraybuffer',
Expand All @@ -36,29 +29,27 @@ export const upgradeWebSocket: UpgradeWebSocket = (createEvents) => async (c, ne
send: (source) => server.send(source),
}
if (events.onOpen) {
server.addEventListener(
'open',
(evt) => events.onOpen && events.onOpen(evt as Event, wsContext)
)
server.addEventListener('open', (evt: Event) => events.onOpen && events.onOpen(evt, wsContext))
}
if (events.onClose) {
server.addEventListener(
'close',
(evt) => events.onClose && events.onClose(evt as CloseEvent, wsContext)
(evt: CloseEvent) => events.onClose && events.onClose(evt, wsContext)
)
}
if (events.onMessage) {
server.addEventListener(
'message',
(evt) => events.onMessage && events.onMessage(evt as MessageEvent, wsContext)
(evt: MessageEvent) => events.onMessage && events.onMessage(evt, wsContext)
)
}
if (events.onError) {
server.addEventListener(
'error',
(evt) => events.onError && events.onError(evt as ErrorEvent as Event, wsContext)
(evt: Event) => events.onError && events.onError(evt, wsContext)
)
}
// @ts-expect-error server.accept is not typed
server.accept()
return new Response(null, {
status: 101,
Expand Down
7 changes: 1 addition & 6 deletions yarn.lock
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1
# bun ./bun.lockb --hash: 03E95F4C55D78A53-2aac63394bface86-D16648EEBEA28A46-92573638c505f925
# bun ./bun.lockb --hash: B415720B4387FBBC-dbeb2a7922252ada-0D12CCEFAA865A1E-cd52ff612e3f385f


"@aashutoshrathi/word-wrap@^1.2.3":
Expand Down Expand Up @@ -159,11 +159,6 @@
resolved "https://registry.npmjs.org/@cloudflare/workerd-windows-64/-/workerd-windows-64-1.20231030.0.tgz"
integrity sha512-fb/Jgj8Yqy3PO1jLhk7mTrHMkR8jklpbQFud6rL/aMAn5d6MQbaSrYOCjzkKGp0Zng8D2LIzSl+Fc0C9Sggxjg==

"@cloudflare/workers-types@^4.20231121.0":
version "4.20240117.0"
resolved "https://registry.npmjs.org/@cloudflare/workers-types/-/workers-types-4.20240117.0.tgz"
integrity sha512-HQU8lJhaJVh8gQXFtVA7lZwd0hK1ckIFjRuxOXkVN2Z9t7DtzNbA2YTwBry5thKNgF5EwjN4THjHg5NUZzj05A==

"@esbuild-plugins/node-globals-polyfill@^0.2.3":
version "0.2.3"
resolved "https://registry.npmjs.org/@esbuild-plugins/node-globals-polyfill/-/node-globals-polyfill-0.2.3.tgz"
Expand Down