Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions types/cacache/cacache-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const cachePath = "";
cacache.ls(cachePath).then(() => {});

cacache.ls.stream(cachePath).on("data", data => {
data; // $ExpectType any
data; // $ExpectType Cache
});

cacache.get(cachePath, "my-thing", { memoize: true }).then(() => {});
Expand All @@ -21,15 +21,20 @@ cacache.get
metadata; // $ExpectType any
})
.on("integrity", integrity => {
integrity; // $ExpectType any
integrity; // $ExpectType string
})
.on("size", size => {
size; // $ExpectType number
})
.pipe(fs.createWriteStream("./x.tgz"));

cacache.get.stream.byDigest(cachePath, "sha512-SoMeDIGest+64==").pipe(fs.createWriteStream("./x.tgz"));

cacache.get.info(cachePath, "my-thing").then(() => {});

cacache.get.hasContent(cachePath, "sha521-NOT+IN/CACHE==").then(() => {});
cacache.get.hasContent(cachePath, "sha521-NOT+IN/CACHE==").then((res) => {
res; // $ExpectType HasContentObject | false
});

cacache
.put(cachePath, "registry.npmjs.org|cacache@1.0.0", Buffer.from([]), {
Expand All @@ -43,7 +48,12 @@ cacache
fs.createReadStream("").pipe(
cacache.put
.stream(cachePath, "registry.npmjs.org|cacache@1.0.0")
.on("integrity", d => console.log(`integrity digest is ${d}`)),
.on("integrity", integrity => {
integrity; // $ExpectType string
})
.on("size", size => {
size; // $ExpectType number
}),
);

cacache.rm.all(cachePath).then(() => {
Expand Down
73 changes: 66 additions & 7 deletions types/cacache/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
/// <reference types="node" />

import { Minipass } from "minipass";
import * as fs from "node:fs";
import { EventEmitter } from "node:stream";

export interface CacheObject {
/** Subresource Integrity hash for the content this entry refers to. */
integrity: string;
Expand Down Expand Up @@ -36,6 +40,7 @@ export namespace get {
options: any[];
source: string;
};
stat: fs.Stats;
}

interface Options {
Expand Down Expand Up @@ -74,16 +79,29 @@ export namespace get {
size?: number | undefined;
}

interface GetStreamEvents extends Minipass.Events<Buffer> {
// eslint-disable-next-line @definitelytyped/no-single-element-tuple-type
integrity: [integrity: string];

// eslint-disable-next-line @definitelytyped/no-single-element-tuple-type
size: [size: number];

// eslint-disable-next-line @definitelytyped/no-single-element-tuple-type
metadata: [metadata: any];
}

type CopyResultObject = Pick<CacheObject, "metadata" | "integrity" | "size">;

namespace copy {
function byDigest(cachePath: string, hash: string, dest: string, opts?: Options): Promise<string>;
}

namespace stream {
function byDigest(cachePath: string, hash: string, opts?: Options): NodeJS.ReadableStream;
function byDigest(cachePath: string, hash: string, opts?: Options): Minipass<Buffer, never>;
}

function byDigest(cachePath: string, hash: string, opts?: Options): Promise<string>;
function copy(cachePath: string, key: string, dest: string, opts?: Options): Promise<CacheObject>;
function byDigest(cachePath: string, hash: string, opts?: Options): Promise<Buffer>;
function copy(cachePath: string, key: string, dest: string, opts?: Options): Promise<CopyResultObject>;

/**
* Looks up a Subresource Integrity hash in the cache. If content exists
Expand Down Expand Up @@ -114,7 +132,7 @@ export namespace get {
* entirely. This version does not emit the `metadata` and `integrity`
* events at all.
*/
function stream(cachePath: string, key: string, opts?: Options): NodeJS.ReadableStream;
function stream(cachePath: string, key: string, opts?: Options): Minipass<Buffer, never, GetStreamEvents>;
}

export namespace ls {
Expand All @@ -127,10 +145,21 @@ export namespace ls {
* This works just like `ls`, except `get.info` entries are returned as
* `'data'` events on the returned stream.
*/
function stream(cachePath: string): NodeJS.ReadableStream;
function stream(cachePath: string): Minipass<Cache, never>;
}

export namespace put {
interface IntegrityEmitterEvents {
// eslint-disable-next-line @definitelytyped/no-single-element-tuple-type
integrity: [integrity: string];

// eslint-disable-next-line @definitelytyped/no-single-element-tuple-type
size: [size: number];

// eslint-disable-next-line @definitelytyped/no-single-element-tuple-type
error: [err: unknown];
}

interface Options {
/**
* Default: `['sha512']`
Expand All @@ -144,7 +173,8 @@ export namespace put {
* Currently only supports one algorithm at a time (i.e., an array
* length of exactly `1`). Has no effect if `opts.integrity` is present.
*/
algorithms?: string[] | undefined;
// eslint-disable-next-line @definitelytyped/no-single-element-tuple-type
algorithms?: [string] | undefined;

/**
* If present, the pre-calculated digest for the inserted content. If
Expand Down Expand Up @@ -189,14 +219,43 @@ export namespace put {
* Prefix to append on the temporary directory name inside the cache's tmp dir.
*/
tmpPrefix?: null | string | undefined;

/**
* Default: `undefined`
*
* (Streaming only) If present, uses the provided event emitter as a
* source of truth for both integrity and size. This allows use cases
* where integrity is already being calculated outside of cacache to
* reuse that data instead of calculating it a second time.
*
* The emitter must emit both the 'integrity' and 'size' events.
*
* NOTE: If this option is provided, you must verify that you receive
* the correct integrity value yourself and emit an 'error' event if
* there is a mismatch. ssri Integrity Streams do this for you when
* given an expected integrity.
*/
integrityEmitter?: EventEmitter<IntegrityEmitterEvents> | undefined;
}

interface PutStreamEvents extends Minipass.Events<never> {
// eslint-disable-next-line @definitelytyped/no-single-element-tuple-type
integrity: [integrity: string];

// eslint-disable-next-line @definitelytyped/no-single-element-tuple-type
size: [size: number];
}

/**
* Returns a Writable Stream that inserts data written to it into the cache.
* Emits an `integrity` event with the digest of written contents when it
* succeeds.
*/
function stream(cachePath: string, key: string, opts?: Options): NodeJS.WritableStream;
function stream(
cachePath: string,
key: string,
opts?: Options,
): Minipass<never, Minipass.ContiguousData, PutStreamEvents>;
}

export namespace rm {
Expand Down
5 changes: 3 additions & 2 deletions types/cacache/package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
{
"private": true,
"name": "@types/cacache",
"version": "19.0.9999",
"version": "20.0.9999",
"projects": [
"https://github.com/npm/cacache#readme"
],
"dependencies": {
"@types/node": "*"
"@types/node": "*",
"minipass": "*"
},
"devDependencies": {
"@types/cacache": "workspace:."
Expand Down