@syncot/util
Advanced tools
Comparing version 0.0.9 to 0.0.10
export * from './buffer'; | ||
export * from './eventEmitter'; | ||
export * from './invertedStreams'; | ||
export * from './random'; | ||
export * from './error'; | ||
export * from './event'; | ||
export * from './misc'; | ||
export * from './stream'; | ||
export * from './taskRunner'; | ||
export * from './types'; | ||
export * from './validation'; |
export * from './buffer'; | ||
export * from './eventEmitter'; | ||
export * from './invertedStreams'; | ||
export * from './random'; | ||
export * from './error'; | ||
export * from './event'; | ||
export * from './misc'; | ||
@@ -6,0 +5,0 @@ export * from './stream'; |
@@ -0,1 +1,8 @@ | ||
/** | ||
* Keeps only public properties. | ||
* See https://github.com/Microsoft/TypeScript/issues/471#issuecomment-381842426 | ||
*/ | ||
export declare type Interface<T> = { | ||
[P in keyof T]: T[P]; | ||
}; | ||
export declare function noop(): void; | ||
@@ -10,2 +17,3 @@ /** | ||
export declare function delay(minDelayMilliseconds?: number): Promise<unknown>; | ||
export declare function randomInteger(minInclusive: number, maxExclusive: number): number; | ||
/** | ||
@@ -16,1 +24,5 @@ * Generates a unique ID using an approach inspired by MongoDB ObjectID. | ||
export declare function generateId(): string; | ||
/** | ||
* Throws an `AssertError` if `value` is falsy. | ||
*/ | ||
export declare function assert(value: any, message?: string): void; |
@@ -1,2 +0,2 @@ | ||
import { randomInteger } from './random'; | ||
import { createAssertError } from './error'; | ||
export function noop() { | ||
@@ -17,2 +17,8 @@ // Do nothing. | ||
} | ||
export function randomInteger(minInclusive, maxExclusive) { | ||
assert(Number.isSafeInteger(minInclusive), 'Argument "minInclusive" must be a safe integer.'); | ||
assert(Number.isSafeInteger(maxExclusive), 'Argument "maxExclusive" must be a safe integer.'); | ||
assert(minInclusive <= maxExclusive, 'Argument "minInclusive" must be less or equal to argument "maxExclusive".'); | ||
return Math.floor(minInclusive + Math.random() * (maxExclusive - minInclusive)); | ||
} | ||
const randomIdBuffer = Buffer.allocUnsafeSlow(12); | ||
@@ -34,1 +40,9 @@ randomIdBuffer.writeUInt32BE(randomInteger(0, 0x100000000), 4); | ||
} | ||
/** | ||
* Throws an `AssertError` if `value` is falsy. | ||
*/ | ||
export function assert(value, message) { | ||
if (!value) { | ||
throw createAssertError(message); | ||
} | ||
} |
@@ -12,1 +12,9 @@ import { Duplex, Readable, Stream, Transform, Writable } from 'readable-stream'; | ||
export declare function isOpenTransformStream(stream: any): stream is Transform; | ||
/** | ||
* Creates a pair of Duplex Streams [a, b], such that | ||
* a.write(x) -> b.emit('data', x) and b.write(y) -> a.emit('data', y). | ||
*/ | ||
export declare function invertedStreams({ allowHalfOpen, objectMode, }?: { | ||
allowHalfOpen?: boolean | undefined; | ||
objectMode?: boolean | undefined; | ||
}): [Duplex, Duplex]; |
@@ -0,1 +1,2 @@ | ||
import { Duplex } from 'readable-stream'; | ||
export function isStream(stream) { | ||
@@ -52,1 +53,46 @@ return (stream !== null && | ||
} | ||
/** | ||
* Creates a pair of Duplex Streams [a, b], such that | ||
* a.write(x) -> b.emit('data', x) and b.write(y) -> a.emit('data', y). | ||
*/ | ||
export function invertedStreams({ allowHalfOpen = true, objectMode = false, } = {}) { | ||
const a = new Duplex({ | ||
allowHalfOpen, | ||
objectMode, | ||
read() { | ||
return; | ||
}, | ||
write(data, _encoding, callback) { | ||
b.push(data); | ||
callback(null); | ||
}, | ||
final(callback) { | ||
b.push(null); | ||
callback(null); | ||
}, | ||
destroy(error, callback) { | ||
b.destroy(); | ||
callback(error); | ||
}, | ||
}); | ||
const b = new Duplex({ | ||
allowHalfOpen, | ||
objectMode, | ||
read() { | ||
return; | ||
}, | ||
write(data, _encoding, callback) { | ||
a.push(data); | ||
callback(null); | ||
}, | ||
final(callback) { | ||
a.push(null); | ||
callback(null); | ||
}, | ||
destroy(error, callback) { | ||
a.destroy(); | ||
callback(error); | ||
}, | ||
}); | ||
return [a, b]; | ||
} |
@@ -1,2 +0,2 @@ | ||
import { EmitterInterface, SyncOtEmitter } from './eventEmitter'; | ||
import { EmitterInterface, SyncOtEmitter } from './event'; | ||
/** | ||
@@ -3,0 +3,0 @@ * The type of tasks run by `TaskRunner`s. |
@@ -1,3 +0,3 @@ | ||
import { strict as assert } from 'assert'; | ||
import { SyncOtEmitter } from './eventEmitter'; | ||
import { SyncOtEmitter } from './event'; | ||
import { assert } from './misc'; | ||
const emptyOptions = {}; | ||
@@ -25,7 +25,7 @@ /** | ||
}; | ||
assert.ok(typeof this.task === 'function', 'Argument "task" must be a function.'); | ||
assert.ok(Number.isSafeInteger(this.minDelay) && this.minDelay >= 0, 'Argument "minDelay" must be a safe integer >= 0.'); | ||
assert.ok(Number.isSafeInteger(this.maxDelay) && | ||
assert(typeof this.task === 'function', 'Argument "task" must be a function.'); | ||
assert(Number.isSafeInteger(this.minDelay) && this.minDelay >= 0, 'Argument "minDelay" must be a safe integer >= 0.'); | ||
assert(Number.isSafeInteger(this.maxDelay) && | ||
this.maxDelay >= this.minDelay, 'Argument "maxDelay" must be a safe integer >= minDelay.'); | ||
assert.ok((Number.isFinite(this.delayFactor) && this.delayFactor >= 1) || | ||
assert((Number.isFinite(this.delayFactor) && this.delayFactor >= 1) || | ||
this.delayFactor === 0, 'Argument "delayFactor" must be a finite number >= 1 or === 0.'); | ||
@@ -32,0 +32,0 @@ } |
@@ -1,2 +0,2 @@ | ||
import { strict as assert } from 'assert'; | ||
import { createAssertError } from './error'; | ||
/** | ||
@@ -8,3 +8,3 @@ * A simple function which throws an error, when a theoretically unreachable code path is executed anyway. | ||
export function assertUnreachable(_never) { | ||
return assert.fail('This should never happen!'); | ||
throw createAssertError('This should never happen!'); | ||
} | ||
@@ -11,0 +11,0 @@ /** |
{ | ||
"name": "@syncot/util", | ||
"version": "0.0.9", | ||
"version": "0.0.10", | ||
"description": "A collection of utilities required by other @syncot modules.", | ||
@@ -34,3 +34,3 @@ "keywords": [ | ||
}, | ||
"gitHead": "59f7016cbffea18fc14049e00fd4351c08748887" | ||
"gitHead": "5c7ad0bf43495567d9fc857718802b7ea52d37bb" | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
38366
994
18
1