patch-node-modules.js 2.5 KB

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