@atproto/common
Advanced tools
Comparing version 0.0.1 to 0.1.0
@@ -1,9 +0,18 @@ | ||
export interface Def<T> { | ||
import { ZodError } from 'zod'; | ||
export interface Checkable<T> { | ||
parse: (obj: unknown) => T; | ||
safeParse: (obj: unknown) => { | ||
success: boolean; | ||
success: true; | ||
data: T; | ||
} | { | ||
success: false; | ||
error: ZodError; | ||
}; | ||
} | ||
export declare const is: <T>(obj: unknown, def: Def<T>) => obj is T; | ||
export declare const assure: <T>(def: Def<T>, obj: unknown) => T; | ||
export interface Def<T> { | ||
name: string; | ||
schema: Checkable<T>; | ||
} | ||
export declare const is: <T>(obj: unknown, def: Checkable<T>) => obj is T; | ||
export declare const assure: <T>(def: Checkable<T>, obj: unknown) => T; | ||
export declare const isObject: (obj: unknown) => obj is Record<string, unknown>; |
export * as check from './check'; | ||
export * as util from './util'; | ||
export * from './async'; | ||
export * from './util'; | ||
export * from './tid'; | ||
export * from './blocks'; | ||
export * from './ipld'; | ||
export * from './logger'; | ||
export * from './types'; | ||
export * from './streams'; | ||
export * from './times'; |
import * as mf from 'multiformats/cid'; | ||
import { z } from 'zod'; | ||
export declare const isCid: (str: string) => boolean; | ||
declare const bytes: z.ZodType<Uint8Array, z.ZodTypeDef, Uint8Array>; | ||
export declare type Bytes = z.infer<typeof bytes>; | ||
export declare const def: { | ||
string: z.ZodString; | ||
import { Def } from './check'; | ||
export declare const schema: { | ||
cid: z.ZodEffects<z.ZodEffects<z.ZodAny, any, any>, mf.CID, any>; | ||
strToCid: z.ZodEffects<z.ZodEffects<z.ZodString, string, string>, mf.CID, string>; | ||
bytes: z.ZodType<Uint8Array, z.ZodTypeDef, Uint8Array>; | ||
strToInt: z.ZodEffects<z.ZodEffects<z.ZodString, string, string>, number, string>; | ||
strToBool: z.ZodEffects<z.ZodString, boolean, string>; | ||
string: z.ZodString; | ||
record: z.ZodRecord<z.ZodString, z.ZodUnknown>; | ||
unknown: z.ZodUnknown; | ||
}; | ||
export {}; | ||
export declare const def: { | ||
cid: Def<mf.CID>; | ||
bytes: Def<Uint8Array>; | ||
string: Def<string>; | ||
record: Def<Record<string, unknown>>; | ||
unknown: Def<unknown>; | ||
}; | ||
export declare type ArrayEl<A> = A extends readonly (infer T)[] ? T : never; |
@@ -1,4 +0,7 @@ | ||
/// <reference types="node" /> | ||
export declare const noUndefinedVals: <T>(obj: Record<string, T>) => Record<string, T>; | ||
export declare const wait: (ms: number) => Promise<unknown>; | ||
export declare const bailableWait: (ms: number) => { | ||
bail: () => void; | ||
wait: () => Promise<void>; | ||
}; | ||
export declare const flattenUint8Arrays: (arrs: Uint8Array[]) => Uint8Array; | ||
@@ -11,1 +14,3 @@ export declare const streamToArray: (stream: AsyncIterable<Uint8Array>) => Promise<Uint8Array>; | ||
export declare const errHasMsg: (err: unknown, msg: string) => boolean; | ||
export declare const chunkArray: <T>(arr: T[], chunkSize: number) => T[][]; | ||
export declare const range: (num: number) => number[]; |
{ | ||
"name": "@atproto/common", | ||
"version": "0.0.1", | ||
"main": "dist/index.js", | ||
"version": "0.1.0", | ||
"main": "src/index.ts", | ||
"license": "MIT", | ||
@@ -6,0 +6,0 @@ "scripts": { |
@@ -1,11 +0,20 @@ | ||
export interface Def<T> { | ||
import { ZodError } from 'zod' | ||
export interface Checkable<T> { | ||
parse: (obj: unknown) => T | ||
safeParse: (obj: unknown) => { success: boolean } | ||
safeParse: ( | ||
obj: unknown, | ||
) => { success: true; data: T } | { success: false; error: ZodError } | ||
} | ||
export const is = <T>(obj: unknown, def: Def<T>): obj is T => { | ||
export interface Def<T> { | ||
name: string | ||
schema: Checkable<T> | ||
} | ||
export const is = <T>(obj: unknown, def: Checkable<T>): obj is T => { | ||
return def.safeParse(obj).success | ||
} | ||
export const assure = <T>(def: Def<T>, obj: unknown): T => { | ||
export const assure = <T>(def: Checkable<T>, obj: unknown): T => { | ||
return def.parse(obj) | ||
@@ -12,0 +21,0 @@ } |
export * as check from './check' | ||
export * as util from './util' | ||
export * from './async' | ||
export * from './util' | ||
export * from './tid' | ||
export * from './blocks' | ||
export * from './ipld' | ||
export * from './logger' | ||
export * from './types' | ||
export * from './streams' | ||
export * from './times' |
import * as mf from 'multiformats/cid' | ||
import { z } from 'zod' | ||
import { Def } from './check' | ||
const cid = z | ||
const cidSchema = z | ||
.any() | ||
@@ -11,35 +12,33 @@ .refine((obj: unknown) => mf.CID.asCID(obj) !== null, { | ||
export const isCid = (str: string): boolean => { | ||
try { | ||
mf.CID.parse(str) | ||
return true | ||
} catch (err) { | ||
return false | ||
} | ||
export const schema = { | ||
cid: cidSchema, | ||
bytes: z.instanceof(Uint8Array), | ||
string: z.string(), | ||
record: z.record(z.string(), z.unknown()), | ||
unknown: z.unknown(), | ||
} | ||
const strToCid = z | ||
.string() | ||
.refine(isCid, { message: 'Not a valid CID' }) | ||
.transform((str: string) => mf.CID.parse(str)) | ||
const bytes = z.instanceof(Uint8Array) | ||
export type Bytes = z.infer<typeof bytes> | ||
const strToInt = z | ||
.string() | ||
.refine((str) => !isNaN(parseInt(str)), { | ||
message: 'Cannot parse string to integer', | ||
}) | ||
.transform((str) => parseInt(str)) | ||
const strToBool = z.string().transform((str) => str === 'true' || str === 't') | ||
export const def = { | ||
string: z.string(), | ||
cid, | ||
strToCid, | ||
bytes, | ||
strToInt, | ||
strToBool, | ||
cid: { | ||
name: 'cid', | ||
schema: schema.cid, | ||
} as Def<mf.CID>, | ||
bytes: { | ||
name: 'bytes', | ||
schema: schema.bytes, | ||
} as Def<Uint8Array>, | ||
string: { | ||
name: 'string', | ||
schema: schema.string, | ||
} as Def<string>, | ||
record: { | ||
name: 'record', | ||
schema: schema.record, | ||
} as Def<Record<string, unknown>>, | ||
unknown: { | ||
name: 'unknown', | ||
schema: schema.unknown, | ||
} as Def<unknown>, | ||
} | ||
export type ArrayEl<A> = A extends readonly (infer T)[] ? T : never |
@@ -16,2 +16,16 @@ export const noUndefinedVals = <T>( | ||
export const bailableWait = ( | ||
ms: number, | ||
): { bail: () => void; wait: () => Promise<void> } => { | ||
let bail | ||
const waitPromise = new Promise<void>((res) => { | ||
const timeout = setTimeout(res, ms) | ||
bail = () => { | ||
clearTimeout(timeout) | ||
res() | ||
} | ||
}) | ||
return { bail, wait: () => waitPromise } | ||
} | ||
export const flattenUint8Arrays = (arrs: Uint8Array[]): Uint8Array => { | ||
@@ -71,3 +85,3 @@ const length = arrs.reduce((acc, cur) => { | ||
): err is NodeJS.ErrnoException => { | ||
return !!err && 'code' in err | ||
return !!err && err['code'] | ||
} | ||
@@ -78,1 +92,20 @@ | ||
} | ||
export const chunkArray = <T>(arr: T[], chunkSize: number): T[][] => { | ||
return arr.reduce((acc, cur, i) => { | ||
const chunkI = Math.floor(i / chunkSize) | ||
if (!acc[chunkI]) { | ||
acc[chunkI] = [] | ||
} | ||
acc[chunkI].push(cur) | ||
return acc | ||
}, [] as T[][]) | ||
} | ||
export const range = (num: number): number[] => { | ||
const nums: number[] = [] | ||
for (let i = 0; i < num; i++) { | ||
nums.push(i) | ||
} | ||
return nums | ||
} |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
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
45
10681
857495
3