def METHOD_NAME(
    resource_id: str, output: Any, fmt: str, offset: int, limit: Optional[int],
        options: dict[str, Any], sort: str, search_params: dict[str, Any]
):
    if fmt == u'csv':
        writer_factory = csv_writer
        records_format = u'csv'
    elif fmt == u'tsv':
        writer_factory = tsv_writer
        records_format = u'tsv'
    elif fmt == u'json':
        writer_factory = json_writer
        records_format = u'lists'
    elif fmt == u'xml':
        writer_factory = xml_writer
        records_format = u'objects'
    else:
        assert False, u'Unsupported format'
    def start_writer(fields: Any):
        bom = options.get(u'bom', False)
        return writer_factory(output, fields, resource_id, bom)
    def result_page(offs: int, lim: Optional[int]):
        return get_action(u'datastore_search')(
            {},
            dict({
                u'resource_id': resource_id,
                u'limit': PAGINATE_BY
                if lim is None else min(PAGINATE_BY, lim),
                u'offset': offs,
                u'sort': sort,
                u'records_format': records_format,
                u'include_total': False,
            }, **search_params)
        )
    result = result_page(offset, limit)
    if result[u'limit'] != limit:
        # `limit` (from PAGINATE_BY) must have been more than
        # ckan.datastore.search.rows_max, so datastore_search responded with a
        # limit matching ckan.datastore.search.rows_max. So we need to paginate
        # by that amount instead, otherwise we'll have gaps in the records.
        paginate_by = result[u'limit']
    else:
        paginate_by = PAGINATE_BY
    with start_writer(result[u'fields']) as wr:
        while True:
            if limit is not None and limit <= 0:
                break
            records = result[u'records']
            wr.write_records(records)
            if records_format == u'objects' or records_format == u'lists':
                if len(records) < paginate_by:
                    break
            elif not records:
                break
            offset += paginate_by
            if limit is not None:
                limit -= paginate_by
                if limit <= 0:
                    break
            result = result_page(offset, limit)