diff --git a/packages/core/src/access.test.ts b/packages/core/src/access.test.ts new file mode 100644 index 0000000..fedad88 --- /dev/null +++ b/packages/core/src/access.test.ts @@ -0,0 +1,124 @@ +import { describe, expect, test } from 'vitest'; +import { describeAccessStore } from '../test/access-contract.ts'; +import { + allowance, + assertAllowance, + createApiKey, + dayWindow, + findApiKey, + hashIp, + hashKey, + KEY_SCANS_PER_DAY, + KEYS_PER_IP_PER_DAY, + keySubject, + LimitError, + MemoryAccessStore, + visitorSubject, +} from './access.ts'; + +describeAccessStore('MemoryAccessStore', async () => new MemoryAccessStore()); + +// A clock the tests move by hand. +function clock(start: string) { + let now = new Date(start); + return { + now: () => now, + set: (iso: string) => { + now = new Date(iso); + }, + }; +} + +describe('API keys', () => { + test('a new key is sp_ and 43 url-safe characters, stored only hashed', async () => { + const store = new MemoryAccessStore(); + const { key, record } = await createApiKey('ip-1', { store }); + expect(key).toMatch(/^sp_[A-Za-z0-9_-]{43}$/); + expect(record.prefix).toBe(key.slice(0, 10)); + expect(record.scansPerDay).toBe(KEY_SCANS_PER_DAY); + expect(JSON.stringify(record)).not.toContain(key.slice(10)); + expect(await store.findKey(await hashKey(key))).toEqual(record); + }); + + test('a key is found by itself, and not once revoked', async () => { + const store = new MemoryAccessStore(); + const { key, record } = await createApiKey('ip-1', { store }); + expect(await findApiKey(key, { store })).toEqual(record); + expect(await findApiKey(`${key}x`, { store })).toBeNull(); + expect(await findApiKey('not-a-key', { store })).toBeNull(); + await store.revokeKey(record.id); + expect(await findApiKey(key, { store })).toBeNull(); + }); + + test(`at most ${KEYS_PER_IP_PER_DAY} keys a day from one IP; the next UTC day allows more`, async () => { + const time = clock('2026-10-12T22:00:00.000Z'); + const store = new MemoryAccessStore({ now: time.now }); + const deps = { store, now: time.now }; + for (let i = 0; i < KEYS_PER_IP_PER_DAY; i++) await createApiKey('ip-1', deps); + await expect(createApiKey('ip-1', deps)).rejects.toThrow(LimitError); + await expect(createApiKey('ip-1', deps)).rejects.toMatchObject({ + resetAt: '2026-10-13T00:00:00.000Z', + }); + await expect(createApiKey('ip-2', deps)).resolves.toBeDefined(); + time.set('2026-10-13T00:00:01.000Z'); + await expect(createApiKey('ip-1', deps)).resolves.toBeDefined(); + }); + + test('making a key drops usage older than 30 days', async () => { + const time = clock('2026-09-01T12:00:00.000Z'); + const store = new MemoryAccessStore({ now: time.now }); + await store.recordUsage({ subject: 'key:old', endpoint: 'x', newScan: true }); + time.set('2026-10-12T12:00:00.000Z'); + await store.recordUsage({ subject: 'key:new', endpoint: 'x', newScan: true }); + await createApiKey('ip-1', { store, now: time.now }); + expect(await store.countNewScans('key:old', '2000-01-01T00:00:00.000Z')).toBe(0); + expect(await store.countNewScans('key:new', '2000-01-01T00:00:00.000Z')).toBe(1); + }); +}); + +describe('daily allowance', () => { + test('counts new scans today, ignores reused ones, and resets at UTC midnight', async () => { + const time = clock('2026-10-12T09:00:00.000Z'); + const store = new MemoryAccessStore({ now: time.now }); + const deps = { store, now: time.now }; + const subject = 'key:k1' as const; + await store.recordUsage({ subject, endpoint: 'POST /api/scans', newScan: true }); + await store.recordUsage({ subject, endpoint: 'POST /api/scans', newScan: false }); + expect(await allowance(subject, 2, deps)).toEqual({ + limit: 2, + used: 1, + remaining: 1, + resetAt: '2026-10-13T00:00:00.000Z', + }); + await store.recordUsage({ subject, endpoint: 'POST /api/scans', newScan: true }); + await expect(assertAllowance(subject, 2, deps)).rejects.toThrow( + 'Daily limit of 2 new scans reached. Recent scans are still free to read. Resets at 2026-10-13T00:00:00.000Z.', + ); + time.set('2026-10-13T00:00:00.000Z'); + await expect(assertAllowance(subject, 2, deps)).resolves.toMatchObject({ remaining: 2 }); + }); + + test('dayWindow is the UTC day around a moment', () => { + expect(dayWindow(new Date('2026-10-12T23:59:59.999Z'))).toEqual({ + start: '2026-10-12T00:00:00.000Z', + resetAt: '2026-10-13T00:00:00.000Z', + }); + }); +}); + +describe('subjects', () => { + test('a key spends as key:', () => { + expect( + keySubject({ id: 'k1', prefix: 'sp_x', scansPerDay: 10, createdAt: '', revokedAt: null }), + ).toBe('key:k1'); + }); + + test('a visitor is a keyed hash of the IP: stable, secret-dependent, never the IP', async () => { + const a = await visitorSubject('203.0.113.7', 'secret-1'); + expect(a).toMatch(/^visitor:[0-9a-f]{64}$/); + expect(a).not.toContain('203.0.113.7'); + expect(await visitorSubject('203.0.113.7', 'secret-1')).toBe(a); + expect(await visitorSubject('203.0.113.7', 'secret-2')).not.toBe(a); + expect(await hashIp('203.0.113.8', 'secret-1')).not.toBe(a.slice('visitor:'.length)); + }); +}); diff --git a/packages/core/src/access.ts b/packages/core/src/access.ts new file mode 100644 index 0000000..07af640 --- /dev/null +++ b/packages/core/src/access.ts @@ -0,0 +1,242 @@ +// API keys and the daily allowance of new scans. One allowance mechanism for every caller: an +// API key, or an anonymous website visitor (known only by a keyed hash of their IP). Reading +// reports is never limited, and returning a recent scan doesn't spend anything. +// Local tools (CLI, MCP over stdio) are never gated and don't use this. + +const DAY_MS = 24 * 60 * 60 * 1000; + +export const KEY_PREFIX = 'sp_'; +// How many characters of a key are kept in clear, to tell keys apart in logs and lists. +export const KEY_PREFIX_LENGTH = 10; +export const KEY_SCANS_PER_DAY = 10; +export const VISITOR_SCANS_PER_DAY = 3; +export const KEYS_PER_IP_PER_DAY = 3; +export const USAGE_KEPT_DAYS = 30; + +export interface ApiKeyRecord { + id: string; + // The first characters of the key, e.g. "sp_k3J9x2a". The rest is only ever stored hashed. + prefix: string; + scansPerDay: number; + createdAt: string; + revokedAt: string | null; +} + +// Who is spending the allowance: an API key, or an anonymous visitor's hashed IP. +export type Subject = `key:${string}` | `visitor:${string}`; + +export interface UsageEvent { + subject: Subject; + endpoint: string; + // True when the request started a new scan (and so spent the allowance). + newScan: boolean; +} + +// Where keys and usage are kept. Implementations: MemoryAccessStore here, Postgres in its own +// package. Times are ISO strings; `since`/`before` are inclusive/exclusive bounds. +export interface AccessStore { + createKey(input: { + hash: string; + prefix: string; + ipHash: string; + scansPerDay: number; + }): Promise; + // The key with this hash, revoked or not; null when there's none. + findKey(hash: string): Promise; + revokeKey(id: string): Promise; + countKeysCreated(ipHash: string, since: string): Promise; + recordUsage(event: UsageEvent): Promise; + countNewScans(subject: Subject, since: string): Promise; + // Deletes usage older than `before`; returns how many rows went. + pruneUsage(before: string): Promise; +} + +// A limit was reached. `resetAt` is when the caller may try again. +export class LimitError extends Error { + readonly resetAt: string; + + constructor(message: string, resetAt: string) { + super(message); + this.name = 'LimitError'; + this.resetAt = resetAt; + } +} + +export interface AccessDeps { + store: AccessStore; + now?: () => Date; +} + +// Days run on UTC: an allowance resets at the next UTC midnight, the same moment for everyone. +export function dayWindow(now: Date): { start: string; resetAt: string } { + const start = Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate()); + return { start: new Date(start).toISOString(), resetAt: new Date(start + DAY_MS).toISOString() }; +} + +// Creates a key for a caller, at most KEYS_PER_IP_PER_DAY a day per IP. Returns the key itself: +// the only time it exists outside the caller's hands. +export async function createApiKey( + ipHash: string, + deps: AccessDeps, +): Promise<{ key: string; record: ApiKeyRecord }> { + const now = deps.now?.() ?? new Date(); + const { start, resetAt } = dayWindow(now); + if ((await deps.store.countKeysCreated(ipHash, start)) >= KEYS_PER_IP_PER_DAY) { + throw new LimitError( + `At most ${KEYS_PER_IP_PER_DAY} new keys a day from one address. Try again after ${resetAt}.`, + resetAt, + ); + } + const key = KEY_PREFIX + base64url(crypto.getRandomValues(new Uint8Array(32))); + const record = await deps.store.createKey({ + hash: await hashKey(key), + prefix: key.slice(0, KEY_PREFIX_LENGTH), + ipHash, + scansPerDay: KEY_SCANS_PER_DAY, + }); + // Old usage goes when keys are made: often enough, and nothing else to schedule. + await deps.store.pruneUsage(new Date(now.getTime() - USAGE_KEPT_DAYS * DAY_MS).toISOString()); + return { key, record }; +} + +// The live key for a key string, or null when it's unknown, malformed or revoked. +export async function findApiKey(key: string, deps: AccessDeps): Promise { + if (!key.startsWith(KEY_PREFIX) || key.length > 100) return null; + const record = await deps.store.findKey(await hashKey(key)); + return record && !record.revokedAt ? record : null; +} + +export interface Allowance { + limit: number; + used: number; + remaining: number; + resetAt: string; +} + +// How much of today's allowance a subject has left. +export async function allowance( + subject: Subject, + limit: number, + deps: AccessDeps, +): Promise { + const { start, resetAt } = dayWindow(deps.now?.() ?? new Date()); + const used = await deps.store.countNewScans(subject, start); + return { limit, used, remaining: Math.max(0, limit - used), resetAt }; +} + +// Throws LimitError when the subject has no new scans left today. +export async function assertAllowance( + subject: Subject, + limit: number, + deps: AccessDeps, +): Promise { + const left = await allowance(subject, limit, deps); + if (left.remaining === 0) { + throw new LimitError( + `Daily limit of ${limit} new scans reached. Recent scans are still free to read. ` + + `Resets at ${left.resetAt}.`, + left.resetAt, + ); + } + return left; +} + +export function keySubject(record: ApiKeyRecord): Subject { + return `key:${record.id}`; +} + +// A visitor is a keyed hash of their IP: enough to count, never the address itself. +export async function visitorSubject(ip: string, secret: string): Promise { + return `visitor:${await hashIp(ip, secret)}`; +} + +export async function hashIp(ip: string, secret: string): Promise { + const key = await crypto.subtle.importKey( + 'raw', + new TextEncoder().encode(secret), + { name: 'HMAC', hash: 'SHA-256' }, + false, + ['sign'], + ); + return hex(await crypto.subtle.sign('HMAC', key, new TextEncoder().encode(ip))); +} + +export async function hashKey(key: string): Promise { + return hex(await crypto.subtle.digest('SHA-256', new TextEncoder().encode(key))); +} + +function hex(buffer: ArrayBuffer): string { + return [...new Uint8Array(buffer)].map((b) => b.toString(16).padStart(2, '0')).join(''); +} + +function base64url(bytes: Uint8Array): string { + return btoa(String.fromCharCode(...bytes)) + .replace(/\+/g, '-') + .replace(/\//g, '_') + .replace(/=+$/, ''); +} + +// Keeps everything in memory. For tests. +export class MemoryAccessStore implements AccessStore { + private readonly keys = new Map(); + private usage: (UsageEvent & { at: string })[] = []; + private readonly now: () => Date; + + constructor(options: { now?: () => Date } = {}) { + this.now = options.now ?? (() => new Date()); + } + + async createKey(input: { + hash: string; + prefix: string; + ipHash: string; + scansPerDay: number; + }): Promise { + const record = { + id: crypto.randomUUID(), + prefix: input.prefix, + scansPerDay: input.scansPerDay, + createdAt: this.now().toISOString(), + revokedAt: null, + hash: input.hash, + ipHash: input.ipHash, + }; + this.keys.set(record.id, record); + return publicKey(record); + } + + async findKey(hash: string): Promise { + const found = [...this.keys.values()].find((k) => k.hash === hash); + return found ? publicKey(found) : null; + } + + async revokeKey(id: string): Promise { + const found = this.keys.get(id); + if (found && !found.revokedAt) found.revokedAt = this.now().toISOString(); + } + + async countKeysCreated(ipHash: string, since: string): Promise { + return [...this.keys.values()].filter((k) => k.ipHash === ipHash && k.createdAt >= since) + .length; + } + + async recordUsage(event: UsageEvent): Promise { + this.usage.push({ ...event, at: this.now().toISOString() }); + } + + async countNewScans(subject: Subject, since: string): Promise { + return this.usage.filter((u) => u.subject === subject && u.newScan && u.at >= since).length; + } + + async pruneUsage(before: string): Promise { + const kept = this.usage.filter((u) => u.at >= before); + const removed = this.usage.length - kept.length; + this.usage = kept; + return removed; + } +} + +function publicKey(record: ApiKeyRecord): ApiKeyRecord { + const { id, prefix, scansPerDay, createdAt, revokedAt } = record; + return { id, prefix, scansPerDay, createdAt, revokedAt }; +} diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index b0fcf9c..fa8fe2e 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -1,3 +1,30 @@ +export type { + AccessDeps, + AccessStore, + Allowance, + ApiKeyRecord, + Subject, + UsageEvent, +} from './access.ts'; +export { + allowance, + assertAllowance, + createApiKey, + dayWindow, + findApiKey, + hashIp, + hashKey, + KEY_PREFIX, + KEY_PREFIX_LENGTH, + KEY_SCANS_PER_DAY, + KEYS_PER_IP_PER_DAY, + keySubject, + LimitError, + MemoryAccessStore, + USAGE_KEPT_DAYS, + VISITOR_SCANS_PER_DAY, + visitorSubject, +} from './access.ts'; export type { BatchBrowser, Browser, BrowserCapture, CaptureOptions } from './browser.ts'; export { browserLayer, diff --git a/packages/core/test/access-contract.ts b/packages/core/test/access-contract.ts new file mode 100644 index 0000000..f045be6 --- /dev/null +++ b/packages/core/test/access-contract.ts @@ -0,0 +1,71 @@ +import { describe, expect, test } from 'vitest'; +import type { AccessStore } from '../src/access.ts'; + +// The behaviour every AccessStore must have. Each implementation runs this suite. Times come +// from the store's own clock, so bounds here are far in the past or future. +export function describeAccessStore(name: string, makeStore: () => Promise) { + const past = '2000-01-01T00:00:00.000Z'; + const future = '2100-01-01T00:00:00.000Z'; + const key = (hash: string, ipHash = 'ip-a') => ({ + hash, + prefix: `sp_${hash.slice(0, 7)}`, + ipHash, + scansPerDay: 10, + }); + + describe(`${name} (AccessStore)`, () => { + test('creates a key and finds it by its hash, without the hash or IP', async () => { + const store = await makeStore(); + const created = await store.createKey(key('hash-1')); + expect(created).toEqual({ + id: expect.any(String), + prefix: 'sp_hash-1', + scansPerDay: 10, + createdAt: expect.any(String), + revokedAt: null, + }); + expect(await store.findKey('hash-1')).toEqual(created); + expect(await store.findKey('no-such-hash')).toBeNull(); + }); + + test('revoking marks the key, and revoking twice keeps the first time', async () => { + const store = await makeStore(); + const created = await store.createKey(key('hash-2')); + await store.revokeKey(created.id); + const revoked = await store.findKey('hash-2'); + expect(revoked?.revokedAt).toEqual(expect.any(String)); + await store.revokeKey(created.id); + expect((await store.findKey('hash-2'))?.revokedAt).toBe(revoked?.revokedAt); + }); + + test('counts keys created from one IP since a time', async () => { + const store = await makeStore(); + await store.createKey(key('hash-3', 'ip-b')); + await store.createKey(key('hash-4', 'ip-b')); + await store.createKey(key('hash-5', 'ip-c')); + expect(await store.countKeysCreated('ip-b', past)).toBe(2); + expect(await store.countKeysCreated('ip-b', future)).toBe(0); + expect(await store.countKeysCreated('ip-z', past)).toBe(0); + }); + + test('counts only new scans, per subject and since a time', async () => { + const store = await makeStore(); + await store.recordUsage({ subject: 'key:1', endpoint: 'POST /api/scans', newScan: true }); + await store.recordUsage({ subject: 'key:1', endpoint: 'POST /api/scans', newScan: false }); + await store.recordUsage({ subject: 'key:1', endpoint: 'POST /api/scans', newScan: true }); + await store.recordUsage({ subject: 'visitor:x', endpoint: 'scan form', newScan: true }); + expect(await store.countNewScans('key:1', past)).toBe(2); + expect(await store.countNewScans('visitor:x', past)).toBe(1); + expect(await store.countNewScans('key:1', future)).toBe(0); + }); + + test('prunes usage older than a time', async () => { + const store = await makeStore(); + await store.recordUsage({ subject: 'key:2', endpoint: 'x', newScan: true }); + expect(await store.pruneUsage(past)).toBe(0); + expect(await store.countNewScans('key:2', past)).toBe(1); + expect(await store.pruneUsage(future)).toBe(1); + expect(await store.countNewScans('key:2', past)).toBe(0); + }); + }); +} diff --git a/packages/store-postgres/migrations/0003_access.sql b/packages/store-postgres/migrations/0003_access.sql new file mode 100644 index 0000000..9ed04cd --- /dev/null +++ b/packages/store-postgres/migrations/0003_access.sql @@ -0,0 +1,30 @@ +-- API keys. Only a SHA-256 of each key is stored, plus its first characters to tell keys +-- apart; the IP that made it is kept as a keyed hash, never in the clear. +create table stackprobe.api_keys ( + id uuid primary key default gen_random_uuid(), + hash text not null unique, + prefix text not null, + ip_hash text not null, + scans_per_day integer not null, + created_at timestamptz not null default now(), + revoked_at timestamptz +); + +create index api_keys_ip_created_at on stackprobe.api_keys (ip_hash, created_at); + +-- One row per request made with a key, or per scan a website visitor starts. Subjects are +-- "key:" or "visitor:". Rows older than 30 days are deleted. +create table stackprobe.usage ( + id bigint generated always as identity primary key, + subject text not null, + endpoint text not null, + new_scan boolean not null, + at timestamptz not null default now() +); + +create index usage_new_scans on stackprobe.usage (subject, at) where new_scan; +create index usage_at on stackprobe.usage (at); + +-- No policies, like the other tables: only the server can read or write. +alter table stackprobe.api_keys enable row level security; +alter table stackprobe.usage enable row level security; diff --git a/packages/store-postgres/src/access-store.ts b/packages/store-postgres/src/access-store.ts new file mode 100644 index 0000000..5d3109c --- /dev/null +++ b/packages/store-postgres/src/access-store.ts @@ -0,0 +1,92 @@ +import type { AccessStore, ApiKeyRecord, Subject, UsageEvent } from '@drippa/stackprobe-core'; +import type { Sql } from './sql.ts'; + +interface KeyRow { + id: string; + prefix: string; + scans_per_day: number; + created_at: Date | string; + revoked_at: Date | string | null; +} + +const KEY_COLUMNS = 'id, prefix, scans_per_day, created_at, revoked_at'; + +// Keeps API keys and usage in Postgres, in the tables of migration 0003. +export class PostgresAccessStore implements AccessStore { + private readonly sql: Sql; + + constructor(sql: Sql) { + this.sql = sql; + } + + async createKey(input: { + hash: string; + prefix: string; + ipHash: string; + scansPerDay: number; + }): Promise { + const [row] = await this.sql.query( + `insert into stackprobe.api_keys (hash, prefix, ip_hash, scans_per_day) + values ($1, $2, $3, $4) returning ${KEY_COLUMNS}`, + [input.hash, input.prefix, input.ipHash, input.scansPerDay], + ); + return toKey(row as KeyRow); + } + + async findKey(hash: string): Promise { + const [row] = await this.sql.query( + `select ${KEY_COLUMNS} from stackprobe.api_keys where hash = $1`, + [hash], + ); + return row ? toKey(row) : null; + } + + async revokeKey(id: string): Promise { + await this.sql.query( + 'update stackprobe.api_keys set revoked_at = now() where id = $1 and revoked_at is null', + [id], + ); + } + + async countKeysCreated(ipHash: string, since: string): Promise { + const [row] = await this.sql.query<{ count: string | number }>( + 'select count(*) as count from stackprobe.api_keys where ip_hash = $1 and created_at >= $2', + [ipHash, since], + ); + return Number(row?.count ?? 0); + } + + async recordUsage(event: UsageEvent): Promise { + await this.sql.query( + 'insert into stackprobe.usage (subject, endpoint, new_scan) values ($1, $2, $3)', + [event.subject, event.endpoint, event.newScan], + ); + } + + async countNewScans(subject: Subject, since: string): Promise { + const [row] = await this.sql.query<{ count: string | number }>( + `select count(*) as count from stackprobe.usage + where subject = $1 and new_scan and at >= $2`, + [subject, since], + ); + return Number(row?.count ?? 0); + } + + async pruneUsage(before: string): Promise { + const rows = await this.sql.query<{ id: string }>( + 'delete from stackprobe.usage where at < $1 returning id', + [before], + ); + return rows.length; + } +} + +function toKey(row: KeyRow): ApiKeyRecord { + return { + id: row.id, + prefix: row.prefix, + scansPerDay: row.scans_per_day, + createdAt: new Date(row.created_at).toISOString(), + revokedAt: row.revoked_at ? new Date(row.revoked_at).toISOString() : null, + }; +} diff --git a/packages/store-postgres/src/index.ts b/packages/store-postgres/src/index.ts index 9a86ac8..4e4f535 100644 --- a/packages/store-postgres/src/index.ts +++ b/packages/store-postgres/src/index.ts @@ -1,14 +1,25 @@ import postgres from 'postgres'; +import { PostgresAccessStore } from './access-store.ts'; import { fromPostgresJs } from './sql.ts'; import { PostgresStore } from './store.ts'; +export { PostgresAccessStore } from './access-store.ts'; export type { Sql } from './sql.ts'; export { fromPostgresJs } from './sql.ts'; export { PostgresStore } from './store.ts'; // Connects to a Postgres URL. Works with Supabase's transaction pooler, which doesn't // support prepared statements, and with serverless functions (few connections each). -export function connectPostgres(url: string): { store: PostgresStore; close: () => Promise } { +export function connectPostgres(url: string): { + store: PostgresStore; + access: PostgresAccessStore; + close: () => Promise; +} { const client = postgres(url, { prepare: false, max: 5, idle_timeout: 20 }); - return { store: new PostgresStore(fromPostgresJs(client)), close: () => client.end() }; + const sql = fromPostgresJs(client); + return { + store: new PostgresStore(sql), + access: new PostgresAccessStore(sql), + close: () => client.end(), + }; } diff --git a/packages/store-postgres/src/store.test.ts b/packages/store-postgres/src/store.test.ts index 7c2425b..b5b7e69 100644 --- a/packages/store-postgres/src/store.test.ts +++ b/packages/store-postgres/src/store.test.ts @@ -1,7 +1,9 @@ import { readFileSync } from 'node:fs'; import { PGlite } from '@electric-sql/pglite'; import { beforeAll, describe, expect, test } from 'vitest'; +import { describeAccessStore } from '../../core/test/access-contract.ts'; import { describeStore } from '../../core/test/store-contract.ts'; +import { PostgresAccessStore } from './access-store.ts'; import { migrate } from './migrate.ts'; import type { Sql } from './sql.ts'; import { PostgresStore } from './store.ts'; @@ -34,11 +36,19 @@ beforeAll(async () => { }, 60_000); describeStore('PostgresStore', async () => new PostgresStore(await freshDatabase())); +describeAccessStore( + 'PostgresAccessStore', + async () => new PostgresAccessStore(await freshDatabase()), +); describe('migrate', () => { test('applies each migration once', async () => { const sql = fromPGlite(new PGlite()); - expect(await migrate(sql)).toEqual(['0001_scans.sql', '0002_unwrap_json_strings.sql']); + expect(await migrate(sql)).toEqual([ + '0001_scans.sql', + '0002_unwrap_json_strings.sql', + '0003_access.sql', + ]); expect(await migrate(sql)).toEqual([]); }); @@ -50,10 +60,12 @@ describe('migrate', () => { where n.nspname = 'stackprobe' and c.relkind = 'r' order by c.relname`, ); expect(tables).toEqual([ + { relname: 'api_keys', relrowsecurity: true }, { relname: 'detections', relrowsecurity: true }, { relname: 'layer_results', relrowsecurity: true }, { relname: 'migrations', relrowsecurity: true }, { relname: 'scans', relrowsecurity: true }, + { relname: 'usage', relrowsecurity: true }, ]); }); });