|
1 | 1 | import { describe, expect, it } from 'vitest' |
2 | | -import { getAllProxyConfigs } from '../../src/first-party' |
| 2 | +import { generatePartytownResolveUrl, getAllProxyConfigs } from '../../src/first-party' |
3 | 3 | import { rewriteScriptUrlsAST } from '../../src/plugins/rewrite-ast' |
4 | 4 |
|
5 | 5 | const rewrites = [ |
@@ -232,4 +232,121 @@ describe('rewriteScriptUrlsAST', () => { |
232 | 232 | expect(rewrite('navigator.sendBeacon("https://example.com/collect", data)')).toContain('__nuxtScripts.sendBeacon') |
233 | 233 | }) |
234 | 234 | }) |
| 235 | + |
| 236 | + describe('skipApiRewrites (partytown mode)', () => { |
| 237 | + function rewritePartytown(code: string): string { |
| 238 | + return rewriteScriptUrlsAST(code, 'test.js', rewrites, undefined, { skipApiRewrites: true }) |
| 239 | + } |
| 240 | + |
| 241 | + it('still rewrites URL string literals', () => { |
| 242 | + expect(rewritePartytown('"https://example.com/api"')).toContain('self.location.origin+"/_proxy/ex/api"') |
| 243 | + }) |
| 244 | + |
| 245 | + it('skips fetch rewriting', () => { |
| 246 | + const result = rewritePartytown('fetch("https://example.com/api")') |
| 247 | + expect(result).not.toContain('__nuxtScripts') |
| 248 | + expect(result).toContain('fetch(') |
| 249 | + // URL literal is still rewritten |
| 250 | + expect(result).toContain('self.location.origin+"/_proxy/ex/api"') |
| 251 | + }) |
| 252 | + |
| 253 | + it('skips navigator.sendBeacon rewriting', () => { |
| 254 | + const result = rewritePartytown('navigator.sendBeacon("https://example.com/collect", data)') |
| 255 | + expect(result).not.toContain('__nuxtScripts') |
| 256 | + expect(result).toContain('navigator.sendBeacon(') |
| 257 | + }) |
| 258 | + |
| 259 | + it('skips XMLHttpRequest rewriting', () => { |
| 260 | + const result = rewritePartytown('var a = new XMLHttpRequest();') |
| 261 | + expect(result).not.toContain('__nuxtScripts') |
| 262 | + expect(result).toContain('new XMLHttpRequest()') |
| 263 | + }) |
| 264 | + |
| 265 | + it('skips Image rewriting', () => { |
| 266 | + const result = rewritePartytown('var img = new Image();') |
| 267 | + expect(result).not.toContain('__nuxtScripts') |
| 268 | + expect(result).toContain('new Image()') |
| 269 | + }) |
| 270 | + |
| 271 | + it('skips canvas toDataURL neutralization', () => { |
| 272 | + const result = rewritePartytown('ctx.canvas.toDataURL()') |
| 273 | + expect(result).not.toContain('data:image/png') |
| 274 | + expect(result).toContain('toDataURL()') |
| 275 | + }) |
| 276 | + }) |
| 277 | +}) |
| 278 | + |
| 279 | +describe('generatePartytownResolveUrl', () => { |
| 280 | + it('generates a valid function string', () => { |
| 281 | + const rules = [ |
| 282 | + { pattern: 'www.google-analytics.com', pathPrefix: '', target: '/_scripts/p/ga' }, |
| 283 | + ] |
| 284 | + const fn = generatePartytownResolveUrl(rules) |
| 285 | + expect(fn).toContain('function(url, location, type)') |
| 286 | + expect(fn).toContain('www.google-analytics.com') |
| 287 | + expect(fn).toContain('/_scripts/p/ga') |
| 288 | + }) |
| 289 | + |
| 290 | + it('embeds all rules as JSON', () => { |
| 291 | + const rules = [ |
| 292 | + { pattern: 'www.google-analytics.com', pathPrefix: '', target: '/_scripts/p/ga' }, |
| 293 | + { pattern: 'www.facebook.com', pathPrefix: '/tr', target: '/_scripts/p/meta' }, |
| 294 | + ] |
| 295 | + const fn = generatePartytownResolveUrl(rules) |
| 296 | + expect(fn).toContain('"www.facebook.com"') |
| 297 | + expect(fn).toContain('"/tr"') |
| 298 | + }) |
| 299 | + |
| 300 | + it('returns undefined for non-matching URLs (no return statement for miss)', () => { |
| 301 | + const rules = [{ pattern: 'example.com', pathPrefix: '', target: '/_scripts/p/ex' }] |
| 302 | + const fn = generatePartytownResolveUrl(rules) |
| 303 | + // eslint-disable-next-line no-new-func |
| 304 | + const resolveUrl = new Function(`return ${fn}`)() |
| 305 | + const url = new URL('https://other.com/path') |
| 306 | + const location = new URL('https://mysite.com') |
| 307 | + expect(resolveUrl(url, location, 'fetch')).toBeUndefined() |
| 308 | + }) |
| 309 | + |
| 310 | + it('rewrites matching URLs to first-party proxy', () => { |
| 311 | + const rules = [{ pattern: 'example.com', pathPrefix: '', target: '/_scripts/p/ex' }] |
| 312 | + const fn = generatePartytownResolveUrl(rules) |
| 313 | + // eslint-disable-next-line no-new-func |
| 314 | + const resolveUrl = new Function(`return ${fn}`)() |
| 315 | + const url = new URL('https://example.com/collect?v=1') |
| 316 | + const location = new URL('https://mysite.com') |
| 317 | + const result = resolveUrl(url, location, 'fetch') |
| 318 | + expect(result).toBeInstanceOf(URL) |
| 319 | + expect(result.pathname).toBe('/_scripts/p/ex/collect') |
| 320 | + expect(result.search).toBe('?v=1') |
| 321 | + expect(result.origin).toBe('https://mysite.com') |
| 322 | + }) |
| 323 | + |
| 324 | + it('matches subdomains', () => { |
| 325 | + const rules = [{ pattern: 'google-analytics.com', pathPrefix: '', target: '/_scripts/p/ga' }] |
| 326 | + const fn = generatePartytownResolveUrl(rules) |
| 327 | + // eslint-disable-next-line no-new-func |
| 328 | + const resolveUrl = new Function(`return ${fn}`)() |
| 329 | + const url = new URL('https://www.google-analytics.com/g/collect') |
| 330 | + const location = new URL('https://mysite.com') |
| 331 | + const result = resolveUrl(url, location, 'fetch') |
| 332 | + expect(result.pathname).toBe('/_scripts/p/ga/g/collect') |
| 333 | + }) |
| 334 | + |
| 335 | + it('respects pathPrefix matching', () => { |
| 336 | + const rules = [{ pattern: 'www.facebook.com', pathPrefix: '/tr', target: '/_scripts/p/meta' }] |
| 337 | + const fn = generatePartytownResolveUrl(rules) |
| 338 | + // eslint-disable-next-line no-new-func |
| 339 | + const resolveUrl = new Function(`return ${fn}`)() |
| 340 | + |
| 341 | + // Matching path prefix |
| 342 | + const url1 = new URL('https://www.facebook.com/tr?id=123') |
| 343 | + const location = new URL('https://mysite.com') |
| 344 | + const result1 = resolveUrl(url1, location, 'fetch') |
| 345 | + expect(result1.pathname).toBe('/_scripts/p/meta/') |
| 346 | + expect(result1.search).toBe('?id=123') |
| 347 | + |
| 348 | + // Non-matching path prefix |
| 349 | + const url2 = new URL('https://www.facebook.com/other') |
| 350 | + expect(resolveUrl(url2, location, 'fetch')).toBeUndefined() |
| 351 | + }) |
235 | 352 | }) |
0 commit comments