feat(supabase): publish gate on the Rust core's sheet.check, fail closed
publishSheetVersion(client, versionId, validate) takes the core's sheet.check (the package never loads the module itself); anything but a publishable verdict refuses publication before the RPC: invalid_shape, not_publishable (same message as before: first error code), and validator_unavailable when the core cannot run. Tests run on the real Node core; the shape case now asserts the closed kind instead of any throw. The package no longer depends on @faunosteo/domain.
This commit is contained in:
1 parent
21906c4358
commit
6891f9ea25
6 files changed
+119
-18
No files matched your search
@@ -21,12 +21,12 @@
|
|||||||
"assets:upload": "node scripts/upload-sheet-assets.ts"
|
"assets:upload": "node scripts/upload-sheet-assets.ts"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@faunosteo/domain": "workspace:*",
|
|
||||||
"@supabase/supabase-js": "^2.100.0",
|
"@supabase/supabase-js": "^2.100.0",
|
||||||
"zod": "^3.24.1"
|
"zod": "^3.24.1"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@electric-sql/pglite": "^0.3.0",
|
"@electric-sql/pglite": "^0.3.0",
|
||||||
|
"@faunosteo/core-wasm": "workspace:*",
|
||||||
"@types/node": "^20.19.41",
|
"@types/node": "^20.19.41",
|
||||||
"eslint": "^9.17.0",
|
"eslint": "^9.17.0",
|
||||||
"globals": "^15.14.0",
|
"globals": "^15.14.0",
|
||||||
|
|||||||
@@ -1,18 +1,83 @@
|
|||||||
import {
|
|
||||||
AnatomicalSheetDocumentSchema,
|
|
||||||
assertPublishable,
|
|
||||||
} from '@faunosteo/domain'
|
|
||||||
import type { FaunosteoClient } from './client'
|
import type { FaunosteoClient } from './client'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Publication gate on the client side: the full Zod shape and every
|
* Publication gate on the client side: the full shape and every integrity
|
||||||
* integrity rule run before the RPC, so the editor gets the complete issue
|
* rule run before the RPC, so the editor gets the complete issue list. The
|
||||||
* list. The database re-checks the referential rules on its own
|
* rules run in the Rust core (R2): the caller passes its `sheet.check`
|
||||||
|
* operation, so this package never loads the module itself. The gate fails
|
||||||
|
* closed: anything but a publishable verdict refuses publication. The
|
||||||
|
* database re-checks the referential rules on its own
|
||||||
* (private.sheet_document_issues); this call never replaces that guard.
|
* (private.sheet_document_issues); this call never replaces that guard.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
export interface SheetIssue {
|
||||||
|
code: string
|
||||||
|
severity: 'error' | 'warning'
|
||||||
|
path: string
|
||||||
|
message: string
|
||||||
|
}
|
||||||
|
|
||||||
|
/** The core's verdict on a document (`sheet.check`), or why it could not give one. */
|
||||||
|
export type SheetCheckOutcome =
|
||||||
|
| { ok: true; value: { issues: SheetIssue[]; publishable: boolean } }
|
||||||
|
| { ok: false; kind: string; detail?: string }
|
||||||
|
|
||||||
|
export type SheetValidator = (content: unknown) => SheetCheckOutcome
|
||||||
|
|
||||||
|
export type SheetPublishErrorKind =
|
||||||
|
/** The document does not match the strict schema. */
|
||||||
|
| 'invalid_shape'
|
||||||
|
/** Integrity errors (warnings never block). */
|
||||||
|
| 'not_publishable'
|
||||||
|
/** The validator could not run (core unavailable, trap): publication refused. */
|
||||||
|
| 'validator_unavailable'
|
||||||
|
|
||||||
|
export class SheetPublishError extends Error {
|
||||||
|
constructor(
|
||||||
|
readonly kind: SheetPublishErrorKind,
|
||||||
|
readonly issues: SheetIssue[] = [],
|
||||||
|
detail = '',
|
||||||
|
) {
|
||||||
|
super(SheetPublishError.messageOf(kind, issues, detail))
|
||||||
|
this.name = 'SheetPublishError'
|
||||||
|
}
|
||||||
|
|
||||||
|
private static messageOf(
|
||||||
|
kind: SheetPublishErrorKind,
|
||||||
|
issues: SheetIssue[],
|
||||||
|
detail: string,
|
||||||
|
): string {
|
||||||
|
if (kind === 'not_publishable') {
|
||||||
|
const errors = issues.filter((issue) => issue.severity === 'error')
|
||||||
|
return (
|
||||||
|
`Sheet document is not publishable: ${errors.length} error(s), ` +
|
||||||
|
`first: ${errors[0]?.code ?? 'none'}`
|
||||||
|
)
|
||||||
|
}
|
||||||
|
if (kind === 'invalid_shape') {
|
||||||
|
return `Sheet document does not match the schema${detail ? `: ${detail}` : ''}`
|
||||||
|
}
|
||||||
|
return `Sheet validator unavailable${detail ? ` (${detail})` : ''}`
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Throws unless the validator says the document is publishable. */
|
||||||
|
function assertPublishable(validate: SheetValidator, content: unknown): void {
|
||||||
|
const check = validate(content)
|
||||||
|
if (!check.ok) {
|
||||||
|
throw check.kind === 'invalid_shape'
|
||||||
|
? new SheetPublishError('invalid_shape', [], check.detail)
|
||||||
|
: new SheetPublishError('validator_unavailable', [], check.kind)
|
||||||
|
}
|
||||||
|
if (!check.value.publishable) {
|
||||||
|
throw new SheetPublishError('not_publishable', check.value.issues)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export async function publishSheetVersion(
|
export async function publishSheetVersion(
|
||||||
client: FaunosteoClient,
|
client: FaunosteoClient,
|
||||||
versionId: string,
|
versionId: string,
|
||||||
|
validate: SheetValidator,
|
||||||
): Promise<string> {
|
): Promise<string> {
|
||||||
const draft = await client
|
const draft = await client
|
||||||
.from('anatomical_sheet_versions')
|
.from('anatomical_sheet_versions')
|
||||||
@@ -21,7 +86,7 @@ export async function publishSheetVersion(
|
|||||||
.single()
|
.single()
|
||||||
if (draft.error) throw draft.error
|
if (draft.error) throw draft.error
|
||||||
|
|
||||||
assertPublishable(AnatomicalSheetDocumentSchema.parse(draft.data.content))
|
assertPublishable(validate, draft.data.content)
|
||||||
|
|
||||||
const published = await client.rpc('publish_sheet_version', {
|
const published = await client.rpc('publish_sheet_version', {
|
||||||
version_id: versionId,
|
version_id: versionId,
|
||||||
|
|||||||
@@ -43,7 +43,14 @@ export {
|
|||||||
} from './profiles'
|
} from './profiles'
|
||||||
/** owner | admin | moderator | support. Not a professional status. */
|
/** owner | admin | moderator | support. Not a professional status. */
|
||||||
export { fetchOwnPlatformRoles, type PlatformRole } from './platform-roles'
|
export { fetchOwnPlatformRoles, type PlatformRole } from './platform-roles'
|
||||||
export { publishSheetVersion } from './anatomical-sheets'
|
export {
|
||||||
|
publishSheetVersion,
|
||||||
|
SheetPublishError,
|
||||||
|
type SheetCheckOutcome,
|
||||||
|
type SheetIssue,
|
||||||
|
type SheetPublishErrorKind,
|
||||||
|
type SheetValidator,
|
||||||
|
} from './anatomical-sheets'
|
||||||
export {
|
export {
|
||||||
ASSET_FAILURE_KINDS,
|
ASSET_FAILURE_KINDS,
|
||||||
ASSET_OWNER_COLUMNS,
|
ASSET_OWNER_COLUMNS,
|
||||||
|
|||||||
@@ -1,5 +1,15 @@
|
|||||||
import { publishSheetVersion } from '../src'
|
import { nodeCoreRuntime } from '@faunosteo/core-wasm/node'
|
||||||
import type { FaunosteoClient } from '../src'
|
import type { CoreRuntime } from '@faunosteo/core-wasm'
|
||||||
|
import { publishSheetVersion, SheetPublishError } from '../src'
|
||||||
|
import type { FaunosteoClient, SheetCheckOutcome, SheetValidator } from '../src'
|
||||||
|
|
||||||
|
/** The real core's `sheet.check` (R2): the gate is never tested against a stub verdict. */
|
||||||
|
let core: CoreRuntime
|
||||||
|
beforeAll(async () => {
|
||||||
|
core = await nodeCoreRuntime()
|
||||||
|
})
|
||||||
|
const validate: SheetValidator = (content) =>
|
||||||
|
core.call('sheet.check', JSON.stringify(content)) as SheetCheckOutcome
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Stub of the two client calls publishSheetVersion makes. The real RPC is
|
* Stub of the two client calls publishSheetVersion makes. The real RPC is
|
||||||
@@ -48,9 +58,21 @@ const publishable = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
describe('publishSheetVersion', () => {
|
describe('publishSheetVersion', () => {
|
||||||
|
it('refuses publication when the validator cannot run (fail closed)', async () => {
|
||||||
|
const { client, rpcCalls } = stubClient(publishable)
|
||||||
|
const unavailable: SheetValidator = () => ({
|
||||||
|
ok: false,
|
||||||
|
kind: 'core_unavailable',
|
||||||
|
})
|
||||||
|
await expect(
|
||||||
|
publishSheetVersion(client, 'v1', unavailable),
|
||||||
|
).rejects.toMatchObject({ kind: 'validator_unavailable' })
|
||||||
|
expect(rpcCalls).toEqual([])
|
||||||
|
})
|
||||||
|
|
||||||
it('calls the RPC once the document passes shape and integrity checks', async () => {
|
it('calls the RPC once the document passes shape and integrity checks', async () => {
|
||||||
const { client, rpcCalls } = stubClient(publishable)
|
const { client, rpcCalls } = stubClient(publishable)
|
||||||
await expect(publishSheetVersion(client, 'v1')).resolves.toBe(
|
await expect(publishSheetVersion(client, 'v1', validate)).resolves.toBe(
|
||||||
'published-id',
|
'published-id',
|
||||||
)
|
)
|
||||||
expect(rpcCalls).toEqual([['publish_sheet_version', { version_id: 'v1' }]])
|
expect(rpcCalls).toEqual([['publish_sheet_version', { version_id: 'v1' }]])
|
||||||
@@ -61,7 +83,7 @@ describe('publishSheetVersion', () => {
|
|||||||
...publishable,
|
...publishable,
|
||||||
views: [{ ...publishable.views[0], svg_asset_id: null }],
|
views: [{ ...publishable.views[0], svg_asset_id: null }],
|
||||||
})
|
})
|
||||||
await expect(publishSheetVersion(client, 'v1')).rejects.toThrow(
|
await expect(publishSheetVersion(client, 'v1', validate)).rejects.toThrow(
|
||||||
/view\.asset\.missing/,
|
/view\.asset\.missing/,
|
||||||
)
|
)
|
||||||
expect(rpcCalls).toEqual([])
|
expect(rpcCalls).toEqual([])
|
||||||
@@ -69,7 +91,12 @@ describe('publishSheetVersion', () => {
|
|||||||
|
|
||||||
it('refuses a document that does not match the schema', async () => {
|
it('refuses a document that does not match the schema', async () => {
|
||||||
const { client, rpcCalls } = stubClient({ schema_version: 1 })
|
const { client, rpcCalls } = stubClient({ schema_version: 1 })
|
||||||
await expect(publishSheetVersion(client, 'v1')).rejects.toThrow()
|
await expect(
|
||||||
|
publishSheetVersion(client, 'v1', validate),
|
||||||
|
).rejects.toBeInstanceOf(SheetPublishError)
|
||||||
|
await expect(
|
||||||
|
publishSheetVersion(client, 'v1', validate),
|
||||||
|
).rejects.toMatchObject({ kind: 'invalid_shape' })
|
||||||
expect(rpcCalls).toEqual([])
|
expect(rpcCalls).toEqual([])
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
@@ -5,6 +5,8 @@ export default defineConfig({
|
|||||||
environment: 'node',
|
environment: 'node',
|
||||||
globals: true,
|
globals: true,
|
||||||
include: ['tests/**/*.test.ts'],
|
include: ['tests/**/*.test.ts'],
|
||||||
|
// The publish gate runs on the real core: refuse a missing or stale module.
|
||||||
|
globalSetup: ['../core-wasm/tests/global-setup.ts'],
|
||||||
// PGlite boots a WASM Postgres per file: give it room on slow CI runners.
|
// PGlite boots a WASM Postgres per file: give it room on slow CI runners.
|
||||||
testTimeout: 60_000,
|
testTimeout: 60_000,
|
||||||
hookTimeout: 120_000,
|
hookTimeout: 120_000,
|
||||||
|
|||||||
Generated
+3
-3
@@ -244,9 +244,6 @@ importers:
|
|||||||
|
|
||||||
packages/supabase:
|
packages/supabase:
|
||||||
dependencies:
|
dependencies:
|
||||||
'@faunosteo/domain':
|
|
||||||
specifier: workspace:*
|
|
||||||
version: link:../domain
|
|
||||||
'@supabase/supabase-js':
|
'@supabase/supabase-js':
|
||||||
specifier: ^2.100.0
|
specifier: ^2.100.0
|
||||||
version: 2.117.3
|
version: 2.117.3
|
||||||
@@ -257,6 +254,9 @@ importers:
|
|||||||
'@electric-sql/pglite':
|
'@electric-sql/pglite':
|
||||||
specifier: ^0.3.0
|
specifier: ^0.3.0
|
||||||
version: 0.3.16
|
version: 0.3.16
|
||||||
|
'@faunosteo/core-wasm':
|
||||||
|
specifier: workspace:*
|
||||||
|
version: link:../core-wasm
|
||||||
'@types/node':
|
'@types/node':
|
||||||
specifier: ^20.19.41
|
specifier: ^20.19.41
|
||||||
version: 20.19.41
|
version: 20.19.41
|
||||||
|
|||||||
Reference in new issue
Block a user