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

Code before:
            );
          };
    Object.defineProperties((reference: any), {
      $$FORM_ACTION: {value: $$FORM_ACTION},
      $$IS_SIGNATURE_EQUAL: {value: isSignatureEqual},
    });
    defineBind(reference);
  }
  knownServerReferences.set(reference, {id, bound});
}

// TODO: Ideally we'd use `isServerReference` from
// 'react-server/src/ReactFlightServerConfig', but that can only be imported
// in a react-server environment.
function isServerReference(reference: Object): boolean {
  return reference.$$typeof === Symbol.for('react.server.reference');
}

export function registerServerReference<T: Function>(
  reference: T,
  id: ServerReferenceId,
  encodeFormAction?: EncodeFormActionCallback,
): ServerReference<T> {
  const bound =
    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__) {

Review comment (suggestion):
We can't make these assumptions about the structure of a Server Reference here. It's up to the bundler.

In fact, these should not even be expandos. I've been meaning to move all the fields on Server/Client References onto WeakMaps since they're internal state owned by the server and should be opaque to the surrounding environment - including Flight's Client.

Code after:
  if (usedWithSSR) {
    // Only expose this in builds that would actually use it. Not needed in the browser.
    const $$FORM_ACTION =
      encodeFormAction === undefined
        ? defaultEncodeFormAction
        : function (
            this: any => Promise<any>,
            identifierPrefix: string,
          ): ReactCustomFormAction {
            return customEncodeFormAction(
              this,
              identifierPrefix,
              encodeFormAction,
            );
          };
    Object.defineProperties((reference: any), {
      $$FORM_ACTION: {value: $$FORM_ACTION},
      $$IS_SIGNATURE_EQUAL: {value: isSignatureEqual},
      bind: {value: bind},
    });
  }
}

export function registerServerReference<T: Function>(
  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.