mirror of
https://github.com/HeyPuter/puter.git
synced 2026-11-25 07:09:11 +00:00
* fix(localworker): swap Miniflare instance before disposing previous one and dedupe concurrent cold starts * test(localworker): restore the service's tests and cover the swap on the real runtime Brings back the existing LocalWorkerService tests and adds cases for a request during a redeploy, concurrent first requests, and a retry after a failed first deploy. The first-request deploy moves into its own method, and both dispose paths share one helper. --------- Co-authored-by: U-ITACHII <356922991+U-ITACHII@users.noreply.github.com> Co-authored-by: Daniel Salazar <daniel.salazar@puter.com>
421 lines
16 KiB
TypeScript
421 lines
16 KiB
TypeScript
/**
|
||
* Copyright (C) 2024-present Puter Technologies Inc.
|
||
*
|
||
* This file is part of Puter.
|
||
*
|
||
* Puter is free software: you can redistribute it and/or modify
|
||
* it under the terms of the GNU Affero General Public License as published
|
||
* by the Free Software Foundation, either version 3 of the License, or
|
||
* (at your option) any later version.
|
||
*
|
||
* This program is distributed in the hope that it will be useful,
|
||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
* GNU Affero General Public License for more details.
|
||
*
|
||
* You should have received a copy of the GNU Affero General Public License
|
||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||
*/
|
||
|
||
import { createServer } from 'node:http';
|
||
import type { AddressInfo } from 'node:net';
|
||
import { Readable } from 'node:stream';
|
||
import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest';
|
||
import {
|
||
WORKER_SUBDOMAIN_PREFIX,
|
||
type SubdomainRow,
|
||
} from '../../stores/subdomain/SubdomainStore.js';
|
||
import type { PuterServer } from '../../server.js';
|
||
import { createTestUser, setupTestServer } from '../../testUtil.js';
|
||
import { getWorkerPreamble } from '../../drivers/workers/WorkerDriver.js';
|
||
import type { LocalWorkerService } from './LocalWorkerService.js';
|
||
|
||
let server: PuterServer;
|
||
let localWorkers: LocalWorkerService;
|
||
let ownerUserId: number;
|
||
let ownerUsername: string;
|
||
|
||
beforeAll(async () => {
|
||
server = await setupTestServer({
|
||
// Miniflare binds this into every local worker; the constructor
|
||
// rejects an undefined binding value.
|
||
api_base_url: 'http://api.puter.localhost:4100',
|
||
} as never);
|
||
localWorkers = server.services
|
||
.localworkerservice as unknown as LocalWorkerService;
|
||
const created = await createTestUser(server, {
|
||
username: 'lws',
|
||
password: 'local-worker-password',
|
||
});
|
||
ownerUsername = created.username;
|
||
const row = await server.stores.user.getByUsername(created.username);
|
||
ownerUserId = row!.id;
|
||
}, 60_000);
|
||
|
||
afterAll(async () => {
|
||
localWorkers?.onServerShutdown();
|
||
await server?.shutdown();
|
||
}, 60_000);
|
||
|
||
/** Write a worker source file into the owner's home and return its entry. */
|
||
const writeSource = async (
|
||
filename: string,
|
||
source: string,
|
||
): Promise<{ id: number; uuid: string }> => {
|
||
const path = `/${ownerUsername}/${filename}`;
|
||
await server.services.fs.write(ownerUserId, {
|
||
fileMetadata: {
|
||
path,
|
||
size: Buffer.byteLength(source),
|
||
contentType: 'text/javascript',
|
||
overwrite: true,
|
||
createMissingParents: true,
|
||
},
|
||
fileContent: Readable.from(Buffer.from(source)),
|
||
});
|
||
const entry = await server.stores.fsEntry.getEntryByPath(path);
|
||
return { id: entry!.id as number, uuid: String(entry!.uuid) };
|
||
};
|
||
|
||
const subdomainRow = (over: Partial<SubdomainRow>): SubdomainRow =>
|
||
({
|
||
user_id: ownerUserId,
|
||
app_owner: null,
|
||
root_dir_id: null,
|
||
...over,
|
||
}) as SubdomainRow;
|
||
|
||
/** A stored worker row: the minted token is only valid while it exists. */
|
||
const seedWorkerRow = async (
|
||
workerName: string,
|
||
over: { root_dir_id: number; app_owner?: number },
|
||
): Promise<SubdomainRow> =>
|
||
(await server.stores.subdomain.create({
|
||
userId: ownerUserId,
|
||
subdomain: `${WORKER_SUBDOMAIN_PREFIX}${workerName}`,
|
||
rootDirId: over.root_dir_id,
|
||
appOwner: over.app_owner ?? null,
|
||
})) as unknown as SubdomainRow;
|
||
|
||
describe('LocalWorkerService.reconstructDeployArgs', () => {
|
||
it('mints a user-scoped worker session token and prepends the preamble', async () => {
|
||
const src = await writeSource(
|
||
'worker-a.js',
|
||
"export default { fetch: () => new Response('a') };",
|
||
);
|
||
const [name, authorization, code] =
|
||
await localWorkers.reconstructDeployArgs(
|
||
'worker-a',
|
||
await seedWorkerRow('worker-a', { root_dir_id: src.id }),
|
||
);
|
||
|
||
const source = "export default { fetch: () => new Response('a') };";
|
||
|
||
expect(name).toBe('worker-a');
|
||
// Assert the concatenation contract directly against the preamble the
|
||
// driver actually loaded. `src/worker/dist/` is a gitignored build
|
||
// artifact, so asserting a non-empty prefix instead would make this
|
||
// test pass or fail on whether the tree happens to be built.
|
||
expect(code).toBe(getWorkerPreamble() + source);
|
||
|
||
// The credential is a real worker session token for the owner.
|
||
const auth = await server.services.auth.authenticate(authorization);
|
||
expect(auth.actor?.user?.id).toBe(ownerUserId);
|
||
expect(auth.actor?.session?.kind).toBe('worker');
|
||
});
|
||
|
||
it('mints an app-scoped token when the worker belongs to an app', async () => {
|
||
const src = await writeSource(
|
||
'worker-b.js',
|
||
"export default { fetch: () => new Response('b') };",
|
||
);
|
||
const app = await (
|
||
server.stores.app.create as unknown as (
|
||
f: Record<string, unknown>,
|
||
o: { ownerUserId: number },
|
||
) => Promise<{ id: number; uid: string }>
|
||
)(
|
||
{
|
||
name: `lws-app-${Date.now()}`,
|
||
title: 'LWS app',
|
||
index_url: 'https://lws-app.test/',
|
||
},
|
||
{ ownerUserId },
|
||
);
|
||
|
||
const [, authorization] = await localWorkers.reconstructDeployArgs(
|
||
'worker-b',
|
||
await seedWorkerRow('worker-b', {
|
||
root_dir_id: src.id,
|
||
app_owner: app.id,
|
||
}),
|
||
);
|
||
|
||
const auth = await server.services.auth.authenticate(authorization);
|
||
expect(auth.actor?.app?.uid).toBe(app.uid);
|
||
expect(auth.actor?.user?.id).toBe(ownerUserId);
|
||
});
|
||
|
||
it('refuses when the owning user no longer exists', async () => {
|
||
await expect(
|
||
localWorkers.reconstructDeployArgs(
|
||
'worker-ghost',
|
||
subdomainRow({ user_id: 999_999, root_dir_id: 1 }),
|
||
),
|
||
).rejects.toThrow('Owner seems to not exist');
|
||
});
|
||
|
||
it('refuses when the owning app no longer exists', async () => {
|
||
const src = await writeSource('worker-c.js', 'export default {};');
|
||
await expect(
|
||
localWorkers.reconstructDeployArgs(
|
||
'worker-c',
|
||
subdomainRow({ root_dir_id: src.id, app_owner: 999_999 }),
|
||
),
|
||
).rejects.toThrow(/existant application/);
|
||
});
|
||
|
||
it('refuses a worker whose subdomain has no source file', async () => {
|
||
await expect(
|
||
localWorkers.reconstructDeployArgs(
|
||
'worker-d',
|
||
subdomainRow({ root_dir_id: null }),
|
||
),
|
||
).rejects.toThrow(/no root_dir_id/);
|
||
});
|
||
|
||
it('refuses when the source entry id points at nothing', async () => {
|
||
await expect(
|
||
localWorkers.reconstructDeployArgs(
|
||
'worker-e',
|
||
subdomainRow({ root_dir_id: 999_999 }),
|
||
),
|
||
).rejects.toThrow(/source file not found/);
|
||
});
|
||
|
||
it('refuses a worker whose source file belongs to another user', async () => {
|
||
const other = await createTestUser(server, {
|
||
username: 'lws-other',
|
||
password: 'local-worker-password',
|
||
});
|
||
const otherRow = (await server.stores.user.getByUsername(
|
||
other.username,
|
||
))!;
|
||
const path = `/${other.username}/worker-f.js`;
|
||
await server.services.fs.write(otherRow.id, {
|
||
fileMetadata: {
|
||
path,
|
||
size: 2,
|
||
contentType: 'text/javascript',
|
||
overwrite: true,
|
||
},
|
||
fileContent: Readable.from(Buffer.from('//')),
|
||
});
|
||
const foreign = await server.stores.fsEntry.getEntryByPath(path);
|
||
|
||
await expect(
|
||
localWorkers.reconstructDeployArgs(
|
||
'worker-f',
|
||
await seedWorkerRow('worker-f', {
|
||
root_dir_id: foreign!.id as number,
|
||
}),
|
||
),
|
||
).rejects.toThrow(/not owned by the worker's owner/);
|
||
});
|
||
});
|
||
|
||
describe('LocalWorkerService.cfCallLocal', () => {
|
||
it('404s a request for a worker with no subdomain row', async () => {
|
||
const res = await localWorkers.cfCallLocal(
|
||
`unknown-${Date.now()}`,
|
||
new Request('http://x.localhost/'),
|
||
);
|
||
expect(res.status).toBe(404);
|
||
expect(await res.text()).toBe('subdomain not found');
|
||
});
|
||
|
||
it('404s a request for a suspended owner’s worker', async () => {
|
||
const name = `lws-susp-${Date.now()}`;
|
||
const src = await writeSource(
|
||
`${name}.js`,
|
||
"router.get('/', () => new Response('up'));",
|
||
);
|
||
await seedWorkerRow(name, { root_dir_id: src.id });
|
||
const call = () =>
|
||
localWorkers.cfCallLocal(name, new Request('http://x.localhost/'));
|
||
|
||
await server.stores.user.update(ownerUserId, { suspended: 1 });
|
||
try {
|
||
expect((await call()).status).toBe(404);
|
||
} finally {
|
||
await server.stores.user.update(ownerUserId, { suspended: 0 });
|
||
}
|
||
const res = await call();
|
||
expect(await res.text()).toBe('up');
|
||
await localWorkers.cfDeleteLocal(name);
|
||
});
|
||
|
||
it('serves concurrent first requests from a single deploy', async () => {
|
||
const name = `lws-cold-${Date.now()}`;
|
||
const src = await writeSource(
|
||
`${name}.js`,
|
||
"router.get('/', () => new Response('up'));",
|
||
);
|
||
await seedWorkerRow(name, { root_dir_id: src.id });
|
||
const deploy = vi.spyOn(localWorkers, 'cfDeployLocal');
|
||
try {
|
||
const responses = await Promise.all(
|
||
Array.from({ length: 8 }, () =>
|
||
localWorkers.cfCallLocal(
|
||
name,
|
||
new Request('http://x.localhost/'),
|
||
),
|
||
),
|
||
);
|
||
const bodies = await Promise.all(responses.map((r) => r.text()));
|
||
expect(bodies).toEqual(Array(8).fill('up'));
|
||
expect(deploy).toHaveBeenCalledTimes(1);
|
||
} finally {
|
||
deploy.mockRestore();
|
||
await localWorkers.cfDeleteLocal(name);
|
||
}
|
||
}, 60_000);
|
||
|
||
it('retries the deploy on the request after a failed first one', async () => {
|
||
const name = `lws-retry-${Date.now()}`;
|
||
const src = await writeSource(
|
||
`${name}.js`,
|
||
"router.get('/', () => new Response('up'));",
|
||
);
|
||
const row = await seedWorkerRow(name, { root_dir_id: src.id });
|
||
const call = () =>
|
||
localWorkers.cfCallLocal(name, new Request('http://x.localhost/'));
|
||
|
||
// With no source file the deploy throws for every waiting request.
|
||
await server.stores.subdomain.update(row.uuid as string, {
|
||
root_dir_id: null,
|
||
});
|
||
const failed = await Promise.allSettled([call(), call()]);
|
||
expect(failed.map((r) => r.status)).toEqual(['rejected', 'rejected']);
|
||
|
||
await server.stores.subdomain.update(row.uuid as string, {
|
||
root_dir_id: src.id,
|
||
});
|
||
const res = await call();
|
||
expect(await res.text()).toBe('up');
|
||
await localWorkers.cfDeleteLocal(name);
|
||
}, 60_000);
|
||
});
|
||
|
||
describe('LocalWorkerService.cfDeleteLocal', () => {
|
||
it('mirrors the upstream delete response shape', async () => {
|
||
expect(await localWorkers.cfDeleteLocal('never-deployed')).toEqual({
|
||
success: true,
|
||
errors: [],
|
||
messages: [],
|
||
result: { id: 'never-deployed' },
|
||
});
|
||
});
|
||
});
|
||
|
||
describe('LocalWorkerService.cfDeployLocal', () => {
|
||
it('deploys, serves a request, and stops serving after delete', async () => {
|
||
const name = `lws-live-${Date.now()}`;
|
||
const deployed = await localWorkers.cfDeployLocal(
|
||
name,
|
||
'test-authorization',
|
||
`addEventListener('fetch', (e) => {
|
||
e.respondWith(new Response('hello ' + puter_auth));
|
||
});`,
|
||
);
|
||
|
||
expect(deployed.success).toBe(true);
|
||
expect(deployed.errors).toEqual([]);
|
||
expect(deployed.url).toContain(`${name}.workers.puter.localhost`);
|
||
|
||
const res = await localWorkers.cfCallLocal(
|
||
name,
|
||
new Request('http://worker.localhost/hi'),
|
||
);
|
||
// The binding carries the authorization the deploy was given.
|
||
expect(await res.text()).toBe('hello test-authorization');
|
||
|
||
await localWorkers.cfDeleteLocal(name);
|
||
// With the instance disposed and no subdomain row, the next call 404s.
|
||
const after = await localWorkers.cfCallLocal(
|
||
name,
|
||
new Request('http://worker.localhost/hi'),
|
||
);
|
||
expect(after.status).toBe(404);
|
||
}, 60_000);
|
||
|
||
it('serves a request that arrives while a redeploy is in progress', async () => {
|
||
const name = `lws-swap-${Date.now()}`;
|
||
const respond = (text: string) =>
|
||
`addEventListener('fetch', (e) => e.respondWith(new Response('${text}')));`;
|
||
const call = () =>
|
||
localWorkers.cfCallLocal(name, new Request('http://x.localhost/'));
|
||
try {
|
||
await localWorkers.cfDeployLocal(name, 'auth', respond('v1'));
|
||
expect(await (await call()).text()).toBe('v1');
|
||
|
||
const redeploy = localWorkers.cfDeployLocal(
|
||
name,
|
||
'auth',
|
||
respond('v2'),
|
||
);
|
||
const during = await call();
|
||
expect(await during.text()).toBe('v2');
|
||
expect((await redeploy).success).toBe(true);
|
||
} finally {
|
||
await localWorkers.cfDeleteLocal(name);
|
||
}
|
||
}, 60_000);
|
||
|
||
it('reaches this machine, but not link-local or LAN addresses by default', async () => {
|
||
const local = createServer((_req, res) => res.end('reached'));
|
||
await new Promise<void>((r) => local.listen(0, '127.0.0.1', r));
|
||
const { port } = local.address() as AddressInfo;
|
||
const name = `lws-egress-${Date.now()}`;
|
||
try {
|
||
await localWorkers.cfDeployLocal(
|
||
name,
|
||
'auth',
|
||
`addEventListener('fetch', (e) => e.respondWith((async () => {
|
||
const probe = (url) => fetch(url, {
|
||
signal: AbortSignal.timeout(10000),
|
||
}).then((r) => r.text(), (err) => String(err));
|
||
return Response.json({
|
||
local: await probe('http://127.0.0.1:${port}/'),
|
||
metadata: await probe('http://169.254.169.254/'),
|
||
lan: await probe('http://10.0.0.1/'),
|
||
});
|
||
})()));`,
|
||
);
|
||
const res = await localWorkers.cfCallLocal(
|
||
name,
|
||
new Request('http://worker.localhost/'),
|
||
);
|
||
const body = (await res.json()) as Record<string, string>;
|
||
expect(body.local).toBe('reached');
|
||
// Refused outright by the runtime, not left to time out.
|
||
expect(body.metadata).toMatch(/internal error/);
|
||
expect(body.lan).toMatch(/internal error/);
|
||
} finally {
|
||
await localWorkers.cfDeleteLocal(name);
|
||
local.close();
|
||
}
|
||
}, 60_000);
|
||
|
||
it('reports failure instead of throwing when the runtime rejects the options', async () => {
|
||
const result = await localWorkers.cfDeployLocal(
|
||
'bad-worker',
|
||
'auth',
|
||
// `script` and `scriptPath` are mutually exclusive; the Miniflare
|
||
// constructor validates eagerly and throws.
|
||
undefined as unknown as string,
|
||
);
|
||
expect(result).toEqual({ success: false, errors: [], url: null });
|
||
});
|
||
});
|