From 282876da7beee7f17dd2835f6a0f38c4dc936556 Mon Sep 17 00:00:00 2001 From: Joey Wunderlich Date: Thu, 20 Jul 2023 13:19:25 -0700 Subject: [PATCH] Local serve share page quick fixes (#9610) * 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 --- cli/server.ts | 9 +++++---- pxtlib/emitter/cloud.ts | 2 +- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/cli/server.ts b/cli/server.ts index a01f12accbc7..8a269125d586 100644 --- a/cli/server.ts +++ b/cli/server.ts @@ -825,7 +825,7 @@ function certificateTestAsync(): Promise { // 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 @@ -854,7 +854,7 @@ function scriptPageTestAsync(id: string) { filepath: "/" + id }) return html - }) + }); } // use http://localhost:3232/pkg/microsoft/pxt-neopixel for testing @@ -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 } diff --git a/pxtlib/emitter/cloud.ts b/pxtlib/emitter/cloud.ts index 58718dd184de..a5b5e38c34ce 100644 --- a/pxtlib/emitter/cloud.ts +++ b/pxtlib/emitter/cloud.ts @@ -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 **/;