Skip to content
This repository has been archived by the owner on Nov 24, 2018. It is now read-only.

Introduce setEmulateMedia so that we can emulate print css for example #302

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,12 @@ export default class Chromeless<T extends any> implements Promise<T> {
return this
}

setEmulatedMedia(mediaType : 'screen' | 'print' | null): Chromeless<T> {
this.queue.enqueue({ type: 'setEmulatedMedia', mediaType })

return this
}

setHtml(html: string): Chromeless<T> {
this.queue.enqueue({ type: 'setHtml', html })

Expand Down
7 changes: 5 additions & 2 deletions src/chrome/local-runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
setHtml,
press,
setViewport,
setEmulateMedia,
clearCookies,
deleteCookie,
getCookies,
Expand Down Expand Up @@ -55,6 +56,8 @@ export default class LocalRuntime {
return this.goto(command.url)
case 'setViewport':
return setViewport(this.client, command.options)
case 'setEmulatedMedia':
return setEmulateMedia(this.client, command.mediaType)
case 'wait': {
if (command.selector) {
return this.waitSelector(command.selector, command.timeout)
Expand Down Expand Up @@ -383,7 +386,7 @@ export default class LocalRuntime {
const data = await screenshot(this.client, selector)

if (isS3Configured()) {
return await uploadToS3(data, 'image/png')
return await uploadToS3(data, 'image/png', options ? options.s3Options : {})
} else {
return writeToFile(data, 'png', options && options.filePath)
}
Expand All @@ -402,7 +405,7 @@ export default class LocalRuntime {
const data = await pdf(this.client, cdpOptions)

if (isS3Configured()) {
return await uploadToS3(data, 'application/pdf')
return await uploadToS3(data, 'application/pdf', options.s3Options)
} else {
return writeToFile(data, 'pdf', filePath)
}
Expand Down
1 change: 1 addition & 0 deletions src/chrome/local.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export default class LocalChrome implements Chrome {
this.chromeInstance = await launch({
logLevel: this.options.debug ? 'info' : 'silent',
port: this.options.cdp.port,
chromeFlags : this.options.chromeFlags
})
const target = await CDP.New({
port: this.chromeInstance.port,
Expand Down
13 changes: 13 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export interface ChromelessOptions {
launchChrome?: boolean // auto-launch chrome (local) `true`
cdp?: CDPOptions
remote?: RemoteOptions | boolean
chromeFlags? : string[]
}

export interface Chrome {
Expand All @@ -58,6 +59,10 @@ export interface Chrome {
}

export type Command =
|{
type:'setEmulatedMedia'
mediaType : 'screen' | 'print' | null
}
| {
type: 'goto'
url: string
Expand Down Expand Up @@ -217,10 +222,12 @@ export interface PdfOptions {
pageRanges?: string
ignoreInvalidPageRanges?: boolean
filePath?: string // for internal use
s3Options?: S3UploadOptions
}

export interface ScreenshotOptions {
filePath?: string
s3Options?: S3UploadOptions
}

export type Quad = Array<number>
Expand Down Expand Up @@ -248,3 +255,9 @@ export interface Viewport {
height: number
scale: number
}

export interface S3UploadOptions{
public?: boolean
signedPeriod? : number
fileName?:string
}
30 changes: 24 additions & 6 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as fs from 'fs'
import * as os from 'os'
import * as path from 'path'
import * as cuid from 'cuid'
import { Client, Cookie, DeviceMetrics, PdfOptions, BoxModel, Viewport } from './types'
import { Client, Cookie, DeviceMetrics, PdfOptions, BoxModel, Viewport, S3UploadOptions } from './types'
import * as CDP from 'chrome-remote-interface'
import * as AWS from 'aws-sdk'

Expand Down Expand Up @@ -53,6 +53,13 @@ export async function setViewport(
return
}

export async function setEmulateMedia(
client: Client,
mediaType: "screen" | "print" | null
): Promise<void> {
await client.Emulation.setEmulatedMedia({ media: mediaType || "" });
}

export async function waitForNode(
client: Client,
selector: string,
Expand Down Expand Up @@ -587,22 +594,33 @@ const s3ContentTypes = {
},
}

export async function uploadToS3(data: string, contentType: string): Promise<string> {
function getSignedUrl(bucket, key, period = 120) {
const s3Bucket = new AWS.S3({params: {Bucket: bucket}});
return s3Bucket.getSignedUrl('getObject', {
Bucket: bucket,
Key: key,
Expires: period
})
}

export async function uploadToS3(data: string, contentType: string, uploadOptions: S3UploadOptions = {}): Promise<string> {
const s3ContentType = s3ContentTypes[contentType]
if (!s3ContentType) {
throw new Error(`Unknown S3 Content type ${contentType}`)
}
const s3Path = `${getS3ObjectKeyPrefix()}${cuid()}.${s3ContentType.extension}`
const s3Path = `${getS3ObjectKeyPrefix()}${cuid()}${uploadOptions.fileName ? `/${uploadOptions.fileName}` : ""}.${s3ContentType.extension}`
const s3 = new AWS.S3()
await s3
.putObject({
Bucket: getS3BucketName(),
Key: s3Path,
ContentType: contentType,
ACL: 'public-read',
ACL: uploadOptions.public ? 'public-read' : null,
Body: Buffer.from(data, 'base64'),
})
.promise()

if (!uploadOptions.public && uploadOptions.signedPeriod){
return getSignedUrl(getS3BucketName(),s3Path,uploadOptions.signedPeriod );
}
return `https://${getS3BucketUrl()}/${s3Path}`
}
}