pi-ext bugabingas pi extensions
owned by admin
README Source History Refs Compare Notes Search fix(bak): use strong ETags and the real session directory d181cfb39439f0c2b2b5fd608fc17e1e218e9dd1
Oliver Krylow <o.krylow@isp-insoft.de > · 17 Sep 2026
SSH signature · not checked
Browse files Compare with HEAD
Strip weak ETag prefixes on read, write, and if-match, so conditional catalog writes work against stores that answer with weak validators.
Request identity encoding on GET, because a re-encoded body returns an ETag that no longer matches the stored object.
List the default session store when Pi reports no session directory, instead of scanning the working directory.
Add a real-bucket E2E suite behind the PI_BAK_S3_ environment that skips itself when unconfigured, plus an in-memory S3 unit test.
Changed files (9) extensions/bak/README.md Modified 46 46 `/bak backup` uploads all locally discoverable sessions.
47 47 Completed agent runs back up their current session asynchronously.
48 48 Restore defaults to the current configured session directory and never silently overwrites differing files.
52 + `mise run //extensions/bak:test` covers the archive logic against an in-memory store.
54 + `mise run //extensions/bak:e2e` drives the real bucket named by the `PI_BAK_S3_*` environment.
55 + It skips itself when those variables are missing.
56 + Every test host uses a throwaway `e2e-*` alias and deletes its objects and catalog entry afterwards.
extensions/bak/__e2e__/commands.test.ts Added new file mode 100644 1 + import { mkdir, mkdtemp, readdir, readFile, rm }
from "node:fs/promises" ;
2 + import { tmpdir }
from "node:os" ;
3 + import { join }
from "node:path" ;
4 + import type { SessionInfo }
from "@earendil-works/pi-coding-agent" ;
5 + import { afterAll, beforeAll, describe, expect, it }
from "vitest" ;
6 + import { BakArchive }
from "../archive.js" ;
7 + import bakExtension
from "../index.js" ;
8 + import { type S3Config, S3Store }
from "../s3.js" ;
9 + import { type HostIdentity, statePaths }
from "../state.js" ;
15 + }
from "./support.js" ;
17 + const config
= storageConfig;
24 + type CommandHandler = (args
: string , ctx
: unknown )
=> Promise <
void >
| void ;
25 + type EventHandler = (event
: unknown , ctx
: unknown )
=> Promise <
void >
| void ;
27 + const aliases
: string []
= [];
28 + let workspace
: string ;
29 + let sessionDir
: string ;
30 + let notes
: Note []
= [];
31 + let run : (args
: string )
=> Promise <
void >;
32 + let emit : (event
: string )
=> Promise <
void >;
33 + let context
: Record <
string ,
unknown >;
34 + let currentSession
: SessionInfo | undefined ;
40 + function loadExtension ()
: void {
41 + const handlers
= new Map <
string ,
EventHandler []>();
42 + let command
: { handler
: CommandHandler }
| undefined ;
44 + on (event
: string , handler
: EventHandler ) {
45 + handlers.
set (
event , [
... (handlers.
get (
event )
?? []), handler]);
47 + registerCommand (_name
: string , definition
: { handler
: CommandHandler }) {
48 + command
= definition;
51 + bakExtension (pi
as never );
52 + if (
! command)
throw new Error (
"bak command was not registered" );
53 + const registered
= command;
54 + run = async (args
: string )
=> {
55 + await registered.
handler (args, context);
57 + emit = async (event
: string )
=> {
58 + for (
const handler
of handlers.
get (
event )
?? [])
59 + await handler ({ type:
event }, context);
63 + function sessionContext ()
: Record <
string ,
unknown > {
67 + notify : (message
: string , level
= "info" )
=> {
68 + notes.
push ({ message, level });
70 + setStatus : ()
=> undefined ,
71 + theme: {
fg : (_style
: string , text
: string )
=> text },
74 + getSessionDir : ()
=> sessionDir,
75 + usesDefaultSessionDir : ()
=> false ,
76 + getSessionFile : ()
=> currentSession?.path,
80 + id: currentSession.
id ,
81 + cwd: currentSession.cwd,
82 + timestamp: currentSession.created.
toISOString (),
85 + getSessionName : ()
=> currentSession?.
name ,
86 + getEntries : ()
=> [],
91 + function lastNote ()
: Note {
92 + const note
= notes.
at (
- 1 );
93 + if (
! note)
throw new Error (
"no bak notification recorded" );
97 + async function archivedIds (alias
: string )
: Promise <
string []> {
98 + const cache
= await sandboxArchive ().
refresh ();
101 + .
find ((index)
=> index.alias
=== alias)
102 + ?.sessions.
map ((record)
=> record.
id )
?? []
106 + async function waitForArchived (alias
: string , id
: string )
: Promise <
void > {
107 + for (
let attempt
= 0 ; attempt
< 30 ; attempt
++ ) {
108 + if ((
await archivedIds (alias)).
includes (id))
return ;
109 + await new Promise ((resolve)
=> setTimeout (resolve,
500 ));
111 + throw new Error (
`session ${id} never reached the archive` );
114 + function sandboxArchive ()
: BakArchive {
115 + return new BakArchive (
116 + new S3Store (config
as S3Config ),
117 + statePaths (
process .
env ),
121 + describe.
skipIf (
! config)(
"bak commands against real S3 storage" , ()
=> {
123 + state:
process .
env .XDG_STATE_HOME,
124 + cache:
process .
env .XDG_CACHE_HOME,
127 + beforeAll (
async ()
=> {
128 + workspace
= await mkdtemp (
join (
tmpdir (),
"bak-cmd-e2e-" ));
129 + process .
env .XDG_STATE_HOME
= join (workspace,
"state" );
130 + process .
env .XDG_CACHE_HOME
= join (workspace,
"cache" );
131 + sessionDir
= join (workspace,
"sessions" );
132 + await mkdir (sessionDir, { recursive:
true });
133 + context
= sessionContext ();
137 + afterAll (
async ()
=> {
138 + if (config
&& aliases.
length > 0 ) {
139 + const catalog
= await new S3Store (config).
get (
"catalog/hosts.json" );
140 + const hosts
= catalog
142 + JSON .
parse (
new TextDecoder ().
decode (catalog.
body ))
as {
143 + hosts
: HostIdentity [];
150 + .
filter ((host)
=> aliases.
includes (host.alias))
151 + .
map ((host)
=> host.hostId),
154 + process .
env .XDG_STATE_HOME
= original.state;
155 + process .
env .XDG_CACHE_HOME
= original.cache;
156 + if (original.state
=== undefined )
delete process .
env .XDG_STATE_HOME;
157 + if (original.cache
=== undefined )
delete process .
env .XDG_CACHE_HOME;
158 + await rm (workspace, { recursive:
true , force:
true });
161 + it (
"reports an uninitialized host" ,
async ()
=> {
163 + await run (
"status" );
165 + expect (
lastNote ().message).
toContain (
"not initialized" );
166 + expect (
lastNote ().message).
toContain (
"0 pending" );
169 + it (
"initializes, backs up local sessions, and reports status" ,
async ()
=> {
170 + const alias
= testAlias ();
171 + aliases.
push (alias);
174 + await run (
`init ${alias}` );
175 + expect (
lastNote ().message).
toBe (
`bak: initialized as ${alias}` );
176 + const identity
= JSON .
parse (
177 + await readFile (
statePaths (
process .
env ).identity,
"utf8" ),
179 + expect (identity.alias).
toBe (alias);
181 + const first
= await writeSession (sessionDir, { name:
"command session" });
182 + const second
= await writeSession (sessionDir, { messages: [
"x" ,
"y" ] });
183 + await run (
"backup" );
184 + expect (
lastNote ().message).
toBe (
"bak: backed up 2; 2 indexed" );
186 + await run (
"status" );
187 + expect (
lastNote ().message).
toContain (alias);
188 + expect (
lastNote ().message).
toContain (
"ready" );
190 + expect (
lastNote ().message).
toMatch (
/[1-9]\d * indexed/ );
191 + expect (
lastNote ().message).
toContain (
"0 pending" );
192 + expect (
lastNote ().message).not.
toContain (
"error" );
194 + const cache
= await sandboxArchive ().
refresh ();
197 + .
find ((index)
=> index.alias
=== alias)
198 + ?.sessions.
map ((record)
=> record.
id )
?? [];
199 + expect (ids.
sort ()).
toEqual ([first.
id , second.
id ].
sort ());
202 + it (
"refreshes the catalog and restores by alias prefix" ,
async ()
=> {
203 + const alias
= aliases.
at (
- 1 )
as string ;
205 + await run (
"refresh" );
206 + expect (
lastNote ().message).
toMatch (
/refreshed \d + host\(s\)/ );
208 + const cache
= await sandboxArchive ().
refresh ();
209 + const record
= cache.indexes.
find ((index)
=> index.alias
=== alias)
211 + expect (record).
toBeDefined ();
212 + const target
= join (workspace,
"restore-target" );
214 + await run (
`restore ${alias}/${record?. id . slice (0, 8)} ${target}` );
215 + expect (
lastNote ().message).
toBe (
"bak: restored 1, skipped 0, conflicts 0" );
216 + expect (
await readdir (target)).
toHaveLength (
1 );
218 + await run (
`restore ${alias}/${record?. id . slice (0, 8)} ${target}` );
219 + expect (
lastNote ().message).
toBe (
"bak: restored 0, skipped 1, conflicts 0" );
222 + it (
"restores every archived session of the host" ,
async ()
=> {
223 + const alias
= aliases.
at (
- 1 )
as string ;
224 + const target
= join (workspace,
"restore-all" );
227 + await run (
`restore all ${target}` );
228 + expect (
lastNote ().message).
toMatch (
/restored \d + , skipped \d + / );
230 + const cache
= await sandboxArchive ().
refresh ();
232 + cache.indexes.
find ((index)
=> index.alias
=== alias)?.sessions.
length ?? 234 + expect ((
await readdir (target)).
length ).
toBeGreaterThanOrEqual (own);
237 + it (
"keeps working after session lifecycle events" ,
async ()
=> {
239 + await emit (
"session_start" );
240 + await writeSession (sessionDir, { messages: [
"after restart" ] });
241 + await run (
"backup" );
242 + expect (
lastNote ().message).
toBe (
"bak: backed up 3; 3 indexed" );
244 + await run (
"status" );
245 + expect (
lastNote ().message).
toMatch (
/[1-9]\d * indexed/ );
246 + expect (
lastNote ().message).
toContain (
"0 pending" );
247 + expect (
lastNote ().message).not.
toContain (
"error" );
250 + it (
"backs up the active session when the agent settles" ,
async ()
=> {
251 + const alias
= aliases.
at (
- 1 )
as string ;
253 + currentSession
= await writeSession (sessionDir, {
254 + messages: [
"settled" ],
255 + name:
"settled session" ,
258 + await emit (
"agent_settled" );
259 + await waitForArchived (alias, currentSession.
id );
261 + expect (notes.
filter ((note)
=> note.level
!== "info" )).
toEqual ([]);
262 + const markers
= await readdir (
statePaths (
process .
env ).pendingDir);
263 + expect (markers).
toEqual ([]);
266 + it (
"retains a pending retry after a failed backup and recovers" ,
async ()
=> {
267 + const alias
= aliases.
at (
- 1 )
as string ;
268 + const bucket
= process .
env .PI_BAK_S3_BUCKET;
270 + currentSession
= await writeSession (sessionDir, { messages: [
"pending" ] });
272 + process .
env .PI_BAK_S3_BUCKET
= `${(config as S3Config ).bucket}-missing` ;
274 + await run (
"backup" );
275 + expect (
lastNote ().level).
toBe (
"error" );
276 + expect (
await readdir (
statePaths (
process .
env ).pendingDir)).not.
toEqual ([]);
278 + process .
env .PI_BAK_S3_BUCKET
= bucket;
279 + if (bucket
=== undefined )
delete process .
env .PI_BAK_S3_BUCKET;
281 + await emit (
"session_start" );
282 + await emit (
"agent_settled" );
283 + await waitForArchived (alias, currentSession.
id );
285 + for (
let attempt
= 0 ; attempt
< 30 ; attempt
++ ) {
286 + if ((
await readdir (
statePaths (
process .
env ).pendingDir)).
length === 0 )
288 + await new Promise ((resolve)
=> setTimeout (resolve,
500 ));
290 + expect (
await readdir (
statePaths (
process .
env ).pendingDir)).
toEqual ([]);
293 + it (
"reports usage and initialization errors" ,
async ()
=> {
295 + await run (
"bogus" );
296 + expect (
lastNote ().level).
toBe (
"error" );
297 + expect (
lastNote ().message).
toContain (
"Usage:" );
299 + await run (
`init ${ testAlias ()}` );
300 + expect (
lastNote ().level).
toBe (
"error" );
301 + expect (
lastNote ().message).
toMatch (
/Already initialized as/ );
extensions/bak/__e2e__/r2.test.ts Added new file mode 100644 1 + import { mkdir, mkdtemp, readFile, rm, writeFile }
from "node:fs/promises" ;
2 + import { tmpdir }
from "node:os" ;
3 + import { dirname, join }
from "node:path" ;
4 + import { afterAll, beforeAll, describe, expect, it }
from "vitest" ;
5 + import { BakArchive, sessionInfoFromPath }
from "../archive.js" ;
6 + import { type S3Config, S3Store }
from "../s3.js" ;
7 + import type { StatePaths }
from "../state.js" ;
15 + }
from "./support.js" ;
17 + const config
= storageConfig;
18 + const hostIds
: string []
= [];
19 + let workspace
: string ;
21 + function statePathsIn (name
: string )
: StatePaths {
22 + const root
= join (workspace, name);
24 + identity:
join (
root ,
"identity.json" ),
25 + cache:
join (
root ,
"catalog.json" ),
26 + pendingDir:
join (
root ,
"pending" ),
30 + function newArchive (paths
: StatePaths )
: BakArchive {
31 + return new BakArchive (
new S3Store (config
as S3Config ), paths);
34 + async function initHost (name
: string , alias
: string ) {
35 + const paths
= statePathsIn (name);
36 + const archive
= newArchive (paths);
37 + const identity
= await archive.
init (alias);
38 + hostIds.
push (identity.hostId);
39 + return { archive, paths, identity };
42 + async function sessionDir (name
: string )
: Promise <
string > {
43 + const directory
= join (workspace, name);
44 + await mkdir (directory, { recursive:
true });
48 + describe.
skipIf (
! config)(
"bak archive against real S3 storage" , ()
=> {
49 + beforeAll (
async ()
=> {
50 + workspace
= await mkdtemp (
join (
tmpdir (),
"bak-e2e-" ));
53 + afterAll (
async ()
=> {
54 + if (config)
await removeHosts (config, hostIds);
55 + await rm (workspace, { recursive:
true , force:
true });
58 + it (
"registers a host and creates an empty index" ,
async ()
=> {
59 + const alias
= testAlias ();
60 + const { archive, identity }
= await initHost (
"alpha" , alias);
62 + expect (identity.alias).
toBe (alias);
63 + const cache
= await archive.
refresh ();
64 + const index
= cache.indexes.
find (
65 + (entry)
=> entry.hostId
=== identity.hostId,
67 + expect (cache.catalog.hosts).
toContainEqual (identity);
68 + expect (index?.sessions).
toEqual ([]);
71 + it (
"rejects a duplicate alias and rolls back the local identity" ,
async ()
=> {
72 + const alias
= testAlias ();
73 + const first
= await initHost (
"collide-a" , alias);
74 + const paths
= statePathsIn (
"collide-b" );
75 + const archive
= newArchive (paths);
77 + await expect (archive.
init (alias)).rejects.
toThrow (
/Alias already exists/ );
78 + await expect (
readFile (paths.identity,
"utf8" )).rejects.
toMatchObject ({
81 + expect (first.identity.alias).
toBe (alias);
84 + it (
"uploads sessions, indexes them, and stays idempotent" ,
async ()
=> {
85 + const { archive, identity }
= await initHost (
"backup" ,
testAlias ());
86 + const directory
= await sessionDir (
"backup-sessions" );
87 + const first
= await writeSession (directory, { name:
"first session" });
88 + const second
= await writeSession (directory, { messages: [
"a" ,
"b" ] });
90 + expect (
await archive.
backup ([first, second])).
toEqual ({
94 + expect (
await archive.
backup ([first, second])).
toEqual ({
99 + const cache
= await archive.
refresh ();
100 + const index
= cache.indexes.
find (
101 + (entry)
=> entry.hostId
=== identity.hostId,
103 + expect (index?.sessions).
toHaveLength (
2 );
104 + expect (index?.sessions.
find ((record)
=> record.
id === first.
id )?.
name ).
toBe (
107 + expect (index?.sessions.
map ((record)
=> record.
id ).
sort ()).
toEqual (
108 + [first.
id , second.
id ].
sort (),
112 + it (
"extends an appended session and rejects divergent rewrites" ,
async ()
=> {
113 + const { archive }
= await initHost (
"append" ,
testAlias ());
114 + const directory
= await sessionDir (
"append-sessions" );
115 + const info
= await writeSession (directory, { messages: [
"one" ] });
116 + await archive.
backup ([info]);
118 + const grown
= await appendMessage (info.path,
"two" );
119 + expect (
await archive.
backup ([grown])).
toEqual ({ uploaded:
1 , indexed:
1 });
123 + sessionBody (info.
id , info.created.
toISOString (), info.cwd, [
"other" ]),
125 + const diverged
= await sessionInfoFromPath (info.path);
126 + await expect (archive.
backup ([diverged])).rejects.
toThrow (
127 + /Archived session diverged/ ,
131 + it (
"restores, skips identical files, and reports conflicts" ,
async ()
=> {
132 + const { archive, identity }
= await initHost (
"restore" ,
testAlias ());
133 + const directory
= await sessionDir (
"restore-source" );
134 + const info
= await writeSession (directory, { messages: [
"restore me" ] });
135 + await archive.
backup ([info]);
136 + const cache
= await archive.
refresh ();
138 + cache.indexes.
find ((entry)
=> entry.hostId
=== identity.hostId)
140 + expect (records).
toHaveLength (
1 );
142 + const target
= join (workspace,
"restore-target" );
143 + expect (
await archive.
restore (records, target)).
toEqual ({
148 + expect (
await archive.
restore (records, target)).
toEqual ({
154 + const conflictDir
= await sessionDir (
"restore-conflict" );
156 + join (conflictDir,
`conflict_${info. id }.jsonl` ),
157 + sessionBody (info.
id , info.created.
toISOString (), info.cwd, [
"local" ]),
159 + expect (
await archive.
restore (records, conflictDir)).
toEqual ({
166 + it (
"survives concurrent index updates from two archives" ,
async ()
=> {
167 + const { archive, identity, paths }
= await initHost (
"cas-a" ,
testAlias ());
168 + const secondPaths
= statePathsIn (
"cas-b" );
169 + await mkdir (
dirname (secondPaths.identity), { recursive:
true });
170 + await writeFile (secondPaths.identity,
`${ JSON . stringify (identity)}\n` );
171 + const other
= newArchive (secondPaths);
172 + const directory
= await sessionDir (
"cas-sessions" );
173 + const left
= await writeSession (directory, { messages: [
"left" ] });
174 + const right
= await writeSession (directory, { messages: [
"right" ] });
176 + const [a, b]
= await Promise .
all ([
177 + archive.
backup ([left]),
178 + other.
backup ([right]),
181 + expect (a.uploaded).
toBe (
1 );
182 + expect (b.uploaded).
toBe (
1 );
183 + const cache
= await newArchive (paths).
refresh ();
184 + const index
= cache.indexes.
find (
185 + (entry)
=> entry.hostId
=== identity.hostId,
187 + expect (index?.sessions.
map ((record)
=> record.
id ).
sort ()).
toEqual (
188 + [left.
id , right.
id ].
sort (),
extensions/bak/__e2e__/support.ts Added new file mode 100644 1 + import { randomUUID }
from "node:crypto" ;
2 + import { readFile, writeFile }
from "node:fs/promises" ;
3 + import { join }
from "node:path" ;
4 + import type { SessionInfo }
from "@earendil-works/pi-coding-agent" ;
5 + import { AwsClient }
from "aws4fetch" ;
6 + import { sessionInfoFromPath }
from "../archive.js" ;
7 + import { type S3Config, S3Store, s3Config }
from "../s3.js" ;
8 + import type { HostIdentity }
from "../state.js" ;
10 + const decoder
= new TextDecoder ();
12 + export const CATALOG_KEY
= "catalog/hosts.json" ;
13 + export const storageConfig
= s3Config ().config;
15 + export function testAlias ()
: string {
16 + return `e2e-${ randomUUID (). slice (0, 8)}` ;
19 + function signedClient (config
: S3Config )
: AwsClient {
20 + return new AwsClient ({
21 + accessKeyId: config.accessKeyId,
22 + secretAccessKey: config.secretAccessKey,
24 + region: config.region,
29 + function objectUrl (config
: S3Config , key
: string )
: string {
30 + const endpoint
= config.endpoint.
replace (
/\/ +$ / ,
"" );
31 + const path
= [config.bucket,
... key.
split (
"/" )]
32 + .
map ((part)
=> encodeURIComponent (part))
34 + return `${endpoint}/${path}` ;
37 + export async function listKeys (
40 + )
: Promise <
string []> {
41 + const url
= `${config.endpoint. replace (/\/ +$ /, "")}/${ encodeURIComponent (config.bucket)}?list-type=2&max-keys=1000&prefix=${ encodeURIComponent (prefix)}` ;
42 + const response
= await signedClient (config).
fetch (url, { method:
"GET" });
43 + const xml
= await response.
text ();
44 + return [
... xml.
matchAll (
/<Key>([ ^ <] + )<\/Key>/ g )].
map (
45 + (match)
=> match[
1 ]
as string ,
49 + export async function deleteKey (
53 + const response
= await signedClient (config).
fetch (
objectUrl (config, key), {
56 + return response.
status ;
63 + export async function removeHosts (
67 + if (hostIds.
length === 0 )
return ;
68 + for (
const hostId
of hostIds) {
69 + for (
const key
of await listKeys (config,
`sessions/${hostId}/` ))
70 + await deleteKey (config, key);
71 + await deleteKey (config,
`hosts/${hostId}.json` );
73 + const store
= new S3Store (config);
74 + for (
let attempt
= 0 ; attempt
< 5 ; attempt
++ ) {
75 + const current
= await store.
get (CATALOG_KEY);
76 + if (
! current?.etag)
return ;
77 + const catalog
= JSON .
parse (decoder.
decode (current.
body ))
as {
79 + hosts
: HostIdentity [];
81 + const hosts
= catalog.hosts.
filter (
82 + (host)
=> ! hostIds.
includes (host.hostId),
84 + if (hosts.
length === catalog.hosts.
length )
return ;
88 + `${ JSON . stringify ({ ... catalog, hosts })}\n` ,
90 + contentType:
"application/json" ,
91 + ifMatch: current.etag,
101 + export function sessionBody (
105 + messages
: string [],
114 + timestamp: created,
118 + let parentId
: string | null = null ;
119 + for (
const text
of messages) {
120 + const entryId
= randomUUID ().
slice (
0 ,
8 );
126 + timestamp: created,
127 + message: { role:
"user" , content: [{ type:
"text" , text }] },
130 + parentId
= entryId;
135 + type:
"session_info" ,
136 + id:
randomUUID ().
slice (
0 ,
8 ),
138 + timestamp: created,
142 + return `${lines. join ("\n")}\n` ;
145 + export async function writeSession (
151 + messages
?: string [];
154 + )
: Promise <
SessionInfo > {
155 + const id
= options.
id ?? randomUUID ();
156 + const created
= options.created
?? new Date ().
toISOString ();
157 + const path
= join (directory,
`${created. replace (/[:.]/ g , "-")}_${id}.jsonl` );
163 + options.cwd
?? directory,
164 + options.messages
?? [
"hello" ],
168 + return sessionInfoFromPath (path);
171 + export async function appendMessage (
174 + )
: Promise <
SessionInfo > {
175 + const body
= await readFile (path,
"utf8" );
178 + `${body}${ JSON . stringify ({ type: "message", role: "assistant", content: text })}\n` ,
180 + return sessionInfoFromPath (path);
extensions/bak/__tests__/s3.test.ts Added new file mode 100644 1 + import { describe, expect, it }
from "vitest" ;
2 + import { S3Store }
from "../s3.js" ;
5 + endpoint:
"https://example.invalid" ,
9 + secretAccessKey:
"secret" ,
12 + function recorder (
response : ()
=> Response ) {
13 + const calls
: { url
: string ; init
: RequestInit }[]
= [];
15 + fetch : (input
: RequestInfo | URL , init
?: RequestInit )
=> {
16 + calls.
push ({ url:
String (input), init: init
?? {} });
17 + return Promise .
resolve (
response ());
19 + }
as unknown as Pick <
import (
"aws4fetch" ).
AwsClient ,
"fetch" >;
20 + return { calls, client };
23 + function header (init
: RequestInit , name
: string )
: string | null {
24 + return new Headers (init.
headers ).
get (name);
27 + describe (
"S3Store weak ETag handling" , ()
=> {
28 + it (
"requests identity encoding and strips weak validators on get" ,
async ()
=> {
29 + const { calls, client }
= recorder (
31 + new Response (
"body" , {
33 + headers: { etag:
'W/"abc"' },
36 + const store
= new S3Store (config, client);
38 + const object
= await store.
get (
"hosts/host.json" );
40 + expect (object?.etag).
toBe (
'"abc"' );
41 + expect (
header (calls[
0 ]?.init
?? {},
"accept-encoding" )).
toBe (
"identity" );
44 + it (
"sends a strong validator when given a weak ifMatch" ,
async ()
=> {
45 + const { calls, client }
= recorder (
46 + ()
=> new Response (
null , { status:
200 , headers: { etag:
'W/"def"' } }),
48 + const store
= new S3Store (config, client);
50 + const etag
= await store.
put (
"hosts/host.json" ,
"body" , {
51 + contentType:
"application/json" ,
55 + expect (
header (calls[
0 ]?.init
?? {},
"if-match" )).
toBe (
'"abc"' );
56 + expect (etag).
toBe (
'"def"' );
extensions/bak/index.ts Modified 93 93 const manager
= ctx.sessionManager
as typeof ctx.sessionManager
& {
94 94 usesDefaultSessionDir ?: ()
=> boolean ;
96 - return manager.
usesDefaultSessionDir ?.()
98 + const directory
= ctx.sessionManager.
getSessionDir ();
99 + return ! directory
|| manager.
usesDefaultSessionDir ?.()
97 100 ? SessionManager.
listAll ()
98 - : SessionManager.
listAll (ctx.sessionManager.
getSessionDir ());
101 + : SessionManager.
listAll (directory);
101 104 export default function bakExtension (pi
: ExtensionAPI ) {
extensions/bak/mise.toml Modified 2 + description =
"Run bak real S3 storage E2E" 3 + depends = [
"//:install" ]
4 + run =
"npx vitest run --config vitest.e2e.config.ts" 2 7 description =
"Format bak extension" 3 8 depends = [
"//:install" ]
extensions/bak/s3.ts Modified 41 41 return status
=== 408 || status
=== 429 || status
>= 500 ;
44 + function strongEtag (value
: string | null )
: string | undefined {
45 + const etag
= value?.
trim ().
replace (
/ ^ W\// ,
"" );
46 + return etag
|| undefined ;
44 49 async function boundedError (response
: Response )
: Promise <
string > {
45 50 const text
= (
await response.
text ()).
replace (
/\s + / g ,
" " ).
trim ();
100 105 options : { signal
?: AbortSignal }
= {},
101 106 ):
Promise < StoredObject
| undefined > {
102 - const response
= await this.
request (key, { method:
"GET" }, options.signal);
107 + const response
= await this.
request (
109 + { method:
"GET" , headers: {
"accept-encoding" :
"identity" } },
103 112 if (response.status === 404) return undefined;
104 113 if (!response.ok)
105 114 throw new
S3Error (response.status, await boundedError(response));
107 116 body:
new Uint8Array (
await response.
arrayBuffer ()),
108 - etag: response.
headers .
get (
"etag" )
?? undefined ,
117 + etag:
strongEtag (response.
headers .
get (
"etag" )),
122 131 const headers
= new Headers ({
123 132 "content-type" : options.contentType
?? "application/octet-stream" ,
125 - if (options.ifMatch) headers.
set (
"if-match" , options.ifMatch);
134 + const ifMatch
= strongEtag (options.ifMatch
?? null );
135 + if (ifMatch) headers.
set (
"if-match" , ifMatch);
126 136 if (options.ifNoneMatch) headers.
set (
"if-none-match" , options.ifNoneMatch);
127 137 const requestBody
= 128 138 typeof body
=== "string" ? body
: Uint8Array .
from (body).buffer;
138 148 if (
! response.ok)
139 149 throw new S3Error (response.
status ,
await boundedError (response));
140 - return response.
headers .
get (
"etag" )
?? undefined ;
150 + return strongEtag (response.
headers .
get (
"etag" ));
extensions/bak/vitest.e2e.config.ts Added new file mode 100644 1 + import { defineConfig }
from "vitest/config" ;
3 + export default defineConfig ({
5 + include: [
"__e2e__/**/*.test.ts" ],
6 + exclude: [
"node_modules/**" ],
7 + fileParallelism:
false ,
8 + testTimeout:
120_000 ,
9 + hookTimeout:
120_000 ,