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

fix: conditional binding (#23) #24

Closed
wants to merge 6 commits into from

Conversation

mmaietta
Copy link

Enhancement and fixes: #23 (Module did not self-register)
#23 occurs because of Linux having no export defined due to having no sources

"sources": [ ],
"conditions": [
['OS=="mac"', {
"sources": [
"permissions.mm"
],
}]

Adding conditional binding so that we can retain type-safety of the import for things like permissions.AuthType as function params with a generic import that is platform-agnostic.

import * as permissions from 'node-mac-permissions'
async requestSystemAccessPermission (accessType: permissions.AuthType): Promise<permissions.PermissionType> { }

Adding an additional test to run on a separate Linux node for this use case

Opening as DRAFT because I'm unable to actually test this on Linux, only verified through Github actions Ubuntu node and through headless docker container (via xvfb-run). Going to test on windows soon. Wanted to get a head start on any feedback though to make sure I'm not already off the rails

Mike Maietta added 2 commits March 12, 2021 18:27
…mport for things like `permissions.AuthType` as function params with a generic import that is platform-agnostic. Enhancement and fixes: codebytere#23

Adding additional test to run on a separate linux node
@mmaietta
Copy link
Author

mmaietta commented Mar 13, 2021

@codebytere it seems that xcode 12.4 changed some things and now it throws this error when attempting to build:

warning: /Applications/Xcode_12.4.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/libtool: archive library: Release/nothing.a the table of contents is empty (no object file members in the library define global symbols)
  CXX(target) Release/obj.target/permissions/permissions.o
../permissions.mm:274:8: error: duplicate case value: 'kCLAuthorizationStatusAuthorized' and 'kCLAuthorizationStatusAuthorizedAlways' both equal '3'
  case kCLAuthorizationStatusAuthorizedAlways:
       ^
../permissions.mm:273:8: note: previous case defined here
  case kCLAuthorizationStatusAuthorized:
       ^
1 error generated.
make: *** [Release/obj.target/permissions/permissions.o] Error 1

I've commented out that line since they're duplicate values and left an additional comment on the new behavior.

// Xcode 12.4+ : duplicate case value: 'kCLAuthorizationStatusAuthorized' and 'kCLAuthorizationStatusAuthorizedAlways' both equal '3'
// case kCLAuthorizationStatusAuthorizedAlways:

@codebytere
Copy link
Owner

@mmaietta if you rebase it should be good now

.github/workflows/test.yml Show resolved Hide resolved
index.js Outdated Show resolved Hide resolved
test/module.spec.js Outdated Show resolved Hide resolved
@@ -34,16 +51,7 @@ function askForFoldersAccess(folder) {
}

module.exports = {
askForCalendarAccess: permissions.askForCalendarAccess,
askForContactsAccess: permissions.askForContactsAccess,
...permissions,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we probably also need to handle calls to askForFoldersAccess and getAuthStatus - permissions.askForFoldersAccess.call(this, folder) @ L33 for example probably blows up, does it not?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These should also be covered in the additional unit test that runs on Linux 🙂

expect(permissions.getAuthStatus('contacts')).to.be.undefined
expect(permissions.askForFoldersAccess('desktop')).to.be.undefined

The type/folder is validated, and the call is still to permissions.askForFoldersAccess: () => undefined from the original stub, so it behaves as expected with the test's assert. I originally had nonMacResponse = (...args) => undefined but then ...args was just flagged as unused.

These two tests also run on all nodes to enforce consistency of the arg cross-platform.

it('should throw on invalid types', () => {
expect(() => {
permissions.getAuthStatus('bad-type')
}).to.throw(/bad-type is not a valid type/)
})

it('should throw on invalid types', () => {
expect(() => {
permissions.askForFoldersAccess('bad-type')
}).to.throw(/bad-type is not a valid protected folder/)
})

The only notable difference is that the index.d.ts does not specify the new undefined behavior. I felt that was acceptable as this is explicitly a mac-only module, this enhancement is mainly to allow type-safety with a standard import in local projects.

package.json Show resolved Hide resolved
@mmaietta mmaietta force-pushed the misc/conditional-binding branch from 321a1f8 to 92e80cd Compare March 14, 2021 16:50
…mport for things like `permissions.AuthType` as function params with a generic import that is platform-agnostic. Enhancement and fixes: codebytere#23

Adding additional test to run on a separate linux node
@mmaietta mmaietta force-pushed the misc/conditional-binding branch from 92e80cd to b1d9399 Compare March 14, 2021 17:19
mmaietta and others added 2 commits April 1, 2021 19:57
# Conflicts:
#	README.md
#	index.js
#	package.json
#	test/module.spec.js
@mmaietta mmaietta marked this pull request as ready for review June 1, 2021 16:50
@mmaietta
Copy link
Author

mmaietta commented Jun 1, 2021

Marking as ready for review.

My team and I have already been leveraging this implementation via patch-package.
We're already running with this fix in our prod release.

@codebytere codebytere deleted the branch codebytere:master November 11, 2021 18:14
@codebytere codebytere closed this Nov 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Mocha - Error: Module did not self-register
2 participants