fix(cors): fix bugs handling pre-flight (#792)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Yusuke Wada · committed by GitHub · parent 897c401 · Browse files · 4 files·+9 added−13 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+9 added−13 removed
- tests:+4 added−4 removed
- other:+5 added−9 removed
Provenance
- Commit
4298f82- Tree
6b54092- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
GitHub <noreply@github.com>- Parent
897c401
4 files changed+9 added−13 removed
modified:deno_dist/middleware/cors/index.ts+2 added-4 removed
30 unmodified lines31323334353536373817 unmodified lines565758596162606162636417 unmodified lines828384858886878889903 unmodified lines30 unmodified lines } })(opts.origin)
return async (c, next) => { function set(key: string, value: string) { c.res.headers.append(key, value) }
17 unmodified lines }
if (c.req.method !== 'OPTIONS') { await next() } else { } else { // Preflight
if (opts.maxAge != null) { set('Access-Control-Max-Age', opts.maxAge.toString())17 unmodified lines
c.res.headers.delete('Content-Length') c.res.headers.delete('Content-Type')
c.res = new Response(null, { return new Response(null, { headers: c.res.headers, status: 204, statusText: c.res.statusText, })3 unmodified linesmodified:deno_dist/utils/encode.ts+1 added-1 removed
11234572 unmodified linesimport { Buffer } from "https://deno.land/std@0.170.0/node/buffer.ts";import { Buffer } from "https://deno.land/std@0.171.0/node/buffer.ts";export const encodeBase64 = (str: string): string => { if (str === null) { throw new TypeError('1st argument of "encodeBase64" should not be null.') }72 unmodified linesmodified:src/middleware/cors/index.test.ts+4 added-4 removed
29 unmodified lines30 origin: (origin) => (origin.endsWith('.example.com') ? origin : 'http://example.com'),31 })32 )33 34- app.all('/api/abc', (c) => {34+ app.get('/api/abc', (c) => {35 return c.json({ success: true })36 })37- app.all('/api2/abc', (c) => {37+ app.get('/api2/abc', (c) => {38 return c.json({ success: true })39 })40- app.all('/api3/abc', (c) => {40+ app.get('/api3/abc', (c) => {41 return c.json({ success: true })42 })43- app.all('/api4/abc', (c) => {43+ app.get('/api4/abc', (c) => {44 return c.json({ success: true })45 })46 47 it('GET default', async () => {86 unmodified linesmodified:src/middleware/cors/index.ts+2 added-4 removed
30 unmodified lines31 }32 })(opts.origin)33 34 return async (c, next) => {35- 35 function set(key: string, value: string) {36 c.res.headers.append(key, value)37 }38 17 unmodified lines56 }57 58 if (c.req.method !== 'OPTIONS') {59 await next()61- }62- else {60+ } else {61 // Preflight62 63 if (opts.maxAge != null) {64 set('Access-Control-Max-Age', opts.maxAge.toString())17 unmodified lines82 83 c.res.headers.delete('Content-Length')84 c.res.headers.delete('Content-Type')85 88- c.res = new Response(null, {86+ return new Response(null, {87 headers: c.res.headers,88 status: 204,89 statusText: c.res.statusText,90 })3 unmodified lines