-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwrite.ts
More file actions
59 lines (54 loc) · 1.7 KB
/
write.ts
File metadata and controls
59 lines (54 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/**
* @fileoverview Cache write entrypoints — `put` (insert/replace by
* key) and `remove` (single-key delete). Both reject wildcards; for
* pattern deletes use `clear({ prefix: 'foo*' })`.
*/
import { getSocketCacacheDir } from '../paths/socket'
import { TypeErrorCtor } from '../primordials/error'
import { StringPrototypeIncludes } from '../primordials/string'
import { getCacache } from './_internal'
import type { PutOptions } from './types'
/**
* Put data into the Socket shared cache with a key.
*
* @throws {TypeError} If key contains wildcards (*)
*
* @example
* ```typescript
* await put('socket-sdk:scans:abc123', Buffer.from('result data'))
* ```
*/
export async function put(
key: string,
data: string | Buffer,
options?: PutOptions | undefined,
) {
if (StringPrototypeIncludes(key, '*')) {
throw new TypeErrorCtor(
'Cache key cannot contain wildcards (*). Wildcards are only supported in clear({ prefix: "pattern*" }).',
)
}
const cacache = getCacache()
/* c8 ignore next - External cacache call */
return await cacache.put(getSocketCacacheDir(), key, data, options)
}
/**
* Remove an entry from the Socket shared cache by key.
*
* @throws {TypeError} If key contains wildcards (*)
*
* @example
* ```typescript
* await remove('socket-sdk:scans:abc123')
* ```
*/
export async function remove(key: string): Promise<unknown> {
if (StringPrototypeIncludes(key, '*')) {
throw new TypeErrorCtor(
'Cache key cannot contain wildcards (*). Use clear({ prefix: "pattern*" }) to remove multiple entries.',
)
}
const cacache = getCacache() as any
/* c8 ignore next - External cacache call */
return await cacache.rm.entry(getSocketCacacheDir(), key)
}