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