Repository: facebook/react
File: packages/react-client/src/ReactFlightReplyClient.js (JavaScript)

Code before:
    isServerReference(reference) && reference.$$bound
      ? Promise.resolve(reference.$$bound)
      : null;

  registerBoundServerReference(reference, id, bound, encodeFormAction);
  return reference;
}

// $FlowFixMe[method-unbinding]
const FunctionBind = Function.prototype.bind;
// $FlowFixMe[method-unbinding]
const ArraySlice = Array.prototype.slice;

function defineBind<T: Function>(reference: T) {
  // TODO: Instead of checking if it's a server reference, we could also use
  // `reference.hasOwnProperty('bind')`.
  const originalBind = isServerReference(reference)
    ? reference.bind
    : FunctionBind;

  function bind(): Function {
    // $FlowFixMe[prop-missing]
    const newFn = originalBind.apply(reference, arguments);
    const metaData = knownServerReferences.get(reference);

    if (metaData) {
      if (__DEV__) {
        const thisBind = arguments[0];
        if (thisBind != null) {
          // This doesn't warn in browser environments since it's not
          // instrumented outside usedWithSSR. This makes this an SSR only
          // warning which we don't generally do.
          // TODO: Consider a DEV only instrumentation in the browser.
          console.error(
            'Cannot bind "this" of a Server Action. Pass null or undefined as the first argument to .bind().',
          );
        }
      }
      const args = ArraySlice.call(arguments, 1);
      let boundPromise = null;
      if (metaData.bound !== null) {
        boundPromise = Promise.resolve((metaData.bound: any)).then(boundArgs =>
          boundArgs.concat(args),
        );
      } else {
        boundPromise = Promise.resolve(args);
      }
      // Expose encoder for use by SSR, as well as a special bind that can be
      // used to keep server capabilities.
      Object.defineProperties((newFn: any), {
        $$FORM_ACTION: {value: reference.$$FORM_ACTION},

Review comment (suggestion):
`$$FORM_ACTION` and `$$IS_SIGNATURE_EQUAL` should never be included in a non-SSR build and we definitely don't want to include the extra code in browser builds sent to clients.

In fact, we really need to build a separate version of the Flight Clients for `react-server` and non-`react-server` environments because these should really never be in any `react-server` environment.

We cheat in other places like:

https://github.com/facebook/react/blob/main/packages/react-client/src/ReactFlightClient.js#L104-L113

This is going to get us in trouble eventually.

Code after:
  reference: T,
  id: ServerReferenceId,
  encodeFormAction?: EncodeFormActionCallback,
): ServerReference<T> {
  registerBoundServerReference(reference, id, null, encodeFormAction);
  return reference;
}

// $FlowFixMe[method-unbinding]
const FunctionBind = Function.prototype.bind;
// $FlowFixMe[method-unbinding]
const ArraySlice = Array.prototype.slice;
function bind(this: Function): Function {
  const referenceClosure = knownServerReferences.get(this);

  if (!referenceClosure) {
    // $FlowFixMe[prop-missing]
    return FunctionBind.apply(this, arguments);
  }

  const newFn = referenceClosure.originalBind.apply(this, arguments);

  if (__DEV__) {
    const thisBind = arguments[0];
    if (thisBind != null) {
      // This doesn't warn in browser environments since it's not instrumented outside
      // usedWithSSR. This makes this an SSR only warning which we don't generally do.
      // TODO: Consider a DEV only instrumentation in the browser.
      console.error(
        'Cannot bind "this" of a Server Action. Pass null or undefined as the first argument to .bind().',
      );
    }
  }

  const args = ArraySlice.call(arguments, 1);
  let boundPromise = null;
  if (referenceClosure.bound !== null) {
    boundPromise = Promise.resolve((referenceClosure.bound: any)).then(
      boundArgs => boundArgs.concat(args),
    );
  } else {
    boundPromise = Promise.resolve(args);
  }

  knownServerReferences.set(newFn, {
    id: referenceClosure.id,
    originalBind: newFn.bind,
    bound: boundPromise,
  });

  // Expose encoder for use by SSR, as well as a special bind that can be used to