viveworker 0.7.0-beta.0 → 0.7.0-beta.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.
@@ -7,7 +7,7 @@
7
7
  * .html .htm .pdf .png .jpg .jpeg .gif .webp .csv
8
8
  *
9
9
  * Commands:
10
- * viveworker share upload <file> [--password <pw>] [--price <usd> --pay-to <0x…>] [--expires-days <n>] [--json]
10
+ * viveworker share upload <file> [--password <pw>] [--price <usd> --pay-to <0x…>] [--expires-days <n>] [--no-optimize] [--json]
11
11
  * viveworker share list [--json]
12
12
  * viveworker share update <slug> [--password <pw>] [--no-password] [--price <usd>] [--no-price] [--pay-to <0x…>] [--expires-days <n>] [--json]
13
13
  * viveworker share link <slug> --password <pw> [--ttl-hours <n>] [--json]
@@ -77,7 +77,7 @@ export async function runShareCli(args) {
77
77
 
78
78
  function printHelp() {
79
79
  console.log("Commands:");
80
- console.log(" viveworker share upload <file> [--password <pw>] [--price <usd> --pay-to <0x…>] [--expires-days <n>] [--json]");
80
+ console.log(" viveworker share upload <file> [--password <pw>] [--price <usd> --pay-to <0x…>] [--expires-days <n>] [--no-optimize] [--json]");
81
81
  console.log(" viveworker share list [--metrics] [--json]");
82
82
  console.log(" viveworker share update <slug> [--password <pw>] [--no-password] [--price <usd>] [--no-price] [--pay-to <0x…>] [--expires-days <n>] [--json]");
83
83
  console.log(" viveworker share link <slug> --password <pw> [--ttl-hours <n>] [--json]");
@@ -85,9 +85,11 @@ function printHelp() {
85
85
  console.log("");
86
86
  console.log(`Accepted file types: ${ALLOWED_EXTENSIONS.join(" / ")}`);
87
87
  console.log("CSV files are rendered as an HTML table on view; append ?raw=1 for bytes.");
88
+ console.log("HTML uploads are optimized by default when possible (use --no-optimize to disable).");
88
89
  console.log("");
89
90
  console.log("Paid shares (x402 / USDC on Base — CLOSED BETA, testnet only): --price 0.10 --pay-to 0x…");
90
91
  console.log(" Buyers use any x402-compatible client (e.g. `x402-fetch` on npm).");
92
+ console.log(" To use a hazbase wallet as payTo, resolve it first via the local /api/hazbase/payout-address endpoint.");
91
93
  console.log(" --price and --password are mutually exclusive on a single share.");
92
94
  console.log(" `share list --metrics` prints 24h / 7d payment-flow stats for your shares.");
93
95
  console.log("");
@@ -121,9 +123,6 @@ async function handleUpload(args) {
121
123
  if (stat.size === 0) {
122
124
  throw new Error(`File is empty: ${filePath}`);
123
125
  }
124
- if (stat.size > MAX_FILE_SIZE) {
125
- throw new Error(`File too large (${stat.size} bytes, max ${MAX_FILE_SIZE})`);
126
- }
127
126
  const ext = path.extname(absolute).toLowerCase();
128
127
  if (!ALLOWED_EXTENSIONS.includes(ext)) {
129
128
  throw new Error(
@@ -168,7 +167,18 @@ async function handleUpload(args) {
168
167
 
169
168
  const { apiKey, userId, shareUrl } = await resolveCredentials();
170
169
 
171
- const bytes = await fs.readFile(absolute);
170
+ const originalBytes = await fs.readFile(absolute);
171
+ const optimization =
172
+ flags["no-optimize"] || flags["noOptimize"]
173
+ ? { bytes: originalBytes, info: null }
174
+ : maybeOptimizeUpload({ absolute, ext, bytes: originalBytes });
175
+ const bytes = optimization.bytes;
176
+ if (bytes.length > MAX_FILE_SIZE) {
177
+ const detail = optimization.info
178
+ ? ` after optimization to ${formatSize(bytes.length)}`
179
+ : "";
180
+ throw new Error(`File too large (${originalBytes.length} bytes, max ${MAX_FILE_SIZE})${detail}`);
181
+ }
172
182
  const form = new FormData();
173
183
  const blob = new Blob([bytes], { type: mime });
174
184
  const file = new File([blob], path.basename(absolute), { type: mime });
@@ -202,6 +212,15 @@ async function handleUpload(args) {
202
212
  console.log("");
203
213
  console.log(`✅ Uploaded ${body.originalName || path.basename(absolute)} (${formatSize(body.size)})`);
204
214
  console.log("");
215
+ if (optimization.info) {
216
+ console.log(
217
+ ` Optimized HTML: ${formatSize(optimization.info.originalBytes)} → ${formatSize(optimization.info.optimizedBytes)}`
218
+ );
219
+ console.log(
220
+ ` Removed embedded fonts: ${optimization.info.removedFonts}`
221
+ );
222
+ console.log("");
223
+ }
205
224
  console.log(` ${body.url}`);
206
225
  console.log("");
207
226
  if (body.hasPassword) console.log(` 🔒 Password-protected`);
@@ -220,6 +239,97 @@ async function handleUpload(args) {
220
239
  console.log("");
221
240
  }
222
241
 
242
+ function maybeOptimizeUpload({ absolute, ext, bytes }) {
243
+ if (ext !== ".html" && ext !== ".htm") {
244
+ return { bytes, info: null };
245
+ }
246
+
247
+ const source = bytes.toString("utf8");
248
+ const optimized = optimizeBundledStandaloneHtml(source);
249
+ if (!optimized) {
250
+ return { bytes, info: null };
251
+ }
252
+
253
+ const optimizedBytes = Buffer.from(optimized.html, "utf8");
254
+ if (optimizedBytes.length >= bytes.length) {
255
+ return { bytes, info: null };
256
+ }
257
+
258
+ return {
259
+ bytes: optimizedBytes,
260
+ info: {
261
+ kind: "bundled-standalone-html",
262
+ removedFonts: optimized.removedFonts,
263
+ originalBytes: bytes.length,
264
+ optimizedBytes: optimizedBytes.length,
265
+ file: absolute,
266
+ },
267
+ };
268
+ }
269
+
270
+ function optimizeBundledStandaloneHtml(source) {
271
+ const manifestTagPattern = /(<script\b[^>]*type="__bundler\/manifest"[^>]*>)([\s\S]*?)(<\/script>)/i;
272
+ const templateTagPattern = /(<script\b[^>]*type="__bundler\/template"[^>]*>)([\s\S]*?)(<\/script>)/i;
273
+ const manifestMatch = source.match(manifestTagPattern);
274
+ const templateMatch = source.match(templateTagPattern);
275
+ if (!manifestMatch || !templateMatch) {
276
+ return null;
277
+ }
278
+
279
+ let manifest;
280
+ let template;
281
+ try {
282
+ manifest = JSON.parse(manifestMatch[2]);
283
+ template = JSON.parse(templateMatch[2]);
284
+ } catch {
285
+ return null;
286
+ }
287
+
288
+ const fontUuids = Object.entries(manifest)
289
+ .filter(([, entry]) => entry?.mime === "font/woff2")
290
+ .map(([uuid]) => uuid);
291
+ if (fontUuids.length === 0) {
292
+ return null;
293
+ }
294
+
295
+ for (const uuid of fontUuids) {
296
+ delete manifest[uuid];
297
+ const escapedUuid = escapeRegExp(uuid);
298
+ template = template.replace(
299
+ new RegExp(`@font-face\\s*\\{[^{}]*${escapedUuid}[^{}]*\\}`, "g"),
300
+ "",
301
+ );
302
+ template = template.split(uuid).join("");
303
+ }
304
+
305
+ let html = source.replace(
306
+ manifestTagPattern,
307
+ (_, openTag, _json, closeTag) => `${openTag}${stringifyForHtmlScriptTag(manifest)}${closeTag}`,
308
+ );
309
+ html = html.replace(
310
+ templateTagPattern,
311
+ (_, openTag, _json, closeTag) => `${openTag}${stringifyForHtmlScriptTag(template)}${closeTag}`,
312
+ );
313
+
314
+ return {
315
+ html,
316
+ removedFonts: fontUuids.length,
317
+ };
318
+ }
319
+
320
+ function escapeRegExp(value) {
321
+ return String(value).replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
322
+ }
323
+
324
+ function stringifyForHtmlScriptTag(value) {
325
+ return JSON.stringify(value)
326
+ .replace(/</g, "\\u003C")
327
+ .replace(/>/g, "\\u003E")
328
+ .replace(/&/g, "\\u0026")
329
+ .replace(/\u2028/g, "\\u2028")
330
+ .replace(/\u2029/g, "\\u2029");
331
+ }
332
+
223
333
  // ---------------------------------------------------------------------------
224
334
  // list
225
335
  // ---------------------------------------------------------------------------