Matthieu Sieben a07b21151f
PDS pipethrough optimizations (#2770)
* Micro optimization in request proxying

* Request NSID parsing optimization

* DID document parsing optimization

* remove un-necessary call to next()

* Allow HandlerPipeThrough to be used with streams

* Refactor pipethrough to work with streams

* Expose "unicastLookup" DNS lookup and "isUnicastIp" utilities

* Use a hardened, HTTP2 compatible, client to perform proxied requests

* changeset

* tidy

* Properly handle compressed streams

* tidy

* update @types/node

* refactor

* Improved error management

* Expose parseContentEncoding() util

* use pipeline from nodejs

* Avoid decoding in read-after-write (if possible)

* Various fixes

* Return Buffer instance from streamToBytes

* fixes

* Add omit() utility

* tidy

* lint

* typo

* Use Buffer instead of ArrayBuffer form pipe through handler result

* optimization

* tidy

* refactor

* increase highWaterMark

* remove un-necessary type check

* Use undici.request where more relevant

* Improve soc in fetch utils

* feedback

* fidy

* tidy

* test refactor

* safer fetch

* changeset

* expose and re-use extractUrl util

* small optimizations

* tidy

* optimization

* build branch

---------

Co-authored-by: dholms <dtholmgren@gmail.com>
2024-09-19 18:24:20 -05:00

90 lines
3.0 KiB
TypeScript

import { parseUrlNsid } from '../src/util'
const testValid = (url: string, expected: string) => {
expect(parseUrlNsid(url)).toBe(expected)
}
const testInvalid = (url: string, errorMessage = 'invalid xrpc path') => {
expect(() => parseUrlNsid(url)).toThrow(errorMessage)
}
describe('parseUrlNsid', () => {
it('should extract the NSID from the URL', () => {
testValid('/xrpc/blee.blah.bloo', 'blee.blah.bloo')
testValid('/xrpc/blee.blah.bloo?foo[]', 'blee.blah.bloo')
testValid('/xrpc/blee.blah.bloo?foo=bar', 'blee.blah.bloo')
testValid('/xrpc/com.example.nsid', 'com.example.nsid')
testValid('/xrpc/com.example.nsid?foo=bar', 'com.example.nsid')
testValid('/xrpc/com.example-domain.nsid', 'com.example-domain.nsid')
})
it('should allow a trailing slash', () => {
testValid('/xrpc/blee.blah.bloo/?', 'blee.blah.bloo')
testValid('/xrpc/blee.blah.bloo/?foo=', 'blee.blah.bloo')
testValid('/xrpc/blee.blah.bloo/?bool', 'blee.blah.bloo')
testValid('/xrpc/com.example.nsid/', 'com.example.nsid')
})
it('should throw an error if the URL is too short', () => {
testInvalid('/xrpc/a')
})
it('should throw an error if the URL is empty', () => {
testInvalid('')
})
it('should throw an error if the URL is missing the NSID', () => {
testInvalid('/xrpc/')
testInvalid('/xrpc/?')
testInvalid('/xrpc/?foo=bar')
})
it('should throw an error if the URL contains extra path segments', () => {
testInvalid('/xrpc/123/extra')
testInvalid('/xrpc/123/extra?foo=bar')
})
it('should throw an error if the URL is missing the XRPC path prefix', () => {
testInvalid('/foo/123')
testInvalid('/foo/com.example.nsid')
})
it('should throw an error if the NSID starts with a dot', () => {
testInvalid('/xrpc/.')
testInvalid('/xrpc/..')
testInvalid('/xrpc/....')
testInvalid('/xrpc/.com.example.nsid')
testInvalid('/xrpc/com..example.nsid')
testInvalid('/xrpc/com.example..nsid')
testInvalid('/xrpc/com.example.nsid.')
testInvalid('/xrpc/com.example.nsid./')
testInvalid('/xrpc/com.example.nsid.?foo=bar')
testInvalid('/xrpc/com.example.nsid./?foo=bar')
})
it('should throw an error if the NSID contains a misplaced dash', () => {
testInvalid('/xrpc/-')
testInvalid('/xrpc/com.example.-nsid')
testInvalid('/xrpc/com.example-.nsid')
testInvalid('/xrpc/com.-example.nsid')
testInvalid('/xrpc/com.-example-.nsid')
testInvalid('/xrpc/com.example.nsid-')
testInvalid('/xrpc/-com.example.nsid')
testInvalid('/xrpc/com.example--domain.nsid')
})
it('should throw an error if the URL starts with a space', () => {
testInvalid(' /xrpc/com.example.nsid')
})
it('should throw an error if the NSID contains invalid characters', () => {
testInvalid('/xrpc/com.example.nsid#')
testInvalid('/xrpc/com.example.nsid!')
testInvalid('/xrpc/com.example#?nsid')
testInvalid('/xrpc/!com.example.nsid')
testInvalid('/xrpc/com.example.nsid ')
testInvalid('/xrpc/ com.example.nsid')
testInvalid('/xrpc/com. example.nsid')
})
})