bare-stow 0.0.0 → 0.1.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 +237 -0
- package/bin.js +109 -0
- package/index.d.ts +83 -0
- package/index.js +207 -0
- package/lib/fs.js +78 -0
- package/lib/host.d.ts +19 -0
- package/lib/host.js +62 -0
- package/lib/protocol.d.ts +16 -0
- package/lib/protocol.js +129 -0
- package/lib/rpc/bare-rpc.js +24 -0
- package/lib/rpc.js +23 -0
- package/lib/shim.js +86 -0
- package/lib/target/bare-sidecar.js +63 -0
- package/lib/target.js +27 -0
- package/package.json +91 -1
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,237 @@
|
|
|
1
|
+
# bare-stow
|
|
2
|
+
|
|
3
|
+
Module bundling and harness generation for Bare. Built on top of `bare-pack` (<https://github.com/holepunchto/bare-pack>), `bare-stow` produces a bundle for a target runtime together with a small harness that boots it from the host environment.
|
|
4
|
+
|
|
5
|
+
A [CLI](#cli) is also included and provides support for stowing a module graph from the command line.
|
|
6
|
+
|
|
7
|
+
```
|
|
8
|
+
npm i [-g] bare-stow
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
Given an entry module `core.js` that exports a `start` function receiving the IPC stream and an optional `ready` callback. The function may optionally return (or resolve to) a function that runs before the bundle exits:
|
|
14
|
+
|
|
15
|
+
```js
|
|
16
|
+
module.exports = async function start(ipc, ready) {
|
|
17
|
+
ipc.on('data', (data) => {
|
|
18
|
+
// Handle messages from the host.
|
|
19
|
+
})
|
|
20
|
+
|
|
21
|
+
// Optionally call `ready()` to signal readiness early. Otherwise it is
|
|
22
|
+
// signaled automatically when `start()` resolves.
|
|
23
|
+
ready()
|
|
24
|
+
|
|
25
|
+
return async function stop() {
|
|
26
|
+
// Clean up before the bundle exits.
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
The `ipc` argument is a duplex byte stream. Anything written to it is delivered to the host, and `'data'` events fire when the host writes back.
|
|
32
|
+
|
|
33
|
+
Stow it for the `bare-sidecar` target:
|
|
34
|
+
|
|
35
|
+
```js
|
|
36
|
+
const stow = require('bare-stow')
|
|
37
|
+
|
|
38
|
+
const entry = new URL('file:///app/core.js')
|
|
39
|
+
const out = new URL('file:///app/out/index.js')
|
|
40
|
+
|
|
41
|
+
for await (const artifact of stow(entry, 'bare-sidecar', out)) {
|
|
42
|
+
console.log(artifact.url.href)
|
|
43
|
+
}
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
Or equivalently from the command line:
|
|
47
|
+
|
|
48
|
+
```console
|
|
49
|
+
$ bare-stow --target bare-sidecar --out ./out/index.js ./core.js
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
This writes the harness to `out` and the bundle alongside it. The harness can then be required from the host and booted with `start()`. The harness awaits the worker's `ready` signal before resolving, so by the time `start()` returns the bundle is up:
|
|
53
|
+
|
|
54
|
+
```js
|
|
55
|
+
const harness = require('/app/out/index.js')
|
|
56
|
+
|
|
57
|
+
const { ipc } = await harness.start()
|
|
58
|
+
|
|
59
|
+
ipc.write(Buffer.from('hello from the host'))
|
|
60
|
+
|
|
61
|
+
ipc.on('exit', (code) => {
|
|
62
|
+
console.log('bundle exited with', code)
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
// Request a graceful shutdown.
|
|
66
|
+
ipc.destroy()
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
## Protocol
|
|
70
|
+
|
|
71
|
+
The harness multiplexes a control channel and a user data channel over the underlying binary duplex stream. The `ipc` returned from `start()` is itself a duplex stream carrying the user data; control frames ride alongside it on the same handle. RPC libraries (such as `bare-rpc`) bind to `ipc` just like a raw stream.
|
|
72
|
+
|
|
73
|
+
Control frames signal lifecycle events:
|
|
74
|
+
|
|
75
|
+
| Direction | Type | Payload |
|
|
76
|
+
| :------------- | :---------- | :------------------- |
|
|
77
|
+
| Worker -> Host | `ready` | - |
|
|
78
|
+
| Worker -> Host | `exit` | `{ code }` |
|
|
79
|
+
| Worker -> Host | `error` | `{ message, stack }` |
|
|
80
|
+
| Host -> Worker | `terminate` | - |
|
|
81
|
+
|
|
82
|
+
An incoming `error` frame destroys the local `ipc` with the carried error, surfacing it as a normal stream `error` event. Other control frames emit a regular event named after the type.
|
|
83
|
+
|
|
84
|
+
The `ipc` handle exposes the lifecycle as events and methods on the same duplex:
|
|
85
|
+
|
|
86
|
+
- `ipc.ready`: A promise that resolves once the worker has signaled ready. Rejects if the worker errors before ready.
|
|
87
|
+
- `ipc.on('exit', code)`: Emitted when the worker signals exit. `code` carries the exit code.
|
|
88
|
+
- `ipc.on('error', err)`: Emitted when the worker reports a fault, or when the underlying transport errors. Either way the `ipc` is destroyed.
|
|
89
|
+
- `ipc.on('close')`: Emitted when the underlying transport closes.
|
|
90
|
+
- `ipc.destroy()`: Standard stream destroy. Sends a `terminate` control frame to the worker and waits for the underlying transport to close.
|
|
91
|
+
- `ipc.terminate()`: Like `destroy()` but resolves with the worker's exit code.
|
|
92
|
+
|
|
93
|
+
The framing layer is also exported directly for embedders that bring their own transport:
|
|
94
|
+
|
|
95
|
+
```js
|
|
96
|
+
const protocol = require('bare-stow/protocol')
|
|
97
|
+
|
|
98
|
+
const ipc = protocol.attach(stream) // Any duplex byte stream
|
|
99
|
+
ipc.write(Buffer.from('payload')) // User data
|
|
100
|
+
ipc.send('ready') // Control frame
|
|
101
|
+
ipc.on('exit', () => {
|
|
102
|
+
/* ... */
|
|
103
|
+
})
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
The host helper layers the lifecycle promise and `terminate` method on top of the protocol:
|
|
107
|
+
|
|
108
|
+
```js
|
|
109
|
+
const { wrap } = require('bare-stow/host')
|
|
110
|
+
|
|
111
|
+
const ipc = wrap(stream) // Any duplex byte stream
|
|
112
|
+
await ipc.ready
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
## API
|
|
116
|
+
|
|
117
|
+
#### `const artifacts = stow(entry, target, out[, opts])`
|
|
118
|
+
|
|
119
|
+
Bundle the module graph rooted at `entry` for a `target` runtime and write the resulting artifacts to disk at and alongside `out`. Returns an async generator that yields `{ url }` objects as each artifact is written, allowing callers to observe progress.
|
|
120
|
+
|
|
121
|
+
`entry` is a `URL` (or `URL`-coercible string) pointing at the entry module. `target` selects the target runtime, given either as the built-in name `'bare-sidecar'` or as a [target provider](#targets-and-rpc-providers) object. Other targets ship as their own packages; require one and pass it as the provider. The target determines the harness format, bundle extension, host triples, and whether assets and addons are linked into the bundle or offloaded as sibling files. `out` is the output `URL` of the harness; the bundle is written next to it with the target's extension.
|
|
122
|
+
|
|
123
|
+
The harness artifacts are yielded first: The harness itself at `out`, followed by a TypeScript declaration (`.d.ts`) alongside it so hosts importing the harness are typed. The bundle is yielded next, then any offloaded assets and native addons when the target supports offloading.
|
|
124
|
+
|
|
125
|
+
Options include:
|
|
126
|
+
|
|
127
|
+
```js
|
|
128
|
+
opts = {
|
|
129
|
+
client,
|
|
130
|
+
server,
|
|
131
|
+
base,
|
|
132
|
+
hosts,
|
|
133
|
+
resolveTarget,
|
|
134
|
+
resolveRPC
|
|
135
|
+
}
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
- `client`: The RPC library to wire into the harness as a client, given either as a built-in name (`'bare-rpc'`) or as an [RPC provider](#targets-and-rpc-providers) object.
|
|
139
|
+
- `server`: The RPC library to wire into the bundle entry shim as a server, given either as a built-in name (`'bare-rpc'`) or as an [RPC provider](#targets-and-rpc-providers) object.
|
|
140
|
+
- `base`: The base `URL` of the module graph. Defaults to the directory containing `entry`.
|
|
141
|
+
- `hosts`: An array of host triples to build for. Must be a subset of the host triples supported by the target; passing a host the target does not support throws. Defaults to all host triples supported by the target.
|
|
142
|
+
- `resolveTarget`: A function mapping a target name to a [target provider](#targets-and-rpc-providers) object, called when `target` is a name not in the built-in registry (which only knows `'bare-sidecar'`).
|
|
143
|
+
- `resolveRPC`: A function mapping an RPC library name to an [RPC provider](#targets-and-rpc-providers) object, called when `client` or `server` is a name not in the built-in registry (which only knows `'bare-rpc'`).
|
|
144
|
+
|
|
145
|
+
Any additional options are forwarded to `bare-pack`. See <https://github.com/holepunchto/bare-pack> for the full set, including `builtins`, `imports`, `defer`, and `resolve`.
|
|
146
|
+
|
|
147
|
+
### Targets and RPC providers
|
|
148
|
+
|
|
149
|
+
The harness and RPC code generators are decoupled from the bundler: `target`, `client`, and `server` each accept a provider object directly, so generators can live outside `bare-stow`. Passing a name instead resolves it through `resolveTarget` / `resolveRPC`, which default to the built-in registries.
|
|
150
|
+
|
|
151
|
+
Both kinds of provider return an ordered array of artifacts. An artifact is `{ extension?, source }`:
|
|
152
|
+
|
|
153
|
+
```js
|
|
154
|
+
artifacts = [
|
|
155
|
+
{ source }, // The primary output.
|
|
156
|
+
{ extension, source } // A companion, named after the primary plus this extension.
|
|
157
|
+
]
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
A target provider describes the runtime profile it owns and generates the harness that boots the bundle from the host:
|
|
161
|
+
|
|
162
|
+
```js
|
|
163
|
+
target = {
|
|
164
|
+
name, // The target's name, used in diagnostics.
|
|
165
|
+
linked, // Whether assets and addons are linked into the bundle.
|
|
166
|
+
offload, // Whether (and which) assets and addons are offloaded as sibling files.
|
|
167
|
+
format, // The bundle encoding: 'bundle', 'bundle.cjs', 'bundle.mjs', or 'bundle.json'.
|
|
168
|
+
encoding, // The string encoding for encoded formats, or null for raw bytes.
|
|
169
|
+
extension, // The bundle file extension, e.g. '.bundle'.
|
|
170
|
+
module, // The harness module system: 'esm' or 'cjs'.
|
|
171
|
+
hosts, // The host triples the target supports.
|
|
172
|
+
generate({ bundleSpecifier, ipc, rpc, client }) {
|
|
173
|
+
// Return an array of harness artifacts.
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
For a target, each artifact is a file. The first has no `extension` and is written to `out`; each remaining artifact is written next to `out` with its own `extension` substituted for the harness extension (e.g. `{ extension: '.d.ts' }` becomes `index.d.ts`). The built-in targets emit two artifacts: The harness and its TypeScript declaration.
|
|
179
|
+
|
|
180
|
+
`generate()` receives `bundleSpecifier`, the relative specifier from the harness to the bundle artifact; `ipc`, the identifier the harness should bind the IPC stream to; `rpc`, the identifier the RPC client setup binds its instance to; and `client`, the resolved RPC client (or `null` when no client is requested). When present, `client` is `{ source, type }`: `source` is the runtime setup to splice into the harness (referencing `ipc` and binding `rpc`), and `type` is the TypeScript type of the bound instance to splice into the declaration. The target owns `linked`, `offload`, `format`, `encoding`, and `extension`; they are not overridable through `opts`.
|
|
181
|
+
|
|
182
|
+
An RPC provider generates the wiring that binds an RPC instance to the IPC stream, used on both the client (harness) and server (shim) side:
|
|
183
|
+
|
|
184
|
+
```js
|
|
185
|
+
rpc = {
|
|
186
|
+
name, // The library's name, used in diagnostics.
|
|
187
|
+
generate({ ipc, rpc, module, role }) {
|
|
188
|
+
// Return an array of RPC artifacts.
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
For an RPC provider, each artifact is a fragment contributed to the harness file with the matching extension. The artifact with no `extension` is the runtime setup (reading `ipc`, binding `rpc`); the `{ extension: '.d.ts' }` artifact is the TypeScript type of the bound instance. `generate()` receives `ipc`, the identifier of the IPC stream in scope; `rpc`, the identifier to bind the RPC instance to; `module`, the surrounding module system (`'esm'` or `'cjs'`) so the runtime fragment can emit the matching import form; and `role`, either `'client'` or `'server'`. The bundler owns the `ipc` and `rpc` binding names and threads them to both the target and the RPC provider so neither has to assume them. It renders the client in the target's `module` and the server as `'esm'` (the shim is always an ES module), splicing the runtime fragment and, for the client, the type fragment into the target's artifacts. The shim is not a typed, host-facing module, so the server's type fragment is unused.
|
|
194
|
+
|
|
195
|
+
## CLI
|
|
196
|
+
|
|
197
|
+
#### `bare-stow [flags] <entry>`
|
|
198
|
+
|
|
199
|
+
Stow the module graph rooted at `<entry>`, writing the harness and bundle to the path given by `--out`.
|
|
200
|
+
|
|
201
|
+
```console
|
|
202
|
+
--version|-v
|
|
203
|
+
--target <name>
|
|
204
|
+
--client <name>
|
|
205
|
+
--server <name>
|
|
206
|
+
--base <path>
|
|
207
|
+
--out|-o <path>
|
|
208
|
+
--builtins <path>
|
|
209
|
+
--imports <path>
|
|
210
|
+
--defer <specifier>
|
|
211
|
+
--host <name>
|
|
212
|
+
--help|-h
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
##### `--target <name>`
|
|
216
|
+
|
|
217
|
+
The target runtime. Required. The built-in `bare-sidecar` is resolved directly; any other `<name>` is loaded from its `bare-stow-target-<name>` package, or from `<name>` itself as a full module specifier, resolved from the working directory.
|
|
218
|
+
|
|
219
|
+
##### `--client <name>` and `--server <name>`
|
|
220
|
+
|
|
221
|
+
The RPC library to wire into the harness (`--client`) or the bundle entry shim (`--server`). The built-in `bare-rpc` is resolved directly; any other `<name>` is loaded from its `bare-stow-rpc-<name>` package, or from `<name>` itself as a full module specifier, resolved from the working directory.
|
|
222
|
+
|
|
223
|
+
##### `--builtins <path>` and `--imports <path>`
|
|
224
|
+
|
|
225
|
+
Paths to JavaScript or JSON files exporting a list of builtin module names and a map of global import overrides respectively. Forwarded to `bare-pack`.
|
|
226
|
+
|
|
227
|
+
##### `--defer <specifier>`
|
|
228
|
+
|
|
229
|
+
A module specifier whose resolution should be deferred. May be passed multiple times.
|
|
230
|
+
|
|
231
|
+
##### `--host <name>`
|
|
232
|
+
|
|
233
|
+
A host triple to build for. Must be a subset of the host triples supported by the target. May be passed multiple times. Defaults to all host triples supported by the target.
|
|
234
|
+
|
|
235
|
+
## License
|
|
236
|
+
|
|
237
|
+
Apache-2.0
|
package/bin.js
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
const path = require('path')
|
|
3
|
+
const { pathToFileURL } = require('url')
|
|
4
|
+
const { command, flag, arg, summary } = require('paparam')
|
|
5
|
+
const { resolve } = require('bare-module-traverse')
|
|
6
|
+
const pkg = require('./package')
|
|
7
|
+
const stow = require('.')
|
|
8
|
+
|
|
9
|
+
const cmd = command(
|
|
10
|
+
pkg.name,
|
|
11
|
+
summary(pkg.description),
|
|
12
|
+
arg('<entry>', 'The entry point of the module graph'),
|
|
13
|
+
flag('--version|-v', 'Print the current version'),
|
|
14
|
+
flag(
|
|
15
|
+
'--target <name>',
|
|
16
|
+
'The target runtime (bare-sidecar, or a bare-stow-target-<name> package)'
|
|
17
|
+
),
|
|
18
|
+
flag(
|
|
19
|
+
'--client <name>',
|
|
20
|
+
'The RPC client to include (bare-rpc, or a bare-stow-rpc-<name> package)'
|
|
21
|
+
),
|
|
22
|
+
flag(
|
|
23
|
+
'--server <name>',
|
|
24
|
+
'The RPC server to include (bare-rpc, or a bare-stow-rpc-<name> package)'
|
|
25
|
+
),
|
|
26
|
+
flag('--base <path>', 'The base path of the bundle'),
|
|
27
|
+
flag('--out|-o <path>', 'The output path of the harness'),
|
|
28
|
+
flag('--builtins <path>', 'A list of builtin modules'),
|
|
29
|
+
flag('--imports <path>', 'A map of global import overrides'),
|
|
30
|
+
flag('--defer <specifier>', 'A module specifier to defer resolution of').multiple(),
|
|
31
|
+
flag('--host <name>', 'A host triple to include (must be a subset of the target)').multiple(),
|
|
32
|
+
async (cmd) => {
|
|
33
|
+
const { entry } = cmd.args
|
|
34
|
+
let {
|
|
35
|
+
version,
|
|
36
|
+
target,
|
|
37
|
+
client,
|
|
38
|
+
server,
|
|
39
|
+
base = '.',
|
|
40
|
+
out,
|
|
41
|
+
builtins,
|
|
42
|
+
imports,
|
|
43
|
+
defer,
|
|
44
|
+
host: hosts
|
|
45
|
+
} = cmd.flags
|
|
46
|
+
|
|
47
|
+
if (version) return console.log(`v${pkg.version}`)
|
|
48
|
+
|
|
49
|
+
if (!target) throw new Error('--target is required')
|
|
50
|
+
if (!out) throw new Error('--out is required')
|
|
51
|
+
|
|
52
|
+
if (builtins) {
|
|
53
|
+
builtins = require(path.resolve(builtins))
|
|
54
|
+
|
|
55
|
+
if ('default' in builtins) builtins = builtins.default
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
if (imports) {
|
|
59
|
+
imports = require(path.resolve(imports))
|
|
60
|
+
|
|
61
|
+
if ('default' in imports) imports = imports.default
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
base = pathToFileURL(base)
|
|
65
|
+
|
|
66
|
+
if (!base.pathname.endsWith('/')) base.pathname += '/'
|
|
67
|
+
|
|
68
|
+
for await (const _ of stow(pathToFileURL(entry), target, pathToFileURL(out), {
|
|
69
|
+
client,
|
|
70
|
+
server,
|
|
71
|
+
base,
|
|
72
|
+
hosts,
|
|
73
|
+
resolveTarget,
|
|
74
|
+
resolveRPC,
|
|
75
|
+
resolve: resolve.bare,
|
|
76
|
+
builtins,
|
|
77
|
+
imports,
|
|
78
|
+
defer
|
|
79
|
+
})) {
|
|
80
|
+
//
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
)
|
|
84
|
+
|
|
85
|
+
// Built-in names are resolved by bare-stow itself; any other name is loaded
|
|
86
|
+
// from its `bare-stow-target-<name>` / `bare-stow-rpc-<name>` package, or as a
|
|
87
|
+
// full module specifier, resolved from the working directory.
|
|
88
|
+
|
|
89
|
+
function resolveTarget(name) {
|
|
90
|
+
return load('bare-stow-target', name)
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function resolveRPC(name) {
|
|
94
|
+
return load('bare-stow-rpc', name)
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function load(prefix, name) {
|
|
98
|
+
for (const specifier of [`${prefix}-${name}`, name]) {
|
|
99
|
+
try {
|
|
100
|
+
return require(require.resolve(specifier, { paths: [process.cwd()] }))
|
|
101
|
+
} catch {
|
|
102
|
+
continue
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
throw new Error(`Cannot resolve '${name}'`)
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
cmd.parse()
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import URL from 'bare-url'
|
|
2
|
+
|
|
3
|
+
interface Artifact {
|
|
4
|
+
extension?: string
|
|
5
|
+
source: string
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
type TargetName = 'bare-sidecar'
|
|
9
|
+
|
|
10
|
+
interface TargetContext {
|
|
11
|
+
bundleSpecifier: string
|
|
12
|
+
ipc: string
|
|
13
|
+
rpc: string
|
|
14
|
+
client: RPCClient | null
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
interface Target {
|
|
18
|
+
name: string
|
|
19
|
+
linked: boolean
|
|
20
|
+
offload: boolean | { addons?: boolean; assets?: boolean }
|
|
21
|
+
format: 'bundle' | 'bundle.cjs' | 'bundle.mjs' | 'bundle.json'
|
|
22
|
+
encoding: string | null
|
|
23
|
+
extension: string
|
|
24
|
+
module: 'esm' | 'cjs'
|
|
25
|
+
hosts: string[]
|
|
26
|
+
generate(context: TargetContext): Artifact[]
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
type RPCName = 'bare-rpc'
|
|
30
|
+
|
|
31
|
+
interface RPCContext {
|
|
32
|
+
ipc: string
|
|
33
|
+
rpc: string
|
|
34
|
+
module: 'esm' | 'cjs'
|
|
35
|
+
role: 'client' | 'server'
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
interface RPC {
|
|
39
|
+
name: string
|
|
40
|
+
generate(context: RPCContext): Artifact[]
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
interface RPCClient {
|
|
44
|
+
source: string
|
|
45
|
+
type: string
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
interface StowOptions {
|
|
49
|
+
client?: RPC | RPCName
|
|
50
|
+
server?: RPC | RPCName
|
|
51
|
+
base?: URL | string
|
|
52
|
+
hosts?: string[]
|
|
53
|
+
resolveTarget?(name: string): Target
|
|
54
|
+
resolveRPC?(name: string): RPC
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
interface StowArtifact {
|
|
58
|
+
url: URL
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
declare function stow(
|
|
62
|
+
entry: URL | string,
|
|
63
|
+
target: Target | TargetName,
|
|
64
|
+
out: URL | string,
|
|
65
|
+
opts?: StowOptions
|
|
66
|
+
): AsyncGenerator<StowArtifact>
|
|
67
|
+
|
|
68
|
+
declare namespace stow {
|
|
69
|
+
export {
|
|
70
|
+
type Target,
|
|
71
|
+
type TargetName,
|
|
72
|
+
type TargetContext,
|
|
73
|
+
type RPC,
|
|
74
|
+
type RPCName,
|
|
75
|
+
type RPCContext,
|
|
76
|
+
type RPCClient,
|
|
77
|
+
type Artifact,
|
|
78
|
+
type StowOptions,
|
|
79
|
+
type StowArtifact
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export = stow
|