Repository navigation
Expand file tree
/
Copy pathstorage-bundler.js
More file actions
235 lines (213 loc) · 8.34 KB
/
Copy pathstorage-bundler.js
File metadata and controls
235 lines (213 loc) · 8.34 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import webpack from "webpack";
import path from "node:path";
import fs from "node:fs/promises";
import { fileURLToPath } from "node:url";
import { EsbuildPlugin } from "esbuild-loader";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
/**
* Configuration mapping storage provider names to their pure npm/workspace packages.
*/
const STORAGE_PROVIDERS = [
{ provider: "amazon-redshift", package: "@cocreate/amazon-redshift" },
{ provider: "auroradb", package: "@cocreate/auroradb" },
{ provider: "cockroachdb", package: "@cocreate/cockroachdb" },
{ provider: "cosmosdb", package: "@cocreate/cosmosdb" },
{ provider: "documentdb", package: "@cocreate/documentdb" },
{ provider: "dqlite", package: "@cocreate/dqlite" },
{ provider: "dynamodb", package: "@cocreate/dynamodb" },
{ provider: "ferretdb", package: "@cocreate/ferretdb" },
{ provider: "firestore", package: "@cocreate/firestore" },
{ provider: "mariadb", package: "@cocreate/mariadb" },
{ provider: "mongodb", package: "@cocreate/mongodb" },
{ provider: "mysql", package: "@cocreate/mysql" },
{ provider: "oracledb", package: "@cocreate/oracledb" },
{ provider: "planetscale", package: "@cocreate/planetscale" },
{ provider: "postgresql", package: "@cocreate/postgresql" },
{ provider: "rqlite", package: "@cocreate/rqlite" },
{ provider: "qdrant", package: "@cocreate/qdrant" },
{ provider: "sqlite", package: "@cocreate/sqlite" },
{ provider: "tidb", package: "@cocreate/tidb" },
{ provider: "timescaledb", package: "@cocreate/timescaledb" },
{ provider: "turso", package: "@cocreate/turso" },
{ provider: "yugabytedb", package: "@cocreate/yugabytedb" }
];
/**
* Generates the wrapper entry point code for a target database adapter.
* Re-emits 'orgDeleted' as an isolate-local process event so underlying database
* connection utilities can perform automatic internal socket/tunnel teardowns.
*
* @param {string} packageName - Target database package name
* @returns {string} Raw JavaScript wrapper code
*/
function generateWorkerWrapperCode(packageName) {
return `
/********************************************************************************
* Auto-generated Worker Wrapper for ${packageName}
* Generated by @cocreate/storage-bundler
********************************************************************************/
import { parentPort, isMainThread } from "node:worker_threads";
import adapter from "${packageName}";
if (!isMainThread && parentPort) {
parentPort.on("message", async (msg) => {
const { id, action, data } = msg;
// 1. Handle CRUD Query Execution
if (action === "EXECUTE") {
try {
const result = await adapter.send(data);
parentPort.postMessage({
id,
status: true,
result
});
} catch (err) {
parentPort.postMessage({
id,
status: false,
error: err.message || err
});
}
}
// 2. Handle Organization Teardown
else if (action === "TEARDOWN_ORG") {
try {
const orgId = data?.organization_id;
if (orgId) {
// Re-emit process event inside the worker thread isolate.
// This triggers any internal DB connection pools or utils listening on 'orgDeleted'.
process.emit("orgDeleted", orgId);
// Call adapter explicit close connection method if available
if (typeof adapter.closeConnection === "function") {
await adapter.closeConnection(orgId);
}
}
parentPort.postMessage({
id,
status: true,
action: "TEARDOWN_ORG_COMPLETE",
organization_id: orgId
});
} catch (err) {
console.error("Error tearing down org in worker isolate:", err);
parentPort.postMessage({
id,
status: false,
error: err.message || err
});
}
}
});
}
export default adapter;
`;
}
/**
* Creates a Webpack configuration object for bundling a worker entry point.
*
* @param {string} entryPath - Path to the generated entry file
* @param {string} provider - Storage provider identifier
* @param {string} outputDir - Target output directory
* @returns {Object} Webpack configuration object
*/
function createWebpackConfig(entryPath, provider, outputDir) {
return {
target: "node",
mode: "production",
devtool: "source-map",
entry: {
[provider]: entryPath
},
output: {
path: outputDir,
filename: "[name].bundle.js",
library: {
type: "module"
},
globalObject: "this"
},
experiments: {
outputModule: true
},
node: {
__dirname: false,
__filename: false
},
plugins: [
// Ignore optional native drivers and cloud dependencies to avoid build warnings
new webpack.IgnorePlugin({
resourceRegExp: /^(kerberos|@mongodb-js\/zstd|@aws-sdk\/credential-providers|gcp-metadata|snappy|socks|mongodb-client-encryption|aws4)$/
})
],
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: "esbuild-loader",
options: {
loader: "js",
target: "node18"
}
}
}
]
},
optimization: {
minimize: true,
minimizer: [
new EsbuildPlugin({
target: "node18"
})
],
splitChunks: false,
runtimeChunk: false
}
};
}
/**
* Orchestrates the full build process for all registered database adapters.
*/
export async function buildAll() {
const tempDir = path.resolve(__dirname, "../.tmp-entries");
const distDir = path.resolve(__dirname, "../dist");
// Ensure output directories exist
await fs.mkdir(tempDir, { recursive: true });
await fs.mkdir(distDir, { recursive: true });
console.log("==================================================");
console.log("[@cocreate/storage-bundler] Starting Bundle Pipeline...");
console.log("==================================================");
for (const item of STORAGE_PROVIDERS) {
const { provider, package: pkgName } = item;
console.log(`\n[Building Provider] "${provider}" from package "${pkgName}"`);
// 1. Generate dynamic wrapper script file
const entryCode = generateWorkerWrapperCode(pkgName);
const entryPath = path.join(tempDir, `${provider}.entry.js`);
await fs.writeFile(entryPath, entryCode, "utf8");
// 2. Build via Webpack
const webpackConfig = createWebpackConfig(entryPath, provider, distDir);
await new Promise((resolve, reject) => {
webpack(webpackConfig, (err, stats) => {
if (err) return reject(err);
if (stats.hasErrors()) {
const info = stats.toJson();
return reject(new Error(info.errors.map((e) => e.message).join("\n")));
}
console.log(`[Success] Bundled "${provider}.bundle.js" into dist/`);
resolve();
});
});
}
// Clean up temporary generated entry files
await fs.rm(tempDir, { recursive: true, force: true });
console.log("\n==================================================");
console.log("[@cocreate/storage-bundler] All Bundles Generated Successfully!");
console.log("==================================================");
}
// Execute when invoked directly
if (import.meta.url === `file://${process.argv[1]}`) {
buildAll().catch((err) => {
console.error("[@cocreate/storage-bundler] Build Failed:", err);
process.exit(1);
});
}