Giter Site home page Giter Site logo

ssube / noicejs Goto Github PK

View Code? Open in Web Editor NEW
17.0 3.0 6.0 2.24 MB

extremely thin async dependency injection

Home Page: https://ssube.github.io/noicejs/

License: MIT License

JavaScript 0.13% Makefile 6.64% TypeScript 91.75% Shell 1.48%
dependency-injection typescript async service-locator dependency-injection-container guice di

noicejs's Introduction

noicejs

Extremely thin, async dependency injection, now with a getting started guide.

Inspired by Google's Guice library and written in Typescript.

Features

  • async dependency resolution
  • constructor and property injection
  • modular containers with inheritance
  • named dependencies using strings or unique symbols
  • typed errors
  • typescript typedefs
  • zero runtime dependencies, bundled or otherwise
  • extensive test coverage

Contents

Status

Pipeline status Lines of Code Test coverage MIT license FOSSA Status

Open bug count Open issue count Closed issue count

Renovate badge Dependency status Dev dependency status Known vulnerabilities

Maintainability score Technical debt ratio Quality issues Language grade: JavaScript Total alerts

Releases

  • 3.x versions are compatible with Node 12+
  • 4.x versions are compatible with Node 16+

github release link github release version github commits since release

npm package link npm release version Typescript definitions

Usage

Consider a Server class that needs to fetch data from the Cache and Filesystem, but doesn't know (or need to know) how those are implemented. The following example is also part of the unit tests.

import { LocalModule } from './local';
import { NetworkModule } from './network';

class Cache {
  public get(path: string, ttl: number, fallback: () => Promise<string>): Promise<string> {
    /* ... */
  }
}

class Filesystem {
  public get(path: string): Promise<string> {
    /* ... */
  }
}

/**
 * Constructors, strings, and symbols are supported. Symbols are
 * preferred, as the most unique, but names can be convenient.
 */
@Inject(Cache.name.toLowerCase(), Filesystem.name.toLowerCase())
class Server {
  protected readonly cache: Cache;
  protected readonly filesystem: Filesystem;
  protected readonly ttl: number;

  constructor(options) {
    this.cache = options.cache;
    this.filesystem = options.filesystem;
    this.ttl = defaultTo(options.ttl, 0);
  }

  get(path: string) {
    return options.cache.get(path, this.ttl, () => options.filesystem.get(path));
  }
}

const TEST_TTL = 60;

function module() {
  if (process.env['DEBUG'] === 'TRUE') {
    return new LocalModule();
  } else {
    return new NetworkModule();
  }
}

async function main() {
  const container = Container.from(module());
  await container.configure();

  const server = await container.create(Server, {
    /* cache and filesystem are found and injected by container */
    ttl: TEST_TTL,
  });

  /* server.cache.get and server.filesystem.get will be called in order */
  const result = await server.get('some/file');
}

noicejs will collect dependencies from the decorated constructor and any superclasses, find a provider for each injected dependency, and asynchronously resolve them before calling the constructor. Any extra parameters are passed on to the original constructor, along with the container and resolved dependencies.

Build

To build a bundle and run tests:

> make

yarn
yarn install v1.17.3
[1/4] Resolving packages...
success Already up-to-date.
Done in 0.20s.
/home/ssube/code/ssube/noicejs//node_modules/.bin/rollup --config /home/ssube/code/ssube/noicejs//config/rollup.js

src/index.ts, test/harness.ts, test/**/Test*.ts → out/...
...
created out/ in 3.3s
/home/ssube/code/ssube/noicejs//node_modules/.bin/api-extractor run --config /home/ssube/code/ssube/noicejs//config/api-extractor.json --local -v

api-extractor 7.3.8  - https://api-extractor.com/
...

API Extractor completed successfully
Success!

License

FOSSA Status

noicejs's People

Contributors

dependabot[bot] avatar greenkeeper[bot] avatar isolex[bot] avatar jakxz avatar kamilczak020 avatar octogonz avatar renovate-bot avatar renovate[bot] avatar ssube avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

noicejs's Issues

shrink the bundle

The main-bundle.js file is much too large. According to the bundle report, it contains some dependencies. Remove them, minify it, and make sure things still work.

  • named deps after minification
  • lodash, etc outside of bundle

Action required: Greenkeeper could not be activated 🚨

🚨 You need to enable Continuous Integration on all branches of this repository. 🚨

To enable Greenkeeper, you need to make sure that a commit status is reported on all branches. This is required by Greenkeeper because it uses your CI build statuses to figure out when to notify you about breaking changes.

Since we didn’t receive a CI status on the greenkeeper/initial branch, it’s possible that you don’t have CI set up yet. We recommend using Travis CI, but Greenkeeper will work with every other CI service as well.

If you have already set up a CI for this repository, you might need to check how it’s configured. Make sure it is set to run on all new branches. If you don’t want it to run on absolutely every branch, you can whitelist branches starting with greenkeeper/.

Once you have installed and configured CI on this repository correctly, you’ll need to re-trigger Greenkeeper’s initial pull request. To do this, please delete the greenkeeper/initial branch in this repository, and then remove and re-add this repository to the Greenkeeper App’s white list on Github. You'll find this list on your repo or organization’s settings page, under Installed GitHub Apps.

test logger compatibility

Summary

The Logger interface matches and interoperates with bunyan, which is good enough for isolex but may not be for general use. Ensure the logging is compatible with winston, at least.

Write an adapter, if necessary, and tests that get as close as possible to the bunyan and winston interfaces. Perhaps install them as devdeps and register a fake output stream.

make runnable notebook of examples

Summary

The readme, getting started guide, and docs/examples folders all have roughly the same examples, but no way to keep them in sync or run them.

Scope

  • include code snippets from docs/examples, ideally
  • make examples runnable, links or inline

fix doc pages

Summary

The current markdown docs don't translate perfectly into the GH pages site.

Details

  • links in getting-started are not links (add titles)
  • no index page causes a 404

update build

Update dependencies, lint config, and webpack config to roughly match isolex.

replace webpack with rollup

Webpack is very slow, requires 6-8GB of memory, and regularly produces incorrect output. Replace it with rollup.

update readme

The readme is out of date:

  • pipeline badge (replace travis with gitlab)
  • turn off travis entirely
  • async examples

Action required: Greenkeeper could not be activated 🚨

🚨 You need to enable Continuous Integration on Greenkeeper branches of this repository. 🚨

To enable Greenkeeper, you need to make sure that a commit status is reported on all branches. This is required by Greenkeeper because it uses your CI build statuses to figure out when to notify you about breaking changes.

Since we didn’t receive a CI status on the greenkeeper/initial branch, it’s possible that you don’t have CI set up yet. We recommend using Travis CI, but Greenkeeper will work with every other CI service as well.

If you have already set up a CI for this repository, you might need to check how it’s configured. Make sure it is set to run on all new branches. If you don’t want it to run on absolutely every branch, you can whitelist branches starting with greenkeeper/.

Once you have installed and configured CI on this repository correctly, you’ll need to re-trigger Greenkeeper’s initial pull request. To do this, please click the 'fix repo' button on account.greenkeeper.io.

Action required: Greenkeeper could not be activated 🚨

🚨 You need to enable Continuous Integration on Greenkeeper branches of this repository. 🚨

To enable Greenkeeper, you need to make sure that a commit status is reported on all branches. This is required by Greenkeeper because it uses your CI build statuses to figure out when to notify you about breaking changes.

Since we didn’t receive a CI status on the greenkeeper/initial branch, it’s possible that you don’t have CI set up yet. We recommend using Travis CI, but Greenkeeper will work with every other CI service as well.

If you have already set up a CI for this repository, you might need to check how it’s configured. Make sure it is set to run on all new branches. If you don’t want it to run on absolutely every branch, you can whitelist branches starting with greenkeeper/.

Once you have installed and configured CI on this repository correctly, you’ll need to re-trigger Greenkeeper’s initial pull request. To do this, please click the 'fix repo' button on account.greenkeeper.io.

make container implement module

Summary

To keep the module-tree consistent and allow container inheritance, make Container implement Module a proxy to its own modules and pass has/get through. This may help reduce logic in Container.create for lint.

Scope

  • Container implements Module
    • has
    • get
  • Container.create lint cleanup

Use Case

This will allow Container.from(container, ...modules) and tree inheritance.

Questions

Container.create currently has a cognitive complexity warning and is doing too much logic in one place. This is the first change to the container's API in some time, and a good chance to fix that.

This will be complicated by Module being an abstract base class, rather than the interface it should be. That will be a breaking change since consumers would have to extend BaseModule instead of extend Module. Introducing an interface with a different name would work, and may be otherwise beneficial since the current Module API contains a variety of bind/bindTo methods that Container shouldn't implement.

Action required: Greenkeeper could not be activated 🚨

🚨 You need to enable Continuous Integration on Greenkeeper branches of this repository. 🚨

To enable Greenkeeper, you need to make sure that a commit status is reported on all branches. This is required by Greenkeeper because it uses your CI build statuses to figure out when to notify you about breaking changes.

Since we didn’t receive a CI status on the greenkeeper/initial branch, it’s possible that you don’t have CI set up yet. We recommend using Travis CI, but Greenkeeper will work with every other CI service as well.

If you have already set up a CI for this repository, you might need to check how it’s configured. Make sure it is set to run on all new branches. If you don’t want it to run on absolutely every branch, you can whitelist branches starting with greenkeeper/.

Once you have installed and configured CI on this repository correctly, you’ll need to re-trigger Greenkeeper’s initial pull request. To do this, please click the 'fix repo' button on account.greenkeeper.io.

publish to npm

I need to start publishing a package of noice, so folks can actually use it.

support React views

I need to add support for injecting properties into React views. This will rely on object(/named) injection, #3.

support object injection

I need to add support for injecting parameters as an object with named fields, so we can provide names for the dependencies and either provide an object or merge them with an existing object. This is required for #2.

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.


Warning

Renovate failed to look up the following dependencies: Failed to look up npm package @istanbuljs/schema, Failed to look up npm package @microsoft/api-documenter, Failed to look up npm package @microsoft/api-extractor, Failed to look up npm package @types/chai, Failed to look up npm package @types/chai-as-promised, Failed to look up npm package @types/mocha, Failed to look up npm package @types/node, Failed to look up npm package @types/sinon-chai, Failed to look up npm package @types/source-map-support, Failed to look up npm package @typescript-eslint/eslint-plugin, Failed to look up npm package @typescript-eslint/parser, Failed to look up npm package c8, Failed to look up npm package chai, Failed to look up npm package chai-as-promised, Failed to look up npm package eslint, Failed to look up npm package eslint-plugin-chai, Failed to look up npm package eslint-plugin-chai-expect, Failed to look up npm package eslint-plugin-chai-expect-keywords, Failed to look up npm package eslint-plugin-import, Failed to look up npm package eslint-plugin-mocha, Failed to look up npm package eslint-plugin-no-null, Failed to look up npm package eslint-plugin-sonarjs, Failed to look up npm package mocha, Failed to look up npm package sinon, Failed to look up npm package sinon-chai, Failed to look up npm package source-map-support, Failed to look up npm package standard-version, Failed to look up npm package tslib, Failed to look up npm package typescript.

Files affected: package.json


Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

dockerfile
Dockerfile.alpine
  • node 17-alpine
Dockerfile.stretch
  • node 17-stretch
gitlabci
.gitlab/ci-tools.yml
  • docker.artifacts.apextoaster.com/apextoaster/base 1.5
  • docker.artifacts.apextoaster.com/apextoaster/code-climate 0.6
  • docker.artifacts.apextoaster.com/apextoaster/docker 20.10
  • docker.artifacts.apextoaster.com/apextoaster/docker-dind 20.10
  • docker.artifacts.apextoaster.com/apextoaster/node 16.19
  • docker.artifacts.apextoaster.com/apextoaster/sonar-scanner 4.4
npm
package.json
  • @istanbuljs/schema 0.1.3
  • @microsoft/api-documenter 7.22.8
  • @microsoft/api-extractor 7.35.1
  • @types/chai 4.3.5
  • @types/chai-as-promised 7.1.5
  • @types/mocha 10.0.1
  • @types/node 18.15.3
  • @types/sinon-chai 3.2.9
  • @types/source-map-support 0.5.6
  • @typescript-eslint/eslint-plugin 5.59.11
  • @typescript-eslint/parser 5.59.11
  • c8 7.14.0
  • chai 4.3.7
  • chai-as-promised 7.1.1
  • eslint 8.42.0
  • eslint-plugin-chai 0.0.1
  • eslint-plugin-chai-expect 3.0.0
  • eslint-plugin-chai-expect-keywords 2.1.0
  • eslint-plugin-import 2.27.5
  • eslint-plugin-mocha 10.1.0
  • eslint-plugin-no-null 1.0.2
  • eslint-plugin-sonarjs 0.19.0
  • mocha 10.2.0
  • sinon 15.1.0
  • sinon-chai 3.7.0
  • source-map-support 0.5.21
  • standard-version 9.5.0
  • tslib 2.5.3
  • typescript 4.9.5

  • Check this box to trigger a request for Renovate to run again on this repository

log level should be an enum

Summary

The LogLevel type cannot be used, so logger definitions have to be written as { level: 'info' } despite the error message saying:

(property) BotData.logger: {
    level: LogLevel;
    name: string;
}
Property 'level' is missing in type '{ name: string; }' but required in type '{ level: LogLevel; name: string; }'.ts(2741)

ensure signature omit containers correctly

Summary

Wrapped constructors, factories, and other signature may not require a container property in their options, even if they did before. Luckily, mapped types offer Omit to handle this, so Omit<BaseOptions, 'container'> works for any options type that extends BaseOptions: that is, all of them.

Scope

  • review typedefs for signatures with options, base or generic
  • replace them with container-omitted options as appropriate

Use Case

Better typedefs.

update dependencies

Some of the dev dependencies are out of date. Update them and make everything green.

add dict module

Feature

Add a reference/example module that takes a dict or map of dependencies and registers them all as providers.

Summary

This will help keep the examples concise and should suffice for most simple uses of the library, saving folks the trouble of writing their own modules for very short programs.

Scope

  • dict module
  • map module

They might be the same module taking a MapLike and iterating over entries.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.