1#!/usr/bin/env node
2
3/// Usage: oe-npm-cache <cache-dir> <type> <key> <file-name>
4///    <type> ... meta - metainformation about package
5///               tgz  - tarball
6
7const process = require("node:process");
8
9module.paths.unshift("@@libdir@@/node_modules/npm/node_modules");
10
11const cacache = require('cacache')
12const fs = require('fs')
13
14// argv[0] is 'node', argv[1] is this script
15const cache_dir = process.argv[2]
16const type      = process.argv[3]
17const key       = process.argv[4]
18const file      = process.argv[5]
19
20const data = fs.readFileSync(file)
21
22// metadata content is highly nodejs dependent; when cache entries are not
23// found, place debug statements in 'make-fetch-happen/lib/cache/policy.js'
24// (CachePolicy::satisfies())
25const xlate = {
26    'meta': {
27	'key_prefix': 'make-fetch-happen:request-cache:',
28	'metadata': function() {
29	    return {
30		time: Date.now(),
31		url:  key,
32		reqHeaders: {
33		    'accept': 'application/json',
34		},
35		resHeaders: {
36		    "content-type": "application/json",
37		    "status": 200,
38		},
39		options: {
40		    compress: true,
41		}
42	    };
43	},
44    },
45
46    'tgz': {
47	'key_prefix': 'make-fetch-happen:request-cache:',
48	'metadata': function() {
49	    return {
50		time: Date.now(),
51		url:  key,
52		reqHeaders: {
53		    'accept': '*/*',
54		},
55		resHeaders: {
56		    "content-type": "application/octet-stream",
57		    "status": 200,
58		},
59		options: {
60		    compress: true,
61		},
62	    };
63	},
64    },
65};
66
67const info = xlate[type];
68let opts = {}
69
70if (info.metadata) {
71    opts['metadata'] = info.metadata();
72}
73
74cacache.put(cache_dir, info.key_prefix + key, data, opts)
75    .then(integrity => {
76	console.log(`Saved content of ${key} (${file}).`);
77})
78