@chillicream/nitro-server-adapter-plugin 19.0.0-insider.1
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 +95 -0
- package/README.md +150 -0
- package/index.cjs +187 -0
- package/index.d.cts +40 -0
- package/index.d.mts +40 -0
- package/index.mjs +184 -0
- package/package.json +67 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
Copyright (c) ChilliCream, Inc.
|
|
2
|
+
|
|
3
|
+
Source code in this repository is covered by the ChilliCream License 1.0 license as designated by a licensing file in a subdirectory or file header. The default throughout the repository is a license under the ChilliCream License 1.0, unless a file header or a licensing file in a subdirectory specifies another license.
|
|
4
|
+
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
# ChilliCream License 1.0
|
|
8
|
+
|
|
9
|
+
URL: https://chillicream.com/licensing/chillicream-license
|
|
10
|
+
|
|
11
|
+
## Acceptance
|
|
12
|
+
|
|
13
|
+
By using the software, you agree to all of the terms and conditions below.
|
|
14
|
+
|
|
15
|
+
## Copyright License
|
|
16
|
+
|
|
17
|
+
The licensor grants you a non-exclusive, royalty-free, worldwide,
|
|
18
|
+
non-sublicensable, non-transferable license to use, copy, distribute, make
|
|
19
|
+
available, and prepare derivative works of the software, in each case subject to
|
|
20
|
+
the limitations and conditions below.
|
|
21
|
+
|
|
22
|
+
## Limitations
|
|
23
|
+
|
|
24
|
+
You may not move, change, disable, or circumvent the license key functionality
|
|
25
|
+
in the software, and you may not remove or obscure any functionality in the
|
|
26
|
+
software that is protected by the license key.
|
|
27
|
+
|
|
28
|
+
You may not alter, remove, or obscure any licensing, copyright, or other notices
|
|
29
|
+
of the licensor in the software. Any use of the licensor’s trademarks is subject
|
|
30
|
+
to applicable law.
|
|
31
|
+
|
|
32
|
+
## Patents
|
|
33
|
+
|
|
34
|
+
The licensor grants you a license, under any patent claims the licensor can
|
|
35
|
+
license, or becomes able to license, to make, have made, use, sell, offer for
|
|
36
|
+
sale, import and have imported the software, in each case subject to the
|
|
37
|
+
limitations and conditions in this license. This license does not cover any
|
|
38
|
+
patent claims that you cause to be infringed by modifications or additions to
|
|
39
|
+
the software. If you or your company make any written claim that the software
|
|
40
|
+
infringes or contributes to infringement of any patent, your patent license for
|
|
41
|
+
the software granted under these terms ends immediately. If your company makes
|
|
42
|
+
such a claim, your patent license ends immediately for work on behalf of your
|
|
43
|
+
company.
|
|
44
|
+
|
|
45
|
+
## Notices
|
|
46
|
+
|
|
47
|
+
You must ensure that anyone who gets a copy of any part of the software from you
|
|
48
|
+
also gets a copy of these terms.
|
|
49
|
+
|
|
50
|
+
If you modify the software, you must include in any modified copies of the
|
|
51
|
+
software prominent notices stating that you have modified the software.
|
|
52
|
+
|
|
53
|
+
## No Other Rights
|
|
54
|
+
|
|
55
|
+
These terms do not imply any licenses other than those expressly granted in
|
|
56
|
+
these terms.
|
|
57
|
+
|
|
58
|
+
## Termination
|
|
59
|
+
|
|
60
|
+
If you use the software in violation of these terms, such use is not licensed,
|
|
61
|
+
and your licenses will automatically terminate. If the licensor provides you
|
|
62
|
+
with a notice of your violation, and you cease all violation of this license no
|
|
63
|
+
later than 30 days after you receive that notice, your licenses will be
|
|
64
|
+
reinstated retroactively. However, if you violate these terms after such
|
|
65
|
+
reinstatement, any additional violation of these terms will cause your licenses
|
|
66
|
+
to terminate automatically and permanently.
|
|
67
|
+
|
|
68
|
+
## No Liability
|
|
69
|
+
|
|
70
|
+
_As far as the law allows, the software comes as is, without any warranty or
|
|
71
|
+
condition, and the licensor will not be liable to you for any damages arising
|
|
72
|
+
out of these terms or the use or nature of the software, under any kind of
|
|
73
|
+
legal claim._
|
|
74
|
+
|
|
75
|
+
## Definitions
|
|
76
|
+
|
|
77
|
+
The **licensor** is the entity offering these terms, and the **software** is the
|
|
78
|
+
software the licensor makes available under these terms, including any portion
|
|
79
|
+
of it.
|
|
80
|
+
|
|
81
|
+
**you** refers to the individual or entity agreeing to these terms.
|
|
82
|
+
|
|
83
|
+
**your company** is any legal entity, sole proprietorship, or other kind of
|
|
84
|
+
organization that you work for, plus all organizations that have control over,
|
|
85
|
+
are under the control of, or are under common control with that
|
|
86
|
+
organization. **control** means ownership of substantially all the assets of an
|
|
87
|
+
entity, or the power to direct its management and policies by vote, contract, or
|
|
88
|
+
otherwise. Control can be direct or indirect.
|
|
89
|
+
|
|
90
|
+
**your licenses** are all the licenses granted to you for the software under
|
|
91
|
+
these terms.
|
|
92
|
+
|
|
93
|
+
**use** means anything you do with the software requiring one of your licenses.
|
|
94
|
+
|
|
95
|
+
**trademark** means trademarks, service marks, and similar rights.
|
package/README.md
ADDED
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
# Nitro Server Adapter Plugin
|
|
2
|
+
|
|
3
|
+
_Server Adapter Plugin for Nitro GraphQL IDE_
|
|
4
|
+
|
|
5
|
+
## Description
|
|
6
|
+
|
|
7
|
+
This plugin allows to setup _Nitro GraphQL IDE_ for your GraphQL Yoga server or any other server that implements the Server Adapter (see `@whatwg-node`).
|
|
8
|
+
|
|
9
|
+
You can use a **cdn** hosted version of the app or a **self** hosted version using the ad hoc package.
|
|
10
|
+
|
|
11
|
+
## Installation
|
|
12
|
+
|
|
13
|
+
Install this package and the required `peerDependencies` in your project:
|
|
14
|
+
|
|
15
|
+
```sh
|
|
16
|
+
npm install @chillicream/nitro-server-adapter-plugin --save-dev
|
|
17
|
+
# or
|
|
18
|
+
yarn add @chillicream/nitro-server-adapter-plugin --dev
|
|
19
|
+
# or
|
|
20
|
+
pnpm add @chillicream/nitro-server-adapter-plugin --save-dev
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
**Note**: `@chillicream/nitro-embedded` is optional and only needed if you prefer to **self** host the app.
|
|
24
|
+
|
|
25
|
+
## Usage
|
|
26
|
+
|
|
27
|
+
Add the plugin to your server.
|
|
28
|
+
|
|
29
|
+
```javascript
|
|
30
|
+
import { useNitro } from "@chillicream/nitro-server-adapter-plugin";
|
|
31
|
+
import { createYoga } from "graphql-yoga";
|
|
32
|
+
import { createServer } from "node:http";
|
|
33
|
+
|
|
34
|
+
const yoga = createYoga({
|
|
35
|
+
schema: // Schema omitted for brevity,
|
|
36
|
+
plugins: [useNitro()], // Adds Nitro
|
|
37
|
+
graphiql: false, // Disables the default GraphQL IDE
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
const server = createServer(yoga);
|
|
41
|
+
|
|
42
|
+
server.listen(3000, () => {
|
|
43
|
+
console.log("Running a GraphQL API server at http://localhost:3000/graphql");
|
|
44
|
+
});
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
### Extended configuration
|
|
48
|
+
|
|
49
|
+
- To pin a specific version instead of using "latest":
|
|
50
|
+
|
|
51
|
+
```javascript
|
|
52
|
+
useNitro({
|
|
53
|
+
mode: "cdn",
|
|
54
|
+
target: { version: "x.0.0" },
|
|
55
|
+
});
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
- To use your own infrastructure:
|
|
59
|
+
|
|
60
|
+
```javascript
|
|
61
|
+
useNitro({
|
|
62
|
+
mode: "cdn",
|
|
63
|
+
target: "https://mycompany.com/nitro",
|
|
64
|
+
});
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
### Custom options
|
|
68
|
+
|
|
69
|
+
- To pass `options` supported by _Nitro GraphQL IDE_:
|
|
70
|
+
|
|
71
|
+
```javascript
|
|
72
|
+
useNitro({
|
|
73
|
+
mode: "cdn",
|
|
74
|
+
options: {
|
|
75
|
+
title: "Nitro",
|
|
76
|
+
},
|
|
77
|
+
});
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
## Recipes
|
|
81
|
+
|
|
82
|
+
<details id="graphql-yoga">
|
|
83
|
+
<summary><a href="#graphql-yoga"><sub><sup>🔗</sup></sub></a> With <a href="https://the-guild.dev/graphql/yoga-server">graphql-yoga</a></summary>
|
|
84
|
+
<hr>
|
|
85
|
+
|
|
86
|
+
```javascript
|
|
87
|
+
import { useNitro } from "@chillicream/nitro-server-adapter-plugin";
|
|
88
|
+
import { createYoga } from "graphql-yoga";
|
|
89
|
+
import { createServer } from "node:http";
|
|
90
|
+
|
|
91
|
+
const yoga = createYoga({
|
|
92
|
+
schema: createSchema({
|
|
93
|
+
typeDefs: /* GraphQL */ `
|
|
94
|
+
type Query {
|
|
95
|
+
greeting: String
|
|
96
|
+
}
|
|
97
|
+
`,
|
|
98
|
+
resolvers: {
|
|
99
|
+
Query: {
|
|
100
|
+
greeting: () => "Hello, World!",
|
|
101
|
+
},
|
|
102
|
+
},
|
|
103
|
+
}),
|
|
104
|
+
plugins: [useNitro()], // Adds Nitro
|
|
105
|
+
graphiql: false, // Disables the default GraphQL IDE
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
const server = createServer(yoga);
|
|
109
|
+
|
|
110
|
+
server.listen(3000, () => {
|
|
111
|
+
console.log("Running a GraphQL API server at http://localhost:3000/graphql");
|
|
112
|
+
});
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
<hr>
|
|
116
|
+
</details>
|
|
117
|
+
|
|
118
|
+
<details id="graphql-yoga-with-grats">
|
|
119
|
+
<summary><a href="#graphql-yoga-with-grats"><sub><sup>🔗</sup></sub></a> With <a href="https://the-guild.dev/graphql/yoga-server">graphql-yoga</a> and <a href="https://grats.capt.dev/">grats</a></summary>
|
|
120
|
+
<hr>
|
|
121
|
+
|
|
122
|
+
```typescript
|
|
123
|
+
import { useNitro } from "@chillicream/nitro-server-adapter-plugin";
|
|
124
|
+
import { createYoga, createSchema } from "graphql-yoga";
|
|
125
|
+
import { createServer } from "node:http";
|
|
126
|
+
import { getSchema } from "./schema"; // Will be generated by Grats
|
|
127
|
+
|
|
128
|
+
/** @gqlType */
|
|
129
|
+
type Query = unknown;
|
|
130
|
+
|
|
131
|
+
/** @gqlField */
|
|
132
|
+
export function hello(_: Query): string {
|
|
133
|
+
return "Hello world!";
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
const yoga = createYoga({
|
|
137
|
+
schema: getSchema(),
|
|
138
|
+
plugins: [useNitro()], // Adds Nitro
|
|
139
|
+
graphiql: false, // Disables the default GraphQL IDE
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
const server = createServer(yoga);
|
|
143
|
+
|
|
144
|
+
server.listen(3000, () => {
|
|
145
|
+
console.log("Running a GraphQL API server at http://localhost:3000/graphql");
|
|
146
|
+
});
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
<hr>
|
|
150
|
+
</details>
|
package/index.cjs
ADDED
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* @license ChilliCream License 1.0
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) ChilliCream, Inc.
|
|
5
|
+
*
|
|
6
|
+
* This source code is licensed under the ChilliCream License 1.0 found in the
|
|
7
|
+
* LICENSE file in the root directory of this source tree.
|
|
8
|
+
*/
|
|
9
|
+
'use strict';
|
|
10
|
+
|
|
11
|
+
var fs = require('node:fs');
|
|
12
|
+
var module$1 = require('node:module');
|
|
13
|
+
var path = require('node:path');
|
|
14
|
+
|
|
15
|
+
const Defaults = {
|
|
16
|
+
CDN_URL: "https://cdn.chillicream.com/web",
|
|
17
|
+
VERSION: "latest",
|
|
18
|
+
EMBEDDED_ID: "@chillicream/nitro-embedded",
|
|
19
|
+
CONFIG: {
|
|
20
|
+
mode: "cdn",
|
|
21
|
+
},
|
|
22
|
+
OPTIONS: {
|
|
23
|
+
useBrowserUrlAsEndpoint: true,
|
|
24
|
+
},
|
|
25
|
+
};
|
|
26
|
+
const Paths = {
|
|
27
|
+
ROOT: "/",
|
|
28
|
+
INDEX: "/index.html",
|
|
29
|
+
CONFIG: "/nitro-config.json",
|
|
30
|
+
};
|
|
31
|
+
function resolveUrl(target) {
|
|
32
|
+
return typeof target === "string"
|
|
33
|
+
? target
|
|
34
|
+
: `${target?.baseUrl || Defaults.CDN_URL}/${target?.version || Defaults.VERSION}`;
|
|
35
|
+
}
|
|
36
|
+
function resolvePath(target) {
|
|
37
|
+
const require$1 = module$1.createRequire(require('url').pathToFileURL(__filename).toString());
|
|
38
|
+
return path.dirname(require$1.resolve(target || Defaults.EMBEDDED_ID));
|
|
39
|
+
}
|
|
40
|
+
function resolveConfig(config) {
|
|
41
|
+
return Object.assign({}, Defaults.CONFIG, config);
|
|
42
|
+
}
|
|
43
|
+
function resolveOptions(options) {
|
|
44
|
+
return Object.assign({}, Defaults.OPTIONS, options);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function useNitro(config) {
|
|
48
|
+
const resolvedConfig = resolveConfig(config);
|
|
49
|
+
const basePath = resolveBasePath(resolvedConfig.path);
|
|
50
|
+
return {
|
|
51
|
+
onRequest({ request, url, fetchAPI, endResponse }) {
|
|
52
|
+
if (request.method === "GET" || request.method === "HEAD") {
|
|
53
|
+
const acceptHeader = request.headers.get("accept");
|
|
54
|
+
// handle root request
|
|
55
|
+
if (url.pathname === basePath &&
|
|
56
|
+
(acceptHeader?.includes("*/*") || acceptHeader?.includes("text/html"))) {
|
|
57
|
+
const [path, query] = request.url.split("?");
|
|
58
|
+
if (!path.endsWith("/")) {
|
|
59
|
+
const newPath = path + "/" + (query?.length ? "?" + query : "");
|
|
60
|
+
return endResponse(fetchAPI.Response.redirect(newPath, 301));
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
const configPath = basePath + Paths.CONFIG;
|
|
64
|
+
// handle config request
|
|
65
|
+
if (url.pathname === configPath &&
|
|
66
|
+
(acceptHeader?.includes("*/*") ||
|
|
67
|
+
acceptHeader?.includes("application/json"))) {
|
|
68
|
+
const resolvedOptions = resolveOptions(resolvedConfig.options);
|
|
69
|
+
const responseMetadata = {
|
|
70
|
+
status: 200,
|
|
71
|
+
statusText: "OK",
|
|
72
|
+
headers: {
|
|
73
|
+
"Content-Type": "application/json",
|
|
74
|
+
"Content-Length": JSON.stringify(resolvedOptions).length + "",
|
|
75
|
+
},
|
|
76
|
+
};
|
|
77
|
+
if (request.method === "GET") {
|
|
78
|
+
return endResponse(fetchAPI.Response.json(resolvedOptions, responseMetadata));
|
|
79
|
+
}
|
|
80
|
+
else {
|
|
81
|
+
return endResponse(new fetchAPI.Response(undefined, responseMetadata));
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
const pathname = url.pathname.substring(basePath.length);
|
|
85
|
+
if (resolvedConfig.mode === "cdn") {
|
|
86
|
+
const baseUrl = resolveUrl(resolvedConfig.target);
|
|
87
|
+
const requestHeaders = new Headers(request.headers);
|
|
88
|
+
requestHeaders.delete("host");
|
|
89
|
+
requestHeaders.delete("accept-encoding");
|
|
90
|
+
return fetchAPI
|
|
91
|
+
.fetch(baseUrl + pathname, {
|
|
92
|
+
method: request.method,
|
|
93
|
+
headers: requestHeaders,
|
|
94
|
+
body: request.body,
|
|
95
|
+
mode: request.mode,
|
|
96
|
+
credentials: request.credentials,
|
|
97
|
+
cache: request.cache,
|
|
98
|
+
redirect: request.redirect,
|
|
99
|
+
referrer: request.referrer,
|
|
100
|
+
referrerPolicy: request.referrerPolicy,
|
|
101
|
+
integrity: request.integrity,
|
|
102
|
+
keepalive: request.keepalive,
|
|
103
|
+
// NOTE: if uncommented, leads to an unhandled exception in the
|
|
104
|
+
// fetchNodeHttp module; needs to be investigated!
|
|
105
|
+
//signal: request.signal,
|
|
106
|
+
})
|
|
107
|
+
.then(endResponse, () => endResponse(fetchAPI.Response.error()));
|
|
108
|
+
}
|
|
109
|
+
else {
|
|
110
|
+
const baseFilePath = resolvePath(resolvedConfig.target);
|
|
111
|
+
return serverStatic(pathname, baseFilePath, fetchAPI, endResponse);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
},
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
function resolveBasePath(path) {
|
|
118
|
+
return path?.length
|
|
119
|
+
? ensureStartsWithSlash(ensureEndsNotWithSlash(path))
|
|
120
|
+
: "/graphql";
|
|
121
|
+
}
|
|
122
|
+
function ensureStartsWithSlash(path) {
|
|
123
|
+
return path.startsWith("/") ? path : "/" + path;
|
|
124
|
+
}
|
|
125
|
+
function ensureEndsNotWithSlash(path) {
|
|
126
|
+
return path.endsWith("/") && path !== Paths.ROOT
|
|
127
|
+
? path.substring(0, path.length - 1)
|
|
128
|
+
: path;
|
|
129
|
+
}
|
|
130
|
+
function serverStatic(path, baseFilePath, fetchAPI, endResponse) {
|
|
131
|
+
function write(filePath, contentType) {
|
|
132
|
+
try {
|
|
133
|
+
const data = fs.readFileSync(filePath);
|
|
134
|
+
return endResponse(new fetchAPI.Response(data, {
|
|
135
|
+
status: 200,
|
|
136
|
+
statusText: "OK",
|
|
137
|
+
headers: {
|
|
138
|
+
"Content-Type": contentType,
|
|
139
|
+
"Content-Length": data.length + "",
|
|
140
|
+
},
|
|
141
|
+
}));
|
|
142
|
+
}
|
|
143
|
+
catch { }
|
|
144
|
+
}
|
|
145
|
+
if (path.endsWith(".css")) {
|
|
146
|
+
return write(baseFilePath + path, "text/css");
|
|
147
|
+
}
|
|
148
|
+
if (path.endsWith(".html")) {
|
|
149
|
+
return write(baseFilePath + path, "text/html");
|
|
150
|
+
}
|
|
151
|
+
if (path.endsWith(".ico")) {
|
|
152
|
+
return write(baseFilePath + path, "image/x-icon");
|
|
153
|
+
}
|
|
154
|
+
if (path.endsWith(".js")) {
|
|
155
|
+
return write(baseFilePath + path, "application/javascript");
|
|
156
|
+
}
|
|
157
|
+
if (path.endsWith(".json")) {
|
|
158
|
+
return write(baseFilePath + path, "application/json");
|
|
159
|
+
}
|
|
160
|
+
if (path.endsWith(".png")) {
|
|
161
|
+
return write(baseFilePath + path, "image/png");
|
|
162
|
+
}
|
|
163
|
+
if (path.endsWith(".svg")) {
|
|
164
|
+
return write(baseFilePath + path, "image/svg+xml");
|
|
165
|
+
}
|
|
166
|
+
if (path.endsWith(".ttf")) {
|
|
167
|
+
return write(baseFilePath + path, "font/ttf");
|
|
168
|
+
}
|
|
169
|
+
if (path.endsWith(".txt")) {
|
|
170
|
+
return write(baseFilePath + path, "text/plain");
|
|
171
|
+
}
|
|
172
|
+
if (path.endsWith(".webmanifest")) {
|
|
173
|
+
return write(baseFilePath + path, "application/manifest+json");
|
|
174
|
+
}
|
|
175
|
+
if (path.endsWith(".woff2")) {
|
|
176
|
+
return write(baseFilePath + path, "font/woff2");
|
|
177
|
+
}
|
|
178
|
+
if (path.endsWith(".woff2")) {
|
|
179
|
+
return write(baseFilePath + path, "font/woff2");
|
|
180
|
+
}
|
|
181
|
+
if (path === Paths.ROOT) {
|
|
182
|
+
return write(baseFilePath + "/index.html", "text/html");
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
exports.resolveBasePath = resolveBasePath;
|
|
187
|
+
exports.useNitro = useNitro;
|
package/index.d.cts
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { ServerAdapterPlugin } from "@whatwg-node/server";
|
|
2
|
+
interface HttpHeaderDictionary {
|
|
3
|
+
readonly [key: string]: string | string[];
|
|
4
|
+
}
|
|
5
|
+
type SubscriptionProtocol = "auto" | "graphql-transport-ws" | "graphql-ws" | "graphql-sse";
|
|
6
|
+
interface Options {
|
|
7
|
+
readonly title?: string;
|
|
8
|
+
readonly disableTelemetry?: boolean;
|
|
9
|
+
readonly gaTrackingId?: string;
|
|
10
|
+
readonly useBrowserUrlAsEndpoint: boolean;
|
|
11
|
+
readonly includeCookies?: boolean;
|
|
12
|
+
readonly useGet?: boolean;
|
|
13
|
+
readonly subscriptionProtocol?: SubscriptionProtocol;
|
|
14
|
+
readonly sdlEndpoint?: string;
|
|
15
|
+
readonly endpoint?: string;
|
|
16
|
+
readonly httpHeaders?: HttpHeaderDictionary;
|
|
17
|
+
readonly graphQLDocument?: string;
|
|
18
|
+
readonly variables?: Record<string, any>;
|
|
19
|
+
}
|
|
20
|
+
type Config = {
|
|
21
|
+
readonly mode: "cdn";
|
|
22
|
+
/** Endpoint **url** for the `cdn` hosted version. */
|
|
23
|
+
readonly target?: {
|
|
24
|
+
baseUrl: string;
|
|
25
|
+
version?: string;
|
|
26
|
+
} | string;
|
|
27
|
+
readonly options?: Options;
|
|
28
|
+
} | {
|
|
29
|
+
readonly mode: "embedded";
|
|
30
|
+
/** Package **id** or **path** for the `embedded` version. */
|
|
31
|
+
readonly target?: string;
|
|
32
|
+
readonly options?: Options;
|
|
33
|
+
};
|
|
34
|
+
type NitroConfig = Config & {
|
|
35
|
+
/** a path on this plugin is hosted; default is `/graphql` */
|
|
36
|
+
readonly path?: string;
|
|
37
|
+
};
|
|
38
|
+
declare function useNitro<TServerContext>(config?: NitroConfig): ServerAdapterPlugin<TServerContext>;
|
|
39
|
+
declare function resolveBasePath(path?: string): string;
|
|
40
|
+
export { NitroConfig, useNitro, resolveBasePath };
|
package/index.d.mts
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { ServerAdapterPlugin } from "@whatwg-node/server";
|
|
2
|
+
interface HttpHeaderDictionary {
|
|
3
|
+
readonly [key: string]: string | string[];
|
|
4
|
+
}
|
|
5
|
+
type SubscriptionProtocol = "auto" | "graphql-transport-ws" | "graphql-ws" | "graphql-sse";
|
|
6
|
+
interface Options {
|
|
7
|
+
readonly title?: string;
|
|
8
|
+
readonly disableTelemetry?: boolean;
|
|
9
|
+
readonly gaTrackingId?: string;
|
|
10
|
+
readonly useBrowserUrlAsEndpoint: boolean;
|
|
11
|
+
readonly includeCookies?: boolean;
|
|
12
|
+
readonly useGet?: boolean;
|
|
13
|
+
readonly subscriptionProtocol?: SubscriptionProtocol;
|
|
14
|
+
readonly sdlEndpoint?: string;
|
|
15
|
+
readonly endpoint?: string;
|
|
16
|
+
readonly httpHeaders?: HttpHeaderDictionary;
|
|
17
|
+
readonly graphQLDocument?: string;
|
|
18
|
+
readonly variables?: Record<string, any>;
|
|
19
|
+
}
|
|
20
|
+
type Config = {
|
|
21
|
+
readonly mode: "cdn";
|
|
22
|
+
/** Endpoint **url** for the `cdn` hosted version. */
|
|
23
|
+
readonly target?: {
|
|
24
|
+
baseUrl: string;
|
|
25
|
+
version?: string;
|
|
26
|
+
} | string;
|
|
27
|
+
readonly options?: Options;
|
|
28
|
+
} | {
|
|
29
|
+
readonly mode: "embedded";
|
|
30
|
+
/** Package **id** or **path** for the `embedded` version. */
|
|
31
|
+
readonly target?: string;
|
|
32
|
+
readonly options?: Options;
|
|
33
|
+
};
|
|
34
|
+
type NitroConfig = Config & {
|
|
35
|
+
/** a path on this plugin is hosted; default is `/graphql` */
|
|
36
|
+
readonly path?: string;
|
|
37
|
+
};
|
|
38
|
+
declare function useNitro<TServerContext>(config?: NitroConfig): ServerAdapterPlugin<TServerContext>;
|
|
39
|
+
declare function resolveBasePath(path?: string): string;
|
|
40
|
+
export { NitroConfig, useNitro, resolveBasePath };
|
package/index.mjs
ADDED
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* @license ChilliCream License 1.0
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) ChilliCream, Inc.
|
|
5
|
+
*
|
|
6
|
+
* This source code is licensed under the ChilliCream License 1.0 found in the
|
|
7
|
+
* LICENSE file in the root directory of this source tree.
|
|
8
|
+
*/
|
|
9
|
+
import fs from 'node:fs';
|
|
10
|
+
import module from 'node:module';
|
|
11
|
+
import path from 'node:path';
|
|
12
|
+
|
|
13
|
+
const Defaults = {
|
|
14
|
+
CDN_URL: "https://cdn.chillicream.com/web",
|
|
15
|
+
VERSION: "latest",
|
|
16
|
+
EMBEDDED_ID: "@chillicream/nitro-embedded",
|
|
17
|
+
CONFIG: {
|
|
18
|
+
mode: "cdn",
|
|
19
|
+
},
|
|
20
|
+
OPTIONS: {
|
|
21
|
+
useBrowserUrlAsEndpoint: true,
|
|
22
|
+
},
|
|
23
|
+
};
|
|
24
|
+
const Paths = {
|
|
25
|
+
ROOT: "/",
|
|
26
|
+
INDEX: "/index.html",
|
|
27
|
+
CONFIG: "/nitro-config.json",
|
|
28
|
+
};
|
|
29
|
+
function resolveUrl(target) {
|
|
30
|
+
return typeof target === "string"
|
|
31
|
+
? target
|
|
32
|
+
: `${target?.baseUrl || Defaults.CDN_URL}/${target?.version || Defaults.VERSION}`;
|
|
33
|
+
}
|
|
34
|
+
function resolvePath(target) {
|
|
35
|
+
const require = module.createRequire(import.meta.url);
|
|
36
|
+
return path.dirname(require.resolve(target || Defaults.EMBEDDED_ID));
|
|
37
|
+
}
|
|
38
|
+
function resolveConfig(config) {
|
|
39
|
+
return Object.assign({}, Defaults.CONFIG, config);
|
|
40
|
+
}
|
|
41
|
+
function resolveOptions(options) {
|
|
42
|
+
return Object.assign({}, Defaults.OPTIONS, options);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function useNitro(config) {
|
|
46
|
+
const resolvedConfig = resolveConfig(config);
|
|
47
|
+
const basePath = resolveBasePath(resolvedConfig.path);
|
|
48
|
+
return {
|
|
49
|
+
onRequest({ request, url, fetchAPI, endResponse }) {
|
|
50
|
+
if (request.method === "GET" || request.method === "HEAD") {
|
|
51
|
+
const acceptHeader = request.headers.get("accept");
|
|
52
|
+
// handle root request
|
|
53
|
+
if (url.pathname === basePath &&
|
|
54
|
+
(acceptHeader?.includes("*/*") || acceptHeader?.includes("text/html"))) {
|
|
55
|
+
const [path, query] = request.url.split("?");
|
|
56
|
+
if (!path.endsWith("/")) {
|
|
57
|
+
const newPath = path + "/" + (query?.length ? "?" + query : "");
|
|
58
|
+
return endResponse(fetchAPI.Response.redirect(newPath, 301));
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
const configPath = basePath + Paths.CONFIG;
|
|
62
|
+
// handle config request
|
|
63
|
+
if (url.pathname === configPath &&
|
|
64
|
+
(acceptHeader?.includes("*/*") ||
|
|
65
|
+
acceptHeader?.includes("application/json"))) {
|
|
66
|
+
const resolvedOptions = resolveOptions(resolvedConfig.options);
|
|
67
|
+
const responseMetadata = {
|
|
68
|
+
status: 200,
|
|
69
|
+
statusText: "OK",
|
|
70
|
+
headers: {
|
|
71
|
+
"Content-Type": "application/json",
|
|
72
|
+
"Content-Length": JSON.stringify(resolvedOptions).length + "",
|
|
73
|
+
},
|
|
74
|
+
};
|
|
75
|
+
if (request.method === "GET") {
|
|
76
|
+
return endResponse(fetchAPI.Response.json(resolvedOptions, responseMetadata));
|
|
77
|
+
}
|
|
78
|
+
else {
|
|
79
|
+
return endResponse(new fetchAPI.Response(undefined, responseMetadata));
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
const pathname = url.pathname.substring(basePath.length);
|
|
83
|
+
if (resolvedConfig.mode === "cdn") {
|
|
84
|
+
const baseUrl = resolveUrl(resolvedConfig.target);
|
|
85
|
+
const requestHeaders = new Headers(request.headers);
|
|
86
|
+
requestHeaders.delete("host");
|
|
87
|
+
requestHeaders.delete("accept-encoding");
|
|
88
|
+
return fetchAPI
|
|
89
|
+
.fetch(baseUrl + pathname, {
|
|
90
|
+
method: request.method,
|
|
91
|
+
headers: requestHeaders,
|
|
92
|
+
body: request.body,
|
|
93
|
+
mode: request.mode,
|
|
94
|
+
credentials: request.credentials,
|
|
95
|
+
cache: request.cache,
|
|
96
|
+
redirect: request.redirect,
|
|
97
|
+
referrer: request.referrer,
|
|
98
|
+
referrerPolicy: request.referrerPolicy,
|
|
99
|
+
integrity: request.integrity,
|
|
100
|
+
keepalive: request.keepalive,
|
|
101
|
+
// NOTE: if uncommented, leads to an unhandled exception in the
|
|
102
|
+
// fetchNodeHttp module; needs to be investigated!
|
|
103
|
+
//signal: request.signal,
|
|
104
|
+
})
|
|
105
|
+
.then(endResponse, () => endResponse(fetchAPI.Response.error()));
|
|
106
|
+
}
|
|
107
|
+
else {
|
|
108
|
+
const baseFilePath = resolvePath(resolvedConfig.target);
|
|
109
|
+
return serverStatic(pathname, baseFilePath, fetchAPI, endResponse);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
},
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
function resolveBasePath(path) {
|
|
116
|
+
return path?.length
|
|
117
|
+
? ensureStartsWithSlash(ensureEndsNotWithSlash(path))
|
|
118
|
+
: "/graphql";
|
|
119
|
+
}
|
|
120
|
+
function ensureStartsWithSlash(path) {
|
|
121
|
+
return path.startsWith("/") ? path : "/" + path;
|
|
122
|
+
}
|
|
123
|
+
function ensureEndsNotWithSlash(path) {
|
|
124
|
+
return path.endsWith("/") && path !== Paths.ROOT
|
|
125
|
+
? path.substring(0, path.length - 1)
|
|
126
|
+
: path;
|
|
127
|
+
}
|
|
128
|
+
function serverStatic(path, baseFilePath, fetchAPI, endResponse) {
|
|
129
|
+
function write(filePath, contentType) {
|
|
130
|
+
try {
|
|
131
|
+
const data = fs.readFileSync(filePath);
|
|
132
|
+
return endResponse(new fetchAPI.Response(data, {
|
|
133
|
+
status: 200,
|
|
134
|
+
statusText: "OK",
|
|
135
|
+
headers: {
|
|
136
|
+
"Content-Type": contentType,
|
|
137
|
+
"Content-Length": data.length + "",
|
|
138
|
+
},
|
|
139
|
+
}));
|
|
140
|
+
}
|
|
141
|
+
catch { }
|
|
142
|
+
}
|
|
143
|
+
if (path.endsWith(".css")) {
|
|
144
|
+
return write(baseFilePath + path, "text/css");
|
|
145
|
+
}
|
|
146
|
+
if (path.endsWith(".html")) {
|
|
147
|
+
return write(baseFilePath + path, "text/html");
|
|
148
|
+
}
|
|
149
|
+
if (path.endsWith(".ico")) {
|
|
150
|
+
return write(baseFilePath + path, "image/x-icon");
|
|
151
|
+
}
|
|
152
|
+
if (path.endsWith(".js")) {
|
|
153
|
+
return write(baseFilePath + path, "application/javascript");
|
|
154
|
+
}
|
|
155
|
+
if (path.endsWith(".json")) {
|
|
156
|
+
return write(baseFilePath + path, "application/json");
|
|
157
|
+
}
|
|
158
|
+
if (path.endsWith(".png")) {
|
|
159
|
+
return write(baseFilePath + path, "image/png");
|
|
160
|
+
}
|
|
161
|
+
if (path.endsWith(".svg")) {
|
|
162
|
+
return write(baseFilePath + path, "image/svg+xml");
|
|
163
|
+
}
|
|
164
|
+
if (path.endsWith(".ttf")) {
|
|
165
|
+
return write(baseFilePath + path, "font/ttf");
|
|
166
|
+
}
|
|
167
|
+
if (path.endsWith(".txt")) {
|
|
168
|
+
return write(baseFilePath + path, "text/plain");
|
|
169
|
+
}
|
|
170
|
+
if (path.endsWith(".webmanifest")) {
|
|
171
|
+
return write(baseFilePath + path, "application/manifest+json");
|
|
172
|
+
}
|
|
173
|
+
if (path.endsWith(".woff2")) {
|
|
174
|
+
return write(baseFilePath + path, "font/woff2");
|
|
175
|
+
}
|
|
176
|
+
if (path.endsWith(".woff2")) {
|
|
177
|
+
return write(baseFilePath + path, "font/woff2");
|
|
178
|
+
}
|
|
179
|
+
if (path === Paths.ROOT) {
|
|
180
|
+
return write(baseFilePath + "/index.html", "text/html");
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
export { resolveBasePath, useNitro };
|
package/package.json
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@chillicream/nitro-server-adapter-plugin",
|
|
3
|
+
"version": "19.0.0-insider.1",
|
|
4
|
+
"description": "Server Adapter Plugin for Nitro GraphQL IDE",
|
|
5
|
+
"homepage": "https://chillicream.com",
|
|
6
|
+
"repository": "github:ChilliCream/graphql-platform",
|
|
7
|
+
"keywords": [
|
|
8
|
+
"apollo",
|
|
9
|
+
"apollo-server",
|
|
10
|
+
"banana",
|
|
11
|
+
"bananacakepop",
|
|
12
|
+
"bcp",
|
|
13
|
+
"graphql",
|
|
14
|
+
"graphql-http",
|
|
15
|
+
"graphql-ide",
|
|
16
|
+
"graphql-yoga",
|
|
17
|
+
"middleware",
|
|
18
|
+
"editor",
|
|
19
|
+
"ide",
|
|
20
|
+
"server-adapter",
|
|
21
|
+
"server-adapter-plugin",
|
|
22
|
+
"whatwg-node",
|
|
23
|
+
"node",
|
|
24
|
+
"bun",
|
|
25
|
+
"deno"
|
|
26
|
+
],
|
|
27
|
+
"author": {
|
|
28
|
+
"name": "ChilliCream",
|
|
29
|
+
"url": "https://chillicream.com",
|
|
30
|
+
"email": "info@chillicream.com"
|
|
31
|
+
},
|
|
32
|
+
"license": "SEE LICENSE FILE",
|
|
33
|
+
"engines": {
|
|
34
|
+
"node": ">=12"
|
|
35
|
+
},
|
|
36
|
+
"type": "module",
|
|
37
|
+
"types": "index.d.mts",
|
|
38
|
+
"module": "index.mjs",
|
|
39
|
+
"main": "index.cjs",
|
|
40
|
+
"exports": {
|
|
41
|
+
".": {
|
|
42
|
+
"import": {
|
|
43
|
+
"types": "./index.d.mts",
|
|
44
|
+
"default": "./index.mjs"
|
|
45
|
+
},
|
|
46
|
+
"require": {
|
|
47
|
+
"types": "./index.d.cts",
|
|
48
|
+
"default": "./index.cjs"
|
|
49
|
+
},
|
|
50
|
+
"default": {
|
|
51
|
+
"types": "./index.d.mts",
|
|
52
|
+
"default": "./index.mjs"
|
|
53
|
+
}
|
|
54
|
+
},
|
|
55
|
+
"./package.json": "./package.json"
|
|
56
|
+
},
|
|
57
|
+
"peerDependencies": {
|
|
58
|
+
"@chillicream/nitro-embedded": "*",
|
|
59
|
+
"@whatwg-node/server": "*"
|
|
60
|
+
},
|
|
61
|
+
"peerDependenciesMeta": {
|
|
62
|
+
"@chillicream/nitro-embedded": {
|
|
63
|
+
"optional": true
|
|
64
|
+
}
|
|
65
|
+
},
|
|
66
|
+
"stableVersion": "0.0.0"
|
|
67
|
+
}
|