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