UNPKG

@worker-tools/deno-kv-storage

Version:

An implementation of the StorageArea (1,2,3) interface for Deno with an extensible system for supporting various database backends.

91 lines 3.43 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getSocketName = exports.isTemplateString = exports.parseConnectionUri = exports.readUInt32BE = exports.readInt32BE = exports.readUInt16BE = exports.readInt16BE = void 0; const deps_js_1 = require("../deps.js"); function readInt16BE(buffer, offset) { offset = offset >>> 0; const val = buffer[offset + 1] | (buffer[offset] << 8); return val & 0x8000 ? val | 0xffff0000 : val; } exports.readInt16BE = readInt16BE; function readUInt16BE(buffer, offset) { offset = offset >>> 0; return buffer[offset] | (buffer[offset + 1] << 8); } exports.readUInt16BE = readUInt16BE; function readInt32BE(buffer, offset) { offset = offset >>> 0; return ((buffer[offset] << 24) | (buffer[offset + 1] << 16) | (buffer[offset + 2] << 8) | buffer[offset + 3]); } exports.readInt32BE = readInt32BE; function readUInt32BE(buffer, offset) { offset = offset >>> 0; return (buffer[offset] * 0x1000000 + ((buffer[offset + 1] << 16) | (buffer[offset + 2] << 8) | buffer[offset + 3])); } exports.readUInt32BE = readUInt32BE; /** * This function parses valid connection strings according to https://www.postgresql.org/docs/14/libpq-connect.html#LIBPQ-CONNSTRING * * The only exception to this rule are multi-host connection strings */ function parseConnectionUri(uri) { var _a, _b; const parsed_uri = uri.match(/(?<driver>\w+):\/{2}((?<user>[^\/?#\s:]+?)?(:(?<password>[^\/?#\s]+)?)?@)?(?<full_host>[^\/?#\s]+)?(\/(?<path>[^?#\s]*))?(\?(?<params>[^#\s]+))?.*/); if (!parsed_uri) throw new Error("Could not parse the provided URL"); let { driver = "", full_host = "", params = "", password = "", path = "", user = "", } = (_a = parsed_uri.groups) !== null && _a !== void 0 ? _a : {}; const parsed_host = full_host.match(/(?<host>(\[.+\])|(.*?))(:(?<port>[\w]*))?$/); if (!parsed_host) throw new Error(`Could not parse "${full_host}" host`); let { host = "", port = "", } = (_b = parsed_host.groups) !== null && _b !== void 0 ? _b : {}; try { if (host) { host = decodeURIComponent(host); } } catch (_e) { console.error((0, deps_js_1.bold)((0, deps_js_1.yellow)("Failed to decode URL host") + "\nDefaulting to raw host")); } if (port && Number.isNaN(Number(port))) { throw new Error(`The provided port "${port}" is not a valid number`); } try { if (password) { password = decodeURIComponent(password); } } catch (_e) { console.error((0, deps_js_1.bold)((0, deps_js_1.yellow)("Failed to decode URL password") + "\nDefaulting to raw password")); } return { driver, host, params: Object.fromEntries(new URLSearchParams(params).entries()), password, path, port, user, }; } exports.parseConnectionUri = parseConnectionUri; function isTemplateString(template) { if (!Array.isArray(template)) { return false; } return true; } exports.isTemplateString = isTemplateString; /** * https://www.postgresql.org/docs/14/runtime-config-connection.html#RUNTIME-CONFIG-CONNECTION-SETTINGS * unix_socket_directories */ const getSocketName = (port) => `.s.PGSQL.${port}`; exports.getSocketName = getSocketName; //# sourceMappingURL=utils.js.map