previewer-agency 0.2.0 → 0.2.2
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/bin/previewer.mjs +29 -19
- package/lib/api.mjs +17 -8
- package/lib/files.mjs +25 -3
- package/lib/menu.mjs +30 -2
- package/package.json +1 -1
package/bin/previewer.mjs
CHANGED
|
@@ -31,19 +31,29 @@ const fail = (message, hint) => {
|
|
|
31
31
|
process.exit(1);
|
|
32
32
|
};
|
|
33
33
|
|
|
34
|
+
// How this person runs us, so help and hints show a command that works when typed: through npx
|
|
35
|
+
// (no global install, so `previewer` itself isn't on their PATH) it's `npx previewer-agency`.
|
|
36
|
+
const RAN_VIA_NPX = process.env.npm_command === 'exec' || /[\\/]_npx[\\/]/.test(process.argv[1] || '');
|
|
37
|
+
const CMD = RAN_VIA_NPX ? 'npx previewer-agency' : 'previewer';
|
|
38
|
+
|
|
39
|
+
const COMMAND_HELP = [
|
|
40
|
+
['login', 'Sign in (opens the browser to approve)'],
|
|
41
|
+
['push [folder]', 'Upload a folder: ZIPs, banner folders, images, videos, backups'],
|
|
42
|
+
['status', "Approvals and views of this folder's campaign"],
|
|
43
|
+
['open', "Open the campaign's preview link"],
|
|
44
|
+
['link', 'Choose which campaign this folder publishes to'],
|
|
45
|
+
['campaigns', 'List campaigns'],
|
|
46
|
+
['whoami', "Show who you're signed in as"],
|
|
47
|
+
['logout', "Sign out (revokes this computer's token)"],
|
|
48
|
+
['mcp', 'Run as an MCP server, for AI agents (Claude Code, Cursor, …)'],
|
|
49
|
+
];
|
|
50
|
+
const commandWidth = Math.max(...COMMAND_HELP.map(([command]) => `${CMD} ${command}`.length)) + 3;
|
|
51
|
+
|
|
34
52
|
const HELP = `${bold('previewer')} ${dim(VERSION)} — upload banners to Previewer and check on them
|
|
35
53
|
|
|
36
54
|
${bold('Usage')}
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
previewer status Approvals and views of this folder's campaign
|
|
40
|
-
previewer open Open the campaign's preview link
|
|
41
|
-
previewer link Choose which campaign this folder publishes to
|
|
42
|
-
previewer campaigns List campaigns
|
|
43
|
-
previewer whoami Show who you're signed in as
|
|
44
|
-
previewer logout Sign out (revokes this computer's token)
|
|
45
|
-
previewer mcp Run as an MCP server, for AI agents (Claude Code, Cursor, …)
|
|
46
|
-
|
|
55
|
+
${COMMAND_HELP.map(([command, text]) => ` ${`${CMD} ${command}`.padEnd(commandWidth)}${text}`).join('\n')}
|
|
56
|
+
${RAN_VIA_NPX ? `\n ${dim('Tip: npm install -g previewer-agency, then type just "previewer".')}\n` : ''}
|
|
47
57
|
${bold('Options')}
|
|
48
58
|
--client <name|id> Client to publish to (push, link, campaigns)
|
|
49
59
|
--campaign <name|id> Campaign to publish to (push, link, status, open)
|
|
@@ -55,12 +65,12 @@ ${bold('Options')}
|
|
|
55
65
|
|
|
56
66
|
${bold('In CI')}
|
|
57
67
|
Set PREVIEWER_TOKEN (create one under API in the Previewer admin), then:
|
|
58
|
-
previewer push dist --campaign 42
|
|
68
|
+
npx -y previewer-agency push dist --campaign 42
|
|
59
69
|
|
|
60
70
|
${bold('Folders')}
|
|
61
71
|
*.zip and folders with an index.html are HTML5 banners. An image with the same name as a banner
|
|
62
72
|
(300x250.zip + 300x250.jpg), or in a backups/ folder, is its backup image. Other images and videos
|
|
63
|
-
are creatives. The first push saves the campaign to ${PROJECT_FILE}; after that, just run push.`;
|
|
73
|
+
are creatives. The first push saves the campaign to ${PROJECT_FILE}; after that, just run ${CMD} push.`;
|
|
64
74
|
|
|
65
75
|
// --- small helpers -------------------------------------------------------------------------------
|
|
66
76
|
|
|
@@ -141,7 +151,7 @@ const matchName = (items, wanted, key = 'name') => {
|
|
|
141
151
|
|
|
142
152
|
const clientFor = async (options, project) => {
|
|
143
153
|
const auth = await resolveAuth({ site: options.site, project });
|
|
144
|
-
if (!auth.token) fail(`Not signed in to ${auth.site}.`,
|
|
154
|
+
if (!auth.token) fail(`Not signed in to ${auth.site}.`, `Run: ${CMD} login`);
|
|
145
155
|
return { client: new PreviewerClient({ baseUrl: auth.site, token: auth.token, userAgent: `previewer-cli/${VERSION}` }), auth };
|
|
146
156
|
};
|
|
147
157
|
|
|
@@ -151,9 +161,9 @@ const explain = (error) => {
|
|
|
151
161
|
error.code === 'auth'
|
|
152
162
|
? process.env.PREVIEWER_TOKEN
|
|
153
163
|
? 'Check PREVIEWER_TOKEN.'
|
|
154
|
-
:
|
|
164
|
+
: `Run: ${CMD} login`
|
|
155
165
|
: error.code === 'forbidden'
|
|
156
|
-
?
|
|
166
|
+
? `Your token can't do this. Ask an owner, or sign in again with ${CMD} login.`
|
|
157
167
|
: error.code === 'not_found'
|
|
158
168
|
? 'Check the id, or that your token can reach it.'
|
|
159
169
|
: undefined;
|
|
@@ -186,7 +196,7 @@ async function login(options) {
|
|
|
186
196
|
if (status === 'denied') fail('The login was denied.');
|
|
187
197
|
if (status === 'expired') break;
|
|
188
198
|
}
|
|
189
|
-
fail('The login expired before it was approved.',
|
|
199
|
+
fail('The login expired before it was approved.', `Run ${CMD} login again.`);
|
|
190
200
|
}
|
|
191
201
|
|
|
192
202
|
async function logout(options) {
|
|
@@ -273,7 +283,7 @@ const pickCampaign = async (api, options, { allowCreate }) => {
|
|
|
273
283
|
const rememberCampaign = async (site, campaign, project) => {
|
|
274
284
|
if (project?.campaignId === campaign.id && project?.site === site) return;
|
|
275
285
|
const file = await saveProject(process.cwd(), { site, campaignId: campaign.id, clientId: campaign.client.id, client: campaign.client.name, campaign: campaign.name });
|
|
276
|
-
say(dim(`Saved to ${path.relative(process.cwd(), file) || file}: next time just run
|
|
286
|
+
say(dim(`Saved to ${path.relative(process.cwd(), file) || file}: next time just run ${CMD} push.`));
|
|
277
287
|
};
|
|
278
288
|
|
|
279
289
|
async function link(options) {
|
|
@@ -409,14 +419,14 @@ const main = async () => {
|
|
|
409
419
|
},
|
|
410
420
|
});
|
|
411
421
|
} catch (error) {
|
|
412
|
-
fail(error.message,
|
|
422
|
+
fail(error.message, `Run ${CMD} help.`);
|
|
413
423
|
}
|
|
414
424
|
const { values, positionals } = parsed;
|
|
415
425
|
if (values.version) return say(VERSION);
|
|
416
426
|
const [command, ...rest] = positionals;
|
|
417
427
|
if (!command || values.help || command === 'help') return say(HELP);
|
|
418
428
|
const run = COMMANDS[command];
|
|
419
|
-
if (!run) fail(`Unknown command "${command}".`,
|
|
429
|
+
if (!run) fail(`Unknown command "${command}".`, `Run ${CMD} help.`);
|
|
420
430
|
try {
|
|
421
431
|
await run(values, ...rest);
|
|
422
432
|
} catch (error) {
|
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
|
-
/**
|
|
30
|
-
|
|
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
|
|
139
|
-
const
|
|
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 =
|
|
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
|
-
|
|
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
|
-
|
|
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
|
|
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
|
-
|
|
49
|
-
|
|
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