Enrico Scholz 5bffa3f2f2 nodejs-oe-cache-native: initial checkin
This implements an 'npm cache add' like functionality but allows to
specify the key of the data and sets metadata which are required to
find the data.

It is used to cache information as done during 'npm install'.

Keyformat and metadata are nodejs version specific.

Signed-off-by: Enrico Scholz <enrico.scholz@sigma-chemnitz.de>
Signed-off-by: Khem Raj <raj.khem@gmail.com>
2022-05-20 10:59:47 -07:00

78 lines
1.7 KiB
JavaScript
Executable File

#!/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}).`);
})