Add package @cosmjs/utils
This commit is contained in:
parent
92417373cd
commit
e582313b17
3
NOTICE
3
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
|
||||
|
||||
8
packages/utils/.eslintignore
Normal file
8
packages/utils/.eslintignore
Normal file
@ -0,0 +1,8 @@
|
||||
node_modules/
|
||||
|
||||
build/
|
||||
custom_types/
|
||||
dist/
|
||||
docs/
|
||||
generated/
|
||||
types/
|
||||
3
packages/utils/.gitignore
vendored
Normal file
3
packages/utils/.gitignore
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
build/
|
||||
dist/
|
||||
docs/
|
||||
13
packages/utils/README.md
Normal file
13
packages/utils/README.md
Normal file
@ -0,0 +1,13 @@
|
||||
# @cosmjs/utils
|
||||
|
||||
[](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)).
|
||||
26
packages/utils/jasmine-testrunner.js
Executable file
26
packages/utils/jasmine-testrunner.js
Executable 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();
|
||||
45
packages/utils/karma.conf.js
Normal file
45
packages/utils/karma.conf.js
Normal 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,
|
||||
});
|
||||
};
|
||||
1
packages/utils/nonces/README.txt
Normal file
1
packages/utils/nonces/README.txt
Normal file
@ -0,0 +1 @@
|
||||
Directory used to trigger lerna package updates for all packages
|
||||
40
packages/utils/package.json
Normal file
40
packages/utils/package.json
Normal 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"
|
||||
}
|
||||
}
|
||||
5
packages/utils/src/assert.ts
Normal file
5
packages/utils/src/assert.ts
Normal file
@ -0,0 +1,5 @@
|
||||
export function assert(condition: any, msg?: string): asserts condition {
|
||||
if (!condition) {
|
||||
throw new Error(msg || "condition is not truthy");
|
||||
}
|
||||
}
|
||||
2
packages/utils/src/index.ts
Normal file
2
packages/utils/src/index.ts
Normal file
@ -0,0 +1,2 @@
|
||||
export { assert } from "./assert";
|
||||
export { sleep } from "./sleep";
|
||||
27
packages/utils/src/sleep.spec.ts
Normal file
27
packages/utils/src/sleep.spec.ts
Normal 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);
|
||||
}
|
||||
});
|
||||
});
|
||||
3
packages/utils/src/sleep.ts
Normal file
3
packages/utils/src/sleep.ts
Normal file
@ -0,0 +1,3 @@
|
||||
export async function sleep(ms: number): Promise<void> {
|
||||
return new Promise((resolve) => setTimeout(resolve, ms));
|
||||
}
|
||||
12
packages/utils/tsconfig.json
Normal file
12
packages/utils/tsconfig.json
Normal 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
14
packages/utils/typedoc.js
Normal 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
1
packages/utils/types/assert.d.ts
vendored
Normal file
@ -0,0 +1 @@
|
||||
export declare function assert(condition: any, msg?: string): asserts condition;
|
||||
2
packages/utils/types/index.d.ts
vendored
Normal file
2
packages/utils/types/index.d.ts
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
export { assert } from "./assert";
|
||||
export { sleep } from "./sleep";
|
||||
1
packages/utils/types/sleep.d.ts
vendored
Normal file
1
packages/utils/types/sleep.d.ts
vendored
Normal file
@ -0,0 +1 @@
|
||||
export declare function sleep(ms: number): Promise<void>;
|
||||
18
packages/utils/webpack.web.config.js
Normal file
18
packages/utils/webpack.web.config.js
Normal 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: [],
|
||||
},
|
||||
];
|
||||
Loading…
Reference in New Issue
Block a user