@supacloud/function-adapter 0.2.0
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 +42 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.js +20 -0
- package/dist/sveltekit-adapter.d.ts +6 -0
- package/dist/sveltekit-adapter.js +67 -0
- package/dist/sveltekit.d.ts +15 -0
- package/dist/sveltekit.js +19 -0
- package/package.json +49 -0
package/README.md
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
# @supacloud/function-adapter
|
|
2
|
+
|
|
3
|
+
Fetch-based adapters for SupaCloud Edge Functions.
|
|
4
|
+
|
|
5
|
+
The adapter returns a default-exportable object with `fetch(request)` and
|
|
6
|
+
explicit SupaCloud metadata. It never listens on a port, starts a process, or
|
|
7
|
+
serves static assets.
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
import { createSupaCloudHandler } from "@supacloud/function-adapter";
|
|
11
|
+
|
|
12
|
+
export default createSupaCloudHandler(
|
|
13
|
+
(request) => Response.json({ path: new URL(request.url).pathname }),
|
|
14
|
+
"hono",
|
|
15
|
+
);
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
For SvelteKit API-only builds, wrap the generated server `respond` function:
|
|
19
|
+
|
|
20
|
+
```ts
|
|
21
|
+
import { createSvelteKitHandler } from "@supacloud/function-adapter/sveltekit";
|
|
22
|
+
|
|
23
|
+
export default createSvelteKitHandler({ respond: server.respond.bind(server) });
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
Or configure the build adapter directly:
|
|
27
|
+
|
|
28
|
+
```js
|
|
29
|
+
import adapter from "@supacloud/function-adapter/sveltekit-adapter";
|
|
30
|
+
|
|
31
|
+
export default {
|
|
32
|
+
kit: { adapter: adapter() },
|
|
33
|
+
};
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
The SvelteKit adapter rejects page routes, prerendered output, static assets,
|
|
37
|
+
server asset reads, and instrumentation. API routes and `hooks.server` remain
|
|
38
|
+
supported.
|
|
39
|
+
|
|
40
|
+
Use the `sveltekit-function` profile only for Fetch-compatible server routes.
|
|
41
|
+
Complete SvelteKit SSR applications should continue to use SupaCloud Frontend
|
|
42
|
+
Hosting with an adapter-node process.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export type SupaCloudFunctionFramework = "fetch" | "elysia" | "hono" | "sveltekit-function";
|
|
2
|
+
export type SupaCloudFunctionHandler = (request: Request) => Response | Promise<Response>;
|
|
3
|
+
export type SupaCloudFunctionExport = {
|
|
4
|
+
fetch: SupaCloudFunctionHandler;
|
|
5
|
+
__supacloud: {
|
|
6
|
+
framework: SupaCloudFunctionFramework;
|
|
7
|
+
routeAware: true;
|
|
8
|
+
};
|
|
9
|
+
};
|
|
10
|
+
/** Mark a Fetch handler as a route-aware SupaCloud Function adapter. */
|
|
11
|
+
export declare function createSupaCloudHandler(handler: SupaCloudFunctionHandler, framework?: SupaCloudFunctionFramework): SupaCloudFunctionExport;
|
|
12
|
+
export { createSvelteKitHandler } from "./sveltekit";
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
// src/sveltekit.ts
|
|
3
|
+
function createSvelteKitHandler(options) {
|
|
4
|
+
return createSupaCloudHandler((request) => options.respond(request, {
|
|
5
|
+
platform: options.platform,
|
|
6
|
+
getClientAddress: () => options.getClientAddress?.(request) ?? "0.0.0.0"
|
|
7
|
+
}), "sveltekit-function");
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
// src/index.ts
|
|
11
|
+
function createSupaCloudHandler(handler, framework = "fetch") {
|
|
12
|
+
return {
|
|
13
|
+
fetch: handler,
|
|
14
|
+
__supacloud: { framework, routeAware: true }
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
export {
|
|
18
|
+
createSupaCloudHandler,
|
|
19
|
+
createSvelteKitHandler
|
|
20
|
+
};
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { Adapter } from "@sveltejs/kit";
|
|
2
|
+
export type SupaCloudSvelteKitAdapterOptions = {
|
|
3
|
+
out?: string;
|
|
4
|
+
};
|
|
5
|
+
/** Build an API-only SvelteKit app as a SupaCloud Fetch Function bundle. */
|
|
6
|
+
export default function adapter(options?: SupaCloudSvelteKitAdapterOptions): Adapter;
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
// src/sveltekit-adapter.ts
|
|
3
|
+
import { existsSync, readdirSync, writeFileSync } from "fs";
|
|
4
|
+
function routeList(routes) {
|
|
5
|
+
return routes.map((route) => ` - ${route.id}`).join(`
|
|
6
|
+
`);
|
|
7
|
+
}
|
|
8
|
+
function directoryHasEntries(directory) {
|
|
9
|
+
return existsSync(directory) && readdirSync(directory).length > 0;
|
|
10
|
+
}
|
|
11
|
+
function adapter(options = {}) {
|
|
12
|
+
const out = options.out ?? "build";
|
|
13
|
+
return {
|
|
14
|
+
name: "@supacloud/function-adapter/sveltekit-adapter",
|
|
15
|
+
adapt(builder) {
|
|
16
|
+
const pageRoutes = builder.routes.filter((route) => route.page.methods.length > 0);
|
|
17
|
+
if (pageRoutes.length > 0) {
|
|
18
|
+
throw new Error(`sveltekit-function supports API routes only. Deploy page routes with framework=sveltekit:
|
|
19
|
+
${routeList(pageRoutes)}`);
|
|
20
|
+
}
|
|
21
|
+
if (builder.prerendered.paths.length > 0) {
|
|
22
|
+
throw new Error("sveltekit-function does not serve prerendered or static output");
|
|
23
|
+
}
|
|
24
|
+
if (directoryHasEntries(builder.config.kit.files.assets)) {
|
|
25
|
+
throw new Error("sveltekit-function does not serve static assets; deploy the application with SvelteKit Hosting");
|
|
26
|
+
}
|
|
27
|
+
const serverAssets = builder.findServerAssets(builder.routes);
|
|
28
|
+
if (serverAssets.length > 0) {
|
|
29
|
+
throw new Error("sveltekit-function does not support server asset reads; deploy the application with SvelteKit Hosting");
|
|
30
|
+
}
|
|
31
|
+
builder.rimraf(out);
|
|
32
|
+
builder.mkdirp(out);
|
|
33
|
+
builder.writeServer(`${out}/server`);
|
|
34
|
+
writeFileSync(`${out}/manifest.js`, `export const manifest = ${builder.generateManifest({ relativePath: "./server" })};
|
|
35
|
+
`);
|
|
36
|
+
writeFileSync(`${out}/index.js`, `import { Server } from "./server/index.js";
|
|
37
|
+
import { manifest } from "./manifest.js";
|
|
38
|
+
|
|
39
|
+
const server = new Server(manifest);
|
|
40
|
+
let initialized;
|
|
41
|
+
|
|
42
|
+
async function fetch(request) {
|
|
43
|
+
initialized ??= server.init({
|
|
44
|
+
env: typeof Deno !== "undefined" && Deno.env?.toObject ? Deno.env.toObject() : {},
|
|
45
|
+
});
|
|
46
|
+
await initialized;
|
|
47
|
+
return server.respond(request, {
|
|
48
|
+
getClientAddress: () => request.headers.get("x-forwarded-for")?.split(",", 1)[0]?.trim() || "0.0.0.0",
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export default {
|
|
53
|
+
fetch,
|
|
54
|
+
__supacloud: { framework: "sveltekit-function", routeAware: true },
|
|
55
|
+
};
|
|
56
|
+
`);
|
|
57
|
+
builder.log(`Wrote API-only SupaCloud Function bundle to "${out}"`);
|
|
58
|
+
},
|
|
59
|
+
supports: {
|
|
60
|
+
read: () => false,
|
|
61
|
+
instrumentation: () => false
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
export {
|
|
66
|
+
adapter as default
|
|
67
|
+
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { type SupaCloudFunctionExport } from "./index";
|
|
2
|
+
export type SvelteKitRespond = (request: Request, options: {
|
|
3
|
+
platform?: unknown;
|
|
4
|
+
getClientAddress?: () => string;
|
|
5
|
+
}) => Response | Promise<Response>;
|
|
6
|
+
export type SvelteKitHandlerOptions = {
|
|
7
|
+
respond: SvelteKitRespond;
|
|
8
|
+
platform?: unknown;
|
|
9
|
+
getClientAddress?: (request: Request) => string;
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
* Bridge a SvelteKit server `respond` function to SupaCloud's Fetch contract.
|
|
13
|
+
* This intentionally does not start a listener or serve static assets.
|
|
14
|
+
*/
|
|
15
|
+
export declare function createSvelteKitHandler(options: SvelteKitHandlerOptions): SupaCloudFunctionExport;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
// src/index.ts
|
|
3
|
+
function createSupaCloudHandler(handler, framework = "fetch") {
|
|
4
|
+
return {
|
|
5
|
+
fetch: handler,
|
|
6
|
+
__supacloud: { framework, routeAware: true }
|
|
7
|
+
};
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
// src/sveltekit.ts
|
|
11
|
+
function createSvelteKitHandler(options) {
|
|
12
|
+
return createSupaCloudHandler((request) => options.respond(request, {
|
|
13
|
+
platform: options.platform,
|
|
14
|
+
getClientAddress: () => options.getClientAddress?.(request) ?? "0.0.0.0"
|
|
15
|
+
}), "sveltekit-function");
|
|
16
|
+
}
|
|
17
|
+
export {
|
|
18
|
+
createSvelteKitHandler
|
|
19
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@supacloud/function-adapter",
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "Fetch-based adapters for SupaCloud Edge Functions",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.js"
|
|
12
|
+
},
|
|
13
|
+
"./sveltekit": {
|
|
14
|
+
"types": "./dist/sveltekit.d.ts",
|
|
15
|
+
"import": "./dist/sveltekit.js"
|
|
16
|
+
},
|
|
17
|
+
"./sveltekit-adapter": {
|
|
18
|
+
"types": "./dist/sveltekit-adapter.d.ts",
|
|
19
|
+
"import": "./dist/sveltekit-adapter.js"
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
"files": [
|
|
23
|
+
"dist",
|
|
24
|
+
"README.md"
|
|
25
|
+
],
|
|
26
|
+
"scripts": {
|
|
27
|
+
"prebuild": "bun -e \"import { rm } from 'node:fs/promises'; await rm('dist', { recursive: true, force: true })\"",
|
|
28
|
+
"build": "bun build src/index.ts --outfile dist/index.js --target bun --format esm && bun build src/sveltekit.ts --outfile dist/sveltekit.js --target bun --format esm && bun build src/sveltekit-adapter.ts --outfile dist/sveltekit-adapter.js --target bun --format esm && bun x tsc -p tsconfig.json --emitDeclarationOnly --declaration --outDir dist",
|
|
29
|
+
"typecheck": "bun x tsc --noEmit -p tsconfig.json",
|
|
30
|
+
"test": "bun test"
|
|
31
|
+
},
|
|
32
|
+
"license": "Apache-2.0",
|
|
33
|
+
"peerDependencies": {
|
|
34
|
+
"@sveltejs/kit": "^2.0.0"
|
|
35
|
+
},
|
|
36
|
+
"peerDependenciesMeta": {
|
|
37
|
+
"@sveltejs/kit": {
|
|
38
|
+
"optional": true
|
|
39
|
+
}
|
|
40
|
+
},
|
|
41
|
+
"devDependencies": {
|
|
42
|
+
"@sveltejs/kit": "^2.70.3",
|
|
43
|
+
"@sveltejs/vite-plugin-svelte": "^7.3.0",
|
|
44
|
+
"@types/bun": "^1.4.0",
|
|
45
|
+
"svelte": "^5.57.0",
|
|
46
|
+
"vite": "^8.2.2",
|
|
47
|
+
"typescript": "^7.0.2"
|
|
48
|
+
}
|
|
49
|
+
}
|