aboutsummaryrefslogtreecommitdiffstats
path: root/meta-oe/recipes-devtools/nodejs/nodejs-oe-cache-16.19/oe-npm-cache
blob: f59620764851aea353b2b9a2b8778862cfc7bfc7 (plain)
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/usr/bin/env node

/// Usage: oe-npm-cache <cache-dir> <type> <key> <file-name>
///    <type> ... meta - metainformation about package
///               tgz  - tarball

const process = require("node:process");

module.paths.unshift("@@libdir@@/node_modules/npm/node_modules");

const cacache = require('cacache')
const fs = require('fs')

// argv[0] is 'node', argv[1] is this script
const cache_dir = process.argv[2]
const type      = process.argv[3]
const key       = process.argv[4]
const file      = process.argv[5]

const data = fs.readFileSync(file)

// metadata content is highly nodejs dependent; when cache entries are not
// found, place debug statements in 'make-fetch-happen/lib/cache/policy.js'
// (CachePolicy::satisfies())
const xlate = {
    'meta': {
	'key_prefix': 'make-fetch-happen:request-cache:',
	'metadata': function() {
	    return {
		time: Date.now(),
		url:  key,
		reqHeaders: {
		    'accept': 'application/vnd.npm.install-v1+json; q=1.0, application/json; q=0.8, */*',
		},
		resHeaders: {
		    "content-type": "application/json",
		    "status": 200,
		},
		options: {
		    compress: true,
		}
	    };
	},
    },

    'tgz': {
	'key_prefix': 'make-fetch-happen:request-cache:',
	'metadata': function() {
	    return {
		time: Date.now(),
		url:  key,
		reqHeaders: {
		    'accept': '*/*',
		},
		resHeaders: {
		    "content-type": "application/octet-stream",
		    "status": 200,
		},
		options: {
		    compress: true,
		},
	    };
	},
    },
};

const info = xlate[type];
let opts = {}

if (info.metadata) {
    opts['metadata'] = info.metadata();
}

cacache.put(cache_dir, info.key_prefix + key, data, opts)
    .then(integrity => {
	console.log(`Saved content of ${key} (${file}).`);
})