@ttt-productions/media-processing-core 0.8.17 → 0.8.18
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/dist/audio/audio-processor.d.ts.map +1 -1
- package/dist/audio/audio-processor.js +1 -2
- package/dist/audio/audio-processor.js.map +1 -1
- package/dist/audio/probe.d.ts.map +1 -1
- package/dist/audio/probe.js +0 -2
- package/dist/audio/probe.js.map +1 -1
- package/dist/duration/media-duration.d.ts +0 -11
- package/dist/duration/media-duration.d.ts.map +1 -1
- package/dist/duration/media-duration.js +1 -28
- package/dist/duration/media-duration.js.map +1 -1
- package/dist/image/formats.d.ts +0 -11
- package/dist/image/formats.d.ts.map +1 -1
- package/dist/image/formats.js +0 -10
- package/dist/image/formats.js.map +1 -1
- package/dist/image/image-processor.d.ts.map +1 -1
- package/dist/image/image-processor.js +1 -20
- package/dist/image/image-processor.js.map +1 -1
- package/dist/image/resize.d.ts +0 -14
- package/dist/image/resize.d.ts.map +1 -1
- package/dist/image/resize.js +0 -15
- package/dist/image/resize.js.map +1 -1
- package/dist/inspection/inspect-media.d.ts +0 -7
- package/dist/inspection/inspect-media.d.ts.map +1 -1
- package/dist/inspection/inspect-media.js +4 -64
- package/dist/inspection/inspect-media.js.map +1 -1
- package/dist/io/fs.d.ts.map +1 -1
- package/dist/io/fs.js +0 -1
- package/dist/io/fs.js.map +1 -1
- package/dist/io/local-input.d.ts +0 -20
- package/dist/io/local-input.d.ts.map +1 -1
- package/dist/io/local-input.js +0 -20
- package/dist/io/local-input.js.map +1 -1
- package/dist/io/types.d.ts +0 -3
- package/dist/io/types.d.ts.map +1 -1
- package/dist/io/types.js.map +1 -1
- package/dist/moderation/merge.d.ts.map +1 -1
- package/dist/moderation/merge.js +0 -1
- package/dist/moderation/merge.js.map +1 -1
- package/dist/moderation/types.d.ts +0 -2
- package/dist/moderation/types.d.ts.map +1 -1
- package/dist/moderation/types.js.map +1 -1
- package/dist/run-pipeline.d.ts +0 -17
- package/dist/run-pipeline.d.ts.map +1 -1
- package/dist/run-pipeline.js +2 -27
- package/dist/run-pipeline.js.map +1 -1
- package/dist/server/download-token.d.ts +0 -6
- package/dist/server/download-token.d.ts.map +1 -1
- package/dist/server/download-token.js +0 -10
- package/dist/server/download-token.js.map +1 -1
- package/dist/server/firebase-media-io.d.ts +1 -11
- package/dist/server/firebase-media-io.d.ts.map +1 -1
- package/dist/server/firebase-media-io.js +0 -5
- package/dist/server/firebase-media-io.js.map +1 -1
- package/dist/server/gcs-errors.d.ts +0 -8
- package/dist/server/gcs-errors.d.ts.map +1 -1
- package/dist/server/gcs-errors.js +0 -8
- package/dist/server/gcs-errors.js.map +1 -1
- package/dist/server/pending-media.d.ts +1 -32
- package/dist/server/pending-media.d.ts.map +1 -1
- package/dist/server/pending-media.js +1 -24
- package/dist/server/pending-media.js.map +1 -1
- package/dist/server/r2-retry.d.ts +0 -48
- package/dist/server/r2-retry.d.ts.map +1 -1
- package/dist/server/r2-retry.js +4 -90
- package/dist/server/r2-retry.js.map +1 -1
- package/dist/server/staged-object.d.ts +0 -44
- package/dist/server/staged-object.d.ts.map +1 -1
- package/dist/server/staged-object.js +0 -44
- package/dist/server/staged-object.js.map +1 -1
- package/dist/server/storage-ops.d.ts +2 -62
- package/dist/server/storage-ops.d.ts.map +1 -1
- package/dist/server/storage-ops.js +9 -88
- package/dist/server/storage-ops.js.map +1 -1
- package/dist/types.d.ts +0 -2
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/dist/utils/safe-path.d.ts.map +1 -1
- package/dist/utils/safe-path.js +1 -2
- package/dist/utils/safe-path.js.map +1 -1
- package/dist/video/ffmpeg.d.ts +0 -22
- package/dist/video/ffmpeg.d.ts.map +1 -1
- package/dist/video/ffmpeg.js +6 -21
- package/dist/video/ffmpeg.js.map +1 -1
- package/dist/video/probe.d.ts.map +1 -1
- package/dist/video/probe.js +0 -1
- package/dist/video/probe.js.map +1 -1
- package/dist/video/video-processor.d.ts.map +1 -1
- package/dist/video/video-processor.js +2 -15
- package/dist/video/video-processor.js.map +1 -1
- package/package.json +2 -2
|
@@ -1,14 +1,9 @@
|
|
|
1
1
|
import type { getStorage } from 'firebase-admin/storage';
|
|
2
|
-
/**
|
|
3
|
-
* A copy found an object at its destination key that is not this copy's result, and
|
|
4
|
-
* wrote nothing; that object is untouched. The key is a PROPERTY, never message text, so
|
|
5
|
-
* an escaping error never carries an object key to monitoring.
|
|
6
|
-
*/
|
|
7
2
|
export declare class ObjectAlreadyExistsError extends Error {
|
|
8
3
|
readonly key: string;
|
|
9
4
|
constructor(key: string);
|
|
10
5
|
}
|
|
11
|
-
/**
|
|
6
|
+
/** Inferred from usage so the package takes no @google-cloud/storage dependency. */
|
|
12
7
|
export type Bucket = ReturnType<ReturnType<typeof getStorage>['bucket']>;
|
|
13
8
|
export interface ObjectWriteResult {
|
|
14
9
|
key: string;
|
|
@@ -21,71 +16,25 @@ export interface ObjectReadResult {
|
|
|
21
16
|
}
|
|
22
17
|
export interface ObjectCopyResult {
|
|
23
18
|
key: string;
|
|
24
|
-
/**
|
|
25
|
-
* `true` when the destination already held this copy's result — the same source version,
|
|
26
|
-
* committed by an earlier attempt whose answer was lost or by a concurrent identical
|
|
27
|
-
* copy; `false` when this call committed it.
|
|
28
|
-
*/
|
|
29
19
|
alreadyPresent: boolean;
|
|
30
20
|
}
|
|
31
|
-
/**
|
|
32
|
-
* What `deleteCopy` found at the destination: `deleted` — a copy of the source, now removed;
|
|
33
|
-
* `absent` — no object; `notThisCopy` — an object this package did not copy from that
|
|
34
|
-
* source, left untouched.
|
|
35
|
-
*/
|
|
36
21
|
export type DeleteCopyResult = 'deleted' | 'absent' | 'notThisCopy';
|
|
37
22
|
export interface MediaObjectStore {
|
|
38
|
-
/**
|
|
39
|
-
* Upload a local file to the store at `key`. Objects are written with a
|
|
40
|
-
* one-year `immutable` cache-control, so keys are write-once: a replacement is
|
|
41
|
-
* written under a NEW key and the app repoints its reference to it. Overwriting
|
|
42
|
-
* an existing key would keep serving the old bytes from caches for up to a year.
|
|
43
|
-
*/
|
|
23
|
+
/** Objects carry a one-year `immutable` cache-control, so a key is write-once: an overwrite keeps serving the old bytes from caches. */
|
|
44
24
|
putFile(args: {
|
|
45
25
|
localPath: string;
|
|
46
26
|
key: string;
|
|
47
27
|
contentType?: string;
|
|
48
28
|
}): Promise<ObjectWriteResult>;
|
|
49
|
-
/**
|
|
50
|
-
* Server-side copy within the store. It never overwrites and is idempotent: it commits
|
|
51
|
-
* only when `toKey` holds no object, in one call that writes the destination fully
|
|
52
|
-
* formed (the source version's bytes and content headers, its provenance, and this
|
|
53
|
-
* store's token policy). When `toKey` already holds this copy's result — a retry of
|
|
54
|
-
* the same copy of the same source version — it resolves with `alreadyPresent: true`;
|
|
55
|
-
* when it holds anything else, it throws `ObjectAlreadyExistsError` and leaves that
|
|
56
|
-
* object untouched. It never deletes anything.
|
|
57
|
-
*/
|
|
58
29
|
copy(args: {
|
|
59
30
|
fromKey: string;
|
|
60
31
|
toKey: string;
|
|
61
32
|
}): Promise<ObjectCopyResult>;
|
|
62
|
-
/**
|
|
63
|
-
* Download an object's bytes from the store to a local file (streamed, so a
|
|
64
|
-
* large video never sits fully in memory). Used by safety evidence capture to
|
|
65
|
-
* pull final media — which may live in a different backend (R2) — before
|
|
66
|
-
* writing it into the separate (GCS) evidence vault: capture is inherently
|
|
67
|
-
* read-here-then-write, NOT a server-side copy, because source and vault can
|
|
68
|
-
* be different backends.
|
|
69
|
-
*/
|
|
70
33
|
readToFile(args: {
|
|
71
34
|
key: string;
|
|
72
35
|
localPath: string;
|
|
73
36
|
}): Promise<ObjectReadResult>;
|
|
74
|
-
/** Delete an object. Missing objects are a no-op, not an error. */
|
|
75
37
|
delete(key: string): Promise<void>;
|
|
76
|
-
/**
|
|
77
|
-
* Delete `toKey` only when the object there is a copy of `fromKey` this package made: its
|
|
78
|
-
* own recorded provenance must name `fromKey` as its source. Only the destination is read,
|
|
79
|
-
* so this works after the source is gone. Any other object — a copy of another source, or
|
|
80
|
-
* one with no provenance — is left untouched and answers `notThisCopy`.
|
|
81
|
-
*
|
|
82
|
-
* Firebase Storage deletes only the generation it checked; an object replaced after the
|
|
83
|
-
* check is checked again, never deleted blind. R2's DeleteObject takes no precondition, so
|
|
84
|
-
* there the delete follows the check unconditioned. That gap is harmless only under the key
|
|
85
|
-
* contract: keys are write-once and a copy never replaces an object, so in the gap the
|
|
86
|
-
* checked object can only be removed; and a copy's key is derived from its source, so the
|
|
87
|
-
* only object that can land there afterwards is another copy of the same source.
|
|
88
|
-
*/
|
|
89
38
|
deleteCopy(args: {
|
|
90
39
|
fromKey: string;
|
|
91
40
|
toKey: string;
|
|
@@ -96,29 +45,20 @@ export interface CreateR2ObjectStoreArgs {
|
|
|
96
45
|
accessKeyId: string;
|
|
97
46
|
secretAccessKey: string;
|
|
98
47
|
bucket: string;
|
|
99
|
-
/** Override for tests; defaults to the real R2 S3 endpoint. */
|
|
100
48
|
endpoint?: string;
|
|
101
|
-
/** Override fetch for tests. */
|
|
102
49
|
fetchImpl?: typeof fetch;
|
|
103
|
-
/** Test seam: override the retry sleep (defaults to real `setTimeout`). */
|
|
104
50
|
sleepImpl?: (ms: number) => Promise<void>;
|
|
105
|
-
/** Test seam: override the backoff jitter source (defaults to `Math.random`). */
|
|
106
51
|
randomImpl?: () => number;
|
|
107
|
-
/** Test seam: override the clock used for HTTP-date `Retry-After` (defaults to `Date.now`). */
|
|
108
52
|
nowImpl?: () => number;
|
|
109
53
|
}
|
|
110
54
|
export declare function createR2ObjectStore(args: CreateR2ObjectStoreArgs): MediaObjectStore;
|
|
111
55
|
export interface CreateFirebaseStorageObjectStoreArgs {
|
|
112
56
|
bucket: Bucket;
|
|
113
57
|
}
|
|
114
|
-
/** Firebase Storage store for deployed environments. Token-free (see header). */
|
|
115
58
|
export declare function createFirebaseStorageObjectStore(args: CreateFirebaseStorageObjectStoreArgs): MediaObjectStore;
|
|
116
59
|
export interface CreateEmulatorObjectStoreArgs extends CreateFirebaseStorageObjectStoreArgs {
|
|
117
|
-
/** Fixed token attached to every object so deterministic emulator URLs work
|
|
118
|
-
* (token URLs bypass rules in the emulator). Pass the app's constant. */
|
|
119
60
|
downloadToken: string;
|
|
120
61
|
}
|
|
121
|
-
/** Local dev / tests only: the Firebase Storage store plus a fixed download token. */
|
|
122
62
|
export declare function createFirebaseEmulatorObjectStore(args: CreateEmulatorObjectStoreArgs): MediaObjectStore;
|
|
123
63
|
export declare function getMimeFromExt(ext: string): string;
|
|
124
64
|
//# sourceMappingURL=storage-ops.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"storage-ops.d.ts","sourceRoot":"","sources":["../../src/server/storage-ops.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"storage-ops.d.ts","sourceRoot":"","sources":["../../src/server/storage-ops.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAC;AAczD,qBAAa,wBAAyB,SAAQ,KAAK;aACrB,GAAG,EAAE,MAAM;gBAAX,GAAG,EAAE,MAAM;CAIxC;AAED,oFAAoF;AACpF,MAAM,MAAM,MAAM,GAAG,UAAU,CAAC,UAAU,CAAC,OAAO,UAAU,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC;AAEzE,MAAM,WAAW,iBAAiB;IAChC,GAAG,EAAE,MAAM,CAAC;IACZ,SAAS,EAAE,MAAM,CAAC;IAClB,WAAW,EAAE,MAAM,CAAC;CACrB;AAED,MAAM,WAAW,gBAAgB;IAC/B,SAAS,EAAE,MAAM,CAAC;IAClB,WAAW,EAAE,MAAM,CAAC;CACrB;AAED,MAAM,WAAW,gBAAgB;IAC/B,GAAG,EAAE,MAAM,CAAC;IACZ,cAAc,EAAE,OAAO,CAAC;CACzB;AAED,MAAM,MAAM,gBAAgB,GAAG,SAAS,GAAG,QAAQ,GAAG,aAAa,CAAC;AA2CpE,MAAM,WAAW,gBAAgB;IAC/B,wIA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AE,oCAAoC,GACzC,gBAAgB,CAElB;AAED,MAAM,WAAW,6BAA8B,SAAQ,oCAAoC;IACzF,aAAa,EAAE,MAAM,CAAC;CACvB;AAED,wBAAgB,iCAAiC,CAC/C,IAAI,EAAE,6BAA6B,GAClC,gBAAgB,CAElB;AAsHD,wBAAgB,cAAc,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAsBlD"}
|
|
@@ -1,22 +1,3 @@
|
|
|
1
|
-
// Final-media object-store chokepoint — every final-media write/copy/delete in
|
|
2
|
-
// the system goes through a MediaObjectStore. Inline storage-provider calls for
|
|
3
|
-
// FINAL media are forbidden outside this module. (Upload STAGING stays plain
|
|
4
|
-
// Firebase Storage and is not this module's concern.)
|
|
5
|
-
//
|
|
6
|
-
// Implementations (the consuming app picks one per environment):
|
|
7
|
-
// - R2 (S3-compatible) for deployed environments whose final media lives in
|
|
8
|
-
// R2 and is served by an edge gateway.
|
|
9
|
-
// - Firebase Storage for deployed environments whose final media lives in
|
|
10
|
-
// Firebase Storage. Token-free: it writes and copies no download token, so
|
|
11
|
-
// its objects are readable only through the app's own access path (Storage
|
|
12
|
-
// rules). Firebase can still mint a token later (a client getDownloadURL, the
|
|
13
|
-
// console); the app's read path must never do so.
|
|
14
|
-
// - Firebase Storage emulator for local dev/tests — the Firebase Storage store
|
|
15
|
-
// plus a FIXED caller-supplied download token on every object, so
|
|
16
|
-
// deterministic emulator URLs work. Production NEVER mints download tokens.
|
|
17
|
-
//
|
|
18
|
-
// Writes return `{ key }` — never URLs. Display URLs are built at render time
|
|
19
|
-
// from asset refs (see the consuming app's media-asset-url helper).
|
|
20
1
|
import { createReadStream, createWriteStream } from 'node:fs';
|
|
21
2
|
import { stat } from 'node:fs/promises';
|
|
22
3
|
import { Readable } from 'node:stream';
|
|
@@ -26,11 +7,6 @@ import { NEUTRAL_CONTENT_TYPE } from '@ttt-productions/media-schemas';
|
|
|
26
7
|
import { isObjectNotFoundError } from './gcs-errors.js';
|
|
27
8
|
import { defaultR2RetrySeams, fetchWithR2Retry, R2StorageError, } from './r2-retry.js';
|
|
28
9
|
import { requireStorageObjectGeneration, UnreadableStorageObjectMetadataError, } from './staged-object.js';
|
|
29
|
-
/**
|
|
30
|
-
* A copy found an object at its destination key that is not this copy's result, and
|
|
31
|
-
* wrote nothing; that object is untouched. The key is a PROPERTY, never message text, so
|
|
32
|
-
* an escaping error never carries an object key to monitoring.
|
|
33
|
-
*/
|
|
34
10
|
export class ObjectAlreadyExistsError extends Error {
|
|
35
11
|
key;
|
|
36
12
|
constructor(key) {
|
|
@@ -39,10 +15,7 @@ export class ObjectAlreadyExistsError extends Error {
|
|
|
39
15
|
this.name = 'ObjectAlreadyExistsError';
|
|
40
16
|
}
|
|
41
17
|
}
|
|
42
|
-
//
|
|
43
|
-
// destination already exists compares this record with its own, so it recognizes its
|
|
44
|
-
// own earlier result and never mistakes another writer's object for it. The values are
|
|
45
|
-
// URI-encoded because R2 carries custom metadata in HTTP headers, which must be ASCII.
|
|
18
|
+
// URI-encoded: R2 carries custom metadata in HTTP headers, which must be ASCII.
|
|
46
19
|
const COPY_SOURCE_METADATA_KEY = 'copy-source';
|
|
47
20
|
const COPY_SOURCE_VERSION_METADATA_KEY = 'copy-source-version';
|
|
48
21
|
function copyProvenance(fromKey, sourceVersion) {
|
|
@@ -51,22 +24,15 @@ function copyProvenance(fromKey, sourceVersion) {
|
|
|
51
24
|
[COPY_SOURCE_VERSION_METADATA_KEY]: encodeURIComponent(sourceVersion),
|
|
52
25
|
};
|
|
53
26
|
}
|
|
54
|
-
/** Whether an object's custom metadata, read through `readMetadata`, names exactly this provenance. */
|
|
55
27
|
function holdsProvenance(readMetadata, provenance) {
|
|
56
28
|
return Object.entries(provenance).every(([name, value]) => readMetadata(name) === value);
|
|
57
29
|
}
|
|
58
|
-
/**
|
|
59
|
-
* Whether an object's custom metadata records a copy of `fromKey`, of whichever source
|
|
60
|
-
* version: `deleteCopy` reads only the destination, so it cannot know the source's version.
|
|
61
|
-
* A record naming the source without a version is not provenance this package wrote.
|
|
62
|
-
*/
|
|
63
30
|
function recordsCopyOf(readMetadata, fromKey) {
|
|
64
31
|
const version = readMetadata(COPY_SOURCE_VERSION_METADATA_KEY);
|
|
65
32
|
return (readMetadata(COPY_SOURCE_METADATA_KEY) === encodeURIComponent(fromKey) &&
|
|
66
33
|
typeof version === 'string' &&
|
|
67
34
|
version !== '');
|
|
68
35
|
}
|
|
69
|
-
/** The content headers a copy carries from its source: HTTP header name → Cloud Storage field. */
|
|
70
36
|
const CARRIED_CONTENT_HEADERS = {
|
|
71
37
|
'content-type': 'contentType',
|
|
72
38
|
'cache-control': 'cacheControl',
|
|
@@ -83,9 +49,6 @@ export function createR2ObjectStore(args) {
|
|
|
83
49
|
region: 'auto',
|
|
84
50
|
});
|
|
85
51
|
const doFetch = args.fetchImpl ?? fetch;
|
|
86
|
-
// Retry seams: production defaults are internal; tests inject deterministic
|
|
87
|
-
// sleep/random/now. putFile/copy/readToFile ride the bounded retry helper;
|
|
88
|
-
// delete stays one-shot (never routes through it — see below).
|
|
89
52
|
const defaults = defaultR2RetrySeams();
|
|
90
53
|
const seams = {
|
|
91
54
|
sleep: args.sleepImpl ?? defaults.sleep,
|
|
@@ -97,8 +60,7 @@ export function createR2ObjectStore(args) {
|
|
|
97
60
|
const signed = await client.sign(url, { ...init, aws: { allHeaders: true } });
|
|
98
61
|
return doFetch(signed, { ...(init.body ? { duplex: 'half' } : {}) });
|
|
99
62
|
}
|
|
100
|
-
// A GET
|
|
101
|
-
// body, so the S3 error code that tells a missing key from a missing bucket would be lost.
|
|
63
|
+
// A GET, not a HEAD: a HEAD's error response has no body, so the S3 error code would be lost.
|
|
102
64
|
async function readObjectHeaders(key, operation) {
|
|
103
65
|
return fetchWithR2Retry({
|
|
104
66
|
operation,
|
|
@@ -114,10 +76,7 @@ export function createR2ObjectStore(args) {
|
|
|
114
76
|
},
|
|
115
77
|
});
|
|
116
78
|
}
|
|
117
|
-
// R2 exposes no object version id, so a source version is its ETag, last-modified time,
|
|
118
|
-
// and size together: replacing the source changes at least one of them unless the new
|
|
119
|
-
// content has the same MD5-derived ETag and lands within the same second. A multipart
|
|
120
|
-
// upload's ETag is not the content's MD5, but it is the same kind of version token.
|
|
79
|
+
// R2 exposes no object version id, so a source version is its ETag, last-modified time, and size together.
|
|
121
80
|
function readSourceVersion(headers, key) {
|
|
122
81
|
const etag = headers.get('etag');
|
|
123
82
|
if (!etag)
|
|
@@ -132,8 +91,6 @@ export function createR2ObjectStore(args) {
|
|
|
132
91
|
}
|
|
133
92
|
return {
|
|
134
93
|
async putFile({ localPath, key, contentType }) {
|
|
135
|
-
// stat() once, outside the retry loop: a missing input file is a
|
|
136
|
-
// deterministic local error and must fail immediately, never retry.
|
|
137
94
|
const { size } = await stat(localPath);
|
|
138
95
|
const ct = contentType ?? getMimeFromExt(extractExt(localPath));
|
|
139
96
|
return fetchWithR2Retry({
|
|
@@ -141,8 +98,7 @@ export function createR2ObjectStore(args) {
|
|
|
141
98
|
bucket: args.bucket,
|
|
142
99
|
key,
|
|
143
100
|
seams,
|
|
144
|
-
//
|
|
145
|
-
// consumed stream is never reused.
|
|
101
|
+
// A fresh signed request and read stream on every attempt: a consumed stream cannot be re-read.
|
|
146
102
|
runAttempt: async () => {
|
|
147
103
|
const res = await signedFetch(objectUrl(key), {
|
|
148
104
|
method: 'PUT',
|
|
@@ -166,13 +122,9 @@ export function createR2ObjectStore(args) {
|
|
|
166
122
|
const provenance = copyProvenance(fromKey, version);
|
|
167
123
|
const headers = {
|
|
168
124
|
'x-amz-copy-source': `/${args.bucket}/${fromKey.split('/').map(encodeURIComponent).join('/')}`,
|
|
169
|
-
// Pins the copy to the source version the provenance names.
|
|
170
125
|
'x-amz-copy-source-if-match': etag,
|
|
171
|
-
// R2's destination-conditional CopyObject extension
|
|
172
|
-
// S3's x-amz-copy-source-if-* headers condition only the source.
|
|
126
|
+
// R2's destination-conditional CopyObject extension; S3's x-amz-copy-source-if-* headers condition only the source.
|
|
173
127
|
'cf-copy-destination-if-none-match': '*',
|
|
174
|
-
// The destination takes exactly the headers below rather than the source's, so it
|
|
175
|
-
// commits fully formed in this one call.
|
|
176
128
|
'x-amz-metadata-directive': 'REPLACE',
|
|
177
129
|
};
|
|
178
130
|
for (const name of Object.keys(CARRIED_CONTENT_HEADERS)) {
|
|
@@ -200,8 +152,7 @@ export function createR2ObjectStore(args) {
|
|
|
200
152
|
catch (e) {
|
|
201
153
|
if (!(e instanceof R2StorageError && e.status === 412))
|
|
202
154
|
throw e;
|
|
203
|
-
// R2 answers 412 both for an existing destination and for a source that changed
|
|
204
|
-
// after it was read; only an existing destination can be this copy's result.
|
|
155
|
+
// R2 answers 412 both for an existing destination and for a source that changed after it was read.
|
|
205
156
|
let destination;
|
|
206
157
|
try {
|
|
207
158
|
destination = await readObjectHeaders(toKey, 'copy');
|
|
@@ -229,12 +180,7 @@ export function createR2ObjectStore(args) {
|
|
|
229
180
|
return { kind: 'response', response: res };
|
|
230
181
|
}
|
|
231
182
|
const contentType = res.headers.get('content-type') ?? NEUTRAL_CONTENT_TYPE;
|
|
232
|
-
//
|
|
233
|
-
// truncating mode ('w') on every attempt so a partial download from a
|
|
234
|
-
// prior attempt can never prefix/corrupt this one. res.body is a web
|
|
235
|
-
// ReadableStream; stream it to disk so large media never sits fully in
|
|
236
|
-
// memory. A transient body/network stream failure here throws and is
|
|
237
|
-
// retried by the helper; a deterministic local fs error is not.
|
|
183
|
+
// Truncating mode ('w') on every attempt, so a partial earlier download never prefixes this one.
|
|
238
184
|
await pipeline(Readable.fromWeb(res.body), createWriteStream(localPath, { flags: 'w' }));
|
|
239
185
|
const { size } = await stat(localPath);
|
|
240
186
|
return { kind: 'success', value: { sizeBytes: size, contentType } };
|
|
@@ -242,7 +188,6 @@ export function createR2ObjectStore(args) {
|
|
|
242
188
|
});
|
|
243
189
|
},
|
|
244
190
|
async delete(key) {
|
|
245
|
-
// Intentionally one-shot: delete is never retried (see r2-retry.ts header).
|
|
246
191
|
const res = await signedFetch(objectUrl(key), { method: 'DELETE' });
|
|
247
192
|
// S3 DELETE returns 204 even for missing keys; anything else non-ok is real.
|
|
248
193
|
if (!res.ok && res.status !== 404) {
|
|
@@ -268,9 +213,7 @@ export function createR2ObjectStore(args) {
|
|
|
268
213
|
}
|
|
269
214
|
if (!recordsCopyOf((name) => destination.get(`x-amz-meta-${name}`), fromKey))
|
|
270
215
|
return 'notThisCopy';
|
|
271
|
-
//
|
|
272
|
-
// it silently ignored would only look like protection. `MediaObjectStore.deleteCopy`
|
|
273
|
-
// states why the gap after the check is harmless. One-shot, like `delete`.
|
|
216
|
+
// R2's DeleteObject honors no precondition header, so none is sent.
|
|
274
217
|
const res = await signedFetch(objectUrl(toKey), { method: 'DELETE' });
|
|
275
218
|
if (res.ok)
|
|
276
219
|
return 'deleted';
|
|
@@ -296,26 +239,14 @@ async function safeText(res) {
|
|
|
296
239
|
return '<no body>';
|
|
297
240
|
}
|
|
298
241
|
}
|
|
299
|
-
// ---------------------------------------------------------------------------
|
|
300
|
-
// Firebase Storage stores — deployed (token-free) and emulator (fixed token)
|
|
301
|
-
// ---------------------------------------------------------------------------
|
|
302
|
-
/** Custom-metadata key Firebase Storage reads download tokens from. */
|
|
303
242
|
const DOWNLOAD_TOKENS_METADATA_KEY = 'firebaseStorageDownloadTokens';
|
|
304
|
-
/**
|
|
305
|
-
* How many times `deleteCopy` checks a Firebase Storage destination before giving up. Keys
|
|
306
|
-
* are write-once, so one change between the check and the delete is already rare; an object
|
|
307
|
-
* still changing after that is left alone and the precondition failure is rethrown.
|
|
308
|
-
*/
|
|
309
243
|
const DELETE_COPY_MAX_READS = 2;
|
|
310
|
-
/** Firebase Storage store for deployed environments. Token-free (see header). */
|
|
311
244
|
export function createFirebaseStorageObjectStore(args) {
|
|
312
245
|
return createFirebaseStore(args.bucket, null);
|
|
313
246
|
}
|
|
314
|
-
/** Local dev / tests only: the Firebase Storage store plus a fixed download token. */
|
|
315
247
|
export function createFirebaseEmulatorObjectStore(args) {
|
|
316
248
|
return createFirebaseStore(args.bucket, args.downloadToken);
|
|
317
249
|
}
|
|
318
|
-
/** `downloadToken` is the store's token policy: `null` = token-free, a string = that token on every object. */
|
|
319
250
|
function createFirebaseStore(bucket, downloadToken) {
|
|
320
251
|
async function deleteObject(key) {
|
|
321
252
|
try {
|
|
@@ -331,8 +262,6 @@ function createFirebaseStore(bucket, downloadToken) {
|
|
|
331
262
|
async putFile({ localPath, key, contentType }) {
|
|
332
263
|
const { size } = await stat(localPath);
|
|
333
264
|
const ct = contentType ?? getMimeFromExt(extractExt(localPath));
|
|
334
|
-
// An upload replaces the object and all of its metadata, so a token-free
|
|
335
|
-
// put carries no custom metadata at all.
|
|
336
265
|
await bucket.upload(localPath, {
|
|
337
266
|
destination: key,
|
|
338
267
|
metadata: {
|
|
@@ -359,11 +288,7 @@ function createFirebaseStore(bucket, downloadToken) {
|
|
|
359
288
|
contentHeaders[field] = value;
|
|
360
289
|
}
|
|
361
290
|
try {
|
|
362
|
-
// A rewrite's resource
|
|
363
|
-
// the source's, so this one call commits the destination fully formed: the source's
|
|
364
|
-
// content headers, the provenance, and this store's token policy — never a token the
|
|
365
|
-
// source carried. The source is pinned to the generation the provenance names, so a
|
|
366
|
-
// 412 can only mean the destination exists; a replaced source fails as not-found.
|
|
291
|
+
// A rewrite's resource replaces the destination's metadata rather than merging with the source's.
|
|
367
292
|
await bucket.file(fromKey, { generation }).copy(bucket.file(toKey), {
|
|
368
293
|
...contentHeaders,
|
|
369
294
|
metadata: downloadToken === null
|
|
@@ -418,7 +343,6 @@ function createFirebaseStore(bucket, downloadToken) {
|
|
|
418
343
|
}
|
|
419
344
|
if (!recordsCopyOf((name) => custom?.[name], fromKey))
|
|
420
345
|
return 'notThisCopy';
|
|
421
|
-
// The delete names the generation just checked, so it can only remove that object.
|
|
422
346
|
const checked = requireStorageObjectGeneration({ generation }, { bucket: bucket.name, key: toKey });
|
|
423
347
|
try {
|
|
424
348
|
await file.delete({ ifGenerationMatch: checked });
|
|
@@ -427,7 +351,6 @@ function createFirebaseStore(bucket, downloadToken) {
|
|
|
427
351
|
catch (e) {
|
|
428
352
|
if (isObjectNotFoundError(e))
|
|
429
353
|
return 'absent';
|
|
430
|
-
// A 412 means the object changed after it was checked: check what is there now.
|
|
431
354
|
if (!isPreconditionFailedError(e) || read >= DELETE_COPY_MAX_READS)
|
|
432
355
|
throw e;
|
|
433
356
|
}
|
|
@@ -435,11 +358,9 @@ function createFirebaseStore(bucket, downloadToken) {
|
|
|
435
358
|
},
|
|
436
359
|
};
|
|
437
360
|
}
|
|
438
|
-
/** The Cloud Storage Admin SDK's numeric `412`: a generation precondition did not hold. */
|
|
439
361
|
function isPreconditionFailedError(e) {
|
|
440
362
|
return e?.code === 412;
|
|
441
363
|
}
|
|
442
|
-
// ---------------------------------------------------------------------------
|
|
443
364
|
function extractExt(localPath) {
|
|
444
365
|
return localPath.split('.').pop() || '';
|
|
445
366
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"storage-ops.js","sourceRoot":"","sources":["../../src/server/storage-ops.ts"],"names":[],"mappings":"AAAA,+EAA+E;AAC/E,gFAAgF;AAChF,6EAA6E;AAC7E,sDAAsD;AACtD,EAAE;AACF,iEAAiE;AACjE,6EAA6E;AAC7E,0CAA0C;AAC1C,2EAA2E;AAC3E,8EAA8E;AAC9E,8EAA8E;AAC9E,iFAAiF;AACjF,qDAAqD;AACrD,gFAAgF;AAChF,qEAAqE;AACrE,+EAA+E;AAC/E,EAAE;AACF,8EAA8E;AAC9E,oEAAoE;AAEpE,OAAO,EAAE,gBAAgB,EAAE,iBAAiB,EAAE,MAAM,SAAS,CAAC;AAC9D,OAAO,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAC;AACxC,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AACvC,OAAO,EAAE,QAAQ,EAAE,MAAM,sBAAsB,CAAC;AAChD,OAAO,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AAEt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AC,GAAW;IACxC,QAAQ,GAAG,CAAC,WAAW,EAAE,EAAE,CAAC;QAC1B,KAAK,KAAK,CAAC;QACX,KAAK,MAAM;YACT,OAAO,YAAY,CAAC;QACtB,KAAK,KAAK;YACR,OAAO,WAAW,CAAC;QACrB,KAAK,MAAM;YACT,OAAO,YAAY,CAAC;QACtB,KAAK,KAAK;YACR,OAAO,WAAW,CAAC;QACrB,KAAK,MAAM;YACT,OAAO,YAAY,CAAC;QACtB,KAAK,KAAK;YACR,OAAO,WAAW,CAAC;QACrB,KAAK,KAAK;YACR,OAAO,WAAW,CAAC;QACrB,KAAK,KAAK;YACR,OAAO,YAAY,CAAC;QACtB;YACE,OAAO,oBAAoB,CAAC;IAChC,CAAC;AACH,CAAC","sourcesContent":["// Final-media object-store chokepoint — every final-media write/copy/delete in\n// the system goes through a MediaObjectStore. Inline storage-provider calls for\n// FINAL media are forbidden outside this module. (Upload STAGING stays plain\n// Firebase Storage and is not this module's concern.)\n//\n// Implementations (the consuming app picks one per environment):\n// - R2 (S3-compatible) for deployed environments whose final media lives in\n// R2 and is served by an edge gateway.\n// - Firebase Storage for deployed environments whose final media lives in\n// Firebase Storage. Token-free: it writes and copies no download token, so\n// its objects are readable only through the app's own access path (Storage\n// rules). Firebase can still mint a token later (a client getDownloadURL, the\n// console); the app's read path must never do so.\n// - Firebase Storage emulator for local dev/tests — the Firebase Storage store\n// plus a FIXED caller-supplied download token on every object, so\n// deterministic emulator URLs work. Production NEVER mints download tokens.\n//\n// Writes return `{ key }` — never URLs. Display URLs are built at render time\n// from asset refs (see the consuming app's media-asset-url helper).\n\nimport { createReadStream, createWriteStream } from 'node:fs';\nimport { stat } from 'node:fs/promises';\nimport { Readable } from 'node:stream';\nimport { pipeline } from 'node:stream/promises';\nimport { AwsClient } from 'aws4fetch';\nimport type { getStorage } from 'firebase-admin/storage';\nimport { NEUTRAL_CONTENT_TYPE } from '@ttt-productions/media-schemas';\nimport { isObjectNotFoundError } from './gcs-errors.js';\nimport {\n defaultR2RetrySeams,\n fetchWithR2Retry,\n R2StorageError,\n type R2RetrySeams,\n} from './r2-retry.js';\nimport {\n requireStorageObjectGeneration,\n UnreadableStorageObjectMetadataError,\n} from './staged-object.js';\n\n/**\n * A copy found an object at its destination key that is not this copy's result, and\n * wrote nothing; that object is untouched. The key is a PROPERTY, never message text, so\n * an escaping error never carries an object key to monitoring.\n */\nexport class ObjectAlreadyExistsError extends Error {\n constructor(public readonly key: string) {\n super('copy destination already holds another object');\n this.name = 'ObjectAlreadyExistsError';\n }\n}\n\n/** Firebase Admin Storage Bucket — inferred to avoid @google-cloud/storage dep. */\nexport type Bucket = ReturnType<ReturnType<typeof getStorage>['bucket']>;\n\nexport interface ObjectWriteResult {\n key: string;\n sizeBytes: number;\n contentType: string;\n}\n\nexport interface ObjectReadResult {\n sizeBytes: number;\n contentType: string;\n}\n\nexport interface ObjectCopyResult {\n key: string;\n /**\n * `true` when the destination already held this copy's result — the same source version,\n * committed by an earlier attempt whose answer was lost or by a concurrent identical\n * copy; `false` when this call committed it.\n */\n alreadyPresent: boolean;\n}\n\n/**\n * What `deleteCopy` found at the destination: `deleted` — a copy of the source, now removed;\n * `absent` — no object; `notThisCopy` — an object this package did not copy from that\n * source, left untouched.\n */\nexport type DeleteCopyResult = 'deleted' | 'absent' | 'notThisCopy';\n\n// Every copy records on its destination which source version it holds. A copy whose\n// destination already exists compares this record with its own, so it recognizes its\n// own earlier result and never mistakes another writer's object for it. The values are\n// URI-encoded because R2 carries custom metadata in HTTP headers, which must be ASCII.\nconst COPY_SOURCE_METADATA_KEY = 'copy-source';\nconst COPY_SOURCE_VERSION_METADATA_KEY = 'copy-source-version';\n\ntype CopyProvenance = Record<\n typeof COPY_SOURCE_METADATA_KEY | typeof COPY_SOURCE_VERSION_METADATA_KEY,\n string\n>;\n\nfunction copyProvenance(fromKey: string, sourceVersion: string): CopyProvenance {\n return {\n [COPY_SOURCE_METADATA_KEY]: encodeURIComponent(fromKey),\n [COPY_SOURCE_VERSION_METADATA_KEY]: encodeURIComponent(sourceVersion),\n };\n}\n\n/** Whether an object's custom metadata, read through `readMetadata`, names exactly this provenance. */\nfunction holdsProvenance(\n readMetadata: (name: string) => unknown,\n provenance: CopyProvenance,\n): boolean {\n return Object.entries(provenance).every(([name, value]) => readMetadata(name) === value);\n}\n\n/**\n * Whether an object's custom metadata records a copy of `fromKey`, of whichever source\n * version: `deleteCopy` reads only the destination, so it cannot know the source's version.\n * A record naming the source without a version is not provenance this package wrote.\n */\nfunction recordsCopyOf(readMetadata: (name: string) => unknown, fromKey: string): boolean {\n const version = readMetadata(COPY_SOURCE_VERSION_METADATA_KEY);\n return (\n readMetadata(COPY_SOURCE_METADATA_KEY) === encodeURIComponent(fromKey) &&\n typeof version === 'string' &&\n version !== ''\n );\n}\n\n/** The content headers a copy carries from its source: HTTP header name → Cloud Storage field. */\nconst CARRIED_CONTENT_HEADERS = {\n 'content-type': 'contentType',\n 'cache-control': 'cacheControl',\n 'content-disposition': 'contentDisposition',\n 'content-encoding': 'contentEncoding',\n} as const;\n\ntype CarriedContentField = (typeof CARRIED_CONTENT_HEADERS)[keyof typeof CARRIED_CONTENT_HEADERS];\n\nexport interface MediaObjectStore {\n /**\n * Upload a local file to the store at `key`. Objects are written with a\n * one-year `immutable` cache-control, so keys are write-once: a replacement is\n * written under a NEW key and the app repoints its reference to it. Overwriting\n * an existing key would keep serving the old bytes from caches for up to a year.\n */\n putFile(args: {\n localPath: string;\n key: string;\n contentType?: string;\n }): Promise<ObjectWriteResult>;\n /**\n * Server-side copy within the store. It never overwrites and is idempotent: it commits\n * only when `toKey` holds no object, in one call that writes the destination fully\n * formed (the source version's bytes and content headers, its provenance, and this\n * store's token policy). When `toKey` already holds this copy's result — a retry of\n * the same copy of the same source version — it resolves with `alreadyPresent: true`;\n * when it holds anything else, it throws `ObjectAlreadyExistsError` and leaves that\n * object untouched. It never deletes anything.\n */\n copy(args: { fromKey: string; toKey: string }): Promise<ObjectCopyResult>;\n /**\n * Download an object's bytes from the store to a local file (streamed, so a\n * large video never sits fully in memory). Used by safety evidence capture to\n * pull final media — which may live in a different backend (R2) — before\n * writing it into the separate (GCS) evidence vault: capture is inherently\n * read-here-then-write, NOT a server-side copy, because source and vault can\n * be different backends.\n */\n readToFile(args: { key: string; localPath: string }): Promise<ObjectReadResult>;\n /** Delete an object. Missing objects are a no-op, not an error. */\n delete(key: string): Promise<void>;\n /**\n * Delete `toKey` only when the object there is a copy of `fromKey` this package made: its\n * own recorded provenance must name `fromKey` as its source. Only the destination is read,\n * so this works after the source is gone. Any other object — a copy of another source, or\n * one with no provenance — is left untouched and answers `notThisCopy`.\n *\n * Firebase Storage deletes only the generation it checked; an object replaced after the\n * check is checked again, never deleted blind. R2's DeleteObject takes no precondition, so\n * there the delete follows the check unconditioned. That gap is harmless only under the key\n * contract: keys are write-once and a copy never replaces an object, so in the gap the\n * checked object can only be removed; and a copy's key is derived from its source, so the\n * only object that can land there afterwards is another copy of the same source.\n */\n deleteCopy(args: { fromKey: string; toKey: string }): Promise<DeleteCopyResult>;\n}\n\nconst DEFAULT_CACHE_CONTROL = 'public, max-age=31536000, immutable';\n\n// ---------------------------------------------------------------------------\n// R2 (S3-compatible) store — deployed environments\n// ---------------------------------------------------------------------------\n\nexport interface CreateR2ObjectStoreArgs {\n accountId: string;\n accessKeyId: string;\n secretAccessKey: string;\n bucket: string;\n /** Override for tests; defaults to the real R2 S3 endpoint. */\n endpoint?: string;\n /** Override fetch for tests. */\n fetchImpl?: typeof fetch;\n /** Test seam: override the retry sleep (defaults to real `setTimeout`). */\n sleepImpl?: (ms: number) => Promise<void>;\n /** Test seam: override the backoff jitter source (defaults to `Math.random`). */\n randomImpl?: () => number;\n /** Test seam: override the clock used for HTTP-date `Retry-After` (defaults to `Date.now`). */\n nowImpl?: () => number;\n}\n\nexport function createR2ObjectStore(args: CreateR2ObjectStoreArgs): MediaObjectStore {\n const endpoint = (args.endpoint ?? `https://${args.accountId}.r2.cloudflarestorage.com`).replace(\n /\\/$/,\n '',\n );\n const client = new AwsClient({\n accessKeyId: args.accessKeyId,\n secretAccessKey: args.secretAccessKey,\n service: 's3',\n region: 'auto',\n });\n const doFetch = args.fetchImpl ?? fetch;\n\n // Retry seams: production defaults are internal; tests inject deterministic\n // sleep/random/now. putFile/copy/readToFile ride the bounded retry helper;\n // delete stays one-shot (never routes through it — see below).\n const defaults = defaultR2RetrySeams();\n const seams: R2RetrySeams = {\n sleep: args.sleepImpl ?? defaults.sleep,\n random: args.randomImpl ?? defaults.random,\n now: args.nowImpl ?? defaults.now,\n };\n\n const objectUrl = (key: string) =>\n `${endpoint}/${args.bucket}/${key.split('/').map(encodeURIComponent).join('/')}`;\n\n async function signedFetch(\n url: string,\n init: RequestInit & { headers?: Record<string, string> },\n ) {\n const signed = await client.sign(url, { ...init, aws: { allHeaders: true } } as any);\n return doFetch(signed, { ...(init.body ? { duplex: 'half' } : {}) } as any);\n }\n\n // A GET whose body is discarded unread, not a HEAD: a HEAD's error response carries no\n // body, so the S3 error code that tells a missing key from a missing bucket would be lost.\n async function readObjectHeaders(\n key: string,\n operation: 'copy' | 'deleteCopy',\n ): Promise<Headers> {\n return fetchWithR2Retry({\n operation,\n bucket: args.bucket,\n key,\n seams,\n runAttempt: async () => {\n const res = await signedFetch(objectUrl(key), { method: 'GET' });\n if (!res.ok) return { kind: 'response' as const, response: res };\n await res.body?.cancel();\n return { kind: 'success' as const, value: res.headers };\n },\n });\n }\n\n // R2 exposes no object version id, so a source version is its ETag, last-modified time,\n // and size together: replacing the source changes at least one of them unless the new\n // content has the same MD5-derived ETag and lands within the same second. A multipart\n // upload's ETag is not the content's MD5, but it is the same kind of version token.\n function readSourceVersion(headers: Headers, key: string): { etag: string; version: string } {\n const etag = headers.get('etag');\n if (!etag) throw new UnreadableStorageObjectMetadataError(args.bucket, key, 'etag');\n const lastModified = headers.get('last-modified');\n if (!lastModified)\n throw new UnreadableStorageObjectMetadataError(args.bucket, key, 'lastModified');\n const size = headers.get('content-length');\n if (!size) throw new UnreadableStorageObjectMetadataError(args.bucket, key, 'size');\n return { etag, version: `${etag} ${lastModified} ${size}` };\n }\n\n return {\n async putFile({ localPath, key, contentType }) {\n // stat() once, outside the retry loop: a missing input file is a\n // deterministic local error and must fail immediately, never retry.\n const { size } = await stat(localPath);\n const ct = contentType ?? getMimeFromExt(extractExt(localPath));\n return fetchWithR2Retry({\n operation: 'putFile',\n bucket: args.bucket,\n key,\n seams,\n // Fresh signed request AND a fresh read stream on every attempt — a\n // consumed stream is never reused.\n runAttempt: async () => {\n const res = await signedFetch(objectUrl(key), {\n method: 'PUT',\n headers: {\n 'content-type': ct,\n 'content-length': String(size),\n 'cache-control': DEFAULT_CACHE_CONTROL,\n },\n body: createReadStream(localPath) as unknown as BodyInit,\n });\n if (res.ok) {\n return { kind: 'success' as const, value: { key, sizeBytes: size, contentType: ct } };\n }\n return { kind: 'response' as const, response: res };\n },\n });\n },\n\n async copy({ fromKey, toKey }) {\n const source = await readObjectHeaders(fromKey, 'copy');\n const { etag, version } = readSourceVersion(source, fromKey);\n const provenance = copyProvenance(fromKey, version);\n const headers: Record<string, string> = {\n 'x-amz-copy-source': `/${args.bucket}/${fromKey.split('/').map(encodeURIComponent).join('/')}`,\n // Pins the copy to the source version the provenance names.\n 'x-amz-copy-source-if-match': etag,\n // R2's destination-conditional CopyObject extension, checked when the copy commits;\n // S3's x-amz-copy-source-if-* headers condition only the source.\n 'cf-copy-destination-if-none-match': '*',\n // The destination takes exactly the headers below rather than the source's, so it\n // commits fully formed in this one call.\n 'x-amz-metadata-directive': 'REPLACE',\n };\n for (const name of Object.keys(CARRIED_CONTENT_HEADERS)) {\n const value = source.get(name);\n if (value !== null) headers[name] = value;\n }\n for (const [name, value] of Object.entries(provenance)) headers[`x-amz-meta-${name}`] = value;\n\n try {\n await fetchWithR2Retry({\n operation: 'copy',\n bucket: args.bucket,\n key: toKey,\n seams,\n runAttempt: async () => {\n const res = await signedFetch(objectUrl(toKey), { method: 'PUT', headers });\n if (res.ok) return { kind: 'success' as const, value: undefined };\n return { kind: 'response' as const, response: res };\n },\n });\n return { key: toKey, alreadyPresent: false };\n } catch (e) {\n if (!(e instanceof R2StorageError && e.status === 412)) throw e;\n // R2 answers 412 both for an existing destination and for a source that changed\n // after it was read; only an existing destination can be this copy's result.\n let destination: Headers;\n try {\n destination = await readObjectHeaders(toKey, 'copy');\n } catch (readError) {\n if (isObjectNotFoundError(readError)) throw e;\n throw readError;\n }\n if (holdsProvenance((name) => destination.get(`x-amz-meta-${name}`), provenance)) {\n return { key: toKey, alreadyPresent: true };\n }\n throw new ObjectAlreadyExistsError(toKey);\n }\n },\n\n async readToFile({ key, localPath }) {\n return fetchWithR2Retry({\n operation: 'readToFile',\n bucket: args.bucket,\n key,\n seams,\n runAttempt: async () => {\n const res = await signedFetch(objectUrl(key), { method: 'GET' });\n if (!res.ok || !res.body) {\n return { kind: 'response' as const, response: res };\n }\n const contentType = res.headers.get('content-type') ?? NEUTRAL_CONTENT_TYPE;\n // Pipe ONLY after a successful response, and open the destination in\n // truncating mode ('w') on every attempt so a partial download from a\n // prior attempt can never prefix/corrupt this one. res.body is a web\n // ReadableStream; stream it to disk so large media never sits fully in\n // memory. A transient body/network stream failure here throws and is\n // retried by the helper; a deterministic local fs error is not.\n await pipeline(\n Readable.fromWeb(res.body as Parameters<typeof Readable.fromWeb>[0]),\n createWriteStream(localPath, { flags: 'w' }),\n );\n const { size } = await stat(localPath);\n return { kind: 'success' as const, value: { sizeBytes: size, contentType } };\n },\n });\n },\n\n async delete(key) {\n // Intentionally one-shot: delete is never retried (see r2-retry.ts header).\n const res = await signedFetch(objectUrl(key), { method: 'DELETE' });\n // S3 DELETE returns 204 even for missing keys; anything else non-ok is real.\n if (!res.ok && res.status !== 404) {\n throw new R2StorageError({\n operation: 'delete',\n bucket: args.bucket,\n key,\n attempts: 1,\n status: res.status,\n responseText: await safeText(res),\n });\n }\n },\n\n async deleteCopy({ fromKey, toKey }) {\n let destination: Headers;\n try {\n destination = await readObjectHeaders(toKey, 'deleteCopy');\n } catch (e) {\n if (isObjectNotFoundError(e)) return 'absent';\n throw e;\n }\n if (!recordsCopyOf((name) => destination.get(`x-amz-meta-${name}`), fromKey))\n return 'notThisCopy';\n // Unconditioned: R2's DeleteObject honors no precondition header, so none is sent — one\n // it silently ignored would only look like protection. `MediaObjectStore.deleteCopy`\n // states why the gap after the check is harmless. One-shot, like `delete`.\n const res = await signedFetch(objectUrl(toKey), { method: 'DELETE' });\n if (res.ok) return 'deleted';\n const error = new R2StorageError({\n operation: 'deleteCopy',\n bucket: args.bucket,\n key: toKey,\n attempts: 1,\n status: res.status,\n responseText: await safeText(res),\n });\n if (isObjectNotFoundError(error)) return 'absent';\n throw error;\n },\n };\n}\n\nasync function safeText(res: Response): Promise<string> {\n try {\n return (await res.text()).slice(0, 500);\n } catch {\n return '<no body>';\n }\n}\n\n// ---------------------------------------------------------------------------\n// Firebase Storage stores — deployed (token-free) and emulator (fixed token)\n// ---------------------------------------------------------------------------\n\n/** Custom-metadata key Firebase Storage reads download tokens from. */\nconst DOWNLOAD_TOKENS_METADATA_KEY = 'firebaseStorageDownloadTokens';\n\n/**\n * How many times `deleteCopy` checks a Firebase Storage destination before giving up. Keys\n * are write-once, so one change between the check and the delete is already rare; an object\n * still changing after that is left alone and the precondition failure is rethrown.\n */\nconst DELETE_COPY_MAX_READS = 2;\n\nexport interface CreateFirebaseStorageObjectStoreArgs {\n bucket: Bucket;\n}\n\n/** Firebase Storage store for deployed environments. Token-free (see header). */\nexport function createFirebaseStorageObjectStore(\n args: CreateFirebaseStorageObjectStoreArgs,\n): MediaObjectStore {\n return createFirebaseStore(args.bucket, null);\n}\n\nexport interface CreateEmulatorObjectStoreArgs extends CreateFirebaseStorageObjectStoreArgs {\n /** Fixed token attached to every object so deterministic emulator URLs work\n * (token URLs bypass rules in the emulator). Pass the app's constant. */\n downloadToken: string;\n}\n\n/** Local dev / tests only: the Firebase Storage store plus a fixed download token. */\nexport function createFirebaseEmulatorObjectStore(\n args: CreateEmulatorObjectStoreArgs,\n): MediaObjectStore {\n return createFirebaseStore(args.bucket, args.downloadToken);\n}\n\n/** `downloadToken` is the store's token policy: `null` = token-free, a string = that token on every object. */\nfunction createFirebaseStore(bucket: Bucket, downloadToken: string | null): MediaObjectStore {\n async function deleteObject(key: string): Promise<void> {\n try {\n await bucket.file(key).delete();\n } catch (e) {\n if (isObjectNotFoundError(e)) return;\n throw e;\n }\n }\n\n return {\n async putFile({ localPath, key, contentType }) {\n const { size } = await stat(localPath);\n const ct = contentType ?? getMimeFromExt(extractExt(localPath));\n // An upload replaces the object and all of its metadata, so a token-free\n // put carries no custom metadata at all.\n await bucket.upload(localPath, {\n destination: key,\n metadata: {\n contentType: ct,\n cacheControl: DEFAULT_CACHE_CONTROL,\n ...(downloadToken === null\n ? {}\n : { metadata: { [DOWNLOAD_TOKENS_METADATA_KEY]: downloadToken } }),\n },\n });\n return { key, sizeBytes: size, contentType: ct };\n },\n\n async copy({ fromKey, toKey }) {\n const [source] = await bucket.file(fromKey).getMetadata();\n const generation = requireStorageObjectGeneration(source, {\n bucket: bucket.name,\n key: fromKey,\n });\n const provenance = copyProvenance(fromKey, generation);\n const contentHeaders: Partial<Record<CarriedContentField, string>> = {};\n for (const field of Object.values(CARRIED_CONTENT_HEADERS)) {\n const value = source[field];\n if (typeof value === 'string' && value !== '') contentHeaders[field] = value;\n }\n try {\n // A rewrite's resource REPLACES the destination's metadata rather than merging with\n // the source's, so this one call commits the destination fully formed: the source's\n // content headers, the provenance, and this store's token policy — never a token the\n // source carried. The source is pinned to the generation the provenance names, so a\n // 412 can only mean the destination exists; a replaced source fails as not-found.\n await bucket.file(fromKey, { generation }).copy(bucket.file(toKey), {\n ...contentHeaders,\n metadata:\n downloadToken === null\n ? provenance\n : { ...provenance, [DOWNLOAD_TOKENS_METADATA_KEY]: downloadToken },\n preconditionOpts: { ifGenerationMatch: 0 },\n });\n return { key: toKey, alreadyPresent: false };\n } catch (e) {\n if (!isPreconditionFailedError(e)) throw e;\n let destinationMetadata: Record<string, unknown> | undefined;\n try {\n const [destination] = await bucket.file(toKey).getMetadata();\n destinationMetadata = destination.metadata ?? undefined;\n } catch (readError) {\n if (isObjectNotFoundError(readError)) throw e;\n throw readError;\n }\n if (holdsProvenance((name) => destinationMetadata?.[name], provenance)) {\n return { key: toKey, alreadyPresent: true };\n }\n throw new ObjectAlreadyExistsError(toKey);\n }\n },\n\n async readToFile({ key, localPath }) {\n const file = bucket.file(key);\n await file.download({ destination: localPath });\n const [meta] = await file.getMetadata();\n const { size } = await stat(localPath);\n const contentType = (meta.contentType as string | undefined) ?? NEUTRAL_CONTENT_TYPE;\n return { sizeBytes: size, contentType };\n },\n\n delete: deleteObject,\n\n async deleteCopy({ fromKey, toKey }) {\n const file = bucket.file(toKey);\n for (let read = 1; ; read++) {\n let custom: Record<string, unknown> | undefined;\n let generation: string | number | null | undefined;\n try {\n const [destination] = await file.getMetadata();\n custom = destination.metadata ?? undefined;\n generation = destination.generation;\n } catch (e) {\n if (isObjectNotFoundError(e)) return 'absent';\n throw e;\n }\n if (!recordsCopyOf((name) => custom?.[name], fromKey)) return 'notThisCopy';\n // The delete names the generation just checked, so it can only remove that object.\n const checked = requireStorageObjectGeneration(\n { generation },\n { bucket: bucket.name, key: toKey },\n );\n try {\n await file.delete({ ifGenerationMatch: checked });\n return 'deleted';\n } catch (e) {\n if (isObjectNotFoundError(e)) return 'absent';\n // A 412 means the object changed after it was checked: check what is there now.\n if (!isPreconditionFailedError(e) || read >= DELETE_COPY_MAX_READS) throw e;\n }\n }\n },\n };\n}\n\n/** The Cloud Storage Admin SDK's numeric `412`: a generation precondition did not hold. */\nfunction isPreconditionFailedError(e: unknown): boolean {\n return (e as { code?: unknown } | null | undefined)?.code === 412;\n}\n\n// ---------------------------------------------------------------------------\n\nfunction extractExt(localPath: string): string {\n return localPath.split('.').pop() || '';\n}\n\nexport function getMimeFromExt(ext: string): string {\n switch (ext.toLowerCase()) {\n case 'jpg':\n case 'jpeg':\n return 'image/jpeg';\n case 'png':\n return 'image/png';\n case 'webp':\n return 'image/webp';\n case 'mp4':\n return 'video/mp4';\n case 'webm':\n return 'video/webm';\n case 'm4a':\n return 'audio/mp4';\n case 'aac':\n return 'audio/aac';\n case 'mp3':\n return 'audio/mpeg';\n default:\n return NEUTRAL_CONTENT_TYPE;\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"storage-ops.js","sourceRoot":"","sources":["../../src/server/storage-ops.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,iBAAiB,EAAE,MAAM,SAAS,CAAC;AAC9D,OAAO,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAC;AACxC,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AACvC,OAAO,EAAE,QAAQ,EAAE,MAAM,sBAAsB,CAAC;AAChD,OAAO,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AAEtC,OAAO,EAAE,oBAAoB,EAAE,MAAM,gCAAgC,CAAC;AACtE,OAAO,EAAE,qBAAqB,EAAE,MAAM,iBAAiB,CAAC;AACxD,OAAO,EACL,mBAAmB,EACnB,gBAAgB,EAChB,cAAc,GAEf,MAAM,eAAe,CAAC;AACvB,OAAO,EACL,8BAA8B,EAC9B,oCAAoC,GACrC,MAAM,oBAAoB,CAAC;AAE5B,MAAM,OAAO,wBAAyB,SAAQ,KAAK;IACrB;IAA5B,YAA4B,GAAW;QACrC,KAAK,CAAC,+CAA+C,CAAC,CAAC;QAD7B,QAAG,GAAH,GAAG,CAAQ;QAErC,IAAI,CAAC,IAAI,GAAG,0BAA0B,CAAC;IACzC,CAAC;CACF;AAuBD,gFAAgF;AAChF,MAAM,wBAAwB,GAAG,aAAa,CAAC;AAC/C,MAAM,gCAAgC,GAAG,qBAAqB,CAAC;AAO/D,SAAS,cAAc,CAAC,OAAe,EAAE,aAAqB;IAC5D,OAAO;QACL,CAAC,wBAAwB,CAAC,EAAE,kBAAkB,CAAC,OAAO,CAAC;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AAW,CAAC;QACrB,KAAK,MAAM;YACT,OAAO,YAAY,CAAC;QACtB,KAAK,KAAK;YACR,OAAO,WAAW,CAAC;QACrB,KAAK,MAAM;YACT,OAAO,YAAY,CAAC;QACtB,KAAK,KAAK;YACR,OAAO,WAAW,CAAC;QACrB,KAAK,KAAK;YACR,OAAO,WAAW,CAAC;QACrB,KAAK,KAAK;YACR,OAAO,YAAY,CAAC;QACtB;YACE,OAAO,oBAAoB,CAAC;IAChC,CAAC;AACH,CAAC","sourcesContent":["import { createReadStream, createWriteStream } from 'node:fs';\nimport { stat } from 'node:fs/promises';\nimport { Readable } from 'node:stream';\nimport { pipeline } from 'node:stream/promises';\nimport { AwsClient } from 'aws4fetch';\nimport type { getStorage } from 'firebase-admin/storage';\nimport { NEUTRAL_CONTENT_TYPE } from '@ttt-productions/media-schemas';\nimport { isObjectNotFoundError } from './gcs-errors.js';\nimport {\n defaultR2RetrySeams,\n fetchWithR2Retry,\n R2StorageError,\n type R2RetrySeams,\n} from './r2-retry.js';\nimport {\n requireStorageObjectGeneration,\n UnreadableStorageObjectMetadataError,\n} from './staged-object.js';\n\nexport class ObjectAlreadyExistsError extends Error {\n constructor(public readonly key: string) {\n super('copy destination already holds another object');\n this.name = 'ObjectAlreadyExistsError';\n }\n}\n\n/** Inferred from usage so the package takes no @google-cloud/storage dependency. */\nexport type Bucket = ReturnType<ReturnType<typeof getStorage>['bucket']>;\n\nexport interface ObjectWriteResult {\n key: string;\n sizeBytes: number;\n contentType: string;\n}\n\nexport interface ObjectReadResult {\n sizeBytes: number;\n contentType: string;\n}\n\nexport interface ObjectCopyResult {\n key: string;\n alreadyPresent: boolean;\n}\n\nexport type DeleteCopyResult = 'deleted' | 'absent' | 'notThisCopy';\n\n// URI-encoded: R2 carries custom metadata in HTTP headers, which must be ASCII.\nconst COPY_SOURCE_METADATA_KEY = 'copy-source';\nconst COPY_SOURCE_VERSION_METADATA_KEY = 'copy-source-version';\n\ntype CopyProvenance = Record<\n typeof COPY_SOURCE_METADATA_KEY | typeof COPY_SOURCE_VERSION_METADATA_KEY,\n string\n>;\n\nfunction copyProvenance(fromKey: string, sourceVersion: string): CopyProvenance {\n return {\n [COPY_SOURCE_METADATA_KEY]: encodeURIComponent(fromKey),\n [COPY_SOURCE_VERSION_METADATA_KEY]: encodeURIComponent(sourceVersion),\n };\n}\n\nfunction holdsProvenance(\n readMetadata: (name: string) => unknown,\n provenance: CopyProvenance,\n): boolean {\n return Object.entries(provenance).every(([name, value]) => readMetadata(name) === value);\n}\n\nfunction recordsCopyOf(readMetadata: (name: string) => unknown, fromKey: string): boolean {\n const version = readMetadata(COPY_SOURCE_VERSION_METADATA_KEY);\n return (\n readMetadata(COPY_SOURCE_METADATA_KEY) === encodeURIComponent(fromKey) &&\n typeof version === 'string' &&\n version !== ''\n );\n}\n\nconst CARRIED_CONTENT_HEADERS = {\n 'content-type': 'contentType',\n 'cache-control': 'cacheControl',\n 'content-disposition': 'contentDisposition',\n 'content-encoding': 'contentEncoding',\n} as const;\n\ntype CarriedContentField = (typeof CARRIED_CONTENT_HEADERS)[keyof typeof CARRIED_CONTENT_HEADERS];\n\nexport interface MediaObjectStore {\n /** Objects carry a one-year `immutable` cache-control, so a key is write-once: an overwrite keeps serving the old bytes from caches. */\n putFile(args: {\n localPath: string;\n key: string;\n contentType?: string;\n }): Promise<ObjectWriteResult>;\n copy(args: { fromKey: string; toKey: string }): Promise<ObjectCopyResult>;\n readToFile(args: { key: string; localPath: string }): Promise<ObjectReadResult>;\n delete(key: string): Promise<void>;\n deleteCopy(args: { fromKey: string; toKey: string }): Promise<DeleteCopyResult>;\n}\n\nconst DEFAULT_CACHE_CONTROL = 'public, max-age=31536000, immutable';\n\nexport interface CreateR2ObjectStoreArgs {\n accountId: string;\n accessKeyId: string;\n secretAccessKey: string;\n bucket: string;\n endpoint?: string;\n fetchImpl?: typeof fetch;\n sleepImpl?: (ms: number) => Promise<void>;\n randomImpl?: () => number;\n nowImpl?: () => number;\n}\n\nexport function createR2ObjectStore(args: CreateR2ObjectStoreArgs): MediaObjectStore {\n const endpoint = (args.endpoint ?? `https://${args.accountId}.r2.cloudflarestorage.com`).replace(\n /\\/$/,\n '',\n );\n const client = new AwsClient({\n accessKeyId: args.accessKeyId,\n secretAccessKey: args.secretAccessKey,\n service: 's3',\n region: 'auto',\n });\n const doFetch = args.fetchImpl ?? fetch;\n\n const defaults = defaultR2RetrySeams();\n const seams: R2RetrySeams = {\n sleep: args.sleepImpl ?? defaults.sleep,\n random: args.randomImpl ?? defaults.random,\n now: args.nowImpl ?? defaults.now,\n };\n\n const objectUrl = (key: string) =>\n `${endpoint}/${args.bucket}/${key.split('/').map(encodeURIComponent).join('/')}`;\n\n async function signedFetch(\n url: string,\n init: RequestInit & { headers?: Record<string, string> },\n ) {\n const signed = await client.sign(url, { ...init, aws: { allHeaders: true } } as any);\n return doFetch(signed, { ...(init.body ? { duplex: 'half' } : {}) } as any);\n }\n\n // A GET, not a HEAD: a HEAD's error response has no body, so the S3 error code would be lost.\n async function readObjectHeaders(\n key: string,\n operation: 'copy' | 'deleteCopy',\n ): Promise<Headers> {\n return fetchWithR2Retry({\n operation,\n bucket: args.bucket,\n key,\n seams,\n runAttempt: async () => {\n const res = await signedFetch(objectUrl(key), { method: 'GET' });\n if (!res.ok) return { kind: 'response' as const, response: res };\n await res.body?.cancel();\n return { kind: 'success' as const, value: res.headers };\n },\n });\n }\n\n // R2 exposes no object version id, so a source version is its ETag, last-modified time, and size together.\n function readSourceVersion(headers: Headers, key: string): { etag: string; version: string } {\n const etag = headers.get('etag');\n if (!etag) throw new UnreadableStorageObjectMetadataError(args.bucket, key, 'etag');\n const lastModified = headers.get('last-modified');\n if (!lastModified)\n throw new UnreadableStorageObjectMetadataError(args.bucket, key, 'lastModified');\n const size = headers.get('content-length');\n if (!size) throw new UnreadableStorageObjectMetadataError(args.bucket, key, 'size');\n return { etag, version: `${etag} ${lastModified} ${size}` };\n }\n\n return {\n async putFile({ localPath, key, contentType }) {\n const { size } = await stat(localPath);\n const ct = contentType ?? getMimeFromExt(extractExt(localPath));\n return fetchWithR2Retry({\n operation: 'putFile',\n bucket: args.bucket,\n key,\n seams,\n // A fresh signed request and read stream on every attempt: a consumed stream cannot be re-read.\n runAttempt: async () => {\n const res = await signedFetch(objectUrl(key), {\n method: 'PUT',\n headers: {\n 'content-type': ct,\n 'content-length': String(size),\n 'cache-control': DEFAULT_CACHE_CONTROL,\n },\n body: createReadStream(localPath) as unknown as BodyInit,\n });\n if (res.ok) {\n return { kind: 'success' as const, value: { key, sizeBytes: size, contentType: ct } };\n }\n return { kind: 'response' as const, response: res };\n },\n });\n },\n\n async copy({ fromKey, toKey }) {\n const source = await readObjectHeaders(fromKey, 'copy');\n const { etag, version } = readSourceVersion(source, fromKey);\n const provenance = copyProvenance(fromKey, version);\n const headers: Record<string, string> = {\n 'x-amz-copy-source': `/${args.bucket}/${fromKey.split('/').map(encodeURIComponent).join('/')}`,\n 'x-amz-copy-source-if-match': etag,\n // R2's destination-conditional CopyObject extension; S3's x-amz-copy-source-if-* headers condition only the source.\n 'cf-copy-destination-if-none-match': '*',\n 'x-amz-metadata-directive': 'REPLACE',\n };\n for (const name of Object.keys(CARRIED_CONTENT_HEADERS)) {\n const value = source.get(name);\n if (value !== null) headers[name] = value;\n }\n for (const [name, value] of Object.entries(provenance)) headers[`x-amz-meta-${name}`] = value;\n\n try {\n await fetchWithR2Retry({\n operation: 'copy',\n bucket: args.bucket,\n key: toKey,\n seams,\n runAttempt: async () => {\n const res = await signedFetch(objectUrl(toKey), { method: 'PUT', headers });\n if (res.ok) return { kind: 'success' as const, value: undefined };\n return { kind: 'response' as const, response: res };\n },\n });\n return { key: toKey, alreadyPresent: false };\n } catch (e) {\n if (!(e instanceof R2StorageError && e.status === 412)) throw e;\n // R2 answers 412 both for an existing destination and for a source that changed after it was read.\n let destination: Headers;\n try {\n destination = await readObjectHeaders(toKey, 'copy');\n } catch (readError) {\n if (isObjectNotFoundError(readError)) throw e;\n throw readError;\n }\n if (holdsProvenance((name) => destination.get(`x-amz-meta-${name}`), provenance)) {\n return { key: toKey, alreadyPresent: true };\n }\n throw new ObjectAlreadyExistsError(toKey);\n }\n },\n\n async readToFile({ key, localPath }) {\n return fetchWithR2Retry({\n operation: 'readToFile',\n bucket: args.bucket,\n key,\n seams,\n runAttempt: async () => {\n const res = await signedFetch(objectUrl(key), { method: 'GET' });\n if (!res.ok || !res.body) {\n return { kind: 'response' as const, response: res };\n }\n const contentType = res.headers.get('content-type') ?? NEUTRAL_CONTENT_TYPE;\n // Truncating mode ('w') on every attempt, so a partial earlier download never prefixes this one.\n await pipeline(\n Readable.fromWeb(res.body as Parameters<typeof Readable.fromWeb>[0]),\n createWriteStream(localPath, { flags: 'w' }),\n );\n const { size } = await stat(localPath);\n return { kind: 'success' as const, value: { sizeBytes: size, contentType } };\n },\n });\n },\n\n async delete(key) {\n const res = await signedFetch(objectUrl(key), { method: 'DELETE' });\n // S3 DELETE returns 204 even for missing keys; anything else non-ok is real.\n if (!res.ok && res.status !== 404) {\n throw new R2StorageError({\n operation: 'delete',\n bucket: args.bucket,\n key,\n attempts: 1,\n status: res.status,\n responseText: await safeText(res),\n });\n }\n },\n\n async deleteCopy({ fromKey, toKey }) {\n let destination: Headers;\n try {\n destination = await readObjectHeaders(toKey, 'deleteCopy');\n } catch (e) {\n if (isObjectNotFoundError(e)) return 'absent';\n throw e;\n }\n if (!recordsCopyOf((name) => destination.get(`x-amz-meta-${name}`), fromKey))\n return 'notThisCopy';\n // R2's DeleteObject honors no precondition header, so none is sent.\n const res = await signedFetch(objectUrl(toKey), { method: 'DELETE' });\n if (res.ok) return 'deleted';\n const error = new R2StorageError({\n operation: 'deleteCopy',\n bucket: args.bucket,\n key: toKey,\n attempts: 1,\n status: res.status,\n responseText: await safeText(res),\n });\n if (isObjectNotFoundError(error)) return 'absent';\n throw error;\n },\n };\n}\n\nasync function safeText(res: Response): Promise<string> {\n try {\n return (await res.text()).slice(0, 500);\n } catch {\n return '<no body>';\n }\n}\n\nconst DOWNLOAD_TOKENS_METADATA_KEY = 'firebaseStorageDownloadTokens';\n\nconst DELETE_COPY_MAX_READS = 2;\n\nexport interface CreateFirebaseStorageObjectStoreArgs {\n bucket: Bucket;\n}\n\nexport function createFirebaseStorageObjectStore(\n args: CreateFirebaseStorageObjectStoreArgs,\n): MediaObjectStore {\n return createFirebaseStore(args.bucket, null);\n}\n\nexport interface CreateEmulatorObjectStoreArgs extends CreateFirebaseStorageObjectStoreArgs {\n downloadToken: string;\n}\n\nexport function createFirebaseEmulatorObjectStore(\n args: CreateEmulatorObjectStoreArgs,\n): MediaObjectStore {\n return createFirebaseStore(args.bucket, args.downloadToken);\n}\n\nfunction createFirebaseStore(bucket: Bucket, downloadToken: string | null): MediaObjectStore {\n async function deleteObject(key: string): Promise<void> {\n try {\n await bucket.file(key).delete();\n } catch (e) {\n if (isObjectNotFoundError(e)) return;\n throw e;\n }\n }\n\n return {\n async putFile({ localPath, key, contentType }) {\n const { size } = await stat(localPath);\n const ct = contentType ?? getMimeFromExt(extractExt(localPath));\n await bucket.upload(localPath, {\n destination: key,\n metadata: {\n contentType: ct,\n cacheControl: DEFAULT_CACHE_CONTROL,\n ...(downloadToken === null\n ? {}\n : { metadata: { [DOWNLOAD_TOKENS_METADATA_KEY]: downloadToken } }),\n },\n });\n return { key, sizeBytes: size, contentType: ct };\n },\n\n async copy({ fromKey, toKey }) {\n const [source] = await bucket.file(fromKey).getMetadata();\n const generation = requireStorageObjectGeneration(source, {\n bucket: bucket.name,\n key: fromKey,\n });\n const provenance = copyProvenance(fromKey, generation);\n const contentHeaders: Partial<Record<CarriedContentField, string>> = {};\n for (const field of Object.values(CARRIED_CONTENT_HEADERS)) {\n const value = source[field];\n if (typeof value === 'string' && value !== '') contentHeaders[field] = value;\n }\n try {\n // A rewrite's resource replaces the destination's metadata rather than merging with the source's.\n await bucket.file(fromKey, { generation }).copy(bucket.file(toKey), {\n ...contentHeaders,\n metadata:\n downloadToken === null\n ? provenance\n : { ...provenance, [DOWNLOAD_TOKENS_METADATA_KEY]: downloadToken },\n preconditionOpts: { ifGenerationMatch: 0 },\n });\n return { key: toKey, alreadyPresent: false };\n } catch (e) {\n if (!isPreconditionFailedError(e)) throw e;\n let destinationMetadata: Record<string, unknown> | undefined;\n try {\n const [destination] = await bucket.file(toKey).getMetadata();\n destinationMetadata = destination.metadata ?? undefined;\n } catch (readError) {\n if (isObjectNotFoundError(readError)) throw e;\n throw readError;\n }\n if (holdsProvenance((name) => destinationMetadata?.[name], provenance)) {\n return { key: toKey, alreadyPresent: true };\n }\n throw new ObjectAlreadyExistsError(toKey);\n }\n },\n\n async readToFile({ key, localPath }) {\n const file = bucket.file(key);\n await file.download({ destination: localPath });\n const [meta] = await file.getMetadata();\n const { size } = await stat(localPath);\n const contentType = (meta.contentType as string | undefined) ?? NEUTRAL_CONTENT_TYPE;\n return { sizeBytes: size, contentType };\n },\n\n delete: deleteObject,\n\n async deleteCopy({ fromKey, toKey }) {\n const file = bucket.file(toKey);\n for (let read = 1; ; read++) {\n let custom: Record<string, unknown> | undefined;\n let generation: string | number | null | undefined;\n try {\n const [destination] = await file.getMetadata();\n custom = destination.metadata ?? undefined;\n generation = destination.generation;\n } catch (e) {\n if (isObjectNotFoundError(e)) return 'absent';\n throw e;\n }\n if (!recordsCopyOf((name) => custom?.[name], fromKey)) return 'notThisCopy';\n const checked = requireStorageObjectGeneration(\n { generation },\n { bucket: bucket.name, key: toKey },\n );\n try {\n await file.delete({ ifGenerationMatch: checked });\n return 'deleted';\n } catch (e) {\n if (isObjectNotFoundError(e)) return 'absent';\n if (!isPreconditionFailedError(e) || read >= DELETE_COPY_MAX_READS) throw e;\n }\n }\n },\n };\n}\n\nfunction isPreconditionFailedError(e: unknown): boolean {\n return (e as { code?: unknown } | null | undefined)?.code === 412;\n}\n\nfunction extractExt(localPath: string): string {\n return localPath.split('.').pop() || '';\n}\n\nexport function getMimeFromExt(ext: string): string {\n switch (ext.toLowerCase()) {\n case 'jpg':\n case 'jpeg':\n return 'image/jpeg';\n case 'png':\n return 'image/png';\n case 'webp':\n return 'image/webp';\n case 'mp4':\n return 'video/mp4';\n case 'webm':\n return 'video/webm';\n case 'm4a':\n return 'audio/mp4';\n case 'aac':\n return 'audio/aac';\n case 'mp3':\n return 'audio/mpeg';\n default:\n return NEUTRAL_CONTENT_TYPE;\n }\n}\n"]}
|
package/dist/types.d.ts
CHANGED
|
@@ -1,14 +1,12 @@
|
|
|
1
1
|
import type { MediaProcessingResult, MediaProcessingSpec } from '@ttt-productions/media-schemas';
|
|
2
2
|
export interface MediaPipelineProgress {
|
|
3
3
|
phase: 'read_input' | 'moderation_input' | 'process' | 'persist_outputs' | 'moderation_output' | 'done';
|
|
4
|
-
/** 0..1 when known */
|
|
5
4
|
percent?: number;
|
|
6
5
|
detail?: Record<string, unknown>;
|
|
7
6
|
}
|
|
8
7
|
export interface ProcessMediaContext {
|
|
9
8
|
inputPath: string;
|
|
10
9
|
outputBasePath: string;
|
|
11
|
-
/** optional mime hint from caller */
|
|
12
10
|
inputMime?: string;
|
|
13
11
|
}
|
|
14
12
|
export interface ProcessMediaOptions {
|
package/dist/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,qBAAqB,EAAE,mBAAmB,EAAE,MAAM,gCAAgC,CAAC;AAEjG,MAAM,WAAW,qBAAqB;IACpC,KAAK,EACD,YAAY,GACZ,kBAAkB,GAClB,SAAS,GACT,iBAAiB,GACjB,mBAAmB,GACnB,MAAM,CAAC;IACX,
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,qBAAqB,EAAE,mBAAmB,EAAE,MAAM,gCAAgC,CAAC;AAEjG,MAAM,WAAW,qBAAqB;IACpC,KAAK,EACD,YAAY,GACZ,kBAAkB,GAClB,SAAS,GACT,iBAAiB,GACjB,mBAAmB,GACnB,MAAM,CAAC;IACX,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CAClC;AAED,MAAM,WAAW,mBAAmB;IAClC,SAAS,EAAE,MAAM,CAAC;IAClB,cAAc,EAAE,MAAM,CAAC;IACvB,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AAED,MAAM,WAAW,mBAAmB;IAClC,MAAM,CAAC,EAAE,WAAW,CAAC;IACrB,UAAU,CAAC,EAAE,CAAC,CAAC,EAAE,qBAAqB,KAAK,IAAI,CAAC;CACjD;AAED,MAAM,MAAM,cAAc,GAAG,CAC3B,IAAI,EAAE,mBAAmB,EACzB,GAAG,EAAE,mBAAmB,EACxB,IAAI,CAAC,EAAE,mBAAmB,KACvB,OAAO,CAAC,qBAAqB,CAAC,CAAC;AAEpC,MAAM,WAAW,eAAe;IAC9B,KAAK,EAAE,cAAc,CAAC;IACtB,KAAK,EAAE,cAAc,CAAC;IACtB,KAAK,EAAE,cAAc,CAAC;CACvB"}
|
package/dist/types.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"","sourcesContent":["import type { MediaProcessingResult, MediaProcessingSpec } from '@ttt-productions/media-schemas';\n\nexport interface MediaPipelineProgress {\n phase:\n | 'read_input'\n | 'moderation_input'\n | 'process'\n | 'persist_outputs'\n | 'moderation_output'\n | 'done';\n
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"","sourcesContent":["import type { MediaProcessingResult, MediaProcessingSpec } from '@ttt-productions/media-schemas';\n\nexport interface MediaPipelineProgress {\n phase:\n | 'read_input'\n | 'moderation_input'\n | 'process'\n | 'persist_outputs'\n | 'moderation_output'\n | 'done';\n percent?: number;\n detail?: Record<string, unknown>;\n}\n\nexport interface ProcessMediaContext {\n inputPath: string;\n outputBasePath: string;\n inputMime?: string;\n}\n\nexport interface ProcessMediaOptions {\n signal?: AbortSignal;\n onProgress?: (p: MediaPipelineProgress) => void;\n}\n\nexport type ProcessMediaFn = (\n spec: MediaProcessingSpec,\n ctx: ProcessMediaContext,\n opts?: ProcessMediaOptions,\n) => Promise<MediaProcessingResult>;\n\nexport interface MediaProcessors {\n image: ProcessMediaFn;\n video: ProcessMediaFn;\n audio: ProcessMediaFn;\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"safe-path.d.ts","sourceRoot":"","sources":["../../src/utils/safe-path.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"safe-path.d.ts","sourceRoot":"","sources":["../../src/utils/safe-path.ts"],"names":[],"mappings":"AAEA,wBAAgB,WAAW,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAM/C;AAED,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,MAAM,CAchF"}
|
package/dist/utils/safe-path.js
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
import path from 'node:path';
|
|
2
|
-
// Very conservative key sanitizer. Prevents path traversal / separators.
|
|
3
2
|
export function sanitizeKey(key) {
|
|
4
3
|
const k = String(key ?? '');
|
|
5
4
|
const stripped = k.replace(/[\\/]+/g, '_');
|
|
6
|
-
const cleaned = stripped.replace(/\.+/g, '.');
|
|
5
|
+
const cleaned = stripped.replace(/\.+/g, '.');
|
|
7
6
|
const safe = cleaned.replace(/[^a-zA-Z0-9._-]/g, '_');
|
|
8
7
|
return safe.slice(0, 80) || 'file';
|
|
9
8
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"safe-path.js","sourceRoot":"","sources":["../../src/utils/safe-path.ts"],"names":[],"mappings":"AAAA,OAAO,IAAI,MAAM,WAAW,CAAC;AAE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,IAAI,WAAW,KAAK,WAAW,EAAE,CAAC;QACnF,MAAM,IAAI,KAAK,CAAC,oBAAoB,CAAC,CAAC;IACxC,CAAC;IAED,OAAO,GAAG,CAAC;AACb,CAAC","sourcesContent":["import path from 'node:path';\n\nexport function sanitizeKey(key: string): string {\n const k = String(key ?? '');\n const stripped = k.replace(/[\\\\/]+/g, '_');\n const cleaned = stripped.replace(/\\.+/g, '.');\n const safe = cleaned.replace(/[^a-zA-Z0-9._-]/g, '_');\n return safe.slice(0, 80) || 'file';\n}\n\nexport function safeOutputPathFor(base: string, key: string, ext: string): string {\n const safeKey = sanitizeKey(key);\n const safeExt = sanitizeKey(ext).replace(/^\\./, '');\n const dir = path.dirname(base);\n const baseName = path.basename(base);\n const out = path.join(dir, `${baseName}_${safeKey}.${safeExt}`);\n\n const resolvedDir = path.resolve(dir);\n const resolvedOut = path.resolve(out);\n if (!resolvedOut.startsWith(resolvedDir + path.sep) && resolvedOut !== resolvedDir) {\n throw new Error('Unsafe output path');\n }\n\n return out;\n}\n"]}
|