From 67f492d4bf571e7486767ffbe0266a1b5b3be924 Mon Sep 17 00:00:00 2001 From: LiviaMedeiros Date: Sat, 26 Aug 2023 16:44:58 +0800 Subject: [PATCH] rearrange `lib` --- lib/generic.mjs | 6 ---- lib/http.mjs | 26 +++++++++++++++-- lib/poteto.mjs | 75 ++++--------------------------------------------- lib/request.mjs | 57 +++++++++++++++++++++++++++++++++++++ 4 files changed, 86 insertions(+), 78 deletions(-) create mode 100644 lib/request.mjs diff --git a/lib/generic.mjs b/lib/generic.mjs index 35de0b5..d9bd95b 100644 --- a/lib/generic.mjs +++ b/lib/generic.mjs @@ -1,13 +1,7 @@ -import { sep } from 'node:path'; -import { cwd } from 'node:process'; -import { pathToFileURL } from 'node:url'; - const requestConstructor = Reflect.construct.bind(Reflect, Request); const responseConstructor = Reflect.construct.bind(Reflect, Response); -const getCwdURL = () => pathToFileURL(cwd() + sep).href; export { - getCwdURL, requestConstructor, responseConstructor, }; diff --git a/lib/http.mjs b/lib/http.mjs index 42a107c..db0cc44 100644 --- a/lib/http.mjs +++ b/lib/http.mjs @@ -1,6 +1,9 @@ import { readlink } from 'node:fs/promises'; import { pathToFileURL } from 'node:url'; +const _params = Object.fromEntries(new URL(import.meta.url).searchParams.entries()); +const PREFIX = `X-${_params.prefix ?? 'Poteto'}-`; + const errorAsUndefined = () => {}; const HEADERS_ALLOW = Object.freeze(new Headers({ @@ -73,6 +76,26 @@ const genericResponse = (status = 200, { ...opts, }); +const adjustHeaders = ({ headers }) => + new Headers([ + ...getGenericHeaders(), + ...Object.entries(headers).map(([$, _]) => [ + `${PREFIX}${$}`, + _ instanceof Date ? _.toISOString() : `${_}`, + ]), + + // TODO: Etag? + // TODO: Content-Type? + ['Content-Length', headers.size], + ['Last-Modified', headers.mtime?.toUTCString()], + ].filter(([, $]) => $ !== undefined)); + +const statsAsOptions = async (statsOrError, { status } = {}) => + Promise.resolve(statsOrError) + .catch($ => $) + .then(headers => ({ ...errAsStatus(headers, status), headers })) + .then($ => Object.assign($, { headers: adjustHeaders($) })); + const blockingHooks = [ // if request.redirect is not follow, async (url, { redirect }) => { @@ -100,9 +123,8 @@ const getRanges = headers => headers.get('Range')?.split(';')[0].split('=')[1]?.split(',').map($ => $.trim()) ?? []; export { - errAsStatus, genericResponse, - getGenericHeaders, getRanges, preHooks, + statsAsOptions, }; diff --git a/lib/poteto.mjs b/lib/poteto.mjs index f0281ca..65c526a 100644 --- a/lib/poteto.mjs +++ b/lib/poteto.mjs @@ -2,18 +2,16 @@ import fs from 'node:fs/promises'; import { Writable } from 'node:stream'; import { - errAsStatus, genericResponse, - getGenericHeaders, getRanges, preHooks, + statsAsOptions, } from './http.mjs'; import { STAT_OPTS, READDIR_OPTS, } from './constants.mjs'; import { - getCwdURL, responseConstructor, } from './generic.mjs'; import { @@ -23,74 +21,11 @@ import { readRange, writeRange, } from './fs.mjs'; +import { + PotetoRequest, + ProxyRequest, +} from './request.mjs'; -const _params = Object.fromEntries(new URL(import.meta.url).searchParams.entries()); -const cwdURL = _params.persistCwd - ? getCwdURL() - : { [Symbol.toPrimitive]: getCwdURL }; -const PREFIX = `X-${_params.prefix ?? 'Poteto'}-`; - -const adjustHeaders = ({ headers }) => - new Headers([ - ...getGenericHeaders(), - ...Object.entries(headers).map(([$, _]) => [ - `${PREFIX}${$}`, - _ instanceof Date ? _.toISOString() : `${_}`, - ]), - - // TODO: Etag? - // TODO: Content-Type? - ['Content-Length', headers.size], - ['Last-Modified', headers.mtime?.toUTCString()], - ].filter(([, $]) => $ !== undefined)); - -const statsAsOptions = async (statsOrError, { status } = {}) => - Promise.resolve(statsOrError) - .catch($ => $) - .then(headers => ({ ...errAsStatus(headers, status), headers })) - .then($ => Object.assign($, { headers: adjustHeaders($) })); - -const requestInitOptions = new Set([ - 'method', - 'headers', - 'body', - 'referrer', - 'referrerPolicy', - 'mode', - 'credentials', - 'cache', - 'redirect', - 'integrity', - 'keepalive', - 'signal', - 'duplex', - 'priority', - 'window', -]); - -const potetoRequestInit = ([ resource, options ], requestInit = {}, url = null) => { - if (resource instanceof Request) { - requestInitOptions.forEach($ => requestInit[$] = resource[$]); - resource = resource.url; - } - return [ - (url = new URL(resource, cwdURL)).protocol === 'file:' - ? url - : resource, - { ...requestInit, ...options }, - ]; -}; - -class PotetoRequest extends Request { - constructor(...$) { - super(...potetoRequestInit($)); - } -} - -const ProxyRequest = new Proxy(Request, { - construct: (target, argumentsList) => - Reflect.construct(target, potetoRequestInit(argumentsList)) -}); const DELETE = async url => fs.rm(url).then(() => genericResponse(204)); diff --git a/lib/request.mjs b/lib/request.mjs new file mode 100644 index 0000000..b8a6a20 --- /dev/null +++ b/lib/request.mjs @@ -0,0 +1,57 @@ +import { sep } from 'node:path'; +import { cwd } from 'node:process'; +import { pathToFileURL } from 'node:url'; + +const getCwdURL = () => pathToFileURL(cwd() + sep).href; + +const _params = Object.fromEntries(new URL(import.meta.url).searchParams.entries()); +const cwdURL = _params.persistCwd + ? getCwdURL() + : { [Symbol.toPrimitive]: getCwdURL }; + +const requestInitOptions = new Set([ + 'method', + 'headers', + 'body', + 'referrer', + 'referrerPolicy', + 'mode', + 'credentials', + 'cache', + 'redirect', + 'integrity', + 'keepalive', + 'signal', + 'duplex', + 'priority', + 'window', +]); + +const potetoRequestInit = ([ resource, options ], requestInit = {}, url = null) => { + if (resource instanceof Request) { + requestInitOptions.forEach($ => requestInit[$] = resource[$]); + resource = resource.url; + } + return [ + (url = new URL(resource, cwdURL)).protocol === 'file:' + ? url + : resource, + { ...requestInit, ...options }, + ]; +}; + +class PotetoRequest extends Request { + constructor(...$) { + super(...potetoRequestInit($)); + } +} + +const ProxyRequest = new Proxy(Request, { + construct: (target, argumentsList) => + Reflect.construct(target, potetoRequestInit(argumentsList)) +}); + +export { + PotetoRequest, + ProxyRequest, +};