oe-npm-cache 1.7 KB

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