happy-dom 8.8.0 → 9.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of happy-dom might be problematic. Click here for more details.
- package/README.md +2 -2
- package/lib/cookie/CookieJar.js +1 -1
- package/lib/cookie/CookieJar.js.map +1 -1
- package/lib/dom-token-list/DOMTokenList.js +24 -20
- package/lib/dom-token-list/DOMTokenList.js.map +1 -1
- package/lib/event/EventTarget.js +6 -3
- package/lib/event/EventTarget.js.map +1 -1
- package/lib/exception/DOMExceptionNameEnum.d.ts +5 -1
- package/lib/exception/DOMExceptionNameEnum.js +4 -0
- package/lib/exception/DOMExceptionNameEnum.js.map +1 -1
- package/lib/fetch/AbortController.d.ts +19 -0
- package/lib/fetch/AbortController.js +30 -0
- package/lib/fetch/AbortController.js.map +1 -0
- package/lib/fetch/AbortSignal.d.ts +25 -0
- package/lib/fetch/AbortSignal.js +51 -0
- package/lib/fetch/AbortSignal.js.map +1 -0
- package/lib/fetch/Fetch.d.ts +117 -0
- package/lib/fetch/Fetch.js +530 -0
- package/lib/fetch/Fetch.js.map +1 -0
- package/lib/fetch/Headers.d.ts +81 -3
- package/lib/fetch/Headers.js +139 -24
- package/lib/fetch/Headers.js.map +1 -1
- package/lib/fetch/Request.d.ts +56 -32
- package/lib/fetch/Request.js +165 -110
- package/lib/fetch/Request.js.map +1 -1
- package/lib/fetch/{ResourceFetchHandler.d.ts → ResourceFetch.d.ts} +1 -1
- package/lib/fetch/{ResourceFetchHandler.js → ResourceFetch.js} +5 -5
- package/lib/fetch/ResourceFetch.js.map +1 -0
- package/lib/fetch/Response.d.ts +60 -26
- package/lib/fetch/Response.js +196 -104
- package/lib/fetch/Response.js.map +1 -1
- package/lib/fetch/data-uri/DataURIParser.d.ts +20 -0
- package/lib/fetch/data-uri/DataURIParser.js +60 -0
- package/lib/fetch/data-uri/DataURIParser.js.map +1 -0
- package/lib/fetch/multipart/MultipartFormDataParser.d.ts +40 -0
- package/lib/fetch/multipart/MultipartFormDataParser.js +81 -0
- package/lib/fetch/multipart/MultipartFormDataParser.js.map +1 -0
- package/lib/fetch/multipart/MultipartReader.d.ts +49 -0
- package/lib/fetch/multipart/MultipartReader.js +178 -0
- package/lib/fetch/multipart/MultipartReader.js.map +1 -0
- package/lib/fetch/types/IHeaders.d.ts +71 -0
- package/lib/fetch/{IHeaders.js.map → types/IHeaders.js.map} +1 -1
- package/lib/fetch/types/IHeadersInit.js.map +1 -0
- package/lib/fetch/types/IRequest.d.ts +60 -0
- package/lib/fetch/{IRequest.js.map → types/IRequest.js.map} +1 -1
- package/lib/fetch/types/IRequestBody.d.ts +7 -0
- package/lib/fetch/{IBody.js → types/IRequestBody.js} +1 -1
- package/lib/fetch/types/IRequestBody.js.map +1 -0
- package/lib/fetch/types/IRequestCredentials.d.ts +2 -0
- package/lib/fetch/types/IRequestCredentials.js +3 -0
- package/lib/fetch/types/IRequestCredentials.js.map +1 -0
- package/lib/fetch/{RequestInfo.d.ts → types/IRequestInfo.d.ts} +2 -2
- package/lib/fetch/types/IRequestInfo.js +3 -0
- package/lib/fetch/types/IRequestInfo.js.map +1 -0
- package/lib/fetch/types/IRequestInit.d.ts +21 -0
- package/lib/fetch/types/IRequestInit.js.map +1 -0
- package/lib/fetch/types/IRequestRedirect.d.ts +2 -0
- package/lib/fetch/types/IRequestRedirect.js +3 -0
- package/lib/fetch/types/IRequestRedirect.js.map +1 -0
- package/lib/fetch/types/IRequestReferrerPolicy.d.ts +2 -0
- package/lib/fetch/types/IRequestReferrerPolicy.js +3 -0
- package/lib/fetch/types/IRequestReferrerPolicy.js.map +1 -0
- package/lib/fetch/types/IResponse.d.ts +27 -0
- package/lib/fetch/{IResponse.js.map → types/IResponse.js.map} +1 -1
- package/lib/fetch/types/IResponseBody.d.ts +7 -0
- package/lib/fetch/types/IResponseBody.js +3 -0
- package/lib/fetch/types/IResponseBody.js.map +1 -0
- package/lib/fetch/types/IResponseInit.d.ts +9 -0
- package/lib/fetch/types/IResponseInit.js.map +1 -0
- package/lib/fetch/utilities/FetchBodyUtility.d.ts +44 -0
- package/lib/fetch/utilities/FetchBodyUtility.js +156 -0
- package/lib/fetch/utilities/FetchBodyUtility.js.map +1 -0
- package/lib/fetch/utilities/FetchCORSUtility.d.ts +12 -0
- package/lib/fetch/utilities/FetchCORSUtility.js +20 -0
- package/lib/fetch/utilities/FetchCORSUtility.js.map +1 -0
- package/lib/fetch/utilities/FetchRequestHeaderUtility.d.ts +12 -0
- package/lib/fetch/utilities/FetchRequestHeaderUtility.js +45 -0
- package/lib/fetch/utilities/FetchRequestHeaderUtility.js.map +1 -0
- package/lib/fetch/utilities/FetchRequestReferrerUtility.d.ts +55 -0
- package/lib/fetch/utilities/FetchRequestReferrerUtility.js +194 -0
- package/lib/fetch/utilities/FetchRequestReferrerUtility.js.map +1 -0
- package/lib/fetch/utilities/FetchRequestValidationUtility.d.ts +38 -0
- package/lib/fetch/utilities/FetchRequestValidationUtility.js +70 -0
- package/lib/fetch/utilities/FetchRequestValidationUtility.js.map +1 -0
- package/lib/file/Blob.d.ts +2 -2
- package/lib/file/Blob.js +1 -1
- package/lib/file/Blob.js.map +1 -1
- package/lib/file/File.d.ts +2 -1
- package/lib/file/File.js.map +1 -1
- package/lib/form-data/FormData.d.ts +98 -0
- package/lib/form-data/FormData.js +212 -0
- package/lib/form-data/FormData.js.map +1 -0
- package/lib/index.d.ts +14 -1
- package/lib/index.js +20 -2
- package/lib/index.js.map +1 -1
- package/lib/nodes/document/Document.d.ts +5 -17
- package/lib/nodes/document/Document.js +11 -54
- package/lib/nodes/document/Document.js.map +1 -1
- package/lib/nodes/document/IDocument.d.ts +1 -1
- package/lib/nodes/document-fragment/DocumentFragment.d.ts +3 -14
- package/lib/nodes/document-fragment/DocumentFragment.js +7 -51
- package/lib/nodes/document-fragment/DocumentFragment.js.map +1 -1
- package/lib/nodes/document-fragment/IDocumentFragment.d.ts +2 -1
- package/lib/nodes/element/Element.d.ts +8 -8
- package/lib/nodes/element/Element.js +28 -45
- package/lib/nodes/element/Element.js.map +1 -1
- package/lib/nodes/element/ElementUtility.d.ts +32 -0
- package/lib/nodes/element/ElementUtility.js +105 -0
- package/lib/nodes/element/ElementUtility.js.map +1 -0
- package/lib/nodes/element/HTMLCollection.d.ts +26 -3
- package/lib/nodes/element/HTMLCollection.js +51 -0
- package/lib/nodes/element/HTMLCollection.js.map +1 -1
- package/lib/nodes/element/IHTMLCollection.d.ts +9 -2
- package/lib/nodes/html-anchor-element/HTMLAnchorElement.js +24 -24
- package/lib/nodes/html-anchor-element/HTMLAnchorElement.js.map +1 -1
- package/lib/nodes/html-audio-element/Audio.d.ts +15 -0
- package/lib/nodes/html-audio-element/Audio.js +27 -0
- package/lib/nodes/html-audio-element/Audio.js.map +1 -0
- package/lib/nodes/html-base-element/HTMLBaseElement.js +4 -4
- package/lib/nodes/html-base-element/HTMLBaseElement.js.map +1 -1
- package/lib/nodes/html-button-element/HTMLButtonElement.d.ts +79 -3
- package/lib/nodes/html-button-element/HTMLButtonElement.js +158 -10
- package/lib/nodes/html-button-element/HTMLButtonElement.js.map +1 -1
- package/lib/nodes/html-button-element/IHTMLButtonElement.d.ts +28 -2
- package/lib/nodes/html-dialog-element/HTMLDialogElement.js +3 -3
- package/lib/nodes/html-dialog-element/HTMLDialogElement.js.map +1 -1
- package/lib/nodes/html-element/HTMLElement.js +12 -12
- package/lib/nodes/html-element/HTMLElement.js.map +1 -1
- package/lib/nodes/html-form-element/HTMLFormControlsCollection.d.ts +32 -0
- package/lib/nodes/html-form-element/HTMLFormControlsCollection.js +58 -0
- package/lib/nodes/html-form-element/HTMLFormControlsCollection.js.map +1 -0
- package/lib/nodes/html-form-element/HTMLFormElement.d.ts +37 -17
- package/lib/nodes/html-form-element/HTMLFormElement.js +128 -37
- package/lib/nodes/html-form-element/HTMLFormElement.js.map +1 -1
- package/lib/nodes/html-form-element/IHTMLFormControlsCollection.d.ts +25 -0
- package/lib/nodes/html-form-element/IHTMLFormControlsCollection.js +3 -0
- package/lib/nodes/html-form-element/IHTMLFormControlsCollection.js.map +1 -0
- package/lib/nodes/html-form-element/IHTMLFormElement.d.ts +16 -6
- package/lib/nodes/html-form-element/IRadioNodeList.d.ts +15 -0
- package/lib/nodes/html-form-element/IRadioNodeList.js +3 -0
- package/lib/nodes/html-form-element/IRadioNodeList.js.map +1 -0
- package/lib/nodes/html-form-element/RadioNodeList.d.ts +16 -0
- package/lib/nodes/html-form-element/RadioNodeList.js +28 -0
- package/lib/nodes/html-form-element/RadioNodeList.js.map +1 -0
- package/lib/nodes/html-image-element/HTMLImageElement.js +19 -19
- package/lib/nodes/html-image-element/HTMLImageElement.js.map +1 -1
- package/lib/nodes/html-input-element/HTMLInputElement.d.ts +43 -19
- package/lib/nodes/html-input-element/HTMLInputElement.js +206 -111
- package/lib/nodes/html-input-element/HTMLInputElement.js.map +1 -1
- package/lib/nodes/html-input-element/IHTMLInputElement.d.ts +14 -1
- package/lib/nodes/html-label-element/HTMLLabelElement.js +3 -7
- package/lib/nodes/html-label-element/HTMLLabelElement.js.map +1 -1
- package/lib/nodes/html-link-element/HTMLLinkElement.js +21 -21
- package/lib/nodes/html-link-element/HTMLLinkElement.js.map +1 -1
- package/lib/nodes/html-media-element/HTMLMediaElement.js +21 -21
- package/lib/nodes/html-media-element/HTMLMediaElement.js.map +1 -1
- package/lib/nodes/html-meta-element/HTMLMetaElement.js +8 -8
- package/lib/nodes/html-meta-element/HTMLMetaElement.js.map +1 -1
- package/lib/nodes/html-opt-group-element/HTMLOptGroupElement.js +6 -6
- package/lib/nodes/html-opt-group-element/HTMLOptGroupElement.js.map +1 -1
- package/lib/nodes/html-option-element/HTMLOptionElement.d.ts +3 -4
- package/lib/nodes/html-option-element/HTMLOptionElement.js +26 -29
- package/lib/nodes/html-option-element/HTMLOptionElement.js.map +1 -1
- package/lib/nodes/html-script-element/HTMLScriptElement.js +16 -16
- package/lib/nodes/html-script-element/HTMLScriptElement.js.map +1 -1
- package/lib/nodes/html-script-element/ScriptUtility.js +5 -5
- package/lib/nodes/html-script-element/ScriptUtility.js.map +1 -1
- package/lib/nodes/{html-option-element → html-select-element}/HTMLOptionsCollection.d.ts +5 -6
- package/lib/nodes/{html-option-element → html-select-element}/HTMLOptionsCollection.js.map +1 -1
- package/lib/nodes/html-select-element/HTMLSelectElement.d.ts +42 -41
- package/lib/nodes/html-select-element/HTMLSelectElement.js +134 -138
- package/lib/nodes/html-select-element/HTMLSelectElement.js.map +1 -1
- package/lib/nodes/{html-option-element → html-select-element}/IHTMLOptionsCollection.d.ts +4 -5
- package/lib/nodes/html-select-element/IHTMLOptionsCollection.js.map +1 -0
- package/lib/nodes/html-select-element/IHTMLSelectElement.d.ts +22 -2
- package/lib/nodes/html-slot-element/HTMLSlotElement.js +2 -2
- package/lib/nodes/html-slot-element/HTMLSlotElement.js.map +1 -1
- package/lib/nodes/html-style-element/HTMLStyleElement.js +7 -7
- package/lib/nodes/html-style-element/HTMLStyleElement.js.map +1 -1
- package/lib/nodes/html-text-area-element/HTMLTextAreaElement.d.ts +55 -2
- package/lib/nodes/html-text-area-element/HTMLTextAreaElement.js +150 -40
- package/lib/nodes/html-text-area-element/HTMLTextAreaElement.js.map +1 -1
- package/lib/nodes/html-text-area-element/IHTMLTextAreaElement.d.ts +16 -0
- package/lib/nodes/node/Node.d.ts +12 -1
- package/lib/nodes/node/Node.js +50 -4
- package/lib/nodes/node/Node.js.map +1 -1
- package/lib/nodes/node/NodeList.d.ts +2 -3
- package/lib/nodes/node/NodeList.js.map +1 -1
- package/lib/nodes/parent-node/IParentNode.d.ts +4 -4
- package/lib/nodes/parent-node/ParentNodeUtility.d.ts +3 -3
- package/lib/nodes/parent-node/ParentNodeUtility.js +13 -11
- package/lib/nodes/parent-node/ParentNodeUtility.js.map +1 -1
- package/lib/nodes/text/Text.d.ts +13 -0
- package/lib/nodes/text/Text.js +30 -0
- package/lib/nodes/text/Text.js.map +1 -1
- package/lib/query-selector/QuerySelector.js +2 -2
- package/lib/query-selector/QuerySelector.js.map +1 -1
- package/lib/query-selector/SelectorItem.js +2 -0
- package/lib/query-selector/SelectorItem.js.map +1 -1
- package/lib/validity-state/ValidityState.d.ts +87 -0
- package/lib/validity-state/ValidityState.js +171 -0
- package/lib/validity-state/ValidityState.js.map +1 -0
- package/lib/window/GlobalWindow.d.ts +0 -8
- package/lib/window/GlobalWindow.js +0 -2
- package/lib/window/GlobalWindow.js.map +1 -1
- package/lib/window/INodeJSGlobal.d.ts +71 -0
- package/lib/window/INodeJSGlobal.js +2 -0
- package/lib/window/INodeJSGlobal.js.map +1 -0
- package/lib/window/IWindow.d.ts +30 -10
- package/lib/window/Window.d.ts +37 -20
- package/lib/window/Window.js +22 -10
- package/lib/window/Window.js.map +1 -1
- package/lib/xml-http-request/XMLHttpRequest.js +20 -12
- package/lib/xml-http-request/XMLHttpRequest.js.map +1 -1
- package/lib/xml-parser/XMLParser.js +1 -1
- package/lib/xml-parser/XMLParser.js.map +1 -1
- package/package.json +2 -3
- package/src/cookie/CookieJar.ts +1 -1
- package/src/dom-token-list/DOMTokenList.ts +24 -20
- package/src/event/EventTarget.ts +7 -3
- package/src/exception/DOMExceptionNameEnum.ts +5 -1
- package/src/fetch/AbortController.ts +26 -0
- package/src/fetch/AbortSignal.ts +44 -0
- package/src/fetch/Fetch.ts +666 -0
- package/src/fetch/Headers.ts +151 -3
- package/src/fetch/Request.ts +226 -94
- package/src/fetch/{ResourceFetchHandler.ts → ResourceFetch.ts} +3 -3
- package/src/fetch/Response.ts +252 -89
- package/src/fetch/data-uri/DataURIParser.ts +66 -0
- package/src/fetch/multipart/MultipartFormDataParser.ts +113 -0
- package/src/fetch/multipart/MultipartReader.ts +217 -0
- package/src/fetch/types/IHeaders.ts +80 -0
- package/src/fetch/types/IRequest.ts +65 -0
- package/src/fetch/types/IRequestBody.ts +15 -0
- package/src/fetch/types/IRequestCredentials.ts +2 -0
- package/src/fetch/types/IRequestInfo.ts +6 -0
- package/src/fetch/types/IRequestInit.ts +21 -0
- package/src/fetch/types/IRequestRedirect.ts +3 -0
- package/src/fetch/types/IRequestReferrerPolicy.ts +11 -0
- package/src/fetch/types/IResponse.ts +27 -0
- package/src/fetch/types/IResponseBody.ts +15 -0
- package/src/fetch/types/IResponseInit.ts +10 -0
- package/src/fetch/utilities/FetchBodyUtility.ts +175 -0
- package/src/fetch/utilities/FetchCORSUtility.ts +18 -0
- package/src/fetch/utilities/FetchRequestHeaderUtility.ts +47 -0
- package/src/fetch/utilities/FetchRequestReferrerUtility.ts +241 -0
- package/src/fetch/utilities/FetchRequestValidationUtility.ts +82 -0
- package/src/file/Blob.ts +2 -2
- package/src/file/File.ts +5 -1
- package/src/form-data/FormData.ts +237 -0
- package/src/index.ts +27 -1
- package/src/nodes/document/Document.ts +23 -64
- package/src/nodes/document/IDocument.ts +1 -1
- package/src/nodes/document-fragment/DocumentFragment.ts +9 -59
- package/src/nodes/document-fragment/IDocumentFragment.ts +2 -1
- package/src/nodes/element/Element.ts +43 -56
- package/src/nodes/element/ElementUtility.ts +137 -0
- package/src/nodes/element/HTMLCollection.ts +60 -3
- package/src/nodes/element/IHTMLCollection.ts +10 -2
- package/src/nodes/html-anchor-element/HTMLAnchorElement.ts +24 -24
- package/src/nodes/html-audio-element/Audio.ts +22 -0
- package/src/nodes/html-base-element/HTMLBaseElement.ts +4 -4
- package/src/nodes/html-button-element/HTMLButtonElement.ts +184 -10
- package/src/nodes/html-button-element/IHTMLButtonElement.ts +31 -2
- package/src/nodes/html-dialog-element/HTMLDialogElement.ts +3 -3
- package/src/nodes/html-element/HTMLElement.ts +12 -12
- package/src/nodes/html-form-element/HTMLFormControlsCollection.ts +76 -0
- package/src/nodes/html-form-element/HTMLFormElement.ts +147 -41
- package/src/nodes/html-form-element/IHTMLFormControlsCollection.ts +45 -0
- package/src/nodes/html-form-element/IHTMLFormElement.ts +17 -6
- package/src/nodes/html-form-element/IRadioNodeList.ts +16 -0
- package/src/nodes/html-form-element/RadioNodeList.ts +24 -0
- package/src/nodes/html-image-element/HTMLImageElement.ts +19 -19
- package/src/nodes/html-input-element/HTMLInputElement.ts +232 -113
- package/src/nodes/html-input-element/IHTMLInputElement.ts +20 -1
- package/src/nodes/html-label-element/HTMLLabelElement.ts +3 -7
- package/src/nodes/html-link-element/HTMLLinkElement.ts +21 -21
- package/src/nodes/html-media-element/HTMLMediaElement.ts +21 -21
- package/src/nodes/html-meta-element/HTMLMetaElement.ts +8 -8
- package/src/nodes/html-opt-group-element/HTMLOptGroupElement.ts +6 -6
- package/src/nodes/html-option-element/HTMLOptionElement.ts +29 -30
- package/src/nodes/html-script-element/HTMLScriptElement.ts +16 -16
- package/src/nodes/html-script-element/ScriptUtility.ts +5 -5
- package/src/nodes/{html-option-element → html-select-element}/HTMLOptionsCollection.ts +5 -9
- package/src/nodes/html-select-element/HTMLSelectElement.ts +159 -170
- package/src/nodes/{html-option-element → html-select-element}/IHTMLOptionsCollection.ts +4 -8
- package/src/nodes/html-select-element/IHTMLSelectElement.ts +25 -2
- package/src/nodes/html-slot-element/HTMLSlotElement.ts +2 -2
- package/src/nodes/html-style-element/HTMLStyleElement.ts +7 -7
- package/src/nodes/html-text-area-element/HTMLTextAreaElement.ts +174 -41
- package/src/nodes/html-text-area-element/IHTMLTextAreaElement.ts +18 -0
- package/src/nodes/node/Node.ts +61 -6
- package/src/nodes/node/NodeList.ts +2 -3
- package/src/nodes/parent-node/IParentNode.ts +7 -4
- package/src/nodes/parent-node/ParentNodeUtility.ts +25 -15
- package/src/nodes/text/Text.ts +38 -0
- package/src/query-selector/QuerySelector.ts +2 -2
- package/src/query-selector/SelectorItem.ts +3 -0
- package/src/validity-state/ValidityState.ts +211 -0
- package/src/window/GlobalWindow.ts +0 -2
- package/src/window/INodeJSGlobal.ts +70 -0
- package/src/window/IWindow.ts +32 -10
- package/src/window/Window.ts +43 -21
- package/src/xml-http-request/XMLHttpRequest.ts +27 -26
- package/src/xml-parser/XMLParser.ts +1 -1
- package/lib/fetch/FetchHandler.d.ts +0 -18
- package/lib/fetch/FetchHandler.js +0 -83
- package/lib/fetch/FetchHandler.js.map +0 -1
- package/lib/fetch/IAbortSignal.d.ts +0 -16
- package/lib/fetch/IAbortSignal.js +0 -4
- package/lib/fetch/IAbortSignal.js.map +0 -1
- package/lib/fetch/IBody.d.ts +0 -17
- package/lib/fetch/IBody.js.map +0 -1
- package/lib/fetch/IHeaders.d.ts +0 -18
- package/lib/fetch/IHeadersInit.js.map +0 -1
- package/lib/fetch/IRequest.d.ts +0 -18
- package/lib/fetch/IRequestInit.d.ts +0 -16
- package/lib/fetch/IRequestInit.js.map +0 -1
- package/lib/fetch/IResponse.d.ts +0 -20
- package/lib/fetch/IResponseInit.d.ts +0 -9
- package/lib/fetch/IResponseInit.js.map +0 -1
- package/lib/fetch/RequestInfo.js +0 -3
- package/lib/fetch/RequestInfo.js.map +0 -1
- package/lib/fetch/ResourceFetchHandler.js.map +0 -1
- package/lib/form-data/IFormData.d.ts +0 -32
- package/lib/form-data/IFormData.js +0 -3
- package/lib/form-data/IFormData.js.map +0 -1
- package/lib/location/RelativeURL.d.ts +0 -15
- package/lib/location/RelativeURL.js +0 -19
- package/lib/location/RelativeURL.js.map +0 -1
- package/lib/nodes/element/HTMLCollectionFactory.d.ts +0 -21
- package/lib/nodes/element/HTMLCollectionFactory.js +0 -31
- package/lib/nodes/element/HTMLCollectionFactory.js.map +0 -1
- package/lib/nodes/html-option-element/IHTMLOptionsCollection.js.map +0 -1
- package/lib/nodes/node/NodeListFactory.d.ts +0 -21
- package/lib/nodes/node/NodeListFactory.js +0 -31
- package/lib/nodes/node/NodeListFactory.js.map +0 -1
- package/lib/nodes/validity-state/ValidityState.d.ts +0 -52
- package/lib/nodes/validity-state/ValidityState.js +0 -81
- package/lib/nodes/validity-state/ValidityState.js.map +0 -1
- package/src/fetch/FetchHandler.ts +0 -90
- package/src/fetch/IAbortSignal.ts +0 -34
- package/src/fetch/IBody.ts +0 -18
- package/src/fetch/IHeaders.ts +0 -18
- package/src/fetch/IRequest.ts +0 -59
- package/src/fetch/IRequestInit.ts +0 -41
- package/src/fetch/IResponse.ts +0 -22
- package/src/fetch/IResponseInit.ts +0 -10
- package/src/fetch/RequestInfo.ts +0 -6
- package/src/form-data/IFormData.ts +0 -33
- package/src/location/RelativeURL.ts +0 -17
- package/src/nodes/element/HTMLCollectionFactory.ts +0 -31
- package/src/nodes/node/NodeListFactory.ts +0 -31
- package/src/nodes/validity-state/ValidityState.ts +0 -84
- /package/lib/fetch/{IHeaders.js → types/IHeaders.js} +0 -0
- /package/lib/fetch/{IHeadersInit.d.ts → types/IHeadersInit.d.ts} +0 -0
- /package/lib/fetch/{IHeadersInit.js → types/IHeadersInit.js} +0 -0
- /package/lib/fetch/{IRequest.js → types/IRequest.js} +0 -0
- /package/lib/fetch/{IRequestInit.js → types/IRequestInit.js} +0 -0
- /package/lib/fetch/{IResponse.js → types/IResponse.js} +0 -0
- /package/lib/fetch/{IResponseInit.js → types/IResponseInit.js} +0 -0
- /package/lib/nodes/{html-option-element → html-select-element}/HTMLOptionsCollection.js +0 -0
- /package/lib/nodes/{html-option-element → html-select-element}/IHTMLOptionsCollection.js +0 -0
- /package/src/fetch/{IHeadersInit.ts → types/IHeadersInit.ts} +0 -0
@@ -0,0 +1,217 @@
|
|
1
|
+
import DOMException from '../../exception/DOMException';
|
2
|
+
import DOMExceptionNameEnum from '../../exception/DOMExceptionNameEnum';
|
3
|
+
import File from '../../file/File';
|
4
|
+
import FormData from '../../form-data/FormData';
|
5
|
+
|
6
|
+
enum MultiparParserStateEnum {
|
7
|
+
boundary = 0,
|
8
|
+
headerStart = 2,
|
9
|
+
header = 3,
|
10
|
+
data = 5
|
11
|
+
}
|
12
|
+
|
13
|
+
const CHARACTER_CODE = {
|
14
|
+
lf: 10,
|
15
|
+
cr: 13
|
16
|
+
};
|
17
|
+
|
18
|
+
/**
|
19
|
+
* Multipart reader.
|
20
|
+
*
|
21
|
+
* Based on:
|
22
|
+
* https://github.com/node-fetch/node-fetch/blob/main/src/utils/multipart-parser.js (MIT)
|
23
|
+
*/
|
24
|
+
export default class MultipartReader {
|
25
|
+
private formData = new FormData();
|
26
|
+
private boundary: Uint8Array;
|
27
|
+
private boundaryIndex = 0;
|
28
|
+
private state = MultiparParserStateEnum.boundary;
|
29
|
+
private data: {
|
30
|
+
contentDisposition: { [key: string]: string } | null;
|
31
|
+
value: number[];
|
32
|
+
contentType: string | null;
|
33
|
+
header: string;
|
34
|
+
} = {
|
35
|
+
contentDisposition: null,
|
36
|
+
value: [],
|
37
|
+
contentType: null,
|
38
|
+
header: ''
|
39
|
+
};
|
40
|
+
|
41
|
+
/**
|
42
|
+
* Constructor.
|
43
|
+
*
|
44
|
+
* @param formData Form data.
|
45
|
+
* @param boundary Boundary.
|
46
|
+
*/
|
47
|
+
constructor(boundary: string) {
|
48
|
+
const boundaryHeader = `--${boundary}`;
|
49
|
+
this.boundary = new Uint8Array(boundaryHeader.length);
|
50
|
+
|
51
|
+
for (let i = 0, max = boundaryHeader.length; i < max; i++) {
|
52
|
+
this.boundary[i] = boundaryHeader.charCodeAt(i);
|
53
|
+
}
|
54
|
+
}
|
55
|
+
|
56
|
+
/**
|
57
|
+
* Appends data.
|
58
|
+
*
|
59
|
+
* @param data Data.
|
60
|
+
*/
|
61
|
+
public write(data: Uint8Array): void {
|
62
|
+
let char: number;
|
63
|
+
let nextChar: number;
|
64
|
+
|
65
|
+
for (let i = 0, max = data.length; i < max; i++) {
|
66
|
+
char = data[i];
|
67
|
+
nextChar = data[i + 1];
|
68
|
+
|
69
|
+
switch (this.state) {
|
70
|
+
case MultiparParserStateEnum.boundary:
|
71
|
+
if (char === this.boundary[this.boundaryIndex]) {
|
72
|
+
this.boundaryIndex++;
|
73
|
+
} else {
|
74
|
+
this.boundaryIndex = 0;
|
75
|
+
}
|
76
|
+
|
77
|
+
if (this.boundaryIndex === this.boundary.length) {
|
78
|
+
this.state = MultiparParserStateEnum.headerStart;
|
79
|
+
this.boundaryIndex = 0;
|
80
|
+
}
|
81
|
+
|
82
|
+
break;
|
83
|
+
|
84
|
+
case MultiparParserStateEnum.headerStart:
|
85
|
+
if (nextChar !== CHARACTER_CODE.cr && nextChar !== CHARACTER_CODE.lf) {
|
86
|
+
this.data.header = '';
|
87
|
+
this.state =
|
88
|
+
data[i - 2] === CHARACTER_CODE.lf
|
89
|
+
? MultiparParserStateEnum.data
|
90
|
+
: MultiparParserStateEnum.header;
|
91
|
+
}
|
92
|
+
|
93
|
+
break;
|
94
|
+
|
95
|
+
case MultiparParserStateEnum.header:
|
96
|
+
if (char === CHARACTER_CODE.cr) {
|
97
|
+
if (this.data.header) {
|
98
|
+
const headerParts = this.data.header.split(':');
|
99
|
+
const headerName = headerParts[0].toLowerCase();
|
100
|
+
const headerValue = headerParts[1].trim();
|
101
|
+
|
102
|
+
switch (headerName) {
|
103
|
+
case 'content-disposition':
|
104
|
+
this.data.contentDisposition = this.getContentDisposition(headerValue);
|
105
|
+
break;
|
106
|
+
case 'content-type':
|
107
|
+
this.data.contentType = headerValue;
|
108
|
+
break;
|
109
|
+
}
|
110
|
+
}
|
111
|
+
|
112
|
+
this.state = MultiparParserStateEnum.headerStart;
|
113
|
+
} else {
|
114
|
+
this.data.header += String.fromCharCode(char);
|
115
|
+
}
|
116
|
+
|
117
|
+
break;
|
118
|
+
|
119
|
+
case MultiparParserStateEnum.data:
|
120
|
+
if (char === this.boundary[this.boundaryIndex]) {
|
121
|
+
this.boundaryIndex++;
|
122
|
+
} else {
|
123
|
+
this.boundaryIndex = 0;
|
124
|
+
}
|
125
|
+
|
126
|
+
if (this.boundaryIndex === this.boundary.length) {
|
127
|
+
this.state = MultiparParserStateEnum.headerStart;
|
128
|
+
|
129
|
+
if (this.data.value.length) {
|
130
|
+
this.appendFormData(
|
131
|
+
this.data.contentDisposition.name,
|
132
|
+
Buffer.from(this.data.value.slice(0, -(this.boundary.length + 1))),
|
133
|
+
this.data.contentDisposition.filename,
|
134
|
+
this.data.contentType
|
135
|
+
);
|
136
|
+
|
137
|
+
this.data.value = [];
|
138
|
+
this.data.contentDisposition = null;
|
139
|
+
this.data.contentType = null;
|
140
|
+
}
|
141
|
+
|
142
|
+
this.boundaryIndex = 0;
|
143
|
+
} else {
|
144
|
+
this.data.value.push(char);
|
145
|
+
}
|
146
|
+
|
147
|
+
break;
|
148
|
+
}
|
149
|
+
}
|
150
|
+
}
|
151
|
+
|
152
|
+
/**
|
153
|
+
* Ends the stream.
|
154
|
+
*
|
155
|
+
* @returns Form data.
|
156
|
+
*/
|
157
|
+
public end(): FormData {
|
158
|
+
if (this.state !== MultiparParserStateEnum.data) {
|
159
|
+
throw new DOMException(
|
160
|
+
`Unexpected end of multipart stream. Expected state to be "${MultiparParserStateEnum.data}" but got "${this.state}".`,
|
161
|
+
DOMExceptionNameEnum.invalidStateError
|
162
|
+
);
|
163
|
+
}
|
164
|
+
|
165
|
+
this.appendFormData(
|
166
|
+
this.data.contentDisposition.name,
|
167
|
+
Buffer.from(this.data.value.slice(0, -2)),
|
168
|
+
this.data.contentDisposition.filename,
|
169
|
+
this.data.contentType
|
170
|
+
);
|
171
|
+
|
172
|
+
return this.formData;
|
173
|
+
}
|
174
|
+
|
175
|
+
/**
|
176
|
+
* Appends data.
|
177
|
+
*
|
178
|
+
* @param key Key.
|
179
|
+
* @param value value.
|
180
|
+
* @param filename Filename.
|
181
|
+
* @param type Type.
|
182
|
+
*/
|
183
|
+
private appendFormData(key: string, value: Buffer, filename?: string, type?: string): void {
|
184
|
+
if (!value.length) {
|
185
|
+
return;
|
186
|
+
}
|
187
|
+
|
188
|
+
if (filename) {
|
189
|
+
this.formData.append(
|
190
|
+
key,
|
191
|
+
new File([value], filename, {
|
192
|
+
type
|
193
|
+
})
|
194
|
+
);
|
195
|
+
} else {
|
196
|
+
this.formData.append(key, value.toString());
|
197
|
+
}
|
198
|
+
}
|
199
|
+
|
200
|
+
/**
|
201
|
+
* Returns content disposition.
|
202
|
+
*
|
203
|
+
* @param headerValue Header value.
|
204
|
+
* @returns Content disposition.
|
205
|
+
*/
|
206
|
+
private getContentDisposition(headerValue: string): { [key: string]: string } {
|
207
|
+
const regex = /([a-z]+) *= *"([^"]+)"/g;
|
208
|
+
const contentDisposition: { [key: string]: string } = {};
|
209
|
+
let match: RegExpExecArray;
|
210
|
+
|
211
|
+
while ((match = regex.exec(headerValue))) {
|
212
|
+
contentDisposition[match[1]] = match[2];
|
213
|
+
}
|
214
|
+
|
215
|
+
return contentDisposition;
|
216
|
+
}
|
217
|
+
}
|
@@ -0,0 +1,80 @@
|
|
1
|
+
/**
|
2
|
+
* Fetch headers.
|
3
|
+
*
|
4
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/API/Headers
|
5
|
+
*/
|
6
|
+
export default interface IHeaders extends Iterable<[string, string]> {
|
7
|
+
/**
|
8
|
+
* Appends a new value onto an existing header inside a Headers object, or adds the header if it does not already exist.
|
9
|
+
*
|
10
|
+
* @param name Name.
|
11
|
+
* @param value Value.
|
12
|
+
*/
|
13
|
+
append(name: string, value: string): void;
|
14
|
+
|
15
|
+
/**
|
16
|
+
* Removes an header.
|
17
|
+
*
|
18
|
+
* @param name Name.
|
19
|
+
*/
|
20
|
+
delete(name: string): void;
|
21
|
+
|
22
|
+
/**
|
23
|
+
* Returns header value.
|
24
|
+
*
|
25
|
+
* @param name Name.
|
26
|
+
* @returns Value.
|
27
|
+
*/
|
28
|
+
get(name: string): string | null;
|
29
|
+
|
30
|
+
/**
|
31
|
+
* Sets a new value for an existing header inside a Headers object, or adds the header if it does not already exist.
|
32
|
+
*
|
33
|
+
* @param name Name.
|
34
|
+
* @param value Value.
|
35
|
+
*/
|
36
|
+
set(name: string, value: string): void;
|
37
|
+
|
38
|
+
/**
|
39
|
+
* Returns whether an Headers object contains a certain key.
|
40
|
+
*
|
41
|
+
* @param name Name.
|
42
|
+
* @returns "true" if the Headers object contains the key.
|
43
|
+
*/
|
44
|
+
has(name: string): boolean;
|
45
|
+
|
46
|
+
/**
|
47
|
+
* Executes a callback function once per each key/value pair in the Headers object.
|
48
|
+
*
|
49
|
+
* @param callback Callback.
|
50
|
+
*/
|
51
|
+
forEach(callback: (name: string, value: string, object: IHeaders) => void): void;
|
52
|
+
|
53
|
+
/**
|
54
|
+
* Returns an iterator, allowing you to go through all keys of the key/value pairs contained in this object.
|
55
|
+
*
|
56
|
+
* @returns Iterator.
|
57
|
+
*/
|
58
|
+
keys(): IterableIterator<string>;
|
59
|
+
|
60
|
+
/**
|
61
|
+
* Returns an iterator, allowing you to go through all values of the key/value pairs contained in this object.
|
62
|
+
*
|
63
|
+
* @returns Iterator.
|
64
|
+
*/
|
65
|
+
values(): IterableIterator<string>;
|
66
|
+
|
67
|
+
/**
|
68
|
+
* Returns an iterator, allowing you to go through all key/value pairs contained in this object.
|
69
|
+
*
|
70
|
+
* @returns Iterator.
|
71
|
+
*/
|
72
|
+
entries(): IterableIterator<[string, string]>;
|
73
|
+
|
74
|
+
/**
|
75
|
+
* Iterator.
|
76
|
+
*
|
77
|
+
* @returns Iterator.
|
78
|
+
*/
|
79
|
+
[Symbol.iterator](): IterableIterator<[string, string]>;
|
80
|
+
}
|
@@ -0,0 +1,65 @@
|
|
1
|
+
import IHeaders from './IHeaders';
|
2
|
+
import IBlob from '../../file/IBlob';
|
3
|
+
import AbortSignal from '../AbortSignal';
|
4
|
+
import Stream from 'stream';
|
5
|
+
import IRequestReferrerPolicy from './IRequestReferrerPolicy';
|
6
|
+
import IRequestRedirect from './IRequestRedirect';
|
7
|
+
import IRequestCredentials from './IRequestCredentials';
|
8
|
+
|
9
|
+
/**
|
10
|
+
* Fetch request.
|
11
|
+
*/
|
12
|
+
export default interface IRequest {
|
13
|
+
readonly headers: IHeaders;
|
14
|
+
readonly method: string;
|
15
|
+
readonly redirect: IRequestRedirect;
|
16
|
+
readonly referrer: string;
|
17
|
+
readonly url: string;
|
18
|
+
readonly body: Stream.Readable | null;
|
19
|
+
readonly bodyUsed: boolean;
|
20
|
+
readonly referrerPolicy: IRequestReferrerPolicy;
|
21
|
+
readonly signal: AbortSignal | null;
|
22
|
+
readonly credentials: IRequestCredentials;
|
23
|
+
|
24
|
+
/**
|
25
|
+
* Returns array buffer.
|
26
|
+
*
|
27
|
+
* @returns Array buffer.
|
28
|
+
*/
|
29
|
+
arrayBuffer(): Promise<ArrayBuffer>;
|
30
|
+
|
31
|
+
/**
|
32
|
+
* Returns blob.
|
33
|
+
*
|
34
|
+
* @returns Blob.
|
35
|
+
*/
|
36
|
+
blob(): Promise<IBlob>;
|
37
|
+
|
38
|
+
/**
|
39
|
+
* Returns buffer.
|
40
|
+
*
|
41
|
+
* @returns Buffer.
|
42
|
+
*/
|
43
|
+
buffer(): Promise<Buffer>;
|
44
|
+
|
45
|
+
/**
|
46
|
+
* Returns text.
|
47
|
+
*
|
48
|
+
* @returns Text.
|
49
|
+
*/
|
50
|
+
text(): Promise<string>;
|
51
|
+
|
52
|
+
/**
|
53
|
+
* Returns json.
|
54
|
+
*
|
55
|
+
* @returns JSON.
|
56
|
+
*/
|
57
|
+
json(): Promise<string>;
|
58
|
+
|
59
|
+
/**
|
60
|
+
* Clones request.
|
61
|
+
*
|
62
|
+
* @returns Clone.
|
63
|
+
*/
|
64
|
+
clone(): IRequest;
|
65
|
+
}
|
@@ -0,0 +1,15 @@
|
|
1
|
+
import { URLSearchParams } from 'url';
|
2
|
+
import FormData from '../../form-data/FormData';
|
3
|
+
import Blob from '../../file/Blob';
|
4
|
+
|
5
|
+
type IRequestBody =
|
6
|
+
| ArrayBuffer
|
7
|
+
| ArrayBufferView
|
8
|
+
| NodeJS.ReadableStream
|
9
|
+
| string
|
10
|
+
| URLSearchParams
|
11
|
+
| Blob
|
12
|
+
| FormData
|
13
|
+
| null;
|
14
|
+
|
15
|
+
export default IRequestBody;
|
@@ -0,0 +1,21 @@
|
|
1
|
+
import IHeadersInit from './IHeadersInit';
|
2
|
+
import AbortSignal from '../AbortSignal';
|
3
|
+
import { URL } from 'url';
|
4
|
+
import IRequestReferrerPolicy from './IRequestReferrerPolicy';
|
5
|
+
import IRequestRedirect from './IRequestRedirect';
|
6
|
+
import IRequestBody from './IRequestBody';
|
7
|
+
import IRequestCredentials from './IRequestCredentials';
|
8
|
+
|
9
|
+
/**
|
10
|
+
* Fetch request init.
|
11
|
+
*/
|
12
|
+
export default interface IRequestInit {
|
13
|
+
body?: IRequestBody;
|
14
|
+
headers?: IHeadersInit;
|
15
|
+
method?: string;
|
16
|
+
redirect?: IRequestRedirect;
|
17
|
+
signal?: AbortSignal | null;
|
18
|
+
referrer?: '' | 'no-referrer' | 'client' | string | URL;
|
19
|
+
credentials?: IRequestCredentials;
|
20
|
+
referrerPolicy?: IRequestReferrerPolicy;
|
21
|
+
}
|
@@ -0,0 +1,11 @@
|
|
1
|
+
type IRequestReferrerPolicy =
|
2
|
+
| ''
|
3
|
+
| 'no-referrer'
|
4
|
+
| 'no-referrer-when-downgrade'
|
5
|
+
| 'same-origin'
|
6
|
+
| 'origin'
|
7
|
+
| 'strict-origin'
|
8
|
+
| 'origin-when-cross-origin'
|
9
|
+
| 'strict-origin-when-cross-origin'
|
10
|
+
| 'unsafe-url';
|
11
|
+
export default IRequestReferrerPolicy;
|
@@ -0,0 +1,27 @@
|
|
1
|
+
import IHeaders from './IHeaders';
|
2
|
+
import IBlob from 'src/file/IBlob';
|
3
|
+
import Stream from 'stream';
|
4
|
+
|
5
|
+
/**
|
6
|
+
* Fetch response.
|
7
|
+
*
|
8
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/API/Response/Response
|
9
|
+
*/
|
10
|
+
export default interface IResponse {
|
11
|
+
readonly headers: IHeaders;
|
12
|
+
readonly ok: boolean;
|
13
|
+
readonly redirected: boolean;
|
14
|
+
readonly status: number;
|
15
|
+
readonly statusText: string;
|
16
|
+
readonly type: 'basic' | 'cors' | 'default' | 'error' | 'opaque' | 'opaqueredirect';
|
17
|
+
readonly url: string;
|
18
|
+
readonly body: Stream.Readable | null;
|
19
|
+
readonly bodyUsed: boolean;
|
20
|
+
|
21
|
+
arrayBuffer(): Promise<ArrayBuffer>;
|
22
|
+
blob(): Promise<IBlob>;
|
23
|
+
buffer(): Promise<Buffer>;
|
24
|
+
json(): Promise<unknown>;
|
25
|
+
text(): Promise<string>;
|
26
|
+
clone(): IResponse;
|
27
|
+
}
|
@@ -0,0 +1,15 @@
|
|
1
|
+
import { URLSearchParams } from 'url';
|
2
|
+
import FormData from '../../form-data/FormData';
|
3
|
+
import Blob from '../../file/Blob';
|
4
|
+
|
5
|
+
type IResponseBody =
|
6
|
+
| ArrayBuffer
|
7
|
+
| ArrayBufferView
|
8
|
+
| NodeJS.ReadableStream
|
9
|
+
| string
|
10
|
+
| URLSearchParams
|
11
|
+
| Blob
|
12
|
+
| FormData
|
13
|
+
| null;
|
14
|
+
|
15
|
+
export default IResponseBody;
|
@@ -0,0 +1,175 @@
|
|
1
|
+
import MultipartFormDataParser from '../multipart/MultipartFormDataParser';
|
2
|
+
import Stream from 'stream';
|
3
|
+
import { URLSearchParams } from 'url';
|
4
|
+
import FormData from '../../form-data/FormData';
|
5
|
+
import Blob from '../../file/Blob';
|
6
|
+
import DOMException from '../../exception/DOMException';
|
7
|
+
import DOMExceptionNameEnum from '../../exception/DOMExceptionNameEnum';
|
8
|
+
import IRequestBody from '../types/IRequestBody';
|
9
|
+
import IResponseBody from '../types/IResponseBody';
|
10
|
+
import Request from '../Request';
|
11
|
+
|
12
|
+
/**
|
13
|
+
* Fetch body utility.
|
14
|
+
*/
|
15
|
+
export default class FetchBodyUtility {
|
16
|
+
/**
|
17
|
+
* Parses body and returns stream and type.
|
18
|
+
*
|
19
|
+
* Based on:
|
20
|
+
* https://github.com/node-fetch/node-fetch/blob/main/src/body.js (MIT)
|
21
|
+
*
|
22
|
+
* @param body Body.
|
23
|
+
* @returns Stream and type.
|
24
|
+
*/
|
25
|
+
public static getBodyStream(body: IRequestBody | IResponseBody): {
|
26
|
+
contentType: string;
|
27
|
+
contentLength: number | null;
|
28
|
+
stream: Stream.Readable;
|
29
|
+
buffer: Buffer | null;
|
30
|
+
} {
|
31
|
+
if (body === null || body === undefined) {
|
32
|
+
return { stream: null, buffer: null, contentType: null, contentLength: null };
|
33
|
+
} else if (body instanceof URLSearchParams) {
|
34
|
+
const buffer = Buffer.from(body.toString());
|
35
|
+
return {
|
36
|
+
buffer,
|
37
|
+
stream: Stream.Readable.from(Buffer.from(buffer)),
|
38
|
+
contentType: 'application/x-www-form-urlencoded;charset=UTF-8',
|
39
|
+
contentLength: buffer.length
|
40
|
+
};
|
41
|
+
} else if (body instanceof Blob) {
|
42
|
+
const buffer = (<Blob>body)._buffer;
|
43
|
+
return {
|
44
|
+
buffer,
|
45
|
+
stream: Stream.Readable.from(buffer),
|
46
|
+
contentType: (<Blob>body).type,
|
47
|
+
contentLength: body.size
|
48
|
+
};
|
49
|
+
} else if (Buffer.isBuffer(body)) {
|
50
|
+
return {
|
51
|
+
buffer: body,
|
52
|
+
stream: Stream.Readable.from(body),
|
53
|
+
contentType: null,
|
54
|
+
contentLength: body.length
|
55
|
+
};
|
56
|
+
} else if (body instanceof ArrayBuffer) {
|
57
|
+
const buffer = Buffer.from(body);
|
58
|
+
return {
|
59
|
+
buffer,
|
60
|
+
stream: Stream.Readable.from(buffer),
|
61
|
+
contentType: null,
|
62
|
+
contentLength: body.byteLength
|
63
|
+
};
|
64
|
+
} else if (ArrayBuffer.isView(body)) {
|
65
|
+
const buffer = Buffer.from(body.buffer, body.byteOffset, body.byteLength);
|
66
|
+
return {
|
67
|
+
buffer,
|
68
|
+
stream: Stream.Readable.from(buffer),
|
69
|
+
contentType: null,
|
70
|
+
contentLength: body.byteLength
|
71
|
+
};
|
72
|
+
} else if (body instanceof Stream.Stream) {
|
73
|
+
return {
|
74
|
+
buffer: null,
|
75
|
+
stream: <Stream.Readable>body,
|
76
|
+
contentType: null,
|
77
|
+
contentLength: null
|
78
|
+
};
|
79
|
+
} else if (body instanceof FormData) {
|
80
|
+
return MultipartFormDataParser.formDataToStream(body);
|
81
|
+
}
|
82
|
+
|
83
|
+
const buffer = Buffer.from(String(body));
|
84
|
+
return {
|
85
|
+
buffer,
|
86
|
+
stream: Stream.Readable.from(buffer),
|
87
|
+
contentType: 'text/plain;charset=UTF-8',
|
88
|
+
contentLength: buffer.length
|
89
|
+
};
|
90
|
+
}
|
91
|
+
|
92
|
+
/**
|
93
|
+
* Clones a request body stream.
|
94
|
+
*
|
95
|
+
* @param request Request.
|
96
|
+
* @returns Stream.
|
97
|
+
*/
|
98
|
+
public static cloneRequestBodyStream(request: Request): Stream.Readable {
|
99
|
+
if (request.bodyUsed) {
|
100
|
+
throw new DOMException(
|
101
|
+
`Failed to clone body stream of request: Request body is already used.`,
|
102
|
+
DOMExceptionNameEnum.invalidStateError
|
103
|
+
);
|
104
|
+
}
|
105
|
+
|
106
|
+
const p1 = new Stream.PassThrough();
|
107
|
+
const p2 = new Stream.PassThrough();
|
108
|
+
|
109
|
+
request.body.pipe(p1);
|
110
|
+
request.body.pipe(p2);
|
111
|
+
|
112
|
+
// Sets the body of the cloned request to the first pass through stream.
|
113
|
+
(<Stream.Readable>request.body) = p1;
|
114
|
+
|
115
|
+
// Returns the clone.
|
116
|
+
return p2;
|
117
|
+
}
|
118
|
+
|
119
|
+
/**
|
120
|
+
* Consume and convert an entire Body to a Buffer.
|
121
|
+
*
|
122
|
+
* Based on:
|
123
|
+
* https://github.com/node-fetch/node-fetch/blob/main/src/body.js (MIT)
|
124
|
+
*
|
125
|
+
* @see https://fetch.spec.whatwg.org/#concept-body-consume-body
|
126
|
+
* @param body Body stream.
|
127
|
+
* @returns Promise.
|
128
|
+
*/
|
129
|
+
public static async consumeBodyStream(body: Stream.Readable | null): Promise<Buffer> {
|
130
|
+
if (body === null || !(body instanceof Stream.Stream)) {
|
131
|
+
return Buffer.alloc(0);
|
132
|
+
}
|
133
|
+
|
134
|
+
const chunks = [];
|
135
|
+
let bytes = 0;
|
136
|
+
|
137
|
+
try {
|
138
|
+
for await (const chunk of body) {
|
139
|
+
bytes += chunk.length;
|
140
|
+
chunks.push(chunk);
|
141
|
+
}
|
142
|
+
} catch (error) {
|
143
|
+
if (error instanceof DOMException) {
|
144
|
+
throw error;
|
145
|
+
}
|
146
|
+
throw new DOMException(
|
147
|
+
`Failed to read response body. Error: ${error.message}.`,
|
148
|
+
DOMExceptionNameEnum.encodingError
|
149
|
+
);
|
150
|
+
}
|
151
|
+
|
152
|
+
if (
|
153
|
+
(<Stream.Readable>body).readableEnded === false ||
|
154
|
+
(<Stream.Readable>body)['_readableState']?.ended === false
|
155
|
+
) {
|
156
|
+
throw new DOMException(
|
157
|
+
`Premature close of server response.`,
|
158
|
+
DOMExceptionNameEnum.invalidStateError
|
159
|
+
);
|
160
|
+
}
|
161
|
+
|
162
|
+
try {
|
163
|
+
if (typeof chunks[0] === 'string') {
|
164
|
+
return Buffer.from(chunks.join(''));
|
165
|
+
}
|
166
|
+
|
167
|
+
return Buffer.concat(chunks, bytes);
|
168
|
+
} catch (error) {
|
169
|
+
throw new DOMException(
|
170
|
+
`Could not create Buffer from response body. Error: ${error.message}.`,
|
171
|
+
DOMExceptionNameEnum.invalidStateError
|
172
|
+
);
|
173
|
+
}
|
174
|
+
}
|
175
|
+
}
|
@@ -0,0 +1,18 @@
|
|
1
|
+
/**
|
2
|
+
* Fetch CORS utility.
|
3
|
+
*/
|
4
|
+
export default class FetchCORSUtility {
|
5
|
+
/**
|
6
|
+
* Validates request headers.
|
7
|
+
*
|
8
|
+
* @param originURL Origin URL.
|
9
|
+
* @param targetURL Target URL.
|
10
|
+
*/
|
11
|
+
public static isCORS(originURL, targetURL): boolean {
|
12
|
+
return (
|
13
|
+
(originURL.hostname !== targetURL.hostname &&
|
14
|
+
!originURL.hostname.endsWith(targetURL.hostname)) ||
|
15
|
+
originURL.protocol !== targetURL.protocol
|
16
|
+
);
|
17
|
+
}
|
18
|
+
}
|