"use strict";var t=this&&this.__awaiter||function(t,e,r,o){return new(r||(r=Promise))((function(i,a){function n(t){try{u(o.next(t))}catch(t){a(t)}}function s(t){try{u(o.throw(t))}catch(t){a(t)}}function u(t){var e;t.done?i(t.value):(e=t.value,e instanceof r?e:new r((function(t){t(e)}))).then(n,s)}u((o=o.apply(t,e||[])).next())}))},e=this&&this.__importDefault||function(t){return t&&t.__esModule?t:{default:t}};Object.defineProperty(exports,"__esModule",{value:!0}),exports.StreamUploader=void 0;const r=require("../../utils/genPublicationFormData"),o=e(require("node-fetch")),i=e(require("path")),a=require("../../../registry"),n=require("../../../proxy");exports.StreamUploader=class{uploadPackage(e,r,o){return t(this,void 0,void 0,(function*(){const t=yield e.getAuthorization(o);return this.publishPkgViaStream(o,t,r)}))}publishPkgViaStream(e,s,u){return t(this,void 0,void 0,(function*(){const c=yield(0,r.genPublicationFormData)(u),d=u.name.replace("/","%2f");return(0,o.default)(e.concat("stream").concat(i.default.sep).concat(d),{headers:Object.assign({command:"publish",version:"v1","user-agent":(0,a.getUserAgent)(),authorization:s},c.getHeaders()),body:c,method:"POST",agent:(0,n.getProxyAgent)(e),timeout:(0,a.getFetchTimeout)()}).then((e=>t(this,void 0,void 0,(function*(){if(r.publicationStream&&r.publicationStream.close(),e.ok)return e.json();if(404===e.status)throw Object.assign(new Error(`HttpCode ${e.status}, ${e.statusText}`),{code:e.status});let t={};try{t=yield e.json()}catch(t){throw Object.assign(new Error(`${e.statusText}`),{code:e.status})}throw Object.assign(new Error(`HttpCode ${e.status}, ${t.error}`),{code:e.status})})))).catch((t=>{throw r.publicationStream&&r.publicationStream.close(),t}))}))}};