update README, added url builder to urlTools.ts
This commit is contained in:
parent
03969051a4
commit
2b41b4c66c
|
@ -11,4 +11,5 @@
|
|||
> Long: `Yj3ryVkyOzDg6UbEHkWiNkNZcbbR5wTwtm6TZ6F7OLCI`
|
||||
>
|
||||
> Invalid: `bZLMOC157NAlKtFP3wHXkXj0eLAu6E/Bv6rGL4HLQ===`
|
||||
|
||||
- [x] build url from component parts
|
||||
- [ ] fetch data from url
|
||||
|
|
31
package-lock.json
generated
31
package-lock.json
generated
|
@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
{
|
||||
"devDependencies": {
|
||||
"typescript": "^4.7.4"
|
||||
"@types/node": "^18.7.1",
|
||||
"typescript": "^4.7.4",
|
||||
"xmlhttprequest": "^1.8.0"
|
||||
}
|
||||
}
|
||||
|
|
40
src/parse.js
40
src/parse.js
|
@ -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));
|
24
src/urlTools.ts
Normal file
24
src/urlTools.ts
Normal file
|
@ -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")))
|
|
@ -1,5 +1,6 @@
|
|||
{
|
||||
"files": [
|
||||
"src/parse.ts"
|
||||
"src/parse.ts",
|
||||
"src/urlTools.ts"
|
||||
]
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue