zephyr-agent
Version:
Zephyr plugin agent
109 lines (108 loc) • 4.77 kB
JavaScript
;
var __webpack_require__ = {};
(()=>{
__webpack_require__.d = (exports1, getters, values)=>{
var define = (defs, kind)=>{
for(var key in defs)if (__webpack_require__.o(defs, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
enumerable: true,
[kind]: defs[key]
});
};
define(getters, "get");
define(values, "value");
};
})();
(()=>{
__webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
})();
(()=>{
__webpack_require__.r = (exports1)=>{
if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
value: 'Module'
});
Object.defineProperty(exports1, '__esModule', {
value: true
});
};
})();
var __webpack_exports__ = {};
__webpack_require__.r(__webpack_exports__);
__webpack_require__.d(__webpack_exports__, {
getSessionKey: ()=>getSessionKey,
waitForUnlock: ()=>waitForUnlock
});
const external_node_crypto_namespaceObject = require("node:crypto");
const external_node_fs_namespaceObject = require("node:fs");
const promises_namespaceObject = require("node:timers/promises");
const proper_lockfile2_namespaceObject = require("@bybrave/proper-lockfile2");
const auth_flags_js_namespaceObject = require("../auth/auth-flags.js");
const index_js_namespaceObject = require("../logging/index.js");
const external_storage_keys_js_namespaceObject = require("./storage-keys.js");
const SESSION_LENGTH = 9;
const UNLOCK_INTERVAL = 250;
function getSessionKey() {
let session = (0, external_node_crypto_namespaceObject.randomBytes)(SESSION_LENGTH);
const unlock = safeLockSync();
if (unlock) {
index_js_namespaceObject.ze_log.misc('Lock acquired, writing session key to lockfile');
external_node_fs_namespaceObject.writeFileSync(external_storage_keys_js_namespaceObject.ZE_SESSION_LOCK, session, {
flush: true,
mode: 384
});
(0, external_storage_keys_js_namespaceObject.ensurePrivateFilePermissions)(external_storage_keys_js_namespaceObject.ZE_SESSION_LOCK);
} else {
(0, external_storage_keys_js_namespaceObject.ensurePrivateFilePermissions)(external_storage_keys_js_namespaceObject.ZE_SESSION_LOCK);
index_js_namespaceObject.ze_log.misc('Lock is already held by another process, using the same session key');
session = external_node_fs_namespaceObject.readFileSync(external_storage_keys_js_namespaceObject.ZE_SESSION_LOCK);
if (session.length !== SESSION_LENGTH) session = external_node_fs_namespaceObject.readFileSync(external_storage_keys_js_namespaceObject.ZE_SESSION_LOCK);
}
return {
owner: !!unlock,
session: session.toString('base64url'),
[Symbol.dispose] () {
unlock?.();
}
};
}
function safeLockSync(createIfNotExists = true) {
try {
return (0, proper_lockfile2_namespaceObject.lockSync)(external_storage_keys_js_namespaceObject.ZE_SESSION_LOCK, {
stale: auth_flags_js_namespaceObject.DEFAULT_AUTH_COMPLETION_TIMEOUT_MS
});
} catch (ex) {
const error = ex;
if ('ELOCKED' === error.code) return null;
if ('ENOENT' === error.code && createIfNotExists) {
try {
external_node_fs_namespaceObject.writeFileSync(external_storage_keys_js_namespaceObject.ZE_SESSION_LOCK, '', {
encoding: 'utf8',
flag: 'wx',
mode: 384
});
} catch (creationError) {
if ('EEXIST' !== creationError.code) throw creationError;
}
(0, external_storage_keys_js_namespaceObject.ensurePrivateFilePermissions)(external_storage_keys_js_namespaceObject.ZE_SESSION_LOCK);
return safeLockSync(false);
}
throw error;
}
}
async function waitForUnlock(signal) {
for await (const _ of (0, promises_namespaceObject.setInterval)(UNLOCK_INTERVAL, null, {
ref: false,
signal
}))if (!(0, proper_lockfile2_namespaceObject.checkSync)(external_storage_keys_js_namespaceObject.ZE_SESSION_LOCK, {
stale: auth_flags_js_namespaceObject.DEFAULT_AUTH_COMPLETION_TIMEOUT_MS
})) return;
}
exports.getSessionKey = __webpack_exports__.getSessionKey;
exports.waitForUnlock = __webpack_exports__.waitForUnlock;
for(var __rspack_i in __webpack_exports__)if (-1 === [
"getSessionKey",
"waitForUnlock"
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
Object.defineProperty(exports, '__esModule', {
value: true
});
//# sourceMappingURL=session-lock.js.map