arangojs
Version:
The official ArangoDB JavaScript driver.
109 lines • 4.25 kB
JavaScript
;
/**
* Transaction ID scope for opt-in asynchronous stream transaction steps.
*
* @packageDocumentation
* @internal
*/
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.runTransactionStep = exports.getActiveTransactionId = void 0;
let nodeAsyncLocalStorage = null;
let nodeAsyncLocalStorageReady = null;
let nodeAsyncLocalStorageError;
const browserTransactionIds = new WeakMap();
function isNodeRuntime() {
return (typeof process !== "undefined" &&
typeof process.versions === "object" &&
process.versions !== null &&
typeof process.versions.node === "string");
}
function ensureNodeAsyncLocalStorage() {
if (!isNodeRuntime() || nodeAsyncLocalStorage) {
return Promise.resolve();
}
if (!nodeAsyncLocalStorageReady) {
nodeAsyncLocalStorageReady = (async () => {
try {
// Keep the specifier dynamic so browser bundlers do not try to resolve
// the Node-only module.
const specifier = ["node:", "async_hooks"].join("");
const { AsyncLocalStorage } = (await Promise.resolve(`${specifier}`).then(s => __importStar(require(s))));
nodeAsyncLocalStorage =
new AsyncLocalStorage();
}
catch (error) {
nodeAsyncLocalStorageError = error;
}
})();
}
return nodeAsyncLocalStorageReady;
}
/**
* Returns the transaction ID scoped to `connection`, if any.
*/
function getActiveTransactionId(connection) {
const context = nodeAsyncLocalStorage?.getStore();
if (context?.connection === connection) {
return context.transactionId;
}
return browserTransactionIds.get(connection);
}
exports.getActiveTransactionId = getActiveTransactionId;
/**
* Runs `callback` with `transactionId` scoped to `connection` until the
* callback's returned Promise settles.
*/
async function runTransactionStep(connection, transactionId, callback) {
await ensureNodeAsyncLocalStorage();
const invoke = () => {
const promise = callback();
if (!promise || typeof promise.then !== "function") {
throw new Error("Transaction callback was not an async function or did not return a promise!");
}
return Promise.resolve(promise);
};
if (isNodeRuntime()) {
if (!nodeAsyncLocalStorage) {
const detail = nodeAsyncLocalStorageError instanceof Error
? `: ${nodeAsyncLocalStorageError.message}`
: "";
throw new Error(`Async transaction context is unavailable in this Node.js runtime${detail}`);
}
return nodeAsyncLocalStorage.run({ connection, transactionId }, invoke);
}
if (browserTransactionIds.has(connection)) {
throw new Error("Concurrent asynchronous transaction steps on the same connection are not supported in browsers.");
}
browserTransactionIds.set(connection, transactionId);
try {
return await invoke();
}
finally {
browserTransactionIds.delete(connection);
}
}
exports.runTransactionStep = runTransactionStep;
//# sourceMappingURL=transaction-context.js.map