diff --git a/README.md b/README.md index 73b0b64..29d12dc 100644 --- a/README.md +++ b/README.md @@ -11,4 +11,5 @@ > Long: `Yj3ryVkyOzDg6UbEHkWiNkNZcbbR5wTwtm6TZ6F7OLCI` > > Invalid: `bZLMOC157NAlKtFP3wHXkXj0eLAu6E/Bv6rGL4HLQ===` - +- [x] build url from component parts +- [ ] fetch data from url diff --git a/package-lock.json b/package-lock.json index 02f7b8a..2c054b4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -5,9 +5,17 @@ "packages": { "": { "devDependencies": { - "typescript": "^4.7.4" + "@types/node": "^18.7.1", + "typescript": "^4.7.4", + "xmlhttprequest": "^1.8.0" } }, + "node_modules/@types/node": { + "version": "18.7.1", + "resolved": "https://registry.npmjs.org/@types/node/-/node-18.7.1.tgz", + "integrity": "sha512-GKX1Qnqxo4S+Z/+Z8KKPLpH282LD7jLHWJcVryOflnsnH+BtSDfieR6ObwBMwpnNws0bUK8GI7z0unQf9bARNQ==", + "dev": true + }, "node_modules/typescript": { "version": "4.7.4", "resolved": "https://registry.npmjs.org/typescript/-/typescript-4.7.4.tgz", @@ -20,14 +28,35 @@ "engines": { "node": ">=4.2.0" } + }, + "node_modules/xmlhttprequest": { + "version": "1.8.0", + "resolved": "https://registry.npmjs.org/xmlhttprequest/-/xmlhttprequest-1.8.0.tgz", + "integrity": "sha512-58Im/U0mlVBLM38NdZjHyhuMtCqa61469k2YP/AaPbvCoV9aQGUpbJBj1QRm2ytRiVQBD/fsw7L2bJGDVQswBA==", + "dev": true, + "engines": { + "node": ">=0.4.0" + } } }, "dependencies": { + "@types/node": { + "version": "18.7.1", + "resolved": "https://registry.npmjs.org/@types/node/-/node-18.7.1.tgz", + "integrity": "sha512-GKX1Qnqxo4S+Z/+Z8KKPLpH282LD7jLHWJcVryOflnsnH+BtSDfieR6ObwBMwpnNws0bUK8GI7z0unQf9bARNQ==", + "dev": true + }, "typescript": { "version": "4.7.4", "resolved": "https://registry.npmjs.org/typescript/-/typescript-4.7.4.tgz", "integrity": "sha512-C0WQT0gezHuw6AdY1M2jxUO83Rjf0HP7Sk1DtXj6j1EwkQNZrHAg2XPWlq62oqEhYvONq5pkC2Y9oPljWToLmQ==", "dev": true + }, + "xmlhttprequest": { + "version": "1.8.0", + "resolved": "https://registry.npmjs.org/xmlhttprequest/-/xmlhttprequest-1.8.0.tgz", + "integrity": "sha512-58Im/U0mlVBLM38NdZjHyhuMtCqa61469k2YP/AaPbvCoV9aQGUpbJBj1QRm2ytRiVQBD/fsw7L2bJGDVQswBA==", + "dev": true } } } diff --git a/package.json b/package.json index 8315a6f..55fbeb0 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,7 @@ { "devDependencies": { - "typescript": "^4.7.4" + "@types/node": "^18.7.1", + "typescript": "^4.7.4", + "xmlhttprequest": "^1.8.0" } } diff --git a/src/parse.js b/src/parse.js deleted file mode 100644 index 686105a..0000000 --- a/src/parse.js +++ /dev/null @@ -1,40 +0,0 @@ -var baseUrl = "https://envs.sh"; -var id = "QnK"; // < unicode > -var key = "LhBRttf+oI/PNHyTBrPa7HGDK/uC9fcMkGGgz63zT4Y="; // 32 bytes b64 -var completeUrl = baseUrl + "/" + id + ".bin#" + key; -console.log(completeUrl); -var urlObj = new URL(completeUrl); -/** will return a tuple of the key and id if valid - * 1 if no id ( creator ) - * 2 if encryption key is missing / wrong length - * 3 if key is malformed - */ -function validUrl(url) { - var key; - try { - key = atob(url.hash.substring(1)); - } - catch (e) { - return 3; - } - var id = url.pathname.substring(1); - if (id === "/") { - return 1; - } - var binKey = binaryToArray(key); - if (key.length === 32) { - return [id, binKey]; - } - else { - return 2; - } -} -function binaryToArray(raw) { - var rawLength = raw.length; - var array = new Uint8Array(new ArrayBuffer(rawLength)); - for (var i = 0; i < rawLength; i++) { - array[i] = raw.charCodeAt(i); - } - return array; -} -console.log(validUrl(urlObj)); diff --git a/src/urlTools.ts b/src/urlTools.ts new file mode 100644 index 0000000..108ead6 --- /dev/null +++ b/src/urlTools.ts @@ -0,0 +1,24 @@ +import XMLHttpRequest = require("xmlhttprequest"); + +/** + * converts component parts of url to a full url + */ +function buildUrl(baseUrl: string, id: string): string { + return (baseUrl + "/" + id + ".bin") +} + +/** + * gets data from a constructed url + */ +function getData(url: string): string { + try { + const request = new XMLHttpRequest() + request.open("GET", url, false) + request.send + return(request.responseText) + } catch(e) { + console.log(e) + return null + } +} +console.log(getData(buildUrl("https://envs.sh", "QnK"))) diff --git a/tsconfig.json b/tsconfig.json index 751bfc5..1440dcb 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,5 +1,6 @@ { "files": [ - "src/parse.ts" + "src/parse.ts", + "src/urlTools.ts" ] }