Skip to content

Commit

Permalink
Update web contents
Browse files Browse the repository at this point in the history
  • Loading branch information
actions-user committed Sep 5, 2023
1 parent e99661c commit e53ed68
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 26 deletions.
7 changes: 6 additions & 1 deletion _includes/nav.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,13 @@
<li>
<a href="{{ site.basepath }}docs/apis/">APIs</a>
<ul class="nav nav-pills nav-stacked">
<li><a href="{{ site.basepath }}docs/apis/pjax/">Pjax</a></li>
<li><a href="{{ site.basepath }}docs/apis/pjax/">Pjax</a>
<ul class="nav nav-pills nav-stacked">
<li><a href="{{ site.basepath }}docs/apis/pjax/config/">Config</a></li>
</ul>
</li>
<li><a href="{{ site.basepath }}docs/apis/events/">Events</a></li>
<li><a href="{{ site.basepath }}docs/apis/util/">Util</a></li>
</ul>
</li>
</ul>
Expand Down
118 changes: 95 additions & 23 deletions assets/dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6030,7 +6030,7 @@ class ReadonlyURLSearchParams extends URLSearchParams {
Object.defineProperty(exports, "__esModule", ({
value: true
}));
exports["default"] = exports.Pjax = void 0;
exports.FakeXMLHttpRequest = exports["default"] = exports.Pjax = void 0;
var gui_1 = __webpack_require__(524);
Object.defineProperty(exports, "Pjax", ({
enumerable: true,
Expand All @@ -6044,6 +6044,13 @@ Object.defineProperty(exports, "default", ({
return gui_1.GUI;
}
}));
var xhr_1 = __webpack_require__(5061);
Object.defineProperty(exports, "FakeXMLHttpRequest", ({
enumerable: true,
get: function () {
return xhr_1.FakeXMLHttpRequest;
}
}));

/***/ }),

Expand Down Expand Up @@ -6227,7 +6234,7 @@ class Config {
wait: 0
};
this.update = {
rewrite: (_path, _doc, _area, _memory) => undefined,
rewrite: (_url, _document, _area, _cache) => undefined,
head: 'base, meta, link',
css: true,
script: true,
Expand Down Expand Up @@ -6609,7 +6616,7 @@ function xhr(method, displayURL, base, headers, body, timeout, cache, cancellati
headers.set('If-None-Match', cache.get(displayURL.path).etag);
}
return new promise_1.AtomicPromise(resolve => {
const xhr = rewrite(displayURL.path, method, headers, timeout, body);
const xhr = rewrite(displayURL.href, method, headers, timeout, body) ?? request(displayURL.href, method, headers, timeout, body);
if (xhr.responseType !== 'document') throw new Error(`Response type must be 'document'`);
xhr.addEventListener("abort", () => void resolve((0, either_1.Left)(new Error(`Failed to request a page by abort`))));
xhr.addEventListener("error", () => void resolve((0, either_1.Left)(new Error(`Failed to request a page by error`))));
Expand Down Expand Up @@ -6638,9 +6645,9 @@ function xhr(method, displayURL, base, headers, body, timeout, cache, cancellati
});
}
exports.xhr = xhr;
function request(path, method, headers, timeout, body) {
function request(url, method, headers, timeout, body) {
const xhr = new XMLHttpRequest();
xhr.open(method, path, true);
xhr.open(method, url, true);
for (const [name, value] of headers) {
xhr.setRequestHeader(name, value);
}
Expand All @@ -6650,23 +6657,21 @@ function request(path, method, headers, timeout, body) {
return xhr;
}
function verify(base, method, xhr, cache) {
return (0, either_1.Right)(xhr).bind(xhr => {
const url = new url_1.URL((0, url_1.standardize)(xhr.responseURL, base.href));
switch (true) {
case !xhr.responseURL:
return (0, either_1.Left)(new Error(`Failed to get the response URL`));
case url.origin !== new url_1.URL('', window.location.origin).origin:
return (0, either_1.Left)(new Error(`Redirected to another origin`));
case !/2..|304/.test(`${xhr.status}`):
return (0, either_1.Left)(new Error(`Failed to validate the status of response`));
case !xhr.response:
return method === 'GET' && xhr.status === 304 && cache.has(url.path) ? (0, either_1.Right)(cache.get(url.path).xhr) : (0, either_1.Left)(new Error(`Failed to get the response body`));
case !match(xhr.getResponseHeader('Content-Type'), 'text/html'):
return (0, either_1.Left)(new Error(`Failed to validate the content type of response`));
default:
return (0, either_1.Right)(xhr);
}
});
const url = new url_1.URL((0, url_1.standardize)(xhr.responseURL, base.href));
switch (true) {
case !xhr.responseURL:
return (0, either_1.Left)(new Error(`Failed to get the response URL`));
case url.origin !== new url_1.URL('', window.location.origin).origin:
return (0, either_1.Left)(new Error(`Redirected to another origin`));
case !/2..|304/.test(`${xhr.status}`):
return (0, either_1.Left)(new Error(`Failed to validate the status of response`));
case !xhr.response:
return method === 'GET' && xhr.status === 304 && cache.has(url.path) ? (0, either_1.Right)(cache.get(url.path).xhr) : (0, either_1.Left)(new Error(`Failed to get the response body`));
case !match(xhr.getResponseHeader('Content-Type'), 'text/html'):
return (0, either_1.Left)(new Error(`Failed to validate the content type of response`));
default:
return (0, either_1.Right)(xhr);
}
}
function match(actualContentType, expectedContentType) {
const as = parse(actualContentType || '').sort();
Expand Down Expand Up @@ -6733,7 +6738,7 @@ function update({
};
return promise_1.AtomicPromise.resolve(seq).then(process.either).then(m => m.bind(() => (0, content_1.separate)(documents, config.areas).extract(() => (0, either_1.Left)(new Error(`Failed to separate the areas`)), () => m))).then(m => m.bind(seqA => (0, content_1.separate)(documents, config.areas).fmap(([area]) => [seqA, area]).extract(() => (0, either_1.Left)(new Error(`Failed to separate the areas`)), process.either)).fmap(([seqB, area]) => {
const memory = event.type === router_1.RouterEventType.Popstate ? config.memory?.get(event.location.dest.path) : undefined;
config.update.rewrite(event.location.dest.path, documents.src, area, memory && (0, content_1.separate)({
config.update.rewrite(event.location.dest.href, documents.src, area, memory && (0, content_1.separate)({
src: memory,
dst: documents.dst
}, [area]).extract(() => false) ? memory : undefined);
Expand Down Expand Up @@ -7931,6 +7936,73 @@ function test(parser) {

/***/ }),

/***/ 5061:
/***/ ((__unused_webpack_module, exports, __webpack_require__) => {

"use strict";


Object.defineProperty(exports, "__esModule", ({
value: true
}));
exports.FakeXMLHttpRequest = void 0;
const promise_1 = __webpack_require__(4879);
class FakeXMLHttpRequest extends XMLHttpRequest {
static create(url, response) {
const xhr = new FakeXMLHttpRequest();
promise_1.AtomicPromise.resolve(response).then(response => {
Object.defineProperties(xhr, {
responseURL: {
value: url
},
responseXML: {
value: response
}
});
xhr.send();
});
return xhr;
}
constructor() {
super();
this.responseType = 'document';
}
send(_) {
let state = 3;
Object.defineProperties(this, {
readyState: {
get: () => state
},
status: {
value: 200
},
statusText: {
value: 'OK'
},
response: {
get: () => this.responseType === 'document' ? this.responseXML : this.responseText
}
});
setTimeout(() => {
this.dispatchEvent(new ProgressEvent('loadstart'));
state = 4;
this.dispatchEvent(new ProgressEvent('loadend'));
this.dispatchEvent(new ProgressEvent('load'));
});
}
getResponseHeader(name) {
switch (name.toLowerCase()) {
case 'content-type':
return 'text/html';
default:
return null;
}
}
}
exports.FakeXMLHttpRequest = FakeXMLHttpRequest;

/***/ }),

/***/ 3252:
/***/ (function(module) {

Expand Down
4 changes: 4 additions & 0 deletions docs/apis/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,7 @@ Pjax APIs.
## [Events]({{ site.basepath }}docs/apis/events/)

Global events.

## [Util]({{ site.basepath }}docs/apis/util/)

Utilities.
4 changes: 2 additions & 2 deletions docs/apis/pjax/config/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ Set a dictionary object having has/get/set/delete methods of Map to pass the doc

## fetch: {...} = ...

### rewrite: (path: string, method: string, headers: Headers, timeout: number, body: FormData | null) => XMLHttpRequest
### rewrite: (url: string, method: string, headers: Headers, timeout: number, body: FormData | null) => XMLHttpRequest | undefined

Rewrite the XHR object, or replace it with another or fake.

Expand All @@ -79,7 +79,7 @@ Wait for the specified milliseconds after sending a request.

## update: {...} = ...

### rewrite: (doc: Document, area: string, memory?: Document) => void = `() => undefined`
### rewrite: (url: string, document: Document, area: string, cache?: Document) => void = `() => undefined`

Rewrite the source document object.
If you use the sequence option, you should use only it instead of this.
Expand Down
31 changes: 31 additions & 0 deletions docs/apis/util/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
---
layout: layout
title: Util
type: page
nav: nav
class: style-api style-api-detail
---

# Util

## FakeXMLHttpRequest

Make a fake XHR object that doesn't send a request.

```ts
import Pjax, { FakeXMLHttpRequest } from 'pjax-api';

new Pjax({
fetch: {
rewrite: url =>
FakeXMLHttpRequest.create(
url,
fetch(url, { headers: { 'Content-Type': 'application/json' } })
.then(res => res.json())
.then(data =>
new DOMParser().parseFromString(
`<title>${data.title}</title><body>${data.body}</body>`,
'text/html'))),
},
});
```
2 changes: 2 additions & 0 deletions index.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ This site is also powered by PJAX as a demo. Try page transitions.

- [APIs]({{ site.basepath }}docs/apis/)
- [Pjax]({{ site.basepath }}docs/apis/pjax/)
- [Config]({{ site.basepath }}docs/apis/pjax/config/)
- [Events]({{ site.basepath }}docs/apis/events/)
- [Util]({{ site.basepath }}docs/apis/util/)
</div>

<div class="col-md-4">
Expand Down

0 comments on commit e53ed68

Please sign in to comment.