MCPcopy Index your code
hub / github.com/nodejs/node / parseTypeAndSubtype

Function parseTypeAndSubtype

lib/internal/mime.js:42–83  ·  view source on GitHub ↗
(str)

Source from the content-addressed store, hash-verified

40const SEMICOLON = ';';
41
42function parseTypeAndSubtype(str) {
43 // Skip only HTTP whitespace from start
44 let position = SafeStringPrototypeSearch(str, END_BEGINNING_WHITESPACE);
45 // read until '/'
46 const typeEnd = StringPrototypeIndexOf(str, SOLIDUS, position);
47 const trimmedType = typeEnd === -1 ?
48 StringPrototypeSlice(str, position) :
49 StringPrototypeSlice(str, position, typeEnd);
50 const invalidTypeIndex = SafeStringPrototypeSearch(trimmedType,
51 NOT_HTTP_TOKEN_CODE_POINT);
52 if (trimmedType === '' || invalidTypeIndex !== -1 || typeEnd === -1) {
53 throw new ERR_INVALID_MIME_SYNTAX('type', str, invalidTypeIndex);
54 }
55 // skip type and '/'
56 position = typeEnd + 1;
57 const type = toASCIILower(trimmedType);
58 // read until ';'
59 const subtypeEnd = StringPrototypeIndexOf(str, SEMICOLON, position);
60 const rawSubtype = subtypeEnd === -1 ?
61 StringPrototypeSlice(str, position) :
62 StringPrototypeSlice(str, position, subtypeEnd);
63 position += rawSubtype.length;
64 if (subtypeEnd !== -1) {
65 // skip ';'
66 position += 1;
67 }
68 const trimmedSubtype = StringPrototypeSlice(
69 rawSubtype,
70 0,
71 SafeStringPrototypeSearch(rawSubtype, START_ENDING_WHITESPACE));
72 const invalidSubtypeIndex = SafeStringPrototypeSearch(trimmedSubtype,
73 NOT_HTTP_TOKEN_CODE_POINT);
74 if (trimmedSubtype === '' || invalidSubtypeIndex !== -1) {
75 throw new ERR_INVALID_MIME_SYNTAX('subtype', str, invalidSubtypeIndex);
76 }
77 const subtype = toASCIILower(trimmedSubtype);
78 return [
79 type,
80 subtype,
81 position,
82 ];
83}
84
85const EQUALS_SEMICOLON_OR_END = /[;=]|$/;
86const QUOTED_VALUE_PATTERN = /^(?:([\\]$)|[\\][\s\S]|[^"])*(?:(")|$)/u;

Callers 1

constructorMethod · 0.85

Calls 1

toASCIILowerFunction · 0.85

Tested by

no test coverage detected