1
0
mirror of synced 2026-02-04 12:08:33 -05:00

Compare commits

...

48 Commits

Author SHA1 Message Date
Dillon Raphael
21ca3a9b02 pnpmlock 2022-06-21 10:27:09 -04:00
github-actions[bot]
32274803d9 Version Packages (alpha) (#3449)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2022-06-21 10:25:53 -04:00
Dillon Raphael
9ded8dacba Export useParam & useParams from @blitzjs/next (#3448) 2022-06-21 14:45:29 +02:00
beerose
80ffbeaa4c Update pnpm-lock.yaml 2022-06-19 18:20:49 +02:00
github-actions[bot]
6bde1b07da Version Packages (alpha) (#3445)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2022-06-19 18:18:57 +02:00
Datner
b918055bf3 Add aliases for Blitz CLI commands (#3410)
Co-authored-by: Aleksandra <alexsandra.sikora@gmail.com>
2022-06-19 18:13:43 +02:00
Aleksandra
f9a2971f05 Improve typings in blitz cli package (#3444) 2022-06-19 15:48:27 +02:00
Dillon Raphael
72b08f2269 pnpmlock 2022-06-13 15:45:47 -04:00
github-actions[bot]
2124a4d0c5 Version Packages (alpha) (#3440)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2022-06-13 15:41:12 -04:00
Dillon Raphael
8aee25c58a changeset 2022-06-13 15:37:27 -04:00
Dillon Raphael
f1003faf94 update generator template (#3439) 2022-06-13 15:35:38 -04:00
Andreas Asprou
50468a3bb0 Fix duplicate react-query clients (#3431)
* Fix duplicate react-query clients

* fix tests

* add codemod & remove queryClient export from generator templates

Co-authored-by: Dillon Raphael <dillon@creatorsneverdie.com>
2022-06-13 11:07:41 -04:00
Dillon Raphael
891d91bf4d pnpmlock 2022-06-10 16:19:50 -04:00
github-actions[bot]
f96c953457 Version Packages (alpha) (#3430)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2022-06-10 16:18:48 -04:00
Dillon Raphael
a80d2a8f77 Rename Blitz server plugin type from Middleware to RequestMiddleware (#3428)
* rename to requestMiddleware

* update plugins

* merge main

* changeset
2022-06-10 16:14:21 -04:00
beerose
b336ad05f4 Update pnpm-lock.yaml 2022-06-10 11:32:17 -07:00
github-actions[bot]
39ca0ef8bf Version Packages (alpha) (#3429)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2022-06-10 11:31:27 -07:00
Aleksandra
4cad9cca25 Add queryClient to RPC plugin exports (#3424) 2022-06-10 10:18:50 -07:00
Dillon Raphael
b6fc940bf2 pnpmlock 2022-06-10 11:21:33 -04:00
github-actions[bot]
a946dd5889 Version Packages (alpha) (#3427)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2022-06-10 11:20:25 -04:00
Dillon Raphael
e3750b049d Codemod fixes (#3420)
* change BlitzApiRequest to NextApiRequest & update import map & fail catch for custom server

* fix page consolidation when there are sub directories

* Update prisma db export/import

* Update db imports in templates

* update root types file

* update error messages + merge main + changeset

Co-authored-by: beerose <alexsandra.sikora@gmail.com>
2022-06-10 11:16:18 -04:00
Dillon Raphael
fb01cc7788 pnpmlock 2022-06-10 10:34:59 -04:00
github-actions[bot]
ac8c412da2 Version Packages (alpha) (#3419)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2022-06-10 10:31:19 -04:00
David
dfd2408e95 Added resolverBasePath option to rpc loaders (#3380)
Co-authored-by: beerose <alexsandra.sikora@gmail.com>
2022-06-08 14:50:00 -07:00
Dillon Raphael
9741287050 pnpmlock 2022-06-07 18:28:53 -04:00
github-actions[bot]
0e9c81abdc Version Packages (alpha) (#3409)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2022-06-07 18:28:29 -04:00
Dillon Raphael
9e05d6e155 Filter file extensions in codemod (#3408)
* add allowedExt to getAllFiles for codemod

* pnpmlock

* codemod
2022-06-07 18:24:10 -04:00
beerose
17f70e65ef Update pnpm-lock.yaml 2022-06-07 13:21:28 -07:00
github-actions[bot]
0ddc5a8169 Version Packages (alpha) (#3407)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2022-06-07 13:20:44 -07:00
Aleksandra
e6fb09d494 Use dist/templates for rpc template (#3406) 2022-06-07 13:02:13 -07:00
beerose
d846fc6be9 Update pnpm-lock.yaml 2022-06-07 11:47:05 -07:00
github-actions[bot]
f5e80e3835 Version Packages (alpha) (#3405)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2022-06-07 11:46:31 -07:00
Aleksandra
17ce29e5e4 Update rpc plugin setup in templates (#3404) 2022-06-07 11:33:06 -07:00
Aleksandra
46d9f81adf Update templates directory for codemod (#3402) 2022-06-07 11:04:34 -07:00
Dillon Raphael
994cfc6292 pnpmlock 2022-06-07 12:09:09 -04:00
github-actions[bot]
7811748526 Version Packages (alpha) (#3401)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2022-06-07 12:08:45 -04:00
Dillon Raphael
ce45368334 Add codemods to upgrade from framework to toolkit (#3353)
* init migrate cli command

* make sure migrate command can only be run in legacy blitz directory

* extract codemods into its own package/cli

* removed unused dependencies

* renamed codemods bin directory

* update next.config file to use withBlitz

* codemods update package.json step

* codemod for blitz server & client setup, as well as the blitz rpc route in pages dir

* fix ignore previous step if statement to check for less than instead of equal

* move files from app/pages to root pages directory

* remove blitz babel plugin & update imports inside app dir

* consolidate pages from app dir to root pages dir

* move all api directories to the root pages dir

* use generator templates for blitz rpc route & server/client setup files

* update custom server file

* make custom server step work with require statement & import statements

* useRouterQuery to useRouter().query

* pkg dependecy & import map updates

* Change import map

* Add BlitzLayout to import map

* import withBlitz and wrap App function in _app

* modify _document to use next.js imports

* fix default import for next modules & add useParam to source map

* gssp/gsp

* api routes

* dont run api wrap on rpc/[[...blitz]] & error on usage of local middleware

* DRY cleanup

* update codemod steps

* add ignore extension to getAllFiles

* add more imports to source map

* check for invokeWithMiddleware

* add error counter to middleware checker

* rename codemod & fix silly typo error

* update bin file & change all invokeWithMiddleware to invoke

* add logging from blitz

* manypkg fix

* add codemod test

* lockfile

* fix test

* show errors for invokeWithMiddleware

* update invokeWithMiddleware error message

* line break in new app generator before your new blitz app is ready

* Apply suggestions from code review

* Add changeset

* changeset

* pnpmlock

Co-authored-by: beerose <alexsandra.sikora@gmail.com>
2022-06-07 12:03:37 -04:00
Dillon Raphael
4e9c1f60b6 pnpm lock 2022-06-07 11:36:45 -04:00
github-actions[bot]
508682c8f8 Version Packages (alpha) (#3400)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2022-06-07 11:35:42 -04:00
Dillon Raphael
962eb58af6 Migrate printEnvInfo to CLI (#3394)
* init printEnvInfo function

* changeset
2022-06-07 11:31:56 -04:00
Dillon Raphael
17669b3af8 pnpmlock 2022-06-07 11:06:31 -04:00
github-actions[bot]
ec6299c36a Version Packages (alpha) (#3399)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2022-06-07 11:05:15 -04:00
Dillon Raphael
6ac2d3412a Update generator template npmrc & next versions (#3398)
* add strict-peer-dependencies=false
side-effects-cache=false to npmrc generator template

* add comment to generated npmrc file

* update versions of next for full and minimal templates

* pinned next version to 12.1.6 in generator templates

* add next back to root package

* fix lock file

* add changeset

Co-authored-by: Aleksandra <alexsandra.sikora@gmail.com>
2022-06-07 10:59:28 -04:00
Dillon Raphael
85f9959d1f update pnpm lock file 2022-06-07 10:23:51 -04:00
github-actions[bot]
354f0440d6 Version Packages (alpha) (#3392)
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2022-06-07 10:21:41 -04:00
Dillon Raphael
ac365a0656 No suspense test and Trailing slash test (#3390)
* no suspense test and trailing slash test

* use playwright for integration tests

* remove seed from server mode test in auth integration

* change pkg-dir version for blitz cli

* use pkg-dir 5.0.0

* clean up

* move seed location in auth test

* explict timeout for auth test

Co-authored-by: beerose <alexsandra.sikora@gmail.com>
2022-06-01 20:54:29 -04:00
Aleksandra
0729291099 Add invokeWithCtx (#3391) 2022-05-31 16:06:45 -04:00
Dillon Raphael
9cf924ee86 Regenerate route manifest on page changes (Working build) (#3385) 2022-05-30 10:09:20 -04:00
150 changed files with 6398 additions and 1724 deletions

View File

@@ -0,0 +1,5 @@
---
"@blitzjs/rpc": patch
---
Add queryClient to RPC Plugin exports

View File

@@ -0,0 +1,5 @@
---
"@blitzjs/rpc": patch
---
Add invokeWithCtx function

View File

@@ -0,0 +1,6 @@
---
"@blitzjs/codemod": patch
"@blitzjs/generator": patch
---
codemod fixes

View File

@@ -0,0 +1,5 @@
---
"@blitzjs/generator": patch
---
updated nextjs version in generator & npmrc file

View File

@@ -0,0 +1,5 @@
---
"@blitzjs/codemod": patch
---
Update queryClient import in codemod

View File

@@ -0,0 +1,5 @@
---
"@blitzjs/generator": patch
---
Update codemod and template with a new queryClient import location

View File

@@ -0,0 +1,5 @@
---
"blitz": patch
---
detailed print env info

View File

@@ -0,0 +1,7 @@
---
"blitz": patch
"@blitzjs/auth": patch
"@blitzjs/next": patch
---
rename middleware type for blitz server plugin

View File

@@ -0,0 +1,5 @@
---
"@blitzjs/next": patch
---
useParam & useParams functions now accessible from @blitzjs/next

View File

@@ -0,0 +1,5 @@
---
"@blitzjs/codemod": patch
---
Fix templates source in RPC codemod step

View File

@@ -0,0 +1,5 @@
---
"@blitzjs/codemod": patch
---
Add codemod to upgrade from legacy framework to the Blitz Toolkit

View File

@@ -12,44 +12,65 @@
"@blitzjs/rpc": "2.0.0-alpha.0",
"@blitzjs/config": "0.0.0",
"@blitzjs/generator": "2.0.0-alpha.0",
"@blitzjs/codemod": "2.0.0-alpha.0",
"template": "0.0.0",
"toolkit-app": "1.0.0",
"test-qm": "0.0.0"
"test-qm": "0.0.0",
"test-no-suspense": "0.0.0",
"test-trailing-slash": "0.0.0"
},
"changesets": [
"big-phones-bow",
"breezy-cameras-double",
"bright-mangos-run",
"cool-doors-invent",
"dirty-monkeys-greet",
"eleven-humans-sort",
"empty-berries-rule",
"fair-wombats-sneeze",
"famous-kings-explain",
"fast-trainers-kneel",
"flat-bees-approve",
"four-meals-fry",
"gentle-dogs-reply",
"great-months-train",
"green-papayas-do",
"healthy-rice-shout",
"hot-drinks-approve",
"lovely-colts-share",
"lucky-cows-try",
"modern-cameras-pull",
"moody-squids-cheer",
"nervous-beds-travel",
"nice-starfishes-live",
"nine-onions-admire",
"ninety-pets-heal",
"olive-bees-buy",
"olive-feet-rhyme",
"plenty-bottles-swim",
"poor-peas-lick",
"poor-penguins-look",
"poor-shrimps-think",
"purple-singers-greet",
"quiet-feet-travel",
"rich-chairs-invent",
"sharp-falcons-begin",
"shy-olives-hang",
"silent-colts-reply",
"small-socks-confess",
"stupid-walls-sell",
"swift-drinks-dress",
"tame-keys-reply",
"tasty-news-collect",
"ten-rivers-burn",
"tender-pianos-check",
"thick-parrots-float",
"thirty-countries-build",
"twenty-beans-pump",
"two-kiwis-help",
"unlucky-papayas-sleep",
"violet-bags-leave",
"violet-lions-help",
"weak-suns-shave",
"wicked-ghosts-cough",
"wise-frogs-give"

View File

@@ -0,0 +1,7 @@
---
"@blitzjs/rpc": patch
"@blitzjs/codemod": patch
"@blitzjs/generator": patch
---
getQueryClient function & queryClient codemod updates & shared plugin config

View File

@@ -0,0 +1,6 @@
---
"@blitzjs/rpc": patch
"@blitzjs/generator": patch
---
Update RPC plugin setup in templates

View File

@@ -0,0 +1,5 @@
---
"blitz": patch
---
Add aliases for Blitz CLI commands

View File

@@ -0,0 +1,6 @@
---
"blitz": patch
"@blitzjs/codemod": patch
---
init codemod generator

View File

@@ -0,0 +1,5 @@
---
"@blitzjs/codemod": patch
---
allow extension catch in getAllFiles codemod util

View File

@@ -0,0 +1,5 @@
---
"@blitzjs/codemod": patch
---
Update templates directory for codemod

View File

@@ -0,0 +1,5 @@
---
"@blitzjs/rpc": patch
---
Add resolverBasePath to Blitz config to change the way rpc routes are generated

1
.npmrc
View File

@@ -6,3 +6,4 @@ public-hoist-pattern[]=*types*
public-hoist-pattern[]=*eslint*
public-hoist-pattern[]=@prettier/plugin-*
public-hoist-pattern[]=*prettier-plugin-*
strict-peer-dependencies=false

View File

@@ -7,12 +7,6 @@ export const { withBlitz } = setupBlitzClient({
AuthClientPlugin({
cookiePrefix: "web-cookie-prefix",
}),
BlitzRpcPlugin({
reactQueryOptions: {
queries: {
staleTime: 7000,
},
},
}),
BlitzRpcPlugin({}),
],
})

View File

@@ -3,7 +3,7 @@
"version": "1.0.1-alpha.16",
"scripts": {
"start:dev": "pnpm run prisma:start && next dev",
"buildapp": "pnpm i && pnpm prisma generate && next build",
"buildapp": "NODE_ENV=production pnpm blitz codegen && pnpm prisma generate && next build",
"start": "next start",
"lint": "next lint",
"prisma:start": "prisma generate && prisma migrate deploy",
@@ -29,7 +29,7 @@
"@blitzjs/rpc": "workspace:*",
"@hookform/resolvers": "2.8.8",
"@prisma/client": "3.9.0",
"blitz": "workspace:2.0.0-alpha.26",
"blitz": "workspace:2.0.0-alpha.40",
"next": "12.1.6-canary.17",
"prisma": "3.9.0",
"react": "18.0.0",

View File

@@ -0,0 +1,8 @@
import {resolver} from "@blitzjs/rpc"
import db from "db"
export default resolver.pipe(resolver.authorize(), async () => {
const users = await db.user.findMany()
return users
})

View File

@@ -10,6 +10,7 @@ module.exports = withBlitz(
customServer: {
hotReload: false,
},
resolverBasePath: "root",
},
}),
)

View File

@@ -25,6 +25,7 @@ export default api(async (req, res, ctx) => {
await blitzContext.session.$create({
userId: user.id,
role: "USER",
})
res.status(200).json({email: req.query.email, userId: blitzContext.session.userId})

View File

@@ -16,6 +16,7 @@ export default api(async (req, res, ctx) => {
await blitzContext.session.$create({
userId: user.id,
role: "USER",
})
res.status(200).json({userId: blitzContext.session.userId, ...user, email: req.query.email})

View File

@@ -9,7 +9,7 @@ export const getServerSideProps = gSSP(async ({ctx}) => {
return {props: {}}
})
function PageWithGssp(props) {
function PageWithPrefetchInfiniteQuery(props) {
const [usersPages] = useInfiniteQuery(getInfiniteUsers, (page = {take: 3, skip: 0}) => page, {
getNextPageParam: (lastPage) => lastPage.nextPage,
})
@@ -29,4 +29,4 @@ function PageWithGssp(props) {
)
}
export default PageWithGssp
export default PageWithPrefetchInfiniteQuery

View File

@@ -0,0 +1,31 @@
import {SessionContext} from "@blitzjs/auth"
import {invokeWithCtx} from "@blitzjs/rpc"
import {gSSP} from "app/blitz-server"
import getUsersAuth from "app/queries/getUsersAuth"
type Props = {
userId: unknown
publicData: SessionContext["$publicData"]
}
export const getServerSideProps = gSSP<Props>(async ({ctx}) => {
const {session} = ctx
const users = await invokeWithCtx(getUsersAuth, {}, ctx)
console.log({users})
return {
props: {
userId: session.userId,
publicData: session.$publicData,
publishedAt: new Date(0),
},
}
})
function PageWithInvokeCtx(props: Props) {
return <div>{JSON.stringify(props, null, 2)}</div>
}
export default PageWithInvokeCtx

View File

@@ -9,7 +9,7 @@ export const getServerSideProps = gSSP(async ({ctx}) => {
return {props: {}}
})
function PageWithGssp(props) {
function PageWithPrefetch(props) {
const [users] = useQuery(getUsers, {})
return (
<div>
@@ -25,4 +25,4 @@ function PageWithGssp(props) {
)
}
export default PageWithGssp
export default PageWithPrefetch

15
apps/web/types.ts Normal file
View File

@@ -0,0 +1,15 @@
import {SimpleRolesIsAuthorized} from "@blitzjs/auth"
import {User} from "db"
export type Role = "ADMIN" | "USER"
declare module "@blitzjs/auth" {
export interface Session {
isAuthorized: SimpleRolesIsAuthorized<Role>
PublicData: {
userId: User["id"]
role: Role
views?: number
}
}
}

View File

@@ -5,7 +5,7 @@ import {withBlitz} from "../app/blitz-client"
function RootErrorFallback({error}: ErrorFallbackProps) {
if (error instanceof AuthenticationError) {
return <div>Error: You are not authenticated</div>
return <div id="error">Error: You are not authenticated</div>
} else if (error instanceof AuthorizationError) {
return (
<ErrorComponent

View File

@@ -35,20 +35,24 @@ const runTests = (mode?: string) => {
"should render error for protected query",
async () => {
const browser = await webdriver(appPort, "/authenticated-page")
let errorMsg = await browser.elementByXpath(`//*[@id="__next"]/div`)
let errorMsg = await browser.elementById(`error`).text()
expect(errorMsg).toMatch(/Error: You are not authenticated/)
if (browser) browser.close()
},
5000 * 60 * 2,
)
it("should render result for open query", async () => {
const res = await fetch(`http://localhost:${appPort}/api/noauth`, {
method: "GET",
headers: {"Content-Type": "application/json; charset=utf-8"},
})
expect(res.status).toBe(200)
})
it(
"should render result for open query",
async () => {
const res = await fetch(`http://localhost:${appPort}/api/noauth`, {
method: "GET",
headers: {"Content-Type": "application/json; charset=utf-8"},
})
expect(res.status).toBe(200)
},
5000 * 60 * 2,
)
it("sets correct cookie", async () => {
const res = await fetch(`http://localhost:${appPort}/api/noauth`, {
@@ -118,31 +122,33 @@ const runTests = (mode?: string) => {
})
}
describe("dev mode", () => {
beforeAll(async () => {
try {
appPort = await findPort()
app = await launchApp(appDir, appPort, {})
await seed()
} catch (error) {
console.log(error)
}
}, 5000 * 60 * 2)
afterAll(async () => await killApp(app))
runTests()
})
describe("Auth Tests", () => {
describe("dev mode", () => {
beforeAll(async () => {
try {
appPort = await findPort()
app = await launchApp(appDir, appPort, {cwd: process.cwd()})
await seed()
} catch (error) {
console.log(error)
}
}, 5000 * 60 * 2)
afterAll(async () => await killApp(app))
runTests()
})
describe("server mode", () => {
beforeAll(async () => {
try {
appPort = await findPort()
await nextBuild(appDir)
app = await nextStart(appDir, appPort)
} catch (err) {
console.log(err)
}
}, 5000 * 60 * 2)
afterAll(async () => await killApp(app))
describe("server mode", () => {
beforeAll(async () => {
try {
await nextBuild(appDir)
appPort = await findPort()
app = await nextStart(appDir, appPort, {cwd: process.cwd()})
} catch (err) {
console.log(err)
}
}, 5000 * 60 * 2)
afterAll(async () => await killApp(app))
runTests()
runTests()
})
})

View File

@@ -0,0 +1,2 @@
SESSION_SECRET_KEY=hsdenhJfpLHrGjgdgg3jdF8g2bYD2PaQ
HEADLESS=true

View File

@@ -0,0 +1 @@
module.exports = require("@blitzjs/config/eslint")

View File

@@ -0,0 +1,3 @@
node_modules
# Keep environment variables out of version control
*.sqlite

View File

@@ -0,0 +1,14 @@
import {BlitzRpcPlugin} from "@blitzjs/rpc"
import {setupBlitzClient} from "@blitzjs/next"
import {AuthClientPlugin} from "@blitzjs/auth"
const {withBlitz} = setupBlitzClient({
plugins: [
AuthClientPlugin({
cookiePrefix: "no-suspense-tests-cookie-prefix",
}),
BlitzRpcPlugin({}),
],
})
export {withBlitz}

View File

@@ -0,0 +1,16 @@
import {setupBlitzServer} from "@blitzjs/next"
import {AuthServerPlugin, PrismaStorage} from "@blitzjs/auth"
import {simpleRolesIsAuthorized} from "@blitzjs/auth"
import {prisma as db} from "../prisma/index"
const {gSSP, gSP, api} = setupBlitzServer({
plugins: [
AuthServerPlugin({
cookiePrefix: "no-suspense-tests-cookie-prefix",
storage: PrismaStorage(db as any),
isAuthorized: simpleRolesIsAuthorized,
}),
],
})
export {gSSP, gSP, api}

View File

@@ -0,0 +1,3 @@
export default async function getBasic() {
return "basic-result"
}

View File

@@ -0,0 +1,5 @@
/// <reference types="next" />
/// <reference types="next/image-types/global" />
// NOTE: This file should not be edited
// see https://nextjs.org/docs/basic-features/typescript for more information.

View File

@@ -0,0 +1,2 @@
const {withBlitz} = require("@blitzjs/next")
module.exports = withBlitz({})

View File

@@ -0,0 +1,41 @@
{
"name": "test-no-suspense",
"version": "0.0.0",
"private": true,
"scripts": {
"start:dev": "pnpm run prisma:start && next dev",
"test": "pnpm run prisma:start && vitest run",
"test-watch": "vitest",
"start": "next start",
"lint": "next lint",
"clean": "rm -rf .turbo && rm -rf node_modules && rm -rf .next",
"prisma:start": "prisma generate && prisma migrate deploy",
"prisma:studio": "prisma studio"
},
"dependencies": {
"@blitzjs/auth": "workspace:*",
"@blitzjs/next": "workspace:*",
"@blitzjs/rpc": "workspace:*",
"@prisma/client": "3.9.0",
"blitz": "workspace:*",
"lowdb": "3.0.0",
"next": "12.1.6-canary.17",
"prisma": "3.9.0",
"react": "18.0.0",
"react-dom": "18.0.0"
},
"devDependencies": {
"@blitzjs/config": "workspace:*",
"@next/bundle-analyzer": "12.0.8",
"@types/express": "4.17.13",
"@types/fs-extra": "9.0.13",
"@types/node-fetch": "2.6.1",
"@types/react": "18.0.1",
"b64-lite": "1.4.0",
"eslint": "7.32.0",
"fs-extra": "10.0.1",
"get-port": "6.1.2",
"node-fetch": "3.2.3",
"typescript": "^4.5.3"
}
}

View File

@@ -0,0 +1,34 @@
import {ErrorFallbackProps, ErrorComponent, ErrorBoundary, AppProps} from "@blitzjs/next"
import {AuthenticationError, AuthorizationError} from "blitz"
import React from "react"
import {withBlitz} from "../app/blitz-client"
function RootErrorFallback({error}: ErrorFallbackProps) {
if (error instanceof AuthenticationError) {
return <div>Error: You are not authenticated</div>
} else if (error instanceof AuthorizationError) {
return (
<ErrorComponent
statusCode={error.statusCode}
title="Sorry, you are not authorized to access this"
/>
)
} else {
return (
<ErrorComponent
statusCode={(error as any)?.statusCode || 400}
title={error.message || error.name}
/>
)
}
}
function MyApp({Component, pageProps}: AppProps) {
return (
<ErrorBoundary FallbackComponent={RootErrorFallback}>
<Component {...pageProps} />
</ErrorBoundary>
)
}
export default withBlitz(MyApp)

View File

@@ -0,0 +1,4 @@
import {rpcHandler} from "@blitzjs/rpc"
import {api} from "../../../app/blitz-server"
export default api(rpcHandler({onError: console.log}))

View File

@@ -0,0 +1,23 @@
import getBasic from "../app/queries/getBasic"
import {useQuery} from "@blitzjs/rpc"
import React from "react"
function Content() {
const [result, {isFetching}] = useQuery(getBasic, undefined)
if (isFetching) {
return <>Loading...</>
} else {
return <div id="content">{result}</div>
}
}
function Page() {
return (
<div id="page">
<Content />
</div>
)
}
export default Page

View File

@@ -0,0 +1,8 @@
import {enhancePrisma} from "blitz"
import {PrismaClient} from "@prisma/client"
const EnhancedPrisma = enhancePrisma(PrismaClient)
export * from "@prisma/client"
const prisma = new EnhancedPrisma()
export {prisma}

View File

@@ -0,0 +1,47 @@
-- CreateTable
CREATE TABLE "User" (
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
"createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" DATETIME NOT NULL,
"name" TEXT,
"email" TEXT NOT NULL,
"hashedPassword" TEXT,
"role" TEXT NOT NULL DEFAULT 'user'
);
-- CreateTable
CREATE TABLE "Session" (
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
"createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" DATETIME NOT NULL,
"expiresAt" DATETIME,
"handle" TEXT NOT NULL,
"userId" INTEGER,
"hashedSessionToken" TEXT,
"antiCSRFToken" TEXT,
"publicData" TEXT,
"privateData" TEXT,
CONSTRAINT "Session_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User" ("id") ON DELETE SET NULL ON UPDATE CASCADE
);
-- CreateTable
CREATE TABLE "Token" (
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
"createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" DATETIME NOT NULL,
"hashedToken" TEXT NOT NULL,
"type" TEXT NOT NULL,
"expiresAt" DATETIME NOT NULL,
"sentTo" TEXT NOT NULL,
"userId" INTEGER NOT NULL,
CONSTRAINT "Token_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User" ("id") ON DELETE RESTRICT ON UPDATE CASCADE
);
-- CreateIndex
CREATE UNIQUE INDEX "User_email_key" ON "User"("email");
-- CreateIndex
CREATE UNIQUE INDEX "Session_handle_key" ON "Session"("handle");
-- CreateIndex
CREATE UNIQUE INDEX "Token_hashedToken_type_key" ON "Token"("hashedToken", "type");

View File

@@ -0,0 +1,3 @@
# Please do not edit this file manually
# It should be added in your version-control system (i.e. Git)
provider = "sqlite"

View File

@@ -0,0 +1,50 @@
datasource sqlite {
provider = "sqlite"
url = "file:./db.sqlite"
}
generator client {
provider = "prisma-client-js"
}
model User {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
name String?
email String @unique
hashedPassword String?
role String @default("user")
sessions Session[]
tokens Token[]
}
model Session {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
expiresAt DateTime?
handle String @unique
user User? @relation(fields: [userId], references: [id])
userId Int?
hashedSessionToken String?
antiCSRFToken String?
publicData String?
privateData String?
}
model Token {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
hashedToken String
type String
expiresAt DateTime
sentTo String
user User @relation(fields: [userId], references: [id])
userId Int
@@unique([hashedToken, type])
}

View File

@@ -0,0 +1,7 @@
import {prisma} from "./index"
const seed = async () => {
await prisma.$reset()
}
export default seed

View File

@@ -0,0 +1,57 @@
import {describe, it, expect, beforeAll, afterAll} from "vitest"
import {killApp, findPort, launchApp, nextBuild, nextStart} from "../../utils/next-test-utils"
import webdriver from "../../utils/next-webdriver"
import {join} from "path"
let app: any
let appPort: number
const appDir = join(__dirname, "../")
const runTests = (mode?: string) => {
describe("useQuery without suspense", () => {
it(
"should render query result",
async () => {
const browser = await webdriver(appPort, "/use-query")
let text
browser.waitForElementByCss("#content", 0)
text = await browser.elementByCss("#content").text()
expect(text).toMatch(/basic-result/)
if (browser) await browser.close()
},
5000 * 60 * 2,
)
})
}
describe("No Suspense Tests", () => {
describe("dev mode", () => {
beforeAll(async () => {
try {
appPort = await findPort()
app = await launchApp(appDir, appPort, {cwd: process.cwd()})
} catch (error) {
console.log(error)
}
}, 5000 * 60 * 2)
afterAll(async () => await killApp(app))
runTests()
})
describe("server mode", () => {
beforeAll(async () => {
try {
await nextBuild(appDir)
appPort = await findPort()
app = await nextStart(appDir, appPort, {cwd: process.cwd()})
} catch (err) {
console.log(err)
}
}, 5000 * 60 * 2)
afterAll(async () => await killApp(app))
runTests()
})
})

View File

@@ -0,0 +1,11 @@
{
"extends": "@blitzjs/config/tsconfig.nextjs.json",
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
"compilerOptions": {
"paths": {
"react": ["./node_modules/@types/react"]
}
},
"exclude": ["node_modules"],
"baseUrl": "."
}

View File

@@ -143,7 +143,7 @@ describe("RPC", () => {
beforeAll(async () => {
try {
appPort = await findPort()
app = await launchApp(appDir, appPort)
app = await launchApp(appDir, appPort, {cwd: process.cwd()})
} catch (err) {
console.log(err)
}
@@ -162,7 +162,7 @@ describe("RPC", () => {
await nextBuild(appDir)
mode = "server"
appPort = await findPort()
app = await nextStart(appDir, appPort)
app = await nextStart(appDir, appPort, {cwd: process.cwd()})
})
afterAll(() => killApp(app))
@@ -185,7 +185,7 @@ describe("RPC", () => {
await nextBuild(appDir)
mode = "serverless"
appPort = await findPort()
app = await nextStart(appDir, appPort)
app = await nextStart(appDir, appPort, {cwd: process.cwd()})
})
afterAll(async () => {
await killApp(app)

View File

@@ -0,0 +1,2 @@
SESSION_SECRET_KEY=hsdenhJfpLHrGjgdgg3jdF8g2bYD2PaQ
HEADLESS=true

View File

@@ -0,0 +1 @@
module.exports = require("@blitzjs/config/eslint")

View File

@@ -0,0 +1,3 @@
node_modules
# Keep environment variables out of version control
*.sqlite

View File

@@ -0,0 +1,14 @@
import {BlitzRpcPlugin} from "@blitzjs/rpc"
import {setupBlitzClient} from "@blitzjs/next"
import {AuthClientPlugin} from "@blitzjs/auth"
const {withBlitz} = setupBlitzClient({
plugins: [
AuthClientPlugin({
cookiePrefix: "trailing-slash-tests-cookie-prefix",
}),
BlitzRpcPlugin({}),
],
})
export {withBlitz}

View File

@@ -0,0 +1,16 @@
import {setupBlitzServer} from "@blitzjs/next"
import {AuthServerPlugin, PrismaStorage} from "@blitzjs/auth"
import {simpleRolesIsAuthorized} from "@blitzjs/auth"
import {prisma as db} from "../prisma/index"
const {gSSP, gSP, api} = setupBlitzServer({
plugins: [
AuthServerPlugin({
cookiePrefix: "trailing-slash-tests-cookie-prefix",
storage: PrismaStorage(db as any),
isAuthorized: simpleRolesIsAuthorized,
}),
],
})
export {gSSP, gSP, api}

View File

@@ -0,0 +1,3 @@
export default async function getBasic() {
return "basic-result"
}

View File

@@ -0,0 +1,5 @@
/// <reference types="next" />
/// <reference types="next/image-types/global" />
// NOTE: This file should not be edited
// see https://nextjs.org/docs/basic-features/typescript for more information.

View File

@@ -0,0 +1,4 @@
const {withBlitz} = require("@blitzjs/next")
module.exports = withBlitz({
trailingSlash: true,
})

View File

@@ -0,0 +1,41 @@
{
"name": "test-trailing-slash",
"version": "0.0.0",
"private": true,
"scripts": {
"start:dev": "pnpm run prisma:start && next dev",
"test": "pnpm run prisma:start && vitest run",
"test-watch": "vitest",
"start": "next start",
"lint": "next lint",
"clean": "rm -rf .turbo && rm -rf node_modules && rm -rf .next",
"prisma:start": "prisma generate && prisma migrate deploy",
"prisma:studio": "prisma studio"
},
"dependencies": {
"@blitzjs/auth": "workspace:*",
"@blitzjs/next": "workspace:*",
"@blitzjs/rpc": "workspace:*",
"@prisma/client": "3.9.0",
"blitz": "workspace:*",
"lowdb": "3.0.0",
"next": "12.1.6-canary.17",
"prisma": "3.9.0",
"react": "18.0.0",
"react-dom": "18.0.0"
},
"devDependencies": {
"@blitzjs/config": "workspace:*",
"@next/bundle-analyzer": "12.0.8",
"@types/express": "4.17.13",
"@types/fs-extra": "9.0.13",
"@types/node-fetch": "2.6.1",
"@types/react": "18.0.1",
"b64-lite": "1.4.0",
"eslint": "7.32.0",
"fs-extra": "10.0.1",
"get-port": "6.1.2",
"node-fetch": "3.2.3",
"typescript": "^4.5.3"
}
}

View File

@@ -0,0 +1,34 @@
import {ErrorFallbackProps, ErrorComponent, ErrorBoundary, AppProps} from "@blitzjs/next"
import {AuthenticationError, AuthorizationError} from "blitz"
import React from "react"
import {withBlitz} from "../app/blitz-client"
function RootErrorFallback({error}: ErrorFallbackProps) {
if (error instanceof AuthenticationError) {
return <div>Error: You are not authenticated</div>
} else if (error instanceof AuthorizationError) {
return (
<ErrorComponent
statusCode={error.statusCode}
title="Sorry, you are not authorized to access this"
/>
)
} else {
return (
<ErrorComponent
statusCode={(error as any)?.statusCode || 400}
title={error.message || error.name}
/>
)
}
}
function MyApp({Component, pageProps}: AppProps) {
return (
<ErrorBoundary FallbackComponent={RootErrorFallback}>
<Component {...pageProps} />
</ErrorBoundary>
)
}
export default withBlitz(MyApp)

View File

@@ -0,0 +1,4 @@
import {rpcHandler} from "@blitzjs/rpc"
import {api} from "../../../app/blitz-server"
export default api(rpcHandler({onError: console.log}))

View File

@@ -0,0 +1,20 @@
import getBasic from "../app/queries/getBasic"
import {useQuery} from "@blitzjs/rpc"
import {Suspense} from "react"
function Content() {
const [result] = useQuery(getBasic, undefined)
return <div id="content">{result}</div>
}
function Page() {
return (
<div id="page">
<Suspense fallback={"Loading..."}>
<Content />
</Suspense>
</div>
)
}
export default Page

View File

@@ -0,0 +1,8 @@
import {enhancePrisma} from "blitz"
import {PrismaClient} from "@prisma/client"
const EnhancedPrisma = enhancePrisma(PrismaClient)
export * from "@prisma/client"
const prisma = new EnhancedPrisma()
export {prisma}

View File

@@ -0,0 +1,47 @@
-- CreateTable
CREATE TABLE "User" (
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
"createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" DATETIME NOT NULL,
"name" TEXT,
"email" TEXT NOT NULL,
"hashedPassword" TEXT,
"role" TEXT NOT NULL DEFAULT 'user'
);
-- CreateTable
CREATE TABLE "Session" (
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
"createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" DATETIME NOT NULL,
"expiresAt" DATETIME,
"handle" TEXT NOT NULL,
"userId" INTEGER,
"hashedSessionToken" TEXT,
"antiCSRFToken" TEXT,
"publicData" TEXT,
"privateData" TEXT,
CONSTRAINT "Session_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User" ("id") ON DELETE SET NULL ON UPDATE CASCADE
);
-- CreateTable
CREATE TABLE "Token" (
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
"createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" DATETIME NOT NULL,
"hashedToken" TEXT NOT NULL,
"type" TEXT NOT NULL,
"expiresAt" DATETIME NOT NULL,
"sentTo" TEXT NOT NULL,
"userId" INTEGER NOT NULL,
CONSTRAINT "Token_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User" ("id") ON DELETE RESTRICT ON UPDATE CASCADE
);
-- CreateIndex
CREATE UNIQUE INDEX "User_email_key" ON "User"("email");
-- CreateIndex
CREATE UNIQUE INDEX "Session_handle_key" ON "Session"("handle");
-- CreateIndex
CREATE UNIQUE INDEX "Token_hashedToken_type_key" ON "Token"("hashedToken", "type");

View File

@@ -0,0 +1,3 @@
# Please do not edit this file manually
# It should be added in your version-control system (i.e. Git)
provider = "sqlite"

View File

@@ -0,0 +1,50 @@
datasource sqlite {
provider = "sqlite"
url = "file:./db.sqlite"
}
generator client {
provider = "prisma-client-js"
}
model User {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
name String?
email String @unique
hashedPassword String?
role String @default("user")
sessions Session[]
tokens Token[]
}
model Session {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
expiresAt DateTime?
handle String @unique
user User? @relation(fields: [userId], references: [id])
userId Int?
hashedSessionToken String?
antiCSRFToken String?
publicData String?
privateData String?
}
model Token {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
hashedToken String
type String
expiresAt DateTime
sentTo String
user User @relation(fields: [userId], references: [id])
userId Int
@@unique([hashedToken, type])
}

View File

@@ -0,0 +1,7 @@
import {prisma} from "./index"
const seed = async () => {
await prisma.$reset()
}
export default seed

View File

@@ -0,0 +1,56 @@
import {describe, it, expect, beforeAll, afterAll} from "vitest"
import {killApp, findPort, launchApp, nextBuild, nextStart} from "../../utils/next-test-utils"
import webdriver from "../../utils/next-webdriver"
import {join} from "path"
let app: any
let appPort: number
const appDir = join(__dirname, "../")
const runTests = (mode?: string) => {
describe("Trailing Slash", () => {
it(
"should render query result",
async () => {
const browser = await webdriver(appPort, "/use-query")
let text = await browser.elementByCss("#page").text()
expect(text).toMatch(/Loading/)
await browser.waitForElementByCss("#content", 0)
text = await browser.elementByCss("#content").text()
expect(text).toMatch(/basic-result/)
if (browser) await browser.close()
},
5000 * 60 * 2,
)
})
}
describe("Trailing Slash Tests", () => {
describe("dev mode", () => {
beforeAll(async () => {
try {
appPort = await findPort()
app = await launchApp(appDir, appPort, {cwd: process.cwd()})
} catch (error) {
console.log(error)
}
}, 5000 * 60 * 2)
afterAll(async () => await killApp(app))
runTests()
})
describe("server mode", () => {
beforeAll(async () => {
try {
await nextBuild(appDir)
appPort = await findPort()
app = await nextStart(appDir, appPort, {cwd: process.cwd()})
} catch (err) {
console.log(err)
}
}, 5000 * 60 * 2)
afterAll(async () => await killApp(app))
runTests()
})
})

View File

@@ -0,0 +1,11 @@
{
"extends": "@blitzjs/config/tsconfig.nextjs.json",
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
"compilerOptions": {
"paths": {
"react": ["./node_modules/@types/react"]
}
},
"exclude": ["node_modules"],
"baseUrl": "."
}

View File

@@ -80,21 +80,7 @@ const BlitzWrapper = ({plugins, children}) => {
export function render(ui: RenderUI, {wrapper, router, ...options}: RenderOptions = {}) {
if (!wrapper) {
wrapper = ({children}) => {
return (
<BlitzWrapper
plugins={[
BlitzRpcPlugin({
reactQueryOptions: {
queries: {
staleTime: 7000,
},
},
}),
]}
>
{children}
</BlitzWrapper>
)
return <BlitzWrapper plugins={[BlitzRpcPlugin({})]}>{children}</BlitzWrapper>
}
}

View File

@@ -0,0 +1,100 @@
export type Event = "request"
// This is the base Browser interface all browser
// classes should build off of, it is the bare
// methods we aim to support across tests
export class BrowserInterface {
private promise: any
private then: any
private catch: any
protected chain(nextCall: any): BrowserInterface {
if (!this.promise) {
this.promise = Promise.resolve(this)
}
this.promise = this.promise.then(nextCall)
this.then = (...args) => this.promise.then(...args)
this.catch = (...args) => this.promise.catch(...args)
return this
}
async setup(browserName: string, locale?: string): Promise<void> {}
async close(): Promise<void> {}
async quit(): Promise<void> {}
elementsByCss(selector: string): BrowserInterface[] {
return [this]
}
elementByCss(selector: string): BrowserInterface {
return this
}
elementById(selector: string): BrowserInterface {
return this
}
click(opts?: {modifierKey?: boolean}): BrowserInterface {
return this
}
keydown(key: string): BrowserInterface {
return this
}
keyup(key: string): BrowserInterface {
return this
}
focusPage(): BrowserInterface {
return this
}
type(text: string): BrowserInterface {
return this
}
waitForElementByCss(selector: string, timeout?: number): BrowserInterface {
return this
}
waitForCondition(snippet: string, timeout?: number): BrowserInterface {
return this
}
back(): BrowserInterface {
return this
}
forward(): BrowserInterface {
return this
}
refresh(): BrowserInterface {
return this
}
setDimensions(opts: {height: number; width: number}): BrowserInterface {
return this
}
addCookie(opts: {name: string; value: string}): BrowserInterface {
return this
}
deleteCookies(): BrowserInterface {
return this
}
on(event: Event, cb: (...args: any[]) => void) {}
off(event: Event, cb: (...args: any[]) => void) {}
async loadPage(url: string, {disableCache: boolean, beforePageLoad: Function}): Promise<void> {}
async get(url: string): Promise<void> {}
async getValue(): Promise<any> {}
async getAttribute(name: string): Promise<any> {}
async eval(snippet: string | Function): Promise<any> {}
async evalAsync(snippet: string | Function): Promise<any> {}
async text(): Promise<string> {
return ""
}
async getComputedCss(prop: string): Promise<string> {
return ""
}
async hasElementByCssSelector(selector: string): Promise<boolean> {
return false
}
async log(): Promise<any[]> {
return []
}
async websocketFrames(): Promise<any[]> {
return []
}
async url(): Promise<string> {
return ""
}
}

View File

@@ -0,0 +1,312 @@
import {BrowserInterface, Event} from "./base"
import {
chromium,
webkit,
firefox,
Browser,
BrowserContext,
Page,
ElementHandle,
} from "playwright-chromium"
let page: Page
let browser: Browser | undefined
let context: BrowserContext | undefined
let pageLogs: Array<{source: string; message: string}> = []
let websocketFrames: Array<{payload: string | Buffer}> = []
export async function quit() {
await context?.close()
await browser?.close()
context = undefined
browser = undefined
}
class Playwright extends BrowserInterface {
private eventCallbacks: Record<Event, Set<(...args: any[]) => void>> = {
request: new Set(),
}
on(event: Event, cb: (...args: any[]) => void) {
if (!this.eventCallbacks[event]) {
throw new Error(
`Invalid event passed to browser.on, received ${event}. Valid events are ${Object.keys(
event,
)}`,
)
}
this.eventCallbacks[event]?.add(cb)
}
off(event: Event, cb: (...args: any[]) => void) {
this.eventCallbacks[event]?.delete(cb)
}
async setup(browserName: string, locale?: string) {
if (browser) return
const headless = !!process.env.HEADLESS || true
if (browserName === "safari") {
browser = await webkit.launch({headless})
} else if (browserName === "firefox") {
browser = await firefox.launch({headless})
} else {
browser = await chromium.launch({headless, devtools: !headless})
}
context = await browser.newContext({locale})
}
async get(url: string): Promise<void> {
return page.goto(url) as any
}
async loadPage(
url: string,
opts?: {disableCache: boolean; beforePageLoad?: (...args: any[]) => void},
) {
// clean-up existing pages
for (const oldPage of context!.pages()) {
await oldPage.close()
}
page = await context!.newPage()
pageLogs = []
websocketFrames = []
page.on("console", (msg) => {
console.log("browser log:", msg)
pageLogs.push({source: msg.type(), message: msg.text()})
})
page.on("crash", (page) => {
console.error("page crashed")
})
page.on("pageerror", (error) => {
console.error("page error", error)
})
page.on("request", (req) => {
this.eventCallbacks.request.forEach((cb) => cb(req))
})
if (opts?.disableCache) {
// TODO: this doesn't seem to work (dev tools does not check the box as expected)
const session = await context!.newCDPSession(page)
session.send("Network.setCacheDisabled", {cacheDisabled: true})
}
page.on("websocket", (ws) => {
ws.on("framereceived", (frame) => {
websocketFrames.push({payload: frame.payload})
})
})
opts?.beforePageLoad?.(page)
await page.goto(url, {waitUntil: "load"})
}
back(): BrowserInterface {
return this.chain(() => {
return page.goBack()
})
}
forward(): BrowserInterface {
return this.chain(() => {
return page.goForward()
})
}
refresh(): BrowserInterface {
return this.chain(() => {
return page.reload()
})
}
setDimensions({width, height}: {height: number; width: number}): BrowserInterface {
return this.chain(() => page.setViewportSize({width, height}))
}
addCookie(opts: {name: string; value: string}): BrowserInterface {
return this.chain(async () =>
context!.addCookies([
{
path: "/",
domain: await page.evaluate("window.location.hostname"),
...opts,
},
]),
)
}
deleteCookies(): BrowserInterface {
return this.chain(async () => context!.clearCookies())
}
focusPage() {
return this.chain(() => page.bringToFront())
}
private wrapElement(el: ElementHandle, selector: string) {
;(el as any).selector = selector
;(el as any).text = () => el.innerText()
;(el as any).getComputedCss = (prop) =>
page.evaluate(
function (args) {
return (
getComputedStyle(document.querySelector(args.selector) as Element)[args.prop] || null
)
},
{selector, prop},
)
;(el as any).getCssValue = (el as any).getComputedCss
;(el as any).getValue = () =>
page.evaluate(
function (args) {
return (document.querySelector(args.selector) as any).value
},
{selector},
)
return el
}
elementByCss(selector: string) {
return this.waitForElementByCss(selector)
}
elementById(sel) {
return this.elementByCss(`#${sel}`)
}
getValue() {
return this.chain((el) =>
page.evaluate(
function (args) {
return document.querySelector(args.selector).value
},
{selector: el.selector},
),
) as any
}
text() {
return this.chain((el) => el.text()) as any
}
type(text) {
return this.chain((el) => el.type(text))
}
moveTo() {
return this.chain((el) => {
return page.hover(el.selector).then(() => el)
})
}
async getComputedCss(prop: string) {
return this.chain((el) => {
return el.getCssValue(prop)
}) as any
}
async getAttribute(attr) {
return this.chain((el) => el.getAttribute(attr))
}
async hasElementByCssSelector(selector: string) {
return this.eval(`!!document.querySelector('${selector}')`) as any
}
keydown(key: string): BrowserInterface {
return this.chain((el) => {
return page.keyboard.down(key).then(() => el)
})
}
keyup(key: string): BrowserInterface {
return this.chain((el) => {
return page.keyboard.up(key).then(() => el)
})
}
click() {
return this.chain((el) => {
return el.click().then(() => el)
})
}
elementsByCss(sel) {
return this.chain(() =>
page.$$(sel).then((els) => {
return els.map((el) => {
const origGetAttribute = el.getAttribute.bind(el)
el.getAttribute = (name) => {
// ensure getAttribute defaults to empty string to
// match selenium
return origGetAttribute(name).then((val) => val || "")
}
return el
})
}),
) as any as BrowserInterface[]
}
waitForElementByCss(selector, timeout?: number) {
return this.chain(() => {
return page.waitForSelector(selector, {timeout, state: "attached"}).then(async (el) => {
// it seems selenium waits longer and tests rely on this behavior
// so we wait for the load event fire before returning
await page.waitForLoadState()
return this.wrapElement(el, selector)
})
})
}
waitForCondition(condition, timeout) {
return this.chain(() => {
return page.waitForFunction(condition, {timeout})
})
}
async eval(snippet) {
// TODO: should this and evalAsync be chained? Might lead
// to bad chains
return page
.evaluate(snippet)
.catch((err) => {
console.error("eval error:", err)
return null
})
.then(async (val) => {
await page.waitForLoadState()
return val
})
}
async evalAsync(snippet) {
if (typeof snippet === "function") {
snippet = snippet.toString()
}
if (snippet.includes(`var callback = arguments[arguments.length - 1]`)) {
snippet = `(function() {
return new Promise((resolve, reject) => {
const origFunc = ${snippet}
try {
origFunc(resolve)
} catch (err) {
reject(err)
}
})
})()`
}
return page.evaluate(snippet).catch(() => null)
}
async log() {
return this.chain(() => pageLogs) as any
}
async websocketFrames() {
return this.chain(() => websocketFrames) as any
}
async url() {
return this.chain(() => page.evaluate("window.location.href")) as any
}
}
export default Playwright

View File

@@ -0,0 +1,331 @@
import path from "path"
import resolveFrom from "resolve-from"
import {execSync} from "child_process"
import {Options as ChromeOptions} from "selenium-webdriver/chrome"
import {Options as SafariOptions} from "selenium-webdriver/safari"
import {Options as FireFoxOptions} from "selenium-webdriver/firefox"
import {Builder, By, ThenableWebDriver, until} from "selenium-webdriver"
import {BrowserInterface} from "./base"
const {
BROWSERSTACK,
BROWSERSTACK_USERNAME,
BROWSERSTACK_ACCESS_KEY,
HEADLESS,
CHROME_BIN,
LEGACY_SAFARI,
SKIP_LOCAL_SELENIUM_SERVER,
} = process.env
if (process.env.ChromeWebDriver) {
process.env.PATH = `${process.env.ChromeWebDriver}${path.delimiter}${process.env.PATH}`
}
let seleniumServer: any
let browserStackLocal: any
let browser: ThenableWebDriver
export async function quit() {
await Promise.all([
browser?.quit(),
new Promise<void>((resolve) => {
browserStackLocal ? browserStackLocal.killAllProcesses(() => resolve()) : resolve()
}),
])
seleniumServer?.kill()
browser = undefined
browserStackLocal = undefined
seleniumServer = undefined
}
class Selenium extends BrowserInterface {
private browserName: string
// TODO: support setting locale
async setup(browserName: string, locale?: string) {
if (browser) return
this.browserName = browserName
let capabilities = {}
const isSafari = browserName === "safari"
const isFirefox = browserName === "firefox"
const isIE = browserName === "internet explorer"
const isBrowserStack = BROWSERSTACK
const localSeleniumServer = SKIP_LOCAL_SELENIUM_SERVER !== "true"
// install conditional packages globally so the entire
// monorepo doesn't need to rebuild when testing
let globalNodeModules: string
if (isBrowserStack || localSeleniumServer) {
globalNodeModules = execSync("npm root -g").toString().trim()
}
if (isBrowserStack) {
const {Local} = require(resolveFrom(globalNodeModules, "browserstack-local"))
browserStackLocal = new Local()
const localBrowserStackOpts = {
key: process.env.BROWSERSTACK_ACCESS_KEY,
// Add a unique local identifier to run parallel tests
// on BrowserStack
localIdentifier: new Date().getTime(),
}
await new Promise<void>((resolve, reject) => {
browserStackLocal.start(localBrowserStackOpts, (err) => {
if (err) return reject(err)
console.log("Started BrowserStackLocal", browserStackLocal.isRunning())
resolve()
})
})
const safariOpts = {
os: "OS X",
os_version: "Mojave",
browser: "Safari",
}
const safariLegacyOpts = {
os: "OS X",
os_version: "Sierra",
browserName: "Safari",
browser_version: "10.1",
}
const ieOpts = {
os: "Windows",
os_version: "10",
browser: "IE",
}
const firefoxOpts = {
os: "Windows",
os_version: "10",
browser: "Firefox",
}
const sharedOpts = {
"browserstack.local": true,
"browserstack.video": false,
"browserstack.user": BROWSERSTACK_USERNAME,
"browserstack.key": BROWSERSTACK_ACCESS_KEY,
"browserstack.localIdentifier": localBrowserStackOpts.localIdentifier,
}
capabilities = {
...capabilities,
...sharedOpts,
...(isIE ? ieOpts : {}),
...(isSafari ? (LEGACY_SAFARI ? safariLegacyOpts : safariOpts) : {}),
...(isFirefox ? firefoxOpts : {}),
}
} else if (localSeleniumServer) {
console.log("Installing selenium server")
const seleniumServerMod = require(resolveFrom(globalNodeModules, "selenium-standalone"))
await new Promise<void>((resolve, reject) => {
seleniumServerMod.install((err) => {
if (err) return reject(err)
resolve()
})
})
console.log("Starting selenium server")
await new Promise<void>((resolve, reject) => {
seleniumServerMod.start((err, child) => {
if (err) return reject(err)
seleniumServer = child
resolve()
})
})
console.log("Started selenium server")
}
let chromeOptions = new ChromeOptions()
let firefoxOptions = new FireFoxOptions()
let safariOptions = new SafariOptions()
if (HEADLESS) {
const screenSize = {width: 1280, height: 720}
chromeOptions = chromeOptions.headless().windowSize(screenSize) as any
firefoxOptions = firefoxOptions.headless().windowSize(screenSize)
}
if (CHROME_BIN) {
chromeOptions = chromeOptions.setChromeBinaryPath(path.resolve(CHROME_BIN))
}
let seleniumServerUrl
if (isBrowserStack) {
seleniumServerUrl = "http://hub-cloud.browserstack.com/wd/hub"
} else if (localSeleniumServer) {
seleniumServerUrl = `http://localhost:4444/wd/hub`
}
browser = new Builder()
.usingServer(seleniumServerUrl)
.withCapabilities(capabilities)
.forBrowser(browserName)
.setChromeOptions(chromeOptions)
.setFirefoxOptions(firefoxOptions)
.setSafariOptions(safariOptions)
.build()
}
async get(url: string): Promise<void> {
return browser.get(url)
}
async loadPage(url: string) {
// in chrome we use a new tab for testing
if (this.browserName === "chrome") {
const initialHandle = await browser.getWindowHandle()
await browser.switchTo().newWindow("tab")
const newHandle = await browser.getWindowHandle()
await browser.switchTo().window(initialHandle)
await browser.close()
await browser.switchTo().window(newHandle)
// clean-up extra windows created from links and such
for (const handle of await browser.getAllWindowHandles()) {
if (handle !== newHandle) {
await browser.switchTo().window(handle)
await browser.close()
}
}
await browser.switchTo().window(newHandle)
} else {
await browser.get("about:blank")
}
return browser.get(url)
}
back(): BrowserInterface {
return this.chain(() => {
return browser.navigate().back()
})
}
forward(): BrowserInterface {
return this.chain(() => {
return browser.navigate().forward()
})
}
refresh(): BrowserInterface {
return this.chain(() => {
return browser.navigate().refresh()
})
}
setDimensions({width, height}: {height: number; width: number}): BrowserInterface {
return this.chain(() => browser.manage().window().setRect({width, height, x: 0, y: 0}))
}
addCookie(opts: {name: string; value: string}): BrowserInterface {
return this.chain(() => browser.manage().addCookie(opts))
}
deleteCookies(): BrowserInterface {
return this.chain(() => browser.manage().deleteAllCookies())
}
elementByCss(selector: string) {
return this.chain(() => {
return browser.findElement(By.css(selector)).then((el: any) => {
el.selector = selector
el.text = () => el.getText()
el.getComputedCss = (prop) => el.getCssValue(prop)
el.type = (text) => el.sendKeys(text)
el.getValue = () =>
browser.executeScript(`return document.querySelector('${selector}').value`)
return el
})
})
}
elementById(sel) {
return this.elementByCss(`#${sel}`)
}
getValue() {
return this.chain((el) =>
browser.executeScript(`return document.querySelector('${el.selector}').value`),
) as any
}
text() {
return this.chain((el) => el.getText()) as any
}
type(text) {
return this.chain((el) => el.sendKeys(text))
}
moveTo() {
return this.chain((el) => {
return browser
.actions()
.move({origin: el})
.perform()
.then(() => el)
})
}
async getComputedCss(prop: string) {
return this.chain((el) => {
return el.getCssValue(prop)
}) as any
}
async getAttribute(attr) {
return this.chain((el) => el.getAttribute(attr))
}
async hasElementByCssSelector(selector: string) {
return this.eval(`!!document.querySelector('${selector}')`) as any
}
click() {
return this.chain((el) => {
return el.click().then(() => el)
})
}
elementsByCss(sel) {
return this.chain(() => browser.findElements(By.css(sel))) as any as BrowserInterface[]
}
waitForElementByCss(sel, timeout) {
return this.chain(() => browser.wait(until.elementLocated(By.css(sel)), timeout))
}
waitForCondition(condition, timeout) {
return this.chain(() =>
browser.wait(async (driver) => {
return driver.executeScript("return " + condition).catch(() => false)
}, timeout),
)
}
async eval(snippet) {
if (typeof snippet === "string" && !snippet.startsWith("return")) {
snippet = `return ${snippet}`
}
return browser.executeScript(snippet)
}
async evalAsync(snippet) {
if (typeof snippet === "string" && !snippet.startsWith("return")) {
snippet = `return ${snippet}`
}
return browser.executeAsyncScript(snippet)
}
async log() {
return this.chain(() => browser.manage().logs().get("browser")) as any
}
async url() {
return this.chain(() => browser.getCurrentUrl()) as any
}
}
export default Selenium

View File

@@ -1,30 +1,33 @@
import spawn from "cross-spawn"
import {ChildProcess} from "child_process"
import express from "express"
import {existsSync, readFileSync, unlinkSync, writeFileSync, createReadStream} from "fs"
import {existsSync, readFileSync, createReadStream} from "fs"
import {writeFile} from "fs-extra"
import getPort from "get-port"
import http from "http"
// `next` here is the symlink in `test/node_modules/next` which points to the root directory.
// This is done so that requiring from `next` works.
// The reason we don't import the relative path `../../dist/<etc>` is that it would lead to inconsistent module singletons
import https from "https"
import server from "next/dist/server/next"
import _pkg from "next/package.json"
import fetch from "node-fetch"
import path from "path"
import qs from "querystring"
import treeKill from "tree-kill"
import {readJSONSync} from "fs-extra"
// import {packageDirectorySync} from "pkg-dir"
import pkgDir from "pkg-dir"
import resolveCwd from "resolve-cwd"
export const nextServer = server
// polyfill Object.fromEntries for the test/integration/relay-analytics tests
// on node 10, this can be removed after we no longer support node 10
if (!Object.fromEntries) {
Object.fromEntries = require("core-js/features/object/from-entries")
}
export const pkg = _pkg
export function initNextServerScript(scriptPath, successRegexp, env, failRegexp, opts) {
return new Promise((resolve, reject) => {
const instance = spawn("node", ["--no-deprecation", scriptPath], {env})
const instance = spawn(
"node",
[...((opts && opts.nodeArgs) || []), "--no-deprecation", scriptPath],
{
env,
cwd: opts && opts.cwd,
},
)
function handleStdout(data) {
const message = data.toString()
@@ -65,6 +68,27 @@ export function initNextServerScript(scriptPath, successRegexp, env, failRegexp,
})
}
export function getFullUrl(appPortOrUrl: string | number, url: string, hostname?: string) {
let fullUrl =
typeof appPortOrUrl === "string" && appPortOrUrl.startsWith("http")
? appPortOrUrl
: `http://${hostname ? hostname : "localhost"}:${appPortOrUrl}${url}`
if (typeof appPortOrUrl === "string" && url) {
const parsedUrl = new URL(fullUrl)
const parsedPathQuery = new URL(url, fullUrl)
parsedUrl.search = parsedPathQuery.search
parsedUrl.pathname = parsedPathQuery.pathname
if (hostname && parsedUrl.hostname === "localhost") {
parsedUrl.hostname = hostname
}
fullUrl = parsedUrl.toString()
}
return fullUrl
}
export function renderViaAPI(app, pathname, query) {
const url = `${pathname}${query ? `?${qs.stringify(query)}` : ""}`
return app.renderToHTML({url}, {}, pathname, query)
@@ -75,40 +99,63 @@ export function renderViaHTTP(appPort, pathname, query, opts) {
}
export function fetchViaHTTP(appPort, pathname, query, opts) {
const url = `http://localhost:${appPort}${pathname}${
const url = `${pathname}${
typeof query === "string" ? query : query ? `?${qs.stringify(query)}` : ""
}`
return fetch(url, opts)
return fetch(getFullUrl(appPort, url), {
// in node.js v17 fetch favors IPv6 but Next.js is
// listening on IPv4 by default so force IPv4 DNS resolving
agent: (parsedUrl) => {
if (parsedUrl.protocol === "https:") {
return new https.Agent({family: 4})
}
if (parsedUrl.protocol === "http:") {
return new http.Agent({family: 4})
}
},
...opts,
})
}
export function findPort() {
return getPort()
}
interface RunNextCommandOptions {
cwd?: string
env?: Record<any, any>
spawnOptions?: any
instance?: any
stderr?: boolean
stdout?: boolean
ignoreFail?: boolean
export function resolveBin(pkg: string, executable = pkg) {
const packageDir = pkgDir.sync()!
if (!packageDir) throw new Error(`Could not find package.json for '${pkg}'`)
const bin = readJSONSync(path.join(packageDir, "package.json"))
const binPath = typeof bin === "object" ? bin.dependencies[executable] : bin
if (!binPath) throw new Error(`No bin '${executable}' in module '${pkg}'`)
const fullPath = path.join(packageDir, `node_modules`, `${pkg}`)
return fullPath
}
export function getCommandBin(
command: string,
rootFolder: string = process.cwd(),
_usePatched: boolean = false,
) {
const bin = resolveBin(command)
return path.resolve(rootFolder, bin)
}
export function runNextCommand(argv: any[], options: RunNextCommandOptions = {}) {
const cwd = options.cwd
export function runNextCommand(argv, options: RunNextCommandOptions = {}) {
const nextnextbin = getCommandBin("next", options.cwd)
const nextBin = path.join(nextnextbin, "dist/bin/next")
const cwd = options.cwd || process.cwd()
// Let Next.js decide the environment
const env = {
...process.env,
...options.env,
NODE_ENV: "",
NODE_ENV: "production" as const,
__NEXT_TEST_MODE: "true",
...options.env,
}
return new Promise<any>((resolve, reject) => {
return new Promise((resolve, reject) => {
console.log(`Running command "next ${argv.join(" ")}"`)
const instance = spawn("pnpm", ["exec", "next", ...argv], {
...options.spawnOptions,
const instance = spawn("node", [nextBin, ...argv], {
cwd,
env,
stdio: ["ignore", "pipe", "pipe"],
@@ -118,22 +165,43 @@ export function runNextCommand(argv: any[], options: RunNextCommandOptions = {})
options.instance(instance)
}
let mergedStdio = ""
let stderrOutput = ""
instance.stderr?.on("data", function (chunk) {
stderrOutput += chunk
})
if (options.stderr) {
instance.stderr?.on("data", function (chunk) {
mergedStdio += chunk
stderrOutput += chunk
if (options.stderr === "log") {
console.log(chunk.toString())
}
})
} else {
instance.stderr?.on("data", function (chunk) {
mergedStdio += chunk
})
}
let stdoutOutput = ""
if (options.stdout) {
instance.stdout?.on("data", function (chunk) {
mergedStdio += chunk
stdoutOutput += chunk
if (options.stdout === "log") {
console.log(chunk.toString())
}
})
} else {
instance.stdout?.on("data", function (chunk) {
mergedStdio += chunk
})
}
instance.on("close", (code, signal) => {
if (!options.stderr && !options.stdout && !options.ignoreFail && code !== 0) {
console.log(stderrOutput)
return reject(new Error(`command failed with code ${code}`))
return reject(new Error(`command failed with code ${code}\n${mergedStdio}`))
}
resolve({
@@ -145,7 +213,6 @@ export function runNextCommand(argv: any[], options: RunNextCommandOptions = {})
})
instance.on("error", (err: any) => {
console.log(stderrOutput)
err.stdout = stdoutOutput
err.stderr = stderrOutput
reject(err)
@@ -153,41 +220,41 @@ export function runNextCommand(argv: any[], options: RunNextCommandOptions = {})
})
}
interface RunNextCommandDevOptions {
cwd?: string
env?: Record<any, any>
onStdout?: (stdout: string) => void
onStderr?: (stderr: string) => void
stderr?: boolean
stdout?: boolean
nodeArgs?: []
bootupMarker?: any
nextStart?: boolean
}
export function runNextCommandDev(argv, stdOut, opts: RunNextCommandDevOptions = {}) {
const nextnextbin = getCommandBin("next", opts.cwd)
const nextDir = path.resolve(require.resolve("next/package"))
const nextBin = path.join(nextnextbin, "dist/bin/next")
export function runNextCommandDev(argv, opts: RunNextCommandDevOptions = {}) {
const cwd = opts.cwd // || nextDir
const cwd = opts.cwd || nextDir
const env = {
...process.env,
NODE_ENV: opts.nextStart ? ("production" as const) : ("development" as const),
NODE_ENV: undefined,
__NEXT_TEST_MODE: "true",
...opts.env,
}
return new Promise<void | string | ChildProcess>((resolve, reject) => {
const instance = spawn("pnpm", ["exec", "next", ...argv], {
const nodeArgs = opts.nodeArgs || []
return new Promise<void>((resolve, reject) => {
const instance = spawn("node", [...nodeArgs, "--no-deprecation", nextBin, ...argv], {
cwd,
env,
})
} as {})
let didResolve = false
function handleStdout(data) {
const message = data.toString()
if (!didResolve) {
didResolve = true
resolve(instance)
const bootupMarkers = {
dev: /compiled .*successfully/i,
start: /started server/i,
}
if (
(opts.bootupMarker && opts.bootupMarker.test(message)) ||
bootupMarkers[opts.nextStart || stdOut ? "start" : "dev"].test(message)
) {
if (!didResolve) {
didResolve = true
resolve(stdOut ? message : instance)
}
}
if (typeof opts.onStdout === "function") {
@@ -229,43 +296,42 @@ export function runNextCommandDev(argv, opts: RunNextCommandDevOptions = {}) {
}
// Launch the app in dev mode.
export function launchApp(dir, port, opts) {
return runNextCommandDev(["-p", port], {cwd: dir, ...opts})
export function launchApp(dir, port, opts: RunNextCommandDevOptions) {
return runNextCommandDev([dir, "-p", port], undefined, opts)
}
export function nextBuild(dir, args = [], opts = {}) {
return runNextCommand(["build", ...args], {cwd: dir, ...opts})
export function nextBuild(dir, args = [], opts = {}): any {
return runNextCommand(["build", dir, ...args], opts)
}
export function nextExport(dir, {outdir}, opts = {}) {
return runNextCommand(["export", dir, "--outdir", outdir], {cwd: dir, ...opts})
return runNextCommand(["export", dir, "--outdir", outdir], opts)
}
export function nextExportDefault(dir, opts = {}) {
return runNextCommand(["export", dir], {cwd: dir, ...opts})
return runNextCommand(["export", dir], opts)
}
export function nextLint(dir, args = [], opts = {}) {
return runNextCommand(["lint", dir, ...args], {cwd: dir, ...opts})
return runNextCommand(["lint", dir, ...args], opts)
}
export function nextStart(dir, port, opts = {}) {
return runNextCommandDev(["start", "-p", port], {
cwd: dir,
return runNextCommandDev(["start", "-p", port, dir], undefined, {
...opts,
nextStart: true,
})
}
export function buildTS(args = [], cwd, env = {NODE_ENV: "production" as const}) {
cwd = cwd
env = {...process.env, ...env}
export function buildTS(args = [], cwd, env = {}) {
cwd = cwd || path.dirname(require.resolve("next/package"))
env = {...process.env, NODE_ENV: undefined, ...env}
return new Promise<void>((resolve, reject) => {
const instance = spawn(
"node",
["--no-deprecation", require.resolve("typescript/lib/tsc"), ...args],
{cwd, env},
{cwd, env} as {},
)
let output = ""
@@ -285,10 +351,9 @@ export function buildTS(args = [], cwd, env = {NODE_ENV: "production" as const})
})
}
// Kill a launched app
export async function killApp(instance) {
export async function killProcess(pid) {
await new Promise<void>((resolve, reject) => {
treeKill(instance.pid, (err) => {
treeKill(pid, (err) => {
if (err) {
if (
process.platform === "win32" &&
@@ -311,11 +376,25 @@ export async function killApp(instance) {
})
}
export async function startApp(app: any) {
// Kill a launched app
export async function killApp(instance) {
if (instance) {
await killProcess(instance.pid)
}
}
export async function startApp(app) {
// force require usage instead of dynamic import in jest
// x-ref: https://github.com/nodejs/node/issues/35889
process.env.__NEXT_TEST_MODE = "jest"
// TODO: tests that use this should be migrated to use
// the nextStart test function instead as it tests outside
// of jest's context
await app.prepare()
const handler = app.getRequestHandler()
const server = http.createServer(handler)
;(server as any).__app = app
const server: any = http.createServer(handler)
server.__app = app
await promiseCall(server, "listen")
return server
@@ -346,7 +425,7 @@ export function waitFor(millis) {
return new Promise((resolve) => setTimeout(resolve, millis))
}
export async function startStaticServer(dir, notFoundFile) {
export async function startStaticServer(dir, notFoundFile, fixedPort) {
const app = express()
const server = http.createServer(app)
app.use(express.static(dir))
@@ -357,7 +436,7 @@ export async function startStaticServer(dir, notFoundFile) {
})
}
await promiseCall(server, "listen")
await promiseCall(server, "listen", fixedPort)
return server
}
@@ -401,55 +480,9 @@ export async function check(contentFn, regex, hardError = true) {
return false
}
export class File {
path: string
originalContent: any
constructor(path) {
this.path = path
this.originalContent = existsSync(this.path) ? readFileSync(this.path, "utf8") : null
}
write(content) {
if (!this.originalContent) {
this.originalContent = content
}
writeFileSync(this.path, content, "utf8")
}
replace(pattern, newValue) {
const currentContent = readFileSync(this.path, "utf8")
if (pattern instanceof RegExp) {
if (!pattern.test(currentContent)) {
throw new Error(
`Failed to replace content.\n\nPattern: ${pattern.toString()}\n\nContent: ${currentContent}`,
)
}
} else if (typeof pattern === "string") {
if (!currentContent.includes(pattern)) {
throw new Error(
`Failed to replace content.\n\nPattern: ${pattern}\n\nContent: ${currentContent}`,
)
}
} else {
throw new Error(`Unknown replacement attempt type: ${pattern}`)
}
const newContent = currentContent.replace(pattern, newValue)
this.write(newContent)
}
delete() {
unlinkSync(this.path)
}
restore() {
this.write(this.originalContent)
}
}
export async function evaluate(browser, input) {
if (typeof input === "function") {
const result = await browser.executeScript(input)
const result = await browser.eval(input)
await new Promise((resolve) => setTimeout(resolve, 30))
return result
} else {
@@ -479,9 +512,8 @@ export async function retry(fn, duration = 3000, interval = 500, description) {
}
export async function hasRedbox(browser, expected = true) {
let attempts = 30
do {
const has = await evaluate(browser, () => {
for (let i = 0; i < 30; i++) {
const result = await evaluate(browser, () => {
return Boolean(
[].slice
.call(document.querySelectorAll("nextjs-portal"))
@@ -492,15 +524,12 @@ export async function hasRedbox(browser, expected = true) {
),
)
})
if (has) {
return true
}
if (--attempts < 0) {
break
}
await new Promise((resolve) => setTimeout(resolve, 1000))
} while (expected)
if (result === expected) {
return result
}
await waitFor(1000)
}
return false
}
@@ -544,6 +573,24 @@ export async function getRedboxSource(browser) {
)
}
export async function getRedboxDescription(browser) {
return retry(
() =>
evaluate(browser, () => {
const portal = [].slice
.call(document.querySelectorAll("nextjs-portal"))
.find((p) => p.shadowRoot.querySelector("[data-nextjs-dialog-header]"))
const root = portal.shadowRoot
return root
.querySelector("#nextjs__container_errors_desc")
.innerText.replace(/__WEBPACK_DEFAULT_EXPORT__/, "Unknown")
}),
3000,
500,
"getRedboxDescription",
)
}
export function getBrowserBodyText(browser) {
return browser.eval('document.getElementsByTagName("body")[0].innerText')
}
@@ -552,8 +599,8 @@ export function normalizeRegEx(src) {
return new RegExp(src).source.replace(/\^\//g, "^\\/")
}
function readJson(path: string) {
return JSON.parse(readFileSync(path) as any)
function readJson(path) {
return JSON.parse(readFileSync(path).toString())
}
export function getBuildManifest(dir) {
@@ -614,3 +661,96 @@ export function readNextBuildServerPageFile(appDir, page) {
const pageFile = getPageFileFromPagesManifest(appDir, page)
return readFileSync(path.join(appDir, ".next", "server", pageFile), "utf8")
}
/**
*
* @param {string} suiteName
* @param {{env: 'prod' | 'dev', appDir: string}} context
* @param {{beforeAll?: Function; afterAll?: Function; runTests: Function}} options
*/
function runSuite(suiteName, context, options) {
const {appDir, env} = context
describe(`${suiteName} ${env}`, () => {
beforeAll(async () => {
options.beforeAll?.(env)
context.stderr = ""
const onStderr = (msg) => {
context.stderr += msg
}
context.stdout = ""
const onStdout = (msg) => {
context.stdout += msg
}
if (env === "prod") {
context.appPort = await findPort()
const {stdout, stderr, code} = await nextBuild(appDir, [], {
stderr: true,
stdout: true,
})
context.stdout = stdout
context.stderr = stderr
context.code = code
context.server = await nextStart(context.appDir, context.appPort, {
onStderr,
onStdout,
})
} else if (env === "dev") {
context.appPort = await findPort()
context.server = await launchApp(context.appDir, context.appPort, {
onStderr,
onStdout,
})
}
})
afterAll(async () => {
options.afterAll?.(env)
if (context.server) {
await killApp(context.server)
}
})
options.runTests(context, env)
})
}
/**
*
* @param {string} suiteName
* @param {string} appDir
* @param {{beforeAll?: Function; afterAll?: Function; runTests: Function}} options
*/
export function runDevSuite(suiteName, appDir, options) {
return runSuite(suiteName, {appDir, env: "dev"}, options)
}
/**
*
* @param {string} suiteName
* @param {string} appDir
* @param {{beforeAll?: Function; afterAll?: Function; runTests: Function}} options
*/
export function runProdSuite(suiteName, appDir, options) {
return runSuite(suiteName, {appDir, env: "prod"}, options)
}
interface RunNextCommandOptions {
cwd?: string
env?: Record<any, any>
spawnOptions?: any
instance?: any
stderr?: string
stdout?: string
ignoreFail?: boolean
nodeArgs?: []
}
interface RunNextCommandDevOptions {
cwd?: string
env?: Record<any, any>
onStdout?: (stdout: string) => void
onStderr?: (stderr: string) => void
stderr?: string | boolean
stdout?: string | boolean
nodeArgs?: []
bootupMarker?: any
nextStart?: boolean
}

View File

@@ -1,236 +1,105 @@
/// <reference types="./next-webdriver" />
import fetch from "node-fetch"
import os from "os"
import path from "path"
import {Builder, By} from "selenium-webdriver"
import {Options as ChromeOptions} from "selenium-webdriver/chrome"
import {Options as FireFoxOptions} from "selenium-webdriver/firefox"
import {Options as SafariOptions} from "selenium-webdriver/safari"
import Chain from "./wd-chain"
import {getFullUrl} from "./next-test-utils"
import {BrowserInterface} from "./browsers/base"
import browserMod, {quit} from "./browsers/playwright"
;(global as any).browserName = process.env.BROWSER_NAME || "chrome"
export function waitFor(millis) {
return new Promise((resolve) => setTimeout(resolve, millis))
}
let browserQuit
export {By}
const {
BROWSER_NAME: browserName = "chrome",
BROWSERSTACK,
BROWSERSTACK_USERNAME,
BROWSERSTACK_ACCESS_KEY,
HEADLESS,
CHROME_BIN,
LEGACY_SAFARI,
CHROMEWEBDRIVER,
} = process.env
let capabilities = {}
const isChrome = browserName === "chrome"
const isSafari = browserName === "safari"
const isFirefox = browserName === "firefox"
const isIE = browserName === "internet explorer"
if (CHROMEWEBDRIVER) {
console.log("path", process.env.PATH)
console.log("comspec", process.env.COMSPEC)
console.log("chromedriver", `${CHROMEWEBDRIVER}${path.delimiter}${process.env.PATH}`)
}
if (process.env.ChromeWebDriver) {
process.env.PATH = `${process.env.ChromeWebDriver}${path.delimiter}${process.env.PATH}`
}
const isBrowserStack = BROWSERSTACK && BROWSERSTACK_USERNAME && BROWSERSTACK_ACCESS_KEY
if (isBrowserStack) {
const safariOpts = {
os: "OS X",
os_version: "Mojave",
browser: "Safari",
}
const safariLegacyOpts = {
os: "OS X",
os_version: "Sierra",
browserName: "Safari",
browser_version: "10.1",
}
const ieOpts = {
os: "Windows",
os_version: "10",
browser: "IE",
}
const firefoxOpts = {
os: "Windows",
os_version: "10",
browser: "Firefox",
}
const sharedOpts = {
"browserstack.local": true,
"browserstack.video": false,
"browserstack.user": BROWSERSTACK_USERNAME,
"browserstack.key": BROWSERSTACK_ACCESS_KEY,
"browserstack.localIdentifier": global.browserStackLocalId,
}
capabilities = {
...capabilities,
...sharedOpts,
...(isIE ? ieOpts : {}),
...(isSafari ? (LEGACY_SAFARI ? safariLegacyOpts : safariOpts) : {}),
...(isFirefox ? firefoxOpts : {}),
}
}
let chromeOptions = new ChromeOptions()
let firefoxOptions = new FireFoxOptions()
let safariOptions = new SafariOptions()
chromeOptions.addArguments("--remote-debugging-port=9222")
chromeOptions.addArguments("--headless")
chromeOptions.addArguments("--no-sandbox")
chromeOptions.addArguments("--disable-gpu")
chromeOptions.addArguments("--disable-dev-shm-usage")
if (HEADLESS) {
const screenSize = {width: 1280, height: 720}
chromeOptions = chromeOptions.headless().windowSize(screenSize)
firefoxOptions = firefoxOptions.headless().windowSize(screenSize)
}
if (CHROME_BIN) {
console.log("chrome bin", CHROME_BIN)
chromeOptions.setChromeBinaryPath(path.resolve(CHROME_BIN))
}
let seleniumServer
if (isBrowserStack) {
seleniumServer = "http://hub-cloud.browserstack.com/wd/hub"
} else if (global.seleniumServerPort) {
seleniumServer = `http://localhost:${global.seleniumServerPort}/wd/hub`
}
let browser = new Builder()
.usingServer(seleniumServer)
.withCapabilities(capabilities)
.forBrowser(browserName)
.setChromeOptions(chromeOptions)
.setFirefoxOptions(firefoxOptions)
.setSafariOptions(safariOptions)
.build()
global.wd = browser
let initialWindow
let deviceIP = "localhost"
const getDeviceIP = async () => {
const networkIntfs = os.networkInterfaces()
// find deviceIP to use with BrowserStack
for (const intf of Object.keys(networkIntfs)) {
const addresses = networkIntfs[intf]
for (const {internal, address, family} of addresses || []) {
if (family !== "IPv4" || internal) continue
try {
const res = await fetch(`http://${address}:${global._newTabPort}`)
if (res.ok) {
deviceIP = address
break
}
} catch (_) {}
if (typeof afterAll === "function") {
afterAll(async () => {
if (browserQuit) {
await browserQuit()
}
}
})
}
export const closeBrowser = async () => {
// First we close all extra windows left over
let allWindows = await browser.getAllWindowHandles()
for (const win of allWindows) {
if (win === initialWindow) continue
try {
await browser.switchTo().window(win)
await browser.close()
} catch (_) {}
}
}
// eslint-disable-next-line no-unused-vars
const freshWindow = async (appPort) => {
// First we close all extra windows left over
let allWindows = await browser.getAllWindowHandles()
for (const win of allWindows) {
if (win === initialWindow) continue
try {
await browser.switchTo().window(win)
await browser.close()
} catch (_) {}
}
await browser.switchTo().window(initialWindow)
// now we open a fresh window
await browser.get(`http://${deviceIP}:${global._newTabPort}`)
// await browser.executeScript(`window.location.href = "http://${deviceIP}:${appPort}"`)
const newTabLink = await browser.findElement(By.css("#new"))
await newTabLink.click()
allWindows = await browser.getAllWindowHandles()
const newWindow = allWindows.find((win) => win !== initialWindow)
await browser.switchTo().window(newWindow!)
}
export const USE_SELENIUM = Boolean(
process.env.LEGACY_SAFARI ||
process.env.BROWSER_NAME === "internet explorer" ||
process.env.SKIP_LOCAL_SELENIUM_SERVER,
)
/**
*
* @param appPortOrUrl can either be the port or the full URL
* @param url the path/query to append when using appPort
* @param options.waitHydration whether to wait for react hydration to finish
* @param options.retryWaitHydration allow retrying hydration wait if reload occurs
* @param options.disableCache disable cache for page load
* @param options.beforePageLoad the callback receiving page instance before loading page
* @returns thenable browser instance
*/
export default async function webdriver(
appPort,
path,
waitHydration = true,
allowHydrationRetry = false,
) {
if (!initialWindow) {
initialWindow = await browser.getWindowHandle()
appPortOrUrl: string | number,
url: string,
options?: {
waitHydration?: boolean
retryWaitHydration?: boolean
disableCache?: boolean
beforePageLoad?: (page: any) => void
locale?: string
},
): Promise<BrowserInterface> {
let CurrentInterface: typeof BrowserInterface
const defaultOptions = {
waitHydration: true,
retryWaitHydration: false,
disableCache: false,
}
if (isBrowserStack && deviceIP === "localhost" && !LEGACY_SAFARI) {
await getDeviceIP()
}
// browser.switchTo().window() fails with `missing field `handle``
// in safari and firefox so disabling freshWindow since our
// tests shouldn't rely on it
// if (isChrome) {
// await freshWindow(appPort)
options = Object.assign(defaultOptions, options)
const {waitHydration, retryWaitHydration, disableCache, beforePageLoad, locale} = options
// we import only the needed interface
// if (USE_SELENIUM) {
// const browserMod = require('browsers/selenium')
// CurrentInterface = browserMod.default
// browserQuit = browserMod.quit
// } else {
// const browserMod = require('./browsers/playwright')
// CurrentInterface = browserMod.default
// browserQuit = browserMod.quit
// }
const url = `http://${deviceIP}:${appPort}${path}`
;(browser as any).initUrl = url
console.log(`> Loading browser with ${url}`)
CurrentInterface = browserMod
browserQuit = quit
await browser.get(url)
console.log(`> Loaded browser with ${url}`)
const browser = new CurrentInterface()
const browserName = process.env.BROWSER_NAME || "chrome"
await browser.setup(browserName, locale)
;(global as any).browserName = browserName
const fullUrl = getFullUrl(appPortOrUrl, url, "localhost")
console.log(`\n> Loading browser with ${fullUrl}\n`)
await browser.loadPage(fullUrl, {disableCache, beforePageLoad})
console.log(`\n> Loaded browser with ${fullUrl}\n`)
// Wait for application to hydrate
if (waitHydration) {
// console.log(`\n> Waiting hydration for ${url}\n`)
console.log(`\n> Waiting hydration for ${fullUrl}\n`)
const checkHydrated = async () => {
await browser.executeAsyncScript(function () {
await browser.evalAsync(function () {
var callback = arguments[arguments.length - 1]
// if it's not a Next.js app return
if (document.documentElement.innerHTML.indexOf("__NEXT_DATA__") === -1) {
console.log("Not a next.js page, resolving hydrate check")
callback()
}
// TODO: should we also ensure router.isReady is true
// by default before resolving?
if ((window as any).__NEXT_HYDRATED) {
console.log("Next.js page already hydrated")
callback()
} else {
var timeout = setTimeout(callback, 10 * 1000)
;(window as any).__NEXT_HYDRATED_CB = function () {
clearTimeout(timeout)
console.log("Next.js hydrate callback fired")
callback()
}
}
@@ -240,9 +109,9 @@ export default async function webdriver(
try {
await checkHydrated()
} catch (err) {
if (allowHydrationRetry) {
if (retryWaitHydration) {
// re-try in case the page reloaded during check
await waitFor(2000)
await new Promise((resolve) => setTimeout(resolve, 2000))
await checkHydrated()
} else {
console.error("failed to check hydration")
@@ -250,17 +119,7 @@ export default async function webdriver(
}
}
// console.log(`\n> Hydration complete for ${url}\n`)
console.log(`\n> Hydration complete for ${fullUrl}\n`)
}
const promiseProp = new Set(["then", "catch", "finally"])
return new Proxy(new Chain(browser), {
get(obj, prop) {
if (obj[prop] || promiseProp.has(prop as string)) {
return obj[prop]
}
return browser[prop]
},
})
return browser
}

View File

@@ -19,9 +19,13 @@
"fs-extra": "10.0.1",
"get-port": "6.1.2",
"node-fetch": "3.2.3",
"pkg-dir": "5.0.0",
"playwright-chromium": "1.14.1",
"react": "18.0.0",
"react-dom": "18.0.0",
"react-query": "3.39.0",
"resolve-cwd": "3.0.0",
"resolve-from": "5.0.0",
"rimraf": "3.0.2",
"selenium-webdriver": "4.1.1",
"tree-kill": "1.2.2",

View File

@@ -1,5 +1,94 @@
# @blitzjs/auth
## 2.0.0-alpha.40
### Patch Changes
- blitz@2.0.0-alpha.40
## 2.0.0-alpha.39
### Patch Changes
- Updated dependencies [b918055b]
- blitz@2.0.0-alpha.39
## 2.0.0-alpha.38
### Patch Changes
- blitz@2.0.0-alpha.38
## 2.0.0-alpha.37
### Patch Changes
- a80d2a8f: rename middleware type for blitz server plugin
- Updated dependencies [a80d2a8f]
- blitz@2.0.0-alpha.37
## 2.0.0-alpha.36
### Patch Changes
- blitz@2.0.0-alpha.36
## 2.0.0-alpha.35
### Patch Changes
- blitz@2.0.0-alpha.35
## 2.0.0-alpha.34
### Patch Changes
- blitz@2.0.0-alpha.34
## 2.0.0-alpha.33
### Patch Changes
- blitz@2.0.0-alpha.33
## 2.0.0-alpha.32
### Patch Changes
- blitz@2.0.0-alpha.32
## 2.0.0-alpha.31
### Patch Changes
- blitz@2.0.0-alpha.31
## 2.0.0-alpha.30
### Patch Changes
- Updated dependencies [ce453683]
- blitz@2.0.0-alpha.30
## 2.0.0-alpha.29
### Patch Changes
- Updated dependencies [962eb58a]
- blitz@2.0.0-alpha.29
## 2.0.0-alpha.28
### Patch Changes
- blitz@2.0.0-alpha.28
## 2.0.0-alpha.27
### Patch Changes
- blitz@2.0.0-alpha.27
## 2.0.0-alpha.26
### Patch Changes

View File

@@ -1,6 +1,6 @@
{
"name": "@blitzjs/auth",
"version": "2.0.0-alpha.26",
"version": "2.0.0-alpha.40",
"scripts": {
"build": "unbuild",
"predev": "wait-on -d 250 ../blitz/dist/index-server.d.ts",
@@ -26,7 +26,7 @@
"@types/secure-password": "3.1.1",
"b64-lite": "1.4.0",
"bad-behavior": "1.0.1",
"blitz": "2.0.0-alpha.26",
"blitz": "2.0.0-alpha.40",
"cookie": "0.4.1",
"cookie-session": "2.0.0",
"debug": "4.3.3",
@@ -39,7 +39,7 @@
"url": "0.11.0"
},
"devDependencies": {
"@blitzjs/config": "workspace:2.0.0-alpha.26",
"@blitzjs/config": "workspace:2.0.0-alpha.40",
"@testing-library/react": "13.0.0",
"@testing-library/react-hooks": "7.0.2",
"@types/cookie": "0.4.1",

View File

@@ -12,6 +12,8 @@ import {
createClientPlugin,
AuthenticationError,
RedirectError,
RouteUrlObject,
Ctx,
} from "blitz"
import {
COOKIE_CSRF_TOKEN,
@@ -26,7 +28,6 @@ import {
} from "../shared"
import _debug from "debug"
import {formatWithValidation} from "../shared/url-utils"
import {Ctx} from "blitz"
import {ComponentType} from "react"
import {ComponentProps} from "react"
@@ -190,9 +191,9 @@ export const useRedirectAuthenticated = (to: UrlObject | string) => {
}
}
export interface RouteUrlObject extends Pick<UrlObject, "pathname" | "query"> {
pathname: string
}
// export interface RouteUrlObject extends Pick<UrlObject, "pathname" | "query"> {
// pathname: string
// }
export type RedirectAuthenticatedTo = string | RouteUrlObject | false
export type RedirectAuthenticatedToFnCtx = {

View File

@@ -1,4 +1,4 @@
import type {BlitzServerPlugin, Middleware, Ctx} from "blitz"
import type {BlitzServerPlugin, RequestMiddleware, Ctx} from "blitz"
import {assert} from "blitz"
import {IncomingMessage, ServerResponse} from "http"
import {PublicData, SessionModel, SessionConfigMethods} from "../shared/types"
@@ -101,7 +101,7 @@ export function AuthServerPlugin(options: AuthPluginOptions): BlitzServerPlugin<
`The cookie prefix used has invalid characters. Only alphanumeric characters, "-" and "_" character are supported`,
)
const blitzSessionMiddleware: Middleware<
const blitzSessionMiddleware: RequestMiddleware<
IncomingMessage,
ServerResponse & {blitzCtx: Ctx}
> = async (req, res, next) => {
@@ -119,6 +119,6 @@ export function AuthServerPlugin(options: AuthPluginOptions): BlitzServerPlugin<
return blitzSessionMiddleware
}
return {
middlewares: [authPluginSessionMiddleware()],
requestMiddlewares: [authPluginSessionMiddleware()],
}
}

View File

@@ -8,7 +8,7 @@ import {
connectMiddleware,
Ctx,
handleRequestWithMiddleware,
Middleware,
RequestMiddleware,
MiddlewareResponse,
secureProxyMiddleware,
} from "blitz"
@@ -79,9 +79,9 @@ export function passportAuth(config: BlitzPassportConfig): ApiHandler {
const passportMiddleware = passport.initialize()
const middleware: Middleware<ApiHandlerIncomingMessage, MiddlewareResponse<Ctx>>[] = [
connectMiddleware(cookieSessionMiddleware as Middleware),
connectMiddleware(passportMiddleware as Middleware),
const middleware: RequestMiddleware<ApiHandlerIncomingMessage, MiddlewareResponse<Ctx>>[] = [
connectMiddleware(cookieSessionMiddleware as RequestMiddleware),
connectMiddleware(passportMiddleware as RequestMiddleware),
connectMiddleware(passport.session()),
]

View File

@@ -1,5 +1,96 @@
# @blitzjs/next
## 2.0.0-alpha.40
### Patch Changes
- 9ded8dac: useParam & useParams functions now accessible from @blitzjs/next
- @blitzjs/rpc@2.0.0-alpha.40
## 2.0.0-alpha.39
### Patch Changes
- @blitzjs/rpc@2.0.0-alpha.39
## 2.0.0-alpha.38
### Patch Changes
- Updated dependencies [8aee25c5]
- @blitzjs/rpc@2.0.0-alpha.38
## 2.0.0-alpha.37
### Patch Changes
- a80d2a8f: rename middleware type for blitz server plugin
- @blitzjs/rpc@2.0.0-alpha.37
## 2.0.0-alpha.36
### Patch Changes
- Updated dependencies [4cad9cca]
- @blitzjs/rpc@2.0.0-alpha.36
## 2.0.0-alpha.35
### Patch Changes
- @blitzjs/rpc@2.0.0-alpha.35
## 2.0.0-alpha.34
### Patch Changes
- Updated dependencies [dfd2408e]
- @blitzjs/rpc@2.0.0-alpha.34
## 2.0.0-alpha.33
### Patch Changes
- @blitzjs/rpc@2.0.0-alpha.33
## 2.0.0-alpha.32
### Patch Changes
- @blitzjs/rpc@2.0.0-alpha.32
## 2.0.0-alpha.31
### Patch Changes
- Updated dependencies [17ce29e5]
- @blitzjs/rpc@2.0.0-alpha.31
## 2.0.0-alpha.30
### Patch Changes
- @blitzjs/rpc@2.0.0-alpha.30
## 2.0.0-alpha.29
### Patch Changes
- @blitzjs/rpc@2.0.0-alpha.29
## 2.0.0-alpha.28
### Patch Changes
- @blitzjs/rpc@2.0.0-alpha.28
## 2.0.0-alpha.27
### Patch Changes
- Updated dependencies [07292910]
- @blitzjs/rpc@2.0.0-alpha.27
## 2.0.0-alpha.26
### Patch Changes

View File

@@ -1,6 +1,6 @@
{
"name": "@blitzjs/next",
"version": "2.0.0-alpha.26",
"version": "2.0.0-alpha.40",
"scripts": {
"build": "unbuild",
"dev": "pnpm predev && pnpm watch unbuild src --wait=0.2",
@@ -23,7 +23,7 @@
"eslint.js"
],
"dependencies": {
"@blitzjs/rpc": "2.0.0-alpha.26",
"@blitzjs/rpc": "2.0.0-alpha.40",
"@types/hoist-non-react-statics": "3.3.1",
"debug": "4.3.3",
"fs-extra": "10.0.1",
@@ -32,21 +32,19 @@
"superjson": "1.8.0"
},
"devDependencies": {
"@blitzjs/config": "workspace:2.0.0-alpha.26",
"@blitzjs/config": "workspace:2.0.0-alpha.40",
"@testing-library/dom": "8.13.0",
"@testing-library/jest-dom": "5.16.3",
"@testing-library/react": "13.0.0",
"@testing-library/react-hooks": "7.0.2",
"@testing-library/user-event": "13.5.0",
"@types/lodash.frompairs": "4.0.6",
"@types/node": "17.0.16",
"@types/react": "18.0.1",
"@types/react-dom": "17.0.14",
"@types/testing-library__react-hooks": "4.0.0",
"blitz": "2.0.0-alpha.26",
"blitz": "2.0.0-alpha.40",
"cross-spawn": "7.0.3",
"find-up": "4.1.0",
"lodash.frompairs": "4.0.1",
"next": "12.1.6-canary.17",
"react": "18.0.0",
"react-dom": "18.0.0",

View File

@@ -17,6 +17,7 @@ import {Router} from "next/router"
export * from "./error-boundary"
export * from "./error-component"
export * from "./use-params"
export {Routes} from ".blitz"
const compose =

View File

@@ -1,3 +1,4 @@
import type {NextConfig} from "next"
import {
GetServerSideProps,
GetServerSidePropsResult,
@@ -7,21 +8,20 @@ import {
NextApiResponse,
} from "next"
import type {
Ctx as BlitzCtx,
BlitzServerPlugin,
Middleware,
MiddlewareResponse,
AsyncFunc,
FirstParam,
AddParameters,
AsyncFunc,
BlitzServerPlugin,
Ctx as BlitzCtx,
FirstParam,
RequestMiddleware,
MiddlewareResponse,
} from "blitz"
import {handleRequestWithMiddleware} from "blitz"
import type {NextConfig} from "next"
import {getQueryKey, getInfiniteQueryKey, installWebpackConfig} from "@blitzjs/rpc"
import {dehydrate} from "@blitzjs/rpc"
import {handleRequestWithMiddleware, startWatcher, stopWatcher} from "blitz"
import {dehydrate, getQueryKey, getInfiniteQueryKey, loaderClient, loaderServer} from "@blitzjs/rpc"
import {DefaultOptions, QueryClient} from "react-query"
import {IncomingMessage, ServerResponse} from "http"
import {withSuperJsonProps} from "./superjson"
import {ResolverBasePath} from "@blitzjs/rpc/src/index-server"
export * from "./index-browser"
@@ -38,7 +38,7 @@ export type NextApiHandler = (
) => void | Promise<void>
type SetupBlitzOptions = {
plugins: BlitzServerPlugin<Middleware, Ctx>[]
plugins: BlitzServerPlugin<RequestMiddleware, Ctx>[]
}
export type BlitzGSSPHandler<TProps> = ({
@@ -60,7 +60,7 @@ export type BlitzAPIHandler = (
) => ReturnType<NextApiHandler>
export const setupBlitzServer = ({plugins}: SetupBlitzOptions) => {
const middlewares = plugins.flatMap((p) => p.middlewares)
const middlewares = plugins.flatMap((p) => p.requestMiddlewares)
const contextMiddleware = plugins.flatMap((p) => p.contextMiddleware).filter(Boolean)
const gSSP =
@@ -133,22 +133,88 @@ export const setupBlitzServer = ({plugins}: SetupBlitzOptions) => {
export interface BlitzConfig extends NextConfig {
blitz?: {
resolverBasePath?: ResolverBasePath
customServer?: {
hotReload?: boolean
}
}
}
interface WebpackRuleOptions {
resolverBasePath?: ResolverBasePath
}
interface WebpackRule {
test: RegExp
use: Array<{
loader: string
options: WebpackRuleOptions
}>
}
interface InstallWebpackConfigOptions {
webpackConfig: {
module: {
rules: WebpackRule[]
}
}
nextConfig: BlitzConfig
}
export function installWebpackConfig({webpackConfig, nextConfig}: InstallWebpackConfigOptions) {
const options: WebpackRuleOptions = {
resolverBasePath: nextConfig.blitz?.resolverBasePath,
}
webpackConfig.module.rules.push({
test: /\/\[\[\.\.\.blitz]]\.[jt]s$/,
use: [
{
loader: loaderServer,
options,
},
],
})
webpackConfig.module.rules.push({
test: /[\\/](queries|mutations)[\\/]/,
use: [
{
loader: loaderClient,
options,
},
],
})
}
export function withBlitz(nextConfig: BlitzConfig = {}) {
return Object.assign({}, nextConfig, {
webpack: (config: any, options: any) => {
installWebpackConfig(config)
if (
process.env.NODE_ENV !== "production" &&
process.env.NODE_ENV !== "test" &&
process.env.MODE !== "test"
) {
void startWatcher()
process.on("SIGINT", () => {
void stopWatcher()
process.exit(0)
})
process.on("exit", function () {
void stopWatcher()
})
}
const config = Object.assign({}, nextConfig, {
webpack: (config: InstallWebpackConfigOptions["webpackConfig"], options: any) => {
installWebpackConfig({webpackConfig: config, nextConfig})
if (typeof nextConfig.webpack === "function") {
return nextConfig.webpack(config, options)
}
return config
},
} as NextConfig)
})
return config
}
export type PrefetchQueryFn = <T extends AsyncFunc, TInput = FirstParam<T>>(

View File

@@ -1,4 +1,3 @@
import fromPairs from "lodash.frompairs"
import {NextRouter} from "next/router"
import {ParsedUrlQuery} from "querystring"
import React from "react"
@@ -81,7 +80,7 @@ function areQueryValuesEqual(value1: ParsedUrlQueryValue, value2: ParsedUrlQuery
}
export function extractRouterParams(routerQuery: ParsedUrlQuery, asPathQuery: ParsedUrlQuery) {
return fromPairs(
return Object.fromEntries(
Object.entries(routerQuery).filter(
([key, value]) =>
typeof asPathQuery[key] === "undefined" || !areQueryValuesEqual(value, asPathQuery[key]),

View File

@@ -1,5 +1,112 @@
# @blitzjs/rpc
## 2.0.0-alpha.40
### Patch Changes
- @blitzjs/auth@2.0.0-alpha.40
- blitz@2.0.0-alpha.40
## 2.0.0-alpha.39
### Patch Changes
- Updated dependencies [b918055b]
- blitz@2.0.0-alpha.39
- @blitzjs/auth@2.0.0-alpha.39
## 2.0.0-alpha.38
### Patch Changes
- 8aee25c5: getQueryClient function & queryClient codemod updates & shared plugin config
- blitz@2.0.0-alpha.38
- @blitzjs/auth@2.0.0-alpha.38
## 2.0.0-alpha.37
### Patch Changes
- Updated dependencies [a80d2a8f]
- blitz@2.0.0-alpha.37
- @blitzjs/auth@2.0.0-alpha.37
## 2.0.0-alpha.36
### Patch Changes
- 4cad9cca: Add queryClient to RPC Plugin exports
- blitz@2.0.0-alpha.36
- @blitzjs/auth@2.0.0-alpha.36
## 2.0.0-alpha.35
### Patch Changes
- blitz@2.0.0-alpha.35
- @blitzjs/auth@2.0.0-alpha.35
## 2.0.0-alpha.34
### Patch Changes
- dfd2408e: Add resolverBasePath to Blitz config to change the way rpc routes are generated
- @blitzjs/auth@2.0.0-alpha.34
- blitz@2.0.0-alpha.34
## 2.0.0-alpha.33
### Patch Changes
- @blitzjs/auth@2.0.0-alpha.33
- blitz@2.0.0-alpha.33
## 2.0.0-alpha.32
### Patch Changes
- @blitzjs/auth@2.0.0-alpha.32
- blitz@2.0.0-alpha.32
## 2.0.0-alpha.31
### Patch Changes
- 17ce29e5: Update RPC plugin setup in templates
- blitz@2.0.0-alpha.31
- @blitzjs/auth@2.0.0-alpha.31
## 2.0.0-alpha.30
### Patch Changes
- Updated dependencies [ce453683]
- blitz@2.0.0-alpha.30
- @blitzjs/auth@2.0.0-alpha.30
## 2.0.0-alpha.29
### Patch Changes
- Updated dependencies [962eb58a]
- blitz@2.0.0-alpha.29
- @blitzjs/auth@2.0.0-alpha.29
## 2.0.0-alpha.28
### Patch Changes
- blitz@2.0.0-alpha.28
- @blitzjs/auth@2.0.0-alpha.28
## 2.0.0-alpha.27
### Patch Changes
- 07292910: Add invokeWithCtx function
- @blitzjs/auth@2.0.0-alpha.27
- blitz@2.0.0-alpha.27
## 2.0.0-alpha.26
### Patch Changes

View File

@@ -1,6 +1,6 @@
{
"name": "@blitzjs/rpc",
"version": "2.0.0-alpha.26",
"version": "2.0.0-alpha.40",
"scripts": {
"build": "unbuild",
"predev": "wait-on -d 250 ../blitz/dist/index-server.d.ts && wait-on -d 250 ../blitz-auth/dist/index-browser.d.ts",
@@ -20,7 +20,7 @@
"dist/**"
],
"dependencies": {
"@blitzjs/auth": "2.0.0-alpha.26",
"@blitzjs/auth": "2.0.0-alpha.40",
"b64-lite": "1.4.0",
"bad-behavior": "1.0.1",
"chalk": "^4.1.0",
@@ -30,11 +30,11 @@
"zod": "3.10.1"
},
"devDependencies": {
"@blitzjs/config": "workspace:2.0.0-alpha.26",
"@blitzjs/config": "workspace:2.0.0-alpha.40",
"@types/debug": "4.1.7",
"@types/react": "18.0.1",
"@types/react-dom": "17.0.14",
"blitz": "2.0.0-alpha.26",
"blitz": "2.0.0-alpha.40",
"next": "12.1.6-canary.17",
"react": "18.0.0",
"react-dom": "18.0.0",
@@ -43,7 +43,7 @@
"watch": "1.0.2"
},
"peerDependencies": {
"blitz": "2.0.0-alpha.26",
"blitz": "2.0.0-alpha.40",
"next": "*"
},
"publishConfig": {

View File

@@ -2,12 +2,13 @@ export * from "./rpc"
export {useQuery, usePaginatedQuery, useInfiniteQuery, useMutation} from "./react-query"
export type {MutateFunction} from "./react-query"
export {
queryClient,
getQueryKey,
getInfiniteQueryKey,
invalidateQuery,
setQueryData,
getQueryClient,
} from "./react-query-utils"
export {useQueryErrorResetBoundary, QueryClient} from "react-query"
export {dehydrate} from "react-query/hydration"
export {invoke} from "./invoke"
export {invokeWithCtx} from "./invokeWithCtx"

View File

@@ -0,0 +1,15 @@
import {FirstParam, PromiseReturnType, Ctx} from "blitz"
export function invokeWithCtx<T extends (...args: any) => any, TInput = FirstParam<T>>(
queryFn: T,
params: TInput,
ctx: Ctx,
): Promise<PromiseReturnType<T>> {
if (typeof queryFn === "undefined") {
throw new Error(
"invokeWithCtx is missing the first argument - it must be a query or mutation function",
)
}
return queryFn(params, ctx)
}

View File

@@ -54,8 +54,8 @@ export const initializeQueryClient = () => {
})
}
// Create internal QueryClient instance
export const queryClient = initializeQueryClient()
// Query client is initialised in `BlitzRpcPlugin`, and can only be used with BlitzRpcPlugin right now
export const getQueryClient = () => globalThis.queryClient
function isRpcClient(f: any): f is RpcClient<any, any> {
return !!f._isRpcClient
@@ -176,7 +176,7 @@ export function invalidateQuery<TInput, TResult, T extends AsyncFunc>(
// Params not provided, only use first query key item (url)
queryKey = fullQueryKey[0]
}
return queryClient.invalidateQueries(queryKey)
return getQueryClient().invalidateQueries(queryKey)
}
export function setQueryData<TInput, TResult, T extends AsyncFunc>(
@@ -184,15 +184,15 @@ export function setQueryData<TInput, TResult, T extends AsyncFunc>(
params: TInput,
newData: TResult | ((oldData: TResult | undefined) => TResult),
opts: MutateOptions = {refetch: true},
): Promise<void | ReturnType<typeof queryClient.invalidateQueries>> {
): Promise<void | ReturnType<ReturnType<typeof getQueryClient>["invalidateQueries"]>> {
if (typeof resolver === "undefined") {
throw new Error("setQueryData is missing the first argument - it must be a resolver function")
}
const queryKey = getQueryKey(resolver, params)
return new Promise((res) => {
queryClient.setQueryData(queryKey, newData)
let result: void | ReturnType<typeof queryClient.invalidateQueries>
getQueryClient().setQueryData(queryKey, newData)
let result: void | ReturnType<ReturnType<typeof getQueryClient>["invalidateQueries"]>
if (opts.refetch) {
result = invalidateQuery(resolver, params)
}

View File

@@ -2,8 +2,8 @@ import {normalizePathTrailingSlash} from "next/dist/client/normalize-trailing-sl
import {addBasePath} from "next/dist/shared/lib/router/router"
import {deserialize, serialize} from "superjson"
import {SuperJSONResult} from "superjson/dist/types"
import {isServer, CSRFTokenMismatchError} from "blitz"
import {getQueryKeyFromUrlAndParams, queryClient} from "./react-query-utils"
import {CSRFTokenMismatchError, isServer} from "blitz"
import {getQueryKeyFromUrlAndParams, getQueryClient} from "./react-query-utils"
import {
getAntiCSRFToken,
getPublicDataStore,
@@ -55,7 +55,7 @@ export function __internal_buildRpcClient({
resolverType,
routePath,
}: BuildRpcClientParams): RpcClient {
const fullRoutePath = normalizeApiRoute("/api/rpc/" + routePath)
const fullRoutePath = normalizeApiRoute("/api/rpc" + routePath)
const httpClient: RpcClientBase = async (params, opts = {}) => {
const debug = (await import("debug")).default("blitz:rpc")
@@ -123,9 +123,9 @@ export function __internal_buildRpcClient({
setTimeout(async () => {
// Do these in the next tick to prevent various bugs like https://github.com/blitz-js/blitz/issues/2207
debug("Invalidating react-query cache...")
await queryClient.cancelQueries()
await queryClient.resetQueries()
queryClient.getMutationCache().clear()
await getQueryClient().cancelQueries()
await getQueryClient().resetQueries()
getQueryClient().getMutationCache().clear()
// We have a 100ms delay here to prevent unnecessary stale queries from running
// This prevents the case where you logout on a page with
// Page.authenticate = {redirectTo: '/login'}
@@ -184,7 +184,7 @@ export function __internal_buildRpcClient({
if (!opts.fromQueryHook) {
const queryKey = getQueryKeyFromUrlAndParams(routePath, params)
queryClient.setQueryData(queryKey, data)
getQueryClient().setQueryData(queryKey, data)
}
return data
}

View File

@@ -4,14 +4,13 @@ import {DefaultOptions, QueryClient} from "react-query"
export * from "./data-client/index"
export const queryClient = globalThis.queryClient
interface BlitzRpcOptions {
reactQueryOptions?: DefaultOptions
}
export const BlitzRpcPlugin = createClientPlugin<BlitzRpcOptions, any>(
({reactQueryOptions}: BlitzRpcOptions) => {
export const BlitzRpcPlugin = createClientPlugin<BlitzRpcOptions, {queryClient: QueryClient}>(
(options?: BlitzRpcOptions) => {
const initializeQueryClient = () => {
const {reactQueryOptions} = options || {}
let suspenseEnabled = reactQueryOptions?.queries?.suspense ?? true
if (!process.env.CLI_COMMAND_CONSOLE && !process.env.CLI_COMMAND_DB) {
globalThis.__BLITZ_SUSPENSE_ENABLED = suspenseEnabled
@@ -36,12 +35,14 @@ export const BlitzRpcPlugin = createClientPlugin<BlitzRpcOptions, any>(
},
})
}
globalThis.queryClient = initializeQueryClient()
const queryClient = initializeQueryClient()
globalThis.queryClient = queryClient
return {
events: {},
middleware: {},
exports: () => {},
exports: () => ({
queryClient,
}),
}
},
)

View File

@@ -1,7 +1,8 @@
import {assert, Ctx, baseLogger, prettyMs, newLine} from "blitz"
import {assert, baseLogger, Ctx, newLine, prettyMs} from "blitz"
import {NextApiRequest, NextApiResponse} from "next"
import {deserialize, serialize as superjsonSerialize} from "superjson"
import chalk from "chalk"
import {resolve} from "path"
// TODO - optimize end user server bundles by not exporting all client stuff here
export * from "./index-browser"
@@ -24,6 +25,7 @@ function getGlobalObject<T extends Record<string, unknown>>(key: string, default
type Resolver = (...args: unknown[]) => Promise<unknown>
type ResolverFiles = Record<string, () => Promise<{default?: Resolver}>>
export type ResolverBasePath = "queries|mutations" | "root" | undefined
// We define `global.__internal_blitzRpcResolverFiles` to ensure we use the same global object.
// Needed for Next.js. I'm guessing that Next.js is including the `node_modules/` files in a seperate bundle than user files.
@@ -47,21 +49,9 @@ export function __internal_addBlitzRpcResolver(
return resolver
}
import {resolve} from "path"
const dir = __dirname + (() => "")() // trick to avoid `@vercel/ncc` to glob import
const loaderServer = resolve(dir, "./loader-server.cjs")
const loaderClient = resolve(dir, "./loader-client.cjs")
export function installWebpackConfig<T extends any[]>(config: {module?: {rules?: T}}) {
config.module!.rules!.push({
test: /\/\[\[\.\.\.blitz]]\.[jt]s$/,
use: [{loader: loaderServer}],
})
config.module!.rules!.push({
test: /[\\/](queries|mutations)[\\/]/,
use: [{loader: loaderClient}],
})
}
export const loaderServer = resolve(dir, "./loader-server.cjs")
export const loaderClient = resolve(dir, "./loader-client.cjs")
// ----------
// END LOADER

View File

@@ -3,20 +3,13 @@ import {
convertFilePathToResolverName,
convertFilePathToResolverType,
convertPageFilePathToRoutePath,
Loader,
LoaderOptions,
toPosixPath,
} from "./loader-utils"
import {assert} from "blitz"
import {posix} from "path"
// Subset of `import type { LoaderDefinitionFunction } from 'webpack'`
type Loader = {
_compiler?: {
name: string
context: string
}
resource: string
cacheable: (enabled: boolean) => void
}
export async function loader(this: Loader, input: string): Promise<string> {
const compiler = this._compiler!
@@ -25,8 +18,12 @@ export async function loader(this: Loader, input: string): Promise<string> {
const isSSR = compiler.name === "server"
if (!isSSR) {
const code = await transformBlitzRpcResolverClient(input, toPosixPath(id), toPosixPath(root))
return code
return await transformBlitzRpcResolverClient(
input,
toPosixPath(id),
toPosixPath(root),
this.query,
)
}
return input
@@ -34,13 +31,18 @@ export async function loader(this: Loader, input: string): Promise<string> {
module.exports = loader
export async function transformBlitzRpcResolverClient(_src: string, id: string, root: string) {
export async function transformBlitzRpcResolverClient(
_src: string,
id: string,
root: string,
options?: LoaderOptions,
) {
assertPosixPath(id)
assertPosixPath(root)
const resolverFilePath = "/" + posix.relative(root, id)
assertPosixPath(resolverFilePath)
const routePath = convertPageFilePathToRoutePath(resolverFilePath)
const routePath = convertPageFilePathToRoutePath(resolverFilePath, options?.resolverBasePath)
const resolverName = convertFilePathToResolverName(resolverFilePath)
const resolverType = convertFilePathToResolverType(resolverFilePath)

View File

@@ -1,23 +1,17 @@
import {posix, join, dirname} from "path"
import {dirname, join, posix} from "path"
import {promises} from "fs"
import {
assertPosixPath,
toPosixPath,
buildPageExtensionRegex,
getIsRpcFile,
topLevelFoldersThatMayContainResolvers,
convertPageFilePathToRoutePath,
getIsRpcFile,
Loader,
LoaderOptions,
topLevelFoldersThatMayContainResolvers,
toPosixPath,
} from "./loader-utils"
// Subset of `import type { LoaderDefinitionFunction } from 'webpack'`
type Loader = {
_compiler?: {
name: string
context: string
}
resource: string
cacheable: (enabled: boolean) => void
}
export async function loader(this: Loader, input: string): Promise<string> {
const compiler = this._compiler!
@@ -29,8 +23,13 @@ export async function loader(this: Loader, input: string): Promise<string> {
this.cacheable(false)
const resolvers = await collectResolvers(root, ["ts", "js"])
const code = await transformBlitzRpcServer(input, toPosixPath(id), toPosixPath(root), resolvers)
return code
return await transformBlitzRpcServer(
input,
toPosixPath(id),
toPosixPath(root),
resolvers,
this.query,
)
}
return input
@@ -43,6 +42,7 @@ export async function transformBlitzRpcServer(
id: string,
root: string,
resolvers: string[],
options?: LoaderOptions,
) {
assertPosixPath(id)
assertPosixPath(root)
@@ -55,7 +55,7 @@ export async function transformBlitzRpcServer(
for (let resolverFilePath of resolvers) {
const relativeResolverPath = posix.relative(dirname(id), join(root, resolverFilePath))
const routePath = convertPageFilePathToRoutePath(resolverFilePath)
const routePath = convertPageFilePathToRoutePath(resolverFilePath, options?.resolverBasePath)
code += `__internal_addBlitzRpcResolver('${routePath}', () => import('${relativeResolverPath}'));`
code += "\n"
}

View File

@@ -0,0 +1,24 @@
import {describe, expect, it} from "vitest"
import {convertPageFilePathToRoutePath} from "./loader-utils"
const FILE_PATH = "app/queries/getData.ts"
describe("convertPageFilePathToRoutePath", () => {
it("should return the full path when resolverBasePath is set to root", () => {
const res = convertPageFilePathToRoutePath(FILE_PATH, "root")
expect(res).toEqual("app/queries/getData")
})
it("should return the relative path when resolverBasePath is set to queries|mutations", () => {
const res = convertPageFilePathToRoutePath(FILE_PATH, "queries|mutations")
expect(res).toEqual("/getData")
})
it("should return the relative path when resolverBasePath is set to undefined", () => {
const res = convertPageFilePathToRoutePath(FILE_PATH, undefined)
expect(res).toEqual("/getData")
})
})

View File

@@ -1,5 +1,20 @@
import {assert} from "blitz"
import {win32, posix, sep} from "path"
import {posix, sep, win32} from "path"
import {ResolverBasePath} from "./index-server"
export interface LoaderOptions {
resolverBasePath?: ResolverBasePath
}
export interface Loader {
_compiler?: {
name: string
context: string
}
resource: string
cacheable: (enabled: boolean) => void
query: LoaderOptions
}
export function assertPosixPath(path: string) {
const errMsg = `Wrongly formatted path: ${path}`
@@ -34,7 +49,14 @@ export function buildPageExtensionRegex(pageExtensions: string[]) {
const fileExtensionRegex = /\.([a-z]+)$/
export function convertPageFilePathToRoutePath(filePath: string) {
export function convertPageFilePathToRoutePath(
filePath: string,
resolverBasePath: ResolverBasePath,
) {
if (resolverBasePath === "root") {
return filePath.replace(fileExtensionRegex, "")
}
return filePath
.replace(/^.*?[\\/]queries[\\/]/, "/")
.replace(/^.*?[\\/]mutations[\\/]/, "/")

Some files were not shown because too many files have changed in this diff Show More