access.md and the 1.16.4 upgrade notes led projects to import isAdmin from
'@intecion/ipal-kit' in a client component; with `next build --webpack` that
fails ('server-only' cannot be imported from a Client Component module).
No existing entry had a client-safe one, so '@intecion/ipal-kit/client' now
exports hasMinimumRole, isAdmin, isEditor and the type Role; the main entry
keeps them for server code.
Docs: access.md (snippet with imports, server vs client, useConfig() carries
admin.user only), AGENT.md, ipal-debug, ipal-reference, ipal-upgrade, the
1.16.4 note, an Unreleased changelog entry. Tests: /client reaches no
server-only module, the predicates' answers with a client config, no
client-component example in the docs imports the main entry.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
87 lines
3.8 KiB
TypeScript
87 lines
3.8 KiB
TypeScript
/**
|
|
* The main entry (payload.config, the payload CLI, server code) and the proxy
|
|
* entry must not load 'use client' modules: in plain Node every re-exported
|
|
* module is evaluated, and the proxy bundle should stay free of React UI.
|
|
*/
|
|
import { existsSync, readFileSync } from 'node:fs'
|
|
import path from 'node:path'
|
|
import { describe, expect, test } from 'vitest'
|
|
|
|
const root = path.resolve(import.meta.dirname, '..')
|
|
|
|
function resolve(from: string, spec: string): null | string {
|
|
if (!spec.startsWith('.')) {return null}
|
|
const base = path.resolve(path.dirname(from), spec).replace(/\.js$/, '')
|
|
for (const ext of ['.ts', '.tsx', '/index.ts']) {
|
|
if (existsSync(base + ext)) {return base + ext}
|
|
}
|
|
return null
|
|
}
|
|
|
|
/** Runtime (non-type) imports and re-exports reachable from `entry`; returns the 'use client' ones. */
|
|
function clientModulesReachable(entry: string): string[] {
|
|
const seen = new Set<string>()
|
|
const hits: string[] = []
|
|
const stack = [path.join(root, entry)]
|
|
while (stack.length) {
|
|
const file = stack.pop()!
|
|
if (seen.has(file)) {continue}
|
|
seen.add(file)
|
|
const src = readFileSync(file, 'utf8')
|
|
if (/^\s*['"]use client['"]/.test(src)) {hits.push(path.relative(root, file))}
|
|
for (const m of src.matchAll(/^(?:import|export) (?!type\b)[^'"]*?from ['"]([^'"]+)['"]/gm)) {
|
|
const next = resolve(file, m[1])
|
|
if (next) {stack.push(next)}
|
|
}
|
|
}
|
|
return hits.sort()
|
|
}
|
|
|
|
describe('server entries load no client modules', () => {
|
|
test.each(['src/exports/next-middleware.ts', 'src/index.ts'])('%s', (entry) => {
|
|
expect(clientModulesReachable(entry)).toEqual([])
|
|
})
|
|
})
|
|
|
|
/** Modules importing 'server-only' reachable from `entry` — static and dynamic imports (a bundler follows both). */
|
|
function serverOnlyReachable(entry: string): string[] {
|
|
const seen = new Set<string>()
|
|
const hits: string[] = []
|
|
const stack = [path.join(root, entry)]
|
|
while (stack.length) {
|
|
const file = stack.pop()!
|
|
if (seen.has(file)) {continue}
|
|
seen.add(file)
|
|
const src = readFileSync(file, 'utf8')
|
|
if (/^import ['"]server-only['"]/m.test(src)) {hits.push(path.relative(root, file))}
|
|
for (const m of src.matchAll(/^(?:import|export) (?!type\b)[^'"]*?from ['"]([^'"]+)['"]|\bimport\(\s*['"]([^'"]+)['"]/gm)) {
|
|
const next = resolve(file, m[1] ?? m[2])
|
|
if (next) {stack.push(next)}
|
|
}
|
|
}
|
|
return hits.sort()
|
|
}
|
|
|
|
describe('client components', () => {
|
|
test('/client reaches no server-only module', () => {
|
|
expect(serverOnlyReachable('src/exports/client.ts')).toEqual([])
|
|
// why a client component must not import the main entry: webpack stops at these
|
|
expect(serverOnlyReachable('src/index.ts')).not.toEqual([])
|
|
})
|
|
|
|
test('/client exports the role predicates, with the same fail-closed answers', async () => {
|
|
const { hasMinimumRole, isAdmin, isEditor } = await import('../src/modules/access/predicates.js')
|
|
const client = readFileSync(path.join(root, 'src/exports/client.ts'), 'utf8')
|
|
expect(client).toMatch(/export \{ hasMinimumRole, isAdmin, isEditor \} from '\.\.\/modules\/access\/predicates\.js'/)
|
|
const config = { admin: { user: 'users' } } // what useConfig() carries: `custom` is not in the client config
|
|
const admin = { collection: 'users', roles: ['admin'] }
|
|
const editor = { collection: 'users', roles: ['editor'] }
|
|
expect([isAdmin(admin, config), isAdmin(editor, config), isAdmin(null, config)]).toEqual([true, false, false])
|
|
expect([isEditor(editor, config), hasMinimumRole(editor, 'user', config)]).toEqual([true, true])
|
|
expect(isAdmin({ ...admin, collection: 'customers' }, config)).toBe(false)
|
|
// access.staffCollections is not in the client config: name the collections
|
|
expect(isAdmin({ ...admin, collection: 'partners' }, config)).toBe(false)
|
|
expect(isAdmin({ ...admin, collection: 'partners' }, { staffCollections: ['users', 'partners'] })).toBe(true)
|
|
})
|
|
})
|