@twick/cloud-caption-video 0.15.15 → 0.15.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.
@@ -0,0 +1,73 @@
1
+ /**
2
+ * Sanitizes a project identifier for use in object keys and URLs.
3
+ * Replaces special characters with hyphens, lowercases, and trims.
4
+ *
5
+ * @param {string|null|undefined} value - Raw identifier
6
+ * @returns {string} Sanitized identifier, or 'twick-project' if value is missing
7
+ */
8
+ export function sanitizeIdentifier(value) {
9
+ if (value === undefined || value === null) {
10
+ return 'twick-project';
11
+ }
12
+ const str = String(value).trim();
13
+ if (!str) return 'twick-project';
14
+ const normalized = str
15
+ .toLowerCase()
16
+ .replace(/[^a-z0-9]+/g, '-')
17
+ .replace(/^-+|-+$/g, '');
18
+ return normalized || 'twick-project';
19
+ }
20
+
21
+ /**
22
+ * Builds an S3 object key for a project JSON file.
23
+ *
24
+ * @param {Object} projectData - Project object (may have project.properties.id, properties.id, or id)
25
+ * @param {number|string} [uniqueSuffix] - Optional suffix (e.g. year or timestamp)
26
+ * @returns {string} Object key like "project-id-2025.json"
27
+ */
28
+ export function buildObjectKey(projectData, uniqueSuffix) {
29
+ const identifier =
30
+ projectData?.project?.properties?.id ??
31
+ projectData?.properties?.id ??
32
+ projectData?.id;
33
+ const baseName = sanitizeIdentifier(identifier);
34
+ const suffix = uniqueSuffix ?? Date.now();
35
+ return `${baseName}-${suffix}.json`;
36
+ }
37
+
38
+ /**
39
+ * Encodes S3 key segments for URL usage.
40
+ * @param {string} key - Object key (may contain path segments)
41
+ * @returns {string} Encoded key
42
+ */
43
+ function encodeS3Key(key) {
44
+ return key
45
+ .split('/')
46
+ .map((segment) => encodeURIComponent(segment))
47
+ .join('/');
48
+ }
49
+
50
+ /**
51
+ * Builds a public URL for an S3 object.
52
+ *
53
+ * @param {Object} params - URL parameters
54
+ * @param {string} params.bucket - S3 bucket name
55
+ * @param {string} params.key - Object key
56
+ * @param {string} [params.region] - AWS region (e.g. us-east-1, us-west-2)
57
+ * @param {string} [params.baseUrl] - Custom base URL (trims trailing slash)
58
+ * @returns {string} Public URL to the object
59
+ */
60
+ export function buildPublicUrl({ bucket, key, region, baseUrl }) {
61
+ if (baseUrl) {
62
+ const trimmedBase = baseUrl.endsWith('/') ? baseUrl.slice(0, -1) : baseUrl;
63
+ return `${trimmedBase}/${encodeS3Key(key)}`;
64
+ }
65
+
66
+ const encodedKey = encodeS3Key(key);
67
+
68
+ if (!region || region === 'us-east-1') {
69
+ return `https://${bucket}.s3.amazonaws.com/${encodedKey}`;
70
+ }
71
+
72
+ return `https://${bucket}.s3.${region}.amazonaws.com/${encodedKey}`;
73
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@twick/cloud-caption-video",
3
- "version": "0.15.15",
3
+ "version": "0.15.18",
4
4
  "description": "Twick cloud function for generating caption video from audio using Google Cloud Speech-to-Text",
5
5
  "type": "module",
6
6
  "main": "core/index.js",