worker.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. importScripts(
  2. // Batched optimization
  3. "/static/js/lightning-fs.min.js?v=0.0.2.3",
  4. "https://cdn.jsdelivr.net/npm/[email protected]/index.umd.min.js",
  5. "https://cdn.jsdelivr.net/npm/[email protected]/http/web/index.umd.js",
  6. // Fixed a bug
  7. "/static/js/magic_portal.js"
  8. );
  9. const detect = () => {
  10. if (typeof window !== 'undefined' && !self.skipWaiting) {
  11. return 'window'
  12. } else if (typeof self !== 'undefined' && !self.skipWaiting) {
  13. return 'Worker'
  14. } else if (typeof self !== 'undefined' && self.skipWaiting) {
  15. return 'ServiceWorker'
  16. }
  17. };
  18. function basicAuth (username, token) {
  19. return "Basic " + btoa("tiensonqin:" + token);
  20. }
  21. const fsName = 'logseq';
  22. const createFS = () => new LightningFS(fsName);
  23. let fs = createFS();
  24. let pfs = fs.promises;
  25. if (detect() === 'Worker') {
  26. const portal = new MagicPortal(self);
  27. portal.set('git', git);
  28. portal.set('fs', fs);
  29. portal.set('pfs', pfs);
  30. portal.set('gitHttp', GitHttp);
  31. portal.set('workerThread', {
  32. setConfig: function (dir, path, value) {
  33. return git.setConfig ({
  34. fs,
  35. dir,
  36. path,
  37. value
  38. });
  39. },
  40. clone: function (dir, url, corsProxy, depth, branch, username, token) {
  41. return git.clone ({
  42. fs,
  43. dir,
  44. http: GitHttp,
  45. url,
  46. corsProxy,
  47. ref: branch,
  48. singleBranch: true,
  49. depth,
  50. headers: {
  51. "Authorization": basicAuth(username, token)
  52. }
  53. });
  54. },
  55. fetch: function (dir, url, corsProxy, depth, branch, username, token) {
  56. return git.fetch ({
  57. fs,
  58. dir,
  59. http: GitHttp,
  60. url,
  61. corsProxy,
  62. ref: branch,
  63. singleBranch: true,
  64. depth,
  65. headers: {
  66. "Authorization": basicAuth(username, token)
  67. }
  68. });
  69. },
  70. pull: function (dir, corsProxy, branch, username, token) {
  71. return git.pull ({
  72. fs,
  73. dir,
  74. http: GitHttp,
  75. corsProxy,
  76. ref: branch,
  77. singleBranch: true,
  78. // fast: true,
  79. headers: {
  80. "Authorization": basicAuth(username, token)
  81. }
  82. });
  83. },
  84. push: function (dir, corsProxy, branch, force, username, token) {
  85. return git.push ({
  86. fs,
  87. dir,
  88. http: GitHttp,
  89. ref: branch,
  90. corsProxy,
  91. remote: "origin",
  92. force,
  93. headers: {
  94. "Authorization": basicAuth(username, token)
  95. }
  96. });
  97. },
  98. merge: function (dir, branch) {
  99. return git.merge ({
  100. fs,
  101. dir,
  102. ours: branch,
  103. theirs: "remotes/origin/" + branch,
  104. // fastForwardOnly: true
  105. });
  106. },
  107. checkout: function (dir, branch) {
  108. return git.checkout ({
  109. fs,
  110. dir,
  111. ref: branch,
  112. });
  113. },
  114. log: function (dir, branch, depth) {
  115. return git.log ({
  116. fs,
  117. dir,
  118. ref: branch,
  119. depth,
  120. singleBranch: true
  121. })
  122. },
  123. add: function (dir, file) {
  124. return git.add ({
  125. fs,
  126. dir,
  127. filepath: file
  128. });
  129. },
  130. remove: function (dir, file) {
  131. return git.remove ({
  132. fs,
  133. dir,
  134. filepath: file
  135. });
  136. },
  137. commit: function (dir, message, name, email, parent) {
  138. if (parent) {
  139. return git.commit ({
  140. fs,
  141. dir,
  142. message,
  143. author: {name: name,
  144. email: email},
  145. parent: parent
  146. });
  147. } else {
  148. return git.commit ({
  149. fs,
  150. dir,
  151. message,
  152. author: {name: name,
  153. email: email}
  154. });
  155. }
  156. },
  157. readCommit: function (dir, oid) {
  158. return git.readCommit ({
  159. fs,
  160. dir,
  161. oid
  162. });
  163. },
  164. readBlob: function (dir, oid, path) {
  165. return git.readBlob ({
  166. fs,
  167. dir,
  168. oid,
  169. path
  170. });
  171. },
  172. writeRef: function (dir, branch, oid) {
  173. return git.writeRef ({
  174. fs,
  175. dir,
  176. ref: "refs/heads/" + branch,
  177. value: oid,
  178. force: true
  179. });
  180. },
  181. resolveRef: function (dir, ref) {
  182. return git.resolveRef ({
  183. fs,
  184. dir,
  185. ref
  186. });
  187. },
  188. listFiles: function (dir, branch) {
  189. return git.listFiles ({
  190. fs,
  191. dir,
  192. ref: branch
  193. });
  194. },
  195. rimraf: async function (path) {
  196. // try {
  197. // // First assume path is itself a file
  198. // await pfs.unlink(path)
  199. // // if that worked we're done
  200. // return
  201. // } catch (err) {
  202. // // Otherwise, path must be a directory
  203. // if (err.code !== 'EISDIR') throw err
  204. // }
  205. // Knowing path is a directory,
  206. // first, assume everything inside path is a file.
  207. let files = await pfs.readdir(path);
  208. for (let file of files) {
  209. let child = path + '/' + file
  210. try {
  211. await pfs.unlink(child)
  212. } catch (err) {
  213. if (err.code !== 'EISDIR') throw err
  214. }
  215. }
  216. // Assume what's left are directories and recurse.
  217. let dirs = await pfs.readdir(path)
  218. for (let dir of dirs) {
  219. let child = path + '/' + dir
  220. await rimraf(child, pfs)
  221. }
  222. // Finally, delete the empty directory
  223. await pfs.rmdir(path)
  224. },
  225. getFileStateChanges: async function (commitHash1, commitHash2, dir) {
  226. return git.walk({
  227. fs,
  228. dir,
  229. trees: [git.TREE({ ref: commitHash1 }), git.TREE({ ref: commitHash2 })],
  230. map: async function(filepath, [A, B]) {
  231. var type = 'equal';
  232. if (A === null) {
  233. type = "add";
  234. }
  235. if (B === null) {
  236. type = "remove";
  237. }
  238. // ignore directories
  239. if (filepath === '.') {
  240. return
  241. }
  242. if ((A !== null && (await A.type()) === 'tree')
  243. ||
  244. (B !== null && (await B.type()) === 'tree')) {
  245. return
  246. }
  247. // generate ids
  248. const Aoid = A !== null && await A.oid();
  249. const Boid = B !== null && await B.oid();
  250. if (type === "equal") {
  251. // determine modification type
  252. if (Aoid !== Boid) {
  253. type = 'modify'
  254. }
  255. if (Aoid === undefined) {
  256. type = 'add'
  257. }
  258. if (Boid === undefined) {
  259. type = 'remove'
  260. }
  261. }
  262. if (Aoid === undefined && Boid === undefined) {
  263. console.log('Something weird happened:')
  264. console.log(A)
  265. console.log(B)
  266. }
  267. return {
  268. path: `/${filepath}`,
  269. type: type,
  270. }
  271. },
  272. })
  273. },
  274. statusMatrix: async function (dir) {
  275. await git.statusMatrix({ fs, dir });
  276. },
  277. getChangedFiles: async function (dir) {
  278. try {
  279. const FILE = 0, HEAD = 1, WORKDIR = 2;
  280. let filenames = (await git.statusMatrix({ fs, dir }))
  281. .filter(row => row[HEAD] !== row[WORKDIR])
  282. .map(row => row[FILE]);
  283. return filenames;
  284. } catch (err) {
  285. console.error(err);
  286. return [];
  287. }
  288. }
  289. });
  290. // self.addEventListener("message", ({ data }) => console.log(data));
  291. }