@squadbase/vite-server 0.1.19-dev.a00d9c3 → 0.1.19-dev.f16bde6

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.
@@ -0,0 +1,18 @@
1
+ // Server build entry used by `squadbasePlugin` (see vite-plugin.ts).
2
+ //
3
+ // It re-exports the vite-server app as default (so @hono/vite-build's injected
4
+ // `serve({ fetch: app.fetch })` works), and statically imports the project's
5
+ // TypeScript server-logic handlers via the `virtual:squadbase-server-logics`
6
+ // module (generated by the plugin from `server-logic/*.json`). Because the
7
+ // handlers are compiled into THIS bundle, they share the server's module graph
8
+ // — in particular hono's `contextStorage` AsyncLocalStorage — so `getContext()`
9
+ // works. This replaces runtime jiti transpilation.
10
+ //
11
+ // Registration goes through `@squadbase/vite-server/main` (not `/`) because
12
+ // `./main` and `./` are separate bundle entry points with separate module state.
13
+ import app, { registerBundledHandlers } from "@squadbase/vite-server/main";
14
+ import { HANDLERS } from "virtual:squadbase-server-logics";
15
+
16
+ registerBundledHandlers(HANDLERS);
17
+
18
+ export default app;
package/dist/index.d.ts CHANGED
@@ -1,6 +1,13 @@
1
1
  import * as hono_types from 'hono/types';
2
+ import * as hono from 'hono';
2
3
  import { Hono } from 'hono';
3
4
 
5
+ /**
6
+ * Called once at startup by the build-generated server entry with the map of
7
+ * bundled TypeScript handlers. See `vite-plugin.ts`.
8
+ */
9
+ declare function registerBundledHandlers(handlers: Record<string, (c: hono.Context) => Promise<unknown>>): void;
10
+
4
11
  interface ConnectionEntry {
5
12
  connector: {
6
13
  slug: string;
@@ -283,4 +290,4 @@ declare const storage: {
283
290
 
284
291
  declare const app: Hono<hono_types.BlankEnv, hono_types.BlankSchema, "/">;
285
292
 
286
- export { type AirtableClient, type AirtableRecord, type ConnectionEntry, type ConnectionFetchOptions, type ConnectionsMap, type DbtClient, type GoogleAnalyticsClient, type KintoneClient, type PresignedGetResult, PresignedHttpsTransport, type PresignedPutResult, type QueryFn, type GetResult as StorageGetResult, type StorageListItem, type ListOptions as StorageListOptions, type StorageListResult, type StorageMetadata, type PutOptions as StoragePutOptions, type StorageTransport, StorageUploadsClient, type StorageUsageDelta, type WixStoreClient, connection, createAirtableClient, createDbtClient, createGoogleAnalyticsClient, createKintoneClient, createWixStoreClient, app as default, getQuery, loadConnections, storage };
293
+ export { type AirtableClient, type AirtableRecord, type ConnectionEntry, type ConnectionFetchOptions, type ConnectionsMap, type DbtClient, type GoogleAnalyticsClient, type KintoneClient, type PresignedGetResult, PresignedHttpsTransport, type PresignedPutResult, type QueryFn, type GetResult as StorageGetResult, type StorageListItem, type ListOptions as StorageListOptions, type StorageListResult, type StorageMetadata, type PutOptions as StoragePutOptions, type StorageTransport, StorageUploadsClient, type StorageUsageDelta, type WixStoreClient, connection, createAirtableClient, createDbtClient, createGoogleAnalyticsClient, createKintoneClient, createWixStoreClient, app as default, getQuery, loadConnections, registerBundledHandlers, storage };
package/dist/index.js CHANGED
@@ -47707,31 +47707,29 @@ function validateHandlerPath(dirPath, handlerPath) {
47707
47707
  }
47708
47708
  return absolute;
47709
47709
  }
47710
- var jitiInstance = null;
47711
- var jitiModuleCache = /* @__PURE__ */ new Map();
47712
- async function loadTsHandlerWithJiti(absolutePath) {
47713
- const cached = jitiModuleCache.get(absolutePath);
47714
- if (cached) return cached;
47715
- if (!jitiInstance) {
47716
- const { createJiti } = await import("jiti");
47717
- jitiInstance = createJiti(import.meta.url, { fsCache: false });
47718
- }
47719
- const mod = await jitiInstance.import(absolutePath);
47720
- jitiModuleCache.set(absolutePath, mod);
47721
- return mod;
47722
- }
47723
- async function loadTypeScriptHandler(absolutePath) {
47724
- let mod;
47710
+ var bundledHandlers = {};
47711
+ function registerBundledHandlers(handlers) {
47712
+ bundledHandlers = handlers;
47713
+ }
47714
+ async function loadTypeScriptHandler(slug, absolutePath) {
47725
47715
  if (viteServer) {
47726
47716
  const module = viteServer.moduleGraph.getModuleById(absolutePath);
47727
47717
  if (module) viteServer.moduleGraph.invalidateModule(module);
47728
- mod = await viteServer.ssrLoadModule(absolutePath);
47729
- } else {
47730
- mod = await loadTsHandlerWithJiti(absolutePath);
47718
+ const mod = await viteServer.ssrLoadModule(absolutePath);
47719
+ const handler2 = mod.default;
47720
+ if (typeof handler2 !== "function") {
47721
+ throw new Error(`Handler must export a default function: ${absolutePath}`);
47722
+ }
47723
+ return handler2;
47724
+ }
47725
+ if (!(slug in bundledHandlers)) {
47726
+ throw new Error(
47727
+ `Bundled handler not found for server logic: ${slug} (not compiled into the build)`
47728
+ );
47731
47729
  }
47732
- const handler = mod.default;
47730
+ const handler = bundledHandlers[slug];
47733
47731
  if (typeof handler !== "function") {
47734
- throw new Error(`Handler must export a default function: ${absolutePath}`);
47732
+ throw new Error(`Server logic '${slug}' must export a default function`);
47735
47733
  }
47736
47734
  return handler;
47737
47735
  }
@@ -48028,7 +48026,7 @@ app.get("/:slug", async (c) => {
48028
48026
  }
48029
48027
  try {
48030
48028
  if (ds._isTypescript && ds._tsHandlerPath) {
48031
- const handler = await loadTypeScriptHandler(ds._tsHandlerPath);
48029
+ const handler = await loadTypeScriptHandler(slug, ds._tsHandlerPath);
48032
48030
  const result = await handler(c);
48033
48031
  return result;
48034
48032
  } else {
@@ -48056,7 +48054,7 @@ app.post("/:slug", async (c) => {
48056
48054
  const ttl = cacheConfig?.ttl ?? 0;
48057
48055
  if (ttl <= 0) {
48058
48056
  if (ds._isTypescript && ds._tsHandlerPath) {
48059
- const handler = await loadTypeScriptHandler(ds._tsHandlerPath);
48057
+ const handler = await loadTypeScriptHandler(slug, ds._tsHandlerPath);
48060
48058
  const result = await handler(c);
48061
48059
  return result;
48062
48060
  } else {
@@ -48080,7 +48078,7 @@ app.post("/:slug", async (c) => {
48080
48078
  void (async () => {
48081
48079
  try {
48082
48080
  if (ds._isTypescript && ds._tsHandlerPath) {
48083
- const tsHandler = await loadTypeScriptHandler(ds._tsHandlerPath);
48081
+ const tsHandler = await loadTypeScriptHandler(slug, ds._tsHandlerPath);
48084
48082
  const freshResponse = await tsHandler(c);
48085
48083
  const outcome = await cacheTypescriptResponse(freshResponse);
48086
48084
  if (outcome.cacheable) {
@@ -48104,7 +48102,7 @@ app.post("/:slug", async (c) => {
48104
48102
  }
48105
48103
  }
48106
48104
  if (ds._isTypescript && ds._tsHandlerPath) {
48107
- const handler = await loadTypeScriptHandler(ds._tsHandlerPath);
48105
+ const handler = await loadTypeScriptHandler(slug, ds._tsHandlerPath);
48108
48106
  const response = await handler(c);
48109
48107
  const outcome = await cacheTypescriptResponse(response);
48110
48108
  if (outcome.cacheable) {
@@ -48568,5 +48566,6 @@ export {
48568
48566
  src_default as default,
48569
48567
  getQuery,
48570
48568
  loadConnections,
48569
+ registerBundledHandlers,
48571
48570
  storage
48572
48571
  };
package/dist/main.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import app from './index.js';
2
+ export { registerBundledHandlers } from './index.js';
2
3
  import 'hono/types';
3
4
  import 'hono';
4
5
 
package/dist/main.js CHANGED
@@ -47440,31 +47440,29 @@ function validateHandlerPath(dirPath, handlerPath) {
47440
47440
  }
47441
47441
  return absolute;
47442
47442
  }
47443
- var jitiInstance = null;
47444
- var jitiModuleCache = /* @__PURE__ */ new Map();
47445
- async function loadTsHandlerWithJiti(absolutePath) {
47446
- const cached = jitiModuleCache.get(absolutePath);
47447
- if (cached) return cached;
47448
- if (!jitiInstance) {
47449
- const { createJiti } = await import("jiti");
47450
- jitiInstance = createJiti(import.meta.url, { fsCache: false });
47451
- }
47452
- const mod = await jitiInstance.import(absolutePath);
47453
- jitiModuleCache.set(absolutePath, mod);
47454
- return mod;
47455
- }
47456
- async function loadTypeScriptHandler(absolutePath) {
47457
- let mod;
47443
+ var bundledHandlers = {};
47444
+ function registerBundledHandlers(handlers) {
47445
+ bundledHandlers = handlers;
47446
+ }
47447
+ async function loadTypeScriptHandler(slug, absolutePath) {
47458
47448
  if (viteServer) {
47459
47449
  const module = viteServer.moduleGraph.getModuleById(absolutePath);
47460
47450
  if (module) viteServer.moduleGraph.invalidateModule(module);
47461
- mod = await viteServer.ssrLoadModule(absolutePath);
47462
- } else {
47463
- mod = await loadTsHandlerWithJiti(absolutePath);
47451
+ const mod = await viteServer.ssrLoadModule(absolutePath);
47452
+ const handler2 = mod.default;
47453
+ if (typeof handler2 !== "function") {
47454
+ throw new Error(`Handler must export a default function: ${absolutePath}`);
47455
+ }
47456
+ return handler2;
47457
+ }
47458
+ if (!(slug in bundledHandlers)) {
47459
+ throw new Error(
47460
+ `Bundled handler not found for server logic: ${slug} (not compiled into the build)`
47461
+ );
47464
47462
  }
47465
- const handler = mod.default;
47463
+ const handler = bundledHandlers[slug];
47466
47464
  if (typeof handler !== "function") {
47467
- throw new Error(`Handler must export a default function: ${absolutePath}`);
47465
+ throw new Error(`Server logic '${slug}' must export a default function`);
47468
47466
  }
47469
47467
  return handler;
47470
47468
  }
@@ -47761,7 +47759,7 @@ app.get("/:slug", async (c) => {
47761
47759
  }
47762
47760
  try {
47763
47761
  if (ds._isTypescript && ds._tsHandlerPath) {
47764
- const handler = await loadTypeScriptHandler(ds._tsHandlerPath);
47762
+ const handler = await loadTypeScriptHandler(slug, ds._tsHandlerPath);
47765
47763
  const result = await handler(c);
47766
47764
  return result;
47767
47765
  } else {
@@ -47789,7 +47787,7 @@ app.post("/:slug", async (c) => {
47789
47787
  const ttl = cacheConfig?.ttl ?? 0;
47790
47788
  if (ttl <= 0) {
47791
47789
  if (ds._isTypescript && ds._tsHandlerPath) {
47792
- const handler = await loadTypeScriptHandler(ds._tsHandlerPath);
47790
+ const handler = await loadTypeScriptHandler(slug, ds._tsHandlerPath);
47793
47791
  const result = await handler(c);
47794
47792
  return result;
47795
47793
  } else {
@@ -47813,7 +47811,7 @@ app.post("/:slug", async (c) => {
47813
47811
  void (async () => {
47814
47812
  try {
47815
47813
  if (ds._isTypescript && ds._tsHandlerPath) {
47816
- const tsHandler = await loadTypeScriptHandler(ds._tsHandlerPath);
47814
+ const tsHandler = await loadTypeScriptHandler(slug, ds._tsHandlerPath);
47817
47815
  const freshResponse = await tsHandler(c);
47818
47816
  const outcome = await cacheTypescriptResponse(freshResponse);
47819
47817
  if (outcome.cacheable) {
@@ -47837,7 +47835,7 @@ app.post("/:slug", async (c) => {
47837
47835
  }
47838
47836
  }
47839
47837
  if (ds._isTypescript && ds._tsHandlerPath) {
47840
- const handler = await loadTypeScriptHandler(ds._tsHandlerPath);
47838
+ const handler = await loadTypeScriptHandler(slug, ds._tsHandlerPath);
47841
47839
  const response = await handler(c);
47842
47840
  const outcome = await cacheTypescriptResponse(response);
47843
47841
  if (outcome.cacheable) {
@@ -48242,5 +48240,6 @@ src_default.get("*", (c) => {
48242
48240
  });
48243
48241
  var main_default = src_default;
48244
48242
  export {
48245
- main_default as default
48243
+ main_default as default,
48244
+ registerBundledHandlers
48246
48245
  };
@@ -1,7 +1,6 @@
1
1
  import { Plugin } from 'vite';
2
2
 
3
3
  interface SquadbasePluginOptions {
4
- buildEntry?: string;
5
4
  devEntry?: string;
6
5
  external?: string[];
7
6
  exclude?: RegExp[];
@@ -622,6 +622,7 @@ import devServer from "@hono/vite-dev-server";
622
622
  import nodeAdapter from "@hono/vite-dev-server/node";
623
623
  import { fileURLToPath } from "url";
624
624
  import path3 from "path";
625
+ import fs from "fs";
625
626
 
626
627
  // src/registry.ts
627
628
  import { readdir, readFile as readFile2, mkdir } from "fs/promises";
@@ -47421,6 +47422,23 @@ var viteServer = null;
47421
47422
  function setViteServer(server) {
47422
47423
  viteServer = server;
47423
47424
  }
47425
+ function validateHandlerPath(dirPath, handlerPath) {
47426
+ const normalizedDir = path2.resolve(dirPath);
47427
+ const dirBase = path2.basename(normalizedDir);
47428
+ let normalized = handlerPath.replace(/^\.?[/\\]/, "");
47429
+ const prefix = dirBase + "/";
47430
+ if (normalized.startsWith(prefix)) {
47431
+ normalized = normalized.slice(prefix.length);
47432
+ }
47433
+ const absolute = path2.resolve(dirPath, normalized);
47434
+ if (!absolute.startsWith(normalizedDir + path2.sep)) {
47435
+ throw new Error(`Handler path escapes server-logic directory: ${handlerPath}`);
47436
+ }
47437
+ if (!absolute.endsWith(".ts")) {
47438
+ throw new Error(`Handler must be a .ts file: ${handlerPath}`);
47439
+ }
47440
+ return absolute;
47441
+ }
47424
47442
  var defaultServerLogicDir = path2.join(process.cwd(), "server-logic");
47425
47443
 
47426
47444
  // src/vite-plugin.ts
@@ -47435,20 +47453,17 @@ var DEFAULT_EXCLUDE = [
47435
47453
  /^\/node_modules\/.*/,
47436
47454
  /^(?!\/api)/
47437
47455
  ];
47438
- function resolveEntry(entry) {
47439
- if (entry.startsWith(".") || entry.startsWith("/")) return entry;
47440
- try {
47441
- const resolvedUrl = import.meta.resolve(entry);
47442
- const absolutePath = fileURLToPath(resolvedUrl);
47443
- const relativePath = path3.relative(process.cwd(), absolutePath).replace(/\\/g, "/");
47444
- return "./" + relativePath;
47445
- } catch {
47446
- return entry;
47447
- }
47456
+ var SERVER_LOGICS_VIRTUAL = "virtual:squadbase-server-logics";
47457
+ var SERVER_LOGICS_RESOLVED = "\0" + SERVER_LOGICS_VIRTUAL;
47458
+ function resolveServerBuildEntry() {
47459
+ const absolute = fileURLToPath(new URL("../build-entry/server-entry.mjs", import.meta.url));
47460
+ return "./" + path3.relative(process.cwd(), absolute).replace(/\\/g, "/");
47461
+ }
47462
+ function resolveServerLogicDir() {
47463
+ return process.env.SERVER_LOGIC_DIR || process.env.DATA_SOURCE_DIR || path3.resolve(process.cwd(), "server-logic");
47448
47464
  }
47449
47465
  function squadbasePlugin(options = {}) {
47450
47466
  const {
47451
- buildEntry = "@squadbase/vite-server/main",
47452
47467
  devEntry = "@squadbase/vite-server",
47453
47468
  external = [
47454
47469
  "pg",
@@ -47460,16 +47475,68 @@ function squadbasePlugin(options = {}) {
47460
47475
  "@aws-sdk/client-cost-explorer",
47461
47476
  "@aws-sdk/client-redshift-data",
47462
47477
  "@google-analytics/data",
47463
- "@kintone/rest-api-client",
47464
- // jiti はランタイムで server-logic/*.ts をトランスパイルするため、
47465
- // @hono/vite-build がサーバービルドにバンドルしないよう external にする。
47466
- "jiti"
47478
+ "@kintone/rest-api-client"
47467
47479
  ],
47468
47480
  exclude = DEFAULT_EXCLUDE
47469
47481
  } = options;
47470
47482
  const isServerBuild = (_, { command, mode }) => command === "build" && mode !== "client";
47483
+ const serverLogicBarrelPlugin = {
47484
+ name: "squadbase-server-logic-barrel",
47485
+ apply: isServerBuild,
47486
+ resolveId(id) {
47487
+ if (id === SERVER_LOGICS_VIRTUAL) return SERVER_LOGICS_RESOLVED;
47488
+ return null;
47489
+ },
47490
+ load(id) {
47491
+ if (id !== SERVER_LOGICS_RESOLVED) return null;
47492
+ const dir = resolveServerLogicDir();
47493
+ let jsonFiles = [];
47494
+ try {
47495
+ jsonFiles = fs.readdirSync(dir).filter((f) => f.endsWith(".json"));
47496
+ } catch {
47497
+ }
47498
+ let imports = "";
47499
+ let entries = "";
47500
+ jsonFiles.forEach((file, i) => {
47501
+ const slug = file.replace(/\.json$/, "");
47502
+ let raw;
47503
+ try {
47504
+ raw = JSON.parse(fs.readFileSync(path3.join(dir, file), "utf-8"));
47505
+ } catch {
47506
+ console.warn(`[squadbase] skipping ${file}: invalid JSON`);
47507
+ return;
47508
+ }
47509
+ const parsed = anyJsonServerLogicSchema.safeParse(raw);
47510
+ if (!parsed.success) return;
47511
+ const def = parsed.data;
47512
+ if (def.type !== "typescript") return;
47513
+ let handlerAbs;
47514
+ try {
47515
+ handlerAbs = validateHandlerPath(dir, def.handlerPath);
47516
+ } catch (e) {
47517
+ console.warn(
47518
+ `[squadbase] skipping server logic '${slug}': ${e.message}`
47519
+ );
47520
+ return;
47521
+ }
47522
+ if (!fs.existsSync(handlerAbs)) {
47523
+ console.warn(
47524
+ `[squadbase] skipping server logic '${slug}': handler file not found (${def.handlerPath})`
47525
+ );
47526
+ return;
47527
+ }
47528
+ imports += `import h${i} from ${JSON.stringify(handlerAbs)};
47529
+ `;
47530
+ entries += ` ${JSON.stringify(slug)}: h${i},
47531
+ `;
47532
+ });
47533
+ return `${imports}export const HANDLERS = {
47534
+ ${entries}};
47535
+ `;
47536
+ }
47537
+ };
47471
47538
  const rawBuildPlugin = buildPlugin({
47472
- entry: resolveEntry(buildEntry),
47539
+ entry: resolveServerBuildEntry(),
47473
47540
  outputDir: "./dist/server",
47474
47541
  output: "index.js",
47475
47542
  external
@@ -47492,7 +47559,7 @@ function squadbasePlugin(options = {}) {
47492
47559
  setViteServer(server);
47493
47560
  }
47494
47561
  };
47495
- return [...buildPlugins, ...devPlugins, viteServerInjectionPlugin];
47562
+ return [serverLogicBarrelPlugin, ...buildPlugins, ...devPlugins, viteServerInjectionPlugin];
47496
47563
  }
47497
47564
  export {
47498
47565
  squadbasePlugin
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@squadbase/vite-server",
3
- "version": "0.1.19-dev.a00d9c3",
3
+ "version": "0.1.19-dev.f16bde6",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": {
@@ -325,7 +325,8 @@
325
325
  }
326
326
  },
327
327
  "files": [
328
- "dist"
328
+ "dist",
329
+ "build-entry"
329
330
  ],
330
331
  "publishConfig": {
331
332
  "access": "public",
@@ -355,7 +356,6 @@
355
356
  "@kintone/rest-api-client": "^5.5.0",
356
357
  "google-auth-library": "^9.15.1",
357
358
  "hono": "^4.12.25",
358
- "jiti": "^2.7.0",
359
359
  "mongodb": "^7.1.1",
360
360
  "mssql": "^11.0.1",
361
361
  "mysql2": "^3.11.0",