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

Commit

Permalink
Fix AbortSignal from crosis disagreeing with node-fetch
Browse files Browse the repository at this point in the history
node-fetch expects the signal to be an instanceof their signal
deferring abort signals for a future feat
Solutions proposed here node-fetch/node-fetch#784
  • Loading branch information
masad-frost committed Jan 9, 2021
1 parent 72cedd6 commit e2e6be3
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 18 deletions.
2 changes: 0 additions & 2 deletions .vscodeignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,5 @@ src/**
vsc-extension-quickstart.md
**/tsconfig.json
**/.eslintrc.json
**/*.map
**/*.ts
node_modules
webpack.config.js
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "replit",
"displayName": "replit",
"description": "",
"version": "0.0.1",
"version": "0.0.2",
"publisher": "masad-frost",
"repository": "https://github.com/replit/replit-vscode",
"engines": {
Expand Down Expand Up @@ -48,7 +48,7 @@
"build": "webpack --mode production",
"watch": "webpack --mode development --watch",
"lint": "eslint ./src --quiet",
"test-types": "tsc -p ./ --noEmit",
"tscc": "tsc -p ./ --noEmit",
"test": "echo \"no teste :(\""
},
"prettier": {
Expand Down
8 changes: 1 addition & 7 deletions src/api.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import fetch from 'node-fetch';
import { AbortSignal } from 'node-fetch/externals';
import { GraphQLClient, gql } from 'graphql-request';
import { ReplInfo } from './types';

Expand Down Expand Up @@ -83,13 +82,8 @@ export async function getReplInfo(input: string): Promise<ReplInfo> {
return getReplInfoByUrl(`https://repl.it/@${user}/${slug}`);
}

export async function fetchToken(
abortSignal: AbortSignal,
replId: string,
apiKey: string,
): Promise<string> {
export async function fetchToken(replId: string, apiKey: string): Promise<string> {
const r = await fetch(`https://repl.it/api/v0/repls/${replId}/token`, {
signal: abortSignal,
method: 'POST',
headers: {
accept: 'application/json',
Expand Down
9 changes: 3 additions & 6 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import ReplitTerminal from './shell';
import { CrosisClient, ReplInfo } from './types';
import { fetchToken, getReplInfo } from './api';

const BAD_KEY_MSG = 'Please enter a valid crosis key';

// Simple key regex. No need to be strict here.
const validKey = (key: string): boolean => !!key && /[a-zA-Z0-9/=]+:[a-zA-Z0-9/=]+/.test(key);

Expand Down Expand Up @@ -41,7 +39,7 @@ const ensureKey = async (
placeHolder: 'Enter your api key from https://devs.turbio.repl.co',
value: '',
ignoreFocusOut: true,
validateInput: (val) => (validKey(val) ? '' : BAD_KEY_MSG),
validateInput: (val) => (validKey(val) ? '' : 'Please enter a valid crosis key'),
});

if (newKey && validKey(newKey)) {
Expand Down Expand Up @@ -82,15 +80,14 @@ function openReplClient(
extensionContext: context,
replInfo,
},
// eslint-disable-next-line @typescript-eslint/no-explicit-any
fetchToken: async (abortSignal: any) => {
fetchToken: async (_abortSignal: any) => {
if (!apiKey) {
throw new Error('Repl.it: Failed to open repl, no API key provided');
}

let token;
try {
token = await fetchToken(abortSignal, replInfo.id, apiKey);
token = await fetchToken(replInfo.id, apiKey);
} catch (e) {
if (e.name === 'AbortError') {
return { aborted: true, token: null };
Expand Down

0 comments on commit e2e6be3

Please sign in to comment.