@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/dist/utils.js
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { CloudflareMediaValidationError } from "./errors.js";
|
|
2
|
+
import { MediaValidationError, normalizeMediaChecksum, normalizeMediaObjectReference } from "@puzzle-ui/media";
|
|
3
|
+
// src/utils.ts
|
|
4
|
+
const validateTenantId = (tenantId) => {
|
|
5
|
+
if (typeof tenantId !== "string" || tenantId.trim() === "") throw new MediaValidationError("tenantId must be a non-empty string.");
|
|
6
|
+
if (tenantId.length > 200 || /[\u0000-\u001f\u007f]/.test(tenantId)) throw new MediaValidationError("tenantId contains unsupported characters.");
|
|
7
|
+
return tenantId;
|
|
8
|
+
};
|
|
9
|
+
const validateUploadConstraints = (input) => {
|
|
10
|
+
validateTenantId(input.tenantId);
|
|
11
|
+
if (typeof input.fileName !== "string" || input.fileName.trim() === "") throw new MediaValidationError("fileName must be a non-empty string.");
|
|
12
|
+
if (input.fileName.length > 512 || /[\u0000-\u001f\u007f]/.test(input.fileName)) throw new MediaValidationError("fileName contains unsupported characters.");
|
|
13
|
+
if (typeof input.contentType !== "string" || !/^[a-z0-9!#$&^_.+-]+\/[a-z0-9!#$&^_.+-]+$/i.test(input.contentType)) throw new MediaValidationError("contentType must be a valid MIME type.");
|
|
14
|
+
if (input.expectedSize !== void 0 && (!Number.isSafeInteger(input.expectedSize) || input.expectedSize < 1)) throw new MediaValidationError("expectedSize must be a positive safe integer.");
|
|
15
|
+
validatePositiveInteger(input.expiresInMs, "expiresInMs");
|
|
16
|
+
if (input.objectPrefix !== void 0) {
|
|
17
|
+
if (input.objectPrefix.trim() === "" || input.objectPrefix.startsWith("/") || input.objectPrefix.includes("\\") || input.objectPrefix.split("/").some((segment) => segment === "." || segment === "..") || /[\u0000-\u001f\u007f]/.test(input.objectPrefix)) throw new MediaValidationError("objectPrefix must be a safe relative prefix.");
|
|
18
|
+
}
|
|
19
|
+
const expectedChecksum = input.expectedChecksum ? normalizeMediaChecksum(input.expectedChecksum) : void 0;
|
|
20
|
+
return {
|
|
21
|
+
...input,
|
|
22
|
+
tenantId: input.tenantId.trim(),
|
|
23
|
+
contentType: input.contentType.toLowerCase(),
|
|
24
|
+
...expectedChecksum ? { expectedChecksum } : {}
|
|
25
|
+
};
|
|
26
|
+
};
|
|
27
|
+
const validatePositiveInteger = (value, name) => {
|
|
28
|
+
if (!Number.isSafeInteger(value) || value < 1) throw new MediaValidationError(`${name} must be a positive safe integer.`);
|
|
29
|
+
};
|
|
30
|
+
const validateExpiry = (value, name, max) => {
|
|
31
|
+
validatePositiveInteger(value, name);
|
|
32
|
+
if (value > max) throw new MediaValidationError(`${name} exceeds the configured limit.`);
|
|
33
|
+
};
|
|
34
|
+
const createTenantObjectKey = (tenantId, uploadId, fileName, prefix) => {
|
|
35
|
+
const safeName = fileName.replace(/[^\w.\-]+/g, "_").replace(/^\.*/, "") || "file";
|
|
36
|
+
return `${prefix ? `${prefix.replace(/\/+$/g, "")}/` : ""}tenants/${encodeURIComponent(tenantId)}/${uploadId}/${safeName}`;
|
|
37
|
+
};
|
|
38
|
+
const normalizeReference = (object) => normalizeMediaObjectReference(object);
|
|
39
|
+
const sha256 = async (bytes) => {
|
|
40
|
+
if (!globalThis.crypto?.subtle) throw new CloudflareMediaValidationError("Web Crypto is required for checksum verification.");
|
|
41
|
+
const digest = await globalThis.crypto.subtle.digest("SHA-256", new Uint8Array(bytes));
|
|
42
|
+
return {
|
|
43
|
+
algorithm: "sha256",
|
|
44
|
+
value: [...new Uint8Array(digest)].map((byte) => byte.toString(16).padStart(2, "0")).join("")
|
|
45
|
+
};
|
|
46
|
+
};
|
|
47
|
+
const checksumEquals = (left, right) => normalizeMediaChecksum(left).value === normalizeMediaChecksum(right).value;
|
|
48
|
+
const cursorForObject = (key) => encodeURIComponent(key);
|
|
49
|
+
export { checksumEquals, createTenantObjectKey, cursorForObject, normalizeReference, sha256, validateExpiry, validatePositiveInteger, validateTenantId, validateUploadConstraints };
|
|
50
|
+
|
|
51
|
+
//# sourceMappingURL=utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.js","names":[],"sources":["../src/utils.ts"],"sourcesContent":["import {\n MediaValidationError,\n normalizeMediaChecksum,\n normalizeMediaObjectReference,\n} from \"@puzzle-ui/media\";\nimport type {\n MediaChecksum,\n MediaObjectReference,\n MediaUploadConstraints,\n} from \"@puzzle-ui/media\";\nimport { CloudflareMediaValidationError } from \"./errors\";\n\nexport const validateTenantId = (tenantId: string): string => {\n if (typeof tenantId !== \"string\" || tenantId.trim() === \"\") {\n throw new MediaValidationError(\"tenantId must be a non-empty string.\");\n }\n if (tenantId.length > 200 || /[\\u0000-\\u001f\\u007f]/.test(tenantId)) {\n throw new MediaValidationError(\"tenantId contains unsupported characters.\");\n }\n return tenantId;\n};\n\nexport const validateUploadConstraints = (\n input: MediaUploadConstraints,\n): MediaUploadConstraints => {\n validateTenantId(input.tenantId);\n if (typeof input.fileName !== \"string\" || input.fileName.trim() === \"\") {\n throw new MediaValidationError(\"fileName must be a non-empty string.\");\n }\n if (input.fileName.length > 512 || /[\\u0000-\\u001f\\u007f]/.test(input.fileName)) {\n throw new MediaValidationError(\"fileName contains unsupported characters.\");\n }\n if (\n typeof input.contentType !== \"string\" ||\n !/^[a-z0-9!#$&^_.+-]+\\/[a-z0-9!#$&^_.+-]+$/i.test(input.contentType)\n ) {\n throw new MediaValidationError(\"contentType must be a valid MIME type.\");\n }\n if (\n input.expectedSize !== undefined &&\n (!Number.isSafeInteger(input.expectedSize) || input.expectedSize < 1)\n ) {\n throw new MediaValidationError(\"expectedSize must be a positive safe integer.\");\n }\n validatePositiveInteger(input.expiresInMs, \"expiresInMs\");\n if (input.objectPrefix !== undefined) {\n if (\n input.objectPrefix.trim() === \"\" ||\n input.objectPrefix.startsWith(\"/\") ||\n input.objectPrefix.includes(\"\\\\\") ||\n input.objectPrefix.split(\"/\").some((segment) => segment === \".\" || segment === \"..\") ||\n /[\\u0000-\\u001f\\u007f]/.test(input.objectPrefix)\n ) {\n throw new MediaValidationError(\"objectPrefix must be a safe relative prefix.\");\n }\n }\n const expectedChecksum = input.expectedChecksum\n ? normalizeMediaChecksum(input.expectedChecksum)\n : undefined;\n return {\n ...input,\n tenantId: input.tenantId.trim(),\n contentType: input.contentType.toLowerCase(),\n ...(expectedChecksum ? { expectedChecksum } : {}),\n };\n};\n\nexport const validatePositiveInteger = (value: number, name: string): void => {\n if (!Number.isSafeInteger(value) || value < 1) {\n throw new MediaValidationError(`${name} must be a positive safe integer.`);\n }\n};\n\nexport const validateExpiry = (\n value: number,\n name: string,\n max: number,\n): void => {\n validatePositiveInteger(value, name);\n if (value > max) {\n throw new MediaValidationError(`${name} exceeds the configured limit.`);\n }\n};\n\nexport const createTenantObjectKey = (\n tenantId: string,\n uploadId: string,\n fileName: string,\n prefix?: string,\n): string => {\n const safeName = fileName.replace(/[^\\w.\\-]+/g, \"_\").replace(/^\\.*/, \"\") || \"file\";\n const safePrefix = prefix ? `${prefix.replace(/\\/+$/g, \"\")}/` : \"\";\n return `${safePrefix}tenants/${encodeURIComponent(tenantId)}/${uploadId}/${safeName}`;\n};\n\nexport const normalizeReference = (object: MediaObjectReference): MediaObjectReference =>\n normalizeMediaObjectReference(object);\n\nexport const sha256 = async (bytes: Uint8Array): Promise<MediaChecksum> => {\n if (!globalThis.crypto?.subtle) {\n throw new CloudflareMediaValidationError(\"Web Crypto is required for checksum verification.\");\n }\n const digest = await globalThis.crypto.subtle.digest(\n \"SHA-256\",\n new Uint8Array(bytes),\n );\n return {\n algorithm: \"sha256\",\n value: [...new Uint8Array(digest)]\n .map((byte) => byte.toString(16).padStart(2, \"0\"))\n .join(\"\"),\n };\n};\n\nexport const checksumEquals = (\n left: MediaChecksum,\n right: MediaChecksum,\n): boolean => normalizeMediaChecksum(left).value === normalizeMediaChecksum(right).value;\n\nexport const cursorForObject = (key: string): string =>\n encodeURIComponent(key);\n"],"mappings":";;;AAYA,MAAa,oBAAoB,aAA6B;CAC5D,IAAI,OAAO,aAAa,YAAY,SAAS,KAAK,MAAM,IACtD,MAAM,IAAI,qBAAqB,sCAAsC;CAEvE,IAAI,SAAS,SAAS,OAAO,wBAAwB,KAAK,QAAQ,GAChE,MAAM,IAAI,qBAAqB,2CAA2C;CAE5E,OAAO;AACT;AAEA,MAAa,6BACX,UAC2B;CAC3B,iBAAiB,MAAM,QAAQ;CAC/B,IAAI,OAAO,MAAM,aAAa,YAAY,MAAM,SAAS,KAAK,MAAM,IAClE,MAAM,IAAI,qBAAqB,sCAAsC;CAEvE,IAAI,MAAM,SAAS,SAAS,OAAO,wBAAwB,KAAK,MAAM,QAAQ,GAC5E,MAAM,IAAI,qBAAqB,2CAA2C;CAE5E,IACE,OAAO,MAAM,gBAAgB,YAC7B,CAAC,4CAA4C,KAAK,MAAM,WAAW,GAEnE,MAAM,IAAI,qBAAqB,wCAAwC;CAEzE,IACE,MAAM,iBAAiB,KAAA,MACtB,CAAC,OAAO,cAAc,MAAM,YAAY,KAAK,MAAM,eAAe,IAEnE,MAAM,IAAI,qBAAqB,+CAA+C;CAEhF,wBAAwB,MAAM,aAAa,aAAa;CACxD,IAAI,MAAM,iBAAiB,KAAA,GAEvB;MAAA,MAAM,aAAa,KAAK,MAAM,MAC9B,MAAM,aAAa,WAAW,GAAG,KACjC,MAAM,aAAa,SAAS,IAAI,KAChC,MAAM,aAAa,MAAM,GAAG,CAAC,CAAC,MAAM,YAAY,YAAY,OAAO,YAAY,IAAI,KACnF,wBAAwB,KAAK,MAAM,YAAY,GAE/C,MAAM,IAAI,qBAAqB,8CAA8C;CAAA;CAGjF,MAAM,mBAAmB,MAAM,mBAC3B,uBAAuB,MAAM,gBAAgB,IAC7C,KAAA;CACJ,OAAO;EACL,GAAG;EACH,UAAU,MA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le-ui/media-cloudflare",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.17.0",
|
|
4
|
+
"description": "Cloudflare R2 and Images adapters for Puzzle media contracts.",
|
|
5
|
+
"homepage": "https://puzzle-ui.com/docs/media-cloudflare",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"puzzle-ui",
|
|
8
|
+
"cloudflare",
|
|
9
|
+
"r2",
|
|
10
|
+
"images",
|
|
11
|
+
"media",
|
|
12
|
+
"object-storage"
|
|
13
|
+
],
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "git+https://github.com/senorprogramador/puzzle.git"
|
|
17
|
+
},
|
|
18
|
+
"license": "MIT",
|
|
19
|
+
"publishConfig": {
|
|
20
|
+
"access": "public",
|
|
21
|
+
"registry": "https://registry.npmjs.org/"
|
|
22
|
+
},
|
|
23
|
+
"type": "module",
|
|
24
|
+
"main": "./dist/index.cjs",
|
|
25
|
+
"module": "./dist/index.js",
|
|
26
|
+
"types": "./dist/index.d.ts",
|
|
27
|
+
"exports": {
|
|
28
|
+
".": {
|
|
29
|
+
"import": {
|
|
30
|
+
"types": "./dist/index.d.ts",
|
|
31
|
+
"default": "./dist/index.js"
|
|
32
|
+
},
|
|
33
|
+
"require": {
|
|
34
|
+
"types": "./dist/index.d.cts",
|
|
35
|
+
"default": "./dist/index.cjs"
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
"files": [
|
|
40
|
+
"dist",
|
|
41
|
+
"ai"
|
|
42
|
+
],
|
|
43
|
+
"sideEffects": false,
|
|
44
|
+
"scripts": {
|
|
45
|
+
"build": "tsdown",
|
|
46
|
+
"typecheck": "tsc --noEmit -p .",
|
|
47
|
+
"test": "bun test tests --timeout 30000",
|
|
48
|
+
"vitest": "bun test tests --timeout 30000",
|
|
49
|
+
"clean": "rm -rf dist",
|
|
50
|
+
"prepack": "tsdown"
|
|
51
|
+
},
|
|
52
|
+
"devDependencies": {
|
|
53
|
+
"@types/bun": "^1.4.4",
|
|
54
|
+
"bun": "^1.4.4",
|
|
55
|
+
"tsdown": "^0.23.1",
|
|
56
|
+
"typescript": "^7.0.2"
|
|
57
|
+
},
|
|
58
|
+
"dependencies": {
|
|
59
|
+
"@puzzle-ui/media": "^0.17.0"
|
|
60
|
+
}
|
|
61
|
+
}
|