@puzzle-ui/media-cloudflare 0.0.0-stage → 0.17.0
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/README.md +71 -2
- package/ai/instructions.md +20 -0
- package/ai/manifest.json +10 -0
- package/dist/LICENSE +21 -0
- package/dist/bucket-provisioner.cjs +37 -0
- package/dist/bucket-provisioner.cjs.map +1 -0
- package/dist/bucket-provisioner.d.cts +16 -0
- package/dist/bucket-provisioner.d.ts +16 -0
- package/dist/bucket-provisioner.js +37 -0
- package/dist/bucket-provisioner.js.map +1 -0
- package/dist/errors.cjs +35 -0
- package/dist/errors.cjs.map +1 -0
- package/dist/errors.d.cts +17 -0
- package/dist/errors.d.ts +17 -0
- package/dist/errors.js +32 -0
- package/dist/errors.js.map +1 -0
- package/dist/images-processor.cjs +100 -0
- package/dist/images-processor.cjs.map +1 -0
- package/dist/images-processor.d.cts +15 -0
- package/dist/images-processor.d.ts +15 -0
- package/dist/images-processor.js +100 -0
- package/dist/images-processor.js.map +1 -0
- package/dist/index.cjs +28 -0
- package/dist/index.d.cts +9 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +8 -0
- package/dist/metadata-store.cjs +69 -0
- package/dist/metadata-store.cjs.map +1 -0
- package/dist/metadata-store.d.cts +23 -0
- package/dist/metadata-store.d.ts +23 -0
- package/dist/metadata-store.js +69 -0
- package/dist/metadata-store.js.map +1 -0
- package/dist/r2-adapter.cjs +393 -0
- package/dist/r2-adapter.cjs.map +1 -0
- package/dist/r2-adapter.d.cts +40 -0
- package/dist/r2-adapter.d.ts +40 -0
- package/dist/r2-adapter.js +393 -0
- package/dist/r2-adapter.js.map +1 -0
- package/dist/s3-signer.cjs +106 -0
- package/dist/s3-signer.cjs.map +1 -0
- package/dist/s3-signer.d.cts +20 -0
- package/dist/s3-signer.d.ts +20 -0
- package/dist/s3-signer.js +105 -0
- package/dist/s3-signer.js.map +1 -0
- package/dist/types.d.cts +212 -0
- package/dist/types.d.ts +212 -0
- package/dist/utils.cjs +59 -0
- package/dist/utils.cjs.map +1 -0
- package/dist/utils.d.cts +10 -0
- package/dist/utils.d.ts +10 -0
- package/dist/utils.js +51 -0
- package/dist/utils.js.map +1 -0
- package/package.json +59 -4
package/README.md
CHANGED
|
@@ -1,3 +1,72 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @puzzle-ui/media-cloudflare
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Cloudflare integrations for the provider-neutral [`@puzzle-ui/media`](../media)
|
|
4
|
+
contracts.
|
|
5
|
+
|
|
6
|
+
## Installation
|
|
7
|
+
|
|
8
|
+
```sh
|
|
9
|
+
npm install @puzzle-ui/media @puzzle-ui/media-cloudflare
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
## R2 object storage
|
|
13
|
+
|
|
14
|
+
`CloudflareR2MediaObjectStorageAdapter` supports single and multipart upload
|
|
15
|
+
intents, actual size/SHA-256 verification, idempotent completion, abort and
|
|
16
|
+
delete cleanup, metadata, cursor listing, tenant-prefixed object keys, and
|
|
17
|
+
short-lived read/part targets. It requires a metadata store:
|
|
18
|
+
|
|
19
|
+
```ts
|
|
20
|
+
import {
|
|
21
|
+
CloudflareR2MediaObjectStorageAdapter,
|
|
22
|
+
InMemoryCloudflareMediaMetadataStore,
|
|
23
|
+
type CloudflareMediaUrlSigner,
|
|
24
|
+
type R2BucketLike,
|
|
25
|
+
} from "@puzzle-ui/media-cloudflare";
|
|
26
|
+
|
|
27
|
+
const adapter = new CloudflareR2MediaObjectStorageAdapter({
|
|
28
|
+
tenantId: "tenant-a",
|
|
29
|
+
bucket: {
|
|
30
|
+
bucket: env.MEDIA_BUCKET as R2BucketLike,
|
|
31
|
+
signer: env.MEDIA_SIGNER as CloudflareMediaUrlSigner,
|
|
32
|
+
},
|
|
33
|
+
metadataStore: new InMemoryCloudflareMediaMetadataStore(),
|
|
34
|
+
});
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
R2 Worker bindings expose object operations but do not generate presigned
|
|
38
|
+
URLs. `CloudflareR2S3UrlSigner` implements Worker-compatible AWS Signature
|
|
39
|
+
Version 4 presigning for an R2 S3-compatible endpoint. `CloudflareMediaUrlSigner`
|
|
40
|
+
is intentionally structural so applications can instead use an authenticated
|
|
41
|
+
upload gateway. `CloudflareMediaS3CompatibleResolver` and
|
|
42
|
+
`CloudflareMediaBucketResolver` can select a bucket and signer dynamically per
|
|
43
|
+
tenant; construct a signer per resolved bucket when using separate buckets.
|
|
44
|
+
|
|
45
|
+
Use a durable, tenant-scoped implementation of `CloudflareMediaMetadataStore`
|
|
46
|
+
in production. The in-memory store and `putForTesting` helpers are only for
|
|
47
|
+
tests/local development. The metadata store is required because R2 does not
|
|
48
|
+
persist the media upload-session and application-level object records required
|
|
49
|
+
by the media contract.
|
|
50
|
+
|
|
51
|
+
## Bucket provisioning
|
|
52
|
+
|
|
53
|
+
`CloudflareR2BucketProvisioner` accepts a narrow `getBucket`/`createBucket`
|
|
54
|
+
interface. The package does not assume a Cloudflare account API SDK, account
|
|
55
|
+
credentials, or a particular REST client:
|
|
56
|
+
|
|
57
|
+
```ts
|
|
58
|
+
const provisioner = new CloudflareR2BucketProvisioner({ api });
|
|
59
|
+
await provisioner.ensureBucket({ tenantId: "tenant-a" });
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
## Cloudflare Images
|
|
63
|
+
|
|
64
|
+
`CloudflareImagesProcessor` accepts the Images binding, an image source
|
|
65
|
+
resolver, and a variant writer. This keeps the binding version and object
|
|
66
|
+
storage policy in the host application while implementing `MediaProcessor`.
|
|
67
|
+
The binding and Admin API types are narrow structural interfaces because
|
|
68
|
+
Cloudflare's runtime APIs vary between Workers releases.
|
|
69
|
+
|
|
70
|
+
All provider keys, multipart IDs, signed headers, and signed URLs are
|
|
71
|
+
server-only. Host applications must authorize tenant requests before invoking
|
|
72
|
+
the adapter and must never log upload/read targets.
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# Cloudflare Media Guidance
|
|
2
|
+
|
|
3
|
+
Use `@puzzle-ui/media-cloudflare` only in server or Cloudflare Worker code.
|
|
4
|
+
Construct `CloudflareR2MediaObjectStorageAdapter` with a tenant-scoped
|
|
5
|
+
`CloudflareMediaMetadataStore`, an R2 bucket target, and a
|
|
6
|
+
`CloudflareMediaUrlSigner`. R2 Worker bindings do not issue presigned URLs;
|
|
7
|
+
use `CloudflareR2S3UrlSigner` for Worker-compatible AWS Signature Version 4
|
|
8
|
+
presigning or inject an authenticated application upload gateway.
|
|
9
|
+
|
|
10
|
+
The adapter verifies the actual object size and SHA-256 checksum during
|
|
11
|
+
completion, keeps provider upload IDs in the metadata store, supports
|
|
12
|
+
multipart abort/complete, and issues read targets only for recorded objects.
|
|
13
|
+
Use a durable metadata store in production. `InMemoryCloudflareMediaMetadataStore`
|
|
14
|
+
and the adapter's `putForTesting`/`putPartForTesting` helpers are for tests and
|
|
15
|
+
local development.
|
|
16
|
+
|
|
17
|
+
`CloudflareR2BucketProvisioner` accepts a narrow injected Admin API because
|
|
18
|
+
Cloudflare account API transports and credentials are application-specific.
|
|
19
|
+
`CloudflareImagesProcessor` similarly accepts the Images binding, source
|
|
20
|
+
resolver, and variant writer without assuming a framework or storage driver.
|
package/ai/manifest.json
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
{
|
|
2
|
+
"schemaVersion": 1,
|
|
3
|
+
"packageName": "@puzzle-ui/media-cloudflare",
|
|
4
|
+
"instructionVersion": 1,
|
|
5
|
+
"contentFile": "instructions.md",
|
|
6
|
+
"instructionDependencies": [
|
|
7
|
+
"@puzzle-ui/media"
|
|
8
|
+
],
|
|
9
|
+
"documentationUrl": "https://puzzle-ui.com/docs/media-cloudflare"
|
|
10
|
+
}
|
package/dist/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Sjoerd van Den Berg
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
const require_errors = require("./errors.cjs");
|
|
2
|
+
const require_utils = require("./utils.cjs");
|
|
3
|
+
// src/bucket-provisioner.ts
|
|
4
|
+
const bucketNamePattern = /^[a-z0-9][a-z0-9-]{1,61}[a-z0-9]$/;
|
|
5
|
+
/**
|
|
6
|
+
* Provisions R2 buckets through an injected Cloudflare API client. Cloudflare
|
|
7
|
+
* account authentication and the exact REST API transport remain application
|
|
8
|
+
* concerns, so the package intentionally accepts this narrow interface.
|
|
9
|
+
*/
|
|
10
|
+
var CloudflareR2BucketProvisioner = class {
|
|
11
|
+
api;
|
|
12
|
+
nameForTenant;
|
|
13
|
+
constructor(options) {
|
|
14
|
+
this.api = options.api;
|
|
15
|
+
this.nameForTenant = options.nameForTenant ?? ((tenantId) => `puzzle-${tenantId.toLowerCase().replace(/[^a-z0-9-]+/g, "-")}`);
|
|
16
|
+
}
|
|
17
|
+
async ensureBucket(input) {
|
|
18
|
+
require_utils.validateTenantId(input.tenantId);
|
|
19
|
+
const name = input.name ?? this.nameForTenant(input.tenantId);
|
|
20
|
+
if (!bucketNamePattern.test(name)) throw new require_errors.CloudflareMediaValidationError("R2 bucket names must be 3-63 lowercase characters, numbers, or hyphens.");
|
|
21
|
+
const existing = await this.api.getBucket(name);
|
|
22
|
+
if (existing) return existing;
|
|
23
|
+
try {
|
|
24
|
+
return await this.api.createBucket({
|
|
25
|
+
name,
|
|
26
|
+
...input.locationHint ? { locationHint: input.locationHint } : {}
|
|
27
|
+
});
|
|
28
|
+
} catch (error) {
|
|
29
|
+
const raced = await this.api.getBucket(name);
|
|
30
|
+
if (raced) return raced;
|
|
31
|
+
throw new require_errors.CloudflareMediaConfigurationError(`Unable to provision R2 bucket "${name}".`);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
exports.CloudflareR2BucketProvisioner = CloudflareR2BucketProvisioner;
|
|
36
|
+
|
|
37
|
+
//# sourceMappingURL=bucket-provisioner.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bucket-provisioner.cjs","names":["CloudflareMediaValidationError","CloudflareMediaConfigurationError"],"sources":["../src/bucket-provisioner.ts"],"sourcesContent":["import {\n CloudflareMediaConfigurationError,\n CloudflareMediaValidationError,\n} from \"./errors\";\nimport type {\n CloudflareR2BucketInfo,\n CloudflareR2BucketProvisionerApi,\n CloudflareR2BucketProvisionerOptions,\n} from \"./types\";\nimport { validateTenantId } from \"./utils\";\n\nconst bucketNamePattern = /^[a-z0-9][a-z0-9-]{1,61}[a-z0-9]$/;\n\n/**\n * Provisions R2 buckets through an injected Cloudflare API client. Cloudflare\n * account authentication and the exact REST API transport remain application\n * concerns, so the package intentionally accepts this narrow interface.\n */\nexport class CloudflareR2BucketProvisioner {\n private readonly api: CloudflareR2BucketProvisionerApi;\n private readonly nameForTenant: (tenantId: string) => string;\n\n constructor(options: CloudflareR2BucketProvisionerOptions) {\n this.api = options.api;\n this.nameForTenant =\n options.nameForTenant ??\n ((tenantId) => `puzzle-${tenantId.toLowerCase().replace(/[^a-z0-9-]+/g, \"-\")}`);\n }\n\n async ensureBucket(input: {\n readonly tenantId: string;\n readonly name?: string;\n readonly locationHint?: string;\n }): Promise<CloudflareR2BucketInfo> {\n validateTenantId(input.tenantId);\n const name = input.name ?? this.nameForTenant(input.tenantId);\n if (!bucketNamePattern.test(name)) {\n throw new CloudflareMediaValidationError(\n \"R2 bucket names must be 3-63 lowercase characters, numbers, or hyphens.\",\n );\n }\n const existing = await this.api.getBucket(name);\n if (existing) return existing;\n try {\n return await this.api.createBucket({\n name,\n ...(input.locationHint ? { locationHint: input.locationHint } : {}),\n });\n } catch (error) {\n const raced = await this.api.getBucket(name);\n if (raced) return raced;\n throw new CloudflareMediaConfigurationError(\n `Unable to provision R2 bucket \"${name}\".`,\n );\n }\n }\n}\n"],"mappings":";;;AAWA,MAAM,oBAAoB;;;;;;AAO1B,IAAa,gCAAb,MAA2C;CACzC;CACA;CAEA,YAAY,SAA+C;EACzD,KAAK,MAAM,QAAQ;EACnB,KAAK,gBACH,QAAQ,mBACN,aAAa,UAAU,SAAS,YAAY,CAAC,CAAC,QAAQ,gBAAgB,GAAG;CAC/E;CAEA,MAAM,aAAa,OAIiB;EAClC,cAAA,iBAAiB,MAAM,QAAQ;EAC/B,MAAM,OAAO,MAAM,QAAQ,KAAK,cAAc,MAAM,QAAQ;EAC5D,IAAI,CAAC,kBAAkB,KAAK,IAAI,GAC9B,MAAM,IAAIA,eAAAA,+BACR,yEACF;EAEF,MAAM,WAAW,MAAM,KAAK,IAAI,UAAU,IAAI;EAC9C,IAAI,UAAU,OAAO;EACrB,IAAI;GACF,OAAO,MAAM,KAAK,IAAI,aAAa;IACjC;IACA,GAAI,MAAM,eAAe,EAAE,cAAc,MAAM,aAAa,IAAI,CAAC;GACnE,CAAC;EACH,SAAS,OAAO;GACd,MAAM,QAAQ,MAAM,KAAK,IAAI,UAAU,IAAI;GAC3C,IAAI,OAAO,OAAO;GAClB,MAAM,IAAIC,eAAAA,kCACR,kCAAkC,KAAK,GACzC;EACF;CACF;AACF"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { CloudflareR2BucketInfo, CloudflareR2BucketProvisionerOptions } from "./types.cjs";
|
|
2
|
+
/**
|
|
3
|
+
* Provisions R2 buckets through an injected Cloudflare API client. Cloudflare
|
|
4
|
+
* account authentication and the exact REST API transport remain application
|
|
5
|
+
* concerns, so the package intentionally accepts this narrow interface.
|
|
6
|
+
*/
|
|
7
|
+
export declare class CloudflareR2BucketProvisioner {
|
|
8
|
+
private readonly api;
|
|
9
|
+
private readonly nameForTenant;
|
|
10
|
+
constructor(options: CloudflareR2BucketProvisionerOptions);
|
|
11
|
+
ensureBucket(input: {
|
|
12
|
+
readonly tenantId: string;
|
|
13
|
+
readonly name?: string;
|
|
14
|
+
readonly locationHint?: string;
|
|
15
|
+
}): Promise<CloudflareR2BucketInfo>;
|
|
16
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { CloudflareR2BucketInfo, CloudflareR2BucketProvisionerOptions } from "./types.js";
|
|
2
|
+
/**
|
|
3
|
+
* Provisions R2 buckets through an injected Cloudflare API client. Cloudflare
|
|
4
|
+
* account authentication and the exact REST API transport remain application
|
|
5
|
+
* concerns, so the package intentionally accepts this narrow interface.
|
|
6
|
+
*/
|
|
7
|
+
export declare class CloudflareR2BucketProvisioner {
|
|
8
|
+
private readonly api;
|
|
9
|
+
private readonly nameForTenant;
|
|
10
|
+
constructor(options: CloudflareR2BucketProvisionerOptions);
|
|
11
|
+
ensureBucket(input: {
|
|
12
|
+
readonly tenantId: string;
|
|
13
|
+
readonly name?: string;
|
|
14
|
+
readonly locationHint?: string;
|
|
15
|
+
}): Promise<CloudflareR2BucketInfo>;
|
|
16
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { CloudflareMediaConfigurationError, CloudflareMediaValidationError } from "./errors.js";
|
|
2
|
+
import { validateTenantId } from "./utils.js";
|
|
3
|
+
// src/bucket-provisioner.ts
|
|
4
|
+
const bucketNamePattern = /^[a-z0-9][a-z0-9-]{1,61}[a-z0-9]$/;
|
|
5
|
+
/**
|
|
6
|
+
* Provisions R2 buckets through an injected Cloudflare API client. Cloudflare
|
|
7
|
+
* account authentication and the exact REST API transport remain application
|
|
8
|
+
* concerns, so the package intentionally accepts this narrow interface.
|
|
9
|
+
*/
|
|
10
|
+
var CloudflareR2BucketProvisioner = class {
|
|
11
|
+
api;
|
|
12
|
+
nameForTenant;
|
|
13
|
+
constructor(options) {
|
|
14
|
+
this.api = options.api;
|
|
15
|
+
this.nameForTenant = options.nameForTenant ?? ((tenantId) => `puzzle-${tenantId.toLowerCase().replace(/[^a-z0-9-]+/g, "-")}`);
|
|
16
|
+
}
|
|
17
|
+
async ensureBucket(input) {
|
|
18
|
+
validateTenantId(input.tenantId);
|
|
19
|
+
const name = input.name ?? this.nameForTenant(input.tenantId);
|
|
20
|
+
if (!bucketNamePattern.test(name)) throw new CloudflareMediaValidationError("R2 bucket names must be 3-63 lowercase characters, numbers, or hyphens.");
|
|
21
|
+
const existing = await this.api.getBucket(name);
|
|
22
|
+
if (existing) return existing;
|
|
23
|
+
try {
|
|
24
|
+
return await this.api.createBucket({
|
|
25
|
+
name,
|
|
26
|
+
...input.locationHint ? { locationHint: input.locationHint } : {}
|
|
27
|
+
});
|
|
28
|
+
} catch (error) {
|
|
29
|
+
const raced = await this.api.getBucket(name);
|
|
30
|
+
if (raced) return raced;
|
|
31
|
+
throw new CloudflareMediaConfigurationError(`Unable to provision R2 bucket "${name}".`);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
export { CloudflareR2BucketProvisioner };
|
|
36
|
+
|
|
37
|
+
//# sourceMappingURL=bucket-provisioner.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bucket-provisioner.js","names":[],"sources":["../src/bucket-provisioner.ts"],"sourcesContent":["import {\n CloudflareMediaConfigurationError,\n CloudflareMediaValidationError,\n} from \"./errors\";\nimport type {\n CloudflareR2BucketInfo,\n CloudflareR2BucketProvisionerApi,\n CloudflareR2BucketProvisionerOptions,\n} from \"./types\";\nimport { validateTenantId } from \"./utils\";\n\nconst bucketNamePattern = /^[a-z0-9][a-z0-9-]{1,61}[a-z0-9]$/;\n\n/**\n * Provisions R2 buckets through an injected Cloudflare API client. Cloudflare\n * account authentication and the exact REST API transport remain application\n * concerns, so the package intentionally accepts this narrow interface.\n */\nexport class CloudflareR2BucketProvisioner {\n private readonly api: CloudflareR2BucketProvisionerApi;\n private readonly nameForTenant: (tenantId: string) => string;\n\n constructor(options: CloudflareR2BucketProvisionerOptions) {\n this.api = options.api;\n this.nameForTenant =\n options.nameForTenant ??\n ((tenantId) => `puzzle-${tenantId.toLowerCase().replace(/[^a-z0-9-]+/g, \"-\")}`);\n }\n\n async ensureBucket(input: {\n readonly tenantId: string;\n readonly name?: string;\n readonly locationHint?: string;\n }): Promise<CloudflareR2BucketInfo> {\n validateTenantId(input.tenantId);\n const name = input.name ?? this.nameForTenant(input.tenantId);\n if (!bucketNamePattern.test(name)) {\n throw new CloudflareMediaValidationError(\n \"R2 bucket names must be 3-63 lowercase characters, numbers, or hyphens.\",\n );\n }\n const existing = await this.api.getBucket(name);\n if (existing) return existing;\n try {\n return await this.api.createBucket({\n name,\n ...(input.locationHint ? { locationHint: input.locationHint } : {}),\n });\n } catch (error) {\n const raced = await this.api.getBucket(name);\n if (raced) return raced;\n throw new CloudflareMediaConfigurationError(\n `Unable to provision R2 bucket \"${name}\".`,\n );\n }\n }\n}\n"],"mappings":";;;AAWA,MAAM,oBAAoB;;;;;;AAO1B,IAAa,gCAAb,MAA2C;CACzC;CACA;CAEA,YAAY,SAA+C;EACzD,KAAK,MAAM,QAAQ;EACnB,KAAK,gBACH,QAAQ,mBACN,aAAa,UAAU,SAAS,YAAY,CAAC,CAAC,QAAQ,gBAAgB,GAAG;CAC/E;CAEA,MAAM,aAAa,OAIiB;EAClC,iBAAiB,MAAM,QAAQ;EAC/B,MAAM,OAAO,MAAM,QAAQ,KAAK,cAAc,MAAM,QAAQ;EAC5D,IAAI,CAAC,kBAAkB,KAAK,IAAI,GAC9B,MAAM,IAAI,+BACR,yEACF;EAEF,MAAM,WAAW,MAAM,KAAK,IAAI,UAAU,IAAI;EAC9C,IAAI,UAAU,OAAO;EACrB,IAAI;GACF,OAAO,MAAM,KAAK,IAAI,aAAa;IACjC;IACA,GAAI,MAAM,eAAe,EAAE,cAAc,MAAM,aAAa,IAAI,CAAC;GACnE,CAAC;EACH,SAAS,OAAO;GACd,MAAM,QAAQ,MAAM,KAAK,IAAI,UAAU,IAAI;GAC3C,IAAI,OAAO,OAAO;GAClB,MAAM,IAAI,kCACR,kCAAkC,KAAK,GACzC;EACF;CACF;AACF"}
|
package/dist/errors.cjs
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
// src/errors.ts
|
|
2
|
+
var CloudflareMediaError = class extends Error {
|
|
3
|
+
code;
|
|
4
|
+
cause;
|
|
5
|
+
constructor(code, message, options) {
|
|
6
|
+
super(message);
|
|
7
|
+
this.name = "CloudflareMediaError";
|
|
8
|
+
this.code = code;
|
|
9
|
+
this.cause = options?.cause;
|
|
10
|
+
}
|
|
11
|
+
};
|
|
12
|
+
var CloudflareMediaConfigurationError = class extends CloudflareMediaError {
|
|
13
|
+
constructor(message) {
|
|
14
|
+
super("CONFIGURATION", message);
|
|
15
|
+
this.name = "CloudflareMediaConfigurationError";
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
var CloudflareMediaProviderError = class extends CloudflareMediaError {
|
|
19
|
+
constructor(message, cause) {
|
|
20
|
+
super("PROVIDER", message, { cause });
|
|
21
|
+
this.name = "CloudflareMediaProviderError";
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
var CloudflareMediaValidationError = class extends CloudflareMediaError {
|
|
25
|
+
constructor(message) {
|
|
26
|
+
super("VALIDATION", message);
|
|
27
|
+
this.name = "CloudflareMediaValidationError";
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
exports.CloudflareMediaConfigurationError = CloudflareMediaConfigurationError;
|
|
31
|
+
exports.CloudflareMediaError = CloudflareMediaError;
|
|
32
|
+
exports.CloudflareMediaProviderError = CloudflareMediaProviderError;
|
|
33
|
+
exports.CloudflareMediaValidationError = CloudflareMediaValidationError;
|
|
34
|
+
|
|
35
|
+
//# sourceMappingURL=errors.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.cjs","names":[],"sources":["../src/errors.ts"],"sourcesContent":["export type CloudflareMediaErrorCode =\n | \"CONFIGURATION\"\n | \"PROVIDER\"\n | \"VALIDATION\";\n\nexport class CloudflareMediaError extends Error {\n readonly code: CloudflareMediaErrorCode;\n override readonly cause?: unknown;\n\n constructor(\n code: CloudflareMediaErrorCode,\n message: string,\n options?: { readonly cause?: unknown },\n ) {\n super(message);\n this.name = \"CloudflareMediaError\";\n this.code = code;\n this.cause = options?.cause;\n }\n}\n\nexport class CloudflareMediaConfigurationError extends CloudflareMediaError {\n constructor(message: string) {\n super(\"CONFIGURATION\", message);\n this.name = \"CloudflareMediaConfigurationError\";\n }\n}\n\nexport class CloudflareMediaProviderError extends CloudflareMediaError {\n constructor(message: string, cause?: unknown) {\n super(\"PROVIDER\", message, { cause });\n this.name = \"CloudflareMediaProviderError\";\n }\n}\n\nexport class CloudflareMediaValidationError extends CloudflareMediaError {\n constructor(message: string) {\n super(\"VALIDATION\", message);\n this.name = \"CloudflareMediaValidationError\";\n }\n}\n"],"mappings":";AAKA,IAAa,uBAAb,cAA0C,MAAM;CAC9C;CACA;CAEA,YACE,MACA,SACA,SACA;EACA,MAAM,OAAO;EACb,KAAK,OAAO;EACZ,KAAK,OAAO;EACZ,KAAK,QAAQ,SAAS;CACxB;AACF;AAEA,IAAa,oCAAb,cAAuD,qBAAqB;CAC1E,YAAY,SAAiB;EAC3B,MAAM,iBAAiB,OAAO;EAC9B,KAAK,OAAO;CACd;AACF;AAEA,IAAa,+BAAb,cAAkD,qBAAqB;CACrE,YAAY,SAAiB,OAAiB;EAC5C,MAAM,YAAY,SAAS,EAAE,MAAM,CAAC;EACpC,KAAK,OAAO;CACd;AACF;AAEA,IAAa,iCAAb,cAAoD,qBAAqB;CACvE,YAAY,SAAiB;EAC3B,MAAM,cAAc,OAAO;EAC3B,KAAK,OAAO;CACd;AACF"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export type CloudflareMediaErrorCode = "CONFIGURATION" | "PROVIDER" | "VALIDATION";
|
|
2
|
+
export declare class CloudflareMediaError extends Error {
|
|
3
|
+
readonly code: CloudflareMediaErrorCode;
|
|
4
|
+
readonly cause?: unknown;
|
|
5
|
+
constructor(code: CloudflareMediaErrorCode, message: string, options?: {
|
|
6
|
+
readonly cause?: unknown;
|
|
7
|
+
});
|
|
8
|
+
}
|
|
9
|
+
export declare class CloudflareMediaConfigurationError extends CloudflareMediaError {
|
|
10
|
+
constructor(message: string);
|
|
11
|
+
}
|
|
12
|
+
export declare class CloudflareMediaProviderError extends CloudflareMediaError {
|
|
13
|
+
constructor(message: string, cause?: unknown);
|
|
14
|
+
}
|
|
15
|
+
export declare class CloudflareMediaValidationError extends CloudflareMediaError {
|
|
16
|
+
constructor(message: string);
|
|
17
|
+
}
|
package/dist/errors.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export type CloudflareMediaErrorCode = "CONFIGURATION" | "PROVIDER" | "VALIDATION";
|
|
2
|
+
export declare class CloudflareMediaError extends Error {
|
|
3
|
+
readonly code: CloudflareMediaErrorCode;
|
|
4
|
+
readonly cause?: unknown;
|
|
5
|
+
constructor(code: CloudflareMediaErrorCode, message: string, options?: {
|
|
6
|
+
readonly cause?: unknown;
|
|
7
|
+
});
|
|
8
|
+
}
|
|
9
|
+
export declare class CloudflareMediaConfigurationError extends CloudflareMediaError {
|
|
10
|
+
constructor(message: string);
|
|
11
|
+
}
|
|
12
|
+
export declare class CloudflareMediaProviderError extends CloudflareMediaError {
|
|
13
|
+
constructor(message: string, cause?: unknown);
|
|
14
|
+
}
|
|
15
|
+
export declare class CloudflareMediaValidationError extends CloudflareMediaError {
|
|
16
|
+
constructor(message: string);
|
|
17
|
+
}
|
package/dist/errors.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
// src/errors.ts
|
|
2
|
+
var CloudflareMediaError = class extends Error {
|
|
3
|
+
code;
|
|
4
|
+
cause;
|
|
5
|
+
constructor(code, message, options) {
|
|
6
|
+
super(message);
|
|
7
|
+
this.name = "CloudflareMediaError";
|
|
8
|
+
this.code = code;
|
|
9
|
+
this.cause = options?.cause;
|
|
10
|
+
}
|
|
11
|
+
};
|
|
12
|
+
var CloudflareMediaConfigurationError = class extends CloudflareMediaError {
|
|
13
|
+
constructor(message) {
|
|
14
|
+
super("CONFIGURATION", message);
|
|
15
|
+
this.name = "CloudflareMediaConfigurationError";
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
var CloudflareMediaProviderError = class extends CloudflareMediaError {
|
|
19
|
+
constructor(message, cause) {
|
|
20
|
+
super("PROVIDER", message, { cause });
|
|
21
|
+
this.name = "CloudflareMediaProviderError";
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
var CloudflareMediaValidationError = class extends CloudflareMediaError {
|
|
25
|
+
constructor(message) {
|
|
26
|
+
super("VALIDATION", message);
|
|
27
|
+
this.name = "CloudflareMediaValidationError";
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
export { CloudflareMediaConfigurationError, CloudflareMediaError, CloudflareMediaProviderError, CloudflareMediaValidationError };
|
|
31
|
+
|
|
32
|
+
//# sourceMappingURL=errors.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.js","names":[],"sources":["../src/errors.ts"],"sourcesContent":["export type CloudflareMediaErrorCode =\n | \"CONFIGURATION\"\n | \"PROVIDER\"\n | \"VALIDATION\";\n\nexport class CloudflareMediaError extends Error {\n readonly code: CloudflareMediaErrorCode;\n override readonly cause?: unknown;\n\n constructor(\n code: CloudflareMediaErrorCode,\n message: string,\n options?: { readonly cause?: unknown },\n ) {\n super(message);\n this.name = \"CloudflareMediaError\";\n this.code = code;\n this.cause = options?.cause;\n }\n}\n\nexport class CloudflareMediaConfigurationError extends CloudflareMediaError {\n constructor(message: string) {\n super(\"CONFIGURATION\", message);\n this.name = \"CloudflareMediaConfigurationError\";\n }\n}\n\nexport class CloudflareMediaProviderError extends CloudflareMediaError {\n constructor(message: string, cause?: unknown) {\n super(\"PROVIDER\", message, { cause });\n this.name = \"CloudflareMediaProviderError\";\n }\n}\n\nexport class CloudflareMediaValidationError extends CloudflareMediaError {\n constructor(message: string) {\n super(\"VALIDATION\", message);\n this.name = \"CloudflareMediaValidationError\";\n }\n}\n"],"mappings":";AAKA,IAAa,uBAAb,cAA0C,MAAM;CAC9C;CACA;CAEA,YACE,MACA,SACA,SACA;EACA,MAAM,OAAO;EACb,KAAK,OAAO;EACZ,KAAK,OAAO;EACZ,KAAK,QAAQ,SAAS;CACxB;AACF;AAEA,IAAa,oCAAb,cAAuD,qBAAqB;CAC1E,YAAY,SAAiB;EAC3B,MAAM,iBAAiB,OAAO;EAC9B,KAAK,OAAO;CACd;AACF;AAEA,IAAa,+BAAb,cAAkD,qBAAqB;CACrE,YAAY,SAAiB,OAAiB;EAC5C,MAAM,YAAY,SAAS,EAAE,MAAM,CAAC;EACpC,KAAK,OAAO;CACd;AACF;AAEA,IAAa,iCAAb,cAAoD,qBAAqB;CACvE,YAAY,SAAiB;EAC3B,MAAM,cAAc,OAAO;EAC3B,KAAK,OAAO;CACd;AACF"}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
const require_errors = require("./errors.cjs");
|
|
2
|
+
const require_utils = require("./utils.cjs");
|
|
3
|
+
let _puzzle_ui_media = require("@puzzle-ui/media");
|
|
4
|
+
// src/images-processor.ts
|
|
5
|
+
const readOutput = async (output) => {
|
|
6
|
+
const response = await output.response();
|
|
7
|
+
if ("ok" in response && response.ok === false) throw new require_errors.CloudflareMediaProviderError(`Cloudflare Images returned HTTP ${response.status}.`);
|
|
8
|
+
const bytes = new Uint8Array(await response.arrayBuffer());
|
|
9
|
+
const contentType = response.headers?.get("content-type") ?? response.headers?.get("Content-Type") ?? void 0;
|
|
10
|
+
return {
|
|
11
|
+
bytes,
|
|
12
|
+
...contentType ? { mimeType: contentType } : {}
|
|
13
|
+
};
|
|
14
|
+
};
|
|
15
|
+
const throwIfAborted = (signal) => {
|
|
16
|
+
if (!signal?.aborted) return;
|
|
17
|
+
const error = /* @__PURE__ */ new Error("Cloudflare Images processing was aborted.");
|
|
18
|
+
error.name = "AbortError";
|
|
19
|
+
throw error;
|
|
20
|
+
};
|
|
21
|
+
const profileKey = (profile) => JSON.stringify([
|
|
22
|
+
profile.maxWidth ?? null,
|
|
23
|
+
profile.maxHeight ?? null,
|
|
24
|
+
profile.format ?? null,
|
|
25
|
+
profile.quality ?? null
|
|
26
|
+
]);
|
|
27
|
+
/**
|
|
28
|
+
* MediaProcessor implementation for the Cloudflare Images binding. The
|
|
29
|
+
* binding's exact runtime type differs between Workers releases, so it is
|
|
30
|
+
* injected as a small structural interface. Source reads and variant writes
|
|
31
|
+
* are also injected to keep credentials and object keys server-side.
|
|
32
|
+
*/
|
|
33
|
+
var CloudflareImagesProcessor = class {
|
|
34
|
+
options;
|
|
35
|
+
nextId = 0;
|
|
36
|
+
constructor(options) {
|
|
37
|
+
this.options = options;
|
|
38
|
+
}
|
|
39
|
+
async processImage(input) {
|
|
40
|
+
const profiles = (0, _puzzle_ui_media.validateMediaImageProfiles)(input.profiles);
|
|
41
|
+
const variants = [];
|
|
42
|
+
const transformedByProfile = /* @__PURE__ */ new Map();
|
|
43
|
+
for (let index = 0; index < profiles.length; index += 1) {
|
|
44
|
+
throwIfAborted(input.signal);
|
|
45
|
+
const profile = profiles[index];
|
|
46
|
+
const key = profileKey(profile);
|
|
47
|
+
const existing = transformedByProfile.get(key);
|
|
48
|
+
if (existing) {
|
|
49
|
+
variants.push({
|
|
50
|
+
...existing,
|
|
51
|
+
name: profile.name
|
|
52
|
+
});
|
|
53
|
+
await input.onProgress((index + 1) / profiles.length);
|
|
54
|
+
continue;
|
|
55
|
+
}
|
|
56
|
+
const source = await this.options.source(input.asset);
|
|
57
|
+
throwIfAborted(input.signal);
|
|
58
|
+
const transformed = this.options.images.input(source).transform({
|
|
59
|
+
...profile.maxWidth !== void 0 ? { width: profile.maxWidth } : {},
|
|
60
|
+
...profile.maxHeight !== void 0 ? { height: profile.maxHeight } : {},
|
|
61
|
+
fit: "scale-down"
|
|
62
|
+
}).output({
|
|
63
|
+
...profile.format ? { format: profile.format.replace(/^image\//, "") } : {},
|
|
64
|
+
...profile.quality !== void 0 ? { quality: profile.quality } : {}
|
|
65
|
+
});
|
|
66
|
+
const result = await readOutput(transformed);
|
|
67
|
+
throwIfAborted(input.signal);
|
|
68
|
+
const checksum = await require_utils.sha256(result.bytes);
|
|
69
|
+
const written = await this.options.writeVariant({
|
|
70
|
+
asset: input.asset,
|
|
71
|
+
profile,
|
|
72
|
+
bytes: result.bytes,
|
|
73
|
+
mimeType: result.mimeType ?? profile.format ?? input.asset.mimeType,
|
|
74
|
+
checksum
|
|
75
|
+
});
|
|
76
|
+
const mimeType = written.mimeType ?? result.mimeType ?? profile.format ?? input.asset.mimeType;
|
|
77
|
+
const variant = {
|
|
78
|
+
name: profile.name,
|
|
79
|
+
kind: "image",
|
|
80
|
+
object: written.object,
|
|
81
|
+
mimeType,
|
|
82
|
+
size: written.size ?? result.bytes.byteLength,
|
|
83
|
+
checksum,
|
|
84
|
+
...written.width !== void 0 ? { width: written.width } : {},
|
|
85
|
+
...written.height !== void 0 ? { height: written.height } : {}
|
|
86
|
+
};
|
|
87
|
+
transformedByProfile.set(key, variant);
|
|
88
|
+
variants.push(variant);
|
|
89
|
+
await input.onProgress((index + 1) / profiles.length);
|
|
90
|
+
}
|
|
91
|
+
return { variants };
|
|
92
|
+
}
|
|
93
|
+
createVariantId() {
|
|
94
|
+
this.nextId += 1;
|
|
95
|
+
return this.options.idFactory?.() ?? `cloudflare-image-${this.nextId}`;
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
exports.CloudflareImagesProcessor = CloudflareImagesProcessor;
|
|
99
|
+
|
|
100
|
+
//# sourceMappingURL=images-processor.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"images-processor.cjs","names":["CloudflareMediaProviderError","validateMediaImageProfiles","sha256"],"sources":["../src/images-processor.ts"],"sourcesContent":["import type {\n MediaProcessor,\n MediaProcessingOutput,\n MediaProcessingOutputVariant,\n MediaProcessorInput,\n} from \"@puzzle-ui/media\";\nimport { validateMediaImageProfiles } from \"@puzzle-ui/media\";\nimport { CloudflareMediaProviderError } from \"./errors\";\nimport type {\n CloudflareImagesOutputLike,\n CloudflareImagesProcessorOptions,\n} from \"./types\";\nimport { sha256 } from \"./utils\";\n\nconst readOutput = async (\n output: CloudflareImagesOutputLike,\n): Promise<{ readonly bytes: Uint8Array; readonly mimeType?: string }> => {\n const response = await output.response();\n if (\"ok\" in response && response.ok === false) {\n throw new CloudflareMediaProviderError(\n `Cloudflare Images returned HTTP ${response.status}.`,\n );\n }\n const bytes = new Uint8Array(await response.arrayBuffer());\n const contentType =\n response.headers?.get(\"content-type\") ?? response.headers?.get(\"Content-Type\") ?? undefined;\n return { bytes, ...(contentType ? { mimeType: contentType } : {}) };\n};\n\nconst throwIfAborted = (signal?: AbortSignal): void => {\n if (!signal?.aborted) return;\n const error = new Error(\"Cloudflare Images processing was aborted.\");\n error.name = \"AbortError\";\n throw error;\n};\n\nconst profileKey = (profile: MediaProcessorInput[\"profiles\"][number]): string =>\n JSON.stringify([\n profile.maxWidth ?? null,\n profile.maxHeight ?? null,\n profile.format ?? null,\n profile.quality ?? null,\n ]);\n\n/**\n * MediaProcessor implementation for the Cloudflare Images binding. The\n * binding's exact runtime type differs between Workers releases, so it is\n * injected as a small structural interface. Source reads and variant writes\n * are also injected to keep credentials and object keys server-side.\n */\nexport class CloudflareImagesProcessor implements MediaProcessor {\n private readonly options: CloudflareImagesProcessorOptions;\n private nextId = 0;\n\n constructor(options: CloudflareImagesProcessorOptions) {\n this.options = options;\n }\n\n async processImage(input: MediaProcessorInput): Promise<MediaProcessingOutput> {\n const profiles = validateMediaImageProfiles(input.profiles);\n const variants: MediaProcessingOutputVariant[] = [];\n const transformedByProfile = new Map<string, MediaProcessingOutputVariant>();\n for (let index = 0; index < profiles.length; index += 1) {\n throwIfAborted(input.signal);\n const profile = profiles[index]!;\n const key = profileKey(profile);\n const existing = transformedByProfile.get(key);\n if (existing) {\n variants.push({ ...existing, name: profile.name });\n await input.onProgress((index + 1) / profiles.length);\n continue;\n }\n const source = await this.options.source(input.asset);\n throwIfAborted(input.signal);\n const transformed = this.options.images\n .input(source)\n .transform({\n ...(profile.maxWidth !== undefined ? { width: profile.maxWidth } : {}),\n ...(profile.maxHeight !== undefined ? { height: profile.maxHeight } : {}),\n fit: \"scale-down\",\n })\n .output({\n ...(profile.format\n ? { format: profile.format.replace(/^image\\//, \"\") }\n : {}),\n ...(profile.quality !== undefined ? { quality: profile.quality } : {}),\n });\n const result = await readOutput(transformed);\n throwIfAborted(input.signal);\n const checksum = await sha256(result.bytes);\n const written = await this.options.writeVariant({\n asset: input.asset,\n profile,\n bytes: result.bytes,\n mimeType: result.mimeType ?? profile.format ?? input.asset.mimeType,\n checksum,\n });\n const mimeType = written.mimeType ?? result.mimeType ?? profile.format ?? input.asset.mimeType;\n const variant: MediaProcessingOutputVariant = {\n name: profile.name,\n kind: \"image\",\n object: written.object,\n mimeType,\n size: written.size ?? result.bytes.byteLength,\n checksum,\n ...(written.width !== undefined ? { width: written.width } : {}),\n ...(written.height !== undefined ? { height: written.height } : {}),\n };\n transformedByProfile.set(key, variant);\n variants.push(variant);\n await input.onProgress((index + 1) / profiles.length);\n }\n return { variants };\n }\n\n createVariantId(): string {\n this.nextId += 1;\n return this.options.idFactory?.() ?? `cloudflare-image-${this.nextId}`;\n }\n}\n"],"mappings":"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"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { CloudflareImagesProcessorOptions } from "./types.cjs";
|
|
2
|
+
import { MediaProcessingOutput, MediaProcessor, MediaProcessorInput } from "@puzzle-ui/media";
|
|
3
|
+
/**
|
|
4
|
+
* MediaProcessor implementation for the Cloudflare Images binding. The
|
|
5
|
+
* binding's exact runtime type differs between Workers releases, so it is
|
|
6
|
+
* injected as a small structural interface. Source reads and variant writes
|
|
7
|
+
* are also injected to keep credentials and object keys server-side.
|
|
8
|
+
*/
|
|
9
|
+
export declare class CloudflareImagesProcessor implements MediaProcessor {
|
|
10
|
+
private readonly options;
|
|
11
|
+
private nextId;
|
|
12
|
+
constructor(options: CloudflareImagesProcessorOptions);
|
|
13
|
+
processImage(input: MediaProcessorInput): Promise<MediaProcessingOutput>;
|
|
14
|
+
createVariantId(): string;
|
|
15
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { CloudflareImagesProcessorOptions } from "./types.js";
|
|
2
|
+
import { MediaProcessingOutput, MediaProcessor, MediaProcessorInput } from "@puzzle-ui/media";
|
|
3
|
+
/**
|
|
4
|
+
* MediaProcessor implementation for the Cloudflare Images binding. The
|
|
5
|
+
* binding's exact runtime type differs between Workers releases, so it is
|
|
6
|
+
* injected as a small structural interface. Source reads and variant writes
|
|
7
|
+
* are also injected to keep credentials and object keys server-side.
|
|
8
|
+
*/
|
|
9
|
+
export declare class CloudflareImagesProcessor implements MediaProcessor {
|
|
10
|
+
private readonly options;
|
|
11
|
+
private nextId;
|
|
12
|
+
constructor(options: CloudflareImagesProcessorOptions);
|
|
13
|
+
processImage(input: MediaProcessorInput): Promise<MediaProcessingOutput>;
|
|
14
|
+
createVariantId(): string;
|
|
15
|
+
}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { CloudflareMediaProviderError } from "./errors.js";
|
|
2
|
+
import { sha256 } from "./utils.js";
|
|
3
|
+
import { validateMediaImageProfiles } from "@puzzle-ui/media";
|
|
4
|
+
// src/images-processor.ts
|
|
5
|
+
const readOutput = async (output) => {
|
|
6
|
+
const response = await output.response();
|
|
7
|
+
if ("ok" in response && response.ok === false) throw new CloudflareMediaProviderError(`Cloudflare Images returned HTTP ${response.status}.`);
|
|
8
|
+
const bytes = new Uint8Array(await response.arrayBuffer());
|
|
9
|
+
const contentType = response.headers?.get("content-type") ?? response.headers?.get("Content-Type") ?? void 0;
|
|
10
|
+
return {
|
|
11
|
+
bytes,
|
|
12
|
+
...contentType ? { mimeType: contentType } : {}
|
|
13
|
+
};
|
|
14
|
+
};
|
|
15
|
+
const throwIfAborted = (signal) => {
|
|
16
|
+
if (!signal?.aborted) return;
|
|
17
|
+
const error = /* @__PURE__ */ new Error("Cloudflare Images processing was aborted.");
|
|
18
|
+
error.name = "AbortError";
|
|
19
|
+
throw error;
|
|
20
|
+
};
|
|
21
|
+
const profileKey = (profile) => JSON.stringify([
|
|
22
|
+
profile.maxWidth ?? null,
|
|
23
|
+
profile.maxHeight ?? null,
|
|
24
|
+
profile.format ?? null,
|
|
25
|
+
profile.quality ?? null
|
|
26
|
+
]);
|
|
27
|
+
/**
|
|
28
|
+
* MediaProcessor implementation for the Cloudflare Images binding. The
|
|
29
|
+
* binding's exact runtime type differs between Workers releases, so it is
|
|
30
|
+
* injected as a small structural interface. Source reads and variant writes
|
|
31
|
+
* are also injected to keep credentials and object keys server-side.
|
|
32
|
+
*/
|
|
33
|
+
var CloudflareImagesProcessor = class {
|
|
34
|
+
options;
|
|
35
|
+
nextId = 0;
|
|
36
|
+
constructor(options) {
|
|
37
|
+
this.options = options;
|
|
38
|
+
}
|
|
39
|
+
async processImage(input) {
|
|
40
|
+
const profiles = validateMediaImageProfiles(input.profiles);
|
|
41
|
+
const variants = [];
|
|
42
|
+
const transformedByProfile = /* @__PURE__ */ new Map();
|
|
43
|
+
for (let index = 0; index < profiles.length; index += 1) {
|
|
44
|
+
throwIfAborted(input.signal);
|
|
45
|
+
const profile = profiles[index];
|
|
46
|
+
const key = profileKey(profile);
|
|
47
|
+
const existing = transformedByProfile.get(key);
|
|
48
|
+
if (existing) {
|
|
49
|
+
variants.push({
|
|
50
|
+
...existing,
|
|
51
|
+
name: profile.name
|
|
52
|
+
});
|
|
53
|
+
await input.onProgress((index + 1) / profiles.length);
|
|
54
|
+
continue;
|
|
55
|
+
}
|
|
56
|
+
const source = await this.options.source(input.asset);
|
|
57
|
+
throwIfAborted(input.signal);
|
|
58
|
+
const transformed = this.options.images.input(source).transform({
|
|
59
|
+
...profile.maxWidth !== void 0 ? { width: profile.maxWidth } : {},
|
|
60
|
+
...profile.maxHeight !== void 0 ? { height: profile.maxHeight } : {},
|
|
61
|
+
fit: "scale-down"
|
|
62
|
+
}).output({
|
|
63
|
+
...profile.format ? { format: profile.format.replace(/^image\//, "") } : {},
|
|
64
|
+
...profile.quality !== void 0 ? { quality: profile.quality } : {}
|
|
65
|
+
});
|
|
66
|
+
const result = await readOutput(transformed);
|
|
67
|
+
throwIfAborted(input.signal);
|
|
68
|
+
const checksum = await sha256(result.bytes);
|
|
69
|
+
const written = await this.options.writeVariant({
|
|
70
|
+
asset: input.asset,
|
|
71
|
+
profile,
|
|
72
|
+
bytes: result.bytes,
|
|
73
|
+
mimeType: result.mimeType ?? profile.format ?? input.asset.mimeType,
|
|
74
|
+
checksum
|
|
75
|
+
});
|
|
76
|
+
const mimeType = written.mimeType ?? result.mimeType ?? profile.format ?? input.asset.mimeType;
|
|
77
|
+
const variant = {
|
|
78
|
+
name: profile.name,
|
|
79
|
+
kind: "image",
|
|
80
|
+
object: written.object,
|
|
81
|
+
mimeType,
|
|
82
|
+
size: written.size ?? result.bytes.byteLength,
|
|
83
|
+
checksum,
|
|
84
|
+
...written.width !== void 0 ? { width: written.width } : {},
|
|
85
|
+
...written.height !== void 0 ? { height: written.height } : {}
|
|
86
|
+
};
|
|
87
|
+
transformedByProfile.set(key, variant);
|
|
88
|
+
variants.push(variant);
|
|
89
|
+
await input.onProgress((index + 1) / profiles.length);
|
|
90
|
+
}
|
|
91
|
+
return { variants };
|
|
92
|
+
}
|
|
93
|
+
createVariantId() {
|
|
94
|
+
this.nextId += 1;
|
|
95
|
+
return this.options.idFactory?.() ?? `cloudflare-image-${this.nextId}`;
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
export { CloudflareImagesProcessor };
|
|
99
|
+
|
|
100
|
+
//# sourceMappingURL=images-processor.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"images-processor.js","names":[],"sources":["../src/images-processor.ts"],"sourcesContent":["import type {\n MediaProcessor,\n MediaProcessingOutput,\n MediaProcessingOutputVariant,\n MediaProcessorInput,\n} from \"@puzzle-ui/media\";\nimport { validateMediaImageProfiles } from \"@puzzle-ui/media\";\nimport { CloudflareMediaProviderError } from \"./errors\";\nimport type {\n CloudflareImagesOutputLike,\n CloudflareImagesProcessorOptions,\n} from \"./types\";\nimport { sha256 } from \"./utils\";\n\nconst readOutput = async (\n output: CloudflareImagesOutputLike,\n): Promise<{ readonly bytes: Uint8Array; readonly mimeType?: string }> => {\n const response = await output.response();\n if (\"ok\" in response && response.ok === false) {\n throw new CloudflareMediaProviderError(\n `Cloudflare Images returned HTTP ${response.status}.`,\n );\n }\n const bytes = new Uint8Array(await response.arrayBuffer());\n const contentType =\n response.headers?.get(\"content-type\") ?? response.headers?.get(\"Content-Type\") ?? undefined;\n return { bytes, ...(contentType ? { mimeType: contentType } : {}) };\n};\n\nconst throwIfAborted = (signal?: AbortSignal): void => {\n if (!signal?.aborted) return;\n const error = new Error(\"Cloudflare Images processing was aborted.\");\n error.name = \"AbortError\";\n throw error;\n};\n\nconst profileKey = (profile: MediaProcessorInput[\"profiles\"][number]): string =>\n JSON.stringify([\n profile.maxWidth ?? null,\n profile.maxHeight ?? null,\n profile.format ?? null,\n profile.quality ?? null,\n ]);\n\n/**\n * MediaProcessor implementation for the Cloudflare Images binding. The\n * binding's exact runtime type differs between Workers releases, so it is\n * injected as a small structural interface. Source reads and variant writes\n * are also injected to keep credentials and object keys server-side.\n */\nexport class CloudflareImagesProcessor implements MediaProcessor {\n private readonly options: CloudflareImagesProcessorOptions;\n private nextId = 0;\n\n constructor(options: CloudflareImagesProcessorOptions) {\n this.options = options;\n }\n\n async processImage(input: MediaProcessorInput): Promise<MediaProcessingOutput> {\n const profiles = validateMediaImageProfiles(input.profiles);\n const variants: MediaProcessingOutputVariant[] = [];\n const transformedByProfile = new Map<string, MediaProcessingOutputVariant>();\n for (let index = 0; index < profiles.length; index += 1) {\n throwIfAborted(input.signal);\n const profile = profiles[index]!;\n const key = profileKey(profile);\n const existing = transformedByProfile.get(key);\n if (existing) {\n variants.push({ ...existing, name: profile.name });\n await input.onProgress((index + 1) / profiles.length);\n continue;\n }\n const source = await this.options.source(input.asset);\n throwIfAborted(input.signal);\n const transformed = this.options.images\n .input(source)\n .transform({\n ...(profile.maxWidth !== undefined ? { width: profile.maxWidth } : {}),\n ...(profile.maxHeight !== undefined ? { height: profile.maxHeight } : {}),\n fit: \"scale-down\",\n })\n .output({\n ...(profile.format\n ? { format: profile.format.replace(/^image\\//, \"\") }\n : {}),\n ...(profile.quality !== undefined ? { quality: profile.quality } : {}),\n });\n const result = await readOutput(transformed);\n throwIfAborted(input.signal);\n const checksum = await sha256(result.bytes);\n const written = await this.options.writeVariant({\n asset: input.asset,\n profile,\n bytes: result.bytes,\n mimeType: result.mimeType ?? profile.format ?? input.asset.mimeType,\n checksum,\n });\n const mimeType = written.mimeType ?? result.mimeType ?? profile.format ?? input.asset.mimeType;\n const variant: MediaProcessingOutputVariant = {\n name: profile.name,\n kind: \"image\",\n object: written.object,\n mimeType,\n size: written.size ?? result.bytes.byteLength,\n checksum,\n ...(written.width !== undefined ? { width: written.width } : {}),\n ...(written.height !== undefined ? { height: written.height } : {}),\n };\n transformedByProfile.set(key, variant);\n variants.push(variant);\n await input.onProgress((index + 1) / profiles.length);\n }\n return { variants };\n }\n\n createVariantId(): string {\n this.nextId += 1;\n return this.options.idFactory?.() ?? `cloudflare-image-${this.nextId}`;\n }\n}\n"],"mappings":"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"}
|