11// @ts -check
2- import axios from 'axios'
3- import https from 'node:https'
42import fs from 'node:fs/promises'
53
6- const axiosInstance = axios . create ( {
7- httpsAgent : new https . Agent ( {
8- rejectUnauthorized : false ,
9- } ) ,
10- } )
11-
124/**
135 * @returns {import('rollup').OutputPlugin }
146 */
157export const uploadPlugin = ( server , bundleId , development ) => {
168 return {
179 name : 'upload' ,
18- writeBundle ( options , bundle ) {
10+ async writeBundle ( options , bundle ) {
1911 // console.log(options, bundle)
2012 // console.log('Writing bundle', this.getModuleIds())
2113
@@ -28,22 +20,24 @@ export const uploadPlugin = (server, bundleId, development) => {
2820 } else if ( item . type === 'chunk' ) {
2921 if ( ! item . isEntry ) throw new Error ( `Expected a single code chunk: ${ id } ` )
3022
31- axiosInstance
32- . post (
23+ try {
24+ const res = await fetch (
3325 server + '/api/private/blueprints/restore/' + bundleId + ( development ? '?developmentMode=1' : '' ) ,
34- item . code ,
3526 {
27+ method : 'POST' ,
28+ body : item . code ,
3629 headers : {
3730 'Content-Type' : 'text/javascript' ,
3831 } ,
3932 }
4033 )
41- . then ( ( ) => {
42- console . log ( `Blueprints '${ id } ' uploaded` )
43- } )
44- . catch ( ( e ) => {
45- console . error ( `Blueprints '${ id } ' upload failed:` , e . toString ( ) , e . stack )
46- } )
34+ if ( ! res . ok ) {
35+ throw new Error ( `HTTP ${ res . status } : ${ await res . text ( ) } ` )
36+ }
37+ console . log ( `Blueprints '${ id } ' uploaded` )
38+ } catch ( e ) {
39+ console . error ( `Blueprints '${ id } ' upload failed:` , e . toString ( ) , e . stack )
40+ }
4741 } else {
4842 // @ts -expect-error
4943 throw new Error ( `Unknown bundle type ${ item . type } ` )
@@ -54,18 +48,21 @@ export const uploadPlugin = (server, bundleId, development) => {
5448 fs . writeFile ( `dist/${ bundleId } -assets.json` , JSON . stringify ( assetsBundle ) ) . catch ( ( e ) => {
5549 console . error ( `Failed to write assets bundle to disk:` , e . toString ( ) , e . stack )
5650 } )
57- axiosInstance
58- . post ( server + '/api/private/blueprints/assets' , JSON . stringify ( assetsBundle ) , {
51+ try {
52+ const res = await fetch ( server + '/api/private/blueprints/assets' , {
53+ method : 'POST' ,
54+ body : JSON . stringify ( assetsBundle ) ,
5955 headers : {
6056 'Content-Type' : 'application/json' ,
6157 } ,
6258 } )
63- . then ( ( ) => {
64- console . log ( `Blueprints assets uploaded` )
65- } )
66- . catch ( ( e ) => {
67- console . error ( `Blueprints assets upload failed:` , e . toString ( ) , e . stack )
68- } )
59+ if ( ! res . ok ) {
60+ throw new Error ( `HTTP ${ res . status } : ${ await res . text ( ) } ` )
61+ }
62+ console . log ( `Blueprints assets uploaded` )
63+ } catch ( e ) {
64+ console . error ( `Blueprints assets upload failed:` , e . toString ( ) , e . stack )
65+ }
6966 } else {
7067 fs . rm ( `dist/${ bundleId } -assets.json` ) . catch ( ( ) => {
7168 // Ignore, likely doesn't exist
0 commit comments