@mapl/web 0.2.0-alpha.1 → 0.2.0-alpha.2
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 +127 -100
- package/compiler/aot.js +1 -1
- package/compiler/config.d.ts +1 -1
- package/compiler/config.js +1 -1
- package/compiler/index.d.ts +4 -0
- package/compiler/index.js +1 -0
- package/compiler/jit.d.ts +1 -0
- package/compiler/jit.js +1 -1
- package/constants.d.ts +1 -0
- package/constants.js +1 -1
- package/core/handler.js +1 -1
- package/package.json +8 -7
package/README.md
CHANGED
|
@@ -54,24 +54,31 @@ import { compileToString } from '@mapl/web/compiler/jit';
|
|
|
54
54
|
|
|
55
55
|
import { rolldown } from 'rolldown';
|
|
56
56
|
import { minifySync } from '@swc/core';
|
|
57
|
-
import { writeFileSync
|
|
57
|
+
import { writeFileSync } from 'node:fs';
|
|
58
58
|
|
|
59
|
-
|
|
59
|
+
// Output file
|
|
60
|
+
const ENTRY = ...;
|
|
60
61
|
|
|
61
62
|
writeFileSync(
|
|
62
63
|
ENTRY,
|
|
63
64
|
`
|
|
64
|
-
import
|
|
65
|
+
import { hydrating } from "@mapl/web/compiler/config";
|
|
66
|
+
hydrating();
|
|
67
|
+
|
|
68
|
+
import app from '${import.meta.resolve('./main.js')}';
|
|
65
69
|
import hydrate from '@mapl/web/compiler/aot';
|
|
66
70
|
|
|
67
|
-
// Compatible with Bun, Deno, Cloudflare, ...
|
|
68
71
|
export default {
|
|
69
72
|
fetch: (${compileToString(app)})(...hydrate(app))
|
|
70
73
|
};
|
|
71
|
-
|
|
74
|
+
`,
|
|
72
75
|
);
|
|
73
76
|
const input = await rolldown({
|
|
74
77
|
input: ENTRY,
|
|
78
|
+
treeshake: {
|
|
79
|
+
propertyReadSideEffects: false,
|
|
80
|
+
moduleSideEffects: false,
|
|
81
|
+
},
|
|
75
82
|
transform: {
|
|
76
83
|
typescript: {
|
|
77
84
|
rewriteImportExtensions: true,
|
|
@@ -79,128 +86,148 @@ const input = await rolldown({
|
|
|
79
86
|
},
|
|
80
87
|
});
|
|
81
88
|
const output = await input.generate();
|
|
82
|
-
const code = minifySync(output.output[0].code, {
|
|
89
|
+
const code = minifySync(output.output[0].code, {
|
|
90
|
+
module: true,
|
|
91
|
+
mangle: false,
|
|
92
|
+
}).code;
|
|
93
|
+
|
|
83
94
|
writeFileSync(ENTRY, code);
|
|
84
95
|
```
|
|
85
96
|
|
|
86
|
-
Example output
|
|
97
|
+
Example output:
|
|
87
98
|
```js
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
0 ===
|
|
98
|
-
|
|
99
|
-
var r = e(
|
|
99
|
+
var core_default = (middlewares, handlers, children) => [
|
|
100
|
+
middlewares,
|
|
101
|
+
handlers,
|
|
102
|
+
,
|
|
103
|
+
children,
|
|
104
|
+
];
|
|
105
|
+
let attach = (prop, f) => [1, f, prop],
|
|
106
|
+
noType = { type: null },
|
|
107
|
+
mergeData = (...dat) =>
|
|
108
|
+
0 === dat.length ? noType : Object.assign({ type: null }, ...dat);
|
|
109
|
+
var main_default = core_default(
|
|
100
110
|
[
|
|
101
|
-
// inlined layer.tap()
|
|
102
111
|
[
|
|
103
112
|
0,
|
|
104
|
-
(
|
|
105
|
-
console.log(
|
|
113
|
+
(c) => {
|
|
114
|
+
console.log(c.req);
|
|
106
115
|
},
|
|
107
116
|
],
|
|
108
|
-
|
|
109
|
-
|
|
117
|
+
attach('id', () => performance.now()),
|
|
118
|
+
],
|
|
119
|
+
[
|
|
120
|
+
((path, handler, ...dat) => ['', path, handler, mergeData(...dat)])(
|
|
121
|
+
'/path',
|
|
122
|
+
(c) => c.id,
|
|
123
|
+
),
|
|
110
124
|
],
|
|
111
|
-
// handle.any()
|
|
112
|
-
[((...e) => n("", ...e))("/path", (e) => e.id)],
|
|
113
125
|
{
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
[
|
|
117
|
-
|
|
118
|
-
|
|
126
|
+
'/api': core_default(
|
|
127
|
+
[attach('body', async (c) => c.req.text())],
|
|
128
|
+
[
|
|
129
|
+
((path, handler, ...dat) => ['POST', path, handler, mergeData(...dat)])(
|
|
130
|
+
'/body',
|
|
131
|
+
(c) => c.body,
|
|
132
|
+
),
|
|
133
|
+
],
|
|
119
134
|
),
|
|
120
135
|
},
|
|
121
136
|
);
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
137
|
+
let AsyncFunction = (async () => {}).constructor,
|
|
138
|
+
compilerState = [, , , , ,],
|
|
139
|
+
compileErrorHandler$1 = (scope) =>
|
|
140
|
+
(scope[3] ??= compilerState[4](scope[2][0], scope[2][1], scope)),
|
|
141
|
+
clearErrorHandler = (scope) => {
|
|
142
|
+
null != scope[2] && (scope[3] = null);
|
|
126
143
|
},
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
null !=
|
|
138
|
-
for (let
|
|
139
|
-
let
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
-1 ===
|
|
143
|
-
?
|
|
144
|
-
: (
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
1 ===
|
|
148
|
-
?
|
|
149
|
-
: 2 ===
|
|
150
|
-
? (
|
|
151
|
-
: 3 ===
|
|
144
|
+
createContext = (scope) =>
|
|
145
|
+
scope[1]
|
|
146
|
+
? ''
|
|
147
|
+
: ((scope[1] = !0), clearErrorHandler(scope), compilerState[2]),
|
|
148
|
+
createAsyncScope = (scope) =>
|
|
149
|
+
scope[0]
|
|
150
|
+
? ''
|
|
151
|
+
: ((scope[0] = !0), clearErrorHandler(scope), 'return (async()=>{'),
|
|
152
|
+
setTmp = (scope) => (scope[4] ? 't' : ((scope[4] = !0), 'let t')),
|
|
153
|
+
hydrateDependency = (group, scope, prefix) => {
|
|
154
|
+
null != group[2] && ((scope[2] = group[2]), (scope[3] = null));
|
|
155
|
+
for (let i = 0, middlewares = group[0]; i < middlewares.length; i++) {
|
|
156
|
+
let middleware = middlewares[i],
|
|
157
|
+
fn = middleware[1],
|
|
158
|
+
id = middleware[0];
|
|
159
|
+
-1 === id
|
|
160
|
+
? fn(scope)
|
|
161
|
+
: (compilerState[1].push(fn),
|
|
162
|
+
fn.length > 0 && createContext(scope),
|
|
163
|
+
fn instanceof AsyncFunction && createAsyncScope(scope),
|
|
164
|
+
1 === id
|
|
165
|
+
? createContext(scope)
|
|
166
|
+
: 2 === id
|
|
167
|
+
? (setTmp(scope), compileErrorHandler$1(scope))
|
|
168
|
+
: 3 === id &&
|
|
169
|
+
(setTmp(scope),
|
|
170
|
+
compileErrorHandler$1(scope),
|
|
171
|
+
createContext(scope)));
|
|
152
172
|
}
|
|
153
|
-
for (let
|
|
154
|
-
let
|
|
155
|
-
|
|
156
|
-
|
|
173
|
+
for (let i = 0, handlers = group[1]; i < handlers.length; i++) {
|
|
174
|
+
let handler = handlers[i];
|
|
175
|
+
compilerState[3](
|
|
176
|
+
handler[2],
|
|
177
|
+
handler[3],
|
|
178
|
+
prefix + ('/' === handler[1] || '' !== prefix ? '' : handler[1]),
|
|
179
|
+
scope,
|
|
180
|
+
);
|
|
157
181
|
}
|
|
158
|
-
let
|
|
159
|
-
if (null !=
|
|
182
|
+
let childGroups = group[3];
|
|
183
|
+
if (null != childGroups)
|
|
184
|
+
for (let childPrefix in childGroups)
|
|
185
|
+
hydrateDependency(
|
|
186
|
+
childGroups[childPrefix],
|
|
187
|
+
scope.slice(),
|
|
188
|
+
'/' === childPrefix ? prefix : prefix + childPrefix,
|
|
189
|
+
);
|
|
160
190
|
},
|
|
161
|
-
|
|
162
|
-
var
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
[
|
|
167
|
-
[
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
let r = t(e);
|
|
191
|
+
_ = Symbol.for('@safe-std/error');
|
|
192
|
+
var aot_default = {
|
|
193
|
+
fetch: ((me, mwc, f1, f2, f3, f4, f5) => {
|
|
194
|
+
var t = ['text/html', 'application/json'].map((c) => ['Content-Type', c]),
|
|
195
|
+
[mwh, mwj] = t,
|
|
196
|
+
[mwoh, mwoj] = t.map((c) => ({ headers: [c] })),
|
|
197
|
+
[mwn, mwb] = [404, 400].map((s) => new Response(null, { status: s }));
|
|
198
|
+
return (r) => {
|
|
199
|
+
let u = r.url,
|
|
200
|
+
s = u.indexOf('/', 12) + 1,
|
|
201
|
+
e = u.indexOf('?', s),
|
|
202
|
+
p = -1 === e ? u.slice(s) : u.slice(s, e);
|
|
203
|
+
if ('POST' === r.method && 'api' === p) {
|
|
204
|
+
let c = mwc(r);
|
|
176
205
|
return (
|
|
177
|
-
|
|
178
|
-
(
|
|
179
|
-
(async () => ((
|
|
206
|
+
f1(c),
|
|
207
|
+
(c.id = f2()),
|
|
208
|
+
(async () => ((c.body = await f4(c)), new Response(f5(c), c)))()
|
|
180
209
|
);
|
|
181
210
|
}
|
|
182
|
-
if (
|
|
183
|
-
let
|
|
184
|
-
return (
|
|
211
|
+
if ('path' === p) {
|
|
212
|
+
let c = mwc(r);
|
|
213
|
+
return f1(c), (c.id = f2()), new Response(f3(c), c);
|
|
185
214
|
}
|
|
186
|
-
return
|
|
215
|
+
return mwn;
|
|
187
216
|
};
|
|
188
217
|
})(
|
|
189
|
-
...((
|
|
190
|
-
(
|
|
191
|
-
(
|
|
192
|
-
(
|
|
193
|
-
(
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
// inject dependencies
|
|
218
|
+
...((compilerState[0] = {}),
|
|
219
|
+
(compilerState[1] = []),
|
|
220
|
+
(compilerState[2] = ''),
|
|
221
|
+
(compilerState[3] = (fn) => (compilerState[1].push(fn), '')),
|
|
222
|
+
(compilerState[4] = (fn) => (compilerState[1].push(fn), '')),
|
|
223
|
+
hydrateDependency(main_default, [!1, !1, , '', !1], ''),
|
|
197
224
|
[
|
|
198
|
-
(
|
|
199
|
-
(
|
|
200
|
-
].concat(
|
|
225
|
+
(u) => Array.isArray(u) && u[0] === _,
|
|
226
|
+
(r) => ({ status: 200, req: r, headers: [] }),
|
|
227
|
+
].concat(compilerState[1])),
|
|
201
228
|
),
|
|
202
229
|
};
|
|
203
|
-
export {
|
|
230
|
+
export { aot_default as default };
|
|
204
231
|
```
|
|
205
232
|
|
|
206
233
|
### Hydration
|
package/compiler/aot.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{hydrateDependency,
|
|
1
|
+
import{hydrateDependency,externalDependencies,setHooks}from"@mapl/framework";import{isErr}from"@safe-std/error";import createContext from"../core/context.js";export default router=>{externalDependencies.length=0;let hook=fn=>{externalDependencies.push(fn);return``};setHooks({compileHandler:hook,compileErrorHandler:hook});hydrateDependency(router,[false,false,,``,false],``);return[isErr,createContext].concat(externalDependencies)};
|
package/compiler/config.d.ts
CHANGED
package/compiler/config.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
let hydrateDependency=
|
|
1
|
+
let hydrateDependency=false;export let isHydrating=()=>hydrateDependency;export let hydrating=()=>{hydrateDependency=true};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{externalDependencies}from"@mapl/framework";import{isHydrating}from"./config.js";let localDeps=``,localDepsCnt=0;export let injectLocalDependency=isHydrating()?()=>++localDepsCnt:val=>{localDepsCnt++;localDeps+=`,l`+localDepsCnt+`=`+val;return localDepsCnt};export let localDependencies=()=>localDeps;export let stateToArgs=()=>{let depsString=`me,mwc`;for(let i=0;i<externalDependencies.length;i++)depsString+=`,f`+(i+1);return depsString};
|
package/compiler/jit.d.ts
CHANGED
package/compiler/jit.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{compileGroup,createArgSet,AsyncFunction,
|
|
1
|
+
import{compileGroup,createArgSet,AsyncFunction,setHooks,externalDependencies,contextInit,setContextInit}from"@mapl/framework";import compile from"@mapl/router/method/compiler";import{countParams}from"@mapl/router/path";import{insertItem}from"@mapl/router/method";import{isErr}from"@safe-std/error";import createContext from"../core/context.js";import{localDependencies,stateToArgs}from"./index.js";let urlRouter;let paramArgs=createArgSet(new Array(16).fill(0).map((_1,i)=>`q`+i));let compileReturn=(dat,fnAsync,scopeAsync,contextCreated,result)=>{let typ=dat.type;if(typ===`raw`)return`return `+result;fnAsync&&(result=`await `+result);let str=typ==null?`return new Response(`+result+(contextCreated?`,c)`:`)`):contextCreated?`c.headers.push(`+(typ===`json`?`mwj`:`mwh`)+`);return new Response(`+(typ===`json`?`JSON.stringify(`+result+`)`:result)+`,c)`:`return new Response(`+(typ===`json`?`JSON.stringify(`+result+`),mwoj`:result+`,mwoh`)+`)`;return fnAsync&&!scopeAsync?`return (async()=>{`+str+`})()`:str};let compileToState=router=>{urlRouter={};externalDependencies.length=0;setHooks({compileHandler:(fn,dat,path,scope)=>{let call=`f`+externalDependencies.push(fn)+`(`;let paramCount=countParams(path);paramCount>0&&(call+=paramArgs[paramCount]);if(fn.length>paramCount){call+=paramCount===0?`c`:`,c`;if(!scope[1])return contextInit()+compileReturn(dat,fn instanceof AsyncFunction,scope[0],true,call+`)`)}return compileReturn(dat,fn instanceof AsyncFunction,scope[0],scope[1],call+`)`)},compileErrorHandler:(fn,dat,scope)=>{let call=`f`+externalDependencies.push(fn)+`(t`;if(fn.length>1){call+=`,c`;if(!scope[1])return contextInit()+compileReturn(dat,fn instanceof AsyncFunction,scope[0],true,call+`)`)}return compileReturn(dat,fn instanceof AsyncFunction,scope[0],scope[1],call+`)`)},registerCompiled:(method,path,content)=>insertItem(urlRouter,method,path,content)});setContextInit(`let c=mwc(r);`);compileGroup(router,[false,false,,`return mwb`,false],``,``)};export let stateToString=()=>`"use strict";var t=["text/html","application/json"].map(c=>["Content-Type",c]),[mwh,mwj]=t,[mwoh,mwoj]=t.map(c=>({headers:[c]})),[mwn,mwb]=[404,400].map(s=>new Response(null,{status:s}))`+localDependencies()+`;return(r)=>{`+compile(urlRouter,`r.method`,`let u=r.url,s=u.indexOf("/",12)+1,e=u.indexOf("?",s),p=e===-1?u.slice(s):u.slice(s,e);`,1)+`return mwn}`;export let compileToString=router=>{compileToState(router);return`(`+stateToArgs()+`)=>{`+stateToString()+`}`};export let compileToHandler=router=>{compileToState(router);return Function(stateToArgs(),stateToString())(isErr,createContext,...externalDependencies)};
|
package/constants.d.ts
CHANGED
package/constants.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{MAPL,CTX,PATH,REQ,URL,PATH_START,PATH_END,TMP}from"@mapl/framework/constants";export*from"@mapl/framework/constants";export let WEB=MAPL+`w`;export let CTX_FN=WEB+`c`;export let CTX_INIT=`let `+CTX+`=`+CTX_FN+`(`+REQ+`);`;export let PARSE_PATH=`let `+URL+`=`+REQ+`.url,`+PATH_START+`=`+URL+`.indexOf("/",12)+1,`+PATH_END+`=`+URL+`.indexOf("?",`+PATH_START+`),`+PATH+`=`+PATH_END+`===-1?`+URL+`.slice(`+PATH_START+`):`+URL+`.slice(`+PATH_START+`,`+PATH_END+`);`;export let CHTML=WEB+`h`;export let CJSON=WEB+`j`;export let OHTML=WEB+`oh`;export let OJSON=WEB+`oj`;export let R404=WEB+`n`;export let R400=WEB+`b`;export let GLOBALS=`var `+TMP+`=["text/html","application/json"].map(c=>["Content-Type",c]),[`+CHTML+`,`+CJSON+`]=`+TMP+`,[`+OHTML+`,`+OJSON+`]=`+TMP+`.map(c=>({headers:[c]})),[`+R404+`,`+R400+`]=[404,400].map(s=>new Response(null,{status:s}))`;
|
|
1
|
+
import{MAPL,CTX,PATH,REQ,URL,PATH_START,PATH_END,TMP}from"@mapl/framework/constants";export*from"@mapl/framework/constants";export let WEB=MAPL+`w`;export let LOCAL_DEP=`l`;export let CTX_FN=WEB+`c`;export let CTX_INIT=`let `+CTX+`=`+CTX_FN+`(`+REQ+`);`;export let PARSE_PATH=`let `+URL+`=`+REQ+`.url,`+PATH_START+`=`+URL+`.indexOf("/",12)+1,`+PATH_END+`=`+URL+`.indexOf("?",`+PATH_START+`),`+PATH+`=`+PATH_END+`===-1?`+URL+`.slice(`+PATH_START+`):`+URL+`.slice(`+PATH_START+`,`+PATH_END+`);`;export let CHTML=WEB+`h`;export let CJSON=WEB+`j`;export let OHTML=WEB+`oh`;export let OJSON=WEB+`oj`;export let R404=WEB+`n`;export let R400=WEB+`b`;export let GLOBALS=`var `+TMP+`=["text/html","application/json"].map(c=>["Content-Type",c]),[`+CHTML+`,`+CJSON+`]=`+TMP+`,[`+OHTML+`,`+OJSON+`]=`+TMP+`.map(c=>({headers:[c]})),[`+R404+`,`+R400+`]=[404,400].map(s=>new Response(null,{status:s}))`;
|
package/core/handler.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export let json={type:`json`};export let html={type:`html`};export let raw={type:`raw`};let noType={type:null};export let error=(r,f,...dat)=>{r[2]=[f,dat.length===0?noType:Object.assign({type:null},...dat)];return r};
|
|
1
|
+
export let json={type:`json`};export let html={type:`html`};export let raw={type:`raw`};let noType={type:null};export let error=(r,f,...dat)=>{r[2]=[f,dat.length===0?noType:Object.assign({type:null},...dat)];return r};let mergeData=(...dat)=>dat.length===0?noType:Object.assign({type:null},...dat);export let route=(method,path,handler,...dat)=>[method,path,handler,mergeData(...dat)];export let any=(path,handler,...dat)=>[``,path,handler,mergeData(...dat)];export let get=(path,handler,...dat)=>[`GET`,path,handler,mergeData(...dat)];export let post=(path,handler,...dat)=>[`POST`,path,handler,mergeData(...dat)];export let put=(path,handler,...dat)=>[`PUT`,path,handler,mergeData(...dat)];export let del=(path,handler,...dat)=>[`DELETE`,path,handler,mergeData(...dat)];export let patch=(path,handler,...dat)=>[`PATCH`,path,handler,mergeData(...dat)];export let options=(path,handler,...dat)=>[`OPTIONS`,path,handler,mergeData(...dat)];export let trace=(path,handler,...dat)=>[`TRACE`,path,handler,mergeData(...dat)];
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mapl/web",
|
|
3
|
-
"version": "0.2.0-alpha.
|
|
3
|
+
"version": "0.2.0-alpha.2",
|
|
4
4
|
"description": "A compiled web framework for all runtimes",
|
|
5
5
|
"keywords": ["fast", "lightweight", "cross-runtime", "framework", "web", "backend"],
|
|
6
6
|
"repository": {
|
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
"main": "./index.js",
|
|
13
13
|
"types": "./index.d.ts",
|
|
14
14
|
"dependencies": {
|
|
15
|
-
"@mapl/framework": "^0.3.
|
|
15
|
+
"@mapl/framework": "^0.3.7",
|
|
16
16
|
"@mapl/router": "^0.6.2",
|
|
17
17
|
"@safe-std/error": "^1.0.1"
|
|
18
18
|
},
|
|
@@ -23,15 +23,16 @@
|
|
|
23
23
|
"./constants": "./constants.js",
|
|
24
24
|
".": "./index.js",
|
|
25
25
|
"./core/utils": "./core/utils.js",
|
|
26
|
-
"./core/
|
|
26
|
+
"./core/context": "./core/context.js",
|
|
27
27
|
"./core": "./core/index.js",
|
|
28
|
-
"./utils/cors": "./utils/cors.js",
|
|
29
28
|
"./core/handler": "./core/handler.js",
|
|
30
|
-
"./
|
|
29
|
+
"./utils/cors": "./utils/cors.js",
|
|
31
30
|
"./utils/headers": "./utils/headers.js",
|
|
31
|
+
"./utils/secure-headers": "./utils/secure-headers.js",
|
|
32
32
|
"./compiler/aot": "./compiler/aot.js",
|
|
33
33
|
"./compiler/jit": "./compiler/jit.js",
|
|
34
|
-
"./
|
|
35
|
-
"./
|
|
34
|
+
"./compiler/config": "./compiler/config.js",
|
|
35
|
+
"./core/middleware": "./core/middleware.js",
|
|
36
|
+
"./compiler": "./compiler/index.js"
|
|
36
37
|
}
|
|
37
38
|
}
|