-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: res content-type named router
- Loading branch information
Showing
7 changed files
with
133 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
const { Transform } = require('stream'); | ||
const logger = require('./logger'); | ||
|
||
// Define a Transform stream to parse JSON objects line by line | ||
class JSONStream extends Transform { | ||
constructor(options) { | ||
super({ ...options, objectMode: true }); | ||
this.buffer = ''; | ||
} | ||
|
||
_transform(chunk, encoding, callback) { | ||
this.buffer += chunk.toString(); // Append chunk to buffer | ||
const lines = this.buffer.split('\n'); // Split buffer into lines | ||
|
||
// Process complete lines | ||
while (lines.length > 1) { | ||
const line = lines.shift() || ''; // Get the first complete line | ||
if (line.trim() === '') continue; // Skip empty lines | ||
try { | ||
const obj = JSON.parse(line); // Parse JSON object | ||
logger.debug('data chunk %j', obj); | ||
this.push(obj); // Push the object to the stream | ||
} catch (error) { | ||
this.emit('error', error); // Emit error if JSON parsing fails | ||
} | ||
} | ||
|
||
// Save the incomplete line for the next chunk | ||
this.buffer = lines[0] || ''; | ||
|
||
callback(); // Callback to indicate processing is complete | ||
} | ||
|
||
_flush(callback) { | ||
// Process any remaining data in the buffer | ||
if (this.buffer.trim() !== '') { | ||
try { | ||
const obj = JSON.parse(this.buffer); // Parse JSON object | ||
logger.info('buffer %j', obj); | ||
this.push(obj); // Push the object to the stream | ||
} catch (error) { | ||
this.emit('error', error); // Emit error if JSON parsing fails | ||
} | ||
} | ||
callback(); // Callback to indicate processing is complete | ||
} | ||
} | ||
|
||
module.exports = JSONStream; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const { getStaticFile } = require('../common/utils'); | ||
const logger = require('../common/logger'); | ||
const utils = require('../common/utils'); | ||
const JSONStream = require('../common/jsonStream'); | ||
|
||
const prefix = '/res'; | ||
module.exports = router => { | ||
router | ||
.get(`${prefix}/:contentType`, async (ctx, next) => { | ||
const { contentType } = ctx.params; | ||
switch (contentType) { | ||
case 'html': | ||
ctx.type = 'text/html'; | ||
ctx.body = getStaticFile('index.html'); | ||
break; | ||
case 'text': | ||
ctx.type = 'text/plain'; | ||
ctx.body = getStaticFile('index.txt'); | ||
break; | ||
case 'img': | ||
ctx.type = 'image/png'; | ||
// ctx.body = fs.readFileSync(path.resolve('static', 'index.png')); | ||
// ctx.body = fs.readFileSync(path.resolve('static', 'index.png')).toString('base64'); | ||
ctx.body = getStaticFile('index.png'); | ||
break; | ||
case 'video': | ||
ctx.type = 'video/mp4'; | ||
ctx.body = getStaticFile('index.mp4'); | ||
break; | ||
case 'css': | ||
ctx.type = 'text/css'; | ||
ctx.body = getStaticFile('index.css'); | ||
break; | ||
case 'xml': | ||
ctx.type = 'application/xml'; | ||
ctx.body = getStaticFile('index.xml'); | ||
break; | ||
case 'js': | ||
ctx.type = 'application/javascript'; | ||
ctx.body = getStaticFile('index.js'); | ||
break; | ||
case 'file': | ||
const fileName = 'index.txt'; | ||
ctx.set('Content-Disposition', `attachment; filename="${fileName}"`); | ||
ctx.type = 'application/octet-stream'; | ||
ctx.body = getStaticFile(fileName); | ||
break; | ||
|
||
case 'json': | ||
default: | ||
ctx.type = 'application/json'; | ||
// WARNNING json must be write one-time | ||
// ctx.body = getStaticFile('index.json'); | ||
ctx.body = JSON.parse( | ||
fs.readFileSync(path.resolve(__dirname, '../../static/index.json')).toString() | ||
); | ||
break; | ||
} | ||
await next(); | ||
}) | ||
.get(`${prefix}/stream/:streamType`, async ctx => { | ||
const { streamType } = ctx.params; | ||
const jsonType = streamType == 'json'; | ||
const data = jsonType | ||
? fs.readFileSync(path.resolve('static', 'index.json.text')).toString().split(/\n/) | ||
: Array.from({ length: 3 }).map((val, i) => `This is chunk ${i + 1}`); | ||
|
||
ctx.res.writeHead(200, { 'Content-Type': 'text/plain' }); | ||
|
||
for (let index = 0; index < data.length; index++) { | ||
const chunk = data[index]; | ||
ctx.res.write(chunk); | ||
index != data.length - 1 && (await utils.waitForSeconds(3e3)); | ||
} | ||
// Optionally, you can end the response after writing all data | ||
ctx.res.end(); | ||
}); | ||
}; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# File |