@cloudbase/cloudbase-mcp 2.4.0-alpha.0 → 2.4.0
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/dist/cli.cjs +423 -18
- package/dist/index.cjs +423 -18
- package/dist/index.js +7716 -7630
- package/package.json +2 -1
package/dist/cli.cjs
CHANGED
|
@@ -135174,7 +135174,7 @@ class TelemetryReporter {
|
|
|
135174
135174
|
const nodeVersion = process.version; // Node.js版本
|
|
135175
135175
|
const arch = os_1.default.arch(); // 系统架构
|
|
135176
135176
|
// 从构建时注入的版本号获取MCP版本信息
|
|
135177
|
-
const mcpVersion = process.env.npm_package_version || "2.4.0
|
|
135177
|
+
const mcpVersion = process.env.npm_package_version || "2.4.0" || 0;
|
|
135178
135178
|
return {
|
|
135179
135179
|
userAgent: `${osType} ${osRelease} ${arch} ${nodeVersion} CloudBase-MCP/${mcpVersion}`,
|
|
135180
135180
|
deviceId: this.deviceId,
|
|
@@ -185656,6 +185656,7 @@ exports.getClaudePrompt = getClaudePrompt;
|
|
|
185656
185656
|
exports.registerRagTools = registerRagTools;
|
|
185657
185657
|
const adm_zip_1 = __importDefault(__webpack_require__(30283));
|
|
185658
185658
|
const fs = __importStar(__webpack_require__(79748));
|
|
185659
|
+
const lockfile_1 = __importDefault(__webpack_require__(80127));
|
|
185659
185660
|
const os = __importStar(__webpack_require__(21820));
|
|
185660
185661
|
const path = __importStar(__webpack_require__(39902));
|
|
185661
185662
|
const zod_1 = __webpack_require__(21614);
|
|
@@ -185672,7 +185673,39 @@ const KnowledgeBaseIdMap = {
|
|
|
185672
185673
|
// ============ 缓存配置 ============
|
|
185673
185674
|
const CACHE_BASE_DIR = path.join(os.homedir(), ".cloudbase-mcp");
|
|
185674
185675
|
const CACHE_META_FILE = path.join(CACHE_BASE_DIR, "cache-meta.json");
|
|
185676
|
+
const LOCK_FILE = path.join(CACHE_BASE_DIR, ".download.lock");
|
|
185675
185677
|
const DEFAULT_CACHE_TTL_MS = 24 * 60 * 60 * 1000; // 默认 24 小时
|
|
185678
|
+
// Promise wrapper for lockfile methods
|
|
185679
|
+
function acquireLock(lockPath, options) {
|
|
185680
|
+
return new Promise((resolve, reject) => {
|
|
185681
|
+
if (options) {
|
|
185682
|
+
lockfile_1.default.lock(lockPath, options, (err) => {
|
|
185683
|
+
if (err)
|
|
185684
|
+
reject(err);
|
|
185685
|
+
else
|
|
185686
|
+
resolve();
|
|
185687
|
+
});
|
|
185688
|
+
}
|
|
185689
|
+
else {
|
|
185690
|
+
lockfile_1.default.lock(lockPath, (err) => {
|
|
185691
|
+
if (err)
|
|
185692
|
+
reject(err);
|
|
185693
|
+
else
|
|
185694
|
+
resolve();
|
|
185695
|
+
});
|
|
185696
|
+
}
|
|
185697
|
+
});
|
|
185698
|
+
}
|
|
185699
|
+
function releaseLock(lockPath) {
|
|
185700
|
+
return new Promise((resolve, reject) => {
|
|
185701
|
+
lockfile_1.default.unlock(lockPath, (err) => {
|
|
185702
|
+
if (err)
|
|
185703
|
+
reject(err);
|
|
185704
|
+
else
|
|
185705
|
+
resolve();
|
|
185706
|
+
});
|
|
185707
|
+
});
|
|
185708
|
+
}
|
|
185676
185709
|
// 支持环境变量 CLOUDBASE_MCP_CACHE_TTL_MS 控制缓存过期时间(毫秒)
|
|
185677
185710
|
const parsedCacheTTL = process.env.CLOUDBASE_MCP_CACHE_TTL_MS
|
|
185678
185711
|
? parseInt(process.env.CLOUDBASE_MCP_CACHE_TTL_MS, 10)
|
|
@@ -185842,14 +185875,19 @@ async function _doDownloadResources() {
|
|
|
185842
185875
|
async function downloadResources() {
|
|
185843
185876
|
const webTemplateDir = path.join(CACHE_BASE_DIR, "web-template");
|
|
185844
185877
|
const openAPIDir = path.join(CACHE_BASE_DIR, "openapi");
|
|
185845
|
-
//
|
|
185878
|
+
// 如果已有下载任务在进行中,共享该 Promise
|
|
185879
|
+
if (resourceDownloadPromise) {
|
|
185880
|
+
(0, logger_js_1.debug)("[downloadResources] 共享已有下载任务");
|
|
185881
|
+
return resourceDownloadPromise;
|
|
185882
|
+
}
|
|
185883
|
+
// 先快速检查缓存(不需要锁,因为只是读取)
|
|
185846
185884
|
if (await canUseCache()) {
|
|
185847
185885
|
try {
|
|
185848
185886
|
// 检查两个目录都存在
|
|
185849
185887
|
await Promise.all([fs.access(webTemplateDir), fs.access(openAPIDir)]);
|
|
185850
185888
|
const files = await fs.readdir(openAPIDir);
|
|
185851
185889
|
if (files.length > 0) {
|
|
185852
|
-
(0, logger_js_1.debug)("[downloadResources]
|
|
185890
|
+
(0, logger_js_1.debug)("[downloadResources] 使用缓存(快速路径)");
|
|
185853
185891
|
return {
|
|
185854
185892
|
webTemplateDir,
|
|
185855
185893
|
openAPIDocs: OPENAPI_SOURCES.map((source) => ({
|
|
@@ -185864,21 +185902,61 @@ async function downloadResources() {
|
|
|
185864
185902
|
// 缓存无效,需要重新下载
|
|
185865
185903
|
}
|
|
185866
185904
|
}
|
|
185867
|
-
//
|
|
185868
|
-
if (resourceDownloadPromise) {
|
|
185869
|
-
(0, logger_js_1.debug)("[downloadResources] 共享已有下载任务");
|
|
185870
|
-
return resourceDownloadPromise;
|
|
185871
|
-
}
|
|
185872
|
-
// 创建新的下载任务
|
|
185905
|
+
// 创建新的下载任务,使用文件锁保护
|
|
185873
185906
|
(0, logger_js_1.debug)("[downloadResources] 开始新下载任务");
|
|
185874
185907
|
await fs.mkdir(CACHE_BASE_DIR, { recursive: true });
|
|
185875
|
-
resourceDownloadPromise =
|
|
185876
|
-
|
|
185877
|
-
|
|
185878
|
-
|
|
185879
|
-
|
|
185880
|
-
|
|
185881
|
-
|
|
185908
|
+
resourceDownloadPromise = (async () => {
|
|
185909
|
+
// 尝试获取文件锁,最多等待 6 秒(30 次 × 200ms),每 200ms 轮询一次
|
|
185910
|
+
let lockAcquired = false;
|
|
185911
|
+
try {
|
|
185912
|
+
await acquireLock(LOCK_FILE, {
|
|
185913
|
+
wait: 30 * 200, // 总等待时间:6000ms (6 秒)
|
|
185914
|
+
pollPeriod: 200, // 轮询间隔:200ms
|
|
185915
|
+
stale: 5 * 60 * 1000, // 5 分钟,如果锁文件超过这个时间认为是过期的
|
|
185916
|
+
});
|
|
185917
|
+
lockAcquired = true;
|
|
185918
|
+
(0, logger_js_1.debug)("[downloadResources] 文件锁已获取");
|
|
185919
|
+
// 在持有锁的情况下再次检查缓存(可能其他进程已经下载完成)
|
|
185920
|
+
if (await canUseCache()) {
|
|
185921
|
+
try {
|
|
185922
|
+
// 检查两个目录都存在
|
|
185923
|
+
await Promise.all([fs.access(webTemplateDir), fs.access(openAPIDir)]);
|
|
185924
|
+
const files = await fs.readdir(openAPIDir);
|
|
185925
|
+
if (files.length > 0) {
|
|
185926
|
+
(0, logger_js_1.debug)("[downloadResources] 使用缓存(在锁保护下检查)");
|
|
185927
|
+
return {
|
|
185928
|
+
webTemplateDir,
|
|
185929
|
+
openAPIDocs: OPENAPI_SOURCES.map((source) => ({
|
|
185930
|
+
name: source.name,
|
|
185931
|
+
description: source.description,
|
|
185932
|
+
absolutePath: path.join(openAPIDir, `${source.name}.openapi.yaml`),
|
|
185933
|
+
})).filter((item) => files.includes(`${item.name}.openapi.yaml`)),
|
|
185934
|
+
};
|
|
185935
|
+
}
|
|
185936
|
+
}
|
|
185937
|
+
catch {
|
|
185938
|
+
// 缓存无效,需要重新下载
|
|
185939
|
+
}
|
|
185940
|
+
}
|
|
185941
|
+
// 执行下载
|
|
185942
|
+
const result = await _doDownloadResources();
|
|
185943
|
+
await updateCache();
|
|
185944
|
+
(0, logger_js_1.debug)("[downloadResources] 缓存已更新");
|
|
185945
|
+
return result;
|
|
185946
|
+
}
|
|
185947
|
+
finally {
|
|
185948
|
+
// 释放文件锁
|
|
185949
|
+
if (lockAcquired) {
|
|
185950
|
+
try {
|
|
185951
|
+
await releaseLock(LOCK_FILE);
|
|
185952
|
+
(0, logger_js_1.debug)("[downloadResources] 文件锁已释放");
|
|
185953
|
+
}
|
|
185954
|
+
catch (error) {
|
|
185955
|
+
(0, logger_js_1.warn)("[downloadResources] 释放文件锁失败", { error });
|
|
185956
|
+
}
|
|
185957
|
+
}
|
|
185958
|
+
}
|
|
185959
|
+
})().finally(() => {
|
|
185882
185960
|
resourceDownloadPromise = null;
|
|
185883
185961
|
});
|
|
185884
185962
|
return resourceDownloadPromise;
|
|
@@ -201022,7 +201100,7 @@ ${envIdSection}
|
|
|
201022
201100
|
## 环境信息
|
|
201023
201101
|
- 操作系统: ${os_1.default.type()} ${os_1.default.release()}
|
|
201024
201102
|
- Node.js版本: ${process.version}
|
|
201025
|
-
- MCP 版本:${process.env.npm_package_version || "2.4.0
|
|
201103
|
+
- MCP 版本:${process.env.npm_package_version || "2.4.0" || 0}
|
|
201026
201104
|
- 系统架构: ${os_1.default.arch()}
|
|
201027
201105
|
- 时间: ${new Date().toISOString()}
|
|
201028
201106
|
- 请求ID: ${requestId}
|
|
@@ -215691,7 +215769,7 @@ function registerSetupTools(server) {
|
|
|
215691
215769
|
title: "下载项目模板",
|
|
215692
215770
|
description: `自动下载并部署CloudBase项目模板。⚠️ **MANDATORY FOR NEW PROJECTS** ⚠️
|
|
215693
215771
|
|
|
215694
|
-
**CRITICAL**: This tool MUST be called FIRST when starting a new project.\n\n支持的模板:\n- react: React + CloudBase 全栈应用模板\n- vue: Vue + CloudBase 全栈应用模板\n- miniprogram: 微信小程序 + 云开发模板 \n- uniapp: UniApp + CloudBase 跨端应用模板\n- rules: 只包含AI编辑器配置文件(包含Cursor、WindSurf、CodeBuddy等所有主流编辑器配置),适合在已有项目中补充AI编辑器配置\n\n支持的IDE类型:\n- all: 下载所有IDE配置(默认)\n- cursor: Cursor AI编辑器\n- windsurf: WindSurf AI编辑器\n- codebuddy: CodeBuddy AI编辑器\n- claude-code: Claude Code AI编辑器\n- cline: Cline AI编辑器\n- gemini-cli: Gemini CLI\n- opencode: OpenCode AI编辑器\n- qwen-code: 通义灵码\n- baidu-comate: 百度Comate\n- openai-codex-cli: OpenAI Codex CLI\n- augment-code: Augment Code\n- github-copilot: GitHub Copilot\n- roocode: RooCode AI编辑器\n- tongyi-lingma: 通义灵码\n- trae: Trae AI编辑器\n- qoder: Qoder AI编辑器\n- antigravity: Google Antigravity AI编辑器\n- vscode: Visual Studio Code\n\n特别说明:\n- rules 模板会自动包含当前 mcp 版本号信息(版本号:${ true ? "2.4.0
|
|
215772
|
+
**CRITICAL**: This tool MUST be called FIRST when starting a new project.\n\n支持的模板:\n- react: React + CloudBase 全栈应用模板\n- vue: Vue + CloudBase 全栈应用模板\n- miniprogram: 微信小程序 + 云开发模板 \n- uniapp: UniApp + CloudBase 跨端应用模板\n- rules: 只包含AI编辑器配置文件(包含Cursor、WindSurf、CodeBuddy等所有主流编辑器配置),适合在已有项目中补充AI编辑器配置\n\n支持的IDE类型:\n- all: 下载所有IDE配置(默认)\n- cursor: Cursor AI编辑器\n- windsurf: WindSurf AI编辑器\n- codebuddy: CodeBuddy AI编辑器\n- claude-code: Claude Code AI编辑器\n- cline: Cline AI编辑器\n- gemini-cli: Gemini CLI\n- opencode: OpenCode AI编辑器\n- qwen-code: 通义灵码\n- baidu-comate: 百度Comate\n- openai-codex-cli: OpenAI Codex CLI\n- augment-code: Augment Code\n- github-copilot: GitHub Copilot\n- roocode: RooCode AI编辑器\n- tongyi-lingma: 通义灵码\n- trae: Trae AI编辑器\n- qoder: Qoder AI编辑器\n- antigravity: Google Antigravity AI编辑器\n- vscode: Visual Studio Code\n\n特别说明:\n- rules 模板会自动包含当前 mcp 版本号信息(版本号:${ true ? "2.4.0" : 0}),便于后续维护和版本追踪\n- 下载 rules 模板时,如果项目中已存在 README.md 文件,系统会自动保护该文件不被覆盖(除非设置 overwrite=true)`,
|
|
215695
215773
|
inputSchema: {
|
|
215696
215774
|
template: zod_1.z
|
|
215697
215775
|
.enum(["react", "vue", "miniprogram", "uniapp", "rules"])
|
|
@@ -224736,6 +224814,333 @@ function resolveIds(schema) {
|
|
|
224736
224814
|
}
|
|
224737
224815
|
|
|
224738
224816
|
|
|
224817
|
+
/***/ }),
|
|
224818
|
+
|
|
224819
|
+
/***/ 80127:
|
|
224820
|
+
/***/ ((__unused_webpack_module, exports, __webpack_require__) => {
|
|
224821
|
+
|
|
224822
|
+
var fs = __webpack_require__(29021)
|
|
224823
|
+
|
|
224824
|
+
var wx = 'wx'
|
|
224825
|
+
if (process.version.match(/^v0\.[0-6]/)) {
|
|
224826
|
+
var c = __webpack_require__(81115)
|
|
224827
|
+
wx = c.O_TRUNC | c.O_CREAT | c.O_WRONLY | c.O_EXCL
|
|
224828
|
+
}
|
|
224829
|
+
|
|
224830
|
+
var os = __webpack_require__(21820)
|
|
224831
|
+
exports.filetime = 'ctime'
|
|
224832
|
+
if (os.platform() == "win32") {
|
|
224833
|
+
exports.filetime = 'mtime'
|
|
224834
|
+
}
|
|
224835
|
+
|
|
224836
|
+
var debug
|
|
224837
|
+
var util = __webpack_require__(28354)
|
|
224838
|
+
if (util.debuglog)
|
|
224839
|
+
debug = util.debuglog('LOCKFILE')
|
|
224840
|
+
else if (/\blockfile\b/i.test(process.env.NODE_DEBUG))
|
|
224841
|
+
debug = function() {
|
|
224842
|
+
var msg = util.format.apply(util, arguments)
|
|
224843
|
+
console.error('LOCKFILE %d %s', process.pid, msg)
|
|
224844
|
+
}
|
|
224845
|
+
else
|
|
224846
|
+
debug = function() {}
|
|
224847
|
+
|
|
224848
|
+
var locks = {}
|
|
224849
|
+
|
|
224850
|
+
function hasOwnProperty (obj, prop) {
|
|
224851
|
+
return Object.prototype.hasOwnProperty.call(obj, prop)
|
|
224852
|
+
}
|
|
224853
|
+
|
|
224854
|
+
var onExit = __webpack_require__(29468)
|
|
224855
|
+
onExit(function () {
|
|
224856
|
+
debug('exit listener')
|
|
224857
|
+
// cleanup
|
|
224858
|
+
Object.keys(locks).forEach(exports.unlockSync)
|
|
224859
|
+
})
|
|
224860
|
+
|
|
224861
|
+
// XXX https://github.com/joyent/node/issues/3555
|
|
224862
|
+
// Remove when node 0.8 is deprecated.
|
|
224863
|
+
if (/^v0\.[0-8]\./.test(process.version)) {
|
|
224864
|
+
debug('uncaughtException, version = %s', process.version)
|
|
224865
|
+
process.on('uncaughtException', function H (er) {
|
|
224866
|
+
debug('uncaughtException')
|
|
224867
|
+
var l = process.listeners('uncaughtException').filter(function (h) {
|
|
224868
|
+
return h !== H
|
|
224869
|
+
})
|
|
224870
|
+
if (!l.length) {
|
|
224871
|
+
// cleanup
|
|
224872
|
+
try { Object.keys(locks).forEach(exports.unlockSync) } catch (e) {}
|
|
224873
|
+
process.removeListener('uncaughtException', H)
|
|
224874
|
+
throw er
|
|
224875
|
+
}
|
|
224876
|
+
})
|
|
224877
|
+
}
|
|
224878
|
+
|
|
224879
|
+
exports.unlock = function (path, cb) {
|
|
224880
|
+
debug('unlock', path)
|
|
224881
|
+
// best-effort. unlocking an already-unlocked lock is a noop
|
|
224882
|
+
delete locks[path]
|
|
224883
|
+
fs.unlink(path, function (unlinkEr) { cb && cb() })
|
|
224884
|
+
}
|
|
224885
|
+
|
|
224886
|
+
exports.unlockSync = function (path) {
|
|
224887
|
+
debug('unlockSync', path)
|
|
224888
|
+
// best-effort. unlocking an already-unlocked lock is a noop
|
|
224889
|
+
try { fs.unlinkSync(path) } catch (er) {}
|
|
224890
|
+
delete locks[path]
|
|
224891
|
+
}
|
|
224892
|
+
|
|
224893
|
+
|
|
224894
|
+
// if the file can be opened in readonly mode, then it's there.
|
|
224895
|
+
// if the error is something other than ENOENT, then it's not.
|
|
224896
|
+
exports.check = function (path, opts, cb) {
|
|
224897
|
+
if (typeof opts === 'function') cb = opts, opts = {}
|
|
224898
|
+
debug('check', path, opts)
|
|
224899
|
+
fs.open(path, 'r', function (er, fd) {
|
|
224900
|
+
if (er) {
|
|
224901
|
+
if (er.code !== 'ENOENT') return cb(er)
|
|
224902
|
+
return cb(null, false)
|
|
224903
|
+
}
|
|
224904
|
+
|
|
224905
|
+
if (!opts.stale) {
|
|
224906
|
+
return fs.close(fd, function (er) {
|
|
224907
|
+
return cb(er, true)
|
|
224908
|
+
})
|
|
224909
|
+
}
|
|
224910
|
+
|
|
224911
|
+
fs.fstat(fd, function (er, st) {
|
|
224912
|
+
if (er) return fs.close(fd, function (er2) {
|
|
224913
|
+
return cb(er)
|
|
224914
|
+
})
|
|
224915
|
+
|
|
224916
|
+
fs.close(fd, function (er) {
|
|
224917
|
+
var age = Date.now() - st[exports.filetime].getTime()
|
|
224918
|
+
return cb(er, age <= opts.stale)
|
|
224919
|
+
})
|
|
224920
|
+
})
|
|
224921
|
+
})
|
|
224922
|
+
}
|
|
224923
|
+
|
|
224924
|
+
exports.checkSync = function (path, opts) {
|
|
224925
|
+
opts = opts || {}
|
|
224926
|
+
debug('checkSync', path, opts)
|
|
224927
|
+
if (opts.wait) {
|
|
224928
|
+
throw new Error('opts.wait not supported sync for obvious reasons')
|
|
224929
|
+
}
|
|
224930
|
+
|
|
224931
|
+
try {
|
|
224932
|
+
var fd = fs.openSync(path, 'r')
|
|
224933
|
+
} catch (er) {
|
|
224934
|
+
if (er.code !== 'ENOENT') throw er
|
|
224935
|
+
return false
|
|
224936
|
+
}
|
|
224937
|
+
|
|
224938
|
+
if (!opts.stale) {
|
|
224939
|
+
try { fs.closeSync(fd) } catch (er) {}
|
|
224940
|
+
return true
|
|
224941
|
+
}
|
|
224942
|
+
|
|
224943
|
+
// file exists. however, might be stale
|
|
224944
|
+
if (opts.stale) {
|
|
224945
|
+
try {
|
|
224946
|
+
var st = fs.fstatSync(fd)
|
|
224947
|
+
} finally {
|
|
224948
|
+
fs.closeSync(fd)
|
|
224949
|
+
}
|
|
224950
|
+
var age = Date.now() - st[exports.filetime].getTime()
|
|
224951
|
+
return (age <= opts.stale)
|
|
224952
|
+
}
|
|
224953
|
+
}
|
|
224954
|
+
|
|
224955
|
+
|
|
224956
|
+
|
|
224957
|
+
var req = 1
|
|
224958
|
+
exports.lock = function (path, opts, cb) {
|
|
224959
|
+
if (typeof opts === 'function') cb = opts, opts = {}
|
|
224960
|
+
opts.req = opts.req || req++
|
|
224961
|
+
debug('lock', path, opts)
|
|
224962
|
+
opts.start = opts.start || Date.now()
|
|
224963
|
+
|
|
224964
|
+
if (typeof opts.retries === 'number' && opts.retries > 0) {
|
|
224965
|
+
debug('has retries', opts.retries)
|
|
224966
|
+
var retries = opts.retries
|
|
224967
|
+
opts.retries = 0
|
|
224968
|
+
cb = (function (orig) { return function cb (er, fd) {
|
|
224969
|
+
debug('retry-mutated callback')
|
|
224970
|
+
retries -= 1
|
|
224971
|
+
if (!er || retries < 0) return orig(er, fd)
|
|
224972
|
+
|
|
224973
|
+
debug('lock retry', path, opts)
|
|
224974
|
+
|
|
224975
|
+
if (opts.retryWait) setTimeout(retry, opts.retryWait)
|
|
224976
|
+
else retry()
|
|
224977
|
+
|
|
224978
|
+
function retry () {
|
|
224979
|
+
opts.start = Date.now()
|
|
224980
|
+
debug('retrying', opts.start)
|
|
224981
|
+
exports.lock(path, opts, cb)
|
|
224982
|
+
}
|
|
224983
|
+
}})(cb)
|
|
224984
|
+
}
|
|
224985
|
+
|
|
224986
|
+
// try to engage the lock.
|
|
224987
|
+
// if this succeeds, then we're in business.
|
|
224988
|
+
fs.open(path, wx, function (er, fd) {
|
|
224989
|
+
if (!er) {
|
|
224990
|
+
debug('locked', path, fd)
|
|
224991
|
+
locks[path] = fd
|
|
224992
|
+
return fs.close(fd, function () {
|
|
224993
|
+
return cb()
|
|
224994
|
+
})
|
|
224995
|
+
}
|
|
224996
|
+
|
|
224997
|
+
debug('failed to acquire lock', er)
|
|
224998
|
+
|
|
224999
|
+
// something other than "currently locked"
|
|
225000
|
+
// maybe eperm or something.
|
|
225001
|
+
if (er.code !== 'EEXIST') {
|
|
225002
|
+
debug('not EEXIST error', er)
|
|
225003
|
+
return cb(er)
|
|
225004
|
+
}
|
|
225005
|
+
|
|
225006
|
+
// someone's got this one. see if it's valid.
|
|
225007
|
+
if (!opts.stale) return notStale(er, path, opts, cb)
|
|
225008
|
+
|
|
225009
|
+
return maybeStale(er, path, opts, false, cb)
|
|
225010
|
+
})
|
|
225011
|
+
debug('lock return')
|
|
225012
|
+
}
|
|
225013
|
+
|
|
225014
|
+
|
|
225015
|
+
// Staleness checking algorithm
|
|
225016
|
+
// 1. acquire $lock, fail
|
|
225017
|
+
// 2. stat $lock, find that it is stale
|
|
225018
|
+
// 3. acquire $lock.STALE
|
|
225019
|
+
// 4. stat $lock, assert that it is still stale
|
|
225020
|
+
// 5. unlink $lock
|
|
225021
|
+
// 6. link $lock.STALE $lock
|
|
225022
|
+
// 7. unlink $lock.STALE
|
|
225023
|
+
// On any failure, clean up whatever we've done, and raise the error.
|
|
225024
|
+
function maybeStale (originalEr, path, opts, hasStaleLock, cb) {
|
|
225025
|
+
fs.stat(path, function (statEr, st) {
|
|
225026
|
+
if (statEr) {
|
|
225027
|
+
if (statEr.code === 'ENOENT') {
|
|
225028
|
+
// expired already!
|
|
225029
|
+
opts.stale = false
|
|
225030
|
+
debug('lock stale enoent retry', path, opts)
|
|
225031
|
+
exports.lock(path, opts, cb)
|
|
225032
|
+
return
|
|
225033
|
+
}
|
|
225034
|
+
return cb(statEr)
|
|
225035
|
+
}
|
|
225036
|
+
|
|
225037
|
+
var age = Date.now() - st[exports.filetime].getTime()
|
|
225038
|
+
if (age <= opts.stale) return notStale(originalEr, path, opts, cb)
|
|
225039
|
+
|
|
225040
|
+
debug('lock stale', path, opts)
|
|
225041
|
+
if (hasStaleLock) {
|
|
225042
|
+
exports.unlock(path, function (er) {
|
|
225043
|
+
if (er) return cb(er)
|
|
225044
|
+
debug('lock stale retry', path, opts)
|
|
225045
|
+
fs.link(path + '.STALE', path, function (er) {
|
|
225046
|
+
fs.unlink(path + '.STALE', function () {
|
|
225047
|
+
// best effort. if the unlink fails, oh well.
|
|
225048
|
+
cb(er)
|
|
225049
|
+
})
|
|
225050
|
+
})
|
|
225051
|
+
})
|
|
225052
|
+
} else {
|
|
225053
|
+
debug('acquire .STALE file lock', opts)
|
|
225054
|
+
exports.lock(path + '.STALE', opts, function (er) {
|
|
225055
|
+
if (er) return cb(er)
|
|
225056
|
+
maybeStale(originalEr, path, opts, true, cb)
|
|
225057
|
+
})
|
|
225058
|
+
}
|
|
225059
|
+
})
|
|
225060
|
+
}
|
|
225061
|
+
|
|
225062
|
+
function notStale (er, path, opts, cb) {
|
|
225063
|
+
debug('notStale', path, opts)
|
|
225064
|
+
|
|
225065
|
+
// if we can't wait, then just call it a failure
|
|
225066
|
+
if (typeof opts.wait !== 'number' || opts.wait <= 0) {
|
|
225067
|
+
debug('notStale, wait is not a number')
|
|
225068
|
+
return cb(er)
|
|
225069
|
+
}
|
|
225070
|
+
|
|
225071
|
+
// poll for some ms for the lock to clear
|
|
225072
|
+
var now = Date.now()
|
|
225073
|
+
var start = opts.start || now
|
|
225074
|
+
var end = start + opts.wait
|
|
225075
|
+
|
|
225076
|
+
if (end <= now)
|
|
225077
|
+
return cb(er)
|
|
225078
|
+
|
|
225079
|
+
debug('now=%d, wait until %d (delta=%d)', start, end, end-start)
|
|
225080
|
+
var wait = Math.min(end - start, opts.pollPeriod || 100)
|
|
225081
|
+
var timer = setTimeout(poll, wait)
|
|
225082
|
+
|
|
225083
|
+
function poll () {
|
|
225084
|
+
debug('notStale, polling', path, opts)
|
|
225085
|
+
exports.lock(path, opts, cb)
|
|
225086
|
+
}
|
|
225087
|
+
}
|
|
225088
|
+
|
|
225089
|
+
exports.lockSync = function (path, opts) {
|
|
225090
|
+
opts = opts || {}
|
|
225091
|
+
opts.req = opts.req || req++
|
|
225092
|
+
debug('lockSync', path, opts)
|
|
225093
|
+
if (opts.wait || opts.retryWait) {
|
|
225094
|
+
throw new Error('opts.wait not supported sync for obvious reasons')
|
|
225095
|
+
}
|
|
225096
|
+
|
|
225097
|
+
try {
|
|
225098
|
+
var fd = fs.openSync(path, wx)
|
|
225099
|
+
locks[path] = fd
|
|
225100
|
+
try { fs.closeSync(fd) } catch (er) {}
|
|
225101
|
+
debug('locked sync!', path, fd)
|
|
225102
|
+
return
|
|
225103
|
+
} catch (er) {
|
|
225104
|
+
if (er.code !== 'EEXIST') return retryThrow(path, opts, er)
|
|
225105
|
+
|
|
225106
|
+
if (opts.stale) {
|
|
225107
|
+
var st = fs.statSync(path)
|
|
225108
|
+
var ct = st[exports.filetime].getTime()
|
|
225109
|
+
if (!(ct % 1000) && (opts.stale % 1000)) {
|
|
225110
|
+
// probably don't have subsecond resolution.
|
|
225111
|
+
// round up the staleness indicator.
|
|
225112
|
+
// Yes, this will be wrong 1/1000 times on platforms
|
|
225113
|
+
// with subsecond stat precision, but that's acceptable
|
|
225114
|
+
// in exchange for not mistakenly removing locks on
|
|
225115
|
+
// most other systems.
|
|
225116
|
+
opts.stale = 1000 * Math.ceil(opts.stale / 1000)
|
|
225117
|
+
}
|
|
225118
|
+
var age = Date.now() - ct
|
|
225119
|
+
if (age > opts.stale) {
|
|
225120
|
+
debug('lockSync stale', path, opts, age)
|
|
225121
|
+
exports.unlockSync(path)
|
|
225122
|
+
return exports.lockSync(path, opts)
|
|
225123
|
+
}
|
|
225124
|
+
}
|
|
225125
|
+
|
|
225126
|
+
// failed to lock!
|
|
225127
|
+
debug('failed to lock', path, opts, er)
|
|
225128
|
+
return retryThrow(path, opts, er)
|
|
225129
|
+
}
|
|
225130
|
+
}
|
|
225131
|
+
|
|
225132
|
+
function retryThrow (path, opts, er) {
|
|
225133
|
+
if (typeof opts.retries === 'number' && opts.retries > 0) {
|
|
225134
|
+
var newRT = opts.retries - 1
|
|
225135
|
+
debug('retryThrow', path, opts, newRT)
|
|
225136
|
+
opts.retries = newRT
|
|
225137
|
+
return exports.lockSync(path, opts)
|
|
225138
|
+
}
|
|
225139
|
+
throw er
|
|
225140
|
+
}
|
|
225141
|
+
|
|
225142
|
+
|
|
225143
|
+
|
|
224739
225144
|
/***/ }),
|
|
224740
225145
|
|
|
224741
225146
|
/***/ 80280:
|