Skip to content

Commit ff064c4

Browse files
refactor(canvas): migrate shared extension infrastructure (#52)
<!-- Use a conventional commit-style title, such as "fix(hub): retain prototype history". --> ## Why <!-- Explain the problem and why this change matters in a short, plain-English paragraph. Link the issue when applicable. --> Continue the canvas extension's TypeScript migration on top of #51. Shared command, storage, and repository helpers need compiler-checked implementations before the remaining workflows can migrate without relying on declarations for JavaScript behavior. ## What changes <!-- Describe what becomes different for a first-time reader. Explain necessary terms. Include accurate Mermaid diagrams when they clarify the proposal, using before/after diagrams when helpful. Add screenshots for meaningful UI changes. --> <!-- Omit validation sections, checklists, and routine test/CI summaries such as "CI is green" or "tests passed". Mention only known verification gaps that affect the change, not disclaimers about drafting this description. --> Move command execution, private storage, state-file reads and writes, and repository identity and context to strict TypeScript sources. Remove the temporary command declaration and share a checked error-code reader. Parsed storage data remains unknown until its consuming workflow checks the fields it uses; this change does not introduce a new stored-data format. Keep the generated runnable `.mjs` modules committed, with the same host entry point and no compiler or package installation required for plugin consumers. The module build now formats every compiler-emitted runtime file rather than naming a single module. Reuse Hub's locked Node types and document the migration boundary. Hub clients, publishing and review workflows, servers, browser assets, and SDK registration remain outside this layer. --------- Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: 9d7306a9-9831-4c78-89e7-3c5b581c895e Copilot-Session: 1aff284a-77f1-4bcf-8245-e30889a2f392
1 parent 1f4670d commit ff064c4

23 files changed

Lines changed: 842 additions & 88 deletions

File tree

‎.github/extensions/replica-prototype/create-canvas-context/index.mjs‎

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
import { createHash } from "node:crypto"
22
import path from "node:path"
3-
43
import { readRepositoryName } from "../read-repository-name/index.mjs"
54
import { runCommand } from "../run-command/index.mjs"
6-
75
export async function createCanvasContext({
86
prototypeUrl,
97
name,
@@ -25,7 +23,6 @@ export async function createCanvasContext({
2523
"This Copilot session has no working directory, so repository status cannot be read.",
2624
)
2725
}
28-
2926
const normalizedUrl = normalizePrototypeUrl(prototypeUrl)
3027
const stateUrl = new URL(normalizedUrl)
3128
stateUrl.hash = ""
@@ -50,7 +47,6 @@ export async function createCanvasContext({
5047
.digest("hex")
5148
.slice(0, 24)
5249
const stateDirectory = path.join(sessionWorkspacePath, "replica-prototype")
53-
5450
return {
5551
name: normalizeName(name, normalizedUrl),
5652
normalizedUrl,
@@ -64,7 +60,6 @@ export async function createCanvasContext({
6460
urlHost: new URL(normalizedUrl).host,
6561
}
6662
}
67-
6863
// Keep stored publish state tied to the branch currently shown in the canvas.
6964
export async function currentCanvasContext(context) {
7065
const [repository, branch] = await Promise.all([
@@ -81,7 +76,6 @@ export async function currentCanvasContext(context) {
8176
const currentBranch = branch.ok ? branch.stdout.replace(/\r?\n$/, "") || null : null
8277
if (currentRepository === context.target.repository && currentBranch === context.target.branch)
8378
return context
84-
8579
return createCanvasContext({
8680
prototypeUrl: context.normalizedUrl,
8781
name: context.name,
@@ -93,7 +87,6 @@ export async function currentCanvasContext(context) {
9387
repositoryPath: context.repositoryPath,
9488
})
9589
}
96-
9790
function normalizePrototypeUrl(value) {
9891
let url
9992
try {
@@ -104,29 +97,24 @@ function normalizePrototypeUrl(value) {
10497
if (url.protocol !== "http:" && url.protocol !== "https:") {
10598
throw new Error("prototypeUrl must use HTTP or HTTPS.")
10699
}
107-
108100
url.hostname = url.hostname.toLowerCase()
109101
if (
110102
(url.protocol === "http:" && url.port === "80") ||
111103
(url.protocol === "https:" && url.port === "443")
112104
) {
113105
url.port = ""
114106
}
115-
116107
return url.toString()
117108
}
118-
119109
function normalizeName(name, normalizedUrl) {
120110
if (name === undefined) {
121111
return new URL(normalizedUrl).hostname
122112
}
123113
if (typeof name !== "string" || name.trim() === "") {
124114
throw new Error("name must contain visible text.")
125115
}
126-
127116
return name.trim()
128117
}
129-
130118
async function resolvePublishTarget({
131119
repository,
132120
branch,
@@ -147,15 +135,13 @@ async function resolvePublishTarget({
147135
if (branch != null && branchResult.ok && hintedBranch !== checkoutBranch) {
148136
throw new Error("branch does not match the current checkout.")
149137
}
150-
151138
return {
152139
repository: resolvedRepository,
153140
branch: hintedBranch ?? checkoutBranch,
154141
commitSha: normalizeCommitSha(commitSha),
155142
publishedUrl: normalizeOptionalUrl(publishedUrl, "publishedUrl"),
156143
}
157144
}
158-
159145
export async function resolveRepository(hint, repositoryPath) {
160146
const repository = normalizeRepository(hint)
161147
const checkout = await readRepositoryName(repositoryPath)
@@ -174,7 +160,6 @@ export async function resolveRepository(hint, repositoryPath) {
174160
}
175161
return repository ?? checkoutRepository
176162
}
177-
178163
function isLocalRepositoryCheckout(repository, repositoryPath) {
179164
const localRoot = path.dirname(repositoryPath)
180165
return (
@@ -183,7 +168,6 @@ function isLocalRepositoryCheckout(repository, repositoryPath) {
183168
path.basename(path.dirname(localRoot)) === ".local"
184169
)
185170
}
186-
187171
function normalizeRepository(value) {
188172
if (value == null) {
189173
return null
@@ -206,10 +190,8 @@ function normalizeRepository(value) {
206190
) {
207191
throw new Error("repository must use GitHub owner/name format.")
208192
}
209-
210193
return value.toLowerCase()
211194
}
212-
213195
export async function normalizeBranch(value, repositoryPath) {
214196
if (value === "HEAD") return null
215197
if (value == null) {
@@ -235,23 +217,19 @@ export async function normalizeBranch(value, repositoryPath) {
235217
}
236218
return value
237219
}
238-
239220
function normalizeCommitSha(value) {
240221
if (value == null) {
241222
return null
242223
}
243224
if (typeof value !== "string" || !/^[0-9a-f]{40}$/.test(value)) {
244225
throw new Error("commitSha must be a full lowercase Git commit SHA.")
245226
}
246-
247227
return value
248228
}
249-
250229
function normalizeOptionalUrl(value, name) {
251230
if (value == null) {
252231
return null
253232
}
254-
255233
let url
256234
try {
257235
url = new URL(value)
@@ -261,6 +239,5 @@ function normalizeOptionalUrl(value, name) {
261239
if (url.protocol !== "http:" && url.protocol !== "https:") {
262240
throw new Error(`${name} must use HTTP or HTTPS.`)
263241
}
264-
265242
return url.toString()
266243
}

0 commit comments

Comments
 (0)