@tupik/edge 0.0.0-stage → 1.0.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/LICENSE +21 -0
- package/README.md +38 -2
- package/dist/index.d.mts +32 -0
- package/dist/index.d.ts +32 -0
- package/dist/index.mjs +15 -0
- package/package.json +50 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Carlos Alberto Barbosa Stenzel / Tupik Contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,39 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @tupik/edge
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Edge adapter for **Tupik**: exposes an app as a fetch handler for Cloudflare Workers. Worker `env` and `ctx`
|
|
4
|
+
are forwarded to `ctx.runtime`. Fastly and other fetch-based runtimes are not verified yet (planned).
|
|
5
|
+
|
|
6
|
+
## Install
|
|
7
|
+
|
|
8
|
+
```bash
|
|
9
|
+
npm install @tupik/edge @tupik/core
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
## Usage
|
|
13
|
+
|
|
14
|
+
```ts
|
|
15
|
+
import { createApp, createModule } from '@tupik/core';
|
|
16
|
+
import { createEdgeWorker, getEnv } from '@tupik/edge';
|
|
17
|
+
|
|
18
|
+
const app = createApp().register(
|
|
19
|
+
createModule().get('/', { access: 'public', handler: (ctx) => ({ greeting: getEnv<{ GREETING?: string }>(ctx).GREETING ?? 'hello' }) }),
|
|
20
|
+
);
|
|
21
|
+
|
|
22
|
+
// Cloudflare Workers: `export default worker;`
|
|
23
|
+
const worker = createEdgeWorker(app);
|
|
24
|
+
|
|
25
|
+
const response = await worker.fetch(new Request('https://example.com/'), { GREETING: 'hi from the edge' });
|
|
26
|
+
console.log(await response.json()); // { greeting: 'hi from the edge' }
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## Key API
|
|
30
|
+
|
|
31
|
+
| Export | Purpose |
|
|
32
|
+
| --- | --- |
|
|
33
|
+
| `createEdgeWorker(app)` | `{ fetch(request, env, ctx) }` module-worker object |
|
|
34
|
+
| `toEdgeHandler(app)` | Bare `(request, env?, ctx?) => Promise<Response>` handler |
|
|
35
|
+
| `getEnv<TEnv>(ctx)` | Typed access to the runtime bindings inside a handler |
|
|
36
|
+
|
|
37
|
+
## License
|
|
38
|
+
|
|
39
|
+
MIT
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { ExecutionContextLike, RuntimeBindings } from '@tupik/core';
|
|
2
|
+
export { ExecutionContextLike } from '@tupik/core';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Structural subset of `@tupik/core` apps accepted by the edge adapter.
|
|
6
|
+
* `env`/`executionCtx` are forwarded to `ctx.runtime` (Cloudflare Workers).
|
|
7
|
+
*/
|
|
8
|
+
interface EdgeApp {
|
|
9
|
+
fetch(request: Request, env?: unknown, executionCtx?: ExecutionContextLike): Promise<Response>;
|
|
10
|
+
}
|
|
11
|
+
/** WinterCG-style Fetch handler; Workers pass `env` and `ctx` as extra arguments. */
|
|
12
|
+
type EdgeHandler<TEnv = unknown> = (request: Request, env?: TEnv, executionCtx?: ExecutionContextLike) => Promise<Response>;
|
|
13
|
+
|
|
14
|
+
/** Zero-conversion Fetch adapter for Edge runtimes (Cloudflare, Fastly, Deno). */
|
|
15
|
+
declare function toEdgeHandler<TEnv = unknown>(app: EdgeApp): EdgeHandler<TEnv>;
|
|
16
|
+
/**
|
|
17
|
+
* Worker-style module export: `export default createEdgeWorker<Env>(app);`.
|
|
18
|
+
* Cloudflare calls `fetch(request, env, ctx)`; both reach handlers as `ctx.runtime`.
|
|
19
|
+
*/
|
|
20
|
+
declare function createEdgeWorker<TEnv = unknown>(app: EdgeApp): {
|
|
21
|
+
fetch: EdgeHandler<TEnv>;
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* Typed access to Worker bindings from a handler: `getEnv<Env>(ctx).MY_KV`.
|
|
25
|
+
* Throws when the runtime did not provide an `env` (Deno, Fastly, tests).
|
|
26
|
+
*/
|
|
27
|
+
declare function getEnv<TEnv>(ctx: {
|
|
28
|
+
readonly runtime: RuntimeBindings;
|
|
29
|
+
}): TEnv;
|
|
30
|
+
|
|
31
|
+
export { createEdgeWorker, getEnv, toEdgeHandler };
|
|
32
|
+
export type { EdgeApp, EdgeHandler };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { ExecutionContextLike, RuntimeBindings } from '@tupik/core';
|
|
2
|
+
export { ExecutionContextLike } from '@tupik/core';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Structural subset of `@tupik/core` apps accepted by the edge adapter.
|
|
6
|
+
* `env`/`executionCtx` are forwarded to `ctx.runtime` (Cloudflare Workers).
|
|
7
|
+
*/
|
|
8
|
+
interface EdgeApp {
|
|
9
|
+
fetch(request: Request, env?: unknown, executionCtx?: ExecutionContextLike): Promise<Response>;
|
|
10
|
+
}
|
|
11
|
+
/** WinterCG-style Fetch handler; Workers pass `env` and `ctx` as extra arguments. */
|
|
12
|
+
type EdgeHandler<TEnv = unknown> = (request: Request, env?: TEnv, executionCtx?: ExecutionContextLike) => Promise<Response>;
|
|
13
|
+
|
|
14
|
+
/** Zero-conversion Fetch adapter for Edge runtimes (Cloudflare, Fastly, Deno). */
|
|
15
|
+
declare function toEdgeHandler<TEnv = unknown>(app: EdgeApp): EdgeHandler<TEnv>;
|
|
16
|
+
/**
|
|
17
|
+
* Worker-style module export: `export default createEdgeWorker<Env>(app);`.
|
|
18
|
+
* Cloudflare calls `fetch(request, env, ctx)`; both reach handlers as `ctx.runtime`.
|
|
19
|
+
*/
|
|
20
|
+
declare function createEdgeWorker<TEnv = unknown>(app: EdgeApp): {
|
|
21
|
+
fetch: EdgeHandler<TEnv>;
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* Typed access to Worker bindings from a handler: `getEnv<Env>(ctx).MY_KV`.
|
|
25
|
+
* Throws when the runtime did not provide an `env` (Deno, Fastly, tests).
|
|
26
|
+
*/
|
|
27
|
+
declare function getEnv<TEnv>(ctx: {
|
|
28
|
+
readonly runtime: RuntimeBindings;
|
|
29
|
+
}): TEnv;
|
|
30
|
+
|
|
31
|
+
export { createEdgeWorker, getEnv, toEdgeHandler };
|
|
32
|
+
export type { EdgeApp, EdgeHandler };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
function toEdgeHandler(app) {
|
|
2
|
+
return (request, env, executionCtx) => app.fetch(request, env, executionCtx);
|
|
3
|
+
}
|
|
4
|
+
function createEdgeWorker(app) {
|
|
5
|
+
return { fetch: toEdgeHandler(app) };
|
|
6
|
+
}
|
|
7
|
+
function getEnv(ctx) {
|
|
8
|
+
const env = ctx.runtime.env;
|
|
9
|
+
if (env === void 0) {
|
|
10
|
+
throw new Error("No platform env available: the runtime did not pass bindings to fetch()");
|
|
11
|
+
}
|
|
12
|
+
return env;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export { createEdgeWorker, getEnv, toEdgeHandler };
|
package/package.json
CHANGED
|
@@ -1,6 +1,53 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tupik/edge",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Edge adapter for Tupik: Cloudflare Workers, Fastly and any fetch-handler runtime.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"tupik",
|
|
8
|
+
"typescript",
|
|
9
|
+
"web-framework",
|
|
10
|
+
"edge",
|
|
11
|
+
"cloudflare-workers",
|
|
12
|
+
"fastly",
|
|
13
|
+
"serverless"
|
|
14
|
+
],
|
|
15
|
+
"homepage": "https://github.com/CodeHamper/tupikjs/tree/main/packages/edge#readme",
|
|
16
|
+
"repository": {
|
|
17
|
+
"type": "git",
|
|
18
|
+
"url": "https://github.com/CodeHamper/tupikjs.git",
|
|
19
|
+
"directory": "packages/edge"
|
|
20
|
+
},
|
|
21
|
+
"bugs": {
|
|
22
|
+
"url": "https://github.com/CodeHamper/tupikjs/issues"
|
|
23
|
+
},
|
|
24
|
+
"type": "module",
|
|
25
|
+
"publishConfig": {
|
|
26
|
+
"access": "public"
|
|
27
|
+
},
|
|
28
|
+
"files": [
|
|
29
|
+
"dist"
|
|
30
|
+
],
|
|
31
|
+
"exports": {
|
|
32
|
+
".": {
|
|
33
|
+
"types": "./dist/index.d.ts",
|
|
34
|
+
"import": "./dist/index.mjs"
|
|
35
|
+
}
|
|
36
|
+
},
|
|
37
|
+
"engines": {
|
|
38
|
+
"node": ">=22.12"
|
|
39
|
+
},
|
|
40
|
+
"dependencies": {
|
|
41
|
+
"@tupik/core": "^1.0.0"
|
|
42
|
+
},
|
|
43
|
+
"devDependencies": {
|
|
44
|
+
"typescript": "^5.6.0",
|
|
45
|
+
"unbuild": "^3.0.0",
|
|
46
|
+
"vitest": "^2.1.0"
|
|
47
|
+
},
|
|
48
|
+
"scripts": {
|
|
49
|
+
"build": "unbuild",
|
|
50
|
+
"typecheck": "tsc --noEmit",
|
|
51
|
+
"test": "vitest run"
|
|
52
|
+
}
|
|
6
53
|
}
|