@epic-web/workshop-utils 0.0.0-semantically-released
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -0
- package/dist/esm/apps.server.d.ts +4205 -0
- package/dist/esm/apps.server.d.ts.map +1 -0
- package/dist/esm/apps.server.js +1198 -0
- package/dist/esm/apps.server.js.map +1 -0
- package/dist/esm/cache.server.d.ts +940 -0
- package/dist/esm/cache.server.d.ts.map +1 -0
- package/dist/esm/cache.server.js +161 -0
- package/dist/esm/cache.server.js.map +1 -0
- package/dist/esm/compile-mdx.server.d.ts +12 -0
- package/dist/esm/compile-mdx.server.d.ts.map +1 -0
- package/dist/esm/compile-mdx.server.js +285 -0
- package/dist/esm/compile-mdx.server.js.map +1 -0
- package/dist/esm/config.server.d.ts +348 -0
- package/dist/esm/config.server.d.ts.map +1 -0
- package/dist/esm/config.server.js +231 -0
- package/dist/esm/config.server.js.map +1 -0
- package/dist/esm/db.server.d.ts +463 -0
- package/dist/esm/db.server.d.ts.map +1 -0
- package/dist/esm/db.server.js +260 -0
- package/dist/esm/db.server.js.map +1 -0
- package/dist/esm/diff.server.d.ts +18 -0
- package/dist/esm/diff.server.d.ts.map +1 -0
- package/dist/esm/diff.server.js +437 -0
- package/dist/esm/diff.server.js.map +1 -0
- package/dist/esm/env.server.d.ts +61 -0
- package/dist/esm/env.server.d.ts.map +1 -0
- package/dist/esm/env.server.js +42 -0
- package/dist/esm/env.server.js.map +1 -0
- package/dist/esm/epic-api.server.d.ts +227 -0
- package/dist/esm/epic-api.server.d.ts.map +1 -0
- package/dist/esm/epic-api.server.js +529 -0
- package/dist/esm/epic-api.server.js.map +1 -0
- package/dist/esm/git.server.d.ts +49 -0
- package/dist/esm/git.server.d.ts.map +1 -0
- package/dist/esm/git.server.js +135 -0
- package/dist/esm/git.server.js.map +1 -0
- package/dist/esm/iframe-sync.d.ts +10 -0
- package/dist/esm/iframe-sync.d.ts.map +1 -0
- package/dist/esm/iframe-sync.js +97 -0
- package/dist/esm/iframe-sync.js.map +1 -0
- package/dist/esm/modified-time.server.d.ts +7 -0
- package/dist/esm/modified-time.server.d.ts.map +1 -0
- package/dist/esm/modified-time.server.js +80 -0
- package/dist/esm/modified-time.server.js.map +1 -0
- package/dist/esm/notifications.server.d.ts +56 -0
- package/dist/esm/notifications.server.d.ts.map +1 -0
- package/dist/esm/notifications.server.js +65 -0
- package/dist/esm/notifications.server.js.map +1 -0
- package/dist/esm/package.json +3 -0
- package/dist/esm/playwright.server.d.ts +6 -0
- package/dist/esm/playwright.server.d.ts.map +1 -0
- package/dist/esm/playwright.server.js +95 -0
- package/dist/esm/playwright.server.js.map +1 -0
- package/dist/esm/process-manager.server.d.ts +77 -0
- package/dist/esm/process-manager.server.d.ts.map +1 -0
- package/dist/esm/process-manager.server.js +266 -0
- package/dist/esm/process-manager.server.js.map +1 -0
- package/dist/esm/test.d.ts +16 -0
- package/dist/esm/test.d.ts.map +1 -0
- package/dist/esm/test.js +56 -0
- package/dist/esm/test.js.map +1 -0
- package/dist/esm/timing.server.d.ts +20 -0
- package/dist/esm/timing.server.d.ts.map +1 -0
- package/dist/esm/timing.server.js +88 -0
- package/dist/esm/timing.server.js.map +1 -0
- package/dist/esm/user.server.d.ts +17 -0
- package/dist/esm/user.server.d.ts.map +1 -0
- package/dist/esm/user.server.js +38 -0
- package/dist/esm/user.server.js.map +1 -0
- package/dist/esm/utils.d.ts +2 -0
- package/dist/esm/utils.d.ts.map +1 -0
- package/dist/esm/utils.js +13 -0
- package/dist/esm/utils.js.map +1 -0
- package/dist/esm/utils.server.d.ts +9 -0
- package/dist/esm/utils.server.d.ts.map +1 -0
- package/dist/esm/utils.server.js +45 -0
- package/dist/esm/utils.server.js.map +1 -0
- package/package.json +221 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cache.server.d.ts","sourceRoot":"","sources":["../../src/cache.server.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,CAAC,MAA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CAAA;IACjB,UAAU,CAAC,EAAE,OAAO,GAAG,MAAM,CAAA;IAC7B,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB,oBAAoB,CAAC,EAAE,KAAK,CAAA;CAC5B,GAAG,OAAO,CAAC,KAAK,CAAC,CAwBjB;AAED,wBAAsB,gBAAgB,CAAC,EACtC,UAAU,EACV,OAAO,EACP,GAAG,GACH,EAAE;IACF,UAAU,CAAC,EAAE,OAAO,GAAG,MAAM,CAAA;IAC7B,OAAO,CAAC,EAAE,OAAO,CAAA;IACjB,GAAG,CAAC,EAAE,MAAM,CAAA;CACZ,oBAaA"}
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
import os from 'os';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
import * as C from '@epic-web/cachified';
|
|
4
|
+
import { verboseReporter } from '@epic-web/cachified';
|
|
5
|
+
import { remember } from '@epic-web/remember';
|
|
6
|
+
import fsExtra from 'fs-extra';
|
|
7
|
+
import { LRUCache } from 'lru-cache';
|
|
8
|
+
import md5 from 'md5-hex';
|
|
9
|
+
import { cachifiedTimingReporter } from './timing.server.js';
|
|
10
|
+
import { checkConnectionCached } from './utils.server.js';
|
|
11
|
+
export const solutionAppCache = makeSingletonCache('SolutionAppCache');
|
|
12
|
+
export const problemAppCache = makeSingletonCache('ProblemAppCache');
|
|
13
|
+
export const exampleAppCache = makeSingletonCache('ExampleAppCache');
|
|
14
|
+
export const playgroundAppCache = makeSingletonCache('PlaygroundAppCache');
|
|
15
|
+
export const appsCache = makeSingletonCache('AppsCache');
|
|
16
|
+
export const diffCodeCache = makeSingletonCache('DiffCodeCache');
|
|
17
|
+
export const diffFilesCache = makeSingletonCache('DiffFilesCache');
|
|
18
|
+
export const compiledMarkdownCache = makeSingletonCache('CompiledMarkdownCache');
|
|
19
|
+
export const compiledCodeCache = makeSingletonCache('CompiledCodeCache');
|
|
20
|
+
export const ogCache = makeSingletonCache('OgCache');
|
|
21
|
+
export const compiledInstructionMarkdownCache = makeSingletonFsCache('CompiledInstructionMarkdownCache');
|
|
22
|
+
export const dirModifiedTimeCache = makeSingletonCache('DirModifiedTimeCache');
|
|
23
|
+
export const connectionCache = makeSingletonCache('ConnectionCache');
|
|
24
|
+
export const checkForUpdatesCache = makeSingletonCache('CheckForUpdatesCache');
|
|
25
|
+
export const notificationsCache = makeSingletonCache('NotificationsCache');
|
|
26
|
+
const cacheDir = path.join(os.homedir(), '.epicshop', 'cache');
|
|
27
|
+
export const fsCache = makeSingletonFsCache('FsCache');
|
|
28
|
+
async function readJsonFilesInDirectory(dir) {
|
|
29
|
+
const files = await fsExtra.readdir(dir);
|
|
30
|
+
const entries = await Promise.all(files.map(async (file) => {
|
|
31
|
+
const filePath = path.join(dir, file);
|
|
32
|
+
const stats = await fsExtra.stat(filePath);
|
|
33
|
+
if (stats.isDirectory()) {
|
|
34
|
+
const subEntries = await readJsonFilesInDirectory(filePath);
|
|
35
|
+
return [file, subEntries];
|
|
36
|
+
}
|
|
37
|
+
else {
|
|
38
|
+
const data = await fsExtra.readJSON(filePath);
|
|
39
|
+
return [file, data];
|
|
40
|
+
}
|
|
41
|
+
}));
|
|
42
|
+
return Object.fromEntries(entries);
|
|
43
|
+
}
|
|
44
|
+
export async function getAllFileCacheEntries() {
|
|
45
|
+
return readJsonFilesInDirectory(cacheDir);
|
|
46
|
+
}
|
|
47
|
+
export async function deleteCache() {
|
|
48
|
+
if (process.env.EPICSHOP_DEPLOYED)
|
|
49
|
+
return null;
|
|
50
|
+
try {
|
|
51
|
+
if (await fsExtra.exists(cacheDir)) {
|
|
52
|
+
await fsExtra.remove(cacheDir);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
catch (error) {
|
|
56
|
+
console.error(`Error deleting the cache in ${cacheDir}`, error);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
export function makeSingletonCache(name) {
|
|
60
|
+
return remember(name, () => {
|
|
61
|
+
const lruInstance = new LRUCache({
|
|
62
|
+
max: 1000,
|
|
63
|
+
});
|
|
64
|
+
const lru = {
|
|
65
|
+
name,
|
|
66
|
+
set: (key, value) => {
|
|
67
|
+
const ttl = C.totalTtl(value.metadata);
|
|
68
|
+
lruInstance.set(key, value, {
|
|
69
|
+
ttl: ttl === Infinity ? undefined : ttl,
|
|
70
|
+
start: value.metadata.createdTime,
|
|
71
|
+
});
|
|
72
|
+
return value;
|
|
73
|
+
},
|
|
74
|
+
get: (key) => lruInstance.get(key),
|
|
75
|
+
delete: (key) => lruInstance.delete(key),
|
|
76
|
+
};
|
|
77
|
+
return lru;
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
export function makeSingletonFsCache(name) {
|
|
81
|
+
return remember(name, () => {
|
|
82
|
+
const cacheDir = path.join(os.homedir(), '.epicshop', 'cache', name);
|
|
83
|
+
const fsCache = {
|
|
84
|
+
name: `Filesystem cache (${name})`,
|
|
85
|
+
async get(key) {
|
|
86
|
+
try {
|
|
87
|
+
const filePath = path.join(cacheDir, md5(key));
|
|
88
|
+
const data = await fsExtra.readJSON(filePath);
|
|
89
|
+
if (data.entry)
|
|
90
|
+
return data.entry;
|
|
91
|
+
return null;
|
|
92
|
+
}
|
|
93
|
+
catch (error) {
|
|
94
|
+
if (error instanceof Error &&
|
|
95
|
+
'code' in error &&
|
|
96
|
+
error.code === 'ENOENT') {
|
|
97
|
+
return null;
|
|
98
|
+
}
|
|
99
|
+
throw error;
|
|
100
|
+
}
|
|
101
|
+
},
|
|
102
|
+
async set(key, entry) {
|
|
103
|
+
const filePath = path.join(cacheDir, md5(key));
|
|
104
|
+
await fsExtra.ensureDir(path.dirname(filePath));
|
|
105
|
+
await fsExtra.writeJSON(filePath, { key, entry });
|
|
106
|
+
},
|
|
107
|
+
async delete(key) {
|
|
108
|
+
const filePath = path.join(cacheDir, md5(key));
|
|
109
|
+
await fsExtra.remove(filePath);
|
|
110
|
+
},
|
|
111
|
+
};
|
|
112
|
+
return fsCache;
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* This wraps @epic-web/cachified to add a few handy features:
|
|
117
|
+
*
|
|
118
|
+
* 1. Automatic timing for timing headers
|
|
119
|
+
* 2. Automatic force refresh based on the request and enhancement of forceFresh
|
|
120
|
+
* to support comma-separated keys to force
|
|
121
|
+
* 3. Offline fallback support. If a fallback is given and we are detected to be
|
|
122
|
+
* offline, then the cached value is used regardless of whether it's expired and
|
|
123
|
+
* if one is not present then the given fallback will be used.
|
|
124
|
+
*/
|
|
125
|
+
export async function cachified({ request, timings, key, timingKey = key.length > 18 ? `${key.slice(0, 7)}...${key.slice(-8)}` : key, offlineFallbackValue, ...options }) {
|
|
126
|
+
if (offlineFallbackValue !== undefined) {
|
|
127
|
+
const isOnline = await checkConnectionCached({ request, timings });
|
|
128
|
+
if (!isOnline) {
|
|
129
|
+
const cacheEntry = await options.cache.get(key);
|
|
130
|
+
return cacheEntry?.value ?? offlineFallbackValue;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
const forceFresh = await shouldForceFresh({
|
|
134
|
+
forceFresh: options.forceFresh,
|
|
135
|
+
request,
|
|
136
|
+
key,
|
|
137
|
+
});
|
|
138
|
+
return C.cachified({
|
|
139
|
+
...options,
|
|
140
|
+
key,
|
|
141
|
+
forceFresh,
|
|
142
|
+
}, C.mergeReporters(cachifiedTimingReporter(timings, timingKey), process.env.EPICSHOP_DEBUG_CACHE ? verboseReporter() : undefined));
|
|
143
|
+
}
|
|
144
|
+
export async function shouldForceFresh({ forceFresh, request, key, }) {
|
|
145
|
+
if (typeof forceFresh === 'boolean')
|
|
146
|
+
return forceFresh;
|
|
147
|
+
if (typeof forceFresh === 'string' && key) {
|
|
148
|
+
return forceFresh.split(',').includes(key);
|
|
149
|
+
}
|
|
150
|
+
if (!request)
|
|
151
|
+
return false;
|
|
152
|
+
const fresh = new URL(request.url).searchParams.get('fresh');
|
|
153
|
+
if (typeof fresh !== 'string')
|
|
154
|
+
return false;
|
|
155
|
+
if (fresh === '')
|
|
156
|
+
return true;
|
|
157
|
+
if (!key)
|
|
158
|
+
return false;
|
|
159
|
+
return fresh.split(',').includes(key);
|
|
160
|
+
}
|
|
161
|
+
//# sourceMappingURL=cache.server.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cache.server.js","sourceRoot":"","sources":["../../src/cache.server.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,IAAI,CAAA;AACnB,OAAO,IAAI,MAAM,MAAM,CAAA;AACvB,OAAO,KAAK,CAAC,MAAM,qBAAqB,CAAA;AACxC,OAAO,EAAE,eAAe,EAAmB,MAAM,qBAAqB,CAAA;AACtE,OAAO,EAAE,QAAQ,EAAE,MAAM,oBAAoB,CAAA;AAC7C,OAAO,OAAO,MAAM,UAAU,CAAA;AAC9B,OAAO,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAA;AACpC,OAAO,GAAG,MAAM,SAAS,CAAA;AASzB,OAAO,EAAE,uBAAuB,EAAgB,MAAM,oBAAoB,CAAA;AAC1E,OAAO,EAAE,qBAAqB,EAAE,MAAM,mBAAmB,CAAA;AAEzD,MAAM,CAAC,MAAM,gBAAgB,GAC5B,kBAAkB,CAAc,kBAAkB,CAAC,CAAA;AACpD,MAAM,CAAC,MAAM,eAAe,GAAG,kBAAkB,CAAa,iBAAiB,CAAC,CAAA;AAChF,MAAM,CAAC,MAAM,eAAe,GAAG,kBAAkB,CAAa,iBAAiB,CAAC,CAAA;AAChF,MAAM,CAAC,MAAM,kBAAkB,GAC9B,kBAAkB,CAAgB,oBAAoB,CAAC,CAAA;AACxD,MAAM,CAAC,MAAM,SAAS,GAAG,kBAAkB,CAAM,WAAW,CAAC,CAAA;AAC7D,MAAM,CAAC,MAAM,aAAa,GAAG,kBAAkB,CAAS,eAAe,CAAC,CAAA;AACxE,MAAM,CAAC,MAAM,cAAc,GAAG,kBAAkB,CAAS,gBAAgB,CAAC,CAAA;AAC1E,MAAM,CAAC,MAAM,qBAAqB,GAAG,kBAAkB,CACtD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ACF,CAAC;IACD,MAAM,UAAU,GAAG,MAAM,gBAAgB,CAAC;QACzC,UAAU,EAAE,OAAO,CAAC,UAAU;QAC9B,OAAO;QACP,GAAG;KACH,CAAC,CAAA;IACF,OAAO,CAAC,CAAC,SAAS,CACjB;QACC,GAAG,OAAO;QACV,GAAG;QACH,UAAU;KACV,EACD,CAAC,CAAC,cAAc,CACf,uBAAuB,CAAC,OAAO,EAAE,SAAS,CAAC,EAC3C,OAAO,CAAC,GAAG,CAAC,oBAAoB,CAAC,CAAC,CAAC,eAAe,EAAE,CAAC,CAAC,CAAC,SAAS,CAChE,CACD,CAAA;AACF,CAAC;AAED,MAAM,CAAC,KAAK,UAAU,gBAAgB,CAAC,EACtC,UAAU,EACV,OAAO,EACP,GAAG,GAKH;IACA,IAAI,OAAO,UAAU,KAAK,SAAS;QAAE,OAAO,UAAU,CAAA;IACtD,IAAI,OAAO,UAAU,KAAK,QAAQ,IAAI,GAAG,EAAE,CAAC;QAC3C,OAAO,UAAU,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAA;IAC3C,CAAC;IAED,IAAI,CAAC,OAAO;QAAE,OAAO,KAAK,CAAA;IAC1B,MAAM,KAAK,GAAG,IAAI,GAAG,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,YAAY,CAAC,GAAG,CAAC,OAAO,CAAC,CAAA;IAC5D,IAAI,OAAO,KAAK,KAAK,QAAQ;QAAE,OAAO,KAAK,CAAA;IAC3C,IAAI,KAAK,KAAK,EAAE;QAAE,OAAO,IAAI,CAAA;IAC7B,IAAI,CAAC,GAAG;QAAE,OAAO,KAAK,CAAA;IAEtB,OAAO,KAAK,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAA;AACtC,CAAC","sourcesContent":["import os from 'os'\nimport path from 'path'\nimport * as C from '@epic-web/cachified'\nimport { verboseReporter, type CacheEntry } from '@epic-web/cachified'\nimport { remember } from '@epic-web/remember'\nimport fsExtra from 'fs-extra'\nimport { LRUCache } from 'lru-cache'\nimport md5 from 'md5-hex'\nimport {\n\ttype App,\n\ttype ExampleApp,\n\ttype PlaygroundApp,\n\ttype ProblemApp,\n\ttype SolutionApp,\n} from './apps.server.js'\nimport { type Notification } from './notifications.server.js'\nimport { cachifiedTimingReporter, type Timings } from './timing.server.js'\nimport { checkConnectionCached } from './utils.server.js'\n\nexport const solutionAppCache =\n\tmakeSingletonCache<SolutionApp>('SolutionAppCache')\nexport const problemAppCache = makeSingletonCache<ProblemApp>('ProblemAppCache')\nexport const exampleAppCache = makeSingletonCache<ExampleApp>('ExampleAppCache')\nexport const playgroundAppCache =\n\tmakeSingletonCache<PlaygroundApp>('PlaygroundAppCache')\nexport const appsCache = makeSingletonCache<App>('AppsCache')\nexport const diffCodeCache = makeSingletonCache<string>('DiffCodeCache')\nexport const diffFilesCache = makeSingletonCache<string>('DiffFilesCache')\nexport const compiledMarkdownCache = makeSingletonCache<string>(\n\t'CompiledMarkdownCache',\n)\nexport const compiledCodeCache = makeSingletonCache<string>('CompiledCodeCache')\nexport const ogCache = makeSingletonCache<string>('OgCache')\nexport const compiledInstructionMarkdownCache = makeSingletonFsCache<{\n\tcode: string\n\ttitle: string | null\n\tepicVideoEmbeds: Array<string>\n}>('CompiledInstructionMarkdownCache')\nexport const dirModifiedTimeCache = makeSingletonCache<number>(\n\t'DirModifiedTimeCache',\n)\nexport const connectionCache = makeSingletonCache<boolean>('ConnectionCache')\nexport const checkForUpdatesCache = makeSingletonCache<{\n\tupdatesAvailable: boolean\n\tlocalCommit: string\n\tremoteCommit: string\n\tdiffLink: string | null\n}>('CheckForUpdatesCache')\nexport const notificationsCache =\n\tmakeSingletonCache<Array<Notification>>('NotificationsCache')\n\nconst cacheDir = path.join(os.homedir(), '.epicshop', 'cache')\n\nexport const fsCache = makeSingletonFsCache('FsCache')\n\nasync function readJsonFilesInDirectory(\n\tdir: string,\n): Promise<Record<string, any>> {\n\tconst files = await fsExtra.readdir(dir)\n\tconst entries = await Promise.all(\n\t\tfiles.map(async (file) => {\n\t\t\tconst filePath = path.join(dir, file)\n\t\t\tconst stats = await fsExtra.stat(filePath)\n\t\t\tif (stats.isDirectory()) {\n\t\t\t\tconst subEntries = await readJsonFilesInDirectory(filePath)\n\t\t\t\treturn [file, subEntries]\n\t\t\t} else {\n\t\t\t\tconst data = await fsExtra.readJSON(filePath)\n\t\t\t\treturn [file, data]\n\t\t\t}\n\t\t}),\n\t)\n\treturn Object.fromEntries(entries)\n}\n\nexport async function getAllFileCacheEntries() {\n\treturn readJsonFilesInDirectory(cacheDir)\n}\n\nexport async function deleteCache() {\n\tif (process.env.EPICSHOP_DEPLOYED) return null\n\n\ttry {\n\t\tif (await fsExtra.exists(cacheDir)) {\n\t\t\tawait fsExtra.remove(cacheDir)\n\t\t}\n\t} catch (error) {\n\t\tconsole.error(`Error deleting the cache in ${cacheDir}`, error)\n\t}\n}\n\nexport function makeSingletonCache<CacheEntryType>(name: string) {\n\treturn remember(name, () => {\n\t\tconst lruInstance = new LRUCache<string, CacheEntry<CacheEntryType>>({\n\t\t\tmax: 1000,\n\t\t})\n\n\t\tconst lru = {\n\t\t\tname,\n\t\t\tset: (key, value) => {\n\t\t\t\tconst ttl = C.totalTtl(value.metadata)\n\t\t\t\tlruInstance.set(key, value, {\n\t\t\t\t\tttl: ttl === Infinity ? undefined : ttl,\n\t\t\t\t\tstart: value.metadata.createdTime,\n\t\t\t\t})\n\t\t\t\treturn value\n\t\t\t},\n\t\t\tget: (key) => lruInstance.get(key),\n\t\t\tdelete: (key) => lruInstance.delete(key),\n\t\t} satisfies C.Cache<CacheEntryType>\n\n\t\treturn lru\n\t})\n}\n\nexport function makeSingletonFsCache<CacheEntryType>(name: string) {\n\treturn remember(name, () => {\n\t\tconst cacheDir = path.join(os.homedir(), '.epicshop', 'cache', name)\n\n\t\tconst fsCache: C.Cache<CacheEntryType> = {\n\t\t\tname: `Filesystem cache (${name})`,\n\t\t\tasync get(key) {\n\t\t\t\ttry {\n\t\t\t\t\tconst filePath = path.join(cacheDir, md5(key))\n\t\t\t\t\tconst data = await fsExtra.readJSON(filePath)\n\t\t\t\t\tif (data.entry) return data.entry\n\t\t\t\t\treturn null\n\t\t\t\t} catch (error: unknown) {\n\t\t\t\t\tif (\n\t\t\t\t\t\terror instanceof Error &&\n\t\t\t\t\t\t'code' in error &&\n\t\t\t\t\t\terror.code === 'ENOENT'\n\t\t\t\t\t) {\n\t\t\t\t\t\treturn null\n\t\t\t\t\t}\n\t\t\t\t\tthrow error\n\t\t\t\t}\n\t\t\t},\n\t\t\tasync set(key, entry) {\n\t\t\t\tconst filePath = path.join(cacheDir, md5(key))\n\t\t\t\tawait fsExtra.ensureDir(path.dirname(filePath))\n\t\t\t\tawait fsExtra.writeJSON(filePath, { key, entry })\n\t\t\t},\n\t\t\tasync delete(key) {\n\t\t\t\tconst filePath = path.join(cacheDir, md5(key))\n\t\t\t\tawait fsExtra.remove(filePath)\n\t\t\t},\n\t\t}\n\n\t\treturn fsCache\n\t})\n}\n\n/**\n * This wraps @epic-web/cachified to add a few handy features:\n *\n * 1. Automatic timing for timing headers\n * 2. Automatic force refresh based on the request and enhancement of forceFresh\n * to support comma-separated keys to force\n * 3. Offline fallback support. If a fallback is given and we are detected to be\n * offline, then the cached value is used regardless of whether it's expired and\n * if one is not present then the given fallback will be used.\n */\nexport async function cachified<Value>({\n\trequest,\n\ttimings,\n\tkey,\n\ttimingKey = key.length > 18 ? `${key.slice(0, 7)}...${key.slice(-8)}` : key,\n\tofflineFallbackValue,\n\t...options\n}: Omit<C.CachifiedOptions<Value>, 'forceFresh'> & {\n\trequest?: Request\n\ttimings?: Timings\n\tforceFresh?: boolean | string\n\ttimingKey?: string\n\tofflineFallbackValue?: Value\n}): Promise<Value> {\n\tif (offlineFallbackValue !== undefined) {\n\t\tconst isOnline = await checkConnectionCached({ request, timings })\n\t\tif (!isOnline) {\n\t\t\tconst cacheEntry = await options.cache.get(key)\n\t\t\treturn cacheEntry?.value ?? offlineFallbackValue\n\t\t}\n\t}\n\tconst forceFresh = await shouldForceFresh({\n\t\tforceFresh: options.forceFresh,\n\t\trequest,\n\t\tkey,\n\t})\n\treturn C.cachified(\n\t\t{\n\t\t\t...options,\n\t\t\tkey,\n\t\t\tforceFresh,\n\t\t},\n\t\tC.mergeReporters(\n\t\t\tcachifiedTimingReporter(timings, timingKey),\n\t\t\tprocess.env.EPICSHOP_DEBUG_CACHE ? verboseReporter() : undefined,\n\t\t),\n\t)\n}\n\nexport async function shouldForceFresh({\n\tforceFresh,\n\trequest,\n\tkey,\n}: {\n\tforceFresh?: boolean | string\n\trequest?: Request\n\tkey?: string\n}) {\n\tif (typeof forceFresh === 'boolean') return forceFresh\n\tif (typeof forceFresh === 'string' && key) {\n\t\treturn forceFresh.split(',').includes(key)\n\t}\n\n\tif (!request) return false\n\tconst fresh = new URL(request.url).searchParams.get('fresh')\n\tif (typeof fresh !== 'string') return false\n\tif (fresh === '') return true\n\tif (!key) return false\n\n\treturn fresh.split(',').includes(key)\n}\n"]}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { type Timings } from './timing.server.js';
|
|
2
|
+
export declare function compileMdx(file: string, { request, timings, forceFresh, }?: {
|
|
3
|
+
request?: Request;
|
|
4
|
+
timings?: Timings;
|
|
5
|
+
forceFresh?: boolean;
|
|
6
|
+
}): Promise<{
|
|
7
|
+
code: string;
|
|
8
|
+
title: string | null;
|
|
9
|
+
epicVideoEmbeds: Array<string>;
|
|
10
|
+
}>;
|
|
11
|
+
export declare function compileMarkdownString(markdownString: string): Promise<string>;
|
|
12
|
+
//# sourceMappingURL=compile-mdx.server.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"compile-mdx.server.d.ts","sourceRoot":"","sources":["../../src/compile-mdx.server.ts"],"names":[],"mappings":"AAoBA,OAAO,EAAE,KAAK,OAAO,EAAE,MAAM,oBAAoB,CAAA;AAwJjD,wBAAsB,UAAU,CAC/B,IAAI,EAAE,MAAM,EACZ,EACC,OAAO,EACP,OAAO,EACP,UAAU,GACV,GAAE;IACF,OAAO,CAAC,EAAE,OAAO,CAAA;IACjB,OAAO,CAAC,EAAE,OAAO,CAAA;IACjB,UAAU,CAAC,EAAE,OAAO,CAAA;CACf;UA4BA,MAAM;WACL,MAAM,GAAG,IAAI;qBACH,KAAK,CAAC,MAAM,CAAC;GAL9B;AAoED,wBAAsB,qBAAqB,CAAC,cAAc,EAAE,MAAM,mBA8BjE"}
|
|
@@ -0,0 +1,285 @@
|
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
import { rehypeCodeBlocksShiki } from '@kentcdodds/md-temp';
|
|
4
|
+
import lz from 'lz-string';
|
|
5
|
+
import { bundleMDX } from 'mdx-bundler';
|
|
6
|
+
import PQueue from 'p-queue';
|
|
7
|
+
import rehypeAutolinkHeadings from 'rehype-autolink-headings';
|
|
8
|
+
import emoji from 'remark-emoji';
|
|
9
|
+
import gfm from 'remark-gfm';
|
|
10
|
+
import { visit } from 'unist-util-visit';
|
|
11
|
+
import { cachified, compiledInstructionMarkdownCache, compiledMarkdownCache, shouldForceFresh, } from './cache.server.js';
|
|
12
|
+
import { checkConnectionCached } from './utils.server.js';
|
|
13
|
+
function remarkMermaidCodeToSvg() {
|
|
14
|
+
return async (tree) => {
|
|
15
|
+
const promises = [];
|
|
16
|
+
visit(tree, 'code', (node, index, parent) => {
|
|
17
|
+
if (node.lang === 'mermaid' && parent && typeof index === 'number') {
|
|
18
|
+
const promise = (async () => {
|
|
19
|
+
const isConnected = await checkConnectionCached();
|
|
20
|
+
if (isConnected) {
|
|
21
|
+
const compressed = lz.compressToEncodedURIComponent(node.value);
|
|
22
|
+
const url = `https://mermaid-to-svg.kentcdodds.workers.dev/svg?mermaid=${compressed}`;
|
|
23
|
+
const timeout = AbortSignal.timeout(5000);
|
|
24
|
+
const svgResponse = await fetch(url, { signal: timeout }).catch(() => null);
|
|
25
|
+
if (svgResponse?.ok) {
|
|
26
|
+
const svgText = await svgResponse.text();
|
|
27
|
+
if (svgText) {
|
|
28
|
+
parent.children[index] = {
|
|
29
|
+
type: 'mdxJsxFlowElement',
|
|
30
|
+
name: 'div',
|
|
31
|
+
attributes: [
|
|
32
|
+
{
|
|
33
|
+
type: 'mdxJsxAttribute',
|
|
34
|
+
name: 'className',
|
|
35
|
+
value: 'mermaid not-prose',
|
|
36
|
+
},
|
|
37
|
+
{
|
|
38
|
+
type: 'mdxJsxAttribute',
|
|
39
|
+
name: 'dangerouslySetInnerHTML',
|
|
40
|
+
value: {
|
|
41
|
+
type: 'mdxJsxAttributeValueExpression',
|
|
42
|
+
value: `{__html: ${JSON.stringify(svgText)}}`,
|
|
43
|
+
// This hack brought to you by this: https://github.com/syntax-tree/hast-util-to-estree/blob/e5ccb97e9f42bba90359ea6d0f83a11d74e0dad6/lib/handlers/mdx-expression.js#L35-L38
|
|
44
|
+
// no idea why we're required to have estree here, but I'm pretty sure someone is supposed to add it automatically for us and it just never happens...
|
|
45
|
+
data: {
|
|
46
|
+
estree: {
|
|
47
|
+
type: 'Program',
|
|
48
|
+
sourceType: 'script',
|
|
49
|
+
body: [
|
|
50
|
+
{
|
|
51
|
+
type: 'ExpressionStatement',
|
|
52
|
+
expression: {
|
|
53
|
+
type: 'ObjectExpression',
|
|
54
|
+
properties: [
|
|
55
|
+
{
|
|
56
|
+
type: 'Property',
|
|
57
|
+
method: false,
|
|
58
|
+
shorthand: false,
|
|
59
|
+
computed: false,
|
|
60
|
+
kind: 'init',
|
|
61
|
+
key: {
|
|
62
|
+
type: 'Identifier',
|
|
63
|
+
name: '__html',
|
|
64
|
+
},
|
|
65
|
+
value: {
|
|
66
|
+
type: 'Literal',
|
|
67
|
+
value: svgText,
|
|
68
|
+
},
|
|
69
|
+
},
|
|
70
|
+
],
|
|
71
|
+
},
|
|
72
|
+
},
|
|
73
|
+
],
|
|
74
|
+
},
|
|
75
|
+
},
|
|
76
|
+
},
|
|
77
|
+
},
|
|
78
|
+
],
|
|
79
|
+
children: [],
|
|
80
|
+
};
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
parent.children[index] = {
|
|
86
|
+
type: 'mdxJsxFlowElement',
|
|
87
|
+
name: 'Mermaid',
|
|
88
|
+
attributes: [
|
|
89
|
+
{
|
|
90
|
+
type: 'mdxJsxAttribute',
|
|
91
|
+
name: 'code',
|
|
92
|
+
value: node.value,
|
|
93
|
+
},
|
|
94
|
+
],
|
|
95
|
+
children: [],
|
|
96
|
+
};
|
|
97
|
+
})();
|
|
98
|
+
promises.push(promise);
|
|
99
|
+
}
|
|
100
|
+
});
|
|
101
|
+
await Promise.all(promises);
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
function trimCodeBlocks() {
|
|
105
|
+
return async function transformer(tree) {
|
|
106
|
+
visit(tree, 'element', (preNode) => {
|
|
107
|
+
if (preNode.tagName !== 'pre' || !preNode.children.length) {
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
const codeNode = preNode.children[0];
|
|
111
|
+
if (!codeNode ||
|
|
112
|
+
codeNode.type !== 'element' ||
|
|
113
|
+
codeNode.tagName !== 'code') {
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
const [codeStringNode] = codeNode.children;
|
|
117
|
+
if (!codeStringNode)
|
|
118
|
+
return;
|
|
119
|
+
if (codeStringNode.type !== 'text') {
|
|
120
|
+
console.warn(`trimCodeBlocks: Unexpected: codeStringNode type is not "text": ${codeStringNode.type}`);
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
codeStringNode.value = codeStringNode.value.trimEnd();
|
|
124
|
+
});
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
function removePreContainerDivs() {
|
|
128
|
+
return async function preContainerDivsTransformer(tree) {
|
|
129
|
+
visit(tree, { type: 'element', tagName: 'pre' }, function visitor(node, index, parent) {
|
|
130
|
+
if (parent?.type !== 'element')
|
|
131
|
+
return;
|
|
132
|
+
if (parent.tagName !== 'div')
|
|
133
|
+
return;
|
|
134
|
+
if (parent.children.length !== 1 && index === 0)
|
|
135
|
+
return;
|
|
136
|
+
Object.assign(parent, node);
|
|
137
|
+
});
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
const rehypePlugins = [
|
|
141
|
+
[rehypeAutolinkHeadings, { behavior: 'wrap' }],
|
|
142
|
+
trimCodeBlocks,
|
|
143
|
+
rehypeCodeBlocksShiki,
|
|
144
|
+
removePreContainerDivs,
|
|
145
|
+
];
|
|
146
|
+
const verboseLog = process.env.EPICSHOP_VERBOSE_LOG === 'true' ? console.log : () => { };
|
|
147
|
+
export async function compileMdx(file, { request, timings, forceFresh, } = {}) {
|
|
148
|
+
const stat = await fs.promises
|
|
149
|
+
.stat(file)
|
|
150
|
+
.catch((error) => ({ error }));
|
|
151
|
+
if ('error' in stat) {
|
|
152
|
+
throw new Error(`File stat cannot be read: ${stat.error}`);
|
|
153
|
+
}
|
|
154
|
+
const key = `file:${file}`;
|
|
155
|
+
forceFresh = await shouldForceFresh({ forceFresh, request, key });
|
|
156
|
+
const existingCacheEntry = await compiledInstructionMarkdownCache.get(key);
|
|
157
|
+
if (!forceFresh && existingCacheEntry) {
|
|
158
|
+
forceFresh = stat.mtimeMs > existingCacheEntry.metadata.createdTime;
|
|
159
|
+
}
|
|
160
|
+
return cachified({
|
|
161
|
+
key,
|
|
162
|
+
cache: compiledInstructionMarkdownCache,
|
|
163
|
+
request,
|
|
164
|
+
timings,
|
|
165
|
+
forceFresh,
|
|
166
|
+
getFreshValue: () => compileMdxImpl(file),
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
async function compileMdxImpl(file) {
|
|
170
|
+
let title = null;
|
|
171
|
+
const epicVideoEmbeds = [];
|
|
172
|
+
try {
|
|
173
|
+
verboseLog(`Compiling ${file}`);
|
|
174
|
+
const bundleResult = await queuedBundleMDX({
|
|
175
|
+
file,
|
|
176
|
+
cwd: path.dirname(file),
|
|
177
|
+
mdxOptions(options) {
|
|
178
|
+
options.remarkPlugins = [
|
|
179
|
+
...(options.remarkPlugins ?? []),
|
|
180
|
+
gfm,
|
|
181
|
+
remarkMermaidCodeToSvg,
|
|
182
|
+
() => (tree) => {
|
|
183
|
+
visit(tree, 'heading', (node) => {
|
|
184
|
+
if (title)
|
|
185
|
+
return;
|
|
186
|
+
if (node.depth === 1) {
|
|
187
|
+
visit(node, 'text', (textNode) => {
|
|
188
|
+
title = textNode.value.trim();
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
});
|
|
192
|
+
title = title ? title.replace(/^\d+\. /, '').trim() : null;
|
|
193
|
+
},
|
|
194
|
+
() => (tree) => {
|
|
195
|
+
visit(tree, 'mdxJsxFlowElement', (jsxEl) => {
|
|
196
|
+
if (jsxEl.name !== 'EpicVideo')
|
|
197
|
+
return;
|
|
198
|
+
const urlAttr = jsxEl.attributes.find((a) => a.type === 'mdxJsxAttribute' && a.name === 'url');
|
|
199
|
+
if (!urlAttr)
|
|
200
|
+
return;
|
|
201
|
+
let url = urlAttr.value;
|
|
202
|
+
if (typeof url !== 'string')
|
|
203
|
+
return;
|
|
204
|
+
if (url.endsWith('/'))
|
|
205
|
+
url = url.slice(0, -1);
|
|
206
|
+
epicVideoEmbeds.push(url);
|
|
207
|
+
});
|
|
208
|
+
},
|
|
209
|
+
emoji,
|
|
210
|
+
];
|
|
211
|
+
options.rehypePlugins = [
|
|
212
|
+
...(options.rehypePlugins ?? []),
|
|
213
|
+
...rehypePlugins,
|
|
214
|
+
];
|
|
215
|
+
options.mdxExtensions = ['.mdx', '.md'];
|
|
216
|
+
options.format = 'mdx';
|
|
217
|
+
options.development = false;
|
|
218
|
+
return options;
|
|
219
|
+
},
|
|
220
|
+
});
|
|
221
|
+
if (!bundleResult)
|
|
222
|
+
throw new Error(`Timeout for file: ${file}`);
|
|
223
|
+
const result = { code: bundleResult.code, title, epicVideoEmbeds };
|
|
224
|
+
return result;
|
|
225
|
+
}
|
|
226
|
+
catch (error) {
|
|
227
|
+
console.error(`Compilation error for file: `, file, error);
|
|
228
|
+
throw error;
|
|
229
|
+
}
|
|
230
|
+
finally {
|
|
231
|
+
verboseLog(`Successfully compiled ${file}`);
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
export async function compileMarkdownString(markdownString) {
|
|
235
|
+
return cachified({
|
|
236
|
+
key: markdownString,
|
|
237
|
+
cache: compiledMarkdownCache,
|
|
238
|
+
ttl: 1000 * 60 * 60 * 24,
|
|
239
|
+
getFreshValue: async () => {
|
|
240
|
+
try {
|
|
241
|
+
verboseLog(`Compiling string`, markdownString);
|
|
242
|
+
const result = await queuedBundleMDX({
|
|
243
|
+
source: markdownString,
|
|
244
|
+
mdxOptions(options) {
|
|
245
|
+
options.rehypePlugins = [
|
|
246
|
+
...(options.rehypePlugins ?? []),
|
|
247
|
+
...rehypePlugins,
|
|
248
|
+
];
|
|
249
|
+
options.development = false;
|
|
250
|
+
return options;
|
|
251
|
+
},
|
|
252
|
+
});
|
|
253
|
+
if (!result)
|
|
254
|
+
throw new Error(`Timed out compiling markdown string`);
|
|
255
|
+
return result.code;
|
|
256
|
+
}
|
|
257
|
+
catch (error) {
|
|
258
|
+
console.error(`Compilation error for code: `, markdownString, error);
|
|
259
|
+
throw error;
|
|
260
|
+
}
|
|
261
|
+
finally {
|
|
262
|
+
verboseLog(`Successfully compiled string`, markdownString);
|
|
263
|
+
}
|
|
264
|
+
},
|
|
265
|
+
});
|
|
266
|
+
}
|
|
267
|
+
let _queue = null;
|
|
268
|
+
async function getQueue() {
|
|
269
|
+
if (_queue)
|
|
270
|
+
return _queue;
|
|
271
|
+
_queue = new PQueue({
|
|
272
|
+
concurrency: 1,
|
|
273
|
+
throwOnTimeout: true,
|
|
274
|
+
timeout: 1000 * 60,
|
|
275
|
+
});
|
|
276
|
+
return _queue;
|
|
277
|
+
}
|
|
278
|
+
// We have to use a queue because we can't run more than one of these at a time
|
|
279
|
+
// or we'll hit an out of memory error because esbuild uses a lot of memory...
|
|
280
|
+
async function queuedBundleMDX(...args) {
|
|
281
|
+
const queue = await getQueue();
|
|
282
|
+
const result = await queue.add(() => bundleMDX(...args));
|
|
283
|
+
return result;
|
|
284
|
+
}
|
|
285
|
+
//# sourceMappingURL=compile-mdx.server.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"compile-mdx.server.js","sourceRoot":"","sources":["../../src/compile-mdx.server.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,IAAI,CAAA;AACnB,OAAO,IAAI,MAAM,MAAM,CAAA;AACvB,OAAO,EAAE,qBAAqB,EAAE,MAAM,qBAAqB,CAAA;AAE3D,OAAO,EAAE,MAAM,WAAW,CAAA;AAG1B,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAA;AACvC,OAAO,MAAM,MAAM,SAAS,CAAA;AAC5B,OAAO,sBAAsB,MAAM,0BAA0B,CAAA;AAC7D,OAAO,KAAK,MAAM,cAAc,CAAA;AAChC,OAAO,GAAG,MAAM,YAAY,CAAA;AAE5B,OAAO,EAAE,KAAK,EAAE,MAAM,kBAAkB,CAAA;AACxC,OAAO,EACN,SAAS,EACT,gCAAgC,EAChC,qBAAqB,EACrB,gBAAgB,GAChB,MAAM,mBAAmB,CAAA;AAE1B,OAAO,EAAE,qBAAqB,EAAE,MAAM,mBAAmB,CAAA;AAEzD,SAAS,sBAAsB;IAC9B,OAAO,KAAK,EAAE,IAAe,EAAE,EAAE;QAChC,MAAM,QAAQ,GAAyB,EAAE,CAAA;QACzC,KAAK,CAAC,IAAI,EAAE,MAAM,EAAE,CAAC,IAAI,EAAE,KAAK,EAAE,MAAM,EAAE,EAAE;YAC3C,IAAI,IAAI,CAAC,IAAI,KAAK,SAAS,IAAI,MAAM,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE,CAAC;gBACpE,MAAM,OAAO,GAAG,CAAC,KAAK,IAAI,EAAE;oBAC3B,M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C,GAAG,IAAI,CAAC,CAAC,CAAA;IACxD,OAAO,MAAM,CAAA;AACd,CAAC","sourcesContent":["import fs from 'fs'\nimport path from 'path'\nimport { rehypeCodeBlocksShiki } from '@kentcdodds/md-temp'\nimport { type Element, type Root as HastRoot } from 'hast'\nimport lz from 'lz-string'\nimport { type Root as MdastRoot } from 'mdast'\nimport { type MdxJsxFlowElement } from 'mdast-util-mdx-jsx'\nimport { bundleMDX } from 'mdx-bundler'\nimport PQueue from 'p-queue'\nimport rehypeAutolinkHeadings from 'rehype-autolink-headings'\nimport emoji from 'remark-emoji'\nimport gfm from 'remark-gfm'\nimport { type PluggableList } from 'unified'\nimport { visit } from 'unist-util-visit'\nimport {\n\tcachified,\n\tcompiledInstructionMarkdownCache,\n\tcompiledMarkdownCache,\n\tshouldForceFresh,\n} from './cache.server.js'\nimport { type Timings } from './timing.server.js'\nimport { checkConnectionCached } from './utils.server.js'\n\nfunction remarkMermaidCodeToSvg() {\n\treturn async (tree: MdastRoot) => {\n\t\tconst promises: Array<Promise<void>> = []\n\t\tvisit(tree, 'code', (node, index, parent) => {\n\t\t\tif (node.lang === 'mermaid' && parent && typeof index === 'number') {\n\t\t\t\tconst promise = (async () => {\n\t\t\t\t\tconst isConnected = await checkConnectionCached()\n\t\t\t\t\tif (isConnected) {\n\t\t\t\t\t\tconst compressed = lz.compressToEncodedURIComponent(node.value)\n\t\t\t\t\t\tconst url = `https://mermaid-to-svg.kentcdodds.workers.dev/svg?mermaid=${compressed}`\n\n\t\t\t\t\t\tconst timeout = AbortSignal.timeout(5000)\n\t\t\t\t\t\tconst svgResponse = await fetch(url, { signal: timeout }).catch(\n\t\t\t\t\t\t\t() => null,\n\t\t\t\t\t\t)\n\t\t\t\t\t\tif (svgResponse?.ok) {\n\t\t\t\t\t\t\tconst svgText = await svgResponse.text()\n\t\t\t\t\t\t\tif (svgText) {\n\t\t\t\t\t\t\t\tparent.children[index] = {\n\t\t\t\t\t\t\t\t\ttype: 'mdxJsxFlowElement',\n\t\t\t\t\t\t\t\t\tname: 'div',\n\t\t\t\t\t\t\t\t\tattributes: [\n\t\t\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\t\t\ttype: 'mdxJsxAttribute',\n\t\t\t\t\t\t\t\t\t\t\tname: 'className',\n\t\t\t\t\t\t\t\t\t\t\tvalue: 'mermaid not-prose',\n\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\t\t\ttype: 'mdxJsxAttribute',\n\t\t\t\t\t\t\t\t\t\t\tname: 'dangerouslySetInnerHTML',\n\t\t\t\t\t\t\t\t\t\t\tvalue: {\n\t\t\t\t\t\t\t\t\t\t\t\ttype: 'mdxJsxAttributeValueExpression',\n\t\t\t\t\t\t\t\t\t\t\t\tvalue: `{__html: ${JSON.stringify(svgText)}}`,\n\t\t\t\t\t\t\t\t\t\t\t\t// This hack brought to you by this: https://github.com/syntax-tree/hast-util-to-estree/blob/e5ccb97e9f42bba90359ea6d0f83a11d74e0dad6/lib/handlers/mdx-expression.js#L35-L38\n\t\t\t\t\t\t\t\t\t\t\t\t// no idea why we're required to have estree here, but I'm pretty sure someone is supposed to add it automatically for us and it just never happens...\n\t\t\t\t\t\t\t\t\t\t\t\tdata: {\n\t\t\t\t\t\t\t\t\t\t\t\t\testree: {\n\t\t\t\t\t\t\t\t\t\t\t\t\t\ttype: 'Program',\n\t\t\t\t\t\t\t\t\t\t\t\t\t\tsourceType: 'script',\n\t\t\t\t\t\t\t\t\t\t\t\t\t\tbody: [\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\ttype: 'ExpressionStatement',\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\texpression: {\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\ttype: 'ObjectExpression',\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tproperties: [\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\ttype: 'Property',\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tmethod: false,\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tshorthand: false,\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tcomputed: false,\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tkind: 'init',\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tkey: {\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\ttype: 'Identifier',\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tname: '__html',\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tvalue: {\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\ttype: 'Literal',\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tvalue: svgText,\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t],\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t],\n\t\t\t\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\t\t],\n\t\t\t\t\t\t\t\t\tchildren: [],\n\t\t\t\t\t\t\t\t} satisfies MdxJsxFlowElement\n\t\t\t\t\t\t\t\treturn\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\n\t\t\t\t\tparent.children[index] = {\n\t\t\t\t\t\ttype: 'mdxJsxFlowElement',\n\t\t\t\t\t\tname: 'Mermaid',\n\t\t\t\t\t\tattributes: [\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\ttype: 'mdxJsxAttribute',\n\t\t\t\t\t\t\t\tname: 'code',\n\t\t\t\t\t\t\t\tvalue: node.value,\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t],\n\t\t\t\t\t\tchildren: [],\n\t\t\t\t\t} satisfies MdxJsxFlowElement\n\t\t\t\t})()\n\t\t\t\tpromises.push(promise)\n\t\t\t}\n\t\t})\n\t\tawait Promise.all(promises)\n\t}\n}\n\nfunction trimCodeBlocks() {\n\treturn async function transformer(tree: HastRoot) {\n\t\tvisit(tree, 'element', (preNode: Element) => {\n\t\t\tif (preNode.tagName !== 'pre' || !preNode.children.length) {\n\t\t\t\treturn\n\t\t\t}\n\t\t\tconst codeNode = preNode.children[0]\n\t\t\tif (\n\t\t\t\t!codeNode ||\n\t\t\t\tcodeNode.type !== 'element' ||\n\t\t\t\tcodeNode.tagName !== 'code'\n\t\t\t) {\n\t\t\t\treturn\n\t\t\t}\n\t\t\tconst [codeStringNode] = codeNode.children\n\t\t\tif (!codeStringNode) return\n\n\t\t\tif (codeStringNode.type !== 'text') {\n\t\t\t\tconsole.warn(\n\t\t\t\t\t`trimCodeBlocks: Unexpected: codeStringNode type is not \"text\": ${codeStringNode.type}`,\n\t\t\t\t)\n\t\t\t\treturn\n\t\t\t}\n\t\t\tcodeStringNode.value = codeStringNode.value.trimEnd()\n\t\t})\n\t}\n}\n\nfunction removePreContainerDivs() {\n\treturn async function preContainerDivsTransformer(tree: HastRoot) {\n\t\tvisit(\n\t\t\ttree,\n\t\t\t{ type: 'element', tagName: 'pre' },\n\t\t\tfunction visitor(node, index, parent) {\n\t\t\t\tif (parent?.type !== 'element') return\n\t\t\t\tif (parent.tagName !== 'div') return\n\t\t\t\tif (parent.children.length !== 1 && index === 0) return\n\t\t\t\tObject.assign(parent, node)\n\t\t\t},\n\t\t)\n\t}\n}\n\nconst rehypePlugins = [\n\t[rehypeAutolinkHeadings, { behavior: 'wrap' }],\n\ttrimCodeBlocks,\n\trehypeCodeBlocksShiki,\n\tremovePreContainerDivs,\n] satisfies PluggableList\n\nconst verboseLog =\n\tprocess.env.EPICSHOP_VERBOSE_LOG === 'true' ? console.log : () => {}\n\nexport async function compileMdx(\n\tfile: string,\n\t{\n\t\trequest,\n\t\ttimings,\n\t\tforceFresh,\n\t}: {\n\t\trequest?: Request\n\t\ttimings?: Timings\n\t\tforceFresh?: boolean\n\t} = {},\n) {\n\tconst stat = await fs.promises\n\t\t.stat(file)\n\t\t.catch((error: unknown) => ({ error }))\n\tif ('error' in stat) {\n\t\tthrow new Error(`File stat cannot be read: ${stat.error}`)\n\t}\n\n\tconst key = `file:${file}`\n\tforceFresh = await shouldForceFresh({ forceFresh, request, key })\n\n\tconst existingCacheEntry = await compiledInstructionMarkdownCache.get(key)\n\tif (!forceFresh && existingCacheEntry) {\n\t\tforceFresh = stat.mtimeMs > existingCacheEntry.metadata.createdTime\n\t}\n\n\treturn cachified({\n\t\tkey,\n\t\tcache: compiledInstructionMarkdownCache,\n\t\trequest,\n\t\ttimings,\n\t\tforceFresh,\n\t\tgetFreshValue: () => compileMdxImpl(file),\n\t})\n}\n\nasync function compileMdxImpl(file: string): Promise<{\n\tcode: string\n\ttitle: string | null\n\tepicVideoEmbeds: Array<string>\n}> {\n\tlet title: string | null = null\n\tconst epicVideoEmbeds: Array<string> = []\n\n\ttry {\n\t\tverboseLog(`Compiling ${file}`)\n\t\tconst bundleResult = await queuedBundleMDX({\n\t\t\tfile,\n\t\t\tcwd: path.dirname(file),\n\t\t\tmdxOptions(options) {\n\t\t\t\toptions.remarkPlugins = [\n\t\t\t\t\t...(options.remarkPlugins ?? []),\n\t\t\t\t\tgfm,\n\t\t\t\t\tremarkMermaidCodeToSvg,\n\t\t\t\t\t() => (tree: MdastRoot) => {\n\t\t\t\t\t\tvisit(tree, 'heading', (node) => {\n\t\t\t\t\t\t\tif (title) return\n\t\t\t\t\t\t\tif (node.depth === 1) {\n\t\t\t\t\t\t\t\tvisit(node, 'text', (textNode) => {\n\t\t\t\t\t\t\t\t\ttitle = textNode.value.trim()\n\t\t\t\t\t\t\t\t})\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t})\n\t\t\t\t\t\ttitle = title ? title.replace(/^\\d+\\. /, '').trim() : null\n\t\t\t\t\t},\n\t\t\t\t\t() => (tree: MdastRoot) => {\n\t\t\t\t\t\tvisit(tree, 'mdxJsxFlowElement', (jsxEl) => {\n\t\t\t\t\t\t\tif (jsxEl.name !== 'EpicVideo') return\n\t\t\t\t\t\t\tconst urlAttr = jsxEl.attributes.find(\n\t\t\t\t\t\t\t\t(a) => a.type === 'mdxJsxAttribute' && a.name === 'url',\n\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\tif (!urlAttr) return\n\t\t\t\t\t\t\tlet url = urlAttr.value\n\t\t\t\t\t\t\tif (typeof url !== 'string') return\n\t\t\t\t\t\t\tif (url.endsWith('/')) url = url.slice(0, -1)\n\t\t\t\t\t\t\tepicVideoEmbeds.push(url)\n\t\t\t\t\t\t})\n\t\t\t\t\t},\n\t\t\t\t\temoji,\n\t\t\t\t]\n\t\t\t\toptions.rehypePlugins = [\n\t\t\t\t\t...(options.rehypePlugins ?? []),\n\t\t\t\t\t...rehypePlugins,\n\t\t\t\t]\n\t\t\t\toptions.mdxExtensions = ['.mdx', '.md']\n\t\t\t\toptions.format = 'mdx'\n\t\t\t\toptions.development = false\n\t\t\t\treturn options\n\t\t\t},\n\t\t})\n\t\tif (!bundleResult) throw new Error(`Timeout for file: ${file}`)\n\n\t\tconst result = { code: bundleResult.code, title, epicVideoEmbeds }\n\t\treturn result\n\t} catch (error: unknown) {\n\t\tconsole.error(`Compilation error for file: `, file, error)\n\t\tthrow error\n\t} finally {\n\t\tverboseLog(`Successfully compiled ${file}`)\n\t}\n}\n\nexport async function compileMarkdownString(markdownString: string) {\n\treturn cachified({\n\t\tkey: markdownString,\n\t\tcache: compiledMarkdownCache,\n\t\tttl: 1000 * 60 * 60 * 24,\n\t\tgetFreshValue: async () => {\n\t\t\ttry {\n\t\t\t\tverboseLog(`Compiling string`, markdownString)\n\t\t\t\tconst result = await queuedBundleMDX({\n\t\t\t\t\tsource: markdownString,\n\t\t\t\t\tmdxOptions(options) {\n\t\t\t\t\t\toptions.rehypePlugins = [\n\t\t\t\t\t\t\t...(options.rehypePlugins ?? []),\n\t\t\t\t\t\t\t...rehypePlugins,\n\t\t\t\t\t\t]\n\t\t\t\t\t\toptions.development = false\n\t\t\t\t\t\treturn options\n\t\t\t\t\t},\n\t\t\t\t})\n\t\t\t\tif (!result) throw new Error(`Timed out compiling markdown string`)\n\n\t\t\t\treturn result.code\n\t\t\t} catch (error: unknown) {\n\t\t\t\tconsole.error(`Compilation error for code: `, markdownString, error)\n\t\t\t\tthrow error\n\t\t\t} finally {\n\t\t\t\tverboseLog(`Successfully compiled string`, markdownString)\n\t\t\t}\n\t\t},\n\t})\n}\n\nlet _queue: PQueue | null = null\nasync function getQueue() {\n\tif (_queue) return _queue\n\n\t_queue = new PQueue({\n\t\tconcurrency: 1,\n\t\tthrowOnTimeout: true,\n\t\ttimeout: 1000 * 60,\n\t})\n\treturn _queue\n}\n\n// We have to use a queue because we can't run more than one of these at a time\n// or we'll hit an out of memory error because esbuild uses a lot of memory...\nasync function queuedBundleMDX(...args: Parameters<typeof bundleMDX>) {\n\tconst queue = await getQueue()\n\tconst result = await queue.add(() => bundleMDX(...args))\n\treturn result\n}\n"]}
|