@harperfast/vite 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 +201 -0
- package/README.md +297 -0
- package/config.yaml +7 -0
- package/dist/auth.d.ts +49 -0
- package/dist/auth.js +174 -0
- package/dist/auth.js.map +1 -0
- package/dist/buildLock.d.ts +10 -0
- package/dist/buildLock.js +58 -0
- package/dist/buildLock.js.map +1 -0
- package/dist/development.d.ts +8 -0
- package/dist/development.js +139 -0
- package/dist/development.js.map +1 -0
- package/dist/http.d.ts +18 -0
- package/dist/http.js +87 -0
- package/dist/http.js.map +1 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +23 -0
- package/dist/index.js.map +1 -0
- package/dist/log.d.ts +9 -0
- package/dist/log.js +10 -0
- package/dist/log.js.map +1 -0
- package/dist/options.d.ts +19 -0
- package/dist/options.js +35 -0
- package/dist/options.js.map +1 -0
- package/dist/production.d.ts +12 -0
- package/dist/production.js +197 -0
- package/dist/production.js.map +1 -0
- package/dist/wrappers.d.ts +8 -0
- package/dist/wrappers.js +9 -0
- package/dist/wrappers.js.map +1 -0
- package/package.json +78 -0
- package/schema.graphql +7 -0
- package/schema.json +45 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,297 @@
|
|
|
1
|
+
# `@harperfast/vite`
|
|
2
|
+
|
|
3
|
+
## Overview
|
|
4
|
+
|
|
5
|
+
A Harper application integration that runs a [Vite](https://vite.dev/) frontend inside a Harper component — with hot module replacement (HMR) for local development and a real production build when deployed (including on Harper Fabric).
|
|
6
|
+
|
|
7
|
+
It runs in one of two modes, chosen automatically:
|
|
8
|
+
|
|
9
|
+
| Mode | When | Behavior |
|
|
10
|
+
| --------------------- | ------------------------------------- | ------------------------------------------------------------------------------------------------------------------------- |
|
|
11
|
+
| **Development** | `harper dev` (Harper sets `DEV_MODE`) | Vite dev server in middleware mode with **HMR**. |
|
|
12
|
+
| **Hybrid production** | `harper run` / Fabric (default) | Builds your app with Vite (the real production build). **No HMR**, but it **recompiles automatically on source changes.** |
|
|
13
|
+
|
|
14
|
+
**Serving the built output is delegated to Harper's built-in [`static`](https://docs.harperdb.io/) plugin**, configured alongside this plugin and pointed at the same Vite output directory. So the two run in parallel: this plugin **builds** (and, for SSR, **renders** the HTML document — which `static` can't do), while `static` **serves** the assets. For an SPA you don't even need a handler from this plugin in production — `static` serves everything.
|
|
15
|
+
|
|
16
|
+
This separation makes for a clean deploy story: the output directory is the only contract between the two. You can build on the node, **build once and ship the output** (`static` serves it; this plugin can stay idle), or **pre-build and still edit live** (this plugin rebuilds on change → `static`'s watcher picks it up).
|
|
17
|
+
|
|
18
|
+
## Usage
|
|
19
|
+
|
|
20
|
+
Install the plugin in your Harper application:
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
npm install @harperfast/vite --save-dev
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
In your Harper application's `config.yaml`, add the plugin **and** a `static` block pointed at your Vite build output directory (the plugin builds; `static` serves). List the plugin first so its dev server takes precedence in `harper dev`:
|
|
27
|
+
|
|
28
|
+
```yaml
|
|
29
|
+
# Builds the app (and renders HTML in SSR mode) into the `output` directory.
|
|
30
|
+
'@harperfast/vite':
|
|
31
|
+
package: '@harperfast/vite'
|
|
32
|
+
files: 'src/**/*'
|
|
33
|
+
output: 'dist'
|
|
34
|
+
|
|
35
|
+
# Serves the built assets from that same directory.
|
|
36
|
+
static:
|
|
37
|
+
files: 'dist/**'
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
> For an **SSR** app, add `index: false` to the `static` block so it serves only assets and lets the plugin render `index.html`. For an **SPA**, leave `index` on (and optionally set `notFound: index.html` for client-side routing).
|
|
41
|
+
|
|
42
|
+
Then point your `dev` script at Harper:
|
|
43
|
+
|
|
44
|
+
```jsonc
|
|
45
|
+
// package.json
|
|
46
|
+
"scripts": {
|
|
47
|
+
"dev": "harper dev .", // development mode (HMR)
|
|
48
|
+
"start": "harper run ." // hybrid production mode
|
|
49
|
+
}
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
Your application runs on [http://localhost:9926](http://localhost:9926/).
|
|
53
|
+
|
|
54
|
+
### How it works
|
|
55
|
+
|
|
56
|
+
1. Harper loads your application and calls the plugin's `handleApplication`.
|
|
57
|
+
2. The plugin picks development or hybrid-production mode from the [`hmr`](#hmr) option, defaulting to `DEV_MODE` (set by `harper dev`).
|
|
58
|
+
3. **Development:** a Vite dev server is created with `root` set to the application directory, `middlewareMode: true`, and HMR enabled. Your `vite.config.{js,ts,mjs,...}` is auto-resolved. The dev server handles all requests and falls through to Harper for the rest.
|
|
59
|
+
4. **Hybrid production:** the plugin runs `vite build` if the output is missing and — when `files` is configured — rebuilds whenever those files change. The `static` plugin serves the built assets; for SSR, this plugin additionally renders the HTML document for `text/html` navigations (everything else falls through to Harper).
|
|
60
|
+
|
|
61
|
+
## Configuration
|
|
62
|
+
|
|
63
|
+
To enable autocompletion and validation, reference the bundled schema:
|
|
64
|
+
|
|
65
|
+
```yaml
|
|
66
|
+
# yaml-language-server: $schema=node_modules/@harperfast/vite/schema.json
|
|
67
|
+
'@harperfast/vite':
|
|
68
|
+
package: '@harperfast/vite'
|
|
69
|
+
files: 'src/**/*'
|
|
70
|
+
output: 'dist'
|
|
71
|
+
ssr: 'src/entry-server.tsx'
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
### `output`
|
|
75
|
+
|
|
76
|
+
- **Type**: `string`
|
|
77
|
+
- **Optional** (defaults to `dist`)
|
|
78
|
+
|
|
79
|
+
The build output directory (relative to the app root) the plugin builds the client into. **Point your `static` block at the same directory** (e.g. `files: 'dist/**'`) — that pairing is the whole contract between the two plugins. The plugin sets Vite's build `outDir` to this value, so it takes precedence over any `outDir` in `vite.config`.
|
|
80
|
+
|
|
81
|
+
### `files`
|
|
82
|
+
|
|
83
|
+
- **Type**: `string | string[] | FilesOptionObject`
|
|
84
|
+
- **Optional**
|
|
85
|
+
|
|
86
|
+
A glob (or array of globs) of files to watch. In **hybrid production mode**, a change to any matched file triggers a rebuild. Rebuilds keep serving the previously built assets until the new build finishes, then swap atomically — so there's no downtime window. Omit it to disable rebuild-on-change (the app is built once at startup).
|
|
87
|
+
|
|
88
|
+
Builds are coordinated across Harper's worker threads through a small Harper table (`harperfast_vite.vite_build_info`, defined in the plugin's `schema.graphql`): one worker claims the build while the others wait, so the app is compiled **once per instance** rather than once per thread — important when running multi-threaded (e.g. on Harper Fabric).
|
|
89
|
+
|
|
90
|
+
### `ssr`
|
|
91
|
+
|
|
92
|
+
- **Type**: `string`
|
|
93
|
+
- **Optional**
|
|
94
|
+
|
|
95
|
+
Path (relative to the application root) to your **server-side render entry**, e.g. `src/entry-server.tsx`. When set, the plugin renders HTML on the server. When omitted, the app is treated as a client-only **SPA**, fully served by the `static` plugin in production.
|
|
96
|
+
|
|
97
|
+
### `hmr`
|
|
98
|
+
|
|
99
|
+
- **Type**: `boolean`
|
|
100
|
+
- **Optional** (defaults to `DEV_MODE`)
|
|
101
|
+
|
|
102
|
+
Whether to run the Vite dev server with **hot module replacement**. When omitted, it follows Harper's dev mode (the `DEV_MODE` env, set by `harper dev`). Set `hmr: true` to force the dev server, or `hmr: false` to force the hybrid production build (build + recompile-on-change) — useful for testing the production path locally without `DEV_MODE`.
|
|
103
|
+
|
|
104
|
+
> **Caching** is configured on the `static` plugin, not here — set `cacheControl`/`maxAge`/`immutable` (or `setHeaders`) on the `static` block. Content-hashed assets (under Vite's `assets` dir) are safe to cache long-term; the HTML document this plugin renders is always sent with `no-cache`.
|
|
105
|
+
|
|
106
|
+
## Security
|
|
107
|
+
|
|
108
|
+
This plugin compiles — and, for SSR, executes — your application **inside the Harper process at runtime** rather than shipping pre-built artifacts. That convenience has a few security implications worth understanding.
|
|
109
|
+
|
|
110
|
+
### Runtime compilation: treat the app directory as trusted code
|
|
111
|
+
|
|
112
|
+
In production the Harper process runs `vite build` at startup and — when [`files`](#files) is set — again on every change to a watched file. For an **SSR** app the resulting server bundle is then imported and its `render()` runs **in-process**. Consequences:
|
|
113
|
+
|
|
114
|
+
- **Write access to the watched source is code execution.** Anyone who can modify the files matched by `files` (or the app source generally) gains arbitrary code execution in the Harper process on the next rebuild (SSR), or controls the JavaScript served to every visitor (SPA). Ensure nothing that handles untrusted input — file uploads especially — can write into the app directory or the build output, and restrict filesystem permissions to your deploy process.
|
|
115
|
+
- **The whole build toolchain runs with Harper's privileges.** `vite.config.*`, every Vite/Rollup plugin, and any build-time dependency hook execute in the Harper process during the build. Pin and vet build-time dependencies (committed lockfile, `npm ci`); a supply-chain compromise there runs on your server, not in an isolated CI step.
|
|
116
|
+
- **Build-time env can leak into client JS.** Vite inlines `import.meta.env.VITE_*` and `define` values into the **client** bundle at build time. Because the build runs on the deployed node with production env present, never expose a server-only secret through a `VITE_`-prefixed variable or `define`.
|
|
117
|
+
|
|
118
|
+
If you prefer the traditional model, **build in CI and ship the output**: point `static` at the committed build directory and omit `files` so this plugin never compiles on the node (it stays idle while `static` serves the artifacts).
|
|
119
|
+
|
|
120
|
+
### HMR / the dev server is gated behind super*user auth — HTTP \_and* WebSocket
|
|
121
|
+
|
|
122
|
+
The Vite dev server (HMR mode) exposes powerful endpoints — on-the-fly module transforms and arbitrary file reads via `/@fs/` — and is not designed to face untrusted networks. This plugin therefore **requires Harper `super_user` credentials for the entire dev server: both its HTTP surface and its HMR WebSocket.** That gate is what lets you safely turn HMR on against a _deployed_ instance — e.g. from a cloud IDE — for a live-edit workflow when a local environment isn't an option.
|
|
123
|
+
|
|
124
|
+
- **Local development is unaffected.** Harper auto-authorizes loopback requests as super_user under `authentication.authorizeLocal` (the default in `harper dev`), so `localhost` just works.
|
|
125
|
+
- **Remote/exposed HTTP requests are challenged.** A request Harper did not authenticate as super_user receives a `401` with a `WWW-Authenticate: Basic` header; the browser then prompts for credentials, which are validated against Harper's user store. (This is why hitting the dev server from another device — e.g. a phone on the LAN — prompts for your admin login.)
|
|
126
|
+
- **The HMR WebSocket runs on Harper's port and is gated too.** Instead of Vite's default standalone WebSocket port, the plugin routes HMR over Harper's own port on a dedicated path and authorizes every upgrade as super_user — by trusting a loopback peer under `authorizeLocal` (so local `harper dev` needs no credentials, just like the HTTP surface), or, for a remote browser, by validating the `hdb-session` cookie Harper sets when you log in (or an `Authorization` header). So once the page has authenticated, the HMR socket connects under the same identity; an unauthenticated remote upgrade is refused and the socket closed. Nothing is exposed on a second port.
|
|
127
|
+
- **Host checking is relaxed by design.** Because every request is already gated, the plugin sets Vite's `server.allowedHosts: true` so HMR also works when reached at a non-localhost hostname; the super_user gate — not Vite's host allowlist — is what protects the surface.
|
|
128
|
+
- **Older Harper hosts fall back to a separate port.** If the host Harper is too old to expose the WebSocket `upgrade` hook this relies on, the plugin reverts to Vite's standalone WebSocket port, which is **not** gated (the plugin logs a warning). Keep it bound to localhost.
|
|
129
|
+
|
|
130
|
+
As always, only grant `super_user` to people you trust, and prefer the production build (`hmr: false`) for anything that doesn't specifically need live editing.
|
|
131
|
+
|
|
132
|
+
### SSR renders before authentication
|
|
133
|
+
|
|
134
|
+
The SSR HTML handler is intentionally reachable by **unauthenticated** users (a public page must render for anonymous visitors). Your `render(url)` runs with no authenticated user and receives the **raw, attacker-controllable request URL**. So:
|
|
135
|
+
|
|
136
|
+
- If a rendered page contains sensitive data, enforce authorization **inside** your render path — don't assume a logged-in user.
|
|
137
|
+
- Treat `url` as untrusted input: validate or encode it before using it for routing, data lookups, or reflecting it into markup (to avoid reflected XSS or unintended data access).
|
|
138
|
+
|
|
139
|
+
## Server-Side Rendering (SSR)
|
|
140
|
+
|
|
141
|
+
SSR follows Vite's standard [server rendering](https://vite.dev/guide/ssr) conventions. You provide two entries and an HTML template; the plugin wires them into Harper.
|
|
142
|
+
|
|
143
|
+
**`index.html`** — a placeholder marks where rendered markup is injected:
|
|
144
|
+
|
|
145
|
+
```html
|
|
146
|
+
<div id="app"><!--ssr-outlet--></div>
|
|
147
|
+
<script type="module" src="/src/entry-client.tsx"></script>
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
**`src/entry-server.tsx`** — exports `render`, returning the markup for a URL:
|
|
151
|
+
|
|
152
|
+
```tsx
|
|
153
|
+
import { renderToString } from 'react-dom/server';
|
|
154
|
+
import { App } from './App.tsx';
|
|
155
|
+
|
|
156
|
+
export function render(url: string): string {
|
|
157
|
+
// Use `url` to drive routing / per-request data loading.
|
|
158
|
+
return renderToString(<App />);
|
|
159
|
+
}
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
**`src/entry-client.tsx`** — hydrates the server-rendered DOM:
|
|
163
|
+
|
|
164
|
+
```tsx
|
|
165
|
+
import { hydrateRoot } from 'react-dom/client';
|
|
166
|
+
import { App } from './App.tsx';
|
|
167
|
+
|
|
168
|
+
hydrateRoot(document.getElementById('app')!, <App />);
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
Enable it with `ssr: 'src/entry-server.tsx'`. The plugin then:
|
|
172
|
+
|
|
173
|
+
- **Development:** serves assets via Vite, and for HTML navigations transforms `index.html` (`transformIndexHtml`), loads the entry with `ssrLoadModule` (so edits are reflected immediately, with HMR for client code), calls `render(url)`, and injects the result into `<!--ssr-outlet-->`.
|
|
174
|
+
- **Hybrid production:** builds the client bundle and a separate SSR bundle, then for HTML navigations imports the built server entry, calls `render(url)`, and injects it into the built `index.html`.
|
|
175
|
+
|
|
176
|
+
Because the plugin only renders HTML for requests that accept `text/html` (i.e. browser navigations) and falls through otherwise, your Harper resources and REST API remain reachable on the same port. Inside `render`, you can `await` data from Harper resources (e.g. `tables.Product.get(id)`) to render data-driven pages.
|
|
177
|
+
|
|
178
|
+
> See `test-fixture/` for a complete, runnable SSR example.
|
|
179
|
+
|
|
180
|
+
## Caching
|
|
181
|
+
|
|
182
|
+
Two complementary layers:
|
|
183
|
+
|
|
184
|
+
**1. HTTP caching (via the `static` plugin).** Assets are served by Harper's `static` plugin, which sets validators (`ETag`/`Last-Modified`) and supports `cacheControl`/`maxAge`/`immutable`/`setHeaders` options on its config block. Content-hashed assets (under Vite's `assets` dir) are safe to cache long-term — e.g. `static: { files: 'dist/**', cacheControl: 'public, max-age=31536000, immutable' }`. The HTML document this plugin renders for SSR is always sent with `Cache-Control: no-cache` so navigations see fresh markup.
|
|
185
|
+
|
|
186
|
+
**2. Harper data/render caching.** Use a Harper table with a TTL as a cache. Define it with the `@table(expiration:)` directive, then populate it on a miss — ideal for caching expensive SSR output or upstream API responses:
|
|
187
|
+
|
|
188
|
+
```graphql
|
|
189
|
+
# schema.graphql — entries automatically expire after 60 seconds.
|
|
190
|
+
type RenderCache @table(expiration: 60) @export {
|
|
191
|
+
path: ID @primaryKey
|
|
192
|
+
html: String
|
|
193
|
+
renderedAt: Date @createdTime
|
|
194
|
+
}
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
```js
|
|
198
|
+
// In your SSR render path: serve from cache, render + store on a miss.
|
|
199
|
+
async function renderCached(url) {
|
|
200
|
+
const cached = await tables.RenderCache.get(url);
|
|
201
|
+
if (cached) return cached.html;
|
|
202
|
+
|
|
203
|
+
const html = await render(url);
|
|
204
|
+
await tables.RenderCache.put({ path: url, html });
|
|
205
|
+
return html;
|
|
206
|
+
}
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
Call `tables.RenderCache.invalidate(path)` (or `delete`) to bust an entry when the underlying data changes. Records are evicted automatically once they pass `expiration`.
|
|
210
|
+
|
|
211
|
+
## Precompute
|
|
212
|
+
|
|
213
|
+
Avoid doing the same work on every request:
|
|
214
|
+
|
|
215
|
+
- **Computed fields** — derive presentation- or denormalization-friendly values declaratively with `@computed`, instead of storing and maintaining them. The `from` expression is evaluated per Harper's computed-field rules (see the [Harper schema docs](https://docs.harperdb.io/)):
|
|
216
|
+
|
|
217
|
+
```graphql
|
|
218
|
+
type Product @table @export {
|
|
219
|
+
id: ID @primaryKey
|
|
220
|
+
name: String
|
|
221
|
+
priceCents: Int
|
|
222
|
+
priceLabel: String @computed(from: "...") # derived from priceCents
|
|
223
|
+
}
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
- **Startup precompute** — work at module load in a `jsResource` runs once when the component starts, not per request. Use it to build manifests, warm caches, or precompute derived data, then expose it through a resource:
|
|
227
|
+
|
|
228
|
+
```js
|
|
229
|
+
// resources.js
|
|
230
|
+
const manifest = buildExpensiveManifest(); // runs once at startup
|
|
231
|
+
|
|
232
|
+
export class Manifest extends Resource {
|
|
233
|
+
get() {
|
|
234
|
+
return manifest;
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
- **Build-time pre-rendering (SSG)** — for fully static pages, render them during the build and let the static server serve the resulting HTML, skipping per-request rendering entirely.
|
|
240
|
+
|
|
241
|
+
See [`test-fixture/resources.js`](test-fixture/resources.js) and [`test-fixture/schema.graphql`](test-fixture/schema.graphql) for working examples.
|
|
242
|
+
|
|
243
|
+
## Migrating from `@harperfast/vite-plugin`
|
|
244
|
+
|
|
245
|
+
This package was previously published as `@harperfast/vite-plugin` (through `0.3.0-beta.9`). Starting with `1.0.0` it is `@harperfast/vite`. The plugin's own API and behavior are unchanged — steps 1–2 are the rename; steps 3–4 are a quick check that the rest of your setup is in the shape the plugin expects.
|
|
246
|
+
|
|
247
|
+
1. Swap the dependency:
|
|
248
|
+
|
|
249
|
+
```bash
|
|
250
|
+
npm uninstall @harperfast/vite-plugin
|
|
251
|
+
npm install @harperfast/vite --save-dev
|
|
252
|
+
```
|
|
253
|
+
|
|
254
|
+
2. Update `config.yaml` — both the component key and its `package`:
|
|
255
|
+
|
|
256
|
+
```diff
|
|
257
|
+
-'@harperfast/vite-plugin':
|
|
258
|
+
- package: '@harperfast/vite-plugin'
|
|
259
|
+
+'@harperfast/vite':
|
|
260
|
+
+ package: '@harperfast/vite'
|
|
261
|
+
files: 'src/**/*'
|
|
262
|
+
output: 'dist'
|
|
263
|
+
```
|
|
264
|
+
|
|
265
|
+
3. Make sure you have a `static` block. This plugin **builds** (and, in SSR mode, **renders** `index.html`); Harper's built-in `static` plugin **serves** the built assets. If you don't already have one, add it after the plugin block, pointed at the same directory as `output`:
|
|
266
|
+
|
|
267
|
+
```yaml
|
|
268
|
+
static:
|
|
269
|
+
files: 'dist/**' # same directory as the plugin's `output`
|
|
270
|
+
```
|
|
271
|
+
|
|
272
|
+
For an **SSR** app add `index: false` (the plugin renders `index.html`); for an **SPA** leave `index` on and optionally set `notFound: index.html` for client-side routing. See [Usage](#usage).
|
|
273
|
+
|
|
274
|
+
4. Review your `vite.config.ts`. Nothing is strictly required — the plugin auto-resolves it and overrides Vite's build `outDir` with its `output` when it builds — but set `build.outDir` to the same directory so a standalone `vite build` stays consistent, alongside your framework plugin(s):
|
|
275
|
+
|
|
276
|
+
```ts
|
|
277
|
+
import { defineConfig } from 'vite';
|
|
278
|
+
import react from '@vitejs/plugin-react'; // or your framework's Vite plugin
|
|
279
|
+
|
|
280
|
+
export default defineConfig({
|
|
281
|
+
plugins: [react()],
|
|
282
|
+
build: {
|
|
283
|
+
outDir: 'dist', // match the plugin's `output`
|
|
284
|
+
emptyOutDir: true,
|
|
285
|
+
},
|
|
286
|
+
});
|
|
287
|
+
```
|
|
288
|
+
|
|
289
|
+
`@harperfast/vite-plugin` is deprecated and will receive no further updates.
|
|
290
|
+
|
|
291
|
+
## Tools Used
|
|
292
|
+
|
|
293
|
+
1. [TypeScript](https://www.typescriptlang.org/) for static typing
|
|
294
|
+
2. [ESLint](https://eslint.org/) for linting
|
|
295
|
+
3. [Prettier](https://prettier.io/) for code formatting
|
|
296
|
+
4. [Vite](https://vite.dev/) for the development server, middleware, and production build
|
|
297
|
+
5. Harper's built-in `static` plugin for serving the built output in production
|
package/config.yaml
ADDED
package/dist/auth.d.ts
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { type Scope, type User } from 'harper';
|
|
2
|
+
import type { Middleware } from './http.ts';
|
|
3
|
+
/**
|
|
4
|
+
* Harper's canonical "is this a super_user" check. Matches how Harper itself gates privileged paths
|
|
5
|
+
* (e.g. impersonation, token minting): the resolved user carries `role.permission.super_user === true`.
|
|
6
|
+
*/
|
|
7
|
+
export declare function isSuperUser(user: User | undefined): boolean;
|
|
8
|
+
/**
|
|
9
|
+
* A Connect middleware that restricts the chain behind it (the Vite dev server) to Harper `super_user`s,
|
|
10
|
+
* authenticated via HTTP Basic auth. It runs the check itself against Harper's APIs:
|
|
11
|
+
*
|
|
12
|
+
* - A super_user Harper already resolved (`req.user`, bridged onto the node request by `registerHttp` —
|
|
13
|
+
* e.g. an authenticated admin) passes straight through.
|
|
14
|
+
* - A loopback peer under `authorizeLocal` (the default in `harper dev`) also passes, authorized here the
|
|
15
|
+
* same way the HMR WebSocket gate does it (`isUpgradeAuthorized`): Harper applies `authorizeLocal` to
|
|
16
|
+
* MQTT (and its Bun HTTP inject path) but NOT the Node HTTP middleware layer this runs in, so `req.user`
|
|
17
|
+
* is unset for a plain loopback `harper dev` request — we trust the real socket peer ourselves rather
|
|
18
|
+
* than relying on Harper to pre-set it. So local development passes straight through with no prompt.
|
|
19
|
+
* - Otherwise we validate an `Authorization: Basic` header directly with `server.authenticateUser` and, on
|
|
20
|
+
* failure, reply `401` with a `WWW-Authenticate: Basic` header so a browser prompts for credentials.
|
|
21
|
+
*
|
|
22
|
+
* NOTE: this guards the HTTP surface — the Vite dev server's asset, module-transform and `/@fs/`
|
|
23
|
+
* (arbitrary file read) endpoints, which is the dangerous part. Vite's HMR *WebSocket* runs on its own
|
|
24
|
+
* port and is not routed through here; keep it bound to localhost (or otherwise unexposed) in any
|
|
25
|
+
* non-local deployment.
|
|
26
|
+
*/
|
|
27
|
+
export declare function superUserAuth(scope: Scope, realm: string): Middleware;
|
|
28
|
+
/**
|
|
29
|
+
* Whether a WebSocket upgrade request may reach the Vite dev server — i.e. whether it represents a Harper
|
|
30
|
+
* `super_user`.
|
|
31
|
+
*
|
|
32
|
+
* The HMR WebSocket is served on Harper's own port (see `setupDevelopment`), so the same super_user gate as
|
|
33
|
+
* the HTTP surface should apply. But Harper runs its auth layer on the HTTP request chain, NOT the upgrade
|
|
34
|
+
* chain — `req.user` is unset here — so we authorize the raw upgrade ourselves, from the three signals a real
|
|
35
|
+
* client carries on a same-origin upgrade, cheapest first:
|
|
36
|
+
*
|
|
37
|
+
* 1. A loopback peer under `authorizeLocal` — mirrors how Harper trusts localhost for the HTTP surface, so
|
|
38
|
+
* plain `harper dev` works with no prompt (the upgrade carries neither a Basic header nor a cookie, and
|
|
39
|
+
* `authorizeLocal` sets no cookie). We trust the real socket peer, not a spoofable `X-Forwarded-For`.
|
|
40
|
+
* 2. An `Authorization: Basic` header, validated exactly like the HTTP gate. Browsers seldom attach this to
|
|
41
|
+
* a WebSocket handshake, but CLI clients (and some browsers) do.
|
|
42
|
+
* 3. The `hdb-session` cookie Harper sets after any successful login (sessions are on by default). Cookies
|
|
43
|
+
* ARE reliably sent on a same-origin upgrade, so this is the path that carries a logged-in admin's
|
|
44
|
+
* identity from the page to a remotely-exposed HMR socket.
|
|
45
|
+
*
|
|
46
|
+
* Best-effort and fails closed: a missing global, store, or lookup error yields `false` (the upgrade is then
|
|
47
|
+
* refused), never an unauthenticated pass.
|
|
48
|
+
*/
|
|
49
|
+
export declare function isUpgradeAuthorized(scope: Scope, req: any): Promise<boolean>;
|