@sqlitecloud/drivers 1.0.422 → 1.0.438
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/drivers/database.js
CHANGED
|
@@ -114,6 +114,7 @@ class Database extends eventemitter3_1.default {
|
|
|
114
114
|
})
|
|
115
115
|
.catch(error => {
|
|
116
116
|
this.handleError(error, callback);
|
|
117
|
+
this.close();
|
|
117
118
|
done(error);
|
|
118
119
|
});
|
|
119
120
|
});
|
|
@@ -134,6 +135,7 @@ class Database extends eventemitter3_1.default {
|
|
|
134
135
|
})
|
|
135
136
|
.catch(error => {
|
|
136
137
|
this.handleError(error, callback);
|
|
138
|
+
this.close();
|
|
137
139
|
done(error);
|
|
138
140
|
});
|
|
139
141
|
});
|
|
@@ -144,20 +146,20 @@ class Database extends eventemitter3_1.default {
|
|
|
144
146
|
let error = null;
|
|
145
147
|
// we don't wont to silently open a new connection after a disconnession
|
|
146
148
|
if (this.connection && this.connection.connected) {
|
|
147
|
-
this.connection.sendCommands(command,
|
|
149
|
+
this.connection.sendCommands(command, (error, results) => {
|
|
150
|
+
callback === null || callback === void 0 ? void 0 : callback.call(this, error, results);
|
|
151
|
+
done(error);
|
|
152
|
+
});
|
|
148
153
|
}
|
|
149
154
|
else {
|
|
150
155
|
error = new types_1.SQLiteCloudError('Connection unavailable. Maybe it got disconnected?', { errorCode: 'ERR_CONNECTION_NOT_ESTABLISHED' });
|
|
151
|
-
|
|
156
|
+
callback === null || callback === void 0 ? void 0 : callback.call(this, error, null);
|
|
157
|
+
done(error);
|
|
152
158
|
}
|
|
153
|
-
done(error);
|
|
154
159
|
});
|
|
155
160
|
}
|
|
156
161
|
/** Handles an error by closing the connection, calling the callback and/or emitting an error event */
|
|
157
162
|
handleError(error, callback) {
|
|
158
|
-
var _a;
|
|
159
|
-
// an errored connection is thrown out
|
|
160
|
-
(_a = this.connection) === null || _a === void 0 ? void 0 : _a.close();
|
|
161
163
|
if (callback) {
|
|
162
164
|
callback.call(this, error);
|
|
163
165
|
}
|
|
@@ -340,11 +342,14 @@ class Database extends eventemitter3_1.default {
|
|
|
340
342
|
* parameters is emitted, regardless of whether a callback was provided or not.
|
|
341
343
|
*/
|
|
342
344
|
close(callback) {
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
345
|
+
this.operations.enqueue(done => {
|
|
346
|
+
var _a;
|
|
347
|
+
(_a = this.connection) === null || _a === void 0 ? void 0 : _a.close();
|
|
348
|
+
callback === null || callback === void 0 ? void 0 : callback.call(this, null);
|
|
349
|
+
this.emitEvent('close');
|
|
350
|
+
this.operations.clear();
|
|
351
|
+
done(null);
|
|
352
|
+
});
|
|
348
353
|
}
|
|
349
354
|
/**
|
|
350
355
|
* Loads a compiled SQLite extension into the database connection object.
|
package/lib/drivers/utilities.js
CHANGED
|
@@ -194,7 +194,9 @@ function parseconnectionstring(connectionstring) {
|
|
|
194
194
|
url.searchParams.forEach((value, key) => {
|
|
195
195
|
options[key.toLowerCase().replace(/-/g, '_')] = value;
|
|
196
196
|
});
|
|
197
|
-
const config = Object.assign({ username: decodeURIComponent(url.username), password: decodeURIComponent(url.password),
|
|
197
|
+
const config = Object.assign(Object.assign({}, options), { username: decodeURIComponent(url.username), password: decodeURIComponent(url.password), password_hashed: options.password_hashed ? parseBoolean(options.password_hashed) : undefined, host: url.hostname,
|
|
198
|
+
// type cast values
|
|
199
|
+
port: url.port ? parseInt(url.port) : undefined, insecure: options.insecure ? parseBoolean(options.insecure) : undefined, timeout: options.timeout ? parseInt(options.timeout) : undefined, zerotext: options.zerotext ? parseBoolean(options.zerotext) : undefined, create: options.create ? parseBoolean(options.create) : undefined, memory: options.memory ? parseBoolean(options.memory) : undefined, compression: options.compression ? parseBoolean(options.compression) : undefined, non_linearizable: options.non_linearizable ? parseBoolean(options.non_linearizable) : undefined, noblob: options.noblob ? parseBoolean(options.noblob) : undefined, maxdata: options.maxdata ? parseInt(options.maxdata) : undefined, maxrows: options.maxrows ? parseInt(options.maxrows) : undefined, maxrowset: options.maxrowset ? parseInt(options.maxrowset) : undefined, usewebsocket: options.usewebsocket ? parseBoolean(options.usewebsocket) : undefined, verbose: options.verbose ? parseBoolean(options.verbose) : undefined });
|
|
198
200
|
// either you use an apikey or username and password
|
|
199
201
|
if (config.apikey) {
|
|
200
202
|
if (config.username || config.password) {
|
|
@@ -59,7 +59,7 @@ eval("\n/**\n * connection.ts - base abstract class for sqlitecloud server conne
|
|
|
59
59
|
/***/ (function(__unused_webpack_module, exports, __webpack_require__) {
|
|
60
60
|
|
|
61
61
|
"use strict";
|
|
62
|
-
eval("\n//\n// database.ts - database driver api, implements and extends sqlite3\n//\nvar __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {\n if (k2 === undefined) k2 = k;\n var desc = Object.getOwnPropertyDescriptor(m, k);\n if (!desc || (\"get\" in desc ? !m.__esModule : desc.writable || desc.configurable)) {\n desc = { enumerable: true, get: function() { return m[k]; } };\n }\n Object.defineProperty(o, k2, desc);\n}) : (function(o, m, k, k2) {\n if (k2 === undefined) k2 = k;\n o[k2] = m[k];\n}));\nvar __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {\n Object.defineProperty(o, \"default\", { enumerable: true, value: v });\n}) : function(o, v) {\n o[\"default\"] = v;\n});\nvar __importStar = (this && this.__importStar) || (function () {\n var ownKeys = function(o) {\n ownKeys = Object.getOwnPropertyNames || function (o) {\n var ar = [];\n for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;\n return ar;\n };\n return ownKeys(o);\n };\n return function (mod) {\n if (mod && mod.__esModule) return mod;\n var result = {};\n if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== \"default\") __createBinding(result, mod, k[i]);\n __setModuleDefault(result, mod);\n return result;\n };\n})();\nvar __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {\n function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }\n return new (P || (P = Promise))(function (resolve, reject) {\n function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }\n function rejected(value) { try { step(generator[\"throw\"](value)); } catch (e) { reject(e); } }\n function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }\n step((generator = generator.apply(thisArg, _arguments || [])).next());\n });\n};\nvar __importDefault = (this && this.__importDefault) || function (mod) {\n return (mod && mod.__esModule) ? mod : { \"default\": mod };\n};\nObject.defineProperty(exports, \"__esModule\", ({ value: true }));\nexports.Database = void 0;\n// Trying as much as possible to be a drop-in replacement for SQLite3 API\n// https://github.com/TryGhost/node-sqlite3/wiki/API\n// https://github.com/TryGhost/node-sqlite3\n// https://github.com/TryGhost/node-sqlite3/blob/master/lib/sqlite3.d.ts\nconst eventemitter3_1 = __importDefault(__webpack_require__(/*! eventemitter3 */ \"./node_modules/eventemitter3/index.js\"));\nconst pubsub_1 = __webpack_require__(/*! ./pubsub */ \"./lib/drivers/pubsub.js\");\nconst queue_1 = __webpack_require__(/*! ./queue */ \"./lib/drivers/queue.js\");\nconst rowset_1 = __webpack_require__(/*! ./rowset */ \"./lib/drivers/rowset.js\");\nconst statement_1 = __webpack_require__(/*! ./statement */ \"./lib/drivers/statement.js\");\nconst types_1 = __webpack_require__(/*! ./types */ \"./lib/drivers/types.js\");\nconst utilities_1 = __webpack_require__(/*! ./utilities */ \"./lib/drivers/utilities.js\");\n// Uses eventemitter3 instead of node events for browser compatibility\n// https://github.com/primus/eventemitter3\n/**\n * Creating a Database object automatically opens a connection to the SQLite database.\n * When the connection is established the Database object emits an open event and calls\n * the optional provided callback. If the connection cannot be established an error event\n * will be emitted and the optional callback is called with the error information.\n */\nclass Database extends eventemitter3_1.default {\n constructor(config, mode, callback) {\n super();\n /** Used to syncronize opening of connection and commands */\n this.operations = new queue_1.OperationsQueue();\n this.config = typeof config === 'string' ? { connectionstring: config } : config;\n this.connection = null;\n // mode is optional and so is callback\n // https://github.com/TryGhost/node-sqlite3/wiki/API#new-sqlite3databasefilename--mode--callback\n if (typeof mode === 'function') {\n callback = mode;\n mode = undefined;\n }\n // mode is ignored for now\n // opens the connection to the database automatically\n this.createConnection(error => {\n if (callback) {\n callback.call(this, error);\n }\n });\n }\n //\n // private methods\n //\n /** Returns first available connection from connection pool */\n createConnection(callback) {\n var _a, _b;\n // connect using websocket if tls is not supported or if explicitly requested\n const useWebsocket = utilities_1.isBrowser || ((_a = this.config) === null || _a === void 0 ? void 0 : _a.usewebsocket) || ((_b = this.config) === null || _b === void 0 ? void 0 : _b.gatewayurl);\n if (useWebsocket) {\n // socket.io transport works in both node.js and browser environments and connects via SQLite Cloud Gateway\n this.operations.enqueue(done => {\n Promise.resolve().then(() => __importStar(__webpack_require__(/*! ./connection-ws */ \"./lib/drivers/connection-ws.js\"))).then(module => {\n this.connection = new module.default(this.config, (error) => {\n if (error) {\n this.handleError(error, callback);\n }\n else {\n callback === null || callback === void 0 ? void 0 : callback.call(this, null);\n this.emitEvent('open');\n }\n done(error);\n });\n })\n .catch(error => {\n this.handleError(error, callback);\n done(error);\n });\n });\n }\n else {\n this.operations.enqueue(done => {\n Promise.resolve().then(() => __importStar(__webpack_require__(/*! ./connection-tls */ \"./lib/drivers/connection-tls.js\"))).then(module => {\n this.connection = new module.default(this.config, (error) => {\n if (error) {\n this.handleError(error, callback);\n }\n else {\n callback === null || callback === void 0 ? void 0 : callback.call(this, null);\n this.emitEvent('open');\n }\n done(error);\n });\n })\n .catch(error => {\n this.handleError(error, callback);\n done(error);\n });\n });\n }\n }\n enqueueCommand(command, callback) {\n this.operations.enqueue(done => {\n let error = null;\n // we don't wont to silently open a new connection after a disconnession\n if (this.connection && this.connection.connected) {\n this.connection.sendCommands(command, callback);\n }\n else {\n error = new types_1.SQLiteCloudError('Connection unavailable. Maybe it got disconnected?', { errorCode: 'ERR_CONNECTION_NOT_ESTABLISHED' });\n this.handleError(error, callback);\n }\n done(error);\n });\n }\n /** Handles an error by closing the connection, calling the callback and/or emitting an error event */\n handleError(error, callback) {\n var _a;\n // an errored connection is thrown out\n (_a = this.connection) === null || _a === void 0 ? void 0 : _a.close();\n if (callback) {\n callback.call(this, error);\n }\n else {\n this.emitEvent('error', error);\n }\n }\n /**\n * Some queries like inserts or updates processed via run or exec may generate\n * an empty result (eg. no data was selected), but still have some metadata.\n * For example the server may pass the id of the last row that was modified.\n * In this case the callback results should be empty but the context may contain\n * additional information like lastID, etc.\n * @see https://github.com/TryGhost/node-sqlite3/wiki/API#runsql--param---callback\n * @param results Results received from the server\n * @returns A context object if one makes sense, otherwise undefined\n */\n processContext(results) {\n if (results) {\n if (Array.isArray(results) && results.length > 0) {\n switch (results[0]) {\n case types_1.SQLiteCloudArrayType.ARRAY_TYPE_SQLITE_EXEC:\n return {\n lastID: results[2], // ROWID (sqlite3_last_insert_rowid)\n changes: results[3], // CHANGES(sqlite3_changes)\n totalChanges: results[4], // TOTAL_CHANGES (sqlite3_total_changes)\n finalized: results[5] // FINALIZED\n };\n }\n }\n }\n return undefined;\n }\n /** Emits given event with optional arguments on the next tick so callbacks can complete first */\n emitEvent(event, ...args) {\n setTimeout(() => {\n this.emit(event, ...args);\n }, 0);\n }\n //\n // public methods\n //\n /**\n * Returns the configuration with which this database was opened.\n * The configuration is readonly and cannot be changed as there may\n * be multiple connections using the same configuration.\n * @returns {SQLiteCloudConfig} A configuration object\n */\n getConfiguration() {\n return JSON.parse(JSON.stringify(this.config));\n }\n /** Enable verbose mode */\n verbose() {\n var _a;\n this.config.verbose = true;\n (_a = this.connection) === null || _a === void 0 ? void 0 : _a.verbose();\n return this;\n }\n /** Set a configuration option for the database */\n configure(_option, _value) {\n // https://github.com/TryGhost/node-sqlite3/wiki/API#configureoption-value\n return this;\n }\n run(sql, ...params) {\n const { args, callback } = (0, utilities_1.popCallback)(params);\n const command = { query: sql, parameters: args };\n this.enqueueCommand(command, (error, results) => {\n if (error) {\n this.handleError(error, callback);\n }\n else {\n // context may include id of last row inserted, total changes, etc...\n const context = this.processContext(results);\n callback === null || callback === void 0 ? void 0 : callback.call(context || this, null, context ? context : results);\n }\n });\n return this;\n }\n get(sql, ...params) {\n const { args, callback } = (0, utilities_1.popCallback)(params);\n const command = { query: sql, parameters: args };\n this.enqueueCommand(command, (error, results) => {\n if (error) {\n this.handleError(error, callback);\n }\n else {\n if (results && results instanceof rowset_1.SQLiteCloudRowset && results.length > 0) {\n callback === null || callback === void 0 ? void 0 : callback.call(this, null, results[0]);\n }\n else {\n callback === null || callback === void 0 ? void 0 : callback.call(this, null);\n }\n }\n });\n return this;\n }\n all(sql, ...params) {\n const { args, callback } = (0, utilities_1.popCallback)(params);\n const command = { query: sql, parameters: args };\n this.enqueueCommand(command, (error, results) => {\n if (error) {\n this.handleError(error, callback);\n }\n else {\n if (results && results instanceof rowset_1.SQLiteCloudRowset) {\n callback === null || callback === void 0 ? void 0 : callback.call(this, null, results);\n }\n else {\n callback === null || callback === void 0 ? void 0 : callback.call(this, null);\n }\n }\n });\n return this;\n }\n each(sql, ...params) {\n // extract optional parameters and one or two callbacks\n const { args, callback, complete } = (0, utilities_1.popCallback)(params);\n const command = { query: sql, parameters: args };\n this.enqueueCommand(command, (error, rowset) => {\n if (error) {\n this.handleError(error, callback);\n }\n else {\n if (rowset && rowset instanceof rowset_1.SQLiteCloudRowset) {\n if (callback) {\n for (const row of rowset) {\n callback.call(this, null, row);\n }\n }\n if (complete) {\n ;\n complete.call(this, null, rowset.numberOfRows);\n }\n }\n else {\n callback === null || callback === void 0 ? void 0 : callback.call(this, new types_1.SQLiteCloudError('Invalid rowset'));\n }\n }\n });\n return this;\n }\n /**\n * Prepares the SQL statement and optionally binds the specified parameters and\n * calls the callback when done. The function returns a Statement object.\n * When preparing was successful, the first and only argument to the callback\n * is null, otherwise it is the error object. When bind parameters are supplied,\n * they are bound to the prepared statement before calling the callback.\n */\n prepare(sql, ...params) {\n return new statement_1.Statement(this, sql, ...params);\n }\n /**\n * Runs all SQL queries in the supplied string. No result rows are retrieved.\n * The function returns the Database object to allow for function chaining.\n * If a query fails, no subsequent statements will be executed (wrap it in a\n * transaction if you want all or none to be executed). When all statements\n * have been executed successfully, or when an error occurs, the callback\n * function is called, with the first parameter being either null or an error\n * object. When no callback is provided and an error occurs, an error event\n * will be emitted on the database object.\n */\n exec(sql, callback) {\n this.enqueueCommand(sql, (error, results) => {\n if (error) {\n this.handleError(error, callback);\n }\n else {\n const context = this.processContext(results);\n callback === null || callback === void 0 ? void 0 : callback.call(context ? context : this, null);\n }\n });\n return this;\n }\n /**\n * If the optional callback is provided, this function will be called when the\n * database was closed successfully or when an error occurred. The first argument\n * is an error object. When it is null, closing succeeded. If no callback is provided\n * and an error occurred, an error event with the error object as the only parameter\n * will be emitted on the database object. If closing succeeded, a close event with no\n * parameters is emitted, regardless of whether a callback was provided or not.\n */\n close(callback) {\n var _a;\n this.operations.clear();\n (_a = this.connection) === null || _a === void 0 ? void 0 : _a.close();\n callback === null || callback === void 0 ? void 0 : callback.call(this, null);\n this.emitEvent('close');\n }\n /**\n * Loads a compiled SQLite extension into the database connection object.\n * @param path Filename of the extension to load.\n * @param callback If provided, this function will be called when the extension\n * was loaded successfully or when an error occurred. The first argument is an\n * error object. When it is null, loading succeeded. If no callback is provided\n * and an error occurred, an error event with the error object as the only parameter\n * will be emitted on the database object.\n */\n loadExtension(_path, callback) {\n // TODO sqlitecloud-js / implement database loadExtension #17\n if (callback) {\n callback.call(this, new Error('Database.loadExtension - Not implemented'));\n }\n else {\n this.emitEvent('error', new Error('Database.loadExtension - Not implemented'));\n }\n return this;\n }\n /**\n * Allows the user to interrupt long-running queries. Wrapper around\n * sqlite3_interrupt and causes other data-fetching functions to be\n * passed an err with code = sqlite3.INTERRUPT. The database must be\n * open to use this function.\n */\n interrupt() {\n // TODO sqlitecloud-js / implement database interrupt #13\n }\n //\n // extended APIs\n //\n /**\n * Sql is a promise based API for executing SQL statements. You can\n * pass a simple string with a SQL statement or a template string\n * using backticks and parameters in ${parameter} format. These parameters\n * will be properly escaped and quoted like when using a prepared statement.\n * @param sql A sql string or a template string in `backticks` format\n * @returns An array of rows in case of selections or an object with\n * metadata in case of insert, update, delete.\n */\n sql(sql, ...values) {\n return __awaiter(this, void 0, void 0, function* () {\n let commands = { query: '' };\n // sql is a TemplateStringsArray, the 'raw' property is specific to TemplateStringsArray\n if (Array.isArray(sql) && 'raw' in sql) {\n let query = '';\n sql.forEach((string, i) => {\n // TemplateStringsArray splits the string before each variable\n // used in the template. Add the question mark\n // to the end of the string for the number of used variables.\n query += string + (i < values.length ? '?' : '');\n });\n commands = { query, parameters: values };\n }\n else if (typeof sql === 'string') {\n commands = { query: sql, parameters: values };\n }\n else if (typeof sql === 'object') {\n commands = sql;\n }\n else {\n throw new Error('Invalid sql');\n }\n return new Promise((resolve, reject) => {\n this.enqueueCommand(commands, (error, results) => {\n if (error) {\n reject(error);\n }\n else {\n // metadata for operations like insert, update, delete?\n const context = this.processContext(results);\n resolve(context ? context : results);\n }\n });\n });\n });\n }\n /**\n * Returns true if the database connection is open.\n */\n isConnected() {\n return this.connection != null && this.connection.connected;\n }\n /**\n * PubSub class provides a Pub/Sub real-time updates and notifications system to\n * allow multiple applications to communicate with each other asynchronously.\n * It allows applications to subscribe to tables and receive notifications whenever\n * data changes in the database table. It also enables sending messages to anyone\n * subscribed to a specific channel.\n * @returns {PubSub} A PubSub object\n */\n getPubSub() {\n return __awaiter(this, void 0, void 0, function* () {\n return new Promise((resolve, reject) => {\n this.operations.enqueue(done => {\n let error = null;\n try {\n if (!this.connection) {\n error = new types_1.SQLiteCloudError('Connection not established', { errorCode: 'ERR_CONNECTION_NOT_ESTABLISHED' });\n reject(error);\n }\n else {\n resolve(new pubsub_1.PubSub(this.connection));\n }\n }\n finally {\n done(error);\n }\n });\n });\n });\n }\n}\nexports.Database = Database;\n\n\n//# sourceURL=webpack://sqlitecloud/./lib/drivers/database.js?");
|
|
62
|
+
eval("\n//\n// database.ts - database driver api, implements and extends sqlite3\n//\nvar __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {\n if (k2 === undefined) k2 = k;\n var desc = Object.getOwnPropertyDescriptor(m, k);\n if (!desc || (\"get\" in desc ? !m.__esModule : desc.writable || desc.configurable)) {\n desc = { enumerable: true, get: function() { return m[k]; } };\n }\n Object.defineProperty(o, k2, desc);\n}) : (function(o, m, k, k2) {\n if (k2 === undefined) k2 = k;\n o[k2] = m[k];\n}));\nvar __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {\n Object.defineProperty(o, \"default\", { enumerable: true, value: v });\n}) : function(o, v) {\n o[\"default\"] = v;\n});\nvar __importStar = (this && this.__importStar) || (function () {\n var ownKeys = function(o) {\n ownKeys = Object.getOwnPropertyNames || function (o) {\n var ar = [];\n for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;\n return ar;\n };\n return ownKeys(o);\n };\n return function (mod) {\n if (mod && mod.__esModule) return mod;\n var result = {};\n if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== \"default\") __createBinding(result, mod, k[i]);\n __setModuleDefault(result, mod);\n return result;\n };\n})();\nvar __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {\n function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }\n return new (P || (P = Promise))(function (resolve, reject) {\n function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }\n function rejected(value) { try { step(generator[\"throw\"](value)); } catch (e) { reject(e); } }\n function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }\n step((generator = generator.apply(thisArg, _arguments || [])).next());\n });\n};\nvar __importDefault = (this && this.__importDefault) || function (mod) {\n return (mod && mod.__esModule) ? mod : { \"default\": mod };\n};\nObject.defineProperty(exports, \"__esModule\", ({ value: true }));\nexports.Database = void 0;\n// Trying as much as possible to be a drop-in replacement for SQLite3 API\n// https://github.com/TryGhost/node-sqlite3/wiki/API\n// https://github.com/TryGhost/node-sqlite3\n// https://github.com/TryGhost/node-sqlite3/blob/master/lib/sqlite3.d.ts\nconst eventemitter3_1 = __importDefault(__webpack_require__(/*! eventemitter3 */ \"./node_modules/eventemitter3/index.js\"));\nconst pubsub_1 = __webpack_require__(/*! ./pubsub */ \"./lib/drivers/pubsub.js\");\nconst queue_1 = __webpack_require__(/*! ./queue */ \"./lib/drivers/queue.js\");\nconst rowset_1 = __webpack_require__(/*! ./rowset */ \"./lib/drivers/rowset.js\");\nconst statement_1 = __webpack_require__(/*! ./statement */ \"./lib/drivers/statement.js\");\nconst types_1 = __webpack_require__(/*! ./types */ \"./lib/drivers/types.js\");\nconst utilities_1 = __webpack_require__(/*! ./utilities */ \"./lib/drivers/utilities.js\");\n// Uses eventemitter3 instead of node events for browser compatibility\n// https://github.com/primus/eventemitter3\n/**\n * Creating a Database object automatically opens a connection to the SQLite database.\n * When the connection is established the Database object emits an open event and calls\n * the optional provided callback. If the connection cannot be established an error event\n * will be emitted and the optional callback is called with the error information.\n */\nclass Database extends eventemitter3_1.default {\n constructor(config, mode, callback) {\n super();\n /** Used to syncronize opening of connection and commands */\n this.operations = new queue_1.OperationsQueue();\n this.config = typeof config === 'string' ? { connectionstring: config } : config;\n this.connection = null;\n // mode is optional and so is callback\n // https://github.com/TryGhost/node-sqlite3/wiki/API#new-sqlite3databasefilename--mode--callback\n if (typeof mode === 'function') {\n callback = mode;\n mode = undefined;\n }\n // mode is ignored for now\n // opens the connection to the database automatically\n this.createConnection(error => {\n if (callback) {\n callback.call(this, error);\n }\n });\n }\n //\n // private methods\n //\n /** Returns first available connection from connection pool */\n createConnection(callback) {\n var _a, _b;\n // connect using websocket if tls is not supported or if explicitly requested\n const useWebsocket = utilities_1.isBrowser || ((_a = this.config) === null || _a === void 0 ? void 0 : _a.usewebsocket) || ((_b = this.config) === null || _b === void 0 ? void 0 : _b.gatewayurl);\n if (useWebsocket) {\n // socket.io transport works in both node.js and browser environments and connects via SQLite Cloud Gateway\n this.operations.enqueue(done => {\n Promise.resolve().then(() => __importStar(__webpack_require__(/*! ./connection-ws */ \"./lib/drivers/connection-ws.js\"))).then(module => {\n this.connection = new module.default(this.config, (error) => {\n if (error) {\n this.handleError(error, callback);\n }\n else {\n callback === null || callback === void 0 ? void 0 : callback.call(this, null);\n this.emitEvent('open');\n }\n done(error);\n });\n })\n .catch(error => {\n this.handleError(error, callback);\n this.close();\n done(error);\n });\n });\n }\n else {\n this.operations.enqueue(done => {\n Promise.resolve().then(() => __importStar(__webpack_require__(/*! ./connection-tls */ \"./lib/drivers/connection-tls.js\"))).then(module => {\n this.connection = new module.default(this.config, (error) => {\n if (error) {\n this.handleError(error, callback);\n }\n else {\n callback === null || callback === void 0 ? void 0 : callback.call(this, null);\n this.emitEvent('open');\n }\n done(error);\n });\n })\n .catch(error => {\n this.handleError(error, callback);\n this.close();\n done(error);\n });\n });\n }\n }\n enqueueCommand(command, callback) {\n this.operations.enqueue(done => {\n let error = null;\n // we don't wont to silently open a new connection after a disconnession\n if (this.connection && this.connection.connected) {\n this.connection.sendCommands(command, (error, results) => {\n callback === null || callback === void 0 ? void 0 : callback.call(this, error, results);\n done(error);\n });\n }\n else {\n error = new types_1.SQLiteCloudError('Connection unavailable. Maybe it got disconnected?', { errorCode: 'ERR_CONNECTION_NOT_ESTABLISHED' });\n callback === null || callback === void 0 ? void 0 : callback.call(this, error, null);\n done(error);\n }\n });\n }\n /** Handles an error by closing the connection, calling the callback and/or emitting an error event */\n handleError(error, callback) {\n if (callback) {\n callback.call(this, error);\n }\n else {\n this.emitEvent('error', error);\n }\n }\n /**\n * Some queries like inserts or updates processed via run or exec may generate\n * an empty result (eg. no data was selected), but still have some metadata.\n * For example the server may pass the id of the last row that was modified.\n * In this case the callback results should be empty but the context may contain\n * additional information like lastID, etc.\n * @see https://github.com/TryGhost/node-sqlite3/wiki/API#runsql--param---callback\n * @param results Results received from the server\n * @returns A context object if one makes sense, otherwise undefined\n */\n processContext(results) {\n if (results) {\n if (Array.isArray(results) && results.length > 0) {\n switch (results[0]) {\n case types_1.SQLiteCloudArrayType.ARRAY_TYPE_SQLITE_EXEC:\n return {\n lastID: results[2], // ROWID (sqlite3_last_insert_rowid)\n changes: results[3], // CHANGES(sqlite3_changes)\n totalChanges: results[4], // TOTAL_CHANGES (sqlite3_total_changes)\n finalized: results[5] // FINALIZED\n };\n }\n }\n }\n return undefined;\n }\n /** Emits given event with optional arguments on the next tick so callbacks can complete first */\n emitEvent(event, ...args) {\n setTimeout(() => {\n this.emit(event, ...args);\n }, 0);\n }\n //\n // public methods\n //\n /**\n * Returns the configuration with which this database was opened.\n * The configuration is readonly and cannot be changed as there may\n * be multiple connections using the same configuration.\n * @returns {SQLiteCloudConfig} A configuration object\n */\n getConfiguration() {\n return JSON.parse(JSON.stringify(this.config));\n }\n /** Enable verbose mode */\n verbose() {\n var _a;\n this.config.verbose = true;\n (_a = this.connection) === null || _a === void 0 ? void 0 : _a.verbose();\n return this;\n }\n /** Set a configuration option for the database */\n configure(_option, _value) {\n // https://github.com/TryGhost/node-sqlite3/wiki/API#configureoption-value\n return this;\n }\n run(sql, ...params) {\n const { args, callback } = (0, utilities_1.popCallback)(params);\n const command = { query: sql, parameters: args };\n this.enqueueCommand(command, (error, results) => {\n if (error) {\n this.handleError(error, callback);\n }\n else {\n // context may include id of last row inserted, total changes, etc...\n const context = this.processContext(results);\n callback === null || callback === void 0 ? void 0 : callback.call(context || this, null, context ? context : results);\n }\n });\n return this;\n }\n get(sql, ...params) {\n const { args, callback } = (0, utilities_1.popCallback)(params);\n const command = { query: sql, parameters: args };\n this.enqueueCommand(command, (error, results) => {\n if (error) {\n this.handleError(error, callback);\n }\n else {\n if (results && results instanceof rowset_1.SQLiteCloudRowset && results.length > 0) {\n callback === null || callback === void 0 ? void 0 : callback.call(this, null, results[0]);\n }\n else {\n callback === null || callback === void 0 ? void 0 : callback.call(this, null);\n }\n }\n });\n return this;\n }\n all(sql, ...params) {\n const { args, callback } = (0, utilities_1.popCallback)(params);\n const command = { query: sql, parameters: args };\n this.enqueueCommand(command, (error, results) => {\n if (error) {\n this.handleError(error, callback);\n }\n else {\n if (results && results instanceof rowset_1.SQLiteCloudRowset) {\n callback === null || callback === void 0 ? void 0 : callback.call(this, null, results);\n }\n else {\n callback === null || callback === void 0 ? void 0 : callback.call(this, null);\n }\n }\n });\n return this;\n }\n each(sql, ...params) {\n // extract optional parameters and one or two callbacks\n const { args, callback, complete } = (0, utilities_1.popCallback)(params);\n const command = { query: sql, parameters: args };\n this.enqueueCommand(command, (error, rowset) => {\n if (error) {\n this.handleError(error, callback);\n }\n else {\n if (rowset && rowset instanceof rowset_1.SQLiteCloudRowset) {\n if (callback) {\n for (const row of rowset) {\n callback.call(this, null, row);\n }\n }\n if (complete) {\n ;\n complete.call(this, null, rowset.numberOfRows);\n }\n }\n else {\n callback === null || callback === void 0 ? void 0 : callback.call(this, new types_1.SQLiteCloudError('Invalid rowset'));\n }\n }\n });\n return this;\n }\n /**\n * Prepares the SQL statement and optionally binds the specified parameters and\n * calls the callback when done. The function returns a Statement object.\n * When preparing was successful, the first and only argument to the callback\n * is null, otherwise it is the error object. When bind parameters are supplied,\n * they are bound to the prepared statement before calling the callback.\n */\n prepare(sql, ...params) {\n return new statement_1.Statement(this, sql, ...params);\n }\n /**\n * Runs all SQL queries in the supplied string. No result rows are retrieved.\n * The function returns the Database object to allow for function chaining.\n * If a query fails, no subsequent statements will be executed (wrap it in a\n * transaction if you want all or none to be executed). When all statements\n * have been executed successfully, or when an error occurs, the callback\n * function is called, with the first parameter being either null or an error\n * object. When no callback is provided and an error occurs, an error event\n * will be emitted on the database object.\n */\n exec(sql, callback) {\n this.enqueueCommand(sql, (error, results) => {\n if (error) {\n this.handleError(error, callback);\n }\n else {\n const context = this.processContext(results);\n callback === null || callback === void 0 ? void 0 : callback.call(context ? context : this, null);\n }\n });\n return this;\n }\n /**\n * If the optional callback is provided, this function will be called when the\n * database was closed successfully or when an error occurred. The first argument\n * is an error object. When it is null, closing succeeded. If no callback is provided\n * and an error occurred, an error event with the error object as the only parameter\n * will be emitted on the database object. If closing succeeded, a close event with no\n * parameters is emitted, regardless of whether a callback was provided or not.\n */\n close(callback) {\n this.operations.enqueue(done => {\n var _a;\n (_a = this.connection) === null || _a === void 0 ? void 0 : _a.close();\n callback === null || callback === void 0 ? void 0 : callback.call(this, null);\n this.emitEvent('close');\n this.operations.clear();\n done(null);\n });\n }\n /**\n * Loads a compiled SQLite extension into the database connection object.\n * @param path Filename of the extension to load.\n * @param callback If provided, this function will be called when the extension\n * was loaded successfully or when an error occurred. The first argument is an\n * error object. When it is null, loading succeeded. If no callback is provided\n * and an error occurred, an error event with the error object as the only parameter\n * will be emitted on the database object.\n */\n loadExtension(_path, callback) {\n // TODO sqlitecloud-js / implement database loadExtension #17\n if (callback) {\n callback.call(this, new Error('Database.loadExtension - Not implemented'));\n }\n else {\n this.emitEvent('error', new Error('Database.loadExtension - Not implemented'));\n }\n return this;\n }\n /**\n * Allows the user to interrupt long-running queries. Wrapper around\n * sqlite3_interrupt and causes other data-fetching functions to be\n * passed an err with code = sqlite3.INTERRUPT. The database must be\n * open to use this function.\n */\n interrupt() {\n // TODO sqlitecloud-js / implement database interrupt #13\n }\n //\n // extended APIs\n //\n /**\n * Sql is a promise based API for executing SQL statements. You can\n * pass a simple string with a SQL statement or a template string\n * using backticks and parameters in ${parameter} format. These parameters\n * will be properly escaped and quoted like when using a prepared statement.\n * @param sql A sql string or a template string in `backticks` format\n * @returns An array of rows in case of selections or an object with\n * metadata in case of insert, update, delete.\n */\n sql(sql, ...values) {\n return __awaiter(this, void 0, void 0, function* () {\n let commands = { query: '' };\n // sql is a TemplateStringsArray, the 'raw' property is specific to TemplateStringsArray\n if (Array.isArray(sql) && 'raw' in sql) {\n let query = '';\n sql.forEach((string, i) => {\n // TemplateStringsArray splits the string before each variable\n // used in the template. Add the question mark\n // to the end of the string for the number of used variables.\n query += string + (i < values.length ? '?' : '');\n });\n commands = { query, parameters: values };\n }\n else if (typeof sql === 'string') {\n commands = { query: sql, parameters: values };\n }\n else if (typeof sql === 'object') {\n commands = sql;\n }\n else {\n throw new Error('Invalid sql');\n }\n return new Promise((resolve, reject) => {\n this.enqueueCommand(commands, (error, results) => {\n if (error) {\n reject(error);\n }\n else {\n // metadata for operations like insert, update, delete?\n const context = this.processContext(results);\n resolve(context ? context : results);\n }\n });\n });\n });\n }\n /**\n * Returns true if the database connection is open.\n */\n isConnected() {\n return this.connection != null && this.connection.connected;\n }\n /**\n * PubSub class provides a Pub/Sub real-time updates and notifications system to\n * allow multiple applications to communicate with each other asynchronously.\n * It allows applications to subscribe to tables and receive notifications whenever\n * data changes in the database table. It also enables sending messages to anyone\n * subscribed to a specific channel.\n * @returns {PubSub} A PubSub object\n */\n getPubSub() {\n return __awaiter(this, void 0, void 0, function* () {\n return new Promise((resolve, reject) => {\n this.operations.enqueue(done => {\n let error = null;\n try {\n if (!this.connection) {\n error = new types_1.SQLiteCloudError('Connection not established', { errorCode: 'ERR_CONNECTION_NOT_ESTABLISHED' });\n reject(error);\n }\n else {\n resolve(new pubsub_1.PubSub(this.connection));\n }\n }\n finally {\n done(error);\n }\n });\n });\n });\n }\n}\nexports.Database = Database;\n\n\n//# sourceURL=webpack://sqlitecloud/./lib/drivers/database.js?");
|
|
63
63
|
|
|
64
64
|
/***/ }),
|
|
65
65
|
|
|
@@ -136,7 +136,7 @@ eval("\n/**\n * types.ts - shared types and interfaces\n */\nObject.defineProper
|
|
|
136
136
|
/***/ ((__unused_webpack_module, exports, __webpack_require__) => {
|
|
137
137
|
|
|
138
138
|
"use strict";
|
|
139
|
-
eval("\n//\n// utilities.ts - utility methods to manipulate SQL statements\n//\nObject.defineProperty(exports, \"__esModule\", ({ value: true }));\nexports.isNode = exports.isBrowser = void 0;\nexports.anonimizeCommand = anonimizeCommand;\nexports.anonimizeError = anonimizeError;\nexports.getInitializationCommands = getInitializationCommands;\nexports.sanitizeSQLiteIdentifier = sanitizeSQLiteIdentifier;\nexports.getUpdateResults = getUpdateResults;\nexports.popCallback = popCallback;\nexports.validateConfiguration = validateConfiguration;\nexports.parseconnectionstring = parseconnectionstring;\nexports.parseBoolean = parseBoolean;\nexports.parseBooleanToZeroOne = parseBooleanToZeroOne;\nconst types_1 = __webpack_require__(/*! ./types */ \"./lib/drivers/types.js\");\nconst types_2 = __webpack_require__(/*! ./types */ \"./lib/drivers/types.js\");\n// explicitly importing these libraries to allow cross-platform support by replacing them\nconst whatwg_url_1 = __webpack_require__(/*! whatwg-url */ \"./node_modules/whatwg-url/index.js\");\n//\n// determining running environment, thanks to browser-or-node\n// https://www.npmjs.com/package/browser-or-node\n//\nexports.isBrowser = typeof window !== 'undefined' && typeof window.document !== 'undefined';\nexports.isNode = typeof process !== 'undefined' && process.versions != null && process.versions.node != null;\n//\n// utility methods\n//\n/** Messages going to the server are sometimes logged when error conditions occour and need to be stripped of user credentials */\nfunction anonimizeCommand(message) {\n // hide password in AUTH command if needed\n message = message.replace(/USER \\S+/, 'USER ******');\n message = message.replace(/PASSWORD \\S+?(?=;)/, 'PASSWORD ******');\n message = message.replace(/HASH \\S+?(?=;)/, 'HASH ******');\n return message;\n}\n/** Strip message code in error of user credentials */\nfunction anonimizeError(error) {\n if (error === null || error === void 0 ? void 0 : error.message) {\n error.message = anonimizeCommand(error.message);\n }\n return error;\n}\n/** Initialization commands sent to database when connection is established */\nfunction getInitializationCommands(config) {\n // we check the credentials using non linearizable so we're quicker\n // then we bring back linearizability unless specified otherwise\n let commands = 'SET CLIENT KEY NONLINEARIZABLE TO 1; ';\n // first user authentication, then all other commands\n if (config.apikey) {\n commands += `AUTH APIKEY ${config.apikey}; `;\n }\n else {\n commands += `AUTH USER ${config.username || ''} ${config.password_hashed ? 'HASH' : 'PASSWORD'} ${config.password || ''}; `;\n }\n if (config.compression) {\n commands += 'SET CLIENT KEY COMPRESSION TO 1; ';\n }\n if (config.zerotext) {\n commands += 'SET CLIENT KEY ZEROTEXT TO 1; ';\n }\n if (config.noblob) {\n commands += 'SET CLIENT KEY NOBLOB TO 1; ';\n }\n if (config.maxdata) {\n commands += `SET CLIENT KEY MAXDATA TO ${config.maxdata}; `;\n }\n if (config.maxrows) {\n commands += `SET CLIENT KEY MAXROWS TO ${config.maxrows}; `;\n }\n if (config.maxrowset) {\n commands += `SET CLIENT KEY MAXROWSET TO ${config.maxrowset}; `;\n }\n // we ALWAYS set non linearizable to 1 when we start so we can be quicker on login\n // but then we need to put it back to its default value if \"linearizable\" unless set\n if (!config.non_linearizable) {\n commands += 'SET CLIENT KEY NONLINEARIZABLE TO 0; ';\n }\n if (config.database) {\n if (config.create && !config.memory) {\n commands += `CREATE DATABASE ${config.database} IF NOT EXISTS; `;\n }\n commands += `USE DATABASE ${config.database}; `;\n }\n return commands;\n}\n/** Sanitizes an SQLite identifier (e.g., table name, column name). */\nfunction sanitizeSQLiteIdentifier(identifier) {\n const trimmed = identifier.trim();\n // it's not empty\n if (trimmed.length === 0) {\n throw new Error('Identifier cannot be empty.');\n }\n // escape double quotes\n const escaped = trimmed.replace(/\"/g, '\"\"');\n // Wrap in double quotes for safety\n return `\"${escaped}\"`;\n}\n/** Converts results of an update or insert call into a more meaning full result set */\nfunction getUpdateResults(results) {\n if (results) {\n if (Array.isArray(results) && results.length > 0) {\n switch (results[0]) {\n case types_2.SQLiteCloudArrayType.ARRAY_TYPE_SQLITE_EXEC:\n return {\n type: results[0],\n index: results[1],\n lastID: results[2], // ROWID (sqlite3_last_insert_rowid)\n changes: results[3], // CHANGES(sqlite3_changes)\n totalChanges: results[4], // TOTAL_CHANGES (sqlite3_total_changes)\n finalized: results[5], // FINALIZED\n //\n rowId: results[2] // same as lastId\n };\n }\n }\n }\n return undefined;\n}\n/**\n * Many of the methods in our API may contain a callback as their last argument.\n * This method will take the arguments array passed to the method and return an object\n * containing the arguments array with the callbacks removed (if any), and the callback itself.\n * If there are multiple callbacks, the first one is returned as 'callback' and the last one\n * as 'completeCallback'.\n *\n * @returns args is a simple list of SQLiteCloudDataTypes, we flat them into a single array\n */\nfunction popCallback(args) {\n const remaining = args;\n // at least 1 callback?\n if (args && args.length > 0 && typeof args[args.length - 1] === 'function') {\n // at least 2 callbacks?\n if (args.length > 1 && typeof args[args.length - 2] === 'function') {\n return { args: remaining.slice(0, -2).flat(), callback: args[args.length - 2], complete: args[args.length - 1] };\n }\n return { args: remaining.slice(0, -1).flat(), callback: args[args.length - 1] };\n }\n return { args: remaining.flat() };\n}\n//\n// configuration validation\n//\n/** Validate configuration, apply defaults, throw if something is missing or misconfigured */\nfunction validateConfiguration(config) {\n console.assert(config, 'SQLiteCloudConnection.validateConfiguration - missing config');\n if (config.connectionstring) {\n config = Object.assign(Object.assign(Object.assign({}, config), parseconnectionstring(config.connectionstring)), { connectionstring: config.connectionstring // keep original connection string\n });\n }\n // apply defaults where needed\n config.port || (config.port = types_1.DEFAULT_PORT);\n config.timeout = config.timeout && config.timeout > 0 ? config.timeout : types_1.DEFAULT_TIMEOUT;\n config.clientid || (config.clientid = 'SQLiteCloud');\n config.verbose = parseBoolean(config.verbose);\n config.noblob = parseBoolean(config.noblob);\n config.compression = config.compression != undefined && config.compression != null ? parseBoolean(config.compression) : true; // default: true\n config.create = parseBoolean(config.create);\n config.non_linearizable = parseBoolean(config.non_linearizable);\n config.insecure = parseBoolean(config.insecure);\n const hasCredentials = (config.username && config.password) || config.apikey;\n if (!config.host || !hasCredentials) {\n console.error('SQLiteCloudConnection.validateConfiguration - missing arguments', config);\n throw new types_1.SQLiteCloudError('The user, password and host arguments or the ?apikey= must be specified.', { errorCode: 'ERR_MISSING_ARGS' });\n }\n if (!config.connectionstring) {\n // build connection string from configuration, values are already validated\n if (config.apikey) {\n config.connectionstring = `sqlitecloud://${config.host}:${config.port}/${config.database || ''}?apikey=${config.apikey}`;\n }\n else {\n config.connectionstring = `sqlitecloud://${encodeURIComponent(config.username || '')}:${encodeURIComponent(config.password || '')}@${config.host}:${config.port}/${config.database}`;\n }\n }\n return config;\n}\n/**\n * Parse connectionstring like sqlitecloud://username:password@host:port/database?option1=xxx&option2=xxx\n * or sqlitecloud://host.sqlite.cloud:8860/chinook.sqlite?apikey=mIiLARzKm9XBVllbAzkB1wqrgijJ3Gx0X5z1Agm3xBo\n * into its basic components.\n */\nfunction parseconnectionstring(connectionstring) {\n try {\n // The URL constructor throws a TypeError if the URL is not valid.\n // in spite of having the same structure as a regular url\n // protocol://username:password@host:port/database?option1=xxx&option2=xxx)\n // the sqlitecloud: protocol is not recognized by the URL constructor in browsers\n // so we need to replace it with https: to make it work\n const knownProtocolUrl = connectionstring.replace('sqlitecloud:', 'https:');\n const url = new whatwg_url_1.URL(knownProtocolUrl);\n // all lowecase options\n const options = {};\n url.searchParams.forEach((value, key) => {\n options[key.toLowerCase().replace(/-/g, '_')] = value;\n });\n const config = Object.assign({ username: decodeURIComponent(url.username), password: decodeURIComponent(url.password), host: url.hostname, port: url.port ? parseInt(url.port) : undefined }, options);\n // either you use an apikey or username and password\n if (config.apikey) {\n if (config.username || config.password) {\n console.warn('SQLiteCloudConnection.parseconnectionstring - apikey and username/password are both specified, using apikey');\n }\n delete config.username;\n delete config.password;\n }\n const database = url.pathname.replace('/', ''); // pathname is database name, remove the leading slash\n if (database) {\n config.database = database;\n }\n return config;\n }\n catch (error) {\n throw new types_1.SQLiteCloudError(`Invalid connection string: ${connectionstring}`);\n }\n}\n/** Returns true if value is 1 or true */\nfunction parseBoolean(value) {\n if (typeof value === 'string') {\n return value.toLowerCase() === 'true' || value === '1';\n }\n return value ? true : false;\n}\n/** Returns true if value is 1 or true */\nfunction parseBooleanToZeroOne(value) {\n if (typeof value === 'string') {\n return value.toLowerCase() === 'true' || value === '1' ? 1 : 0;\n }\n return value ? 1 : 0;\n}\n\n\n//# sourceURL=webpack://sqlitecloud/./lib/drivers/utilities.js?");
|
|
139
|
+
eval("\n//\n// utilities.ts - utility methods to manipulate SQL statements\n//\nObject.defineProperty(exports, \"__esModule\", ({ value: true }));\nexports.isNode = exports.isBrowser = void 0;\nexports.anonimizeCommand = anonimizeCommand;\nexports.anonimizeError = anonimizeError;\nexports.getInitializationCommands = getInitializationCommands;\nexports.sanitizeSQLiteIdentifier = sanitizeSQLiteIdentifier;\nexports.getUpdateResults = getUpdateResults;\nexports.popCallback = popCallback;\nexports.validateConfiguration = validateConfiguration;\nexports.parseconnectionstring = parseconnectionstring;\nexports.parseBoolean = parseBoolean;\nexports.parseBooleanToZeroOne = parseBooleanToZeroOne;\nconst types_1 = __webpack_require__(/*! ./types */ \"./lib/drivers/types.js\");\nconst types_2 = __webpack_require__(/*! ./types */ \"./lib/drivers/types.js\");\n// explicitly importing these libraries to allow cross-platform support by replacing them\nconst whatwg_url_1 = __webpack_require__(/*! whatwg-url */ \"./node_modules/whatwg-url/index.js\");\n//\n// determining running environment, thanks to browser-or-node\n// https://www.npmjs.com/package/browser-or-node\n//\nexports.isBrowser = typeof window !== 'undefined' && typeof window.document !== 'undefined';\nexports.isNode = typeof process !== 'undefined' && process.versions != null && process.versions.node != null;\n//\n// utility methods\n//\n/** Messages going to the server are sometimes logged when error conditions occour and need to be stripped of user credentials */\nfunction anonimizeCommand(message) {\n // hide password in AUTH command if needed\n message = message.replace(/USER \\S+/, 'USER ******');\n message = message.replace(/PASSWORD \\S+?(?=;)/, 'PASSWORD ******');\n message = message.replace(/HASH \\S+?(?=;)/, 'HASH ******');\n return message;\n}\n/** Strip message code in error of user credentials */\nfunction anonimizeError(error) {\n if (error === null || error === void 0 ? void 0 : error.message) {\n error.message = anonimizeCommand(error.message);\n }\n return error;\n}\n/** Initialization commands sent to database when connection is established */\nfunction getInitializationCommands(config) {\n // we check the credentials using non linearizable so we're quicker\n // then we bring back linearizability unless specified otherwise\n let commands = 'SET CLIENT KEY NONLINEARIZABLE TO 1; ';\n // first user authentication, then all other commands\n if (config.apikey) {\n commands += `AUTH APIKEY ${config.apikey}; `;\n }\n else {\n commands += `AUTH USER ${config.username || ''} ${config.password_hashed ? 'HASH' : 'PASSWORD'} ${config.password || ''}; `;\n }\n if (config.compression) {\n commands += 'SET CLIENT KEY COMPRESSION TO 1; ';\n }\n if (config.zerotext) {\n commands += 'SET CLIENT KEY ZEROTEXT TO 1; ';\n }\n if (config.noblob) {\n commands += 'SET CLIENT KEY NOBLOB TO 1; ';\n }\n if (config.maxdata) {\n commands += `SET CLIENT KEY MAXDATA TO ${config.maxdata}; `;\n }\n if (config.maxrows) {\n commands += `SET CLIENT KEY MAXROWS TO ${config.maxrows}; `;\n }\n if (config.maxrowset) {\n commands += `SET CLIENT KEY MAXROWSET TO ${config.maxrowset}; `;\n }\n // we ALWAYS set non linearizable to 1 when we start so we can be quicker on login\n // but then we need to put it back to its default value if \"linearizable\" unless set\n if (!config.non_linearizable) {\n commands += 'SET CLIENT KEY NONLINEARIZABLE TO 0; ';\n }\n if (config.database) {\n if (config.create && !config.memory) {\n commands += `CREATE DATABASE ${config.database} IF NOT EXISTS; `;\n }\n commands += `USE DATABASE ${config.database}; `;\n }\n return commands;\n}\n/** Sanitizes an SQLite identifier (e.g., table name, column name). */\nfunction sanitizeSQLiteIdentifier(identifier) {\n const trimmed = identifier.trim();\n // it's not empty\n if (trimmed.length === 0) {\n throw new Error('Identifier cannot be empty.');\n }\n // escape double quotes\n const escaped = trimmed.replace(/\"/g, '\"\"');\n // Wrap in double quotes for safety\n return `\"${escaped}\"`;\n}\n/** Converts results of an update or insert call into a more meaning full result set */\nfunction getUpdateResults(results) {\n if (results) {\n if (Array.isArray(results) && results.length > 0) {\n switch (results[0]) {\n case types_2.SQLiteCloudArrayType.ARRAY_TYPE_SQLITE_EXEC:\n return {\n type: results[0],\n index: results[1],\n lastID: results[2], // ROWID (sqlite3_last_insert_rowid)\n changes: results[3], // CHANGES(sqlite3_changes)\n totalChanges: results[4], // TOTAL_CHANGES (sqlite3_total_changes)\n finalized: results[5], // FINALIZED\n //\n rowId: results[2] // same as lastId\n };\n }\n }\n }\n return undefined;\n}\n/**\n * Many of the methods in our API may contain a callback as their last argument.\n * This method will take the arguments array passed to the method and return an object\n * containing the arguments array with the callbacks removed (if any), and the callback itself.\n * If there are multiple callbacks, the first one is returned as 'callback' and the last one\n * as 'completeCallback'.\n *\n * @returns args is a simple list of SQLiteCloudDataTypes, we flat them into a single array\n */\nfunction popCallback(args) {\n const remaining = args;\n // at least 1 callback?\n if (args && args.length > 0 && typeof args[args.length - 1] === 'function') {\n // at least 2 callbacks?\n if (args.length > 1 && typeof args[args.length - 2] === 'function') {\n return { args: remaining.slice(0, -2).flat(), callback: args[args.length - 2], complete: args[args.length - 1] };\n }\n return { args: remaining.slice(0, -1).flat(), callback: args[args.length - 1] };\n }\n return { args: remaining.flat() };\n}\n//\n// configuration validation\n//\n/** Validate configuration, apply defaults, throw if something is missing or misconfigured */\nfunction validateConfiguration(config) {\n console.assert(config, 'SQLiteCloudConnection.validateConfiguration - missing config');\n if (config.connectionstring) {\n config = Object.assign(Object.assign(Object.assign({}, config), parseconnectionstring(config.connectionstring)), { connectionstring: config.connectionstring // keep original connection string\n });\n }\n // apply defaults where needed\n config.port || (config.port = types_1.DEFAULT_PORT);\n config.timeout = config.timeout && config.timeout > 0 ? config.timeout : types_1.DEFAULT_TIMEOUT;\n config.clientid || (config.clientid = 'SQLiteCloud');\n config.verbose = parseBoolean(config.verbose);\n config.noblob = parseBoolean(config.noblob);\n config.compression = config.compression != undefined && config.compression != null ? parseBoolean(config.compression) : true; // default: true\n config.create = parseBoolean(config.create);\n config.non_linearizable = parseBoolean(config.non_linearizable);\n config.insecure = parseBoolean(config.insecure);\n const hasCredentials = (config.username && config.password) || config.apikey;\n if (!config.host || !hasCredentials) {\n console.error('SQLiteCloudConnection.validateConfiguration - missing arguments', config);\n throw new types_1.SQLiteCloudError('The user, password and host arguments or the ?apikey= must be specified.', { errorCode: 'ERR_MISSING_ARGS' });\n }\n if (!config.connectionstring) {\n // build connection string from configuration, values are already validated\n if (config.apikey) {\n config.connectionstring = `sqlitecloud://${config.host}:${config.port}/${config.database || ''}?apikey=${config.apikey}`;\n }\n else {\n config.connectionstring = `sqlitecloud://${encodeURIComponent(config.username || '')}:${encodeURIComponent(config.password || '')}@${config.host}:${config.port}/${config.database}`;\n }\n }\n return config;\n}\n/**\n * Parse connectionstring like sqlitecloud://username:password@host:port/database?option1=xxx&option2=xxx\n * or sqlitecloud://host.sqlite.cloud:8860/chinook.sqlite?apikey=mIiLARzKm9XBVllbAzkB1wqrgijJ3Gx0X5z1Agm3xBo\n * into its basic components.\n */\nfunction parseconnectionstring(connectionstring) {\n try {\n // The URL constructor throws a TypeError if the URL is not valid.\n // in spite of having the same structure as a regular url\n // protocol://username:password@host:port/database?option1=xxx&option2=xxx)\n // the sqlitecloud: protocol is not recognized by the URL constructor in browsers\n // so we need to replace it with https: to make it work\n const knownProtocolUrl = connectionstring.replace('sqlitecloud:', 'https:');\n const url = new whatwg_url_1.URL(knownProtocolUrl);\n // all lowecase options\n const options = {};\n url.searchParams.forEach((value, key) => {\n options[key.toLowerCase().replace(/-/g, '_')] = value;\n });\n const config = Object.assign(Object.assign({}, options), { username: decodeURIComponent(url.username), password: decodeURIComponent(url.password), password_hashed: options.password_hashed ? parseBoolean(options.password_hashed) : undefined, host: url.hostname, \n // type cast values\n port: url.port ? parseInt(url.port) : undefined, insecure: options.insecure ? parseBoolean(options.insecure) : undefined, timeout: options.timeout ? parseInt(options.timeout) : undefined, zerotext: options.zerotext ? parseBoolean(options.zerotext) : undefined, create: options.create ? parseBoolean(options.create) : undefined, memory: options.memory ? parseBoolean(options.memory) : undefined, compression: options.compression ? parseBoolean(options.compression) : undefined, non_linearizable: options.non_linearizable ? parseBoolean(options.non_linearizable) : undefined, noblob: options.noblob ? parseBoolean(options.noblob) : undefined, maxdata: options.maxdata ? parseInt(options.maxdata) : undefined, maxrows: options.maxrows ? parseInt(options.maxrows) : undefined, maxrowset: options.maxrowset ? parseInt(options.maxrowset) : undefined, usewebsocket: options.usewebsocket ? parseBoolean(options.usewebsocket) : undefined, verbose: options.verbose ? parseBoolean(options.verbose) : undefined });\n // either you use an apikey or username and password\n if (config.apikey) {\n if (config.username || config.password) {\n console.warn('SQLiteCloudConnection.parseconnectionstring - apikey and username/password are both specified, using apikey');\n }\n delete config.username;\n delete config.password;\n }\n const database = url.pathname.replace('/', ''); // pathname is database name, remove the leading slash\n if (database) {\n config.database = database;\n }\n return config;\n }\n catch (error) {\n throw new types_1.SQLiteCloudError(`Invalid connection string: ${connectionstring}`);\n }\n}\n/** Returns true if value is 1 or true */\nfunction parseBoolean(value) {\n if (typeof value === 'string') {\n return value.toLowerCase() === 'true' || value === '1';\n }\n return value ? true : false;\n}\n/** Returns true if value is 1 or true */\nfunction parseBooleanToZeroOne(value) {\n if (typeof value === 'string') {\n return value.toLowerCase() === 'true' || value === '1' ? 1 : 0;\n }\n return value ? 1 : 0;\n}\n\n\n//# sourceURL=webpack://sqlitecloud/./lib/drivers/utilities.js?");
|
|
140
140
|
|
|
141
141
|
/***/ }),
|
|
142
142
|
|