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

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

Closed
whoabuddy opened this issue Jan 30, 2024 · 3 comments
Labels
bug Unwanted or unintended logic causing harm

Comments

@whoabuddy
Copy link
Contributor

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!

@whoabuddy whoabuddy added the bug Unwanted or unintended logic causing harm label Jan 30, 2024
@github-project-automation github-project-automation bot moved this to 🆕 New in DevTools Jan 30, 2024
@janniks
Copy link
Collaborator

janniks commented Jan 30, 2024

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
Copy link
Contributor Author

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
Copy link
Contributor

Closing for now, reopen if needed.

@github-project-automation github-project-automation bot moved this from 🆕 New to ✅ Done in DevTools Feb 5, 2024
whoabuddy added a commit to aibtcdev/gated-402-api that referenced this issue Mar 4, 2024
Possible workaround for anyone using Cloudflare Workers or Hono.js, related issue: hirosystems/stacks.js#1620
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Unwanted or unintended logic causing harm
Projects
Archived in project
Development

No branches or pull requests

3 participants