plugin.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. import fs, { readFileSync } from 'fs';
  2. import path from 'path';
  3. import { GrowiThemeMetadata, ViteManifest } from '@growi/core';
  4. // eslint-disable-next-line no-restricted-imports
  5. import axios from 'axios';
  6. import mongoose from 'mongoose';
  7. import streamToPromise from 'stream-to-promise';
  8. import unzipper from 'unzipper';
  9. import {
  10. GrowiPlugin, GrowiPluginOrigin, GrowiPluginResourceType, GrowiThemePluginMeta, GrowiPluginMeta,
  11. } from '~/interfaces/plugin';
  12. import loggerFactory from '~/utils/logger';
  13. import { resolveFromRoot } from '~/utils/project-dir-utils';
  14. import type { GrowiPluginModel } from '../models/growi-plugin';
  15. const logger = loggerFactory('growi:plugins:plugin-utils');
  16. const pluginStoringPath = resolveFromRoot('tmp/plugins');
  17. // https://regex101.com/r/fK2rV3/1
  18. const githubReposIdPattern = new RegExp(/^\/([^/]+)\/([^/]+)$/);
  19. const PLUGINS_STATIC_DIR = '/static/plugins'; // configured by express.static
  20. export type GrowiPluginResourceEntries = [installedPath: string, href: string][];
  21. function retrievePluginManifest(growiPlugin: GrowiPlugin): ViteManifest {
  22. const manifestPath = resolveFromRoot(path.join('tmp/plugins', growiPlugin.installedPath, 'dist/manifest.json'));
  23. const manifestStr: string = readFileSync(manifestPath, 'utf-8');
  24. return JSON.parse(manifestStr);
  25. }
  26. export interface IPluginService {
  27. install(origin: GrowiPluginOrigin): Promise<string>
  28. retrieveThemeHref(theme: string): Promise<string | undefined>
  29. retrieveAllPluginResourceEntries(): Promise<GrowiPluginResourceEntries>
  30. downloadNotExistPluginRepositories(): Promise<void>
  31. }
  32. export class PluginService implements IPluginService {
  33. /*
  34. * Downloading a non-existent repository to the file system
  35. */
  36. async downloadNotExistPluginRepositories(): Promise<void> {
  37. try {
  38. // find all growi plugin documents
  39. const GrowiPlugin = mongoose.model<GrowiPlugin>('GrowiPlugin');
  40. const growiPlugins = await GrowiPlugin.find({});
  41. // if not exists repository in file system, download latest plugin repository
  42. for await (const growiPlugin of growiPlugins) {
  43. const pluginPath = path.join(pluginStoringPath, growiPlugin.installedPath);
  44. if (fs.existsSync(pluginPath)) {
  45. continue;
  46. }
  47. else {
  48. // TODO: imprv Document version and repository version possibly different.
  49. const ghUrl = new URL(growiPlugin.origin.url);
  50. const ghPathname = ghUrl.pathname;
  51. // TODO: Branch names can be specified.
  52. const ghBranch = 'main';
  53. const match = ghPathname.match(githubReposIdPattern);
  54. if (ghUrl.hostname !== 'github.com' || match == null) {
  55. throw new Error('GitHub repository URL is invalid.');
  56. }
  57. const ghOrganizationName = match[1];
  58. const ghReposName = match[2];
  59. const requestUrl = `https://github.com/${ghOrganizationName}/${ghReposName}/archive/refs/heads/${ghBranch}.zip`;
  60. const zipFilePath = path.join(pluginStoringPath, `${ghBranch}.zip`);
  61. const unzippedPath = pluginStoringPath;
  62. const unzippedReposPath = path.join(pluginStoringPath, `${ghReposName}-${ghBranch}`);
  63. try {
  64. // download github repository to local file system
  65. await this.download(requestUrl, zipFilePath);
  66. await this.unzip(zipFilePath, unzippedPath);
  67. fs.renameSync(unzippedReposPath, pluginPath);
  68. }
  69. catch (err) {
  70. // clean up, documents are not operated
  71. if (fs.existsSync(unzippedReposPath)) await fs.promises.rm(unzippedReposPath, { recursive: true });
  72. if (fs.existsSync(pluginPath)) await fs.promises.rm(pluginPath, { recursive: true });
  73. logger.error(err);
  74. }
  75. continue;
  76. }
  77. }
  78. }
  79. catch (err) {
  80. logger.error(err);
  81. }
  82. }
  83. /*
  84. * Install a plugin from URL and save it in the DB and file system.
  85. */
  86. async install(origin: GrowiPluginOrigin): Promise<string> {
  87. const ghUrl = new URL(origin.url);
  88. const ghPathname = ghUrl.pathname;
  89. // TODO: Branch names can be specified.
  90. const ghBranch = 'main';
  91. const match = ghPathname.match(githubReposIdPattern);
  92. if (ghUrl.hostname !== 'github.com' || match == null) {
  93. throw new Error('GitHub repository URL is invalid.');
  94. }
  95. const ghOrganizationName = match[1];
  96. const ghReposName = match[2];
  97. const installedPath = `${ghOrganizationName}/${ghReposName}`;
  98. const requestUrl = `https://github.com/${ghOrganizationName}/${ghReposName}/archive/refs/heads/${ghBranch}.zip`;
  99. const zipFilePath = path.join(pluginStoringPath, `${ghBranch}.zip`);
  100. const unzippedPath = pluginStoringPath;
  101. const unzippedReposPath = path.join(pluginStoringPath, `${ghReposName}-${ghBranch}`);
  102. const temporaryReposPath = path.join(pluginStoringPath, ghReposName);
  103. const reposStoringPath = path.join(pluginStoringPath, `${installedPath}`);
  104. let plugins: GrowiPlugin<GrowiPluginMeta>[];
  105. try {
  106. // download github repository to file system's temporary path
  107. await this.download(requestUrl, zipFilePath);
  108. await this.unzip(zipFilePath, unzippedPath);
  109. fs.renameSync(unzippedReposPath, temporaryReposPath);
  110. // detect plugins
  111. plugins = await PluginService.detectPlugins(origin, ghOrganizationName, ghReposName);
  112. // remove the old repository from the storing path
  113. if (fs.existsSync(reposStoringPath)) await fs.promises.rm(reposStoringPath, { recursive: true });
  114. // move new repository from temporary path to storing path.
  115. fs.renameSync(temporaryReposPath, reposStoringPath);
  116. }
  117. catch (err) {
  118. // clean up
  119. if (fs.existsSync(zipFilePath)) await fs.promises.rm(zipFilePath);
  120. if (fs.existsSync(unzippedReposPath)) await fs.promises.rm(unzippedReposPath, { recursive: true });
  121. if (fs.existsSync(temporaryReposPath)) await fs.promises.rm(temporaryReposPath, { recursive: true });
  122. logger.error(err);
  123. throw err;
  124. }
  125. try {
  126. // delete plugin documents if these exist
  127. await this.deleteOldPluginDocument(installedPath);
  128. // save new plugins metadata
  129. await this.savePluginMetaData(plugins);
  130. return plugins[0].meta.name;
  131. }
  132. catch (err) {
  133. // clean up
  134. if (fs.existsSync(reposStoringPath)) await fs.promises.rm(reposStoringPath, { recursive: true });
  135. await this.deleteOldPluginDocument(installedPath);
  136. logger.error(err);
  137. throw err;
  138. }
  139. }
  140. private async deleteOldPluginDocument(path: string): Promise<void> {
  141. const GrowiPlugin = mongoose.model<GrowiPlugin>('GrowiPlugin');
  142. await GrowiPlugin.deleteMany({ installedPath: path });
  143. }
  144. // !! DO NOT USE WHERE NOT SSRF GUARDED !! -- 2022.12.26 ryoji-s
  145. private async download(requestUrl: string, filePath: string): Promise<void> {
  146. return new Promise<void>((resolve, rejects) => {
  147. axios({
  148. method: 'GET',
  149. url: requestUrl,
  150. responseType: 'stream',
  151. })
  152. .then((res) => {
  153. if (res.status === 200) {
  154. const file = fs.createWriteStream(filePath);
  155. res.data.pipe(file)
  156. .on('close', () => file.close())
  157. .on('finish', () => {
  158. return resolve();
  159. });
  160. }
  161. else {
  162. rejects(res.status);
  163. }
  164. }).catch((err) => {
  165. logger.error(err);
  166. // eslint-disable-next-line prefer-promise-reject-errors
  167. rejects('Filed to download file.');
  168. });
  169. });
  170. }
  171. private async unzip(zipFilePath: fs.PathLike, unzippedPath: fs.PathLike): Promise<void> {
  172. try {
  173. const stream = fs.createReadStream(zipFilePath);
  174. const unzipStream = stream.pipe(unzipper.Extract({ path: unzippedPath }));
  175. await streamToPromise(unzipStream);
  176. await fs.promises.rm(zipFilePath);
  177. }
  178. catch (err) {
  179. logger.error(err);
  180. throw new Error('Filed to unzip.');
  181. }
  182. }
  183. private async savePluginMetaData(plugins: GrowiPlugin[]): Promise<void> {
  184. const GrowiPlugin = mongoose.model('GrowiPlugin');
  185. await GrowiPlugin.insertMany(plugins);
  186. }
  187. // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types, max-len
  188. private static async detectPlugins(origin: GrowiPluginOrigin, ghOrganizationName: string, ghReposName: string, parentPackageJson?: any): Promise<GrowiPlugin[]> {
  189. const packageJsonPath = path.resolve(pluginStoringPath, ghReposName, 'package.json');
  190. const packageJson = JSON.parse(readFileSync(packageJsonPath, 'utf-8'));
  191. const { growiPlugin } = packageJson;
  192. const {
  193. name: packageName, description: packageDesc, author: packageAuthor,
  194. } = parentPackageJson ?? packageJson;
  195. if (growiPlugin == null) {
  196. throw new Error('This package does not include \'growiPlugin\' section.');
  197. }
  198. // detect sub plugins for monorepo
  199. if (growiPlugin.isMonorepo && growiPlugin.packages != null) {
  200. const plugins = await Promise.all(
  201. growiPlugin.packages.map(async(subPackagePath) => {
  202. const subPackageInstalledPath = path.join(ghReposName, subPackagePath);
  203. return this.detectPlugins(origin, subPackageInstalledPath, packageJson);
  204. }),
  205. );
  206. return plugins.flat();
  207. }
  208. if (growiPlugin.types == null) {
  209. throw new Error('\'growiPlugin\' section must have a \'types\' property.');
  210. }
  211. const plugin = {
  212. isEnabled: true,
  213. installedPath: `${ghOrganizationName}/${ghReposName}`,
  214. origin,
  215. meta: {
  216. name: growiPlugin.name ?? packageName,
  217. desc: growiPlugin.desc ?? packageDesc,
  218. author: growiPlugin.author ?? packageAuthor,
  219. types: growiPlugin.types,
  220. },
  221. };
  222. // add theme metadata
  223. if (growiPlugin.types.includes(GrowiPluginResourceType.Theme)) {
  224. (plugin as GrowiPlugin<GrowiThemePluginMeta>).meta = {
  225. ...plugin.meta,
  226. themes: growiPlugin.themes,
  227. };
  228. }
  229. logger.info('Plugin detected => ', plugin);
  230. return [plugin];
  231. }
  232. async listPlugins(): Promise<GrowiPlugin[]> {
  233. return [];
  234. }
  235. /**
  236. * Delete plugin
  237. */
  238. async deletePlugin(pluginId: mongoose.Types.ObjectId): Promise<string> {
  239. const deleteFolder = (path: fs.PathLike): Promise<void> => {
  240. return fs.promises.rm(path, { recursive: true });
  241. };
  242. const GrowiPlugin = mongoose.model<GrowiPlugin>('GrowiPlugin');
  243. const growiPlugins = await GrowiPlugin.findById(pluginId);
  244. if (growiPlugins == null) {
  245. throw new Error('No plugin found for this ID.');
  246. }
  247. try {
  248. const growiPluginsPath = path.join(pluginStoringPath, growiPlugins.installedPath);
  249. await deleteFolder(growiPluginsPath);
  250. }
  251. catch (err) {
  252. logger.error(err);
  253. throw new Error('Filed to delete plugin repository.');
  254. }
  255. try {
  256. await GrowiPlugin.deleteOne({ _id: pluginId });
  257. }
  258. catch (err) {
  259. logger.error(err);
  260. throw new Error('Filed to delete plugin from GrowiPlugin documents.');
  261. }
  262. return growiPlugins.meta.name;
  263. }
  264. async retrieveThemeHref(theme: string): Promise<string | undefined> {
  265. const GrowiPlugin = mongoose.model('GrowiPlugin') as GrowiPluginModel;
  266. let matchedPlugin: GrowiPlugin | undefined;
  267. let matchedThemeMetadata: GrowiThemeMetadata | undefined;
  268. try {
  269. // retrieve plugin manifests
  270. const growiPlugins = await GrowiPlugin.findEnabledPluginsIncludingAnyTypes([GrowiPluginResourceType.Theme]) as GrowiPlugin<GrowiThemePluginMeta>[];
  271. growiPlugins
  272. .forEach(async(growiPlugin) => {
  273. const themeMetadatas = growiPlugin.meta.themes;
  274. const themeMetadata = themeMetadatas.find(t => t.name === theme);
  275. // found
  276. if (themeMetadata != null) {
  277. matchedPlugin = growiPlugin;
  278. matchedThemeMetadata = themeMetadata;
  279. }
  280. });
  281. }
  282. catch (e) {
  283. logger.error(`Could not find the theme '${theme}' from GrowiPlugin documents.`, e);
  284. }
  285. try {
  286. if (matchedPlugin != null && matchedThemeMetadata != null) {
  287. const manifest = await retrievePluginManifest(matchedPlugin);
  288. return `${PLUGINS_STATIC_DIR}/${matchedPlugin.installedPath}/dist/${manifest[matchedThemeMetadata.manifestKey].file}`;
  289. }
  290. }
  291. catch (e) {
  292. logger.error(`Could not read manifest file for the theme '${theme}'`, e);
  293. }
  294. }
  295. async retrieveAllPluginResourceEntries(): Promise<GrowiPluginResourceEntries> {
  296. const GrowiPlugin = mongoose.model('GrowiPlugin') as GrowiPluginModel;
  297. const entries: GrowiPluginResourceEntries = [];
  298. try {
  299. const growiPlugins = await GrowiPlugin.findEnabledPlugins();
  300. growiPlugins.forEach(async(growiPlugin) => {
  301. try {
  302. const { types } = growiPlugin.meta;
  303. const manifest = await retrievePluginManifest(growiPlugin);
  304. // add script
  305. if (types.includes(GrowiPluginResourceType.Script) || types.includes(GrowiPluginResourceType.Template)) {
  306. const href = `${PLUGINS_STATIC_DIR}/${growiPlugin.installedPath}/dist/${manifest['client-entry.tsx'].file}`;
  307. entries.push([growiPlugin.installedPath, href]);
  308. }
  309. // add link
  310. if (types.includes(GrowiPluginResourceType.Script) || types.includes(GrowiPluginResourceType.Style)) {
  311. const href = `${PLUGINS_STATIC_DIR}/${growiPlugin.installedPath}/dist/${manifest['client-entry.tsx'].css}`;
  312. entries.push([growiPlugin.installedPath, href]);
  313. }
  314. }
  315. catch (e) {
  316. logger.warn(e);
  317. }
  318. });
  319. }
  320. catch (e) {
  321. logger.error('Could not retrieve GrowiPlugin documents.', e);
  322. }
  323. return entries;
  324. }
  325. }