finish parser script

This commit is contained in:
Bit Borealis 2022-08-12 01:22:55 +00:00
parent fc97b12562
commit e55a845642
Signed by: theotheroracle
GPG key ID: 2D816A2DCA6E5649
5 changed files with 94 additions and 17 deletions

View file

@ -1,7 +1,40 @@
// number will be 0 if valid var baseUrl = "https://0x0.st";
// 1 if no id ( creator ) var id = "a0eiaje"; // < unicode >
// 2 if encryption key is missing / malformed / wrong length var key = "LhBRttf+oI/PNHyTBrPa7HGDK/uC9fcMkGGgz63zT4Y="; // 32 bytes b64
var completeUrl = baseUrl + "/" + id + "#" + 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) { function validUrl(url) {
// do stuff var key;
return 0; 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));

View file

@ -1,7 +1,43 @@
// number will be 0 if valid const baseUrl: string = "https://0x0.st"
// 1 if no id ( creator ) const id: string = "a0eiaje" // < unicode >
// 2 if encryption key is missing / malformed / wrong length const key: string = "LhBRttf+oI/PNHyTBrPa7HGDK/uC9fcMkGGgz63zT4Y=" // 32 bytes b64
function validUrl(url: string): number { const completeUrl: string = baseUrl + "/" + id + "#" + key
// do stuff console.log(completeUrl);
return 0 const urlObj:URL = 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: URL): [string,Uint8Array] | number {
var key: string
try {
key = atob(url.hash.substring(1))
} catch(e) {
return 3
} }
const id: string = url.pathname.substring(1);
if ( id === "/" ) {
return 1
}
const binKey = binaryToArray(key)
if ( key.length === 32 ) {
return [id,binKey]
}
else {
return 2
}
}
function binaryToArray(raw: string): Uint8Array {
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));

11
todo.md Normal file
View file

@ -0,0 +1,11 @@
- [x] baseurl / id # < 32 bytes of b64 ( key ) >
- parse handling happypath and stormypath
- no id < no creator >
- < key missing / malformed / wrong length >
- if malformed re-request the url
- test :
Valid: `LhBRttf+oI/PNHyTBrPa7HGDK/uC9fcMkGGgz63zT4Y=`
Short: `bi83sPUmUNknOITgXStHuCfimOjpELmizvVnW8LL2Q==`
Long: `Yj3ryVkyOzDg6UbEHkWiNkNZcbbR5wTwtm6TZ6F7OLCI`
Invalid: `bZLMOC157NAlKtFP3wHXkXj0eLAu6E/Bv6rGL4HLQ===`

View file

@ -1,5 +0,0 @@
- baseurl / id # < 32 bytes of b64 ( key ) >
- parse handling happypath and stormypath
- no id < no creator >
- < key missing / malformed / wrong length >
- if malformed re-request the url

View file

@ -1,3 +1,5 @@
{ {
"include": ["src/**/*", "tests/**/*"] "files": [
"src/parse.ts"
]
} }