@bdelab/roar-firekit

中文文档
9.17.0发布于 2026/04/10ISCTypeScriptCommonJS构建来源已验证近 30 天下载 6103

A library to facilitate Firebase authentication and Cloud Firestore interaction for ROAR apps

原始使用文档
文档目录8 节
  1. Installation
  2. Usage
  3. Constructor inputs
  4. Constructing the firekit
  5. Starting a run
  6. Writing a trial to Firestore
  7. Finishing a run
  8. Full example

npm version NPM License

roar-firekit

Welcome to roar-firekit! Roar-firekit helps you store the data from your ROAR application in Cloud Firestore.

Installation

You can install roar-firekit from npm with

npm i @bdelab/roar-firekit

Usage

Roar-firekit is agnostic about where your data comes from, but I anticipate most users will use roar-firekit with their experiments written in jsPsych.

The main entrypoint to roar-firekit's API is the [[RoarAppkit]] class. Its constructor expects an object with keys userInfo, taskInfo, and config, where userInfo is a [[UserDataInAdminDb]] object, taskInfo is a [[TaskVariantInput]] object, and config is a [[AssessmentConfigData]] object.

Constructor inputs

userInfo

User information is encapsulated in a [[UserDataInAdminDb]] object. Its only required key is id, which should be the current user's ROAR UID, which is also sometimes called the ROAR PID:

const minimalUserInfo = { id: 'roar-user-id' };

But you can supply other information about the user if you know it:

const fullUserInfo = {
  id: 'roar-user-id',
  birthMonth: 7,
  birthYear: 2014,
  classId: 'roar-class-id',
  schoolId: 'roar-school-id',
  districtId: 'roar-district-id',
  groupId: 'roar-group-id',
  userCategory: 'student',
};

taskInfo

Information about the current task is encapsulated in a [[TaskVariantInput]] object. Here is the task information for a fictitious "Not Hotdog" task:

const taskInfo = {
  taskId: 'nhd',
  taskName: 'Not Hotdog',
  variantName: 'Not Hotdog, one block',
  taskDescription: 'A demonstration task using the hot dog / not hot dog problem',
  variantDescription: 'One block, random order',
  blocks: [
    {
      blockNumber: 1,
      trialMethod: 'random-without-replacement',
      corpus: 'pointer-to-location-of-stimulus-corpus',
    },
  ],
};

config

The config object contains configuration information for your Firebase project. You may want to store your config object in separate javascript file (named "roarConfig.js" for this documentation). A template is provided below

Click to expand!
export const roarConfig = {
  firebaseConfig: {
    apiKey: 'insert your firebase API key here',
    authDomain: 'insert your firebase auth domain here',
    projectId: 'insert your firebase project ID here',
    storageBucket: 'insert your firebase storage bucket here',
    messagingSenderId: 'insert your firebase messaging sender ID here',
    appId: 'insert your firebase app ID here',
    measurementId: 'insert your firebase measurement ID here',
  },
  rootDoc: ['some collection name', 'some document name'],
};
firebaseConfig

To get the firebaseConfig fields, see this article on how to retrieve your firebase config. TLDR: go directly to your project's settings, scroll down to "SDK setup and configuration," click the "Config" radio button, and copy the snippet for your app's Firebase config object.

rootDoc

The rootDoc is an array of strings representing the document under which all ROAR data will be stored. Note that rootDoc does not have to be in the actual root of your Cloud Firestore database.

Constructing the firekit

With the above defined input, you would construct a firekit using

import { RoarAppkit } from '@bdelab/roar-firekit';
import { roarConfig } from './roarConfig.js';

// Insert input definition code from above

const firekit = new RoarAppkit({
  userInfo: minimalUserInfo,
  taskInfo,
  config: roarConfig,
});

Starting a run

Starting a run writes the user, task, and run information to Cloud Firestore:

await firekit.startRun();

If you are using roar-firekit with jsPsych, you should call this method before experiment starts, either by awaiting it before the jsPsych.run method,

await firekit.startRun();
jsPsych.run(timeline);

or by calling it as part of the on_timeline_start callback,

const procedure = {
  timeline: [trial1, trial2],
  on_timeline_start: function() {
    await firekit.startRun();
  }
}

Writing a trial to Firestore

After starting a run, you can write individual trial data to Cloud Firestore using the writeTrial method. This method can be added to individual jsPsych trials by calling it from the on_finish function, like so:

var trial = {
  type: 'image-keyboard-response',
  stimulus: 'imgA.png',
  on_finish: function (data) {
    firekit.writeTrial(data);
  },
};

Or you can call it from all trials in a jsPsych timeline by calling it from the on_data_update callback. In this case, you can avoid saving extraneous trials by conditionally calling this method based on the data. For example:

initJsPsych({
  on_data_update: function (data) {
    if (data.saveToFirestore) {
      firekit.addTrialData(data);
    }
  },
});
const timeline = [
  // A fixation trial; don't save to Firestore
  {
    type: htmlKeyboardResponse,
    stimulus: '<div style="font-size:60px;">+</div>',
    choices: 'NO_KEYS',
    trial_duration: 500,
  },
  // A stimulus and response trial; save to Firestore
  {
    type: imageKeyboardResponse,
    stimulus: 'imgA.png',
    data: { saveToFirestore: true },
  },
];

Finishing a run

After your experiment is over, you can mark it as completed in Firestore using the finishRun method. For example, you can call this method in the on_finish (experiment) callback:

initJsPsych({
  on_finish: function (data) {
    firekit.finishRun();
  },
});

Full example

The following is an example jsPsych experiment that implements the NoHotdog assessment while writing data to Cloud Firestore using roar-firekit.

Click to expand!
import { initJsPsych } from 'jspsych';
import preload from '@jspsych/plugin-preload';
import htmlKeyboardResponse from '@jspsych/plugin-html-keyboard-response';
import imageButtonResponse from '@jspsych/plugin-image-button-response';
import { RoarAppkit } from '@bdelab/roar-firekit';
import { roarConfig } from './roarConfig.js';

const taskInfo = {
  taskId: 'nhd',
  taskName: 'Not Hotdog',
  variantName: 'nhd-1block-random',
  taskDescription: 'A ROAR demonstration using the hot dog / not hot dog task.',
  variantDescription: 'One block, random order',
  blocks: [
    {
      blockNumber: 1,
      trialMethod: 'random-without-replacement',
      corpus: 'assets',
    },
  ],
};

const minimalUserInfo = { id: 'roar-user-id' };

const firekit = new RoarAppkit({
  userInfo: minimalUserInfo,
  taskInfo,
  config: roarConfig,
});

await firekit.startRun();

const jsPsych = initJsPsych({
  on_data_update: function (data) {
    if (data.saveToFirestore) {
      firekit.writeTrial(data);
    }
  },
  on_finish: function () {
    firekit.finishRun();
  },
});

// This example assumes that the hot dog / not hot dog images are stored in the
// assets folder.
const numFiles = 30;
const hotDogFiles = Array.from(Array(numFiles), (_, i) => i + 1).map(
  (idx) => new URL(`../assets/hotdog/${idx}.jpg`, import.meta.url),
);
const notHotDogFiles = Array.from(Array(numFiles), (_, i) => i + 1).map(
  (idx) => new URL(`../assets/nothotdog/${idx}.jpg`, import.meta.url),
);
const allFiles = hotDogFiles.concat(notHotDogFiles);
const allTargets = allFiles.map((url) => {
  return { target: url, isHotDog: !url.pathname.includes('nothotdog') };
});

let timeline = [];

/* preload images */
const preloadImages = {
  type: preload,
  auto_preload: true,
};
timeline.push(preloadImages);

/* define welcome message trial */
const welcome = {
  type: htmlKeyboardResponse,
  stimulus: 'Welcome to ROAR-HD, a rapid online assessment of hot dog differentiating ability. Press any key to begin.',
};
timeline.push(welcome);

const hotDogTrials = {
  timeline: [
    {
      type: htmlKeyboardResponse,
      stimulus: '<div style="font-size:60px;">+</div>',
      choices: 'NO_KEYS',
      trial_duration: 500,
    },
    {
      type: imageButtonResponse,
      stimulus: jsPsych.timelineVariable('target'),
      choices: ['Hot Dog', 'Not a Hot Dog'],
      prompt: 'Is this a hot dog?',
      data: { saveToFirestore: true },
      on_finish: function (data) {
        data.correct = jsPsych.timelineVariable('isHotDog') == data.response;
      },
    },
  ],
  timeline_variables: allTargets,
  sample: {
    type: 'without-replacement',
    size: 20,
  },
};

timeline.push(hotDogTrials);

const fixation = {
  type: htmlKeyboardResponse,
  stimulus: 'You are all done. Thanks!',
  choices: 'NO_KEYS',
};
timeline.push(fixation);

jsPsych.run(timeline);

版本与发布记录

@bdelab/roar-firekit 共发布 179 个版本,首次发布于 2022/03/09;过去 12 个月发布了 4 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
9.17.0latest2026/04/106 个月前
9.16.32026/03/177 个月前
9.15.42025/12/0411 个月前
9.15.32025/11/2711 个月前
9.15.22025/10/281 年前
9.15.02025/10/281 年前

关于 @bdelab/roar-firekit 的常见问题

@bdelab/roar-firekit 是什么?

A library to facilitate Firebase authentication and Cloud Firestore interaction for ROAR apps

如何安装 @bdelab/roar-firekit?

在项目目录运行 npm install @bdelab/roar-firekit,或使用 pnpm add / yarn add / bun add。

@bdelab/roar-firekit 支持 TypeScript 吗?

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

@bdelab/roar-firekit 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@bdelab/roar-firekit 可以商用吗?

@bdelab/roar-firekit 使用 ISC 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@bdelab/roar-firekit 的下载量有多少?

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

@bdelab/roar-firekit 还在维护吗?

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