-
Notifications
You must be signed in to change notification settings - Fork 27.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wip removing unnecessary decode operations
- Loading branch information
1 parent
51b878f
commit a47aa8a
Showing
3 changed files
with
104 additions
and
31 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
export const ENCODED_TAGS = { | ||
OPENING: {}, | ||
CLOSED: { | ||
// </head> | ||
HEAD: new Uint8Array([60, 47, 104, 101, 97, 100, 62]), | ||
// </body> | ||
BODY: new Uint8Array([60, 47, 98, 111, 100, 121, 62]), | ||
// </html> | ||
HTML: new Uint8Array([60, 47, 104, 116, 109, 108, 62]), | ||
// </body></html> | ||
BODY_AND_HTML: new Uint8Array([ | ||
60, 47, 98, 111, 100, 121, 62, 60, 47, 104, 116, 109, 108, 62, | ||
]), | ||
}, | ||
} as const |
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
51 changes: 51 additions & 0 deletions
51
packages/next/src/server/stream-utils/uint8array-helpers.ts
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,51 @@ | ||
/** | ||
* Find the starting index of Uint8Array `b` within Uint8Array `a`. | ||
*/ | ||
export function indexOfUint8Array(a: Uint8Array, b: Uint8Array) { | ||
if (a.length === 0 || b.length === 0 || b.length > a.length) return -1 | ||
|
||
// start iterating through `a` | ||
for (let i = 0; i <= a.length - b.length; i++) { | ||
let completeMatch = true | ||
// from index `i`, iterate through `b` and check for mismatch | ||
for (let j = 0; j < b.length; j++) { | ||
// if the values do not match, then this isn't a complete match, exit `b` iteration early and iterate to next index of `a`. | ||
if (a[i + j] !== b[j]) { | ||
completeMatch = false | ||
break | ||
} | ||
} | ||
|
||
if (completeMatch) { | ||
return i | ||
} | ||
} | ||
|
||
return -1 | ||
} | ||
|
||
/** | ||
* Check if two Uint8Arrays are strictly equivalent. | ||
*/ | ||
export function isEquivalentUint8Arrays(a: Uint8Array, b: Uint8Array) { | ||
return a.length === b.length && a.every((v, i) => v === b[i]) | ||
} | ||
|
||
/** | ||
* Remove Uint8Array `b` from Uint8Array `a`. | ||
* | ||
* If `b` is not in `a`, `a` is returned unchanged. | ||
* | ||
* Otherwise, the function returns a new Uint8Array instance with size `a.length - b.length` | ||
*/ | ||
export function removeFromUint8Array(a: Uint8Array, b: Uint8Array) { | ||
const tagIndex = indexOfUint8Array(a, b) | ||
if (tagIndex) { | ||
const removed = new Uint8Array(a.length - b.length) | ||
removed.set(a.slice(0, tagIndex)) | ||
removed.set(a.slice(tagIndex + b.length), tagIndex) | ||
return removed | ||
} else { | ||
return a | ||
} | ||
} |