This commit is contained in:
10
src/util/error/MethodNotSupportedError.ts
Normal file
10
src/util/error/MethodNotSupportedError.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
import {ErrorWithContext} from './ErrorWithContext'
|
||||
|
||||
export class MethodNotSupportedError extends ErrorWithContext {
|
||||
constructor(
|
||||
message = 'Method not supported',
|
||||
context: {[key: string]: any} = {},
|
||||
) {
|
||||
super(message, context)
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,7 @@
|
||||
import {RequestInfo, RequestInit, Response} from 'node-fetch'
|
||||
import {unsafeESMImport} from './unsafe'
|
||||
export const fetch = (url: RequestInfo, init?: RequestInit): Promise<Response> => unsafeESMImport('node-fetch').then(({default: nodeFetch}) => nodeFetch(url, init))
|
||||
|
||||
export * from './cache/Cache'
|
||||
export * from './cache/InMemCache'
|
||||
|
||||
@@ -10,6 +14,7 @@ export * from './collection/where'
|
||||
export * from './const/http'
|
||||
|
||||
export * from './error/ErrorWithContext'
|
||||
export * from './error/MethodNotSupportedError'
|
||||
|
||||
export * from './logging/Logger'
|
||||
export * from './logging/StandardLogger'
|
||||
|
||||
@@ -5,6 +5,7 @@ import * as mime from 'mime-types'
|
||||
import {FileNotFoundError, Filesystem} from './path/Filesystem'
|
||||
import {Collection} from '../collection/Collection'
|
||||
import {Readable, Writable} from 'stream'
|
||||
import {Pipe} from './Pipe'
|
||||
|
||||
/**
|
||||
* An item that could represent a path.
|
||||
@@ -82,6 +83,8 @@ export class UniversalPath {
|
||||
|
||||
protected resourceLocalPath!: string
|
||||
|
||||
protected resourceQuery: URLSearchParams = new URLSearchParams()
|
||||
|
||||
constructor(
|
||||
/**
|
||||
* The path string this path refers to.
|
||||
@@ -94,6 +97,10 @@ export class UniversalPath {
|
||||
) {
|
||||
this.setPrefix()
|
||||
this.setLocal()
|
||||
|
||||
if ( this.isRemote ) {
|
||||
this.resourceQuery = (new URL(this.toRemote)).searchParams
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -140,6 +147,13 @@ export class UniversalPath {
|
||||
return new UniversalPath(this.initial)
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the URLSearchParams for this resource.
|
||||
*/
|
||||
get query(): URLSearchParams {
|
||||
return this.resourceQuery
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the string of this resource.
|
||||
*/
|
||||
@@ -183,7 +197,8 @@ export class UniversalPath {
|
||||
* Get the fully-prefixed path to this resource.
|
||||
*/
|
||||
get toRemote(): string {
|
||||
return `${this.prefix}${this.resourceLocalPath}`
|
||||
const query = this.query.toString()
|
||||
return `${this.prefix}${this.resourceLocalPath}${query ? '?' + query : ''}`
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -517,4 +532,9 @@ export class UniversalPath {
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
/** Get a new Pipe instance wrapping this. */
|
||||
toPipe(): Pipe<UniversalPath> {
|
||||
return Pipe.wrap(this)
|
||||
}
|
||||
}
|
||||
|
||||
24
src/util/unsafe.ts
Normal file
24
src/util/unsafe.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
/**
|
||||
* UNSAFE
|
||||
*
|
||||
* Sometimes, we need to make a literal `import()` call from within commonJS
|
||||
* modules in order to pull in ES modules from commonJS.
|
||||
*
|
||||
* However, when tsc renders the modules to commonJS, it rewrites _all_ calls
|
||||
* to `import` as calls to `require`, which means we cannot actually use ES
|
||||
* modules from commonJS-transpiled TypeScript.
|
||||
*
|
||||
* To bypass this, we can eval the literal string. This is a stupid hack and
|
||||
* I hate it so much, but unfortunately it works.
|
||||
*
|
||||
* So, this is a wrapper function that results in a call to the literal
|
||||
* `import(...)` function in the transpiled code. It should be used VERY
|
||||
* sparingly.
|
||||
*
|
||||
* @see https://github.com/microsoft/TypeScript/issues/43329
|
||||
* @param path
|
||||
*/
|
||||
export function unsafeESMImport(path: string): Promise<any> {
|
||||
((p: string) => p)(path)
|
||||
return eval('import(path)') // eslint-disable-line no-eval
|
||||
}
|
||||
Reference in New Issue
Block a user