Skip to content

Commit

Permalink
Local serve share page quick fixes (#9610)
Browse files Browse the repository at this point in the history
* handle leading slash in share id so location.pathname works

* handle S links, _asdfasdfasfd in local serve

* catch invalid url so it doesn't crash serve
  • Loading branch information
jwunderl authored Jul 20, 2023
1 parent 59057c7 commit 282876d
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
9 changes: 5 additions & 4 deletions cli/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -825,7 +825,7 @@ function certificateTestAsync(): Promise<string> {

// use http://localhost:3232/45912-50568-62072-42379 for testing
function scriptPageTestAsync(id: string) {
return Cloud.privateGetAsync(id)
return Cloud.privateGetAsync(pxt.Cloud.parseScriptId(id))
.then((info: Cloud.JsonScript) => {
// if running against old cloud, infer 'thumb' field
// can be removed after new cloud deployment
Expand Down Expand Up @@ -854,7 +854,7 @@ function scriptPageTestAsync(id: string) {
filepath: "/" + id
})
return html
})
});
}

// use http://localhost:3232/pkg/microsoft/pxt-neopixel for testing
Expand Down Expand Up @@ -1140,9 +1140,10 @@ export function serveAsync(options: ServeOptions) {
return
}

if (/^\/(\d\d\d\d[\d-]+)$/.test(pathname)) {
scriptPageTestAsync(pathname.slice(1))
if (!!pxt.Cloud.parseScriptId(pathname)) {
scriptPageTestAsync(pathname)
.then(sendHtml)
.catch(() => error(404, "Script not found"));
return
}

Expand Down
2 changes: 1 addition & 1 deletion pxtlib/emitter/cloud.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ namespace pxt.Cloud {
const domainCheck = `(?:(?:https:\/\/)?(?:${domains.join('|')})\/)`;
const versionRefCheck = "(?:v[0-9]+\/)";
const oembedCheck = "api\/oembed\\?url=.*%2F([^&#]*)&.*";
const sharePageCheck = "([a-z0-9\\-_]+)(?:[#?&].*)?";
const sharePageCheck = "\/?([a-z0-9\\-_]+)(?:[#?&].*)?";
const scriptIdCheck = `^${domainCheck}?${versionRefCheck}?(?:(?:${oembedCheck})|(?:${sharePageCheck}))$`;
const m = new RegExp(scriptIdCheck, 'i').exec(uri.trim());
const scriptid = m?.[1] /** oembed res **/ || m?.[2] /** share page res **/;
Expand Down

0 comments on commit 282876d

Please sign in to comment.