pkg-pr-new 0.0.25 → 0.0.27

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.
Files changed (3) hide show
  1. package/dist/index.js +78 -2
  2. package/index.ts +91 -0
  3. package/package.json +1 -1
package/index.ts CHANGED
@@ -40,6 +40,9 @@ type OutputMetadata = {
40
40
 
41
41
  const apiUrl = process.env.API_URL ?? API_URL;
42
42
  const publishUrl = new URL("/publish", apiUrl);
43
+ const createMultipart = new URL("/multipart/create", apiUrl);
44
+ const uploadMultipart = new URL("/multipart/upload", apiUrl);
45
+ const completeMultipart = new URL("/multipart/complete", apiUrl);
43
46
 
44
47
  const main = defineCommand({
45
48
  meta: {
@@ -360,6 +363,87 @@ const main = defineCommand({
360
363
  }
361
364
  }
362
365
 
366
+ const formDataPackagesSize = [...formData.entries()].reduce(
367
+ (prev, [_, entry]) => prev + getFormEntrySize(entry),
368
+ 0,
369
+ );
370
+
371
+ // multipart uploading
372
+ if (formDataPackagesSize > 1024 * 1024 * 99) {
373
+ for (const [name, entry] of [...formData]) {
374
+ if (name.startsWith("package:")) {
375
+ const file = entry as File;
376
+ const chunkSize = 1024 * 1024 * 5;
377
+ if (file.size <= chunkSize) {
378
+ continue;
379
+ }
380
+ const totalChunks = Math.ceil(file.size / chunkSize);
381
+ const createMultipartRes = await fetch(createMultipart, {
382
+ method: "POST",
383
+ headers: {
384
+ "sb-key": key,
385
+ "sb-name": name.slice("package:".length),
386
+ },
387
+ });
388
+ if (!createMultipartRes.ok) {
389
+ console.error(await createMultipartRes.text());
390
+ continue;
391
+ }
392
+ const { key: uploadKey, id: uploadId, ...data } =
393
+ await createMultipartRes.json();
394
+
395
+ interface R2UploadedPart {
396
+ partNumber: number;
397
+ etag: string;
398
+ }
399
+ const uploadedParts: R2UploadedPart[] = [];
400
+
401
+ for (let i = 0; i < totalChunks; i++) {
402
+ const start = i * chunkSize;
403
+ const end = Math.min(file.size, start + chunkSize);
404
+ const chunk = file.slice(start, end);
405
+
406
+ const uploadMultipartRes = await fetch(uploadMultipart, {
407
+ method: "PUT",
408
+ headers: {
409
+ key: uploadKey,
410
+ id: uploadId,
411
+ "part-number": `${i + 1}`,
412
+ },
413
+ body: chunk,
414
+ });
415
+
416
+ if (!uploadMultipartRes.ok) {
417
+ console.error(
418
+ `Error uploading part ${i + 1}: ${await uploadMultipartRes.text()}`,
419
+ );
420
+ break;
421
+ }
422
+ const { part } = await uploadMultipartRes.json();
423
+ uploadedParts.push(part);
424
+ }
425
+ const completeMultipartRes = await fetch(completeMultipart, {
426
+ method: "POST",
427
+ headers: {
428
+ key: uploadKey,
429
+ id: uploadId,
430
+ "uploaded-parts": JSON.stringify(uploadedParts),
431
+ },
432
+ });
433
+ if (!completeMultipartRes.ok) {
434
+ console.error(
435
+ `Error completing ${key}: ${await completeMultipartRes.text()}`,
436
+ );
437
+ break;
438
+ }
439
+ const { key: completionKey } =
440
+ await completeMultipartRes.json();
441
+
442
+ formData.set(name, `object:${completionKey}`);
443
+ }
444
+ }
445
+ }
446
+
363
447
  const packageManager = await detect();
364
448
  const res = await fetch(publishUrl, {
365
449
  method: "POST",
@@ -469,6 +553,13 @@ function hijackDeps(
469
553
  }
470
554
  }
471
555
 
556
+ function getFormEntrySize(entry: FormDataEntryValue) {
557
+ if (typeof entry === 'string') {
558
+ return entry.length;
559
+ }
560
+ return entry.size;
561
+ }
562
+
472
563
  async function verifyCompactMode(packageName: string) {
473
564
  let manifest: PackageManifest;
474
565
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pkg-pr-new",
3
- "version": "0.0.25",
3
+ "version": "0.0.27",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "type": "module",