UNPKG

node-web-mvc

Version:
152 lines (151 loc) 6.19 kB
"use strict"; /** * @module MediaType * @description 内容类型 */ Object.defineProperty(exports, "__esModule", { value: true }); class MediaType { get charset() { return (this.parameters.charset || 'utf-8'); } get name() { return `${this.type}/${this.subtype}`; } get isWildcardType() { return this.type === '*'; } get isWildcardSubtype() { return this.subtype[0] === '*'; } get subTypeSuffix() { return this.subtype.split('+').pop(); } get isConcrete() { return !this.isWildcardType && !this.isWildcardSubtype; } isPresentIn(mimeTypes) { return mimeTypes.find((m) => m.type.toLowerCase() == this.type.toLowerCase() && m.subtype.toLowerCase() == this.subtype.toLowerCase()); } isCompatibleWith(other) { if (!other) return false; const thisSuffix = this.subTypeSuffix; const otherSuffix = other.subTypeSuffix; if (!other) { return false; } else if (this.isWildcardType || other.isWildcardType) { // 如果是类型通配符,则返回 true return true; } else if (this.type != other.type) { // 如果主类型不匹配,则返回false return false; } else if (this.subtype == other.subtype) { // 如果完全匹配,返回 true return true; } else if (this.subtype === '*' || other.subtype === '*') { // 在主类型匹配下,如果子类型是通配符,则返回 true return true; } else if (this.isWildcardSubtype && thisSuffix) { return thisSuffix == other.subtype || thisSuffix == otherSuffix; } else if (other.isWildcardSubtype && otherSuffix) { return otherSuffix == this.subtype || otherSuffix == thisSuffix; } else { return false; } } constructor(mediaType, sub, parameters) { if (arguments.length === 1) { const parts = (mediaType || '').split(';'); const segments = parts.shift().split('/'); this.type = segments.shift().toLowerCase().trim(); this.subtype = segments.join('/').toLowerCase().trim(); this.parameters = {}; parts.forEach((part) => { const kv = part.split('='); this.parameters[kv[0].trim()] = kv[1].trim(); }); } else { this.type = (mediaType || '').toLowerCase().trim(); this.subtype = (sub || '').toLowerCase().trim(); this.parameters = parameters || {}; } } isEmpty() { return !this.type && !this.subtype; } toString() { const keys = Object.keys(this.parameters || {}); const joinChar = (keys === null || keys === void 0 ? void 0 : keys.length) > 0 ? ';' : ''; return `${this.name}${joinChar}${keys.map((k) => `${k}=${this.parameters[k]}`)}`; } copyQualityValue(mediaType) { if (mediaType.parameters['q']) { // 复制质量因子 const parameters = {}; Object.keys(this.parameters).forEach((k) => { parameters[k] = this.parameters[k]; }); parameters['q'] = mediaType.parameters['q']; return new MediaType(this.type, this.subtype, parameters); } return this; } static specificityCompare(mediaType1, mediaType2) { if (mediaType1.isWildcardType && !mediaType2.isWildcardType) { return 1; } else if (mediaType2.isWildcardType && !mediaType1.isWildcardType) { return -1; } else if (mediaType1.type != mediaType2.type) { return 0; } else if (mediaType1.isWildcardSubtype && !mediaType2.isWildcardSubtype) { return 1; } else if (mediaType2.isWildcardSubtype && !mediaType1.isWildcardSubtype) { return -1; } else if (mediaType1.subtype !== mediaType2.subtype) { return 0; } const size1 = Object.keys(mediaType1.parameters || {}).length; const size2 = Object.keys(mediaType2.parameters || {}).length; return size2 - size1; } } MediaType.ALL = new MediaType('*', '*'); MediaType.APPLICATION_ATOM_XML = new MediaType('application', 'atom+xml'); MediaType.APPLICATION_CBOR = new MediaType('application', 'cbor'); MediaType.APPLICATION_FORM_URLENCODED = new MediaType('application', 'x-www-form-urlencoded'); MediaType.APPLICATION_JSON = new MediaType('application', 'json'); MediaType.APPLICATION_JSON_UTF8 = new MediaType('application', 'json', { charset: 'utf-8' }); MediaType.APPLICATION_OCTET_STREAM = new MediaType('application', 'octet-stream'); MediaType.APPLICATION_PDF = new MediaType('application', 'pdf'); MediaType.APPLICATION_PROBLEM_JSON = new MediaType('application', 'problem+json'); MediaType.APPLICATION_PROBLEM_JSON_UTF8 = new MediaType('application', 'problem+json', { charset: 'utf-8' }); MediaType.APPLICATION_PROBLEM_XML = new MediaType('application', 'problem+xml'); MediaType.APPLICATION_RSS_XML = new MediaType('application', 'rss+xml'); MediaType.APPLICATION_STREAM_JSON = new MediaType('application', 'stream+json'); MediaType.APPLICATION_XHTML_XML = new MediaType('application', 'xhtml+xml'); MediaType.APPLICATION_XML = new MediaType('application', 'xml'); MediaType.IMAGE_GIF = new MediaType('image', 'gif'); MediaType.IMAGE_JPEG = new MediaType('image', 'jpeg'); MediaType.IMAGE_PNG = new MediaType('image', 'png'); MediaType.MULTIPART_FORM_DATA = new MediaType('multipart', 'form-data'); MediaType.MULTIPART_MIXED = new MediaType('multipart', 'mixed'); MediaType.MULTIPART_RELATED = new MediaType('multipart', 'related'); MediaType.TEXT_EVENT_STREAM = new MediaType('text', 'event-stream'); MediaType.TEXT_HTML = new MediaType('text', 'html'); MediaType.TEXT_MARKDOWN = new MediaType('text', 'markdown'); MediaType.TEXT_PLAIN = new MediaType('text', 'plain'); MediaType.TEXT_XML = new MediaType('text', 'xml'); exports.default = MediaType;