acrylblends 0.7.3 → 0.7.5
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/dist/index.js +5 -1
- package/dist/index.js.map +1 -1
- package/docs/framework/README.md +6 -4
- package/docs/framework/UPSTREAM.md +1 -1
- package/docs/framework/getting-started/aws-lambda.md +115 -0
- package/docs/framework/getting-started/bun.md +1 -3
- package/docs/framework/getting-started/cloudflare-pages.md +53 -0
- package/docs/framework/getting-started/cloudflare-workers.md +67 -0
- package/docs/framework/getting-started/deploy.md +5 -5
- package/docs/framework/getting-started/lambda-edge.md +41 -0
- package/docs/framework/getting-started/netlify.md +25 -0
- package/docs/framework/getting-started/other-runtimes.md +20 -9
- package/docs/framework/getting-started/service-worker.md +33 -0
- package/docs/framework/getting-started/vercel.md +28 -0
- package/docs/framework/guides/create-acrylblends.md +40 -2
- package/docs/framework/guides/data.md +202 -0
- package/docs/framework/guides/faq.md +2 -2
- package/docs/framework/guides/forms-and-sessions.md +34 -30
- package/docs/framework/helpers/conninfo.md +1 -1
- package/docs/framework/helpers/ssg.md +1 -1
- package/docs/framework/helpers/streaming.md +3 -2
- package/docs/framework/helpers/websocket.md +2 -2
- package/docs/framework/middleware/builtin/ip-restriction.md +1 -1
- package/docs/framework/upstream.json +3 -3
- package/lib/aws-lambda.d.ts +324 -0
- package/lib/aws-lambda.js +357 -0
- package/lib/bun.d.ts +75 -0
- package/lib/bun.js +134 -0
- package/lib/cloudflare-pages.d.ts +58 -0
- package/lib/cloudflare-pages.js +78 -0
- package/lib/cloudflare-workers.d.ts +21 -0
- package/lib/cloudflare-workers.js +80 -0
- package/lib/db/sqlite.d.ts +27 -0
- package/lib/db/sqlite.js +197 -0
- package/lib/db.d.ts +2 -0
- package/lib/db.js +2 -0
- package/lib/index-CseujaYI.d.ts +31 -0
- package/lib/index-D4FAsBZu.d.ts +85 -0
- package/lib/index-DaJtL7ci.d.ts +156 -0
- package/lib/lambda-edge.d.ts +92 -0
- package/lib/lambda-edge.js +105 -0
- package/lib/netlify.d.ts +29 -0
- package/lib/netlify.js +30 -0
- package/lib/node-server/register.js +1 -1
- package/lib/node-server/serve-static.js +1 -1
- package/lib/node-server.js +3 -3
- package/lib/serve-static-Cp5adzGo.js +77 -0
- package/lib/serve-static.d.ts +1 -30
- package/lib/serve-static.js +1 -76
- package/lib/service-worker.d.ts +48 -0
- package/lib/service-worker.js +38 -0
- package/lib/src-MWcsMS8i.js +85 -0
- package/lib/ssg.d.ts +1 -155
- package/lib/vercel.d.ts +10 -0
- package/lib/vercel.js +9 -0
- package/package.json +41 -1
- package/plugins/acryl-db/LICENSE +47 -0
- package/plugins/acryl-db/plugin.json +12 -0
- package/plugins/acryl-db/src/cordis.ts +13 -0
- package/plugins/acryl-db/src/facade/database.ts +41 -0
- package/plugins/acryl-db/src/facade/migrations.ts +50 -0
- package/plugins/acryl-db/src/facade/schema.ts +9 -0
- package/plugins/acryl-db/src/index.ts +19 -0
- package/plugins/acryl-db/src/migration-files.ts +31 -0
- package/plugins/acryl-db/upstream.json +18 -0
- package/plugins/acryl-db-sqlite/LICENSE +47 -0
- package/plugins/acryl-db-sqlite/plugin.json +13 -0
- package/plugins/acryl-db-sqlite/src/facade/node.ts +199 -0
- package/plugins/acryl-db-sqlite/src/index.ts +88 -0
- package/plugins/acryl-db-sqlite/src/open.ts +61 -0
- package/plugins/acryl-db-sqlite/upstream.json +21 -0
- package/plugins/acryl-inspect/upstream.json +1 -1
- package/plugins/acryl-node-server/upstream.json +1 -1
- package/plugins/acryl-router/src/core/adapter/aws-lambda/conninfo.ts +73 -0
- package/plugins/acryl-router/src/core/adapter/aws-lambda/handler.ts +682 -0
- package/plugins/acryl-router/src/core/adapter/aws-lambda/index.ts +14 -0
- package/plugins/acryl-router/src/core/adapter/aws-lambda/types.ts +173 -0
- package/plugins/acryl-router/src/core/adapter/bun/conninfo.ts +43 -0
- package/plugins/acryl-router/src/core/adapter/bun/index.ts +11 -0
- package/plugins/acryl-router/src/core/adapter/bun/serve-static.ts +32 -0
- package/plugins/acryl-router/src/core/adapter/bun/server.ts +14 -0
- package/plugins/acryl-router/src/core/adapter/bun/ssg.ts +26 -0
- package/plugins/acryl-router/src/core/adapter/bun/websocket.ts +113 -0
- package/plugins/acryl-router/src/core/adapter/cloudflare-pages/conninfo.ts +26 -0
- package/plugins/acryl-router/src/core/adapter/cloudflare-pages/handler.ts +122 -0
- package/plugins/acryl-router/src/core/adapter/cloudflare-pages/index.ts +11 -0
- package/plugins/acryl-router/src/core/adapter/cloudflare-workers/conninfo.ts +7 -0
- package/plugins/acryl-router/src/core/adapter/cloudflare-workers/index.ts +8 -0
- package/plugins/acryl-router/src/core/adapter/cloudflare-workers/serve-static-module.ts +12 -0
- package/plugins/acryl-router/src/core/adapter/cloudflare-workers/serve-static.ts +41 -0
- package/plugins/acryl-router/src/core/adapter/cloudflare-workers/utils.ts +50 -0
- package/plugins/acryl-router/src/core/adapter/cloudflare-workers/websocket.ts +52 -0
- package/plugins/acryl-router/src/core/adapter/lambda-edge/conninfo.ts +15 -0
- package/plugins/acryl-router/src/core/adapter/lambda-edge/handler.ts +239 -0
- package/plugins/acryl-router/src/core/adapter/lambda-edge/index.ts +14 -0
- package/plugins/acryl-router/src/core/adapter/netlify/conninfo.ts +57 -0
- package/plugins/acryl-router/src/core/adapter/netlify/handler.ts +10 -0
- package/plugins/acryl-router/src/core/adapter/netlify/index.ts +6 -0
- package/plugins/acryl-router/src/core/adapter/netlify/mod.ts +2 -0
- package/plugins/acryl-router/src/core/adapter/service-worker/handler.ts +37 -0
- package/plugins/acryl-router/src/core/adapter/service-worker/index.ts +36 -0
- package/plugins/acryl-router/src/core/adapter/service-worker/types.ts +14 -0
- package/plugins/acryl-router/src/core/adapter/vercel/conninfo.ts +8 -0
- package/plugins/acryl-router/src/core/adapter/vercel/handler.ts +8 -0
- package/plugins/acryl-router/src/core/adapter/vercel/index.ts +7 -0
- package/plugins/acryl-router/upstream.json +2 -2
- package/plugins/acryl-router-http/upstream.json +1 -1
- package/plugins/acryl-state/upstream.json +1 -1
- package/plugins/desktop/upstream.json +1 -1
- package/plugins/desktop-web/main.ts +13 -3
- package/plugins/desktop-web/upstream.json +1 -1
- package/plugins/tsconfig.json +1 -0
- package/templates/todo/upstream.json +1 -1
- package/lib/{serve-static-B14MF1Va.js → serve-static-B849HlX2.js} +1 -1
- package/lib/{transpile-CTnWof4z.js → transpile-QUK9b3eG.js} +1 -1
package/lib/bun.d.ts
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { L as MiddlewareHandler, t as Context, v as Env } from "./context-DS3fYyG-.js";
|
|
2
|
+
import { r as GetConnInfo } from "./types-Dv_nV2cv.js";
|
|
3
|
+
import { f as FileSystemModule, m as ToSSGAdaptorInterface } from "./index-DaJtL7ci.js";
|
|
4
|
+
import { a as WSEvents, n as UpgradeWebSocket } from "./index-BvFh1Gfx.js";
|
|
5
|
+
import { t as ServeStaticOptions } from "./index-CseujaYI.js";
|
|
6
|
+
|
|
7
|
+
//#region ../../plugins/acryl-router/src/core/adapter/bun/serve-static.d.ts
|
|
8
|
+
declare const serveStatic: <E extends Env = Env>(options?: ServeStaticOptions<E>) => MiddlewareHandler;
|
|
9
|
+
//#endregion
|
|
10
|
+
//#region ../../plugins/acryl-router/src/core/adapter/bun/ssg.d.ts
|
|
11
|
+
/**
|
|
12
|
+
* @experimental
|
|
13
|
+
* `bunFileSystemModule` is an experimental feature.
|
|
14
|
+
* The API might be changed.
|
|
15
|
+
*/
|
|
16
|
+
declare const bunFileSystemModule: FileSystemModule;
|
|
17
|
+
/**
|
|
18
|
+
* @experimental
|
|
19
|
+
* `toSSG` is an experimental feature.
|
|
20
|
+
* The API might be changed.
|
|
21
|
+
*/
|
|
22
|
+
declare const toSSG: ToSSGAdaptorInterface;
|
|
23
|
+
//#endregion
|
|
24
|
+
//#region ../../plugins/acryl-router/src/core/adapter/bun/websocket.d.ts
|
|
25
|
+
/**
|
|
26
|
+
* @internal
|
|
27
|
+
*/
|
|
28
|
+
interface BunServerWebSocket<T> {
|
|
29
|
+
send(data: string | ArrayBuffer | Uint8Array, compress?: boolean): void;
|
|
30
|
+
close(code?: number, reason?: string): void;
|
|
31
|
+
data: T;
|
|
32
|
+
readyState: 0 | 1 | 2 | 3;
|
|
33
|
+
}
|
|
34
|
+
interface BunWebSocketHandler<T> {
|
|
35
|
+
open(ws: BunServerWebSocket<T>): void;
|
|
36
|
+
close(ws: BunServerWebSocket<T>, code?: number, reason?: string): void;
|
|
37
|
+
message(ws: BunServerWebSocket<T>, message: string | {
|
|
38
|
+
buffer: ArrayBufferLike;
|
|
39
|
+
}): void;
|
|
40
|
+
}
|
|
41
|
+
interface CreateWebSocket<T> {
|
|
42
|
+
upgradeWebSocket: UpgradeWebSocket<T>;
|
|
43
|
+
websocket: BunWebSocketHandler<BunWebSocketData>;
|
|
44
|
+
}
|
|
45
|
+
interface BunWebSocketData {
|
|
46
|
+
events: WSEvents;
|
|
47
|
+
url: URL;
|
|
48
|
+
protocol: string;
|
|
49
|
+
}
|
|
50
|
+
declare const upgradeWebSocket: UpgradeWebSocket<any>;
|
|
51
|
+
declare const websocket: BunWebSocketHandler<BunWebSocketData>;
|
|
52
|
+
/**
|
|
53
|
+
* @deprecated Import `upgradeWebSocket` and `websocket` directly from `acryl-router/bun` instead.
|
|
54
|
+
* @returns A function to create a Bun WebSocket handler.
|
|
55
|
+
*/
|
|
56
|
+
declare const createBunWebSocket: <T>() => CreateWebSocket<T>;
|
|
57
|
+
//#endregion
|
|
58
|
+
//#region ../../plugins/acryl-router/src/core/adapter/bun/conninfo.d.ts
|
|
59
|
+
/**
|
|
60
|
+
* Get ConnInfo with Bun
|
|
61
|
+
* @param c Context
|
|
62
|
+
* @returns ConnInfo
|
|
63
|
+
*/
|
|
64
|
+
declare const getConnInfo: GetConnInfo;
|
|
65
|
+
//#endregion
|
|
66
|
+
//#region ../../plugins/acryl-router/src/core/adapter/bun/server.d.ts
|
|
67
|
+
/**
|
|
68
|
+
* Get Bun Server Object from Context
|
|
69
|
+
* @template T - The type of Bun Server
|
|
70
|
+
* @param c Context
|
|
71
|
+
* @returns Bun Server
|
|
72
|
+
*/
|
|
73
|
+
declare const getBunServer: <T>(c: Context) => T | undefined;
|
|
74
|
+
//#endregion
|
|
75
|
+
export { type BunWebSocketData, type BunWebSocketHandler, bunFileSystemModule, createBunWebSocket, getBunServer, getConnInfo, serveStatic, toSSG, upgradeWebSocket, websocket };
|
package/lib/bun.js
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import { t as serveStatic$1 } from "./serve-static-Cp5adzGo.js";
|
|
2
|
+
import { c as toSSG$1 } from "./ssg-BwjcA-Ge.js";
|
|
3
|
+
import { WSContext, createWSMessageEvent, defineWebSocketHelper } from "./ws.js";
|
|
4
|
+
import { stat } from "node:fs/promises";
|
|
5
|
+
import { join } from "node:path";
|
|
6
|
+
//#region ../../plugins/acryl-router/src/core/adapter/bun/serve-static.ts
|
|
7
|
+
const serveStatic = (options = {}) => {
|
|
8
|
+
return async function serveStatic(c, next) {
|
|
9
|
+
const getContent = async (path) => {
|
|
10
|
+
const file = Bun.file(path);
|
|
11
|
+
return await file.exists() ? file : null;
|
|
12
|
+
};
|
|
13
|
+
const isDir = async (path) => {
|
|
14
|
+
let isDir;
|
|
15
|
+
try {
|
|
16
|
+
isDir = (await stat(path)).isDirectory();
|
|
17
|
+
} catch {}
|
|
18
|
+
return isDir;
|
|
19
|
+
};
|
|
20
|
+
return serveStatic$1({
|
|
21
|
+
...options,
|
|
22
|
+
getContent,
|
|
23
|
+
join,
|
|
24
|
+
isDir
|
|
25
|
+
})(c, next);
|
|
26
|
+
};
|
|
27
|
+
};
|
|
28
|
+
//#endregion
|
|
29
|
+
//#region ../../plugins/acryl-router/src/core/adapter/bun/ssg.ts
|
|
30
|
+
/**
|
|
31
|
+
* @experimental
|
|
32
|
+
* `bunFileSystemModule` is an experimental feature.
|
|
33
|
+
* The API might be changed.
|
|
34
|
+
*/
|
|
35
|
+
const bunFileSystemModule = {
|
|
36
|
+
writeFile: async (path, data) => {
|
|
37
|
+
await Bun.write(path, data);
|
|
38
|
+
},
|
|
39
|
+
mkdir: async () => {}
|
|
40
|
+
};
|
|
41
|
+
/**
|
|
42
|
+
* @experimental
|
|
43
|
+
* `toSSG` is an experimental feature.
|
|
44
|
+
* The API might be changed.
|
|
45
|
+
*/
|
|
46
|
+
const toSSG = async (app, options) => {
|
|
47
|
+
return toSSG$1(app, bunFileSystemModule, options);
|
|
48
|
+
};
|
|
49
|
+
//#endregion
|
|
50
|
+
//#region ../../plugins/acryl-router/src/core/adapter/bun/server.ts
|
|
51
|
+
/**
|
|
52
|
+
* Get Bun Server Object from Context
|
|
53
|
+
* @template T - The type of Bun Server
|
|
54
|
+
* @param c Context
|
|
55
|
+
* @returns Bun Server
|
|
56
|
+
*/
|
|
57
|
+
const getBunServer = (c) => "server" in c.env ? c.env.server : c.env;
|
|
58
|
+
//#endregion
|
|
59
|
+
//#region ../../plugins/acryl-router/src/core/adapter/bun/websocket.ts
|
|
60
|
+
/**
|
|
61
|
+
* @internal
|
|
62
|
+
*/
|
|
63
|
+
const createWSContext = (ws) => {
|
|
64
|
+
return new WSContext({
|
|
65
|
+
send: (source, options) => {
|
|
66
|
+
ws.send(source, options?.compress);
|
|
67
|
+
},
|
|
68
|
+
raw: ws,
|
|
69
|
+
readyState: ws.readyState,
|
|
70
|
+
url: ws.data.url,
|
|
71
|
+
protocol: ws.data.protocol,
|
|
72
|
+
close(code, reason) {
|
|
73
|
+
ws.close(code, reason);
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
};
|
|
77
|
+
const upgradeWebSocket = defineWebSocketHelper((c, events) => {
|
|
78
|
+
const server = getBunServer(c);
|
|
79
|
+
if (!server) throw new TypeError("env has to include the 2nd argument of fetch.");
|
|
80
|
+
if (server.upgrade(c.req.raw, { data: {
|
|
81
|
+
events,
|
|
82
|
+
url: new URL(c.req.url),
|
|
83
|
+
protocol: c.req.header("sec-websocket-protocol")?.split(",")[0]?.trim() ?? ""
|
|
84
|
+
} })) return new Response(null);
|
|
85
|
+
});
|
|
86
|
+
const websocket = {
|
|
87
|
+
open(ws) {
|
|
88
|
+
const websocketListeners = ws.data.events;
|
|
89
|
+
if (websocketListeners.onOpen) websocketListeners.onOpen(new Event("open"), createWSContext(ws));
|
|
90
|
+
},
|
|
91
|
+
close(ws, code, reason) {
|
|
92
|
+
const websocketListeners = ws.data.events;
|
|
93
|
+
if (websocketListeners.onClose) websocketListeners.onClose(new CloseEvent("close", {
|
|
94
|
+
code,
|
|
95
|
+
reason
|
|
96
|
+
}), createWSContext(ws));
|
|
97
|
+
},
|
|
98
|
+
message(ws, message) {
|
|
99
|
+
const websocketListeners = ws.data.events;
|
|
100
|
+
if (websocketListeners.onMessage) {
|
|
101
|
+
const normalizedReceiveData = typeof message === "string" ? message : message.buffer;
|
|
102
|
+
websocketListeners.onMessage(createWSMessageEvent(normalizedReceiveData), createWSContext(ws));
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
};
|
|
106
|
+
/**
|
|
107
|
+
* @deprecated Import `upgradeWebSocket` and `websocket` directly from `acryl-router/bun` instead.
|
|
108
|
+
* @returns A function to create a Bun WebSocket handler.
|
|
109
|
+
*/
|
|
110
|
+
const createBunWebSocket = () => ({
|
|
111
|
+
upgradeWebSocket,
|
|
112
|
+
websocket
|
|
113
|
+
});
|
|
114
|
+
//#endregion
|
|
115
|
+
//#region ../../plugins/acryl-router/src/core/adapter/bun/conninfo.ts
|
|
116
|
+
/**
|
|
117
|
+
* Get ConnInfo with Bun
|
|
118
|
+
* @param c Context
|
|
119
|
+
* @returns ConnInfo
|
|
120
|
+
*/
|
|
121
|
+
const getConnInfo = (c) => {
|
|
122
|
+
const server = getBunServer(c);
|
|
123
|
+
if (!server) throw new TypeError("env has to include the 2nd argument of fetch.");
|
|
124
|
+
if (typeof server.requestIP !== "function") throw new TypeError("server.requestIP is not a function.");
|
|
125
|
+
const info = server.requestIP(c.req.raw);
|
|
126
|
+
if (!info) return { remote: {} };
|
|
127
|
+
return { remote: {
|
|
128
|
+
address: info.address,
|
|
129
|
+
addressType: info.family === "IPv6" || info.family === "IPv4" ? info.family : void 0,
|
|
130
|
+
port: info.port
|
|
131
|
+
} };
|
|
132
|
+
};
|
|
133
|
+
//#endregion
|
|
134
|
+
export { bunFileSystemModule, createBunWebSocket, getBunServer, getConnInfo, serveStatic, toSSG, upgradeWebSocket, websocket };
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { L as MiddlewareHandler, Y as Schema, g as BlankSchema, j as Input, v as Env } from "./context-DS3fYyG-.js";
|
|
2
|
+
import { r as GetConnInfo } from "./types-Dv_nV2cv.js";
|
|
3
|
+
import { t as Acryl } from "./app-D8riatvv.js";
|
|
4
|
+
|
|
5
|
+
//#region ../../plugins/acryl-router/src/core/adapter/cloudflare-pages/handler.d.ts
|
|
6
|
+
type Params<P extends string = any> = Record<P, string | string[]>;
|
|
7
|
+
type EventContext<Env = {}, P extends string = any, Data = Record<string, unknown>> = {
|
|
8
|
+
request: Request;
|
|
9
|
+
functionPath: string;
|
|
10
|
+
waitUntil: (promise: Promise<unknown>) => void;
|
|
11
|
+
passThroughOnException: () => void;
|
|
12
|
+
props: any;
|
|
13
|
+
next: (input?: Request | string, init?: RequestInit) => Promise<Response>;
|
|
14
|
+
env: Env & {
|
|
15
|
+
ASSETS: {
|
|
16
|
+
fetch: typeof fetch;
|
|
17
|
+
};
|
|
18
|
+
};
|
|
19
|
+
params: Params<P>;
|
|
20
|
+
data: Data;
|
|
21
|
+
};
|
|
22
|
+
declare type PagesFunction<Env = unknown, Params extends string = any, Data extends Record<string, unknown> = Record<string, unknown>> = (context: EventContext<Env, Params, Data>) => Response | Promise<Response>;
|
|
23
|
+
declare const handle: <E extends Env = Env, S extends Schema = BlankSchema, BasePath extends string = "/">(app: Acryl<E, S, BasePath>) => PagesFunction<E["Bindings"]>;
|
|
24
|
+
declare function handleMiddleware<E extends Env = {}, P extends string = any, I extends Input = {}>(middleware: MiddlewareHandler<E & {
|
|
25
|
+
Bindings: {
|
|
26
|
+
eventContext: EventContext;
|
|
27
|
+
};
|
|
28
|
+
}, P, I>): PagesFunction<E['Bindings']>;
|
|
29
|
+
/**
|
|
30
|
+
*
|
|
31
|
+
* @description `serveStatic()` is for advanced mode:
|
|
32
|
+
* https://developers.cloudflare.com/pages/platform/functions/advanced-mode/#set-up-a-function
|
|
33
|
+
*/
|
|
34
|
+
declare const serveStatic: () => MiddlewareHandler;
|
|
35
|
+
//#endregion
|
|
36
|
+
//#region ../../plugins/acryl-router/src/core/adapter/cloudflare-pages/conninfo.d.ts
|
|
37
|
+
/**
|
|
38
|
+
* Get connection information from Cloudflare Pages
|
|
39
|
+
* @param c - Context
|
|
40
|
+
* @returns Connection information including remote address
|
|
41
|
+
* @example
|
|
42
|
+
* ```ts
|
|
43
|
+
* import { Acryl } from 'acrylblends'
|
|
44
|
+
* import { handle, getConnInfo } from 'acrylblends/cloudflare-pages'
|
|
45
|
+
*
|
|
46
|
+
* const app = new Acryl()
|
|
47
|
+
*
|
|
48
|
+
* app.get('/', (c) => {
|
|
49
|
+
* const info = getConnInfo(c)
|
|
50
|
+
* return c.text(`Your IP: ${info.remote.address}`)
|
|
51
|
+
* })
|
|
52
|
+
*
|
|
53
|
+
* export const onRequest = handle(app)
|
|
54
|
+
* ```
|
|
55
|
+
*/
|
|
56
|
+
declare const getConnInfo: GetConnInfo;
|
|
57
|
+
//#endregion
|
|
58
|
+
export { type EventContext, getConnInfo, handle, handleMiddleware, serveStatic };
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { HTTPException } from "./http-exception.js";
|
|
2
|
+
import { t as Context } from "./context-DDO0cfuP.js";
|
|
3
|
+
//#region ../../plugins/acryl-router/src/core/adapter/cloudflare-pages/handler.ts
|
|
4
|
+
const handle = (app) => (eventContext) => {
|
|
5
|
+
return app.fetch(eventContext.request, {
|
|
6
|
+
...eventContext.env,
|
|
7
|
+
eventContext
|
|
8
|
+
}, {
|
|
9
|
+
waitUntil: eventContext.waitUntil,
|
|
10
|
+
passThroughOnException: eventContext.passThroughOnException,
|
|
11
|
+
props: {}
|
|
12
|
+
});
|
|
13
|
+
};
|
|
14
|
+
function handleMiddleware(middleware) {
|
|
15
|
+
return async (executionCtx) => {
|
|
16
|
+
const context = new Context(executionCtx.request, {
|
|
17
|
+
env: {
|
|
18
|
+
...executionCtx.env,
|
|
19
|
+
eventContext: executionCtx
|
|
20
|
+
},
|
|
21
|
+
executionCtx
|
|
22
|
+
});
|
|
23
|
+
let response = void 0;
|
|
24
|
+
try {
|
|
25
|
+
response = await middleware(context, async () => {
|
|
26
|
+
try {
|
|
27
|
+
context.res = await executionCtx.next();
|
|
28
|
+
} catch (error) {
|
|
29
|
+
if (error instanceof Error) context.error = error;
|
|
30
|
+
else throw error;
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
} catch (error) {
|
|
34
|
+
if (error instanceof Error) context.error = error;
|
|
35
|
+
else throw error;
|
|
36
|
+
}
|
|
37
|
+
if (response) return response;
|
|
38
|
+
if (context.error instanceof HTTPException) return context.error.getResponse();
|
|
39
|
+
if (context.error) throw context.error;
|
|
40
|
+
return context.res;
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
*
|
|
45
|
+
* @description `serveStatic()` is for advanced mode:
|
|
46
|
+
* https://developers.cloudflare.com/pages/platform/functions/advanced-mode/#set-up-a-function
|
|
47
|
+
*/
|
|
48
|
+
const serveStatic = () => {
|
|
49
|
+
return async (c) => {
|
|
50
|
+
const res = await c.env.ASSETS.fetch(c.req.raw);
|
|
51
|
+
if (res.status === 404) return c.notFound();
|
|
52
|
+
return res;
|
|
53
|
+
};
|
|
54
|
+
};
|
|
55
|
+
//#endregion
|
|
56
|
+
//#region ../../plugins/acryl-router/src/core/adapter/cloudflare-pages/conninfo.ts
|
|
57
|
+
/**
|
|
58
|
+
* Get connection information from Cloudflare Pages
|
|
59
|
+
* @param c - Context
|
|
60
|
+
* @returns Connection information including remote address
|
|
61
|
+
* @example
|
|
62
|
+
* ```ts
|
|
63
|
+
* import { Acryl } from 'acrylblends'
|
|
64
|
+
* import { handle, getConnInfo } from 'acrylblends/cloudflare-pages'
|
|
65
|
+
*
|
|
66
|
+
* const app = new Acryl()
|
|
67
|
+
*
|
|
68
|
+
* app.get('/', (c) => {
|
|
69
|
+
* const info = getConnInfo(c)
|
|
70
|
+
* return c.text(`Your IP: ${info.remote.address}`)
|
|
71
|
+
* })
|
|
72
|
+
*
|
|
73
|
+
* export const onRequest = handle(app)
|
|
74
|
+
* ```
|
|
75
|
+
*/
|
|
76
|
+
const getConnInfo = (c) => ({ remote: { address: c.req.header("cf-connecting-ip") } });
|
|
77
|
+
//#endregion
|
|
78
|
+
export { getConnInfo, handle, handleMiddleware, serveStatic };
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { L as MiddlewareHandler, v as Env } from "./context-DS3fYyG-.js";
|
|
2
|
+
import { r as GetConnInfo } from "./types-Dv_nV2cv.js";
|
|
3
|
+
import { a as WSEvents, n as UpgradeWebSocket } from "./index-BvFh1Gfx.js";
|
|
4
|
+
import { t as ServeStaticOptions$1 } from "./index-CseujaYI.js";
|
|
5
|
+
|
|
6
|
+
//#region ../../plugins/acryl-router/src/core/adapter/cloudflare-workers/serve-static.d.ts
|
|
7
|
+
type ServeStaticOptions<E extends Env = Env> = ServeStaticOptions$1<E> & {
|
|
8
|
+
namespace?: unknown;
|
|
9
|
+
manifest?: object | string;
|
|
10
|
+
};
|
|
11
|
+
//#endregion
|
|
12
|
+
//#region ../../plugins/acryl-router/src/core/adapter/cloudflare-workers/serve-static-module.d.ts
|
|
13
|
+
declare const module: <E extends Env = Env>(options: Omit<ServeStaticOptions<E>, "namespace">) => MiddlewareHandler;
|
|
14
|
+
//#endregion
|
|
15
|
+
//#region ../../plugins/acryl-router/src/core/adapter/cloudflare-workers/websocket.d.ts
|
|
16
|
+
declare const upgradeWebSocket: UpgradeWebSocket<WebSocket, any, Omit<WSEvents<WebSocket>, 'onOpen'>>;
|
|
17
|
+
//#endregion
|
|
18
|
+
//#region ../../plugins/acryl-router/src/core/adapter/cloudflare-workers/conninfo.d.ts
|
|
19
|
+
declare const getConnInfo: GetConnInfo;
|
|
20
|
+
//#endregion
|
|
21
|
+
export { getConnInfo, module as serveStatic, upgradeWebSocket };
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { t as serveStatic$1 } from "./serve-static-Cp5adzGo.js";
|
|
2
|
+
import { WSContext, defineWebSocketHelper } from "./ws.js";
|
|
3
|
+
//#region ../../plugins/acryl-router/src/core/adapter/cloudflare-workers/utils.ts
|
|
4
|
+
const getContentFromKVAsset = async (path, options) => {
|
|
5
|
+
let ASSET_MANIFEST;
|
|
6
|
+
if (options && options.manifest) if (typeof options.manifest === "string") ASSET_MANIFEST = JSON.parse(options.manifest);
|
|
7
|
+
else ASSET_MANIFEST = options.manifest;
|
|
8
|
+
else if (typeof __STATIC_CONTENT_MANIFEST === "string") ASSET_MANIFEST = JSON.parse(__STATIC_CONTENT_MANIFEST);
|
|
9
|
+
else ASSET_MANIFEST = __STATIC_CONTENT_MANIFEST;
|
|
10
|
+
let ASSET_NAMESPACE;
|
|
11
|
+
if (options && options.namespace) ASSET_NAMESPACE = options.namespace;
|
|
12
|
+
else ASSET_NAMESPACE = __STATIC_CONTENT;
|
|
13
|
+
const key = ASSET_MANIFEST[path];
|
|
14
|
+
if (!key) return null;
|
|
15
|
+
const content = await ASSET_NAMESPACE.get(key, { type: "stream" });
|
|
16
|
+
if (!content) return null;
|
|
17
|
+
return content;
|
|
18
|
+
};
|
|
19
|
+
//#endregion
|
|
20
|
+
//#region ../../plugins/acryl-router/src/core/adapter/cloudflare-workers/serve-static.ts
|
|
21
|
+
/**
|
|
22
|
+
* @deprecated
|
|
23
|
+
* `serveStatic` in the Cloudflare Workers adapter is deprecated.
|
|
24
|
+
* You can serve static files directly using Cloudflare Static Assets.
|
|
25
|
+
* @see https://developers.cloudflare.com/workers/static-assets/
|
|
26
|
+
* Cloudflare Static Assets is currently in open beta. If this doesn't work for you,
|
|
27
|
+
* please consider using Cloudflare Pages.
|
|
28
|
+
*/
|
|
29
|
+
const serveStatic = (options = {}) => {
|
|
30
|
+
return async function serveStatic(c, next) {
|
|
31
|
+
const getContent = async (path) => {
|
|
32
|
+
return getContentFromKVAsset(path, {
|
|
33
|
+
manifest: options.manifest,
|
|
34
|
+
namespace: options.namespace ? options.namespace : c.env ? c.env.__STATIC_CONTENT : void 0
|
|
35
|
+
});
|
|
36
|
+
};
|
|
37
|
+
return serveStatic$1({
|
|
38
|
+
...options,
|
|
39
|
+
getContent
|
|
40
|
+
})(c, next);
|
|
41
|
+
};
|
|
42
|
+
};
|
|
43
|
+
//#endregion
|
|
44
|
+
//#region ../../plugins/acryl-router/src/core/adapter/cloudflare-workers/serve-static-module.ts
|
|
45
|
+
const module = (options) => {
|
|
46
|
+
return serveStatic(options);
|
|
47
|
+
};
|
|
48
|
+
//#endregion
|
|
49
|
+
//#region ../../plugins/acryl-router/src/core/adapter/cloudflare-workers/websocket.ts
|
|
50
|
+
const upgradeWebSocket = defineWebSocketHelper(async (c, events) => {
|
|
51
|
+
if (c.req.header("Upgrade") !== "websocket") return;
|
|
52
|
+
const webSocketPair = new WebSocketPair();
|
|
53
|
+
const client = webSocketPair[0];
|
|
54
|
+
const server = webSocketPair[1];
|
|
55
|
+
const wsContext = new WSContext({
|
|
56
|
+
close: (code, reason) => server.close(code, reason),
|
|
57
|
+
get protocol() {
|
|
58
|
+
return server.protocol;
|
|
59
|
+
},
|
|
60
|
+
raw: server,
|
|
61
|
+
get readyState() {
|
|
62
|
+
return server.readyState;
|
|
63
|
+
},
|
|
64
|
+
url: server.url ? new URL(server.url) : null,
|
|
65
|
+
send: (source) => server.send(source)
|
|
66
|
+
});
|
|
67
|
+
if (events.onClose) server.addEventListener("close", (evt) => events.onClose?.(evt, wsContext));
|
|
68
|
+
if (events.onMessage) server.addEventListener("message", (evt) => events.onMessage?.(evt, wsContext));
|
|
69
|
+
if (events.onError) server.addEventListener("error", (evt) => events.onError?.(evt, wsContext));
|
|
70
|
+
server.accept?.();
|
|
71
|
+
return new Response(null, {
|
|
72
|
+
status: 101,
|
|
73
|
+
webSocket: client
|
|
74
|
+
});
|
|
75
|
+
});
|
|
76
|
+
//#endregion
|
|
77
|
+
//#region ../../plugins/acryl-router/src/core/adapter/cloudflare-workers/conninfo.ts
|
|
78
|
+
const getConnInfo = (c) => ({ remote: { address: c.req.header("cf-connecting-ip") } });
|
|
79
|
+
//#endregion
|
|
80
|
+
export { getConnInfo, module as serveStatic, upgradeWebSocket };
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { c as DbExecutor, l as SqlValue, s as Db } from "../index-D4FAsBZu.js";
|
|
2
|
+
|
|
3
|
+
//#region ../../plugins/acryl-db-sqlite/src/open.d.ts
|
|
4
|
+
interface OpenDbOptions {
|
|
5
|
+
/** The database file, its folder created if missing; `:memory:` for a database that lives until `close`. */
|
|
6
|
+
readonly path: string;
|
|
7
|
+
/** A folder of `NNN-name.sql` files numbered from 1, as a path or a file URL, applied in order before `openDb` resolves. */
|
|
8
|
+
readonly migrations?: string | URL;
|
|
9
|
+
/** How long SQLite waits for a competing file lock, in milliseconds. Defaults to 5000. */
|
|
10
|
+
readonly busyTimeoutMs?: number;
|
|
11
|
+
/** The WAL auto-checkpoint threshold in pages; 0 disables it. Defaults to 1000. */
|
|
12
|
+
readonly walAutoCheckpointPages?: number;
|
|
13
|
+
}
|
|
14
|
+
/** An open database: the port, and the one function that closes it. */
|
|
15
|
+
interface OpenDb {
|
|
16
|
+
readonly db: Db;
|
|
17
|
+
/** Waits for running operations and transactions, checkpoints the WAL and releases the file. Calling it again does nothing. */
|
|
18
|
+
close(): Promise<void>;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Open, configure and migrate a SQLite database.
|
|
22
|
+
* @throws when the migration files are misnamed or misnumbered, or a migration fails (the file is closed, and unchanged), or the
|
|
23
|
+
* database's schema is newer than the migrations.
|
|
24
|
+
*/
|
|
25
|
+
declare function openDb(options: OpenDbOptions): Promise<OpenDb>;
|
|
26
|
+
//#endregion
|
|
27
|
+
export { type Db, type DbExecutor, OpenDb, OpenDbOptions, type SqlValue, openDb };
|
package/lib/db/sqlite.js
ADDED
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
import { i as applyMigrations, n as MIGRATIONS_TABLE, r as readMigrationFiles } from "../src-MWcsMS8i.js";
|
|
2
|
+
import { mkdir } from "node:fs/promises";
|
|
3
|
+
import { dirname } from "node:path";
|
|
4
|
+
import { DatabaseSync } from "node:sqlite";
|
|
5
|
+
//#region ../../plugins/acryl-db-sqlite/src/facade/node.ts
|
|
6
|
+
const DEFAULT_WAL_AUTO_CHECKPOINT_PAGES = 1e3;
|
|
7
|
+
const DEFAULT_BUSY_TIMEOUT_MS = 5e3;
|
|
8
|
+
const ignore = () => {};
|
|
9
|
+
/**
|
|
10
|
+
* Runs operations in call order. An operation starts immediately when nothing is running or waiting;
|
|
11
|
+
* otherwise it waits for everything before it. An asynchronous operation holds the queue until it settles.
|
|
12
|
+
*/
|
|
13
|
+
var SerialOperationQueue = class {
|
|
14
|
+
tail = Promise.resolve();
|
|
15
|
+
pending = 0;
|
|
16
|
+
run(operation) {
|
|
17
|
+
if (this.pending > 0) return this.enqueue(operation);
|
|
18
|
+
try {
|
|
19
|
+
return Promise.resolve(operation());
|
|
20
|
+
} catch (error) {
|
|
21
|
+
return Promise.reject(error);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
runAsync(operation) {
|
|
25
|
+
if (this.pending > 0) return this.enqueue(operation);
|
|
26
|
+
this.pending++;
|
|
27
|
+
const { promise: barrier, resolve: releaseBarrier } = Promise.withResolvers();
|
|
28
|
+
this.tail = barrier;
|
|
29
|
+
let started;
|
|
30
|
+
try {
|
|
31
|
+
started = operation();
|
|
32
|
+
} catch (error) {
|
|
33
|
+
started = Promise.reject(error);
|
|
34
|
+
}
|
|
35
|
+
return started.finally(() => {
|
|
36
|
+
this.pending--;
|
|
37
|
+
releaseBarrier();
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
enqueue(operation) {
|
|
41
|
+
this.pending++;
|
|
42
|
+
return this.release(this.tail.then(operation));
|
|
43
|
+
}
|
|
44
|
+
release(operation) {
|
|
45
|
+
const settled = operation.finally(() => {
|
|
46
|
+
this.pending--;
|
|
47
|
+
});
|
|
48
|
+
this.tail = settled.then(ignore, ignore);
|
|
49
|
+
return settled;
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
/**
|
|
53
|
+
* Executes SQL on one connection. Prepared statements are cached per connection by SQL text, so the
|
|
54
|
+
* database and its transaction handles share them across transactions.
|
|
55
|
+
*/
|
|
56
|
+
var NodeSqliteExecutor = class {
|
|
57
|
+
database;
|
|
58
|
+
statements;
|
|
59
|
+
constructor(database, statements) {
|
|
60
|
+
this.database = database;
|
|
61
|
+
this.statements = statements;
|
|
62
|
+
}
|
|
63
|
+
exec(sql) {
|
|
64
|
+
return this.runOperation(() => {
|
|
65
|
+
this.database.exec(sql);
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
run(sql, ...params) {
|
|
69
|
+
return this.runOperation(() => ({ changes: Number(this.statement(sql).run(...params).changes) }));
|
|
70
|
+
}
|
|
71
|
+
get(sql, ...params) {
|
|
72
|
+
return this.runOperation(() => this.statement(sql).get(...params));
|
|
73
|
+
}
|
|
74
|
+
all(sql, ...params) {
|
|
75
|
+
return this.runOperation(() => this.statement(sql).all(...params));
|
|
76
|
+
}
|
|
77
|
+
statement(sql) {
|
|
78
|
+
let statement = this.statements.get(sql);
|
|
79
|
+
if (statement === void 0) {
|
|
80
|
+
statement = this.database.prepare(sql);
|
|
81
|
+
this.statements.set(sql, statement);
|
|
82
|
+
}
|
|
83
|
+
return statement;
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
var NodeSqliteTransaction = class extends NodeSqliteExecutor {
|
|
87
|
+
scope;
|
|
88
|
+
constructor(database, statements, scope) {
|
|
89
|
+
super(database, statements);
|
|
90
|
+
this.scope = scope;
|
|
91
|
+
}
|
|
92
|
+
async runOperation(operation) {
|
|
93
|
+
if (!this.scope.active) throw new Error("SQLite transaction handle is no longer active");
|
|
94
|
+
return operation();
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
/** `Db` adapter backed by Node's built-in `node:sqlite`. */
|
|
98
|
+
var NodeSqliteDatabase = class extends NodeSqliteExecutor {
|
|
99
|
+
access = new SerialOperationQueue();
|
|
100
|
+
closed = false;
|
|
101
|
+
constructor(database) {
|
|
102
|
+
super(database, /* @__PURE__ */ new Map());
|
|
103
|
+
}
|
|
104
|
+
transaction(callback) {
|
|
105
|
+
return this.access.runAsync(async () => {
|
|
106
|
+
this.database.exec("BEGIN IMMEDIATE");
|
|
107
|
+
const scope = { active: true };
|
|
108
|
+
try {
|
|
109
|
+
const result = await callback(new NodeSqliteTransaction(this.database, this.statements, scope));
|
|
110
|
+
scope.active = false;
|
|
111
|
+
this.database.exec("COMMIT");
|
|
112
|
+
return result;
|
|
113
|
+
} catch (error) {
|
|
114
|
+
scope.active = false;
|
|
115
|
+
try {
|
|
116
|
+
this.database.exec("ROLLBACK");
|
|
117
|
+
} catch (rollbackError) {
|
|
118
|
+
throw new AggregateError([error, rollbackError], "SQLite transaction failed and rollback failed");
|
|
119
|
+
}
|
|
120
|
+
throw error;
|
|
121
|
+
}
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
close() {
|
|
125
|
+
return this.access.run(() => {
|
|
126
|
+
if (this.closed) return;
|
|
127
|
+
this.closed = true;
|
|
128
|
+
this.statements.clear();
|
|
129
|
+
try {
|
|
130
|
+
this.database.exec("PRAGMA wal_checkpoint(TRUNCATE)");
|
|
131
|
+
} finally {
|
|
132
|
+
this.database.close();
|
|
133
|
+
}
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
runOperation(operation) {
|
|
137
|
+
return this.access.run(operation);
|
|
138
|
+
}
|
|
139
|
+
};
|
|
140
|
+
/** Open and configure a Node-backed SQLite database facade. */
|
|
141
|
+
async function openNodeSqliteDatabase(path, options = {}) {
|
|
142
|
+
const checkpointPages = options.walAutoCheckpointPages ?? DEFAULT_WAL_AUTO_CHECKPOINT_PAGES;
|
|
143
|
+
const timeout = options.busyTimeoutMs ?? DEFAULT_BUSY_TIMEOUT_MS;
|
|
144
|
+
if (path !== ":memory:") await mkdir(dirname(path), { recursive: true });
|
|
145
|
+
const adapter = new NodeSqliteDatabase(new DatabaseSync(path, { timeout }));
|
|
146
|
+
try {
|
|
147
|
+
await adapter.exec("PRAGMA journal_mode = WAL");
|
|
148
|
+
await adapter.exec("PRAGMA synchronous = NORMAL");
|
|
149
|
+
await adapter.exec(`PRAGMA wal_autocheckpoint = ${checkpointPages}`);
|
|
150
|
+
return adapter;
|
|
151
|
+
} catch (error) {
|
|
152
|
+
try {
|
|
153
|
+
await adapter.close();
|
|
154
|
+
} catch {}
|
|
155
|
+
throw error;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
//#endregion
|
|
159
|
+
//#region ../../plugins/acryl-db-sqlite/src/open.ts
|
|
160
|
+
/**
|
|
161
|
+
* The plain way to open the SQLite provider, for code without Cordis (an acrylblends app): `openDb({ path, migrations })` opens one
|
|
162
|
+
* `node:sqlite` file with the ported facade's settings (WAL, `synchronous = NORMAL`, a busy timeout) and foreign keys enforced, applies the
|
|
163
|
+
* numbered migrations, and resolves the `db` port with the function that closes it. The Cordis plugin is a thin wrapper over this, so both
|
|
164
|
+
* run one code path.
|
|
165
|
+
* This entry imports nothing from Cordis.
|
|
166
|
+
*/
|
|
167
|
+
/**
|
|
168
|
+
* Open, configure and migrate a SQLite database.
|
|
169
|
+
* @throws when the migration files are misnamed or misnumbered, or a migration fails (the file is closed, and unchanged), or the
|
|
170
|
+
* database's schema is newer than the migrations.
|
|
171
|
+
*/
|
|
172
|
+
async function openDb(options) {
|
|
173
|
+
const migrations = options.migrations === void 0 ? [] : await readMigrationFiles(options.migrations);
|
|
174
|
+
const database = await openNodeSqliteDatabase(options.path, {
|
|
175
|
+
...options.busyTimeoutMs === void 0 ? {} : { busyTimeoutMs: options.busyTimeoutMs },
|
|
176
|
+
...options.walAutoCheckpointPages === void 0 ? {} : { walAutoCheckpointPages: options.walAutoCheckpointPages }
|
|
177
|
+
});
|
|
178
|
+
try {
|
|
179
|
+
await database.exec("PRAGMA foreign_keys = ON");
|
|
180
|
+
await applyMigrations(database, migrations, { table: MIGRATIONS_TABLE });
|
|
181
|
+
} catch (error) {
|
|
182
|
+
await database.close();
|
|
183
|
+
throw error;
|
|
184
|
+
}
|
|
185
|
+
return {
|
|
186
|
+
db: {
|
|
187
|
+
exec: (sql) => database.exec(sql),
|
|
188
|
+
run: (sql, ...params) => database.run(sql, ...params),
|
|
189
|
+
get: (sql, ...params) => database.get(sql, ...params),
|
|
190
|
+
all: (sql, ...params) => database.all(sql, ...params),
|
|
191
|
+
transaction: (work) => database.transaction(work)
|
|
192
|
+
},
|
|
193
|
+
close: () => database.close()
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
//#endregion
|
|
197
|
+
export { openDb };
|
package/lib/db.d.ts
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import { a as Migration, c as DbExecutor, i as validateSchemaPrefix, l as SqlValue, n as MIGRATIONS_TABLE, o as applyMigrations, r as readMigrationFiles, s as Db, t as DB_SERVICE } from "./index-D4FAsBZu.js";
|
|
2
|
+
export { DB_SERVICE, type Db, type DbExecutor, MIGRATIONS_TABLE, type Migration, type SqlValue, applyMigrations, readMigrationFiles, validateSchemaPrefix };
|
package/lib/db.js
ADDED