Skip to content

Commit

Permalink
bump deps, replace barely maintained deps with source code
Browse files Browse the repository at this point in the history
  • Loading branch information
v1rtl committed Aug 28, 2021
1 parent a408455 commit 2fa2c9d
Show file tree
Hide file tree
Showing 25 changed files with 959 additions and 84 deletions.
4 changes: 2 additions & 2 deletions app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
import { Router, serve, Server, rg, pushMiddleware } from './deps.ts'
import { NextFunction, RHandler as Handler, Middleware, UseMethodParams } from './types.ts'
import { onErrorHandler, ErrorHandler } from './onError.ts'
import { setImmediate } from 'https://deno.land/std@0.103.0/node/timers.ts'
import { setImmediate } from 'https://deno.land/std@0.106.0/node/timers.ts'
import type { Request } from './request.ts'
import type { Response } from './response.ts'
import { getURLParams, getPathname } from './utils/parseUrl.ts'
import { extendMiddleware } from './extend.ts'
import * as path from 'https://deno.land/std@0.103.0/path/mod.ts'
import * as path from 'https://deno.land/std@0.106.0/path/mod.ts'

const lead = (x: string) => (x.charCodeAt(0) === 47 ? x : '/' + x)

Expand Down
13 changes: 7 additions & 6 deletions deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,21 @@ export type { Options as ParseRangeOptions }
export { escapeHtml } from 'https://deno.land/x/escape_html@1.0.0/mod.ts'
export { vary } from 'https://deno.land/x/vary@1.0.0/mod.ts'
export { isIP } from 'https://deno.land/x/isIP@1.0.0/mod.ts'
export { Accepts } from 'https://denopkg.com/talentlessguy/accepts@patch-1/mod.ts#=^'
export { encodeUrl } from 'https://deno.land/x/encodeurl@1.0.0/mod.ts'
export { charset, contentType, lookup } from 'https://deno.land/x/media_types@v2.9.3/mod.ts'
export { charset, contentType, lookup } from 'https://deno.land/x/media_types@v2.10.1/mod.ts'
export { parse as rg } from 'https://deno.land/x/regexparam@v2.0.0/src/index.js'
export { forwarded } from 'https://deno.land/x/forwarded@0.0.10/mod.ts'
export * from 'https://deno.land/x/proxy_addr@0.0.16/mod.ts'
import type { ServerRequest as Req, Response as ServerResponse } from 'https://deno.land/std@0.103.0/http/server.ts'
export { forwarded } from 'https://deno.land/x/forwarded@0.0.12/mod.ts'
export * from 'https://deno.land/x/proxy_addr@0.0.19/mod.ts'
import type { ServerRequest as Req, Response as ServerResponse } from 'https://deno.land/std@0.106.0/http/server.ts'
export { default as ipaddr, IPv4, IPv6 } from 'https://cdn.skypack.dev/ipaddr.js'
interface Res extends ServerResponse {
headers: Headers
}

export { default as Negotiator } from 'https://deno.land/x/negotiator@1.0.1/mod.ts'

export type { Req, Res }

export { serve, Server } from 'https://deno.land/std@0.103.0/http/server.ts'
export { serve, Server } from 'https://deno.land/std@0.106.0/http/server.ts'

export { Router, pushMiddleware } from 'https://esm.sh/@tinyhttp/router@1.3.3'
2 changes: 1 addition & 1 deletion egg.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"files": ["./*.ts", "./utils/*.ts", "./extensions/**/*.ts", "README.md"],
"checkFormat": false,
"checkTests": true,
"checkInstallation": false,
"checkInstallation": true,
"check": true,
"unlisted": false,
"ignore": []
Expand Down
2 changes: 1 addition & 1 deletion examples/jwt/server.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { App } from '../../mod.ts'
import { getNumericDate, Payload, Header, create, verify } from 'https://deno.land/x/djwt@v2.2/mod.ts'
import { readAll } from 'https://deno.land/std@0.103.0/io/util.ts'
import { readAll } from 'https://deno.land/std@0.106.0/io/util.ts'

const SECRET = 'my_secret'

Expand Down
2 changes: 1 addition & 1 deletion examples/mongodb/server.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { App } from '../../mod.ts'
import { MongoClient, Bson } from 'https://deno.land/x/mongo@v0.24.0/mod.ts'
import { MongoClient, Bson } from 'https://deno.land/x/mongo@v0.25.0/mod.ts'
import * as dotenv from 'https://deno.land/x/tiny_env@1.0.0/mod.ts'
import { json } from 'https://deno.land/x/parsec/mod.ts'

Expand Down
2 changes: 1 addition & 1 deletion examples/postgresql/server.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { App } from '../../mod.ts'
import { Client } from 'https://deno.land/x/postgres@v0.11.3/mod.ts'
import { Client } from 'https://deno.land/x/postgres@v0.12.0/mod.ts'
import { json } from 'https://deno.land/x/parsec/mod.ts'

const app = new App()
Expand Down
26 changes: 17 additions & 9 deletions extensions/req/accepts.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
import { Accepts } from '../../deps.ts'
import { Accepts } from '../../utils/accepts.ts'
import type { Req } from '../../deps.ts'

export const getAccepts = <Request extends Req = Req>(req: Request) => (...types: string[]) =>
new Accepts(req.headers).types(types)
export const getAccepts =
<Request extends Req = Req>(req: Request) =>
(...types: string[]) =>
new Accepts(req.headers).types(types)

export const getAcceptsEncodings = <Request extends Req = Req>(req: Request) => (...encodings: string[]) =>
new Accepts(req.headers).encodings(encodings)
export const getAcceptsEncodings =
<Request extends Req = Req>(req: Request) =>
(...encodings: string[]) =>
new Accepts(req.headers).encodings(encodings)

export const getAcceptsCharsets = <Request extends Req = Req>(req: Request) => (...charsets: string[]) =>
new Accepts(req.headers).charsets(charsets)
export const getAcceptsCharsets =
<Request extends Req = Req>(req: Request) =>
(...charsets: string[]) =>
new Accepts(req.headers).charsets(charsets)

export const getAcceptsLanguages = <Request extends Req = Req>(req: Request) => (...languages: string[]) =>
new Accepts(req.headers).languages(languages)
export const getAcceptsLanguages =
<Request extends Req = Req>(req: Request) =>
(...languages: string[]) =>
new Accepts(req.headers).languages(languages)
47 changes: 25 additions & 22 deletions extensions/req/headers.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
import { Req, Res, parseRange, ParseRangeOptions } from '../../deps.ts'
import fresh from 'https://deno.land/x/fresh@v1.0.0/mod.ts'
import { is } from 'https://deno.land/x/type_is@1.0.3/mod.ts'

export const getRequestHeader = <Request extends Req = Req>(req: Request) => (header: string) => {
const lc = header.toLowerCase()

switch (lc) {
case 'referer':
case 'referrer':
return (req.headers.get('referrer') as string) || (req.headers.get('referer') as string)
default:
return req.headers.get(lc) as string
import { is } from '../../utils/type_is.ts'

export const getRequestHeader =
<Request extends Req = Req>(req: Request) =>
(header: string) => {
const lc = header.toLowerCase()

switch (lc) {
case 'referer':
case 'referrer':
return (req.headers.get('referrer') as string) || (req.headers.get('referer') as string)
default:
return req.headers.get(lc) as string
}
}
}
export const getRangeFromHeader = <Request extends Req = Req>(req: Request) => (
size: number,
options?: ParseRangeOptions
) => {
const range = getRequestHeader(req)('Range')
export const getRangeFromHeader =
<Request extends Req = Req>(req: Request) =>
(size: number, options?: ParseRangeOptions) => {
const range = getRequestHeader(req)('Range')

if (!range) return
if (!range) return

return parseRange(size, range, options)
}
return parseRange(size, range, options)
}

export const getFreshOrStale = <Request extends Req = Req, Response extends Res = Res>(req: Request, res: Response) => {
const method = req.method
Expand All @@ -48,5 +49,7 @@ export const getFreshOrStale = <Request extends Req = Req, Response extends Res
export const checkIfXMLHttpRequest = <Request extends Req = Req>(req: Request) =>
req.headers?.get('X-Requested-With') === 'XMLHttpRequest'

export const reqIs = <Request extends Req = Req>(req: Request) => (...types: string[]) =>
is(req.headers?.get('content-type') as string, types)
export const reqIs =
<Request extends Req = Req>(req: Request) =>
(...types: string[]) =>
is(req.headers?.get('content-type') as string, types)
2 changes: 1 addition & 1 deletion extensions/res/cookie.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Req, Res } from '../../deps.ts'
import * as cookie from 'https://deno.land/std@0.103.0/http/cookie.ts'
import * as cookie from 'https://deno.land/std@0.106.0/http/cookie.ts'

export const setCookie =
<Request extends Req = Req, Response extends Res = Res>(_: Request, res: Response) =>
Expand Down
58 changes: 29 additions & 29 deletions extensions/res/download.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { contentDisposition } from 'https://esm.sh/@tinyhttp/content-disposition'
import { contentDisposition } from 'https://cdn.skypack.dev/@tinyhttp/content-disposition@2.0.0'
import { SendFileOptions, sendFile } from './send/sendFile.ts'
import { extname } from 'https://deno.land/std@0.103.0/path/mod.ts'
import { extname } from 'https://deno.land/std@0.106.0/path/mod.ts'
import { setContentType, setHeader } from './headers.ts'
import { Req, Res } from '../../deps.ts'

Expand All @@ -9,38 +9,38 @@ export type DownloadOptions = SendFileOptions &
headers: Record<string, unknown>
}>

export const download = <Request extends Req = Req, Response extends Res = Res>(req: Request, res: Response) => (
path: string,
filename?: string,
options: DownloadOptions = {}
): Response => {
const name: string | null = filename as string
let opts: DownloadOptions = options

// set Content-Disposition when file is sent
const headers: Record<string, string> = {
'Content-Disposition': contentDisposition(name || path)
}
export const download =
<Request extends Req = Req, Response extends Res = Res>(req: Request, res: Response) =>
(path: string, filename?: string, options: DownloadOptions = {}): Response => {
const name: string | null = filename as string
let opts: DownloadOptions = options

// merge user-provided headers
if (opts.headers) {
for (const key of Object.keys(opts.headers)) {
if (key.toLowerCase() !== 'content-disposition') headers[key] = opts.headers[key]
// set Content-Disposition when file is sent
const headers: Record<string, string> = {
'Content-Disposition': contentDisposition(name || path)
}

// merge user-provided headers
if (opts.headers) {
for (const key of Object.keys(opts.headers)) {
if (key.toLowerCase() !== 'content-disposition') headers[key] = opts.headers[key]
}
}
}

// merge user-provided options
opts = { ...opts, headers }
// merge user-provided options
opts = { ...opts, headers }

// send file
// send file

return sendFile<Request, Response>(req, res)(path, opts) as Response
}
return sendFile<Request, Response>(req, res)(path, opts) as Response
}

export const attachment = <Response extends Res = Res>(res: Response) => (filename?: string): Response => {
if (filename) setContentType(res)(extname(filename))
export const attachment =
<Response extends Res = Res>(res: Response) =>
(filename?: string): Response => {
if (filename) setContentType(res)(extname(filename))

setHeader(res)('Content-Disposition', contentDisposition(filename))
setHeader(res)('Content-Disposition', contentDisposition(filename))

return res
}
return res
}
2 changes: 1 addition & 1 deletion extensions/res/send/sendFile.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Req, Res } from '../../../deps.ts'
import { isAbsolute, join, extname } from 'https://deno.land/std@0.103.0/path/mod.ts'
import { isAbsolute, join, extname } from 'https://deno.land/std@0.106.0/path/mod.ts'
import { contentType } from '../../../deps.ts'
import { createETag } from '../utils.ts'
import { send } from './send.ts'
Expand Down
2 changes: 1 addition & 1 deletion extensions/res/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { format, parse } from 'https://deno.land/x/content_type@1.0.1/mod.ts'
import { etag as eTag } from 'https://deno.land/x/opine@1.6.0/src/utils/etag.ts'
import { etag as eTag } from 'https://deno.land/x/opine@1.7.2/src/utils/etag.ts'
import { lookup } from '../../deps.ts'

export const createETag = (body: Parameters<typeof eTag>[0]) => {
Expand Down
2 changes: 1 addition & 1 deletion request.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// deno-lint-ignore-file
import { ServerRequest } from 'https://deno.land/std@0.103.0/http/server.ts'
import { ServerRequest } from 'https://deno.land/std@0.106.0/http/server.ts'
import { App } from './app.ts'
import { QueryParams, Ranges, Protocol, AcceptsReturns, Middleware } from './types.ts'

Expand Down
2 changes: 1 addition & 1 deletion response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { SendFileOptions } from './extensions/res/send/sendFile.ts'
import type { TemplateEngineOptions, App } from './app.ts'
import type { FormatProps } from './extensions/res/format.ts'
import type { DownloadOptions } from './extensions/res/download.ts'
import { Cookie } from 'https://deno.land/std@0.103.0/http/cookie.ts'
import { Cookie } from 'https://deno.land/std@0.106.0/http/cookie.ts'

export interface Response<O = any> extends ServerResponse, tinyhttp.Response {
headers: Headers
Expand Down
6 changes: 3 additions & 3 deletions tests/core/app.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { App } from '../../app.ts'
import { BindToSuperDeno, InitAppAndTest } from '../util.ts'
import { renderFile as eta } from 'https://deno.land/x/eta@v1.12.3/mod.ts'
import { EtaConfig } from 'https://deno.land/x/eta@v1.12.3/config.ts'
import * as path from 'https://deno.land/std@0.103.0/path/mod.ts'
import { readFile as readFileCb } from 'https://deno.land/std@0.103.0/node/fs.ts'
import { promisify } from 'https://deno.land/std@0.103.0/node/util.ts'
import * as path from 'https://deno.land/std@0.106.0/path/mod.ts'
import { readFile as readFileCb } from 'https://deno.land/std@0.106.0/node/fs.ts'
import { promisify } from 'https://deno.land/std@0.106.0/node/util.ts'

const readFile = promisify(readFileCb)

Expand Down
54 changes: 54 additions & 0 deletions tests/modules/accepts/charsets.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { assertEquals } from 'https://deno.land/std@0.104.0/testing/asserts.ts'
import { Accepts } from '../../../utils/accepts.ts'

const { test } = Deno

function createRequest(charset?: string) {
const header = new Headers()
if (charset !== undefined) {
header.set('accept-charset', charset)
}
return header
}

test('accepts.charsets() with no arguments when Accept-Charset is populated should return accepted types', function () {
const header = createRequest('utf-8, iso-8859-1;q=0.2, utf-7;q=0.5')
const accept = new Accepts(header)
assertEquals(accept.charsets(), ['utf-8', 'utf-7', 'iso-8859-1'])
})

test('accepts.charsets() when Accept-Charset is not in request should return *', function () {
const header = createRequest()
const accept = new Accepts(header)
assertEquals(accept.charsets(), ['*'])
})

test('accepts.charsets() when Accept-Charset is empty should return an empty array', function () {
const header = createRequest('')
const accept = new Accepts(header)
assertEquals(accept.charsets(), [])
})

test('accepts.charsets() with multiple arguments when Accept-Charset is populated if any types match should return the best fit', function () {
const header = createRequest('utf-8, iso-8859-1;q=0.2, utf-7;q=0.5')
const accept = new Accepts(header)
assertEquals(accept.charsets(['utf-7', 'utf-8']), 'utf-8')
})

test('accepts.charsets() with multiple arguments when Accept-Charset is populated if no types match should return []', function () {
const header = createRequest('utf-8, iso-8859-1;q=0.2, utf-7;q=0.5')
const accept = new Accepts(header)
assertEquals(accept.charsets(['utf-16']), false)
})

test('accepts.charsets() with multiple arguments when Accept-Charset is populated when Accept-Charset is not populated should return the first type', function () {
const header = createRequest()
const accept = new Accepts(header)
assertEquals(accept.charsets(['utf-7', 'utf-8']), 'utf-7')
})

test('accepts.charsets() with an array should return the best fit', function () {
const header = createRequest('utf-8, iso-8859-1;q=0.2, utf-7;q=0.5')
const accept = new Accepts(header)
assertEquals(accept.charsets(['utf-7', 'utf-8']), 'utf-8')
})
Loading

0 comments on commit 2fa2c9d

Please sign in to comment.