@fabrika/check 0.4.1 → 0.4.3
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/package.json +13 -6
- package/src/source-maps-cli.ts +46 -0
- package/src/source-maps.ts +198 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fabrika/check",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.3",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -11,7 +11,8 @@
|
|
|
11
11
|
"access": "public"
|
|
12
12
|
},
|
|
13
13
|
"bin": {
|
|
14
|
-
"fabrika-check": "./src/cli.ts"
|
|
14
|
+
"fabrika-check": "./src/cli.ts",
|
|
15
|
+
"fabrika-source-maps": "./src/source-maps-cli.ts"
|
|
15
16
|
},
|
|
16
17
|
"files": [
|
|
17
18
|
"src",
|
|
@@ -32,6 +33,11 @@
|
|
|
32
33
|
"bun": "./src/bun-plugin.ts",
|
|
33
34
|
"types": "./src/bun-plugin.ts",
|
|
34
35
|
"default": "./src/bun-plugin.ts"
|
|
36
|
+
},
|
|
37
|
+
"./source-maps": {
|
|
38
|
+
"bun": "./src/source-maps.ts",
|
|
39
|
+
"types": "./src/source-maps.ts",
|
|
40
|
+
"default": "./src/source-maps.ts"
|
|
35
41
|
}
|
|
36
42
|
},
|
|
37
43
|
"scripts": {
|
|
@@ -40,10 +46,11 @@
|
|
|
40
46
|
},
|
|
41
47
|
"dependencies": {
|
|
42
48
|
"@babel/parser": "8.0.7",
|
|
43
|
-
"@fabrika/app": "^0.4.
|
|
44
|
-
"@fabrika/auth-core": "^0.4.
|
|
45
|
-
"@fabrika/db": "^0.4.
|
|
46
|
-
"@fabrika/
|
|
49
|
+
"@fabrika/app": "^0.4.3",
|
|
50
|
+
"@fabrika/auth-core": "^0.4.3",
|
|
51
|
+
"@fabrika/db": "^0.4.3",
|
|
52
|
+
"@fabrika/operations-contract": "^0.4.3",
|
|
53
|
+
"@fabrika/platform-node": "^0.4.3",
|
|
47
54
|
"zod": "^3.25.76"
|
|
48
55
|
}
|
|
49
56
|
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
#!/usr/bin/env bun
|
|
2
|
+
|
|
3
|
+
// `fabrika-source-maps <root>`: the last step of a Zerops platform build. It uploads the source maps under the asset
|
|
4
|
+
// roots that `FABRIKA_SOURCE_MAP_UPLOAD` names, with the bearer in `FABRIKA_SOURCE_MAP_UPLOAD_KEY`, and prints one
|
|
5
|
+
// report line the provider reads back from the build log. It always exits 0: a deploy never fails on its maps.
|
|
6
|
+
|
|
7
|
+
import { FABRIKA_SOURCE_MAP_UPLOAD, FABRIKA_SOURCE_MAP_UPLOAD_KEY } from '@fabrika/auth-core'
|
|
8
|
+
import {
|
|
9
|
+
type OperationsSourceMapBuildUpload,
|
|
10
|
+
parseOperationsSourceMapBuildUpload,
|
|
11
|
+
sourceMapUploadReport,
|
|
12
|
+
} from '@fabrika/operations-contract/releases'
|
|
13
|
+
import { resolve } from 'node:path'
|
|
14
|
+
import { type ArtifactFetch, collectAssetSourceMaps, type SourceMapUploadResult, uploadSourceMaps } from './source-maps'
|
|
15
|
+
|
|
16
|
+
const readUpload = (value: string | undefined): OperationsSourceMapBuildUpload | null => {
|
|
17
|
+
if (value === undefined || value === '') return null
|
|
18
|
+
try {
|
|
19
|
+
return parseOperationsSourceMapBuildUpload(JSON.parse(value))
|
|
20
|
+
} catch {
|
|
21
|
+
return null
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** Collect and upload; every failure is an incomplete result, and nothing it prints carries the bearer. */
|
|
26
|
+
export async function runSourceMapUpload(
|
|
27
|
+
root: string,
|
|
28
|
+
environment: Readonly<Record<string, string | undefined>>,
|
|
29
|
+
fetch: ArtifactFetch,
|
|
30
|
+
): Promise<SourceMapUploadResult> {
|
|
31
|
+
const upload = readUpload(environment[FABRIKA_SOURCE_MAP_UPLOAD])
|
|
32
|
+
const bearer = environment[FABRIKA_SOURCE_MAP_UPLOAD_KEY] ?? ''
|
|
33
|
+
if (upload === null || bearer === '') return { uploaded: 0, state: 'incomplete' }
|
|
34
|
+
try {
|
|
35
|
+
const collection = await collectAssetSourceMaps(root, upload.assetRoots)
|
|
36
|
+
return await uploadSourceMaps({ ...upload.destination, bearer }, collection, fetch)
|
|
37
|
+
} catch {
|
|
38
|
+
return { uploaded: 0, state: 'incomplete' }
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
if (import.meta.main) {
|
|
43
|
+
const root = resolve(process.argv[2] ?? '.')
|
|
44
|
+
const result = await runSourceMapUpload(root, process.env, (input, init) => fetch(input, init))
|
|
45
|
+
console.info(sourceMapUploadReport(result.uploaded, result.state))
|
|
46
|
+
}
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
// The source maps of a build, found on disk and uploaded to Operations with the run-scoped credential Control minted
|
|
2
|
+
// (backlog 36). The Cloudflare runner uploads what its checkout holds; a Zerops build runs `fabrika-source-maps`.
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
OPERATIONS_ARTIFACT_HEADERS,
|
|
6
|
+
type OperationsArtifactUploadConfiguration,
|
|
7
|
+
type OperationsAssetRoot,
|
|
8
|
+
type OperationsSourceMapUploadState,
|
|
9
|
+
} from '@fabrika/operations-contract/releases'
|
|
10
|
+
import { stat } from 'node:fs/promises'
|
|
11
|
+
import { relative, resolve, sep } from 'node:path'
|
|
12
|
+
import { z } from 'zod'
|
|
13
|
+
|
|
14
|
+
/** Operations' own limits (`artifact-upload.ts`): a larger map or one past the run's budget is refused there. */
|
|
15
|
+
export const MAX_SOURCE_MAP_BYTES = 8 * 1024 * 1024
|
|
16
|
+
export const MAX_SOURCE_MAPS = 256
|
|
17
|
+
export const MAX_SOURCE_MAP_TOTAL_BYTES = 64 * 1024 * 1024
|
|
18
|
+
|
|
19
|
+
/** One upload request may take this long; a build must not wait out its pipeline limit on an unreachable Operations. */
|
|
20
|
+
const UPLOAD_TIMEOUT_MS = 30_000
|
|
21
|
+
|
|
22
|
+
export interface SourceMapArtifact {
|
|
23
|
+
logicalPath: string
|
|
24
|
+
digest: string
|
|
25
|
+
body: ArrayBuffer
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export interface SourceMapCollection {
|
|
29
|
+
artifacts: SourceMapArtifact[]
|
|
30
|
+
/** A map was skipped: over a limit, or without a logical path. */
|
|
31
|
+
incomplete: boolean
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export type ArtifactFetch = (input: string | URL | Request, init?: RequestInit) => Promise<Response>
|
|
35
|
+
|
|
36
|
+
export interface SourceMapUploadResult {
|
|
37
|
+
uploaded: number
|
|
38
|
+
state: OperationsSourceMapUploadState
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const IGNORED_SEGMENTS = new Set(['node_modules', '.git'])
|
|
42
|
+
|
|
43
|
+
/** Adds the map at `path` when it fits the limits and has a logical path, else marks the collection incomplete; answers the bytes admitted so far. */
|
|
44
|
+
async function admit(
|
|
45
|
+
collection: SourceMapCollection,
|
|
46
|
+
totalBytes: number,
|
|
47
|
+
path: string,
|
|
48
|
+
logicalPath: (raw: string) => string | null,
|
|
49
|
+
): Promise<number> {
|
|
50
|
+
if (collection.artifacts.length >= MAX_SOURCE_MAPS) {
|
|
51
|
+
collection.incomplete = true
|
|
52
|
+
return totalBytes
|
|
53
|
+
}
|
|
54
|
+
const file = Bun.file(path)
|
|
55
|
+
if (file.size > MAX_SOURCE_MAP_BYTES || totalBytes + file.size > MAX_SOURCE_MAP_TOTAL_BYTES) {
|
|
56
|
+
collection.incomplete = true
|
|
57
|
+
return totalBytes
|
|
58
|
+
}
|
|
59
|
+
const body = await file.arrayBuffer()
|
|
60
|
+
const logical = logicalPath(new TextDecoder().decode(body))
|
|
61
|
+
if (logical === null) {
|
|
62
|
+
collection.incomplete = true
|
|
63
|
+
return totalBytes
|
|
64
|
+
}
|
|
65
|
+
collection.artifacts.push({ logicalPath: logical, digest: await digest(body), body })
|
|
66
|
+
return totalBytes + file.size
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
async function* mapsUnder(dir: string): AsyncGenerator<string> {
|
|
70
|
+
const glob = new Bun.Glob('**/*.map')
|
|
71
|
+
for await (const relativePath of glob.scan({ cwd: dir, dot: false, onlyFiles: true, followSymlinks: false })) {
|
|
72
|
+
if (relativePath.split('/').some((part) => IGNORED_SEGMENTS.has(part))) continue
|
|
73
|
+
yield relativePath
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** Every `*.map` of a checkout, at the logical path its own `file` field names (the Cloudflare runner). */
|
|
78
|
+
export async function collectSourceMaps(cwd: string): Promise<SourceMapCollection> {
|
|
79
|
+
const collection: SourceMapCollection = { artifacts: [], incomplete: false }
|
|
80
|
+
let totalBytes = 0
|
|
81
|
+
for await (const relativePath of mapsUnder(cwd)) {
|
|
82
|
+
totalBytes = await admit(collection, totalBytes, `${cwd}/${relativePath}`, (raw) => sourceMapLogicalPath(raw, relativePath))
|
|
83
|
+
}
|
|
84
|
+
return collection
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Every `*.map` under each asset root, at the URL path of the file it maps: `<prefix>/<path under dir>` without its
|
|
89
|
+
* `.map`, as the browser names that file in a stack frame. A root outside `root` is skipped as incomplete.
|
|
90
|
+
*/
|
|
91
|
+
export async function collectAssetSourceMaps(root: string, assetRoots: readonly OperationsAssetRoot[]): Promise<SourceMapCollection> {
|
|
92
|
+
const collection: SourceMapCollection = { artifacts: [], incomplete: false }
|
|
93
|
+
let totalBytes = 0
|
|
94
|
+
const base = resolve(root)
|
|
95
|
+
for (const assetRoot of assetRoots) {
|
|
96
|
+
const dir = resolve(base, assetRoot.dir)
|
|
97
|
+
if (dir !== base && !dir.startsWith(`${base}${sep}`)) {
|
|
98
|
+
collection.incomplete = true
|
|
99
|
+
continue
|
|
100
|
+
}
|
|
101
|
+
if (!(await directoryExists(dir))) continue
|
|
102
|
+
for await (const relativePath of mapsUnder(dir)) {
|
|
103
|
+
const served = `${assetRoot.prefix.replace(/\/+$/, '')}/${relative(dir, resolve(dir, relativePath)).split(sep).join('/')}`
|
|
104
|
+
const logical = normalizedLogicalPath(served.slice(0, -'.map'.length))
|
|
105
|
+
totalBytes = await admit(collection, totalBytes, resolve(dir, relativePath), () => logical)
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
return collection
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
async function directoryExists(path: string): Promise<boolean> {
|
|
112
|
+
try {
|
|
113
|
+
return (await stat(path)).isDirectory()
|
|
114
|
+
} catch {
|
|
115
|
+
return false
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Upload each map with the run's bearer. A refused or failed upload leaves the state incomplete and never throws, so a
|
|
121
|
+
* deploy succeeds without its maps; nothing about the request, the bearer included, is printed.
|
|
122
|
+
*/
|
|
123
|
+
export async function uploadSourceMaps(
|
|
124
|
+
destination: OperationsArtifactUploadConfiguration,
|
|
125
|
+
collection: SourceMapCollection,
|
|
126
|
+
fetch: ArtifactFetch,
|
|
127
|
+
): Promise<SourceMapUploadResult> {
|
|
128
|
+
let uploaded = 0
|
|
129
|
+
let incomplete = collection.incomplete
|
|
130
|
+
try {
|
|
131
|
+
for (const artifact of collection.artifacts) {
|
|
132
|
+
const response = await fetch(destination.url, {
|
|
133
|
+
method: 'POST',
|
|
134
|
+
headers: {
|
|
135
|
+
authorization: `Bearer ${destination.bearer}`,
|
|
136
|
+
'content-type': 'application/json',
|
|
137
|
+
[OPERATIONS_ARTIFACT_HEADERS.appId]: destination.appId,
|
|
138
|
+
[OPERATIONS_ARTIFACT_HEADERS.environment]: destination.environment,
|
|
139
|
+
[OPERATIONS_ARTIFACT_HEADERS.serviceKey]: destination.serviceKey,
|
|
140
|
+
[OPERATIONS_ARTIFACT_HEADERS.release]: destination.release,
|
|
141
|
+
[OPERATIONS_ARTIFACT_HEADERS.runId]: destination.runId,
|
|
142
|
+
[OPERATIONS_ARTIFACT_HEADERS.logicalPath]: artifact.logicalPath,
|
|
143
|
+
[OPERATIONS_ARTIFACT_HEADERS.digest]: artifact.digest,
|
|
144
|
+
},
|
|
145
|
+
body: artifact.body,
|
|
146
|
+
signal: AbortSignal.timeout(UPLOAD_TIMEOUT_MS),
|
|
147
|
+
})
|
|
148
|
+
if (response.ok) uploaded++
|
|
149
|
+
else incomplete = true
|
|
150
|
+
await response.body?.cancel().catch(() => {})
|
|
151
|
+
}
|
|
152
|
+
} catch {
|
|
153
|
+
incomplete = true
|
|
154
|
+
}
|
|
155
|
+
return { uploaded, state: incomplete ? 'incomplete' : 'complete' }
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
const sourceMapFileSchema = z.object({ file: z.string().min(1) })
|
|
159
|
+
|
|
160
|
+
function sourceMapLogicalPath(raw: string, mapPath: string): string | null {
|
|
161
|
+
let value: unknown
|
|
162
|
+
try {
|
|
163
|
+
value = JSON.parse(raw)
|
|
164
|
+
} catch {
|
|
165
|
+
return null
|
|
166
|
+
}
|
|
167
|
+
const parsed = sourceMapFileSchema.safeParse(value)
|
|
168
|
+
if (!parsed.success) return null
|
|
169
|
+
const { file } = parsed.data
|
|
170
|
+
const explicitlyRooted = file.startsWith('/') || file.startsWith('~/') || URL.canParse(file)
|
|
171
|
+
if (!explicitlyRooted && !file.includes('/') && mapPath.includes('/')) {
|
|
172
|
+
// A nested map with only `file: "app.js"` does not reveal its public build root.
|
|
173
|
+
// Guessing from the filesystem path would reintroduce basename collisions.
|
|
174
|
+
return null
|
|
175
|
+
}
|
|
176
|
+
let path = file
|
|
177
|
+
try {
|
|
178
|
+
path = new URL(file).pathname
|
|
179
|
+
} catch {
|
|
180
|
+
const query = path.indexOf('?')
|
|
181
|
+
if (query !== -1) path = path.slice(0, query)
|
|
182
|
+
const hash = path.indexOf('#')
|
|
183
|
+
if (hash !== -1) path = path.slice(0, hash)
|
|
184
|
+
}
|
|
185
|
+
return normalizedLogicalPath(path)
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/** Operations' `logicalAssetPath` of a URL path: no leading slash, no empty, `.` or `..` segment. */
|
|
189
|
+
function normalizedLogicalPath(path: string): string | null {
|
|
190
|
+
const parts = path.replaceAll('\\', '/').replace(/^~?\//, '').split('/').filter((part) => part !== '' && part !== '.')
|
|
191
|
+
if (parts.length === 0 || parts.some((part) => part === '..')) return null
|
|
192
|
+
return parts.join('/')
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
async function digest(body: ArrayBuffer): Promise<string> {
|
|
196
|
+
const bytes = new Uint8Array(await crypto.subtle.digest('SHA-256', body))
|
|
197
|
+
return [...bytes].map((byte) => byte.toString(16).padStart(2, '0')).join('')
|
|
198
|
+
}
|