@cloudron/safetydance

中文文档
3.2.0发布于 2026/10/17MITES Module零依赖近 30 天下载 5798

Exception safety in node.js

原始使用文档
文档目录6 节
  1. Installation
  2. Usage
  3. safe.query
  4. safe.set
  5. safe.unset
  6. API

Safety Dance

This node.js module provides you exception safety. That is to say that the functions of this module never ever throw.

If you are annoyed with node.js built-in API throwing exceptions and using exceptions as multi-value return statements, this module is for you.

This module catches any exception in the function you want to call. If there is no exception, it behaves as always. If there was an exception, it catches it, makes it available via error property (like errno).

So, instead of:

try {
    var result = JSON.parse(something);
} catch (e) {
    ...
}

you can write:

var result = safe.JSON.parse(something);
if (result === null) { ... }

Installation

npm install safetydance

Usage

You can call arbitrary synchronous functions without having to worry about exceptions.

var safeCall = require('safetydance');
var result = safeCall(function () { return 1 + 2; }); // will return 3
result = safeCall(function () { throw new Error('bad'); }); // will return null

To pass 'this':

var obj = { a: 10 };
var result = safeCall(function () { return this.a; }.bind(obj)); // will return 10

This module provide conveniences for commonly used functions.

var safe = require('safetydance');
var json = safe.JSON.parse('This is totally { not json }');
console.log(json); // will be null
console.log(safe.error); // will contain SyntaxError

The error gets cleared for successful operations.

json = safe.JSON.parse('{ "totally" : "json" }');
console.log(json); // { totally: "json" }
console.log(safe.error); // will be null

safe.query

safe.query can be used to access nested object properties. For example, the code below can throw an exception if any of the nested objects does not contain the accesses properties:

var capital = data.countries.norway.capital;

This can be rewritten as:

var capital = safe.query(data, 'countries.norway.capital'); // returns undefined if not found

You can also specify a default:

var capital = safe.query(data, 'countries.norway.capital', 'oslo'); // return oslo if not found

Works for deeply nested properties as well (use [] for array access):

var item = safe.query(data, 'key[0][3].some.fancy[45].nested.5.value');

Property names with '.' in them are not supported.

safe.set

safe.set is the complement of safe.query.

var obj = { };
safe.set(obj, 'some.deep.value', 42);
console.log(obj.some.deep.value); // prints 42

If obj is not a valid object, it will create one for you

obj = safe.set(null, 'some.deep.value', 42);
console.log(obj.some.deep.value); // prints 42

If any of the values in the chain is not an object, it will get replaced.

var obj = { some: 43 };
safe.set(obj, 'some.deep.value', 42);
console.log(obj.some.deep.value); // prints 42; some is gone

safe.unset

safe.unset unsets properties in objects.

var obj = { some: { deep: { value: 42 } } };
safe.unset(obj, 'some.deep.value');
console.log(obj); // prints { some: { deep: { } } }

API

  • safeCall(functionToCall, valueToReturnIfErrored)
  • error - the error of the last function call
  • Convenience (refer to node.js docs for details)
    • JSON.parse - returns null on error, object on success
    • JSON.stringify - returns null on error, string on success
    • fs.readFileSync - returns null on error, string/buffer on success
    • fs.writeFileSync - returns false on error, true on success
    • fs.statSync - returns null on error, stat object on success
    • fs.existsSync - returns false on error, true/false on success
    • fs.mkdirSync - returns false on error, true on success
    • fs.unlinkSync - returns false on error, true on success
    • fs.renameSync - returns false on error, true on success
    • fs.copyFileSync - returns false on error, true on success
    • fs.readdirSync - returns null on error, file array on success
    • fs.utimesSync - returns false on error, true on success
    • fs.futimesSync - returns false on error, true on success
    • url.parse = returns null on error, object on success
    • require - returns null on error, module on success
    • child_process.execSync - returns null on error, stdio on success
    • child_process.spawnSync - return null on error, object on success

版本与发布记录

@cloudron/safetydance 共发布 4 个版本,首次发布于 2026/02/18;过去 12 个月发布了 4 个版本。

版本发布日期距今
3.2.0latest2026/10/1723 天前
3.1.02026/07/044 个月前
3.0.12026/02/188 个月前
3.0.02026/02/188 个月前

关于 @cloudron/safetydance 的常见问题

@cloudron/safetydance 是什么?

Exception safety in node.js

如何安装 @cloudron/safetydance?

在项目目录运行 npm install @cloudron/safetydance,或使用 pnpm add / yarn add / bun add。

@cloudron/safetydance 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/cloudron__safetydance 是否存在。

@cloudron/safetydance 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@cloudron/safetydance 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >= 24.0.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@cloudron/safetydance 可以商用吗?

@cloudron/safetydance 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@cloudron/safetydance 的下载量有多少?

根据 npm 官方下载统计,@cloudron/safetydance 在 2026/10/10 – 2026/11/08 这 30 天内被下载 5,798 次,日均约 193 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@cloudron/safetydance 还在维护吗?

最新版本 3.2.0 发布于 2026/10/17(23 天前);至今共发布 4 个版本,其中过去 12 个月发布了 4 个;首次发布于 2026/02/18。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。