-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #256 from kitsuyui/evaluate
Re-implement evaluates
- Loading branch information
Showing
8 changed files
with
240 additions
and
7 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
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
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,74 @@ | ||
import { parseRenderingProxyHeader } from './request_options'; | ||
|
||
describe('parseRenderingProxyHeader', () => { | ||
it('returns default when empty or invalid header value', async () => { | ||
expect(parseRenderingProxyHeader(undefined)).toStrictEqual({ | ||
waitUntil: 'networkidle', | ||
evaluates: [], | ||
}); | ||
expect(parseRenderingProxyHeader('')).toStrictEqual({ | ||
waitUntil: 'networkidle', | ||
evaluates: [], | ||
}); | ||
expect(parseRenderingProxyHeader([])).toStrictEqual({ | ||
waitUntil: 'networkidle', | ||
evaluates: [], | ||
}); | ||
expect(parseRenderingProxyHeader('1234')).toStrictEqual({ | ||
waitUntil: 'networkidle', | ||
evaluates: [], | ||
}); | ||
expect(parseRenderingProxyHeader('{')).toStrictEqual({ | ||
waitUntil: 'networkidle', | ||
evaluates: [], | ||
}); | ||
expect( | ||
parseRenderingProxyHeader('{"evaluates": 1234, "waitUntil": 3456}') | ||
).toStrictEqual({ | ||
waitUntil: 'networkidle', | ||
evaluates: [], | ||
}); | ||
}); | ||
|
||
it('parses evaluates', async () => { | ||
expect(parseRenderingProxyHeader('{"evaluates": []}')).toStrictEqual({ | ||
waitUntil: 'networkidle', | ||
evaluates: [], | ||
}); | ||
expect(parseRenderingProxyHeader('{"evaluates": ["1 + 1"]}')).toStrictEqual( | ||
{ | ||
waitUntil: 'networkidle', | ||
evaluates: ['1 + 1'], | ||
} | ||
); | ||
expect( | ||
parseRenderingProxyHeader('{"evaluates": ["1 + 1", "document.title"]}') | ||
).toStrictEqual({ | ||
waitUntil: 'networkidle', | ||
evaluates: ['1 + 1', 'document.title'], | ||
}); | ||
}); | ||
|
||
it('parses waitUntil', async () => { | ||
expect( | ||
parseRenderingProxyHeader('{"waitUntil": "networkidle"}') | ||
).toStrictEqual({ | ||
waitUntil: 'networkidle', | ||
evaluates: [], | ||
}); | ||
expect( | ||
parseRenderingProxyHeader('{"waitUntil": "domcontentloaded"}') | ||
).toStrictEqual({ | ||
waitUntil: 'domcontentloaded', | ||
evaluates: [], | ||
}); | ||
expect(parseRenderingProxyHeader('{"waitUntil": "load"}')).toStrictEqual({ | ||
waitUntil: 'load', | ||
evaluates: [], | ||
}); | ||
expect(parseRenderingProxyHeader('{"waitUntil": "commit"}')).toStrictEqual({ | ||
waitUntil: 'commit', | ||
evaluates: [], | ||
}); | ||
}); | ||
}); |
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,47 @@ | ||
import { LifecycleEvent, lifeCycleEvents } from '../render'; | ||
|
||
interface RequestOption { | ||
waitUntil: LifecycleEvent; | ||
evaluates: string[]; | ||
} | ||
|
||
export function parseRenderingProxyHeader( | ||
headerValue: undefined | string | string[] | ||
): RequestOption { | ||
if (typeof headerValue === 'string') { | ||
return parseOptions(headerValue); | ||
} | ||
if (Array.isArray(headerValue)) { | ||
return parseOptions(headerValue.join('')); | ||
} | ||
return parseOptions(''); | ||
} | ||
|
||
function parseOptions(text: string): RequestOption { | ||
let baseParsed: RequestOption = { | ||
waitUntil: 'networkidle', | ||
evaluates: [], | ||
}; | ||
try { | ||
baseParsed = JSON.parse(text); | ||
} catch (e) { | ||
// ignore | ||
} | ||
let waitUntil: LifecycleEvent = 'networkidle'; | ||
if (lifeCycleEvents.indexOf(baseParsed.waitUntil) > -1) { | ||
waitUntil = baseParsed.waitUntil as LifecycleEvent; | ||
} | ||
let evaluates: string[] = []; | ||
if ( | ||
baseParsed.evaluates && | ||
Array.isArray(baseParsed.evaluates) && | ||
baseParsed.evaluates.every((e: unknown) => typeof e === 'string') | ||
) { | ||
evaluates = baseParsed.evaluates; | ||
} | ||
|
||
return { | ||
waitUntil, | ||
evaluates, | ||
}; | ||
} |