hide-a-bed 4.2.0 → 5.0.2
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/README.md +175 -14
- package/impl/changes.mjs +53 -0
- package/impl/errors.mjs +1 -1
- package/impl/query.mjs +14 -6
- package/impl/stream.mjs +2 -2
- package/impl/sugar/lock.mjs +70 -0
- package/impl/sugar/watch.mjs +154 -0
- package/index.mjs +23 -3
- package/integration/changes.mjs +60 -0
- package/integration/disconnect-watch.mjs +36 -0
- package/integration/watch.mjs +40 -0
- package/log.txt +84 -0
- package/package.json +3 -2
- package/schema/bind.mjs +8 -1
- package/schema/changes.mjs +59 -0
- package/schema/sugar/lock.mjs +50 -0
- package/schema/sugar/watch.mjs +29 -0
- package/types/changes-stream.d.ts +11 -0
- package/cjs/impl/bulk.cjs +0 -267
- package/cjs/impl/crud.cjs +0 -121
- package/cjs/impl/errors.cjs +0 -75
- package/cjs/impl/logger.cjs +0 -70
- package/cjs/impl/patch.cjs +0 -95
- package/cjs/impl/query.cjs +0 -110
- package/cjs/impl/queryBuilder.cjs +0 -99
- package/cjs/impl/retry.cjs +0 -54
- package/cjs/impl/stream.cjs +0 -121
- package/cjs/impl/trackedEmitter.cjs +0 -54
- package/cjs/impl/transactionErrors.cjs +0 -70
- package/cjs/index.cjs +0 -95
- package/cjs/schema/bind.cjs +0 -44
- package/cjs/schema/bulk.cjs +0 -88
- package/cjs/schema/config.cjs +0 -48
- package/cjs/schema/crud.cjs +0 -77
- package/cjs/schema/patch.cjs +0 -53
- package/cjs/schema/query.cjs +0 -62
- package/cjs/schema/stream.cjs +0 -42
- package/impl/bulk.d.mts +0 -11
- package/impl/bulk.d.mts.map +0 -1
- package/impl/crud.d.mts +0 -7
- package/impl/crud.d.mts.map +0 -1
- package/impl/errors.d.mts +0 -43
- package/impl/errors.d.mts.map +0 -1
- package/impl/logger.d.mts +0 -32
- package/impl/logger.d.mts.map +0 -1
- package/impl/patch.d.mts +0 -6
- package/impl/patch.d.mts.map +0 -1
- package/impl/query.d.mts +0 -195
- package/impl/query.d.mts.map +0 -1
- package/impl/queryBuilder.d.mts +0 -94
- package/impl/queryBuilder.d.mts.map +0 -1
- package/impl/retry.d.mts +0 -2
- package/impl/retry.d.mts.map +0 -1
- package/impl/stream.d.mts +0 -3
- package/impl/stream.d.mts.map +0 -1
- package/impl/trackedEmitter.d.mts +0 -8
- package/impl/trackedEmitter.d.mts.map +0 -1
- package/impl/transactionErrors.d.mts +0 -57
- package/impl/transactionErrors.d.mts.map +0 -1
- package/index.d.mts +0 -56
- package/index.d.mts.map +0 -1
- package/schema/bind.d.mts +0 -820
- package/schema/bind.d.mts.map +0 -1
- package/schema/bulk.d.mts +0 -910
- package/schema/bulk.d.mts.map +0 -1
- package/schema/config.d.mts +0 -79
- package/schema/config.d.mts.map +0 -1
- package/schema/crud.d.mts +0 -491
- package/schema/crud.d.mts.map +0 -1
- package/schema/patch.d.mts +0 -255
- package/schema/patch.d.mts.map +0 -1
- package/schema/query.d.mts +0 -406
- package/schema/query.d.mts.map +0 -1
- package/schema/stream.d.mts +0 -211
- package/schema/stream.d.mts.map +0 -1
package/cjs/impl/crud.cjs
DELETED
|
@@ -1,121 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __create = Object.create;
|
|
3
|
-
var __defProp = Object.defineProperty;
|
|
4
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
-
var __export = (target, all) => {
|
|
9
|
-
for (var name in all)
|
|
10
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
-
};
|
|
12
|
-
var __copyProps = (to, from, except, desc) => {
|
|
13
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
-
for (let key of __getOwnPropNames(from))
|
|
15
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
-
}
|
|
18
|
-
return to;
|
|
19
|
-
};
|
|
20
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
-
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
-
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
-
mod
|
|
27
|
-
));
|
|
28
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
-
var crud_exports = {};
|
|
30
|
-
__export(crud_exports, {
|
|
31
|
-
get: () => get,
|
|
32
|
-
getAtRev: () => getAtRev,
|
|
33
|
-
put: () => put
|
|
34
|
-
});
|
|
35
|
-
module.exports = __toCommonJS(crud_exports);
|
|
36
|
-
var import_needle = __toESM(require("needle"), 1);
|
|
37
|
-
var import_crud = require("../schema/crud.cjs");
|
|
38
|
-
var import_errors = require("./errors.cjs");
|
|
39
|
-
var import_logger = require("./logger.cjs");
|
|
40
|
-
const opts = {
|
|
41
|
-
json: true,
|
|
42
|
-
headers: {
|
|
43
|
-
"Content-Type": "application/json"
|
|
44
|
-
}
|
|
45
|
-
};
|
|
46
|
-
const _getWithOptions = import_crud.CouchGetWithOptions.implement(async (config, id, getOpts) => {
|
|
47
|
-
const logger = (0, import_logger.createLogger)(config);
|
|
48
|
-
const rev = getOpts?.rev;
|
|
49
|
-
const path = rev ? `${id}?rev=${rev}` : id;
|
|
50
|
-
const url = `${config.couch}/${path}`;
|
|
51
|
-
logger.info(`Getting document with id: ${id}, rev ${rev || "latest"}`);
|
|
52
|
-
try {
|
|
53
|
-
const resp = await (0, import_needle.default)("get", url, opts);
|
|
54
|
-
if (!resp) {
|
|
55
|
-
logger.error("No response received from get request");
|
|
56
|
-
throw new import_errors.RetryableError("no response", 503);
|
|
57
|
-
}
|
|
58
|
-
const result = resp?.body || {};
|
|
59
|
-
if (resp.statusCode === 404) {
|
|
60
|
-
if (config.throwOnGetNotFound) {
|
|
61
|
-
logger.warn(`Document not found (throwing error): ${id}, rev ${rev || "latest"}`);
|
|
62
|
-
throw new import_errors.NotFoundError(id, result.reason || "not_found");
|
|
63
|
-
} else {
|
|
64
|
-
logger.debug(`Document not found (returning undefined): ${id}, rev ${rev || "latest"}`);
|
|
65
|
-
return null;
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
if (import_errors.RetryableError.isRetryableStatusCode(resp.statusCode)) {
|
|
69
|
-
logger.warn(`Retryable status code received: ${resp.statusCode}`);
|
|
70
|
-
throw new import_errors.RetryableError(result.reason || "retryable error", resp.statusCode);
|
|
71
|
-
}
|
|
72
|
-
if (resp.statusCode !== 200) {
|
|
73
|
-
logger.error(`Unexpected status code: ${resp.statusCode}`);
|
|
74
|
-
throw new Error(result.reason || "failed");
|
|
75
|
-
}
|
|
76
|
-
logger.info(`Successfully retrieved document: ${id}, rev ${rev || "latest"}`);
|
|
77
|
-
return result;
|
|
78
|
-
} catch (err) {
|
|
79
|
-
logger.error("Error during get operation:", err);
|
|
80
|
-
import_errors.RetryableError.handleNetworkError(err);
|
|
81
|
-
}
|
|
82
|
-
});
|
|
83
|
-
const get = import_crud.CouchGet.implement(async (config, id) => {
|
|
84
|
-
const getOptions = {};
|
|
85
|
-
return _getWithOptions(config, id, getOptions);
|
|
86
|
-
});
|
|
87
|
-
const getAtRev = import_crud.CouchGetAtRev.implement(async (config, id, rev) => {
|
|
88
|
-
const getOptions = { rev };
|
|
89
|
-
return _getWithOptions(config, id, getOptions);
|
|
90
|
-
});
|
|
91
|
-
const put = import_crud.CouchPut.implement(async (config, doc) => {
|
|
92
|
-
const logger = (0, import_logger.createLogger)(config);
|
|
93
|
-
const url = `${config.couch}/${doc._id}`;
|
|
94
|
-
const body = doc;
|
|
95
|
-
logger.info(`Putting document with id: ${doc._id}`);
|
|
96
|
-
let resp;
|
|
97
|
-
try {
|
|
98
|
-
resp = await (0, import_needle.default)("put", url, body, opts);
|
|
99
|
-
} catch (err) {
|
|
100
|
-
logger.error("Error during put operation:", err);
|
|
101
|
-
import_errors.RetryableError.handleNetworkError(err);
|
|
102
|
-
}
|
|
103
|
-
if (!resp) {
|
|
104
|
-
logger.error("No response received from put request");
|
|
105
|
-
throw new import_errors.RetryableError("no response", 503);
|
|
106
|
-
}
|
|
107
|
-
const result = resp?.body || {};
|
|
108
|
-
result.statusCode = resp.statusCode;
|
|
109
|
-
if (resp.statusCode === 409) {
|
|
110
|
-
logger.warn(`Conflict detected for document: ${doc._id}`);
|
|
111
|
-
result.ok = false;
|
|
112
|
-
result.error = "conflict";
|
|
113
|
-
return result;
|
|
114
|
-
}
|
|
115
|
-
if (import_errors.RetryableError.isRetryableStatusCode(resp.statusCode)) {
|
|
116
|
-
logger.warn(`Retryable status code received: ${resp.statusCode}`);
|
|
117
|
-
throw new import_errors.RetryableError(result.reason || "retryable error", resp.statusCode);
|
|
118
|
-
}
|
|
119
|
-
logger.info(`Successfully saved document: ${doc._id}`);
|
|
120
|
-
return result;
|
|
121
|
-
});
|
package/cjs/impl/errors.cjs
DELETED
|
@@ -1,75 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __export = (target, all) => {
|
|
7
|
-
for (var name in all)
|
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
-
};
|
|
10
|
-
var __copyProps = (to, from, except, desc) => {
|
|
11
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
-
for (let key of __getOwnPropNames(from))
|
|
13
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
-
}
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
var errors_exports = {};
|
|
20
|
-
__export(errors_exports, {
|
|
21
|
-
NotFoundError: () => NotFoundError,
|
|
22
|
-
RetryableError: () => RetryableError
|
|
23
|
-
});
|
|
24
|
-
module.exports = __toCommonJS(errors_exports);
|
|
25
|
-
class NotFoundError extends Error {
|
|
26
|
-
/**
|
|
27
|
-
* @param {string} docId - The ID of the document that wasn't found
|
|
28
|
-
* @param {string} [message] - Optional error message
|
|
29
|
-
*/
|
|
30
|
-
constructor(docId, message = "Document not found") {
|
|
31
|
-
super(message);
|
|
32
|
-
this.name = "NotFoundError";
|
|
33
|
-
this.docId = docId;
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
class RetryableError extends Error {
|
|
37
|
-
/**
|
|
38
|
-
* @param {string} message - The error message
|
|
39
|
-
* @param {number|undefined} statusCode - The HTTP status code
|
|
40
|
-
*/
|
|
41
|
-
constructor(message, statusCode) {
|
|
42
|
-
super(message);
|
|
43
|
-
this.name = "RetryableError";
|
|
44
|
-
this.statusCode = statusCode;
|
|
45
|
-
}
|
|
46
|
-
/**
|
|
47
|
-
* @param {number|undefined} statusCode - The HTTP status code to check
|
|
48
|
-
* @returns {boolean} Whether the status code is retryable
|
|
49
|
-
*/
|
|
50
|
-
static isRetryableStatusCode(statusCode) {
|
|
51
|
-
if (statusCode === void 0) return false;
|
|
52
|
-
return [408, 429, 500, 502, 503, 504].includes(statusCode);
|
|
53
|
-
}
|
|
54
|
-
/**
|
|
55
|
-
* @param {NetworkError | unknown} err - The network error to handle
|
|
56
|
-
* @throws {RetryableError} If the error is retryable
|
|
57
|
-
* @throws {Error} If the error is not retryable
|
|
58
|
-
*/
|
|
59
|
-
static handleNetworkError(err) {
|
|
60
|
-
const networkErrors = {
|
|
61
|
-
ECONNREFUSED: 503,
|
|
62
|
-
ECONNRESET: 503,
|
|
63
|
-
ETIMEDOUT: 503,
|
|
64
|
-
ENETUNREACH: 503,
|
|
65
|
-
ENOTFOUND: 503,
|
|
66
|
-
EPIPE: 503,
|
|
67
|
-
EHOSTUNREACH: 503,
|
|
68
|
-
ESOCKETTIMEDOUT: 503
|
|
69
|
-
};
|
|
70
|
-
if (typeof err === "object" && err !== null && "code" in err && typeof err.code === "string" && networkErrors[err.code]) {
|
|
71
|
-
throw new RetryableError(`Network error: ${err.code}`, networkErrors[err.code]);
|
|
72
|
-
}
|
|
73
|
-
throw err;
|
|
74
|
-
}
|
|
75
|
-
}
|
package/cjs/impl/logger.cjs
DELETED
|
@@ -1,70 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __export = (target, all) => {
|
|
7
|
-
for (var name in all)
|
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
-
};
|
|
10
|
-
var __copyProps = (to, from, except, desc) => {
|
|
11
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
-
for (let key of __getOwnPropNames(from))
|
|
13
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
-
}
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
var logger_exports = {};
|
|
20
|
-
__export(logger_exports, {
|
|
21
|
-
createLogger: () => createLogger
|
|
22
|
-
});
|
|
23
|
-
module.exports = __toCommonJS(logger_exports);
|
|
24
|
-
function createLogger(config) {
|
|
25
|
-
if (config._normalizedLogger) {
|
|
26
|
-
return config._normalizedLogger;
|
|
27
|
-
}
|
|
28
|
-
if (!config.logger) {
|
|
29
|
-
if (config.useConsoleLogger) {
|
|
30
|
-
config._normalizedLogger = {
|
|
31
|
-
error: (...args) => console.error(...args),
|
|
32
|
-
warn: (...args) => console.warn(...args),
|
|
33
|
-
info: (...args) => console.info(...args),
|
|
34
|
-
debug: (...args) => console.debug(...args)
|
|
35
|
-
};
|
|
36
|
-
} else {
|
|
37
|
-
config._normalizedLogger = {
|
|
38
|
-
error: () => {
|
|
39
|
-
},
|
|
40
|
-
warn: () => {
|
|
41
|
-
},
|
|
42
|
-
info: () => {
|
|
43
|
-
},
|
|
44
|
-
debug: () => {
|
|
45
|
-
}
|
|
46
|
-
};
|
|
47
|
-
}
|
|
48
|
-
return config._normalizedLogger;
|
|
49
|
-
}
|
|
50
|
-
if (typeof config.logger === "function") {
|
|
51
|
-
config._normalizedLogger = {
|
|
52
|
-
error: (...args) => config.logger("error", ...args),
|
|
53
|
-
warn: (...args) => config.logger("warn", ...args),
|
|
54
|
-
info: (...args) => config.logger("info", ...args),
|
|
55
|
-
debug: (...args) => config.logger("debug", ...args)
|
|
56
|
-
};
|
|
57
|
-
return config._normalizedLogger;
|
|
58
|
-
}
|
|
59
|
-
config._normalizedLogger = {
|
|
60
|
-
error: config.logger.error || (() => {
|
|
61
|
-
}),
|
|
62
|
-
warn: config.logger.warn || (() => {
|
|
63
|
-
}),
|
|
64
|
-
info: config.logger.info || (() => {
|
|
65
|
-
}),
|
|
66
|
-
debug: config.logger.debug || (() => {
|
|
67
|
-
})
|
|
68
|
-
};
|
|
69
|
-
return config._normalizedLogger;
|
|
70
|
-
}
|
package/cjs/impl/patch.cjs
DELETED
|
@@ -1,95 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __export = (target, all) => {
|
|
7
|
-
for (var name in all)
|
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
-
};
|
|
10
|
-
var __copyProps = (to, from, except, desc) => {
|
|
11
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
-
for (let key of __getOwnPropNames(from))
|
|
13
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
-
}
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
var patch_exports = {};
|
|
20
|
-
__export(patch_exports, {
|
|
21
|
-
patch: () => patch,
|
|
22
|
-
patchDangerously: () => patchDangerously,
|
|
23
|
-
sleep: () => sleep
|
|
24
|
-
});
|
|
25
|
-
module.exports = __toCommonJS(patch_exports);
|
|
26
|
-
var import_crud = require("./crud.cjs");
|
|
27
|
-
var import_patch = require("../schema/patch.cjs");
|
|
28
|
-
var import_logger = require("./logger.cjs");
|
|
29
|
-
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
30
|
-
const patch = import_patch.Patch.implement(async (config, id, properties) => {
|
|
31
|
-
const logger = (0, import_logger.createLogger)(config);
|
|
32
|
-
logger.info(`Starting patch operation for document ${id}`);
|
|
33
|
-
logger.debug("Patch properties:", properties);
|
|
34
|
-
const doc = await (0, import_crud.get)(config, id);
|
|
35
|
-
if (doc._rev !== properties._rev) {
|
|
36
|
-
const result2 = {};
|
|
37
|
-
result2.ok = false;
|
|
38
|
-
result2.error = "conflict";
|
|
39
|
-
result2.statusCode = 409;
|
|
40
|
-
return result2;
|
|
41
|
-
}
|
|
42
|
-
const updatedDoc = { ...doc, ...properties };
|
|
43
|
-
logger.debug("Merged document:", updatedDoc);
|
|
44
|
-
const result = await (0, import_crud.put)(config, updatedDoc);
|
|
45
|
-
logger.info(`Successfully patched document ${id}, rev: ${result.rev}`);
|
|
46
|
-
return result;
|
|
47
|
-
});
|
|
48
|
-
const patchDangerously = import_patch.PatchDangerously.implement(async (config, id, properties) => {
|
|
49
|
-
const logger = (0, import_logger.createLogger)(config);
|
|
50
|
-
const maxRetries = config.maxRetries || 5;
|
|
51
|
-
let delay = config.initialDelay || 1e3;
|
|
52
|
-
let attempts = 0;
|
|
53
|
-
logger.info(`Starting patch operation for document ${id}`);
|
|
54
|
-
logger.debug("Patch properties:", properties);
|
|
55
|
-
while (attempts <= maxRetries) {
|
|
56
|
-
logger.debug(`Attempt ${attempts + 1} of ${maxRetries + 1}`);
|
|
57
|
-
try {
|
|
58
|
-
const doc = await (0, import_crud.get)(config, id);
|
|
59
|
-
if (!doc) {
|
|
60
|
-
logger.warn(`Document ${id} not found`);
|
|
61
|
-
return { ok: false, statusCode: 404, error: "not_found" };
|
|
62
|
-
}
|
|
63
|
-
const updatedDoc = { ...doc, ...properties };
|
|
64
|
-
logger.debug("Merged document:", updatedDoc);
|
|
65
|
-
const result = await (0, import_crud.put)(config, updatedDoc);
|
|
66
|
-
if (result.ok) {
|
|
67
|
-
logger.info(`Successfully patched document ${id}, rev: ${result.rev}`);
|
|
68
|
-
return result;
|
|
69
|
-
}
|
|
70
|
-
attempts++;
|
|
71
|
-
if (attempts > maxRetries) {
|
|
72
|
-
logger.error(`Failed to patch ${id} after ${maxRetries} attempts`);
|
|
73
|
-
throw new Error(`Failed to patch after ${maxRetries} attempts`);
|
|
74
|
-
}
|
|
75
|
-
logger.warn(`Conflict detected for ${id}, retrying (attempt ${attempts})`);
|
|
76
|
-
await sleep(delay);
|
|
77
|
-
delay *= config.backoffFactor;
|
|
78
|
-
logger.debug(`Next retry delay: ${delay}ms`);
|
|
79
|
-
} catch (err) {
|
|
80
|
-
if (err.message === "not_found") {
|
|
81
|
-
logger.warn(`Document ${id} not found during patch operation`);
|
|
82
|
-
return { ok: false, statusCode: 404, error: "not_found" };
|
|
83
|
-
}
|
|
84
|
-
attempts++;
|
|
85
|
-
if (attempts > maxRetries) {
|
|
86
|
-
const error = `Failed to patch after ${maxRetries} attempts: ${err.message}`;
|
|
87
|
-
logger.error(error);
|
|
88
|
-
return { ok: false, statusCode: 500, error };
|
|
89
|
-
}
|
|
90
|
-
logger.warn(`Error during patch attempt ${attempts}: ${err.message}`);
|
|
91
|
-
await sleep(delay);
|
|
92
|
-
logger.debug(`Retrying after ${delay}ms`);
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
});
|
package/cjs/impl/query.cjs
DELETED
|
@@ -1,110 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __create = Object.create;
|
|
3
|
-
var __defProp = Object.defineProperty;
|
|
4
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
-
var __export = (target, all) => {
|
|
9
|
-
for (var name in all)
|
|
10
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
-
};
|
|
12
|
-
var __copyProps = (to, from, except, desc) => {
|
|
13
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
-
for (let key of __getOwnPropNames(from))
|
|
15
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
-
}
|
|
18
|
-
return to;
|
|
19
|
-
};
|
|
20
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
-
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
-
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
-
mod
|
|
27
|
-
));
|
|
28
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
-
var query_exports = {};
|
|
30
|
-
__export(query_exports, {
|
|
31
|
-
query: () => query,
|
|
32
|
-
queryString: () => queryString
|
|
33
|
-
});
|
|
34
|
-
module.exports = __toCommonJS(query_exports);
|
|
35
|
-
var import_zod = require("zod");
|
|
36
|
-
var import_needle = __toESM(require("needle"), 1);
|
|
37
|
-
var import_query = require("../schema/query.cjs");
|
|
38
|
-
var import_errors = require("./errors.cjs");
|
|
39
|
-
var import_logger = require("./logger.cjs");
|
|
40
|
-
var import_lodash = __toESM(require("lodash"), 1);
|
|
41
|
-
const { includes } = import_lodash.default;
|
|
42
|
-
const query = import_query.SimpleViewQuery.implement(async (config, view, options = {}) => {
|
|
43
|
-
const logger = (0, import_logger.createLogger)(config);
|
|
44
|
-
logger.info(`Starting view query: ${view}`);
|
|
45
|
-
logger.debug("Query options:", options);
|
|
46
|
-
let qs = queryString(options, ["key", "startkey", "endkey", "reduce", "group", "group_level", "stale", "limit"]);
|
|
47
|
-
logger.debug("Generated query string:", qs);
|
|
48
|
-
let method = "GET";
|
|
49
|
-
let payload = null;
|
|
50
|
-
const opts = {
|
|
51
|
-
json: true,
|
|
52
|
-
headers: {
|
|
53
|
-
"Content-Type": "application/json"
|
|
54
|
-
}
|
|
55
|
-
};
|
|
56
|
-
if (typeof options.keys !== "undefined") {
|
|
57
|
-
const MAX_URL_LENGTH = 2e3;
|
|
58
|
-
const keysAsString = `keys=${encodeURIComponent(JSON.stringify(options.keys))}`;
|
|
59
|
-
if (keysAsString.length + qs.length + 1 <= MAX_URL_LENGTH) {
|
|
60
|
-
qs += (qs[0] === "?" ? "&" : "?") + keysAsString;
|
|
61
|
-
} else {
|
|
62
|
-
method = "POST";
|
|
63
|
-
payload = { keys: options.keys };
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
const url = `${config.couch}/${view}?${qs.toString()}`;
|
|
67
|
-
let results;
|
|
68
|
-
try {
|
|
69
|
-
logger.debug(`Sending ${method} request to: ${url}`);
|
|
70
|
-
results = method === "GET" ? await (0, import_needle.default)("get", url, opts) : await (0, import_needle.default)("post", url, payload, opts);
|
|
71
|
-
} catch (err) {
|
|
72
|
-
logger.error("Network error during query:", err);
|
|
73
|
-
import_errors.RetryableError.handleNetworkError(err);
|
|
74
|
-
}
|
|
75
|
-
if (!results) {
|
|
76
|
-
logger.error("No response received from query request");
|
|
77
|
-
throw new import_errors.RetryableError("no response", 503);
|
|
78
|
-
}
|
|
79
|
-
const body = results.body;
|
|
80
|
-
if (import_errors.RetryableError.isRetryableStatusCode(results.statusCode)) {
|
|
81
|
-
logger.warn(`Retryable status code received: ${results.statusCode}`);
|
|
82
|
-
throw new import_errors.RetryableError(body.error || "retryable error during query", results.statusCode);
|
|
83
|
-
}
|
|
84
|
-
if (body.error) {
|
|
85
|
-
logger.error(`Query error: ${body.error}`);
|
|
86
|
-
throw new Error(body.error);
|
|
87
|
-
}
|
|
88
|
-
logger.info(`Successfully executed view query: ${view}`);
|
|
89
|
-
logger.debug("Query response:", body);
|
|
90
|
-
return body;
|
|
91
|
-
});
|
|
92
|
-
function queryString(options = {}, params) {
|
|
93
|
-
const parts = Object.keys(options).map((key) => {
|
|
94
|
-
let value = options[key];
|
|
95
|
-
if (includes(params, key)) {
|
|
96
|
-
if (typeof value === "string" && key !== "stale") value = `"${value}"`;
|
|
97
|
-
if (Array.isArray(value)) {
|
|
98
|
-
value = "[" + value.map((i) => {
|
|
99
|
-
if (i === null) return "null";
|
|
100
|
-
if (typeof i === "string") return `"${i}"`;
|
|
101
|
-
if (typeof i === "object" && Object.keys(i).length === 0) return "{}";
|
|
102
|
-
if (typeof i === "object") return JSON.stringify(i);
|
|
103
|
-
return i;
|
|
104
|
-
}).join(",") + "]";
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
return `${key}=${value}`;
|
|
108
|
-
});
|
|
109
|
-
return parts.join("&");
|
|
110
|
-
}
|
|
@@ -1,99 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __export = (target, all) => {
|
|
7
|
-
for (var name in all)
|
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
-
};
|
|
10
|
-
var __copyProps = (to, from, except, desc) => {
|
|
11
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
-
for (let key of __getOwnPropNames(from))
|
|
13
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
-
}
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
var queryBuilder_exports = {};
|
|
20
|
-
__export(queryBuilder_exports, {
|
|
21
|
-
QueryBuilder: () => QueryBuilder,
|
|
22
|
-
createQuery: () => createQuery
|
|
23
|
-
});
|
|
24
|
-
module.exports = __toCommonJS(queryBuilder_exports);
|
|
25
|
-
class QueryBuilder {
|
|
26
|
-
/** @type {QueryOptions} */
|
|
27
|
-
#options = {};
|
|
28
|
-
/**
|
|
29
|
-
* @param {any} key
|
|
30
|
-
* @returns {QueryBuilder}
|
|
31
|
-
*/
|
|
32
|
-
key(key) {
|
|
33
|
-
this.#options.key = key;
|
|
34
|
-
return this;
|
|
35
|
-
}
|
|
36
|
-
/**
|
|
37
|
-
* @param {any} startkey
|
|
38
|
-
* @returns {QueryBuilder}
|
|
39
|
-
*/
|
|
40
|
-
startKey(startkey) {
|
|
41
|
-
this.#options.startkey = startkey;
|
|
42
|
-
return this;
|
|
43
|
-
}
|
|
44
|
-
/**
|
|
45
|
-
* @param {any} endkey
|
|
46
|
-
* @returns {QueryBuilder}
|
|
47
|
-
*/
|
|
48
|
-
endKey(endkey) {
|
|
49
|
-
this.#options.endkey = endkey;
|
|
50
|
-
return this;
|
|
51
|
-
}
|
|
52
|
-
/**
|
|
53
|
-
* @param {boolean} reduce
|
|
54
|
-
* @returns {QueryBuilder}
|
|
55
|
-
*/
|
|
56
|
-
reduce(reduce = true) {
|
|
57
|
-
this.#options.reduce = reduce;
|
|
58
|
-
return this;
|
|
59
|
-
}
|
|
60
|
-
/**
|
|
61
|
-
* @param {boolean} group
|
|
62
|
-
* @returns {QueryBuilder}
|
|
63
|
-
*/
|
|
64
|
-
group(group = true) {
|
|
65
|
-
this.#options.group = group;
|
|
66
|
-
return this;
|
|
67
|
-
}
|
|
68
|
-
/**
|
|
69
|
-
* @param {number} level
|
|
70
|
-
* @returns {QueryBuilder}
|
|
71
|
-
*/
|
|
72
|
-
groupLevel(level) {
|
|
73
|
-
this.#options.group_level = level;
|
|
74
|
-
return this;
|
|
75
|
-
}
|
|
76
|
-
/**
|
|
77
|
-
* @param {string} stale
|
|
78
|
-
* @returns {QueryBuilder}
|
|
79
|
-
*/
|
|
80
|
-
stale(stale) {
|
|
81
|
-
this.#options.stale = stale;
|
|
82
|
-
return this;
|
|
83
|
-
}
|
|
84
|
-
/**
|
|
85
|
-
* @param {number} limit
|
|
86
|
-
* @returns {QueryBuilder}
|
|
87
|
-
*/
|
|
88
|
-
limit(limit) {
|
|
89
|
-
this.#options.limit = limit;
|
|
90
|
-
return this;
|
|
91
|
-
}
|
|
92
|
-
/**
|
|
93
|
-
* @returns {QueryOptions}
|
|
94
|
-
*/
|
|
95
|
-
build() {
|
|
96
|
-
return { ...this.#options };
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
const createQuery = () => new QueryBuilder();
|
package/cjs/impl/retry.cjs
DELETED
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __export = (target, all) => {
|
|
7
|
-
for (var name in all)
|
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
-
};
|
|
10
|
-
var __copyProps = (to, from, except, desc) => {
|
|
11
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
-
for (let key of __getOwnPropNames(from))
|
|
13
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
-
}
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
var retry_exports = {};
|
|
20
|
-
__export(retry_exports, {
|
|
21
|
-
withRetry: () => withRetry
|
|
22
|
-
});
|
|
23
|
-
module.exports = __toCommonJS(retry_exports);
|
|
24
|
-
var import_errors = require("./errors.cjs");
|
|
25
|
-
var import_patch = require("./patch.cjs");
|
|
26
|
-
function withRetry(fn, options = {}) {
|
|
27
|
-
const {
|
|
28
|
-
maxRetries = 3,
|
|
29
|
-
initialDelay = 1e3,
|
|
30
|
-
// 1 second
|
|
31
|
-
backoffFactor = 2
|
|
32
|
-
// exponential backoff multiplier
|
|
33
|
-
} = options;
|
|
34
|
-
return async (...args) => {
|
|
35
|
-
let lastError;
|
|
36
|
-
let delay = initialDelay;
|
|
37
|
-
for (let attempt = 0; attempt <= maxRetries; attempt++) {
|
|
38
|
-
try {
|
|
39
|
-
return await fn(...args);
|
|
40
|
-
} catch (error) {
|
|
41
|
-
lastError = error;
|
|
42
|
-
if (!(error instanceof import_errors.RetryableError)) {
|
|
43
|
-
throw error;
|
|
44
|
-
}
|
|
45
|
-
if (attempt === maxRetries) {
|
|
46
|
-
throw error;
|
|
47
|
-
}
|
|
48
|
-
await (0, import_patch.sleep)(delay);
|
|
49
|
-
delay *= backoffFactor;
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
throw lastError;
|
|
53
|
-
};
|
|
54
|
-
}
|