@zerosumstudio/spool 0.0.0-stage → 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 +21 -0
- package/README.md +600 -2
- package/dist/federation/Remote.svelte +106 -0
- package/dist/federation/Remote.vue +118 -0
- package/dist/federation/core.ts +421 -0
- package/dist/federation/overrides.ts +289 -0
- package/dist/federation/remote.tsx +105 -0
- package/dist/index.js +7798 -0
- package/dist/navigation/binding.react.ts +6 -0
- package/dist/navigation/binding.svelte.ts +13 -0
- package/dist/navigation/binding.vue.ts +12 -0
- package/dist/navigation/history.ts +98 -0
- package/dist/sentry/scrub.ts +157 -0
- package/dist/vite.d.ts +110 -0
- package/dist/vite.js +1068 -0
- package/package.json +87 -3
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { createSubscriber } from 'svelte/reactivity'
|
|
2
|
+
import { getLocation, subscribe, type SpoolLocation } from './history'
|
|
3
|
+
|
|
4
|
+
const track = createSubscriber(update => subscribe(update))
|
|
5
|
+
|
|
6
|
+
/** The current url. Reading `location.current` in a component or an effect re-runs it on navigation. */
|
|
7
|
+
export const location = {
|
|
8
|
+
get current(): SpoolLocation {
|
|
9
|
+
track()
|
|
10
|
+
|
|
11
|
+
return getLocation()
|
|
12
|
+
},
|
|
13
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { onScopeDispose, shallowRef, type ShallowRef } from 'vue'
|
|
2
|
+
import { getLocation, subscribe, type SpoolLocation } from './history'
|
|
3
|
+
|
|
4
|
+
export function useLocation(): ShallowRef<SpoolLocation> {
|
|
5
|
+
const location = shallowRef<SpoolLocation>(getLocation())
|
|
6
|
+
const stop = subscribe(() => {
|
|
7
|
+
location.value = getLocation()
|
|
8
|
+
})
|
|
9
|
+
onScopeDispose(stop)
|
|
10
|
+
|
|
11
|
+
return location
|
|
12
|
+
}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
export interface SpoolLocation {
|
|
2
|
+
pathname: string
|
|
3
|
+
search: string
|
|
4
|
+
hash: string
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
const EVENT = 'spool:navigation'
|
|
8
|
+
|
|
9
|
+
let cached: SpoolLocation = readLocation()
|
|
10
|
+
|
|
11
|
+
function readLocation(): SpoolLocation {
|
|
12
|
+
const { pathname, search, hash } = window.location
|
|
13
|
+
|
|
14
|
+
return { pathname, search, hash }
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
// The flag name is shared with 4.x hosts that may sit on the same page, so it keeps its old name.
|
|
18
|
+
function patchHistory(): void {
|
|
19
|
+
const flagged = window as typeof window & { __spoolShell?: boolean }
|
|
20
|
+
|
|
21
|
+
if (flagged.__spoolShell) return
|
|
22
|
+
|
|
23
|
+
flagged.__spoolShell = true
|
|
24
|
+
|
|
25
|
+
for (const method of ['pushState', 'replaceState'] as const) {
|
|
26
|
+
const original = history[method].bind(history)
|
|
27
|
+
history[method] = function (this: History, ...args: Parameters<History['pushState']>) {
|
|
28
|
+
original(...args)
|
|
29
|
+
window.dispatchEvent(new Event(EVENT))
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
window.addEventListener('popstate', () => window.dispatchEvent(new Event(EVENT)))
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
patchHistory()
|
|
37
|
+
|
|
38
|
+
export function getLocation(): SpoolLocation {
|
|
39
|
+
const next = readLocation()
|
|
40
|
+
const changed =
|
|
41
|
+
next.pathname !== cached.pathname ||
|
|
42
|
+
next.search !== cached.search ||
|
|
43
|
+
next.hash !== cached.hash
|
|
44
|
+
|
|
45
|
+
if (changed) cached = next
|
|
46
|
+
|
|
47
|
+
return cached
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function navigate(to: string, options: { replace?: boolean } = {}): void {
|
|
51
|
+
history[options.replace ? 'replaceState' : 'pushState'](null, '', to)
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
interface LinkClick {
|
|
55
|
+
button: number
|
|
56
|
+
metaKey: boolean
|
|
57
|
+
ctrlKey: boolean
|
|
58
|
+
shiftKey: boolean
|
|
59
|
+
altKey: boolean
|
|
60
|
+
currentTarget: EventTarget | null
|
|
61
|
+
preventDefault(): void
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** The click handler for an `<a href>` that changes the url without a page load. A click that
|
|
65
|
+
* asks the browser for a new tab or window is left to it. Vue types the event as a plain
|
|
66
|
+
* `MouseEvent`, so the link is checked here rather than in the type. */
|
|
67
|
+
export function navigateOnClick(event: LinkClick): void {
|
|
68
|
+
const link = event.currentTarget
|
|
69
|
+
const isPlainClick =
|
|
70
|
+
event.button === 0 && !event.metaKey && !event.ctrlKey && !event.shiftKey && !event.altKey
|
|
71
|
+
|
|
72
|
+
if (!isPlainClick || !(link instanceof HTMLAnchorElement)) return
|
|
73
|
+
|
|
74
|
+
event.preventDefault()
|
|
75
|
+
navigate(`${link.pathname}${link.search}${link.hash}`)
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export function subscribe(listener: () => void): () => void {
|
|
79
|
+
window.addEventListener(EVENT, listener)
|
|
80
|
+
|
|
81
|
+
return () => {
|
|
82
|
+
window.removeEventListener(EVENT, listener)
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function matchesPrefix(pathname: string, prefix: string): boolean {
|
|
87
|
+
return pathname === prefix || pathname.startsWith(`${prefix.replace(/\/$/, '')}/`)
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** The value at the longest url prefix in `routes` that matches `pathname`. */
|
|
91
|
+
export function matchRoute<Name extends string>(
|
|
92
|
+
pathname: string,
|
|
93
|
+
routes: Record<string, Name>,
|
|
94
|
+
): Name | undefined {
|
|
95
|
+
return Object.entries(routes)
|
|
96
|
+
.filter(([prefix]) => matchesPrefix(pathname, prefix))
|
|
97
|
+
.sort((a, b) => b[0].length - a[0].length)[0]?.[1]
|
|
98
|
+
}
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* What leaves the browser in a Sentry event. A url can carry a token, an email or an id in its
|
|
3
|
+
* query string or fragment, and the Referer header and navigation breadcrumbs repeat it, so
|
|
4
|
+
* every url is cut to its origin and path before it is sent. Pure on purpose: nothing here reads
|
|
5
|
+
* the page or the Sentry client.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/*
|
|
9
|
+
* TYPES
|
|
10
|
+
***************************************************************************************************/
|
|
11
|
+
interface ScrubbableRequest {
|
|
12
|
+
url?: string
|
|
13
|
+
query_string?: unknown
|
|
14
|
+
headers?: Record<string, string>
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
interface ScrubbableException {
|
|
18
|
+
value?: string
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface ScrubbableEvent {
|
|
22
|
+
request?: ScrubbableRequest
|
|
23
|
+
message?: string
|
|
24
|
+
exception?: { values?: ScrubbableException[] }
|
|
25
|
+
contexts?: Record<string, Record<string, unknown> | undefined>
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export interface ScrubbableBreadcrumb {
|
|
29
|
+
message?: string
|
|
30
|
+
data?: Record<string, unknown>
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/*
|
|
34
|
+
* URLS
|
|
35
|
+
***************************************************************************************************/
|
|
36
|
+
const BREADCRUMB_URL_KEYS = ['url', 'from', 'to']
|
|
37
|
+
const ABSOLUTE_URL = /^[a-z][a-z\d+.-]*:/i
|
|
38
|
+
// An http(s) url inside prose, up to the first space, quote or bracket.
|
|
39
|
+
const URL_IN_TEXT = /https?:\/\/[^\s"'<>()[\]{}]+/gi
|
|
40
|
+
const TRAILING_PUNCTUATION = /[.,;:!?]+$/
|
|
41
|
+
// Only used to read a relative url; none of it ever reaches the result.
|
|
42
|
+
const PLACEHOLDER_BASE = 'http://spool.invalid'
|
|
43
|
+
|
|
44
|
+
/** `value` as origin plus path: no query string, no fragment, no credentials. A relative url
|
|
45
|
+
* keeps just its path, and a scheme that has no origin (data:, blob:) keeps nothing but itself. */
|
|
46
|
+
export function trimUrl(value: string): string {
|
|
47
|
+
let url: URL
|
|
48
|
+
try {
|
|
49
|
+
url = new URL(value, PLACEHOLDER_BASE)
|
|
50
|
+
} catch {
|
|
51
|
+
return ''
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
if (!ABSOLUTE_URL.test(value)) return url.pathname
|
|
55
|
+
|
|
56
|
+
if (url.protocol !== 'http:' && url.protocol !== 'https:') return url.protocol
|
|
57
|
+
|
|
58
|
+
return `${url.origin}${url.pathname}`
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** `text` with every http(s) url in it cut to origin and path, as in an error message like
|
|
62
|
+
* "Failed to fetch https://api.example.com/users?token=abc". */
|
|
63
|
+
export function trimUrlsIn(text: string): string {
|
|
64
|
+
return text.replace(URL_IN_TEXT, found => {
|
|
65
|
+
const punctuation = TRAILING_PUNCTUATION.exec(found)?.[0] ?? ''
|
|
66
|
+
const url = punctuation ? found.slice(0, -punctuation.length) : found
|
|
67
|
+
|
|
68
|
+
return `${trimUrl(url) || url}${punctuation}`
|
|
69
|
+
})
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/*
|
|
73
|
+
* BREADCRUMBS
|
|
74
|
+
***************************************************************************************************/
|
|
75
|
+
export function scrubBreadcrumb<Breadcrumb extends ScrubbableBreadcrumb>(
|
|
76
|
+
breadcrumb: Breadcrumb,
|
|
77
|
+
): Breadcrumb {
|
|
78
|
+
const changes: ScrubbableBreadcrumb = {}
|
|
79
|
+
|
|
80
|
+
if (breadcrumb.message !== undefined) changes.message = trimUrlsIn(breadcrumb.message)
|
|
81
|
+
|
|
82
|
+
const { data } = breadcrumb
|
|
83
|
+
|
|
84
|
+
if (data) {
|
|
85
|
+
const scrubbed: Record<string, unknown> = { ...data }
|
|
86
|
+
for (const key of BREADCRUMB_URL_KEYS) {
|
|
87
|
+
const value = scrubbed[key]
|
|
88
|
+
|
|
89
|
+
if (typeof value === 'string') scrubbed[key] = trimUrl(value)
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
changes.data = scrubbed
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
return { ...breadcrumb, ...changes }
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/*
|
|
99
|
+
* EVENTS
|
|
100
|
+
***************************************************************************************************/
|
|
101
|
+
function removeReferer(headers: Record<string, string>): Record<string, string> {
|
|
102
|
+
return Object.fromEntries(
|
|
103
|
+
Object.entries(headers).filter(([name]) => name.toLowerCase() !== 'referer'),
|
|
104
|
+
)
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function scrubRequest(request: ScrubbableRequest): ScrubbableRequest {
|
|
108
|
+
const scrubbed: ScrubbableRequest = { ...request }
|
|
109
|
+
delete scrubbed.query_string
|
|
110
|
+
|
|
111
|
+
if (scrubbed.url !== undefined) scrubbed.url = trimUrl(scrubbed.url)
|
|
112
|
+
|
|
113
|
+
if (scrubbed.headers) scrubbed.headers = removeReferer(scrubbed.headers)
|
|
114
|
+
|
|
115
|
+
return scrubbed
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function scrubExceptions(values: ScrubbableException[]): ScrubbableException[] {
|
|
119
|
+
return values.map(exception =>
|
|
120
|
+
exception.value === undefined
|
|
121
|
+
? exception
|
|
122
|
+
: { ...exception, value: trimUrlsIn(exception.value) },
|
|
123
|
+
)
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/** The Vue SDK attaches a component's props to the event. They are the user's data, whatever
|
|
127
|
+
* `attachProps` says, so they are removed here too. */
|
|
128
|
+
function removeVueProps(
|
|
129
|
+
contexts: Record<string, Record<string, unknown> | undefined>,
|
|
130
|
+
): Record<string, Record<string, unknown> | undefined> {
|
|
131
|
+
const { vue } = contexts
|
|
132
|
+
|
|
133
|
+
if (!vue || !('propsData' in vue)) return contexts
|
|
134
|
+
|
|
135
|
+
const rest = { ...vue }
|
|
136
|
+
delete rest.propsData
|
|
137
|
+
|
|
138
|
+
return { ...contexts, vue: rest }
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/** An error event with its urls cut to origin and path (in the request, in exception and event
|
|
142
|
+
* messages), its query string and Referer header removed, and no Vue props. */
|
|
143
|
+
export function scrubEvent<Payload extends ScrubbableEvent>(event: Payload): Payload {
|
|
144
|
+
const changes: ScrubbableEvent = {}
|
|
145
|
+
|
|
146
|
+
if (event.request) changes.request = scrubRequest(event.request)
|
|
147
|
+
|
|
148
|
+
if (event.message !== undefined) changes.message = trimUrlsIn(event.message)
|
|
149
|
+
|
|
150
|
+
if (event.exception?.values) {
|
|
151
|
+
changes.exception = { ...event.exception, values: scrubExceptions(event.exception.values) }
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
if (event.contexts) changes.contexts = removeVueProps(event.contexts)
|
|
155
|
+
|
|
156
|
+
return { ...event, ...changes }
|
|
157
|
+
}
|
package/dist/vite.d.ts
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
declare const SHARE_STRATEGIES: readonly ["version-first", "loaded-first"];
|
|
2
|
+
type ShareStrategy = (typeof SHARE_STRATEGIES)[number];
|
|
3
|
+
interface ProxyEntry {
|
|
4
|
+
target: string;
|
|
5
|
+
changeOrigin?: boolean;
|
|
6
|
+
secure?: boolean;
|
|
7
|
+
ws?: boolean;
|
|
8
|
+
}
|
|
9
|
+
interface ServerConfig {
|
|
10
|
+
proxy?: Record<string, string | ProxyEntry>;
|
|
11
|
+
headers?: Record<string, string>;
|
|
12
|
+
host?: boolean | string;
|
|
13
|
+
/** Origins the dev server allows. Omitted means every app's own localhost origin. */
|
|
14
|
+
cors?: string[];
|
|
15
|
+
fs?: {
|
|
16
|
+
/** Extra directories the dev server may serve, relative to the workspace root. */
|
|
17
|
+
allow: string[];
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Where headers are served:
|
|
22
|
+
* - `build`: the `_headers` file a static host reads. CORS and caching for remotes, and the policy.
|
|
23
|
+
* - `preview`: the built app on a local server. The same policy, strict sources included, and
|
|
24
|
+
* the same caching. CORS stays with the server's own `cors` allowlist, not `*`.
|
|
25
|
+
* - `dev`: everything but what breaks hot reload, which is the strict `script-src` and
|
|
26
|
+
* `connect-src` (HMR needs inline scripts and a websocket) and the immutable `/assets/*` cache.
|
|
27
|
+
*/
|
|
28
|
+
interface HeaderRule {
|
|
29
|
+
/** `/*` for every path, `/prefix/*` for a folder, otherwise an exact path. */
|
|
30
|
+
path: string;
|
|
31
|
+
headers: Record<string, string>;
|
|
32
|
+
}
|
|
33
|
+
type Middleware = (request: {
|
|
34
|
+
url?: string;
|
|
35
|
+
}, response: {
|
|
36
|
+
setHeader: (name: string, value: string) => unknown;
|
|
37
|
+
}, next: () => void) => void;
|
|
38
|
+
/** Sets every matching rule's headers on a response, so dev and preview answer like a static host. */
|
|
39
|
+
declare function rulesMiddleware(rules: HeaderRule[]): Middleware;
|
|
40
|
+
interface DevServer {
|
|
41
|
+
middlewares: {
|
|
42
|
+
use: (handler: ReturnType<typeof rulesMiddleware>) => unknown;
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
/** The slice of vite's Plugin spool uses, so this package needs no vite types. */
|
|
46
|
+
interface SpoolPlugin {
|
|
47
|
+
name: 'spool';
|
|
48
|
+
/** Runs first, so its middleware answers before the federation plugin ends a manifest response. */
|
|
49
|
+
enforce: 'pre';
|
|
50
|
+
config: (userConfig?: unknown, env?: {
|
|
51
|
+
mode?: string;
|
|
52
|
+
isPreview?: boolean;
|
|
53
|
+
}) => {
|
|
54
|
+
define: Record<string, string>;
|
|
55
|
+
preview: {
|
|
56
|
+
headers: Record<string, string>;
|
|
57
|
+
};
|
|
58
|
+
};
|
|
59
|
+
configureServer: (server: DevServer) => void;
|
|
60
|
+
configurePreviewServer: (server: DevServer) => void;
|
|
61
|
+
generateBundle: (this: {
|
|
62
|
+
emitFile: (file: {
|
|
63
|
+
type: 'asset';
|
|
64
|
+
fileName: string;
|
|
65
|
+
source: string;
|
|
66
|
+
}) => string;
|
|
67
|
+
}) => void;
|
|
68
|
+
}
|
|
69
|
+
/** Vite's `command`. Only builds read deployed remote urls and lock the override origins. */
|
|
70
|
+
type SpoolCommand = 'build' | 'serve';
|
|
71
|
+
interface SpoolAppOptions {
|
|
72
|
+
/** The app's folder: `import.meta.dirname` in its vite config. */
|
|
73
|
+
root: string;
|
|
74
|
+
/** Vite's `command`. Only builds read deployed remote urls. */
|
|
75
|
+
command?: SpoolCommand;
|
|
76
|
+
}
|
|
77
|
+
type ViteServer = Omit<ServerConfig, 'cors'> & {
|
|
78
|
+
port: number;
|
|
79
|
+
strictPort: boolean;
|
|
80
|
+
cors: {
|
|
81
|
+
origin: string[];
|
|
82
|
+
};
|
|
83
|
+
headers: Record<string, string>;
|
|
84
|
+
};
|
|
85
|
+
interface SpoolApp {
|
|
86
|
+
/** Use for both `server` and `preview`. */
|
|
87
|
+
server: ViteServer;
|
|
88
|
+
/** Options for `federation()` from @module-federation/vite. */
|
|
89
|
+
federation: {
|
|
90
|
+
name: string;
|
|
91
|
+
filename?: string;
|
|
92
|
+
manifest: true;
|
|
93
|
+
remotes?: Record<string, string>;
|
|
94
|
+
exposes?: Record<string, string>;
|
|
95
|
+
shared: Record<string, {
|
|
96
|
+
singleton: true;
|
|
97
|
+
}>;
|
|
98
|
+
shareStrategy: ShareStrategy;
|
|
99
|
+
dts: false;
|
|
100
|
+
};
|
|
101
|
+
/** Writes `_headers` into the build and defines the override origins. */
|
|
102
|
+
plugin: SpoolPlugin;
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Everything an app's vite config needs from spool.json: the dev and preview server, the
|
|
106
|
+
* federation options, and the plugin that writes `_headers`. spool.json is validated with the
|
|
107
|
+
* CLI's own parser, so a typo fails here with the same message `spool doctor` gives.
|
|
108
|
+
*/
|
|
109
|
+
export declare function spoolApp(name: string, options: SpoolAppOptions): SpoolApp;
|
|
110
|
+
export type { SpoolApp, SpoolAppOptions, SpoolCommand, SpoolPlugin };
|