Comparing version 0.0.20220501 to 0.0.20230121
@@ -1,10 +0,17 @@ | ||
/** Convert ArrayBuffer or ArrayBufferView to DataView. */ | ||
export function asDataView(a) { | ||
if (a instanceof DataView) { | ||
function asArrayBufferView(T, a) { | ||
if (a instanceof T) { | ||
return a; | ||
} | ||
if (a instanceof ArrayBuffer) { | ||
return new DataView(a); | ||
return new T(a); | ||
} | ||
return new DataView(a.buffer, a.byteOffset, a.byteLength); | ||
return new T(a.buffer, a.byteOffset, a.byteLength); | ||
} | ||
/** Convert ArrayBuffer or ArrayBufferView to Uint8Array. */ | ||
export function asUint8Array(a) { | ||
return asArrayBufferView(Uint8Array, a); | ||
} | ||
/** Convert ArrayBuffer or ArrayBufferView to DataView. */ | ||
export function asDataView(a) { | ||
return asArrayBufferView(DataView, a); | ||
} |
@@ -1,10 +0,17 @@ | ||
/** Convert ArrayBuffer or ArrayBufferView to DataView. */ | ||
export function asDataView(a) { | ||
if (a instanceof DataView) { | ||
function asArrayBufferView(T, a) { | ||
if (a instanceof T) { | ||
return a; | ||
} | ||
if (a instanceof ArrayBuffer) { | ||
return new DataView(a); | ||
return new T(a); | ||
} | ||
return new DataView(a.buffer, a.byteOffset, a.byteLength); | ||
return new T(a.buffer, a.byteOffset, a.byteLength); | ||
} | ||
/** Convert ArrayBuffer or ArrayBufferView to Uint8Array. */ | ||
export function asUint8Array(a) { | ||
return asArrayBufferView(Uint8Array, a); | ||
} | ||
/** Convert ArrayBuffer or ArrayBufferView to DataView. */ | ||
export function asDataView(a) { | ||
return asArrayBufferView(DataView, a); | ||
} |
@@ -0,2 +1,4 @@ | ||
/** Convert ArrayBuffer or ArrayBufferView to Uint8Array. */ | ||
export declare function asUint8Array(a: BufferSource): Uint8Array; | ||
/** Convert ArrayBuffer or ArrayBufferView to DataView. */ | ||
export declare function asDataView(a: BufferSource): DataView; |
/** A list of objects that can be closed or destroyed. */ | ||
export class Closers extends Array { | ||
constructor() { | ||
super(...arguments); | ||
/** Close all objects in reverse order and clear the list. */ | ||
this.close = () => { | ||
for (let i = this.length - 1; i >= 0; --i) { | ||
this[i].close(); | ||
} | ||
this.splice(0, Infinity); | ||
}; | ||
} | ||
/** Close all objects in reverse order and clear the list. */ | ||
close = () => { | ||
for (let i = this.length - 1; i >= 0; --i) { | ||
this[i].close(); | ||
} | ||
this.splice(0, Infinity); | ||
}; | ||
/** Schedule a timeout or interval to be canceled via .close(). */ | ||
@@ -14,0 +11,0 @@ addTimeout(t) { |
/** A list of objects that can be closed or destroyed. */ | ||
export class Closers extends Array { | ||
constructor() { | ||
super(...arguments); | ||
/** Close all objects in reverse order and clear the list. */ | ||
this.close = () => { | ||
for (let i = this.length - 1; i >= 0; --i) { | ||
this[i].close(); | ||
} | ||
this.splice(0, Infinity); | ||
}; | ||
} | ||
/** Close all objects in reverse order and clear the list. */ | ||
close = () => { | ||
for (let i = this.length - 1; i >= 0; --i) { | ||
this[i].close(); | ||
} | ||
this.splice(0, Infinity); | ||
}; | ||
/** Schedule a timeout or interval to be canceled via .close(). */ | ||
@@ -14,0 +11,0 @@ addTimeout(t) { |
@@ -7,2 +7,2 @@ /** Yield all values from an iterable but catch any error. */ | ||
*/ | ||
export declare function flatMapOnce<T, R>(f: (item: T) => Iterable<R> | AsyncIterable<R>, iterable: AsyncIterable<T>): AsyncIterable<R>; | ||
export declare function flatMapOnce<T, R>(f: (item: T) => Iterable<R> | AsyncIterable<R>, iterable: Iterable<T> | AsyncIterable<T>): AsyncIterable<R>; |
@@ -10,2 +10,3 @@ /** | ||
export class KeyMap { | ||
keyOf; | ||
/** | ||
@@ -17,4 +18,4 @@ * Constructor. | ||
this.keyOf = keyOf; | ||
this.m = new Map(); | ||
} | ||
m = new Map(); | ||
get size() { return this.m.size; } | ||
@@ -52,5 +53,6 @@ has(key) { | ||
constructor(keyOf) { | ||
this.size_ = 0; | ||
this.m = new KeyMap(keyOf); | ||
} | ||
m; | ||
size_ = 0; | ||
/** Number of distinct keys. */ | ||
@@ -106,3 +108,17 @@ get dimension() { return this.m.size; } | ||
} | ||
/** Iterate over key-value pairs. */ | ||
*[Symbol.iterator]() { | ||
for (const [key, values] of this.associations()) { | ||
for (const value of values) { | ||
yield [key, value]; | ||
} | ||
} | ||
} | ||
} | ||
/** Container that associates a key with multiple distinct values. */ | ||
export class MultiMap extends KeyMultiMap { | ||
constructor() { | ||
super((k) => k); | ||
} | ||
} | ||
/** | ||
@@ -121,5 +137,6 @@ * MultiSet that transforms keys. | ||
constructor(keyOf) { | ||
this.size_ = 0; | ||
this.m = new KeyMap(keyOf); | ||
} | ||
m; | ||
size_ = 0; | ||
/** Number of distinct keys. */ | ||
@@ -126,0 +143,0 @@ get dimension() { return this.m.size; } |
@@ -10,2 +10,3 @@ /** | ||
export class KeyMap { | ||
keyOf; | ||
/** | ||
@@ -17,4 +18,4 @@ * Constructor. | ||
this.keyOf = keyOf; | ||
this.m = new Map(); | ||
} | ||
m = new Map(); | ||
get size() { return this.m.size; } | ||
@@ -52,5 +53,6 @@ has(key) { | ||
constructor(keyOf) { | ||
this.size_ = 0; | ||
this.m = new KeyMap(keyOf); | ||
} | ||
m; | ||
size_ = 0; | ||
/** Number of distinct keys. */ | ||
@@ -106,3 +108,17 @@ get dimension() { return this.m.size; } | ||
} | ||
/** Iterate over key-value pairs. */ | ||
*[Symbol.iterator]() { | ||
for (const [key, values] of this.associations()) { | ||
for (const value of values) { | ||
yield [key, value]; | ||
} | ||
} | ||
} | ||
} | ||
/** Container that associates a key with multiple distinct values. */ | ||
export class MultiMap extends KeyMultiMap { | ||
constructor() { | ||
super((k) => k); | ||
} | ||
} | ||
/** | ||
@@ -121,5 +137,6 @@ * MultiSet that transforms keys. | ||
constructor(keyOf) { | ||
this.size_ = 0; | ||
this.m = new KeyMap(keyOf); | ||
} | ||
m; | ||
size_ = 0; | ||
/** Number of distinct keys. */ | ||
@@ -126,0 +143,0 @@ get dimension() { return this.m.size; } |
@@ -62,3 +62,9 @@ /** | ||
associations(): IterableIterator<[key: K, values: ReadonlySet<V>]>; | ||
/** Iterate over key-value pairs. */ | ||
[Symbol.iterator](): IterableIterator<[key: K, value: V]>; | ||
} | ||
/** Container that associates a key with multiple distinct values. */ | ||
export declare class MultiMap<K, V> extends KeyMultiMap<K, V, K> { | ||
constructor(); | ||
} | ||
/** | ||
@@ -65,0 +71,0 @@ * MultiSet that transforms keys. |
import { __importDefault, __importStar } from "tslib"; | ||
import _cjsDefaultImport0 from "minimalistic-assert"; const assert = __importDefault(_cjsDefaultImport0).default; | ||
export { assert }; | ||
export { console, crypto, } from "./platform_browser.js"; | ||
export { console, crypto, delay, concatBuffers } from "./platform_browser.js"; | ||
export * from "./buffer_browser.js"; | ||
@@ -11,1 +11,2 @@ export * from "./closers_browser.js"; | ||
export * from "./string_browser.js"; | ||
export * from "./timer_browser.js"; |
import { __importDefault, __importStar } from "tslib"; | ||
import _cjsDefaultImport0 from "minimalistic-assert"; const assert = __importDefault(_cjsDefaultImport0).default; | ||
export { assert }; | ||
export { console, crypto, } from "./platform_node.js"; | ||
export { console, crypto, delay, concatBuffers } from "./platform_node.js"; | ||
export * from "./buffer_node.js"; | ||
@@ -11,1 +11,2 @@ export * from "./closers_node.js"; | ||
export * from "./string_node.js"; | ||
export * from "./timer_node.js"; |
import assert from "minimalistic-assert"; | ||
export { assert }; | ||
export { console, crypto, } from "./platform_node"; | ||
export * from "./buffer"; | ||
export * from "./closers"; | ||
export * from "./crypto"; | ||
export * from "./iter"; | ||
export * from "./key-map"; | ||
export * from "./string"; | ||
export { console, crypto, delay, concatBuffers } from "./platform_node.js"; | ||
export * from "./buffer.js"; | ||
export * from "./closers.js"; | ||
export * from "./crypto.js"; | ||
export * from "./iter.js"; | ||
export * from "./key-map.js"; | ||
export * from "./string.js"; | ||
export * from "./timer.js"; |
@@ -0,3 +1,5 @@ | ||
export declare const console: Console; | ||
export declare const crypto: Crypto; | ||
export declare function timingSafeEqual(a: Uint8Array, b: Uint8Array): boolean; | ||
export declare const console: Console; | ||
export declare function delay<T = void>(after: number, value?: T): Promise<T>; | ||
export declare function concatBuffers(list: readonly Uint8Array[], totalLength?: number): Uint8Array; |
@@ -0,4 +1,8 @@ | ||
import { __importDefault, __importStar } from "tslib"; | ||
import _cjsDefaultImport0 from "minimalistic-assert"; const assert = __importDefault(_cjsDefaultImport0).default; | ||
export const console = globalThis.console; | ||
export const crypto = globalThis.crypto; | ||
// https://codahale.com/a-lesson-in-timing-attacks/ | ||
export function timingSafeEqual(a, b) { | ||
// length has been checked by caller | ||
let result = 0; | ||
@@ -10,2 +14,15 @@ for (let i = 0; i < a.byteLength; ++i) { | ||
} | ||
export const console = globalThis.console; | ||
export function delay(after, value) { | ||
return new Promise((resolve) => setTimeout(resolve, after, value)); | ||
} | ||
export function concatBuffers(list, totalLength) { | ||
totalLength ??= list.reduce((l, { byteLength }) => l + byteLength, 0); | ||
const c = new Uint8Array(totalLength); | ||
let offset = 0; | ||
for (const part of list) { | ||
c.set(part, offset); | ||
offset += part.byteLength; | ||
} | ||
assert.equal(offset, totalLength); | ||
return c; | ||
} |
import { timingSafeEqual } from "node:crypto"; | ||
export declare const crypto: Crypto; | ||
export { timingSafeEqual }; | ||
/** Console on stderr. */ | ||
export declare const console: Console; | ||
/** Web Crypto API. */ | ||
export declare const crypto: Crypto; | ||
/** Make a Promise that resolves after specified duration. */ | ||
export declare const delay: <T = void>(after: number, value?: T) => Promise<T>; | ||
/** Concatenate Uint8Arrays. */ | ||
export declare function concatBuffers(arr: readonly Uint8Array[], totalLength?: number): Uint8Array; |
import { Console } from "node:console"; | ||
import { timingSafeEqual, webcrypto } from "node:crypto"; | ||
export const crypto = webcrypto; | ||
import { setTimeout as setTimeoutPromise } from "node:timers/promises"; | ||
export { timingSafeEqual }; | ||
/** Console on stderr. */ | ||
export const console = new Console(process.stderr); | ||
/** Web Crypto API. */ | ||
export const crypto = webcrypto; | ||
/** Make a Promise that resolves after specified duration. */ | ||
export const delay = setTimeoutPromise; | ||
/** Concatenate Uint8Arrays. */ | ||
export function concatBuffers(arr, totalLength) { | ||
return Buffer.concat(arr, totalLength); | ||
} |
{ | ||
"name": "@ndn/util", | ||
"version": "0.0.20220501", | ||
"version": "0.0.20230121", | ||
"description": "NDNts: general utilities", | ||
@@ -27,9 +27,8 @@ "keywords": [ | ||
"minimalistic-assert": "^1.0.1", | ||
"tslib": "^2.4.0" | ||
"tslib": "^2.4.1" | ||
}, | ||
"engines": { | ||
"node": "^16.14.2 || ^18.0.0" | ||
"node": "^16.17.1 || ^18.10.0 || ^19.0.0" | ||
}, | ||
"types": "lib/mod.d.ts", | ||
"readme": "# @ndn/util\n\nThis package is part of [NDNts](https://yoursunny.com/p/NDNts/), Named Data Networking libraries for the modern web.\n\nThis package contains general utilities used in NDNts codebase.\nThis package cannot depend on other NDNts packages.\n" | ||
"types": "lib/mod.d.ts" | ||
} |
Sorry, the diff of this file is not supported yet
27779
31
874
Updatedtslib@^2.4.1