cooper-stack 0.1.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/dist/ai.d.ts +60 -0
- package/dist/ai.d.ts.map +1 -0
- package/dist/ai.js +66 -0
- package/dist/ai.js.map +1 -0
- package/dist/api.d.ts +31 -0
- package/dist/api.d.ts.map +1 -0
- package/dist/api.js +40 -0
- package/dist/api.js.map +1 -0
- package/dist/auth.d.ts +13 -0
- package/dist/auth.d.ts.map +1 -0
- package/dist/auth.js +16 -0
- package/dist/auth.js.map +1 -0
- package/dist/bridge.d.ts +15 -0
- package/dist/bridge.d.ts.map +1 -0
- package/dist/bridge.js +217 -0
- package/dist/bridge.js.map +1 -0
- package/dist/cache.d.ts +27 -0
- package/dist/cache.d.ts.map +1 -0
- package/dist/cache.js +69 -0
- package/dist/cache.js.map +1 -0
- package/dist/cron.d.ts +22 -0
- package/dist/cron.d.ts.map +1 -0
- package/dist/cron.js +26 -0
- package/dist/cron.js.map +1 -0
- package/dist/db.d.ts +46 -0
- package/dist/db.d.ts.map +1 -0
- package/dist/db.js +158 -0
- package/dist/db.js.map +1 -0
- package/dist/error.d.ts +18 -0
- package/dist/error.d.ts.map +1 -0
- package/dist/error.js +30 -0
- package/dist/error.js.map +1 -0
- package/dist/index.d.ts +18 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +18 -0
- package/dist/index.js.map +1 -0
- package/dist/islands.d.ts +16 -0
- package/dist/islands.d.ts.map +1 -0
- package/dist/islands.js +23 -0
- package/dist/islands.js.map +1 -0
- package/dist/middleware.d.ts +20 -0
- package/dist/middleware.d.ts.map +1 -0
- package/dist/middleware.js +26 -0
- package/dist/middleware.js.map +1 -0
- package/dist/nats.d.ts +46 -0
- package/dist/nats.d.ts.map +1 -0
- package/dist/nats.js +157 -0
- package/dist/nats.js.map +1 -0
- package/dist/pubsub.d.ts +27 -0
- package/dist/pubsub.d.ts.map +1 -0
- package/dist/pubsub.js +152 -0
- package/dist/pubsub.js.map +1 -0
- package/dist/queue.d.ts +39 -0
- package/dist/queue.d.ts.map +1 -0
- package/dist/queue.js +298 -0
- package/dist/queue.js.map +1 -0
- package/dist/rateLimit.d.ts +29 -0
- package/dist/rateLimit.d.ts.map +1 -0
- package/dist/rateLimit.js +70 -0
- package/dist/rateLimit.js.map +1 -0
- package/dist/registry.d.ts +75 -0
- package/dist/registry.d.ts.map +1 -0
- package/dist/registry.js +41 -0
- package/dist/registry.js.map +1 -0
- package/dist/secrets.d.ts +10 -0
- package/dist/secrets.d.ts.map +1 -0
- package/dist/secrets.js +35 -0
- package/dist/secrets.js.map +1 -0
- package/dist/ssr.d.ts +53 -0
- package/dist/ssr.d.ts.map +1 -0
- package/dist/ssr.js +39 -0
- package/dist/ssr.js.map +1 -0
- package/dist/storage.d.ts +28 -0
- package/dist/storage.d.ts.map +1 -0
- package/dist/storage.js +61 -0
- package/dist/storage.js.map +1 -0
- package/package.json +40 -0
- package/src/ai.ts +99 -0
- package/src/api.ts +56 -0
- package/src/auth.ts +16 -0
- package/src/bridge.ts +267 -0
- package/src/cache.ts +86 -0
- package/src/cron.ts +32 -0
- package/src/db.ts +211 -0
- package/src/error.ts +44 -0
- package/src/index.ts +17 -0
- package/src/islands.ts +28 -0
- package/src/middleware.ts +27 -0
- package/src/nats.ts +186 -0
- package/src/pubsub.ts +208 -0
- package/src/queue.ts +414 -0
- package/src/rateLimit.ts +89 -0
- package/src/registry.ts +98 -0
- package/src/secrets.ts +40 -0
- package/src/ssr.ts +58 -0
- package/src/storage.ts +79 -0
- package/tsconfig.json +17 -0
package/src/registry.ts
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Internal registry — Cooper's Rust runtime communicates with JS workers
|
|
3
|
+
* by referencing registered handlers by name. This module tracks all
|
|
4
|
+
* declarations (routes, topics, crons, queues, etc.) so the bridge
|
|
5
|
+
* can look them up at runtime.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
export interface RegisteredRoute {
|
|
9
|
+
method: string;
|
|
10
|
+
path: string;
|
|
11
|
+
auth: boolean;
|
|
12
|
+
stream?: "sse" | "websocket";
|
|
13
|
+
validate?: any; // Zod schema
|
|
14
|
+
middleware: MiddlewareFn[];
|
|
15
|
+
handler: Function;
|
|
16
|
+
exportName: string;
|
|
17
|
+
sourceFile: string;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface RegisteredTopic {
|
|
21
|
+
name: string;
|
|
22
|
+
subscribers: Map<string, { handler: Function; options: any }>;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface RegisteredCron {
|
|
26
|
+
name: string;
|
|
27
|
+
schedule: string;
|
|
28
|
+
handler: Function;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export interface RegisteredQueue {
|
|
32
|
+
name: string;
|
|
33
|
+
options: any;
|
|
34
|
+
worker?: { name: string; handler: Function; onFailure?: Function };
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export interface RegisteredDatabase {
|
|
38
|
+
name: string;
|
|
39
|
+
engine: string;
|
|
40
|
+
pool: any;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export interface RegisteredCache {
|
|
44
|
+
name: string;
|
|
45
|
+
options: any;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export interface RegisteredBucket {
|
|
49
|
+
name: string;
|
|
50
|
+
options: any;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export type MiddlewareFn = (req: any, next: (req: any) => Promise<any>) => Promise<any>;
|
|
54
|
+
|
|
55
|
+
export type AuthHandlerFn = (token: string) => Promise<Record<string, any>>;
|
|
56
|
+
|
|
57
|
+
class Registry {
|
|
58
|
+
routes: Map<string, RegisteredRoute> = new Map();
|
|
59
|
+
topics: Map<string, RegisteredTopic> = new Map();
|
|
60
|
+
crons: Map<string, RegisteredCron> = new Map();
|
|
61
|
+
queues: Map<string, RegisteredQueue> = new Map();
|
|
62
|
+
databases: Map<string, RegisteredDatabase> = new Map();
|
|
63
|
+
caches: Map<string, RegisteredCache> = new Map();
|
|
64
|
+
buckets: Map<string, RegisteredBucket> = new Map();
|
|
65
|
+
secrets: Map<string, () => Promise<string>> = new Map();
|
|
66
|
+
globalMiddleware: MiddlewareFn[] = [];
|
|
67
|
+
authHandler: AuthHandlerFn | null = null;
|
|
68
|
+
|
|
69
|
+
registerRoute(key: string, route: RegisteredRoute) {
|
|
70
|
+
this.routes.set(key, route);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
registerTopic(name: string, topic: RegisteredTopic) {
|
|
74
|
+
this.topics.set(name, topic);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
registerCron(name: string, cron: RegisteredCron) {
|
|
78
|
+
this.crons.set(name, cron);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
registerQueue(name: string, queue: RegisteredQueue) {
|
|
82
|
+
this.queues.set(name, queue);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
registerDatabase(name: string, db: RegisteredDatabase) {
|
|
86
|
+
this.databases.set(name, db);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
setAuthHandler(handler: AuthHandlerFn) {
|
|
90
|
+
this.authHandler = handler;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
addGlobalMiddleware(mw: MiddlewareFn) {
|
|
94
|
+
this.globalMiddleware.push(mw);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export const registry = new Registry();
|
package/src/secrets.ts
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Declare a secret — fetched at runtime, never in .env or source code.
|
|
3
|
+
*
|
|
4
|
+
* ```ts
|
|
5
|
+
* const stripeKey = secret("stripe-api-key");
|
|
6
|
+
* const client = new Stripe(await stripeKey());
|
|
7
|
+
* ```
|
|
8
|
+
*/
|
|
9
|
+
export function secret(name: string): () => Promise<string> {
|
|
10
|
+
let cached: string | null = null;
|
|
11
|
+
|
|
12
|
+
const resolve = async (): Promise<string> => {
|
|
13
|
+
if (cached !== null) return cached;
|
|
14
|
+
|
|
15
|
+
// 1. Check env var (set by `cooper secrets set`)
|
|
16
|
+
const envKey = `COOPER_SECRET_${name.toUpperCase().replace(/-/g, "_")}`;
|
|
17
|
+
const envVal = process.env[envKey];
|
|
18
|
+
if (envVal) {
|
|
19
|
+
cached = envVal;
|
|
20
|
+
return envVal;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// 2. Check .cooper/secrets/<env>/<name>
|
|
24
|
+
const fs = require("node:fs");
|
|
25
|
+
const path = require("node:path");
|
|
26
|
+
const env = process.env.COOPER_ENV ?? "local";
|
|
27
|
+
const secretPath = path.join(".cooper", "secrets", env, name);
|
|
28
|
+
if (fs.existsSync(secretPath)) {
|
|
29
|
+
const val: string = fs.readFileSync(secretPath, "utf-8").trim();
|
|
30
|
+
cached = val;
|
|
31
|
+
return val;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
throw new Error(
|
|
35
|
+
`Secret "${name}" not found. Set it with: cooper secrets set ${name} --env ${env}`
|
|
36
|
+
);
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
return resolve;
|
|
40
|
+
}
|
package/src/ssr.ts
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
export interface PageConfig {
|
|
2
|
+
cache?: string;
|
|
3
|
+
auth?: boolean;
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
export interface PageContext {
|
|
7
|
+
params: Record<string, string>;
|
|
8
|
+
req?: any;
|
|
9
|
+
data?: any;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Define a server-rendered page.
|
|
14
|
+
*
|
|
15
|
+
* ```ts
|
|
16
|
+
* export default page(async ({ params }) => {
|
|
17
|
+
* const { user } = await UsersService.getUser({ id: params.id });
|
|
18
|
+
* return <div><h1>{user.name}</h1></div>;
|
|
19
|
+
* });
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
export function page<T = any>(
|
|
23
|
+
render: (ctx: PageContext) => Promise<T>
|
|
24
|
+
): { _cooper_type: "page"; render: typeof render } {
|
|
25
|
+
return { _cooper_type: "page", render };
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Define a layout that wraps pages.
|
|
30
|
+
*
|
|
31
|
+
* ```ts
|
|
32
|
+
* export default layout(({ children }) => (
|
|
33
|
+
* <div><Nav /><main>{children}</main><Footer /></div>
|
|
34
|
+
* ));
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
37
|
+
export function layout<T = any>(
|
|
38
|
+
render: (props: { children: any }) => T
|
|
39
|
+
): { _cooper_type: "layout"; render: typeof render } {
|
|
40
|
+
return { _cooper_type: "layout", render };
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Define a page data loader — runs on the server, data passed to the page.
|
|
45
|
+
*/
|
|
46
|
+
export function pageLoader<T>(
|
|
47
|
+
loader: (ctx: PageContext) => Promise<T>
|
|
48
|
+
): { _cooper_type: "loader"; loader: typeof loader } {
|
|
49
|
+
return { _cooper_type: "loader", loader };
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Suspense boundary for streaming SSR.
|
|
54
|
+
*/
|
|
55
|
+
export function Suspense(_props: { fallback: any; children: any }) {
|
|
56
|
+
// Handled by the SSR renderer — this is a marker component
|
|
57
|
+
return null;
|
|
58
|
+
}
|
package/src/storage.ts
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
export interface BucketConfig {
|
|
2
|
+
public?: boolean;
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
export interface BucketClient {
|
|
6
|
+
upload(key: string, data: Buffer | Uint8Array, opts?: { contentType?: string }): Promise<void>;
|
|
7
|
+
download(key: string): Promise<Buffer>;
|
|
8
|
+
signedUrl(key: string, opts?: { ttl?: string }): Promise<string>;
|
|
9
|
+
delete(key: string): Promise<void>;
|
|
10
|
+
list(prefix?: string): Promise<{ key: string; size: number; lastModified: Date }[]>;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Declare an object storage bucket.
|
|
15
|
+
*
|
|
16
|
+
* ```ts
|
|
17
|
+
* export const avatars = bucket("avatars", { public: true });
|
|
18
|
+
* await avatars.upload("user-123/avatar.png", buffer, { contentType: "image/png" });
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
export function bucket(name: string, config?: BucketConfig): BucketClient {
|
|
22
|
+
const fs = require("node:fs");
|
|
23
|
+
const path = require("node:path");
|
|
24
|
+
|
|
25
|
+
// Local storage directory — in production this maps to S3/GCS/Azure Blob
|
|
26
|
+
const localDir = path.join(
|
|
27
|
+
process.env.COOPER_STORAGE_DIR ?? ".cooper/storage",
|
|
28
|
+
name
|
|
29
|
+
);
|
|
30
|
+
|
|
31
|
+
return {
|
|
32
|
+
async upload(key: string, data: Buffer | Uint8Array, opts?: { contentType?: string }) {
|
|
33
|
+
const fullPath = path.join(localDir, key);
|
|
34
|
+
fs.mkdirSync(path.dirname(fullPath), { recursive: true });
|
|
35
|
+
fs.writeFileSync(fullPath, data);
|
|
36
|
+
},
|
|
37
|
+
|
|
38
|
+
async download(key: string): Promise<Buffer> {
|
|
39
|
+
const fullPath = path.join(localDir, key);
|
|
40
|
+
return fs.readFileSync(fullPath);
|
|
41
|
+
},
|
|
42
|
+
|
|
43
|
+
async signedUrl(key: string, opts?: { ttl?: string }): Promise<string> {
|
|
44
|
+
// In local dev, return a direct file path
|
|
45
|
+
// In production, generate a presigned S3/GCS URL
|
|
46
|
+
return `http://localhost:9400/storage/${name}/${key}`;
|
|
47
|
+
},
|
|
48
|
+
|
|
49
|
+
async delete(key: string): Promise<void> {
|
|
50
|
+
const fullPath = path.join(localDir, key);
|
|
51
|
+
if (fs.existsSync(fullPath)) fs.unlinkSync(fullPath);
|
|
52
|
+
},
|
|
53
|
+
|
|
54
|
+
async list(prefix?: string) {
|
|
55
|
+
const dir = prefix ? path.join(localDir, prefix) : localDir;
|
|
56
|
+
if (!fs.existsSync(dir)) return [];
|
|
57
|
+
|
|
58
|
+
const entries: { key: string; size: number; lastModified: Date }[] = [];
|
|
59
|
+
const walk = (d: string, rel: string) => {
|
|
60
|
+
for (const entry of fs.readdirSync(d, { withFileTypes: true })) {
|
|
61
|
+
const fullPath = path.join(d, entry.name);
|
|
62
|
+
const relPath = rel ? `${rel}/${entry.name}` : entry.name;
|
|
63
|
+
if (entry.isDirectory()) {
|
|
64
|
+
walk(fullPath, relPath);
|
|
65
|
+
} else {
|
|
66
|
+
const stat = fs.statSync(fullPath);
|
|
67
|
+
entries.push({
|
|
68
|
+
key: relPath,
|
|
69
|
+
size: stat.size,
|
|
70
|
+
lastModified: stat.mtime,
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
walk(dir, "");
|
|
76
|
+
return entries;
|
|
77
|
+
},
|
|
78
|
+
};
|
|
79
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES2022",
|
|
4
|
+
"module": "ESNext",
|
|
5
|
+
"moduleResolution": "bundler",
|
|
6
|
+
"declaration": true,
|
|
7
|
+
"declarationMap": true,
|
|
8
|
+
"sourceMap": true,
|
|
9
|
+
"outDir": "dist",
|
|
10
|
+
"rootDir": "src",
|
|
11
|
+
"strict": true,
|
|
12
|
+
"esModuleInterop": true,
|
|
13
|
+
"skipLibCheck": true,
|
|
14
|
+
"forceConsistentCasingInFileNames": true
|
|
15
|
+
},
|
|
16
|
+
"include": ["src/**/*.ts"]
|
|
17
|
+
}
|