@sqlitecloud/drivers 0.0.34

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/types.js ADDED
@@ -0,0 +1,38 @@
1
+ "use strict";
2
+ /**
3
+ * types.ts - shared types and interfaces
4
+ */
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.SQLiteCloudArrayType = exports.SQLiteCloudError = void 0;
7
+ /** Custom error reported by SQLiteCloud drivers */
8
+ class SQLiteCloudError extends Error {
9
+ constructor(message, args) {
10
+ super(message);
11
+ this.name = 'SQLiteCloudError';
12
+ if (args) {
13
+ Object.assign(this, args);
14
+ }
15
+ }
16
+ }
17
+ exports.SQLiteCloudError = SQLiteCloudError;
18
+ /**
19
+ * Certain responses include arrays with various types of metadata.
20
+ * The first entry is always an array type from this list. This enum
21
+ * is called SQCLOUD_ARRAY_TYPE in the C API.
22
+ */
23
+ var SQLiteCloudArrayType;
24
+ (function (SQLiteCloudArrayType) {
25
+ SQLiteCloudArrayType[SQLiteCloudArrayType["ARRAY_TYPE_SQLITE_EXEC"] = 10] = "ARRAY_TYPE_SQLITE_EXEC";
26
+ SQLiteCloudArrayType[SQLiteCloudArrayType["ARRAY_TYPE_DB_STATUS"] = 11] = "ARRAY_TYPE_DB_STATUS";
27
+ SQLiteCloudArrayType[SQLiteCloudArrayType["ARRAY_TYPE_METADATA"] = 12] = "ARRAY_TYPE_METADATA";
28
+ SQLiteCloudArrayType[SQLiteCloudArrayType["ARRAY_TYPE_VM_STEP"] = 20] = "ARRAY_TYPE_VM_STEP";
29
+ SQLiteCloudArrayType[SQLiteCloudArrayType["ARRAY_TYPE_VM_COMPILE"] = 21] = "ARRAY_TYPE_VM_COMPILE";
30
+ SQLiteCloudArrayType[SQLiteCloudArrayType["ARRAY_TYPE_VM_STEP_ONE"] = 22] = "ARRAY_TYPE_VM_STEP_ONE";
31
+ SQLiteCloudArrayType[SQLiteCloudArrayType["ARRAY_TYPE_VM_SQL"] = 23] = "ARRAY_TYPE_VM_SQL";
32
+ SQLiteCloudArrayType[SQLiteCloudArrayType["ARRAY_TYPE_VM_STATUS"] = 24] = "ARRAY_TYPE_VM_STATUS";
33
+ SQLiteCloudArrayType[SQLiteCloudArrayType["ARRAY_TYPE_VM_LIST"] = 25] = "ARRAY_TYPE_VM_LIST";
34
+ SQLiteCloudArrayType[SQLiteCloudArrayType["ARRAY_TYPE_BACKUP_INIT"] = 40] = "ARRAY_TYPE_BACKUP_INIT";
35
+ SQLiteCloudArrayType[SQLiteCloudArrayType["ARRAY_TYPE_BACKUP_STEP"] = 41] = "ARRAY_TYPE_BACKUP_STEP";
36
+ SQLiteCloudArrayType[SQLiteCloudArrayType["ARRAY_TYPE_BACKUP_END"] = 42] = "ARRAY_TYPE_BACKUP_END";
37
+ SQLiteCloudArrayType[SQLiteCloudArrayType["ARRAY_TYPE_SQLITE_STATUS"] = 50] = "ARRAY_TYPE_SQLITE_STATUS"; // used in sqlite_status
38
+ })(SQLiteCloudArrayType = exports.SQLiteCloudArrayType || (exports.SQLiteCloudArrayType = {}));
@@ -0,0 +1,23 @@
1
+ import { SQLiteCloudConfig, SQLiteCloudDataTypes } from './types';
2
+ export declare const isBrowser: boolean;
3
+ export declare const isNode: boolean;
4
+ /** Takes a generic value and escapes it so it can replace ? as a binding in a prepared SQL statement */
5
+ export declare function escapeSqlParameter(param: SQLiteCloudDataTypes): string;
6
+ /** Take a sql statement and replaces ? or $named parameters that are properly serialized and escaped. */
7
+ export declare function prepareSql(sql: string, ...params: (SQLiteCloudDataTypes | SQLiteCloudDataTypes[])[]): string;
8
+ /**
9
+ * Many of the methods in our API may contain a callback as their last argument.
10
+ * This method will take the arguments array passed to the method and return an object
11
+ * containing the arguments array with the callbacks removed (if any), and the callback itself.
12
+ * If there are multiple callbacks, the first one is returned as 'callback' and the last one
13
+ * as 'completeCallback'.
14
+ */
15
+ export declare function popCallback<T extends ErrorCallback = ErrorCallback>(args: (SQLiteCloudDataTypes | T | ErrorCallback)[]): {
16
+ args: SQLiteCloudDataTypes[];
17
+ callback?: T | undefined;
18
+ complete?: ErrorCallback;
19
+ };
20
+ /** Parse connectionString like sqlitecloud://username:password@host:port/database?option1=xxx&option2=xxx into its components */
21
+ export declare function parseConnectionString(connectionString: string): SQLiteCloudConfig;
22
+ /** Returns true if value is 1 or true */
23
+ export declare function parseBoolean(value: string | boolean | null | undefined): boolean;
@@ -0,0 +1,136 @@
1
+ "use strict";
2
+ //
3
+ // utilities.ts - utility methods to manipulate SQL statements
4
+ //
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.parseBoolean = exports.parseConnectionString = exports.popCallback = exports.prepareSql = exports.escapeSqlParameter = exports.isNode = exports.isBrowser = void 0;
7
+ const types_1 = require("./types");
8
+ //
9
+ // determining running environment, thanks to browser-or-node
10
+ // https://www.npmjs.com/package/browser-or-node
11
+ //
12
+ exports.isBrowser = typeof window !== 'undefined' && typeof window.document !== 'undefined';
13
+ exports.isNode = typeof process !== 'undefined' && process.versions != null && process.versions.node != null;
14
+ //
15
+ // utility methods
16
+ //
17
+ /** Takes a generic value and escapes it so it can replace ? as a binding in a prepared SQL statement */
18
+ function escapeSqlParameter(param) {
19
+ if (param === null || param === undefined) {
20
+ return 'NULL';
21
+ }
22
+ if (typeof param === 'string') {
23
+ // replace single quote with two single quotes
24
+ param = param.replace(/'/g, "''");
25
+ return `'${param}'`;
26
+ }
27
+ if (typeof param === 'number') {
28
+ return param.toString();
29
+ }
30
+ if (typeof param === 'boolean') {
31
+ return param ? '1' : '0';
32
+ }
33
+ // serialize buffer as X'...' hex encoded string
34
+ if (Buffer.isBuffer(param)) {
35
+ return `X'${param.toString('hex')}'`;
36
+ }
37
+ if (typeof param === 'object') {
38
+ // serialize json then escape single quotes
39
+ let json = JSON.stringify(param);
40
+ json = json.replace(/'/g, "''");
41
+ return `'${json}'`;
42
+ }
43
+ throw new types_1.SQLiteCloudError(`Unsupported parameter type: ${typeof param}`);
44
+ }
45
+ exports.escapeSqlParameter = escapeSqlParameter;
46
+ /** Take a sql statement and replaces ? or $named parameters that are properly serialized and escaped. */
47
+ function prepareSql(sql, ...params) {
48
+ // parameters where passed as an array of parameters?
49
+ if ((params === null || params === void 0 ? void 0 : params.length) === 1 && Array.isArray(params[0])) {
50
+ params = params[0];
51
+ }
52
+ // replace ? or ?idx parameters passed as args or as an array
53
+ let parameterIndex = 1;
54
+ let preparedSql = sql.replace(/\?(\d+)?/g, (match, matchIndex) => {
55
+ const index = matchIndex ? parseInt(matchIndex) : parameterIndex;
56
+ parameterIndex++;
57
+ let sqlParameter;
58
+ if (params[0] && typeof params[0] === 'object' && !(params[0] instanceof Buffer)) {
59
+ sqlParameter = params[0][index];
60
+ }
61
+ if (!sqlParameter) {
62
+ if (index > params.length) {
63
+ throw new types_1.SQLiteCloudError('Not enough parameters');
64
+ }
65
+ sqlParameter = params[index - 1];
66
+ }
67
+ return sqlParameter !== null && sqlParameter !== undefined ? escapeSqlParameter(sqlParameter) : 'NULL';
68
+ });
69
+ // replace $named or :named parameters passed as an object
70
+ if ((params === null || params === void 0 ? void 0 : params.length) === 1 && params[0] && typeof params[0] === 'object') {
71
+ const namedParams = params[0];
72
+ for (const [paramKey, param] of Object.entries(namedParams)) {
73
+ const firstChar = paramKey.charAt(0);
74
+ if (firstChar == '$' || firstChar == ':' || firstChar == '@') {
75
+ const escapedParam = escapeSqlParameter(param);
76
+ preparedSql = preparedSql.replace(new RegExp(`\\${paramKey}`, 'g'), escapedParam);
77
+ }
78
+ }
79
+ }
80
+ return preparedSql;
81
+ }
82
+ exports.prepareSql = prepareSql;
83
+ /**
84
+ * Many of the methods in our API may contain a callback as their last argument.
85
+ * This method will take the arguments array passed to the method and return an object
86
+ * containing the arguments array with the callbacks removed (if any), and the callback itself.
87
+ * If there are multiple callbacks, the first one is returned as 'callback' and the last one
88
+ * as 'completeCallback'.
89
+ */
90
+ function popCallback(args) {
91
+ const remaining = args;
92
+ // at least 1 callback?
93
+ if (args && args.length > 0 && typeof args[args.length - 1] === 'function') {
94
+ // at least 2 callbacks?
95
+ if (args.length > 1 && typeof args[args.length - 2] === 'function') {
96
+ return { args: remaining.slice(0, -2), callback: args[args.length - 2], complete: args[args.length - 1] };
97
+ }
98
+ return { args: remaining.slice(0, -1), callback: args[args.length - 1] };
99
+ }
100
+ return { args: remaining };
101
+ }
102
+ exports.popCallback = popCallback;
103
+ /** Parse connectionString like sqlitecloud://username:password@host:port/database?option1=xxx&option2=xxx into its components */
104
+ function parseConnectionString(connectionString) {
105
+ try {
106
+ // The URL constructor throws a TypeError if the URL is not valid.
107
+ // in spite of having the same structure as a regular url
108
+ // protocol://username:password@host:port/database?option1=xxx&option2=xxx)
109
+ // the sqlitecloud: protocol is not recognized by the URL constructor in browsers
110
+ // so we need to replace it with https: to make it work
111
+ const knownProtocolUrl = connectionString.replace('sqlitecloud:', 'https:');
112
+ const url = new URL(knownProtocolUrl);
113
+ const options = {};
114
+ url.searchParams.forEach((value, key) => {
115
+ options[key] = value;
116
+ });
117
+ const config = Object.assign({ username: url.username, password: url.password, host: url.hostname, port: url.port ? parseInt(url.port) : undefined }, options);
118
+ const database = url.pathname.replace('/', ''); // pathname is database name, remove the leading slash
119
+ if (database) {
120
+ config.database = database;
121
+ }
122
+ return config;
123
+ }
124
+ catch (error) {
125
+ throw new types_1.SQLiteCloudError(`Invalid connection string: ${connectionString}`);
126
+ }
127
+ }
128
+ exports.parseConnectionString = parseConnectionString;
129
+ /** Returns true if value is 1 or true */
130
+ function parseBoolean(value) {
131
+ if (typeof value === 'string') {
132
+ return value.toLowerCase() === 'true' || value === '1';
133
+ }
134
+ return value ? true : false;
135
+ }
136
+ exports.parseBoolean = parseBoolean;
package/package.json ADDED
@@ -0,0 +1,84 @@
1
+ {
2
+ "name": "@sqlitecloud/drivers",
3
+ "version": "0.0.34",
4
+ "description": "SQLiteCloud drivers for Typescript/Javascript in edge, web and node clients",
5
+ "main": "./lib/index.js",
6
+ "types": "./lib/index.d.ts",
7
+ "files": [
8
+ "lib/**/*"
9
+ ],
10
+ "scripts": {
11
+ "test": "jest --coverage",
12
+ "build": "rm -rf ./lib/ && tsc --project tsconfig.build.json && npx webpack",
13
+ "publish": "npm run build && npm publish --access public",
14
+ "prettier": "prettier --write 'src/**/*'",
15
+ "lint": "eslint ./src/ --fix && tsc --noEmit",
16
+ "typedoc": "rm -rf ./docs/ && typedoc --out docs && typedoc --plugin typedoc-plugin-markdown --out docs/markdown",
17
+ "npmgui": "npx npm-gui@latest"
18
+ },
19
+ "repository": {
20
+ "type": "git",
21
+ "url": "git+https://github.com/sqlitecloud/sqlitecloud-js.git"
22
+ },
23
+ "license": "MIT",
24
+ "author": {
25
+ "name": "SQLiteCloud, Inc.",
26
+ "email": "support@sqlitecloud.io",
27
+ "url": "https://sqlitecloud.io/"
28
+ },
29
+ "engines": {
30
+ "node": ">=18.0"
31
+ },
32
+ "keywords": [
33
+ "sql",
34
+ "sqlite",
35
+ "sqlite3",
36
+ "sqlitecloud",
37
+ "database",
38
+ "cloud"
39
+ ],
40
+ "bugs": {
41
+ "url": "https://github.com/sqlitecloud/sqlitecloud-js/issues"
42
+ },
43
+ "homepage": "https://github.com/sqlitecloud/sqlitecloud-js#readme",
44
+ "dependencies": {
45
+ "eventemitter3": "^5.0.1",
46
+ "lz4js": "^0.2.0",
47
+ "socket.io-client": "^4.7.4"
48
+ },
49
+ "devDependencies": {
50
+ "@types/jest": "^29.5.11",
51
+ "@types/lz4": "^0.6.4",
52
+ "@types/node": "^12.20.55",
53
+ "@typescript-eslint/eslint-plugin": "^4.22.0",
54
+ "@typescript-eslint/parser": "^4.22.0",
55
+ "dotenv": "^16.4.1",
56
+ "eslint": "^7.32.0",
57
+ "eslint-config-prettier": "^8.10.0",
58
+ "eslint-plugin-node": "^11.1.0",
59
+ "eslint-plugin-prettier": "^3.4.1",
60
+ "jest": "^29.7.0",
61
+ "prettier": "^2.2.1",
62
+ "sqlite3": "^5.1.6",
63
+ "ts-jest": "^29.1.1",
64
+ "ts-node": "^10.9.2",
65
+ "typedoc": "^0.25.3",
66
+ "typedoc-plugin-markdown": "^3.17.1",
67
+ "typescript": "^4.9.5",
68
+ "webpack": "^5.90.0",
69
+ "webpack-cli": "^5.1.4"
70
+ },
71
+ "config": {},
72
+ "release": {
73
+ "branches": [
74
+ "main"
75
+ ]
76
+ },
77
+ "prettier": {
78
+ "semi": false,
79
+ "singleQuote": true,
80
+ "trailingComma": "none",
81
+ "arrowParens": "avoid",
82
+ "printWidth": 160
83
+ }
84
+ }