Skip to content

Commit

Permalink
Run npm run format
Browse files Browse the repository at this point in the history
The `npm run format` task is included in package.json, along with `.prettierrc` config. However it appears it is not being run by developers or CI system.

It fails to run by default due to {{ }} templates in package.json which I had to temporarily modify.
  • Loading branch information
grempe authored and lukeed committed Jun 24, 2019
1 parent f02ad6f commit fd65316
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 104 deletions.
1 change: 1 addition & 0 deletions worker-router/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ wrangler publish
```

#### Serverless

To deploy using serverless add a [`serverless.yml`](https://serverless.com/framework/docs/providers/cloudflare/) file.
32 changes: 16 additions & 16 deletions worker-router/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,27 @@ const Router = require('./router')
* Example of how router can be used in an application
* */
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
event.respondWith(handleRequest(event.request))
})

function handler(request) {
const init = {
headers: { 'content-type': 'application/json' },
}
const body = JSON.stringify({ some: 'json' })
return new Response(body, init)
const init = {
headers: { 'content-type': 'application/json' },
}
const body = JSON.stringify({ some: 'json' })
return new Response(body, init)
}

async function handleRequest(request) {
const r = new Router()
// Replace with the approriate paths and handlers
r.get('.*/bar', () => new Response('responding for /bar'))
r.get('.*/foo', req => handler(req))
r.post('.*/foo.*', req => handler(req))
r.get('/demos/router/foo', req => fetch(req)) // return the response from the origin
const r = new Router()
// Replace with the approriate paths and handlers
r.get('.*/bar', () => new Response('responding for /bar'))
r.get('.*/foo', req => handler(req))
r.post('.*/foo.*', req => handler(req))
r.get('/demos/router/foo', req => fetch(req)) // return the response from the origin

r.get('/', () => new Response('Hello worker!')) // return a default message for the root route
const resp = await r.route(request)
return resp
r.get('/', () => new Response('Hello worker!')) // return a default message for the root route

const resp = await r.route(request)
return resp
}
177 changes: 89 additions & 88 deletions worker-router/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
* boolean indicating if the request uses that HTTP method,
* header, host or referrer.
*/
const Method = method => req => req.method.toLowerCase() === method.toLowerCase()
const Method = method => req =>
req.method.toLowerCase() === method.toLowerCase()
const Connect = Method('connect')
const Delete = Method('delete')
const Get = Method('get')
Expand All @@ -19,102 +20,102 @@ const Host = host => Header('host', host.toLowerCase())
const Referrer = host => Header('referrer', host.toLowerCase())

const Path = regExp => req => {
const url = new URL(req.url)
const path = url.pathname
const match = path.match(regExp) || []
return match[0] === path
const url = new URL(req.url)
const path = url.pathname
const match = path.match(regExp) || []
return match[0] === path
}

/**
* The Router handles determines which handler is matched given the
* conditions present for each request.
*/
class Router {
constructor() {
this.routes = []
}

handle(conditions, handler) {
this.routes.push({
conditions,
handler,
})
return this
}

connect(url, handler) {
return this.handle([Connect, Path(url)], handler)
}

delete(url, handler) {
return this.handle([Delete, Path(url)], handler)
}

get(url, handler) {
return this.handle([Get, Path(url)], handler)
}

head(url, handler) {
return this.handle([Head, Path(url)], handler)
}

options(url, handler) {
return this.handle([Options, Path(url)], handler)
}

patch(url, handler) {
return this.handle([Patch, Path(url)], handler)
}

post(url, handler) {
return this.handle([Post, Path(url)], handler)
}

put(url, handler) {
return this.handle([Put, Path(url)], handler)
}

trace(url, handler) {
return this.handle([Trace, Path(url)], handler)
}

all(handler) {
return this.handle([], handler)
}

route(req) {
const route = this.resolve(req)

if (route) {
return route.handler(req)
constructor() {
this.routes = []
}

return new Response('resource not found', {
status: 404,
statusText: 'not found',
headers: {
'content-type': 'text/plain',
},
})
}

/**
* resolve returns the matching route for a request that returns
* true for all conditions (if any).
*/
resolve(req) {
return this.routes.find(r => {
if (!r.conditions || (Array.isArray(r) && !r.conditions.length)) {
return true
}

if (typeof r.conditions === 'function') {
return r.conditions(req)
}

return r.conditions.every(c => c(req))
})
}
handle(conditions, handler) {
this.routes.push({
conditions,
handler,
})
return this
}

connect(url, handler) {
return this.handle([Connect, Path(url)], handler)
}

delete(url, handler) {
return this.handle([Delete, Path(url)], handler)
}

get(url, handler) {
return this.handle([Get, Path(url)], handler)
}

head(url, handler) {
return this.handle([Head, Path(url)], handler)
}

options(url, handler) {
return this.handle([Options, Path(url)], handler)
}

patch(url, handler) {
return this.handle([Patch, Path(url)], handler)
}

post(url, handler) {
return this.handle([Post, Path(url)], handler)
}

put(url, handler) {
return this.handle([Put, Path(url)], handler)
}

trace(url, handler) {
return this.handle([Trace, Path(url)], handler)
}

all(handler) {
return this.handle([], handler)
}

route(req) {
const route = this.resolve(req)

if (route) {
return route.handler(req)
}

return new Response('resource not found', {
status: 404,
statusText: 'not found',
headers: {
'content-type': 'text/plain',
},
})
}

/**
* resolve returns the matching route for a request that returns
* true for all conditions (if any).
*/
resolve(req) {
return this.routes.find(r => {
if (!r.conditions || (Array.isArray(r) && !r.conditions.length)) {
return true
}

if (typeof r.conditions === 'function') {
return r.conditions(req)
}

return r.conditions.every(c => c(req))
})
}
}

module.exports = Router

0 comments on commit fd65316

Please sign in to comment.