@web-widget/node 0.1.2 → 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/dist/koa.cjs +106 -0
- package/dist/koa.d.ts +22 -0
- package/dist/koa.js +81 -0
- package/package.json +7 -1
package/dist/koa.cjs
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/koa.ts
|
|
31
|
+
var koa_exports = {};
|
|
32
|
+
__export(koa_exports, {
|
|
33
|
+
default: () => NodeAdapter
|
|
34
|
+
});
|
|
35
|
+
module.exports = __toCommonJS(koa_exports);
|
|
36
|
+
var import_node_utils = require("@edge-runtime/node-utils");
|
|
37
|
+
var import_primitives = __toESM(require("@edge-runtime/primitives"), 1);
|
|
38
|
+
if (!Reflect.get(global, "DISABLE_INSTALL_MCA_SHIMS")) {
|
|
39
|
+
Object.assign(global, import_primitives.default, { console });
|
|
40
|
+
}
|
|
41
|
+
var FetchEvent = class {
|
|
42
|
+
constructor(request) {
|
|
43
|
+
this.request = request;
|
|
44
|
+
this.response = null;
|
|
45
|
+
this.awaiting = /* @__PURE__ */ new Set();
|
|
46
|
+
}
|
|
47
|
+
respondWith(response) {
|
|
48
|
+
this.response = response;
|
|
49
|
+
}
|
|
50
|
+
waitUntil() {
|
|
51
|
+
throw new Error("waitUntil is not implemented yet for Node.js");
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
var dependencies = {
|
|
55
|
+
Headers,
|
|
56
|
+
ReadableStream,
|
|
57
|
+
// @ts-ignore
|
|
58
|
+
Request: class extends Request {
|
|
59
|
+
constructor(input, init) {
|
|
60
|
+
super(input, addDuplexToInit(init));
|
|
61
|
+
}
|
|
62
|
+
},
|
|
63
|
+
Uint8Array,
|
|
64
|
+
FetchEvent
|
|
65
|
+
};
|
|
66
|
+
var NodeAdapter = class {
|
|
67
|
+
#handler;
|
|
68
|
+
#middleware;
|
|
69
|
+
constructor(webRouter, options = {
|
|
70
|
+
defaultOrigin: webRouter?.options?.origin ?? "https://web-widget.js.org"
|
|
71
|
+
}) {
|
|
72
|
+
this.#handler = (0, import_node_utils.buildToNodeHandler)(
|
|
73
|
+
dependencies,
|
|
74
|
+
options
|
|
75
|
+
)(webRouter.handler);
|
|
76
|
+
const toRequest = (0, import_node_utils.buildToRequest)(dependencies);
|
|
77
|
+
const toFetchEvent = (0, import_node_utils.buildToFetchEvent)(dependencies);
|
|
78
|
+
this.#middleware = async (ctx, next) => {
|
|
79
|
+
const incomingMessage = ctx.req;
|
|
80
|
+
const request = toRequest(incomingMessage, options);
|
|
81
|
+
const response = await webRouter.handler(request, toFetchEvent(request));
|
|
82
|
+
ctx.status = response.status;
|
|
83
|
+
ctx.message = response.statusText;
|
|
84
|
+
ctx.set(
|
|
85
|
+
(0, import_node_utils.toOutgoingHeaders)(response.headers)
|
|
86
|
+
);
|
|
87
|
+
ctx.body = (0, import_node_utils.toToReadable)(response.body);
|
|
88
|
+
await next();
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
get middleware() {
|
|
92
|
+
return this.#middleware;
|
|
93
|
+
}
|
|
94
|
+
get handler() {
|
|
95
|
+
return this.#handler;
|
|
96
|
+
}
|
|
97
|
+
static get primitives() {
|
|
98
|
+
return import_primitives.default;
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
function addDuplexToInit(init) {
|
|
102
|
+
if (typeof init === "undefined" || typeof init === "object") {
|
|
103
|
+
return { duplex: "half", ...init };
|
|
104
|
+
}
|
|
105
|
+
return init;
|
|
106
|
+
}
|
package/dist/koa.d.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import primitives from '@edge-runtime/primitives';
|
|
2
|
+
import { Middleware } from 'koa';
|
|
3
|
+
export { Middleware } from 'koa';
|
|
4
|
+
import { RequestOptions, WebHandler, NodeHandler } from '@edge-runtime/node-utils';
|
|
5
|
+
|
|
6
|
+
interface NodeAdapterOptions extends RequestOptions {
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
declare class NodeAdapter {
|
|
10
|
+
#private;
|
|
11
|
+
constructor(webRouter: {
|
|
12
|
+
handler: WebHandler;
|
|
13
|
+
options?: {
|
|
14
|
+
origin?: string;
|
|
15
|
+
};
|
|
16
|
+
}, options?: NodeAdapterOptions);
|
|
17
|
+
get middleware(): Middleware;
|
|
18
|
+
get handler(): NodeHandler;
|
|
19
|
+
static get primitives(): typeof primitives;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export { NodeAdapterOptions, NodeAdapter as default };
|
package/dist/koa.js
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
// src/koa.ts
|
|
2
|
+
import {
|
|
3
|
+
buildToFetchEvent,
|
|
4
|
+
buildToNodeHandler,
|
|
5
|
+
buildToRequest,
|
|
6
|
+
toOutgoingHeaders,
|
|
7
|
+
toToReadable
|
|
8
|
+
} from "@edge-runtime/node-utils";
|
|
9
|
+
import primitives from "@edge-runtime/primitives";
|
|
10
|
+
if (!Reflect.get(global, "DISABLE_INSTALL_MCA_SHIMS")) {
|
|
11
|
+
Object.assign(global, primitives, { console });
|
|
12
|
+
}
|
|
13
|
+
var FetchEvent = class {
|
|
14
|
+
constructor(request) {
|
|
15
|
+
this.request = request;
|
|
16
|
+
this.response = null;
|
|
17
|
+
this.awaiting = /* @__PURE__ */ new Set();
|
|
18
|
+
}
|
|
19
|
+
respondWith(response) {
|
|
20
|
+
this.response = response;
|
|
21
|
+
}
|
|
22
|
+
waitUntil() {
|
|
23
|
+
throw new Error("waitUntil is not implemented yet for Node.js");
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
var dependencies = {
|
|
27
|
+
Headers,
|
|
28
|
+
ReadableStream,
|
|
29
|
+
// @ts-ignore
|
|
30
|
+
Request: class extends Request {
|
|
31
|
+
constructor(input, init) {
|
|
32
|
+
super(input, addDuplexToInit(init));
|
|
33
|
+
}
|
|
34
|
+
},
|
|
35
|
+
Uint8Array,
|
|
36
|
+
FetchEvent
|
|
37
|
+
};
|
|
38
|
+
var NodeAdapter = class {
|
|
39
|
+
#handler;
|
|
40
|
+
#middleware;
|
|
41
|
+
constructor(webRouter, options = {
|
|
42
|
+
defaultOrigin: webRouter?.options?.origin ?? "https://web-widget.js.org"
|
|
43
|
+
}) {
|
|
44
|
+
this.#handler = buildToNodeHandler(
|
|
45
|
+
dependencies,
|
|
46
|
+
options
|
|
47
|
+
)(webRouter.handler);
|
|
48
|
+
const toRequest = buildToRequest(dependencies);
|
|
49
|
+
const toFetchEvent = buildToFetchEvent(dependencies);
|
|
50
|
+
this.#middleware = async (ctx, next) => {
|
|
51
|
+
const incomingMessage = ctx.req;
|
|
52
|
+
const request = toRequest(incomingMessage, options);
|
|
53
|
+
const response = await webRouter.handler(request, toFetchEvent(request));
|
|
54
|
+
ctx.status = response.status;
|
|
55
|
+
ctx.message = response.statusText;
|
|
56
|
+
ctx.set(
|
|
57
|
+
toOutgoingHeaders(response.headers)
|
|
58
|
+
);
|
|
59
|
+
ctx.body = toToReadable(response.body);
|
|
60
|
+
await next();
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
get middleware() {
|
|
64
|
+
return this.#middleware;
|
|
65
|
+
}
|
|
66
|
+
get handler() {
|
|
67
|
+
return this.#handler;
|
|
68
|
+
}
|
|
69
|
+
static get primitives() {
|
|
70
|
+
return primitives;
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
function addDuplexToInit(init) {
|
|
74
|
+
if (typeof init === "undefined" || typeof init === "object") {
|
|
75
|
+
return { duplex: "half", ...init };
|
|
76
|
+
}
|
|
77
|
+
return init;
|
|
78
|
+
}
|
|
79
|
+
export {
|
|
80
|
+
NodeAdapter as default
|
|
81
|
+
};
|
package/package.json
CHANGED
|
@@ -1,12 +1,17 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@web-widget/node",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"exports": {
|
|
6
6
|
".": {
|
|
7
7
|
"types": "./dist/node.d.ts",
|
|
8
8
|
"require": "./dist/node.cjs",
|
|
9
9
|
"default": "./dist/node.js"
|
|
10
|
+
},
|
|
11
|
+
"./koa": {
|
|
12
|
+
"types": "./dist/koa.d.ts",
|
|
13
|
+
"require": "./dist/koa.cjs",
|
|
14
|
+
"default": "./dist/koa.js"
|
|
10
15
|
}
|
|
11
16
|
},
|
|
12
17
|
"files": [
|
|
@@ -18,6 +23,7 @@
|
|
|
18
23
|
},
|
|
19
24
|
"devDependencies": {
|
|
20
25
|
"@types/node": "^17.0.42",
|
|
26
|
+
"@types/koa": "^2.13.9",
|
|
21
27
|
"tsup": "^7.0.0",
|
|
22
28
|
"typescript": "^5.0.4"
|
|
23
29
|
},
|