The plugin's version is a constant: no package.json among a site's public assets
pluginVersion() named package.json relative to its own module. A bundler takes such a file for an asset: a Turbopack build of a site copied the plugin's whole package.json (dependency lists, scripts, the registry and repository addresses; no secret) to /_next/static/media/package.<hash>.json, served to anyone. Webpack kept it on the server side. The version now comes from src/version.ts (scripts/write-version.mjs, run by the `version` script; a test fails when it differs from package.json). A smoke test of the built site: nothing under .next/static is a package manifest. Co-Authored-By: Claude Fable 5.2 <[email protected]>
This commit is contained in:
co-authored by
Claude Fable 5.2
parent
bc0fa65fd7
commit
9a648b0498
+2
-1
@@ -127,7 +127,8 @@
|
||||
"test:e2e": "playwright test",
|
||||
"test:int": "vitest run",
|
||||
"test:watch": "vitest",
|
||||
"typecheck": "tsc --noEmit -p . && tsc -p test"
|
||||
"typecheck": "tsc --noEmit -p . && tsc -p test",
|
||||
"version": "node scripts/write-version.mjs && git add src/version.ts"
|
||||
},
|
||||
"dependencies": {
|
||||
"busboy": "1.6.0",
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
import { expect, type Page, test } from '@playwright/test'
|
||||
import { existsSync, readdirSync, readFileSync, statSync } from 'node:fs'
|
||||
import path from 'node:path'
|
||||
|
||||
import { attachmentOf, startSmtpSink } from './smtp-sink'
|
||||
import { totp } from './totp'
|
||||
@@ -176,6 +178,24 @@ test('SEO and GEO routes respond', async ({ request }) => {
|
||||
expect(await mcp.text()).toContain('get_page')
|
||||
})
|
||||
|
||||
// A bundler turns a file that server code names relative to its own module into an ASSET. Turbopack published the
|
||||
// plugin's whole package.json that way (dependency lists, scripts, the registry address) under /_next/static/media/.
|
||||
test('no package.json is published: nothing under .next/static holds one, and the obvious addresses answer 404', async ({ request }) => {
|
||||
const statics = path.resolve('.next/static')
|
||||
const walk = (dir: string): string[] => (existsSync(dir) ? readdirSync(dir).flatMap((name) => {
|
||||
const file = path.join(dir, name)
|
||||
return statSync(file).isDirectory() ? walk(file) : [file]
|
||||
}) : [])
|
||||
const files = walk(statics)
|
||||
expect(files.length, 'the built site\'s .next/static (run from the site folder)').toBeGreaterThan(0)
|
||||
// by name, and by content (a manifest of a package: its dependency lists)
|
||||
const manifests = files.filter((file) => /package[^/]*\.json$/.test(file) || (file.endsWith('.json') && /"(?:devDependencies|peerDependencies|publishConfig)"\s*:/.test(readFileSync(file, 'utf8'))))
|
||||
expect(manifests.map((file) => path.relative(statics, file))).toEqual([])
|
||||
for (const address of ['/package.json', '/_next/package.json', '/_next/static/package.json', '/_next/static/media/package.json']) {
|
||||
expect((await request.get(address)).status(), address).toBe(404)
|
||||
}
|
||||
})
|
||||
|
||||
test('security headers: CSP enforced, no X-Powered-By', async ({ request }) => {
|
||||
const headers = (await request.get('/pl')).headers()
|
||||
expect(headers['content-security-policy']).toBeTruthy()
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
#!/usr/bin/env node
|
||||
// Writes the version from package.json into src/version.ts (see the comment there).
|
||||
// Runs as the `version` script of package.json; by hand: node scripts/write-version.mjs
|
||||
import { readFileSync, writeFileSync } from 'node:fs'
|
||||
|
||||
const root = new URL('../', import.meta.url)
|
||||
const { version } = JSON.parse(readFileSync(new URL('package.json', root), 'utf8'))
|
||||
const file = new URL('src/version.ts', root)
|
||||
const before = readFileSync(file, 'utf8')
|
||||
const after = before.replace(/export const PLUGIN_VERSION = '[^']*'/, `export const PLUGIN_VERSION = '${version}'`)
|
||||
if (after === before && !before.includes(`'${version}'`)) {throw new Error('src/version.ts: the PLUGIN_VERSION line was not found')}
|
||||
writeFileSync(file, after)
|
||||
console.log(`src/version.ts → ${version}`)
|
||||
@@ -1,10 +1,10 @@
|
||||
import type { BasePayload, Endpoint, PayloadRequest } from 'payload'
|
||||
|
||||
import { timingSafeEqual } from 'node:crypto'
|
||||
import { readFileSync } from 'node:fs'
|
||||
|
||||
import type { DoctorStatus } from './types.js'
|
||||
|
||||
import { PLUGIN_VERSION } from '../../version.js'
|
||||
import { openSignUpCollections, staffCollectionsOnOwnRoles, staffCollectionsOpenToOthers } from '../access/selfRegistration.js'
|
||||
import { countPendingCookies } from '../consent/scanner/approval.js'
|
||||
import { COOKIE_REVIEW_SLUG } from '../consent/scanner/collection.js'
|
||||
@@ -13,13 +13,9 @@ import { countAccountsWithoutTotp } from '../security/totp/setupRequired.js'
|
||||
import { clientIp, consumeRateLimit, tooManyRequests } from '../shared/rateLimit.js'
|
||||
import { turnstileSecretFromEnv } from '../turnstile/config.js'
|
||||
|
||||
/** The plugin's own version (package.json next to dist/ and src/). */
|
||||
/** The plugin's own version — a constant (src/version.ts), never a file read at run time. */
|
||||
export function pluginVersion(): string {
|
||||
try {
|
||||
return (JSON.parse(readFileSync(new URL('../../../package.json', import.meta.url), 'utf8')) as { version: string }).version
|
||||
} catch {
|
||||
return 'unknown'
|
||||
}
|
||||
return PLUGIN_VERSION
|
||||
}
|
||||
|
||||
type CollectionLike = { fields?: Array<{ name?: string }>; slug: string }
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
/**
|
||||
* The plugin's own version, as a constant in the code.
|
||||
*
|
||||
* Not read from package.json at run time: naming that file relative to the module (a URL built
|
||||
* from the module's own address) in server code makes a bundler treat the file as an ASSET — Turbopack copied the plugin's whole
|
||||
* package.json (dependency lists, scripts, the registry address) into the site's public
|
||||
* `/_next/static/media/`. Written by `node scripts/write-version.mjs` (the `version` script of
|
||||
* package.json runs it on `pnpm version …`); test/version.test.ts fails when the two differ.
|
||||
*/
|
||||
export const PLUGIN_VERSION = '1.16.7'
|
||||
@@ -0,0 +1,32 @@
|
||||
import { readdirSync, readFileSync, statSync } from 'node:fs'
|
||||
import path from 'node:path'
|
||||
import { describe, expect, test } from 'vitest'
|
||||
|
||||
import { pluginVersion } from '../src/modules/doctor/status.js'
|
||||
import { PLUGIN_VERSION } from '../src/version.js'
|
||||
|
||||
const root = path.resolve(import.meta.dirname, '..')
|
||||
const walk = (dir: string): string[] => readdirSync(dir).flatMap((name) => {
|
||||
const file = path.join(dir, name)
|
||||
return statSync(file).isDirectory() ? walk(file) : [file]
|
||||
})
|
||||
|
||||
describe('the plugin version', () => {
|
||||
test('src/version.ts says what package.json says (node scripts/write-version.mjs after a bump)', () => {
|
||||
const { version } = JSON.parse(readFileSync(path.join(root, 'package.json'), 'utf8')) as { version: string }
|
||||
expect(PLUGIN_VERSION).toBe(version)
|
||||
expect(pluginVersion()).toBe(version)
|
||||
})
|
||||
|
||||
// A file named through `new URL(…, import.meta.url)` (or imported) in code a site bundles becomes an ASSET of
|
||||
// that site: Turbopack put the plugin's whole package.json into the public /_next/static/media/.
|
||||
test('no code a site bundles names package.json as a module-relative file', () => {
|
||||
const cli = /^src\/modules\/(?:starter|doctor\/(?:cli|project|run|probe|rules))/ // run by the bin scripts, on the project's own disk
|
||||
const offenders = walk(path.join(root, 'src'))
|
||||
.filter((file) => /\.(?:ts|tsx|mjs|js)$/.test(file))
|
||||
.map((file) => ({ code: readFileSync(file, 'utf8'), rel: path.relative(root, file) }))
|
||||
.filter(({ code, rel }) => (!cli.test(rel) && /new URL\([^)]*import\.meta\.url/.test(code)) || /(?:from|import\()\s*['"][^'"]*package\.json['"]/.test(code))
|
||||
.map(({ rel }) => rel)
|
||||
expect(offenders).toEqual([])
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user