patch-node-modules.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. const fs = require('fs');
  2. const path = require('path');
  3. const log = (msg) => console.log(`[OASIS] [PATCH] ${msg}`);
  4. // === Patch ssb-ref ===
  5. const ssbRefPath = path.resolve(__dirname, '../src/server/node_modules/ssb-ref/index.js');
  6. if (fs.existsSync(ssbRefPath)) {
  7. const data = fs.readFileSync(ssbRefPath, 'utf8');
  8. const patched = data.replace(
  9. /exports\.parseAddress\s*=\s*deprecate\([^)]*\)/,
  10. 'exports.parseAddress = parseAddress'
  11. );
  12. if (patched !== data) {
  13. fs.writeFileSync(ssbRefPath, patched);
  14. log('Patched ssb-ref to remove deprecated usage of parseAddress');
  15. } else {
  16. log('ssb-ref patch skipped: target line not found');
  17. }
  18. }
  19. // === Patch ssb-blobs ===
  20. const ssbBlobsPath = path.resolve(__dirname, '../src/server/node_modules/ssb-blobs/inject.js');
  21. if (fs.existsSync(ssbBlobsPath)) {
  22. let data = fs.readFileSync(ssbBlobsPath, 'utf8');
  23. const marker = 'want: function (id, cb)';
  24. const startIndex = data.indexOf(marker);
  25. if (startIndex !== -1) {
  26. const endIndex = data.indexOf('},', startIndex); // end of function block
  27. if (endIndex !== -1) {
  28. const before = data.slice(0, startIndex);
  29. const after = data.slice(endIndex + 2);
  30. const replacement = `
  31. want: function (id, cb) {
  32. id = toBlobId(id);
  33. if (!isBlobId(id)) return cb(new Error('invalid id:' + id));
  34. if (blobStore.isEmptyHash(id)) return cb(null, true);
  35. if (wantCallbacks[id]) {
  36. if (!Array.isArray(wantCallbacks[id])) wantCallbacks[id] = [];
  37. wantCallbacks[id].push(cb);
  38. } else {
  39. wantCallbacks[id] = [cb];
  40. blobStore.size(id, function (err, size) {
  41. if (err) return cb(err);
  42. if (size != null) {
  43. while (wantCallbacks[id].length) {
  44. const fn = wantCallbacks[id].shift();
  45. if (typeof fn === 'function') fn(null, true);
  46. }
  47. delete wantCallbacks[id];
  48. }
  49. });
  50. }
  51. const peerId = findPeerWithBlob(id);
  52. if (peerId) get(peerId, id);
  53. if (wantCallbacks[id]) registerWant(id);
  54. },`;
  55. const finalData = before + replacement + after;
  56. fs.writeFileSync(ssbBlobsPath, finalData);
  57. log('Patched ssb-blobs to fix wantCallbacks handling');
  58. } else {
  59. log('ssb-blobs patch skipped: end of want function not found');
  60. }
  61. } else {
  62. log('ssb-blobs patch skipped: want function not found');
  63. }
  64. }