11/// <reference types="node" />
22
3+ import { Minipass } from "minipass" ;
4+ import * as fs from "node:fs" ;
5+ import { EventEmitter } from "node:stream" ;
6+
37export 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
120138export 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
133151export 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
202261export namespace rm {
0 commit comments