previewer-agency 0.2.0 → 0.2.1

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/lib/api.mjs CHANGED
@@ -17,6 +17,11 @@ export class PreviewerError extends Error {
17
17
  // 3.5 MB is uploaded straight to storage first (upload-urls) and then registered.
18
18
  const MAX_BATCH_BYTES = 4 * 1024 * 1024;
19
19
  const DIRECT_ABOVE_BYTES = 3.5 * 1024 * 1024;
20
+ // The server takes at most 80 files per request (multipart and upload-urls alike).
21
+ const MAX_BATCH_FILES = 80;
22
+ // Big files are read back and unpacked by the server in the request that registers them, so those
23
+ // go a few at a time to stay well inside its memory and time limits.
24
+ const MAX_DIRECT_PER_REQUEST = 5;
20
25
 
21
26
  const codeForStatus = (status) =>
22
27
  status === 401 ? 'auth' : status === 403 ? 'forbidden' : status === 404 ? 'not_found' : status === 409 ? 'conflict' : status === 429 ? 'rate_limit' : 'http';
@@ -26,13 +31,16 @@ export const newCliToken = () => {
26
31
  return { token, hash: createHash('sha256').update(token).digest('hex'), hint: token.slice(-4) };
27
32
  };
28
33
 
29
- /** Greedily pack `{ size }` items into batches under `limit` bytes (an oversize item goes alone). */
30
- export const packBatches = (items, limit = MAX_BATCH_BYTES) => {
34
+ /**
35
+ * Greedily pack `{ size }` items into batches under `limit` bytes and at most `maxCount` items
36
+ * (an oversize item goes alone).
37
+ */
38
+ export const packBatches = (items, limit = MAX_BATCH_BYTES, maxCount = MAX_BATCH_FILES) => {
31
39
  const batches = [];
32
40
  let current = [];
33
41
  let bytes = 0;
34
42
  for (const item of items) {
35
- if (current.length > 0 && bytes + item.size > limit) {
43
+ if (current.length > 0 && (bytes + item.size > limit || current.length >= maxCount)) {
36
44
  batches.push(current);
37
45
  current = [];
38
46
  bytes = 0;
@@ -135,14 +143,15 @@ export class PreviewerClient {
135
143
  const banners = items.filter((item) => item.type === 'banner');
136
144
  const media = items.filter((item) => item.type === 'media');
137
145
  const backups = items.filter((item) => item.type === 'backup');
138
- const bigZips = banners.filter((item) => item.size > DIRECT_ABOVE_BYTES);
139
- const bigMedia = media.filter((item) => item.size > DIRECT_ABOVE_BYTES);
146
+ const groupsOf = (list) => packBatches(list, Infinity, MAX_DIRECT_PER_REQUEST);
147
+ const bigZipGroups = groupsOf(banners.filter((item) => item.size > DIRECT_ABOVE_BYTES));
148
+ const bigMediaGroups = groupsOf(media.filter((item) => item.size > DIRECT_ABOVE_BYTES));
140
149
  const smallBatches = packBatches([
141
150
  ...banners.filter((item) => item.size <= DIRECT_ABOVE_BYTES),
142
151
  ...media.filter((item) => item.size <= DIRECT_ABOVE_BYTES),
143
152
  ]);
144
153
  const backupBatches = packBatches(backups);
145
- const total = (bigZips.length ? 1 : 0) + (bigMedia.length ? 1 : 0) + smallBatches.length + backupBatches.length;
154
+ const total = bigZipGroups.length + bigMediaGroups.length + smallBatches.length + backupBatches.length;
146
155
 
147
156
  const uploaded = [];
148
157
  let campaign = null;
@@ -157,12 +166,12 @@ export class PreviewerClient {
157
166
  onProgress?.(done, total);
158
167
  };
159
168
 
160
- if (bigZips.length) {
169
+ for (const bigZips of bigZipGroups) {
161
170
  const targets = await this.signUploads(campaignId, 'zip', bigZips);
162
171
  for (const [index, item] of bigZips.entries()) await putToStorage(targets, targets.uploads[index], await read(item), 'application/zip');
163
172
  await send((form) => form.append('stagedZips', JSON.stringify(targets.uploads.map((target) => ({ name: target.name, key: target.key })))));
164
173
  }
165
- if (bigMedia.length) {
174
+ for (const bigMedia of bigMediaGroups) {
166
175
  const targets = await this.signUploads(campaignId, 'media', bigMedia);
167
176
  for (const [index, item] of bigMedia.entries()) await putToStorage(targets, targets.uploads[index], await read(item), item.mimeType);
168
177
  await send((form) =>
package/lib/files.mjs CHANGED
@@ -6,7 +6,7 @@
6
6
  // - any other image or video → an image/video creative
7
7
  // Sizes come from the file itself (images, MP4) or a 300x250-style token in the name.
8
8
 
9
- import { readdir, readFile, stat } from 'node:fs/promises';
9
+ import { open, readdir, readFile, stat } from 'node:fs/promises';
10
10
  import path from 'node:path';
11
11
  import { deflateRawSync } from 'node:zlib';
12
12
 
@@ -83,6 +83,29 @@ export const mp4Size = (buffer) => {
83
83
  }
84
84
  };
85
85
 
86
+ // The tkhd box sits in `moov`, which encoders put at the start (web-optimised) or the end. Read
87
+ // those ends instead of the whole video, which can be hundreds of MB; only a file where neither
88
+ // holds it is read in full.
89
+ const MP4_PROBE_BYTES = 4 * 1024 * 1024;
90
+ const readRange = async (file, start, length) => {
91
+ const handle = await open(file, 'r');
92
+ try {
93
+ const buffer = Buffer.alloc(length);
94
+ const { bytesRead } = await handle.read(buffer, 0, length, start);
95
+ return buffer.subarray(0, bytesRead);
96
+ } finally {
97
+ await handle.close();
98
+ }
99
+ };
100
+ export const mp4SizeFromFile = async (file, fileSize) => {
101
+ if (fileSize <= MP4_PROBE_BYTES * 2) return mp4Size(await readFile(file));
102
+ return (
103
+ mp4Size(await readRange(file, 0, MP4_PROBE_BYTES)) ||
104
+ mp4Size(await readRange(file, fileSize - MP4_PROBE_BYTES, MP4_PROBE_BYTES)) ||
105
+ mp4Size(await readFile(file))
106
+ );
107
+ };
108
+
86
109
  // --- zip a banner folder ------------------------------------------------------------------------
87
110
 
88
111
  const CRC_TABLE = Array.from({ length: 256 }, (_, n) => {
@@ -201,8 +224,7 @@ export const collectUploads = async (dir) => {
201
224
  } else if (IMAGE_RE.test(entry.name)) {
202
225
  images.push({ full, name: entry.name, forceBackup: false });
203
226
  } else if (VIDEO_RE.test(entry.name)) {
204
- const head = await readFile(full);
205
- const size = (/\.mp4$/i.test(entry.name) ? mp4Size(head) : null) || sizeFromName(entry.name);
227
+ const size = (/\.mp4$/i.test(entry.name) ? await mp4SizeFromFile(full, info.size) : null) || sizeFromName(entry.name);
206
228
  if (!size) {
207
229
  skipped.push({ name: entry.name, reason: 'video size unknown: put it in the name, e.g. intro-1080x1920.webm' });
208
230
  continue;
package/lib/menu.mjs CHANGED
@@ -44,9 +44,37 @@ export const menuKey = (state, options, str, key = {}) => {
44
44
  }
45
45
  };
46
46
 
47
+ // Terminal columns a character takes: 0 for combining marks and joiners, 2 for wide East Asian
48
+ // characters and emoji, 1 otherwise. Close enough for names; what matters is that a line never wraps,
49
+ // since the redraw moves the cursor up by line count.
50
+ const charWidth = (char) => {
51
+ if (/[\p{M}​-‏️]/u.test(char)) return 0;
52
+ if (/\p{Extended_Pictographic}/u.test(char)) return 2;
53
+ const code = char.codePointAt(0);
54
+ return (code >= 0x1100 && code <= 0x115f) ||
55
+ (code >= 0x2e80 && code <= 0xa4cf) ||
56
+ (code >= 0xac00 && code <= 0xd7a3) ||
57
+ (code >= 0xf900 && code <= 0xfaff) ||
58
+ (code >= 0xfe30 && code <= 0xfe4f) ||
59
+ (code >= 0xff00 && code <= 0xff60) ||
60
+ (code >= 0xffe0 && code <= 0xffe6) ||
61
+ (code >= 0x20000 && code <= 0x3fffd)
62
+ ? 2
63
+ : 1;
64
+ };
65
+ export const displayWidth = (text) => [...plain(text)].reduce((sum, char) => sum + charWidth(char), 0);
66
+
47
67
  const fit = (line, columns) => {
48
- const text = plain(line);
49
- return text.length < columns ? line : `${text.slice(0, Math.max(columns - 2, 1))}…`;
68
+ if (displayWidth(line) < columns) return line;
69
+ let out = '';
70
+ let width = 0;
71
+ for (const char of plain(line)) {
72
+ const next = charWidth(char);
73
+ if (width + next > columns - 2) break;
74
+ out += char;
75
+ width += next;
76
+ }
77
+ return `${out}…`;
50
78
  };
51
79
 
52
80
  /** The lines to draw for a state: a hint or the filter, then a window of options around the cursor. */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "previewer-agency",
3
- "version": "0.2.0",
3
+ "version": "0.2.1",
4
4
  "description": "Upload HTML5 banners, images and videos to Previewer from any folder, CI job or AI agent (MCP), and check approvals.",
5
5
  "type": "module",
6
6
  "bin": {