hirosystems / stacks.js

JavaScript libraries for identity, auth, storage and transactions on the Stacks blockchain.
https://stacks.js.org
MIT License
949 stars 309 forks source link

Fetch Error: The 'referrerPolicy' field on 'RequestInitializerDict' is not implemented. #1620

Closed whoabuddy closed 8 months ago

whoabuddy commented 8 months ago

What version of Stacks.js are you using?

latest, fresh install from npm

Describe the bug

I have a Cloudflare Worker setup as an API with Hono.js, code is here

I wanted to use callReadOnlyFunction() to fetch some contract data and verify something before returning a resource, but ran into an error: "The 'referrerPolicy' field on 'RequestInitializerDict' is not implemented." (full error below)

[wrangler:inf] GET /bitcoin-face 500 Internal Server Error (16ms)
✘ [ERROR] Error: The 'referrerPolicy' field on 'RequestInitializerDict' is not implemented.
      at checkURL
  (file:///home/whoabuddy/Dev/stacks-m2m/gated-api/.wrangler/tmp/bundle-yFShja/checked-fetch.js:9:9)
      at Object.apply
  (file:///home/whoabuddy/Dev/stacks-m2m/gated-api/.wrangler/tmp/bundle-yFShja/checked-fetch.js:27:3)
      at fetchWrapper
  (file:///home/whoabuddy/Dev/stacks-m2m/gated-api/node_modules/@stacks/network/src/fetch.ts:47:29)
      at StacksMainnet.fetchFn
  (file:///home/whoabuddy/Dev/stacks-m2m/gated-api/node_modules/@stacks/network/src/fetch.ts:160:26)
      at callReadOnlyFunction
  (file:///home/whoabuddy/Dev/stacks-m2m/gated-api/node_modules/@stacks/transactions/src/builders.ts:1438:34)
      at getRecentPaymentData (file:///home/whoabuddy/Dev/stacks-m2m/gated-api/src/index.ts:46:28)
      at Array.<anonymous> (file:///home/whoabuddy/Dev/stacks-m2m/gated-api/src/index.ts:32:28)
      at Hono2.dispatch
  (file:///home/whoabuddy/Dev/stacks-m2m/gated-api/node_modules/hono/dist/hono-base.js:228:37)
      at Object.fetch
  (file:///home/whoabuddy/Dev/stacks-m2m/gated-api/node_modules/hono/dist/hono-base.js:68:19)
      at __facade_modules_fetch__
  (file:///home/whoabuddy/Dev/stacks-m2m/gated-api/.wrangler/tmp/bundle-yFShja/middleware-loader.entry.ts:45:16)

The quick fix was to switch to micro-stacks and use fetchReadOnlyFunction(), but I wanted to report it anyway in case others have run into it.

How to reproduce

This was the original code that caused an error, if more details are needed to reproduce I can push an example.

  1. Setup new project with wrangler
  2. Setup dependencies
  3. Run code below
import { Hono } from 'hono';
import { serveStatic } from 'hono/cloudflare-workers';
import { Cl, callReadOnlyFunction, cvToValue } from '@stacks/transactions';

const app = new Hono();

export interface Env {}

app.get('/', (c) => {
    return c.text('Welcome to the Gated API example using machine-payable transactions on Stacks.');
});

app.get('/favicon.ico', serveStatic({ path: 'favicon.ico' }));

app.get('/bitcoin-face', async (c) => {
    // check resource is registered / payment data exists
    const paymentData = await getRecentPaymentData();
    if (!paymentData.status) {
        // if not, return invoice
        return c.text(`Please pay first: ${paymentData}`);
    }
    // if so, return resource
    return c.text('Bitcoin Face');
});

async function getRecentPaymentData() {
    // testing with hardcoded address at first
    const address = 'ST2TY3WNDVY1ZSXCPCFYK9KDJJC2TFWYVWNBXNHD4';
    const paymentData = await callReadOnlyFunction({
        contractName: 'stacks-m2m-v1',
        contractAddress: 'ST17EAYFJ9JDJAQ7RGSE6CTGH90MQH68B3FPR7EKP',
        functionName: 'get-recent-payment-data-by-address',
        functionArgs: [Cl.stringUtf8('Bitcoin Face'), Cl.principal(address)],
        network: 'testnet',
        senderAddress: address,
    });
    console.log(JSON.stringify(paymentData));
    if (paymentData === Cl.none()) {
        return {
            status: false,
            statusText: `No payment data found for ${address}.`,
            data: null,
        };
    }
    return {
        status: true,
        statusText: `Payment data found for ${address}.`,
        data: cvToValue(paymentData),
    };
}

export default app;

Expected behavior

I expected the fetch call to succeed and return data.

Additional context

None, please let me know if more info is needed!

janniks commented 8 months ago

Might be an issue with the fetch implementation of this setup.

Can you try deleting the referrerPolicy from getFetchOptions()?

import { getFetchOptions } from "@stacks/network";

const opts = getFetchOptions();
delete opts.referrerPolicy;
whoabuddy commented 8 months ago

I bet that'll work and will give it a test - have to finish up a few things first but will report back. That's awesome it's an option I didn't think it'd be so simple to fix!

smcclellan commented 8 months ago

Closing for now, reopen if needed.