Skip to content

Commit

Permalink
Fixed Fetch not commit transaction when caching data
Browse files Browse the repository at this point in the history
Improved fetch testing
  • Loading branch information
james-pre committed Jan 13, 2025
1 parent d2d663c commit 7feb821
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 38 deletions.
5 changes: 3 additions & 2 deletions src/backends/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,12 @@ export class FetchFS extends StoreFS {
for (const [path, node] of index) {
if (!(node.mode & S_IFREG)) continue;

const url = this.baseUrl + path;
const content = await fetchFile(url, 'buffer', this.requestInit);
const content = await fetchFile(this.baseUrl + path, 'buffer', this.requestInit);

await tx.set(node.data, content);
}

await tx.commit();
}

public constructor(
Expand Down
8 changes: 8 additions & 0 deletions tests/fetch.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/bash

tsx tests/fetch.ts &
PID=$!

npx zenfs-test tests/setup/cow+fetch.ts $@

kill $PID
49 changes: 49 additions & 0 deletions tests/fetch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { execSync } from 'node:child_process';
import { appendFileSync, readFileSync } from 'node:fs';
import { createServer } from 'node:http';
import { join } from 'node:path';
import { data, tmp } from './setup.js';

// If you change the port please update the setup file as well
const port = 26514;

const statusCodes = {
ENOENT: 404,
};

try {
execSync(`npm exec make-index -- ${data} --output ${tmp}/index.json --quiet`, { stdio: 'inherit' });
} catch (e: any) {
if (e.signal == 'SIGINT') {
console.log('Aborted whilst creating index.');
process.exit(0);
} else {
console.error('Index creation failed: ' + e.message);
process.exit(1);
}
}

const server = createServer((request, response) => {
const { url = '' } = request;

if (url == '/.ping') {
response.statusCode = 200;
response.end('pong');
return;
}

const path = url == '/.index.json' ? join(tmp, 'index.json') : join(data, url.slice(1) || '');
try {
response.statusCode = 200;
response.end(readFileSync(path));
} catch (e: any) {
response.statusCode = statusCodes[e.code as keyof typeof statusCodes] || 400;
response.end();
}
});

server.listen(port);

process.on('beforeExit', () => {
server.close().unref();
});
40 changes: 4 additions & 36 deletions tests/setup/cow+fetch.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,11 @@
import { execSync } from 'node:child_process';
import { readFileSync } from 'node:fs';
import { createServer } from 'node:http';
import { join } from 'node:path';
import { configureSingle, Fetch, InMemory, Overlay } from '../../dist/index.js';
import { data, tmp } from '../setup.js';
import { configureSingle, Fetch, InMemory, Overlay, resolveMountConfig } from '../../dist/index.js';

const port = 26514,
index = tmp + '/index.json';

const statusCodes = {
ENOENT: 404,
};

execSync(`npm exec make-index -- ${data} --output ${index} --quiet`, { stdio: 'inherit' });

const server = createServer((request, response) => {
const path = request.url == '/.index.json' ? index : join(data, request.url?.slice(1) || '');
try {
response.statusCode = 200;
response.end(readFileSync(path));
} catch (e: any) {
response.statusCode = statusCodes[e.code as keyof typeof statusCodes] || 400;
response.end();
}
});

server
.once('error', (error: NodeJS.ErrnoException) => {
if (error.code == 'EADDRINUSE') return;
throw error;
})
.listen(port)
.unref();

const baseUrl = 'http://localhost:' + port;
const baseUrl = 'http://localhost:26514';

await configureSingle({
backend: Overlay,
readable: Fetch.create({
readable: await resolveMountConfig({
backend: Fetch,
baseUrl,
index: baseUrl + '/.index.json',
}),
Expand Down

0 comments on commit 7feb821

Please sign in to comment.