Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use native fetch, remove request, update minimist #509

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion lib/middleware/auth.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
var request = require("request")
var localCreds = require("./util/creds.js")
var helpers = require("./util/helpers.js")
var os = require('os')
Expand Down
127 changes: 63 additions & 64 deletions lib/middleware/deploy.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
var fs = require("fs")
var request = require("request")
var helpers = require('./util/helpers')
var localCreds = require("./util/creds.js")
var tar = require('tarr')
Expand Down Expand Up @@ -124,29 +123,29 @@ module.exports = function(req, next){
helpers.payment(req, payload["stripe_pk"], payload.card)(function(token){

// can this be passed in?
var uri = url.resolve(req.endpoint, "subscription")
request({
uri: uri,
method: "PUT",
auth: {
'user': 'token',
'pass': req.creds.token,
'sendImmediately': true
var uri = new URL('subscription', req.endpoint)
var auth = Buffer.from(`token:${req.creds.token}`).toString('base64')
var body = new FormData()
body.set('plan', payload.plan.id)
body.set('token', token)
body.set('timestamp', req.headers.timestamp)
fetch(uri, {
method: 'PUT',
headers: {
authorization: `Basic ${auth}`,
},
form: {
plan: payload.plan.id,
token: token,
timestamp: req.headers.timestamp
}
}, function(e,r,b){
if (r.statusCode == 201 || r.statusCode == 200) {
body,
}).then(function(r){
if (r.status == 201 || r.status == 200) {
//console.log("here")
if (token === null) console.log()
// var sub = JSON.parse(b)
// console.log(" plan:".grey, sub.plan.name)
} else {
console.log("ERROR")
}
}, function (e) {
console.log("ERROR", e)
})
})

Expand Down Expand Up @@ -273,72 +272,72 @@ module.exports = function(req, next){
* Upload
*/

// create upload
var uri = url.resolve(req.endpoint, req.domain)
var handshake = request.put(uri, { headers: headers })

// apply basic auth
handshake.auth("token", req.creds.token, true)

// catch errors
handshake.on('error', console.log)

// split replies on new line
handshake.pipe(split())

// output result
handshake.on("data", tick)

// done
handshake.on("end", function(){
if (req.success === true){
return next()
} else {
helpers.log()
helpers.log()
helpers.log(" Error".red + " - Deployment did not succeed.".grey)
helpers.log()
process.exit(1)
}

})
// Read Project
var project = fsReader({ 'path': req.project, ignoreFiles: [".surgeignore"] })

// we always ignore .git directory
project.addIgnoreRules(ignore)

handshake.on("response", function(rsp){
// chain all this together...
var tarballStream = project
.pipe(tar.Pack())
.pipe(zlib.Gzip())

if (rsp.statusCode == 403) {
// create upload
var uri = new URL(req.endpoint, req.domain)
let headers = {
...headers,
auth: `Basic ${Buffer.from(`token:${req.creds.token}`).toString('base64')}`,
}
fetch(uri, {
method: 'PUT',
headers,
body: ReadableStream.from(tarballStream),
}).then(async function (rsp) {
if (rsp.status === 403) {
helpers.log()
if(rsp.headers.hasOwnProperty("reason")){
helpers.trunc("Aborted".yellow + " - " + rsp.headers["reason"])
if(rsp.headers.has("reason")){
helpers.trunc("Aborted".yellow + " - " + rsp.headers.get("reason"))
} else {
helpers.trunc("Aborted".yellow + (" - you do not have permission to publish to " + req.domain.underline).grey)
}
helpers.log()
process.exit(1)
} else if (rsp.statusCode == 401) {
} else if (rsp.status == 401) {
localCreds(req.argv.endpoint).set(null)
helpers.log()
if(rsp.headers.hasOwnProperty("reason")){
helpers.trunc("Aborted".yellow + " - " + rsp.headers["reason"])
if(rsp.headers.has("reason")){
helpers.trunc("Aborted".yellow + " - " + rsp.headers.get("reason"))
} else {
helpers.trunc("Aborted".yellow + " - local token has expired and cleared. please try again.")
}
helpers.log()
process.exit(1)
// console.log(rsp.statusCode)
// console.log(rsp.status)
}
})

// Read Project
var project = fsReader({ 'path': req.project, ignoreFiles: [".surgeignore"] })

// we always ignore .git directory
project.addIgnoreRules(ignore)
var splitStream = split()
if (rsp.body) {
for await (var chunk of rsp.body) {
// output result
tick()
// split replies on new line
splitStream.write(chunk)
}
}

// chain all this together...
project
.pipe(tar.Pack())
.pipe(zlib.Gzip())
.pipe(handshake)
// done
if (req.success === true) {
return next()
} else {
helpers.log()
helpers.log()
helpers.log(" Error".red + " - Deployment did not succeed.".grey)
helpers.log()
process.exit(1)
}

}, function(er) {
console.log(er)
})
}
17 changes: 6 additions & 11 deletions lib/middleware/list.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,18 @@
var url = require("url")
var request = require("request")
var Table = require("cli-table3")
var helpers = require("./util/helpers")

module.exports = function(req, next){

var url = new URL('/list', req.endpoint.format())
var authorization = `Basic ${Buffer.from(`token:${req.creds.token}`).toString('base64')}`
var options = {
'url': url.resolve(req.endpoint.format(), '/list'),
'method': 'get',
'auth': {
'user': "token",
'pass': req.creds.token,
'sendImmediately': true
headers: {
authorization
}
}

request(options, function(e, r, obj){
if (e) throw e
var list = JSON.parse(obj)
fetch(url, options).then(async function(r) {
var list = await r.json()

var table = new Table({
//head: ["cmd".underline.grey, 'REV DOMAIN'.grey, 'AGE'.grey, 'MODE'.grey],
Expand Down
1 change: 0 additions & 1 deletion lib/middleware/plan.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@

var request = require("request")
var url = require("url")
var helpers = require("./util/helpers")
var path = require("path")
Expand Down
25 changes: 11 additions & 14 deletions lib/middleware/plans.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@

var request = require("request")
var url = require("url")
var helpers = require("./util/helpers")
var path = require("path")
Expand All @@ -10,26 +9,24 @@ var parseUrl = require("url-parse-as-address")
module.exports = function(req, next, abort){

var plansUrl = req.domain
? url.resolve(req.endpoint, path.join(req.domain, "plans"))
: url.resolve(req.endpoint, "plans")
? new URL(path.join(req.domain, "plans"), req.endpoint)
: new URL("plans", req.endpoint)

var headers = {}
var authorization = `Basic ${
Buffer.from(`token:${req.creds.token}`).toString('base64')
}`
var headers = {
authorization
}
if (req.argv.promo) headers.promo = req.argv.promo

var options = {
'url': plansUrl,
'method': 'get',
'headers': headers,
'auth': {
'user': "token",
'pass': req.creds.token,
'sendImmediately': true
}
}

request(options, function(e, r, obj){
if (r.statusCode == 200){
req.plans = JSON.parse(obj)
fetch(plansUrl, options).then(async function(r) {
if (r.status == 200){
req.plans = await r.json()
}
return next()
})
Expand Down
54 changes: 20 additions & 34 deletions lib/middleware/plus.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,20 @@

var request = require("request")
var url = require("url")
var helpers = require("./util/helpers")
var path = require("path")
var fs = require("fs")
var os = require("os")
var parseUrl = require("url-parse-as-address")

module.exports = function(req, next, abort){

var plus = function(domain){
var url = new URL(domain + '/subscription', req.endpoint)
var authorization = `Basic ${
Buffer.from(`token:${req.creds.token}`).toString('base64')
}`
var options = {
'url': url.resolve(req.endpoint, domain + "/subscription"),
'method': 'get',
'auth': {
'user': "token",
'pass': req.creds.token,
'sendImmediately': true
}
headers: { authorization }
}

request(options, function(e, r, obj){
if (e) throw e

var payload = JSON.parse(obj)
fetch(url, options).then(async function(r) {
var payload = await r.json()

var msg = " Project requires the ".blue + payload.plan.name.yellow + " plan. ".blue + ("$" + (payload.plan.amount / 100) + "/mo").yellow + " (cancel anytime).".blue

Expand All @@ -38,37 +29,32 @@ module.exports = function(req, next, abort){
}

helpers.payment(req, payload["stripe_pk"], payload.card)(function(token){
var uri = url.resolve(req.endpoint, domain + "/subscription")
request({
uri: uri,
var uri = new URL(domain + "/subscription", req.endpoint)
var body = new FormData()
body.set('plan', payload.plan)
body.set('token', token)
fetch(uri, {
method: "PUT",
auth: {
'user': 'token',
'pass': req.creds.token,
'sendImmediately': true
},
form: {
plan: payload.plan,
token: token
}
}, function(e,r,b){
if (r.statusCode == 201) {
headers: { authorization },
body
}).then(async function(r) {
if (r.status == 201) {
if (token === null){
helpers.space()
}
var sub = JSON.parse(b)
var sub = await r.json()
helpers.log(helpers.smart("plan:").grey + " " + sub.plan.name)
helpers.space()
helpers.trunc(("You are now upgraded to " + sub.plan.name + "!").green)
helpers.space()
} else if (r.statusCode == 200) {
var sub = JSON.parse(b)
} else if (r.status == 200) {
var sub = await r.json()
helpers.log(helpers.smart("plan:").grey + " " + sub.plan.name)
helpers.space()
helpers.trunc(("No charge created. You are already upgraded to " + sub.plan.name + "!").green)
helpers.space()
} else {
helpers.trunc(r.statusCode)
helpers.trunc(r.status)
}
})
})
Expand Down
28 changes: 12 additions & 16 deletions lib/middleware/setcard.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@

var request = require("request")
var url = require("url")
var helpers = require("./util/helpers")

module.exports = function(req, next, abort){
Expand All @@ -15,29 +13,27 @@ module.exports = function(req, next, abort){
helpers.space()
} else {

var fields = {}
var fields = new FormData()

if (req.paymentToken){
fields.token = req.paymentToken
fields.set('token', req.paymentToken)
}
var authorization = `Basic ${
Buffer.from(`token:${req.creds.token}`).toString('base64')
}`

request({
uri: url.resolve(req.endpoint, "card"),
fetch(new URL('card', req.entrypoint), {
method: "PUT",
auth: {
'user': 'token',
'pass': req.creds.token,
'sendImmediately': true
},
form: fields
}, function(e,r,b){
if ([200,201].indexOf(r.statusCode) !== -1) {
var obj = JSON.parse(b)
headers: { authorization },
body: fields
}).then(async function(r){
if ([200,201].indexOf(r.status) !== -1) {
var obj = await r.json()
console.log()
console.log((" Success".green + " - ".grey + obj.msg.grey))
console.log()
} else {
var obj = JSON.parse(b)
var obj = await r.json()
console.log()
console.log(" Error ".red + " - " + (obj.message || obj.msg).grey)
console.log()
Expand Down
Loading