@softure-ai/files 0.0.0-stage → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +26 -0
- package/LICENSE +21 -0
- package/README.md +213 -2
- package/dist/contract.d.ts +51 -0
- package/dist/contract.d.ts.map +1 -0
- package/dist/contract.js +4 -0
- package/dist/contract.js.map +1 -0
- package/dist/index.d.ts +45 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +49 -0
- package/dist/index.js.map +1 -0
- package/dist/jobs/index.d.ts +12 -0
- package/dist/jobs/index.d.ts.map +1 -0
- package/dist/jobs/index.js +22 -0
- package/dist/jobs/index.js.map +1 -0
- package/dist/messages/en.d.ts +17 -0
- package/dist/messages/en.d.ts.map +1 -0
- package/dist/messages/en.js +17 -0
- package/dist/messages/en.js.map +1 -0
- package/dist/messages/index.d.ts +24 -0
- package/dist/messages/index.d.ts.map +1 -0
- package/dist/messages/index.js +10 -0
- package/dist/messages/index.js.map +1 -0
- package/dist/next/context.d.ts +4 -0
- package/dist/next/context.d.ts.map +1 -0
- package/dist/next/context.js +13 -0
- package/dist/next/context.js.map +1 -0
- package/dist/next/index.d.ts +3 -0
- package/dist/next/index.d.ts.map +1 -0
- package/dist/next/index.js +5 -0
- package/dist/next/index.js.map +1 -0
- package/dist/next/route.d.ts +3 -0
- package/dist/next/route.d.ts.map +1 -0
- package/dist/next/route.js +11 -0
- package/dist/next/route.js.map +1 -0
- package/dist/options.d.ts +26 -0
- package/dist/options.d.ts.map +1 -0
- package/dist/options.js +59 -0
- package/dist/options.js.map +1 -0
- package/dist/schema.d.ts +214 -0
- package/dist/schema.d.ts.map +1 -0
- package/dist/schema.js +19 -0
- package/dist/schema.js.map +1 -0
- package/dist/server/context.d.ts +13 -0
- package/dist/server/context.d.ts.map +1 -0
- package/dist/server/context.js +19 -0
- package/dist/server/context.js.map +1 -0
- package/dist/server/files.d.ts +48 -0
- package/dist/server/files.d.ts.map +1 -0
- package/dist/server/files.js +104 -0
- package/dist/server/files.js.map +1 -0
- package/dist/server/health.d.ts +4 -0
- package/dist/server/health.d.ts.map +1 -0
- package/dist/server/health.js +20 -0
- package/dist/server/health.js.map +1 -0
- package/dist/server/index.d.ts +15 -0
- package/dist/server/index.d.ts.map +1 -0
- package/dist/server/index.js +17 -0
- package/dist/server/index.js.map +1 -0
- package/dist/server/keys.d.ts +14 -0
- package/dist/server/keys.d.ts.map +1 -0
- package/dist/server/keys.js +47 -0
- package/dist/server/keys.js.map +1 -0
- package/dist/server/links.d.ts +30 -0
- package/dist/server/links.d.ts.map +1 -0
- package/dist/server/links.js +122 -0
- package/dist/server/links.js.map +1 -0
- package/dist/server/local-storage.d.ts +7 -0
- package/dist/server/local-storage.d.ts.map +1 -0
- package/dist/server/local-storage.js +83 -0
- package/dist/server/local-storage.js.map +1 -0
- package/dist/server/names.d.ts +11 -0
- package/dist/server/names.d.ts.map +1 -0
- package/dist/server/names.js +32 -0
- package/dist/server/names.js.map +1 -0
- package/dist/server/options.d.ts +8 -0
- package/dist/server/options.d.ts.map +1 -0
- package/dist/server/options.js +31 -0
- package/dist/server/options.js.map +1 -0
- package/dist/server/privacy.d.ts +16 -0
- package/dist/server/privacy.d.ts.map +1 -0
- package/dist/server/privacy.js +39 -0
- package/dist/server/privacy.js.map +1 -0
- package/dist/server/prune.d.ts +13 -0
- package/dist/server/prune.d.ts.map +1 -0
- package/dist/server/prune.js +34 -0
- package/dist/server/prune.js.map +1 -0
- package/dist/server/queries.d.ts +7 -0
- package/dist/server/queries.d.ts.map +1 -0
- package/dist/server/queries.js +17 -0
- package/dist/server/queries.js.map +1 -0
- package/dist/server/s3-storage.d.ts +33 -0
- package/dist/server/s3-storage.d.ts.map +1 -0
- package/dist/server/s3-storage.js +223 -0
- package/dist/server/s3-storage.js.map +1 -0
- package/dist/server/save.d.ts +29 -0
- package/dist/server/save.d.ts.map +1 -0
- package/dist/server/save.js +141 -0
- package/dist/server/save.js.map +1 -0
- package/dist/server/serve.d.ts +11 -0
- package/dist/server/serve.d.ts.map +1 -0
- package/dist/server/serve.js +108 -0
- package/dist/server/serve.js.map +1 -0
- package/dist/server/sigv4.d.ts +23 -0
- package/dist/server/sigv4.d.ts.map +1 -0
- package/dist/server/sigv4.js +57 -0
- package/dist/server/sigv4.js.map +1 -0
- package/dist/storage.d.ts +32 -0
- package/dist/storage.d.ts.map +1 -0
- package/dist/storage.js +10 -0
- package/dist/storage.js.map +1 -0
- package/dist/testing/index.d.ts +3 -0
- package/dist/testing/index.d.ts.map +1 -0
- package/dist/testing/index.js +4 -0
- package/dist/testing/index.js.map +1 -0
- package/dist/testing/links.d.ts +22 -0
- package/dist/testing/links.d.ts.map +1 -0
- package/dist/testing/links.js +30 -0
- package/dist/testing/links.js.map +1 -0
- package/dist/testing/memory-storage.d.ts +11 -0
- package/dist/testing/memory-storage.d.ts.map +1 -0
- package/dist/testing/memory-storage.js +52 -0
- package/dist/testing/memory-storage.js.map +1 -0
- package/migrations/0001_create_files.sql +32 -0
- package/module.json +24 -0
- package/package.json +72 -4
- package/src/contract.ts +59 -0
- package/src/index.ts +53 -0
- package/src/jobs/index.ts +23 -0
- package/src/messages/en.ts +16 -0
- package/src/messages/index.ts +13 -0
- package/src/next/context.ts +14 -0
- package/src/next/index.ts +4 -0
- package/src/next/route.ts +11 -0
- package/src/options.ts +64 -0
- package/src/schema.ts +22 -0
- package/src/server/context.ts +32 -0
- package/src/server/files.ts +133 -0
- package/src/server/health.ts +20 -0
- package/src/server/index.ts +45 -0
- package/src/server/keys.ts +56 -0
- package/src/server/links.ts +152 -0
- package/src/server/local-storage.ts +87 -0
- package/src/server/names.ts +35 -0
- package/src/server/options.ts +34 -0
- package/src/server/privacy.ts +53 -0
- package/src/server/prune.ts +46 -0
- package/src/server/queries.ts +19 -0
- package/src/server/s3-storage.ts +255 -0
- package/src/server/save.ts +167 -0
- package/src/server/serve.ts +106 -0
- package/src/server/sigv4.ts +82 -0
- package/src/storage.ts +43 -0
- package/src/testing/index.ts +3 -0
- package/src/testing/links.ts +47 -0
- package/src/testing/memory-storage.ts +64 -0
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
// The public link answer, framework-free: `GET` and `HEAD` with `Content-Length`, `Accept-Ranges`, single byte
|
|
2
|
+
// ranges (`206`, `416`), `Content-Disposition`, and `Cache-Control: private, no-store` so no CDN keeps a copy. Every
|
|
3
|
+
// dead link (forged, expired, revoked, deleted, missing from storage) gets the same `404`. A stored file is user
|
|
4
|
+
// content on the app's origin, so it is sent with `nosniff` and a sandboxing CSP: an HTML or SVG file never runs.
|
|
5
|
+
import { errorLogLabel } from "@softure-ai/core";
|
|
6
|
+
import type { ByteRange } from "../contract.js";
|
|
7
|
+
import type { FilesContext } from "./context.js";
|
|
8
|
+
import { findLiveRow } from "./files.js";
|
|
9
|
+
import { verifyFileLink } from "./links.js";
|
|
10
|
+
import { formatContentDisposition } from "./names.js";
|
|
11
|
+
import { getFilesOptions } from "./options.js";
|
|
12
|
+
|
|
13
|
+
const BASE_HEADERS = {
|
|
14
|
+
"cache-control": "private, no-store",
|
|
15
|
+
"x-content-type-options": "nosniff",
|
|
16
|
+
"referrer-policy": "no-referrer",
|
|
17
|
+
} as const;
|
|
18
|
+
const ALLOWED_METHODS = "GET, HEAD";
|
|
19
|
+
|
|
20
|
+
const NOT_FOUND_BODY = "Not found\n";
|
|
21
|
+
|
|
22
|
+
/** The one answer for every link that does not lead to a live file. */
|
|
23
|
+
function notFound(): Response {
|
|
24
|
+
return new Response(NOT_FOUND_BODY, {
|
|
25
|
+
status: 404,
|
|
26
|
+
headers: {
|
|
27
|
+
...BASE_HEADERS,
|
|
28
|
+
"content-type": "text/plain; charset=utf-8",
|
|
29
|
+
"content-length": String(NOT_FOUND_BODY.length),
|
|
30
|
+
},
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** Answers a request for a public link (`GET` or `HEAD`). */
|
|
35
|
+
export async function serveFileLink(ctx: FilesContext, request: Request): Promise<Response> {
|
|
36
|
+
if (request.method !== "GET" && request.method !== "HEAD") {
|
|
37
|
+
return new Response(null, {
|
|
38
|
+
status: 405,
|
|
39
|
+
headers: { ...BASE_HEADERS, allow: ALLOWED_METHODS },
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
const link = verifyFileLink(ctx, new URL(request.url).pathname);
|
|
43
|
+
if (!link.ok) {
|
|
44
|
+
if (link.error !== "files.not_found") console.error(`@softure-ai/files: a link could not be checked: ${link.error}`);
|
|
45
|
+
return notFound();
|
|
46
|
+
}
|
|
47
|
+
const row = await findLiveRow(ctx, link.value.fileId);
|
|
48
|
+
if (row?.linkGeneration !== link.value.generation) return notFound();
|
|
49
|
+
|
|
50
|
+
const options = getFilesOptions(ctx.config);
|
|
51
|
+
const headers: Record<string, string> = {
|
|
52
|
+
...BASE_HEADERS,
|
|
53
|
+
"content-type": row.contentType,
|
|
54
|
+
"accept-ranges": "bytes",
|
|
55
|
+
"content-disposition": formatContentDisposition(options.links.disposition, link.value.name),
|
|
56
|
+
"content-security-policy": "sandbox; default-src 'none'",
|
|
57
|
+
};
|
|
58
|
+
const range = parseRangeHeader(request.headers.get("range"), row.size);
|
|
59
|
+
if (range === "unsatisfiable") {
|
|
60
|
+
return new Response(null, {
|
|
61
|
+
status: 416,
|
|
62
|
+
headers: { ...headers, "content-range": `bytes */${String(row.size)}` },
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
const status = range === null ? 200 : 206;
|
|
66
|
+
headers["content-length"] = String(range === null ? row.size : range.end - range.start + 1);
|
|
67
|
+
if (range !== null) headers["content-range"] = `bytes ${String(range.start)}-${String(range.end)}/${String(row.size)}`;
|
|
68
|
+
if (request.method === "HEAD") return new Response(null, { status, headers });
|
|
69
|
+
|
|
70
|
+
let body: ReadableStream<Uint8Array> | null;
|
|
71
|
+
try {
|
|
72
|
+
body = await options.storage.get(row.storageKey, range ?? undefined);
|
|
73
|
+
} catch (error) {
|
|
74
|
+
console.error(`@softure-ai/files: reading file ${row.id} from storage failed: ${errorLogLabel(error)}`);
|
|
75
|
+
throw error;
|
|
76
|
+
}
|
|
77
|
+
if (body === null) {
|
|
78
|
+
console.error(`@softure-ai/files: file ${row.id} has a row but no bytes in storage`);
|
|
79
|
+
return notFound();
|
|
80
|
+
}
|
|
81
|
+
return new Response(body, { status, headers });
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* The single byte range a `Range` header asks for in a file of `size` bytes (RFC 9110 §14): null to answer the whole
|
|
86
|
+
* file (no header, a malformed one, another unit or several ranges, which a server may ignore), `"unsatisfiable"`
|
|
87
|
+
* for a range that starts past the end.
|
|
88
|
+
*/
|
|
89
|
+
export function parseRangeHeader(value: string | null, size: number): ByteRange | "unsatisfiable" | null {
|
|
90
|
+
const match = value === null ? null : /^bytes=(\d*)-(\d*)$/.exec(value.trim());
|
|
91
|
+
if (match === null) return null;
|
|
92
|
+
const [, first = "", last = ""] = match;
|
|
93
|
+
if (first === "" && last === "") return null;
|
|
94
|
+
if (first === "") {
|
|
95
|
+
const suffix = Number(last);
|
|
96
|
+
if (suffix === 0 || size === 0) return "unsatisfiable";
|
|
97
|
+
return { start: Math.max(size - suffix, 0), end: size - 1 };
|
|
98
|
+
}
|
|
99
|
+
const start = Number(first);
|
|
100
|
+
if (last !== "" && Number(last) < start) return null;
|
|
101
|
+
if (start >= size) return "unsatisfiable";
|
|
102
|
+
return {
|
|
103
|
+
start,
|
|
104
|
+
end: last === "" ? size - 1 : Math.min(Number(last), size - 1),
|
|
105
|
+
};
|
|
106
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
// AWS Signature Version 4 for S3-compatible storage (R2, MinIO, Garage, AWS), with `node:crypto` only. The signer
|
|
2
|
+
// takes the request as it will be sent and answers the headers to add; it is checked against the test vector AWS
|
|
3
|
+
// publishes for S3 (tests/sigv4.test.ts).
|
|
4
|
+
import { createHash, createHmac } from "node:crypto";
|
|
5
|
+
|
|
6
|
+
const ALGORITHM = "AWS4-HMAC-SHA256";
|
|
7
|
+
export const EMPTY_PAYLOAD_SHA256 = createHash("sha256").update("").digest("hex");
|
|
8
|
+
|
|
9
|
+
export interface SigV4Credentials {
|
|
10
|
+
readonly accessKeyId: string;
|
|
11
|
+
readonly secretAccessKey: string;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export interface SignRequestInput {
|
|
15
|
+
readonly method: string;
|
|
16
|
+
/** The URL as sent: its path already percent-encoded per segment (`encodeRfc3986`), its query in any order. */
|
|
17
|
+
readonly url: URL;
|
|
18
|
+
/** Headers to sign besides `host`, `x-amz-date` and `x-amz-content-sha256`, which the signer sets. */
|
|
19
|
+
readonly headers?: Readonly<Record<string, string>>;
|
|
20
|
+
/** Lowercase hex SHA-256 of the body (`EMPTY_PAYLOAD_SHA256` for none). */
|
|
21
|
+
readonly payloadSha256: string;
|
|
22
|
+
readonly region: string;
|
|
23
|
+
readonly service?: string;
|
|
24
|
+
readonly credentials: SigV4Credentials;
|
|
25
|
+
readonly date: Date;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** RFC 3986 percent-encoding as SigV4 wants it: everything but `A-Z a-z 0-9 - . _ ~` is escaped. */
|
|
29
|
+
export function encodeRfc3986(value: string): string {
|
|
30
|
+
return encodeURIComponent(value).replace(/[!'()*]/g, (char) => `%${char.charCodeAt(0).toString(16).toUpperCase()}`);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function sha256Hex(value: string): string {
|
|
34
|
+
return createHash("sha256").update(value, "utf8").digest("hex");
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function hmac(key: Buffer | string, value: string): Buffer {
|
|
38
|
+
return createHmac("sha256", key).update(value, "utf8").digest();
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** `20130524T000000Z`. */
|
|
42
|
+
function formatAmzDate(date: Date): string {
|
|
43
|
+
return date
|
|
44
|
+
.toISOString()
|
|
45
|
+
.replace(/[-:]/g, "")
|
|
46
|
+
.replace(/\.\d{3}/, "");
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** The request's headers with `authorization` added: every header in the result is signed. */
|
|
50
|
+
export function signRequest(input: SignRequestInput): Record<string, string> {
|
|
51
|
+
const service = input.service ?? "s3";
|
|
52
|
+
const amzDate = formatAmzDate(input.date);
|
|
53
|
+
const day = amzDate.slice(0, 8);
|
|
54
|
+
const headers: Record<string, string> = {};
|
|
55
|
+
for (const [name, value] of Object.entries(input.headers ?? {})) headers[name.toLowerCase()] = value;
|
|
56
|
+
headers.host = input.url.host;
|
|
57
|
+
headers["x-amz-content-sha256"] = input.payloadSha256;
|
|
58
|
+
headers["x-amz-date"] = amzDate;
|
|
59
|
+
|
|
60
|
+
const names = Object.keys(headers).sort();
|
|
61
|
+
const canonicalHeaders = names.map((name) => `${name}:${(headers[name] ?? "").trim().replace(/\s+/g, " ")}\n`).join("");
|
|
62
|
+
const signedHeaders = names.join(";");
|
|
63
|
+
const canonicalQuery = [...input.url.searchParams.entries()]
|
|
64
|
+
.map(([name, value]) => [encodeRfc3986(name), encodeRfc3986(value)] as const)
|
|
65
|
+
.sort(([a, aValue], [b, bValue]) => (a === b ? compare(aValue, bValue) : compare(a, b)))
|
|
66
|
+
.map(([name, value]) => `${name}=${value}`)
|
|
67
|
+
.join("&");
|
|
68
|
+
const canonicalRequest = [input.method, input.url.pathname, canonicalQuery, canonicalHeaders, signedHeaders, input.payloadSha256].join("\n");
|
|
69
|
+
|
|
70
|
+
const scope = `${day}/${input.region}/${service}/aws4_request`;
|
|
71
|
+
const stringToSign = [ALGORITHM, amzDate, scope, sha256Hex(canonicalRequest)].join("\n");
|
|
72
|
+
const signingKey = hmac(hmac(hmac(hmac(`AWS4${input.credentials.secretAccessKey}`, day), input.region), service), "aws4_request");
|
|
73
|
+
const signature = createHmac("sha256", signingKey).update(stringToSign, "utf8").digest("hex");
|
|
74
|
+
headers.authorization = `${ALGORITHM} Credential=${input.credentials.accessKeyId}/${scope}, SignedHeaders=${signedHeaders}, Signature=${signature}`;
|
|
75
|
+
return headers;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** Byte order, as SigV4 sorts (not the locale's). */
|
|
79
|
+
function compare(a: string, b: string): number {
|
|
80
|
+
if (a === b) return 0;
|
|
81
|
+
return a < b ? -1 : 1;
|
|
82
|
+
}
|
package/src/storage.ts
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
// The storage driver contract: where the bytes of a file live. The module keeps the rows; a driver keeps only the
|
|
2
|
+
// blobs, under keys the module makes from the file id (`ab/cd/<uuid>`), never from user input.
|
|
3
|
+
import type { ByteRange } from "./contract.js";
|
|
4
|
+
|
|
5
|
+
/** A file's bytes as a driver receives them: a Node stream, a web stream or any async iterable of chunks. */
|
|
6
|
+
export type FileBody = AsyncIterable<Uint8Array>;
|
|
7
|
+
|
|
8
|
+
/** What a driver's health probe found. */
|
|
9
|
+
export interface StorageCheck {
|
|
10
|
+
/** A probe object could be written and deleted. */
|
|
11
|
+
readonly isWritable: boolean;
|
|
12
|
+
/** The free bytes where the files live, or null when the driver cannot tell (object storage). */
|
|
13
|
+
readonly freeBytes: number | null;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export interface FileStorage {
|
|
17
|
+
/** A short name for logs and the README (`local`, `s3`, `memory`). */
|
|
18
|
+
readonly kind: string;
|
|
19
|
+
/**
|
|
20
|
+
* Stores `body` under `key`, replacing nothing visible until the whole body is written: a body that throws half
|
|
21
|
+
* way leaves no object behind and the error propagates.
|
|
22
|
+
*/
|
|
23
|
+
put(body: FileBody, key: string): Promise<void>;
|
|
24
|
+
/** The object's bytes, or the inclusive `range` of them; null when there is no such object. */
|
|
25
|
+
get(key: string, range?: ByteRange): Promise<ReadableStream<Uint8Array> | null>;
|
|
26
|
+
/** The object's size, or null when there is no such object. */
|
|
27
|
+
head(key: string): Promise<{ readonly size: number } | null>;
|
|
28
|
+
/** Deletes the object; deleting a missing one is not an error. */
|
|
29
|
+
delete(key: string): Promise<void>;
|
|
30
|
+
/** Probes the storage for the health check; a driver without one counts as healthy. */
|
|
31
|
+
check?(): Promise<StorageCheck>;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** Whether a value has the driver methods (the options schema's check). */
|
|
35
|
+
export function isFileStorage(value: unknown): value is FileStorage {
|
|
36
|
+
if (typeof value !== "object" || value === null) return false;
|
|
37
|
+
const candidate = value as Record<string, unknown>;
|
|
38
|
+
return (
|
|
39
|
+
typeof candidate.kind === "string" &&
|
|
40
|
+
["put", "get", "head", "delete"].every((name) => typeof candidate[name] === "function") &&
|
|
41
|
+
(candidate.check === undefined || typeof candidate.check === "function")
|
|
42
|
+
);
|
|
43
|
+
}
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
// Test helpers of @softure-ai/files: an in-memory storage driver, a fixed link keyring and link checks.
|
|
2
|
+
export { createMemoryStorage, type MemoryStorage } from "./memory-storage.js";
|
|
3
|
+
export { createTestLinkKeys, fetchFileLink, readFileLinkExpiry, type FetchFileLinkOptions, type FileLinkAnswer } from "./links.js";
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
// Link helpers for tests: a fixed keyring, a request to a link through the same code the route runs, and the
|
|
2
|
+
// expiry a link carries.
|
|
3
|
+
import type { FilesContext } from "../server/context.js";
|
|
4
|
+
import { FILES_LINK_KEYS_ENV } from "../server/keys.js";
|
|
5
|
+
import { serveFileLink } from "../server/serve.js";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* An environment with a link keyring of the given key ids (first one signs), each key derived from its id, so two
|
|
9
|
+
* calls agree: `env: createTestLinkKeys(["new", "old"])`. Never for production.
|
|
10
|
+
*/
|
|
11
|
+
export function createTestLinkKeys(ids: readonly string[] = ["test"]): Readonly<Record<string, string>> {
|
|
12
|
+
const entries = ids.map((id) => `${id}:${Buffer.alloc(32, `softure-files-test-${id}`).toString("base64url")}`);
|
|
13
|
+
return { [FILES_LINK_KEYS_ENV]: entries.join(",") };
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export interface FetchFileLinkOptions {
|
|
17
|
+
readonly method?: "GET" | "HEAD";
|
|
18
|
+
/** A `Range` header value, e.g. `bytes=0-99`. */
|
|
19
|
+
readonly range?: string;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** What a link answered, with the body read. */
|
|
23
|
+
export interface FileLinkAnswer {
|
|
24
|
+
readonly status: number;
|
|
25
|
+
readonly headers: Readonly<Record<string, string>>;
|
|
26
|
+
readonly body: Uint8Array;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** Requests a link (a URL or a path) as the route would serve it, at the context's clock. */
|
|
30
|
+
export async function fetchFileLink(ctx: FilesContext, link: string, options: FetchFileLinkOptions = {}): Promise<FileLinkAnswer> {
|
|
31
|
+
const url = new URL(link, ctx.config.appOrigin);
|
|
32
|
+
const headers: Record<string, string> = options.range === undefined ? {} : { range: options.range };
|
|
33
|
+
const response = await serveFileLink(ctx, new Request(url, { method: options.method ?? "GET", headers }));
|
|
34
|
+
return {
|
|
35
|
+
status: response.status,
|
|
36
|
+
headers: Object.fromEntries(response.headers.entries()),
|
|
37
|
+
body: new Uint8Array(await response.arrayBuffer()),
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** The expiry a link carries in its path (it may still die earlier: revoked, deleted). */
|
|
42
|
+
export function readFileLinkExpiry(link: string): Date {
|
|
43
|
+
const segments = new URL(link, "http://localhost").pathname.split("/");
|
|
44
|
+
const expiry = segments.at(-3);
|
|
45
|
+
if (expiry === undefined || !/^\d+$/.test(expiry)) throw new RangeError("readFileLinkExpiry: not a file link");
|
|
46
|
+
return new Date(Number(expiry) * 1000);
|
|
47
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
// An in-memory storage driver for tests: the same contract as the disk and S3 drivers, plus a look at what it holds
|
|
2
|
+
// and a switch to make the next writes fail.
|
|
3
|
+
import type { ByteRange } from "../contract.js";
|
|
4
|
+
import type { FileBody, FileStorage, StorageCheck } from "../storage.js";
|
|
5
|
+
|
|
6
|
+
export interface MemoryStorage extends FileStorage {
|
|
7
|
+
/** The stored objects by key (a copy). */
|
|
8
|
+
readonly objects: () => ReadonlyMap<string, Uint8Array>;
|
|
9
|
+
/** What `check()` answers; healthy with plenty of space by default. */
|
|
10
|
+
setCheck(check: StorageCheck): void;
|
|
11
|
+
/** Makes `put` (after reading the whole body) and `delete` throw until called with false. */
|
|
12
|
+
setFailing(isFailing: boolean): void;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function createMemoryStorage(): MemoryStorage {
|
|
16
|
+
const objects = new Map<string, Uint8Array>();
|
|
17
|
+
let health: StorageCheck = {
|
|
18
|
+
isWritable: true,
|
|
19
|
+
freeBytes: Number.MAX_SAFE_INTEGER,
|
|
20
|
+
};
|
|
21
|
+
let isFailing = false;
|
|
22
|
+
|
|
23
|
+
return {
|
|
24
|
+
kind: "memory",
|
|
25
|
+
objects: () => new Map(objects),
|
|
26
|
+
setCheck(check) {
|
|
27
|
+
health = check;
|
|
28
|
+
},
|
|
29
|
+
setFailing(value) {
|
|
30
|
+
isFailing = value;
|
|
31
|
+
},
|
|
32
|
+
async put(body: FileBody, key: string) {
|
|
33
|
+
const chunks: Uint8Array[] = [];
|
|
34
|
+
for await (const chunk of body) chunks.push(chunk);
|
|
35
|
+
if (isFailing) throw new Error("memory storage: put failed (setFailing)");
|
|
36
|
+
objects.set(key, Buffer.concat(chunks));
|
|
37
|
+
},
|
|
38
|
+
get(key: string, range?: ByteRange) {
|
|
39
|
+
const bytes = objects.get(key);
|
|
40
|
+
if (bytes === undefined) return Promise.resolve(null);
|
|
41
|
+
const slice = range === undefined ? bytes : bytes.subarray(range.start, range.end + 1);
|
|
42
|
+
return Promise.resolve(
|
|
43
|
+
new ReadableStream<Uint8Array>({
|
|
44
|
+
start(controller) {
|
|
45
|
+
controller.enqueue(new Uint8Array(slice));
|
|
46
|
+
controller.close();
|
|
47
|
+
},
|
|
48
|
+
}),
|
|
49
|
+
);
|
|
50
|
+
},
|
|
51
|
+
head(key: string) {
|
|
52
|
+
const bytes = objects.get(key);
|
|
53
|
+
return Promise.resolve(bytes === undefined ? null : { size: bytes.byteLength });
|
|
54
|
+
},
|
|
55
|
+
delete(key: string) {
|
|
56
|
+
if (isFailing) return Promise.reject(new Error("memory storage: delete failed (setFailing)"));
|
|
57
|
+
objects.delete(key);
|
|
58
|
+
return Promise.resolve();
|
|
59
|
+
},
|
|
60
|
+
check() {
|
|
61
|
+
return Promise.resolve(health);
|
|
62
|
+
},
|
|
63
|
+
};
|
|
64
|
+
}
|