Skip to content
This repository has been archived by the owner on Oct 4, 2024. It is now read-only.

Commit

Permalink
Merge pull request #300 from nearprotocol/gitpod
Browse files Browse the repository at this point in the history
Add GitPod support
  • Loading branch information
vgrichina authored Apr 3, 2020
2 parents ef3e033 + 6a0b59d commit 0a5705a
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 18 deletions.
8 changes: 8 additions & 0 deletions .gitpod.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
tasks:
- init: yarn install
github:
prebuilds:
addBadge: true
ports:
- port: 6000
onOpen: open-browser
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# NEAR Shell command line interface

[![Build Status](https://travis-ci.com/nearprotocol/near-shell.svg?branch=master)](https://travis-ci.com/nearprotocol/near-shell)
[![Gitpod Ready-to-Code](https://img.shields.io/badge/Gitpod-Ready--to--Code-blue?logo=gitpod)](https://gitpod.io/#https://github.com/nearprotocol/near-shell)

NEAR Shell is a Node.js application that relies on [`nearlib`](https://github.com/nearprotocol/nearlib) to generate secure keys, connect to the NEAR platform and send transactions to the network on your behalf.

Expand Down
21 changes: 14 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ exports.login = async function(options) {

// find a callback URL on the local machine
try {
tempUrl = await capture.callback();
tempUrl = await capture.callback(6000);
} catch (error) {
// console.error("Failed to find suitable port.", error.message)
// TODO: Is it? Try triggering error
Expand All @@ -72,20 +72,27 @@ exports.login = async function(options) {

// if we found a suitable URL, attempt to use it
if (tempUrl) {
// open a browser to capture NEAR Wallet callback (and quietly direct the user if open fails)
try {
if (process.env.GITPOD_WORKSPACE_URL) {
const workspaceUrl = new URL(process.env.GITPOD_WORKSPACE_URL);
newUrl.searchParams.set('success_url', `https://${tempUrl.port}-${workspaceUrl.hostname}`);
// Browser not opened, as will open automatically for opened port
} else {
newUrl.searchParams.set('success_url', `http://${tempUrl.hostname}:${tempUrl.port}`);
await open(newUrl.toString());
} catch (error) {
console.error(`Failed to open the URL [ ${newUrl.toString()} ]`, error);

try {
// open a browser to capture NEAR Wallet callback (and quietly direct the user if open fails)
await open(newUrl.toString());
} catch (error) {
console.error(`Failed to open the URL [ ${newUrl.toString()} ]`, error);
}
}
}

console.log(chalk`\n{dim If your browser doesn't automatically open, please visit this URL\n${newUrl.toString()}}`);

const getAccountFromWebpage = async () => {
// capture account_id as provided by NEAR Wallet
const [accountId] = await capture.payload(['account_id'], tempUrl);
const [accountId] = await capture.payload(['account_id'], tempUrl, newUrl);
return accountId;
};

Expand Down
32 changes: 21 additions & 11 deletions utils/capture-login-success.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,35 @@ let server;
@param port the port the server should use
@param hostname the hostname the server should use
*/
const payload = (fields, { port, hostname }) => new Promise((resolve, reject) => {

const message = renderWebPage('You are logged in. Please close this window.');
const payload = (fields, { port, hostname }, redirectUrl) => new Promise((resolve, reject) => {
server = stoppable(http.createServer(handler)).listen(port, hostname);

/**
request handler for single-use node server
*/
function handler(req, res){
res.statusCode = 200;
res.setHeader('Content-Type', 'text/html');
try {
let parsedUrl = url.parse(req.url, true);
let results = fields.map((field) => parsedUrl.query[field]);
res.end(message, () => {
server.stop();
resolve(results);
});

if (Object.keys(parsedUrl.query).length > 0) {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/html');
// TODO: Make code more specialized (vs handling generic fields) and only output this if login succeeded end to end
res.end(renderWebPage('You are logged in. Please close this window.'), () => {
server.stop();
resolve(results);
});
} else {
res.writeHead(301, { Location: redirectUrl });
res.end();
}
} catch (e) {
console.log('Unexpected error: ', e);
console.error('Unexpected error: ', e);
res.statusCode = 400;
res.end('It\'s a scam!');
server.stop();
reject('Failed to capture accountId');
reject(new Error('Failed to capture accountId'));
}
}
});
Expand All @@ -49,6 +54,11 @@ const payload = (fields, { port, hostname }) => new Promise((resolve, reject) =>
@param range the number of ports to try scanning before giving up
*/
const callback = async (port = 3000, hostname = '127.0.0.1', range = 10) => {
if (process.env.GITPOD_WORKSPACE_URL) {
// NOTE: Port search interferes with GitPod port management
return { port, hostname };
}

const start = port;
const end = start + range;
let inUse = true;
Expand Down

0 comments on commit 0a5705a

Please sign in to comment.