UNPKG

4.92 kBJavaScriptView Raw
1"use strict";
2var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3 if (k2 === undefined) k2 = k;
4 var desc = Object.getOwnPropertyDescriptor(m, k);
5 if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6 desc = { enumerable: true, get: function() { return m[k]; } };
7 }
8 Object.defineProperty(o, k2, desc);
9}) : (function(o, m, k, k2) {
10 if (k2 === undefined) k2 = k;
11 o[k2] = m[k];
12}));
13var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14 Object.defineProperty(o, "default", { enumerable: true, value: v });
15}) : function(o, v) {
16 o["default"] = v;
17});
18var __importStar = (this && this.__importStar) || function (mod) {
19 if (mod && mod.__esModule) return mod;
20 var result = {};
21 if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22 __setModuleDefault(result, mod);
23 return result;
24};
25Object.defineProperty(exports, "__esModule", { value: true });
26exports.ConnSpec = void 0;
27/* eslint jsdoc/require-jsdoc: off */
28const qs = __importStar(require("querystring"));
29const partsMatcher = /((.*):\/\/)?(([^/?:]*)(:([^/?:@]*))?@)?([^/?]*)(\/([^?]*))?(\?(.*))?/;
30const hostMatcher = /((\[[^\]]+\]+)|([^;,:]+))(:([0-9]*))?(;,)?/g;
31const kvMatcher = /([^=]*)=([^&?]*)[&?]?/g;
32class ConnSpec {
33 constructor(data) {
34 this.scheme = 'couchbase';
35 this.hosts = [['localhost', 0]];
36 this.bucket = '';
37 this.options = {};
38 if (data) {
39 Object.assign(this, data);
40 }
41 }
42 static parse(connStr) {
43 const spec = new ConnSpec();
44 if (!connStr) {
45 return spec;
46 }
47 const parts = partsMatcher.exec(connStr);
48 if (!parts) {
49 return spec;
50 }
51 if (parts[2]) {
52 spec.scheme = parts[2];
53 }
54 else {
55 spec.scheme = 'couchbase';
56 }
57 if (parts[7]) {
58 spec.hosts = [];
59 while (hostMatcher) {
60 const hostMatch = hostMatcher.exec(parts[7]);
61 if (!hostMatch) {
62 break;
63 }
64 spec.hosts.push([
65 hostMatch[1],
66 hostMatch[5] ? parseInt(hostMatch[5], 10) : 0,
67 ]);
68 }
69 }
70 else {
71 throw new Error('a connection string with no hosts is illegal');
72 }
73 if (parts[9]) {
74 spec.bucket = parts[9];
75 }
76 else {
77 spec.bucket = '';
78 }
79 if (parts[11]) {
80 spec.options = {};
81 for (;;) {
82 const kvMatch = kvMatcher.exec(parts[11]);
83 if (!kvMatch) {
84 break;
85 }
86 const optKey = qs.unescape(kvMatch[1]);
87 const optVal = qs.unescape(kvMatch[2]);
88 if (optKey in spec.options) {
89 const specOptVal = spec.options[optKey];
90 if (typeof specOptVal === 'string') {
91 spec.options[optKey] = [specOptVal, optVal];
92 }
93 else {
94 specOptVal.push(optVal);
95 }
96 }
97 else {
98 spec.options[optKey] = optVal;
99 }
100 }
101 }
102 else {
103 spec.options = {};
104 }
105 return spec;
106 }
107 toString() {
108 let connStr = '';
109 if (this.scheme) {
110 connStr += this.scheme + '://';
111 }
112 if (this.hosts.length === 0) {
113 throw new Error('a connection string with no hosts is illegal');
114 }
115 for (let i = 0; i < this.hosts.length; ++i) {
116 const host = this.hosts[i];
117 if (i !== 0) {
118 connStr += ',';
119 }
120 connStr += host[0];
121 if (host[1]) {
122 connStr += ':' + host[1];
123 }
124 }
125 if (this.bucket) {
126 connStr += '/' + this.bucket;
127 }
128 if (this.options) {
129 const optParts = [];
130 for (const optKey in this.options) {
131 const optVal = this.options[optKey];
132 if (typeof optVal === 'string') {
133 optParts.push(qs.escape(optKey) + '=' + qs.escape(optVal));
134 }
135 else {
136 for (let optIdx = 0; optIdx < optVal.length; ++optIdx) {
137 optParts.push(qs.escape(optKey) + '=' + qs.escape(optVal[optIdx]));
138 }
139 }
140 }
141 if (optParts.length > 0) {
142 connStr += '?' + optParts.join('&');
143 }
144 }
145 return connStr;
146 }
147}
148exports.ConnSpec = ConnSpec;