From e582313b1717cfb3e6d597bb1cbf24218237c47d Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Fri, 5 Jun 2020 13:23:16 +0200 Subject: [PATCH] Add package @cosmjs/utils --- NOTICE | 3 ++ packages/utils/.eslintignore | 8 +++++ packages/utils/.gitignore | 3 ++ packages/utils/README.md | 13 ++++++++ packages/utils/jasmine-testrunner.js | 26 ++++++++++++++++ packages/utils/karma.conf.js | 45 ++++++++++++++++++++++++++++ packages/utils/nonces/README.txt | 1 + packages/utils/package.json | 40 +++++++++++++++++++++++++ packages/utils/src/assert.ts | 5 ++++ packages/utils/src/index.ts | 2 ++ packages/utils/src/sleep.spec.ts | 27 +++++++++++++++++ packages/utils/src/sleep.ts | 3 ++ packages/utils/tsconfig.json | 12 ++++++++ packages/utils/typedoc.js | 14 +++++++++ packages/utils/types/assert.d.ts | 1 + packages/utils/types/index.d.ts | 2 ++ packages/utils/types/sleep.d.ts | 1 + packages/utils/webpack.web.config.js | 18 +++++++++++ 18 files changed, 224 insertions(+) create mode 100644 packages/utils/.eslintignore create mode 100644 packages/utils/.gitignore create mode 100644 packages/utils/README.md create mode 100755 packages/utils/jasmine-testrunner.js create mode 100644 packages/utils/karma.conf.js create mode 100644 packages/utils/nonces/README.txt create mode 100644 packages/utils/package.json create mode 100644 packages/utils/src/assert.ts create mode 100644 packages/utils/src/index.ts create mode 100644 packages/utils/src/sleep.spec.ts create mode 100644 packages/utils/src/sleep.ts create mode 100644 packages/utils/tsconfig.json create mode 100644 packages/utils/typedoc.js create mode 100644 packages/utils/types/assert.d.ts create mode 100644 packages/utils/types/index.d.ts create mode 100644 packages/utils/types/sleep.d.ts create mode 100644 packages/utils/webpack.web.config.js diff --git a/NOTICE b/NOTICE index 20af5cca..7742ebc0 100644 --- a/NOTICE +++ b/NOTICE @@ -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 diff --git a/packages/utils/.eslintignore b/packages/utils/.eslintignore new file mode 100644 index 00000000..f373a53f --- /dev/null +++ b/packages/utils/.eslintignore @@ -0,0 +1,8 @@ +node_modules/ + +build/ +custom_types/ +dist/ +docs/ +generated/ +types/ diff --git a/packages/utils/.gitignore b/packages/utils/.gitignore new file mode 100644 index 00000000..68bf3735 --- /dev/null +++ b/packages/utils/.gitignore @@ -0,0 +1,3 @@ +build/ +dist/ +docs/ diff --git a/packages/utils/README.md b/packages/utils/README.md new file mode 100644 index 00000000..af296daa --- /dev/null +++ b/packages/utils/README.md @@ -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)). diff --git a/packages/utils/jasmine-testrunner.js b/packages/utils/jasmine-testrunner.js new file mode 100755 index 00000000..9fada59b --- /dev/null +++ b/packages/utils/jasmine-testrunner.js @@ -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(); diff --git a/packages/utils/karma.conf.js b/packages/utils/karma.conf.js new file mode 100644 index 00000000..38218d78 --- /dev/null +++ b/packages/utils/karma.conf.js @@ -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, + }); +}; diff --git a/packages/utils/nonces/README.txt b/packages/utils/nonces/README.txt new file mode 100644 index 00000000..092fe732 --- /dev/null +++ b/packages/utils/nonces/README.txt @@ -0,0 +1 @@ +Directory used to trigger lerna package updates for all packages diff --git a/packages/utils/package.json b/packages/utils/package.json new file mode 100644 index 00000000..afc0fe2c --- /dev/null +++ b/packages/utils/package.json @@ -0,0 +1,40 @@ +{ + "name": "@cosmjs/utils", + "version": "0.8.0", + "description": "Utility tools, primarily for testing code", + "contributors": ["IOV SAS "], + "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" + } +} diff --git a/packages/utils/src/assert.ts b/packages/utils/src/assert.ts new file mode 100644 index 00000000..0ab49030 --- /dev/null +++ b/packages/utils/src/assert.ts @@ -0,0 +1,5 @@ +export function assert(condition: any, msg?: string): asserts condition { + if (!condition) { + throw new Error(msg || "condition is not truthy"); + } +} diff --git a/packages/utils/src/index.ts b/packages/utils/src/index.ts new file mode 100644 index 00000000..3e156674 --- /dev/null +++ b/packages/utils/src/index.ts @@ -0,0 +1,2 @@ +export { assert } from "./assert"; +export { sleep } from "./sleep"; diff --git a/packages/utils/src/sleep.spec.ts b/packages/utils/src/sleep.spec.ts new file mode 100644 index 00000000..579890ca --- /dev/null +++ b/packages/utils/src/sleep.spec.ts @@ -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); + } + }); +}); diff --git a/packages/utils/src/sleep.ts b/packages/utils/src/sleep.ts new file mode 100644 index 00000000..f9a5c4a7 --- /dev/null +++ b/packages/utils/src/sleep.ts @@ -0,0 +1,3 @@ +export async function sleep(ms: number): Promise { + return new Promise((resolve) => setTimeout(resolve, ms)); +} diff --git a/packages/utils/tsconfig.json b/packages/utils/tsconfig.json new file mode 100644 index 00000000..167e8c02 --- /dev/null +++ b/packages/utils/tsconfig.json @@ -0,0 +1,12 @@ +{ + "extends": "../../tsconfig.json", + "compilerOptions": { + "baseUrl": ".", + "outDir": "build", + "declarationDir": "build/types", + "rootDir": "src" + }, + "include": [ + "src/**/*" + ] +} diff --git a/packages/utils/typedoc.js b/packages/utils/typedoc.js new file mode 100644 index 00000000..e2387c7d --- /dev/null +++ b/packages/utils/typedoc.js @@ -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, +}; diff --git a/packages/utils/types/assert.d.ts b/packages/utils/types/assert.d.ts new file mode 100644 index 00000000..2e9da4bf --- /dev/null +++ b/packages/utils/types/assert.d.ts @@ -0,0 +1 @@ +export declare function assert(condition: any, msg?: string): asserts condition; diff --git a/packages/utils/types/index.d.ts b/packages/utils/types/index.d.ts new file mode 100644 index 00000000..3e156674 --- /dev/null +++ b/packages/utils/types/index.d.ts @@ -0,0 +1,2 @@ +export { assert } from "./assert"; +export { sleep } from "./sleep"; diff --git a/packages/utils/types/sleep.d.ts b/packages/utils/types/sleep.d.ts new file mode 100644 index 00000000..deb121ba --- /dev/null +++ b/packages/utils/types/sleep.d.ts @@ -0,0 +1 @@ +export declare function sleep(ms: number): Promise; diff --git a/packages/utils/webpack.web.config.js b/packages/utils/webpack.web.config.js new file mode 100644 index 00000000..3e0a32a6 --- /dev/null +++ b/packages/utils/webpack.web.config.js @@ -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: [], + }, +];