Skip to content

Commit

Permalink
Merge branch 'main' into pr-firebase-fix
Browse files Browse the repository at this point in the history
  • Loading branch information
noire-munich committed Apr 3, 2021
2 parents 32cadb7 + 4642797 commit 2937ee5
Show file tree
Hide file tree
Showing 50 changed files with 1,255 additions and 531 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2020 Redwood
Copyright (c) 2021 Redwood

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion lerna.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "0.28.1",
"version": "0.28.4",
"npmClient": "yarn",
"useWorkspaces": true,
"command": {
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"build:types": "ttsc --build --verbose",
"build:clean": "rimraf ./packages/**/dist",
"build:watch": "lerna run build:watch --parallel; ttsc --build",
"build:link": "./tasks/build-link",
"test": "lerna run test --stream -- --colors --maxWorkers=4",
"lint": "cross-env __REDWOOD__CONFIG_PATH=packages/create-redwood-app/template eslint -c .eslintrc.js packages",
"lint:fix": "cross-env __REDWOOD__CONFIG_PATH=packages/create-redwood-app/template eslint -c .eslintrc.js --fix packages"
Expand Down Expand Up @@ -41,6 +42,7 @@
"lerna": "^3.20.2",
"msw": "0.21.2",
"nodemon": "^2.0.6",
"npm-packlist": "^2.1.5",
"rimraf": "^3.0.2",
"ttypescript": "^1.5.12",
"typescript": "^4.1.3",
Expand Down
2 changes: 1 addition & 1 deletion packages/api-server/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@redwoodjs/api-server",
"description": "Redwood's HTTP server for Serverless Functions",
"version": "0.28.1",
"version": "0.28.4",
"bin": {
"api-server": "./dist/index.js",
"rw-api-server": "./dist/index.js",
Expand Down
9 changes: 6 additions & 3 deletions packages/api-server/src/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ import escape from 'lodash.escape'
import morgan from 'morgan'

import { getPaths } from '@redwoodjs/internal'
const rwjsPaths = getPaths()

import { requestHandler } from './requestHandlers/awsLambda'

const rwjsPaths = getPaths()

export type Lambdas = Record<string, Handler>
const LAMBDA_FUNCTIONS: Lambdas = {}
export const setLambdaFunctions = (foundFunctions: string[]) => {
Expand Down Expand Up @@ -46,9 +47,11 @@ const lambdaRequestHandler = async (req: Request, res: Response) => {
export const http = ({
port = 8911,
socket,
rootPath,
}: {
port: number
socket?: string
rootPath?: string
}) => {
const app = express()

Expand All @@ -67,8 +70,8 @@ export const http = ({

app.use(morgan<Request, Response>('dev'))

app.all('/:routeName', lambdaRequestHandler)
app.all('/:routeName/*', lambdaRequestHandler)
app.all(`${rootPath}:routeName`, lambdaRequestHandler)
app.all(`${rootPath}:routeName/*`, lambdaRequestHandler)

const server = app
.listen(socket || port, () => {
Expand Down
21 changes: 17 additions & 4 deletions packages/api-server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,28 @@ if (process.argv0 === 'api-server') {
console.log()
}

const { port, socket } = yargs
const { port, socket, rootPath } = yargs
.option('port', { default: 8911, type: 'number', alias: 'p' })
.option('socket', { type: 'string' }).argv
.option('socket', { type: 'string' })
.option('rootPath', {
alias: 'root-path',
default: '/',
type: 'string',
desc: 'Root path where your api functions are served',
})
.coerce('rootPath', (path) => {
// Make sure that we create a root path that starts and ends with a slash (/)
const prefix = path.charAt(0) !== '/' ? '/' : ''
const suffix = path.charAt(path.length - 1) !== '/' ? '/' : ''

http({ port, socket }).on('listening', () => {
return `${prefix}${path}${suffix}`
}).argv

http({ port, socket, rootPath }).on('listening', () => {
if (socket) {
console.log(`Listening on ${socket}`)
} else {
console.log(`Listening on http://localhost:${port}`)
console.log(`Listening on http://localhost:${port}${rootPath}`)
}
console.log()
})
10 changes: 5 additions & 5 deletions packages/api/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@redwoodjs/api",
"version": "0.28.1",
"version": "0.28.4",
"files": [
"dist",
"logger"
Expand All @@ -11,9 +11,9 @@
"dependencies": {
"@graphql-tools/merge": "6.2.10",
"@prisma/client": "2.19.0",
"@redwoodjs/internal": "^0.28.1",
"@redwoodjs/internal": "^0.28.4",
"@types/pino": "^6.3.6",
"apollo-server-lambda": "2.18.2",
"apollo-server-lambda": "2.22.2",
"core-js": "3.6.5",
"graphql": "15.5.0",
"graphql-scalars": "1.9.0",
Expand All @@ -25,8 +25,8 @@
"pino-pretty": "^4.7.0"
},
"devDependencies": {
"@redwoodjs/auth": "^0.28.1",
"@redwoodjs/dev-server": "^0.28.1",
"@redwoodjs/auth": "^0.28.4",
"@redwoodjs/dev-server": "^0.28.4",
"@types/jsonwebtoken": "^8.3.9",
"@types/lodash.merge": "^4.6.6",
"@types/lodash.omitby": "^4.6.6",
Expand Down
23 changes: 23 additions & 0 deletions packages/api/src/auth/decoders/auth0.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import jwt from 'jsonwebtoken'

import { verifyAuth0Token } from './auth0'

jest.mock('jsonwebtoken', () => ({
verify: jest.fn(),
decode: jest.fn(),
}))

test('verify, and not decode, should be called in production', () => {
const { NODE_ENV } = process.env
process.env.NODE_ENV = 'production'
process.env.AUTH0_DOMAIN = 'redwoodjs.com'
process.env.AUTH0_AUDIENCE = 'michael bolton'

// @ts-expect-error Ingore this error.
verifyAuth0Token({})

expect(jwt.decode).not.toBeCalled()
expect(jwt.verify).toBeCalled()

process.env.NODE_ENV = NODE_ENV
})
63 changes: 37 additions & 26 deletions packages/api/src/auth/decoders/auth0.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,33 +33,44 @@ export const verifyAuth0Token = (
)
}

const client = jwksClient({
jwksUri: `https://${AUTH0_DOMAIN}/.well-known/jwks.json`,
})

jwt.verify(
bearerToken,
(header, callback) => {
client.getSigningKey(header.kid as string, (error, key) => {
callback(error, key.getPublicKey())
})
},
{
audience: AUTH0_AUDIENCE,
issuer: `https://${AUTH0_DOMAIN}/`,
algorithms: ['RS256'],
},
(verifyError, decoded) => {
if (verifyError) {
return reject(verifyError)
if (
process.env.NODE_ENV === 'development' ||
process.env.NODE_ENV === 'test'
) {
const decoded = jwt.decode(bearerToken)
resolve(
typeof decoded === 'undefined'
? null
: (decoded as Record<string, unknown>)
)
} else {
const client = jwksClient({
jwksUri: `https://${AUTH0_DOMAIN}/.well-known/jwks.json`,
})
jwt.verify(
bearerToken,
(header, callback) => {
client.getSigningKey(header.kid as string, (error, key) => {
callback(error, key.getPublicKey())
})
},
{
audience: AUTH0_AUDIENCE,
issuer: `https://${AUTH0_DOMAIN}/`,
algorithms: ['RS256'],
},
(verifyError, decoded) => {
if (verifyError) {
return reject(verifyError)
}
resolve(
typeof decoded === 'undefined'
? null
: (decoded as Record<string, unknown>)
)
}
resolve(
typeof decoded === 'undefined'
? null
: (decoded as Record<string, unknown>)
)
}
)
)
}
})
}

Expand Down
2 changes: 1 addition & 1 deletion packages/auth/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@redwoodjs/auth",
"version": "0.28.1",
"version": "0.28.4",
"files": [
"dist"
],
Expand Down
8 changes: 4 additions & 4 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@redwoodjs/cli",
"description": "The Redwood Command Line",
"version": "0.28.1",
"version": "0.28.4",
"license": "MIT",
"bin": {
"redwood": "./dist/index.js",
Expand All @@ -14,9 +14,9 @@
],
"dependencies": {
"@prisma/sdk": "2.19.0",
"@redwoodjs/internal": "^0.28.1",
"@redwoodjs/prerender": "^0.28.1",
"@redwoodjs/structure": "^0.28.1",
"@redwoodjs/internal": "^0.28.4",
"@redwoodjs/prerender": "^0.28.4",
"@redwoodjs/structure": "^0.28.4",
"boxen": "^4.2.0",
"camelcase": "^6.0.0",
"chalk": "^4.1.0",
Expand Down
76 changes: 76 additions & 0 deletions packages/cli/src/commands/setup/tsconfig/tsconfig.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import path from 'path'

import chalk from 'chalk'
import Listr from 'listr'

import {
getInstalledRedwoodVersion,
getPaths,
saveRemoteFileToDisk,
} from 'src/lib'
import c from 'src/lib/colors'

export const command = 'tsconfig'

export const description = 'Setup tsconfig for web and api sides'

export const builder = (yargs) => {
yargs.option('force', {
alias: 'f',
default: false,
description: 'Overwrite existing tsconfig.json files',
type: 'boolean',
})
}

export const handler = async ({ force }) => {
const installedRwVersion = getInstalledRedwoodVersion()
const GITHUB_VERSION_TAG = installedRwVersion.match('canary')
? 'main'
: `v${installedRwVersion}`

const CRWA_TEMPLATE_URL = `https://raw.githubusercontent.com/redwoodjs/redwood/${GITHUB_VERSION_TAG}/packages/create-redwood-app/template`

const tasks = new Listr([
{
title: 'Creating tsconfig in web',
task: () => {
const webConfigPath = path.join(getPaths().web.base, 'tsconfig.json')

const templateUrl = `${CRWA_TEMPLATE_URL}/web/tsconfig.json`

return saveRemoteFileToDisk(templateUrl, webConfigPath, {
overwriteExisting: force,
})
},
},
{
title: 'Creating tsconfig in api',
task: () => {
const webConfigPath = path.join(getPaths().api.base, 'tsconfig.json')

const templateUrl = `${CRWA_TEMPLATE_URL}/api/tsconfig.json`

return saveRemoteFileToDisk(templateUrl, webConfigPath, {
overwriteExisting: force,
})
},
},
{
title: 'One more thing...',
task: (_ctx, task) => {
task.title = `One more thing...\n
${c.green('Quick link to the docs on configuring TypeScript')}
${chalk.hex('#e8e8e8')('https://redwoodjs.com/docs/typescript')}
`
},
},
])

try {
await tasks.run()
} catch (e) {
console.error(c.error(e.message))
process.exit(e?.exitCode || 1)
}
}
11 changes: 9 additions & 2 deletions packages/cli/src/commands/storybook.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,14 @@ export const builder = (yargs) => {
type: 'integer',
default: 7910,
})
.option('build-directory', {
describe: 'Directory in web/ to store static files',
type: 'string',
default: 'public/storybook',
})
}

export const handler = ({ open, port, build }) => {
export const handler = ({ open, port, build, buildDirectory }) => {
const cwd = getPaths().web.base

const staticAssetsFolder = path.join(getPaths().web.base, 'public')
Expand All @@ -46,7 +51,9 @@ export const handler = ({ open, port, build }) => {
'--config-dir ../node_modules/@redwoodjs/core/config/storybook',
!build && `--port ${port}`,
!build && '--no-version-updates',
`--static-dir "${staticAssetsFolder}"`,
!build && `--static-dir "${staticAssetsFolder}"`,
build &&
`--output-dir "${path.join(getPaths().web.base, buildDirectory)}"`,
!open && '--ci',
].filter(Boolean),
{
Expand Down
Loading

0 comments on commit 2937ee5

Please sign in to comment.