Skip to content

Commit

Permalink
chore: improve collection SSR
Browse files Browse the repository at this point in the history
  • Loading branch information
zensh committed Dec 2, 2023
1 parent af5379c commit 89e930f
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 60 deletions.
1 change: 1 addition & 0 deletions dist/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export async function initApp(app) {
router.get('/pub/:cid', renderPublication);
router.get('/group/:gid', renderGroup);
router.get('/group/:gid/collection', renderCollection);
router.get('/group/:gid/:other', renderGroup);
router.all('/:other+', (ctx) => {
ctx.redirect('/');
ctx.status = 307;
Expand Down
82 changes: 55 additions & 27 deletions dist/ssr.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,9 @@ export async function renderCollection(ctx) {
$('title').text(siteInfo.title);
$('meta[name="description"]').prop('content', siteInfo.desc);
}
if (!_cid || Array.isArray(_cid)) {
return renderGroup(ctx);
}
try {
const doc = await getCollection(headers, _cid);
const [language, info] = getCollectionInfo(doc, lang) ?? [];
Expand Down Expand Up @@ -453,40 +456,65 @@ async function listCollections(headers, gid) {
const api = new URL('/v1/collection/list', writingBase);
headers.accept = 'application/cbor';
headers['content-type'] = 'application/cbor';
const res = await fetch(api, {
method: 'POST',
headers,
body: Buffer.from(encode({
gid: gid.toBytes(),
status: 2,
fields: ['info', 'updated_at'],
})),
});
if (res.status !== 200) {
throw createError(res.status, await res.text());
const output = new Array();
const input = {
gid: gid.toBytes(),
page_size: 100,
status: 2,
fields: ['info', 'updated_at'],
page_token: undefined,
};
let i = 7;
while (i > 0) {
i -= 1;
const res = await fetch(api, {
method: 'POST',
headers,
body: Buffer.from(encode(input)),
});
if (res.status !== 200) {
break;
}
const data = await res.arrayBuffer();
const obj = decode(Buffer.from(data));
output.push(...obj.result);
if (!obj.next_page_token) {
break;
}
input.page_token = obj.next_page_token;
}
const data = await res.arrayBuffer();
const obj = decode(Buffer.from(data));
return obj.result;
return output;
}
async function listLatestCollections(headers) {
const api = new URL('/v1/collection/list_latest', writingBase);
headers.accept = 'application/cbor';
headers['content-type'] = 'application/cbor';
const res = await fetch(api, {
method: 'POST',
headers,
body: Buffer.from(encode({
page_size: 100,
fields: ['info', 'updated_at'],
})),
});
if (res.status !== 200) {
throw createError(res.status, await res.text());
const output = new Array();
const input = {
page_size: 100,
fields: ['info', 'updated_at'],
page_token: undefined,
};
let i = 7;
while (i > 0) {
i -= 1;
const res = await fetch(api, {
method: 'POST',
headers,
body: Buffer.from(encode(input)),
});
if (res.status !== 200) {
break;
}
const data = await res.arrayBuffer();
const obj = decode(Buffer.from(data));
output.push(...obj.result);
if (!obj.next_page_token) {
break;
}
input.page_token = obj.next_page_token;
}
const data = await res.arrayBuffer();
const obj = decode(Buffer.from(data));
return obj.result;
return output;
}
async function listCollectionChildren(headers, id) {
const api = new URL('/v1/collection/list_children', writingBase);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "webscraper",
"version": "1.2.1",
"version": "1.2.2",
"description": "",
"private": true,
"main": "dist/main.js",
Expand Down
1 change: 1 addition & 0 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export async function initApp(app: Koa): Promise<void> {
router.get('/pub/:cid', renderPublication)
router.get('/group/:gid', renderGroup)
router.get('/group/:gid/collection', renderCollection)
router.get('/group/:gid/:other', renderGroup)
router.all('/:other+', (ctx) => {
ctx.redirect('/')
ctx.status = 307
Expand Down
96 changes: 64 additions & 32 deletions src/ssr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,12 @@ export async function renderCollection(ctx: Context): Promise<void> {
$('meta[name="description"]').prop('content', siteInfo.desc)
}

if (!_cid || Array.isArray(_cid)) {
return renderGroup(ctx)
}

try {
const doc = await getCollection(headers, _cid as string)
const doc = await getCollection(headers, _cid)
const [language, info] = getCollectionInfo(doc, lang) ?? []
if (!info || !language) {
throw createError(404, 'collection not found')
Expand Down Expand Up @@ -690,25 +694,39 @@ async function listCollections(
const api = new URL('/v1/collection/list', writingBase)
headers.accept = 'application/cbor'
headers['content-type'] = 'application/cbor'
const res = await fetch(api, {
method: 'POST',
headers,
body: Buffer.from(
encode({
gid: gid.toBytes(),
status: 2,
fields: ['info', 'updated_at'],
})
),
})
const output = new Array<CollectionOutput>()

const input = {
gid: gid.toBytes(),
page_size: 100,
status: 2,
fields: ['info', 'updated_at'],
page_token: undefined,
}

if (res.status !== 200) {
throw createError(res.status, await res.text())
let i = 7
while (i > 0) {
i -= 1
const res = await fetch(api, {
method: 'POST',
headers,
body: Buffer.from(encode(input)),
})

if (res.status !== 200) {
break
}

const data = await res.arrayBuffer()
const obj = decode(Buffer.from(data))
output.push(...obj.result)
if (!obj.next_page_token) {
break
}
input.page_token = obj.next_page_token
}

const data = await res.arrayBuffer()
const obj = decode(Buffer.from(data))
return obj.result
return output
}

async function listLatestCollections(
Expand All @@ -717,24 +735,38 @@ async function listLatestCollections(
const api = new URL('/v1/collection/list_latest', writingBase)
headers.accept = 'application/cbor'
headers['content-type'] = 'application/cbor'
const res = await fetch(api, {
method: 'POST',
headers,
body: Buffer.from(
encode({
page_size: 100,
fields: ['info', 'updated_at'],
})
),
})
const output = new Array<CollectionOutput>()

if (res.status !== 200) {
throw createError(res.status, await res.text())
const input = {
page_size: 100,
fields: ['info', 'updated_at'],
page_token: undefined,
}

const data = await res.arrayBuffer()
const obj = decode(Buffer.from(data))
return obj.result
let i = 7
while (i > 0) {
i -= 1

const res = await fetch(api, {
method: 'POST',
headers,
body: Buffer.from(encode(input)),
})

if (res.status !== 200) {
break
}

const data = await res.arrayBuffer()
const obj = decode(Buffer.from(data))
output.push(...obj.result)
if (!obj.next_page_token) {
break
}
input.page_token = obj.next_page_token
}

return output
}

async function listCollectionChildren(
Expand Down

0 comments on commit 89e930f

Please sign in to comment.