From 8084ec9626e8795eb05424e5050ec6d9e24abf02 Mon Sep 17 00:00:00 2001 From: ryohiy Date: Wed, 6 Aug 2025 23:25:38 +0900 Subject: [PATCH 1/2] fix: replace deprecated path-match with path-to-regexp --- package.json | 2 +- pnpm-lock.yaml | 42 +++++++----------------------------------- src/runtime-server.ts | 16 ++++++++++------ 3 files changed, 18 insertions(+), 42 deletions(-) diff --git a/package.json b/package.json index 7ea40a1..aaefeb1 100644 --- a/package.json +++ b/package.json @@ -26,7 +26,7 @@ "micro": "9.3.5-canary.3", "ms": "2.1.1", "node-fetch": "2.6.7", - "path-match": "1.2.4", + "path-to-regexp": "^8.2.0", "promisepipe": "3.0.0", "semver": "7.5.4", "stat-mode": "0.3.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 9ba1c3f..0a8d2fd 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -29,9 +29,9 @@ importers: node-fetch: specifier: 2.6.7 version: 2.6.7 - path-match: - specifier: 1.2.4 - version: 1.2.4 + path-to-regexp: + specifier: ^8.2.0 + version: 8.2.0 promisepipe: specifier: 3.0.0 version: 3.0.0 @@ -1145,10 +1145,6 @@ packages: html-escaper@2.0.2: resolution: {integrity: sha512-H2iMtd0I4Mt5eYiapRdIDjp+XzelXQ0tFE4JS7YFwFevXXMmOp9myNrUvCg0D6ws8iqkRPBfKHgbwig1SmlLfg==} - http-errors@1.4.0: - resolution: {integrity: sha512-oLjPqve1tuOl5aRhv8GK5eHpqP1C9fb+Ol+XTLjKfLltE44zdDbEdjPSbU7Ch5rSNsVFqZn97SrMmZLdu1/YMw==} - engines: {node: '>= 0.6'} - http-errors@1.7.3: resolution: {integrity: sha512-ZTTX0MWrsQ2ZAhA1cejAwDLycFsd7I7nVtnkT3Ol0aqodaKW+0CTZDQ1uBv5whptCnc8e8HeRRJxRs0kmm/Qfw==} engines: {node: '>= 0.6'} @@ -1196,9 +1192,6 @@ packages: resolution: {integrity: sha512-k92I/b08q4wvFscXCLvqfsHCrjrF7yiXsQuIVvVE7N82W3+aqpzuUdBbfhWcy/FZR3/4IgflMgKLOsvPDrGCJA==} deprecated: This module is not supported, and leaks memory. Do not use it. Check out lru-cache if you want a good and tested way to coalesce async requests by a key value, which is much more comprehensive and powerful. - inherits@2.0.1: - resolution: {integrity: sha512-8nWq2nLTAwd02jTqJExUYFSD/fKq6VH9Y/oG2accc/kdI0V98Bag8d5a4gi3XHz73rDWa2PvTtvcWYquKqSENA==} - inherits@2.0.4: resolution: {integrity: sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==} @@ -1311,9 +1304,6 @@ packages: resolution: {integrity: sha512-eXK1UInq2bPmjyX6e3VHIzMLobc4J94i4AWn+Hpq3OU5KkrRC96OAcR3PRJ/pGu6m8TRnBHP9dkXQVsT/COVIA==} engines: {node: '>=0.10.0'} - isarray@0.0.1: - resolution: {integrity: sha512-D2S+3GLxWH+uhrNEcoh/fnmYeP8E8/zHl644d/jdA0g2uyXvy3sb0qxotE+ne0LtccHknQzWwZEzhak7oJ0COQ==} - isarray@1.0.0: resolution: {integrity: sha512-VLghIWNM6ELQzo7zwmcg0NmTVyWKYjvIeM83yjp0wRDTmUnrM678fQbcKBo6n2CJEF0szoG//ytg+TKla89ALQ==} @@ -1700,14 +1690,12 @@ packages: resolution: {integrity: sha512-ojmeN0qd+y0jszEtoY48r0Peq5dwMEkIlCOu6Q5f41lfkswXuKtYrhgoTpLnyIcHm24Uhqx+5Tqm2InSwLhE6Q==} engines: {node: '>=8'} - path-match@1.2.4: - resolution: {integrity: sha512-UWlehEdqu36jmh4h5CWJ7tARp1OEVKGHKm6+dg9qMq5RKUTV5WJrGgaZ3dN2m7WFAXDbjlHzvJvL/IUpy84Ktw==} - path-parse@1.0.7: resolution: {integrity: sha512-LDJzPVEEEPR+y48z93A0Ed0yXb8pAByGWo/k5YYdYgpY2/2EsOsksJrq7lOHxryrVOn1ejG6oAp8ahvOIQD8sw==} - path-to-regexp@1.9.0: - resolution: {integrity: sha512-xIp7/apCFJuUHdDLWe8O1HIkb0kQrOMb/0u6FXQjemHn/ii5LrIzU6bdECnsiTF/GjZkMEKg1xdiZwNqDYlZ6g==} + path-to-regexp@8.2.0: + resolution: {integrity: sha512-TdrF7fW9Rphjq4RjrW0Kp2AW0Ahwu9sRGTkS6bvDi0SCwZlEZYmcfDbEsTz8RVk0EHIS/Vd1bv3JhG+1xZuAyQ==} + engines: {node: '>=16'} path-type@3.0.0: resolution: {integrity: sha512-T2ZUsdZFHgA3u4e5PfPbjd7HDDpxPnQb5jN0SrDsjNSuVXHJqtwTnWqG0B1jZrgmJ/7lj1EmVIByWt1gxGkWvg==} @@ -3372,11 +3360,6 @@ snapshots: html-escaper@2.0.2: {} - http-errors@1.4.0: - dependencies: - inherits: 2.0.1 - statuses: 1.5.0 - http-errors@1.7.3: dependencies: depd: 1.1.2 @@ -3428,8 +3411,6 @@ snapshots: once: 1.4.0 wrappy: 1.0.2 - inherits@2.0.1: {} - inherits@2.0.4: {} is-accessor-descriptor@1.0.1: @@ -3514,8 +3495,6 @@ snapshots: is-windows@1.0.2: {} - isarray@0.0.1: {} - isarray@1.0.0: {} isarray@2.0.1: {} @@ -3968,16 +3947,9 @@ snapshots: path-key@3.1.1: {} - path-match@1.2.4: - dependencies: - http-errors: 1.4.0 - path-to-regexp: 1.9.0 - path-parse@1.0.7: {} - path-to-regexp@1.9.0: - dependencies: - isarray: 0.0.1 + path-to-regexp@8.2.0: {} path-type@3.0.0: dependencies: diff --git a/src/runtime-server.ts b/src/runtime-server.ts index 1faabf3..b6f9dd4 100644 --- a/src/runtime-server.ts +++ b/src/runtime-server.ts @@ -3,14 +3,13 @@ import { parse } from 'node:url'; import createDebug from 'debug'; import { run, text } from 'micro'; import { randomUUID as uuid } from 'node:crypto'; -import createPathMatch from 'path-match'; +import { match } from 'path-to-regexp'; import once from '@tootallnate/once'; import { createDeferred, Deferred } from './deferred'; import { Lambda, InvokeParams, InvokeResult } from './types'; -const pathMatch = createPathMatch(); -const match = pathMatch('/:version/runtime/:subject/:target/:action?'); +const matchFn = match('/:version/runtime/:subject/:target{/:action}'); const debug = createDebug('@vercel/fun:runtime-server'); function send404(res: http.ServerResponse) { @@ -52,12 +51,17 @@ export class RuntimeServer extends Server { ): Promise { debug('%s %s', req.method, req.url); - const params = match(parse(req.url).pathname); - if (!params) { + const result = matchFn(parse(req.url).pathname); + if (!result) { return send404(res); } - const { version, subject, target, action } = params; + const { version, subject, target, action } = result.params as { + version: string; + subject: string; + target: string; + action?: string; + }; if (this.version !== version) { debug( 'Invalid API version, expected %o but got %o', From 28b72f8168143e96506bf0f117281fd3e8c8cd6a Mon Sep 17 00:00:00 2001 From: ryohiy Date: Wed, 6 Aug 2025 23:52:56 +0900 Subject: [PATCH 2/2] refactor: add type safety to path matching with generics , exec prettier --- package.json | 2 +- pnpm-lock.yaml | 2 +- src/runtime-server.ts | 14 +++++++------- test/test.ts | 2 +- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/package.json b/package.json index aaefeb1..ca9da7b 100644 --- a/package.json +++ b/package.json @@ -26,7 +26,7 @@ "micro": "9.3.5-canary.3", "ms": "2.1.1", "node-fetch": "2.6.7", - "path-to-regexp": "^8.2.0", + "path-to-regexp": "8.2.0", "promisepipe": "3.0.0", "semver": "7.5.4", "stat-mode": "0.3.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 0a8d2fd..ecff871 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -30,7 +30,7 @@ importers: specifier: 2.6.7 version: 2.6.7 path-to-regexp: - specifier: ^8.2.0 + specifier: 8.2.0 version: 8.2.0 promisepipe: specifier: 3.0.0 diff --git a/src/runtime-server.ts b/src/runtime-server.ts index b6f9dd4..634bf8b 100644 --- a/src/runtime-server.ts +++ b/src/runtime-server.ts @@ -9,7 +9,12 @@ import once from '@tootallnate/once'; import { createDeferred, Deferred } from './deferred'; import { Lambda, InvokeParams, InvokeResult } from './types'; -const matchFn = match('/:version/runtime/:subject/:target{/:action}'); +const matchFn = match<{ + version: string; + subject: string; + target: string; + action?: string; +}>('/:version/runtime/:subject/:target{/:action}'); const debug = createDebug('@vercel/fun:runtime-server'); function send404(res: http.ServerResponse) { @@ -56,12 +61,7 @@ export class RuntimeServer extends Server { return send404(res); } - const { version, subject, target, action } = result.params as { - version: string; - subject: string; - target: string; - action?: string; - }; + const { version, subject, target, action } = result.params; if (this.version !== version) { debug( 'Invalid API version, expected %o but got %o', diff --git a/test/test.ts b/test/test.ts index 746e96a..4602b5f 100644 --- a/test/test.ts +++ b/test/test.ts @@ -813,4 +813,4 @@ it( assert(/^zeit-fun-/.test(basename(env.LAMBDA_TASK_ROOT))); } ) -); \ No newline at end of file +);