@bentley/imodeljs-native 3.4.9 → 3.4.15

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.
@@ -1,3 +1,3 @@
1
- export * from "./NativeLibrary";
2
- export * from "./CloudSqlite";
1
+ export * from "./NativeLibrary";
2
+ export * from "./NativeCloudSqlite";
3
3
  //# sourceMappingURL=imodeljs-native.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"imodeljs-native.d.ts","sourceRoot":"","sources":["../src/imodeljs-native.ts"],"names":[],"mappings":"AAKA,cAAc,iBAAiB,CAAC;AAChC,cAAc,eAAe,CAAC"}
1
+ {"version":3,"file":"imodeljs-native.d.ts","sourceRoot":"","sources":["../src/imodeljs-native.ts"],"names":[],"mappings":"AAKA,cAAc,iBAAiB,CAAC;AAChC,cAAc,qBAAqB,CAAC"}
@@ -1,23 +1,23 @@
1
- "use strict";
2
- var __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
- }));
13
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
- };
16
- Object.defineProperty(exports, "__esModule", { value: true });
17
- /*---------------------------------------------------------------------------------------------
18
- * Copyright (c) Bentley Systems, Incorporated. All rights reserved.
19
- * See LICENSE.md in the project root for license terms and full copyright notice.
20
- *--------------------------------------------------------------------------------------------*/
21
- __exportStar(require("./NativeLibrary"), exports);
22
- __exportStar(require("./CloudSqlite"), exports);
1
+ "use strict";
2
+ var __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
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ /*---------------------------------------------------------------------------------------------
18
+ * Copyright (c) Bentley Systems, Incorporated. All rights reserved.
19
+ * See LICENSE.md in the project root for license terms and full copyright notice.
20
+ *--------------------------------------------------------------------------------------------*/
21
+ __exportStar(require("./NativeLibrary"), exports);
22
+ __exportStar(require("./NativeCloudSqlite"), exports);
23
23
  //# sourceMappingURL=imodeljs-native.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"imodeljs-native.js","sourceRoot":"","sources":["../src/imodeljs-native.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AACA;;;+FAG+F;AAC/F,kDAAgC;AAChC,gDAA8B","sourcesContent":["\r\n/*---------------------------------------------------------------------------------------------\r\n* Copyright (c) Bentley Systems, Incorporated. All rights reserved.\r\n* See LICENSE.md in the project root for license terms and full copyright notice.\r\n*--------------------------------------------------------------------------------------------*/\r\nexport * from \"./NativeLibrary\";\r\nexport * from \"./CloudSqlite\";\r\n"]}
1
+ {"version":3,"file":"imodeljs-native.js","sourceRoot":"","sources":["../src/imodeljs-native.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AACA;;;+FAG+F;AAC/F,kDAAgC;AAChC,sDAAoC","sourcesContent":["\r\n/*---------------------------------------------------------------------------------------------\r\n* Copyright (c) Bentley Systems, Incorporated. All rights reserved.\r\n* See LICENSE.md in the project root for license terms and full copyright notice.\r\n*--------------------------------------------------------------------------------------------*/\r\nexport * from \"./NativeLibrary\";\r\nexport * from \"./NativeCloudSqlite\";\r\n"]}
@@ -68,27 +68,35 @@ function copyFolderRecursiveSync(source, target) {
68
68
  }
69
69
  }
70
70
 
71
- // We have to run npm install in a temp directory. If we try to run it in the current directory,
72
- // it will fight over a (file?) lock that the parent npm install holds.
73
- // Note that we have to copy the current package.json to the temp directory, or else npm install will object.
74
- const installDir = path.join(os.tmpdir(), "install-imodeljs-native");
75
- try { fs.mkdirSync(installDir); } catch (err) { }
76
- fs.copyFileSync(path.join(__dirname, "package.json"), path.join(installDir, "package.json"));
71
+ if (process.env.IMODELJS_NATIVE_OVERRIDE !== undefined) {
72
+ // expect IMODELJS_NATIVE_OVERRIDE to have the necessary platform in it already.
73
+ console.log(`OVERRIDE DETECTED`);
74
+ const overrideFolder = path.resolve(process.env.IMODELJS_NATIVE_OVERRIDE);
75
+ console.log(`Copying contents of folder ${overrideFolder} to ${__dirname}`)
76
+ copyFolderRecursiveSync(overrideFolder, __dirname);
77
+ } else {
78
+ // We have to run npm install in a temp directory. If we try to run it in the current directory,
79
+ // it will fight over a (file?) lock that the parent npm install holds.
80
+ // Note that we have to copy the current package.json to the temp directory, or else npm install will object.
81
+ const installDir = path.join(os.tmpdir(), "install-imodeljs-native");
82
+ try { fs.mkdirSync(installDir); } catch (err) { }
83
+ fs.copyFileSync(path.join(__dirname, "package.json"), path.join(installDir, "package.json"));
77
84
 
78
- // We will then copy the results of the install from the temp directory into sub-directories below this one.
79
- function installNativePackage() {
80
- checkSupportedPlatform();
85
+ // We will then copy the results of the install from the temp directory into sub-directories below this one.
86
+ function installNativePackage() {
87
+ checkSupportedPlatform();
81
88
 
82
- // This platform is supported. Try to install the appropriate addon package.
83
- const cmdLine = `npm install --no-save @bentley/imodeljs-${process.platform}-${process.arch}@${version}`;
84
- console.log(cmdLine);
85
- exec(cmdLine, { cwd: installDir }, (error, stdout, stderr) => {
86
- if (error)
87
- throw error;
88
- console.log(stdout);
89
- console.log(stderr);
90
- copyFolderRecursiveSync(path.join(installDir, "node_modules", "@bentley"), __dirname);
91
- });
92
- }
89
+ // This platform is supported. Try to install the appropriate addon package.
90
+ const cmdLine = `npm install --no-save @bentley/imodeljs-${process.platform}-${process.arch}@${version}`;
91
+ console.log(cmdLine);
92
+ exec(cmdLine, { cwd: installDir }, (error, stdout, stderr) => {
93
+ if (error)
94
+ throw error;
95
+ console.log(stdout);
96
+ console.log(stderr);
97
+ copyFolderRecursiveSync(path.join(installDir, "node_modules", "@bentley"), __dirname);
98
+ });
99
+ }
93
100
 
94
- installNativePackage();
101
+ installNativePackage();
102
+ }
package/package.json CHANGED
@@ -1,23 +1,23 @@
1
- {
2
- "name": "@bentley/imodeljs-native",
3
- "license": "SEE LICENSE IN LICENSE.md",
4
- "version": "3.4.9",
5
- "main": "imodeljs-native.js",
6
- "typings": "imodeljs-native",
7
- "keywords": [
8
- "Bentley",
9
- "iModel"
10
- ],
11
- "repository": {
12
- "type" : "git",
13
- "url" : "https://github.com/imodeljs/imodeljs"
14
- },
15
- "description": "iModel.js native module",
16
- "author": {
17
- "name": "Bentley Systems, Inc.",
18
- "url": "www.bentley.com"
19
- },
20
- "scripts": {
21
- "postinstall": "node ./installNativePlatform.js"
22
- }
23
- }
1
+ {
2
+ "name": "@bentley/imodeljs-native",
3
+ "license": "SEE LICENSE IN LICENSE.md",
4
+ "version": "3.4.15",
5
+ "main": "imodeljs-native.js",
6
+ "typings": "imodeljs-native",
7
+ "keywords": [
8
+ "Bentley",
9
+ "iModel"
10
+ ],
11
+ "repository": {
12
+ "type" : "git",
13
+ "url" : "https://github.com/imodeljs/imodeljs"
14
+ },
15
+ "description": "iModel.js native module",
16
+ "author": {
17
+ "name": "Bentley Systems, Inc.",
18
+ "url": "www.bentley.com"
19
+ },
20
+ "scripts": {
21
+ "postinstall": "node ./installNativePlatform.js"
22
+ }
23
+ }
@@ -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EAAE,SAAS,EAAE,MAAM,CAAC,EAAE,WAAW,CAAC,EAAE,oBAAoB,cAapJ;CACF"}
package/CloudSqlite.js DELETED
@@ -1,156 +0,0 @@
1
- "use strict";
2
- /*---------------------------------------------------------------------------------------------
3
- * Copyright (c) Bentley Systems, Incorporated. All rights reserved.
4
- * See LICENSE.md in the project root for license terms and full copyright notice.
5
- *--------------------------------------------------------------------------------------------*/
6
- /** @packageDocumentation
7
- * @module iModels
8
- */
9
- Object.defineProperty(exports, "__esModule", { value: true });
10
- exports.CloudSqlite = void 0;
11
- const child_process = require("child_process");
12
- const fs = require("fs");
13
- const os = require("os");
14
- const path = require("path");
15
- const NativeLibrary_1 = require("./NativeLibrary");
16
- // cspell:ignore polltime cachesize notimestamps deletetime gctime retrytime nwrite ndelete prefetch httptimeout
17
- /* eslint-disable no-restricted-syntax */
18
- var CloudSqlite;
19
- (function (CloudSqlite) {
20
- ;
21
- class Daemon {
22
- static exeName(props) {
23
- var _a;
24
- return (_a = props.exePath) !== null && _a !== void 0 ? _a : path.join(__dirname, NativeLibrary_1.NativeLibrary.archName, os.platform() === "win32" ? "iTwinDaemon.exe" : "iTwinDaemon");
25
- }
26
- static daemonDir(props) {
27
- var _a;
28
- return (_a = props.rootDir) !== null && _a !== void 0 ? _a : path.join(NativeLibrary_1.NativeLibrary.defaultCacheDir, "itwin-daemon");
29
- }
30
- /** Start the Daemon process using the supplied properties. The process will be detached from the current process. */
31
- static start(props) {
32
- const dir = this.daemonDir(props);
33
- fs.mkdirSync(dir, { recursive: true }); // make sure the directory exists before starting the daemon
34
- const args = [`daemon`];
35
- if (props.addr !== undefined)
36
- args.push(`-addr`, `${props.addr}`);
37
- if (props.portNumber !== undefined)
38
- args.push(`-port`, `${props.portNumber}`);
39
- if (props.cacheSize)
40
- args.push(`-cachesize`, `${props.cacheSize}`);
41
- if (props.log)
42
- args.push(`-log`, `${props.log}`);
43
- if (props.noTimeStamps)
44
- args.push(`-notimestamps`);
45
- if (props.httptimeout !== undefined)
46
- args.push(`-httptimeout`, `${props.httptimeout}`);
47
- args.push(`${dir}`); // This MUST be the last arg when starting the daemon.
48
- return child_process.spawn(this.exeName(props), args, Object.assign(Object.assign({}, props.spawnOptions), { windowsVerbatimArguments: true }));
49
- }
50
- }
51
- CloudSqlite.Daemon = Daemon;
52
- /** @internal */
53
- async function transferDb(direction, container, props) {
54
- if (direction === "download")
55
- fs.mkdirSync(path.dirname(props.localFileName), { recursive: true }); // make sure the directory exists before starting download
56
- let timer;
57
- try {
58
- const transfer = new NativeLibrary_1.NativeLibrary.nativeLib.CloudDbTransfer(direction, container, props);
59
- let total = 0;
60
- const onProgress = props.onProgress;
61
- if (onProgress) {
62
- timer = setInterval(async () => {
63
- const progress = transfer.getProgress();
64
- total = progress.total;
65
- if (onProgress(progress.loaded, progress.total))
66
- transfer.cancelTransfer();
67
- }, 250);
68
- }
69
- await transfer.promise;
70
- onProgress === null || onProgress === void 0 ? void 0 : onProgress(total, total); // make sure we call progress func one last time when download completes
71
- }
72
- catch (err) {
73
- if (err.message === "cancelled")
74
- err.errorNumber = 131079; // BriefcaseStatus.DownloadCancelled
75
- throw err;
76
- }
77
- finally {
78
- if (timer)
79
- clearInterval(timer);
80
- }
81
- }
82
- CloudSqlite.transferDb = transferDb;
83
- /** Upload a database into a CloudContainer
84
- * @param container the CloudContainer holding the database. Must be connected.
85
- * @param props the properties that describe the database to be downloaded, plus optionally an `onProgress` function.
86
- * @note this function requires that the write lock be held on the container
87
- */
88
- async function uploadDb(container, props) {
89
- await transferDb("upload", container, props);
90
- container.checkForChanges(); // re-read the manifest so the database is available locally.
91
- }
92
- CloudSqlite.uploadDb = uploadDb;
93
- /** Download a database from a CloudContainer
94
- * @param container the CloudContainer holding the database. Must be connected.
95
- * @param props the properties that describe the database to be downloaded, plus optionally an `onProgress` function.
96
- * @returns a Promise that is resolved when the download completes.
97
- * @note the download is "restartable." If the transfer is aborted and then re-requested, it will continue from where
98
- * it left off rather than re-downloading the entire file.
99
- */
100
- async function downloadDb(container, props) {
101
- await transferDb("download", container, props);
102
- }
103
- CloudSqlite.downloadDb = downloadDb;
104
- /**
105
- * Attempt to acquire the write lock for a container, with retries.
106
- * If write lock is held by another user, call busyHandler if supplied. If no busyHandler, or handler returns "stop", throw. Otherwise try again.
107
- * @note if write lock is already held, this function does nothing.
108
- * @param user the name to be displayed to other users in the event they attempt to obtain the lock while it is held by us
109
- * @param container the CloudContainer for which the lock is to be acquired
110
- * @param busyHandler if present, function called when the write lock is currently held by another user.
111
- */
112
- async function acquireWriteLock(user, container, busyHandler) {
113
- if (container.hasWriteLock)
114
- return;
115
- while (true) {
116
- try {
117
- return container.acquireWriteLock(user);
118
- }
119
- catch (e) {
120
- if (e.errorNumber === 5 && busyHandler && "stop" !== await busyHandler(e.lockedBy, e.expires)) // 5 === BE_SQLITE_BUSY
121
- continue; // busy handler wants to try again
122
- throw e;
123
- }
124
- }
125
- }
126
- CloudSqlite.acquireWriteLock = acquireWriteLock;
127
- /**
128
- * Perform an asynchronous write operation on a CloudContainer with the write lock held.
129
- * 1. if write lock is already held, call operation and return.
130
- * 2. attempt to acquire the write lock, with retries. Throw if unable to obtain write lock.
131
- * 3. perform the operation
132
- * 3.a if the operation throws, abandon all changes and re-throw
133
- * 4. release the write lock.
134
- * 5. return value from operation
135
- * @param user the name to be displayed to other users in the event they attempt to obtain the lock while it is held by us
136
- * @param container the CloudContainer for which the lock is to be acquired
137
- * @param operation an asynchronous operation performed with the write lock held.
138
- * @param busyHandler if present, function called when the write lock is currently held by another user.
139
- */
140
- async function withWriteLock(user, container, operation, busyHandler) {
141
- if (container.hasWriteLock)
142
- return operation();
143
- await acquireWriteLock(user, container, busyHandler);
144
- try {
145
- const val = await operation(); // wait for work to finish or fail
146
- container.releaseWriteLock();
147
- return val;
148
- }
149
- catch (e) {
150
- container.abandonChanges(); // if operation threw, abandon all changes
151
- throw e;
152
- }
153
- }
154
- CloudSqlite.withWriteLock = withWriteLock;
155
- })(CloudSqlite = exports.CloudSqlite || (exports.CloudSqlite = {}));
156
- //# sourceMappingURL=CloudSqlite.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"CloudSqlite.js","sourceRoot":"","sources":["../src/CloudSqlite.ts"],"names":[],"mappings":";AAAA;;;+FAG+F;AAC/F;;GAEG;;;AAEH,+CAA+C;AAC/C,yBAAyB;AACzB,yBAAyB;AACzB,6BAA6B;AAC7B,mDAAgE;AAOhE,gHAAgH;AAChH,yCAAyC;AAEzC,IAAiB,WAAW,CAgQ3B;AAhQD,WAAiB,WAAW;IAIzB,CAAC;IAgHF,MAAa,MAAM;QACT,MAAM,CAAC,OAAO,CAAC,KAAkB;;YACvC,OAAO,MAAA,KAAK,CAAC,OAAO,mCAAI,IAAI,CAAC,IAAI,CAAC,SAAS,EAAE,6BAAa,CAAC,QAAQ,EAAE,EAAE,CAAC,QAAQ,EAAE,KAAK,OAAO,CAAC,CAAC,CAAC,iBAAiB,CAAC,CAAC,CAAC,aAAa,CAAC,CAAC;QACtI,CAAC;QACM,MAAM,CAAC,SAAS,CAAC,KAAiB;;YACvC,OAAO,MAAA,KAAK,CAAC,OAAO,mCAAI,IAAI,CAAC,IAAI,CAAC,6BAAa,CAAC,eAAe,EAAE,cAAc,CAAC,CAAC;QACnF,CAAC;QAED,qHAAqH;QAC9G,MAAM,CAAC,KAAK,CAAC,KAA+B;YACjD,MAAM,GAAG,GAAG,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC;YAClC,EAAE,CAAC,SAAS,CAAC,GAAG,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC,4DAA4D;YAEpG,MAAM,IAAI,GAAG,CAAC,QAAQ,CAAC,CAAC;YACxB,IAAI,KAAK,CAAC,IAAI,KAAK,SAAS;gBAC1B,IAAI,CAAC,IAAI,CAAC,OAAO,EAAE,GAAG,KAAK,CAAC,IAAI,EAAE,CAAC,CAAC;YACtC,IAAI,KAAK,CAAC,UAAU,KAAK,SAAS;gBAChC,IAA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E,WAAkC;QAC/H,IAAI,SAAS,CAAC,YAAY;YACxB,OAAO;QAET,OAAO,IAAI,EAAE;YACX,IAAI;gBACF,OAAO,SAAS,CAAC,gBAAgB,CAAC,IAAI,CAAC,CAAC;aACzC;YAAC,OAAO,CAAM,EAAE;gBACf,IAAI,CAAC,CAAC,WAAW,KAAK,CAAC,IAAI,WAAW,IAAI,MAAM,KAAK,MAAM,WAAW,CAAC,CAAC,CAAC,QAAQ,EAAE,CAAC,CAAC,OAAO,CAAC,EAAE,uBAAuB;oBACpH,SAAS,CAAC,kCAAkC;gBAC9C,MAAM,CAAC,CAAC;aACT;SACF;IACH,CAAC;IAbqB,4BAAgB,mBAarC,CAAA;IAED;;;;;;;;;;;;OAYG;IACI,KAAK,UAAU,aAAa,CAAI,IAAY,EAAE,SAAwC,EAAE,SAAkB,EAAE,WAAkC;QACnJ,IAAI,SAAS,CAAC,YAAY;YACxB,OAAO,SAAS,EAAE,CAAC;QAErB,MAAM,gBAAgB,CAAC,IAAI,EAAE,SAAS,EAAE,WAAW,CAAC,CAAC;QACrD,IAAI;YACF,MAAM,GAAG,GAAG,MAAM,SAAS,EAAE,CAAC,CAAC,kCAAkC;YACjE,SAAS,CAAC,gBAAgB,EAAE,CAAC;YAC7B,OAAO,GAAG,CAAC;SACZ;QAAC,OAAO,CAAC,EAAE;YACV,SAAS,CAAC,cAAc,EAAE,CAAC,CAAE,0CAA0C;YACvE,MAAM,CAAC,CAAC;SACT;IACH,CAAC;IAbqB,yBAAa,gBAalC,CAAA;AACH,CAAC,EAhQgB,WAAW,GAAX,mBAAW,KAAX,mBAAW,QAgQ3B","sourcesContent":["/*---------------------------------------------------------------------------------------------\r\n* Copyright (c) Bentley Systems, Incorporated. All rights reserved.\r\n* See LICENSE.md in the project root for license terms and full copyright notice.\r\n*--------------------------------------------------------------------------------------------*/\r\n/** @packageDocumentation\r\n * @module iModels\r\n */\r\n\r\nimport * as child_process from \"child_process\";\r\nimport * as fs from \"fs\";\r\nimport * as os from \"os\";\r\nimport * as path from \"path\";\r\nimport { IModelJsNative, NativeLibrary } from \"./NativeLibrary\";\r\n\r\n/**\r\n * @note This package may only have **dev** dependencies on @itwin packages, so they are *not* available at runtime. Therefore we can only import **types** from them.\r\n */\r\nimport type { LocalFileName } from \"@itwin/core-common\";\r\n\r\n// cspell:ignore polltime cachesize notimestamps deletetime gctime retrytime nwrite ndelete prefetch httptimeout\r\n/* eslint-disable no-restricted-syntax */\r\n\r\nexport namespace CloudSqlite {\r\n\r\n export const enum LogMask {\r\n LOG_HTTP = 0x0001, LOG_UPLOAD = 0x0002, LOG_CLEANUP = 0x0004, LOG_EVENT = 0x0008\r\n };\r\n\r\n /** Properties that specify how to access the account for a cloud blob-store container. */\r\n export interface AccountAccessProps {\r\n /** blob storage module: e.g. \"azure\", \"google\", \"aws\". May also include URI style parameters. */\r\n storageType: string;\r\n /** blob store account name, or a URI for custom domains. */\r\n accessName: string;\r\n }\r\n\r\n /** Properties of a CloudContainer. */\r\n export interface ContainerProps {\r\n /** the name of the container. */\r\n containerId: string;\r\n /** an alias for the container. Defaults to `containerId` */\r\n alias?: string;\r\n /** token that grants access to the container. For sas=1 `storageType`s, this is the sasToken. For sas=0, this is the account key */\r\n accessToken: string;\r\n /** if true, container is attached with write permissions, and accessToken must provide write access to the cloud container. */\r\n writeable?: boolean;\r\n /** if true, container is attached in \"secure\" mode (blocks are encrypted). Only supported in daemon mode. */\r\n secure?: boolean;\r\n }\r\n\r\n /** Returned from `CloudContainer.queryDatabase` describing one database in the container */\r\n export interface CachedDbProps {\r\n /** The total of (4Mb) blocks in the database. */\r\n readonly totalBlocks: number,\r\n /** the number of blocks of the database that have been downloaded into the CloudCache */\r\n readonly localBlocks: number,\r\n /** the number of blocks from this database that have been modified in the CloudCache and need to be uploaded. */\r\n readonly dirtyBlocks: number,\r\n /** If true, the database currently has transactions in the WAL file and may not be uploaded until they have been checkPointed. */\r\n readonly transactions: boolean;\r\n\r\n /** the state of this database. Indicates whether the database is new or deleted since last upload */\r\n readonly state: \"\" | \"copied\" | \"deleted\";\r\n }\r\n\r\n /** Properties for accessing a CloudContainer */\r\n export type ContainerAccessProps = AccountAccessProps & ContainerProps & {\r\n\r\n /** Duration for holding write lock, in seconds. After this time the write lock expires if not refreshed. Default is one hour. */\r\n durationSeconds?: number\r\n };\r\n\r\n /** The name of a CloudSqlite database within a CloudContainer. */\r\n export interface DbNameProp {\r\n /** the name of the database within the CloudContainer.\r\n * @note names of databases within a CloudContainer are always **case sensitive** on all platforms.*/\r\n dbName: string\r\n }\r\n\r\n /** Properties for accessing a database within a CloudContainer */\r\n export interface DbProps extends DbNameProp {\r\n /** the name of the local file to access the database. */\r\n localFileName: LocalFileName\r\n }\r\n\r\n export type TransferDirection = \"upload\" | \"download\";\r\n export interface TransferProgress {\r\n /** a user-supplied progress function called during the transfer operation. Return a non-0 value to abort the transfer. */\r\n onProgress?: (loaded: number, total: number) => number\r\n }\r\n\r\n export interface CloudHttpProps {\r\n /** The number of simultaneous HTTP requests. Default is 6. */\r\n nRequests?: number;\r\n }\r\n\r\n export interface PrefetchProps extends CloudHttpProps {\r\n /** timeout between requests, in milliseconds. Default is 100. */\r\n timeout?: number;\r\n /** The number of prefetch requests to issue while there is foreground activity. Default is 3. */\r\n minRequests?: number;\r\n }\r\n\r\n export type TransferDbProps = DbProps & TransferProgress & CloudHttpProps;\r\n\r\n /** Properties for creating a CloudCache. */\r\n export interface CacheProps extends CloudHttpProps {\r\n /** full path of directory for cache to store its files. Must be on a (preferably fast) local drive, and must be empty when the cache is first created. */\r\n rootDir: string;\r\n /** name of this cache. It is possible to have more than one CloudCache in the same session. */\r\n name: string;\r\n /** maximum cache Size. Must be a number followed by either M (for megabytes) or G (for gigabytes.) Default is 1G */\r\n cacheSize?: string;\r\n /** turn on diagnostics for `curl` (outputs to stderr) */\r\n curlDiagnostics?: boolean;\r\n }\r\n\r\n /** Properties for creating a new instance of a daemon process. */\r\n export interface DaemonProps {\r\n /** full path name of daemon.exe file. Default is to find \"iTwinDaemon.exe\" in the same directory as this library. */\r\n exePath?: string;\r\n /** daemon connection address. Default is \"127.0.0.1\" */\r\n addr?: string;\r\n /** port number. Default 22002 */\r\n portNumber?: number;\r\n /** maximum cache Size. Must be a number followed by either M (for megabytes) or G (for gigabytes.) Default is 1G */\r\n cacheSize?: string;\r\n /** logging options */\r\n log?: string;\r\n /** if true, don't include timestamps in log messages */\r\n noTimeStamps?: boolean;\r\n /** The amount of time, in seconds before an http request made to cloud storage by the daemon times out. Default 600 seconds. */\r\n httptimeout?: number;\r\n /** options for spawn */\r\n spawnOptions?: child_process.SpawnOptions;\r\n }\r\n export type DaemonCommandArg = DbNameProp & AccountAccessProps & CacheProps & ContainerProps;\r\n\r\n export class Daemon {\r\n private static exeName(props: DaemonProps) {\r\n return props.exePath ?? path.join(__dirname, NativeLibrary.archName, os.platform() === \"win32\" ? \"iTwinDaemon.exe\" : \"iTwinDaemon\");\r\n }\r\n public static daemonDir(props: CacheProps) {\r\n return props.rootDir ?? path.join(NativeLibrary.defaultCacheDir, \"itwin-daemon\");\r\n }\r\n\r\n /** Start the Daemon process using the supplied properties. The process will be detached from the current process. */\r\n public static start(props: DaemonProps & CacheProps): child_process.ChildProcess {\r\n const dir = this.daemonDir(props);\r\n fs.mkdirSync(dir, { recursive: true }); // make sure the directory exists before starting the daemon\r\n\r\n const args = [`daemon`];\r\n if (props.addr !== undefined)\r\n args.push(`-addr`, `${props.addr}`);\r\n if (props.portNumber !== undefined)\r\n args.push(`-port`, `${props.portNumber}`);\r\n if (props.cacheSize)\r\n args.push(`-cachesize`, `${props.cacheSize}`);\r\n if (props.log)\r\n args.push(`-log`, `${props.log}`);\r\n if (props.noTimeStamps)\r\n args.push(`-notimestamps`);\r\n if (props.httptimeout !== undefined)\r\n args.push(`-httptimeout`, `${props.httptimeout}`)\r\n args.push(`${dir}`); // This MUST be the last arg when starting the daemon.\r\n return child_process.spawn(this.exeName(props), args, { ...props.spawnOptions, windowsVerbatimArguments: true });\r\n }\r\n }\r\n\r\n /** @internal */\r\n export async function transferDb(direction: TransferDirection, container: IModelJsNative.CloudContainer, props: TransferDbProps) {\r\n if (direction === \"download\")\r\n fs.mkdirSync(path.dirname(props.localFileName), { recursive: true }); // make sure the directory exists before starting download\r\n\r\n let timer: NodeJS.Timeout | undefined;\r\n try {\r\n const transfer = new NativeLibrary.nativeLib.CloudDbTransfer(direction, container, props);\r\n let total = 0;\r\n const onProgress = props.onProgress;\r\n if (onProgress) {\r\n timer = setInterval(async () => { // set an interval timer to show progress every 250ms\r\n const progress = transfer.getProgress();\r\n total = progress.total;\r\n if (onProgress(progress.loaded, progress.total))\r\n transfer.cancelTransfer();\r\n }, 250);\r\n }\r\n await transfer.promise;\r\n onProgress?.(total, total); // make sure we call progress func one last time when download completes\r\n } catch (err: any) {\r\n if (err.message === \"cancelled\")\r\n err.errorNumber = 131079; // BriefcaseStatus.DownloadCancelled\r\n\r\n throw err;\r\n } finally {\r\n if (timer)\r\n clearInterval(timer);\r\n }\r\n }\r\n\r\n /** Upload a database into a CloudContainer\r\n * @param container the CloudContainer holding the database. Must be connected.\r\n * @param props the properties that describe the database to be downloaded, plus optionally an `onProgress` function.\r\n * @note this function requires that the write lock be held on the container\r\n */\r\n export async function uploadDb(container: IModelJsNative.CloudContainer, props: TransferDbProps): Promise<void> {\r\n await transferDb(\"upload\", container, props);\r\n container.checkForChanges(); // re-read the manifest so the database is available locally.\r\n }\r\n\r\n /** Download a database from a CloudContainer\r\n * @param container the CloudContainer holding the database. Must be connected.\r\n * @param props the properties that describe the database to be downloaded, plus optionally an `onProgress` function.\r\n * @returns a Promise that is resolved when the download completes.\r\n * @note the download is \"restartable.\" If the transfer is aborted and then re-requested, it will continue from where\r\n * it left off rather than re-downloading the entire file.\r\n */\r\n export async function downloadDb(container: IModelJsNative.CloudContainer, props: TransferDbProps): Promise<void> {\r\n await transferDb(\"download\", container, props);\r\n }\r\n\r\n /** Optional method to be called when an attempt to acquire the write lock fails because another user currently holds it.\r\n * @param lockedBy The identifier supplied by the application/user that currently holds the lock.\r\n * @param expires a stringified Date (in local time) indicating when the lock will expire.\r\n * @return \"stop\" to give up and stop retrying. Generally, it's a good idea to wait for some time before returning.\r\n */\r\n export type WriteLockBusyHandler = (lockedBy: string, expires: string) => Promise<void | \"stop\">;\r\n\r\n /**\r\n * Attempt to acquire the write lock for a container, with retries.\r\n * If write lock is held by another user, call busyHandler if supplied. If no busyHandler, or handler returns \"stop\", throw. Otherwise try again.\r\n * @note if write lock is already held, this function does nothing.\r\n * @param user the name to be displayed to other users in the event they attempt to obtain the lock while it is held by us\r\n * @param container the CloudContainer for which the lock is to be acquired\r\n * @param busyHandler if present, function called when the write lock is currently held by another user.\r\n */\r\n export async function acquireWriteLock(user: string, container: IModelJsNative.CloudContainer, busyHandler?: WriteLockBusyHandler) {\r\n if (container.hasWriteLock)\r\n return;\r\n\r\n while (true) {\r\n try {\r\n return container.acquireWriteLock(user);\r\n } catch (e: any) {\r\n if (e.errorNumber === 5 && busyHandler && \"stop\" !== await busyHandler(e.lockedBy, e.expires)) // 5 === BE_SQLITE_BUSY\r\n continue; // busy handler wants to try again\r\n throw e;\r\n }\r\n }\r\n }\r\n\r\n /**\r\n * Perform an asynchronous write operation on a CloudContainer with the write lock held.\r\n * 1. if write lock is already held, call operation and return.\r\n * 2. attempt to acquire the write lock, with retries. Throw if unable to obtain write lock.\r\n * 3. perform the operation\r\n * 3.a if the operation throws, abandon all changes and re-throw\r\n * 4. release the write lock.\r\n * 5. return value from operation\r\n * @param user the name to be displayed to other users in the event they attempt to obtain the lock while it is held by us\r\n * @param container the CloudContainer for which the lock is to be acquired\r\n * @param operation an asynchronous operation performed with the write lock held.\r\n * @param busyHandler if present, function called when the write lock is currently held by another user.\r\n */\r\n export async function withWriteLock<T>(user: string, container: IModelJsNative.CloudContainer, operation: () => T, busyHandler?: WriteLockBusyHandler) {\r\n if (container.hasWriteLock)\r\n return operation();\r\n\r\n await acquireWriteLock(user, container, busyHandler);\r\n try {\r\n const val = await operation(); // wait for work to finish or fail\r\n container.releaseWriteLock();\r\n return val;\r\n } catch (e) {\r\n container.abandonChanges(); // if operation threw, abandon all changes\r\n throw e;\r\n }\r\n }\r\n}\r\n"]}