@devicefarmer/stf-syrup

中文文档
2.0.0发布于 2026/10/25Apache-2.0TypeScriptCommonJS零依赖构建来源已验证近 30 天下载 3944

Promise-based, configuration-less dependency injection.

原始使用文档
文档目录8 节
  1. Features
  2. Requirements
  3. Getting started
  4. TypeScript
  5. Upgrading from 1.x
  6. Mockability
  7. Contributing
  8. License

syrup

Syrup is an extremely simple Promise-based Dependency Injection framework (or rather, a library) for Node.js. While many related efforts already exist, Syrup attempts to break the mold by focusing on a single feature (dependency resolution) and relying on Node.js for the rest.

Features

  • Configuration-free. No need to set up configuration files just to get up and running.
  • Magic-free module loader. It's just require() and you'll be the one calling it (i.e. you have full control).
  • Promise-only.
  • Mock-friendly. Invoke modules with mock dependencies for easy testing.
  • Non-intrusive. You can use Syrup in just one part of your app.
  • Runs in parallel with an optional serial mode.
  • Ships TypeScript declarations.

Requirements

  • Node.js 22 or newer. Older versions are not supported.

Getting started

Install via NPM:

npm install --save @devicefarmer/stf-syrup

A module declares its dependencies with .dependency() and its body with .define(). The body receives the options followed by the value of every dependency, in the order the dependencies were added:

var syrup = require('@devicefarmer/stf-syrup')

var box = syrup()
  .define(function(options) {
    return {
      put: function(thing) {
        return Promise.resolve(thing)
      }
    }
  })

module.exports = syrup({size: 'large'})
  .dependency(box)
  .define(function(options, box) {
    return box
  })

.consume(options) resolves every dependency, then calls the body and returns a native Promise of whatever the body returns (awaited if it is itself a promise). Each module is consumed only once: a module that several others depend on runs once and they all share its value, and every later .consume() returns the same promise, whatever options it is given. The options are filled in with the module's default options, the ones given to syrup().

syrup() consumes the dependencies in parallel. syrup.serial() consumes them one at a time, each one once the previous one has resolved.

TypeScript

The package is written in TypeScript and ships its declarations. syrup.Syrup<T> is the type of a module whose .consume() resolves to T, and .define() infers T from the body:

import syrup from '@devicefarmer/stf-syrup'

const answer = syrup.serial().define((options: {base: number}) => options.base + 41)
// answer.consume() is a Promise<number>, and answer is a syrup.Syrup<number>

Upgrading from 1.x

.consume() used to return a bluebird 1.x promise and now returns a native Promise. Code that called bluebird-only methods on the result, such as .spread(), .tap() or .isFulfilled(), has to change. As with any native promise, a rejection that nothing handles now makes Node.js exit instead of being logged.

Mockability

Much like in Architect, you can invoke modules directly by passing your own mock dependencies to them.

archive.js

var syrup = require('@devicefarmer/stf-syrup')

module.exports = syrup()
  .dependency(require('./box'))
  .define(function(options, box) {
    return {
      store: function(thing) {
        return box.put(thing)
      }
    }
  })

archive-test.js

var sinon = require('sinon')
var chai = require('chai')
chai.use(require('sinon-chai'))
var expect = chai.expect

var archive = require('./archive')

describe('archive', function() {

  it('should put the thing in the box', function() {
    var mockBox = {
      put: sinon.spy()
    }
    var treasure = 42
    archive.invoke(null, mockBox).store(treasure)
    expect(mockBox.put).to.have.been.calledWith(treasure)
  })

})

Contributing

See CONTRIBUTING.md.

License

See LICENSE.

Copyright © CyberAgent, Inc. All Rights Reserved.

版本与发布记录

@devicefarmer/stf-syrup 共发布 5 个版本,首次发布于 2020/05/16;过去 12 个月发布了 1 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
2.0.0latest2026/10/2518 天前
1.0.32024/11/262 年前
1.0.22021/03/085 年前
1.0.12020/08/286 年前
1.0.02020/05/166 年前

关于 @devicefarmer/stf-syrup 的常见问题

@devicefarmer/stf-syrup 是什么?

Promise-based, configuration-less dependency injection.

如何安装 @devicefarmer/stf-syrup?

在项目目录运行 npm install @devicefarmer/stf-syrup,或使用 pnpm add / yarn add / bun add。

@devicefarmer/stf-syrup 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@devicefarmer/stf-syrup 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@devicefarmer/stf-syrup 需要什么版本的 Node.js?

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

@devicefarmer/stf-syrup 可以商用吗?

@devicefarmer/stf-syrup 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@devicefarmer/stf-syrup 的下载量有多少?

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

@devicefarmer/stf-syrup 还在维护吗?

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