Add package @cosmjs/utils

This commit is contained in:
Simon Warta 2020-06-05 13:23:16 +02:00
parent 92417373cd
commit e582313b17
18 changed files with 224 additions and 0 deletions

3
NOTICE
View File

@ -11,6 +11,9 @@ on 2020-02-06.
The code in packages/crypto was forked from https://github.com/iov-one/iov-core/tree/v2.3.2/packages/iov-crypto
on 2020-06-05.
The code in packages/utils was forked from https://github.com/iov-one/iov-core/tree/v2.3.2/packages/iov-utils
on 2020-06-05.
Copyright 2018-2020 IOV SAS
Copyright 2020 Confio UO
Copyright 2020 Simon Warta

View File

@ -0,0 +1,8 @@
node_modules/
build/
custom_types/
dist/
docs/
generated/
types/

3
packages/utils/.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
build/
dist/
docs/

13
packages/utils/README.md Normal file
View File

@ -0,0 +1,13 @@
# @cosmjs/utils
[![npm version](https://img.shields.io/npm/v/@cosmjs/utils.svg)](https://www.npmjs.com/package/@cosmjs/utils)
Utility functions independent of blockchain applications. Primarily used for testing
but stuff like `sleep` can also be useful at runtime.
## License
This package is part of the cosmwasm-js repository, licensed under the Apache
License 2.0 (see
[NOTICE](https://github.com/confio/cosmwasm-js/blob/master/NOTICE) and
[LICENSE](https://github.com/confio/cosmwasm-js/blob/master/LICENSE)).

View File

@ -0,0 +1,26 @@
#!/usr/bin/env node
require("source-map-support").install();
const defaultSpecReporterConfig = require("../../jasmine-spec-reporter.config.json");
// setup Jasmine
const Jasmine = require("jasmine");
const jasmine = new Jasmine();
jasmine.loadConfig({
spec_dir: "build",
spec_files: ["**/*.spec.js"],
helpers: [],
random: false,
seed: null,
stopSpecOnExpectationFailure: false,
});
jasmine.jasmine.DEFAULT_TIMEOUT_INTERVAL = 15 * 1000;
// setup reporter
const { SpecReporter } = require("jasmine-spec-reporter");
const reporter = new SpecReporter({ ...defaultSpecReporterConfig });
// initialize and execute
jasmine.env.clearReporters();
jasmine.addReporter(reporter);
jasmine.execute();

View File

@ -0,0 +1,45 @@
module.exports = function (config) {
config.set({
// base path that will be used to resolve all patterns (eg. files, exclude)
basePath: ".",
// frameworks to use
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
frameworks: ["jasmine"],
// list of files / patterns to load in the browser
files: ["dist/web/tests.js"],
client: {
jasmine: {
random: false,
timeoutInterval: 15000,
},
},
// test results reporter to use
// possible values: 'dots', 'progress'
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
reporters: ["progress", "kjhtml"],
// web server port
port: 9876,
// enable / disable colors in the output (reporters and logs)
colors: true,
// level of logging
// possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
logLevel: config.LOG_INFO,
// enable / disable watching file and executing tests whenever any file changes
autoWatch: false,
// start these browsers
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
browsers: ["Firefox"],
// Keep brower open for debugging. This is overridden by yarn scripts
singleRun: false,
});
};

View File

@ -0,0 +1 @@
Directory used to trigger lerna package updates for all packages

View File

@ -0,0 +1,40 @@
{
"name": "@cosmjs/utils",
"version": "0.8.0",
"description": "Utility tools, primarily for testing code",
"contributors": ["IOV SAS <admin@iov.one>"],
"license": "Apache-2.0",
"main": "build/index.js",
"types": "types/index.d.ts",
"files": [
"build/",
"types/",
"*.md",
"!*.spec.*",
"!**/testdata/"
],
"repository": {
"type": "git",
"url": "https://github.com/CosmWasm/cosmwasm-js/tree/master/packages/utils"
},
"publishConfig": {
"access": "public"
},
"scripts": {
"docs": "shx rm -rf docs && typedoc --options typedoc.js",
"format": "prettier --write --loglevel warn \"./src/**/*.ts\"",
"format-text": "prettier --write --prose-wrap always --print-width 80 \"./*.md\"",
"lint": "eslint --max-warnings 0 \"**/*.{js,ts}\"",
"move-types": "shx rm -r ./types/* && shx mv build/types/* ./types && rm -rf ./types/testdata && shx rm -f ./types/*.spec.d.ts",
"format-types": "prettier --write --loglevel warn \"./types/**/*.d.ts\"",
"build": "shx rm -rf ./build && tsc && yarn move-types && yarn format-types",
"build-or-skip": "[ -n \"$SKIP_BUILD\" ] || yarn build",
"test-node": "node jasmine-testrunner.js",
"test": "yarn build-or-skip && yarn test-node",
"pack-web": "yarn build-or-skip && webpack --mode development --config webpack.web.config.js",
"test-edge": "yarn pack-web && karma start --single-run --browsers Edge",
"test-firefox": "yarn pack-web && karma start --single-run --browsers Firefox",
"test-chrome": "yarn pack-web && karma start --single-run --browsers ChromeHeadless",
"test-safari": "yarn pack-web && karma start --single-run --browsers Safari"
}
}

View File

@ -0,0 +1,5 @@
export function assert(condition: any, msg?: string): asserts condition {
if (!condition) {
throw new Error(msg || "condition is not truthy");
}
}

View File

@ -0,0 +1,2 @@
export { assert } from "./assert";
export { sleep } from "./sleep";

View File

@ -0,0 +1,27 @@
import { sleep } from "./sleep";
describe("sleep", () => {
it("resolves after at least x milliseconds", async () => {
for (const x of [10, 30, 120, 280]) {
const start = Date.now();
await sleep(x);
const sleepingTime = Date.now() - start;
// Add 1 ms safety margin due to rounding issues. The elapsed time between
// timestamps 1000 and 1010 can be somethting between 10 and 11 ms.
expect(sleepingTime + 1).toBeGreaterThanOrEqual(x);
}
});
it("resolves within a reasonable amount of time >= x milliseconds", async () => {
// Don't be too strict as jest will run many tests at the same time and test systems can be slow sometimes.
const tolerance = 30; // ms
for (const x of [10, 30, 120, 280]) {
const start = Date.now();
await sleep(x);
const sleepingTime = Date.now() - start;
expect(sleepingTime).toBeLessThanOrEqual(x + tolerance);
}
});
});

View File

@ -0,0 +1,3 @@
export async function sleep(ms: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, ms));
}

View File

@ -0,0 +1,12 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"baseUrl": ".",
"outDir": "build",
"declarationDir": "build/types",
"rootDir": "src"
},
"include": [
"src/**/*"
]
}

14
packages/utils/typedoc.js Normal file
View File

@ -0,0 +1,14 @@
const packageJson = require("./package.json");
module.exports = {
src: ["./src"],
out: "docs",
exclude: "**/*.spec.ts",
target: "es6",
name: `${packageJson.name} Documentation`,
readme: "README.md",
mode: "file",
excludeExternals: true,
excludeNotExported: true,
excludePrivate: true,
};

1
packages/utils/types/assert.d.ts vendored Normal file
View File

@ -0,0 +1 @@
export declare function assert(condition: any, msg?: string): asserts condition;

2
packages/utils/types/index.d.ts vendored Normal file
View File

@ -0,0 +1,2 @@
export { assert } from "./assert";
export { sleep } from "./sleep";

1
packages/utils/types/sleep.d.ts vendored Normal file
View File

@ -0,0 +1 @@
export declare function sleep(ms: number): Promise<void>;

View File

@ -0,0 +1,18 @@
const glob = require("glob");
const path = require("path");
const target = "web";
const distdir = path.join(__dirname, "dist", "web");
module.exports = [
{
// bundle used for Karma tests
target: target,
entry: glob.sync("./build/**/*.spec.js"),
output: {
path: distdir,
filename: "tests.js",
},
plugins: [],
},
];