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

TypeError: Right-hand side of 'instanceof' is not an object #20

Closed
Desttro opened this issue Sep 12, 2023 · 4 comments
Closed

TypeError: Right-hand side of 'instanceof' is not an object #20

Desttro opened this issue Sep 12, 2023 · 4 comments

Comments

@Desttro
Copy link

Desttro commented Sep 12, 2023

Hey. Hey,
I'm trying to send a test notification in my API, but I'm getting this error:

TypeError: Right-hand side of 'instanceof' is not an object
    at module.exports [as sign] (index.js:13840:62)
    at AppStoreServerAPIClient2.createBearerToken (index.js:31998:29)
    at AppStoreServerAPIClient2.makeRequest (index.js:31877:45)
    at AppStoreServerAPIClient2.requestTestNotification (index.js:31989:27)
    at index.js:56986:37
    at resolveMiddleware (index.js:55711:29)
    at callRecursive (index.js:55744:31)
    at next (index.js:55753:20)
    at index.js:56961:10
    at callRecursive (index.js:55744:31) {
  stack: TypeError: Right-hand side of 'instanceof' is not …56961:10
    at callRecursive (index.js:55744:31),
  message: Right-hand side of 'instanceof' is not an object
[mf:inf] POST /trpc/apple.sendTestNotification 200 OK (29ms)

I need to load a private key into a variable because my API server does not have a file system. And according to the types signingKey should be a string.

Here is my code (all variables are fictitious):

import {
  AppStoreServerAPIClient,
  Environment,
  SendTestNotificationResponse,
} from "@apple/app-store-server-library";

import { protectedProcedure, router } from "../trpc";

const issuerId = "37a80662-515d-11ee-be56-0242ac120002";
const keyId = "LN5NHPSU2D";
const bundleId = "com.mycompany.app";
const encodedKey = `-----BEGIN PRIVATE KEY-----
fazmQVRp7bRp3Mb5EzQcVRlOWZt7uaDo8RvgHCp7r6z7tyUyAEpN1vj7gEJHiOeY
XA3kRXAZYUQaoToDEOh00N128q9FAvz2tCu47cwXVXefhp2B6gT4v7XXMd2pBldM
T9GbWO6cXs4QVFxXQM1YGGBS9TfyMqDZTQ0gbviN7u6Yb6DcP9TGHB8QSDpMxzLy
v50IEodS
-----END PRIVATE KEY-----`;
const environment = Environment.SANDBOX;

const client = new AppStoreServerAPIClient(encodedKey, keyId, issuerId, bundleId, environment);

export const appleRouter = router({
  sendTestNotification: protectedProcedure.mutation(async () => {
    try {
      const response: SendTestNotificationResponse = await client.requestTestNotification();
      console.info(response);
    } catch (e) {
      console.error(e);
    }
  }),
});

Node.js: v18.17.1

I tried changing the formatting to:

const encodedKey = `-----BEGIN PRIVATE KEY-----fazmQVRp7bRp3Mb5EzQcVRlOWZt7uaDo8RvgHCp7r6z7tyUyAEpN1vj7gEJHiOeYXA3kRXAZYUQaoToDEOh00N128q9FAvz2tCu47cwXVXefhp2B6gT4v7XXMd2pBldMT9GbWO6cXs4QVFxXQM1YGGBS9TfyMqDZTQ0gbviN7u6Yb6DcP9TGHB8QSDpMxzLyv50IEodS-----END PRIVATE KEY-----`;

const encodedKey = `fazmQVRp7bRp3Mb5EzQcVRlOWZt7uaDo8RvgHCp7r6z7tyUyAEpN1vj7gEJHiOeYXA3kRXAZYUQaoToDEOh00N128q9FAvz2tCu47cwXVXefhp2B6gT4v7XXMd2pBldMT9GbWO6cXs4QVFxXQM1YGGBS9TfyMqDZTQ0gbviN7u6Yb6DcP9TGHB8QSDpMxzLyv50IEodS`;

None of that helped.

It could be related to auth0/node-jsonwebtoken#939

@Desttro
Copy link
Author

Desttro commented Sep 12, 2023

Cloudflare Workers not using Node.js. Maybe this will help cloudflare-worker-jwt

The Web Crypto API differs significantly from Node’s Crypto API. If you want to port JavaScript code that relies on Node’s Crypto API, you will need to adapt it to use Web Crypto primitives.
from Worker docs

@Desttro Desttro closed this as not planned Won't fix, can't repro, duplicate, stale Sep 12, 2023
@Desttro
Copy link
Author

Desttro commented Sep 13, 2023

Consider using jose instead of jsonwebtoken

@rodrigoborgesdeoliveira
Copy link
Contributor

Not sure whether or not the following would fix this issue specifically, but for anyone wanting to provide the signing/private key from a variable/constant, it would need to be concatenated with \n. Example:

-----BEGIN PRIVATE KEY-----
fazmQVRp7bRp3Mb5EzQcVRlOWZt7uaDo8RvgHCp7r6z7tyUyAEpN1vj7gEJHiOeY
XA3kRXAZYUQaoToDEOh00N128q9FAvz2tCu47cwXVXefhp2B6gT4v7XXMd2pBldM
T9GbWO6cXs4QVFxXQM1YGGBS9TfyMqDZTQ0gbviN7u6Yb6DcP9TGHB8QSDpMxzLy
v50IEodS
-----END PRIVATE KEY-----

becomes

"-----BEGIN PRIVATE KEY-----\nfazmQVRp7bRp3Mb5EzQcVRlOWZt7uaDo8RvgHCp7r6z7tyUyAEpN1vj7gEJHiOeY\nXA3kRXAZYUQaoToDEOh00N128q9FAvz2tCu47cwXVXefhp2B6gT4v7XXMd2pBldM\nT9GbWO6cXs4QVFxXQM1YGGBS9TfyMqDZTQ0gbviN7u6Yb6DcP9TGHB8QSDpMxzLy\nv50IEodS\n-----END PRIVATE KEY-----"

@voloshink
Copy link

Did anyone have any luck getting this to run in Cloudflare workers?

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

No branches or pull requests

3 participants