diff --git a/resources/rapydscript/compiler.js.xz b/resources/rapydscript/compiler.js.xz index 6a76a11b76..9c504c7333 100644 Binary files a/resources/rapydscript/compiler.js.xz and b/resources/rapydscript/compiler.js.xz differ diff --git a/resources/rapydscript/lib/encodings.pyj b/resources/rapydscript/lib/encodings.pyj index cd8da7041a..d72e9b54f6 100644 --- a/resources/rapydscript/lib/encodings.pyj +++ b/resources/rapydscript/lib/encodings.pyj @@ -1,26 +1,28 @@ # vim:fileencoding=utf-8 # License: BSD Copyright: 2016, Kovid Goyal -def base64encode(bytes): +def base64encode(bytes, altchars, pad_char): # Convert an array of bytes into a base-64 encoded string l = bytes.length remainder = l % 3 main_length = l - remainder - encodings = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/' + encodings = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789' + (altchars or '+/') + pad_char = '=' if pad_char is undefined else pad_char ans = v'[]' for v'var i = 0; i < main_length; i += 3': chunk = (bytes[i] << 16) | (bytes[i + 1] << 8) | bytes[i + 2] ans.push(encodings[(chunk & 16515072) >> 18], encodings[(chunk & 258048) >> 12], encodings[(chunk & 4032) >> 6], encodings[chunk & 63]) if remainder is 1: chunk = bytes[main_length] - ans.push(encodings[(chunk & 252) >> 2], encodings[(chunk & 3) << 4], '=', '=') + ans.push(encodings[(chunk & 252) >> 2], encodings[(chunk & 3) << 4], pad_char, pad_char) elif remainder is 2: chunk = (bytes[main_length] << 8) | bytes[main_length + 1] - ans.push(encodings[(chunk & 64512) >> 10], encodings[(chunk & 1008) >> 4], encodings[(chunk & 15) << 2], '=') + ans.push(encodings[(chunk & 64512) >> 10], encodings[(chunk & 1008) >> 4], encodings[(chunk & 15) << 2], pad_char) return ans.join('') def base64decode(string): - # convert the output of base64encode back into an array of bytes (Uint8Array) + # convert the output of base64encode back into an array of bytes + # (Uint8Array) only works with the standard altchars and pad_char if jstype(window) is not 'undefined': chars = window.atob(string) else: @@ -30,6 +32,13 @@ def base64decode(string): ans[i] = chars.charCodeAt(i) return ans +def urlsafe_b64encode(bytes, pad_char): + return base64encode(bytes, '-_', pad_char) + +def urlsafe_b64decode(string): + string = String.prototype.replace.call(string, /[_-]/g, def(m): return '+' if m is '-' else '/';) + return base64decode(string) + def hexlify(bytes): ans = v'[]' for v'var i = 0; i < bytes.length; i++': diff --git a/resources/rapydscript/lib/uuid.pyj b/resources/rapydscript/lib/uuid.pyj new file mode 100644 index 0000000000..ad69d1e085 --- /dev/null +++ b/resources/rapydscript/lib/uuid.pyj @@ -0,0 +1,77 @@ +# vim:fileencoding=utf-8 +# License: BSD Copyright: 2017, Kovid Goyal +# globals: crypto +from __python__ import hash_literals + +from encodings import hexlify, urlsafe_b64decode, urlsafe_b64encode + +RFC_4122 = 1 + +if jstype(crypto) is 'object' and crypto.getRandomValues: + random_bytes = def (num): + ans = Uint8Array(num or 16) + crypto.getRandomValues(ans) + return ans +else: + random_bytes = def (num): + ans = Uint8Array(num or 16) + for i in range(ans.length): + ans[i] = Math.floor(Math.random() * 256) + return ans + + +def uuid4_bytes(): + data = random_bytes() + data[6] = 0b01000000 | (data[6] & 0b1111) + data[8] = (((data[8] >> 4) & 0b11 | 0b1000) << 4) | (data[8] & 0b1111) + return data + + +def as_str(): + h = this.hex + return h[:8] + '-' + h[8:12] + '-' + h[12:16] + '-' + h[16:20] + '-' + h[20:] + + +def uuid4(): + b = uuid4_bytes() + return { + 'hex': hexlify(b), + 'bytes': b, + 'variant': RFC_4122, + 'version': 4, + '__str__': as_str, + 'toString': as_str, + } + + +def num_to_string(numbers, alphabet, pad_to_length): + ans = v'[]' + alphabet_len = alphabet.length + numbers = Array.prototype.slice.call(numbers) + for v'var i = 0; i < numbers.length - 1; i++': + x = divmod(numbers[i], alphabet_len) + numbers[i] = x[0] + numbers[i+1] += x[1] + for v'var i = 0; i < numbers.length; i++': + number = numbers[i] + while number: + x = divmod(number, alphabet_len) + number = x[0] + ans.push(alphabet[x[1]]) + if pad_to_length and pad_to_length > ans.length: + ans.push(alphabet[0].repeat(pad_to_length - ans.length)) + return ans.join('') + + +def short_uuid(): + # A totally random uuid encoded using only URL and filename safe characters + return urlsafe_b64encode(random_bytes(), '') + + +def short_uuid4(): + # A uuid4 encoded using only URL and filename safe characters + return urlsafe_b64encode(uuid4_bytes(), '') + + +def decode_short_uuid(val): + return urlsafe_b64decode(val + '==')