Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: all modules into NAPI-RS v2 API #542

Merged
merged 1 commit into from
Aug 23, 2022
Merged
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
1 change: 0 additions & 1 deletion __test__/image-data.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ test('properties should be readonly', (t) => {
const fakeData = new Uint8ClampedArray()
const expectation = {
instanceOf: TypeError,
message: /Cannot assign to read only property/,
}

// @ts-expect-error
Expand Down
78 changes: 1 addition & 77 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ const {
clearAllCache,
CanvasRenderingContext2D,
CanvasElement,
createContext,
SVGCanvas,
Path2D,
ImageData,
Expand Down Expand Up @@ -47,84 +46,9 @@ Object.defineProperty(GlobalFonts, 'has', {
writable: false,
})

CanvasRenderingContext2D.prototype.createPattern = function createPattern(image, repetition) {
if (image instanceof ImageData) {
const pattern = new CanvasPattern(image, repetition, 0)
Object.defineProperty(pattern, '_imageData', {
writable: true,
configurable: false,
enumerable: false,
value: null,
})
return pattern
} else if (image instanceof Image) {
return new CanvasPattern(image, repetition, 1)
} else if (image instanceof CanvasElement || image instanceof SVGCanvas) {
return new CanvasPattern(image, repetition, 2)
}
throw TypeError('Image should be instance of ImageData or Image')
}

CanvasRenderingContext2D.prototype.getImageData = function getImageData(x, y, w, h) {
const data = this._getImageData(x, y, w, h)
return new ImageData(data, w, h)
}

function createCanvas(width, height, flag) {
const isSvgBackend = typeof flag !== 'undefined'
const canvasElement = isSvgBackend ? new SVGCanvas(width, height) : new CanvasElement(width, height)

let ctx
canvasElement.getContext = function getContext(type, attr = {}) {
if (type !== '2d') {
throw new Error('Unsupported type')
}
const attrs = { alpha: true, colorSpace: 'srgb', ...attr }
ctx = ctx
? ctx
: isSvgBackend
? new CanvasRenderingContext2D(this.width, this.height, attrs.colorSpace, flag)
: new CanvasRenderingContext2D(this.width, this.height, attrs.colorSpace)
createContext(ctx, this.width, this.height, attrs)

// napi can not define writable: true but enumerable: false property
Object.defineProperty(ctx, '_fillStyle', {
value: '#000',
configurable: false,
enumerable: false,
writable: true,
})

Object.defineProperty(ctx, '_strokeStyle', {
value: '#000',
configurable: false,
enumerable: false,
writable: true,
})

Object.defineProperty(ctx, 'createImageData', {
value: function createImageData(widthOrImage, height, attrs = {}) {
if (widthOrImage instanceof ImageData) {
return new ImageData(widthOrImage.data, widthOrImage.width, widthOrImage.height)
}
return new ImageData(widthOrImage, height, { colorSpace: 'srgb', ...attrs })
},
configurable: true,
enumerable: false,
writable: true,
})

Object.defineProperty(canvasElement, 'ctx', {
value: ctx,
enumerable: false,
configurable: false,
})

ctx.canvas = canvasElement

return ctx
}

const canvasElement = isSvgBackend ? new SVGCanvas(width, height, flag) : new CanvasElement(width, height)
const {
encode: canvasEncode,
encodeSync: canvasEncodeSync,
Expand Down
2 changes: 0 additions & 2 deletions js-binding.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,6 @@ const {
clearAllCache,
CanvasRenderingContext2D,
CanvasElement,
createContext,
SVGCanvas,
Path,
ImageData,
Expand All @@ -216,7 +215,6 @@ const {
module.exports.clearAllCache = clearAllCache
module.exports.CanvasRenderingContext2D = CanvasRenderingContext2D
module.exports.CanvasElement = CanvasElement
module.exports.createContext = createContext
module.exports.SVGCanvas = SVGCanvas
module.exports.Path2D = Path
module.exports.ImageData = ImageData
Expand Down
10 changes: 8 additions & 2 deletions load-image.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,13 @@ module.exports = async function loadImage(source, options = {}) {
source = !(source instanceof URL) ? new URL(source) : source
// attempt to download the remote source and construct image
const data = await new Promise((resolve, reject) =>
makeRequest(source, resolve, reject, options.maxRedirects ?? MAX_REDIRECTS, options.requestOptions),
makeRequest(
source,
resolve,
reject,
typeof options.maxRedirects === 'number' && options.maxRedirects >= 0 ? options.maxRedirects : MAX_REDIRECTS,
options.requestOptions,
),
)
return createImage(data, options.alt)
}
Expand All @@ -54,7 +60,7 @@ function makeRequest(url, resolve, reject, redirectCount, requestOptions) {
// lazy load the lib
const lib = isHttps ? (!https ? (https = require('https')) : https) : !http ? (http = require('http')) : http

lib.get(url, requestOptions ?? {}, (res) => {
lib.get(url, requestOptions || {}, (res) => {
const shouldRedirect = REDIRECT_STATUSES.has(res.statusCode) && typeof res.headers.location === 'string'
if (shouldRedirect && redirectCount > 0)
return makeRequest(new URL(res.headers.location), resolve, reject, redirectCount - 1, requestOptions)
Expand Down
Loading