Skip to content

Commit 2d215e3

Browse files
authored
🤖 Merge PR DefinitelyTyped#74241 [cacache] fix types and update version by @hexchain
1 parent 67d26c0 commit 2d215e3

File tree

3 files changed

+83
-13
lines changed

3 files changed

+83
-13
lines changed

types/cacache/cacache-tests.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const cachePath = "";
66
cacache.ls(cachePath).then(() => {});
77

88
cacache.ls.stream(cachePath).on("data", data => {
9-
data; // $ExpectType any
9+
data; // $ExpectType Cache
1010
});
1111

1212
cacache.get(cachePath, "my-thing", { memoize: true }).then(() => {});
@@ -21,15 +21,20 @@ cacache.get
2121
metadata; // $ExpectType any
2222
})
2323
.on("integrity", integrity => {
24-
integrity; // $ExpectType any
24+
integrity; // $ExpectType string
25+
})
26+
.on("size", size => {
27+
size; // $ExpectType number
2528
})
2629
.pipe(fs.createWriteStream("./x.tgz"));
2730

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

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

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

3439
cacache
3540
.put(cachePath, "registry.npmjs.org|[email protected]", Buffer.from([]), {
@@ -43,7 +48,12 @@ cacache
4348
fs.createReadStream("").pipe(
4449
cacache.put
4550
.stream(cachePath, "registry.npmjs.org|[email protected]")
46-
.on("integrity", d => console.log(`integrity digest is ${d}`)),
51+
.on("integrity", integrity => {
52+
integrity; // $ExpectType string
53+
})
54+
.on("size", size => {
55+
size; // $ExpectType number
56+
}),
4757
);
4858

4959
cacache.rm.all(cachePath).then(() => {

types/cacache/index.d.ts

Lines changed: 66 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
/// <reference types="node" />
22

3+
import { Minipass } from "minipass";
4+
import * as fs from "node:fs";
5+
import { EventEmitter } from "node:stream";
6+
37
export interface CacheObject {
48
/** Subresource Integrity hash for the content this entry refers to. */
59
integrity: string;
@@ -36,6 +40,7 @@ export namespace get {
3640
options: any[];
3741
source: string;
3842
};
43+
stat: fs.Stats;
3944
}
4045

4146
interface Options {
@@ -74,16 +79,29 @@ export namespace get {
7479
size?: number | undefined;
7580
}
7681

82+
interface GetStreamEvents extends Minipass.Events<Buffer> {
83+
// eslint-disable-next-line @definitelytyped/no-single-element-tuple-type
84+
integrity: [integrity: string];
85+
86+
// eslint-disable-next-line @definitelytyped/no-single-element-tuple-type
87+
size: [size: number];
88+
89+
// eslint-disable-next-line @definitelytyped/no-single-element-tuple-type
90+
metadata: [metadata: any];
91+
}
92+
93+
type CopyResultObject = Pick<CacheObject, "metadata" | "integrity" | "size">;
94+
7795
namespace copy {
7896
function byDigest(cachePath: string, hash: string, dest: string, opts?: Options): Promise<string>;
7997
}
8098

8199
namespace stream {
82-
function byDigest(cachePath: string, hash: string, opts?: Options): NodeJS.ReadableStream;
100+
function byDigest(cachePath: string, hash: string, opts?: Options): Minipass<Buffer, never>;
83101
}
84102

85-
function byDigest(cachePath: string, hash: string, opts?: Options): Promise<string>;
86-
function copy(cachePath: string, key: string, dest: string, opts?: Options): Promise<CacheObject>;
103+
function byDigest(cachePath: string, hash: string, opts?: Options): Promise<Buffer>;
104+
function copy(cachePath: string, key: string, dest: string, opts?: Options): Promise<CopyResultObject>;
87105

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

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

133151
export namespace put {
152+
interface IntegrityEmitterEvents {
153+
// eslint-disable-next-line @definitelytyped/no-single-element-tuple-type
154+
integrity: [integrity: string];
155+
156+
// eslint-disable-next-line @definitelytyped/no-single-element-tuple-type
157+
size: [size: number];
158+
159+
// eslint-disable-next-line @definitelytyped/no-single-element-tuple-type
160+
error: [err: unknown];
161+
}
162+
134163
interface Options {
135164
/**
136165
* Default: `['sha512']`
@@ -144,7 +173,8 @@ export namespace put {
144173
* Currently only supports one algorithm at a time (i.e., an array
145174
* length of exactly `1`). Has no effect if `opts.integrity` is present.
146175
*/
147-
algorithms?: string[] | undefined;
176+
// eslint-disable-next-line @definitelytyped/no-single-element-tuple-type
177+
algorithms?: [string] | undefined;
148178

149179
/**
150180
* If present, the pre-calculated digest for the inserted content. If
@@ -189,14 +219,43 @@ export namespace put {
189219
* Prefix to append on the temporary directory name inside the cache's tmp dir.
190220
*/
191221
tmpPrefix?: null | string | undefined;
222+
223+
/**
224+
* Default: `undefined`
225+
*
226+
* (Streaming only) If present, uses the provided event emitter as a
227+
* source of truth for both integrity and size. This allows use cases
228+
* where integrity is already being calculated outside of cacache to
229+
* reuse that data instead of calculating it a second time.
230+
*
231+
* The emitter must emit both the 'integrity' and 'size' events.
232+
*
233+
* NOTE: If this option is provided, you must verify that you receive
234+
* the correct integrity value yourself and emit an 'error' event if
235+
* there is a mismatch. ssri Integrity Streams do this for you when
236+
* given an expected integrity.
237+
*/
238+
integrityEmitter?: EventEmitter<IntegrityEmitterEvents> | undefined;
239+
}
240+
241+
interface PutStreamEvents extends Minipass.Events<never> {
242+
// eslint-disable-next-line @definitelytyped/no-single-element-tuple-type
243+
integrity: [integrity: string];
244+
245+
// eslint-disable-next-line @definitelytyped/no-single-element-tuple-type
246+
size: [size: number];
192247
}
193248

194249
/**
195250
* Returns a Writable Stream that inserts data written to it into the cache.
196251
* Emits an `integrity` event with the digest of written contents when it
197252
* succeeds.
198253
*/
199-
function stream(cachePath: string, key: string, opts?: Options): NodeJS.WritableStream;
254+
function stream(
255+
cachePath: string,
256+
key: string,
257+
opts?: Options,
258+
): Minipass<never, Minipass.ContiguousData, PutStreamEvents>;
200259
}
201260

202261
export namespace rm {

types/cacache/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
{
22
"private": true,
33
"name": "@types/cacache",
4-
"version": "19.0.9999",
4+
"version": "20.0.9999",
55
"projects": [
66
"https://github.com/npm/cacache#readme"
77
],
88
"dependencies": {
9-
"@types/node": "*"
9+
"@types/node": "*",
10+
"minipass": "*"
1011
},
1112
"devDependencies": {
1213
"@types/cacache": "workspace:."

0 commit comments

Comments
 (0)