@didomi/consent-string

中文文档
1.24.0发布于 2026/04/09TypeScriptESM + CJS近 30 天下载 1.6万

Consent strings store user consent status as binary data, encoded in base64. Encoding user consent status into a binary string, or decoding it from an existing one, requires an encoder/decoder library.

原始使用文档
文档目录4 节
  1. Didomi Consent String
  2. Encoding
  3. Decoding
  4. Decoding logs

Consent String Encoder / Decoder

Consent strings store user consent status as binary data, encoded in base64. Encoding user consent status into a binary string, or decoding it from an existing one, requires an encoder/decoder library.

The library supports number of consent string formats defined in @didomi/consent-string-schema library.

See consent string formats description here

The Didomi Consent String format is described in the following files:

Encoding

We are using the library encodeDCS function to encode user status to DCS:

import { encodeDCS } from '@didomi/consent-string';

const consentString = encodeDCS(1, {
  userId: 'd113bb93-7aa3-4684-ab31-aa9ae45470cb',
  created: new Date('2024-01-01'),
  updated: new Date('2024-01-01'),
  vendors: {
    optin: {
      enabled: [],
      disabled: [],
    },
    optout: {
      enabled: [],
      disabled: [],
    },
  },
  purposes: {
    optin: {
      enabled: [],
      disabled: [],
    },
    optout: {
      enabled: [],
      disabled: [],
    },
  },
  sync: null,
});

The encodeDCS accepts the following parameters:

  • version - The number version of the DCS to use
  • userStatus - The user status object to encode
  • options - Options object for encoding the DCS

where optional options object provides SchemaOverrides feature

/**
 * Options for encoding a DCS
 */
export type EncodeDCSOptions = {
  /**
   * Overrides applied to the schema
   */
  schemaOverrides?: SchemaOverrides;
};

Using SchemaOverrides we can restrict the schema to use only selected encoding algorithms, for example:

const userStatus = {
  userId: 'd113bb93-7aa3-4684-ab31-aa9ae45470cb',
  created: new Date('2024-01-01'),
  updated: new Date('2024-01-01'),
  regulationId: 10,
  purposes: {
    optin: {
      enabled: [1, 100],
      disabled: [],
    },
    optout: {
      enabled: [],
      disabled: [],
    },
  },
  vendors: {
    optin: {
      enabled: [],
      disabled: [],
    },
    optout: {
      enabled: [],
      disabled: [],
    },
  },
  sync: null,
};

const consentStringWithVariantRestrictions = encodeDCS(2, userStatus, {
  schemaOverrides: {
    fields: {
      purposes_optin: {
        variants: [EncodingAlgorithm.BIT_FIELD_2_BITS],
      },
    },
  },
});

In this case consentStringWithVariantRestrictions should have BitField encoding used for purposes_optin and produced result will be the same as for DCS v1 encoding.

There is EncodingAlgorithm supported options

export enum EncodingAlgorithm {
  BIT_FIELD_2_BITS = 'bit_field_2_bits',
  RANGES_FIBONACCI = 'ranges_fibonacci',
  RANGES_U16 = 'ranges_u16',
}

Decoding

To decode DCS string we need to use decodeDCS function

import { decodeDCS } from '@didomi/consent-string';

const consentString =
  'BGHWv4UYba5-dZnABdKu__D6iWHsD6iWHsJ9iee2BAAilKiABFKVQAACIscAAAiLGAAA';
const expectedUserStatus = {
  user_id: '1875afe1-461b-6b9f-9d66-700174abbffc',
  created: '2023-04-12T18:10:00.000Z',
  updated: '2023-04-12T18:10:00.000Z',
  sync: '2023-05-24T18:10:00.000Z',
  vendors_optin: {
    enabled: [128, 129, 130, 131, 132],
    disabled: [],
  },
  purposes_optin: {
    enabled: [1, 2, 6, 7, 8],
    disabled: [3, 4],
  },
  vendors_optout: {
    enabled: [128, 129, 130, 131, 132],
    disabled: [],
  },
  purposes_optout: {
    enabled: [1, 2, 6, 7, 8],
    disabled: [3, 4],
  },
};
expect(decodeDCS(consentString)).toEqual(expectedUserStatus);

The decodeDCS accept the consentString as a parameter and returns user status object.

Decoding logs

In some cases we need to know what encoding algorithms used to produce the DCS string. The logs options provides the way to collect encoder information:

Log entry parameterTypeDescription
keystringkey of the user status encoded field
offsetnumberoffset in bits of the key in DCS
usedEncoderstringtype of encoder used

There is example of logging:

import { decodeDCS } from '@didomi/consent-string';

const consentString =
  'CGHWv4UYba5-dZnABdKu__D6iWHsD6iWHsAAKn2J57YEACKUqIAEUpVAAAIixwAACIsY';
const expectedEntries: LogEntry[] = [
  {
    key: 'version',
    offset: 0,
    usedEncoder: 'u6',
  },
  {
    key: 'user_id',
    offset: 6,
    usedEncoder: 'UUID',
  },
  {
    key: 'created',
    offset: 134,
    usedEncoder: 'date',
  },
  {
    key: 'updated',
    offset: 170,
    usedEncoder: 'date',
  },
  {
    key: 'regulation_id',
    offset: 206,
    usedEncoder: 'u16',
  },
  {
    key: 'sync',
    offset: 222,
    usedEncoder: 'date',
  },
  {
    key: 'purposes_optin',
    offset: 259,
    usedEncoder: 'bitField2Bits',
  },
  {
    key: 'purposes_optout',
    offset: 294,
    usedEncoder: 'bitField2Bits',
  },
  {
    key: 'vendors_optin',
    offset: 329,
    usedEncoder: 'rangesFibonacci',
  },
  {
    key: 'vendors_optout',
    offset: 367,
    usedEncoder: 'rangesFibonacci',
  },
];
const entries: LogEntry[] = [];
const logs: Logs = { enabled: true, entries };
decodeDCS(consentString, { logs });
expect(entries).toEqual(expectedEntries);

版本与发布记录

@didomi/consent-string 共发布 1 个版本,首次发布于 2026/04/09;过去 12 个月发布了 1 个版本。

版本发布日期距今
1.24.0latest2026/04/097 个月前

关于 @didomi/consent-string 的常见问题

@didomi/consent-string 是什么?

Consent strings store user consent status as binary data, encoded in base64. Encoding user consent status into a binary string, or decoding it from an existing one, requires an encoder/decoder library.

如何安装 @didomi/consent-string?

在项目目录运行 npm install @didomi/consent-string,或使用 pnpm add / yarn add / bun add。

@didomi/consent-string 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@didomi/consent-string 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@didomi/consent-string 可以商用吗?

@didomi/consent-string 使用 未声明 许可。package.json 没有声明许可证。没有许可证的代码默认保留全部权利,商用前请联系作者或查看仓库中的 LICENSE 文件。以上为概括,不构成法律意见,请以许可原文为准。

@didomi/consent-string 的下载量有多少?

根据 npm 官方下载统计,@didomi/consent-string 在 2026/10/12 – 2026/11/10 这 30 天内被下载 15,863 次,日均约 529 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@didomi/consent-string 还在维护吗?

最新版本 1.24.0 发布于 2026/04/09(7 个月前);至今共发布 1 个版本,其中过去 12 个月发布了 1 个;首次发布于 2026/04/09。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。