From c04dac614e9033bf65eec8ba4eaf46db20497ecf Mon Sep 17 00:00:00 2001 From: Joey Wunderlich Date: Tue, 18 Jul 2023 10:54:55 -0700 Subject: [PATCH 1/3] handle leading slash in share id so location.pathname works --- pxtlib/emitter/cloud.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 **/; From bab2d53e806aa3d9a88d0a832d9104981381702a Mon Sep 17 00:00:00 2001 From: Joey Wunderlich Date: Tue, 18 Jul 2023 10:56:37 -0700 Subject: [PATCH 2/3] handle S links, _asdfasdfasfd in local serve --- cli/server.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cli/server.ts b/cli/server.ts index a01f12accbc7..0cb0659bfbcc 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 @@ -1140,8 +1140,8 @@ 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) return } From bbb33afe89f2aa1fdc91eb9fc337b85462bc3ad9 Mon Sep 17 00:00:00 2001 From: Joey Wunderlich Date: Tue, 18 Jul 2023 11:15:18 -0700 Subject: [PATCH 3/3] catch invalid url so it doesn't crash serve --- cli/server.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cli/server.ts b/cli/server.ts index 0cb0659bfbcc..8a269125d586 100644 --- a/cli/server.ts +++ b/cli/server.ts @@ -854,7 +854,7 @@ function scriptPageTestAsync(id: string) { filepath: "/" + id }) return html - }) + }); } // use http://localhost:3232/pkg/microsoft/pxt-neopixel for testing @@ -1143,6 +1143,7 @@ export function serveAsync(options: ServeOptions) { if (!!pxt.Cloud.parseScriptId(pathname)) { scriptPageTestAsync(pathname) .then(sendHtml) + .catch(() => error(404, "Script not found")); return }