-
Notifications
You must be signed in to change notification settings - Fork 91
Expand file tree
/
Copy pathsplit-bundler-friendly-vfss.mjs
More file actions
142 lines (121 loc) · 4.66 KB
/
Copy pathsplit-bundler-friendly-vfss.mjs
File metadata and controls
142 lines (121 loc) · 4.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import { mkdir, readFile, writeFile } from 'node:fs/promises';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
const rootDir = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');
const sourcePath = path.join(rootDir, 'src/bin/sqlite3-bundler-friendly.mjs');
const outputDir = path.join(rootDir, 'src/bin');
const generatedHeader = (
sourceFile,
) => `// Generated by scripts/split-bundler-friendly-vfss.mjs from ${sourceFile}.
// Do not edit this file directly.
`;
const initRegistry = `const sqlite3BundlerFriendlyOptionalState =
(globalThis.__sqlite3BundlerFriendlyOptional ??= {
initializers: Object.create(null),
});
const sqlite3BundlerFriendlyRegisterOptionalInitializer = (slot, initializer) => {
if (globalThis.sqlite3ApiBootstrap?.initializers) {
globalThis.sqlite3ApiBootstrap.initializers.push(initializer);
} else {
(sqlite3BundlerFriendlyOptionalState.initializers[slot] ??= []).push(initializer);
}
};
`;
const baseHook = `const sqlite3BundlerFriendlyInstallOptionalInitializers = (slot) => {
const sqlite3BundlerFriendlyOptionalState = globalThis.__sqlite3BundlerFriendlyOptional;
const initializers = sqlite3BundlerFriendlyOptionalState?.initializers?.[slot];
if (initializers?.length) {
globalThis.sqlite3ApiBootstrap.initializers.push(
...initializers,
);
}
};
`;
const markerSpecs = {
worker1: 'This file implements the initializer for SQLite\'s "Worker API #1"',
helper: 'This file installs sqlite3.vfs, a namespace of helpers',
vtab: 'This file installs sqlite3.vtab, a namespace of helpers',
kvvfs: 'This file houses the "kvvfs" pieces of the SQLite3 JS API',
opfsShared: 'This file holds code shared by sqlite3-vfs-opfs{,-wl}.c-pp.js',
opfs: 'This file holds the synchronous half of an sqlite3_vfs',
sahpool: 'This file holds a sqlite3_vfs backed by OPFS storage',
opfsWl: 'This file is a reimplementation of the "opfs" VFS',
tail: 'This file is the tail end of the sqlite3-api.js constellation',
};
const findCommentStart = (source, marker) => {
const markerIndex = source.indexOf(marker);
if (markerIndex < 0) {
throw new Error(`Unable to find marker: ${marker}`);
}
const commentStart = source.lastIndexOf('/*', markerIndex);
if (commentStart < 0) {
throw new Error(`Unable to find comment start for marker: ${marker}`);
}
return commentStart;
};
const replaceAll = (value, replacements) => {
let result = value;
for (const [from, to] of replacements) {
result = result.split(from).join(to);
}
return result;
};
const source = await readFile(sourcePath, 'utf8');
const markers = Object.fromEntries(
Object.entries(markerSpecs).map(([name, marker]) => [name, findCommentStart(source, marker)]),
);
const ranges = {
worker1: [markers.worker1, markers.helper],
helper: [markers.helper, markers.vtab],
vtab: [markers.vtab, markers.kvvfs],
kvvfs: [markers.kvvfs, markers.opfsShared],
opfsShared: [markers.opfsShared, markers.opfs],
opfs: [markers.opfs, markers.sahpool],
sahpool: [markers.sahpool, markers.opfsWl],
opfsWl: [markers.opfsWl, markers.tail],
};
for (const [name, [start, end]] of Object.entries(ranges)) {
if (start >= end) {
throw new Error(`Invalid ${name} range: ${start} >= ${end}`);
}
}
const bootstrapAsyncMarker = 'globalThis.sqlite3ApiBootstrap.initializersAsync = [];\n';
if (!source.includes(bootstrapAsyncMarker)) {
throw new Error(`Unable to find bootstrap hook marker: ${bootstrapAsyncMarker.trim()}`);
}
const strippedSource = Object.entries(ranges)
.sort((a, b) => b[1][0] - a[1][0])
.reduce(
(current, [name, [start, end]]) =>
current.slice(0, start) +
`\n/* Optional initializer slot removed by scripts/split-bundler-friendly-vfss.mjs: ${name} */\n` +
`sqlite3BundlerFriendlyInstallOptionalInitializers('${name}');\n` +
current.slice(end),
source,
)
.replace(bootstrapAsyncMarker, bootstrapAsyncMarker + baseHook);
const outputFileNames = {
worker1: 'sqlite3-worker1-api.mjs',
};
const toOptionalModule = (slot, chunk) =>
generatedHeader('sqlite3-bundler-friendly.mjs') +
initRegistry +
replaceAll(chunk, [
[
'globalThis.sqlite3ApiBootstrap.initializers.push(',
`sqlite3BundlerFriendlyRegisterOptionalInitializer('${slot}', `,
],
]);
await mkdir(outputDir, { recursive: true });
await writeFile(
path.join(outputDir, 'sqlite3-bundler-friendly.core.mjs'),
generatedHeader('sqlite3-bundler-friendly.mjs') + strippedSource,
);
await Promise.all(
Object.entries(ranges).map(([name, [start, end]]) =>
writeFile(
path.join(outputDir, outputFileNames[name] ?? `sqlite3-vfs-${name}.mjs`),
toOptionalModule(name, source.slice(start, end)),
),
),
);