@stacksjs/actions 0.75.592 → 0.75.597
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/README.md +84 -0
- package/dist/bump.js +1 -1
- package/dist/github-release-assets.d.ts +2 -0
- package/dist/github-release-assets.js +1 -0
- package/dist/library/archive.d.ts +7 -0
- package/dist/library/archive.js +1 -0
- package/dist/library/browser-syntax.d.ts +2 -0
- package/dist/library/browser-syntax.js +1 -0
- package/dist/library/build.d.ts +3 -1
- package/dist/library/build.js +1 -1
- package/dist/library/hosted.d.ts +46 -0
- package/dist/library/hosted.js +6 -0
- package/dist/library/installed.d.ts +2 -0
- package/dist/library/installed.js +1 -0
- package/dist/library/manifest.d.ts +1 -1
- package/dist/library/packages.d.ts +2 -1
- package/dist/library/packages.js +1 -1
- package/dist/library/publish.d.ts +2 -2
- package/dist/library/publish.js +1 -1
- package/dist/library/release-artifacts.d.ts +55 -0
- package/dist/library/release-artifacts.js +2 -0
- package/dist/library/release-download.d.ts +6 -0
- package/dist/library/release-download.js +1 -0
- package/dist/library/release-lifecycle.d.ts +22 -0
- package/dist/library/release-lifecycle.js +4 -0
- package/dist/library/release-network.d.ts +18 -0
- package/dist/library/release-network.js +1 -0
- package/dist/library/release-plan.d.ts +31 -0
- package/dist/library/release-plan.js +1 -0
- package/dist/library/release-registry-auth.d.ts +15 -0
- package/dist/library/release-registry-auth.js +1 -0
- package/dist/library/release-registry.d.ts +43 -0
- package/dist/library/release-registry.js +1 -0
- package/dist/library/release-selection.d.ts +15 -0
- package/dist/library/release-selection.js +1 -0
- package/dist/library/release-source.d.ts +56 -0
- package/dist/library/release-source.js +1 -0
- package/dist/release-evidence.d.ts +16 -0
- package/dist/release-evidence.js +1 -1
- package/dist/release.js +1 -1
- package/package.json +46 -44
package/README.md
CHANGED
|
@@ -41,6 +41,90 @@ When the API entry is spawned with Bun IPC, it sends
|
|
|
41
41
|
the server is listening. Use this address to configure a fixture proxy or make
|
|
42
42
|
requests, without probing and releasing a port before the child starts.
|
|
43
43
|
|
|
44
|
+
## Retained library release artifacts
|
|
45
|
+
|
|
46
|
+
The narrow `@stacksjs/actions/library/release-artifacts` entrypoint supplies
|
|
47
|
+
`prepareLibraryRelease`, `inspectLibraryRelease`, `materializeLibraryRelease`
|
|
48
|
+
and `retainLibraryRelease`. Preparation uses the existing native resolver and
|
|
49
|
+
builder, runs declared project checks, then packs each public package once with
|
|
50
|
+
Bun. No configured packages, unmatched sample defaults, private-only packages
|
|
51
|
+
and `releaseable: false` are no-ops on this release path. Explicit library
|
|
52
|
+
build/publication keeps its existing strict behavior.
|
|
53
|
+
|
|
54
|
+
A version 1 `candidate.json` records repository/full source SHA, optional source
|
|
55
|
+
tag, Bun/packer versions and the installed stx compiler version when used, independent package versions, registry/access/tag,
|
|
56
|
+
original `.tgz` SHA-256/SHA-512 SRI and per-file identities, named check receipts,
|
|
57
|
+
and optional hosted files. Hosted retention consumes the native builder report
|
|
58
|
+
directly; compiler bundle/CSS/manifest files must equal the package's bytes.
|
|
59
|
+
Candidate directories are created exclusively and their record is written last.
|
|
60
|
+
Inspection validates bounded archives, paths, exports, manifests and every
|
|
61
|
+
retained identity without rebuilding or executing historical source. Restore
|
|
62
|
+
uses an exclusively created clean destination and writes regular files only.
|
|
63
|
+
|
|
64
|
+
`@stacksjs/actions/library/release-registry` supplies
|
|
65
|
+
`publishPreparedLibraryRelease` and `readbackLibraryRelease`. Publication names
|
|
66
|
+
the retained archive in publisher argv and disables lifecycle scripts. Default
|
|
67
|
+
npm discovery chooses installed Bun, then npm; explicit publisher selection
|
|
68
|
+
never falls back. Existing versions are skipped only after comparing actual
|
|
69
|
+
registry archive bytes and the requested tag. Different bytes are terminal;
|
|
70
|
+
there is no overwrite or automatic tag mutation. Receipts retain actual tags,
|
|
71
|
+
source SHA and, for historical readback, a separate current verifier SHA.
|
|
72
|
+
|
|
73
|
+
Readback shares one monotonic deadline across metadata, archive and tag reads,
|
|
74
|
+
with individual request/body budgets. Network errors, 429, 5xx and post-upload
|
|
75
|
+
propagation 404 are retry eligible. Authorization, schema, integrity and tag
|
|
76
|
+
mismatches fail immediately. Redirects and tarballs outside the actual registry
|
|
77
|
+
origin are refused. The npm adapter defaults to `registry.npmjs.org`; an explicit
|
|
78
|
+
private HTTPS origin is used consistently for publication and readback.
|
|
79
|
+
|
|
80
|
+
Bun 1.4.4 documents pre-existing `.tgz` publication and is exercised against an
|
|
81
|
+
owned local npm fixture that compares uploaded attachment bytes. npm accepts a
|
|
82
|
+
package archive as its package spec and is tested by the same local upload fixture
|
|
83
|
+
when installed. Pantry 0.11.206 documents directory/glob
|
|
84
|
+
publication only: exact retained-archive publication and Pantry-registry readback
|
|
85
|
+
are explicitly unsupported here. Legacy directory-based `libs:publish`
|
|
86
|
+
discovery and behavior remain unchanged.
|
|
87
|
+
|
|
88
|
+
`@stacksjs/actions/library/release-source` accepts exact CI repository/SHA/run,
|
|
89
|
+
workflow, branch, required jobs and one nonexpired, bounded artifact owned by
|
|
90
|
+
that run. It rejects forks, duplicate/missing jobs and artifacts, and supplies a
|
|
91
|
+
safe derived download URL. The shared CI evaluator preserves the framework's
|
|
92
|
+
existing job selection and never-started-job retry policy; callers provide their
|
|
93
|
+
own workflow/job names. Ownership receipts are data, never historical code.
|
|
94
|
+
|
|
95
|
+
## Installed library payloads
|
|
96
|
+
|
|
97
|
+
Use the narrow `@stacksjs/actions/library/installed` entrypoint to verify a
|
|
98
|
+
caller-owned, stable installation against the native archive reader:
|
|
99
|
+
|
|
100
|
+
```ts
|
|
101
|
+
import { libraryArchiveEntries } from '@stacksjs/actions/library/archive'
|
|
102
|
+
import { inspectInstalledLibraryPackage } from '@stacksjs/actions/library/installed'
|
|
103
|
+
|
|
104
|
+
const expected = await libraryArchiveEntries(archiveBytes)
|
|
105
|
+
const actual = inspectInstalledLibraryPackage(installedDirectory, expected, {
|
|
106
|
+
consumerRoot: isolatedConsumerDirectory,
|
|
107
|
+
allowBunTag: true, // Optional Bun file-archive installer marker only.
|
|
108
|
+
})
|
|
109
|
+
// Run product checks or serve browser fixtures using this complete actual Map.
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
Inspection checks every actual file and the complete expected key set, then
|
|
113
|
+
returns freshly read bytes. Missing, changed and unexpected files, symlinks,
|
|
114
|
+
special files, unsafe paths and consumer-root escapes fail closed. Standalone
|
|
115
|
+
inspection also checks nested `node_modules`; it has no arbitrary ignore list.
|
|
116
|
+
The optional root `.bun-tag` must be a regular 95-byte SHA-512 SRI marker and is
|
|
117
|
+
excluded from the returned archive payload. This checks its shape, not installer
|
|
118
|
+
authenticity. A marker already present in the expected archive must match its
|
|
119
|
+
expected bytes normally.
|
|
120
|
+
|
|
121
|
+
Default budgets are 100,000 entries (including directories/installer metadata),
|
|
122
|
+
128 nested directory levels, and 256 MiB of total and individual file bytes.
|
|
123
|
+
`maxEntries`, `maxDepth`, `maxBytes` and `maxFileBytes` accept positive safe
|
|
124
|
+
integers. The total includes allowed installer metadata. Inspection does not
|
|
125
|
+
establish registry/source ownership or provide a snapshot against concurrent
|
|
126
|
+
writers; use an isolated installation with no concurrent mutation.
|
|
127
|
+
|
|
44
128
|
## 🧪 Testing
|
|
45
129
|
|
|
46
130
|
```bash
|
package/dist/bump.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import{execSync,log,parseOptions}from"@stacksjs/cli";import{path as p}from"@stacksjs/path";import{incrementVersion,isReleaseType,isValidVersion,versionBump}from"@stacksjs/bumpx";import{isCalendarBump,nextCalendarVersion}from"./calendar-version";import{describeGitHubRelease,publishGitHubRelease}from"./github-release";import{pantryLockViolations}from"./pantry-lock";import{resolvePantryToolchain}from"./pantry-toolchain";import{releasePushTarget}from"./release-push-target";import{generateChangelog,loadLogsmithConfig}from"@stacksjs/logsmith";import{existsSync,readdirSync,readFileSync,unlinkSync,writeFileSync}from"node:fs";import{join,relative}from"node:path";const options=parseOptions();const rawBump=options?.bump?.toString();const bumpArg=rawBump?isReleaseType(rawBump)||isCalendarBump(rawBump)||isValidVersion(rawBump)?rawBump:null:null;if(rawBump&&!bumpArg)throw new Error(`Invalid --bump "${rawBump}"; expected a Bumpx release type, calendar, or semantic version`);const isDryRun=options?.dryRun===true;const isVerbose=options?.verbose===true;async function readPackage(file){if(!existsSync(file))return;return await Bun.file(file).json()}const rootManifest=p.projectPath("package.json");const rootPkg=await readPackage(rootManifest);const isFrameworkRelease=rootPkg?.name==="stacks"&&existsSync(p.frameworkPath("core/package.json"));const bumpCwd=isFrameworkRelease?p.frameworkPath("core"):p.projectPath();const primaryManifest=isFrameworkRelease?p.frameworkPath("core/package.json"):rootManifest;async function resolveBumpArg(bump){if(!bump)return bump;if(isCalendarBump(bump)){const pkg=await readPackage(primaryManifest);return nextCalendarVersion(pkg?.version)}const pkg=await readPackage(primaryManifest);if(!pkg?.version)throw new Error(`${primaryManifest} declares no version`);return incrementVersion(pkg.version,bump,options?.preid)}const resolvedBumpArg=await resolveBumpArg(bumpArg);async function git(args,cwd=p.projectPath(),options={}){return await execSync(["git",...args],{cwd,stdin:"inherit",stderr:"pipe",throwOnError:options.throwOnError??true})}const pushTarget=isDryRun?undefined:releasePushTarget(p.projectPath(),isFrameworkRelease?"main":
|
|
1
|
+
import{execSync,log,parseOptions}from"@stacksjs/cli";import{path as p}from"@stacksjs/path";import{incrementVersion,isReleaseType,isValidVersion,versionBump}from"@stacksjs/bumpx";import{isCalendarBump,nextCalendarVersion}from"./calendar-version";import{describeGitHubRelease,publishGitHubRelease}from"./github-release";import{pantryLockViolations}from"./pantry-lock";import{resolvePantryToolchain}from"./pantry-toolchain";import{releasePushTarget}from"./release-push-target";import{planNativeLibraryRelease}from"./library/release-plan";import{generateChangelog,loadLogsmithConfig}from"@stacksjs/logsmith";import{existsSync,readdirSync,readFileSync,unlinkSync,writeFileSync}from"node:fs";import{join,relative}from"node:path";const options=parseOptions();const rawBump=options?.bump?.toString();const bumpArg=rawBump?isReleaseType(rawBump)||isCalendarBump(rawBump)||isValidVersion(rawBump)?rawBump:null:null;if(rawBump&&!bumpArg)throw new Error(`Invalid --bump "${rawBump}"; expected a Bumpx release type, calendar, or semantic version`);const isDryRun=options?.dryRun===true;const isVerbose=options?.verbose===true;async function readPackage(file){if(!existsSync(file))return;return await Bun.file(file).json()}const rootManifest=p.projectPath("package.json");const rootPkg=await readPackage(rootManifest);const isFrameworkRelease=rootPkg?.name==="stacks"&&existsSync(p.frameworkPath("core/package.json"));const bumpCwd=isFrameworkRelease?p.frameworkPath("core"):p.projectPath();const primaryManifest=isFrameworkRelease?p.frameworkPath("core/package.json"):rootManifest;async function resolveBumpArg(bump){if(!bump)return bump;if(isCalendarBump(bump)){const pkg=await readPackage(primaryManifest);return nextCalendarVersion(pkg?.version)}const pkg=await readPackage(primaryManifest);if(!pkg?.version)throw new Error(`${primaryManifest} declares no version`);return incrementVersion(pkg.version,bump,options?.preid)}const resolvedBumpArg=await resolveBumpArg(bumpArg);async function git(args,cwd=p.projectPath(),options={}){return await execSync(["git",...args],{cwd,stdin:"inherit",stderr:"pipe",throwOnError:options.throwOnError??true})}const libraryPlan=isFrameworkRelease?undefined:await planNativeLibraryRelease({cwd:p.projectPath(),bump:resolvedBumpArg??undefined,preid:options?.preid});const pushTarget=isDryRun?undefined:releasePushTarget(p.projectPath(),isFrameworkRelease?"main":libraryPlan?.sourceCi.branch);async function isGitIgnored(file){const output=await git(["check-ignore","--",file],p.projectPath(),{throwOnError:false});return output.trim().length>0}async function readVersion(file){const pkg=await readPackage(file);if(!pkg?.version||!/^\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?$/.test(pkg.version))throw new Error(`Invalid version in ${file}: ${pkg?.version??"<missing>"}`);return pkg.version}function lockfileVersion(contents){const match=contents.match(/"lockfileVersion"\s*:\s*(\d+)/);return match?Number(match[1]):null}async function packageFilesFor(pattern,cwd){const glob=new Bun.Glob(pattern);const files=[];for await(const file of glob.scan({cwd,absolute:true,onlyFiles:true})){if(file.includes("/node_modules/")||file.includes("/dist/")||file.includes("/pantry/"))continue;files.push(`./${relative(bumpCwd,file)}`)}return files}async function existingPackageFiles(files){const existing=[];for(const file of files){const absolutePath=join(bumpCwd,file);if(await Bun.file(absolutePath).exists())existing.push(file)}return existing}async function frameworkBumpFiles(){return Array.from(new Set([...await packageFilesFor("package.json",bumpCwd),...await packageFilesFor("*/package.json",bumpCwd),...await packageFilesFor("*/*/package.json",bumpCwd),...await existingPackageFiles(["../package.json","../defaults/ide/vscode/package.json","../api/package.json","../cloud/package.json","../docs/package.json","../orm/package.json","../server/package.json"]),...await packageFilesFor("../libs/**/package.json",bumpCwd)]))}const bumpFiles=isFrameworkRelease?await frameworkBumpFiles():["./package.json"];const pantryToolchain=!isDryRun&&existsSync(p.projectPath("pantry.lock"))?resolvePantryToolchain({cwd:p.projectPath()}):undefined;if(pantryToolchain&&!pantryToolchain.ok)throw new Error(pantryToolchain.message);log.debug(`Release mode: ${isFrameworkRelease?"framework monorepo":"consumer app"} (${rootPkg?.name??"unknown"})`);log.debug(`Bumping ${bumpFiles.length} package manifest(s) in ${bumpCwd}`);await versionBump({release:resolvedBumpArg??undefined,preid:options?.preid,files:bumpFiles,cwd:bumpCwd,recursive:false,commit:false,tag:false,push:false,changelog:false,noGitCheck:true,dryRun:isDryRun,yes:Boolean(resolvedBumpArg),verbose:isVerbose});const nextVersion=isDryRun&&resolvedBumpArg&&isValidVersion(resolvedBumpArg)?resolvedBumpArg:await readVersion(primaryManifest);const latestTag=(await git(["describe","--abbrev=0","--tags"],p.projectPath(),{throwOnError:false}).catch(()=>"")).trim();async function writeChangelog(){const config=await loadLogsmithConfig({dir:p.projectPath(),from:latestTag||undefined,to:"HEAD",output:isDryRun?false:"CHANGELOG.md",theme:"github",verbose:isVerbose});const result=await generateChangelog(config);if(isDryRun){log.info(result.content);return}const changelogPath=p.projectPath("CHANGELOG.md");if(!existsSync(changelogPath))return;let content=readFileSync(changelogPath,"utf-8");const versionSeen=new RegExp(`\\bv?${nextVersion.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")}\\b`);if(/\/compare\/[^)\s]+\.\.\.HEAD\)/.test(content)){content=content.replace(/(\/compare\/[^)\s]+\.\.\.)HEAD(\))/,`$1v${nextVersion}$2`)}else if(!versionSeen.test(content.split(`
|
|
2
2
|
`).slice(0,4).join(`
|
|
3
3
|
`))){content=content.replace(/^(#\s.*\n+)?/,(match)=>`${match??""}## v${nextVersion}
|
|
4
4
|
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{createHash}from"node:crypto";import{lstat,readFile}from"node:fs/promises";import{basename,resolve}from"node:path";import{githubHeaders}from"@stacksjs/github";import{parseGitHubRemote,resolveGitHubToken}from"./github-release";import{boundedBody}from"./library/archive";export async function attachGitHubReleaseAssets(options){if(!options.files.length||options.files.length>20||new Set(options.files).size!==options.files.length)throw new Error("Release assets require one to twenty unique files");const assets=await Promise.all(options.files.map(async(file)=>{const path=resolve(options.cwd,file);const stat=await lstat(path);const name=basename(path);if(!stat.isFile()||stat.isSymbolicLink()||stat.size>2*1024*1024||!/^[\w.-]+\.json$/.test(name))throw new Error("Release receipts must be bounded ordinary JSON files");const bytes=await readFile(path);JSON.parse(bytes.toString());const sha256=createHash("sha256").update(bytes).digest("hex");return{name:`${name.slice(0,-5)}-${sha256.slice(0,16)}.json`,bytes,digest:`sha256:${sha256}`}}));const remote=Bun.spawnSync(["git","remote","get-url","origin"],{cwd:options.cwd,stdout:"pipe",stderr:"pipe"});const repository=parseGitHubRemote(remote.stdout.toString());if(!repository)throw new Error("Release receipts require a GitHub origin");const token=options.token??resolveGitHubToken();if(!token)throw new Error("Attaching release receipts requires a GitHub release token");const headers=githubHeaders(token);const request=async(url,init={})=>{const response=await(options.fetch??fetch)(url,{...init,redirect:"manual",signal:AbortSignal.timeout(30000),headers:{...headers,...init.headers}});if(!response.ok){await response.body?.cancel();throw new Error(`GitHub release receipt request failed: HTTP ${response.status}`)}return JSON.parse(new TextDecoder().decode(await boundedBody(response,2*1024*1024)))};const base=`https://api.github.com/repos/${repository.owner}/${repository.repo}/releases`;const release=await request(`${base}/tags/${encodeURIComponent(options.tag)}`);if(!Number.isSafeInteger(release.id)||release.id<=0||!Array.isArray(release.assets))throw new Error("GitHub release has no valid asset inventory");const links=[];for(const asset of assets){const matching=release.assets.filter((existing)=>existing.name===asset.name);if(matching.length>1)throw new Error("GitHub release has duplicate receipt assets");const uploaded=matching[0]??await request(`https://uploads.github.com/repos/${repository.owner}/${repository.repo}/releases/${release.id}/assets?name=${encodeURIComponent(asset.name)}`,{method:"POST",headers:{"Content-Type":"application/json"},body:asset.bytes});if(uploaded.name!==asset.name||uploaded.size!==asset.bytes.length||uploaded.digest!==asset.digest||typeof uploaded.browser_download_url!=="string")throw new Error("GitHub release receipt asset identity differs");links.push(uploaded.browser_download_url)}return links}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export declare function boundedBody(response: Response, limit: number): Promise<Uint8Array>;
|
|
2
|
+
// Check physical tar bounds before an archive reader allocates declared sizes.
|
|
3
|
+
// Sparse files and links cannot describe an installed npm code identity here.
|
|
4
|
+
export declare function validateTarBounds(tar: Uint8Array): number;
|
|
5
|
+
/** Bounded npm archive reader shared by retained candidates and release evidence. */
|
|
6
|
+
export declare function libraryArchiveEntries(bytes: Uint8Array, limit?: unknown): Promise<Map<string, Uint8Array>>;
|
|
7
|
+
export declare function safeArchivePath(path: string): boolean;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
function fail(message){throw new Error(`Release verification blocked: ${message}`)}export async function boundedBody(response,limit){const declared=response.headers.get("content-length");if(declared&&Number(declared)>limit){await response.body?.cancel();fail("Response exceeds its byte budget")}if(!response.body)fail("Response has no body");const reader=response.body.getReader();const chunks=[];let length=0;try{while(true){const next=await reader.read();if(next.done)break;length+=next.value.byteLength;if(length>limit)fail("Response exceeds its byte budget");chunks.push(next.value)}return Buffer.concat(chunks,length)}finally{await reader.cancel().catch(()=>{});reader.releaseLock()}}export function validateTarBounds(tar){if(!tar.byteLength||tar.byteLength%512)fail("Published archive has invalid tar block bounds");let offset=0;let files=0;let headers=0;const text=(start,length)=>new TextDecoder().decode(tar.subarray(start,start+length)).replace(/\0.*$/s,"").trim();while(offset+512<=tar.byteLength){const header=tar.subarray(offset,offset+512);if(header.every((byte)=>byte===0)){if(tar.subarray(offset).some((byte)=>byte!==0))fail("Published archive has data after its tar terminator");return files}if(++headers>1e5)fail("Published archive has too many entries");const rawSize=text(offset+124,12);if(!/^[0-7]+$/.test(rawSize))fail("Published archive has an unsupported tar size");const size=Number.parseInt(rawSize,8);const end=offset+512+Math.ceil(size/512)*512;if(!Number.isSafeInteger(end)||end>tar.byteLength)fail("Published archive declares bytes outside its tar bounds");const type=header[156]===0?"0":String.fromCharCode(header[156]);if(type==="0"||type==="5"){const prefix=text(offset+345,155);validatePackageTarPath(`${prefix?`${prefix}/`:""}${text(offset,100)}`,type==="5")}if(type==="L")validatePackageTarPath(text(offset+512,size),true);if(type==="0")files++;else if(type==="x"||type==="g"){const metadata=tar.subarray(offset+512,offset+512+size);const allowed=new Set(["path","mtime","atime","ctime","uid","gid","uname","gname","comment","charset","hdrcharset"]);for(let start=0;start<metadata.length;){const space=metadata.indexOf(32,start);if(space<0)fail("Published archive has malformed PAX records");const rawLength=new TextDecoder().decode(metadata.subarray(start,space));const length=Number(rawLength);const end=start+length;if(!/^\d+$/.test(rawLength)||!Number.isSafeInteger(end)||end>metadata.length||end<=space+2||metadata[end-1]!==10)fail("Published archive has malformed PAX bounds");const equals=metadata.indexOf(61,space+1);const key=new TextDecoder().decode(metadata.subarray(space+1,equals));if(equals<0||equals>=end-1||!allowed.has(key))fail("Published archive has unsupported PAX allocation or link metadata");if(key==="path")validatePackageTarPath(new TextDecoder().decode(metadata.subarray(equals+1,end-1)),true);start=end}}else if(type!=="5"&&type!=="L")fail("Published archive contains a link or unsupported tar entry");offset=end}fail("Published archive has no tar terminator")}export async function libraryArchiveEntries(bytes,limit=256*1024*1024){const stream=new Blob([new Uint8Array(bytes)]).stream().pipeThrough(new DecompressionStream("gzip"));const tar=await boundedBody(new Response(stream),limit);const count=validateTarBounds(tar);const files=await new Bun.Archive(tar).files();if(!count||files.size!==count)fail("Published archive has missing or duplicate file paths");const result=new Map;const directories=new Set;for(const[name,file]of files){const path=name.slice("package/".length);if(!name.startsWith("package/")||!safeArchivePath(path)||result.has(path))fail("Published archive contains an unsafe or duplicate package path");if(directories.has(path))fail("Published archive has conflicting file and directory paths");const parts=path.split("/");for(let i=1;i<parts.length;i++){const parent=parts.slice(0,i).join("/");if(result.has(parent))fail("Published archive has conflicting file and directory paths");directories.add(parent)}result.set(path,new Uint8Array(await file.arrayBuffer()))}if(!result.has("package.json"))fail("Published archive has no package manifest");return result}export function safeArchivePath(path){return typeof path==="string"&&!!path&&!path.includes("\\")&&!path.includes(":")&&!path.includes("\x00")&&!path.split("/").some((part)=>!part||part==="."||part==="..")}function validatePackageTarPath(name,directory){const clean=directory?name.replace(/\/$/,""):name;if(directory&&clean==="package")return;if(!clean.startsWith("package/")||!safeArchivePath(clean.slice("package/".length)))fail("Published archive contains an unsafe package path")}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{parse}from"@babel/parser";import{isIdentifier,isMemberExpression,isMetaProperty,isOptionalMemberExpression,isStringLiteral,isTemplateLiteral,traverse}from"@babel/types";export function readsEnvironment(contents,path){const source=parse(contents,{sourceType:"module",sourceFilename:path,plugins:/\.[jt]sx$/.test(path)?["typescript","jsx"]:["typescript"]});let found=false;const property=(node)=>{if(!node||!isMemberExpression(node)&&!isOptionalMemberExpression(node))return;const name=!node.computed&&isIdentifier(node.property)?node.property.name:isStringLiteral(node.property)?node.property.value:isTemplateLiteral(node.property)&&!node.property.expressions.length?node.property.quasis[0]?.value.cooked:undefined;return{target:node.object,name}};traverse(source,(node,ancestors)=>{const access=property(node);if(access?.name!=="env")return;const target=access.target;const outer=property(target);const global=isIdentifier(target)&&["process","Bun"].includes(target.name)||outer&&["process","Bun"].includes(outer.name??"")&&isIdentifier(outer.target)&&["globalThis","window","global"].includes(outer.target.name);const meta=isMetaProperty(target)&&target.meta.name==="import"&&target.property.name==="meta";const parent=property(ancestors.at(-1)?.node);const nodeEnv=isIdentifier(target)&&target.name==="process"&&parent?.target===node&&parent.name==="NODE_ENV";if((global||meta)&&!nodeEnv)found=true});return found}
|
package/dist/library/build.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import type { LibraryConfig } from '@stacksjs/types';
|
|
1
|
+
import type { LibraryConfig } from '@stacksjs/types/library';
|
|
2
|
+
import type { LibraryHostedBuildReport } from './hosted';
|
|
2
3
|
import type { ResolvedLibraryPackage } from './packages';
|
|
3
4
|
/**
|
|
4
5
|
* Stage, generate and build every configured library package of the given
|
|
@@ -18,6 +19,7 @@ export declare interface LibraryBuildReport {
|
|
|
18
19
|
kind: string
|
|
19
20
|
dir: string
|
|
20
21
|
sources: number
|
|
22
|
+
hosted?: LibraryHostedBuildReport
|
|
21
23
|
}
|
|
22
24
|
export declare interface LibraryPackageBuildOptions {
|
|
23
25
|
kinds?: Array<ResolvedLibraryPackage['kind']>
|
package/dist/library/build.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import{existsSync}from"node:fs";import{cp,mkdir,rm}from"node:fs/promises";import{dirname,relative,resolve}from"node:path";import{transpilePackage,validateDeclarations,validateRuntimeExports}from"@stacksjs/build";import{log}from"@stacksjs/logging";import{projectPath}from"@stacksjs/path";import{buildComponentLibrary}from"@stacksjs/stx";import{functionEntryData}from"./entries";import{ambientGlobalsUsed,stxAmbientGlobals}from"./globals";import{generatedReadme,libraryManifest,serializeManifest}from"./manifest";import{configuredLibrary,LibraryConfigError,resolveLibraryPackages}from"./packages";export async function buildLibraryPackages(options={}){const config=options.config??await configuredLibrary();const kinds=options.kinds;const compile=options.compile??true;const all=await resolveLibraryPackages(config,{onUnmatched:options.allowEmpty?"skip":"error"});const packages=kinds?all.filter((pkg)=>kinds.includes(pkg.kind)):all;if(!packages.length){if(options.allowEmpty){log.info("No library packages are configured. Nothing to build.");return[]}throw new LibraryConfigError(kinds?`No ${kinds.join("/")} packages are configured. Add one to \`packages\` in config/library.ts.`:"No library packages are configured. Add one to `packages` in config/library.ts.")}const version=await projectVersion();const reports=[];for(const pkg of packages){const verb=compile?"Building":"Generating";log.info(`${verb} ${pkg.name} (${pkg.kind}, ${pkg.sources.length} source${pkg.sources.length===1?"":"s"})...`);if(compile)await rm(resolve(pkg.dir,"dist"),{recursive:true,force:true});await mkdir(pkg.dir,{recursive:true});if(compile)runPrebuild(pkg);if(pkg.kind==="functions")await buildFunctionPackage(pkg,config,version,compile);else await buildComponentPackage(pkg,config,version,compile);
|
|
1
|
+
import{existsSync}from"node:fs";import{cp,lstat,mkdir,rm}from"node:fs/promises";import{dirname,relative,resolve}from"node:path";import{transpilePackage,validateDeclarations,validateRuntimeExports}from"@stacksjs/build";import{log}from"@stacksjs/logging";import{projectPath}from"@stacksjs/path";import{buildComponentLibrary}from"@stacksjs/stx";import{functionEntryData}from"./entries";import{ambientGlobalsUsed,stxAmbientGlobals}from"./globals";import{assertHostedSourcePath,buildHostedOutput,materializeHostedInput,prepareHostedInputs,validateHostedBrowser}from"./hosted";import{generatedReadme,libraryManifest,serializeManifest}from"./manifest";import{configuredLibrary,LibraryConfigError,resolveLibraryPackages}from"./packages";export async function buildLibraryPackages(options={}){const config=options.config??await configuredLibrary();const kinds=options.kinds;const compile=options.compile??true;const all=await resolveLibraryPackages(config,{onUnmatched:options.allowEmpty?"skip":"error"});const packages=kinds?all.filter((pkg)=>kinds.includes(pkg.kind)):all;if(!packages.length){if(options.allowEmpty){log.info("No library packages are configured. Nothing to build.");return[]}throw new LibraryConfigError(kinds?`No ${kinds.join("/")} packages are configured. Add one to \`packages\` in config/library.ts.`:"No library packages are configured. Add one to `packages` in config/library.ts.")}const hostedInputs=await prepareHostedInputs(packages);const version=await projectVersion();const reports=[];for(const pkg of packages){const verb=compile?"Building":"Generating";log.info(`${verb} ${pkg.name} (${pkg.kind}, ${pkg.sources.length} source${pkg.sources.length===1?"":"s"})...`);if(compile)await rm(resolve(pkg.dir,"dist"),{recursive:true,force:true});await mkdir(pkg.dir,{recursive:true});if(compile)runPrebuild(pkg);if(compile)await prepareHostedInputs(packages);const prepared=hostedInputs.get(pkg.name);const input=compile&&prepared?await materializeHostedInput(prepared):undefined;let componentStyles=[];if(pkg.kind==="functions")await buildFunctionPackage(pkg,config,version,compile);else componentStyles=await buildComponentPackage(pkg,config,version,compile,input);const assets=compile?await copyAssets(pkg):[];const hosted=compile&&input?await buildHostedOutput(input,pkg.version??version,componentStyles,assets):undefined;reports.push({name:pkg.name,kind:pkg.kind,dir:pkg.dir,sources:pkg.sources.length,...hosted?{hosted}:{}});if(compile)log.success(`Built ${pkg.name} → ${relative(projectPath(),pkg.dir)}/dist`)}return reports}export async function generateLibraryPackages(options={}){return buildLibraryPackages({...options,compile:false})}async function buildFunctionPackage(pkg,config,version,compile){await assertPublishable(pkg);const srcDir=resolve(pkg.dir,"src");await rm(srcDir,{recursive:true,force:true});const staged=[];for(const source of pkg.sources){const destination=resolve(srcDir,relative(pkg.sourceDir,source));await mkdir(dirname(destination),{recursive:true});await Bun.write(destination,await Bun.file(source).text());staged.push(destination)}const entry=resolve(srcDir,"index.ts");if(staged.includes(entry)){throw new LibraryConfigError(`Package "${pkg.name}" claims resources/functions/index.ts, which collides with the generated barrel. Rename it or exclude it.`)}await Bun.write(entry,functionEntryData(pkg,staged));await writeManifest(pkg,config,version);if(!compile)return;await transpilePackage({dir:pkg.dir,pkgName:pkg.name,external:["@stacksjs/*","bun",...Object.keys(pkg.dependencies),...Object.keys(pkg.peerDependencies)]});await validateDeclarations(pkg.dir);await validateRuntimeExports(pkg.dir)}async function buildComponentPackage(pkg,config,version,compile,hosted){if(!compile){await writeManifest(pkg,config,version);return[]}const result=await buildComponentLibrary({inputDir:pkg.sourceDir,outputDir:resolve(pkg.dir,"dist"),prefix:pkg.prefix,components:pkg.sources.map((source)=>{const file=relative(pkg.sourceDir,source);const stem=file.replace(/\.stx$/,"").split("/").at(-1);return pkg.names[stem]?{file,name:pkg.names[stem]}:{file}}),progressive:true,manifest:true,declarations:true,css:true,bundle:true,minify:true,sourcemap:pkg.sourcemap?"external":false});if(hosted){if(result.components.some((component)=>typeof component.styles!=="string"))throw new LibraryConfigError(`Package "${pkg.name}": the installed stx compiler does not expose component style metadata. Upgrade the STX package cohort together.`);await validateHostedBrowser(hosted)}log.debug(`${pkg.name}: compiled ${result.components.length} components (${result.totalBytes} bytes)`);await writeManifest(pkg,config,version);await validateDeclarations(pkg.dir);await validateRuntimeExports(pkg.dir);return result.components.map((component)=>component.styles).filter(Boolean)}function runPrebuild(pkg){if(!pkg.prebuild)return;log.info(`${pkg.name}: ${pkg.prebuild}`);const proc=Bun.spawnSync({cmd:["sh","-c",pkg.prebuild],cwd:projectPath(),stdout:"inherit",stderr:"inherit"});if(proc.exitCode!==0)throw new LibraryConfigError(`Package "${pkg.name}": prebuild \`${pkg.prebuild}\` exited with code ${proc.exitCode}.`)}async function copyAssets(pkg){const outputs=[];for(const asset of pkg.assets){const source=projectPath(asset.source);const destination=resolve(pkg.dir,"dist",asset.dest);if(!existsSync(source)){throw new LibraryConfigError(`Package "${pkg.name}" ships "${asset.source}" as ${asset.dest}, but it does not exist.`+(pkg.prebuild?` The prebuild command did not produce it.`:" Build it first, or set `prebuild` to the command that does."))}if(existsSync(destination))throw new LibraryConfigError(`Package "${pkg.name}": asset ${asset.dest} collides with a file the build wrote to dist/${asset.dest}.`);await mkdir(dirname(destination),{recursive:true});await cp(source,destination,{recursive:true,filter:async(from,to)=>{if(pkg.hosted)assertHostedSourcePath(pkg,from);const stat=await lstat(from);if(pkg.hosted&&stat.isSymbolicLink())throw new LibraryConfigError(`Package "${pkg.name}": hosted assets cannot contain symbolic links: ${from}`);if(stat.isFile())outputs.push({path:to,packagePath:`dist/${relative(resolve(pkg.dir,"dist"),to).replaceAll("\\","/")}`});return true}})}return outputs.sort((a,b)=>a.packagePath.localeCompare(b.packagePath))}async function assertPublishable(pkg){if(pkg.runtime==="stx")return;const globals=await stxAmbientGlobals();if(!globals.size)return;const offenders=[];for(const source of pkg.sources){const used=ambientGlobalsUsed(await Bun.file(source).text(),globals);if(used.length)offenders.push(` ${relative(projectPath(),source)} → ${used.join(", ")}`)}if(!offenders.length)return;throw new LibraryConfigError(`Package "${pkg.name}" uses stx's ambient globals, which no module exports:
|
|
2
2
|
${offenders.join(`
|
|
3
3
|
`)}
|
|
4
4
|
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import type { LibraryHostedOptions } from '@stacksjs/types/library';
|
|
2
|
+
import type { ResolvedLibraryPackage } from './packages';
|
|
3
|
+
/** Share the browser source policy with the native configured-asset copy. */
|
|
4
|
+
export declare function assertHostedSourcePath(pkg: ResolvedLibraryPackage, path: string): void;
|
|
5
|
+
/** Pure config/path validation before writes; generated inputs need not exist yet. */
|
|
6
|
+
export declare function prepareHostedInputs(packages: ResolvedLibraryPackage[]): Promise<Map<string, LibraryHostedInput>>;
|
|
7
|
+
/** Read/compile only after the package's one native prebuild, rechecking paths. */
|
|
8
|
+
export declare function materializeHostedInput(input: LibraryHostedInput): Promise<LibraryHostedInput>;
|
|
9
|
+
/** Validate the compiler's full browser graph before retaining or publishing bytes. */
|
|
10
|
+
export declare function validateHostedBrowser(input: LibraryHostedInput): Promise<void>;
|
|
11
|
+
/** Retain one immutable revision, then move stable aliases to it. */
|
|
12
|
+
export declare function buildHostedOutput(input: LibraryHostedInput, version: string, componentStyles: readonly string[], packageAssets?: readonly { path: string, packagePath: string }[]): Promise<LibraryHostedBuildReport>;
|
|
13
|
+
export declare interface LibraryHostedFile {
|
|
14
|
+
path: string
|
|
15
|
+
bytes: number
|
|
16
|
+
gzipBytes: number
|
|
17
|
+
sha256: string
|
|
18
|
+
integrity: string
|
|
19
|
+
}
|
|
20
|
+
export declare interface LibraryHostedManifest {
|
|
21
|
+
schemaVersion: 1
|
|
22
|
+
name: string
|
|
23
|
+
version: string
|
|
24
|
+
revision: string
|
|
25
|
+
entrypoint: string
|
|
26
|
+
immutableEntrypoint: string
|
|
27
|
+
files: LibraryHostedFile[]
|
|
28
|
+
csp: { styleSrc: string[] }
|
|
29
|
+
}
|
|
30
|
+
export declare interface LibraryHostedBuildReport {
|
|
31
|
+
revision: string
|
|
32
|
+
outputDir: string
|
|
33
|
+
immutableDir: string
|
|
34
|
+
manifestPath: string
|
|
35
|
+
entryPath: string
|
|
36
|
+
manifest: LibraryHostedManifest
|
|
37
|
+
files: Array<LibraryHostedFile & { relativePath: string, immutable: boolean, packagePath?: string }>
|
|
38
|
+
}
|
|
39
|
+
export declare interface LibraryHostedInput {
|
|
40
|
+
pkg: ResolvedLibraryPackage
|
|
41
|
+
config: LibraryHostedOptions
|
|
42
|
+
outputDir: string
|
|
43
|
+
entryPath: string
|
|
44
|
+
loader?: { bytes: Uint8Array, exports: string[] }
|
|
45
|
+
styles: string[]
|
|
46
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import{createHash}from"node:crypto";import{existsSync,realpathSync}from"node:fs";import{mkdir,readFile,rename,unlink,writeFile}from"node:fs/promises";import{basename,dirname,extname,relative,resolve,sep}from"node:path";import{gzipSync}from"node:zlib";import{projectPath}from"@stacksjs/path";import{LibraryConfigError}from"./packages";function fail(pkg,message){throw new LibraryConfigError(`Package "${pkg.name}": ${message}`)}function plainPath(pkg,value,label){if(typeof value!=="string"||!value||value.includes("\\")||value.includes("\x00")||/[\s?#%]/.test(value)||value.split("/").some((part)=>!part||part==="."||part==="..")||value.includes(":"))fail(pkg,`${label} must be a plain project-relative path.`);return projectPath(value)}function assertInsideProject(pkg,path){let existing=path;while(!existsSync(existing)&&dirname(existing)!==existing)existing=dirname(existing);const canonical=realpathSync(existing);const root=realpathSync(projectPath());if(canonical!==root&&!canonical.startsWith(`${root}${sep}`))fail(pkg,`hosted path escapes the project through a symbolic link: ${path}`);return resolve(canonical,relative(existing,path))}function publicPath(pkg,value,label){const path=plainPath(pkg,value,label);if(!value.startsWith("public/"))fail(pkg,`${label} must be beneath public/.`);if(value.split("/").some((part)=>part.startsWith(".")))fail(pkg,`${label} cannot contain hidden path segments because default CI artifact retention excludes them.`);assertInsideProject(pkg,path);return path}function publicBase(pkg,value){if(typeof value!=="string"||!value||/[\\\s?#%]/.test(value))fail(pkg,"hosted.base must be an absolute public path or HTTPS URL without a query or fragment.");const path=value.startsWith("https://")?value.replace(/^https:\/\/[^/]+/,"")||"/":value;if(!path.startsWith("/")||path.split("/").some((part)=>part==="."||part==="..")||!value.startsWith("https://")&&value.startsWith("//"))fail(pkg,"hosted.base must be an absolute public path or HTTPS URL without traversal.");if(value.startsWith("https://")){const url=new URL(value);if(url.username||url.password)fail(pkg,"hosted.base cannot contain credentials.")}return value.replace(/\/$/,"")}function digest(bytes,algorithm,encoding){return createHash(algorithm).update(bytes).digest(encoding)}function describe(path,bytes){return{path,bytes:bytes.byteLength,gzipBytes:gzipSync(bytes).byteLength,sha256:digest(bytes,"sha256","hex"),integrity:`sha384-${digest(bytes,"sha384","base64")}`}}export function assertHostedSourcePath(pkg,path){const canonical=existsSync(path)?realpathSync(path):path;const file=relative(projectPath(),canonical).replaceAll("\\","/");if(/^(?:app|config|routes|database|storage\/framework\/(?:server|defaults\/app))\//.test(file)||/(?:^|\/)\.env(?:[./]|$)/.test(file))fail(pkg,`server source "${file}" cannot enter a hosted browser artifact.`)}function browserBoundary(pkg){return{name:"stacks-library-browser-boundary",setup(build){build.onResolve({filter:/^(?:node:|bun(?:$|:)|@stacksjs\/(?:config|env|database|orm|server|auth|cloud|queue|email|secrets|actions)(?:\/|$))/},(args)=>fail(pkg,`privileged import "${args.path}" cannot enter a hosted browser artifact.`));build.onLoad({filter:/.*/},async(args)=>{const path=realpathSync(args.path);const file=relative(projectPath(),path).replaceAll("\\","/");assertHostedSourcePath(pkg,path);if(!/\.[cm]?[jt]sx?$/.test(path))return;const contents=await Bun.file(path).text();const{readsEnvironment}=await import("./browser-syntax");if(readsEnvironment(contents,path))fail(pkg,`environment access in "${file}" cannot enter a hosted browser artifact. Use explicit public define mappings.`);return{contents,loader:path.endsWith(".tsx")?"tsx":path.endsWith(".jsx")?"jsx":/\.[cm]?ts$/.test(path)?"ts":"js"}})}}}async function browserBuild(pkg,source,define={}){let result;try{result=await Bun.build({entrypoints:[source],target:"browser",format:"esm",minify:true,define,plugins:[browserBoundary(pkg)]})}catch(error){fail(pkg,`hosted browser compilation failed: ${error instanceof AggregateError?error.errors.join(`
|
|
2
|
+
`):String(error)}`)}if(!result.success||result.outputs.length!==1||!result.outputs[0])fail(pkg,`hosted browser compilation failed: ${result.logs.join(`
|
|
3
|
+
`)}`);return new Uint8Array(await result.outputs[0].arrayBuffer())}async function loaderInput(pkg,loader){const source=plainPath(pkg,loader.source,"hosted.loader.source");assertInsideProject(pkg,source);if(!await Bun.file(source).exists())fail(pkg,`hosted loader source does not exist: ${loader.source}`);const bytes=await browserBuild(pkg,source,loader.define);const available=new Bun.Transpiler({loader:"js"}).scan(new TextDecoder().decode(bytes)).exports;const exports=loader.exports??available;if(!exports.length)fail(pkg,"hosted loader must export at least one value.");for(const name of exports){if(!/^(?:default|[A-Za-z_$][\w$]*)$/.test(name)||!available.includes(name))fail(pkg,`hosted loader has no export "${name}".`)}for(const[key,value]of Object.entries(loader.define??{})){if(!/^[A-Za-z_$][\w$]*$/.test(key)||typeof value!=="string")fail(pkg,"hosted loader defines must map identifiers to explicit build expressions.")}return{bytes,exports:[...new Set(exports)]}}export async function prepareHostedInputs(packages){const inputs=new Map;const owners=[];const publicBases=new Map;for(const pkg of packages){if(!pkg.hosted)continue;if(pkg.kind!=="web-components")fail(pkg,"hosted output is only supported for web-components packages.");const config={...pkg.hosted,base:publicBase(pkg,pkg.hosted.base)};const outputDir=publicPath(pkg,config.outputDir,"hosted.outputDir");const entryPath=publicPath(pkg,config.entry,"hosted.entry");if(entryPath===outputDir||entryPath.startsWith(`${outputDir}${sep}`)&&dirname(entryPath)!==outputDir)fail(pkg,"hosted.entry must be outside outputDir or directly beneath it, away from immutable revision directories.");if(extname(entryPath)!==".js")fail(pkg,"hosted.entry must end in .js.");if(basename(entryPath)==="bundle.js"&&config.loader)fail(pkg,"hosted loader entry collides with the native bundle.js.");for(const asset of pkg.assets){plainPath(pkg,asset.dest,"hosted asset destination");if(asset.dest.split("/").some((part)=>part.startsWith(".")))fail(pkg,"hosted assets cannot contain hidden path segments.");const reserved=["manifest.json","bundle.js","bundle.css","custom-elements.json",...config.loader?[basename(entryPath)]:[]];if(reserved.some((name)=>asset.dest===name||asset.dest.startsWith(`${name}/`)))fail(pkg,`hosted asset ${asset.dest} collides with a native hosted output.`);assertHostedSourcePath(pkg,assertInsideProject(pkg,projectPath(asset.source)))}const canonicalPaths=[outputDir,entryPath].map((path)=>assertInsideProject(pkg,path));for(const owner of owners){if(canonicalPaths.some((path)=>path===owner.path||path.startsWith(`${owner.path}${sep}`)||owner.path.startsWith(`${path}${sep}`)))fail(pkg,`hosted output collides with package "${owner.name}".`)}const baseOwner=publicBases.get(config.base);if(baseOwner)fail(pkg,`hosted public base collides with package "${baseOwner}".`);publicBases.set(config.base,pkg.name);owners.push(...canonicalPaths.map((path)=>({path,name:pkg.name})));for(const source of config.styles??[]){const path=plainPath(pkg,source,"hosted.styles source");assertInsideProject(pkg,path);if(extname(path)!==".css")fail(pkg,`hosted stylesheet must name a literal .css file: ${source}`)}if(config.loader){assertInsideProject(pkg,plainPath(pkg,config.loader.source,"hosted.loader.source"));for(const[key,value]of Object.entries(config.loader.define??{})){if(!/^[A-Za-z_$][\w$]*$/.test(key)||typeof value!=="string")fail(pkg,"hosted loader defines must map identifiers to explicit build expressions.")}}inputs.set(pkg.name,{pkg,config,outputDir,entryPath,styles:[]})}return inputs}export async function materializeHostedInput(input){const{pkg,config}=input;publicPath(pkg,config.outputDir,"hosted.outputDir");publicPath(pkg,config.entry,"hosted.entry");const styles=[];for(const source of config.styles??[]){const path=plainPath(pkg,source,"hosted.styles source");assertInsideProject(pkg,path);if(!await Bun.file(path).exists())fail(pkg,`hosted stylesheet must name an existing literal .css file: ${source}`);styles.push(await Bun.file(path).text())}return{...input,styles,loader:config.loader?await loaderInput(pkg,config.loader):undefined}}export async function validateHostedBrowser(input){await browserBuild(input.pkg,resolve(input.pkg.dir,"dist/index.js"))}async function immutableWrite(pkg,path,bytes){await mkdir(dirname(path),{recursive:true});try{await writeFile(path,bytes,{flag:"wx"})}catch(error){if(error.code!=="EEXIST")throw error;if(!Buffer.from(await readFile(path)).equals(Buffer.from(bytes)))fail(pkg,`Immutable hosted artifact changed at ${path}.`)}}async function stableWrite(path,bytes){await mkdir(dirname(path),{recursive:true});const temporary=`${path}.${crypto.randomUUID()}.tmp`;try{await writeFile(temporary,bytes,{flag:"wx"});await rename(temporary,path)}finally{await unlink(temporary).catch(()=>{})}}export async function buildHostedOutput(input,version,componentStyles,packageAssets=[]){const{pkg,config,outputDir,entryPath,loader}=input;if(!/^[a-zA-Z0-9.+-]+$/.test(version)||version.startsWith("."))fail(pkg,"package version cannot be used in a hosted revision path.");const assets=new Map;const packagePaths=new Map;for(const file of["bundle.js","custom-elements.json"])assets.set(file,await Bun.file(resolve(pkg.dir,"dist",file)).bytes());if(await Bun.file(resolve(pkg.dir,"dist/bundle.css")).exists())assets.set("bundle.css",await Bun.file(resolve(pkg.dir,"dist/bundle.css")).bytes());for(const name of assets.keys())packagePaths.set(name,`dist/${name}`);const entryName=loader?basename(entryPath):"bundle.js";if(loader)assets.set(entryName,loader.bytes);for(const asset of packageAssets){const name=relative(resolve(pkg.dir,"dist"),asset.path).replaceAll("\\","/");plainPath(pkg,name,"hosted asset destination");if(name.split("/").some((part)=>part.startsWith(".")))fail(pkg,"hosted assets cannot contain hidden path segments.");assertInsideProject(pkg,asset.path);if(/\.[cm]?[jt]sx?$/.test(name))await browserBuild(pkg,asset.path);if(name==="manifest.json"||[...assets.keys()].some((existing)=>name===existing||name.startsWith(`${existing}/`)||existing.startsWith(`${name}/`)))fail(pkg,`hosted asset ${name} collides with a native hosted output.`);assets.set(name,await Bun.file(asset.path).bytes());packagePaths.set(name,asset.packagePath)}const styles=[...componentStyles,...input.styles];const csp={styleSrc:[...new Set(styles.map((style)=>`'sha256-${digest(style,"sha256","base64")}'`))].sort()};const identity=JSON.stringify({schemaVersion:1,name:pkg.name,version,csp,base:config.base,entry:config.entry,exports:loader?.exports,assets:[...assets].map(([name,bytes])=>[name,digest(bytes,"sha256","hex")])});const revision=`${version}-${digest(identity,"sha256","hex").slice(0,32)}`;const immutableDir=resolve(outputDir,revision);assertInsideProject(pkg,immutableDir);const immutableEntrypoint=`${config.base}/${revision}/${entryName}`;const origin=config.base.startsWith("https://")?new URL(config.base).origin:"";const entrypoint=`${origin}/${relative(projectPath("public"),entryPath).replaceAll("\\","/")}`;const forwarded=loader?.exports??["*"];const stable=new TextEncoder().encode(forwarded.map((name)=>name==="*"?`export * from ${JSON.stringify(immutableEntrypoint)};`:`export { ${name} } from ${JSON.stringify(immutableEntrypoint)};`).join(`
|
|
4
|
+
`)+`
|
|
5
|
+
`);const manifest={schemaVersion:1,name:pkg.name,version,revision,entrypoint,immutableEntrypoint,files:[...assets].map(([name,bytes])=>describe(`${config.base}/${revision}/${name}`,bytes)).concat(describe(entrypoint,stable)),csp};const manifestBytes=new TextEncoder().encode(`${JSON.stringify(manifest,null,2)}
|
|
6
|
+
`);const report={revision,outputDir,immutableDir,manifestPath:resolve(immutableDir,"manifest.json"),entryPath,manifest,files:[]};const retain=async(path,bytes,immutable,packagePath)=>{assertInsideProject(pkg,path);if(immutable)await immutableWrite(pkg,path,bytes);else await stableWrite(path,bytes);report.files.push({...describe(path,bytes),relativePath:relative(projectPath(),path).replaceAll("\\","/"),immutable,...packagePath?{packagePath}:{}})};for(const[name,bytes]of assets)await retain(resolve(immutableDir,name),bytes,true,packagePaths.get(name));await retain(report.manifestPath,manifestBytes,true);await retain(resolve(outputDir,"manifest.json"),manifestBytes,false);await retain(entryPath,stable,false);return report}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export{inspectInstalledLibraryPackage}from"../release-evidence.js";
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { LibraryConfig, LibraryPackageKind } from '@stacksjs/types';
|
|
1
|
+
import type { LibraryConfig, LibraryHostedOptions, LibraryPackageKind } from '@stacksjs/types/library';
|
|
2
2
|
/**
|
|
3
3
|
* The app's `config/library.ts`, once it has loaded. Read `library` from
|
|
4
4
|
* `@stacksjs/config` straight away and it is still the framework's sample
|
|
@@ -65,6 +65,7 @@ export declare interface LibraryPackageDefinition {
|
|
|
65
65
|
readme?: string
|
|
66
66
|
assets: Array<{ dest: string, source: string }>
|
|
67
67
|
prebuild?: string
|
|
68
|
+
hosted?: LibraryHostedOptions
|
|
68
69
|
runtime: 'standalone' | 'stx'
|
|
69
70
|
sourcemap: boolean
|
|
70
71
|
include: string[]
|
package/dist/library/packages.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{existsSync}from"node:fs";import{relative,resolve}from"node:path";import process from"node:process";import{awaitConfig,library}from"@stacksjs/config";import{componentsPath,functionsPath,libraryPackagePath}from"@stacksjs/path";export async function configuredLibrary(){await awaitConfig();return library}export class LibraryConfigError extends Error{name="LibraryConfigError"}const DEFAULT_FUNCTION_INCLUDE=["**/*.ts"];const DEFAULT_FUNCTION_EXCLUDE=["**/*.test.ts","**/*.spec.ts","**/*.d.ts","**/_*.ts"];const DEFAULT_COMPONENT_INCLUDE=["**/*.stx"];const DEFAULT_COMPONENT_EXCLUDE=[];export function libraryPackageSourceDir(kind){return kind==="functions"?functionsPath():componentsPath()}export function unscopedName(name){return name.split("/").at(-1)??name}function includeFromFilesEntry(entry){const[file,alias]=Array.isArray(entry)?entry:[entry,undefined];const stem=file.replace(/\.ts$/,"");const include=stem==="*"?"*.ts":`${stem}.ts`;return alias?{include,alias:[stem,alias]}:{include}}function toDefinition(option,fallback){const name=option.name?.trim();if(!name)throw new LibraryConfigError(`library.packages[${fallback.index}] has no name. Every package needs the name it is published under.`);if(name.startsWith(".")||name.includes("\\")||name.split("/").length>2)throw new LibraryConfigError(`library.packages[${fallback.index}] has an invalid npm name: "${name}".`);const kind=option.kind??"functions";if(kind!=="functions"&&kind!=="components"&&kind!=="web-components")throw new LibraryConfigError(`Package "${name}" has an unknown kind "${kind}". Expected 'functions', 'components' or 'web-components'.`);const slug=(option.dir??unscopedName(name)).replace(/^@/,"");if(!/^[\w.-]+$/.test(slug))throw new LibraryConfigError(`Package "${name}" resolves to the directory "${slug}", which is not a single safe path segment. Set \`dir\` explicitly.`);const aliases={};const includeFromFiles=[];for(const entry of option.files??[]){const{include,alias}=includeFromFilesEntry(entry);includeFromFiles.push(include);if(alias)aliases[alias[0]]=alias[1]}const names={};for(const tag of option.tags??[]){if(Array.isArray(tag.name))names[tag.name[0]]=tag.name[1]}const defaultInclude=kind==="functions"?DEFAULT_FUNCTION_INCLUDE:DEFAULT_COMPONENT_INCLUDE;const defaultExclude=kind==="functions"?DEFAULT_FUNCTION_EXCLUDE:DEFAULT_COMPONENT_EXCLUDE;const includeFromTags=option.tags?.length&&!option.include?.length?option.tags.map((tag)=>`${Array.isArray(tag.name)?tag.name[0]:tag.name}.stx`):[];const include=option.include?.length?option.include:includeFromFiles.length?includeFromFiles:includeFromTags.length?includeFromTags:defaultInclude;const assets=Object.entries(option.assets??{}).map(([dest,source])=>{const clean=dest.replace(/^\.\//,"").replace(/\/+$/,"");if(!clean||clean.startsWith("/")||clean.split("/").some((part)=>part===".."||part==="."||part===""))throw new LibraryConfigError(`Package "${name}" has an asset path "${dest}" that is not a plain path inside the package.`);if(/^index(\.d)?\.[cm]?[jt]s$/.test(clean)||clean==="bundle.js"||clean==="bundle.css")throw new LibraryConfigError(`Package "${name}" has an asset at "${dest}", which the build itself writes.`);if(!source||typeof source!=="string")throw new LibraryConfigError(`Package "${name}" has an asset "${dest}" with no source path.`);return{dest:clean,source}});return{name,kind,slug,description:option.description??`The ${name} library.`,keywords:option.keywords??[],license:option.license??fallback.license,version:option.version,private:option.private??false,access:option.access??"public",registry:option.registry??"pantry",readme:option.readme,assets,prebuild:option.prebuild,runtime:option.runtime??"standalone",sourcemap:option.shouldGenerateSourcemap??false,include,exclude:option.exclude??defaultExclude,prefix:option.prefix??unscopedName(name).replace(/[^a-z0-9]+/gi,"-").replace(/^-+|-+$/g,"").toLowerCase(),dependencies:option.dependencies??{},peerDependencies:option.peerDependencies??{},aliases,names}}export function normalizeLibraryPackages(config){const cfg=config??{};const options=[];if(cfg.packages?.length){options.push(...cfg.packages)}else{if(cfg.functions?.name)options.push({...cfg.functions,kind:"functions"});if(cfg.webComponents?.name)options.push({...cfg.webComponents,kind:"web-components"})}const definitions=options.map((option,index)=>toDefinition(option,{license:cfg.license,index}));const byName=new Map;const bySlug=new Map;for(const definition of definitions){if(byName.has(definition.name))throw new LibraryConfigError(`Two packages are both named "${definition.name}". npm names must be unique.`);byName.set(definition.name,1);const owner=bySlug.get(definition.slug);if(owner){throw new LibraryConfigError(`"${owner}" and "${definition.name}" both build in storage/framework/libs/packages/${definition.slug}. Give one of them a \`dir\`.`)}bySlug.set(definition.slug,definition.name)}return definitions}export async function resolveLibraryPackages(config,options={}){const definitions=normalizeLibraryPackages(config);const resolved=[];for(const definition of definitions){const sourceDir=libraryPackageSourceDir(definition.kind);const sources=await matchSources(sourceDir,definition.include,definition.exclude);if(!sources.length){if(options.onUnmatched==="skip")continue;throw new LibraryConfigError(`Package "${definition.name}" matched no files in ${relative(process.cwd(),sourceDir)} `+`(include: ${definition.include.join(", ")}${definition.exclude.length?`; exclude: ${definition.exclude.join(", ")}`:""}).`)}resolved.push({...definition,sourceDir,sources,dir:libraryPackagePath(definition.slug),entry:libraryPackagePath(definition.slug,"src/index.ts")})}return resolved}export async function matchSources(sourceDir,include,exclude){if(!existsSync(sourceDir))return[];const expand=async(patterns)=>{const files=new Set;for(const pattern of patterns){for await(const file of new Bun.Glob(pattern).scan({cwd:sourceDir,absolute:true,onlyFiles:true}))files.add(resolve(file))}return files};const included=await expand(include);const excluded=exclude.length?await expand(exclude):new Set;return[...included].filter((file)=>!excluded.has(file)).sort()}export function entrySpecifier(entry,source){const path=relative(resolve(entry,".."),source).replace(/\.ts$/,"");return path.startsWith(".")?path:`./${path}`}
|
|
1
|
+
import{existsSync,realpathSync}from"node:fs";import{relative,resolve,sep}from"node:path";import process from"node:process";import{awaitConfig,library}from"@stacksjs/config";import{componentsPath,functionsPath,libraryPackagePath}from"@stacksjs/path";export async function configuredLibrary(){await awaitConfig();return library}export class LibraryConfigError extends Error{name="LibraryConfigError"}const DEFAULT_FUNCTION_INCLUDE=["**/*.ts"];const DEFAULT_FUNCTION_EXCLUDE=["**/*.test.ts","**/*.spec.ts","**/*.d.ts","**/_*.ts"];const DEFAULT_COMPONENT_INCLUDE=["**/*.stx"];const DEFAULT_COMPONENT_EXCLUDE=[];export function libraryPackageSourceDir(kind){return kind==="functions"?functionsPath():componentsPath()}export function unscopedName(name){return name.split("/").at(-1)??name}function includeFromFilesEntry(entry){const[file,alias]=Array.isArray(entry)?entry:[entry,undefined];const stem=file.replace(/\.ts$/,"");const include=stem==="*"?"*.ts":`${stem}.ts`;return alias?{include,alias:[stem,alias]}:{include}}function toDefinition(option,fallback){const name=option.name?.trim();if(!name)throw new LibraryConfigError(`library.packages[${fallback.index}] has no name. Every package needs the name it is published under.`);if(name.startsWith(".")||name.includes("\\")||name.split("/").length>2)throw new LibraryConfigError(`library.packages[${fallback.index}] has an invalid npm name: "${name}".`);const kind=option.kind??"functions";if(kind!=="functions"&&kind!=="components"&&kind!=="web-components")throw new LibraryConfigError(`Package "${name}" has an unknown kind "${kind}". Expected 'functions', 'components' or 'web-components'.`);const slug=(option.dir??unscopedName(name)).replace(/^@/,"");if(!/^[\w.-]+$/.test(slug)||slug==="."||slug==="..")throw new LibraryConfigError(`Package "${name}" resolves to the directory "${slug}", which is not a single safe path segment. Set \`dir\` explicitly.`);const aliases={};const includeFromFiles=[];for(const entry of option.files??[]){const{include,alias}=includeFromFilesEntry(entry);includeFromFiles.push(include);if(alias)aliases[alias[0]]=alias[1]}const names={};for(const tag of option.tags??[]){if(Array.isArray(tag.name))names[tag.name[0]]=tag.name[1]}const defaultInclude=kind==="functions"?DEFAULT_FUNCTION_INCLUDE:DEFAULT_COMPONENT_INCLUDE;const defaultExclude=kind==="functions"?DEFAULT_FUNCTION_EXCLUDE:DEFAULT_COMPONENT_EXCLUDE;const includeFromTags=option.tags?.length&&!option.include?.length?option.tags.map((tag)=>`${Array.isArray(tag.name)?tag.name[0]:tag.name}.stx`):[];const include=option.include?.length?option.include:includeFromFiles.length?includeFromFiles:includeFromTags.length?includeFromTags:defaultInclude;const assets=Object.entries(option.assets??{}).map(([dest,source])=>{const clean=dest.replace(/^\.\//,"").replace(/\/+$/,"");if(!clean||clean.startsWith("/")||clean.split("/").some((part)=>part===".."||part==="."||part===""))throw new LibraryConfigError(`Package "${name}" has an asset path "${dest}" that is not a plain path inside the package.`);if(/^index(\.d)?\.[cm]?[jt]s$/.test(clean)||clean==="bundle.js"||clean==="bundle.css")throw new LibraryConfigError(`Package "${name}" has an asset at "${dest}", which the build itself writes.`);if(!source||typeof source!=="string")throw new LibraryConfigError(`Package "${name}" has an asset "${dest}" with no source path.`);return{dest:clean,source}});return{name,kind,slug,description:option.description??`The ${name} library.`,keywords:option.keywords??[],license:option.license??fallback.license,version:option.version,private:option.private??false,access:option.access??"public",registry:option.registry??"pantry",readme:option.readme,assets,prebuild:option.prebuild,hosted:option.hosted,runtime:option.runtime??"standalone",sourcemap:option.shouldGenerateSourcemap??false,include,exclude:option.exclude??defaultExclude,prefix:option.prefix??unscopedName(name).replace(/[^a-z0-9]+/gi,"-").replace(/^-+|-+$/g,"").toLowerCase(),dependencies:option.dependencies??{},peerDependencies:option.peerDependencies??{},aliases,names}}export function normalizeLibraryPackages(config){const cfg=config??{};const options=[];if(cfg.packages?.length){options.push(...cfg.packages)}else{if(cfg.functions?.name)options.push({...cfg.functions,kind:"functions"});if(cfg.webComponents?.name)options.push({...cfg.webComponents,kind:"web-components"})}const definitions=options.map((option,index)=>toDefinition(option,{license:cfg.license,index}));const byName=new Map;const bySlug=new Map;for(const definition of definitions){if(byName.has(definition.name))throw new LibraryConfigError(`Two packages are both named "${definition.name}". npm names must be unique.`);byName.set(definition.name,1);const owner=bySlug.get(definition.slug);if(owner){throw new LibraryConfigError(`"${owner}" and "${definition.name}" both build in storage/framework/libs/packages/${definition.slug}. Give one of them a \`dir\`.`)}bySlug.set(definition.slug,definition.name)}return definitions}export async function resolveLibraryPackages(config,options={}){const definitions=normalizeLibraryPackages(config);const resolved=[];for(const definition of definitions){const sourceDir=libraryPackageSourceDir(definition.kind);const sources=await matchSources(sourceDir,definition.include,definition.exclude);if(!sources.length){if(options.onUnmatched==="skip")continue;throw new LibraryConfigError(`Package "${definition.name}" matched no files in ${relative(process.cwd(),sourceDir)} `+`(include: ${definition.include.join(", ")}${definition.exclude.length?`; exclude: ${definition.exclude.join(", ")}`:""}).`)}resolved.push({...definition,sourceDir,sources,dir:libraryPackagePath(definition.slug),entry:libraryPackagePath(definition.slug,"src/index.ts")})}return resolved}export async function matchSources(sourceDir,include,exclude){for(const pattern of[...include,...exclude]){if(!pattern||pattern.startsWith("/")||pattern.includes("\\")||pattern.includes("\x00")||pattern.includes(":")||pattern.split("/").includes(".."))throw new LibraryConfigError(`Library source glob "${pattern}" must stay inside its source directory.`)}if(!existsSync(sourceDir))return[];const expand=async(patterns)=>{const files=new Set;for(const pattern of patterns){for await(const file of new Bun.Glob(pattern).scan({cwd:sourceDir,absolute:true,onlyFiles:true})){const canonical=realpathSync(file);if(!canonical.startsWith(`${realpathSync(sourceDir)}${sep}`))throw new LibraryConfigError(`Library source glob "${pattern}" reaches outside its source directory through a symbolic link.`);files.add(resolve(file))}}return files};const included=await expand(include);const excluded=exclude.length?await expand(exclude):new Set;return[...included].filter((file)=>!excluded.has(file)).sort()}export function entrySpecifier(entry,source){const path=relative(resolve(entry,".."),source).replace(/\.ts$/,"");return path.startsWith(".")?path:`./${path}`}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { LibraryConfig } from '@stacksjs/types';
|
|
1
|
+
import type { LibraryConfig } from '@stacksjs/types/library';
|
|
2
2
|
/**
|
|
3
3
|
* Work out what a publish would push, and refuse anything that would ship an
|
|
4
4
|
* empty or stale tarball.
|
|
@@ -7,7 +7,7 @@ import type { LibraryConfig } from '@stacksjs/types';
|
|
|
7
7
|
* because the publish step ran with `--ignore-scripts` and nothing checked
|
|
8
8
|
* that a build had happened first. This is that check, ahead of time.
|
|
9
9
|
*/
|
|
10
|
-
export declare function planLibraryPublish(config?: LibraryConfig): Promise<LibraryPublishPlan[]>;
|
|
10
|
+
export declare function planLibraryPublish(config?: LibraryConfig, options?: { allowEmpty?: boolean }): Promise<LibraryPublishPlan[]>;
|
|
11
11
|
/**
|
|
12
12
|
* The publisher to shell out to for a package bound for `registry`.
|
|
13
13
|
*
|
package/dist/library/publish.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{existsSync}from"node:fs";import{relative,resolve}from"node:path";import{log}from"@stacksjs/logging";import{projectPath}from"@stacksjs/path";import{configuredLibrary,LibraryConfigError,resolveLibraryPackages}from"./packages";export async function planLibraryPublish(config){const packages=await resolveLibraryPackages(config??await configuredLibrary());const publishable=packages.filter((pkg)=>!pkg.private);if(!publishable.length)throw new LibraryConfigError("Every configured library package is marked private. Nothing to publish.");const plans=[];const missing=[];for(const pkg of publishable){const manifestPath=resolve(pkg.dir,"package.json");const distDir=resolve(pkg.dir,"dist");if(!existsSync(manifestPath)||!existsSync(distDir)){missing.push(pkg.name);continue}const built=await Array.fromAsync(new Bun.Glob("**/*.{js,css,json,d.ts}").scan({cwd:distDir}));if(!built.length){missing.push(pkg.name);continue}const manifest=await Bun.file(manifestPath).json();plans.push({name:pkg.name,version:manifest.version??"0.0.0",dir:pkg.dir,access:pkg.access,registry:pkg.registry,tarballFiles:built.length})}if(missing.length){throw new LibraryConfigError(`These packages have no build output: ${missing.join(", ")}.
|
|
1
|
+
import{existsSync}from"node:fs";import{relative,resolve}from"node:path";import{log}from"@stacksjs/logging";import{projectPath}from"@stacksjs/path";import{configuredLibrary,LibraryConfigError,resolveLibraryPackages}from"./packages";export async function planLibraryPublish(config,options={}){const packages=await resolveLibraryPackages(config??await configuredLibrary(),{onUnmatched:options.allowEmpty?"skip":"error"});const publishable=packages.filter((pkg)=>!pkg.private);if(!publishable.length&&options.allowEmpty)return[];if(!publishable.length)throw new LibraryConfigError("Every configured library package is marked private. Nothing to publish.");const plans=[];const missing=[];for(const pkg of publishable){const manifestPath=resolve(pkg.dir,"package.json");const distDir=resolve(pkg.dir,"dist");if(!existsSync(manifestPath)||!existsSync(distDir)){missing.push(pkg.name);continue}const built=await Array.fromAsync(new Bun.Glob("**/*.{js,css,json,d.ts}").scan({cwd:distDir}));if(!built.length){missing.push(pkg.name);continue}const manifest=await Bun.file(manifestPath).json();plans.push({name:pkg.name,version:manifest.version??"0.0.0",dir:pkg.dir,access:pkg.access,registry:pkg.registry,tarballFiles:built.length})}if(missing.length){throw new LibraryConfigError(`These packages have no build output: ${missing.join(", ")}.
|
|
2
2
|
Run \`buddy build:libs\` before publishing.`)}return plans}export function publishCommand(registry="pantry",which=Bun.which,tag,publisher){if(tag!==undefined&&(!/^[a-z][a-z0-9_-]{0,63}$/.test(tag)||/^v\d/.test(tag)))throw new LibraryConfigError("Use a lower-case distribution tag such as alpha, next or latest, not a version or range.");const tagged=(command)=>tag===undefined?command:[...command,"--tag",tag];if(publisher!==undefined){if(!["pantry","bun","npm"].includes(publisher))throw new LibraryConfigError("Choose publisher pantry, bun or npm.");if(registry==="pantry"&&publisher!=="pantry")throw new LibraryConfigError("Only pantry can publish to the Pantry registry.");if(!which(publisher))throw new LibraryConfigError(`Requested publisher ${publisher} is not available on PATH.`);return tagged(publisher==="pantry"&®istry==="npm"?["pantry","publish","--npm"]:[publisher,"publish"])}if(which("pantry"))return tagged(registry==="npm"?["pantry","publish","--npm"]:["pantry","publish"]);if(registry==="pantry")throw new Error("This package publishes to the Pantry registry, which only `pantry publish` reaches, and `pantry` is not on PATH. Install pantry, or set `registry: 'npm'` on the package in config/library.ts.");if(which("bun"))return tagged(["bun","publish"]);if(which("npm"))return tagged(["npm","publish"]);throw new Error("None of `pantry`, `bun` or `npm` is on PATH, so there is nothing to publish with.")}export async function publishLibraryPackages(options={}){if(options.tag!==undefined||options.publisher!==undefined)publishCommand("npm",(bin)=>bin,options.tag,options.publisher);const plans=await planLibraryPublish(options.config);for(const plan of plans){const args=[...publishCommand(plan.registry,Bun.which,options.tag,options.publisher),"--access",plan.access];if(options.dryRun)args.push("--dry-run");log.info(`${options.dryRun?"[dry run] ":""}${args.join(" ")} (${plan.name}@${plan.version}, ${relative(projectPath(),plan.dir)})`);const proc=Bun.spawnSync({cmd:args,cwd:plan.dir,stdout:"inherit",stderr:"inherit"});if(proc.exitCode!==0)throw new Error(`Publishing ${plan.name}@${plan.version} failed with exit code ${proc.exitCode}.`)}return plans}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import type { LibraryConfig } from '@stacksjs/types/library';
|
|
2
|
+
import type { LibraryPublishPlan } from './publish';
|
|
3
|
+
import type { ProjectScriptReceipt } from '../project-scripts';
|
|
4
|
+
/** Keep hosted aliases and immutable files, binding compiler assets to package bytes. */
|
|
5
|
+
export declare function hostedLibraryRetentionInput(report: LibraryHostedRetentionReport): RetainedHostedInput;
|
|
6
|
+
export declare function librarySha256(bytes: Uint8Array): string;
|
|
7
|
+
export declare function libraryIntegrity(bytes: Uint8Array): string;
|
|
8
|
+
export declare function validateLibraryReleaseSource(source: LibraryReleaseSource): void;
|
|
9
|
+
/** Validate retained data before interpreting it as paths, publisher input or URLs. */
|
|
10
|
+
export declare function validateLibraryReleaseCandidate(value: unknown): asserts value is LibraryReleaseCandidate;
|
|
11
|
+
/** Inspect current data only. Never builds or executes code from retained source. */
|
|
12
|
+
export declare function inspectLibraryRelease(directory: string, source?: LibraryReleaseSource, options?: LibraryReleaseLimits): Promise<LibraryReleaseCandidate>;
|
|
13
|
+
/** Pack exactly once, retain the original archive, and commit the record last. */
|
|
14
|
+
export declare function retainLibraryRelease(options: RetainLibraryReleaseOptions): Promise<LibraryReleaseCandidate>;
|
|
15
|
+
/** Materialize validated bytes for inspection/consumer checks, never for publication. */
|
|
16
|
+
export declare function materializeLibraryRelease(directory: string, destination: string, options?: LibraryReleaseLimits): Promise<LibraryReleaseCandidate>;
|
|
17
|
+
/** Native builder/resolver remain the source of package names, versions and outputs. */
|
|
18
|
+
export declare function prepareLibraryRelease(options: Omit<RetainLibraryReleaseOptions, 'packages' | 'checks'> & { config?: LibraryConfig, checks?: string[], cwd: string }): Promise<LibraryReleaseCandidate | undefined>;
|
|
19
|
+
export declare interface LibraryReleaseSource { repository: string, commit: string, tag?: string }
|
|
20
|
+
export declare interface LibraryFileIdentity { sha256: string, bytes: number }
|
|
21
|
+
export declare interface LibraryReleasePackage {
|
|
22
|
+
name: string
|
|
23
|
+
version: string
|
|
24
|
+
registry: 'npm' | 'pantry'
|
|
25
|
+
access: 'public' | 'restricted'
|
|
26
|
+
tag: string
|
|
27
|
+
archive: LibraryFileIdentity & { path: string, integrity: string, files: Record<string, LibraryFileIdentity> }
|
|
28
|
+
hosted?: { manifest: string, files: Record<string, LibraryFileIdentity & { packagePath?: string }> }
|
|
29
|
+
}
|
|
30
|
+
export declare interface LibraryReleaseCandidate {
|
|
31
|
+
schemaVersion: 1
|
|
32
|
+
source: LibraryReleaseSource
|
|
33
|
+
toolchain: { bun: string, packer: 'bun', packerVersion: string, compiler?: { name: '@stacksjs/stx', version: string } }
|
|
34
|
+
packages: LibraryReleasePackage[]
|
|
35
|
+
checks: ProjectScriptReceipt['checks']
|
|
36
|
+
}
|
|
37
|
+
export declare interface RetainedHostedInput {
|
|
38
|
+
manifest: string
|
|
39
|
+
files: Array<{ path: string, source: string, packagePath?: string, sha256?: string, bytes?: number }>
|
|
40
|
+
}
|
|
41
|
+
/** Structural subset of the native hosted builder report, independent of compiler imports. */
|
|
42
|
+
export declare interface LibraryHostedRetentionReport {
|
|
43
|
+
immutableDir: string
|
|
44
|
+
manifestPath: string
|
|
45
|
+
files: Array<{ path: string, relativePath: string, sha256: string, bytes: number, packagePath?: string }>
|
|
46
|
+
}
|
|
47
|
+
export declare interface LibraryReleaseLimits { maxArchiveBytes?: number, maxExpandedBytes?: number }
|
|
48
|
+
export declare interface RetainLibraryReleaseOptions extends LibraryReleaseLimits {
|
|
49
|
+
directory: string
|
|
50
|
+
source: LibraryReleaseSource
|
|
51
|
+
tag: string
|
|
52
|
+
packages: Array<LibraryPublishPlan & { archivePath?: string, hosted?: RetainedHostedInput }>
|
|
53
|
+
checks?: ProjectScriptReceipt['checks']
|
|
54
|
+
compiler?: LibraryReleaseCandidate['toolchain']['compiler']
|
|
55
|
+
}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import{createHash}from"node:crypto";import{lstat,mkdir,readFile,writeFile}from"node:fs/promises";import{basename,dirname,join,resolve}from"node:path";import{libraryArchiveEntries,safeArchivePath}from"./archive";export function hostedLibraryRetentionInput(report){const manifest=report.files.find((file)=>resolve(file.path)===resolve(report.manifestPath));if(!manifest)fail("Hosted build report is missing its immutable manifest");return{manifest:manifest.relativePath,files:report.files.map((file)=>({path:file.relativePath,source:file.path,sha256:file.sha256,bytes:file.bytes,...file.packagePath?{packagePath:file.packagePath}:["bundle.js","bundle.css","custom-elements.json"].includes(basename(file.path))&&resolve(file.path)===resolve(report.immutableDir,basename(file.path))?{packagePath:`dist/${basename(file.path)}`}:{}}))}}export function librarySha256(bytes){return createHash("sha256").update(bytes).digest("hex")}export function libraryIntegrity(bytes){return`sha512-${createHash("sha512").update(bytes).digest("base64")}`}function identity(bytes){return{sha256:librarySha256(bytes),bytes:bytes.length}}function fail(message){throw new Error(`Library release blocked: ${message}`)}const namePattern=/^(?:@[a-z0-9][\w.-]*\/)?[a-z0-9][\w.-]*$/;const versionPattern=/^\d+\.\d+\.\d+(?:-[\w.-]+)?(?:\+[\w.-]+)?$/;const tagPattern=/^[a-z][a-z0-9_-]{0,63}$/;export function validateLibraryReleaseSource(source){if(!source||!/^[\w-][\w.-]*\/[\w-][\w.-]*$/.test(source.repository)||!/^[a-f0-9]{40}$/.test(source.commit)||source.tag!==undefined&&(typeof source.tag!=="string"||!source.tag||source.tag.length>255))fail("Expected a source repository and full commit SHA")}function fileIdentity(value){return!!value&&/^[a-f0-9]{64}$/.test(value.sha256)&&Number.isSafeInteger(value.bytes)&&value.bytes>=0}export function validateLibraryReleaseCandidate(value){const record=value;if(!record||record.schemaVersion!==1||!Array.isArray(record.packages)||!record.packages.length||record.packages.length>1000||!Array.isArray(record.checks)||!record.toolchain||record.toolchain.packer!=="bun"||!versionPattern.test(record.toolchain.bun)||!versionPattern.test(record.toolchain.packerVersion))fail("Expected a version 1 retained library candidate");if(record.toolchain.compiler&&(record.toolchain.compiler.name!=="@stacksjs/stx"||!versionPattern.test(record.toolchain.compiler.version)))fail("Invalid retained compiler identity");validateLibraryReleaseSource(record.source);const names=new Set;for(const pkg of record.packages){if(!pkg||typeof pkg.name!=="string"||!namePattern.test(pkg.name)||names.has(pkg.name)||!versionPattern.test(pkg.version)||!["npm","pantry"].includes(pkg.registry)||!["public","restricted"].includes(pkg.access)||!tagPattern.test(pkg.tag)||!pkg.archive||!safeArchivePath(pkg.archive.path)||!fileIdentity(pkg.archive)||!/^sha512-[A-Za-z0-9+/]{86}==$/.test(pkg.archive.integrity)||!pkg.archive.files||typeof pkg.archive.files!=="object"||Array.isArray(pkg.archive.files)||!Object.hasOwn(pkg.archive.files,"package.json")||Object.keys(pkg.archive.files).length>1e5)fail("Invalid package identity in retained candidate");names.add(pkg.name);for(const[path,file]of Object.entries(pkg.archive.files)){if(!safeArchivePath(path)||!fileIdentity(file))fail("Invalid retained package file identity")}if(pkg.hosted){if(!safeArchivePath(pkg.hosted.manifest)||!pkg.hosted.files||typeof pkg.hosted.files!=="object"||Array.isArray(pkg.hosted.files)||!Object.hasOwn(pkg.hosted.files,pkg.hosted.manifest)||Object.keys(pkg.hosted.files).length>1e5)fail("Invalid retained hosted manifest");for(const[path,file]of Object.entries(pkg.hosted.files)){if(!safeArchivePath(path)||!fileIdentity(file)||file.packagePath!==undefined&&!safeArchivePath(file.packagePath))fail("Invalid retained hosted file identity")}}}for(const check of record.checks){if(!check||!/^[a-z0-9][\w:.-]*$/i.test(check.script)||check.exitCode!==0||!/^[a-f0-9]{64}$/.test(check.outputSha256))fail("Invalid retained check receipt")}}async function retainedBytes(root,path,limit){if(!safeArchivePath(path))fail("Unsafe retained file path");let current=resolve(root);if(!(await lstat(current)).isDirectory())fail("Retained candidate root is not a directory");const parts=path.split("/");for(let i=0;i<parts.length;i++){current=join(current,parts[i]);const stat=await lstat(current);if(i<parts.length-1?!stat.isDirectory():!stat.isFile()||stat.size>limit)fail("Retained path is not a bounded regular file")}const bytes=new Uint8Array(await readFile(current));if(bytes.length>limit)fail("Retained file exceeds byte budget");return bytes}function limits(options){const archive=options.maxArchiveBytes??64*1024*1024;const expanded=options.maxExpandedBytes??256*1024*1024;if(![archive,expanded].every((value)=>Number.isSafeInteger(value)&&value>0))fail("Invalid archive byte budgets");return{archive,expanded}}class InvalidRelativeExportTarget extends Error{}function validateArchiveExports(manifest,files){function target(value,relativeExport=false){if(value===null)return;if(Array.isArray(value)){let valid=value.length===0;let invalid;for(const item of value){try{target(item,relativeExport);valid=true}catch(error){if(!(error instanceof InvalidRelativeExportTarget))throw error;invalid=error}}if(!valid&&invalid)throw invalid;return}if(value&&typeof value==="object"){Object.values(value).forEach((item)=>target(item,relativeExport));return}if(typeof value!=="string")fail("Archive declares an invalid export target");if(relativeExport&&!value.startsWith("./"))throw new InvalidRelativeExportTarget("Library release blocked: Archive exports require a relative ./ target");const path=value.replace(/^\.\//,"");if(!safeArchivePath(path))fail("Archive export escapes its package");if(path.includes("*")){const pattern=new RegExp(`^${path.split("*").map((part)=>part.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")).join(".*")}$`);if(![...files.keys()].some((file)=>pattern.test(file)))fail(`Archive export ${value} has no files`)}else if(!files.has(path))fail(`Archive export ${value} is missing`)}if(manifest.exports!==undefined)target(manifest.exports,true);for(const key of["main","module","types","typings"])if(manifest[key]!==undefined)target(manifest[key])}function sameIdentity(bytes,expected){return bytes.length===expected.bytes&&librarySha256(bytes)===expected.sha256}export async function inspectLibraryRelease(directory,source,options={}){const budget=limits(options);const record=JSON.parse(new TextDecoder().decode(await retainedBytes(directory,"candidate.json",16*1024*1024)));validateLibraryReleaseCandidate(record);if(source){validateLibraryReleaseSource(source);if(record.source.repository!==source.repository||record.source.commit!==source.commit||source.tag!==undefined&&record.source.tag!==source.tag)fail("Retained source identity differs from the requested source")}for(let i=0;i<record.packages.length;i++){const pkg=record.packages[i];const bytes=await retainedBytes(directory,pkg.archive.path,budget.archive);if(!sameIdentity(bytes,pkg.archive)||libraryIntegrity(bytes)!==pkg.archive.integrity)fail("Retained archive identity differs");const files=await libraryArchiveEntries(bytes,budget.expanded);if(files.size!==Object.keys(pkg.archive.files).length)fail("Retained archive file identity differs");for(const[path,bytes]of files){if(!pkg.archive.files[path]||!sameIdentity(bytes,pkg.archive.files[path]))fail("Retained archive file identity differs")}const manifest=JSON.parse(new TextDecoder().decode(files.get("package.json")));if(manifest.name!==pkg.name||manifest.version!==pkg.version||manifest.private)fail("Retained archive package identity differs");validateArchiveExports(manifest,files);if(pkg.hosted){if(Object.values(pkg.hosted.files).reduce((sum,file)=>sum+file.bytes,0)>budget.expanded)fail("Retained hosted files exceed byte budget");for(const[path,expected]of Object.entries(pkg.hosted.files)){const hosted=await retainedBytes(directory,`hosted/${i}/${path}`,budget.expanded);if(!sameIdentity(hosted,expected)||expected.packagePath&&(!files.has(expected.packagePath)||!sameIdentity(files.get(expected.packagePath),expected)))fail("Retained hosted identity differs from retained package bytes")}JSON.parse(new TextDecoder().decode(await retainedBytes(directory,`hosted/${i}/${pkg.hosted.manifest}`,budget.expanded)))}}return record}export async function retainLibraryRelease(options){validateLibraryReleaseSource(options.source);if(!tagPattern.test(options.tag)||!options.packages.length)fail("Retaining packages requires an explicit distribution tag");const budget=limits(options);await mkdir(dirname(resolve(options.directory)),{recursive:true});await mkdir(options.directory,{recursive:false});const bun=Bun.which("bun");if(!bun)fail("Bun packer is unavailable");const probe=Bun.spawnSync([bun,"--version"],{stdout:"pipe",stderr:"pipe",timeout:1e4});if(probe.exitCode!==0)fail("Bun packer version probe failed");const version=probe.stdout.toString().trim();const record={schemaVersion:1,source:{...options.source},toolchain:{bun:version,packer:"bun",packerVersion:version,...options.compiler?{compiler:options.compiler}:{}},packages:[],checks:options.checks??[]};await mkdir(join(options.directory,"archives"));for(let i=0;i<options.packages.length;i++){const plan=options.packages[i];const path=`archives/${i}.tgz`;if(plan.archivePath){const bytes=await retainedBytes(dirname(resolve(plan.archivePath)),basename(plan.archivePath),budget.archive);await writeFile(join(options.directory,path),bytes,{flag:"wx"})}else{const packed=Bun.spawnSync([bun,"pm","pack","--ignore-scripts","--filename",resolve(options.directory,path)],{cwd:plan.dir,stdout:"pipe",stderr:"pipe",timeout:60000});if(packed.exitCode!==0)fail(`Packing ${plan.name} failed: ${packed.stderr.toString().slice(0,1000)}`)}const bytes=await retainedBytes(options.directory,path,budget.archive);const entries=await libraryArchiveEntries(bytes,budget.expanded);const pkg={name:plan.name,version:plan.version,registry:plan.registry,access:plan.access,tag:options.tag,archive:{path,...identity(bytes),integrity:libraryIntegrity(bytes),files:Object.fromEntries([...entries].map(([name,bytes])=>[name,identity(bytes)]))}};if(plan.hosted){pkg.hosted={manifest:plan.hosted.manifest,files:Object.create(null)};for(const file of plan.hosted.files){if(!safeArchivePath(file.path)||Object.hasOwn(pkg.hosted.files,file.path))fail("Unsafe or duplicate hosted file path");const bytes=await retainedBytes(dirname(resolve(file.source)),basename(file.source),budget.expanded);if(file.sha256!==undefined&&librarySha256(bytes)!==file.sha256||file.bytes!==undefined&&bytes.length!==file.bytes)fail("Hosted output changed after the native build report");const destination=join(options.directory,`hosted/${i}`,file.path);await mkdir(dirname(destination),{recursive:true});await writeFile(destination,bytes,{flag:"wx"});pkg.hosted.files[file.path]={...identity(bytes),...file.packagePath?{packagePath:file.packagePath}:{}}}}record.packages.push(pkg)}validateLibraryReleaseCandidate(record);await writeFile(join(options.directory,"candidate.json"),`${JSON.stringify(record,null,2)}
|
|
2
|
+
`,{flag:"wx"});return inspectLibraryRelease(options.directory,options.source,options)}export async function materializeLibraryRelease(directory,destination,options={}){const record=await inspectLibraryRelease(directory,undefined,options);await mkdir(destination,{recursive:false});for(let i=0;i<record.packages.length;i++){const archive=await retainedBytes(directory,record.packages[i].archive.path,limits(options).archive);for(const[path,bytes]of await libraryArchiveEntries(archive,limits(options).expanded)){const target=join(destination,String(i),path);await mkdir(dirname(target),{recursive:true});await writeFile(target,bytes,{flag:"wx"})}for(const[path,expected]of Object.entries(record.packages[i].hosted?.files??{})){const bytes=await retainedBytes(directory,`hosted/${i}/${path}`,limits(options).expanded);if(!sameIdentity(bytes,expected))fail("Retained hosted identity changed during materialization");const target=join(destination,"hosted",String(i),path);await mkdir(dirname(target),{recursive:true});await writeFile(target,bytes,{flag:"wx"})}}return record}export async function prepareLibraryRelease(options){const{configuredLibrary}=await import("./packages");const config=options.config??await configuredLibrary();if(config?.releaseable===false)return;const{buildLibraryPackages}=await import("./build");const reports=await buildLibraryPackages({config,allowEmpty:true});if(!reports.length)return;const{planLibraryPublish}=await import("./publish");const plans=await planLibraryPublish(config,{allowEmpty:true});if(!plans.length)return;const compiler=reports.some((report)=>report.kind!=="functions")?{name:"@stacksjs/stx",version:(await Bun.file(Bun.resolveSync("@stacksjs/stx/package.json",import.meta.dir)).json()).version}:undefined;const{runProjectScripts}=await import("../project-scripts");const receipt=options.checks?.length?await runProjectScripts({cwd:options.cwd,scripts:options.checks}):undefined;return retainLibraryRelease({...options,compiler,packages:plans.map((plan)=>{const report=reports.find((report)=>report.name===plan.name);return{...plan,...report?.hosted?{hosted:hostedLibraryRetentionInput(report.hosted)}:{}}}),checks:receipt?.checks})}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { LibraryNetworkOptions } from './release-network';
|
|
2
|
+
import type { LibrarySourceReceipt } from './release-source';
|
|
3
|
+
/** Decode only bounded ordinary files, never extracting provider paths or links. */
|
|
4
|
+
export declare function libraryArtifactZipFiles(data: Uint8Array): Map<string, Uint8Array>;
|
|
5
|
+
/** Download only the accepted artifact ID, checking its actual provider digest. */
|
|
6
|
+
export declare function downloadLibraryReleaseSource(receipt: LibrarySourceReceipt, directory: string, options?: LibraryNetworkOptions & { token?: string }): Promise<void>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{mkdir,writeFile}from"node:fs/promises";import{dirname,join}from"node:path";import{inflateRawSync}from"node:zlib";import{safeArchivePath}from"./archive";import{librarySha256,validateLibraryReleaseSource}from"./release-artifacts";import{createLibraryReleaseReader}from"./release-network";export function libraryArtifactZipFiles(data){const bytes=Buffer.from(data);let end=bytes.length-22;while(end>=Math.max(0,bytes.length-65557)&&bytes.readUInt32LE(end)!==101010256)end--;if(end<0||end+22>bytes.length||bytes.readUInt32LE(end)!==101010256||end+22+bytes.readUInt16LE(end+20)!==bytes.length||bytes.readUInt16LE(end+4)||bytes.readUInt16LE(end+6))throw new Error("Invalid provider artifact ZIP");const count=bytes.readUInt16LE(end+10);let offset=bytes.readUInt32LE(end+16);if(!count||count>1e4||bytes.readUInt16LE(end+8)!==count||offset+bytes.readUInt32LE(end+12)!==end)throw new Error("Provider artifact ZIP exceeds bounds");const files=new Map;let expanded=0;for(let i=0;i<count;i++){if(offset+46>end||bytes.readUInt32LE(offset)!==33639248)throw new Error("Invalid provider artifact ZIP directory");const flags=bytes.readUInt16LE(offset+8);const method=bytes.readUInt16LE(offset+10);const compressed=bytes.readUInt32LE(offset+20);const size=bytes.readUInt32LE(offset+24);const nameLength=bytes.readUInt16LE(offset+28);const next=offset+46+nameLength+bytes.readUInt16LE(offset+30)+bytes.readUInt16LE(offset+32);const mode=bytes.readUInt32LE(offset+38)>>>16;const local=bytes.readUInt32LE(offset+42);if(next>end||flags&1||![0,8].includes(method)||![0,32768,16384].includes(mode&61440)||size>64*1024*1024||(expanded+=size)>512*1024*1024)throw new Error("Unsupported or oversized provider artifact ZIP entry");const name=new TextDecoder("utf-8",{fatal:true}).decode(bytes.subarray(offset+46,offset+46+nameLength));offset=next;const path=name.endsWith("/")?name.slice(0,-1):name;if(!safeArchivePath(path)||files.has(path))throw new Error("Unsafe or duplicate provider artifact path");if(name.endsWith("/"))continue;if(local+30>offset||bytes.readUInt32LE(local)!==67324752)throw new Error("Invalid provider artifact ZIP file");const start=local+30+bytes.readUInt16LE(local+26)+bytes.readUInt16LE(local+28);if(start+compressed>bytes.readUInt32LE(end+16))throw new Error("Provider artifact ZIP file exceeds bounds");const raw=bytes.subarray(start,start+compressed);const content=method===0?raw:inflateRawSync(raw,{maxOutputLength:Math.max(1,size)});if(content.length!==size)throw new Error("Provider artifact ZIP expanded size differs");files.set(path,content)}if(offset!==end)throw new Error("Provider artifact ZIP directory length differs");for(const path of files.keys()){const parts=path.split("/");for(let index=1;index<parts.length;index++)if(files.has(parts.slice(0,index).join("/")))throw new Error("Conflicting provider artifact paths")}return files}export async function downloadLibraryReleaseSource(receipt,directory,options={}){validateLibraryReleaseSource(receipt.source);const artifact=receipt.artifact;if(receipt.schemaVersion!==1||!Number.isSafeInteger(artifact?.id)||artifact.id<=0||!Number.isSafeInteger(artifact.bytes)||artifact.bytes<=0||artifact.bytes>256*1024*1024||!/^sha256:[a-f0-9]{64}$/.test(artifact.digest??"")||artifact.downloadUrl!==`https://api.github.com/repos/${receipt.source.repository}/actions/artifacts/${artifact.id}/zip`)throw new Error("Expected a bounded accepted artifact ID, SHA-256 digest and owned GitHub download URL");const reader=createLibraryReleaseReader(options);const request=options.fetch??fetch;const controller=new AbortController;const timeout=setTimeout(()=>controller.abort(),Math.min(options.requestTimeoutMs??15000,reader.remaining()));let location;try{const response=await request(receipt.artifact.downloadUrl,{redirect:"manual",signal:controller.signal,headers:options.token?{Authorization:`Bearer ${options.token}`,Accept:"application/vnd.github+json"}:{Accept:"application/vnd.github+json"}});await response.body?.cancel();const url=new URL(response.headers.get("location")??"");if(response.status!==302||url.protocol!=="https:"||url.username||url.password||!(url.hostname.endsWith(".blob.core.windows.net")||url.hostname.endsWith(".actions.githubusercontent.com")))throw new Error("Provider artifact did not return an approved signed download URL");location=url.href}finally{clearTimeout(timeout)}const bytes=await reader.read(location,{maxBytes:256*1024*1024});if(!bytes||bytes.length!==receipt.artifact.bytes||`sha256:${librarySha256(bytes)}`!==receipt.artifact.digest)throw new Error("Provider artifact digest or length differs");const files=libraryArtifactZipFiles(bytes);await mkdir(directory,{recursive:false});for(const[path,content]of files){const target=join(directory,path);await mkdir(dirname(target),{recursive:true});await writeFile(target,content,{flag:"wx"})}}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { LibraryNetworkOptions } from './release-network';
|
|
2
|
+
import type { LibraryRegistryOptions, LibraryRegistryReceipt } from './release-registry';
|
|
3
|
+
import type { LibraryReleaseCandidate, LibraryReleaseSource } from './release-artifacts';
|
|
4
|
+
import type { LibraryReleasePlan } from './release-plan';
|
|
5
|
+
import type { LibrarySourceReceipt } from './release-source';
|
|
6
|
+
export declare function libraryReleaseGit(cwd: string, args: string[]): string;
|
|
7
|
+
export declare function libraryReleaseTagSource(cwd: string, repository: string, tag: string): LibraryReleaseSource;
|
|
8
|
+
/** Main CI builds/checks/packs once. The workflow uploads only after its tests pass. */
|
|
9
|
+
export declare function prepareNativeLibraryRelease(options: { cwd: string, directory: string, dryRun?: boolean, runId?: number, runAttempt?: number, branch?: string }): Promise<NativeLibraryPreparation>;
|
|
10
|
+
/** Resolve the actual tag, wait for exact CI and download its unique owned bytes. */
|
|
11
|
+
export declare function acceptNativeLibraryRelease(options: NativeLibraryAcceptanceOptions): Promise<LibrarySourceReceipt | undefined>;
|
|
12
|
+
/** Revalidate source acceptance with a read-only token before granting publication. */
|
|
13
|
+
export declare function verifyNativeLibraryAcceptance(options: Omit<NativeLibraryAcceptanceOptions, 'tag'>): Promise<LibrarySourceReceipt>;
|
|
14
|
+
export declare function publishNativeLibraryRelease(options: Omit<NativeLibraryAcceptanceOptions, 'tag'> & LibraryRegistryOptions & { dryRun?: boolean }): Promise<LibraryRegistryReceipt>;
|
|
15
|
+
/** Historical readback runs only current native verifier code over retained data. */
|
|
16
|
+
export declare function readbackNativeLibraryRelease(options: Omit<NativeLibraryAcceptanceOptions, 'tag'> & LibraryRegistryOptions): Promise<LibraryRegistryReceipt>;
|
|
17
|
+
export declare const LIBRARY_CANDIDATE_ARTIFACT: 'stacks-library-candidate';
|
|
18
|
+
export declare const LIBRARY_CANDIDATE_DIRECTORY: 'storage/framework/runtime/library-release';
|
|
19
|
+
export declare interface LibraryCandidateOwner { schemaVersion: 1, repository: string, commit: string, runId: number, runAttempt: number, workflow: string, branch: string, artifact: string }
|
|
20
|
+
export declare interface NativeLibraryAcceptanceOptions extends LibraryNetworkOptions { cwd: string, directory: string, tag: string, token?: string, runId?: number, historical?: boolean }
|
|
21
|
+
export type NativeLibraryPreparation = { prepared: false, plan?: LibraryReleasePlan, commit?: string }
|
|
22
|
+
| { prepared: true, candidate: LibraryReleaseCandidate, owner: LibraryCandidateOwner, sourceEligible: boolean }
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import{mkdir,mkdtemp,readFile,rm,writeFile}from"node:fs/promises";import{tmpdir}from"node:os";import{dirname,join}from"node:path";import{inspectLibraryRelease,prepareLibraryRelease,validateLibraryReleaseSource}from"./release-artifacts";import{downloadLibraryReleaseSource}from"./release-download";import{createLibraryReleaseReader}from"./release-network";import{DEFAULT_LIBRARY_SOURCE_CI,isLibraryPrereleaseVersion,planNativeLibraryRelease}from"./release-plan";import{publishPreparedLibraryRelease,readbackLibraryRelease}from"./release-registry";import{resolveLibraryRegistryReadAuth}from"./release-registry-auth";import{acceptLibraryReleaseSource,latestLibrarySourceRun}from"./release-source";import{configuredLibrary}from"./packages";export const LIBRARY_CANDIDATE_ARTIFACT="stacks-library-candidate";export const LIBRARY_CANDIDATE_DIRECTORY="storage/framework/runtime/library-release";export function libraryReleaseGit(cwd,args){const result=Bun.spawnSync(["git",...args],{cwd,stdout:"pipe",stderr:"pipe"});if(result.exitCode!==0)throw new Error(`Library release Git read failed: ${result.stderr.toString().trim()}`);return result.stdout.toString().trim()}function fullCommit(cwd,ref){const commit=libraryReleaseGit(cwd,["rev-parse","--verify",`${ref}^{commit}`]);if(!/^[a-f0-9]{40}$/.test(commit))throw new Error("Expected full source commit SHA");return commit}export function libraryReleaseTagSource(cwd,repository,tag){if(!/^v\d+\.\d+\.\d+(?:-[\w.-]+)?(?:\+[\w.-]+)?$/.test(tag))throw new Error("Expected an exact semantic release tag");return{repository,commit:fullCommit(cwd,`refs/tags/${tag}`),tag}}export async function prepareNativeLibraryRelease(options){const plan=await planNativeLibraryRelease(options);if(!plan)return{prepared:false};const commit=fullCommit(options.cwd,"HEAD");if(options.dryRun)return{prepared:false,plan,commit};if(!Number.isSafeInteger(options.runId)||options.runId<=0||!Number.isSafeInteger(options.runAttempt)||options.runAttempt<=0||typeof options.branch!=="string"||!options.branch||options.branch.length>255||/[\r\n\0]/.test(options.branch))throw new Error("Retaining a CI candidate requires its exact source run, attempt and actual branch");const candidate=await prepareLibraryRelease({...options,config:plan.config,tag:plan.tag,checks:plan.checks,source:{repository:plan.repository,commit}});if(!candidate)return{prepared:false};const owner={schemaVersion:1,repository:plan.repository,commit,runId:options.runId,runAttempt:options.runAttempt,branch:options.branch,workflow:plan.sourceCi.workflow,artifact:LIBRARY_CANDIDATE_ARTIFACT};await writeFile(join(options.directory,"ownership.json"),`${JSON.stringify(owner,null,2)}
|
|
2
|
+
`,{flag:"wx"});return{prepared:true,candidate,owner,sourceEligible:options.branch===plan.sourceCi.branch}}export async function acceptNativeLibraryRelease(options){const config=await configuredLibrary();const plan=options.historical?{repository:config?.repository??"",sourceCi:config?.release?.sourceCi??DEFAULT_LIBRARY_SOURCE_CI}:await planNativeLibraryRelease(options);if(!plan)return;const source=libraryReleaseTagSource(options.cwd,plan.repository,options.tag);validateLibraryReleaseSource(source);latestLibrarySourceRun({...plan.sourceCi,repository:source.repository,commit:source.commit},[]);if(plan.sourceCi.timeoutMinutes!==undefined&&(!Number.isSafeInteger(plan.sourceCi.timeoutMinutes)||plan.sourceCi.timeoutMinutes<1||plan.sourceCi.timeoutMinutes>120))throw new Error("Invalid historical source CI timeout");const verifierCommit=fullCommit(options.cwd,"HEAD");const reader=createLibraryReleaseReader({...options,deadlineMs:options.deadlineMs??(plan.sourceCi.timeoutMinutes??30)*60000});const headers={Accept:"application/vnd.github+json",...options.token?{Authorization:`Bearer ${options.token}`}:{}};let runId=options.runId;while(!runId&&reader.remaining()>0){const query=new URLSearchParams({head_sha:source.commit,branch:plan.sourceCi.branch,per_page:"100"});const bytes=await reader.read(`https://api.github.com/repos/${source.repository}/actions/workflows/${plan.sourceCi.workflow.split("/").at(-1)}/runs?${query}`,{headers});const result=JSON.parse(new TextDecoder().decode(bytes));if(!Array.isArray(result.workflow_runs))throw new Error("Source CI returned an invalid run collection");const run=latestLibrarySourceRun({...plan.sourceCi,repository:source.repository,commit:source.commit},result.workflow_runs);if(run?.status==="completed"){if(run.conclusion!=="success")throw new Error(`Exact source CI concluded ${run.conclusion}; resume CI before publication`);runId=run.id;break}await Bun.sleep(Math.min(1e4,reader.remaining()))}if(!runId||reader.remaining()<=0)throw new Error("Exact source CI did not complete within the acceptance deadline");const receipt=await acceptLibraryReleaseSource({...options,deadlineMs:Math.max(1,Math.floor(reader.remaining())),source,verifierCommit,runId,...plan.sourceCi,artifactName:LIBRARY_CANDIDATE_ARTIFACT});await mkdir(dirname(options.directory),{recursive:true});await downloadLibraryReleaseSource(receipt,options.directory,{...options,deadlineMs:Math.max(1,Math.floor(reader.remaining()))});const candidate=await inspectLibraryRelease(options.directory,{repository:source.repository,commit:source.commit});const owner=JSON.parse(await readFile(join(options.directory,"ownership.json"),"utf8"));if(owner.schemaVersion!==1||owner.repository!==source.repository||owner.commit!==source.commit||owner.runId!==receipt.runId||owner.runAttempt!==receipt.runAttempt||owner.workflow!==receipt.workflow||owner.branch!==receipt.branch||owner.artifact!==receipt.artifact.name)throw new Error("Candidate ownership record differs from accepted source CI");if(candidate.source.tag&&candidate.source.tag!==source.tag)throw new Error("Candidate tag differs from the actual release tag");await writeFile(join(options.directory,"accepted-source.json"),`${JSON.stringify(receipt,null,2)}
|
|
3
|
+
`,{flag:"wx"});return receipt}export async function verifyNativeLibraryAcceptance(options){const deadline=performance.now()+(options.deadlineMs??300000);const remaining=()=>{const duration=Math.floor(deadline-performance.now());if(duration<=0)throw new Error("Native source acceptance deadline exceeded");return duration};const saved=JSON.parse(await readFile(join(options.directory,"accepted-source.json"),"utf8"));const config=await configuredLibrary();const policy=config?.release?.sourceCi??DEFAULT_LIBRARY_SOURCE_CI;if(!saved?.source?.tag||saved.source.repository!==config?.repository)throw new Error("Expected a native accepted source receipt for this repository");const source=libraryReleaseTagSource(options.cwd,saved.source.repository,saved.source.tag);const actual=await acceptLibraryReleaseSource({...options,deadlineMs:remaining(),source,verifierCommit:fullCommit(options.cwd,"HEAD"),runId:saved.runId,...policy,artifactName:LIBRARY_CANDIDATE_ARTIFACT});if(JSON.stringify(actual.artifact)!==JSON.stringify(saved.artifact)||source.commit!==saved.source.commit||actual.runAttempt!==saved.runAttempt)throw new Error("Source artifact acceptance changed");const temporary=await mkdtemp(join(tmpdir(),"stacks-library-acceptance-"));try{const original=join(temporary,"candidate");await downloadLibraryReleaseSource(actual,original,{...options,deadlineMs:remaining()});const expected=await inspectLibraryRelease(original,{repository:source.repository,commit:source.commit});const local=await inspectLibraryRelease(options.directory,{repository:source.repository,commit:source.commit});if(JSON.stringify(expected)!==JSON.stringify(local))throw new Error("Local candidate differs from the owned provider artifact")}finally{await rm(temporary,{recursive:true,force:true})}return actual}export async function publishNativeLibraryRelease(options){const plan=await planNativeLibraryRelease(options);if(!plan)throw new Error("Library publication is disabled or no public packages are configured");const receipt=await verifyNativeLibraryAcceptance(options);const config=plan.config;const candidate=await inspectLibraryRelease(options.directory,{repository:receipt.source.repository,commit:receipt.source.commit});if(candidate.packages.some((pkg)=>pkg.tag!==(config?.release?.distTag??"latest")||isLibraryPrereleaseVersion(pkg.version)&&pkg.tag==="latest"))throw new Error("Candidate distribution channel differs from current release policy");const result=await publishPreparedLibraryRelease(options.directory,{...options,readAuth:resolveLibraryRegistryReadAuth(options,process.env),source:{repository:receipt.source.repository,commit:receipt.source.commit},publisher:config?.release?.publisher});result.source=receipt.source;result.verifierCommit=fullCommit(options.cwd,"HEAD");if(!options.dryRun)await writeFile(join(options.directory,"registry-receipt.json"),`${JSON.stringify(result,null,2)}
|
|
4
|
+
`);return result}export async function readbackNativeLibraryRelease(options){const receipt=await verifyNativeLibraryAcceptance(options);const readbackOptions={...options,readAuth:resolveLibraryRegistryReadAuth(options,process.env),requireCurrentTag:false,source:{repository:receipt.source.repository,commit:receipt.source.commit},verifierCommit:fullCommit(options.cwd,"HEAD")};const result=await readbackLibraryRelease(options.directory,readbackOptions);result.source=receipt.source;return result}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/** Retry transport/propagation failures only; callers validate schema and identity once. */
|
|
2
|
+
export declare function createLibraryReleaseReader(options?: LibraryNetworkOptions): LibraryReader;
|
|
3
|
+
/** A single monotonic budget shared by every request in one lifecycle operation. */
|
|
4
|
+
export declare interface LibraryNetworkOptions {
|
|
5
|
+
fetch?: (url: string, init?: RequestInit) => Promise<Response>
|
|
6
|
+
deadlineMs?: number
|
|
7
|
+
requestTimeoutMs?: number
|
|
8
|
+
retryDelayMs?: number
|
|
9
|
+
signal?: AbortSignal
|
|
10
|
+
}
|
|
11
|
+
export declare interface LibraryReader {
|
|
12
|
+
read: (url: string, options?: { maxBytes?: number, processing?: boolean, missing?: boolean, headers?: Record<string, string> }) => Promise<Uint8Array | undefined>
|
|
13
|
+
remaining: () => number
|
|
14
|
+
}
|
|
15
|
+
export declare class LibraryReadFailure extends Error {
|
|
16
|
+
readonly status?: number;
|
|
17
|
+
constructor(message: string, status?: number);
|
|
18
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export class LibraryReadFailure extends Error{status;constructor(message,status){super(message);this.status=status}}export function createLibraryReleaseReader(options={}){const duration=options.deadlineMs??300000;const timeout=options.requestTimeoutMs??15000;const delay=options.retryDelayMs??1000;if(![duration,timeout,delay].every((value)=>Number.isSafeInteger(value)&&value>0))throw new Error("Library readback requires positive finite time budgets");const deadline=performance.now()+duration;const remaining=()=>Math.max(0,deadline-performance.now());const request=options.fetch??fetch;const read=async(url,policy={})=>{const maxBytes=policy.maxBytes??2*1024*1024;if(!Number.isSafeInteger(maxBytes)||maxBytes<=0)throw new Error("Invalid response byte budget");while(remaining()>0){options.signal?.throwIfAborted();const controller=new AbortController;const abort=()=>controller.abort(options.signal?.reason);options.signal?.addEventListener("abort",abort,{once:true});let timer;let response;let reader;try{const interrupted=new Promise((_,reject)=>{controller.signal.addEventListener("abort",()=>{reader?.cancel().catch(()=>{});reject(controller.signal.reason??new DOMException("Request timed out","TimeoutError"))},{once:true})});timer=setTimeout(()=>controller.abort(new DOMException("Request timed out","TimeoutError")),Math.max(1,Math.min(timeout,remaining())));const work=(async()=>{response=await request(url,{signal:controller.signal,redirect:"manual",headers:policy.headers});if(controller.signal.aborted){response.body?.cancel().catch(()=>{});controller.signal.throwIfAborted()}if(!response.ok){response.body?.cancel().catch(()=>{});return}const size=response.headers.get("content-length");if(size!==null&&(!/^\d+$/.test(size)||Number(size)>maxBytes))throw new LibraryReadFailure("Library response exceeds its byte budget");if(!response.body)throw new LibraryReadFailure("Library response has no body");reader=response.body.getReader();const chunks=[];let length=0;while(true){controller.signal.throwIfAborted();const chunk=await reader.read();if(chunk.done)break;length+=chunk.value.length;if(length>maxBytes)throw new LibraryReadFailure("Library response exceeds its byte budget");chunks.push(chunk.value)}return new Uint8Array(Buffer.concat(chunks,length))})();const bytes=await Promise.race([work,interrupted]);if(remaining()<=0)break;if(response.ok)return bytes;if(response.status===404&&policy.missing)return;if(!(response.status===429||response.status>=500&&response.status<600||response.status===404&&policy.processing))throw new LibraryReadFailure(`Library registry read failed: HTTP ${response.status}`,response.status)}catch(error){options.signal?.throwIfAborted();if(!(error instanceof TypeError||error instanceof DOMException&&["AbortError","TimeoutError"].includes(error.name)))throw error}finally{clearTimeout(timer);options.signal?.removeEventListener("abort",abort);controller.abort();if(reader)reader.cancel().catch(()=>{});else response?.body?.cancel().catch(()=>{})}const retryAfter=response?.headers.get("retry-after");const requested=retryAfter&&/^\d+(?:\.\d+)?$/.test(retryAfter)?Number(retryAfter)*1000:retryAfter&&Number.isFinite(Date.parse(retryAfter))?Math.max(0,Date.parse(retryAfter)-Date.now()):delay;const wait=Math.max(delay,requested);const wake=performance.now()+Math.min(wait,remaining());while(performance.now()<wake){options.signal?.throwIfAborted();await Bun.sleep(Math.min(50,Math.max(1,Math.ceil(wake-performance.now()))))}}throw new LibraryReadFailure("Library registry readback deadline exceeded; upload acceptance does not establish public bytes")};return{read,remaining}}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { LibraryConfig, LibraryReleaseOptions } from '@stacksjs/types/library';
|
|
2
|
+
/** Classify a validated package version, excluding its build metadata. */
|
|
3
|
+
export declare function isLibraryPrereleaseVersion(version: string): boolean;
|
|
4
|
+
/** Read-only validation used BEFORE generation, Bump, tags or provider writes. */
|
|
5
|
+
export declare function planNativeLibraryRelease(options: { cwd: string, config?: LibraryConfig, bump?: string, preid?: string, validateWorkflow?: boolean, which?: (bin: string) => string | null }): Promise<LibraryReleasePlan | undefined>;
|
|
6
|
+
/**
|
|
7
|
+
* @defaultValue
|
|
8
|
+
* ```ts
|
|
9
|
+
* {
|
|
10
|
+
* workflow: '.github/workflows/ci.yml',
|
|
11
|
+
* branch: 'main',
|
|
12
|
+
* requiredJobs: ['Lint', 'Typecheck', 'Test']
|
|
13
|
+
* }
|
|
14
|
+
* ```
|
|
15
|
+
*/
|
|
16
|
+
export declare const DEFAULT_LIBRARY_SOURCE_CI: {
|
|
17
|
+
/** @defaultValue '.github/workflows/ci.yml' */
|
|
18
|
+
workflow: string;
|
|
19
|
+
/** @defaultValue 'main' */
|
|
20
|
+
branch: string;
|
|
21
|
+
requiredJobs: string[]
|
|
22
|
+
};
|
|
23
|
+
export declare interface LibraryReleasePlan {
|
|
24
|
+
config: LibraryConfig
|
|
25
|
+
repository: string
|
|
26
|
+
tag: string
|
|
27
|
+
publisher?: LibraryReleaseOptions['publisher']
|
|
28
|
+
checks: string[]
|
|
29
|
+
sourceCi: NonNullable<LibraryReleaseOptions['sourceCi']>
|
|
30
|
+
packages: Array<{ name: string, version: string, registry: 'npm' | 'pantry', access: 'public' | 'restricted' }>
|
|
31
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{lstat,readFile}from"node:fs/promises";import{join}from"node:path";import{incrementVersion,isReleaseType,isValidVersion}from"@stacksjs/bumpx";import{isCalendarBump,nextCalendarVersion}from"../calendar-version";import{configuredLibrary,resolveLibraryPackages}from"./packages";import{retainedArchivePublishCommand}from"./release-registry";export const DEFAULT_LIBRARY_SOURCE_CI={workflow:".github/workflows/ci.yml",branch:"main",requiredJobs:["Lint","Typecheck","Test"]};export function isLibraryPrereleaseVersion(version){return/^\d+\.\d+\.\d+-/.test(version)}export async function planNativeLibraryRelease(options){const config=options.config??await configuredLibrary();if(config?.releaseable===false)return;const packages=(await resolveLibraryPackages(config,{onUnmatched:"skip"})).filter((pkg)=>!pkg.private);if(!packages.length)return;const root=JSON.parse(await readFile(join(options.cwd,"package.json"),"utf8"));const release=config?.release??{};const sourceCi=release.sourceCi??DEFAULT_LIBRARY_SOURCE_CI;const repository=config?.repository;if(!repository||!/^[\w-][\w.-]*\/[\w-][\w.-]*$/.test(repository))throw new Error("Library release requires library.repository as owner/repository");if(!/^\.github\/workflows\/[\w.-]+\.ya?ml$/.test(sourceCi.workflow)||!sourceCi.branch||!sourceCi.requiredJobs?.length||sourceCi.requiredJobs.some((name)=>typeof name!=="string"||!name)||new Set(sourceCi.requiredJobs).size!==sourceCi.requiredJobs.length)throw new Error("Library release requires an exact source CI workflow, branch and unique required jobs");if(release.sourceCi?.timeoutMinutes!==undefined&&(!Number.isSafeInteger(release.sourceCi.timeoutMinutes)||release.sourceCi.timeoutMinutes<1||release.sourceCi.timeoutMinutes>120))throw new Error("Library source CI timeoutMinutes must be an integer from 1 to 120");const checks=release.checks??[];if(!Array.isArray(checks)||new Set(checks).size!==checks.length||checks.some((name)=>!/^[a-z0-9][\w:.-]*$/i.test(name)||!root.scripts?.[name]?.trim()))throw new Error("Library release checks must name unique declared package.json scripts");let version=root.version;if(!version||!isValidVersion(version))throw new Error("Library release requires a valid project version");if(options.bump){if(isCalendarBump(options.bump))version=nextCalendarVersion(version);else if(isReleaseType(options.bump)||isValidVersion(options.bump))version=incrementVersion(version,options.bump,options.preid);else throw new Error("Invalid library release bump")}const resolved=packages.map((pkg)=>({name:pkg.name,version:pkg.version??version,registry:pkg.registry,access:pkg.access}));const prerelease=resolved.some((pkg)=>isLibraryPrereleaseVersion(pkg.version));if(prerelease&&(!release.distTag||release.distTag==="latest"))throw new Error("Prerelease packages require an explicit non-latest distribution tag in library.release.distTag; --preid only selects a version identifier");const tag=release.distTag??"latest";for(const pkg of resolved){if(!isValidVersion(pkg.version))throw new Error(`Invalid library package version: ${pkg.name}`);if(pkg.registry!=="npm")throw new Error("Exact retained archive publication/readback for the Pantry registry is unsupported");retainedArchivePublishCommand({...pkg,tag,archive:{path:"",bytes:0,sha256:"",integrity:"",files:{}}},"/validation-only.tgz",{publisher:release.publisher,which:options.which,dryRun:true})}if(options.validateWorkflow){const workflow=await lstat(join(options.cwd,sourceCi.workflow)).catch(()=>{return});if(!workflow?.isFile()||workflow.isSymbolicLink())throw new Error(`Source CI workflow ${sourceCi.workflow} is not installed. Adopt the native app CI/release workflows before releasing libraries.`)}return{config,repository,tag,publisher:release.publisher,checks,sourceCi:{...sourceCi,requiredJobs:[...sourceCi.requiredJobs]},packages:resolved}}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export declare function libraryRegistryOrigin(registry?: string): string;
|
|
2
|
+
/** Resolve standard CLI credentials only for npmjs, never for an overridden origin. */
|
|
3
|
+
export declare function resolveLibraryRegistryReadAuth(options: LibraryRegistryReadAuthOptions, environment: Record<string, string | undefined>): LibraryRegistryReadAuth | undefined;
|
|
4
|
+
/** Headers are used only after exact-origin validation; registry reads forbid redirects. */
|
|
5
|
+
export declare function libraryRegistryReadHeaders(options: LibraryRegistryReadAuthOptions, restricted?: boolean): Record<string, string> | undefined;
|
|
6
|
+
export declare const NPM_REGISTRY_ORIGIN: 'https://registry.npmjs.org';
|
|
7
|
+
/** Input-only registry credential. Never store it in release data or receipts. */
|
|
8
|
+
export declare interface LibraryRegistryReadAuth {
|
|
9
|
+
registry: string
|
|
10
|
+
token: string
|
|
11
|
+
}
|
|
12
|
+
export declare interface LibraryRegistryReadAuthOptions {
|
|
13
|
+
npmRegistry?: string
|
|
14
|
+
readAuth?: LibraryRegistryReadAuth
|
|
15
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const NPM_REGISTRY_ORIGIN="https://registry.npmjs.org";export function libraryRegistryOrigin(registry=NPM_REGISTRY_ORIGIN){let url;try{url=new URL(registry)}catch{throw new Error("Expected a plain HTTPS npm registry origin")}if(url.username||url.password||url.search||url.hash||url.pathname!=="/"||url.protocol!=="https:"&&!(url.protocol==="http:"&&["localhost","127.0.0.1","[::1]"].includes(url.hostname)))throw new Error("Expected a plain HTTPS npm registry origin");return url.origin}function validateAuth(options){const registry=libraryRegistryOrigin(options.npmRegistry);if(options.readAuth===undefined)return;const auth=options.readAuth;if(!auth||typeof auth.registry!=="string"||libraryRegistryOrigin(auth.registry)!==registry)throw new Error("Registry read credentials must match the exact configured registry origin");if(typeof auth.token!=="string"||!/^[A-Za-z0-9._~+/-]+=*$/.test(auth.token))throw new Error("Expected a nonempty valid registry read credential");return{registry,token:auth.token}}export function resolveLibraryRegistryReadAuth(options,environment){const explicit=validateAuth(options);if(explicit)return explicit;const registry=libraryRegistryOrigin(options.npmRegistry);if(registry!==NPM_REGISTRY_ORIGIN)return;const token=environment.NPM_READ_TOKEN||environment.NODE_AUTH_TOKEN;return!token?undefined:validateAuth({npmRegistry:registry,readAuth:{registry,token}})}export function libraryRegistryReadHeaders(options,restricted=false){const auth=validateAuth(options);if(restricted&&!auth)throw new Error("Restricted packages require an explicit scoped registry read credential before publication or readback");return auth?{Authorization:`Bearer ${auth.token}`}:undefined}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import type { LibraryNetworkOptions } from './release-network';
|
|
2
|
+
import type { LibraryPublisher } from './publish';
|
|
3
|
+
import type { LibraryRegistryReadAuthOptions } from './release-registry-auth';
|
|
4
|
+
import type { LibraryReleaseLimits, LibraryReleasePackage, LibraryReleaseSource } from './release-artifacts';
|
|
5
|
+
export type { LibraryRegistryReadAuth } from './release-registry-auth';
|
|
6
|
+
/** Read actual bytes/tags with current verifier code; never has a publisher capability. */
|
|
7
|
+
export declare function readbackLibraryRelease(directory: string, options?: LibraryReadbackOptions): Promise<LibraryRegistryReceipt>;
|
|
8
|
+
/** Archive capability is deliberately narrower than legacy directory publication. */
|
|
9
|
+
export declare function retainedArchivePublishCommand(pkg: LibraryReleasePackage, archive: string, options?: Pick<PublishPreparedLibraryOptions, 'which' | 'publisher' | 'dryRun' | 'npmRegistry'>): string[];
|
|
10
|
+
/** Never repack restored content. Retry skips existing versions only after comparing actual bytes. */
|
|
11
|
+
export declare function publishPreparedLibraryRelease(directory: string, options?: PublishPreparedLibraryOptions): Promise<LibraryRegistryReceipt>;
|
|
12
|
+
export declare interface LibraryRegistryOptions extends LibraryNetworkOptions, LibraryReleaseLimits, LibraryRegistryReadAuthOptions {
|
|
13
|
+
npmRegistry?: string
|
|
14
|
+
}
|
|
15
|
+
export declare interface LibraryRegistryPackageReceipt {
|
|
16
|
+
name: string
|
|
17
|
+
version: string
|
|
18
|
+
registry: 'npm' | 'pantry'
|
|
19
|
+
integrity: string
|
|
20
|
+
archiveSha256: string
|
|
21
|
+
registryOrigin: string
|
|
22
|
+
tags: Record<string, string>
|
|
23
|
+
}
|
|
24
|
+
export declare interface LibraryRegistryReceipt {
|
|
25
|
+
schemaVersion: 1
|
|
26
|
+
source: LibraryReleaseSource
|
|
27
|
+
verifierCommit?: string
|
|
28
|
+
verifiedAt: string
|
|
29
|
+
packages: Array<LibraryRegistryPackageReceipt & { status?: 'published' | 'existing-identical' | 'dry-run' }>
|
|
30
|
+
}
|
|
31
|
+
export declare interface LibraryReadbackOptions extends LibraryRegistryOptions {
|
|
32
|
+
source?: LibraryReleaseSource
|
|
33
|
+
verifierCommit?: string
|
|
34
|
+
requireCurrentTag?: boolean
|
|
35
|
+
}
|
|
36
|
+
export declare interface PublishPreparedLibraryOptions extends LibraryRegistryOptions {
|
|
37
|
+
source?: LibraryReleaseSource
|
|
38
|
+
cwd?: string
|
|
39
|
+
publisher?: LibraryPublisher
|
|
40
|
+
dryRun?: boolean
|
|
41
|
+
which?: (bin: string) => string | null
|
|
42
|
+
execute?: (args: string[], options: { cwd: string, timeoutMs: number }) => Promise<number>
|
|
43
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{spawnOwnedProcess}from"@stacksjs/cli/owned-process";import{resolve}from"node:path";import{inspectLibraryRelease,libraryIntegrity,librarySha256}from"./release-artifacts";import{createLibraryReleaseReader}from"./release-network";import{publishCommand}from"./publish";import{libraryRegistryOrigin,libraryRegistryReadHeaders}from"./release-registry-auth";function requireNpm(pkg){if(pkg.registry!=="npm")throw new Error("Exact retained archive readback for the Pantry registry is not supported by this adapter")}async function publishedPackage(pkg,options,reader,missing,checkTag){requireNpm(pkg);const origin=libraryRegistryOrigin(options.npmRegistry);const headers=libraryRegistryReadHeaders(options,pkg.access==="restricted");const endpoint=`${origin}/${encodeURIComponent(pkg.name)}`;const raw=await reader.read(`${endpoint}/${encodeURIComponent(pkg.version)}`,{missing,processing:!missing,headers});if(!raw)return;const metadata=JSON.parse(new TextDecoder().decode(raw));if(metadata.name!==pkg.name||metadata.version!==pkg.version||metadata.dist?.integrity!==pkg.archive.integrity||typeof metadata.dist.tarball!=="string")throw new Error(`Published ${pkg.name}@${pkg.version} metadata identity differs from the retained candidate`);let tarball;try{tarball=new URL(metadata.dist.tarball)}catch{throw new Error("Published tarball URL is invalid")}if(tarball.origin!==origin||tarball.username||tarball.password||tarball.hash)throw new Error("Published tarball is outside its actual registry origin");const bytes=await reader.read(tarball.href,{maxBytes:options.maxArchiveBytes??64*1024*1024,processing:!missing,headers});if(!bytes||bytes.length!==pkg.archive.bytes||librarySha256(bytes)!==pkg.archive.sha256||libraryIntegrity(bytes)!==pkg.archive.integrity)throw new Error(`Published ${pkg.name}@${pkg.version} archive bytes differ from the retained candidate`);const tagsRaw=await reader.read(endpoint,{processing:!missing,headers});const packument=JSON.parse(new TextDecoder().decode(tagsRaw));const tags=packument["dist-tags"];if(!tags||typeof tags!=="object"||Array.isArray(tags)||Object.values(tags).some((value)=>typeof value!=="string"))throw new Error("Actual registry distribution tags are invalid");if(checkTag&&tags[pkg.tag]!==pkg.version)throw new Error(`Actual registry tag ${pkg.tag} does not identify ${pkg.name}@${pkg.version}`);return{name:pkg.name,version:pkg.version,registry:pkg.registry,integrity:pkg.archive.integrity,archiveSha256:pkg.archive.sha256,registryOrigin:origin,tags}}export async function readbackLibraryRelease(directory,options={}){const candidate=await inspectLibraryRelease(directory,options.source,options);if(options.verifierCommit!==undefined&&!/^[a-f0-9]{40}$/.test(options.verifierCommit))throw new Error("Expected full verifier commit SHA");libraryRegistryReadHeaders(options,candidate.packages.some((pkg)=>pkg.access==="restricted"));const reader=createLibraryReleaseReader(options);const packages=[];for(const pkg of candidate.packages)packages.push(await publishedPackage(pkg,options,reader,false,options.requireCurrentTag!==false));return{schemaVersion:1,source:candidate.source,verifierCommit:options.verifierCommit,verifiedAt:new Date().toISOString(),packages}}export function retainedArchivePublishCommand(pkg,archive,options={}){const available=options.which??Bun.which;const archiveCapable=(bin)=>bin==="pantry"&&options.publisher===undefined?null:available(bin);const command=publishCommand(pkg.registry,archiveCapable,pkg.tag,options.publisher);if(command[0]==="pantry")throw new Error("Unsupported publisher capability: Pantry publish accepts package directories, not an exact retained .tgz. Select bun or npm for an npm-bound package.");return[...command,archive,"--access",pkg.access,"--ignore-scripts","--registry",libraryRegistryOrigin(options.npmRegistry),...options.dryRun?["--dry-run"]:[]]}export async function publishPreparedLibraryRelease(directory,options={}){const candidate=await inspectLibraryRelease(directory,options.source,options);const commands=candidate.packages.map((pkg)=>retainedArchivePublishCommand(pkg,resolve(directory,pkg.archive.path),options));libraryRegistryReadHeaders(options,candidate.packages.some((pkg)=>pkg.access==="restricted"));const reader=createLibraryReleaseReader(options);const original=JSON.stringify(candidate);const existingPackages=[];if(!options.dryRun){for(const pkg of candidate.packages){const existing=await publishedPackage(pkg,options,reader,true,false);if(existing&&existing.tags[pkg.tag]!==pkg.version)throw new Error(`Existing identical archive is missing requested tag ${pkg.tag}; refusing implicit tag mutation`);existingPackages.push(existing)}}const packages=[];const execute=options.execute??(async(args,execution)=>{const bun=Bun.which("bun");if(!bun)throw new Error("The native publisher process owner requires an installed Bun runtime");const proc=spawnOwnedProcess(args,{ownerRuntime:bun,cwd:execution.cwd,stdout:"inherit",stderr:"inherit",stopGraceMs:0});const timer=setTimeout(()=>{proc.stop().catch(()=>{})},Math.max(1,execution.timeoutMs));try{return(await proc.exited).code}finally{clearTimeout(timer);await proc.stop()}});for(let i=0;i<candidate.packages.length;i++){const pkg=candidate.packages[i];if(options.dryRun){const exit=await execute(commands[i],{cwd:resolve(options.cwd??process.cwd()),timeoutMs:options.requestTimeoutMs??15000});if(exit!==0)throw new Error(`Dry-run publishing ${pkg.name} failed with exit code ${exit}`);packages.push({name:pkg.name,version:pkg.version,registry:pkg.registry,integrity:pkg.archive.integrity,archiveSha256:pkg.archive.sha256,registryOrigin:libraryRegistryOrigin(options.npmRegistry),tags:{},status:"dry-run"});continue}const existing=existingPackages[i];if(existing){packages.push({...existing,status:"existing-identical"});continue}if(JSON.stringify(await inspectLibraryRelease(directory,options.source,options))!==original)throw new Error("Retained candidate record changed during publication");if(reader.remaining()<=0)throw new Error("Library publication deadline exceeded");const exit=await execute(commands[i],{cwd:resolve(options.cwd??process.cwd()),timeoutMs:Math.min(60000,reader.remaining())});const actual=await publishedPackage(pkg,options,reader,exit!==0,true);if(!actual)throw new Error(`Publishing ${pkg.name} failed with exit code ${exit}`);packages.push({...actual,status:"published"})}return{schemaVersion:1,source:candidate.source,verifiedAt:new Date().toISOString(),packages}}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { LibraryConfig } from '@stacksjs/types/library';
|
|
2
|
+
/**
|
|
3
|
+
* Bumpx's version prompt is private and writes immediately after selection.
|
|
4
|
+
* Collect the choice through native CLI first so channel validation precedes
|
|
5
|
+
* every generated file and version mutation. Bump still owns the actual bump.
|
|
6
|
+
*/
|
|
7
|
+
export declare function selectNativeLibraryReleaseVersion(options: {
|
|
8
|
+
cwd: string
|
|
9
|
+
config?: LibraryConfig
|
|
10
|
+
preid?: string
|
|
11
|
+
|
|
12
|
+
choose?: (choices: LibraryReleaseVersionChoice[]) => Promise<string>
|
|
13
|
+
custom?: () => Promise<string>
|
|
14
|
+
}): Promise<string | undefined>;
|
|
15
|
+
export declare interface LibraryReleaseVersionChoice { label: string, value: string }
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{readFile}from"node:fs/promises";import{join}from"node:path";import{incrementVersion,isCiEnvironment,isValidVersion}from"@stacksjs/bumpx";import{nextCalendarVersion}from"../calendar-version";import{configuredLibrary,resolveLibraryPackages}from"./packages";export async function selectNativeLibraryReleaseVersion(options){const config=options.config??await configuredLibrary();if(config?.releaseable===false)return;const packages=await resolveLibraryPackages(config,{onUnmatched:"skip"});if(!packages.some((pkg)=>!pkg.private))return;const root=JSON.parse(await readFile(join(options.cwd,"package.json"),"utf8"));if(!root.version||!isValidVersion(root.version))throw new Error("Library release requires a valid project version");if(!options.choose&&(!process.stdin.isTTY||!process.stdout.isTTY||isCiEnvironment()))throw new Error("Library release needs --bump when stdin is not an interactive terminal");const choices=[];for(const type of["patch","minor","major","prepatch","preminor","premajor","prerelease","pre","build","release"]){try{const value=incrementVersion(root.version,type,options.preid);choices.push({label:`${type} ${value}`,value})}catch{}}const calendar=nextCalendarVersion(root.version);choices.push({label:`calendar ${calendar}`,value:calendar},{label:"custom version",value:"custom"});const{select,text}=await import("@stacksjs/cli");const selected=options.choose?await options.choose(choices):await select({message:"Choose the library release version:",choices});const version=selected==="custom"?options.custom?await options.custom():await text("Enter the new version:"):selected;if(!version||!isValidVersion(version))throw new Error("Library release selection requires a valid semantic version");return version}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import type { LibraryNetworkOptions } from './release-network';
|
|
2
|
+
import type { LibraryReleaseSource } from './release-artifacts';
|
|
3
|
+
/** A failed job is retry eligible only if no runner or step ever started it. */
|
|
4
|
+
export declare function sourceJobNeverStarted(job: LibrarySourceJob): boolean;
|
|
5
|
+
export declare function latestLibrarySourceRun(policy: LibrarySourcePolicy, runs: LibrarySourceRun[]): LibrarySourceRun | undefined;
|
|
6
|
+
/** Shared exact-source policy, with product/framework job names supplied by callers. */
|
|
7
|
+
export declare function evaluateLibrarySourceCi(policy: LibrarySourcePolicy, runs: LibrarySourceRun[], jobs: LibrarySourceJob[]): LibrarySourceDecision;
|
|
8
|
+
/** Accept ownership using current verifier code. Historical source is never executed. */
|
|
9
|
+
export declare function acceptLibraryReleaseSource(options: AcceptLibrarySourceOptions): Promise<LibrarySourceReceipt>;
|
|
10
|
+
export declare interface LibrarySourceRun {
|
|
11
|
+
id: number
|
|
12
|
+
head_sha: string
|
|
13
|
+
head_branch: string
|
|
14
|
+
event: string
|
|
15
|
+
path: string
|
|
16
|
+
status: string
|
|
17
|
+
conclusion: string | null
|
|
18
|
+
run_attempt?: number
|
|
19
|
+
repository?: { id?: number, full_name?: string }
|
|
20
|
+
head_repository?: { id?: number, full_name?: string }
|
|
21
|
+
}
|
|
22
|
+
export declare interface LibrarySourceJob { name: string, status: string, conclusion: string | null, runner_id?: number | null, steps?: unknown[] }
|
|
23
|
+
export declare interface LibrarySourcePolicy {
|
|
24
|
+
commit: string
|
|
25
|
+
workflow: string
|
|
26
|
+
branch: string
|
|
27
|
+
requiredJobs: string[]
|
|
28
|
+
events?: string[]
|
|
29
|
+
repository?: string
|
|
30
|
+
}
|
|
31
|
+
export declare interface LibrarySourceReceipt {
|
|
32
|
+
schemaVersion: 1
|
|
33
|
+
source: LibraryReleaseSource
|
|
34
|
+
verifierCommit?: string
|
|
35
|
+
runId: number
|
|
36
|
+
runAttempt: number
|
|
37
|
+
workflow: string
|
|
38
|
+
branch: string
|
|
39
|
+
requiredJobs: string[]
|
|
40
|
+
artifact: { id: number, name: string, bytes: number, expiresAt: string, digest?: string, downloadUrl: string }
|
|
41
|
+
}
|
|
42
|
+
export declare interface AcceptLibrarySourceOptions extends LibraryNetworkOptions {
|
|
43
|
+
source: LibraryReleaseSource
|
|
44
|
+
runId: number
|
|
45
|
+
workflow: string
|
|
46
|
+
branch: string
|
|
47
|
+
requiredJobs: string[]
|
|
48
|
+
artifactName: string
|
|
49
|
+
verifierCommit?: string
|
|
50
|
+
token?: string
|
|
51
|
+
maxArtifactBytes?: number
|
|
52
|
+
}
|
|
53
|
+
export type LibrarySourceDecision = { kind: 'pending' }
|
|
54
|
+
| { kind: 'accepted', runId: number }
|
|
55
|
+
| { kind: 'rejected', runId: number, reason: string }
|
|
56
|
+
| { kind: 'starved', runId: number, attempt: number, jobs: string[] }
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{validateLibraryReleaseSource}from"./release-artifacts";import{createLibraryReleaseReader}from"./release-network";export function sourceJobNeverStarted(job){return job.status==="completed"&&job.conclusion==="cancelled"&&!job.runner_id&&(job.steps?.length??0)===0}function validatePolicy(policy){if(!/^[a-f0-9]{40}$/.test(policy.commit)||!/^\.github\/workflows\/[\w.-]+\.ya?ml$/.test(policy.workflow)||typeof policy.branch!=="string"||!policy.branch||!Array.isArray(policy.requiredJobs)||!policy.requiredJobs.length||policy.requiredJobs.some((name)=>typeof name!=="string"||!name)||new Set(policy.requiredJobs).size!==policy.requiredJobs.length||policy.repository!==undefined&&!/^[\w-][\w.-]*\/[\w-][\w.-]*$/.test(policy.repository)||policy.events!==undefined&&(!policy.events.length||policy.events.some((event)=>!["push","workflow_dispatch"].includes(event))))throw new Error("Expected an exact source commit and CI policy with unique required jobs")}export function latestLibrarySourceRun(policy,runs){validatePolicy(policy);return runs.filter((run)=>Number.isSafeInteger(run.id)&&run.id>0&&run.head_sha===policy.commit&&run.head_branch===policy.branch&&(policy.events??["push","workflow_dispatch"]).includes(run.event)&&run.path===policy.workflow&&(!policy.repository||run.repository?.full_name===policy.repository&&run.head_repository?.full_name===policy.repository)).sort((a,b)=>b.id-a.id)[0]}export function evaluateLibrarySourceCi(policy,runs,jobs){const run=latestLibrarySourceRun(policy,runs);if(!run||run.status!=="completed")return{kind:"pending"};if(run.conclusion!=="success"){const unsuccessful=jobs.filter((job)=>job.conclusion!=="success"&&job.conclusion!=="skipped");if((run.conclusion==="failure"||run.conclusion==="cancelled")&&unsuccessful.length&&unsuccessful.every(sourceJobNeverStarted))return{kind:"starved",runId:run.id,attempt:run.run_attempt??1,jobs:unsuccessful.map((job)=>job.name)};return{kind:"rejected",runId:run.id,reason:`Source CI concluded ${run.conclusion}`}}for(const name of policy.requiredJobs){const matches=jobs.filter((job)=>job.name===name);if(matches.length!==1||matches[0]?.status!=="completed"||matches[0].conclusion!=="success")return{kind:"rejected",runId:run.id,reason:`Required source job did not succeed exactly once: ${name}`}}return{kind:"accepted",runId:run.id}}export async function acceptLibraryReleaseSource(options){validateLibraryReleaseSource(options.source);const policy={...options,commit:options.source.commit,repository:options.source.repository};validatePolicy(policy);if(!Number.isSafeInteger(options.runId)||options.runId<=0||!/^[\w.-]{1,128}$/.test(options.artifactName)||options.verifierCommit!==undefined&&!/^[a-f0-9]{40}$/.test(options.verifierCommit))throw new Error("Expected an exact run ID, artifact name and verifier SHA");const reader=createLibraryReleaseReader(options);const headers={Accept:"application/vnd.github+json"};if(options.token)headers.Authorization=`Bearer ${options.token}`;const base=`https://api.github.com/repos/${options.source.repository}`;const json=async(path)=>JSON.parse(new TextDecoder().decode(await reader.read(`${base}${path}`,{headers})));const run=await json(`/actions/runs/${options.runId}`);if(run.id!==options.runId||latestLibrarySourceRun(policy,[run])?.id!==options.runId||!Number.isSafeInteger(run.run_attempt??1)||(run.run_attempt??1)<=0||!Number.isSafeInteger(run.repository?.id)||run.repository.id<=0||run.head_repository?.id!==run.repository?.id)throw new Error("Source CI repository, SHA, workflow, branch or fork ownership differs");async function pages(path,field){const all=[];for(let page=1;page<=100;page++){const result=await json(`${path}${path.includes("?")?"&":"?"}per_page=100&page=${page}`);const items=result[field];if(!Array.isArray(items))throw new Error("Source CI returned an invalid collection");all.push(...items);if(items.length<100)return all}throw new Error("Source CI collection exceeds its pagination budget")}const jobs=await pages(`/actions/runs/${run.id}/jobs?filter=latest`,"jobs");const decision=evaluateLibrarySourceCi(policy,[run],jobs);if(decision.kind!=="accepted")throw new Error(`Source CI was not accepted: ${decision.kind==="rejected"?decision.reason:decision.kind}`);const artifacts=await pages(`/actions/runs/${run.id}/artifacts`,"artifacts");const matches=artifacts.filter((artifact)=>artifact.name===options.artifactName);if(!matches.length||matches.some((artifact)=>!Number.isSafeInteger(artifact.id)||artifact.id<=0))throw new Error("Expected owned retained candidate artifacts with positive integer IDs");const latestId=matches.reduce((highest,artifact)=>Math.max(highest,artifact.id),0);const latest=matches.filter((artifact)=>artifact.id===latestId);if(latest.length!==1)throw new Error("Expected exactly one latest owned retained candidate artifact");const artifact=latest[0];const ownership=artifact.workflow_run;const maxBytes=options.maxArtifactBytes??256*1024*1024;if(!Number.isSafeInteger(maxBytes)||maxBytes<=0||!Number.isSafeInteger(artifact.id)||artifact.id<=0||artifact.expired!==false||!Number.isSafeInteger(artifact.size_in_bytes)||artifact.size_in_bytes<=0||artifact.size_in_bytes>maxBytes||!Number.isFinite(Date.parse(artifact.expires_at))||Date.parse(artifact.expires_at)<=Date.now()||ownership?.id!==run.id||ownership.head_sha!==options.source.commit||ownership.repository_id!==run.repository.id||ownership.head_repository_id!==run.repository.id||artifact.digest!==undefined&&!/^sha256:[a-f0-9]{64}$/.test(artifact.digest))throw new Error("Retained candidate artifact is expired, oversized or owned by another source");return{schemaVersion:1,source:{...options.source},verifierCommit:options.verifierCommit,runId:run.id,runAttempt:run.run_attempt??1,workflow:options.workflow,branch:options.branch,requiredJobs:[...options.requiredJobs],artifact:{id:artifact.id,name:artifact.name,bytes:artifact.size_in_bytes,expiresAt:artifact.expires_at,digest:artifact.digest,downloadUrl:`${base}/actions/artifacts/${artifact.id}/zip`}}}
|
|
@@ -1,3 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Inspect a stable, caller-owned installation against libraryArchiveEntries().
|
|
3
|
+
* Returns every actual package file only after complete key-set/byte equality.
|
|
4
|
+
* Rejects links, special files, unsafe paths and budget overruns. Optional Bun
|
|
5
|
+
* installer metadata is checked and omitted from the returned archive payload.
|
|
6
|
+
* This verifies local bytes, not registry/source ownership or concurrent writers.
|
|
7
|
+
*/
|
|
8
|
+
export declare function inspectInstalledLibraryPackage(directory: string, expected: ReadonlyMap<string, Uint8Array>, options?: InstalledLibraryPackageOptions): Map<string, Uint8Array>;
|
|
1
9
|
/** Validate data before using any value as a URL, filesystem path or command. */
|
|
2
10
|
export declare function validateReleaseEvidencePlan(value: unknown): asserts value is ReleaseEvidencePlan;
|
|
3
11
|
/**
|
|
@@ -7,6 +15,14 @@ export declare function validateReleaseEvidencePlan(value: unknown): asserts val
|
|
|
7
15
|
* this receipt is not an assertion that registry attestations were validated.
|
|
8
16
|
*/
|
|
9
17
|
export declare function verifyReleaseEvidence(plan: ReleaseEvidencePlan, options: ReleaseEvidenceOptions): Promise<ReleaseEvidenceReceipt>;
|
|
18
|
+
export declare interface InstalledLibraryPackageOptions {
|
|
19
|
+
consumerRoot?: string
|
|
20
|
+
allowBunTag?: boolean
|
|
21
|
+
maxEntries?: number
|
|
22
|
+
maxDepth?: number
|
|
23
|
+
maxBytes?: number
|
|
24
|
+
maxFileBytes?: number
|
|
25
|
+
}
|
|
10
26
|
export declare interface ReleaseSource {
|
|
11
27
|
repository: string
|
|
12
28
|
commit: string
|
package/dist/release-evidence.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{createHash}from"node:crypto";import{lstatSync,readFileSync,readdirSync,realpathSync}from"node:fs";import{join,relative,resolve}from"node:path";import{runProjectScripts}from"./project-scripts";function fail(message){throw new Error(`Release verification blocked: ${message}`)}function hash(bytes){return createHash("sha256").update(bytes).digest("hex")}function nonemptyStrings(value){return Array.isArray(value)&&value.length>0&&value.every((item)=>typeof item==="string"&&item.length>0)}export function validateReleaseEvidencePlan(value){const plan=value;if(!plan||plan.schemaVersion!==1||!Array.isArray(plan.sources)||!plan.sources.length||!nonemptyStrings(plan.checks)||plan.checks.some((name)=>!/^[a-z0-9][\w:.-]*$/i.test(name))||new Set(plan.checks).size!==plan.checks.length)fail("Expected a version 1 plan with sources and named consumer checks");const packages=new Set;for(const source of plan.sources){if(!source||!/^[\w.-]+\/[\w.-]+$/.test(source.repository)||!/^[a-f0-9]{40}$/.test(source.commit)||typeof source.tag!=="string"||!source.tag||!Array.isArray(source.gates)||!source.gates.length||!Array.isArray(source.packages)||!source.packages.length)fail("Each source needs a repository, full commit, tag, release gates and packages");for(const gate of source.gates){if(!gate||!Number.isSafeInteger(gate.runId)||gate.runId<=0||!/^\.github\/workflows\/[\w.-]+\.ya?ml$/.test(gate.workflow)||!nonemptyStrings(gate.requiredJobs)||new Set(gate.requiredJobs).size!==gate.requiredJobs.length)fail("Each release gate needs an exact run, workflow and required jobs")}for(const pkg of source.packages){if(!pkg||!/^(?:@[a-z0-9][\w.-]*\/)?[a-z0-9][\w.-]*$/.test(pkg.name)||!/^\d+\.\d+\.\d+(?:-[\w.-]+)?(?:\+[\w.-]+)?$/.test(pkg.version)||!/^sha512-[A-Za-z0-9+/]{86}==$/.test(pkg.integrity)||packages.has(pkg.name))fail("Packages need unique names, exact versions and original SHA-512 archive identities");packages.add(pkg.name)}}}async function boundedBody(response,limit){const declared=response.headers.get("content-length");if(declared&&Number(declared)>limit){await response.body?.cancel();fail("Response exceeds its byte budget")}if(!response.body)fail("Response has no body");const reader=response.body.getReader();const chunks=[];let length=0;try{while(true){const next=await reader.read();if(next.done)break;length+=next.value.byteLength;if(length>limit)fail("Response exceeds its byte budget");chunks.push(next.value)}return Buffer.concat(chunks,length)}finally{await reader.cancel().catch(()=>{});reader.releaseLock()}}function validateTarBounds(tar){if(!tar.byteLength||tar.byteLength%512)fail("Published archive has invalid tar block bounds");let offset=0;let files=0;let headers=0;const text=(start,length)=>new TextDecoder().decode(tar.subarray(start,start+length)).replace(/\0.*$/s,"").trim();while(offset+512<=tar.byteLength){const header=tar.subarray(offset,offset+512);if(header.every((byte)=>byte===0)){if(tar.subarray(offset).some((byte)=>byte!==0))fail("Published archive has data after its tar terminator");return files}if(++headers>1e5)fail("Published archive has too many entries");const rawSize=text(offset+124,12);if(!/^[0-7]+$/.test(rawSize))fail("Published archive has an unsupported tar size");const size=Number.parseInt(rawSize,8);const end=offset+512+Math.ceil(size/512)*512;if(!Number.isSafeInteger(end)||end>tar.byteLength)fail("Published archive declares bytes outside its tar bounds");const type=header[156]===0?"0":String.fromCharCode(header[156]);if(type==="0")files++;else if(type==="x"||type==="g"){const metadata=tar.subarray(offset+512,offset+512+size);const allowed=new Set(["path","mtime","atime","ctime","uid","gid","uname","gname","comment","charset","hdrcharset"]);for(let start=0;start<metadata.length;){const space=metadata.indexOf(32,start);if(space<0)fail("Published archive has malformed PAX records");const rawLength=new TextDecoder().decode(metadata.subarray(start,space));const length=Number(rawLength);const end=start+length;if(!/^\d+$/.test(rawLength)||!Number.isSafeInteger(end)||end>metadata.length||end<=space+2||metadata[end-1]!==10)fail("Published archive has malformed PAX bounds");const equals=metadata.indexOf(61,space+1);const key=new TextDecoder().decode(metadata.subarray(space+1,equals));if(equals<0||equals>=end-1||!allowed.has(key))fail("Published archive has unsupported PAX allocation or link metadata");start=end}}else if(type!=="5"&&type!=="L")fail("Published archive contains a link or unsupported tar entry");offset=end}fail("Published archive has no tar terminator")}async function archiveFiles(bytes,limit){const stream=new Blob([new Uint8Array(bytes)]).stream().pipeThrough(new DecompressionStream("gzip"));const tar=await boundedBody(new Response(stream),limit);const count=validateTarBounds(tar);const files=await new Bun.Archive(tar).files();if(!count||files.size!==count)fail("Published archive has missing or duplicate file paths");const result=new Map;for(const[name,file]of files){const path=name.slice("package/".length);if(!name.startsWith("package/")||!path||path.includes("\\")||path.includes(":")||path.split("/").some((part)=>!part||part==="."||part==="..")||result.has(path))fail("Published archive contains an unsafe or duplicate package path");result.set(path,{sha256:hash(new Uint8Array(await file.arrayBuffer())),bytes:file.size})}if(!result.has("package.json"))fail("Published archive has no package manifest");return result}function verifyInstalledFiles(directory,expected){const found=new Set;const hasBundledDependencies=[...expected.keys()].some((path)=>path.startsWith("node_modules/"));function visit(path,depth){if(depth>128)fail("Installed package directory depth exceeds its budget");for(const entry of readdirSync(join(directory,path),{withFileTypes:true})){if(!path&&entry.name==="node_modules"&&!hasBundledDependencies)continue;const name=path?`${path}/${entry.name}`:entry.name;if(found.size>=1e5)fail("Installed package has too many files");const fullPath=join(directory,name);if(entry.isDirectory())visit(name,depth+1);else{const identity=expected.get(name);const stat=lstatSync(fullPath);if(!identity||!stat.isFile()||stat.size!==identity.bytes||hash(readFileSync(fullPath))!==identity.sha256)fail(`Installed package file ${name} differs from the verified archive`);found.add(name)}}}visit("",0);if(found.size!==expected.size)fail("Installed package is missing files from the verified archive")}function installedState(plan,cwd,archives){const lock=readFileSync(join(cwd,"bun.lock"),"utf8");const parsed=Bun.JSONC.parse(lock);const nodeModules=realpathSync(join(cwd,"node_modules"));const manifests=[];for(const source of plan.sources){for(const pkg of source.packages){const locked=parsed.packages?.[pkg.name];if(locked?.[0]!==`${pkg.name}@${pkg.version}`||locked.at(-1)!==pkg.integrity)fail(`Consumer lock does not contain ${pkg.name}@${pkg.version} with the original archive identity`);const directory=realpathSync(join(cwd,"node_modules",pkg.name));const path=relative(nodeModules,directory);if(!path||path.startsWith("..")||resolve(nodeModules,path)!==directory)fail(`${pkg.name} resolves outside the installed consumer packages`);const raw=readFileSync(join(directory,"package.json"),"utf8");const manifest=JSON.parse(raw);if(manifest.name!==pkg.name||manifest.version!==pkg.version)fail(`Installed ${pkg.name} does not match tested version ${pkg.version}`);verifyInstalledFiles(directory,archives.get(pkg.name));manifests.push(hash(raw))}}return{lock,sha256:hash(lock),manifests:manifests.join(":")}}export async function verifyReleaseEvidence(plan,options){validateReleaseEvidencePlan(plan);const requestTimeoutMs=options.requestTimeoutMs??30000;const checkTimeoutMs=options.checkTimeoutMs??300000;const maxArchiveBytes=options.maxArchiveBytes??128*1024*1024;const maxExpandedBytes=options.maxExpandedBytes??128*1024*1024;for(const number of[requestTimeoutMs,checkTimeoutMs,maxArchiveBytes,maxExpandedBytes]){if(!Number.isSafeInteger(number)||number<=0)fail("Budgets must be positive safe integers")}const read=options.fetch??fetch;const archives=new Map;const receipt={schemaVersion:1,verifiedAt:"",sources:[],consumer:{lockfile:"bun.lock",sha256:"",bunVersion:"",checks:[]}};async function request(url,github,limit=1024*1024){const timeout=AbortSignal.timeout(requestTimeoutMs);const signal=options.signal?AbortSignal.any([options.signal,timeout]):timeout;signal.throwIfAborted();const response=await read(url,{method:"GET",redirect:"error",signal,headers:github?{Accept:"application/vnd.github+json",...options.token?{Authorization:`Bearer ${options.token}`}:{}}:{}});if(response.status!==200){await response.body?.cancel();fail(`${github?"Source evidence":"Published package"} request returned HTTP ${response.status}`)}return boundedBody(response,limit)}async function github(path){return JSON.parse(new TextDecoder().decode(await request(`https://api.github.com/repos/${path}`,true)))}for(const source of plan.sources){let ref=(await github(`${source.repository}/git/ref/tags/${encodeURIComponent(source.tag)}`)).object;for(let depth=0;ref?.type==="tag"&&depth<5;depth++)ref=(await github(`${source.repository}/git/tags/${ref.sha}`)).object;if(ref?.type!=="commit"||ref.sha!==source.commit)fail(`${source.tag} does not identify ${source.commit}`);const manifest=await github(`${source.repository}/contents/package.json?ref=${source.commit}`);if(manifest.encoding!=="base64"||typeof manifest.content!=="string")fail("Source package manifest is unavailable");const sourcePackage=JSON.parse(Buffer.from(manifest.content,"base64").toString("utf8"));const releaseScript=sourcePackage.scripts?.["release:patch"];if(typeof releaseScript!=="string"||!releaseScript.trim())fail(`${source.repository} has no existing release:patch command`);for(const gate of source.gates){const run=await github(`${source.repository}/actions/runs/${gate.runId}`);if(run.id!==gate.runId||run.repository?.full_name!==source.repository||run.path!==gate.workflow||run.head_sha!==source.commit)fail(`Release gate ${gate.runId} does not verify the exact source and workflow`);if(run.status!=="completed"||run.conclusion!=="success")fail(`Release gate ${gate.runId} is not successful`);const jobs=[];for(let page=1;page<=10;page++){const batch=await github(`${source.repository}/actions/runs/${gate.runId}/jobs?per_page=100&page=${page}`);if(!Array.isArray(batch.jobs))fail(`Release gate ${gate.runId} has no readable jobs`);jobs.push(...batch.jobs);if(batch.jobs.length<100)break;if(page===10)fail("Release job inventory exceeds its budget")}for(const name of gate.requiredJobs){const matches=jobs.filter((job)=>job.name===name);if(matches.length!==1||matches[0]?.status!=="completed"||matches[0]?.conclusion!=="success")fail(`Required release job ${name} did not pass exactly once`)}}const verified={...source,releaseCommand:"bun run release:patch",releaseScript,packages:[]};for(const pkg of source.packages){const metadata=JSON.parse(new TextDecoder().decode(await request(`https://registry.npmjs.org/${encodeURIComponent(pkg.name)}/${encodeURIComponent(pkg.version)}`,false)));if(metadata.name!==pkg.name||metadata.version!==pkg.version||metadata.dist?.integrity!==pkg.integrity)fail(`Published ${pkg.name}@${pkg.version} does not match the original archive identity`);if(metadata.gitHead!==undefined&&metadata.gitHead!==source.commit)fail(`Published ${pkg.name} names a different source commit`);if(!metadata.dist?.tarball)fail(`Published ${pkg.name} has no archive URL`);const tarball=new URL(metadata.dist.tarball);if(tarball.origin!=="https://registry.npmjs.org"||tarball.username||tarball.password)fail("Archive URL is outside the public npm registry");const bytes=await request(tarball.href,false,maxArchiveBytes);if(`sha512-${createHash("sha512").update(bytes).digest("base64")}`!==pkg.integrity)fail(`Published ${pkg.name}@${pkg.version} archive bytes failed SHA-512 verification`);const files=await archiveFiles(bytes,maxExpandedBytes);archives.set(pkg.name,files);const filesSha256=hash(JSON.stringify([...files].sort(([left],[right])=>left.localeCompare(right))));verified.packages.push({...pkg,tarball:tarball.href,bytes:bytes.byteLength,filesSha256})}receipt.sources.push(verified)}const initial=installedState(plan,options.cwd,archives);const checks=await runProjectScripts({cwd:options.cwd,scripts:plan.checks,signal:options.signal,timeoutMs:checkTimeoutMs});receipt.consumer.bunVersion=checks.bunVersion;receipt.consumer.checks=checks.checks;const final=installedState(plan,options.cwd,archives);if(final.sha256!==initial.sha256||final.manifests!==initial.manifests)fail("Consumer checks changed the verified lockfile or package identities");receipt.consumer.sha256=initial.sha256;options.signal?.throwIfAborted();receipt.verifiedAt=new Date().toISOString();return receipt}
|
|
1
|
+
import{boundedBody,libraryArchiveEntries,safeArchivePath}from"./library/archive.js";import{createHash}from"node:crypto";import{closeSync,constants,fstatSync,lstatSync,openSync,opendirSync,readFileSync,readSync,realpathSync}from"node:fs";import{isAbsolute,join,relative,resolve}from"node:path";export function inspectInstalledLibraryPackage(directory,expected,options={}){return verifyInstalledFiles(directory,expected,options)}function contained(root,path){const child=relative(root,path);return!!child&&child!==".."&&!child.startsWith(`..${process.platform==="win32"?"\\":"/"}`)&&!isAbsolute(child)}function verifyInstalledFiles(directory,expected,options={},separateDependencies=false){const maxEntries=options.maxEntries??1e5;const maxDepth=options.maxDepth??128;const maxBytes=options.maxBytes??256*1024*1024;const maxFileBytes=options.maxFileBytes??maxBytes;for(const limit of[maxEntries,maxDepth,maxBytes,maxFileBytes]){if(!Number.isSafeInteger(limit)||limit<=0)fail("Installed package budgets must be positive safe integers")}if(expected.size>maxEntries)fail("Installed package expected inventory exceeds its budget");const identities=new Map;let expectedBytes=0;for(const[name,entry]of expected){const size=entry instanceof Uint8Array?entry.byteLength:entry?.bytes;if(!Number.isSafeInteger(size)||size<0||size>maxFileBytes||expectedBytes+size>maxBytes)fail("Installed package expected inventory exceeds its budget");const identity=entry instanceof Uint8Array?{sha256:hash(entry),bytes:size}:entry;if(!safeArchivePath(name)||!identity||!Number.isSafeInteger(identity.bytes)||identity.bytes<0||!/^[a-f0-9]{64}$/.test(identity.sha256))fail("Installed package expected inventory has an unsafe path or identity");expectedBytes+=identity.bytes;identities.set(name,identity)}const rootStat=lstatSync(directory);if(!rootStat.isDirectory()||rootStat.isSymbolicLink())fail("Installed package root must be a real directory");const root=realpathSync(directory);if(options.consumerRoot&&!contained(realpathSync(options.consumerRoot),root))fail("Installed package resolves outside its consumer root");const actual=new Map;const hasBundledDependencies=[...expected.keys()].some((name)=>name.startsWith("node_modules/"));let entries=0;let bytesRead=0;function visit(path,depth){if(depth>maxDepth)fail("Installed package directory depth exceeds its budget");const fullDirectory=join(root,path);const before=lstatSync(fullDirectory);if(!before.isDirectory()||before.isSymbolicLink()||realpathSync(fullDirectory)!==fullDirectory)fail("Installed package directory is a link or escapes its root");const handle=opendirSync(fullDirectory);try{for(let entry=handle.readSync();entry;entry=handle.readSync()){if(++entries>maxEntries)fail("Installed package entry count exceeds its budget");const name=path?`${path}/${entry.name}`:entry.name;if(!safeArchivePath(name))fail("Installed package contains an unsafe path");if(separateDependencies&&!path&&entry.name==="node_modules"&&!hasBundledDependencies)continue;const fullPath=join(root,name);const stat=lstatSync(fullPath);if(stat.isSymbolicLink())fail(`Installed package file ${name} is a symbolic link`);if(!separateDependencies&&name===".bun-tag"&&!stat.isFile())fail(`Installed package file ${name} is not a regular file`);if(stat.isDirectory()){visit(name,depth+1);continue}if(!stat.isFile())fail(`Installed package file ${name} is not a regular file`);const identity=identities.get(name);const bunTag=!identity&&name===".bun-tag"&&options.allowBunTag===true;if(!identity&&!bunTag)fail(`Installed package file ${name} differs from the verified archive`);if(stat.size>maxFileBytes||bytesRead+stat.size>maxBytes)fail("Installed package file bytes exceed their budget");if(identity&&stat.size!==identity.bytes||bunTag&&stat.size!==95)fail(`Installed package file ${name} differs from the verified archive`);const fd=openSync(fullPath,constants.O_RDONLY|(constants.O_NOFOLLOW??0)|(constants.O_NONBLOCK??0));let bytes;try{const opened=fstatSync(fd);if(!opened.isFile()||opened.dev!==stat.dev||opened.ino!==stat.ino||opened.size!==stat.size)fail(`Installed package file ${name} changed during inspection`);bytes=new Uint8Array(stat.size);let offset=0;while(offset<bytes.length){const count=readSync(fd,bytes,offset,bytes.length-offset,null);if(!count)fail(`Installed package file ${name} changed during inspection`);offset+=count}const after=fstatSync(fd);if(readSync(fd,new Uint8Array(1),0,1,null)||after.size!==stat.size||after.mtimeMs!==opened.mtimeMs||after.ctimeMs!==opened.ctimeMs)fail(`Installed package file ${name} changed during inspection`)}finally{closeSync(fd)}bytesRead+=bytes.length;if(bunTag){if(!/^sha512-[A-Za-z0-9+/]{86}==$/.test(new TextDecoder().decode(bytes)))fail("Installed package has invalid Bun installer metadata")}else{if(hash(bytes)!==identity.sha256)fail(`Installed package file ${name} differs from the verified archive`);actual.set(name,bytes)}}}finally{handle.closeSync()}const after=lstatSync(fullDirectory);if(!after.isDirectory()||after.dev!==before.dev||after.ino!==before.ino||after.mtimeMs!==before.mtimeMs||after.ctimeMs!==before.ctimeMs||realpathSync(fullDirectory)!==fullDirectory)fail("Installed package directory changed during inspection")}visit("",0);if(actual.size!==expected.size)fail("Installed package is missing files from the verified archive");if(realpathSync(directory)!==root)fail("Installed package root changed during inspection");return actual}function fail(message){throw new Error(`Release verification blocked: ${message}`)}function hash(bytes){return createHash("sha256").update(bytes).digest("hex")}function nonemptyStrings(value){return Array.isArray(value)&&value.length>0&&value.every((item)=>typeof item==="string"&&item.length>0)}export function validateReleaseEvidencePlan(value){const plan=value;if(!plan||plan.schemaVersion!==1||!Array.isArray(plan.sources)||!plan.sources.length||!nonemptyStrings(plan.checks)||plan.checks.some((name)=>!/^[a-z0-9][\w:.-]*$/i.test(name))||new Set(plan.checks).size!==plan.checks.length)fail("Expected a version 1 plan with sources and named consumer checks");const packages=new Set;for(const source of plan.sources){if(!source||!/^[\w.-]+\/[\w.-]+$/.test(source.repository)||!/^[a-f0-9]{40}$/.test(source.commit)||typeof source.tag!=="string"||!source.tag||!Array.isArray(source.gates)||!source.gates.length||!Array.isArray(source.packages)||!source.packages.length)fail("Each source needs a repository, full commit, tag, release gates and packages");for(const gate of source.gates){if(!gate||!Number.isSafeInteger(gate.runId)||gate.runId<=0||!/^\.github\/workflows\/[\w.-]+\.ya?ml$/.test(gate.workflow)||!nonemptyStrings(gate.requiredJobs)||new Set(gate.requiredJobs).size!==gate.requiredJobs.length)fail("Each release gate needs an exact run, workflow and required jobs")}for(const pkg of source.packages){if(!pkg||!/^(?:@[a-z0-9][\w.-]*\/)?[a-z0-9][\w.-]*$/.test(pkg.name)||!/^\d+\.\d+\.\d+(?:-[\w.-]+)?(?:\+[\w.-]+)?$/.test(pkg.version)||!/^sha512-[A-Za-z0-9+/]{86}==$/.test(pkg.integrity)||packages.has(pkg.name))fail("Packages need unique names, exact versions and original SHA-512 archive identities");packages.add(pkg.name)}}}async function archiveFiles(bytes,limit){const entries=await libraryArchiveEntries(bytes,limit);return new Map([...entries].map(([name,bytes])=>[name,{sha256:hash(bytes),bytes:bytes.length}]))}function installedState(plan,cwd,archives,maxBytes){const lock=readFileSync(join(cwd,"bun.lock"),"utf8");const parsed=Bun.JSONC.parse(lock);const nodeModules=realpathSync(join(cwd,"node_modules"));const manifests=[];for(const source of plan.sources){for(const pkg of source.packages){const locked=parsed.packages?.[pkg.name];if(locked?.[0]!==`${pkg.name}@${pkg.version}`||locked.at(-1)!==pkg.integrity)fail(`Consumer lock does not contain ${pkg.name}@${pkg.version} with the original archive identity`);const directory=realpathSync(join(cwd,"node_modules",pkg.name));const path=relative(nodeModules,directory);if(!path||path.startsWith("..")||resolve(nodeModules,path)!==directory)fail(`${pkg.name} resolves outside the installed consumer packages`);const raw=readFileSync(join(directory,"package.json"),"utf8");const manifest=JSON.parse(raw);if(manifest.name!==pkg.name||manifest.version!==pkg.version)fail(`Installed ${pkg.name} does not match tested version ${pkg.version}`);verifyInstalledFiles(directory,archives.get(pkg.name),{maxBytes},true);manifests.push(hash(raw))}}return{lock,sha256:hash(lock),manifests:manifests.join(":")}}export async function verifyReleaseEvidence(plan,options){validateReleaseEvidencePlan(plan);const requestTimeoutMs=options.requestTimeoutMs??30000;const checkTimeoutMs=options.checkTimeoutMs??300000;const maxArchiveBytes=options.maxArchiveBytes??128*1024*1024;const maxExpandedBytes=options.maxExpandedBytes??128*1024*1024;for(const number of[requestTimeoutMs,checkTimeoutMs,maxArchiveBytes,maxExpandedBytes]){if(!Number.isSafeInteger(number)||number<=0)fail("Budgets must be positive safe integers")}const read=options.fetch??fetch;const archives=new Map;const receipt={schemaVersion:1,verifiedAt:"",sources:[],consumer:{lockfile:"bun.lock",sha256:"",bunVersion:"",checks:[]}};async function request(url,github,limit=1024*1024){const timeout=AbortSignal.timeout(requestTimeoutMs);const signal=options.signal?AbortSignal.any([options.signal,timeout]):timeout;signal.throwIfAborted();const response=await read(url,{method:"GET",redirect:"error",signal,headers:github?{Accept:"application/vnd.github+json",...options.token?{Authorization:`Bearer ${options.token}`}:{}}:{}});if(response.status!==200){await response.body?.cancel();fail(`${github?"Source evidence":"Published package"} request returned HTTP ${response.status}`)}return boundedBody(response,limit)}async function github(path){return JSON.parse(new TextDecoder().decode(await request(`https://api.github.com/repos/${path}`,true)))}for(const source of plan.sources){let ref=(await github(`${source.repository}/git/ref/tags/${encodeURIComponent(source.tag)}`)).object;for(let depth=0;ref?.type==="tag"&&depth<5;depth++)ref=(await github(`${source.repository}/git/tags/${ref.sha}`)).object;if(ref?.type!=="commit"||ref.sha!==source.commit)fail(`${source.tag} does not identify ${source.commit}`);const manifest=await github(`${source.repository}/contents/package.json?ref=${source.commit}`);if(manifest.encoding!=="base64"||typeof manifest.content!=="string")fail("Source package manifest is unavailable");const sourcePackage=JSON.parse(Buffer.from(manifest.content,"base64").toString("utf8"));const releaseScript=sourcePackage.scripts?.["release:patch"];if(typeof releaseScript!=="string"||!releaseScript.trim())fail(`${source.repository} has no existing release:patch command`);for(const gate of source.gates){const run=await github(`${source.repository}/actions/runs/${gate.runId}`);if(run.id!==gate.runId||run.repository?.full_name!==source.repository||run.path!==gate.workflow||run.head_sha!==source.commit)fail(`Release gate ${gate.runId} does not verify the exact source and workflow`);if(run.status!=="completed"||run.conclusion!=="success")fail(`Release gate ${gate.runId} is not successful`);const jobs=[];for(let page=1;page<=10;page++){const batch=await github(`${source.repository}/actions/runs/${gate.runId}/jobs?per_page=100&page=${page}`);if(!Array.isArray(batch.jobs))fail(`Release gate ${gate.runId} has no readable jobs`);jobs.push(...batch.jobs);if(batch.jobs.length<100)break;if(page===10)fail("Release job inventory exceeds its budget")}for(const name of gate.requiredJobs){const matches=jobs.filter((job)=>job.name===name);if(matches.length!==1||matches[0]?.status!=="completed"||matches[0]?.conclusion!=="success")fail(`Required release job ${name} did not pass exactly once`)}}const verified={...source,releaseCommand:"bun run release:patch",releaseScript,packages:[]};for(const pkg of source.packages){const metadata=JSON.parse(new TextDecoder().decode(await request(`https://registry.npmjs.org/${encodeURIComponent(pkg.name)}/${encodeURIComponent(pkg.version)}`,false)));if(metadata.name!==pkg.name||metadata.version!==pkg.version||metadata.dist?.integrity!==pkg.integrity)fail(`Published ${pkg.name}@${pkg.version} does not match the original archive identity`);if(metadata.gitHead!==undefined&&metadata.gitHead!==source.commit)fail(`Published ${pkg.name} names a different source commit`);if(!metadata.dist?.tarball)fail(`Published ${pkg.name} has no archive URL`);const tarball=new URL(metadata.dist.tarball);if(tarball.origin!=="https://registry.npmjs.org"||tarball.username||tarball.password)fail("Archive URL is outside the public npm registry");const bytes=await request(tarball.href,false,maxArchiveBytes);if(`sha512-${createHash("sha512").update(bytes).digest("base64")}`!==pkg.integrity)fail(`Published ${pkg.name}@${pkg.version} archive bytes failed SHA-512 verification`);const files=await archiveFiles(bytes,maxExpandedBytes);archives.set(pkg.name,files);const filesSha256=hash(JSON.stringify([...files].sort(([left],[right])=>left.localeCompare(right))));verified.packages.push({...pkg,tarball:tarball.href,bytes:bytes.byteLength,filesSha256})}receipt.sources.push(verified)}const initial=installedState(plan,options.cwd,archives,maxExpandedBytes);const{runProjectScripts}=await import("./project-scripts.js");const checks=await runProjectScripts({cwd:options.cwd,scripts:plan.checks,signal:options.signal,timeoutMs:checkTimeoutMs});receipt.consumer.bunVersion=checks.bunVersion;receipt.consumer.checks=checks.checks;const final=installedState(plan,options.cwd,archives,maxExpandedBytes);if(final.sha256!==initial.sha256||final.manifests!==initial.manifests)fail("Consumer checks changed the verified lockfile or package identities");receipt.consumer.sha256=initial.sha256;options.signal?.throwIfAborted();receipt.verifiedAt=new Date().toISOString();return receipt}
|
package/dist/release.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{parseOptions}from"@stacksjs/cli";import{app}from"@stacksjs/config";import{Action}from"@stacksjs/enums";import{log}from"@stacksjs/logging";import{projectPath}from"@stacksjs/path";import{assertPublicationAllowed}from"./publication-preflight";import{runActions}from"./helpers/utils";import{resolvePantryToolchain}from"./pantry-toolchain";import{releasePushTarget}from"./release-push-target";import{BYPASS_ENV,formatPreflightFailure,runPinnedChecks}from"./release-preflight";const raw=parseOptions()??{};const passthrough={};for(const[k,v]of Object.entries(raw)){if(k==="--"||k==="_")continue;passthrough[k]=v}const isDryRun=passthrough.dryRun===true||passthrough.dryRun==="true";const actions=isDryRun?[Action.Bump]:[Action.GenerateLibraryEntries,Action.Bump];if(!isDryRun){await assertPublicationAllowed({cwd:projectPath(),channel:"github-release"});const rootManifest=await Bun.file(projectPath("package.json")).json();const framework=rootManifest.name==="stacks"&&await Bun.file(projectPath("storage/framework/core/package.json")).exists();if(framework)releasePushTarget(projectPath(),"main")}const preflight=await runPinnedChecks({cwd:projectPath()});if(preflight.bypassed){log.warn(`${BYPASS_ENV} is set, so the pinned checks were skipped for this release.`)}else if(preflight.failures.length>0){await log.flush();await log.exit(formatPreflightFailure(preflight.failures),1)}const pantryToolchain=resolvePantryToolchain({cwd:projectPath()});if(!pantryToolchain.ok){await log.flush();await log.exit(pantryToolchain.message,1)}else{log.info(`Pantry ${pantryToolchain.version} (${pantryToolchain.bin})${pantryToolchain.declared?` satisfies ts-pantry ${pantryToolchain.declared}`:""}`)}const result=await runActions(actions,{cwd:projectPath(),...passthrough});if(result&&result.isErr){await log.exit(`Release failed: ${result.error?.message??String(result.error)}`,1)}log.success(isDryRun?`Dry run complete for ${app.name}. Nothing was committed, tagged or pushed.`:`Successfully released ${app.name}`);
|
|
1
|
+
import{planNativeLibraryRelease}from"./library/release-plan";import{selectNativeLibraryReleaseVersion}from"./library/release-selection";import{parseOptions}from"@stacksjs/cli";import{app}from"@stacksjs/config";import{Action}from"@stacksjs/enums";import{log}from"@stacksjs/logging";import{projectPath}from"@stacksjs/path";import{assertPublicationAllowed}from"./publication-preflight";import{runActions}from"./helpers/utils";import{resolvePantryToolchain}from"./pantry-toolchain";import{releasePushTarget}from"./release-push-target";import{BYPASS_ENV,formatPreflightFailure,runPinnedChecks}from"./release-preflight";const raw=parseOptions()??{};const passthrough={};for(const[k,v]of Object.entries(raw)){if(k==="--"||k==="_")continue;passthrough[k]=v}const isDryRun=passthrough.dryRun===true||passthrough.dryRun==="true";const manifest=await Bun.file(projectPath("package.json")).json();const frameworkRelease=manifest.name==="stacks"&&await Bun.file(projectPath("storage/framework/core/package.json")).exists();if(!frameworkRelease&&!passthrough.bump){const selected=await selectNativeLibraryReleaseVersion({cwd:projectPath(),preid:passthrough.preid?.toString()});if(selected)passthrough.bump=selected}const libraryPlan=frameworkRelease?undefined:await planNativeLibraryRelease({cwd:projectPath(),validateWorkflow:true,bump:passthrough.bump?.toString(),preid:passthrough.preid?.toString()});if(libraryPlan){releasePushTarget(projectPath(),libraryPlan.sourceCi.branch);passthrough.skipGithubRelease=true}const actions=isDryRun?[Action.Bump]:[Action.GenerateLibraryEntries,Action.Bump];if(!isDryRun){await assertPublicationAllowed({cwd:projectPath(),channel:"github-release"});const rootManifest=await Bun.file(projectPath("package.json")).json();const framework=rootManifest.name==="stacks"&&await Bun.file(projectPath("storage/framework/core/package.json")).exists();if(framework)releasePushTarget(projectPath(),"main")}const preflight=await runPinnedChecks({cwd:projectPath()});if(preflight.bypassed){log.warn(`${BYPASS_ENV} is set, so the pinned checks were skipped for this release.`)}else if(preflight.failures.length>0){await log.flush();await log.exit(formatPreflightFailure(preflight.failures),1)}const pantryToolchain=resolvePantryToolchain({cwd:projectPath()});if(!pantryToolchain.ok){await log.flush();await log.exit(pantryToolchain.message,1)}else{log.info(`Pantry ${pantryToolchain.version} (${pantryToolchain.bin})${pantryToolchain.declared?` satisfies ts-pantry ${pantryToolchain.declared}`:""}`)}const result=await runActions(actions,{cwd:projectPath(),...passthrough});if(result&&result.isErr){await log.exit(`Release failed: ${result.error?.message??String(result.error)}`,1)}log.success(isDryRun?`Dry run complete for ${app.name}. Nothing was committed, tagged or pushed.`:`Successfully released ${app.name}`);
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "@stacksjs/actions",
|
|
3
3
|
"type": "module",
|
|
4
4
|
"sideEffects": false,
|
|
5
|
-
"version": "0.75.
|
|
5
|
+
"version": "0.75.597",
|
|
6
6
|
"description": "The Stacks actions.",
|
|
7
7
|
"author": "Chris Breuer",
|
|
8
8
|
"contributors": [
|
|
@@ -66,56 +66,58 @@
|
|
|
66
66
|
"prepublishOnly": "bun run build"
|
|
67
67
|
},
|
|
68
68
|
"dependencies": {
|
|
69
|
-
"@
|
|
70
|
-
"@
|
|
71
|
-
"@stacksjs/
|
|
72
|
-
"@stacksjs/
|
|
73
|
-
"@stacksjs/
|
|
74
|
-
"@stacksjs/
|
|
69
|
+
"@babel/parser": "^8.1.2",
|
|
70
|
+
"@babel/types": "^8.1.2",
|
|
71
|
+
"@stacksjs/action-runner": "0.75.597",
|
|
72
|
+
"@stacksjs/ai": "0.75.597",
|
|
73
|
+
"@stacksjs/analytics": "0.75.597",
|
|
74
|
+
"@stacksjs/api": "0.75.597",
|
|
75
|
+
"@stacksjs/auth": "0.75.597",
|
|
76
|
+
"@stacksjs/build": "0.75.597",
|
|
75
77
|
"@stacksjs/bumpx": "^0.2.26",
|
|
76
78
|
"@stacksjs/bunpress": "^0.2.15",
|
|
77
|
-
"@stacksjs/cli": "0.75.
|
|
78
|
-
"@stacksjs/cloud": "0.75.
|
|
79
|
-
"@stacksjs/cms": "0.75.
|
|
80
|
-
"@stacksjs/components": "^0.2.
|
|
81
|
-
"@stacksjs/config": "0.75.
|
|
82
|
-
"@stacksjs/database": "0.75.
|
|
83
|
-
"@stacksjs/desktop-build": "0.75.
|
|
84
|
-
"@stacksjs/dns": "0.75.
|
|
85
|
-
"@stacksjs/docs": "0.75.
|
|
86
|
-
"@stacksjs/enums": "0.75.
|
|
87
|
-
"@stacksjs/env": "0.75.
|
|
88
|
-
"@stacksjs/error-handling": "0.75.
|
|
89
|
-
"@stacksjs/github": "0.75.
|
|
79
|
+
"@stacksjs/cli": "0.75.597",
|
|
80
|
+
"@stacksjs/cloud": "0.75.597",
|
|
81
|
+
"@stacksjs/cms": "0.75.597",
|
|
82
|
+
"@stacksjs/components": "^0.2.889",
|
|
83
|
+
"@stacksjs/config": "0.75.597",
|
|
84
|
+
"@stacksjs/database": "0.75.597",
|
|
85
|
+
"@stacksjs/desktop-build": "0.75.597",
|
|
86
|
+
"@stacksjs/dns": "0.75.597",
|
|
87
|
+
"@stacksjs/docs": "0.75.597",
|
|
88
|
+
"@stacksjs/enums": "0.75.597",
|
|
89
|
+
"@stacksjs/env": "0.75.597",
|
|
90
|
+
"@stacksjs/error-handling": "0.75.597",
|
|
91
|
+
"@stacksjs/github": "0.75.597",
|
|
90
92
|
"@stacksjs/gitit": "^0.2.5",
|
|
91
|
-
"@stacksjs/i18n": "0.75.
|
|
92
|
-
"@stacksjs/image": "0.75.
|
|
93
|
-
"@stacksjs/logging": "0.75.
|
|
93
|
+
"@stacksjs/i18n": "0.75.597",
|
|
94
|
+
"@stacksjs/image": "0.75.597",
|
|
95
|
+
"@stacksjs/logging": "0.75.597",
|
|
94
96
|
"@stacksjs/logsmith": "^0.2.11",
|
|
95
|
-
"@stacksjs/mobile": "0.75.
|
|
96
|
-
"@stacksjs/orm": "0.75.
|
|
97
|
-
"@stacksjs/path": "0.75.
|
|
98
|
-
"@stacksjs/payments": "0.75.
|
|
99
|
-
"@stacksjs/queue": "0.75.
|
|
100
|
-
"@stacksjs/registry": "0.75.
|
|
101
|
-
"@stacksjs/router": "0.75.
|
|
97
|
+
"@stacksjs/mobile": "0.75.597",
|
|
98
|
+
"@stacksjs/orm": "0.75.597",
|
|
99
|
+
"@stacksjs/path": "0.75.597",
|
|
100
|
+
"@stacksjs/payments": "0.75.597",
|
|
101
|
+
"@stacksjs/queue": "0.75.597",
|
|
102
|
+
"@stacksjs/registry": "0.75.597",
|
|
103
|
+
"@stacksjs/router": "0.75.597",
|
|
102
104
|
"@stacksjs/rpx": "^0.11.54",
|
|
103
|
-
"@stacksjs/scheduler": "0.75.
|
|
104
|
-
"@stacksjs/search-engine": "0.75.
|
|
105
|
-
"@stacksjs/security": "0.75.
|
|
106
|
-
"@stacksjs/server": "0.75.
|
|
107
|
-
"@stacksjs/sites": "0.75.
|
|
108
|
-
"@stacksjs/storage": "0.75.
|
|
109
|
-
"@stacksjs/strings": "0.75.
|
|
110
|
-
"@stacksjs/stx": "^0.2.
|
|
105
|
+
"@stacksjs/scheduler": "0.75.597",
|
|
106
|
+
"@stacksjs/search-engine": "0.75.597",
|
|
107
|
+
"@stacksjs/security": "0.75.597",
|
|
108
|
+
"@stacksjs/server": "0.75.597",
|
|
109
|
+
"@stacksjs/sites": "0.75.597",
|
|
110
|
+
"@stacksjs/storage": "0.75.597",
|
|
111
|
+
"@stacksjs/strings": "0.75.597",
|
|
112
|
+
"@stacksjs/stx": "^0.2.889",
|
|
111
113
|
"@stacksjs/tlsx": "^0.13.19",
|
|
112
114
|
"@stacksjs/ts-cloud": "^0.16.129",
|
|
113
115
|
"@stacksjs/ts-md": "^0.1.1",
|
|
114
|
-
"@stacksjs/tunnel": "0.75.
|
|
115
|
-
"@stacksjs/types": "0.75.
|
|
116
|
-
"@stacksjs/utils": "0.75.
|
|
117
|
-
"@stacksjs/whois": "0.75.
|
|
118
|
-
"bun-plugin-stx": "^0.2.
|
|
116
|
+
"@stacksjs/tunnel": "0.75.597",
|
|
117
|
+
"@stacksjs/types": "0.75.597",
|
|
118
|
+
"@stacksjs/utils": "0.75.597",
|
|
119
|
+
"@stacksjs/whois": "0.75.597",
|
|
120
|
+
"bun-plugin-stx": "^0.2.889",
|
|
119
121
|
"pickier": "^0.1.92"
|
|
120
122
|
},
|
|
121
123
|
"peerDependencies": {
|
|
@@ -127,7 +129,7 @@
|
|
|
127
129
|
}
|
|
128
130
|
},
|
|
129
131
|
"devDependencies": {
|
|
130
|
-
"@stacksjs/validation": "0.75.
|
|
132
|
+
"@stacksjs/validation": "0.75.597",
|
|
131
133
|
"better-dx": "^0.2.39"
|
|
132
134
|
}
|
|
133
135
|
}
|