Skip to content

Commit

Permalink
feat(polyfill): bump up Hono to v3 and polyfill webcrypto for Node v16 (
Browse files Browse the repository at this point in the history
  • Loading branch information
yusukebe authored Apr 8, 2023
1 parent 1b76cf7 commit cdfe741
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 19 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"@types/jest": "^29.0.1",
"@types/node": "^18.7.16",
"@types/supertest": "^2.0.12",
"hono": "^2.7.1",
"hono": "^3.1.5",
"jest": "^29.0.3",
"next": "13.1.6",
"np": "^7.6.2",
Expand Down
10 changes: 8 additions & 2 deletions src/globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,14 @@ export function installGlobals() {
global.ReadableStream = NodeReadableStream
global.WritableStream = NodeWritableStream

if (typeof global.crypto === "undefined") {
global.crypto = crypto as Crypto
if (typeof global.crypto === 'undefined') {
// If crypto.subtle is undefined, we're in a Node.js v16 environment
if (typeof (crypto as Crypto).subtle === 'undefined') {
// We can use the webcrypto polyfill
global.crypto = require('crypto').webcrypto as Crypto
} else {
global.crypto = crypto as Crypto
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion test/serve-static.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ describe('Serve Static Middleware', () => {
it('Should return 404 for non-existent files', async () => {
const res = await request(server).get('/static/does-not-exist.html')
expect(res.status).toBe(404)
expect(res.headers['content-type']).toBe('text/plain; charset=UTF-8')
expect(res.headers['content-type']).toBe('text/plain;charset=UTF-8')
expect(res.text).toBe('404 Not Found')
})
})
24 changes: 13 additions & 11 deletions test/server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ describe('Response body', () => {
})

describe('Middleware', () => {
const app = new Hono()
const app = new Hono<{ Variables: { foo: string } }>()
app.use('*', poweredBy())
app.use('*', async (c, next) => {
c.set('foo', 'bar')
Expand Down Expand Up @@ -274,7 +274,7 @@ describe('Stream and non-stream response', () => {
await new Promise((resolve) => setTimeout(resolve, 100))
controller.enqueue('data: end\n\n')
controller.close()
}
},
})

c.header('Content-Type', 'text/event-stream; charset=utf-8')
Expand All @@ -288,7 +288,7 @@ describe('Stream and non-stream response', () => {
await new Promise((resolve) => setTimeout(resolve, 100))
controller.enqueue('data: end\n\n')
controller.error(new Error('test'))
}
},
})

c.header('Content-Type', 'text/event-stream; charset=utf-8')
Expand Down Expand Up @@ -323,15 +323,17 @@ describe('Stream and non-stream response', () => {
})

it('Should return text body - stream', async () => {
const res = await request(server).get('/stream').parse((res, fn) => {
const chunks: string[] = ['data: Hello!\n\n', 'data: end\n\n']
let index = 0
res.on('data', (chunk) => {
const str = chunk.toString()
expect(str).toBe(chunks[index++])
const res = await request(server)
.get('/stream')
.parse((res, fn) => {
const chunks: string[] = ['data: Hello!\n\n', 'data: end\n\n']
let index = 0
res.on('data', (chunk) => {
const str = chunk.toString()
expect(str).toBe(chunks[index++])
})
res.on('end', () => fn(null, ''))
})
res.on('end', () => fn(null, ''))
})
expect(res.status).toBe(200)
expect(res.headers['content-length']).toBeUndefined()
expect(res.headers['content-type']).toMatch(/text\/event-stream/)
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2116,10 +2116,10 @@ [email protected]:
resolved "https://registry.yarnpkg.com/hexoid/-/hexoid-1.0.0.tgz#ad10c6573fb907de23d9ec63a711267d9dc9bc18"
integrity sha512-QFLV0taWQOZtvIRIAdBChesmogZrtuXvVWsFHZTk2SU+anspqZ2vMnoLg7IE1+Uk16N19APic1BuF8bC8c2m5g==

hono@^2.7.1:
version "2.7.1"
resolved "https://registry.yarnpkg.com/hono/-/hono-2.7.1.tgz#c7c1ef1ccdf242a79da68a7a2b6a0858b1b84a60"
integrity sha512-hx2pTusx2hD8IAiQfXXX9iapOuqGTJapqX3hg5a7bC7FQemFEiQfVNK/r5F4GnsdJB7AlsGT4ah9fIYh3vmJnw==
hono@^3.1.5:
version "3.1.5"
resolved "https://registry.yarnpkg.com/hono/-/hono-3.1.5.tgz#a1c5314bb1cf0fd8b72bd2b6b6698eee16fbc520"
integrity sha512-ypFLhNYoXXtep4I9zJt3VpB5/Ze3p9BLU4dpnAp7fxHOmSg8lu/Wwjs5sTJnb2GwVdfjbt9KFB9alA4Zt/P0jw==

hosted-git-info@^2.1.4:
version "2.8.9"
Expand Down

0 comments on commit cdfe741

Please sign in to comment.